* sv.po: Update.
[official-gcc.git] / gcc / dwarf2out.c
blob2b584a58853138f89190f01a4523c97bd8d74267
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2014 Free Software Foundation, Inc.
3 Contributed by Gary Funck (gary@intrepid.com).
4 Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
5 Extensively modified by Jason Merrill (jason@cygnus.com).
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* TODO: Emit .debug_line header even when there are no functions, since
24 the file numbers are used by .debug_info. Alternately, leave
25 out locations for types and decls.
26 Avoid talking about ctors and op= for PODs.
27 Factor out common prologue sequences into multiple CIEs. */
29 /* The first part of this file deals with the DWARF 2 frame unwind
30 information, which is also used by the GCC efficient exception handling
31 mechanism. The second part, controlled only by an #ifdef
32 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
33 information. */
35 /* DWARF2 Abbreviation Glossary:
37 CFA = Canonical Frame Address
38 a fixed address on the stack which identifies a call frame.
39 We define it to be the value of SP just before the call insn.
40 The CFA register and offset, which may change during the course
41 of the function, are used to calculate its value at runtime.
43 CFI = Call Frame Instruction
44 an instruction for the DWARF2 abstract machine
46 CIE = Common Information Entry
47 information describing information common to one or more FDEs
49 DIE = Debugging Information Entry
51 FDE = Frame Description Entry
52 information describing the stack call frame, in particular,
53 how to restore registers
55 DW_CFA_... = DWARF2 CFA call frame instruction
56 DW_TAG_... = DWARF2 DIE tag */
58 #include "config.h"
59 #include "system.h"
60 #include "coretypes.h"
61 #include "tm.h"
62 #include "rtl.h"
63 #include "tree.h"
64 #include "stringpool.h"
65 #include "stor-layout.h"
66 #include "varasm.h"
67 #include "function.h"
68 #include "emit-rtl.h"
69 #include "hash-table.h"
70 #include "version.h"
71 #include "flags.h"
72 #include "hard-reg-set.h"
73 #include "regs.h"
74 #include "insn-config.h"
75 #include "reload.h"
76 #include "function.h"
77 #include "output.h"
78 #include "expr.h"
79 #include "except.h"
80 #include "dwarf2.h"
81 #include "dwarf2out.h"
82 #include "dwarf2asm.h"
83 #include "toplev.h"
84 #include "md5.h"
85 #include "tm_p.h"
86 #include "diagnostic.h"
87 #include "tree-pretty-print.h"
88 #include "debug.h"
89 #include "target.h"
90 #include "common/common-target.h"
91 #include "langhooks.h"
92 #include "cgraph.h"
93 #include "input.h"
94 #include "ira.h"
95 #include "lra.h"
96 #include "dumpfile.h"
97 #include "opts.h"
98 #include "tree-dfa.h"
99 #include "gdb/gdb-index.h"
101 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
102 static rtx last_var_location_insn;
103 static rtx cached_next_real_insn;
105 #ifdef VMS_DEBUGGING_INFO
106 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
108 /* Define this macro to be a nonzero value if the directory specifications
109 which are output in the debug info should end with a separator. */
110 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
111 /* Define this macro to evaluate to a nonzero value if GCC should refrain
112 from generating indirect strings in DWARF2 debug information, for instance
113 if your target is stuck with an old version of GDB that is unable to
114 process them properly or uses VMS Debug. */
115 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
116 #else
117 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
118 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
119 #endif
121 /* ??? Poison these here until it can be done generically. They've been
122 totally replaced in this file; make sure it stays that way. */
123 #undef DWARF2_UNWIND_INFO
124 #undef DWARF2_FRAME_INFO
125 #if (GCC_VERSION >= 3000)
126 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
127 #endif
129 /* The size of the target's pointer type. */
130 #ifndef PTR_SIZE
131 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
132 #endif
134 /* Array of RTXes referenced by the debugging information, which therefore
135 must be kept around forever. */
136 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
138 /* A pointer to the base of a list of incomplete types which might be
139 completed at some later time. incomplete_types_list needs to be a
140 vec<tree, va_gc> *because we want to tell the garbage collector about
141 it. */
142 static GTY(()) vec<tree, va_gc> *incomplete_types;
144 /* A pointer to the base of a table of references to declaration
145 scopes. This table is a display which tracks the nesting
146 of declaration scopes at the current scope and containing
147 scopes. This table is used to find the proper place to
148 define type declaration DIE's. */
149 static GTY(()) vec<tree, va_gc> *decl_scope_table;
151 /* Pointers to various DWARF2 sections. */
152 static GTY(()) section *debug_info_section;
153 static GTY(()) section *debug_skeleton_info_section;
154 static GTY(()) section *debug_abbrev_section;
155 static GTY(()) section *debug_skeleton_abbrev_section;
156 static GTY(()) section *debug_aranges_section;
157 static GTY(()) section *debug_addr_section;
158 static GTY(()) section *debug_macinfo_section;
159 static GTY(()) section *debug_line_section;
160 static GTY(()) section *debug_skeleton_line_section;
161 static GTY(()) section *debug_loc_section;
162 static GTY(()) section *debug_pubnames_section;
163 static GTY(()) section *debug_pubtypes_section;
164 static GTY(()) section *debug_str_section;
165 static GTY(()) section *debug_str_dwo_section;
166 static GTY(()) section *debug_str_offsets_section;
167 static GTY(()) section *debug_ranges_section;
168 static GTY(()) section *debug_frame_section;
170 /* Maximum size (in bytes) of an artificially generated label. */
171 #define MAX_ARTIFICIAL_LABEL_BYTES 30
173 /* According to the (draft) DWARF 3 specification, the initial length
174 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
175 bytes are 0xffffffff, followed by the length stored in the next 8
176 bytes.
178 However, the SGI/MIPS ABI uses an initial length which is equal to
179 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
181 #ifndef DWARF_INITIAL_LENGTH_SIZE
182 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
183 #endif
185 /* Round SIZE up to the nearest BOUNDARY. */
186 #define DWARF_ROUND(SIZE,BOUNDARY) \
187 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
189 /* CIE identifier. */
190 #if HOST_BITS_PER_WIDE_INT >= 64
191 #define DWARF_CIE_ID \
192 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
193 #else
194 #define DWARF_CIE_ID DW_CIE_ID
195 #endif
198 /* A vector for a table that contains frame description
199 information for each routine. */
200 #define NOT_INDEXED (-1U)
201 #define NO_INDEX_ASSIGNED (-2U)
203 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
205 struct GTY(()) indirect_string_node {
206 const char *str;
207 unsigned int refcount;
208 enum dwarf_form form;
209 char *label;
210 unsigned int index;
213 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
215 /* With split_debug_info, both the comp_dir and dwo_name go in the
216 main object file, rather than the dwo, similar to the force_direct
217 parameter elsewhere but with additional complications:
219 1) The string is needed in both the main object file and the dwo.
220 That is, the comp_dir and dwo_name will appear in both places.
222 2) Strings can use three forms: DW_FORM_string, DW_FORM_strp or
223 DW_FORM_GNU_str_index.
225 3) GCC chooses the form to use late, depending on the size and
226 reference count.
228 Rather than forcing the all debug string handling functions and
229 callers to deal with these complications, simply use a separate,
230 special-cased string table for any attribute that should go in the
231 main object file. This limits the complexity to just the places
232 that need it. */
234 static GTY ((param_is (struct indirect_string_node)))
235 htab_t skeleton_debug_str_hash;
237 static GTY(()) int dw2_string_counter;
239 /* True if the compilation unit places functions in more than one section. */
240 static GTY(()) bool have_multiple_function_sections = false;
242 /* Whether the default text and cold text sections have been used at all. */
244 static GTY(()) bool text_section_used = false;
245 static GTY(()) bool cold_text_section_used = false;
247 /* The default cold text section. */
248 static GTY(()) section *cold_text_section;
250 /* The DIE for C++1y 'auto' in a function return type. */
251 static GTY(()) dw_die_ref auto_die;
253 /* The DIE for C++1y 'decltype(auto)' in a function return type. */
254 static GTY(()) dw_die_ref decltype_auto_die;
256 /* Forward declarations for functions defined in this file. */
258 static char *stripattributes (const char *);
259 static void output_call_frame_info (int);
260 static void dwarf2out_note_section_used (void);
262 /* Personality decl of current unit. Used only when assembler does not support
263 personality CFI. */
264 static GTY(()) rtx current_unit_personality;
266 /* Data and reference forms for relocatable data. */
267 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
268 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
270 #ifndef DEBUG_FRAME_SECTION
271 #define DEBUG_FRAME_SECTION ".debug_frame"
272 #endif
274 #ifndef FUNC_BEGIN_LABEL
275 #define FUNC_BEGIN_LABEL "LFB"
276 #endif
278 #ifndef FUNC_END_LABEL
279 #define FUNC_END_LABEL "LFE"
280 #endif
282 #ifndef PROLOGUE_END_LABEL
283 #define PROLOGUE_END_LABEL "LPE"
284 #endif
286 #ifndef EPILOGUE_BEGIN_LABEL
287 #define EPILOGUE_BEGIN_LABEL "LEB"
288 #endif
290 #ifndef FRAME_BEGIN_LABEL
291 #define FRAME_BEGIN_LABEL "Lframe"
292 #endif
293 #define CIE_AFTER_SIZE_LABEL "LSCIE"
294 #define CIE_END_LABEL "LECIE"
295 #define FDE_LABEL "LSFDE"
296 #define FDE_AFTER_SIZE_LABEL "LASFDE"
297 #define FDE_END_LABEL "LEFDE"
298 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
299 #define LINE_NUMBER_END_LABEL "LELT"
300 #define LN_PROLOG_AS_LABEL "LASLTP"
301 #define LN_PROLOG_END_LABEL "LELTP"
302 #define DIE_LABEL_PREFIX "DW"
304 /* Match the base name of a file to the base name of a compilation unit. */
306 static int
307 matches_main_base (const char *path)
309 /* Cache the last query. */
310 static const char *last_path = NULL;
311 static int last_match = 0;
312 if (path != last_path)
314 const char *base;
315 int length = base_of_path (path, &base);
316 last_path = path;
317 last_match = (length == main_input_baselength
318 && memcmp (base, main_input_basename, length) == 0);
320 return last_match;
323 #ifdef DEBUG_DEBUG_STRUCT
325 static int
326 dump_struct_debug (tree type, enum debug_info_usage usage,
327 enum debug_struct_file criterion, int generic,
328 int matches, int result)
330 /* Find the type name. */
331 tree type_decl = TYPE_STUB_DECL (type);
332 tree t = type_decl;
333 const char *name = 0;
334 if (TREE_CODE (t) == TYPE_DECL)
335 t = DECL_NAME (t);
336 if (t)
337 name = IDENTIFIER_POINTER (t);
339 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
340 criterion,
341 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
342 matches ? "bas" : "hdr",
343 generic ? "gen" : "ord",
344 usage == DINFO_USAGE_DFN ? ";" :
345 usage == DINFO_USAGE_DIR_USE ? "." : "*",
346 result,
347 (void*) type_decl, name);
348 return result;
350 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
351 dump_struct_debug (type, usage, criterion, generic, matches, result)
353 #else
355 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
356 (result)
358 #endif
360 static bool
361 should_emit_struct_debug (tree type, enum debug_info_usage usage)
363 enum debug_struct_file criterion;
364 tree type_decl;
365 bool generic = lang_hooks.types.generic_p (type);
367 if (generic)
368 criterion = debug_struct_generic[usage];
369 else
370 criterion = debug_struct_ordinary[usage];
372 if (criterion == DINFO_STRUCT_FILE_NONE)
373 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
374 if (criterion == DINFO_STRUCT_FILE_ANY)
375 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
377 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
379 if (type_decl != NULL)
381 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
382 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
384 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
385 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
388 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
391 /* Return a pointer to a copy of the section string name S with all
392 attributes stripped off, and an asterisk prepended (for assemble_name). */
394 static inline char *
395 stripattributes (const char *s)
397 char *stripped = XNEWVEC (char, strlen (s) + 2);
398 char *p = stripped;
400 *p++ = '*';
402 while (*s && *s != ',')
403 *p++ = *s++;
405 *p = '\0';
406 return stripped;
409 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
410 switch to the data section instead, and write out a synthetic start label
411 for collect2 the first time around. */
413 static void
414 switch_to_eh_frame_section (bool back)
416 tree label;
418 #ifdef EH_FRAME_SECTION_NAME
419 if (eh_frame_section == 0)
421 int flags;
423 if (EH_TABLES_CAN_BE_READ_ONLY)
425 int fde_encoding;
426 int per_encoding;
427 int lsda_encoding;
429 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
430 /*global=*/0);
431 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
432 /*global=*/1);
433 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
434 /*global=*/0);
435 flags = ((! flag_pic
436 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
437 && (fde_encoding & 0x70) != DW_EH_PE_aligned
438 && (per_encoding & 0x70) != DW_EH_PE_absptr
439 && (per_encoding & 0x70) != DW_EH_PE_aligned
440 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
441 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
442 ? 0 : SECTION_WRITE);
444 else
445 flags = SECTION_WRITE;
446 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
448 #endif /* EH_FRAME_SECTION_NAME */
450 if (eh_frame_section)
451 switch_to_section (eh_frame_section);
452 else
454 /* We have no special eh_frame section. Put the information in
455 the data section and emit special labels to guide collect2. */
456 switch_to_section (data_section);
458 if (!back)
460 label = get_file_function_name ("F");
461 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
462 targetm.asm_out.globalize_label (asm_out_file,
463 IDENTIFIER_POINTER (label));
464 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
469 /* Switch [BACK] to the eh or debug frame table section, depending on
470 FOR_EH. */
472 static void
473 switch_to_frame_table_section (int for_eh, bool back)
475 if (for_eh)
476 switch_to_eh_frame_section (back);
477 else
479 if (!debug_frame_section)
480 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
481 SECTION_DEBUG, NULL);
482 switch_to_section (debug_frame_section);
486 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
488 enum dw_cfi_oprnd_type
489 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
491 switch (cfi)
493 case DW_CFA_nop:
494 case DW_CFA_GNU_window_save:
495 case DW_CFA_remember_state:
496 case DW_CFA_restore_state:
497 return dw_cfi_oprnd_unused;
499 case DW_CFA_set_loc:
500 case DW_CFA_advance_loc1:
501 case DW_CFA_advance_loc2:
502 case DW_CFA_advance_loc4:
503 case DW_CFA_MIPS_advance_loc8:
504 return dw_cfi_oprnd_addr;
506 case DW_CFA_offset:
507 case DW_CFA_offset_extended:
508 case DW_CFA_def_cfa:
509 case DW_CFA_offset_extended_sf:
510 case DW_CFA_def_cfa_sf:
511 case DW_CFA_restore:
512 case DW_CFA_restore_extended:
513 case DW_CFA_undefined:
514 case DW_CFA_same_value:
515 case DW_CFA_def_cfa_register:
516 case DW_CFA_register:
517 case DW_CFA_expression:
518 return dw_cfi_oprnd_reg_num;
520 case DW_CFA_def_cfa_offset:
521 case DW_CFA_GNU_args_size:
522 case DW_CFA_def_cfa_offset_sf:
523 return dw_cfi_oprnd_offset;
525 case DW_CFA_def_cfa_expression:
526 return dw_cfi_oprnd_loc;
528 default:
529 gcc_unreachable ();
533 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
535 enum dw_cfi_oprnd_type
536 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
538 switch (cfi)
540 case DW_CFA_def_cfa:
541 case DW_CFA_def_cfa_sf:
542 case DW_CFA_offset:
543 case DW_CFA_offset_extended_sf:
544 case DW_CFA_offset_extended:
545 return dw_cfi_oprnd_offset;
547 case DW_CFA_register:
548 return dw_cfi_oprnd_reg_num;
550 case DW_CFA_expression:
551 return dw_cfi_oprnd_loc;
553 default:
554 return dw_cfi_oprnd_unused;
558 /* Output one FDE. */
560 static void
561 output_fde (dw_fde_ref fde, bool for_eh, bool second,
562 char *section_start_label, int fde_encoding, char *augmentation,
563 bool any_lsda_needed, int lsda_encoding)
565 const char *begin, *end;
566 static unsigned int j;
567 char l1[20], l2[20];
569 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
570 /* empty */ 0);
571 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
572 for_eh + j);
573 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
574 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
575 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
576 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
577 " indicating 64-bit DWARF extension");
578 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
579 "FDE Length");
580 ASM_OUTPUT_LABEL (asm_out_file, l1);
582 if (for_eh)
583 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
584 else
585 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
586 debug_frame_section, "FDE CIE offset");
588 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
589 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
591 if (for_eh)
593 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
594 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
595 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
596 "FDE initial location");
597 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
598 end, begin, "FDE address range");
600 else
602 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
603 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
606 if (augmentation[0])
608 if (any_lsda_needed)
610 int size = size_of_encoded_value (lsda_encoding);
612 if (lsda_encoding == DW_EH_PE_aligned)
614 int offset = ( 4 /* Length */
615 + 4 /* CIE offset */
616 + 2 * size_of_encoded_value (fde_encoding)
617 + 1 /* Augmentation size */ );
618 int pad = -offset & (PTR_SIZE - 1);
620 size += pad;
621 gcc_assert (size_of_uleb128 (size) == 1);
624 dw2_asm_output_data_uleb128 (size, "Augmentation size");
626 if (fde->uses_eh_lsda)
628 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
629 fde->funcdef_number);
630 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
631 gen_rtx_SYMBOL_REF (Pmode, l1),
632 false,
633 "Language Specific Data Area");
635 else
637 if (lsda_encoding == DW_EH_PE_aligned)
638 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
639 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
640 "Language Specific Data Area (none)");
643 else
644 dw2_asm_output_data_uleb128 (0, "Augmentation size");
647 /* Loop through the Call Frame Instructions associated with this FDE. */
648 fde->dw_fde_current_label = begin;
650 size_t from, until, i;
652 from = 0;
653 until = vec_safe_length (fde->dw_fde_cfi);
655 if (fde->dw_fde_second_begin == NULL)
657 else if (!second)
658 until = fde->dw_fde_switch_cfi_index;
659 else
660 from = fde->dw_fde_switch_cfi_index;
662 for (i = from; i < until; i++)
663 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
666 /* If we are to emit a ref/link from function bodies to their frame tables,
667 do it now. This is typically performed to make sure that tables
668 associated with functions are dragged with them and not discarded in
669 garbage collecting links. We need to do this on a per function basis to
670 cope with -ffunction-sections. */
672 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
673 /* Switch to the function section, emit the ref to the tables, and
674 switch *back* into the table section. */
675 switch_to_section (function_section (fde->decl));
676 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
677 switch_to_frame_table_section (for_eh, true);
678 #endif
680 /* Pad the FDE out to an address sized boundary. */
681 ASM_OUTPUT_ALIGN (asm_out_file,
682 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
683 ASM_OUTPUT_LABEL (asm_out_file, l2);
685 j += 2;
688 /* Return true if frame description entry FDE is needed for EH. */
690 static bool
691 fde_needed_for_eh_p (dw_fde_ref fde)
693 if (flag_asynchronous_unwind_tables)
694 return true;
696 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
697 return true;
699 if (fde->uses_eh_lsda)
700 return true;
702 /* If exceptions are enabled, we have collected nothrow info. */
703 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
704 return false;
706 return true;
709 /* Output the call frame information used to record information
710 that relates to calculating the frame pointer, and records the
711 location of saved registers. */
713 static void
714 output_call_frame_info (int for_eh)
716 unsigned int i;
717 dw_fde_ref fde;
718 dw_cfi_ref cfi;
719 char l1[20], l2[20], section_start_label[20];
720 bool any_lsda_needed = false;
721 char augmentation[6];
722 int augmentation_size;
723 int fde_encoding = DW_EH_PE_absptr;
724 int per_encoding = DW_EH_PE_absptr;
725 int lsda_encoding = DW_EH_PE_absptr;
726 int return_reg;
727 rtx personality = NULL;
728 int dw_cie_version;
730 /* Don't emit a CIE if there won't be any FDEs. */
731 if (!fde_vec)
732 return;
734 /* Nothing to do if the assembler's doing it all. */
735 if (dwarf2out_do_cfi_asm ())
736 return;
738 /* If we don't have any functions we'll want to unwind out of, don't emit
739 any EH unwind information. If we make FDEs linkonce, we may have to
740 emit an empty label for an FDE that wouldn't otherwise be emitted. We
741 want to avoid having an FDE kept around when the function it refers to
742 is discarded. Example where this matters: a primary function template
743 in C++ requires EH information, an explicit specialization doesn't. */
744 if (for_eh)
746 bool any_eh_needed = false;
748 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
750 if (fde->uses_eh_lsda)
751 any_eh_needed = any_lsda_needed = true;
752 else if (fde_needed_for_eh_p (fde))
753 any_eh_needed = true;
754 else if (TARGET_USES_WEAK_UNWIND_INFO)
755 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
758 if (!any_eh_needed)
759 return;
762 /* We're going to be generating comments, so turn on app. */
763 if (flag_debug_asm)
764 app_enable ();
766 /* Switch to the proper frame section, first time. */
767 switch_to_frame_table_section (for_eh, false);
769 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
770 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
772 /* Output the CIE. */
773 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
774 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
775 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
776 dw2_asm_output_data (4, 0xffffffff,
777 "Initial length escape value indicating 64-bit DWARF extension");
778 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
779 "Length of Common Information Entry");
780 ASM_OUTPUT_LABEL (asm_out_file, l1);
782 /* Now that the CIE pointer is PC-relative for EH,
783 use 0 to identify the CIE. */
784 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
785 (for_eh ? 0 : DWARF_CIE_ID),
786 "CIE Identifier Tag");
788 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
789 use CIE version 1, unless that would produce incorrect results
790 due to overflowing the return register column. */
791 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
792 dw_cie_version = 1;
793 if (return_reg >= 256 || dwarf_version > 2)
794 dw_cie_version = 3;
795 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
797 augmentation[0] = 0;
798 augmentation_size = 0;
800 personality = current_unit_personality;
801 if (for_eh)
803 char *p;
805 /* Augmentation:
806 z Indicates that a uleb128 is present to size the
807 augmentation section.
808 L Indicates the encoding (and thus presence) of
809 an LSDA pointer in the FDE augmentation.
810 R Indicates a non-default pointer encoding for
811 FDE code pointers.
812 P Indicates the presence of an encoding + language
813 personality routine in the CIE augmentation. */
815 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
816 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
817 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
819 p = augmentation + 1;
820 if (personality)
822 *p++ = 'P';
823 augmentation_size += 1 + size_of_encoded_value (per_encoding);
824 assemble_external_libcall (personality);
826 if (any_lsda_needed)
828 *p++ = 'L';
829 augmentation_size += 1;
831 if (fde_encoding != DW_EH_PE_absptr)
833 *p++ = 'R';
834 augmentation_size += 1;
836 if (p > augmentation + 1)
838 augmentation[0] = 'z';
839 *p = '\0';
842 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
843 if (personality && per_encoding == DW_EH_PE_aligned)
845 int offset = ( 4 /* Length */
846 + 4 /* CIE Id */
847 + 1 /* CIE version */
848 + strlen (augmentation) + 1 /* Augmentation */
849 + size_of_uleb128 (1) /* Code alignment */
850 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
851 + 1 /* RA column */
852 + 1 /* Augmentation size */
853 + 1 /* Personality encoding */ );
854 int pad = -offset & (PTR_SIZE - 1);
856 augmentation_size += pad;
858 /* Augmentations should be small, so there's scarce need to
859 iterate for a solution. Die if we exceed one uleb128 byte. */
860 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
864 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
865 if (dw_cie_version >= 4)
867 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
868 dw2_asm_output_data (1, 0, "CIE Segment Size");
870 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
871 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
872 "CIE Data Alignment Factor");
874 if (dw_cie_version == 1)
875 dw2_asm_output_data (1, return_reg, "CIE RA Column");
876 else
877 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
879 if (augmentation[0])
881 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
882 if (personality)
884 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
885 eh_data_format_name (per_encoding));
886 dw2_asm_output_encoded_addr_rtx (per_encoding,
887 personality,
888 true, NULL);
891 if (any_lsda_needed)
892 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
893 eh_data_format_name (lsda_encoding));
895 if (fde_encoding != DW_EH_PE_absptr)
896 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
897 eh_data_format_name (fde_encoding));
900 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
901 output_cfi (cfi, NULL, for_eh);
903 /* Pad the CIE out to an address sized boundary. */
904 ASM_OUTPUT_ALIGN (asm_out_file,
905 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
906 ASM_OUTPUT_LABEL (asm_out_file, l2);
908 /* Loop through all of the FDE's. */
909 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
911 unsigned int k;
913 /* Don't emit EH unwind info for leaf functions that don't need it. */
914 if (for_eh && !fde_needed_for_eh_p (fde))
915 continue;
917 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
918 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
919 augmentation, any_lsda_needed, lsda_encoding);
922 if (for_eh && targetm.terminate_dw2_eh_frame_info)
923 dw2_asm_output_data (4, 0, "End of Table");
925 /* Turn off app to make assembly quicker. */
926 if (flag_debug_asm)
927 app_disable ();
930 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
932 static void
933 dwarf2out_do_cfi_startproc (bool second)
935 int enc;
936 rtx ref;
937 rtx personality = get_personality_function (current_function_decl);
939 fprintf (asm_out_file, "\t.cfi_startproc\n");
941 if (personality)
943 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
944 ref = personality;
946 /* ??? The GAS support isn't entirely consistent. We have to
947 handle indirect support ourselves, but PC-relative is done
948 in the assembler. Further, the assembler can't handle any
949 of the weirder relocation types. */
950 if (enc & DW_EH_PE_indirect)
951 ref = dw2_force_const_mem (ref, true);
953 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
954 output_addr_const (asm_out_file, ref);
955 fputc ('\n', asm_out_file);
958 if (crtl->uses_eh_lsda)
960 char lab[20];
962 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
963 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
964 current_function_funcdef_no);
965 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
966 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
968 if (enc & DW_EH_PE_indirect)
969 ref = dw2_force_const_mem (ref, true);
971 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
972 output_addr_const (asm_out_file, ref);
973 fputc ('\n', asm_out_file);
977 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
978 this allocation may be done before pass_final. */
980 dw_fde_ref
981 dwarf2out_alloc_current_fde (void)
983 dw_fde_ref fde;
985 fde = ggc_alloc_cleared_dw_fde_node ();
986 fde->decl = current_function_decl;
987 fde->funcdef_number = current_function_funcdef_no;
988 fde->fde_index = vec_safe_length (fde_vec);
989 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
990 fde->uses_eh_lsda = crtl->uses_eh_lsda;
991 fde->nothrow = crtl->nothrow;
992 fde->drap_reg = INVALID_REGNUM;
993 fde->vdrap_reg = INVALID_REGNUM;
995 /* Record the FDE associated with this function. */
996 cfun->fde = fde;
997 vec_safe_push (fde_vec, fde);
999 return fde;
1002 /* Output a marker (i.e. a label) for the beginning of a function, before
1003 the prologue. */
1005 void
1006 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1007 const char *file ATTRIBUTE_UNUSED)
1009 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1010 char * dup_label;
1011 dw_fde_ref fde;
1012 section *fnsec;
1013 bool do_frame;
1015 current_function_func_begin_label = NULL;
1017 do_frame = dwarf2out_do_frame ();
1019 /* ??? current_function_func_begin_label is also used by except.c for
1020 call-site information. We must emit this label if it might be used. */
1021 if (!do_frame
1022 && (!flag_exceptions
1023 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1024 return;
1026 fnsec = function_section (current_function_decl);
1027 switch_to_section (fnsec);
1028 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1029 current_function_funcdef_no);
1030 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1031 current_function_funcdef_no);
1032 dup_label = xstrdup (label);
1033 current_function_func_begin_label = dup_label;
1035 /* We can elide the fde allocation if we're not emitting debug info. */
1036 if (!do_frame)
1037 return;
1039 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1040 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1041 would include pass_dwarf2_frame. If we've not created the FDE yet,
1042 do so now. */
1043 fde = cfun->fde;
1044 if (fde == NULL)
1045 fde = dwarf2out_alloc_current_fde ();
1047 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1048 fde->dw_fde_begin = dup_label;
1049 fde->dw_fde_current_label = dup_label;
1050 fde->in_std_section = (fnsec == text_section
1051 || (cold_text_section && fnsec == cold_text_section));
1053 /* We only want to output line number information for the genuine dwarf2
1054 prologue case, not the eh frame case. */
1055 #ifdef DWARF2_DEBUGGING_INFO
1056 if (file)
1057 dwarf2out_source_line (line, file, 0, true);
1058 #endif
1060 if (dwarf2out_do_cfi_asm ())
1061 dwarf2out_do_cfi_startproc (false);
1062 else
1064 rtx personality = get_personality_function (current_function_decl);
1065 if (!current_unit_personality)
1066 current_unit_personality = personality;
1068 /* We cannot keep a current personality per function as without CFI
1069 asm, at the point where we emit the CFI data, there is no current
1070 function anymore. */
1071 if (personality && current_unit_personality != personality)
1072 sorry ("multiple EH personalities are supported only with assemblers "
1073 "supporting .cfi_personality directive");
1077 /* Output a marker (i.e. a label) for the end of the generated code
1078 for a function prologue. This gets called *after* the prologue code has
1079 been generated. */
1081 void
1082 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1083 const char *file ATTRIBUTE_UNUSED)
1085 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1087 /* Output a label to mark the endpoint of the code generated for this
1088 function. */
1089 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1090 current_function_funcdef_no);
1091 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1092 current_function_funcdef_no);
1093 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1096 /* Output a marker (i.e. a label) for the beginning of the generated code
1097 for a function epilogue. This gets called *before* the prologue code has
1098 been generated. */
1100 void
1101 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1102 const char *file ATTRIBUTE_UNUSED)
1104 dw_fde_ref fde = cfun->fde;
1105 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1107 if (fde->dw_fde_vms_begin_epilogue)
1108 return;
1110 /* Output a label to mark the endpoint of the code generated for this
1111 function. */
1112 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1113 current_function_funcdef_no);
1114 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1115 current_function_funcdef_no);
1116 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1119 /* Output a marker (i.e. a label) for the absolute end of the generated code
1120 for a function definition. This gets called *after* the epilogue code has
1121 been generated. */
1123 void
1124 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1125 const char *file ATTRIBUTE_UNUSED)
1127 dw_fde_ref fde;
1128 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1130 last_var_location_insn = NULL_RTX;
1131 cached_next_real_insn = NULL_RTX;
1133 if (dwarf2out_do_cfi_asm ())
1134 fprintf (asm_out_file, "\t.cfi_endproc\n");
1136 /* Output a label to mark the endpoint of the code generated for this
1137 function. */
1138 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1139 current_function_funcdef_no);
1140 ASM_OUTPUT_LABEL (asm_out_file, label);
1141 fde = cfun->fde;
1142 gcc_assert (fde != NULL);
1143 if (fde->dw_fde_second_begin == NULL)
1144 fde->dw_fde_end = xstrdup (label);
1147 void
1148 dwarf2out_frame_finish (void)
1150 /* Output call frame information. */
1151 if (targetm.debug_unwind_info () == UI_DWARF2)
1152 output_call_frame_info (0);
1154 /* Output another copy for the unwinder. */
1155 if ((flag_unwind_tables || flag_exceptions)
1156 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1157 output_call_frame_info (1);
1160 /* Note that the current function section is being used for code. */
1162 static void
1163 dwarf2out_note_section_used (void)
1165 section *sec = current_function_section ();
1166 if (sec == text_section)
1167 text_section_used = true;
1168 else if (sec == cold_text_section)
1169 cold_text_section_used = true;
1172 static void var_location_switch_text_section (void);
1173 static void set_cur_line_info_table (section *);
1175 void
1176 dwarf2out_switch_text_section (void)
1178 section *sect;
1179 dw_fde_ref fde = cfun->fde;
1181 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1183 if (!in_cold_section_p)
1185 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1186 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1187 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1189 else
1191 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1192 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1193 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1195 have_multiple_function_sections = true;
1197 /* There is no need to mark used sections when not debugging. */
1198 if (cold_text_section != NULL)
1199 dwarf2out_note_section_used ();
1201 if (dwarf2out_do_cfi_asm ())
1202 fprintf (asm_out_file, "\t.cfi_endproc\n");
1204 /* Now do the real section switch. */
1205 sect = current_function_section ();
1206 switch_to_section (sect);
1208 fde->second_in_std_section
1209 = (sect == text_section
1210 || (cold_text_section && sect == cold_text_section));
1212 if (dwarf2out_do_cfi_asm ())
1213 dwarf2out_do_cfi_startproc (true);
1215 var_location_switch_text_section ();
1217 if (cold_text_section != NULL)
1218 set_cur_line_info_table (sect);
1221 /* And now, the subset of the debugging information support code necessary
1222 for emitting location expressions. */
1224 /* Data about a single source file. */
1225 struct GTY(()) dwarf_file_data {
1226 const char * filename;
1227 int emitted_number;
1230 typedef struct GTY(()) deferred_locations_struct
1232 tree variable;
1233 dw_die_ref die;
1234 } deferred_locations;
1237 static GTY(()) vec<deferred_locations, va_gc> *deferred_locations_list;
1240 /* Describe an entry into the .debug_addr section. */
1242 enum ate_kind {
1243 ate_kind_rtx,
1244 ate_kind_rtx_dtprel,
1245 ate_kind_label
1248 typedef struct GTY(()) addr_table_entry_struct {
1249 enum ate_kind kind;
1250 unsigned int refcount;
1251 unsigned int index;
1252 union addr_table_entry_struct_union
1254 rtx GTY ((tag ("0"))) rtl;
1255 char * GTY ((tag ("1"))) label;
1257 GTY ((desc ("%1.kind"))) addr;
1259 addr_table_entry;
1261 /* Location lists are ranges + location descriptions for that range,
1262 so you can track variables that are in different places over
1263 their entire life. */
1264 typedef struct GTY(()) dw_loc_list_struct {
1265 dw_loc_list_ref dw_loc_next;
1266 const char *begin; /* Label and addr_entry for start of range */
1267 addr_table_entry *begin_entry;
1268 const char *end; /* Label for end of range */
1269 char *ll_symbol; /* Label for beginning of location list.
1270 Only on head of list */
1271 const char *section; /* Section this loclist is relative to */
1272 dw_loc_descr_ref expr;
1273 hashval_t hash;
1274 /* True if all addresses in this and subsequent lists are known to be
1275 resolved. */
1276 bool resolved_addr;
1277 /* True if this list has been replaced by dw_loc_next. */
1278 bool replaced;
1279 bool emitted;
1280 /* True if the range should be emitted even if begin and end
1281 are the same. */
1282 bool force;
1283 } dw_loc_list_node;
1285 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1287 /* Convert a DWARF stack opcode into its string name. */
1289 static const char *
1290 dwarf_stack_op_name (unsigned int op)
1292 const char *name = get_DW_OP_name (op);
1294 if (name != NULL)
1295 return name;
1297 return "OP_<unknown>";
1300 /* Return a pointer to a newly allocated location description. Location
1301 descriptions are simple expression terms that can be strung
1302 together to form more complicated location (address) descriptions. */
1304 static inline dw_loc_descr_ref
1305 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1306 unsigned HOST_WIDE_INT oprnd2)
1308 dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
1310 descr->dw_loc_opc = op;
1311 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1312 descr->dw_loc_oprnd1.val_entry = NULL;
1313 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1314 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1315 descr->dw_loc_oprnd2.val_entry = NULL;
1316 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1318 return descr;
1321 /* Return a pointer to a newly allocated location description for
1322 REG and OFFSET. */
1324 static inline dw_loc_descr_ref
1325 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1327 if (reg <= 31)
1328 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1329 offset, 0);
1330 else
1331 return new_loc_descr (DW_OP_bregx, reg, offset);
1334 /* Add a location description term to a location description expression. */
1336 static inline void
1337 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1339 dw_loc_descr_ref *d;
1341 /* Find the end of the chain. */
1342 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1345 *d = descr;
1348 /* Compare two location operands for exact equality. */
1350 static bool
1351 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1353 if (a->val_class != b->val_class)
1354 return false;
1355 switch (a->val_class)
1357 case dw_val_class_none:
1358 return true;
1359 case dw_val_class_addr:
1360 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1362 case dw_val_class_offset:
1363 case dw_val_class_unsigned_const:
1364 case dw_val_class_const:
1365 case dw_val_class_range_list:
1366 case dw_val_class_lineptr:
1367 case dw_val_class_macptr:
1368 /* These are all HOST_WIDE_INT, signed or unsigned. */
1369 return a->v.val_unsigned == b->v.val_unsigned;
1371 case dw_val_class_loc:
1372 return a->v.val_loc == b->v.val_loc;
1373 case dw_val_class_loc_list:
1374 return a->v.val_loc_list == b->v.val_loc_list;
1375 case dw_val_class_die_ref:
1376 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1377 case dw_val_class_fde_ref:
1378 return a->v.val_fde_index == b->v.val_fde_index;
1379 case dw_val_class_lbl_id:
1380 case dw_val_class_high_pc:
1381 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1382 case dw_val_class_str:
1383 return a->v.val_str == b->v.val_str;
1384 case dw_val_class_flag:
1385 return a->v.val_flag == b->v.val_flag;
1386 case dw_val_class_file:
1387 return a->v.val_file == b->v.val_file;
1388 case dw_val_class_decl_ref:
1389 return a->v.val_decl_ref == b->v.val_decl_ref;
1391 case dw_val_class_const_double:
1392 return (a->v.val_double.high == b->v.val_double.high
1393 && a->v.val_double.low == b->v.val_double.low);
1395 case dw_val_class_vec:
1397 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1398 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1400 return (a_len == b_len
1401 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1404 case dw_val_class_data8:
1405 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1407 case dw_val_class_vms_delta:
1408 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1409 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1411 gcc_unreachable ();
1414 /* Compare two location atoms for exact equality. */
1416 static bool
1417 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1419 if (a->dw_loc_opc != b->dw_loc_opc)
1420 return false;
1422 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1423 address size, but since we always allocate cleared storage it
1424 should be zero for other types of locations. */
1425 if (a->dtprel != b->dtprel)
1426 return false;
1428 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1429 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1432 /* Compare two complete location expressions for exact equality. */
1434 bool
1435 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1437 while (1)
1439 if (a == b)
1440 return true;
1441 if (a == NULL || b == NULL)
1442 return false;
1443 if (!loc_descr_equal_p_1 (a, b))
1444 return false;
1446 a = a->dw_loc_next;
1447 b = b->dw_loc_next;
1452 /* Add a constant OFFSET to a location expression. */
1454 static void
1455 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1457 dw_loc_descr_ref loc;
1458 HOST_WIDE_INT *p;
1460 gcc_assert (*list_head != NULL);
1462 if (!offset)
1463 return;
1465 /* Find the end of the chain. */
1466 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1469 p = NULL;
1470 if (loc->dw_loc_opc == DW_OP_fbreg
1471 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1472 p = &loc->dw_loc_oprnd1.v.val_int;
1473 else if (loc->dw_loc_opc == DW_OP_bregx)
1474 p = &loc->dw_loc_oprnd2.v.val_int;
1476 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1477 offset. Don't optimize if an signed integer overflow would happen. */
1478 if (p != NULL
1479 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1480 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1481 *p += offset;
1483 else if (offset > 0)
1484 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1486 else
1488 loc->dw_loc_next = int_loc_descriptor (-offset);
1489 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1493 /* Add a constant OFFSET to a location list. */
1495 static void
1496 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1498 dw_loc_list_ref d;
1499 for (d = list_head; d != NULL; d = d->dw_loc_next)
1500 loc_descr_plus_const (&d->expr, offset);
1503 #define DWARF_REF_SIZE \
1504 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1506 static unsigned long int get_base_type_offset (dw_die_ref);
1508 /* Return the size of a location descriptor. */
1510 static unsigned long
1511 size_of_loc_descr (dw_loc_descr_ref loc)
1513 unsigned long size = 1;
1515 switch (loc->dw_loc_opc)
1517 case DW_OP_addr:
1518 size += DWARF2_ADDR_SIZE;
1519 break;
1520 case DW_OP_GNU_addr_index:
1521 case DW_OP_GNU_const_index:
1522 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1523 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1524 break;
1525 case DW_OP_const1u:
1526 case DW_OP_const1s:
1527 size += 1;
1528 break;
1529 case DW_OP_const2u:
1530 case DW_OP_const2s:
1531 size += 2;
1532 break;
1533 case DW_OP_const4u:
1534 case DW_OP_const4s:
1535 size += 4;
1536 break;
1537 case DW_OP_const8u:
1538 case DW_OP_const8s:
1539 size += 8;
1540 break;
1541 case DW_OP_constu:
1542 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1543 break;
1544 case DW_OP_consts:
1545 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1546 break;
1547 case DW_OP_pick:
1548 size += 1;
1549 break;
1550 case DW_OP_plus_uconst:
1551 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1552 break;
1553 case DW_OP_skip:
1554 case DW_OP_bra:
1555 size += 2;
1556 break;
1557 case DW_OP_breg0:
1558 case DW_OP_breg1:
1559 case DW_OP_breg2:
1560 case DW_OP_breg3:
1561 case DW_OP_breg4:
1562 case DW_OP_breg5:
1563 case DW_OP_breg6:
1564 case DW_OP_breg7:
1565 case DW_OP_breg8:
1566 case DW_OP_breg9:
1567 case DW_OP_breg10:
1568 case DW_OP_breg11:
1569 case DW_OP_breg12:
1570 case DW_OP_breg13:
1571 case DW_OP_breg14:
1572 case DW_OP_breg15:
1573 case DW_OP_breg16:
1574 case DW_OP_breg17:
1575 case DW_OP_breg18:
1576 case DW_OP_breg19:
1577 case DW_OP_breg20:
1578 case DW_OP_breg21:
1579 case DW_OP_breg22:
1580 case DW_OP_breg23:
1581 case DW_OP_breg24:
1582 case DW_OP_breg25:
1583 case DW_OP_breg26:
1584 case DW_OP_breg27:
1585 case DW_OP_breg28:
1586 case DW_OP_breg29:
1587 case DW_OP_breg30:
1588 case DW_OP_breg31:
1589 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1590 break;
1591 case DW_OP_regx:
1592 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1593 break;
1594 case DW_OP_fbreg:
1595 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1596 break;
1597 case DW_OP_bregx:
1598 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1599 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1600 break;
1601 case DW_OP_piece:
1602 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1603 break;
1604 case DW_OP_bit_piece:
1605 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1606 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1607 break;
1608 case DW_OP_deref_size:
1609 case DW_OP_xderef_size:
1610 size += 1;
1611 break;
1612 case DW_OP_call2:
1613 size += 2;
1614 break;
1615 case DW_OP_call4:
1616 size += 4;
1617 break;
1618 case DW_OP_call_ref:
1619 size += DWARF_REF_SIZE;
1620 break;
1621 case DW_OP_implicit_value:
1622 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1623 + loc->dw_loc_oprnd1.v.val_unsigned;
1624 break;
1625 case DW_OP_GNU_implicit_pointer:
1626 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1627 break;
1628 case DW_OP_GNU_entry_value:
1630 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1631 size += size_of_uleb128 (op_size) + op_size;
1632 break;
1634 case DW_OP_GNU_const_type:
1636 unsigned long o
1637 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1638 size += size_of_uleb128 (o) + 1;
1639 switch (loc->dw_loc_oprnd2.val_class)
1641 case dw_val_class_vec:
1642 size += loc->dw_loc_oprnd2.v.val_vec.length
1643 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1644 break;
1645 case dw_val_class_const:
1646 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1647 break;
1648 case dw_val_class_const_double:
1649 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1650 break;
1651 default:
1652 gcc_unreachable ();
1654 break;
1656 case DW_OP_GNU_regval_type:
1658 unsigned long o
1659 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1660 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1661 + size_of_uleb128 (o);
1663 break;
1664 case DW_OP_GNU_deref_type:
1666 unsigned long o
1667 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1668 size += 1 + size_of_uleb128 (o);
1670 break;
1671 case DW_OP_GNU_convert:
1672 case DW_OP_GNU_reinterpret:
1673 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1674 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1675 else
1677 unsigned long o
1678 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1679 size += size_of_uleb128 (o);
1681 break;
1682 case DW_OP_GNU_parameter_ref:
1683 size += 4;
1684 break;
1685 default:
1686 break;
1689 return size;
1692 /* Return the size of a series of location descriptors. */
1694 unsigned long
1695 size_of_locs (dw_loc_descr_ref loc)
1697 dw_loc_descr_ref l;
1698 unsigned long size;
1700 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1701 field, to avoid writing to a PCH file. */
1702 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1704 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1705 break;
1706 size += size_of_loc_descr (l);
1708 if (! l)
1709 return size;
1711 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1713 l->dw_loc_addr = size;
1714 size += size_of_loc_descr (l);
1717 return size;
1720 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1721 static void get_ref_die_offset_label (char *, dw_die_ref);
1722 static unsigned long int get_ref_die_offset (dw_die_ref);
1724 /* Output location description stack opcode's operands (if any).
1725 The for_eh_or_skip parameter controls whether register numbers are
1726 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1727 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1728 info). This should be suppressed for the cases that have not been converted
1729 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1731 static void
1732 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1734 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1735 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1737 switch (loc->dw_loc_opc)
1739 #ifdef DWARF2_DEBUGGING_INFO
1740 case DW_OP_const2u:
1741 case DW_OP_const2s:
1742 dw2_asm_output_data (2, val1->v.val_int, NULL);
1743 break;
1744 case DW_OP_const4u:
1745 if (loc->dtprel)
1747 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1748 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1749 val1->v.val_addr);
1750 fputc ('\n', asm_out_file);
1751 break;
1753 /* FALLTHRU */
1754 case DW_OP_const4s:
1755 dw2_asm_output_data (4, val1->v.val_int, NULL);
1756 break;
1757 case DW_OP_const8u:
1758 if (loc->dtprel)
1760 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1761 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1762 val1->v.val_addr);
1763 fputc ('\n', asm_out_file);
1764 break;
1766 /* FALLTHRU */
1767 case DW_OP_const8s:
1768 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1769 dw2_asm_output_data (8, val1->v.val_int, NULL);
1770 break;
1771 case DW_OP_skip:
1772 case DW_OP_bra:
1774 int offset;
1776 gcc_assert (val1->val_class == dw_val_class_loc);
1777 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1779 dw2_asm_output_data (2, offset, NULL);
1781 break;
1782 case DW_OP_implicit_value:
1783 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1784 switch (val2->val_class)
1786 case dw_val_class_const:
1787 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1788 break;
1789 case dw_val_class_vec:
1791 unsigned int elt_size = val2->v.val_vec.elt_size;
1792 unsigned int len = val2->v.val_vec.length;
1793 unsigned int i;
1794 unsigned char *p;
1796 if (elt_size > sizeof (HOST_WIDE_INT))
1798 elt_size /= 2;
1799 len *= 2;
1801 for (i = 0, p = val2->v.val_vec.array;
1802 i < len;
1803 i++, p += elt_size)
1804 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1805 "fp or vector constant word %u", i);
1807 break;
1808 case dw_val_class_const_double:
1810 unsigned HOST_WIDE_INT first, second;
1812 if (WORDS_BIG_ENDIAN)
1814 first = val2->v.val_double.high;
1815 second = val2->v.val_double.low;
1817 else
1819 first = val2->v.val_double.low;
1820 second = val2->v.val_double.high;
1822 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1823 first, NULL);
1824 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1825 second, NULL);
1827 break;
1828 case dw_val_class_addr:
1829 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1830 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1831 break;
1832 default:
1833 gcc_unreachable ();
1835 break;
1836 #else
1837 case DW_OP_const2u:
1838 case DW_OP_const2s:
1839 case DW_OP_const4u:
1840 case DW_OP_const4s:
1841 case DW_OP_const8u:
1842 case DW_OP_const8s:
1843 case DW_OP_skip:
1844 case DW_OP_bra:
1845 case DW_OP_implicit_value:
1846 /* We currently don't make any attempt to make sure these are
1847 aligned properly like we do for the main unwind info, so
1848 don't support emitting things larger than a byte if we're
1849 only doing unwinding. */
1850 gcc_unreachable ();
1851 #endif
1852 case DW_OP_const1u:
1853 case DW_OP_const1s:
1854 dw2_asm_output_data (1, val1->v.val_int, NULL);
1855 break;
1856 case DW_OP_constu:
1857 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1858 break;
1859 case DW_OP_consts:
1860 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1861 break;
1862 case DW_OP_pick:
1863 dw2_asm_output_data (1, val1->v.val_int, NULL);
1864 break;
1865 case DW_OP_plus_uconst:
1866 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1867 break;
1868 case DW_OP_breg0:
1869 case DW_OP_breg1:
1870 case DW_OP_breg2:
1871 case DW_OP_breg3:
1872 case DW_OP_breg4:
1873 case DW_OP_breg5:
1874 case DW_OP_breg6:
1875 case DW_OP_breg7:
1876 case DW_OP_breg8:
1877 case DW_OP_breg9:
1878 case DW_OP_breg10:
1879 case DW_OP_breg11:
1880 case DW_OP_breg12:
1881 case DW_OP_breg13:
1882 case DW_OP_breg14:
1883 case DW_OP_breg15:
1884 case DW_OP_breg16:
1885 case DW_OP_breg17:
1886 case DW_OP_breg18:
1887 case DW_OP_breg19:
1888 case DW_OP_breg20:
1889 case DW_OP_breg21:
1890 case DW_OP_breg22:
1891 case DW_OP_breg23:
1892 case DW_OP_breg24:
1893 case DW_OP_breg25:
1894 case DW_OP_breg26:
1895 case DW_OP_breg27:
1896 case DW_OP_breg28:
1897 case DW_OP_breg29:
1898 case DW_OP_breg30:
1899 case DW_OP_breg31:
1900 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1901 break;
1902 case DW_OP_regx:
1904 unsigned r = val1->v.val_unsigned;
1905 if (for_eh_or_skip >= 0)
1906 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1907 gcc_assert (size_of_uleb128 (r)
1908 == size_of_uleb128 (val1->v.val_unsigned));
1909 dw2_asm_output_data_uleb128 (r, NULL);
1911 break;
1912 case DW_OP_fbreg:
1913 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1914 break;
1915 case DW_OP_bregx:
1917 unsigned r = val1->v.val_unsigned;
1918 if (for_eh_or_skip >= 0)
1919 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1920 gcc_assert (size_of_uleb128 (r)
1921 == size_of_uleb128 (val1->v.val_unsigned));
1922 dw2_asm_output_data_uleb128 (r, NULL);
1923 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1925 break;
1926 case DW_OP_piece:
1927 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1928 break;
1929 case DW_OP_bit_piece:
1930 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1931 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1932 break;
1933 case DW_OP_deref_size:
1934 case DW_OP_xderef_size:
1935 dw2_asm_output_data (1, val1->v.val_int, NULL);
1936 break;
1938 case DW_OP_addr:
1939 if (loc->dtprel)
1941 if (targetm.asm_out.output_dwarf_dtprel)
1943 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
1944 DWARF2_ADDR_SIZE,
1945 val1->v.val_addr);
1946 fputc ('\n', asm_out_file);
1948 else
1949 gcc_unreachable ();
1951 else
1953 #ifdef DWARF2_DEBUGGING_INFO
1954 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
1955 #else
1956 gcc_unreachable ();
1957 #endif
1959 break;
1961 case DW_OP_GNU_addr_index:
1962 case DW_OP_GNU_const_index:
1963 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1964 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
1965 "(index into .debug_addr)");
1966 break;
1968 case DW_OP_GNU_implicit_pointer:
1970 char label[MAX_ARTIFICIAL_LABEL_BYTES
1971 + HOST_BITS_PER_WIDE_INT / 2 + 2];
1972 gcc_assert (val1->val_class == dw_val_class_die_ref);
1973 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
1974 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
1975 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1977 break;
1979 case DW_OP_GNU_entry_value:
1980 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
1981 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
1982 break;
1984 case DW_OP_GNU_const_type:
1986 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
1987 gcc_assert (o);
1988 dw2_asm_output_data_uleb128 (o, NULL);
1989 switch (val2->val_class)
1991 case dw_val_class_const:
1992 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
1993 dw2_asm_output_data (1, l, NULL);
1994 dw2_asm_output_data (l, val2->v.val_int, NULL);
1995 break;
1996 case dw_val_class_vec:
1998 unsigned int elt_size = val2->v.val_vec.elt_size;
1999 unsigned int len = val2->v.val_vec.length;
2000 unsigned int i;
2001 unsigned char *p;
2003 l = len * elt_size;
2004 dw2_asm_output_data (1, l, NULL);
2005 if (elt_size > sizeof (HOST_WIDE_INT))
2007 elt_size /= 2;
2008 len *= 2;
2010 for (i = 0, p = val2->v.val_vec.array;
2011 i < len;
2012 i++, p += elt_size)
2013 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2014 "fp or vector constant word %u", i);
2016 break;
2017 case dw_val_class_const_double:
2019 unsigned HOST_WIDE_INT first, second;
2020 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2022 dw2_asm_output_data (1, 2 * l, NULL);
2023 if (WORDS_BIG_ENDIAN)
2025 first = val2->v.val_double.high;
2026 second = val2->v.val_double.low;
2028 else
2030 first = val2->v.val_double.low;
2031 second = val2->v.val_double.high;
2033 dw2_asm_output_data (l, first, NULL);
2034 dw2_asm_output_data (l, second, NULL);
2036 break;
2037 default:
2038 gcc_unreachable ();
2041 break;
2042 case DW_OP_GNU_regval_type:
2044 unsigned r = val1->v.val_unsigned;
2045 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2046 gcc_assert (o);
2047 if (for_eh_or_skip >= 0)
2049 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2050 gcc_assert (size_of_uleb128 (r)
2051 == size_of_uleb128 (val1->v.val_unsigned));
2053 dw2_asm_output_data_uleb128 (r, NULL);
2054 dw2_asm_output_data_uleb128 (o, NULL);
2056 break;
2057 case DW_OP_GNU_deref_type:
2059 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2060 gcc_assert (o);
2061 dw2_asm_output_data (1, val1->v.val_int, NULL);
2062 dw2_asm_output_data_uleb128 (o, NULL);
2064 break;
2065 case DW_OP_GNU_convert:
2066 case DW_OP_GNU_reinterpret:
2067 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2068 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2069 else
2071 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2072 gcc_assert (o);
2073 dw2_asm_output_data_uleb128 (o, NULL);
2075 break;
2077 case DW_OP_GNU_parameter_ref:
2079 unsigned long o;
2080 gcc_assert (val1->val_class == dw_val_class_die_ref);
2081 o = get_ref_die_offset (val1->v.val_die_ref.die);
2082 dw2_asm_output_data (4, o, NULL);
2084 break;
2086 default:
2087 /* Other codes have no operands. */
2088 break;
2092 /* Output a sequence of location operations.
2093 The for_eh_or_skip parameter controls whether register numbers are
2094 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2095 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2096 info). This should be suppressed for the cases that have not been converted
2097 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2099 void
2100 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2102 for (; loc != NULL; loc = loc->dw_loc_next)
2104 enum dwarf_location_atom opc = loc->dw_loc_opc;
2105 /* Output the opcode. */
2106 if (for_eh_or_skip >= 0
2107 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2109 unsigned r = (opc - DW_OP_breg0);
2110 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2111 gcc_assert (r <= 31);
2112 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2114 else if (for_eh_or_skip >= 0
2115 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2117 unsigned r = (opc - DW_OP_reg0);
2118 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2119 gcc_assert (r <= 31);
2120 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2123 dw2_asm_output_data (1, opc,
2124 "%s", dwarf_stack_op_name (opc));
2126 /* Output the operand(s) (if any). */
2127 output_loc_operands (loc, for_eh_or_skip);
2131 /* Output location description stack opcode's operands (if any).
2132 The output is single bytes on a line, suitable for .cfi_escape. */
2134 static void
2135 output_loc_operands_raw (dw_loc_descr_ref loc)
2137 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2138 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2140 switch (loc->dw_loc_opc)
2142 case DW_OP_addr:
2143 case DW_OP_GNU_addr_index:
2144 case DW_OP_GNU_const_index:
2145 case DW_OP_implicit_value:
2146 /* We cannot output addresses in .cfi_escape, only bytes. */
2147 gcc_unreachable ();
2149 case DW_OP_const1u:
2150 case DW_OP_const1s:
2151 case DW_OP_pick:
2152 case DW_OP_deref_size:
2153 case DW_OP_xderef_size:
2154 fputc (',', asm_out_file);
2155 dw2_asm_output_data_raw (1, val1->v.val_int);
2156 break;
2158 case DW_OP_const2u:
2159 case DW_OP_const2s:
2160 fputc (',', asm_out_file);
2161 dw2_asm_output_data_raw (2, val1->v.val_int);
2162 break;
2164 case DW_OP_const4u:
2165 case DW_OP_const4s:
2166 fputc (',', asm_out_file);
2167 dw2_asm_output_data_raw (4, val1->v.val_int);
2168 break;
2170 case DW_OP_const8u:
2171 case DW_OP_const8s:
2172 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2173 fputc (',', asm_out_file);
2174 dw2_asm_output_data_raw (8, val1->v.val_int);
2175 break;
2177 case DW_OP_skip:
2178 case DW_OP_bra:
2180 int offset;
2182 gcc_assert (val1->val_class == dw_val_class_loc);
2183 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2185 fputc (',', asm_out_file);
2186 dw2_asm_output_data_raw (2, offset);
2188 break;
2190 case DW_OP_regx:
2192 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2193 gcc_assert (size_of_uleb128 (r)
2194 == size_of_uleb128 (val1->v.val_unsigned));
2195 fputc (',', asm_out_file);
2196 dw2_asm_output_data_uleb128_raw (r);
2198 break;
2200 case DW_OP_constu:
2201 case DW_OP_plus_uconst:
2202 case DW_OP_piece:
2203 fputc (',', asm_out_file);
2204 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2205 break;
2207 case DW_OP_bit_piece:
2208 fputc (',', asm_out_file);
2209 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2210 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2211 break;
2213 case DW_OP_consts:
2214 case DW_OP_breg0:
2215 case DW_OP_breg1:
2216 case DW_OP_breg2:
2217 case DW_OP_breg3:
2218 case DW_OP_breg4:
2219 case DW_OP_breg5:
2220 case DW_OP_breg6:
2221 case DW_OP_breg7:
2222 case DW_OP_breg8:
2223 case DW_OP_breg9:
2224 case DW_OP_breg10:
2225 case DW_OP_breg11:
2226 case DW_OP_breg12:
2227 case DW_OP_breg13:
2228 case DW_OP_breg14:
2229 case DW_OP_breg15:
2230 case DW_OP_breg16:
2231 case DW_OP_breg17:
2232 case DW_OP_breg18:
2233 case DW_OP_breg19:
2234 case DW_OP_breg20:
2235 case DW_OP_breg21:
2236 case DW_OP_breg22:
2237 case DW_OP_breg23:
2238 case DW_OP_breg24:
2239 case DW_OP_breg25:
2240 case DW_OP_breg26:
2241 case DW_OP_breg27:
2242 case DW_OP_breg28:
2243 case DW_OP_breg29:
2244 case DW_OP_breg30:
2245 case DW_OP_breg31:
2246 case DW_OP_fbreg:
2247 fputc (',', asm_out_file);
2248 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2249 break;
2251 case DW_OP_bregx:
2253 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2254 gcc_assert (size_of_uleb128 (r)
2255 == size_of_uleb128 (val1->v.val_unsigned));
2256 fputc (',', asm_out_file);
2257 dw2_asm_output_data_uleb128_raw (r);
2258 fputc (',', asm_out_file);
2259 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2261 break;
2263 case DW_OP_GNU_implicit_pointer:
2264 case DW_OP_GNU_entry_value:
2265 case DW_OP_GNU_const_type:
2266 case DW_OP_GNU_regval_type:
2267 case DW_OP_GNU_deref_type:
2268 case DW_OP_GNU_convert:
2269 case DW_OP_GNU_reinterpret:
2270 case DW_OP_GNU_parameter_ref:
2271 gcc_unreachable ();
2272 break;
2274 default:
2275 /* Other codes have no operands. */
2276 break;
2280 void
2281 output_loc_sequence_raw (dw_loc_descr_ref loc)
2283 while (1)
2285 enum dwarf_location_atom opc = loc->dw_loc_opc;
2286 /* Output the opcode. */
2287 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2289 unsigned r = (opc - DW_OP_breg0);
2290 r = DWARF2_FRAME_REG_OUT (r, 1);
2291 gcc_assert (r <= 31);
2292 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2294 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2296 unsigned r = (opc - DW_OP_reg0);
2297 r = DWARF2_FRAME_REG_OUT (r, 1);
2298 gcc_assert (r <= 31);
2299 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2301 /* Output the opcode. */
2302 fprintf (asm_out_file, "%#x", opc);
2303 output_loc_operands_raw (loc);
2305 if (!loc->dw_loc_next)
2306 break;
2307 loc = loc->dw_loc_next;
2309 fputc (',', asm_out_file);
2313 /* This function builds a dwarf location descriptor sequence from a
2314 dw_cfa_location, adding the given OFFSET to the result of the
2315 expression. */
2317 struct dw_loc_descr_node *
2318 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2320 struct dw_loc_descr_node *head, *tmp;
2322 offset += cfa->offset;
2324 if (cfa->indirect)
2326 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2327 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2328 head->dw_loc_oprnd1.val_entry = NULL;
2329 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2330 add_loc_descr (&head, tmp);
2331 if (offset != 0)
2333 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2334 add_loc_descr (&head, tmp);
2337 else
2338 head = new_reg_loc_descr (cfa->reg, offset);
2340 return head;
2343 /* This function builds a dwarf location descriptor sequence for
2344 the address at OFFSET from the CFA when stack is aligned to
2345 ALIGNMENT byte. */
2347 struct dw_loc_descr_node *
2348 build_cfa_aligned_loc (dw_cfa_location *cfa,
2349 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2351 struct dw_loc_descr_node *head;
2352 unsigned int dwarf_fp
2353 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2355 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2356 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2358 head = new_reg_loc_descr (dwarf_fp, 0);
2359 add_loc_descr (&head, int_loc_descriptor (alignment));
2360 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2361 loc_descr_plus_const (&head, offset);
2363 else
2364 head = new_reg_loc_descr (dwarf_fp, offset);
2365 return head;
2368 /* And now, the support for symbolic debugging information. */
2370 /* .debug_str support. */
2371 static int output_indirect_string (void **, void *);
2373 static void dwarf2out_init (const char *);
2374 static void dwarf2out_finish (const char *);
2375 static void dwarf2out_assembly_start (void);
2376 static void dwarf2out_define (unsigned int, const char *);
2377 static void dwarf2out_undef (unsigned int, const char *);
2378 static void dwarf2out_start_source_file (unsigned, const char *);
2379 static void dwarf2out_end_source_file (unsigned);
2380 static void dwarf2out_function_decl (tree);
2381 static void dwarf2out_begin_block (unsigned, unsigned);
2382 static void dwarf2out_end_block (unsigned, unsigned);
2383 static bool dwarf2out_ignore_block (const_tree);
2384 static void dwarf2out_global_decl (tree);
2385 static void dwarf2out_type_decl (tree, int);
2386 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2387 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2388 dw_die_ref);
2389 static void dwarf2out_abstract_function (tree);
2390 static void dwarf2out_var_location (rtx);
2391 static void dwarf2out_begin_function (tree);
2392 static void dwarf2out_end_function (unsigned int);
2393 static void dwarf2out_set_name (tree, tree);
2395 /* The debug hooks structure. */
2397 const struct gcc_debug_hooks dwarf2_debug_hooks =
2399 dwarf2out_init,
2400 dwarf2out_finish,
2401 dwarf2out_assembly_start,
2402 dwarf2out_define,
2403 dwarf2out_undef,
2404 dwarf2out_start_source_file,
2405 dwarf2out_end_source_file,
2406 dwarf2out_begin_block,
2407 dwarf2out_end_block,
2408 dwarf2out_ignore_block,
2409 dwarf2out_source_line,
2410 dwarf2out_begin_prologue,
2411 #if VMS_DEBUGGING_INFO
2412 dwarf2out_vms_end_prologue,
2413 dwarf2out_vms_begin_epilogue,
2414 #else
2415 debug_nothing_int_charstar,
2416 debug_nothing_int_charstar,
2417 #endif
2418 dwarf2out_end_epilogue,
2419 dwarf2out_begin_function,
2420 dwarf2out_end_function, /* end_function */
2421 dwarf2out_function_decl, /* function_decl */
2422 dwarf2out_global_decl,
2423 dwarf2out_type_decl, /* type_decl */
2424 dwarf2out_imported_module_or_decl,
2425 debug_nothing_tree, /* deferred_inline_function */
2426 /* The DWARF 2 backend tries to reduce debugging bloat by not
2427 emitting the abstract description of inline functions until
2428 something tries to reference them. */
2429 dwarf2out_abstract_function, /* outlining_inline_function */
2430 debug_nothing_rtx, /* label */
2431 debug_nothing_int, /* handle_pch */
2432 dwarf2out_var_location,
2433 dwarf2out_switch_text_section,
2434 dwarf2out_set_name,
2435 1, /* start_end_main_source_file */
2436 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2439 /* NOTE: In the comments in this file, many references are made to
2440 "Debugging Information Entries". This term is abbreviated as `DIE'
2441 throughout the remainder of this file. */
2443 /* An internal representation of the DWARF output is built, and then
2444 walked to generate the DWARF debugging info. The walk of the internal
2445 representation is done after the entire program has been compiled.
2446 The types below are used to describe the internal representation. */
2448 /* Whether to put type DIEs into their own section .debug_types instead
2449 of making them part of the .debug_info section. Only supported for
2450 Dwarf V4 or higher and the user didn't disable them through
2451 -fno-debug-types-section. It is more efficient to put them in a
2452 separate comdat sections since the linker will then be able to
2453 remove duplicates. But not all tools support .debug_types sections
2454 yet. */
2456 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2458 /* Various DIE's use offsets relative to the beginning of the
2459 .debug_info section to refer to each other. */
2461 typedef long int dw_offset;
2463 /* Define typedefs here to avoid circular dependencies. */
2465 typedef struct dw_attr_struct *dw_attr_ref;
2466 typedef struct dw_line_info_struct *dw_line_info_ref;
2467 typedef struct pubname_struct *pubname_ref;
2468 typedef struct dw_ranges_struct *dw_ranges_ref;
2469 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
2470 typedef struct comdat_type_struct *comdat_type_node_ref;
2472 /* The entries in the line_info table more-or-less mirror the opcodes
2473 that are used in the real dwarf line table. Arrays of these entries
2474 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2475 supported. */
2477 enum dw_line_info_opcode {
2478 /* Emit DW_LNE_set_address; the operand is the label index. */
2479 LI_set_address,
2481 /* Emit a row to the matrix with the given line. This may be done
2482 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2483 special opcodes. */
2484 LI_set_line,
2486 /* Emit a DW_LNS_set_file. */
2487 LI_set_file,
2489 /* Emit a DW_LNS_set_column. */
2490 LI_set_column,
2492 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2493 LI_negate_stmt,
2495 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2496 LI_set_prologue_end,
2497 LI_set_epilogue_begin,
2499 /* Emit a DW_LNE_set_discriminator. */
2500 LI_set_discriminator
2503 typedef struct GTY(()) dw_line_info_struct {
2504 enum dw_line_info_opcode opcode;
2505 unsigned int val;
2506 } dw_line_info_entry;
2509 typedef struct GTY(()) dw_line_info_table_struct {
2510 /* The label that marks the end of this section. */
2511 const char *end_label;
2513 /* The values for the last row of the matrix, as collected in the table.
2514 These are used to minimize the changes to the next row. */
2515 unsigned int file_num;
2516 unsigned int line_num;
2517 unsigned int column_num;
2518 int discrim_num;
2519 bool is_stmt;
2520 bool in_use;
2522 vec<dw_line_info_entry, va_gc> *entries;
2523 } dw_line_info_table;
2525 typedef dw_line_info_table *dw_line_info_table_p;
2528 /* Each DIE attribute has a field specifying the attribute kind,
2529 a link to the next attribute in the chain, and an attribute value.
2530 Attributes are typically linked below the DIE they modify. */
2532 typedef struct GTY(()) dw_attr_struct {
2533 enum dwarf_attribute dw_attr;
2534 dw_val_node dw_attr_val;
2536 dw_attr_node;
2539 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2540 The children of each node form a circular list linked by
2541 die_sib. die_child points to the node *before* the "first" child node. */
2543 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
2544 union die_symbol_or_type_node
2546 const char * GTY ((tag ("0"))) die_symbol;
2547 comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
2549 GTY ((desc ("%0.comdat_type_p"))) die_id;
2550 vec<dw_attr_node, va_gc> *die_attr;
2551 dw_die_ref die_parent;
2552 dw_die_ref die_child;
2553 dw_die_ref die_sib;
2554 dw_die_ref die_definition; /* ref from a specification to its definition */
2555 dw_offset die_offset;
2556 unsigned long die_abbrev;
2557 int die_mark;
2558 unsigned int decl_id;
2559 enum dwarf_tag die_tag;
2560 /* Die is used and must not be pruned as unused. */
2561 BOOL_BITFIELD die_perennial_p : 1;
2562 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2563 /* Lots of spare bits. */
2565 die_node;
2567 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2568 #define FOR_EACH_CHILD(die, c, expr) do { \
2569 c = die->die_child; \
2570 if (c) do { \
2571 c = c->die_sib; \
2572 expr; \
2573 } while (c != die->die_child); \
2574 } while (0)
2576 /* The pubname structure */
2578 typedef struct GTY(()) pubname_struct {
2579 dw_die_ref die;
2580 const char *name;
2582 pubname_entry;
2585 struct GTY(()) dw_ranges_struct {
2586 /* If this is positive, it's a block number, otherwise it's a
2587 bitwise-negated index into dw_ranges_by_label. */
2588 int num;
2591 /* A structure to hold a macinfo entry. */
2593 typedef struct GTY(()) macinfo_struct {
2594 unsigned char code;
2595 unsigned HOST_WIDE_INT lineno;
2596 const char *info;
2598 macinfo_entry;
2601 struct GTY(()) dw_ranges_by_label_struct {
2602 const char *begin;
2603 const char *end;
2606 /* The comdat type node structure. */
2607 typedef struct GTY(()) comdat_type_struct
2609 dw_die_ref root_die;
2610 dw_die_ref type_die;
2611 dw_die_ref skeleton_die;
2612 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2613 struct comdat_type_struct *next;
2615 comdat_type_node;
2617 /* The limbo die list structure. */
2618 typedef struct GTY(()) limbo_die_struct {
2619 dw_die_ref die;
2620 tree created_for;
2621 struct limbo_die_struct *next;
2623 limbo_die_node;
2625 typedef struct skeleton_chain_struct
2627 dw_die_ref old_die;
2628 dw_die_ref new_die;
2629 struct skeleton_chain_struct *parent;
2631 skeleton_chain_node;
2633 /* Define a macro which returns nonzero for a TYPE_DECL which was
2634 implicitly generated for a type.
2636 Note that, unlike the C front-end (which generates a NULL named
2637 TYPE_DECL node for each complete tagged type, each array type,
2638 and each function type node created) the C++ front-end generates
2639 a _named_ TYPE_DECL node for each tagged type node created.
2640 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2641 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2642 front-end, but for each type, tagged or not. */
2644 #define TYPE_DECL_IS_STUB(decl) \
2645 (DECL_NAME (decl) == NULL_TREE \
2646 || (DECL_ARTIFICIAL (decl) \
2647 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
2648 /* This is necessary for stub decls that \
2649 appear in nested inline functions. */ \
2650 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2651 && (decl_ultimate_origin (decl) \
2652 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2654 /* Information concerning the compilation unit's programming
2655 language, and compiler version. */
2657 /* Fixed size portion of the DWARF compilation unit header. */
2658 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2659 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2661 /* Fixed size portion of the DWARF comdat type unit header. */
2662 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2663 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2664 + DWARF_OFFSET_SIZE)
2666 /* Fixed size portion of public names info. */
2667 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2669 /* Fixed size portion of the address range info. */
2670 #define DWARF_ARANGES_HEADER_SIZE \
2671 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2672 DWARF2_ADDR_SIZE * 2) \
2673 - DWARF_INITIAL_LENGTH_SIZE)
2675 /* Size of padding portion in the address range info. It must be
2676 aligned to twice the pointer size. */
2677 #define DWARF_ARANGES_PAD_SIZE \
2678 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2679 DWARF2_ADDR_SIZE * 2) \
2680 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2682 /* Use assembler line directives if available. */
2683 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2684 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2685 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2686 #else
2687 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2688 #endif
2689 #endif
2691 /* Minimum line offset in a special line info. opcode.
2692 This value was chosen to give a reasonable range of values. */
2693 #define DWARF_LINE_BASE -10
2695 /* First special line opcode - leave room for the standard opcodes. */
2696 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
2698 /* Range of line offsets in a special line info. opcode. */
2699 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
2701 /* Flag that indicates the initial value of the is_stmt_start flag.
2702 In the present implementation, we do not mark any lines as
2703 the beginning of a source statement, because that information
2704 is not made available by the GCC front-end. */
2705 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2707 /* Maximum number of operations per instruction bundle. */
2708 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2709 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2710 #endif
2712 /* This location is used by calc_die_sizes() to keep track
2713 the offset of each DIE within the .debug_info section. */
2714 static unsigned long next_die_offset;
2716 /* Record the root of the DIE's built for the current compilation unit. */
2717 static GTY(()) dw_die_ref single_comp_unit_die;
2719 /* A list of type DIEs that have been separated into comdat sections. */
2720 static GTY(()) comdat_type_node *comdat_type_list;
2722 /* A list of DIEs with a NULL parent waiting to be relocated. */
2723 static GTY(()) limbo_die_node *limbo_die_list;
2725 /* A list of DIEs for which we may have to generate
2726 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
2727 static GTY(()) limbo_die_node *deferred_asm_name;
2729 /* Filenames referenced by this compilation unit. */
2730 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
2732 /* A hash table of references to DIE's that describe declarations.
2733 The key is a DECL_UID() which is a unique number identifying each decl. */
2734 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
2736 /* A hash table of references to DIE's that describe COMMON blocks.
2737 The key is DECL_UID() ^ die_parent. */
2738 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
2740 typedef struct GTY(()) die_arg_entry_struct {
2741 dw_die_ref die;
2742 tree arg;
2743 } die_arg_entry;
2746 /* Node of the variable location list. */
2747 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2748 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2749 EXPR_LIST chain. For small bitsizes, bitsize is encoded
2750 in mode of the EXPR_LIST node and first EXPR_LIST operand
2751 is either NOTE_INSN_VAR_LOCATION for a piece with a known
2752 location or NULL for padding. For larger bitsizes,
2753 mode is 0 and first operand is a CONCAT with bitsize
2754 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2755 NULL as second operand. */
2756 rtx GTY (()) loc;
2757 const char * GTY (()) label;
2758 struct var_loc_node * GTY (()) next;
2761 /* Variable location list. */
2762 struct GTY (()) var_loc_list_def {
2763 struct var_loc_node * GTY (()) first;
2765 /* Pointer to the last but one or last element of the
2766 chained list. If the list is empty, both first and
2767 last are NULL, if the list contains just one node
2768 or the last node certainly is not redundant, it points
2769 to the last node, otherwise points to the last but one.
2770 Do not mark it for GC because it is marked through the chain. */
2771 struct var_loc_node * GTY ((skip ("%h"))) last;
2773 /* Pointer to the last element before section switch,
2774 if NULL, either sections weren't switched or first
2775 is after section switch. */
2776 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2778 /* DECL_UID of the variable decl. */
2779 unsigned int decl_id;
2781 typedef struct var_loc_list_def var_loc_list;
2783 /* Call argument location list. */
2784 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2785 rtx GTY (()) call_arg_loc_note;
2786 const char * GTY (()) label;
2787 tree GTY (()) block;
2788 bool tail_call_p;
2789 rtx GTY (()) symbol_ref;
2790 struct call_arg_loc_node * GTY (()) next;
2794 /* Table of decl location linked lists. */
2795 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
2797 /* Head and tail of call_arg_loc chain. */
2798 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2799 static struct call_arg_loc_node *call_arg_loc_last;
2801 /* Number of call sites in the current function. */
2802 static int call_site_count = -1;
2803 /* Number of tail call sites in the current function. */
2804 static int tail_call_site_count = -1;
2806 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
2807 DIEs. */
2808 static vec<dw_die_ref> block_map;
2810 /* A cached location list. */
2811 struct GTY (()) cached_dw_loc_list_def {
2812 /* The DECL_UID of the decl that this entry describes. */
2813 unsigned int decl_id;
2815 /* The cached location list. */
2816 dw_loc_list_ref loc_list;
2818 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2820 /* Table of cached location lists. */
2821 static GTY ((param_is (cached_dw_loc_list))) htab_t cached_dw_loc_list_table;
2823 /* A pointer to the base of a list of references to DIE's that
2824 are uniquely identified by their tag, presence/absence of
2825 children DIE's, and list of attribute/value pairs. */
2826 static GTY((length ("abbrev_die_table_allocated")))
2827 dw_die_ref *abbrev_die_table;
2829 /* Number of elements currently allocated for abbrev_die_table. */
2830 static GTY(()) unsigned abbrev_die_table_allocated;
2832 /* Number of elements in type_die_table currently in use. */
2833 static GTY(()) unsigned abbrev_die_table_in_use;
2835 /* Size (in elements) of increments by which we may expand the
2836 abbrev_die_table. */
2837 #define ABBREV_DIE_TABLE_INCREMENT 256
2839 /* A global counter for generating labels for line number data. */
2840 static unsigned int line_info_label_num;
2842 /* The current table to which we should emit line number information
2843 for the current function. This will be set up at the beginning of
2844 assembly for the function. */
2845 static dw_line_info_table *cur_line_info_table;
2847 /* The two default tables of line number info. */
2848 static GTY(()) dw_line_info_table *text_section_line_info;
2849 static GTY(()) dw_line_info_table *cold_text_section_line_info;
2851 /* The set of all non-default tables of line number info. */
2852 static GTY(()) vec<dw_line_info_table_p, va_gc> *separate_line_info;
2854 /* A flag to tell pubnames/types export if there is an info section to
2855 refer to. */
2856 static bool info_section_emitted;
2858 /* A pointer to the base of a table that contains a list of publicly
2859 accessible names. */
2860 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
2862 /* A pointer to the base of a table that contains a list of publicly
2863 accessible types. */
2864 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
2866 /* A pointer to the base of a table that contains a list of macro
2867 defines/undefines (and file start/end markers). */
2868 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
2870 /* True if .debug_macinfo or .debug_macros section is going to be
2871 emitted. */
2872 #define have_macinfo \
2873 (debug_info_level >= DINFO_LEVEL_VERBOSE \
2874 && !macinfo_table->is_empty ())
2876 /* Array of dies for which we should generate .debug_ranges info. */
2877 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
2879 /* Number of elements currently allocated for ranges_table. */
2880 static GTY(()) unsigned ranges_table_allocated;
2882 /* Number of elements in ranges_table currently in use. */
2883 static GTY(()) unsigned ranges_table_in_use;
2885 /* Array of pairs of labels referenced in ranges_table. */
2886 static GTY ((length ("ranges_by_label_allocated")))
2887 dw_ranges_by_label_ref ranges_by_label;
2889 /* Number of elements currently allocated for ranges_by_label. */
2890 static GTY(()) unsigned ranges_by_label_allocated;
2892 /* Number of elements in ranges_by_label currently in use. */
2893 static GTY(()) unsigned ranges_by_label_in_use;
2895 /* Size (in elements) of increments by which we may expand the
2896 ranges_table. */
2897 #define RANGES_TABLE_INCREMENT 64
2899 /* Whether we have location lists that need outputting */
2900 static GTY(()) bool have_location_lists;
2902 /* Unique label counter. */
2903 static GTY(()) unsigned int loclabel_num;
2905 /* Unique label counter for point-of-call tables. */
2906 static GTY(()) unsigned int poc_label_num;
2908 /* Record whether the function being analyzed contains inlined functions. */
2909 static int current_function_has_inlines;
2911 /* The last file entry emitted by maybe_emit_file(). */
2912 static GTY(()) struct dwarf_file_data * last_emitted_file;
2914 /* Number of internal labels generated by gen_internal_sym(). */
2915 static GTY(()) int label_num;
2917 /* Cached result of previous call to lookup_filename. */
2918 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
2920 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
2922 /* Instances of generic types for which we need to generate debug
2923 info that describe their generic parameters and arguments. That
2924 generation needs to happen once all types are properly laid out so
2925 we do it at the end of compilation. */
2926 static GTY(()) vec<tree, va_gc> *generic_type_instances;
2928 /* Offset from the "steady-state frame pointer" to the frame base,
2929 within the current function. */
2930 static HOST_WIDE_INT frame_pointer_fb_offset;
2931 static bool frame_pointer_fb_offset_valid;
2933 static vec<dw_die_ref> base_types;
2935 /* Forward declarations for functions defined in this file. */
2937 static int is_pseudo_reg (const_rtx);
2938 static tree type_main_variant (tree);
2939 static int is_tagged_type (const_tree);
2940 static const char *dwarf_tag_name (unsigned);
2941 static const char *dwarf_attr_name (unsigned);
2942 static const char *dwarf_form_name (unsigned);
2943 static tree decl_ultimate_origin (const_tree);
2944 static tree decl_class_context (tree);
2945 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
2946 static inline enum dw_val_class AT_class (dw_attr_ref);
2947 static inline unsigned int AT_index (dw_attr_ref);
2948 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
2949 static inline unsigned AT_flag (dw_attr_ref);
2950 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
2951 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
2952 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
2953 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
2954 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
2955 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2956 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
2957 unsigned int, unsigned char *);
2958 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
2959 static hashval_t debug_str_do_hash (const void *);
2960 static int debug_str_eq (const void *, const void *);
2961 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
2962 static inline const char *AT_string (dw_attr_ref);
2963 static enum dwarf_form AT_string_form (dw_attr_ref);
2964 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
2965 static void add_AT_specification (dw_die_ref, dw_die_ref);
2966 static inline dw_die_ref AT_ref (dw_attr_ref);
2967 static inline int AT_ref_external (dw_attr_ref);
2968 static inline void set_AT_ref_external (dw_attr_ref, int);
2969 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
2970 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
2971 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
2972 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
2973 dw_loc_list_ref);
2974 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
2975 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
2976 static void remove_addr_table_entry (addr_table_entry *);
2977 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
2978 static inline rtx AT_addr (dw_attr_ref);
2979 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
2980 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
2981 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
2982 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
2983 unsigned HOST_WIDE_INT);
2984 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
2985 unsigned long, bool);
2986 static inline const char *AT_lbl (dw_attr_ref);
2987 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
2988 static const char *get_AT_low_pc (dw_die_ref);
2989 static const char *get_AT_hi_pc (dw_die_ref);
2990 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
2991 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
2992 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
2993 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
2994 static bool is_cxx (void);
2995 static bool is_fortran (void);
2996 static bool is_ada (void);
2997 static void remove_AT (dw_die_ref, enum dwarf_attribute);
2998 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
2999 static void add_child_die (dw_die_ref, dw_die_ref);
3000 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3001 static dw_die_ref lookup_type_die (tree);
3002 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3003 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3004 static void equate_type_number_to_die (tree, dw_die_ref);
3005 static hashval_t decl_die_table_hash (const void *);
3006 static int decl_die_table_eq (const void *, const void *);
3007 static dw_die_ref lookup_decl_die (tree);
3008 static hashval_t common_block_die_table_hash (const void *);
3009 static int common_block_die_table_eq (const void *, const void *);
3010 static hashval_t decl_loc_table_hash (const void *);
3011 static int decl_loc_table_eq (const void *, const void *);
3012 static var_loc_list *lookup_decl_loc (const_tree);
3013 static void equate_decl_number_to_die (tree, dw_die_ref);
3014 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3015 static void print_spaces (FILE *);
3016 static void print_die (dw_die_ref, FILE *);
3017 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3018 static dw_die_ref pop_compile_unit (dw_die_ref);
3019 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3020 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3021 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3022 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3023 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3024 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3025 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
3026 struct md5_ctx *, int *);
3027 struct checksum_attributes;
3028 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3029 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3030 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3031 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3032 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3033 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3034 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3035 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3036 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3037 static void compute_section_prefix (dw_die_ref);
3038 static int is_type_die (dw_die_ref);
3039 static int is_comdat_die (dw_die_ref);
3040 static int is_symbol_die (dw_die_ref);
3041 static inline bool is_template_instantiation (dw_die_ref);
3042 static void assign_symbol_names (dw_die_ref);
3043 static void break_out_includes (dw_die_ref);
3044 static int is_declaration_die (dw_die_ref);
3045 static int should_move_die_to_comdat (dw_die_ref);
3046 static dw_die_ref clone_as_declaration (dw_die_ref);
3047 static dw_die_ref clone_die (dw_die_ref);
3048 static dw_die_ref clone_tree (dw_die_ref);
3049 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3050 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3051 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3052 static dw_die_ref generate_skeleton (dw_die_ref);
3053 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3054 dw_die_ref,
3055 dw_die_ref);
3056 static void break_out_comdat_types (dw_die_ref);
3057 static void copy_decls_for_unworthy_types (dw_die_ref);
3059 static void add_sibling_attributes (dw_die_ref);
3060 static void output_location_lists (dw_die_ref);
3061 static int constant_size (unsigned HOST_WIDE_INT);
3062 static unsigned long size_of_die (dw_die_ref);
3063 static void calc_die_sizes (dw_die_ref);
3064 static void calc_base_type_die_sizes (void);
3065 static void mark_dies (dw_die_ref);
3066 static void unmark_dies (dw_die_ref);
3067 static void unmark_all_dies (dw_die_ref);
3068 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3069 static unsigned long size_of_aranges (void);
3070 static enum dwarf_form value_format (dw_attr_ref);
3071 static void output_value_format (dw_attr_ref);
3072 static void output_abbrev_section (void);
3073 static void output_die_abbrevs (unsigned long, dw_die_ref);
3074 static void output_die_symbol (dw_die_ref);
3075 static void output_die (dw_die_ref);
3076 static void output_compilation_unit_header (void);
3077 static void output_comp_unit (dw_die_ref, int);
3078 static void output_comdat_type_unit (comdat_type_node *);
3079 static const char *dwarf2_name (tree, int);
3080 static void add_pubname (tree, dw_die_ref);
3081 static void add_enumerator_pubname (const char *, dw_die_ref);
3082 static void add_pubname_string (const char *, dw_die_ref);
3083 static void add_pubtype (tree, dw_die_ref);
3084 static void output_pubnames (vec<pubname_entry, va_gc> *);
3085 static void output_aranges (unsigned long);
3086 static unsigned int add_ranges_num (int);
3087 static unsigned int add_ranges (const_tree);
3088 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3089 bool *, bool);
3090 static void output_ranges (void);
3091 static dw_line_info_table *new_line_info_table (void);
3092 static void output_line_info (bool);
3093 static void output_file_names (void);
3094 static dw_die_ref base_type_die (tree);
3095 static int is_base_type (tree);
3096 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3097 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
3098 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3099 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3100 static int type_is_enum (const_tree);
3101 static unsigned int dbx_reg_number (const_rtx);
3102 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3103 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3104 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3105 enum var_init_status);
3106 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3107 enum var_init_status);
3108 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3109 enum var_init_status);
3110 static int is_based_loc (const_rtx);
3111 static int resolve_one_addr (rtx *, void *);
3112 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3113 enum var_init_status);
3114 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
3115 enum var_init_status);
3116 static dw_loc_list_ref loc_list_from_tree (tree, int);
3117 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
3118 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3119 static tree field_type (const_tree);
3120 static unsigned int simple_type_align_in_bits (const_tree);
3121 static unsigned int simple_decl_align_in_bits (const_tree);
3122 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3123 static HOST_WIDE_INT field_byte_offset (const_tree);
3124 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3125 dw_loc_list_ref);
3126 static void add_data_member_location_attribute (dw_die_ref, tree);
3127 static bool add_const_value_attribute (dw_die_ref, rtx);
3128 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3129 static void insert_double (double_int, unsigned char *);
3130 static void insert_float (const_rtx, unsigned char *);
3131 static rtx rtl_for_decl_location (tree);
3132 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3133 enum dwarf_attribute);
3134 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3135 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3136 static void add_name_attribute (dw_die_ref, const char *);
3137 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3138 static void add_comp_dir_attribute (dw_die_ref);
3139 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
3140 static void add_subscript_info (dw_die_ref, tree, bool);
3141 static void add_byte_size_attribute (dw_die_ref, tree);
3142 static void add_bit_offset_attribute (dw_die_ref, tree);
3143 static void add_bit_size_attribute (dw_die_ref, tree);
3144 static void add_prototyped_attribute (dw_die_ref, tree);
3145 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3146 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3147 static void add_src_coords_attributes (dw_die_ref, tree);
3148 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3149 static void push_decl_scope (tree);
3150 static void pop_decl_scope (void);
3151 static dw_die_ref scope_die_for (tree, dw_die_ref);
3152 static inline int local_scope_p (dw_die_ref);
3153 static inline int class_scope_p (dw_die_ref);
3154 static inline int class_or_namespace_scope_p (dw_die_ref);
3155 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
3156 static void add_calling_convention_attribute (dw_die_ref, tree);
3157 static const char *type_tag (const_tree);
3158 static tree member_declared_type (const_tree);
3159 #if 0
3160 static const char *decl_start_label (tree);
3161 #endif
3162 static void gen_array_type_die (tree, dw_die_ref);
3163 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3164 #if 0
3165 static void gen_entry_point_die (tree, dw_die_ref);
3166 #endif
3167 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3168 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3169 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3170 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3171 static void gen_formal_types_die (tree, dw_die_ref);
3172 static void gen_subprogram_die (tree, dw_die_ref);
3173 static void gen_variable_die (tree, tree, dw_die_ref);
3174 static void gen_const_die (tree, dw_die_ref);
3175 static void gen_label_die (tree, dw_die_ref);
3176 static void gen_lexical_block_die (tree, dw_die_ref, int);
3177 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
3178 static void gen_field_die (tree, dw_die_ref);
3179 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3180 static dw_die_ref gen_compile_unit_die (const char *);
3181 static void gen_inheritance_die (tree, tree, dw_die_ref);
3182 static void gen_member_die (tree, dw_die_ref);
3183 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3184 enum debug_info_usage);
3185 static void gen_subroutine_type_die (tree, dw_die_ref);
3186 static void gen_typedef_die (tree, dw_die_ref);
3187 static void gen_type_die (tree, dw_die_ref);
3188 static void gen_block_die (tree, dw_die_ref, int);
3189 static void decls_for_scope (tree, dw_die_ref, int);
3190 static inline int is_redundant_typedef (const_tree);
3191 static bool is_naming_typedef_decl (const_tree);
3192 static inline dw_die_ref get_context_die (tree);
3193 static void gen_namespace_die (tree, dw_die_ref);
3194 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3195 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3196 static dw_die_ref force_decl_die (tree);
3197 static dw_die_ref force_type_die (tree);
3198 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3199 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3200 static struct dwarf_file_data * lookup_filename (const char *);
3201 static void retry_incomplete_types (void);
3202 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3203 static void gen_generic_params_dies (tree);
3204 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3205 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3206 static void splice_child_die (dw_die_ref, dw_die_ref);
3207 static int file_info_cmp (const void *, const void *);
3208 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3209 const char *, const char *);
3210 static void output_loc_list (dw_loc_list_ref);
3211 static char *gen_internal_sym (const char *);
3212 static bool want_pubnames (void);
3214 static void prune_unmark_dies (dw_die_ref);
3215 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3216 static void prune_unused_types_mark (dw_die_ref, int);
3217 static void prune_unused_types_walk (dw_die_ref);
3218 static void prune_unused_types_walk_attribs (dw_die_ref);
3219 static void prune_unused_types_prune (dw_die_ref);
3220 static void prune_unused_types (void);
3221 static int maybe_emit_file (struct dwarf_file_data *fd);
3222 static inline const char *AT_vms_delta1 (dw_attr_ref);
3223 static inline const char *AT_vms_delta2 (dw_attr_ref);
3224 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3225 const char *, const char *);
3226 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3227 static void gen_remaining_tmpl_value_param_die_attribute (void);
3228 static bool generic_type_p (tree);
3229 static void schedule_generic_params_dies_gen (tree t);
3230 static void gen_scheduled_generic_parms_dies (void);
3232 static const char *comp_dir_string (void);
3234 static hashval_t hash_loc_operands (dw_loc_descr_ref, hashval_t);
3236 /* enum for tracking thread-local variables whose address is really an offset
3237 relative to the TLS pointer, which will need link-time relocation, but will
3238 not need relocation by the DWARF consumer. */
3240 enum dtprel_bool
3242 dtprel_false = 0,
3243 dtprel_true = 1
3246 /* Return the operator to use for an address of a variable. For dtprel_true, we
3247 use DW_OP_const*. For regular variables, which need both link-time
3248 relocation and consumer-level relocation (e.g., to account for shared objects
3249 loaded at a random address), we use DW_OP_addr*. */
3251 static inline enum dwarf_location_atom
3252 dw_addr_op (enum dtprel_bool dtprel)
3254 if (dtprel == dtprel_true)
3255 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3256 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3257 else
3258 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3261 /* Return a pointer to a newly allocated address location description. If
3262 dwarf_split_debug_info is true, then record the address with the appropriate
3263 relocation. */
3264 static inline dw_loc_descr_ref
3265 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3267 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3269 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3270 ref->dw_loc_oprnd1.v.val_addr = addr;
3271 ref->dtprel = dtprel;
3272 if (dwarf_split_debug_info)
3273 ref->dw_loc_oprnd1.val_entry
3274 = add_addr_table_entry (addr,
3275 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3276 else
3277 ref->dw_loc_oprnd1.val_entry = NULL;
3279 return ref;
3282 /* Section names used to hold DWARF debugging information. */
3284 #ifndef DEBUG_INFO_SECTION
3285 #define DEBUG_INFO_SECTION ".debug_info"
3286 #endif
3287 #ifndef DEBUG_DWO_INFO_SECTION
3288 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3289 #endif
3290 #ifndef DEBUG_ABBREV_SECTION
3291 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3292 #endif
3293 #ifndef DEBUG_DWO_ABBREV_SECTION
3294 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3295 #endif
3296 #ifndef DEBUG_ARANGES_SECTION
3297 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3298 #endif
3299 #ifndef DEBUG_ADDR_SECTION
3300 #define DEBUG_ADDR_SECTION ".debug_addr"
3301 #endif
3302 #ifndef DEBUG_NORM_MACINFO_SECTION
3303 #define DEBUG_NORM_MACINFO_SECTION ".debug_macinfo"
3304 #endif
3305 #ifndef DEBUG_DWO_MACINFO_SECTION
3306 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3307 #endif
3308 #ifndef DEBUG_MACINFO_SECTION
3309 #define DEBUG_MACINFO_SECTION \
3310 (!dwarf_split_debug_info \
3311 ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3312 #endif
3313 #ifndef DEBUG_NORM_MACRO_SECTION
3314 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3315 #endif
3316 #ifndef DEBUG_DWO_MACRO_SECTION
3317 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3318 #endif
3319 #ifndef DEBUG_MACRO_SECTION
3320 #define DEBUG_MACRO_SECTION \
3321 (!dwarf_split_debug_info \
3322 ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3323 #endif
3324 #ifndef DEBUG_LINE_SECTION
3325 #define DEBUG_LINE_SECTION ".debug_line"
3326 #endif
3327 #ifndef DEBUG_DWO_LINE_SECTION
3328 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3329 #endif
3330 #ifndef DEBUG_LOC_SECTION
3331 #define DEBUG_LOC_SECTION ".debug_loc"
3332 #endif
3333 #ifndef DEBUG_DWO_LOC_SECTION
3334 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3335 #endif
3336 #ifndef DEBUG_PUBNAMES_SECTION
3337 #define DEBUG_PUBNAMES_SECTION \
3338 ((debug_generate_pub_sections == 2) \
3339 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3340 #endif
3341 #ifndef DEBUG_PUBTYPES_SECTION
3342 #define DEBUG_PUBTYPES_SECTION \
3343 ((debug_generate_pub_sections == 2) \
3344 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3345 #endif
3346 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3347 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3348 #ifndef DEBUG_STR_OFFSETS_SECTION
3349 #define DEBUG_STR_OFFSETS_SECTION \
3350 (!dwarf_split_debug_info \
3351 ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3352 #endif
3353 #ifndef DEBUG_STR_DWO_SECTION
3354 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3355 #endif
3356 #ifndef DEBUG_STR_SECTION
3357 #define DEBUG_STR_SECTION ".debug_str"
3358 #endif
3359 #ifndef DEBUG_RANGES_SECTION
3360 #define DEBUG_RANGES_SECTION ".debug_ranges"
3361 #endif
3363 /* Standard ELF section names for compiled code and data. */
3364 #ifndef TEXT_SECTION_NAME
3365 #define TEXT_SECTION_NAME ".text"
3366 #endif
3368 /* Section flags for .debug_macinfo/.debug_macro section. */
3369 #define DEBUG_MACRO_SECTION_FLAGS \
3370 (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3372 /* Section flags for .debug_str section. */
3373 #define DEBUG_STR_SECTION_FLAGS \
3374 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3375 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3376 : SECTION_DEBUG)
3378 /* Section flags for .debug_str.dwo section. */
3379 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3381 /* Labels we insert at beginning sections we can reference instead of
3382 the section names themselves. */
3384 #ifndef TEXT_SECTION_LABEL
3385 #define TEXT_SECTION_LABEL "Ltext"
3386 #endif
3387 #ifndef COLD_TEXT_SECTION_LABEL
3388 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3389 #endif
3390 #ifndef DEBUG_LINE_SECTION_LABEL
3391 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3392 #endif
3393 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3394 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3395 #endif
3396 #ifndef DEBUG_INFO_SECTION_LABEL
3397 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3398 #endif
3399 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3400 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3401 #endif
3402 #ifndef DEBUG_ABBREV_SECTION_LABEL
3403 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3404 #endif
3405 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3406 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3407 #endif
3408 #ifndef DEBUG_ADDR_SECTION_LABEL
3409 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3410 #endif
3411 #ifndef DEBUG_LOC_SECTION_LABEL
3412 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3413 #endif
3414 #ifndef DEBUG_RANGES_SECTION_LABEL
3415 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3416 #endif
3417 #ifndef DEBUG_MACINFO_SECTION_LABEL
3418 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3419 #endif
3420 #ifndef DEBUG_MACRO_SECTION_LABEL
3421 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3422 #endif
3423 #define SKELETON_COMP_DIE_ABBREV 1
3424 #define SKELETON_TYPE_DIE_ABBREV 2
3426 /* Definitions of defaults for formats and names of various special
3427 (artificial) labels which may be generated within this file (when the -g
3428 options is used and DWARF2_DEBUGGING_INFO is in effect.
3429 If necessary, these may be overridden from within the tm.h file, but
3430 typically, overriding these defaults is unnecessary. */
3432 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3433 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3434 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3435 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3436 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3437 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3438 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3439 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3440 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3441 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3442 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3443 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3444 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3445 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3447 #ifndef TEXT_END_LABEL
3448 #define TEXT_END_LABEL "Letext"
3449 #endif
3450 #ifndef COLD_END_LABEL
3451 #define COLD_END_LABEL "Letext_cold"
3452 #endif
3453 #ifndef BLOCK_BEGIN_LABEL
3454 #define BLOCK_BEGIN_LABEL "LBB"
3455 #endif
3456 #ifndef BLOCK_END_LABEL
3457 #define BLOCK_END_LABEL "LBE"
3458 #endif
3459 #ifndef LINE_CODE_LABEL
3460 #define LINE_CODE_LABEL "LM"
3461 #endif
3464 /* Return the root of the DIE's built for the current compilation unit. */
3465 static dw_die_ref
3466 comp_unit_die (void)
3468 if (!single_comp_unit_die)
3469 single_comp_unit_die = gen_compile_unit_die (NULL);
3470 return single_comp_unit_die;
3473 /* We allow a language front-end to designate a function that is to be
3474 called to "demangle" any name before it is put into a DIE. */
3476 static const char *(*demangle_name_func) (const char *);
3478 void
3479 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3481 demangle_name_func = func;
3484 /* Test if rtl node points to a pseudo register. */
3486 static inline int
3487 is_pseudo_reg (const_rtx rtl)
3489 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3490 || (GET_CODE (rtl) == SUBREG
3491 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3494 /* Return a reference to a type, with its const and volatile qualifiers
3495 removed. */
3497 static inline tree
3498 type_main_variant (tree type)
3500 type = TYPE_MAIN_VARIANT (type);
3502 /* ??? There really should be only one main variant among any group of
3503 variants of a given type (and all of the MAIN_VARIANT values for all
3504 members of the group should point to that one type) but sometimes the C
3505 front-end messes this up for array types, so we work around that bug
3506 here. */
3507 if (TREE_CODE (type) == ARRAY_TYPE)
3508 while (type != TYPE_MAIN_VARIANT (type))
3509 type = TYPE_MAIN_VARIANT (type);
3511 return type;
3514 /* Return nonzero if the given type node represents a tagged type. */
3516 static inline int
3517 is_tagged_type (const_tree type)
3519 enum tree_code code = TREE_CODE (type);
3521 return (code == RECORD_TYPE || code == UNION_TYPE
3522 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3525 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3527 static void
3528 get_ref_die_offset_label (char *label, dw_die_ref ref)
3530 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3533 /* Return die_offset of a DIE reference to a base type. */
3535 static unsigned long int
3536 get_base_type_offset (dw_die_ref ref)
3538 if (ref->die_offset)
3539 return ref->die_offset;
3540 if (comp_unit_die ()->die_abbrev)
3542 calc_base_type_die_sizes ();
3543 gcc_assert (ref->die_offset);
3545 return ref->die_offset;
3548 /* Return die_offset of a DIE reference other than base type. */
3550 static unsigned long int
3551 get_ref_die_offset (dw_die_ref ref)
3553 gcc_assert (ref->die_offset);
3554 return ref->die_offset;
3557 /* Convert a DIE tag into its string name. */
3559 static const char *
3560 dwarf_tag_name (unsigned int tag)
3562 const char *name = get_DW_TAG_name (tag);
3564 if (name != NULL)
3565 return name;
3567 return "DW_TAG_<unknown>";
3570 /* Convert a DWARF attribute code into its string name. */
3572 static const char *
3573 dwarf_attr_name (unsigned int attr)
3575 const char *name;
3577 switch (attr)
3579 #if VMS_DEBUGGING_INFO
3580 case DW_AT_HP_prologue:
3581 return "DW_AT_HP_prologue";
3582 #else
3583 case DW_AT_MIPS_loop_unroll_factor:
3584 return "DW_AT_MIPS_loop_unroll_factor";
3585 #endif
3587 #if VMS_DEBUGGING_INFO
3588 case DW_AT_HP_epilogue:
3589 return "DW_AT_HP_epilogue";
3590 #else
3591 case DW_AT_MIPS_stride:
3592 return "DW_AT_MIPS_stride";
3593 #endif
3596 name = get_DW_AT_name (attr);
3598 if (name != NULL)
3599 return name;
3601 return "DW_AT_<unknown>";
3604 /* Convert a DWARF value form code into its string name. */
3606 static const char *
3607 dwarf_form_name (unsigned int form)
3609 const char *name = get_DW_FORM_name (form);
3611 if (name != NULL)
3612 return name;
3614 return "DW_FORM_<unknown>";
3617 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3618 instance of an inlined instance of a decl which is local to an inline
3619 function, so we have to trace all of the way back through the origin chain
3620 to find out what sort of node actually served as the original seed for the
3621 given block. */
3623 static tree
3624 decl_ultimate_origin (const_tree decl)
3626 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3627 return NULL_TREE;
3629 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
3630 nodes in the function to point to themselves; ignore that if
3631 we're trying to output the abstract instance of this function. */
3632 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3633 return NULL_TREE;
3635 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3636 most distant ancestor, this should never happen. */
3637 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3639 return DECL_ABSTRACT_ORIGIN (decl);
3642 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
3643 of a virtual function may refer to a base class, so we check the 'this'
3644 parameter. */
3646 static tree
3647 decl_class_context (tree decl)
3649 tree context = NULL_TREE;
3651 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3652 context = DECL_CONTEXT (decl);
3653 else
3654 context = TYPE_MAIN_VARIANT
3655 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3657 if (context && !TYPE_P (context))
3658 context = NULL_TREE;
3660 return context;
3663 /* Add an attribute/value pair to a DIE. */
3665 static inline void
3666 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
3668 /* Maybe this should be an assert? */
3669 if (die == NULL)
3670 return;
3672 vec_safe_reserve (die->die_attr, 1);
3673 vec_safe_push (die->die_attr, *attr);
3676 static inline enum dw_val_class
3677 AT_class (dw_attr_ref a)
3679 return a->dw_attr_val.val_class;
3682 /* Return the index for any attribute that will be referenced with a
3683 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
3684 are stored in dw_attr_val.v.val_str for reference counting
3685 pruning. */
3687 static inline unsigned int
3688 AT_index (dw_attr_ref a)
3690 if (AT_class (a) == dw_val_class_str)
3691 return a->dw_attr_val.v.val_str->index;
3692 else if (a->dw_attr_val.val_entry != NULL)
3693 return a->dw_attr_val.val_entry->index;
3694 return NOT_INDEXED;
3697 /* Add a flag value attribute to a DIE. */
3699 static inline void
3700 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3702 dw_attr_node attr;
3704 attr.dw_attr = attr_kind;
3705 attr.dw_attr_val.val_class = dw_val_class_flag;
3706 attr.dw_attr_val.val_entry = NULL;
3707 attr.dw_attr_val.v.val_flag = flag;
3708 add_dwarf_attr (die, &attr);
3711 static inline unsigned
3712 AT_flag (dw_attr_ref a)
3714 gcc_assert (a && AT_class (a) == dw_val_class_flag);
3715 return a->dw_attr_val.v.val_flag;
3718 /* Add a signed integer attribute value to a DIE. */
3720 static inline void
3721 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3723 dw_attr_node attr;
3725 attr.dw_attr = attr_kind;
3726 attr.dw_attr_val.val_class = dw_val_class_const;
3727 attr.dw_attr_val.val_entry = NULL;
3728 attr.dw_attr_val.v.val_int = int_val;
3729 add_dwarf_attr (die, &attr);
3732 static inline HOST_WIDE_INT
3733 AT_int (dw_attr_ref a)
3735 gcc_assert (a && AT_class (a) == dw_val_class_const);
3736 return a->dw_attr_val.v.val_int;
3739 /* Add an unsigned integer attribute value to a DIE. */
3741 static inline void
3742 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3743 unsigned HOST_WIDE_INT unsigned_val)
3745 dw_attr_node attr;
3747 attr.dw_attr = attr_kind;
3748 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3749 attr.dw_attr_val.val_entry = NULL;
3750 attr.dw_attr_val.v.val_unsigned = unsigned_val;
3751 add_dwarf_attr (die, &attr);
3754 static inline unsigned HOST_WIDE_INT
3755 AT_unsigned (dw_attr_ref a)
3757 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3758 return a->dw_attr_val.v.val_unsigned;
3761 /* Add an unsigned double integer attribute value to a DIE. */
3763 static inline void
3764 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3765 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3767 dw_attr_node attr;
3769 attr.dw_attr = attr_kind;
3770 attr.dw_attr_val.val_class = dw_val_class_const_double;
3771 attr.dw_attr_val.val_entry = NULL;
3772 attr.dw_attr_val.v.val_double.high = high;
3773 attr.dw_attr_val.v.val_double.low = low;
3774 add_dwarf_attr (die, &attr);
3777 /* Add a floating point attribute value to a DIE and return it. */
3779 static inline void
3780 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3781 unsigned int length, unsigned int elt_size, unsigned char *array)
3783 dw_attr_node attr;
3785 attr.dw_attr = attr_kind;
3786 attr.dw_attr_val.val_class = dw_val_class_vec;
3787 attr.dw_attr_val.val_entry = NULL;
3788 attr.dw_attr_val.v.val_vec.length = length;
3789 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3790 attr.dw_attr_val.v.val_vec.array = array;
3791 add_dwarf_attr (die, &attr);
3794 /* Add an 8-byte data attribute value to a DIE. */
3796 static inline void
3797 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3798 unsigned char data8[8])
3800 dw_attr_node attr;
3802 attr.dw_attr = attr_kind;
3803 attr.dw_attr_val.val_class = dw_val_class_data8;
3804 attr.dw_attr_val.val_entry = NULL;
3805 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3806 add_dwarf_attr (die, &attr);
3809 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
3810 dwarf_split_debug_info, address attributes in dies destined for the
3811 final executable have force_direct set to avoid using indexed
3812 references. */
3814 static inline void
3815 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
3816 bool force_direct)
3818 dw_attr_node attr;
3819 char * lbl_id;
3821 lbl_id = xstrdup (lbl_low);
3822 attr.dw_attr = DW_AT_low_pc;
3823 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3824 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3825 if (dwarf_split_debug_info && !force_direct)
3826 attr.dw_attr_val.val_entry
3827 = add_addr_table_entry (lbl_id, ate_kind_label);
3828 else
3829 attr.dw_attr_val.val_entry = NULL;
3830 add_dwarf_attr (die, &attr);
3832 attr.dw_attr = DW_AT_high_pc;
3833 if (dwarf_version < 4)
3834 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3835 else
3836 attr.dw_attr_val.val_class = dw_val_class_high_pc;
3837 lbl_id = xstrdup (lbl_high);
3838 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3839 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
3840 && dwarf_split_debug_info && !force_direct)
3841 attr.dw_attr_val.val_entry
3842 = add_addr_table_entry (lbl_id, ate_kind_label);
3843 else
3844 attr.dw_attr_val.val_entry = NULL;
3845 add_dwarf_attr (die, &attr);
3848 /* Hash and equality functions for debug_str_hash. */
3850 static hashval_t
3851 debug_str_do_hash (const void *x)
3853 return htab_hash_string (((const struct indirect_string_node *)x)->str);
3856 static int
3857 debug_str_eq (const void *x1, const void *x2)
3859 return strcmp ((((const struct indirect_string_node *)x1)->str),
3860 (const char *)x2) == 0;
3863 /* Add STR to the given string hash table. */
3865 static struct indirect_string_node *
3866 find_AT_string_in_table (const char *str, htab_t table)
3868 struct indirect_string_node *node;
3869 void **slot;
3871 slot = htab_find_slot_with_hash (table, str,
3872 htab_hash_string (str), INSERT);
3873 if (*slot == NULL)
3875 node = ggc_alloc_cleared_indirect_string_node ();
3876 node->str = ggc_strdup (str);
3877 *slot = node;
3879 else
3880 node = (struct indirect_string_node *) *slot;
3882 node->refcount++;
3883 return node;
3886 /* Add STR to the indirect string hash table. */
3888 static struct indirect_string_node *
3889 find_AT_string (const char *str)
3891 if (! debug_str_hash)
3892 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
3893 debug_str_eq, NULL);
3895 return find_AT_string_in_table (str, debug_str_hash);
3898 /* Add a string attribute value to a DIE. */
3900 static inline void
3901 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
3903 dw_attr_node attr;
3904 struct indirect_string_node *node;
3906 node = find_AT_string (str);
3908 attr.dw_attr = attr_kind;
3909 attr.dw_attr_val.val_class = dw_val_class_str;
3910 attr.dw_attr_val.val_entry = NULL;
3911 attr.dw_attr_val.v.val_str = node;
3912 add_dwarf_attr (die, &attr);
3915 static inline const char *
3916 AT_string (dw_attr_ref a)
3918 gcc_assert (a && AT_class (a) == dw_val_class_str);
3919 return a->dw_attr_val.v.val_str->str;
3922 /* Call this function directly to bypass AT_string_form's logic to put
3923 the string inline in the die. */
3925 static void
3926 set_indirect_string (struct indirect_string_node *node)
3928 char label[32];
3929 /* Already indirect is a no op. */
3930 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
3932 gcc_assert (node->label);
3933 return;
3935 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
3936 ++dw2_string_counter;
3937 node->label = xstrdup (label);
3939 if (!dwarf_split_debug_info)
3941 node->form = DW_FORM_strp;
3942 node->index = NOT_INDEXED;
3944 else
3946 node->form = DW_FORM_GNU_str_index;
3947 node->index = NO_INDEX_ASSIGNED;
3951 /* Find out whether a string should be output inline in DIE
3952 or out-of-line in .debug_str section. */
3954 static enum dwarf_form
3955 find_string_form (struct indirect_string_node *node)
3957 unsigned int len;
3959 if (node->form)
3960 return node->form;
3962 len = strlen (node->str) + 1;
3964 /* If the string is shorter or equal to the size of the reference, it is
3965 always better to put it inline. */
3966 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
3967 return node->form = DW_FORM_string;
3969 /* If we cannot expect the linker to merge strings in .debug_str
3970 section, only put it into .debug_str if it is worth even in this
3971 single module. */
3972 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
3973 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
3974 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
3975 return node->form = DW_FORM_string;
3977 set_indirect_string (node);
3979 return node->form;
3982 /* Find out whether the string referenced from the attribute should be
3983 output inline in DIE or out-of-line in .debug_str section. */
3985 static enum dwarf_form
3986 AT_string_form (dw_attr_ref a)
3988 gcc_assert (a && AT_class (a) == dw_val_class_str);
3989 return find_string_form (a->dw_attr_val.v.val_str);
3992 /* Add a DIE reference attribute value to a DIE. */
3994 static inline void
3995 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
3997 dw_attr_node attr;
3999 #ifdef ENABLE_CHECKING
4000 gcc_assert (targ_die != NULL);
4001 #else
4002 /* With LTO we can end up trying to reference something we didn't create
4003 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4004 if (targ_die == NULL)
4005 return;
4006 #endif
4008 attr.dw_attr = attr_kind;
4009 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4010 attr.dw_attr_val.val_entry = NULL;
4011 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4012 attr.dw_attr_val.v.val_die_ref.external = 0;
4013 add_dwarf_attr (die, &attr);
4016 /* Change DIE reference REF to point to NEW_DIE instead. */
4018 static inline void
4019 change_AT_die_ref (dw_attr_ref ref, dw_die_ref new_die)
4021 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4022 ref->dw_attr_val.v.val_die_ref.die = new_die;
4023 ref->dw_attr_val.v.val_die_ref.external = 0;
4026 /* Add an AT_specification attribute to a DIE, and also make the back
4027 pointer from the specification to the definition. */
4029 static inline void
4030 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4032 add_AT_die_ref (die, DW_AT_specification, targ_die);
4033 gcc_assert (!targ_die->die_definition);
4034 targ_die->die_definition = die;
4037 static inline dw_die_ref
4038 AT_ref (dw_attr_ref a)
4040 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4041 return a->dw_attr_val.v.val_die_ref.die;
4044 static inline int
4045 AT_ref_external (dw_attr_ref a)
4047 if (a && AT_class (a) == dw_val_class_die_ref)
4048 return a->dw_attr_val.v.val_die_ref.external;
4050 return 0;
4053 static inline void
4054 set_AT_ref_external (dw_attr_ref a, int i)
4056 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4057 a->dw_attr_val.v.val_die_ref.external = i;
4060 /* Add an FDE reference attribute value to a DIE. */
4062 static inline void
4063 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4065 dw_attr_node attr;
4067 attr.dw_attr = attr_kind;
4068 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4069 attr.dw_attr_val.val_entry = NULL;
4070 attr.dw_attr_val.v.val_fde_index = targ_fde;
4071 add_dwarf_attr (die, &attr);
4074 /* Add a location description attribute value to a DIE. */
4076 static inline void
4077 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4079 dw_attr_node attr;
4081 attr.dw_attr = attr_kind;
4082 attr.dw_attr_val.val_class = dw_val_class_loc;
4083 attr.dw_attr_val.val_entry = NULL;
4084 attr.dw_attr_val.v.val_loc = loc;
4085 add_dwarf_attr (die, &attr);
4088 static inline dw_loc_descr_ref
4089 AT_loc (dw_attr_ref a)
4091 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4092 return a->dw_attr_val.v.val_loc;
4095 static inline void
4096 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4098 dw_attr_node attr;
4100 attr.dw_attr = attr_kind;
4101 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4102 attr.dw_attr_val.val_entry = NULL;
4103 attr.dw_attr_val.v.val_loc_list = loc_list;
4104 add_dwarf_attr (die, &attr);
4105 have_location_lists = true;
4108 static inline dw_loc_list_ref
4109 AT_loc_list (dw_attr_ref a)
4111 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4112 return a->dw_attr_val.v.val_loc_list;
4115 static inline dw_loc_list_ref *
4116 AT_loc_list_ptr (dw_attr_ref a)
4118 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4119 return &a->dw_attr_val.v.val_loc_list;
4122 /* Table of entries into the .debug_addr section. */
4124 static GTY ((param_is (addr_table_entry))) htab_t addr_index_table;
4126 /* Hash an address_table_entry. */
4128 static hashval_t
4129 addr_table_entry_do_hash (const void *x)
4131 const addr_table_entry *a = (const addr_table_entry *) x;
4132 switch (a->kind)
4134 case ate_kind_rtx:
4135 return iterative_hash_rtx (a->addr.rtl, 0);
4136 case ate_kind_rtx_dtprel:
4137 return iterative_hash_rtx (a->addr.rtl, 1);
4138 case ate_kind_label:
4139 return htab_hash_string (a->addr.label);
4140 default:
4141 gcc_unreachable ();
4145 /* Determine equality for two address_table_entries. */
4147 static int
4148 addr_table_entry_eq (const void *x1, const void *x2)
4150 const addr_table_entry *a1 = (const addr_table_entry *) x1;
4151 const addr_table_entry *a2 = (const addr_table_entry *) x2;
4153 if (a1->kind != a2->kind)
4154 return 0;
4155 switch (a1->kind)
4157 case ate_kind_rtx:
4158 case ate_kind_rtx_dtprel:
4159 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4160 case ate_kind_label:
4161 return strcmp (a1->addr.label, a2->addr.label) == 0;
4162 default:
4163 gcc_unreachable ();
4167 /* Initialize an addr_table_entry. */
4169 void
4170 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4172 e->kind = kind;
4173 switch (kind)
4175 case ate_kind_rtx:
4176 case ate_kind_rtx_dtprel:
4177 e->addr.rtl = (rtx) addr;
4178 break;
4179 case ate_kind_label:
4180 e->addr.label = (char *) addr;
4181 break;
4183 e->refcount = 0;
4184 e->index = NO_INDEX_ASSIGNED;
4187 /* Add attr to the address table entry to the table. Defer setting an
4188 index until output time. */
4190 static addr_table_entry *
4191 add_addr_table_entry (void *addr, enum ate_kind kind)
4193 addr_table_entry *node;
4194 addr_table_entry finder;
4195 void **slot;
4197 gcc_assert (dwarf_split_debug_info);
4198 if (! addr_index_table)
4199 addr_index_table = htab_create_ggc (10, addr_table_entry_do_hash,
4200 addr_table_entry_eq, NULL);
4201 init_addr_table_entry (&finder, kind, addr);
4202 slot = htab_find_slot (addr_index_table, &finder, INSERT);
4204 if (*slot == HTAB_EMPTY_ENTRY)
4206 node = ggc_alloc_cleared_addr_table_entry ();
4207 init_addr_table_entry (node, kind, addr);
4208 *slot = node;
4210 else
4211 node = (addr_table_entry *) *slot;
4213 node->refcount++;
4214 return node;
4217 /* Remove an entry from the addr table by decrementing its refcount.
4218 Strictly, decrementing the refcount would be enough, but the
4219 assertion that the entry is actually in the table has found
4220 bugs. */
4222 static void
4223 remove_addr_table_entry (addr_table_entry *entry)
4225 addr_table_entry *node;
4227 gcc_assert (dwarf_split_debug_info && addr_index_table);
4228 node = (addr_table_entry *) htab_find (addr_index_table, entry);
4229 /* After an index is assigned, the table is frozen. */
4230 gcc_assert (node->refcount > 0 && node->index == NO_INDEX_ASSIGNED);
4231 node->refcount--;
4234 /* Given a location list, remove all addresses it refers to from the
4235 address_table. */
4237 static void
4238 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4240 for (; descr; descr = descr->dw_loc_next)
4241 if (descr->dw_loc_oprnd1.val_entry != NULL)
4243 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4244 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4248 /* A helper function for dwarf2out_finish called through
4249 htab_traverse. Assign an addr_table_entry its index. All entries
4250 must be collected into the table when this function is called,
4251 because the indexing code relies on htab_traverse to traverse nodes
4252 in the same order for each run. */
4254 static int
4255 index_addr_table_entry (void **h, void *v)
4257 addr_table_entry *node = (addr_table_entry *) *h;
4258 unsigned int *index = (unsigned int *) v;
4260 /* Don't index unreferenced nodes. */
4261 if (node->refcount == 0)
4262 return 1;
4264 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4265 node->index = *index;
4266 *index += 1;
4268 return 1;
4271 /* Add an address constant attribute value to a DIE. When using
4272 dwarf_split_debug_info, address attributes in dies destined for the
4273 final executable should be direct references--setting the parameter
4274 force_direct ensures this behavior. */
4276 static inline void
4277 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4278 bool force_direct)
4280 dw_attr_node attr;
4282 attr.dw_attr = attr_kind;
4283 attr.dw_attr_val.val_class = dw_val_class_addr;
4284 attr.dw_attr_val.v.val_addr = addr;
4285 if (dwarf_split_debug_info && !force_direct)
4286 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4287 else
4288 attr.dw_attr_val.val_entry = NULL;
4289 add_dwarf_attr (die, &attr);
4292 /* Get the RTX from to an address DIE attribute. */
4294 static inline rtx
4295 AT_addr (dw_attr_ref a)
4297 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4298 return a->dw_attr_val.v.val_addr;
4301 /* Add a file attribute value to a DIE. */
4303 static inline void
4304 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4305 struct dwarf_file_data *fd)
4307 dw_attr_node attr;
4309 attr.dw_attr = attr_kind;
4310 attr.dw_attr_val.val_class = dw_val_class_file;
4311 attr.dw_attr_val.val_entry = NULL;
4312 attr.dw_attr_val.v.val_file = fd;
4313 add_dwarf_attr (die, &attr);
4316 /* Get the dwarf_file_data from a file DIE attribute. */
4318 static inline struct dwarf_file_data *
4319 AT_file (dw_attr_ref a)
4321 gcc_assert (a && AT_class (a) == dw_val_class_file);
4322 return a->dw_attr_val.v.val_file;
4325 /* Add a vms delta attribute value to a DIE. */
4327 static inline void
4328 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4329 const char *lbl1, const char *lbl2)
4331 dw_attr_node attr;
4333 attr.dw_attr = attr_kind;
4334 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4335 attr.dw_attr_val.val_entry = NULL;
4336 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4337 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4338 add_dwarf_attr (die, &attr);
4341 /* Add a label identifier attribute value to a DIE. */
4343 static inline void
4344 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4345 const char *lbl_id)
4347 dw_attr_node attr;
4349 attr.dw_attr = attr_kind;
4350 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4351 attr.dw_attr_val.val_entry = NULL;
4352 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4353 if (dwarf_split_debug_info)
4354 attr.dw_attr_val.val_entry
4355 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4356 ate_kind_label);
4357 add_dwarf_attr (die, &attr);
4360 /* Add a section offset attribute value to a DIE, an offset into the
4361 debug_line section. */
4363 static inline void
4364 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4365 const char *label)
4367 dw_attr_node attr;
4369 attr.dw_attr = attr_kind;
4370 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4371 attr.dw_attr_val.val_entry = NULL;
4372 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4373 add_dwarf_attr (die, &attr);
4376 /* Add a section offset attribute value to a DIE, an offset into the
4377 debug_macinfo section. */
4379 static inline void
4380 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4381 const char *label)
4383 dw_attr_node attr;
4385 attr.dw_attr = attr_kind;
4386 attr.dw_attr_val.val_class = dw_val_class_macptr;
4387 attr.dw_attr_val.val_entry = NULL;
4388 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4389 add_dwarf_attr (die, &attr);
4392 /* Add an offset attribute value to a DIE. */
4394 static inline void
4395 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4396 unsigned HOST_WIDE_INT offset)
4398 dw_attr_node attr;
4400 attr.dw_attr = attr_kind;
4401 attr.dw_attr_val.val_class = dw_val_class_offset;
4402 attr.dw_attr_val.val_entry = NULL;
4403 attr.dw_attr_val.v.val_offset = offset;
4404 add_dwarf_attr (die, &attr);
4407 /* Add a range_list attribute value to a DIE. When using
4408 dwarf_split_debug_info, address attributes in dies destined for the
4409 final executable should be direct references--setting the parameter
4410 force_direct ensures this behavior. */
4412 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4413 #define RELOCATED_OFFSET (NULL)
4415 static void
4416 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4417 long unsigned int offset, bool force_direct)
4419 dw_attr_node attr;
4421 attr.dw_attr = attr_kind;
4422 attr.dw_attr_val.val_class = dw_val_class_range_list;
4423 /* For the range_list attribute, use val_entry to store whether the
4424 offset should follow split-debug-info or normal semantics. This
4425 value is read in output_range_list_offset. */
4426 if (dwarf_split_debug_info && !force_direct)
4427 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4428 else
4429 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4430 attr.dw_attr_val.v.val_offset = offset;
4431 add_dwarf_attr (die, &attr);
4434 /* Return the start label of a delta attribute. */
4436 static inline const char *
4437 AT_vms_delta1 (dw_attr_ref a)
4439 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4440 return a->dw_attr_val.v.val_vms_delta.lbl1;
4443 /* Return the end label of a delta attribute. */
4445 static inline const char *
4446 AT_vms_delta2 (dw_attr_ref a)
4448 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4449 return a->dw_attr_val.v.val_vms_delta.lbl2;
4452 static inline const char *
4453 AT_lbl (dw_attr_ref a)
4455 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4456 || AT_class (a) == dw_val_class_lineptr
4457 || AT_class (a) == dw_val_class_macptr
4458 || AT_class (a) == dw_val_class_high_pc));
4459 return a->dw_attr_val.v.val_lbl_id;
4462 /* Get the attribute of type attr_kind. */
4464 static dw_attr_ref
4465 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4467 dw_attr_ref a;
4468 unsigned ix;
4469 dw_die_ref spec = NULL;
4471 if (! die)
4472 return NULL;
4474 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4475 if (a->dw_attr == attr_kind)
4476 return a;
4477 else if (a->dw_attr == DW_AT_specification
4478 || a->dw_attr == DW_AT_abstract_origin)
4479 spec = AT_ref (a);
4481 if (spec)
4482 return get_AT (spec, attr_kind);
4484 return NULL;
4487 /* Returns the parent of the declaration of DIE. */
4489 static dw_die_ref
4490 get_die_parent (dw_die_ref die)
4492 dw_die_ref t;
4494 if (!die)
4495 return NULL;
4497 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4498 || (t = get_AT_ref (die, DW_AT_specification)))
4499 die = t;
4501 return die->die_parent;
4504 /* Return the "low pc" attribute value, typically associated with a subprogram
4505 DIE. Return null if the "low pc" attribute is either not present, or if it
4506 cannot be represented as an assembler label identifier. */
4508 static inline const char *
4509 get_AT_low_pc (dw_die_ref die)
4511 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4513 return a ? AT_lbl (a) : NULL;
4516 /* Return the "high pc" attribute value, typically associated with a subprogram
4517 DIE. Return null if the "high pc" attribute is either not present, or if it
4518 cannot be represented as an assembler label identifier. */
4520 static inline const char *
4521 get_AT_hi_pc (dw_die_ref die)
4523 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4525 return a ? AT_lbl (a) : NULL;
4528 /* Return the value of the string attribute designated by ATTR_KIND, or
4529 NULL if it is not present. */
4531 static inline const char *
4532 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4534 dw_attr_ref a = get_AT (die, attr_kind);
4536 return a ? AT_string (a) : NULL;
4539 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4540 if it is not present. */
4542 static inline int
4543 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4545 dw_attr_ref a = get_AT (die, attr_kind);
4547 return a ? AT_flag (a) : 0;
4550 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4551 if it is not present. */
4553 static inline unsigned
4554 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4556 dw_attr_ref a = get_AT (die, attr_kind);
4558 return a ? AT_unsigned (a) : 0;
4561 static inline dw_die_ref
4562 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4564 dw_attr_ref a = get_AT (die, attr_kind);
4566 return a ? AT_ref (a) : NULL;
4569 static inline struct dwarf_file_data *
4570 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4572 dw_attr_ref a = get_AT (die, attr_kind);
4574 return a ? AT_file (a) : NULL;
4577 /* Return TRUE if the language is C++. */
4579 static inline bool
4580 is_cxx (void)
4582 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4584 return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
4587 /* Return TRUE if the language is Java. */
4589 static inline bool
4590 is_java (void)
4592 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4594 return lang == DW_LANG_Java;
4597 /* Return TRUE if the language is Fortran. */
4599 static inline bool
4600 is_fortran (void)
4602 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4604 return (lang == DW_LANG_Fortran77
4605 || lang == DW_LANG_Fortran90
4606 || lang == DW_LANG_Fortran95);
4609 /* Return TRUE if the language is Ada. */
4611 static inline bool
4612 is_ada (void)
4614 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4616 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4619 /* Remove the specified attribute if present. */
4621 static void
4622 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4624 dw_attr_ref a;
4625 unsigned ix;
4627 if (! die)
4628 return;
4630 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4631 if (a->dw_attr == attr_kind)
4633 if (AT_class (a) == dw_val_class_str)
4634 if (a->dw_attr_val.v.val_str->refcount)
4635 a->dw_attr_val.v.val_str->refcount--;
4637 /* vec::ordered_remove should help reduce the number of abbrevs
4638 that are needed. */
4639 die->die_attr->ordered_remove (ix);
4640 return;
4644 /* Remove CHILD from its parent. PREV must have the property that
4645 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4647 static void
4648 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4650 gcc_assert (child->die_parent == prev->die_parent);
4651 gcc_assert (prev->die_sib == child);
4652 if (prev == child)
4654 gcc_assert (child->die_parent->die_child == child);
4655 prev = NULL;
4657 else
4658 prev->die_sib = child->die_sib;
4659 if (child->die_parent->die_child == child)
4660 child->die_parent->die_child = prev;
4663 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4664 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4666 static void
4667 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4669 dw_die_ref parent = old_child->die_parent;
4671 gcc_assert (parent == prev->die_parent);
4672 gcc_assert (prev->die_sib == old_child);
4674 new_child->die_parent = parent;
4675 if (prev == old_child)
4677 gcc_assert (parent->die_child == old_child);
4678 new_child->die_sib = new_child;
4680 else
4682 prev->die_sib = new_child;
4683 new_child->die_sib = old_child->die_sib;
4685 if (old_child->die_parent->die_child == old_child)
4686 old_child->die_parent->die_child = new_child;
4689 /* Move all children from OLD_PARENT to NEW_PARENT. */
4691 static void
4692 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4694 dw_die_ref c;
4695 new_parent->die_child = old_parent->die_child;
4696 old_parent->die_child = NULL;
4697 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4700 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4701 matches TAG. */
4703 static void
4704 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4706 dw_die_ref c;
4708 c = die->die_child;
4709 if (c) do {
4710 dw_die_ref prev = c;
4711 c = c->die_sib;
4712 while (c->die_tag == tag)
4714 remove_child_with_prev (c, prev);
4715 /* Might have removed every child. */
4716 if (c == c->die_sib)
4717 return;
4718 c = c->die_sib;
4720 } while (c != die->die_child);
4723 /* Add a CHILD_DIE as the last child of DIE. */
4725 static void
4726 add_child_die (dw_die_ref die, dw_die_ref child_die)
4728 /* FIXME this should probably be an assert. */
4729 if (! die || ! child_die)
4730 return;
4731 gcc_assert (die != child_die);
4733 child_die->die_parent = die;
4734 if (die->die_child)
4736 child_die->die_sib = die->die_child->die_sib;
4737 die->die_child->die_sib = child_die;
4739 else
4740 child_die->die_sib = child_die;
4741 die->die_child = child_die;
4744 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4745 is the specification, to the end of PARENT's list of children.
4746 This is done by removing and re-adding it. */
4748 static void
4749 splice_child_die (dw_die_ref parent, dw_die_ref child)
4751 dw_die_ref p;
4753 /* We want the declaration DIE from inside the class, not the
4754 specification DIE at toplevel. */
4755 if (child->die_parent != parent)
4757 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4759 if (tmp)
4760 child = tmp;
4763 gcc_assert (child->die_parent == parent
4764 || (child->die_parent
4765 == get_AT_ref (parent, DW_AT_specification)));
4767 for (p = child->die_parent->die_child; ; p = p->die_sib)
4768 if (p->die_sib == child)
4770 remove_child_with_prev (child, p);
4771 break;
4774 add_child_die (parent, child);
4777 /* Return a pointer to a newly created DIE node. */
4779 static inline dw_die_ref
4780 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4782 dw_die_ref die = ggc_alloc_cleared_die_node ();
4784 die->die_tag = tag_value;
4786 if (parent_die != NULL)
4787 add_child_die (parent_die, die);
4788 else
4790 limbo_die_node *limbo_node;
4792 limbo_node = ggc_alloc_cleared_limbo_die_node ();
4793 limbo_node->die = die;
4794 limbo_node->created_for = t;
4795 limbo_node->next = limbo_die_list;
4796 limbo_die_list = limbo_node;
4799 return die;
4802 /* Return the DIE associated with the given type specifier. */
4804 static inline dw_die_ref
4805 lookup_type_die (tree type)
4807 return TYPE_SYMTAB_DIE (type);
4810 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4811 anonymous type named by the typedef TYPE_DIE, return the DIE of the
4812 anonymous type instead the one of the naming typedef. */
4814 static inline dw_die_ref
4815 strip_naming_typedef (tree type, dw_die_ref type_die)
4817 if (type
4818 && TREE_CODE (type) == RECORD_TYPE
4819 && type_die
4820 && type_die->die_tag == DW_TAG_typedef
4821 && is_naming_typedef_decl (TYPE_NAME (type)))
4822 type_die = get_AT_ref (type_die, DW_AT_type);
4823 return type_die;
4826 /* Like lookup_type_die, but if type is an anonymous type named by a
4827 typedef[1], return the DIE of the anonymous type instead the one of
4828 the naming typedef. This is because in gen_typedef_die, we did
4829 equate the anonymous struct named by the typedef with the DIE of
4830 the naming typedef. So by default, lookup_type_die on an anonymous
4831 struct yields the DIE of the naming typedef.
4833 [1]: Read the comment of is_naming_typedef_decl to learn about what
4834 a naming typedef is. */
4836 static inline dw_die_ref
4837 lookup_type_die_strip_naming_typedef (tree type)
4839 dw_die_ref die = lookup_type_die (type);
4840 return strip_naming_typedef (type, die);
4843 /* Equate a DIE to a given type specifier. */
4845 static inline void
4846 equate_type_number_to_die (tree type, dw_die_ref type_die)
4848 TYPE_SYMTAB_DIE (type) = type_die;
4851 /* Returns a hash value for X (which really is a die_struct). */
4853 static hashval_t
4854 decl_die_table_hash (const void *x)
4856 return (hashval_t) ((const_dw_die_ref) x)->decl_id;
4859 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
4861 static int
4862 decl_die_table_eq (const void *x, const void *y)
4864 return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
4867 /* Return the DIE associated with a given declaration. */
4869 static inline dw_die_ref
4870 lookup_decl_die (tree decl)
4872 return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
4875 /* Returns a hash value for X (which really is a var_loc_list). */
4877 static hashval_t
4878 decl_loc_table_hash (const void *x)
4880 return (hashval_t) ((const var_loc_list *) x)->decl_id;
4883 /* Return nonzero if decl_id of var_loc_list X is the same as
4884 UID of decl *Y. */
4886 static int
4887 decl_loc_table_eq (const void *x, const void *y)
4889 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
4892 /* Return the var_loc list associated with a given declaration. */
4894 static inline var_loc_list *
4895 lookup_decl_loc (const_tree decl)
4897 if (!decl_loc_table)
4898 return NULL;
4899 return (var_loc_list *)
4900 htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
4903 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
4905 static hashval_t
4906 cached_dw_loc_list_table_hash (const void *x)
4908 return (hashval_t) ((const cached_dw_loc_list *) x)->decl_id;
4911 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
4912 UID of decl *Y. */
4914 static int
4915 cached_dw_loc_list_table_eq (const void *x, const void *y)
4917 return (((const cached_dw_loc_list *) x)->decl_id
4918 == DECL_UID ((const_tree) y));
4921 /* Equate a DIE to a particular declaration. */
4923 static void
4924 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
4926 unsigned int decl_id = DECL_UID (decl);
4927 void **slot;
4929 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
4930 *slot = decl_die;
4931 decl_die->decl_id = decl_id;
4934 /* Return how many bits covers PIECE EXPR_LIST. */
4936 static int
4937 decl_piece_bitsize (rtx piece)
4939 int ret = (int) GET_MODE (piece);
4940 if (ret)
4941 return ret;
4942 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
4943 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
4944 return INTVAL (XEXP (XEXP (piece, 0), 0));
4947 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
4949 static rtx *
4950 decl_piece_varloc_ptr (rtx piece)
4952 if ((int) GET_MODE (piece))
4953 return &XEXP (piece, 0);
4954 else
4955 return &XEXP (XEXP (piece, 0), 1);
4958 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
4959 Next is the chain of following piece nodes. */
4961 static rtx
4962 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
4964 if (bitsize <= (int) MAX_MACHINE_MODE)
4965 return alloc_EXPR_LIST (bitsize, loc_note, next);
4966 else
4967 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
4968 GEN_INT (bitsize),
4969 loc_note), next);
4972 /* Return rtx that should be stored into loc field for
4973 LOC_NOTE and BITPOS/BITSIZE. */
4975 static rtx
4976 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
4977 HOST_WIDE_INT bitsize)
4979 if (bitsize != -1)
4981 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
4982 if (bitpos != 0)
4983 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
4985 return loc_note;
4988 /* This function either modifies location piece list *DEST in
4989 place (if SRC and INNER is NULL), or copies location piece list
4990 *SRC to *DEST while modifying it. Location BITPOS is modified
4991 to contain LOC_NOTE, any pieces overlapping it are removed resp.
4992 not copied and if needed some padding around it is added.
4993 When modifying in place, DEST should point to EXPR_LIST where
4994 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
4995 to the start of the whole list and INNER points to the EXPR_LIST
4996 where earlier pieces cover PIECE_BITPOS bits. */
4998 static void
4999 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5000 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5001 HOST_WIDE_INT bitsize, rtx loc_note)
5003 int diff;
5004 bool copy = inner != NULL;
5006 if (copy)
5008 /* First copy all nodes preceding the current bitpos. */
5009 while (src != inner)
5011 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5012 decl_piece_bitsize (*src), NULL_RTX);
5013 dest = &XEXP (*dest, 1);
5014 src = &XEXP (*src, 1);
5017 /* Add padding if needed. */
5018 if (bitpos != piece_bitpos)
5020 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5021 copy ? NULL_RTX : *dest);
5022 dest = &XEXP (*dest, 1);
5024 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5026 gcc_assert (!copy);
5027 /* A piece with correct bitpos and bitsize already exist,
5028 just update the location for it and return. */
5029 *decl_piece_varloc_ptr (*dest) = loc_note;
5030 return;
5032 /* Add the piece that changed. */
5033 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5034 dest = &XEXP (*dest, 1);
5035 /* Skip over pieces that overlap it. */
5036 diff = bitpos - piece_bitpos + bitsize;
5037 if (!copy)
5038 src = dest;
5039 while (diff > 0 && *src)
5041 rtx piece = *src;
5042 diff -= decl_piece_bitsize (piece);
5043 if (copy)
5044 src = &XEXP (piece, 1);
5045 else
5047 *src = XEXP (piece, 1);
5048 free_EXPR_LIST_node (piece);
5051 /* Add padding if needed. */
5052 if (diff < 0 && *src)
5054 if (!copy)
5055 dest = src;
5056 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5057 dest = &XEXP (*dest, 1);
5059 if (!copy)
5060 return;
5061 /* Finally copy all nodes following it. */
5062 while (*src)
5064 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5065 decl_piece_bitsize (*src), NULL_RTX);
5066 dest = &XEXP (*dest, 1);
5067 src = &XEXP (*src, 1);
5071 /* Add a variable location node to the linked list for DECL. */
5073 static struct var_loc_node *
5074 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5076 unsigned int decl_id;
5077 var_loc_list *temp;
5078 void **slot;
5079 struct var_loc_node *loc = NULL;
5080 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5082 if (TREE_CODE (decl) == VAR_DECL
5083 && DECL_HAS_DEBUG_EXPR_P (decl))
5085 tree realdecl = DECL_DEBUG_EXPR (decl);
5086 if (handled_component_p (realdecl)
5087 || (TREE_CODE (realdecl) == MEM_REF
5088 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5090 HOST_WIDE_INT maxsize;
5091 tree innerdecl;
5092 innerdecl
5093 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5094 if (!DECL_P (innerdecl)
5095 || DECL_IGNORED_P (innerdecl)
5096 || TREE_STATIC (innerdecl)
5097 || bitsize <= 0
5098 || bitpos + bitsize > 256
5099 || bitsize != maxsize)
5100 return NULL;
5101 decl = innerdecl;
5105 decl_id = DECL_UID (decl);
5106 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5107 if (*slot == NULL)
5109 temp = ggc_alloc_cleared_var_loc_list ();
5110 temp->decl_id = decl_id;
5111 *slot = temp;
5113 else
5114 temp = (var_loc_list *) *slot;
5116 /* For PARM_DECLs try to keep around the original incoming value,
5117 even if that means we'll emit a zero-range .debug_loc entry. */
5118 if (temp->last
5119 && temp->first == temp->last
5120 && TREE_CODE (decl) == PARM_DECL
5121 && NOTE_P (temp->first->loc)
5122 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5123 && DECL_INCOMING_RTL (decl)
5124 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5125 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5126 == GET_CODE (DECL_INCOMING_RTL (decl))
5127 && prev_real_insn (temp->first->loc) == NULL_RTX
5128 && (bitsize != -1
5129 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5130 NOTE_VAR_LOCATION_LOC (loc_note))
5131 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5132 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5134 loc = ggc_alloc_cleared_var_loc_node ();
5135 temp->first->next = loc;
5136 temp->last = loc;
5137 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5139 else if (temp->last)
5141 struct var_loc_node *last = temp->last, *unused = NULL;
5142 rtx *piece_loc = NULL, last_loc_note;
5143 int piece_bitpos = 0;
5144 if (last->next)
5146 last = last->next;
5147 gcc_assert (last->next == NULL);
5149 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5151 piece_loc = &last->loc;
5154 int cur_bitsize = decl_piece_bitsize (*piece_loc);
5155 if (piece_bitpos + cur_bitsize > bitpos)
5156 break;
5157 piece_bitpos += cur_bitsize;
5158 piece_loc = &XEXP (*piece_loc, 1);
5160 while (*piece_loc);
5162 /* TEMP->LAST here is either pointer to the last but one or
5163 last element in the chained list, LAST is pointer to the
5164 last element. */
5165 if (label && strcmp (last->label, label) == 0)
5167 /* For SRA optimized variables if there weren't any real
5168 insns since last note, just modify the last node. */
5169 if (piece_loc != NULL)
5171 adjust_piece_list (piece_loc, NULL, NULL,
5172 bitpos, piece_bitpos, bitsize, loc_note);
5173 return NULL;
5175 /* If the last note doesn't cover any instructions, remove it. */
5176 if (temp->last != last)
5178 temp->last->next = NULL;
5179 unused = last;
5180 last = temp->last;
5181 gcc_assert (strcmp (last->label, label) != 0);
5183 else
5185 gcc_assert (temp->first == temp->last
5186 || (temp->first->next == temp->last
5187 && TREE_CODE (decl) == PARM_DECL));
5188 memset (temp->last, '\0', sizeof (*temp->last));
5189 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5190 return temp->last;
5193 if (bitsize == -1 && NOTE_P (last->loc))
5194 last_loc_note = last->loc;
5195 else if (piece_loc != NULL
5196 && *piece_loc != NULL_RTX
5197 && piece_bitpos == bitpos
5198 && decl_piece_bitsize (*piece_loc) == bitsize)
5199 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5200 else
5201 last_loc_note = NULL_RTX;
5202 /* If the current location is the same as the end of the list,
5203 and either both or neither of the locations is uninitialized,
5204 we have nothing to do. */
5205 if (last_loc_note == NULL_RTX
5206 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5207 NOTE_VAR_LOCATION_LOC (loc_note)))
5208 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5209 != NOTE_VAR_LOCATION_STATUS (loc_note))
5210 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5211 == VAR_INIT_STATUS_UNINITIALIZED)
5212 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5213 == VAR_INIT_STATUS_UNINITIALIZED))))
5215 /* Add LOC to the end of list and update LAST. If the last
5216 element of the list has been removed above, reuse its
5217 memory for the new node, otherwise allocate a new one. */
5218 if (unused)
5220 loc = unused;
5221 memset (loc, '\0', sizeof (*loc));
5223 else
5224 loc = ggc_alloc_cleared_var_loc_node ();
5225 if (bitsize == -1 || piece_loc == NULL)
5226 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5227 else
5228 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5229 bitpos, piece_bitpos, bitsize, loc_note);
5230 last->next = loc;
5231 /* Ensure TEMP->LAST will point either to the new last but one
5232 element of the chain, or to the last element in it. */
5233 if (last != temp->last)
5234 temp->last = last;
5236 else if (unused)
5237 ggc_free (unused);
5239 else
5241 loc = ggc_alloc_cleared_var_loc_node ();
5242 temp->first = loc;
5243 temp->last = loc;
5244 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5246 return loc;
5249 /* Keep track of the number of spaces used to indent the
5250 output of the debugging routines that print the structure of
5251 the DIE internal representation. */
5252 static int print_indent;
5254 /* Indent the line the number of spaces given by print_indent. */
5256 static inline void
5257 print_spaces (FILE *outfile)
5259 fprintf (outfile, "%*s", print_indent, "");
5262 /* Print a type signature in hex. */
5264 static inline void
5265 print_signature (FILE *outfile, char *sig)
5267 int i;
5269 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5270 fprintf (outfile, "%02x", sig[i] & 0xff);
5273 /* Print the information associated with a given DIE, and its children.
5274 This routine is a debugging aid only. */
5276 static void
5277 print_die (dw_die_ref die, FILE *outfile)
5279 dw_attr_ref a;
5280 dw_die_ref c;
5281 unsigned ix;
5283 print_spaces (outfile);
5284 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5285 die->die_offset, dwarf_tag_name (die->die_tag),
5286 (void*) die);
5287 print_spaces (outfile);
5288 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5289 fprintf (outfile, " offset: %ld", die->die_offset);
5290 fprintf (outfile, " mark: %d\n", die->die_mark);
5292 if (die->comdat_type_p)
5294 print_spaces (outfile);
5295 fprintf (outfile, " signature: ");
5296 print_signature (outfile, die->die_id.die_type_node->signature);
5297 fprintf (outfile, "\n");
5300 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5302 print_spaces (outfile);
5303 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5305 switch (AT_class (a))
5307 case dw_val_class_addr:
5308 fprintf (outfile, "address");
5309 break;
5310 case dw_val_class_offset:
5311 fprintf (outfile, "offset");
5312 break;
5313 case dw_val_class_loc:
5314 fprintf (outfile, "location descriptor");
5315 break;
5316 case dw_val_class_loc_list:
5317 fprintf (outfile, "location list -> label:%s",
5318 AT_loc_list (a)->ll_symbol);
5319 break;
5320 case dw_val_class_range_list:
5321 fprintf (outfile, "range list");
5322 break;
5323 case dw_val_class_const:
5324 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5325 break;
5326 case dw_val_class_unsigned_const:
5327 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5328 break;
5329 case dw_val_class_const_double:
5330 fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
5331 HOST_WIDE_INT_PRINT_UNSIGNED")",
5332 a->dw_attr_val.v.val_double.high,
5333 a->dw_attr_val.v.val_double.low);
5334 break;
5335 case dw_val_class_vec:
5336 fprintf (outfile, "floating-point or vector constant");
5337 break;
5338 case dw_val_class_flag:
5339 fprintf (outfile, "%u", AT_flag (a));
5340 break;
5341 case dw_val_class_die_ref:
5342 if (AT_ref (a) != NULL)
5344 if (AT_ref (a)->comdat_type_p)
5346 fprintf (outfile, "die -> signature: ");
5347 print_signature (outfile,
5348 AT_ref (a)->die_id.die_type_node->signature);
5350 else if (AT_ref (a)->die_id.die_symbol)
5351 fprintf (outfile, "die -> label: %s",
5352 AT_ref (a)->die_id.die_symbol);
5353 else
5354 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5355 fprintf (outfile, " (%p)", (void *) AT_ref (a));
5357 else
5358 fprintf (outfile, "die -> <null>");
5359 break;
5360 case dw_val_class_vms_delta:
5361 fprintf (outfile, "delta: @slotcount(%s-%s)",
5362 AT_vms_delta2 (a), AT_vms_delta1 (a));
5363 break;
5364 case dw_val_class_lbl_id:
5365 case dw_val_class_lineptr:
5366 case dw_val_class_macptr:
5367 case dw_val_class_high_pc:
5368 fprintf (outfile, "label: %s", AT_lbl (a));
5369 break;
5370 case dw_val_class_str:
5371 if (AT_string (a) != NULL)
5372 fprintf (outfile, "\"%s\"", AT_string (a));
5373 else
5374 fprintf (outfile, "<null>");
5375 break;
5376 case dw_val_class_file:
5377 fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5378 AT_file (a)->emitted_number);
5379 break;
5380 case dw_val_class_data8:
5382 int i;
5384 for (i = 0; i < 8; i++)
5385 fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
5386 break;
5388 default:
5389 break;
5392 fprintf (outfile, "\n");
5395 if (die->die_child != NULL)
5397 print_indent += 4;
5398 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5399 print_indent -= 4;
5401 if (print_indent == 0)
5402 fprintf (outfile, "\n");
5405 /* Print the information collected for a given DIE. */
5407 DEBUG_FUNCTION void
5408 debug_dwarf_die (dw_die_ref die)
5410 print_die (die, stderr);
5413 DEBUG_FUNCTION void
5414 debug (die_struct &ref)
5416 print_die (&ref, stderr);
5419 DEBUG_FUNCTION void
5420 debug (die_struct *ptr)
5422 if (ptr)
5423 debug (*ptr);
5424 else
5425 fprintf (stderr, "<nil>\n");
5429 /* Print all DWARF information collected for the compilation unit.
5430 This routine is a debugging aid only. */
5432 DEBUG_FUNCTION void
5433 debug_dwarf (void)
5435 print_indent = 0;
5436 print_die (comp_unit_die (), stderr);
5439 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5440 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5441 DIE that marks the start of the DIEs for this include file. */
5443 static dw_die_ref
5444 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5446 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5447 dw_die_ref new_unit = gen_compile_unit_die (filename);
5449 new_unit->die_sib = old_unit;
5450 return new_unit;
5453 /* Close an include-file CU and reopen the enclosing one. */
5455 static dw_die_ref
5456 pop_compile_unit (dw_die_ref old_unit)
5458 dw_die_ref new_unit = old_unit->die_sib;
5460 old_unit->die_sib = NULL;
5461 return new_unit;
5464 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5465 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5466 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5468 /* Calculate the checksum of a location expression. */
5470 static inline void
5471 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5473 int tem;
5474 hashval_t hash = 0;
5476 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5477 CHECKSUM (tem);
5478 hash = hash_loc_operands (loc, hash);
5479 CHECKSUM (hash);
5482 /* Calculate the checksum of an attribute. */
5484 static void
5485 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5487 dw_loc_descr_ref loc;
5488 rtx r;
5490 CHECKSUM (at->dw_attr);
5492 /* We don't care that this was compiled with a different compiler
5493 snapshot; if the output is the same, that's what matters. */
5494 if (at->dw_attr == DW_AT_producer)
5495 return;
5497 switch (AT_class (at))
5499 case dw_val_class_const:
5500 CHECKSUM (at->dw_attr_val.v.val_int);
5501 break;
5502 case dw_val_class_unsigned_const:
5503 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5504 break;
5505 case dw_val_class_const_double:
5506 CHECKSUM (at->dw_attr_val.v.val_double);
5507 break;
5508 case dw_val_class_vec:
5509 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5510 (at->dw_attr_val.v.val_vec.length
5511 * at->dw_attr_val.v.val_vec.elt_size));
5512 break;
5513 case dw_val_class_flag:
5514 CHECKSUM (at->dw_attr_val.v.val_flag);
5515 break;
5516 case dw_val_class_str:
5517 CHECKSUM_STRING (AT_string (at));
5518 break;
5520 case dw_val_class_addr:
5521 r = AT_addr (at);
5522 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5523 CHECKSUM_STRING (XSTR (r, 0));
5524 break;
5526 case dw_val_class_offset:
5527 CHECKSUM (at->dw_attr_val.v.val_offset);
5528 break;
5530 case dw_val_class_loc:
5531 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5532 loc_checksum (loc, ctx);
5533 break;
5535 case dw_val_class_die_ref:
5536 die_checksum (AT_ref (at), ctx, mark);
5537 break;
5539 case dw_val_class_fde_ref:
5540 case dw_val_class_vms_delta:
5541 case dw_val_class_lbl_id:
5542 case dw_val_class_lineptr:
5543 case dw_val_class_macptr:
5544 case dw_val_class_high_pc:
5545 break;
5547 case dw_val_class_file:
5548 CHECKSUM_STRING (AT_file (at)->filename);
5549 break;
5551 case dw_val_class_data8:
5552 CHECKSUM (at->dw_attr_val.v.val_data8);
5553 break;
5555 default:
5556 break;
5560 /* Calculate the checksum of a DIE. */
5562 static void
5563 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5565 dw_die_ref c;
5566 dw_attr_ref a;
5567 unsigned ix;
5569 /* To avoid infinite recursion. */
5570 if (die->die_mark)
5572 CHECKSUM (die->die_mark);
5573 return;
5575 die->die_mark = ++(*mark);
5577 CHECKSUM (die->die_tag);
5579 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5580 attr_checksum (a, ctx, mark);
5582 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5585 #undef CHECKSUM
5586 #undef CHECKSUM_BLOCK
5587 #undef CHECKSUM_STRING
5589 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
5590 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5591 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5592 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5593 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5594 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5595 #define CHECKSUM_ATTR(FOO) \
5596 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5598 /* Calculate the checksum of a number in signed LEB128 format. */
5600 static void
5601 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5603 unsigned char byte;
5604 bool more;
5606 while (1)
5608 byte = (value & 0x7f);
5609 value >>= 7;
5610 more = !((value == 0 && (byte & 0x40) == 0)
5611 || (value == -1 && (byte & 0x40) != 0));
5612 if (more)
5613 byte |= 0x80;
5614 CHECKSUM (byte);
5615 if (!more)
5616 break;
5620 /* Calculate the checksum of a number in unsigned LEB128 format. */
5622 static void
5623 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5625 while (1)
5627 unsigned char byte = (value & 0x7f);
5628 value >>= 7;
5629 if (value != 0)
5630 /* More bytes to follow. */
5631 byte |= 0x80;
5632 CHECKSUM (byte);
5633 if (value == 0)
5634 break;
5638 /* Checksum the context of the DIE. This adds the names of any
5639 surrounding namespaces or structures to the checksum. */
5641 static void
5642 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5644 const char *name;
5645 dw_die_ref spec;
5646 int tag = die->die_tag;
5648 if (tag != DW_TAG_namespace
5649 && tag != DW_TAG_structure_type
5650 && tag != DW_TAG_class_type)
5651 return;
5653 name = get_AT_string (die, DW_AT_name);
5655 spec = get_AT_ref (die, DW_AT_specification);
5656 if (spec != NULL)
5657 die = spec;
5659 if (die->die_parent != NULL)
5660 checksum_die_context (die->die_parent, ctx);
5662 CHECKSUM_ULEB128 ('C');
5663 CHECKSUM_ULEB128 (tag);
5664 if (name != NULL)
5665 CHECKSUM_STRING (name);
5668 /* Calculate the checksum of a location expression. */
5670 static inline void
5671 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5673 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5674 were emitted as a DW_FORM_sdata instead of a location expression. */
5675 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5677 CHECKSUM_ULEB128 (DW_FORM_sdata);
5678 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5679 return;
5682 /* Otherwise, just checksum the raw location expression. */
5683 while (loc != NULL)
5685 hashval_t hash = 0;
5687 CHECKSUM_ULEB128 (loc->dtprel);
5688 CHECKSUM_ULEB128 (loc->dw_loc_opc);
5689 hash = hash_loc_operands (loc, hash);
5690 CHECKSUM (hash);
5691 loc = loc->dw_loc_next;
5695 /* Calculate the checksum of an attribute. */
5697 static void
5698 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5699 struct md5_ctx *ctx, int *mark)
5701 dw_loc_descr_ref loc;
5702 rtx r;
5704 if (AT_class (at) == dw_val_class_die_ref)
5706 dw_die_ref target_die = AT_ref (at);
5708 /* For pointer and reference types, we checksum only the (qualified)
5709 name of the target type (if there is a name). For friend entries,
5710 we checksum only the (qualified) name of the target type or function.
5711 This allows the checksum to remain the same whether the target type
5712 is complete or not. */
5713 if ((at->dw_attr == DW_AT_type
5714 && (tag == DW_TAG_pointer_type
5715 || tag == DW_TAG_reference_type
5716 || tag == DW_TAG_rvalue_reference_type
5717 || tag == DW_TAG_ptr_to_member_type))
5718 || (at->dw_attr == DW_AT_friend
5719 && tag == DW_TAG_friend))
5721 dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5723 if (name_attr != NULL)
5725 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5727 if (decl == NULL)
5728 decl = target_die;
5729 CHECKSUM_ULEB128 ('N');
5730 CHECKSUM_ULEB128 (at->dw_attr);
5731 if (decl->die_parent != NULL)
5732 checksum_die_context (decl->die_parent, ctx);
5733 CHECKSUM_ULEB128 ('E');
5734 CHECKSUM_STRING (AT_string (name_attr));
5735 return;
5739 /* For all other references to another DIE, we check to see if the
5740 target DIE has already been visited. If it has, we emit a
5741 backward reference; if not, we descend recursively. */
5742 if (target_die->die_mark > 0)
5744 CHECKSUM_ULEB128 ('R');
5745 CHECKSUM_ULEB128 (at->dw_attr);
5746 CHECKSUM_ULEB128 (target_die->die_mark);
5748 else
5750 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5752 if (decl == NULL)
5753 decl = target_die;
5754 target_die->die_mark = ++(*mark);
5755 CHECKSUM_ULEB128 ('T');
5756 CHECKSUM_ULEB128 (at->dw_attr);
5757 if (decl->die_parent != NULL)
5758 checksum_die_context (decl->die_parent, ctx);
5759 die_checksum_ordered (target_die, ctx, mark);
5761 return;
5764 CHECKSUM_ULEB128 ('A');
5765 CHECKSUM_ULEB128 (at->dw_attr);
5767 switch (AT_class (at))
5769 case dw_val_class_const:
5770 CHECKSUM_ULEB128 (DW_FORM_sdata);
5771 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
5772 break;
5774 case dw_val_class_unsigned_const:
5775 CHECKSUM_ULEB128 (DW_FORM_sdata);
5776 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
5777 break;
5779 case dw_val_class_const_double:
5780 CHECKSUM_ULEB128 (DW_FORM_block);
5781 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
5782 CHECKSUM (at->dw_attr_val.v.val_double);
5783 break;
5785 case dw_val_class_vec:
5786 CHECKSUM_ULEB128 (DW_FORM_block);
5787 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
5788 * at->dw_attr_val.v.val_vec.elt_size);
5789 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5790 (at->dw_attr_val.v.val_vec.length
5791 * at->dw_attr_val.v.val_vec.elt_size));
5792 break;
5794 case dw_val_class_flag:
5795 CHECKSUM_ULEB128 (DW_FORM_flag);
5796 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
5797 break;
5799 case dw_val_class_str:
5800 CHECKSUM_ULEB128 (DW_FORM_string);
5801 CHECKSUM_STRING (AT_string (at));
5802 break;
5804 case dw_val_class_addr:
5805 r = AT_addr (at);
5806 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5807 CHECKSUM_ULEB128 (DW_FORM_string);
5808 CHECKSUM_STRING (XSTR (r, 0));
5809 break;
5811 case dw_val_class_offset:
5812 CHECKSUM_ULEB128 (DW_FORM_sdata);
5813 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
5814 break;
5816 case dw_val_class_loc:
5817 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5818 loc_checksum_ordered (loc, ctx);
5819 break;
5821 case dw_val_class_fde_ref:
5822 case dw_val_class_lbl_id:
5823 case dw_val_class_lineptr:
5824 case dw_val_class_macptr:
5825 case dw_val_class_high_pc:
5826 break;
5828 case dw_val_class_file:
5829 CHECKSUM_ULEB128 (DW_FORM_string);
5830 CHECKSUM_STRING (AT_file (at)->filename);
5831 break;
5833 case dw_val_class_data8:
5834 CHECKSUM (at->dw_attr_val.v.val_data8);
5835 break;
5837 default:
5838 break;
5842 struct checksum_attributes
5844 dw_attr_ref at_name;
5845 dw_attr_ref at_type;
5846 dw_attr_ref at_friend;
5847 dw_attr_ref at_accessibility;
5848 dw_attr_ref at_address_class;
5849 dw_attr_ref at_allocated;
5850 dw_attr_ref at_artificial;
5851 dw_attr_ref at_associated;
5852 dw_attr_ref at_binary_scale;
5853 dw_attr_ref at_bit_offset;
5854 dw_attr_ref at_bit_size;
5855 dw_attr_ref at_bit_stride;
5856 dw_attr_ref at_byte_size;
5857 dw_attr_ref at_byte_stride;
5858 dw_attr_ref at_const_value;
5859 dw_attr_ref at_containing_type;
5860 dw_attr_ref at_count;
5861 dw_attr_ref at_data_location;
5862 dw_attr_ref at_data_member_location;
5863 dw_attr_ref at_decimal_scale;
5864 dw_attr_ref at_decimal_sign;
5865 dw_attr_ref at_default_value;
5866 dw_attr_ref at_digit_count;
5867 dw_attr_ref at_discr;
5868 dw_attr_ref at_discr_list;
5869 dw_attr_ref at_discr_value;
5870 dw_attr_ref at_encoding;
5871 dw_attr_ref at_endianity;
5872 dw_attr_ref at_explicit;
5873 dw_attr_ref at_is_optional;
5874 dw_attr_ref at_location;
5875 dw_attr_ref at_lower_bound;
5876 dw_attr_ref at_mutable;
5877 dw_attr_ref at_ordering;
5878 dw_attr_ref at_picture_string;
5879 dw_attr_ref at_prototyped;
5880 dw_attr_ref at_small;
5881 dw_attr_ref at_segment;
5882 dw_attr_ref at_string_length;
5883 dw_attr_ref at_threads_scaled;
5884 dw_attr_ref at_upper_bound;
5885 dw_attr_ref at_use_location;
5886 dw_attr_ref at_use_UTF8;
5887 dw_attr_ref at_variable_parameter;
5888 dw_attr_ref at_virtuality;
5889 dw_attr_ref at_visibility;
5890 dw_attr_ref at_vtable_elem_location;
5893 /* Collect the attributes that we will want to use for the checksum. */
5895 static void
5896 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
5898 dw_attr_ref a;
5899 unsigned ix;
5901 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5903 switch (a->dw_attr)
5905 case DW_AT_name:
5906 attrs->at_name = a;
5907 break;
5908 case DW_AT_type:
5909 attrs->at_type = a;
5910 break;
5911 case DW_AT_friend:
5912 attrs->at_friend = a;
5913 break;
5914 case DW_AT_accessibility:
5915 attrs->at_accessibility = a;
5916 break;
5917 case DW_AT_address_class:
5918 attrs->at_address_class = a;
5919 break;
5920 case DW_AT_allocated:
5921 attrs->at_allocated = a;
5922 break;
5923 case DW_AT_artificial:
5924 attrs->at_artificial = a;
5925 break;
5926 case DW_AT_associated:
5927 attrs->at_associated = a;
5928 break;
5929 case DW_AT_binary_scale:
5930 attrs->at_binary_scale = a;
5931 break;
5932 case DW_AT_bit_offset:
5933 attrs->at_bit_offset = a;
5934 break;
5935 case DW_AT_bit_size:
5936 attrs->at_bit_size = a;
5937 break;
5938 case DW_AT_bit_stride:
5939 attrs->at_bit_stride = a;
5940 break;
5941 case DW_AT_byte_size:
5942 attrs->at_byte_size = a;
5943 break;
5944 case DW_AT_byte_stride:
5945 attrs->at_byte_stride = a;
5946 break;
5947 case DW_AT_const_value:
5948 attrs->at_const_value = a;
5949 break;
5950 case DW_AT_containing_type:
5951 attrs->at_containing_type = a;
5952 break;
5953 case DW_AT_count:
5954 attrs->at_count = a;
5955 break;
5956 case DW_AT_data_location:
5957 attrs->at_data_location = a;
5958 break;
5959 case DW_AT_data_member_location:
5960 attrs->at_data_member_location = a;
5961 break;
5962 case DW_AT_decimal_scale:
5963 attrs->at_decimal_scale = a;
5964 break;
5965 case DW_AT_decimal_sign:
5966 attrs->at_decimal_sign = a;
5967 break;
5968 case DW_AT_default_value:
5969 attrs->at_default_value = a;
5970 break;
5971 case DW_AT_digit_count:
5972 attrs->at_digit_count = a;
5973 break;
5974 case DW_AT_discr:
5975 attrs->at_discr = a;
5976 break;
5977 case DW_AT_discr_list:
5978 attrs->at_discr_list = a;
5979 break;
5980 case DW_AT_discr_value:
5981 attrs->at_discr_value = a;
5982 break;
5983 case DW_AT_encoding:
5984 attrs->at_encoding = a;
5985 break;
5986 case DW_AT_endianity:
5987 attrs->at_endianity = a;
5988 break;
5989 case DW_AT_explicit:
5990 attrs->at_explicit = a;
5991 break;
5992 case DW_AT_is_optional:
5993 attrs->at_is_optional = a;
5994 break;
5995 case DW_AT_location:
5996 attrs->at_location = a;
5997 break;
5998 case DW_AT_lower_bound:
5999 attrs->at_lower_bound = a;
6000 break;
6001 case DW_AT_mutable:
6002 attrs->at_mutable = a;
6003 break;
6004 case DW_AT_ordering:
6005 attrs->at_ordering = a;
6006 break;
6007 case DW_AT_picture_string:
6008 attrs->at_picture_string = a;
6009 break;
6010 case DW_AT_prototyped:
6011 attrs->at_prototyped = a;
6012 break;
6013 case DW_AT_small:
6014 attrs->at_small = a;
6015 break;
6016 case DW_AT_segment:
6017 attrs->at_segment = a;
6018 break;
6019 case DW_AT_string_length:
6020 attrs->at_string_length = a;
6021 break;
6022 case DW_AT_threads_scaled:
6023 attrs->at_threads_scaled = a;
6024 break;
6025 case DW_AT_upper_bound:
6026 attrs->at_upper_bound = a;
6027 break;
6028 case DW_AT_use_location:
6029 attrs->at_use_location = a;
6030 break;
6031 case DW_AT_use_UTF8:
6032 attrs->at_use_UTF8 = a;
6033 break;
6034 case DW_AT_variable_parameter:
6035 attrs->at_variable_parameter = a;
6036 break;
6037 case DW_AT_virtuality:
6038 attrs->at_virtuality = a;
6039 break;
6040 case DW_AT_visibility:
6041 attrs->at_visibility = a;
6042 break;
6043 case DW_AT_vtable_elem_location:
6044 attrs->at_vtable_elem_location = a;
6045 break;
6046 default:
6047 break;
6052 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6054 static void
6055 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6057 dw_die_ref c;
6058 dw_die_ref decl;
6059 struct checksum_attributes attrs;
6061 CHECKSUM_ULEB128 ('D');
6062 CHECKSUM_ULEB128 (die->die_tag);
6064 memset (&attrs, 0, sizeof (attrs));
6066 decl = get_AT_ref (die, DW_AT_specification);
6067 if (decl != NULL)
6068 collect_checksum_attributes (&attrs, decl);
6069 collect_checksum_attributes (&attrs, die);
6071 CHECKSUM_ATTR (attrs.at_name);
6072 CHECKSUM_ATTR (attrs.at_accessibility);
6073 CHECKSUM_ATTR (attrs.at_address_class);
6074 CHECKSUM_ATTR (attrs.at_allocated);
6075 CHECKSUM_ATTR (attrs.at_artificial);
6076 CHECKSUM_ATTR (attrs.at_associated);
6077 CHECKSUM_ATTR (attrs.at_binary_scale);
6078 CHECKSUM_ATTR (attrs.at_bit_offset);
6079 CHECKSUM_ATTR (attrs.at_bit_size);
6080 CHECKSUM_ATTR (attrs.at_bit_stride);
6081 CHECKSUM_ATTR (attrs.at_byte_size);
6082 CHECKSUM_ATTR (attrs.at_byte_stride);
6083 CHECKSUM_ATTR (attrs.at_const_value);
6084 CHECKSUM_ATTR (attrs.at_containing_type);
6085 CHECKSUM_ATTR (attrs.at_count);
6086 CHECKSUM_ATTR (attrs.at_data_location);
6087 CHECKSUM_ATTR (attrs.at_data_member_location);
6088 CHECKSUM_ATTR (attrs.at_decimal_scale);
6089 CHECKSUM_ATTR (attrs.at_decimal_sign);
6090 CHECKSUM_ATTR (attrs.at_default_value);
6091 CHECKSUM_ATTR (attrs.at_digit_count);
6092 CHECKSUM_ATTR (attrs.at_discr);
6093 CHECKSUM_ATTR (attrs.at_discr_list);
6094 CHECKSUM_ATTR (attrs.at_discr_value);
6095 CHECKSUM_ATTR (attrs.at_encoding);
6096 CHECKSUM_ATTR (attrs.at_endianity);
6097 CHECKSUM_ATTR (attrs.at_explicit);
6098 CHECKSUM_ATTR (attrs.at_is_optional);
6099 CHECKSUM_ATTR (attrs.at_location);
6100 CHECKSUM_ATTR (attrs.at_lower_bound);
6101 CHECKSUM_ATTR (attrs.at_mutable);
6102 CHECKSUM_ATTR (attrs.at_ordering);
6103 CHECKSUM_ATTR (attrs.at_picture_string);
6104 CHECKSUM_ATTR (attrs.at_prototyped);
6105 CHECKSUM_ATTR (attrs.at_small);
6106 CHECKSUM_ATTR (attrs.at_segment);
6107 CHECKSUM_ATTR (attrs.at_string_length);
6108 CHECKSUM_ATTR (attrs.at_threads_scaled);
6109 CHECKSUM_ATTR (attrs.at_upper_bound);
6110 CHECKSUM_ATTR (attrs.at_use_location);
6111 CHECKSUM_ATTR (attrs.at_use_UTF8);
6112 CHECKSUM_ATTR (attrs.at_variable_parameter);
6113 CHECKSUM_ATTR (attrs.at_virtuality);
6114 CHECKSUM_ATTR (attrs.at_visibility);
6115 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6116 CHECKSUM_ATTR (attrs.at_type);
6117 CHECKSUM_ATTR (attrs.at_friend);
6119 /* Checksum the child DIEs. */
6120 c = die->die_child;
6121 if (c) do {
6122 dw_attr_ref name_attr;
6124 c = c->die_sib;
6125 name_attr = get_AT (c, DW_AT_name);
6126 if (is_template_instantiation (c))
6128 /* Ignore instantiations of member type and function templates. */
6130 else if (name_attr != NULL
6131 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6133 /* Use a shallow checksum for named nested types and member
6134 functions. */
6135 CHECKSUM_ULEB128 ('S');
6136 CHECKSUM_ULEB128 (c->die_tag);
6137 CHECKSUM_STRING (AT_string (name_attr));
6139 else
6141 /* Use a deep checksum for other children. */
6142 /* Mark this DIE so it gets processed when unmarking. */
6143 if (c->die_mark == 0)
6144 c->die_mark = -1;
6145 die_checksum_ordered (c, ctx, mark);
6147 } while (c != die->die_child);
6149 CHECKSUM_ULEB128 (0);
6152 /* Add a type name and tag to a hash. */
6153 static void
6154 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6156 CHECKSUM_ULEB128 (tag);
6157 CHECKSUM_STRING (name);
6160 #undef CHECKSUM
6161 #undef CHECKSUM_STRING
6162 #undef CHECKSUM_ATTR
6163 #undef CHECKSUM_LEB128
6164 #undef CHECKSUM_ULEB128
6166 /* Generate the type signature for DIE. This is computed by generating an
6167 MD5 checksum over the DIE's tag, its relevant attributes, and its
6168 children. Attributes that are references to other DIEs are processed
6169 by recursion, using the MARK field to prevent infinite recursion.
6170 If the DIE is nested inside a namespace or another type, we also
6171 need to include that context in the signature. The lower 64 bits
6172 of the resulting MD5 checksum comprise the signature. */
6174 static void
6175 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6177 int mark;
6178 const char *name;
6179 unsigned char checksum[16];
6180 struct md5_ctx ctx;
6181 dw_die_ref decl;
6182 dw_die_ref parent;
6184 name = get_AT_string (die, DW_AT_name);
6185 decl = get_AT_ref (die, DW_AT_specification);
6186 parent = get_die_parent (die);
6188 /* First, compute a signature for just the type name (and its surrounding
6189 context, if any. This is stored in the type unit DIE for link-time
6190 ODR (one-definition rule) checking. */
6192 if (is_cxx () && name != NULL)
6194 md5_init_ctx (&ctx);
6196 /* Checksum the names of surrounding namespaces and structures. */
6197 if (parent != NULL)
6198 checksum_die_context (parent, &ctx);
6200 /* Checksum the current DIE. */
6201 die_odr_checksum (die->die_tag, name, &ctx);
6202 md5_finish_ctx (&ctx, checksum);
6204 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6207 /* Next, compute the complete type signature. */
6209 md5_init_ctx (&ctx);
6210 mark = 1;
6211 die->die_mark = mark;
6213 /* Checksum the names of surrounding namespaces and structures. */
6214 if (parent != NULL)
6215 checksum_die_context (parent, &ctx);
6217 /* Checksum the DIE and its children. */
6218 die_checksum_ordered (die, &ctx, &mark);
6219 unmark_all_dies (die);
6220 md5_finish_ctx (&ctx, checksum);
6222 /* Store the signature in the type node and link the type DIE and the
6223 type node together. */
6224 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6225 DWARF_TYPE_SIGNATURE_SIZE);
6226 die->comdat_type_p = true;
6227 die->die_id.die_type_node = type_node;
6228 type_node->type_die = die;
6230 /* If the DIE is a specification, link its declaration to the type node
6231 as well. */
6232 if (decl != NULL)
6234 decl->comdat_type_p = true;
6235 decl->die_id.die_type_node = type_node;
6239 /* Do the location expressions look same? */
6240 static inline int
6241 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6243 return loc1->dw_loc_opc == loc2->dw_loc_opc
6244 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6245 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6248 /* Do the values look the same? */
6249 static int
6250 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6252 dw_loc_descr_ref loc1, loc2;
6253 rtx r1, r2;
6255 if (v1->val_class != v2->val_class)
6256 return 0;
6258 switch (v1->val_class)
6260 case dw_val_class_const:
6261 return v1->v.val_int == v2->v.val_int;
6262 case dw_val_class_unsigned_const:
6263 return v1->v.val_unsigned == v2->v.val_unsigned;
6264 case dw_val_class_const_double:
6265 return v1->v.val_double.high == v2->v.val_double.high
6266 && v1->v.val_double.low == v2->v.val_double.low;
6267 case dw_val_class_vec:
6268 if (v1->v.val_vec.length != v2->v.val_vec.length
6269 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6270 return 0;
6271 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6272 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6273 return 0;
6274 return 1;
6275 case dw_val_class_flag:
6276 return v1->v.val_flag == v2->v.val_flag;
6277 case dw_val_class_str:
6278 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6280 case dw_val_class_addr:
6281 r1 = v1->v.val_addr;
6282 r2 = v2->v.val_addr;
6283 if (GET_CODE (r1) != GET_CODE (r2))
6284 return 0;
6285 return !rtx_equal_p (r1, r2);
6287 case dw_val_class_offset:
6288 return v1->v.val_offset == v2->v.val_offset;
6290 case dw_val_class_loc:
6291 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6292 loc1 && loc2;
6293 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6294 if (!same_loc_p (loc1, loc2, mark))
6295 return 0;
6296 return !loc1 && !loc2;
6298 case dw_val_class_die_ref:
6299 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6301 case dw_val_class_fde_ref:
6302 case dw_val_class_vms_delta:
6303 case dw_val_class_lbl_id:
6304 case dw_val_class_lineptr:
6305 case dw_val_class_macptr:
6306 case dw_val_class_high_pc:
6307 return 1;
6309 case dw_val_class_file:
6310 return v1->v.val_file == v2->v.val_file;
6312 case dw_val_class_data8:
6313 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6315 default:
6316 return 1;
6320 /* Do the attributes look the same? */
6322 static int
6323 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6325 if (at1->dw_attr != at2->dw_attr)
6326 return 0;
6328 /* We don't care that this was compiled with a different compiler
6329 snapshot; if the output is the same, that's what matters. */
6330 if (at1->dw_attr == DW_AT_producer)
6331 return 1;
6333 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6336 /* Do the dies look the same? */
6338 static int
6339 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6341 dw_die_ref c1, c2;
6342 dw_attr_ref a1;
6343 unsigned ix;
6345 /* To avoid infinite recursion. */
6346 if (die1->die_mark)
6347 return die1->die_mark == die2->die_mark;
6348 die1->die_mark = die2->die_mark = ++(*mark);
6350 if (die1->die_tag != die2->die_tag)
6351 return 0;
6353 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6354 return 0;
6356 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6357 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6358 return 0;
6360 c1 = die1->die_child;
6361 c2 = die2->die_child;
6362 if (! c1)
6364 if (c2)
6365 return 0;
6367 else
6368 for (;;)
6370 if (!same_die_p (c1, c2, mark))
6371 return 0;
6372 c1 = c1->die_sib;
6373 c2 = c2->die_sib;
6374 if (c1 == die1->die_child)
6376 if (c2 == die2->die_child)
6377 break;
6378 else
6379 return 0;
6383 return 1;
6386 /* Do the dies look the same? Wrapper around same_die_p. */
6388 static int
6389 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6391 int mark = 0;
6392 int ret = same_die_p (die1, die2, &mark);
6394 unmark_all_dies (die1);
6395 unmark_all_dies (die2);
6397 return ret;
6400 /* The prefix to attach to symbols on DIEs in the current comdat debug
6401 info section. */
6402 static const char *comdat_symbol_id;
6404 /* The index of the current symbol within the current comdat CU. */
6405 static unsigned int comdat_symbol_number;
6407 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6408 children, and set comdat_symbol_id accordingly. */
6410 static void
6411 compute_section_prefix (dw_die_ref unit_die)
6413 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6414 const char *base = die_name ? lbasename (die_name) : "anonymous";
6415 char *name = XALLOCAVEC (char, strlen (base) + 64);
6416 char *p;
6417 int i, mark;
6418 unsigned char checksum[16];
6419 struct md5_ctx ctx;
6421 /* Compute the checksum of the DIE, then append part of it as hex digits to
6422 the name filename of the unit. */
6424 md5_init_ctx (&ctx);
6425 mark = 0;
6426 die_checksum (unit_die, &ctx, &mark);
6427 unmark_all_dies (unit_die);
6428 md5_finish_ctx (&ctx, checksum);
6430 sprintf (name, "%s.", base);
6431 clean_symbol_name (name);
6433 p = name + strlen (name);
6434 for (i = 0; i < 4; i++)
6436 sprintf (p, "%.2x", checksum[i]);
6437 p += 2;
6440 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6441 comdat_symbol_number = 0;
6444 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6446 static int
6447 is_type_die (dw_die_ref die)
6449 switch (die->die_tag)
6451 case DW_TAG_array_type:
6452 case DW_TAG_class_type:
6453 case DW_TAG_interface_type:
6454 case DW_TAG_enumeration_type:
6455 case DW_TAG_pointer_type:
6456 case DW_TAG_reference_type:
6457 case DW_TAG_rvalue_reference_type:
6458 case DW_TAG_string_type:
6459 case DW_TAG_structure_type:
6460 case DW_TAG_subroutine_type:
6461 case DW_TAG_union_type:
6462 case DW_TAG_ptr_to_member_type:
6463 case DW_TAG_set_type:
6464 case DW_TAG_subrange_type:
6465 case DW_TAG_base_type:
6466 case DW_TAG_const_type:
6467 case DW_TAG_file_type:
6468 case DW_TAG_packed_type:
6469 case DW_TAG_volatile_type:
6470 case DW_TAG_typedef:
6471 return 1;
6472 default:
6473 return 0;
6477 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6478 Basically, we want to choose the bits that are likely to be shared between
6479 compilations (types) and leave out the bits that are specific to individual
6480 compilations (functions). */
6482 static int
6483 is_comdat_die (dw_die_ref c)
6485 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6486 we do for stabs. The advantage is a greater likelihood of sharing between
6487 objects that don't include headers in the same order (and therefore would
6488 put the base types in a different comdat). jason 8/28/00 */
6490 if (c->die_tag == DW_TAG_base_type)
6491 return 0;
6493 if (c->die_tag == DW_TAG_pointer_type
6494 || c->die_tag == DW_TAG_reference_type
6495 || c->die_tag == DW_TAG_rvalue_reference_type
6496 || c->die_tag == DW_TAG_const_type
6497 || c->die_tag == DW_TAG_volatile_type)
6499 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6501 return t ? is_comdat_die (t) : 0;
6504 return is_type_die (c);
6507 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6508 compilation unit. */
6510 static int
6511 is_symbol_die (dw_die_ref c)
6513 return (is_type_die (c)
6514 || is_declaration_die (c)
6515 || c->die_tag == DW_TAG_namespace
6516 || c->die_tag == DW_TAG_module);
6519 /* Returns true iff C is a compile-unit DIE. */
6521 static inline bool
6522 is_cu_die (dw_die_ref c)
6524 return c && c->die_tag == DW_TAG_compile_unit;
6527 /* Returns true iff C is a unit DIE of some sort. */
6529 static inline bool
6530 is_unit_die (dw_die_ref c)
6532 return c && (c->die_tag == DW_TAG_compile_unit
6533 || c->die_tag == DW_TAG_partial_unit
6534 || c->die_tag == DW_TAG_type_unit);
6537 /* Returns true iff C is a namespace DIE. */
6539 static inline bool
6540 is_namespace_die (dw_die_ref c)
6542 return c && c->die_tag == DW_TAG_namespace;
6545 /* Returns true iff C is a class or structure DIE. */
6547 static inline bool
6548 is_class_die (dw_die_ref c)
6550 return c && (c->die_tag == DW_TAG_class_type
6551 || c->die_tag == DW_TAG_structure_type);
6554 /* Return non-zero if this DIE is a template parameter. */
6556 static inline bool
6557 is_template_parameter (dw_die_ref die)
6559 switch (die->die_tag)
6561 case DW_TAG_template_type_param:
6562 case DW_TAG_template_value_param:
6563 case DW_TAG_GNU_template_template_param:
6564 case DW_TAG_GNU_template_parameter_pack:
6565 return true;
6566 default:
6567 return false;
6571 /* Return non-zero if this DIE represents a template instantiation. */
6573 static inline bool
6574 is_template_instantiation (dw_die_ref die)
6576 dw_die_ref c;
6578 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
6579 return false;
6580 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
6581 return false;
6584 static char *
6585 gen_internal_sym (const char *prefix)
6587 char buf[256];
6589 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6590 return xstrdup (buf);
6593 /* Assign symbols to all worthy DIEs under DIE. */
6595 static void
6596 assign_symbol_names (dw_die_ref die)
6598 dw_die_ref c;
6600 if (is_symbol_die (die) && !die->comdat_type_p)
6602 if (comdat_symbol_id)
6604 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6606 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6607 comdat_symbol_id, comdat_symbol_number++);
6608 die->die_id.die_symbol = xstrdup (p);
6610 else
6611 die->die_id.die_symbol = gen_internal_sym ("LDIE");
6614 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6617 struct cu_hash_table_entry
6619 dw_die_ref cu;
6620 unsigned min_comdat_num, max_comdat_num;
6621 struct cu_hash_table_entry *next;
6624 /* Helpers to manipulate hash table of CUs. */
6626 struct cu_hash_table_entry_hasher
6628 typedef cu_hash_table_entry value_type;
6629 typedef die_struct compare_type;
6630 static inline hashval_t hash (const value_type *);
6631 static inline bool equal (const value_type *, const compare_type *);
6632 static inline void remove (value_type *);
6635 inline hashval_t
6636 cu_hash_table_entry_hasher::hash (const value_type *entry)
6638 return htab_hash_string (entry->cu->die_id.die_symbol);
6641 inline bool
6642 cu_hash_table_entry_hasher::equal (const value_type *entry1,
6643 const compare_type *entry2)
6645 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6648 inline void
6649 cu_hash_table_entry_hasher::remove (value_type *entry)
6651 struct cu_hash_table_entry *next;
6653 while (entry)
6655 next = entry->next;
6656 free (entry);
6657 entry = next;
6661 typedef hash_table <cu_hash_table_entry_hasher> cu_hash_type;
6663 /* Check whether we have already seen this CU and set up SYM_NUM
6664 accordingly. */
6665 static int
6666 check_duplicate_cu (dw_die_ref cu, cu_hash_type htable, unsigned int *sym_num)
6668 struct cu_hash_table_entry dummy;
6669 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6671 dummy.max_comdat_num = 0;
6673 slot = htable.find_slot_with_hash (cu,
6674 htab_hash_string (cu->die_id.die_symbol),
6675 INSERT);
6676 entry = *slot;
6678 for (; entry; last = entry, entry = entry->next)
6680 if (same_die_p_wrap (cu, entry->cu))
6681 break;
6684 if (entry)
6686 *sym_num = entry->min_comdat_num;
6687 return 1;
6690 entry = XCNEW (struct cu_hash_table_entry);
6691 entry->cu = cu;
6692 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6693 entry->next = *slot;
6694 *slot = entry;
6696 return 0;
6699 /* Record SYM_NUM to record of CU in HTABLE. */
6700 static void
6701 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type htable,
6702 unsigned int sym_num)
6704 struct cu_hash_table_entry **slot, *entry;
6706 slot = htable.find_slot_with_hash (cu,
6707 htab_hash_string (cu->die_id.die_symbol),
6708 NO_INSERT);
6709 entry = *slot;
6711 entry->max_comdat_num = sym_num;
6714 /* Traverse the DIE (which is always comp_unit_die), and set up
6715 additional compilation units for each of the include files we see
6716 bracketed by BINCL/EINCL. */
6718 static void
6719 break_out_includes (dw_die_ref die)
6721 dw_die_ref c;
6722 dw_die_ref unit = NULL;
6723 limbo_die_node *node, **pnode;
6724 cu_hash_type cu_hash_table;
6726 c = die->die_child;
6727 if (c) do {
6728 dw_die_ref prev = c;
6729 c = c->die_sib;
6730 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6731 || (unit && is_comdat_die (c)))
6733 dw_die_ref next = c->die_sib;
6735 /* This DIE is for a secondary CU; remove it from the main one. */
6736 remove_child_with_prev (c, prev);
6738 if (c->die_tag == DW_TAG_GNU_BINCL)
6739 unit = push_new_compile_unit (unit, c);
6740 else if (c->die_tag == DW_TAG_GNU_EINCL)
6741 unit = pop_compile_unit (unit);
6742 else
6743 add_child_die (unit, c);
6744 c = next;
6745 if (c == die->die_child)
6746 break;
6748 } while (c != die->die_child);
6750 #if 0
6751 /* We can only use this in debugging, since the frontend doesn't check
6752 to make sure that we leave every include file we enter. */
6753 gcc_assert (!unit);
6754 #endif
6756 assign_symbol_names (die);
6757 cu_hash_table.create (10);
6758 for (node = limbo_die_list, pnode = &limbo_die_list;
6759 node;
6760 node = node->next)
6762 int is_dupl;
6764 compute_section_prefix (node->die);
6765 is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6766 &comdat_symbol_number);
6767 assign_symbol_names (node->die);
6768 if (is_dupl)
6769 *pnode = node->next;
6770 else
6772 pnode = &node->next;
6773 record_comdat_symbol_number (node->die, cu_hash_table,
6774 comdat_symbol_number);
6777 cu_hash_table.dispose ();
6780 /* Return non-zero if this DIE is a declaration. */
6782 static int
6783 is_declaration_die (dw_die_ref die)
6785 dw_attr_ref a;
6786 unsigned ix;
6788 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6789 if (a->dw_attr == DW_AT_declaration)
6790 return 1;
6792 return 0;
6795 /* Return non-zero if this DIE is nested inside a subprogram. */
6797 static int
6798 is_nested_in_subprogram (dw_die_ref die)
6800 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
6802 if (decl == NULL)
6803 decl = die;
6804 return local_scope_p (decl);
6807 /* Return non-zero if this DIE contains a defining declaration of a
6808 subprogram. */
6810 static int
6811 contains_subprogram_definition (dw_die_ref die)
6813 dw_die_ref c;
6815 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
6816 return 1;
6817 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
6818 return 0;
6821 /* Return non-zero if this is a type DIE that should be moved to a
6822 COMDAT .debug_types section. */
6824 static int
6825 should_move_die_to_comdat (dw_die_ref die)
6827 switch (die->die_tag)
6829 case DW_TAG_class_type:
6830 case DW_TAG_structure_type:
6831 case DW_TAG_enumeration_type:
6832 case DW_TAG_union_type:
6833 /* Don't move declarations, inlined instances, or types nested in a
6834 subprogram. */
6835 if (is_declaration_die (die)
6836 || get_AT (die, DW_AT_abstract_origin)
6837 || is_nested_in_subprogram (die))
6838 return 0;
6839 /* A type definition should never contain a subprogram definition. */
6840 gcc_assert (!contains_subprogram_definition (die));
6841 return 1;
6842 case DW_TAG_array_type:
6843 case DW_TAG_interface_type:
6844 case DW_TAG_pointer_type:
6845 case DW_TAG_reference_type:
6846 case DW_TAG_rvalue_reference_type:
6847 case DW_TAG_string_type:
6848 case DW_TAG_subroutine_type:
6849 case DW_TAG_ptr_to_member_type:
6850 case DW_TAG_set_type:
6851 case DW_TAG_subrange_type:
6852 case DW_TAG_base_type:
6853 case DW_TAG_const_type:
6854 case DW_TAG_file_type:
6855 case DW_TAG_packed_type:
6856 case DW_TAG_volatile_type:
6857 case DW_TAG_typedef:
6858 default:
6859 return 0;
6863 /* Make a clone of DIE. */
6865 static dw_die_ref
6866 clone_die (dw_die_ref die)
6868 dw_die_ref clone;
6869 dw_attr_ref a;
6870 unsigned ix;
6872 clone = ggc_alloc_cleared_die_node ();
6873 clone->die_tag = die->die_tag;
6875 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6876 add_dwarf_attr (clone, a);
6878 return clone;
6881 /* Make a clone of the tree rooted at DIE. */
6883 static dw_die_ref
6884 clone_tree (dw_die_ref die)
6886 dw_die_ref c;
6887 dw_die_ref clone = clone_die (die);
6889 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
6891 return clone;
6894 /* Make a clone of DIE as a declaration. */
6896 static dw_die_ref
6897 clone_as_declaration (dw_die_ref die)
6899 dw_die_ref clone;
6900 dw_die_ref decl;
6901 dw_attr_ref a;
6902 unsigned ix;
6904 /* If the DIE is already a declaration, just clone it. */
6905 if (is_declaration_die (die))
6906 return clone_die (die);
6908 /* If the DIE is a specification, just clone its declaration DIE. */
6909 decl = get_AT_ref (die, DW_AT_specification);
6910 if (decl != NULL)
6912 clone = clone_die (decl);
6913 if (die->comdat_type_p)
6914 add_AT_die_ref (clone, DW_AT_signature, die);
6915 return clone;
6918 clone = ggc_alloc_cleared_die_node ();
6919 clone->die_tag = die->die_tag;
6921 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6923 /* We don't want to copy over all attributes.
6924 For example we don't want DW_AT_byte_size because otherwise we will no
6925 longer have a declaration and GDB will treat it as a definition. */
6927 switch (a->dw_attr)
6929 case DW_AT_artificial:
6930 case DW_AT_containing_type:
6931 case DW_AT_external:
6932 case DW_AT_name:
6933 case DW_AT_type:
6934 case DW_AT_virtuality:
6935 case DW_AT_linkage_name:
6936 case DW_AT_MIPS_linkage_name:
6937 add_dwarf_attr (clone, a);
6938 break;
6939 case DW_AT_byte_size:
6940 default:
6941 break;
6945 if (die->comdat_type_p)
6946 add_AT_die_ref (clone, DW_AT_signature, die);
6948 add_AT_flag (clone, DW_AT_declaration, 1);
6949 return clone;
6953 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
6955 struct decl_table_entry
6957 dw_die_ref orig;
6958 dw_die_ref copy;
6961 /* Helpers to manipulate hash table of copied declarations. */
6963 /* Hashtable helpers. */
6965 struct decl_table_entry_hasher : typed_free_remove <decl_table_entry>
6967 typedef decl_table_entry value_type;
6968 typedef die_struct compare_type;
6969 static inline hashval_t hash (const value_type *);
6970 static inline bool equal (const value_type *, const compare_type *);
6973 inline hashval_t
6974 decl_table_entry_hasher::hash (const value_type *entry)
6976 return htab_hash_pointer (entry->orig);
6979 inline bool
6980 decl_table_entry_hasher::equal (const value_type *entry1,
6981 const compare_type *entry2)
6983 return entry1->orig == entry2;
6986 typedef hash_table <decl_table_entry_hasher> decl_hash_type;
6988 /* Copy DIE and its ancestors, up to, but not including, the compile unit
6989 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
6990 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
6991 to check if the ancestor has already been copied into UNIT. */
6993 static dw_die_ref
6994 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, decl_hash_type decl_table)
6996 dw_die_ref parent = die->die_parent;
6997 dw_die_ref new_parent = unit;
6998 dw_die_ref copy;
6999 decl_table_entry **slot = NULL;
7000 struct decl_table_entry *entry = NULL;
7002 if (decl_table.is_created ())
7004 /* Check if the entry has already been copied to UNIT. */
7005 slot = decl_table.find_slot_with_hash (die, htab_hash_pointer (die),
7006 INSERT);
7007 if (*slot != HTAB_EMPTY_ENTRY)
7009 entry = *slot;
7010 return entry->copy;
7013 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7014 entry = XCNEW (struct decl_table_entry);
7015 entry->orig = die;
7016 entry->copy = NULL;
7017 *slot = entry;
7020 if (parent != NULL)
7022 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7023 if (spec != NULL)
7024 parent = spec;
7025 if (!is_unit_die (parent))
7026 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7029 copy = clone_as_declaration (die);
7030 add_child_die (new_parent, copy);
7032 if (decl_table.is_created ())
7034 /* Record the pointer to the copy. */
7035 entry->copy = copy;
7038 return copy;
7040 /* Copy the declaration context to the new type unit DIE. This includes
7041 any surrounding namespace or type declarations. If the DIE has an
7042 AT_specification attribute, it also includes attributes and children
7043 attached to the specification, and returns a pointer to the original
7044 parent of the declaration DIE. Returns NULL otherwise. */
7046 static dw_die_ref
7047 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7049 dw_die_ref decl;
7050 dw_die_ref new_decl;
7051 dw_die_ref orig_parent = NULL;
7053 decl = get_AT_ref (die, DW_AT_specification);
7054 if (decl == NULL)
7055 decl = die;
7056 else
7058 unsigned ix;
7059 dw_die_ref c;
7060 dw_attr_ref a;
7062 /* The original DIE will be changed to a declaration, and must
7063 be moved to be a child of the original declaration DIE. */
7064 orig_parent = decl->die_parent;
7066 /* Copy the type node pointer from the new DIE to the original
7067 declaration DIE so we can forward references later. */
7068 decl->comdat_type_p = true;
7069 decl->die_id.die_type_node = die->die_id.die_type_node;
7071 remove_AT (die, DW_AT_specification);
7073 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7075 if (a->dw_attr != DW_AT_name
7076 && a->dw_attr != DW_AT_declaration
7077 && a->dw_attr != DW_AT_external)
7078 add_dwarf_attr (die, a);
7081 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7084 if (decl->die_parent != NULL
7085 && !is_unit_die (decl->die_parent))
7087 new_decl = copy_ancestor_tree (unit, decl, decl_hash_type ());
7088 if (new_decl != NULL)
7090 remove_AT (new_decl, DW_AT_signature);
7091 add_AT_specification (die, new_decl);
7095 return orig_parent;
7098 /* Generate the skeleton ancestor tree for the given NODE, then clone
7099 the DIE and add the clone into the tree. */
7101 static void
7102 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7104 if (node->new_die != NULL)
7105 return;
7107 node->new_die = clone_as_declaration (node->old_die);
7109 if (node->parent != NULL)
7111 generate_skeleton_ancestor_tree (node->parent);
7112 add_child_die (node->parent->new_die, node->new_die);
7116 /* Generate a skeleton tree of DIEs containing any declarations that are
7117 found in the original tree. We traverse the tree looking for declaration
7118 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7120 static void
7121 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7123 skeleton_chain_node node;
7124 dw_die_ref c;
7125 dw_die_ref first;
7126 dw_die_ref prev = NULL;
7127 dw_die_ref next = NULL;
7129 node.parent = parent;
7131 first = c = parent->old_die->die_child;
7132 if (c)
7133 next = c->die_sib;
7134 if (c) do {
7135 if (prev == NULL || prev->die_sib == c)
7136 prev = c;
7137 c = next;
7138 next = (c == first ? NULL : c->die_sib);
7139 node.old_die = c;
7140 node.new_die = NULL;
7141 if (is_declaration_die (c))
7143 if (is_template_instantiation (c))
7145 /* Instantiated templates do not need to be cloned into the
7146 type unit. Just move the DIE and its children back to
7147 the skeleton tree (in the main CU). */
7148 remove_child_with_prev (c, prev);
7149 add_child_die (parent->new_die, c);
7150 c = prev;
7152 else
7154 /* Clone the existing DIE, move the original to the skeleton
7155 tree (which is in the main CU), and put the clone, with
7156 all the original's children, where the original came from
7157 (which is about to be moved to the type unit). */
7158 dw_die_ref clone = clone_die (c);
7159 move_all_children (c, clone);
7161 replace_child (c, clone, prev);
7162 generate_skeleton_ancestor_tree (parent);
7163 add_child_die (parent->new_die, c);
7164 node.new_die = c;
7165 c = clone;
7168 generate_skeleton_bottom_up (&node);
7169 } while (next != NULL);
7172 /* Wrapper function for generate_skeleton_bottom_up. */
7174 static dw_die_ref
7175 generate_skeleton (dw_die_ref die)
7177 skeleton_chain_node node;
7179 node.old_die = die;
7180 node.new_die = NULL;
7181 node.parent = NULL;
7183 /* If this type definition is nested inside another type,
7184 and is not an instantiation of a template, always leave
7185 at least a declaration in its place. */
7186 if (die->die_parent != NULL
7187 && is_type_die (die->die_parent)
7188 && !is_template_instantiation (die))
7189 node.new_die = clone_as_declaration (die);
7191 generate_skeleton_bottom_up (&node);
7192 return node.new_die;
7195 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7196 declaration. The original DIE is moved to a new compile unit so that
7197 existing references to it follow it to the new location. If any of the
7198 original DIE's descendants is a declaration, we need to replace the
7199 original DIE with a skeleton tree and move the declarations back into the
7200 skeleton tree. */
7202 static dw_die_ref
7203 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7204 dw_die_ref prev)
7206 dw_die_ref skeleton, orig_parent;
7208 /* Copy the declaration context to the type unit DIE. If the returned
7209 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7210 that DIE. */
7211 orig_parent = copy_declaration_context (unit, child);
7213 skeleton = generate_skeleton (child);
7214 if (skeleton == NULL)
7215 remove_child_with_prev (child, prev);
7216 else
7218 skeleton->comdat_type_p = true;
7219 skeleton->die_id.die_type_node = child->die_id.die_type_node;
7221 /* If the original DIE was a specification, we need to put
7222 the skeleton under the parent DIE of the declaration.
7223 This leaves the original declaration in the tree, but
7224 it will be pruned later since there are no longer any
7225 references to it. */
7226 if (orig_parent != NULL)
7228 remove_child_with_prev (child, prev);
7229 add_child_die (orig_parent, skeleton);
7231 else
7232 replace_child (child, skeleton, prev);
7235 return skeleton;
7238 /* Traverse the DIE and set up additional .debug_types sections for each
7239 type worthy of being placed in a COMDAT section. */
7241 static void
7242 break_out_comdat_types (dw_die_ref die)
7244 dw_die_ref c;
7245 dw_die_ref first;
7246 dw_die_ref prev = NULL;
7247 dw_die_ref next = NULL;
7248 dw_die_ref unit = NULL;
7250 first = c = die->die_child;
7251 if (c)
7252 next = c->die_sib;
7253 if (c) do {
7254 if (prev == NULL || prev->die_sib == c)
7255 prev = c;
7256 c = next;
7257 next = (c == first ? NULL : c->die_sib);
7258 if (should_move_die_to_comdat (c))
7260 dw_die_ref replacement;
7261 comdat_type_node_ref type_node;
7263 /* Break out nested types into their own type units. */
7264 break_out_comdat_types (c);
7266 /* Create a new type unit DIE as the root for the new tree, and
7267 add it to the list of comdat types. */
7268 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7269 add_AT_unsigned (unit, DW_AT_language,
7270 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7271 type_node = ggc_alloc_cleared_comdat_type_node ();
7272 type_node->root_die = unit;
7273 type_node->next = comdat_type_list;
7274 comdat_type_list = type_node;
7276 /* Generate the type signature. */
7277 generate_type_signature (c, type_node);
7279 /* Copy the declaration context, attributes, and children of the
7280 declaration into the new type unit DIE, then remove this DIE
7281 from the main CU (or replace it with a skeleton if necessary). */
7282 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7283 type_node->skeleton_die = replacement;
7285 /* Add the DIE to the new compunit. */
7286 add_child_die (unit, c);
7288 if (replacement != NULL)
7289 c = replacement;
7291 else if (c->die_tag == DW_TAG_namespace
7292 || c->die_tag == DW_TAG_class_type
7293 || c->die_tag == DW_TAG_structure_type
7294 || c->die_tag == DW_TAG_union_type)
7296 /* Look for nested types that can be broken out. */
7297 break_out_comdat_types (c);
7299 } while (next != NULL);
7302 /* Like clone_tree, but additionally enter all the children into
7303 the hash table decl_table. */
7305 static dw_die_ref
7306 clone_tree_hash (dw_die_ref die, decl_hash_type decl_table)
7308 dw_die_ref c;
7309 dw_die_ref clone = clone_die (die);
7310 struct decl_table_entry *entry;
7311 decl_table_entry **slot = decl_table.find_slot_with_hash (die,
7312 htab_hash_pointer (die), INSERT);
7313 /* Assert that DIE isn't in the hash table yet. If it would be there
7314 before, the ancestors would be necessarily there as well, therefore
7315 clone_tree_hash wouldn't be called. */
7316 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7317 entry = XCNEW (struct decl_table_entry);
7318 entry->orig = die;
7319 entry->copy = clone;
7320 *slot = entry;
7322 FOR_EACH_CHILD (die, c,
7323 add_child_die (clone, clone_tree_hash (c, decl_table)));
7325 return clone;
7328 /* Walk the DIE and its children, looking for references to incomplete
7329 or trivial types that are unmarked (i.e., that are not in the current
7330 type_unit). */
7332 static void
7333 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type decl_table)
7335 dw_die_ref c;
7336 dw_attr_ref a;
7337 unsigned ix;
7339 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7341 if (AT_class (a) == dw_val_class_die_ref)
7343 dw_die_ref targ = AT_ref (a);
7344 decl_table_entry **slot;
7345 struct decl_table_entry *entry;
7347 if (targ->die_mark != 0 || targ->comdat_type_p)
7348 continue;
7350 slot = decl_table.find_slot_with_hash (targ, htab_hash_pointer (targ),
7351 INSERT);
7353 if (*slot != HTAB_EMPTY_ENTRY)
7355 /* TARG has already been copied, so we just need to
7356 modify the reference to point to the copy. */
7357 entry = *slot;
7358 a->dw_attr_val.v.val_die_ref.die = entry->copy;
7360 else
7362 dw_die_ref parent = unit;
7363 dw_die_ref copy = clone_die (targ);
7365 /* Record in DECL_TABLE that TARG has been copied.
7366 Need to do this now, before the recursive call,
7367 because DECL_TABLE may be expanded and SLOT
7368 would no longer be a valid pointer. */
7369 entry = XCNEW (struct decl_table_entry);
7370 entry->orig = targ;
7371 entry->copy = copy;
7372 *slot = entry;
7374 FOR_EACH_CHILD (targ, c,
7375 add_child_die (copy,
7376 clone_tree_hash (c, decl_table)));
7378 /* Make sure the cloned tree is marked as part of the
7379 type unit. */
7380 mark_dies (copy);
7382 /* If TARG has surrounding context, copy its ancestor tree
7383 into the new type unit. */
7384 if (targ->die_parent != NULL
7385 && !is_unit_die (targ->die_parent))
7386 parent = copy_ancestor_tree (unit, targ->die_parent,
7387 decl_table);
7389 add_child_die (parent, copy);
7390 a->dw_attr_val.v.val_die_ref.die = copy;
7392 /* Make sure the newly-copied DIE is walked. If it was
7393 installed in a previously-added context, it won't
7394 get visited otherwise. */
7395 if (parent != unit)
7397 /* Find the highest point of the newly-added tree,
7398 mark each node along the way, and walk from there. */
7399 parent->die_mark = 1;
7400 while (parent->die_parent
7401 && parent->die_parent->die_mark == 0)
7403 parent = parent->die_parent;
7404 parent->die_mark = 1;
7406 copy_decls_walk (unit, parent, decl_table);
7412 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7415 /* Copy declarations for "unworthy" types into the new comdat section.
7416 Incomplete types, modified types, and certain other types aren't broken
7417 out into comdat sections of their own, so they don't have a signature,
7418 and we need to copy the declaration into the same section so that we
7419 don't have an external reference. */
7421 static void
7422 copy_decls_for_unworthy_types (dw_die_ref unit)
7424 decl_hash_type decl_table;
7426 mark_dies (unit);
7427 decl_table.create (10);
7428 copy_decls_walk (unit, unit, decl_table);
7429 decl_table.dispose ();
7430 unmark_dies (unit);
7433 /* Traverse the DIE and add a sibling attribute if it may have the
7434 effect of speeding up access to siblings. To save some space,
7435 avoid generating sibling attributes for DIE's without children. */
7437 static void
7438 add_sibling_attributes (dw_die_ref die)
7440 dw_die_ref c;
7442 if (! die->die_child)
7443 return;
7445 if (die->die_parent && die != die->die_parent->die_child)
7446 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7448 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7451 /* Output all location lists for the DIE and its children. */
7453 static void
7454 output_location_lists (dw_die_ref die)
7456 dw_die_ref c;
7457 dw_attr_ref a;
7458 unsigned ix;
7460 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7461 if (AT_class (a) == dw_val_class_loc_list)
7462 output_loc_list (AT_loc_list (a));
7464 FOR_EACH_CHILD (die, c, output_location_lists (c));
7467 /* We want to limit the number of external references, because they are
7468 larger than local references: a relocation takes multiple words, and
7469 even a sig8 reference is always eight bytes, whereas a local reference
7470 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7471 So if we encounter multiple external references to the same type DIE, we
7472 make a local typedef stub for it and redirect all references there.
7474 This is the element of the hash table for keeping track of these
7475 references. */
7477 struct external_ref
7479 dw_die_ref type;
7480 dw_die_ref stub;
7481 unsigned n_refs;
7484 /* Hashtable helpers. */
7486 struct external_ref_hasher : typed_free_remove <external_ref>
7488 typedef external_ref value_type;
7489 typedef external_ref compare_type;
7490 static inline hashval_t hash (const value_type *);
7491 static inline bool equal (const value_type *, const compare_type *);
7494 inline hashval_t
7495 external_ref_hasher::hash (const value_type *r)
7497 dw_die_ref die = r->type;
7498 hashval_t h = 0;
7500 /* We can't use the address of the DIE for hashing, because
7501 that will make the order of the stub DIEs non-deterministic. */
7502 if (! die->comdat_type_p)
7503 /* We have a symbol; use it to compute a hash. */
7504 h = htab_hash_string (die->die_id.die_symbol);
7505 else
7507 /* We have a type signature; use a subset of the bits as the hash.
7508 The 8-byte signature is at least as large as hashval_t. */
7509 comdat_type_node_ref type_node = die->die_id.die_type_node;
7510 memcpy (&h, type_node->signature, sizeof (h));
7512 return h;
7515 inline bool
7516 external_ref_hasher::equal (const value_type *r1, const compare_type *r2)
7518 return r1->type == r2->type;
7521 typedef hash_table <external_ref_hasher> external_ref_hash_type;
7523 /* Return a pointer to the external_ref for references to DIE. */
7525 static struct external_ref *
7526 lookup_external_ref (external_ref_hash_type map, dw_die_ref die)
7528 struct external_ref ref, *ref_p;
7529 external_ref **slot;
7531 ref.type = die;
7532 slot = map.find_slot (&ref, INSERT);
7533 if (*slot != HTAB_EMPTY_ENTRY)
7534 return *slot;
7536 ref_p = XCNEW (struct external_ref);
7537 ref_p->type = die;
7538 *slot = ref_p;
7539 return ref_p;
7542 /* Subroutine of optimize_external_refs, below.
7544 If we see a type skeleton, record it as our stub. If we see external
7545 references, remember how many we've seen. */
7547 static void
7548 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type map)
7550 dw_die_ref c;
7551 dw_attr_ref a;
7552 unsigned ix;
7553 struct external_ref *ref_p;
7555 if (is_type_die (die)
7556 && (c = get_AT_ref (die, DW_AT_signature)))
7558 /* This is a local skeleton; use it for local references. */
7559 ref_p = lookup_external_ref (map, c);
7560 ref_p->stub = die;
7563 /* Scan the DIE references, and remember any that refer to DIEs from
7564 other CUs (i.e. those which are not marked). */
7565 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7566 if (AT_class (a) == dw_val_class_die_ref
7567 && (c = AT_ref (a))->die_mark == 0
7568 && is_type_die (c))
7570 ref_p = lookup_external_ref (map, c);
7571 ref_p->n_refs++;
7574 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7577 /* htab_traverse callback function for optimize_external_refs, below. SLOT
7578 points to an external_ref, DATA is the CU we're processing. If we don't
7579 already have a local stub, and we have multiple refs, build a stub. */
7582 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
7584 struct external_ref *ref_p = *slot;
7586 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7588 /* We have multiple references to this type, so build a small stub.
7589 Both of these forms are a bit dodgy from the perspective of the
7590 DWARF standard, since technically they should have names. */
7591 dw_die_ref cu = data;
7592 dw_die_ref type = ref_p->type;
7593 dw_die_ref stub = NULL;
7595 if (type->comdat_type_p)
7597 /* If we refer to this type via sig8, use AT_signature. */
7598 stub = new_die (type->die_tag, cu, NULL_TREE);
7599 add_AT_die_ref (stub, DW_AT_signature, type);
7601 else
7603 /* Otherwise, use a typedef with no name. */
7604 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7605 add_AT_die_ref (stub, DW_AT_type, type);
7608 stub->die_mark++;
7609 ref_p->stub = stub;
7611 return 1;
7614 /* DIE is a unit; look through all the DIE references to see if there are
7615 any external references to types, and if so, create local stubs for
7616 them which will be applied in build_abbrev_table. This is useful because
7617 references to local DIEs are smaller. */
7619 static external_ref_hash_type
7620 optimize_external_refs (dw_die_ref die)
7622 external_ref_hash_type map;
7623 map.create (10);
7624 optimize_external_refs_1 (die, map);
7625 map.traverse <dw_die_ref, dwarf2_build_local_stub> (die);
7626 return map;
7629 /* The format of each DIE (and its attribute value pairs) is encoded in an
7630 abbreviation table. This routine builds the abbreviation table and assigns
7631 a unique abbreviation id for each abbreviation entry. The children of each
7632 die are visited recursively. */
7634 static void
7635 build_abbrev_table (dw_die_ref die, external_ref_hash_type extern_map)
7637 unsigned long abbrev_id;
7638 unsigned int n_alloc;
7639 dw_die_ref c;
7640 dw_attr_ref a;
7641 unsigned ix;
7643 /* Scan the DIE references, and replace any that refer to
7644 DIEs from other CUs (i.e. those which are not marked) with
7645 the local stubs we built in optimize_external_refs. */
7646 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7647 if (AT_class (a) == dw_val_class_die_ref
7648 && (c = AT_ref (a))->die_mark == 0)
7650 struct external_ref *ref_p;
7651 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
7653 ref_p = lookup_external_ref (extern_map, c);
7654 if (ref_p->stub && ref_p->stub != die)
7655 change_AT_die_ref (a, ref_p->stub);
7656 else
7657 /* We aren't changing this reference, so mark it external. */
7658 set_AT_ref_external (a, 1);
7661 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7663 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7664 dw_attr_ref die_a, abbrev_a;
7665 unsigned ix;
7666 bool ok = true;
7668 if (abbrev->die_tag != die->die_tag)
7669 continue;
7670 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7671 continue;
7673 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
7674 continue;
7676 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
7678 abbrev_a = &(*abbrev->die_attr)[ix];
7679 if ((abbrev_a->dw_attr != die_a->dw_attr)
7680 || (value_format (abbrev_a) != value_format (die_a)))
7682 ok = false;
7683 break;
7686 if (ok)
7687 break;
7690 if (abbrev_id >= abbrev_die_table_in_use)
7692 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7694 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7695 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7696 n_alloc);
7698 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7699 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7700 abbrev_die_table_allocated = n_alloc;
7703 ++abbrev_die_table_in_use;
7704 abbrev_die_table[abbrev_id] = die;
7707 die->die_abbrev = abbrev_id;
7708 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
7711 /* Return the power-of-two number of bytes necessary to represent VALUE. */
7713 static int
7714 constant_size (unsigned HOST_WIDE_INT value)
7716 int log;
7718 if (value == 0)
7719 log = 0;
7720 else
7721 log = floor_log2 (value);
7723 log = log / 8;
7724 log = 1 << (floor_log2 (log) + 1);
7726 return log;
7729 /* Return the size of a DIE as it is represented in the
7730 .debug_info section. */
7732 static unsigned long
7733 size_of_die (dw_die_ref die)
7735 unsigned long size = 0;
7736 dw_attr_ref a;
7737 unsigned ix;
7738 enum dwarf_form form;
7740 size += size_of_uleb128 (die->die_abbrev);
7741 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7743 switch (AT_class (a))
7745 case dw_val_class_addr:
7746 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7748 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7749 size += size_of_uleb128 (AT_index (a));
7751 else
7752 size += DWARF2_ADDR_SIZE;
7753 break;
7754 case dw_val_class_offset:
7755 size += DWARF_OFFSET_SIZE;
7756 break;
7757 case dw_val_class_loc:
7759 unsigned long lsize = size_of_locs (AT_loc (a));
7761 /* Block length. */
7762 if (dwarf_version >= 4)
7763 size += size_of_uleb128 (lsize);
7764 else
7765 size += constant_size (lsize);
7766 size += lsize;
7768 break;
7769 case dw_val_class_loc_list:
7770 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7772 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7773 size += size_of_uleb128 (AT_index (a));
7775 else
7776 size += DWARF_OFFSET_SIZE;
7777 break;
7778 case dw_val_class_range_list:
7779 size += DWARF_OFFSET_SIZE;
7780 break;
7781 case dw_val_class_const:
7782 size += size_of_sleb128 (AT_int (a));
7783 break;
7784 case dw_val_class_unsigned_const:
7786 int csize = constant_size (AT_unsigned (a));
7787 if (dwarf_version == 3
7788 && a->dw_attr == DW_AT_data_member_location
7789 && csize >= 4)
7790 size += size_of_uleb128 (AT_unsigned (a));
7791 else
7792 size += csize;
7794 break;
7795 case dw_val_class_const_double:
7796 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
7797 if (HOST_BITS_PER_WIDE_INT >= 64)
7798 size++; /* block */
7799 break;
7800 case dw_val_class_vec:
7801 size += constant_size (a->dw_attr_val.v.val_vec.length
7802 * a->dw_attr_val.v.val_vec.elt_size)
7803 + a->dw_attr_val.v.val_vec.length
7804 * a->dw_attr_val.v.val_vec.elt_size; /* block */
7805 break;
7806 case dw_val_class_flag:
7807 if (dwarf_version >= 4)
7808 /* Currently all add_AT_flag calls pass in 1 as last argument,
7809 so DW_FORM_flag_present can be used. If that ever changes,
7810 we'll need to use DW_FORM_flag and have some optimization
7811 in build_abbrev_table that will change those to
7812 DW_FORM_flag_present if it is set to 1 in all DIEs using
7813 the same abbrev entry. */
7814 gcc_assert (a->dw_attr_val.v.val_flag == 1);
7815 else
7816 size += 1;
7817 break;
7818 case dw_val_class_die_ref:
7819 if (AT_ref_external (a))
7821 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
7822 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
7823 is sized by target address length, whereas in DWARF3
7824 it's always sized as an offset. */
7825 if (use_debug_types)
7826 size += DWARF_TYPE_SIGNATURE_SIZE;
7827 else if (dwarf_version == 2)
7828 size += DWARF2_ADDR_SIZE;
7829 else
7830 size += DWARF_OFFSET_SIZE;
7832 else
7833 size += DWARF_OFFSET_SIZE;
7834 break;
7835 case dw_val_class_fde_ref:
7836 size += DWARF_OFFSET_SIZE;
7837 break;
7838 case dw_val_class_lbl_id:
7839 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7841 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7842 size += size_of_uleb128 (AT_index (a));
7844 else
7845 size += DWARF2_ADDR_SIZE;
7846 break;
7847 case dw_val_class_lineptr:
7848 case dw_val_class_macptr:
7849 size += DWARF_OFFSET_SIZE;
7850 break;
7851 case dw_val_class_str:
7852 form = AT_string_form (a);
7853 if (form == DW_FORM_strp)
7854 size += DWARF_OFFSET_SIZE;
7855 else if (form == DW_FORM_GNU_str_index)
7856 size += size_of_uleb128 (AT_index (a));
7857 else
7858 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7859 break;
7860 case dw_val_class_file:
7861 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7862 break;
7863 case dw_val_class_data8:
7864 size += 8;
7865 break;
7866 case dw_val_class_vms_delta:
7867 size += DWARF_OFFSET_SIZE;
7868 break;
7869 case dw_val_class_high_pc:
7870 size += DWARF2_ADDR_SIZE;
7871 break;
7872 default:
7873 gcc_unreachable ();
7877 return size;
7880 /* Size the debugging information associated with a given DIE. Visits the
7881 DIE's children recursively. Updates the global variable next_die_offset, on
7882 each time through. Uses the current value of next_die_offset to update the
7883 die_offset field in each DIE. */
7885 static void
7886 calc_die_sizes (dw_die_ref die)
7888 dw_die_ref c;
7890 gcc_assert (die->die_offset == 0
7891 || (unsigned long int) die->die_offset == next_die_offset);
7892 die->die_offset = next_die_offset;
7893 next_die_offset += size_of_die (die);
7895 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
7897 if (die->die_child != NULL)
7898 /* Count the null byte used to terminate sibling lists. */
7899 next_die_offset += 1;
7902 /* Size just the base type children at the start of the CU.
7903 This is needed because build_abbrev needs to size locs
7904 and sizing of type based stack ops needs to know die_offset
7905 values for the base types. */
7907 static void
7908 calc_base_type_die_sizes (void)
7910 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7911 unsigned int i;
7912 dw_die_ref base_type;
7913 #if ENABLE_ASSERT_CHECKING
7914 dw_die_ref prev = comp_unit_die ()->die_child;
7915 #endif
7917 die_offset += size_of_die (comp_unit_die ());
7918 for (i = 0; base_types.iterate (i, &base_type); i++)
7920 #if ENABLE_ASSERT_CHECKING
7921 gcc_assert (base_type->die_offset == 0
7922 && prev->die_sib == base_type
7923 && base_type->die_child == NULL
7924 && base_type->die_abbrev);
7925 prev = base_type;
7926 #endif
7927 base_type->die_offset = die_offset;
7928 die_offset += size_of_die (base_type);
7932 /* Set the marks for a die and its children. We do this so
7933 that we know whether or not a reference needs to use FORM_ref_addr; only
7934 DIEs in the same CU will be marked. We used to clear out the offset
7935 and use that as the flag, but ran into ordering problems. */
7937 static void
7938 mark_dies (dw_die_ref die)
7940 dw_die_ref c;
7942 gcc_assert (!die->die_mark);
7944 die->die_mark = 1;
7945 FOR_EACH_CHILD (die, c, mark_dies (c));
7948 /* Clear the marks for a die and its children. */
7950 static void
7951 unmark_dies (dw_die_ref die)
7953 dw_die_ref c;
7955 if (! use_debug_types)
7956 gcc_assert (die->die_mark);
7958 die->die_mark = 0;
7959 FOR_EACH_CHILD (die, c, unmark_dies (c));
7962 /* Clear the marks for a die, its children and referred dies. */
7964 static void
7965 unmark_all_dies (dw_die_ref die)
7967 dw_die_ref c;
7968 dw_attr_ref a;
7969 unsigned ix;
7971 if (!die->die_mark)
7972 return;
7973 die->die_mark = 0;
7975 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
7977 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7978 if (AT_class (a) == dw_val_class_die_ref)
7979 unmark_all_dies (AT_ref (a));
7982 /* Calculate if the entry should appear in the final output file. It may be
7983 from a pruned a type. */
7985 static bool
7986 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
7988 /* By limiting gnu pubnames to definitions only, gold can generate a
7989 gdb index without entries for declarations, which don't include
7990 enough information to be useful. */
7991 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
7992 return false;
7994 if (table == pubname_table)
7996 /* Enumerator names are part of the pubname table, but the
7997 parent DW_TAG_enumeration_type die may have been pruned.
7998 Don't output them if that is the case. */
7999 if (p->die->die_tag == DW_TAG_enumerator &&
8000 (p->die->die_parent == NULL
8001 || !p->die->die_parent->die_perennial_p))
8002 return false;
8004 /* Everything else in the pubname table is included. */
8005 return true;
8008 /* The pubtypes table shouldn't include types that have been
8009 pruned. */
8010 return (p->die->die_offset != 0
8011 || !flag_eliminate_unused_debug_types);
8014 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8015 generated for the compilation unit. */
8017 static unsigned long
8018 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8020 unsigned long size;
8021 unsigned i;
8022 pubname_ref p;
8023 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8025 size = DWARF_PUBNAMES_HEADER_SIZE;
8026 FOR_EACH_VEC_ELT (*names, i, p)
8027 if (include_pubname_in_output (names, p))
8028 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8030 size += DWARF_OFFSET_SIZE;
8031 return size;
8034 /* Return the size of the information in the .debug_aranges section. */
8036 static unsigned long
8037 size_of_aranges (void)
8039 unsigned long size;
8041 size = DWARF_ARANGES_HEADER_SIZE;
8043 /* Count the address/length pair for this compilation unit. */
8044 if (text_section_used)
8045 size += 2 * DWARF2_ADDR_SIZE;
8046 if (cold_text_section_used)
8047 size += 2 * DWARF2_ADDR_SIZE;
8048 if (have_multiple_function_sections)
8050 unsigned fde_idx;
8051 dw_fde_ref fde;
8053 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8055 if (DECL_IGNORED_P (fde->decl))
8056 continue;
8057 if (!fde->in_std_section)
8058 size += 2 * DWARF2_ADDR_SIZE;
8059 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8060 size += 2 * DWARF2_ADDR_SIZE;
8064 /* Count the two zero words used to terminated the address range table. */
8065 size += 2 * DWARF2_ADDR_SIZE;
8066 return size;
8069 /* Select the encoding of an attribute value. */
8071 static enum dwarf_form
8072 value_format (dw_attr_ref a)
8074 switch (AT_class (a))
8076 case dw_val_class_addr:
8077 /* Only very few attributes allow DW_FORM_addr. */
8078 switch (a->dw_attr)
8080 case DW_AT_low_pc:
8081 case DW_AT_high_pc:
8082 case DW_AT_entry_pc:
8083 case DW_AT_trampoline:
8084 return (AT_index (a) == NOT_INDEXED
8085 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8086 default:
8087 break;
8089 switch (DWARF2_ADDR_SIZE)
8091 case 1:
8092 return DW_FORM_data1;
8093 case 2:
8094 return DW_FORM_data2;
8095 case 4:
8096 return DW_FORM_data4;
8097 case 8:
8098 return DW_FORM_data8;
8099 default:
8100 gcc_unreachable ();
8102 case dw_val_class_range_list:
8103 case dw_val_class_loc_list:
8104 if (dwarf_version >= 4)
8105 return DW_FORM_sec_offset;
8106 /* FALLTHRU */
8107 case dw_val_class_vms_delta:
8108 case dw_val_class_offset:
8109 switch (DWARF_OFFSET_SIZE)
8111 case 4:
8112 return DW_FORM_data4;
8113 case 8:
8114 return DW_FORM_data8;
8115 default:
8116 gcc_unreachable ();
8118 case dw_val_class_loc:
8119 if (dwarf_version >= 4)
8120 return DW_FORM_exprloc;
8121 switch (constant_size (size_of_locs (AT_loc (a))))
8123 case 1:
8124 return DW_FORM_block1;
8125 case 2:
8126 return DW_FORM_block2;
8127 case 4:
8128 return DW_FORM_block4;
8129 default:
8130 gcc_unreachable ();
8132 case dw_val_class_const:
8133 return DW_FORM_sdata;
8134 case dw_val_class_unsigned_const:
8135 switch (constant_size (AT_unsigned (a)))
8137 case 1:
8138 return DW_FORM_data1;
8139 case 2:
8140 return DW_FORM_data2;
8141 case 4:
8142 /* In DWARF3 DW_AT_data_member_location with
8143 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8144 constant, so we need to use DW_FORM_udata if we need
8145 a large constant. */
8146 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8147 return DW_FORM_udata;
8148 return DW_FORM_data4;
8149 case 8:
8150 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8151 return DW_FORM_udata;
8152 return DW_FORM_data8;
8153 default:
8154 gcc_unreachable ();
8156 case dw_val_class_const_double:
8157 switch (HOST_BITS_PER_WIDE_INT)
8159 case 8:
8160 return DW_FORM_data2;
8161 case 16:
8162 return DW_FORM_data4;
8163 case 32:
8164 return DW_FORM_data8;
8165 case 64:
8166 default:
8167 return DW_FORM_block1;
8169 case dw_val_class_vec:
8170 switch (constant_size (a->dw_attr_val.v.val_vec.length
8171 * a->dw_attr_val.v.val_vec.elt_size))
8173 case 1:
8174 return DW_FORM_block1;
8175 case 2:
8176 return DW_FORM_block2;
8177 case 4:
8178 return DW_FORM_block4;
8179 default:
8180 gcc_unreachable ();
8182 case dw_val_class_flag:
8183 if (dwarf_version >= 4)
8185 /* Currently all add_AT_flag calls pass in 1 as last argument,
8186 so DW_FORM_flag_present can be used. If that ever changes,
8187 we'll need to use DW_FORM_flag and have some optimization
8188 in build_abbrev_table that will change those to
8189 DW_FORM_flag_present if it is set to 1 in all DIEs using
8190 the same abbrev entry. */
8191 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8192 return DW_FORM_flag_present;
8194 return DW_FORM_flag;
8195 case dw_val_class_die_ref:
8196 if (AT_ref_external (a))
8197 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8198 else
8199 return DW_FORM_ref;
8200 case dw_val_class_fde_ref:
8201 return DW_FORM_data;
8202 case dw_val_class_lbl_id:
8203 return (AT_index (a) == NOT_INDEXED
8204 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8205 case dw_val_class_lineptr:
8206 case dw_val_class_macptr:
8207 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8208 case dw_val_class_str:
8209 return AT_string_form (a);
8210 case dw_val_class_file:
8211 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8213 case 1:
8214 return DW_FORM_data1;
8215 case 2:
8216 return DW_FORM_data2;
8217 case 4:
8218 return DW_FORM_data4;
8219 default:
8220 gcc_unreachable ();
8223 case dw_val_class_data8:
8224 return DW_FORM_data8;
8226 case dw_val_class_high_pc:
8227 switch (DWARF2_ADDR_SIZE)
8229 case 1:
8230 return DW_FORM_data1;
8231 case 2:
8232 return DW_FORM_data2;
8233 case 4:
8234 return DW_FORM_data4;
8235 case 8:
8236 return DW_FORM_data8;
8237 default:
8238 gcc_unreachable ();
8241 default:
8242 gcc_unreachable ();
8246 /* Output the encoding of an attribute value. */
8248 static void
8249 output_value_format (dw_attr_ref a)
8251 enum dwarf_form form = value_format (a);
8253 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8256 /* Given a die and id, produce the appropriate abbreviations. */
8258 static void
8259 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8261 unsigned ix;
8262 dw_attr_ref a_attr;
8264 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8265 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8266 dwarf_tag_name (abbrev->die_tag));
8268 if (abbrev->die_child != NULL)
8269 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8270 else
8271 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8273 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8275 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8276 dwarf_attr_name (a_attr->dw_attr));
8277 output_value_format (a_attr);
8280 dw2_asm_output_data (1, 0, NULL);
8281 dw2_asm_output_data (1, 0, NULL);
8285 /* Output the .debug_abbrev section which defines the DIE abbreviation
8286 table. */
8288 static void
8289 output_abbrev_section (void)
8291 unsigned long abbrev_id;
8293 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8294 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8296 /* Terminate the table. */
8297 dw2_asm_output_data (1, 0, NULL);
8300 /* Output a symbol we can use to refer to this DIE from another CU. */
8302 static inline void
8303 output_die_symbol (dw_die_ref die)
8305 const char *sym = die->die_id.die_symbol;
8307 gcc_assert (!die->comdat_type_p);
8309 if (sym == 0)
8310 return;
8312 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8313 /* We make these global, not weak; if the target doesn't support
8314 .linkonce, it doesn't support combining the sections, so debugging
8315 will break. */
8316 targetm.asm_out.globalize_label (asm_out_file, sym);
8318 ASM_OUTPUT_LABEL (asm_out_file, sym);
8321 /* Return a new location list, given the begin and end range, and the
8322 expression. */
8324 static inline dw_loc_list_ref
8325 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8326 const char *section)
8328 dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
8330 retlist->begin = begin;
8331 retlist->begin_entry = NULL;
8332 retlist->end = end;
8333 retlist->expr = expr;
8334 retlist->section = section;
8336 return retlist;
8339 /* Generate a new internal symbol for this location list node, if it
8340 hasn't got one yet. */
8342 static inline void
8343 gen_llsym (dw_loc_list_ref list)
8345 gcc_assert (!list->ll_symbol);
8346 list->ll_symbol = gen_internal_sym ("LLST");
8349 /* Output the location list given to us. */
8351 static void
8352 output_loc_list (dw_loc_list_ref list_head)
8354 dw_loc_list_ref curr = list_head;
8356 if (list_head->emitted)
8357 return;
8358 list_head->emitted = true;
8360 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8362 /* Walk the location list, and output each range + expression. */
8363 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8365 unsigned long size;
8366 /* Don't output an entry that starts and ends at the same address. */
8367 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8368 continue;
8369 size = size_of_locs (curr->expr);
8370 /* If the expression is too large, drop it on the floor. We could
8371 perhaps put it into DW_TAG_dwarf_procedure and refer to that
8372 in the expression, but >= 64KB expressions for a single value
8373 in a single range are unlikely very useful. */
8374 if (size > 0xffff)
8375 continue;
8376 if (dwarf_split_debug_info)
8378 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8379 "Location list start/length entry (%s)",
8380 list_head->ll_symbol);
8381 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8382 "Location list range start index (%s)",
8383 curr->begin);
8384 /* The length field is 4 bytes. If we ever need to support
8385 an 8-byte length, we can add a new DW_LLE code or fall back
8386 to DW_LLE_GNU_start_end_entry. */
8387 dw2_asm_output_delta (4, curr->end, curr->begin,
8388 "Location list range length (%s)",
8389 list_head->ll_symbol);
8391 else if (!have_multiple_function_sections)
8393 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8394 "Location list begin address (%s)",
8395 list_head->ll_symbol);
8396 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8397 "Location list end address (%s)",
8398 list_head->ll_symbol);
8400 else
8402 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8403 "Location list begin address (%s)",
8404 list_head->ll_symbol);
8405 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8406 "Location list end address (%s)",
8407 list_head->ll_symbol);
8410 /* Output the block length for this list of location operations. */
8411 gcc_assert (size <= 0xffff);
8412 dw2_asm_output_data (2, size, "%s", "Location expression size");
8414 output_loc_sequence (curr->expr, -1);
8417 if (dwarf_split_debug_info)
8418 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8419 "Location list terminator (%s)",
8420 list_head->ll_symbol);
8421 else
8423 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8424 "Location list terminator begin (%s)",
8425 list_head->ll_symbol);
8426 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8427 "Location list terminator end (%s)",
8428 list_head->ll_symbol);
8432 /* Output a range_list offset into the debug_range section. Emit a
8433 relocated reference if val_entry is NULL, otherwise, emit an
8434 indirect reference. */
8436 static void
8437 output_range_list_offset (dw_attr_ref a)
8439 const char *name = dwarf_attr_name (a->dw_attr);
8441 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8443 char *p = strchr (ranges_section_label, '\0');
8444 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8445 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8446 debug_ranges_section, "%s", name);
8447 *p = '\0';
8449 else
8450 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8451 "%s (offset from %s)", name, ranges_section_label);
8454 /* Output the offset into the debug_loc section. */
8456 static void
8457 output_loc_list_offset (dw_attr_ref a)
8459 char *sym = AT_loc_list (a)->ll_symbol;
8461 gcc_assert (sym);
8462 if (dwarf_split_debug_info)
8463 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8464 "%s", dwarf_attr_name (a->dw_attr));
8465 else
8466 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8467 "%s", dwarf_attr_name (a->dw_attr));
8470 /* Output an attribute's index or value appropriately. */
8472 static void
8473 output_attr_index_or_value (dw_attr_ref a)
8475 const char *name = dwarf_attr_name (a->dw_attr);
8477 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8479 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8480 return;
8482 switch (AT_class (a))
8484 case dw_val_class_addr:
8485 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8486 break;
8487 case dw_val_class_high_pc:
8488 case dw_val_class_lbl_id:
8489 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8490 break;
8491 case dw_val_class_loc_list:
8492 output_loc_list_offset (a);
8493 break;
8494 default:
8495 gcc_unreachable ();
8499 /* Output a type signature. */
8501 static inline void
8502 output_signature (const char *sig, const char *name)
8504 int i;
8506 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8507 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8510 /* Output the DIE and its attributes. Called recursively to generate
8511 the definitions of each child DIE. */
8513 static void
8514 output_die (dw_die_ref die)
8516 dw_attr_ref a;
8517 dw_die_ref c;
8518 unsigned long size;
8519 unsigned ix;
8521 /* If someone in another CU might refer to us, set up a symbol for
8522 them to point to. */
8523 if (! die->comdat_type_p && die->die_id.die_symbol)
8524 output_die_symbol (die);
8526 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8527 (unsigned long)die->die_offset,
8528 dwarf_tag_name (die->die_tag));
8530 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8532 const char *name = dwarf_attr_name (a->dw_attr);
8534 switch (AT_class (a))
8536 case dw_val_class_addr:
8537 output_attr_index_or_value (a);
8538 break;
8540 case dw_val_class_offset:
8541 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8542 "%s", name);
8543 break;
8545 case dw_val_class_range_list:
8546 output_range_list_offset (a);
8547 break;
8549 case dw_val_class_loc:
8550 size = size_of_locs (AT_loc (a));
8552 /* Output the block length for this list of location operations. */
8553 if (dwarf_version >= 4)
8554 dw2_asm_output_data_uleb128 (size, "%s", name);
8555 else
8556 dw2_asm_output_data (constant_size (size), size, "%s", name);
8558 output_loc_sequence (AT_loc (a), -1);
8559 break;
8561 case dw_val_class_const:
8562 /* ??? It would be slightly more efficient to use a scheme like is
8563 used for unsigned constants below, but gdb 4.x does not sign
8564 extend. Gdb 5.x does sign extend. */
8565 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8566 break;
8568 case dw_val_class_unsigned_const:
8570 int csize = constant_size (AT_unsigned (a));
8571 if (dwarf_version == 3
8572 && a->dw_attr == DW_AT_data_member_location
8573 && csize >= 4)
8574 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8575 else
8576 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8578 break;
8580 case dw_val_class_const_double:
8582 unsigned HOST_WIDE_INT first, second;
8584 if (HOST_BITS_PER_WIDE_INT >= 64)
8585 dw2_asm_output_data (1,
8586 HOST_BITS_PER_DOUBLE_INT
8587 / HOST_BITS_PER_CHAR,
8588 NULL);
8590 if (WORDS_BIG_ENDIAN)
8592 first = a->dw_attr_val.v.val_double.high;
8593 second = a->dw_attr_val.v.val_double.low;
8595 else
8597 first = a->dw_attr_val.v.val_double.low;
8598 second = a->dw_attr_val.v.val_double.high;
8601 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8602 first, "%s", name);
8603 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8604 second, NULL);
8606 break;
8608 case dw_val_class_vec:
8610 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8611 unsigned int len = a->dw_attr_val.v.val_vec.length;
8612 unsigned int i;
8613 unsigned char *p;
8615 dw2_asm_output_data (constant_size (len * elt_size),
8616 len * elt_size, "%s", name);
8617 if (elt_size > sizeof (HOST_WIDE_INT))
8619 elt_size /= 2;
8620 len *= 2;
8622 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8623 i < len;
8624 i++, p += elt_size)
8625 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8626 "fp or vector constant word %u", i);
8627 break;
8630 case dw_val_class_flag:
8631 if (dwarf_version >= 4)
8633 /* Currently all add_AT_flag calls pass in 1 as last argument,
8634 so DW_FORM_flag_present can be used. If that ever changes,
8635 we'll need to use DW_FORM_flag and have some optimization
8636 in build_abbrev_table that will change those to
8637 DW_FORM_flag_present if it is set to 1 in all DIEs using
8638 the same abbrev entry. */
8639 gcc_assert (AT_flag (a) == 1);
8640 if (flag_debug_asm)
8641 fprintf (asm_out_file, "\t\t\t%s %s\n",
8642 ASM_COMMENT_START, name);
8643 break;
8645 dw2_asm_output_data (1, AT_flag (a), "%s", name);
8646 break;
8648 case dw_val_class_loc_list:
8649 output_attr_index_or_value (a);
8650 break;
8652 case dw_val_class_die_ref:
8653 if (AT_ref_external (a))
8655 if (AT_ref (a)->comdat_type_p)
8657 comdat_type_node_ref type_node =
8658 AT_ref (a)->die_id.die_type_node;
8660 gcc_assert (type_node);
8661 output_signature (type_node->signature, name);
8663 else
8665 const char *sym = AT_ref (a)->die_id.die_symbol;
8666 int size;
8668 gcc_assert (sym);
8669 /* In DWARF2, DW_FORM_ref_addr is sized by target address
8670 length, whereas in DWARF3 it's always sized as an
8671 offset. */
8672 if (dwarf_version == 2)
8673 size = DWARF2_ADDR_SIZE;
8674 else
8675 size = DWARF_OFFSET_SIZE;
8676 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8677 name);
8680 else
8682 gcc_assert (AT_ref (a)->die_offset);
8683 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8684 "%s", name);
8686 break;
8688 case dw_val_class_fde_ref:
8690 char l1[20];
8692 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8693 a->dw_attr_val.v.val_fde_index * 2);
8694 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8695 "%s", name);
8697 break;
8699 case dw_val_class_vms_delta:
8700 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
8701 AT_vms_delta2 (a), AT_vms_delta1 (a),
8702 "%s", name);
8703 break;
8705 case dw_val_class_lbl_id:
8706 output_attr_index_or_value (a);
8707 break;
8709 case dw_val_class_lineptr:
8710 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8711 debug_line_section, "%s", name);
8712 break;
8714 case dw_val_class_macptr:
8715 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8716 debug_macinfo_section, "%s", name);
8717 break;
8719 case dw_val_class_str:
8720 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
8721 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8722 a->dw_attr_val.v.val_str->label,
8723 debug_str_section,
8724 "%s: \"%s\"", name, AT_string (a));
8725 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
8726 dw2_asm_output_data_uleb128 (AT_index (a),
8727 "%s: \"%s\"", name, AT_string (a));
8728 else
8729 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8730 break;
8732 case dw_val_class_file:
8734 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8736 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8737 a->dw_attr_val.v.val_file->filename);
8738 break;
8741 case dw_val_class_data8:
8743 int i;
8745 for (i = 0; i < 8; i++)
8746 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
8747 i == 0 ? "%s" : NULL, name);
8748 break;
8751 case dw_val_class_high_pc:
8752 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
8753 get_AT_low_pc (die), "DW_AT_high_pc");
8754 break;
8756 default:
8757 gcc_unreachable ();
8761 FOR_EACH_CHILD (die, c, output_die (c));
8763 /* Add null byte to terminate sibling list. */
8764 if (die->die_child != NULL)
8765 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
8766 (unsigned long) die->die_offset);
8769 /* Output the compilation unit that appears at the beginning of the
8770 .debug_info section, and precedes the DIE descriptions. */
8772 static void
8773 output_compilation_unit_header (void)
8775 int ver = dwarf_version;
8777 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8778 dw2_asm_output_data (4, 0xffffffff,
8779 "Initial length escape value indicating 64-bit DWARF extension");
8780 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8781 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8782 "Length of Compilation Unit Info");
8783 dw2_asm_output_data (2, ver, "DWARF version number");
8784 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8785 debug_abbrev_section,
8786 "Offset Into Abbrev. Section");
8787 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8790 /* Output the compilation unit DIE and its children. */
8792 static void
8793 output_comp_unit (dw_die_ref die, int output_if_empty)
8795 const char *secname, *oldsym;
8796 char *tmp;
8797 external_ref_hash_type extern_map;
8799 /* Unless we are outputting main CU, we may throw away empty ones. */
8800 if (!output_if_empty && die->die_child == NULL)
8801 return;
8803 /* Even if there are no children of this DIE, we must output the information
8804 about the compilation unit. Otherwise, on an empty translation unit, we
8805 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
8806 will then complain when examining the file. First mark all the DIEs in
8807 this CU so we know which get local refs. */
8808 mark_dies (die);
8810 extern_map = optimize_external_refs (die);
8812 build_abbrev_table (die, extern_map);
8814 extern_map.dispose ();
8816 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8817 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8818 calc_die_sizes (die);
8820 oldsym = die->die_id.die_symbol;
8821 if (oldsym)
8823 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8825 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8826 secname = tmp;
8827 die->die_id.die_symbol = NULL;
8828 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8830 else
8832 switch_to_section (debug_info_section);
8833 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
8834 info_section_emitted = true;
8837 /* Output debugging information. */
8838 output_compilation_unit_header ();
8839 output_die (die);
8841 /* Leave the marks on the main CU, so we can check them in
8842 output_pubnames. */
8843 if (oldsym)
8845 unmark_dies (die);
8846 die->die_id.die_symbol = oldsym;
8850 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
8851 and .debug_pubtypes. This is configured per-target, but can be
8852 overridden by the -gpubnames or -gno-pubnames options. */
8854 static inline bool
8855 want_pubnames (void)
8857 if (debug_info_level <= DINFO_LEVEL_TERSE)
8858 return false;
8859 if (debug_generate_pub_sections != -1)
8860 return debug_generate_pub_sections;
8861 return targetm.want_debug_pub_sections;
8864 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
8866 static void
8867 add_AT_pubnames (dw_die_ref die)
8869 if (want_pubnames ())
8870 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
8873 /* Add a string attribute value to a skeleton DIE. */
8875 static inline void
8876 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
8877 const char *str)
8879 dw_attr_node attr;
8880 struct indirect_string_node *node;
8882 if (! skeleton_debug_str_hash)
8883 skeleton_debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
8884 debug_str_eq, NULL);
8886 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
8887 find_string_form (node);
8888 if (node->form == DW_FORM_GNU_str_index)
8889 node->form = DW_FORM_strp;
8891 attr.dw_attr = attr_kind;
8892 attr.dw_attr_val.val_class = dw_val_class_str;
8893 attr.dw_attr_val.val_entry = NULL;
8894 attr.dw_attr_val.v.val_str = node;
8895 add_dwarf_attr (die, &attr);
8898 /* Helper function to generate top-level dies for skeleton debug_info and
8899 debug_types. */
8901 static void
8902 add_top_level_skeleton_die_attrs (dw_die_ref die)
8904 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
8905 const char *comp_dir = comp_dir_string ();
8907 add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
8908 if (comp_dir != NULL)
8909 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
8910 add_AT_pubnames (die);
8911 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
8914 /* Return the single type-unit die for skeleton type units. */
8916 static dw_die_ref
8917 get_skeleton_type_unit (void)
8919 /* For dwarf_split_debug_sections with use_type info, all type units in the
8920 skeleton sections have identical dies (but different headers). This
8921 single die will be output many times. */
8923 static dw_die_ref skeleton_type_unit = NULL;
8925 if (skeleton_type_unit == NULL)
8927 skeleton_type_unit = new_die (DW_TAG_type_unit, NULL, NULL);
8928 add_top_level_skeleton_die_attrs (skeleton_type_unit);
8929 skeleton_type_unit->die_abbrev = SKELETON_TYPE_DIE_ABBREV;
8931 return skeleton_type_unit;
8934 /* Output skeleton debug sections that point to the dwo file. */
8936 static void
8937 output_skeleton_debug_sections (dw_die_ref comp_unit)
8939 /* These attributes will be found in the full debug_info section. */
8940 remove_AT (comp_unit, DW_AT_producer);
8941 remove_AT (comp_unit, DW_AT_language);
8943 switch_to_section (debug_skeleton_info_section);
8944 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
8946 /* Produce the skeleton compilation-unit header. This one differs enough from
8947 a normal CU header that it's better not to call output_compilation_unit
8948 header. */
8949 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8950 dw2_asm_output_data (4, 0xffffffff,
8951 "Initial length escape value indicating 64-bit DWARF extension");
8953 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8954 DWARF_COMPILE_UNIT_HEADER_SIZE
8955 - DWARF_INITIAL_LENGTH_SIZE
8956 + size_of_die (comp_unit),
8957 "Length of Compilation Unit Info");
8958 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
8959 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
8960 debug_abbrev_section,
8961 "Offset Into Abbrev. Section");
8962 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8964 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
8965 output_die (comp_unit);
8967 /* Build the skeleton debug_abbrev section. */
8968 switch_to_section (debug_skeleton_abbrev_section);
8969 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
8971 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
8972 if (use_debug_types)
8973 output_die_abbrevs (SKELETON_TYPE_DIE_ABBREV, get_skeleton_type_unit ());
8975 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
8978 /* Output a comdat type unit DIE and its children. */
8980 static void
8981 output_comdat_type_unit (comdat_type_node *node)
8983 const char *secname;
8984 char *tmp;
8985 int i;
8986 #if defined (OBJECT_FORMAT_ELF)
8987 tree comdat_key;
8988 #endif
8989 external_ref_hash_type extern_map;
8991 /* First mark all the DIEs in this CU so we know which get local refs. */
8992 mark_dies (node->root_die);
8994 extern_map = optimize_external_refs (node->root_die);
8996 build_abbrev_table (node->root_die, extern_map);
8998 extern_map.dispose ();
9000 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9001 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
9002 calc_die_sizes (node->root_die);
9004 #if defined (OBJECT_FORMAT_ELF)
9005 if (!dwarf_split_debug_info)
9006 secname = ".debug_types";
9007 else
9008 secname = ".debug_types.dwo";
9010 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9011 sprintf (tmp, "wt.");
9012 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9013 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9014 comdat_key = get_identifier (tmp);
9015 targetm.asm_out.named_section (secname,
9016 SECTION_DEBUG | SECTION_LINKONCE,
9017 comdat_key);
9018 #else
9019 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9020 sprintf (tmp, ".gnu.linkonce.wt.");
9021 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9022 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9023 secname = tmp;
9024 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9025 #endif
9027 /* Output debugging information. */
9028 output_compilation_unit_header ();
9029 output_signature (node->signature, "Type Signature");
9030 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9031 "Offset to Type DIE");
9032 output_die (node->root_die);
9034 unmark_dies (node->root_die);
9036 #if defined (OBJECT_FORMAT_ELF)
9037 if (dwarf_split_debug_info)
9039 /* Produce the skeleton type-unit header. */
9040 const char *secname = ".debug_types";
9042 targetm.asm_out.named_section (secname,
9043 SECTION_DEBUG | SECTION_LINKONCE,
9044 comdat_key);
9045 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9046 dw2_asm_output_data (4, 0xffffffff,
9047 "Initial length escape value indicating 64-bit DWARF extension");
9049 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9050 DWARF_COMPILE_UNIT_HEADER_SIZE
9051 - DWARF_INITIAL_LENGTH_SIZE
9052 + size_of_die (get_skeleton_type_unit ())
9053 + DWARF_TYPE_SIGNATURE_SIZE + DWARF_OFFSET_SIZE,
9054 "Length of Type Unit Info");
9055 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
9056 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
9057 debug_skeleton_abbrev_section_label,
9058 debug_abbrev_section,
9059 "Offset Into Abbrev. Section");
9060 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9061 output_signature (node->signature, "Type Signature");
9062 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, "Offset to Type DIE");
9064 output_die (get_skeleton_type_unit ());
9066 #endif
9069 /* Return the DWARF2/3 pubname associated with a decl. */
9071 static const char *
9072 dwarf2_name (tree decl, int scope)
9074 if (DECL_NAMELESS (decl))
9075 return NULL;
9076 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9079 /* Add a new entry to .debug_pubnames if appropriate. */
9081 static void
9082 add_pubname_string (const char *str, dw_die_ref die)
9084 pubname_entry e;
9086 e.die = die;
9087 e.name = xstrdup (str);
9088 vec_safe_push (pubname_table, e);
9091 static void
9092 add_pubname (tree decl, dw_die_ref die)
9094 if (!want_pubnames ())
9095 return;
9097 /* Don't add items to the table when we expect that the consumer will have
9098 just read the enclosing die. For example, if the consumer is looking at a
9099 class_member, it will either be inside the class already, or will have just
9100 looked up the class to find the member. Either way, searching the class is
9101 faster than searching the index. */
9102 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9103 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9105 const char *name = dwarf2_name (decl, 1);
9107 if (name)
9108 add_pubname_string (name, die);
9112 /* Add an enumerator to the pubnames section. */
9114 static void
9115 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9117 pubname_entry e;
9119 gcc_assert (scope_name);
9120 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9121 e.die = die;
9122 vec_safe_push (pubname_table, e);
9125 /* Add a new entry to .debug_pubtypes if appropriate. */
9127 static void
9128 add_pubtype (tree decl, dw_die_ref die)
9130 pubname_entry e;
9132 if (!want_pubnames ())
9133 return;
9135 if ((TREE_PUBLIC (decl)
9136 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9137 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9139 tree scope = NULL;
9140 const char *scope_name = "";
9141 const char *sep = is_cxx () ? "::" : ".";
9142 const char *name;
9144 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9145 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9147 scope_name = lang_hooks.dwarf_name (scope, 1);
9148 if (scope_name != NULL && scope_name[0] != '\0')
9149 scope_name = concat (scope_name, sep, NULL);
9150 else
9151 scope_name = "";
9154 if (TYPE_P (decl))
9155 name = type_tag (decl);
9156 else
9157 name = lang_hooks.dwarf_name (decl, 1);
9159 /* If we don't have a name for the type, there's no point in adding
9160 it to the table. */
9161 if (name != NULL && name[0] != '\0')
9163 e.die = die;
9164 e.name = concat (scope_name, name, NULL);
9165 vec_safe_push (pubtype_table, e);
9168 /* Although it might be more consistent to add the pubinfo for the
9169 enumerators as their dies are created, they should only be added if the
9170 enum type meets the criteria above. So rather than re-check the parent
9171 enum type whenever an enumerator die is created, just output them all
9172 here. This isn't protected by the name conditional because anonymous
9173 enums don't have names. */
9174 if (die->die_tag == DW_TAG_enumeration_type)
9176 dw_die_ref c;
9178 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9183 /* Output a single entry in the pubnames table. */
9185 static void
9186 output_pubname (dw_offset die_offset, pubname_entry *entry)
9188 dw_die_ref die = entry->die;
9189 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9191 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9193 if (debug_generate_pub_sections == 2)
9195 /* This logic follows gdb's method for determining the value of the flag
9196 byte. */
9197 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9198 switch (die->die_tag)
9200 case DW_TAG_typedef:
9201 case DW_TAG_base_type:
9202 case DW_TAG_subrange_type:
9203 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9204 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9205 break;
9206 case DW_TAG_enumerator:
9207 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9208 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9209 if (!is_cxx () && !is_java ())
9210 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9211 break;
9212 case DW_TAG_subprogram:
9213 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9214 GDB_INDEX_SYMBOL_KIND_FUNCTION);
9215 if (!is_ada ())
9216 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9217 break;
9218 case DW_TAG_constant:
9219 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9220 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9221 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9222 break;
9223 case DW_TAG_variable:
9224 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9225 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9226 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9227 break;
9228 case DW_TAG_namespace:
9229 case DW_TAG_imported_declaration:
9230 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9231 break;
9232 case DW_TAG_class_type:
9233 case DW_TAG_interface_type:
9234 case DW_TAG_structure_type:
9235 case DW_TAG_union_type:
9236 case DW_TAG_enumeration_type:
9237 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9238 if (!is_cxx () && !is_java ())
9239 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9240 break;
9241 default:
9242 /* An unusual tag. Leave the flag-byte empty. */
9243 break;
9245 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9246 "GDB-index flags");
9249 dw2_asm_output_nstring (entry->name, -1, "external name");
9253 /* Output the public names table used to speed up access to externally
9254 visible names; or the public types table used to find type definitions. */
9256 static void
9257 output_pubnames (vec<pubname_entry, va_gc> *names)
9259 unsigned i;
9260 unsigned long pubnames_length = size_of_pubnames (names);
9261 pubname_ref pub;
9263 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9264 dw2_asm_output_data (4, 0xffffffff,
9265 "Initial length escape value indicating 64-bit DWARF extension");
9266 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length, "Pub Info Length");
9268 /* Version number for pubnames/pubtypes is independent of dwarf version. */
9269 dw2_asm_output_data (2, 2, "DWARF Version");
9271 if (dwarf_split_debug_info)
9272 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9273 debug_skeleton_info_section,
9274 "Offset of Compilation Unit Info");
9275 else
9276 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9277 debug_info_section,
9278 "Offset of Compilation Unit Info");
9279 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9280 "Compilation Unit Length");
9282 FOR_EACH_VEC_ELT (*names, i, pub)
9284 if (include_pubname_in_output (names, pub))
9286 dw_offset die_offset = pub->die->die_offset;
9288 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9289 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9290 gcc_assert (pub->die->die_mark);
9292 /* If we're putting types in their own .debug_types sections,
9293 the .debug_pubtypes table will still point to the compile
9294 unit (not the type unit), so we want to use the offset of
9295 the skeleton DIE (if there is one). */
9296 if (pub->die->comdat_type_p && names == pubtype_table)
9298 comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
9300 if (type_node != NULL)
9301 die_offset = (type_node->skeleton_die != NULL
9302 ? type_node->skeleton_die->die_offset
9303 : comp_unit_die ()->die_offset);
9306 output_pubname (die_offset, pub);
9310 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9313 /* Output public names and types tables if necessary. */
9315 static void
9316 output_pubtables (void)
9318 if (!want_pubnames () || !info_section_emitted)
9319 return;
9321 switch_to_section (debug_pubnames_section);
9322 output_pubnames (pubname_table);
9323 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
9324 It shouldn't hurt to emit it always, since pure DWARF2 consumers
9325 simply won't look for the section. */
9326 switch_to_section (debug_pubtypes_section);
9327 output_pubnames (pubtype_table);
9331 /* Output the information that goes into the .debug_aranges table.
9332 Namely, define the beginning and ending address range of the
9333 text section generated for this compilation unit. */
9335 static void
9336 output_aranges (unsigned long aranges_length)
9338 unsigned i;
9340 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9341 dw2_asm_output_data (4, 0xffffffff,
9342 "Initial length escape value indicating 64-bit DWARF extension");
9343 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9344 "Length of Address Ranges Info");
9345 /* Version number for aranges is still 2, even in DWARF3. */
9346 dw2_asm_output_data (2, 2, "DWARF Version");
9347 if (dwarf_split_debug_info)
9348 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9349 debug_skeleton_info_section,
9350 "Offset of Compilation Unit Info");
9351 else
9352 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9353 debug_info_section,
9354 "Offset of Compilation Unit Info");
9355 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9356 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9358 /* We need to align to twice the pointer size here. */
9359 if (DWARF_ARANGES_PAD_SIZE)
9361 /* Pad using a 2 byte words so that padding is correct for any
9362 pointer size. */
9363 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9364 2 * DWARF2_ADDR_SIZE);
9365 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9366 dw2_asm_output_data (2, 0, NULL);
9369 /* It is necessary not to output these entries if the sections were
9370 not used; if the sections were not used, the length will be 0 and
9371 the address may end up as 0 if the section is discarded by ld
9372 --gc-sections, leaving an invalid (0, 0) entry that can be
9373 confused with the terminator. */
9374 if (text_section_used)
9376 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9377 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9378 text_section_label, "Length");
9380 if (cold_text_section_used)
9382 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9383 "Address");
9384 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9385 cold_text_section_label, "Length");
9388 if (have_multiple_function_sections)
9390 unsigned fde_idx;
9391 dw_fde_ref fde;
9393 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9395 if (DECL_IGNORED_P (fde->decl))
9396 continue;
9397 if (!fde->in_std_section)
9399 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9400 "Address");
9401 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9402 fde->dw_fde_begin, "Length");
9404 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9406 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9407 "Address");
9408 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9409 fde->dw_fde_second_begin, "Length");
9414 /* Output the terminator words. */
9415 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9416 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9419 /* Add a new entry to .debug_ranges. Return the offset at which it
9420 was placed. */
9422 static unsigned int
9423 add_ranges_num (int num)
9425 unsigned int in_use = ranges_table_in_use;
9427 if (in_use == ranges_table_allocated)
9429 ranges_table_allocated += RANGES_TABLE_INCREMENT;
9430 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9431 ranges_table_allocated);
9432 memset (ranges_table + ranges_table_in_use, 0,
9433 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9436 ranges_table[in_use].num = num;
9437 ranges_table_in_use = in_use + 1;
9439 return in_use * 2 * DWARF2_ADDR_SIZE;
9442 /* Add a new entry to .debug_ranges corresponding to a block, or a
9443 range terminator if BLOCK is NULL. */
9445 static unsigned int
9446 add_ranges (const_tree block)
9448 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9451 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9452 When using dwarf_split_debug_info, address attributes in dies destined
9453 for the final executable should be direct references--setting the
9454 parameter force_direct ensures this behavior. */
9456 static void
9457 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9458 bool *added, bool force_direct)
9460 unsigned int in_use = ranges_by_label_in_use;
9461 unsigned int offset;
9463 if (in_use == ranges_by_label_allocated)
9465 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9466 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9467 ranges_by_label,
9468 ranges_by_label_allocated);
9469 memset (ranges_by_label + ranges_by_label_in_use, 0,
9470 RANGES_TABLE_INCREMENT
9471 * sizeof (struct dw_ranges_by_label_struct));
9474 ranges_by_label[in_use].begin = begin;
9475 ranges_by_label[in_use].end = end;
9476 ranges_by_label_in_use = in_use + 1;
9478 offset = add_ranges_num (-(int)in_use - 1);
9479 if (!*added)
9481 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9482 *added = true;
9486 static void
9487 output_ranges (void)
9489 unsigned i;
9490 static const char *const start_fmt = "Offset %#x";
9491 const char *fmt = start_fmt;
9493 for (i = 0; i < ranges_table_in_use; i++)
9495 int block_num = ranges_table[i].num;
9497 if (block_num > 0)
9499 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9500 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9502 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9503 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9505 /* If all code is in the text section, then the compilation
9506 unit base address defaults to DW_AT_low_pc, which is the
9507 base of the text section. */
9508 if (!have_multiple_function_sections)
9510 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9511 text_section_label,
9512 fmt, i * 2 * DWARF2_ADDR_SIZE);
9513 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9514 text_section_label, NULL);
9517 /* Otherwise, the compilation unit base address is zero,
9518 which allows us to use absolute addresses, and not worry
9519 about whether the target supports cross-section
9520 arithmetic. */
9521 else
9523 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9524 fmt, i * 2 * DWARF2_ADDR_SIZE);
9525 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9528 fmt = NULL;
9531 /* Negative block_num stands for an index into ranges_by_label. */
9532 else if (block_num < 0)
9534 int lab_idx = - block_num - 1;
9536 if (!have_multiple_function_sections)
9538 gcc_unreachable ();
9539 #if 0
9540 /* If we ever use add_ranges_by_labels () for a single
9541 function section, all we have to do is to take out
9542 the #if 0 above. */
9543 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9544 ranges_by_label[lab_idx].begin,
9545 text_section_label,
9546 fmt, i * 2 * DWARF2_ADDR_SIZE);
9547 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9548 ranges_by_label[lab_idx].end,
9549 text_section_label, NULL);
9550 #endif
9552 else
9554 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9555 ranges_by_label[lab_idx].begin,
9556 fmt, i * 2 * DWARF2_ADDR_SIZE);
9557 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9558 ranges_by_label[lab_idx].end,
9559 NULL);
9562 else
9564 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9565 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9566 fmt = start_fmt;
9571 /* Data structure containing information about input files. */
9572 struct file_info
9574 const char *path; /* Complete file name. */
9575 const char *fname; /* File name part. */
9576 int length; /* Length of entire string. */
9577 struct dwarf_file_data * file_idx; /* Index in input file table. */
9578 int dir_idx; /* Index in directory table. */
9581 /* Data structure containing information about directories with source
9582 files. */
9583 struct dir_info
9585 const char *path; /* Path including directory name. */
9586 int length; /* Path length. */
9587 int prefix; /* Index of directory entry which is a prefix. */
9588 int count; /* Number of files in this directory. */
9589 int dir_idx; /* Index of directory used as base. */
9592 /* Callback function for file_info comparison. We sort by looking at
9593 the directories in the path. */
9595 static int
9596 file_info_cmp (const void *p1, const void *p2)
9598 const struct file_info *const s1 = (const struct file_info *) p1;
9599 const struct file_info *const s2 = (const struct file_info *) p2;
9600 const unsigned char *cp1;
9601 const unsigned char *cp2;
9603 /* Take care of file names without directories. We need to make sure that
9604 we return consistent values to qsort since some will get confused if
9605 we return the same value when identical operands are passed in opposite
9606 orders. So if neither has a directory, return 0 and otherwise return
9607 1 or -1 depending on which one has the directory. */
9608 if ((s1->path == s1->fname || s2->path == s2->fname))
9609 return (s2->path == s2->fname) - (s1->path == s1->fname);
9611 cp1 = (const unsigned char *) s1->path;
9612 cp2 = (const unsigned char *) s2->path;
9614 while (1)
9616 ++cp1;
9617 ++cp2;
9618 /* Reached the end of the first path? If so, handle like above. */
9619 if ((cp1 == (const unsigned char *) s1->fname)
9620 || (cp2 == (const unsigned char *) s2->fname))
9621 return ((cp2 == (const unsigned char *) s2->fname)
9622 - (cp1 == (const unsigned char *) s1->fname));
9624 /* Character of current path component the same? */
9625 else if (*cp1 != *cp2)
9626 return *cp1 - *cp2;
9630 struct file_name_acquire_data
9632 struct file_info *files;
9633 int used_files;
9634 int max_files;
9637 /* Traversal function for the hash table. */
9639 static int
9640 file_name_acquire (void ** slot, void *data)
9642 struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
9643 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
9644 struct file_info *fi;
9645 const char *f;
9647 gcc_assert (fnad->max_files >= d->emitted_number);
9649 if (! d->emitted_number)
9650 return 1;
9652 gcc_assert (fnad->max_files != fnad->used_files);
9654 fi = fnad->files + fnad->used_files++;
9656 /* Skip all leading "./". */
9657 f = d->filename;
9658 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9659 f += 2;
9661 /* Create a new array entry. */
9662 fi->path = f;
9663 fi->length = strlen (f);
9664 fi->file_idx = d;
9666 /* Search for the file name part. */
9667 f = strrchr (f, DIR_SEPARATOR);
9668 #if defined (DIR_SEPARATOR_2)
9670 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9672 if (g != NULL)
9674 if (f == NULL || f < g)
9675 f = g;
9678 #endif
9680 fi->fname = f == NULL ? fi->path : f + 1;
9681 return 1;
9684 /* Output the directory table and the file name table. We try to minimize
9685 the total amount of memory needed. A heuristic is used to avoid large
9686 slowdowns with many input files. */
9688 static void
9689 output_file_names (void)
9691 struct file_name_acquire_data fnad;
9692 int numfiles;
9693 struct file_info *files;
9694 struct dir_info *dirs;
9695 int *saved;
9696 int *savehere;
9697 int *backmap;
9698 int ndirs;
9699 int idx_offset;
9700 int i;
9702 if (!last_emitted_file)
9704 dw2_asm_output_data (1, 0, "End directory table");
9705 dw2_asm_output_data (1, 0, "End file name table");
9706 return;
9709 numfiles = last_emitted_file->emitted_number;
9711 /* Allocate the various arrays we need. */
9712 files = XALLOCAVEC (struct file_info, numfiles);
9713 dirs = XALLOCAVEC (struct dir_info, numfiles);
9715 fnad.files = files;
9716 fnad.used_files = 0;
9717 fnad.max_files = numfiles;
9718 htab_traverse (file_table, file_name_acquire, &fnad);
9719 gcc_assert (fnad.used_files == fnad.max_files);
9721 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9723 /* Find all the different directories used. */
9724 dirs[0].path = files[0].path;
9725 dirs[0].length = files[0].fname - files[0].path;
9726 dirs[0].prefix = -1;
9727 dirs[0].count = 1;
9728 dirs[0].dir_idx = 0;
9729 files[0].dir_idx = 0;
9730 ndirs = 1;
9732 for (i = 1; i < numfiles; i++)
9733 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9734 && memcmp (dirs[ndirs - 1].path, files[i].path,
9735 dirs[ndirs - 1].length) == 0)
9737 /* Same directory as last entry. */
9738 files[i].dir_idx = ndirs - 1;
9739 ++dirs[ndirs - 1].count;
9741 else
9743 int j;
9745 /* This is a new directory. */
9746 dirs[ndirs].path = files[i].path;
9747 dirs[ndirs].length = files[i].fname - files[i].path;
9748 dirs[ndirs].count = 1;
9749 dirs[ndirs].dir_idx = ndirs;
9750 files[i].dir_idx = ndirs;
9752 /* Search for a prefix. */
9753 dirs[ndirs].prefix = -1;
9754 for (j = 0; j < ndirs; j++)
9755 if (dirs[j].length < dirs[ndirs].length
9756 && dirs[j].length > 1
9757 && (dirs[ndirs].prefix == -1
9758 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9759 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9760 dirs[ndirs].prefix = j;
9762 ++ndirs;
9765 /* Now to the actual work. We have to find a subset of the directories which
9766 allow expressing the file name using references to the directory table
9767 with the least amount of characters. We do not do an exhaustive search
9768 where we would have to check out every combination of every single
9769 possible prefix. Instead we use a heuristic which provides nearly optimal
9770 results in most cases and never is much off. */
9771 saved = XALLOCAVEC (int, ndirs);
9772 savehere = XALLOCAVEC (int, ndirs);
9774 memset (saved, '\0', ndirs * sizeof (saved[0]));
9775 for (i = 0; i < ndirs; i++)
9777 int j;
9778 int total;
9780 /* We can always save some space for the current directory. But this
9781 does not mean it will be enough to justify adding the directory. */
9782 savehere[i] = dirs[i].length;
9783 total = (savehere[i] - saved[i]) * dirs[i].count;
9785 for (j = i + 1; j < ndirs; j++)
9787 savehere[j] = 0;
9788 if (saved[j] < dirs[i].length)
9790 /* Determine whether the dirs[i] path is a prefix of the
9791 dirs[j] path. */
9792 int k;
9794 k = dirs[j].prefix;
9795 while (k != -1 && k != (int) i)
9796 k = dirs[k].prefix;
9798 if (k == (int) i)
9800 /* Yes it is. We can possibly save some memory by
9801 writing the filenames in dirs[j] relative to
9802 dirs[i]. */
9803 savehere[j] = dirs[i].length;
9804 total += (savehere[j] - saved[j]) * dirs[j].count;
9809 /* Check whether we can save enough to justify adding the dirs[i]
9810 directory. */
9811 if (total > dirs[i].length + 1)
9813 /* It's worthwhile adding. */
9814 for (j = i; j < ndirs; j++)
9815 if (savehere[j] > 0)
9817 /* Remember how much we saved for this directory so far. */
9818 saved[j] = savehere[j];
9820 /* Remember the prefix directory. */
9821 dirs[j].dir_idx = i;
9826 /* Emit the directory name table. */
9827 idx_offset = dirs[0].length > 0 ? 1 : 0;
9828 for (i = 1 - idx_offset; i < ndirs; i++)
9829 dw2_asm_output_nstring (dirs[i].path,
9830 dirs[i].length
9831 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9832 "Directory Entry: %#x", i + idx_offset);
9834 dw2_asm_output_data (1, 0, "End directory table");
9836 /* We have to emit them in the order of emitted_number since that's
9837 used in the debug info generation. To do this efficiently we
9838 generate a back-mapping of the indices first. */
9839 backmap = XALLOCAVEC (int, numfiles);
9840 for (i = 0; i < numfiles; i++)
9841 backmap[files[i].file_idx->emitted_number - 1] = i;
9843 /* Now write all the file names. */
9844 for (i = 0; i < numfiles; i++)
9846 int file_idx = backmap[i];
9847 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9849 #ifdef VMS_DEBUGGING_INFO
9850 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
9852 /* Setting these fields can lead to debugger miscomparisons,
9853 but VMS Debug requires them to be set correctly. */
9855 int ver;
9856 long long cdt;
9857 long siz;
9858 int maxfilelen = strlen (files[file_idx].path)
9859 + dirs[dir_idx].length
9860 + MAX_VMS_VERSION_LEN + 1;
9861 char *filebuf = XALLOCAVEC (char, maxfilelen);
9863 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
9864 snprintf (filebuf, maxfilelen, "%s;%d",
9865 files[file_idx].path + dirs[dir_idx].length, ver);
9867 dw2_asm_output_nstring
9868 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
9870 /* Include directory index. */
9871 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9873 /* Modification time. */
9874 dw2_asm_output_data_uleb128
9875 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
9876 ? cdt : 0,
9877 NULL);
9879 /* File length in bytes. */
9880 dw2_asm_output_data_uleb128
9881 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
9882 ? siz : 0,
9883 NULL);
9884 #else
9885 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
9886 "File Entry: %#x", (unsigned) i + 1);
9888 /* Include directory index. */
9889 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9891 /* Modification time. */
9892 dw2_asm_output_data_uleb128 (0, NULL);
9894 /* File length in bytes. */
9895 dw2_asm_output_data_uleb128 (0, NULL);
9896 #endif /* VMS_DEBUGGING_INFO */
9899 dw2_asm_output_data (1, 0, "End file name table");
9903 /* Output one line number table into the .debug_line section. */
9905 static void
9906 output_one_line_info_table (dw_line_info_table *table)
9908 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9909 unsigned int current_line = 1;
9910 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
9911 dw_line_info_entry *ent;
9912 size_t i;
9914 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
9916 switch (ent->opcode)
9918 case LI_set_address:
9919 /* ??? Unfortunately, we have little choice here currently, and
9920 must always use the most general form. GCC does not know the
9921 address delta itself, so we can't use DW_LNS_advance_pc. Many
9922 ports do have length attributes which will give an upper bound
9923 on the address range. We could perhaps use length attributes
9924 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
9925 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
9927 /* This can handle any delta. This takes
9928 4+DWARF2_ADDR_SIZE bytes. */
9929 dw2_asm_output_data (1, 0, "set address %s", line_label);
9930 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9931 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9932 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9933 break;
9935 case LI_set_line:
9936 if (ent->val == current_line)
9938 /* We still need to start a new row, so output a copy insn. */
9939 dw2_asm_output_data (1, DW_LNS_copy,
9940 "copy line %u", current_line);
9942 else
9944 int line_offset = ent->val - current_line;
9945 int line_delta = line_offset - DWARF_LINE_BASE;
9947 current_line = ent->val;
9948 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9950 /* This can handle deltas from -10 to 234, using the current
9951 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
9952 This takes 1 byte. */
9953 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9954 "line %u", current_line);
9956 else
9958 /* This can handle any delta. This takes at least 4 bytes,
9959 depending on the value being encoded. */
9960 dw2_asm_output_data (1, DW_LNS_advance_line,
9961 "advance to line %u", current_line);
9962 dw2_asm_output_data_sleb128 (line_offset, NULL);
9963 dw2_asm_output_data (1, DW_LNS_copy, NULL);
9966 break;
9968 case LI_set_file:
9969 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
9970 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9971 break;
9973 case LI_set_column:
9974 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
9975 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9976 break;
9978 case LI_negate_stmt:
9979 current_is_stmt = !current_is_stmt;
9980 dw2_asm_output_data (1, DW_LNS_negate_stmt,
9981 "is_stmt %d", current_is_stmt);
9982 break;
9984 case LI_set_prologue_end:
9985 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
9986 "set prologue end");
9987 break;
9989 case LI_set_epilogue_begin:
9990 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
9991 "set epilogue begin");
9992 break;
9994 case LI_set_discriminator:
9995 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
9996 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
9997 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
9998 dw2_asm_output_data_uleb128 (ent->val, NULL);
9999 break;
10003 /* Emit debug info for the address of the end of the table. */
10004 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
10005 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10006 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10007 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
10009 dw2_asm_output_data (1, 0, "end sequence");
10010 dw2_asm_output_data_uleb128 (1, NULL);
10011 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10014 /* Output the source line number correspondence information. This
10015 information goes into the .debug_line section. */
10017 static void
10018 output_line_info (bool prologue_only)
10020 char l1[20], l2[20], p1[20], p2[20];
10021 int ver = dwarf_version;
10022 bool saw_one = false;
10023 int opc;
10025 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
10026 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
10027 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
10028 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
10030 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10031 dw2_asm_output_data (4, 0xffffffff,
10032 "Initial length escape value indicating 64-bit DWARF extension");
10033 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10034 "Length of Source Line Info");
10035 ASM_OUTPUT_LABEL (asm_out_file, l1);
10037 dw2_asm_output_data (2, ver, "DWARF Version");
10038 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10039 ASM_OUTPUT_LABEL (asm_out_file, p1);
10041 /* Define the architecture-dependent minimum instruction length (in bytes).
10042 In this implementation of DWARF, this field is used for information
10043 purposes only. Since GCC generates assembly language, we have no
10044 a priori knowledge of how many instruction bytes are generated for each
10045 source line, and therefore can use only the DW_LNE_set_address and
10046 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
10047 this as '1', which is "correct enough" for all architectures,
10048 and don't let the target override. */
10049 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10051 if (ver >= 4)
10052 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10053 "Maximum Operations Per Instruction");
10054 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10055 "Default is_stmt_start flag");
10056 dw2_asm_output_data (1, DWARF_LINE_BASE,
10057 "Line Base Value (Special Opcodes)");
10058 dw2_asm_output_data (1, DWARF_LINE_RANGE,
10059 "Line Range Value (Special Opcodes)");
10060 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10061 "Special Opcode Base");
10063 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10065 int n_op_args;
10066 switch (opc)
10068 case DW_LNS_advance_pc:
10069 case DW_LNS_advance_line:
10070 case DW_LNS_set_file:
10071 case DW_LNS_set_column:
10072 case DW_LNS_fixed_advance_pc:
10073 case DW_LNS_set_isa:
10074 n_op_args = 1;
10075 break;
10076 default:
10077 n_op_args = 0;
10078 break;
10081 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10082 opc, n_op_args);
10085 /* Write out the information about the files we use. */
10086 output_file_names ();
10087 ASM_OUTPUT_LABEL (asm_out_file, p2);
10088 if (prologue_only)
10090 /* Output the marker for the end of the line number info. */
10091 ASM_OUTPUT_LABEL (asm_out_file, l2);
10092 return;
10095 if (separate_line_info)
10097 dw_line_info_table *table;
10098 size_t i;
10100 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10101 if (table->in_use)
10103 output_one_line_info_table (table);
10104 saw_one = true;
10107 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10109 output_one_line_info_table (cold_text_section_line_info);
10110 saw_one = true;
10113 /* ??? Some Darwin linkers crash on a .debug_line section with no
10114 sequences. Further, merely a DW_LNE_end_sequence entry is not
10115 sufficient -- the address column must also be initialized.
10116 Make sure to output at least one set_address/end_sequence pair,
10117 choosing .text since that section is always present. */
10118 if (text_section_line_info->in_use || !saw_one)
10119 output_one_line_info_table (text_section_line_info);
10121 /* Output the marker for the end of the line number info. */
10122 ASM_OUTPUT_LABEL (asm_out_file, l2);
10125 /* Given a pointer to a tree node for some base type, return a pointer to
10126 a DIE that describes the given type.
10128 This routine must only be called for GCC type nodes that correspond to
10129 Dwarf base (fundamental) types. */
10131 static dw_die_ref
10132 base_type_die (tree type)
10134 dw_die_ref base_type_result;
10135 enum dwarf_type encoding;
10137 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10138 return 0;
10140 /* If this is a subtype that should not be emitted as a subrange type,
10141 use the base type. See subrange_type_for_debug_p. */
10142 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10143 type = TREE_TYPE (type);
10145 switch (TREE_CODE (type))
10147 case INTEGER_TYPE:
10148 if ((dwarf_version >= 4 || !dwarf_strict)
10149 && TYPE_NAME (type)
10150 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10151 && DECL_IS_BUILTIN (TYPE_NAME (type))
10152 && DECL_NAME (TYPE_NAME (type)))
10154 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10155 if (strcmp (name, "char16_t") == 0
10156 || strcmp (name, "char32_t") == 0)
10158 encoding = DW_ATE_UTF;
10159 break;
10162 if (TYPE_STRING_FLAG (type))
10164 if (TYPE_UNSIGNED (type))
10165 encoding = DW_ATE_unsigned_char;
10166 else
10167 encoding = DW_ATE_signed_char;
10169 else if (TYPE_UNSIGNED (type))
10170 encoding = DW_ATE_unsigned;
10171 else
10172 encoding = DW_ATE_signed;
10173 break;
10175 case REAL_TYPE:
10176 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10178 if (dwarf_version >= 3 || !dwarf_strict)
10179 encoding = DW_ATE_decimal_float;
10180 else
10181 encoding = DW_ATE_lo_user;
10183 else
10184 encoding = DW_ATE_float;
10185 break;
10187 case FIXED_POINT_TYPE:
10188 if (!(dwarf_version >= 3 || !dwarf_strict))
10189 encoding = DW_ATE_lo_user;
10190 else if (TYPE_UNSIGNED (type))
10191 encoding = DW_ATE_unsigned_fixed;
10192 else
10193 encoding = DW_ATE_signed_fixed;
10194 break;
10196 /* Dwarf2 doesn't know anything about complex ints, so use
10197 a user defined type for it. */
10198 case COMPLEX_TYPE:
10199 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10200 encoding = DW_ATE_complex_float;
10201 else
10202 encoding = DW_ATE_lo_user;
10203 break;
10205 case BOOLEAN_TYPE:
10206 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
10207 encoding = DW_ATE_boolean;
10208 break;
10210 default:
10211 /* No other TREE_CODEs are Dwarf fundamental types. */
10212 gcc_unreachable ();
10215 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10217 add_AT_unsigned (base_type_result, DW_AT_byte_size,
10218 int_size_in_bytes (type));
10219 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10220 add_pubtype (type, base_type_result);
10222 return base_type_result;
10225 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
10226 named 'auto' in its type: return true for it, false otherwise. */
10228 static inline bool
10229 is_cxx_auto (tree type)
10231 if (is_cxx ())
10233 tree name = TYPE_NAME (type);
10234 if (TREE_CODE (name) == TYPE_DECL)
10235 name = DECL_NAME (name);
10236 if (name == get_identifier ("auto")
10237 || name == get_identifier ("decltype(auto)"))
10238 return true;
10240 return false;
10243 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10244 given input type is a Dwarf "fundamental" type. Otherwise return null. */
10246 static inline int
10247 is_base_type (tree type)
10249 switch (TREE_CODE (type))
10251 case ERROR_MARK:
10252 case VOID_TYPE:
10253 case INTEGER_TYPE:
10254 case REAL_TYPE:
10255 case FIXED_POINT_TYPE:
10256 case COMPLEX_TYPE:
10257 case BOOLEAN_TYPE:
10258 return 1;
10260 case ARRAY_TYPE:
10261 case RECORD_TYPE:
10262 case UNION_TYPE:
10263 case QUAL_UNION_TYPE:
10264 case ENUMERAL_TYPE:
10265 case FUNCTION_TYPE:
10266 case METHOD_TYPE:
10267 case POINTER_TYPE:
10268 case REFERENCE_TYPE:
10269 case NULLPTR_TYPE:
10270 case OFFSET_TYPE:
10271 case LANG_TYPE:
10272 case VECTOR_TYPE:
10273 return 0;
10275 default:
10276 if (is_cxx_auto (type))
10277 return 0;
10278 gcc_unreachable ();
10281 return 0;
10284 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10285 node, return the size in bits for the type if it is a constant, or else
10286 return the alignment for the type if the type's size is not constant, or
10287 else return BITS_PER_WORD if the type actually turns out to be an
10288 ERROR_MARK node. */
10290 static inline unsigned HOST_WIDE_INT
10291 simple_type_size_in_bits (const_tree type)
10293 if (TREE_CODE (type) == ERROR_MARK)
10294 return BITS_PER_WORD;
10295 else if (TYPE_SIZE (type) == NULL_TREE)
10296 return 0;
10297 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
10298 return tree_to_uhwi (TYPE_SIZE (type));
10299 else
10300 return TYPE_ALIGN (type);
10303 /* Similarly, but return a double_int instead of UHWI. */
10305 static inline double_int
10306 double_int_type_size_in_bits (const_tree type)
10308 if (TREE_CODE (type) == ERROR_MARK)
10309 return double_int::from_uhwi (BITS_PER_WORD);
10310 else if (TYPE_SIZE (type) == NULL_TREE)
10311 return double_int_zero;
10312 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10313 return tree_to_double_int (TYPE_SIZE (type));
10314 else
10315 return double_int::from_uhwi (TYPE_ALIGN (type));
10318 /* Given a pointer to a tree node for a subrange type, return a pointer
10319 to a DIE that describes the given type. */
10321 static dw_die_ref
10322 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10324 dw_die_ref subrange_die;
10325 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10327 if (context_die == NULL)
10328 context_die = comp_unit_die ();
10330 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10332 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10334 /* The size of the subrange type and its base type do not match,
10335 so we need to generate a size attribute for the subrange type. */
10336 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10339 if (low)
10340 add_bound_info (subrange_die, DW_AT_lower_bound, low);
10341 if (high)
10342 add_bound_info (subrange_die, DW_AT_upper_bound, high);
10344 return subrange_die;
10347 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10348 entry that chains various modifiers in front of the given type. */
10350 static dw_die_ref
10351 modified_type_die (tree type, int is_const_type, int is_volatile_type,
10352 dw_die_ref context_die)
10354 enum tree_code code = TREE_CODE (type);
10355 dw_die_ref mod_type_die;
10356 dw_die_ref sub_die = NULL;
10357 tree item_type = NULL;
10358 tree qualified_type;
10359 tree name, low, high;
10360 dw_die_ref mod_scope;
10362 if (code == ERROR_MARK)
10363 return NULL;
10365 /* See if we already have the appropriately qualified variant of
10366 this type. */
10367 qualified_type
10368 = get_qualified_type (type,
10369 ((is_const_type ? TYPE_QUAL_CONST : 0)
10370 | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
10372 if (qualified_type == sizetype
10373 && TYPE_NAME (qualified_type)
10374 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10376 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10378 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10379 && TYPE_PRECISION (t)
10380 == TYPE_PRECISION (qualified_type)
10381 && TYPE_UNSIGNED (t)
10382 == TYPE_UNSIGNED (qualified_type));
10383 qualified_type = t;
10386 /* If we do, then we can just use its DIE, if it exists. */
10387 if (qualified_type)
10389 mod_type_die = lookup_type_die (qualified_type);
10390 if (mod_type_die)
10391 return mod_type_die;
10394 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10396 /* Handle C typedef types. */
10397 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10398 && !DECL_ARTIFICIAL (name))
10400 tree dtype = TREE_TYPE (name);
10402 if (qualified_type == dtype)
10404 /* For a named type, use the typedef. */
10405 gen_type_die (qualified_type, context_die);
10406 return lookup_type_die (qualified_type);
10408 else if (is_const_type < TYPE_READONLY (dtype)
10409 || is_volatile_type < TYPE_VOLATILE (dtype)
10410 || (is_const_type <= TYPE_READONLY (dtype)
10411 && is_volatile_type <= TYPE_VOLATILE (dtype)
10412 && DECL_ORIGINAL_TYPE (name) != type))
10413 /* cv-unqualified version of named type. Just use the unnamed
10414 type to which it refers. */
10415 return modified_type_die (DECL_ORIGINAL_TYPE (name),
10416 is_const_type, is_volatile_type,
10417 context_die);
10418 /* Else cv-qualified version of named type; fall through. */
10421 mod_scope = scope_die_for (type, context_die);
10423 if (is_const_type
10424 /* If both is_const_type and is_volatile_type, prefer the path
10425 which leads to a qualified type. */
10426 && (!is_volatile_type
10427 || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
10428 || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
10430 mod_type_die = new_die (DW_TAG_const_type, mod_scope, type);
10431 sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
10433 else if (is_volatile_type)
10435 mod_type_die = new_die (DW_TAG_volatile_type, mod_scope, type);
10436 sub_die = modified_type_die (type, is_const_type, 0, context_die);
10438 else if (code == POINTER_TYPE)
10440 mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10441 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10442 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10443 item_type = TREE_TYPE (type);
10444 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10445 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10446 TYPE_ADDR_SPACE (item_type));
10448 else if (code == REFERENCE_TYPE)
10450 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10451 mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10452 type);
10453 else
10454 mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10455 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10456 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10457 item_type = TREE_TYPE (type);
10458 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10459 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10460 TYPE_ADDR_SPACE (item_type));
10462 else if (code == INTEGER_TYPE
10463 && TREE_TYPE (type) != NULL_TREE
10464 && subrange_type_for_debug_p (type, &low, &high))
10466 mod_type_die = subrange_type_die (type, low, high, context_die);
10467 item_type = TREE_TYPE (type);
10469 else if (is_base_type (type))
10470 mod_type_die = base_type_die (type);
10471 else
10473 gen_type_die (type, context_die);
10475 /* We have to get the type_main_variant here (and pass that to the
10476 `lookup_type_die' routine) because the ..._TYPE node we have
10477 might simply be a *copy* of some original type node (where the
10478 copy was created to help us keep track of typedef names) and
10479 that copy might have a different TYPE_UID from the original
10480 ..._TYPE node. */
10481 if (TREE_CODE (type) != VECTOR_TYPE)
10482 return lookup_type_die (type_main_variant (type));
10483 else
10484 /* Vectors have the debugging information in the type,
10485 not the main variant. */
10486 return lookup_type_die (type);
10489 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
10490 don't output a DW_TAG_typedef, since there isn't one in the
10491 user's program; just attach a DW_AT_name to the type.
10492 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10493 if the base type already has the same name. */
10494 if (name
10495 && ((TREE_CODE (name) != TYPE_DECL
10496 && (qualified_type == TYPE_MAIN_VARIANT (type)
10497 || (!is_const_type && !is_volatile_type)))
10498 || (TREE_CODE (name) == TYPE_DECL
10499 && TREE_TYPE (name) == qualified_type
10500 && DECL_NAME (name))))
10502 if (TREE_CODE (name) == TYPE_DECL)
10503 /* Could just call add_name_and_src_coords_attributes here,
10504 but since this is a builtin type it doesn't have any
10505 useful source coordinates anyway. */
10506 name = DECL_NAME (name);
10507 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10509 /* This probably indicates a bug. */
10510 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10512 name = TYPE_NAME (type);
10513 if (name
10514 && TREE_CODE (name) == TYPE_DECL)
10515 name = DECL_NAME (name);
10516 add_name_attribute (mod_type_die,
10517 name ? IDENTIFIER_POINTER (name) : "__unknown__");
10520 if (qualified_type)
10521 equate_type_number_to_die (qualified_type, mod_type_die);
10523 if (item_type)
10524 /* We must do this after the equate_type_number_to_die call, in case
10525 this is a recursive type. This ensures that the modified_type_die
10526 recursion will terminate even if the type is recursive. Recursive
10527 types are possible in Ada. */
10528 sub_die = modified_type_die (item_type,
10529 TYPE_READONLY (item_type),
10530 TYPE_VOLATILE (item_type),
10531 context_die);
10533 if (sub_die != NULL)
10534 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10536 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10537 if (TYPE_ARTIFICIAL (type))
10538 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10540 return mod_type_die;
10543 /* Generate DIEs for the generic parameters of T.
10544 T must be either a generic type or a generic function.
10545 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
10547 static void
10548 gen_generic_params_dies (tree t)
10550 tree parms, args;
10551 int parms_num, i;
10552 dw_die_ref die = NULL;
10553 int non_default;
10555 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10556 return;
10558 if (TYPE_P (t))
10559 die = lookup_type_die (t);
10560 else if (DECL_P (t))
10561 die = lookup_decl_die (t);
10563 gcc_assert (die);
10565 parms = lang_hooks.get_innermost_generic_parms (t);
10566 if (!parms)
10567 /* T has no generic parameter. It means T is neither a generic type
10568 or function. End of story. */
10569 return;
10571 parms_num = TREE_VEC_LENGTH (parms);
10572 args = lang_hooks.get_innermost_generic_args (t);
10573 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
10574 non_default = int_cst_value (TREE_CHAIN (args));
10575 else
10576 non_default = TREE_VEC_LENGTH (args);
10577 for (i = 0; i < parms_num; i++)
10579 tree parm, arg, arg_pack_elems;
10580 dw_die_ref parm_die;
10582 parm = TREE_VEC_ELT (parms, i);
10583 arg = TREE_VEC_ELT (args, i);
10584 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
10585 gcc_assert (parm && TREE_VALUE (parm) && arg);
10587 if (parm && TREE_VALUE (parm) && arg)
10589 /* If PARM represents a template parameter pack,
10590 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
10591 by DW_TAG_template_*_parameter DIEs for the argument
10592 pack elements of ARG. Note that ARG would then be
10593 an argument pack. */
10594 if (arg_pack_elems)
10595 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
10596 arg_pack_elems,
10597 die);
10598 else
10599 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
10600 true /* emit name */, die);
10601 if (i >= non_default)
10602 add_AT_flag (parm_die, DW_AT_default_value, 1);
10607 /* Create and return a DIE for PARM which should be
10608 the representation of a generic type parameter.
10609 For instance, in the C++ front end, PARM would be a template parameter.
10610 ARG is the argument to PARM.
10611 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
10612 name of the PARM.
10613 PARENT_DIE is the parent DIE which the new created DIE should be added to,
10614 as a child node. */
10616 static dw_die_ref
10617 generic_parameter_die (tree parm, tree arg,
10618 bool emit_name_p,
10619 dw_die_ref parent_die)
10621 dw_die_ref tmpl_die = NULL;
10622 const char *name = NULL;
10624 if (!parm || !DECL_NAME (parm) || !arg)
10625 return NULL;
10627 /* We support non-type generic parameters and arguments,
10628 type generic parameters and arguments, as well as
10629 generic generic parameters (a.k.a. template template parameters in C++)
10630 and arguments. */
10631 if (TREE_CODE (parm) == PARM_DECL)
10632 /* PARM is a nontype generic parameter */
10633 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10634 else if (TREE_CODE (parm) == TYPE_DECL)
10635 /* PARM is a type generic parameter. */
10636 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10637 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10638 /* PARM is a generic generic parameter.
10639 Its DIE is a GNU extension. It shall have a
10640 DW_AT_name attribute to represent the name of the template template
10641 parameter, and a DW_AT_GNU_template_name attribute to represent the
10642 name of the template template argument. */
10643 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10644 parent_die, parm);
10645 else
10646 gcc_unreachable ();
10648 if (tmpl_die)
10650 tree tmpl_type;
10652 /* If PARM is a generic parameter pack, it means we are
10653 emitting debug info for a template argument pack element.
10654 In other terms, ARG is a template argument pack element.
10655 In that case, we don't emit any DW_AT_name attribute for
10656 the die. */
10657 if (emit_name_p)
10659 name = IDENTIFIER_POINTER (DECL_NAME (parm));
10660 gcc_assert (name);
10661 add_AT_string (tmpl_die, DW_AT_name, name);
10664 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10666 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10667 TMPL_DIE should have a child DW_AT_type attribute that is set
10668 to the type of the argument to PARM, which is ARG.
10669 If PARM is a type generic parameter, TMPL_DIE should have a
10670 child DW_AT_type that is set to ARG. */
10671 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10672 add_type_attribute (tmpl_die, tmpl_type, 0,
10673 TREE_THIS_VOLATILE (tmpl_type),
10674 parent_die);
10676 else
10678 /* So TMPL_DIE is a DIE representing a
10679 a generic generic template parameter, a.k.a template template
10680 parameter in C++ and arg is a template. */
10682 /* The DW_AT_GNU_template_name attribute of the DIE must be set
10683 to the name of the argument. */
10684 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
10685 if (name)
10686 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
10689 if (TREE_CODE (parm) == PARM_DECL)
10690 /* So PARM is a non-type generic parameter.
10691 DWARF3 5.6.8 says we must set a DW_AT_const_value child
10692 attribute of TMPL_DIE which value represents the value
10693 of ARG.
10694 We must be careful here:
10695 The value of ARG might reference some function decls.
10696 We might currently be emitting debug info for a generic
10697 type and types are emitted before function decls, we don't
10698 know if the function decls referenced by ARG will actually be
10699 emitted after cgraph computations.
10700 So must defer the generation of the DW_AT_const_value to
10701 after cgraph is ready. */
10702 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
10705 return tmpl_die;
10708 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
10709 PARM_PACK must be a template parameter pack. The returned DIE
10710 will be child DIE of PARENT_DIE. */
10712 static dw_die_ref
10713 template_parameter_pack_die (tree parm_pack,
10714 tree parm_pack_args,
10715 dw_die_ref parent_die)
10717 dw_die_ref die;
10718 int j;
10720 gcc_assert (parent_die && parm_pack);
10722 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
10723 add_name_and_src_coords_attributes (die, parm_pack);
10724 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
10725 generic_parameter_die (parm_pack,
10726 TREE_VEC_ELT (parm_pack_args, j),
10727 false /* Don't emit DW_AT_name */,
10728 die);
10729 return die;
10732 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
10733 an enumerated type. */
10735 static inline int
10736 type_is_enum (const_tree type)
10738 return TREE_CODE (type) == ENUMERAL_TYPE;
10741 /* Return the DBX register number described by a given RTL node. */
10743 static unsigned int
10744 dbx_reg_number (const_rtx rtl)
10746 unsigned regno = REGNO (rtl);
10748 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10750 #ifdef LEAF_REG_REMAP
10751 if (crtl->uses_only_leaf_regs)
10753 int leaf_reg = LEAF_REG_REMAP (regno);
10754 if (leaf_reg != -1)
10755 regno = (unsigned) leaf_reg;
10757 #endif
10759 regno = DBX_REGISTER_NUMBER (regno);
10760 gcc_assert (regno != INVALID_REGNUM);
10761 return regno;
10764 /* Optionally add a DW_OP_piece term to a location description expression.
10765 DW_OP_piece is only added if the location description expression already
10766 doesn't end with DW_OP_piece. */
10768 static void
10769 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10771 dw_loc_descr_ref loc;
10773 if (*list_head != NULL)
10775 /* Find the end of the chain. */
10776 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10779 if (loc->dw_loc_opc != DW_OP_piece)
10780 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10784 /* Return a location descriptor that designates a machine register or
10785 zero if there is none. */
10787 static dw_loc_descr_ref
10788 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10790 rtx regs;
10792 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10793 return 0;
10795 /* We only use "frame base" when we're sure we're talking about the
10796 post-prologue local stack frame. We do this by *not* running
10797 register elimination until this point, and recognizing the special
10798 argument pointer and soft frame pointer rtx's.
10799 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
10800 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
10801 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
10803 dw_loc_descr_ref result = NULL;
10805 if (dwarf_version >= 4 || !dwarf_strict)
10807 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
10808 initialized);
10809 if (result)
10810 add_loc_descr (&result,
10811 new_loc_descr (DW_OP_stack_value, 0, 0));
10813 return result;
10816 regs = targetm.dwarf_register_span (rtl);
10818 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
10819 return multiple_reg_loc_descriptor (rtl, regs, initialized);
10820 else
10822 unsigned int dbx_regnum = dbx_reg_number (rtl);
10823 if (dbx_regnum == IGNORED_DWARF_REGNUM)
10824 return 0;
10825 return one_reg_loc_descriptor (dbx_regnum, initialized);
10829 /* Return a location descriptor that designates a machine register for
10830 a given hard register number. */
10832 static dw_loc_descr_ref
10833 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
10835 dw_loc_descr_ref reg_loc_descr;
10837 if (regno <= 31)
10838 reg_loc_descr
10839 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
10840 else
10841 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
10843 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10844 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10846 return reg_loc_descr;
10849 /* Given an RTL of a register, return a location descriptor that
10850 designates a value that spans more than one register. */
10852 static dw_loc_descr_ref
10853 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
10854 enum var_init_status initialized)
10856 int size, i;
10857 dw_loc_descr_ref loc_result = NULL;
10859 /* Simple, contiguous registers. */
10860 if (regs == NULL_RTX)
10862 unsigned reg = REGNO (rtl);
10863 int nregs;
10865 #ifdef LEAF_REG_REMAP
10866 if (crtl->uses_only_leaf_regs)
10868 int leaf_reg = LEAF_REG_REMAP (reg);
10869 if (leaf_reg != -1)
10870 reg = (unsigned) leaf_reg;
10872 #endif
10874 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
10875 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
10877 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
10879 loc_result = NULL;
10880 while (nregs--)
10882 dw_loc_descr_ref t;
10884 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
10885 VAR_INIT_STATUS_INITIALIZED);
10886 add_loc_descr (&loc_result, t);
10887 add_loc_descr_op_piece (&loc_result, size);
10888 ++reg;
10890 return loc_result;
10893 /* Now onto stupid register sets in non contiguous locations. */
10895 gcc_assert (GET_CODE (regs) == PARALLEL);
10897 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10898 loc_result = NULL;
10900 for (i = 0; i < XVECLEN (regs, 0); ++i)
10902 dw_loc_descr_ref t;
10904 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
10905 VAR_INIT_STATUS_INITIALIZED);
10906 add_loc_descr (&loc_result, t);
10907 add_loc_descr_op_piece (&loc_result, size);
10910 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10911 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10912 return loc_result;
10915 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
10917 /* Return a location descriptor that designates a constant i,
10918 as a compound operation from constant (i >> shift), constant shift
10919 and DW_OP_shl. */
10921 static dw_loc_descr_ref
10922 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10924 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
10925 add_loc_descr (&ret, int_loc_descriptor (shift));
10926 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
10927 return ret;
10930 /* Return a location descriptor that designates a constant. */
10932 static dw_loc_descr_ref
10933 int_loc_descriptor (HOST_WIDE_INT i)
10935 enum dwarf_location_atom op;
10937 /* Pick the smallest representation of a constant, rather than just
10938 defaulting to the LEB encoding. */
10939 if (i >= 0)
10941 int clz = clz_hwi (i);
10942 int ctz = ctz_hwi (i);
10943 if (i <= 31)
10944 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
10945 else if (i <= 0xff)
10946 op = DW_OP_const1u;
10947 else if (i <= 0xffff)
10948 op = DW_OP_const2u;
10949 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10950 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10951 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
10952 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
10953 while DW_OP_const4u is 5 bytes. */
10954 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
10955 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10956 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10957 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
10958 while DW_OP_const4u is 5 bytes. */
10959 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10960 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10961 op = DW_OP_const4u;
10962 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10963 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10964 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
10965 while DW_OP_constu of constant >= 0x100000000 takes at least
10966 6 bytes. */
10967 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10968 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10969 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
10970 >= HOST_BITS_PER_WIDE_INT)
10971 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
10972 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
10973 while DW_OP_constu takes in this case at least 6 bytes. */
10974 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
10975 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10976 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10977 && size_of_uleb128 (i) > 6)
10978 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
10979 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
10980 else
10981 op = DW_OP_constu;
10983 else
10985 if (i >= -0x80)
10986 op = DW_OP_const1s;
10987 else if (i >= -0x8000)
10988 op = DW_OP_const2s;
10989 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10991 if (size_of_int_loc_descriptor (i) < 5)
10993 dw_loc_descr_ref ret = int_loc_descriptor (-i);
10994 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10995 return ret;
10997 op = DW_OP_const4s;
10999 else
11001 if (size_of_int_loc_descriptor (i)
11002 < (unsigned long) 1 + size_of_sleb128 (i))
11004 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11005 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11006 return ret;
11008 op = DW_OP_consts;
11012 return new_loc_descr (op, i, 0);
11015 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
11016 without actually allocating it. */
11018 static unsigned long
11019 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11021 return size_of_int_loc_descriptor (i >> shift)
11022 + size_of_int_loc_descriptor (shift)
11023 + 1;
11026 /* Return size_of_locs (int_loc_descriptor (i)) without
11027 actually allocating it. */
11029 static unsigned long
11030 size_of_int_loc_descriptor (HOST_WIDE_INT i)
11032 unsigned long s;
11034 if (i >= 0)
11036 int clz, ctz;
11037 if (i <= 31)
11038 return 1;
11039 else if (i <= 0xff)
11040 return 2;
11041 else if (i <= 0xffff)
11042 return 3;
11043 clz = clz_hwi (i);
11044 ctz = ctz_hwi (i);
11045 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11046 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11047 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11048 - clz - 5);
11049 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11050 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11051 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11052 - clz - 8);
11053 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11054 return 5;
11055 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
11056 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11057 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11058 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11059 - clz - 8);
11060 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11061 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
11062 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11063 - clz - 16);
11064 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11065 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11066 && s > 6)
11067 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11068 - clz - 32);
11069 else
11070 return 1 + s;
11072 else
11074 if (i >= -0x80)
11075 return 2;
11076 else if (i >= -0x8000)
11077 return 3;
11078 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11080 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11082 s = size_of_int_loc_descriptor (-i) + 1;
11083 if (s < 5)
11084 return s;
11086 return 5;
11088 else
11090 unsigned long r = 1 + size_of_sleb128 (i);
11091 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11093 s = size_of_int_loc_descriptor (-i) + 1;
11094 if (s < r)
11095 return s;
11097 return r;
11102 /* Return loc description representing "address" of integer value.
11103 This can appear only as toplevel expression. */
11105 static dw_loc_descr_ref
11106 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
11108 int litsize;
11109 dw_loc_descr_ref loc_result = NULL;
11111 if (!(dwarf_version >= 4 || !dwarf_strict))
11112 return NULL;
11114 litsize = size_of_int_loc_descriptor (i);
11115 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
11116 is more compact. For DW_OP_stack_value we need:
11117 litsize + 1 (DW_OP_stack_value)
11118 and for DW_OP_implicit_value:
11119 1 (DW_OP_implicit_value) + 1 (length) + size. */
11120 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
11122 loc_result = int_loc_descriptor (i);
11123 add_loc_descr (&loc_result,
11124 new_loc_descr (DW_OP_stack_value, 0, 0));
11125 return loc_result;
11128 loc_result = new_loc_descr (DW_OP_implicit_value,
11129 size, 0);
11130 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11131 loc_result->dw_loc_oprnd2.v.val_int = i;
11132 return loc_result;
11135 /* Return a location descriptor that designates a base+offset location. */
11137 static dw_loc_descr_ref
11138 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
11139 enum var_init_status initialized)
11141 unsigned int regno;
11142 dw_loc_descr_ref result;
11143 dw_fde_ref fde = cfun->fde;
11145 /* We only use "frame base" when we're sure we're talking about the
11146 post-prologue local stack frame. We do this by *not* running
11147 register elimination until this point, and recognizing the special
11148 argument pointer and soft frame pointer rtx's. */
11149 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
11151 rtx elim = (ira_use_lra_p
11152 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
11153 : eliminate_regs (reg, VOIDmode, NULL_RTX));
11155 if (elim != reg)
11157 if (GET_CODE (elim) == PLUS)
11159 offset += INTVAL (XEXP (elim, 1));
11160 elim = XEXP (elim, 0);
11162 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11163 && (elim == hard_frame_pointer_rtx
11164 || elim == stack_pointer_rtx))
11165 || elim == (frame_pointer_needed
11166 ? hard_frame_pointer_rtx
11167 : stack_pointer_rtx));
11169 /* If drap register is used to align stack, use frame
11170 pointer + offset to access stack variables. If stack
11171 is aligned without drap, use stack pointer + offset to
11172 access stack variables. */
11173 if (crtl->stack_realign_tried
11174 && reg == frame_pointer_rtx)
11176 int base_reg
11177 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
11178 ? HARD_FRAME_POINTER_REGNUM
11179 : REGNO (elim));
11180 return new_reg_loc_descr (base_reg, offset);
11183 gcc_assert (frame_pointer_fb_offset_valid);
11184 offset += frame_pointer_fb_offset;
11185 return new_loc_descr (DW_OP_fbreg, offset, 0);
11189 regno = REGNO (reg);
11190 #ifdef LEAF_REG_REMAP
11191 if (crtl->uses_only_leaf_regs)
11193 int leaf_reg = LEAF_REG_REMAP (regno);
11194 if (leaf_reg != -1)
11195 regno = (unsigned) leaf_reg;
11197 #endif
11198 regno = DWARF_FRAME_REGNUM (regno);
11200 if (!optimize && fde
11201 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
11203 /* Use cfa+offset to represent the location of arguments passed
11204 on the stack when drap is used to align stack.
11205 Only do this when not optimizing, for optimized code var-tracking
11206 is supposed to track where the arguments live and the register
11207 used as vdrap or drap in some spot might be used for something
11208 else in other part of the routine. */
11209 return new_loc_descr (DW_OP_fbreg, offset, 0);
11212 if (regno <= 31)
11213 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
11214 offset, 0);
11215 else
11216 result = new_loc_descr (DW_OP_bregx, regno, offset);
11218 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11219 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11221 return result;
11224 /* Return true if this RTL expression describes a base+offset calculation. */
11226 static inline int
11227 is_based_loc (const_rtx rtl)
11229 return (GET_CODE (rtl) == PLUS
11230 && ((REG_P (XEXP (rtl, 0))
11231 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11232 && CONST_INT_P (XEXP (rtl, 1)))));
11235 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11236 failed. */
11238 static dw_loc_descr_ref
11239 tls_mem_loc_descriptor (rtx mem)
11241 tree base;
11242 dw_loc_descr_ref loc_result;
11244 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
11245 return NULL;
11247 base = get_base_address (MEM_EXPR (mem));
11248 if (base == NULL
11249 || TREE_CODE (base) != VAR_DECL
11250 || !DECL_THREAD_LOCAL_P (base))
11251 return NULL;
11253 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
11254 if (loc_result == NULL)
11255 return NULL;
11257 if (MEM_OFFSET (mem))
11258 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
11260 return loc_result;
11263 /* Output debug info about reason why we failed to expand expression as dwarf
11264 expression. */
11266 static void
11267 expansion_failed (tree expr, rtx rtl, char const *reason)
11269 if (dump_file && (dump_flags & TDF_DETAILS))
11271 fprintf (dump_file, "Failed to expand as dwarf: ");
11272 if (expr)
11273 print_generic_expr (dump_file, expr, dump_flags);
11274 if (rtl)
11276 fprintf (dump_file, "\n");
11277 print_rtl (dump_file, rtl);
11279 fprintf (dump_file, "\nReason: %s\n", reason);
11283 /* Helper function for const_ok_for_output, called either directly
11284 or via for_each_rtx. */
11286 static int
11287 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
11289 rtx rtl = *rtlp;
11291 if (GET_CODE (rtl) == UNSPEC)
11293 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11294 we can't express it in the debug info. */
11295 #ifdef ENABLE_CHECKING
11296 /* Don't complain about TLS UNSPECs, those are just too hard to
11297 delegitimize. Note this could be a non-decl SYMBOL_REF such as
11298 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11299 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
11300 if (XVECLEN (rtl, 0) == 0
11301 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11302 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
11303 inform (current_function_decl
11304 ? DECL_SOURCE_LOCATION (current_function_decl)
11305 : UNKNOWN_LOCATION,
11306 #if NUM_UNSPEC_VALUES > 0
11307 "non-delegitimized UNSPEC %s (%d) found in variable location",
11308 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11309 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11310 XINT (rtl, 1));
11311 #else
11312 "non-delegitimized UNSPEC %d found in variable location",
11313 XINT (rtl, 1));
11314 #endif
11315 #endif
11316 expansion_failed (NULL_TREE, rtl,
11317 "UNSPEC hasn't been delegitimized.\n");
11318 return 1;
11321 if (targetm.const_not_ok_for_debug_p (rtl))
11323 expansion_failed (NULL_TREE, rtl,
11324 "Expression rejected for debug by the backend.\n");
11325 return 1;
11328 if (GET_CODE (rtl) != SYMBOL_REF)
11329 return 0;
11331 if (CONSTANT_POOL_ADDRESS_P (rtl))
11333 bool marked;
11334 get_pool_constant_mark (rtl, &marked);
11335 /* If all references to this pool constant were optimized away,
11336 it was not output and thus we can't represent it. */
11337 if (!marked)
11339 expansion_failed (NULL_TREE, rtl,
11340 "Constant was removed from constant pool.\n");
11341 return 1;
11345 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11346 return 1;
11348 /* Avoid references to external symbols in debug info, on several targets
11349 the linker might even refuse to link when linking a shared library,
11350 and in many other cases the relocations for .debug_info/.debug_loc are
11351 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
11352 to be defined within the same shared library or executable are fine. */
11353 if (SYMBOL_REF_EXTERNAL_P (rtl))
11355 tree decl = SYMBOL_REF_DECL (rtl);
11357 if (decl == NULL || !targetm.binds_local_p (decl))
11359 expansion_failed (NULL_TREE, rtl,
11360 "Symbol not defined in current TU.\n");
11361 return 1;
11365 return 0;
11368 /* Return true if constant RTL can be emitted in DW_OP_addr or
11369 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
11370 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
11372 static bool
11373 const_ok_for_output (rtx rtl)
11375 if (GET_CODE (rtl) == SYMBOL_REF)
11376 return const_ok_for_output_1 (&rtl, NULL) == 0;
11378 if (GET_CODE (rtl) == CONST)
11379 return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
11381 return true;
11384 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11385 if possible, NULL otherwise. */
11387 static dw_die_ref
11388 base_type_for_mode (enum machine_mode mode, bool unsignedp)
11390 dw_die_ref type_die;
11391 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11393 if (type == NULL)
11394 return NULL;
11395 switch (TREE_CODE (type))
11397 case INTEGER_TYPE:
11398 case REAL_TYPE:
11399 break;
11400 default:
11401 return NULL;
11403 type_die = lookup_type_die (type);
11404 if (!type_die)
11405 type_die = modified_type_die (type, false, false, comp_unit_die ());
11406 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11407 return NULL;
11408 return type_die;
11411 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11412 type matching MODE, or, if MODE is narrower than or as wide as
11413 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
11414 possible. */
11416 static dw_loc_descr_ref
11417 convert_descriptor_to_mode (enum machine_mode mode, dw_loc_descr_ref op)
11419 enum machine_mode outer_mode = mode;
11420 dw_die_ref type_die;
11421 dw_loc_descr_ref cvt;
11423 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11425 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11426 return op;
11428 type_die = base_type_for_mode (outer_mode, 1);
11429 if (type_die == NULL)
11430 return NULL;
11431 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11432 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11433 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11434 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11435 add_loc_descr (&op, cvt);
11436 return op;
11439 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
11441 static dw_loc_descr_ref
11442 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11443 dw_loc_descr_ref op1)
11445 dw_loc_descr_ref ret = op0;
11446 add_loc_descr (&ret, op1);
11447 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11448 if (STORE_FLAG_VALUE != 1)
11450 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11451 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11453 return ret;
11456 /* Return location descriptor for signed comparison OP RTL. */
11458 static dw_loc_descr_ref
11459 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11460 enum machine_mode mem_mode)
11462 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11463 dw_loc_descr_ref op0, op1;
11464 int shift;
11466 if (op_mode == VOIDmode)
11467 op_mode = GET_MODE (XEXP (rtl, 1));
11468 if (op_mode == VOIDmode)
11469 return NULL;
11471 if (dwarf_strict
11472 && (GET_MODE_CLASS (op_mode) != MODE_INT
11473 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11474 return NULL;
11476 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11477 VAR_INIT_STATUS_INITIALIZED);
11478 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11479 VAR_INIT_STATUS_INITIALIZED);
11481 if (op0 == NULL || op1 == NULL)
11482 return NULL;
11484 if (GET_MODE_CLASS (op_mode) != MODE_INT
11485 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11486 return compare_loc_descriptor (op, op0, op1);
11488 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11490 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11491 dw_loc_descr_ref cvt;
11493 if (type_die == NULL)
11494 return NULL;
11495 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11496 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11497 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11498 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11499 add_loc_descr (&op0, cvt);
11500 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11501 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11502 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11503 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11504 add_loc_descr (&op1, cvt);
11505 return compare_loc_descriptor (op, op0, op1);
11508 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11509 /* For eq/ne, if the operands are known to be zero-extended,
11510 there is no need to do the fancy shifting up. */
11511 if (op == DW_OP_eq || op == DW_OP_ne)
11513 dw_loc_descr_ref last0, last1;
11514 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11516 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11518 /* deref_size zero extends, and for constants we can check
11519 whether they are zero extended or not. */
11520 if (((last0->dw_loc_opc == DW_OP_deref_size
11521 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11522 || (CONST_INT_P (XEXP (rtl, 0))
11523 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11524 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11525 && ((last1->dw_loc_opc == DW_OP_deref_size
11526 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11527 || (CONST_INT_P (XEXP (rtl, 1))
11528 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11529 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11530 return compare_loc_descriptor (op, op0, op1);
11532 /* EQ/NE comparison against constant in narrower type than
11533 DWARF2_ADDR_SIZE can be performed either as
11534 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11535 DW_OP_{eq,ne}
11537 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11538 DW_OP_{eq,ne}. Pick whatever is shorter. */
11539 if (CONST_INT_P (XEXP (rtl, 1))
11540 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11541 && (size_of_int_loc_descriptor (shift) + 1
11542 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11543 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11544 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11545 & GET_MODE_MASK (op_mode))))
11547 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
11548 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11549 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11550 & GET_MODE_MASK (op_mode));
11551 return compare_loc_descriptor (op, op0, op1);
11554 add_loc_descr (&op0, int_loc_descriptor (shift));
11555 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11556 if (CONST_INT_P (XEXP (rtl, 1)))
11557 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11558 else
11560 add_loc_descr (&op1, int_loc_descriptor (shift));
11561 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11563 return compare_loc_descriptor (op, op0, op1);
11566 /* Return location descriptor for unsigned comparison OP RTL. */
11568 static dw_loc_descr_ref
11569 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11570 enum machine_mode mem_mode)
11572 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11573 dw_loc_descr_ref op0, op1;
11575 if (op_mode == VOIDmode)
11576 op_mode = GET_MODE (XEXP (rtl, 1));
11577 if (op_mode == VOIDmode)
11578 return NULL;
11579 if (GET_MODE_CLASS (op_mode) != MODE_INT)
11580 return NULL;
11582 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11583 return NULL;
11585 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11586 VAR_INIT_STATUS_INITIALIZED);
11587 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11588 VAR_INIT_STATUS_INITIALIZED);
11590 if (op0 == NULL || op1 == NULL)
11591 return NULL;
11593 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
11595 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
11596 dw_loc_descr_ref last0, last1;
11597 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11599 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11601 if (CONST_INT_P (XEXP (rtl, 0)))
11602 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
11603 /* deref_size zero extends, so no need to mask it again. */
11604 else if (last0->dw_loc_opc != DW_OP_deref_size
11605 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11607 add_loc_descr (&op0, int_loc_descriptor (mask));
11608 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11610 if (CONST_INT_P (XEXP (rtl, 1)))
11611 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11612 /* deref_size zero extends, so no need to mask it again. */
11613 else if (last1->dw_loc_opc != DW_OP_deref_size
11614 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11616 add_loc_descr (&op1, int_loc_descriptor (mask));
11617 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11620 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11622 HOST_WIDE_INT bias = 1;
11623 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11624 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11625 if (CONST_INT_P (XEXP (rtl, 1)))
11626 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
11627 + INTVAL (XEXP (rtl, 1)));
11628 else
11629 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
11630 bias, 0));
11632 return compare_loc_descriptor (op, op0, op1);
11635 /* Return location descriptor for {U,S}{MIN,MAX}. */
11637 static dw_loc_descr_ref
11638 minmax_loc_descriptor (rtx rtl, enum machine_mode mode,
11639 enum machine_mode mem_mode)
11641 enum dwarf_location_atom op;
11642 dw_loc_descr_ref op0, op1, ret;
11643 dw_loc_descr_ref bra_node, drop_node;
11645 if (dwarf_strict
11646 && (GET_MODE_CLASS (mode) != MODE_INT
11647 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
11648 return NULL;
11650 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11651 VAR_INIT_STATUS_INITIALIZED);
11652 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11653 VAR_INIT_STATUS_INITIALIZED);
11655 if (op0 == NULL || op1 == NULL)
11656 return NULL;
11658 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
11659 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
11660 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
11661 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
11663 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11665 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11666 add_loc_descr (&op0, int_loc_descriptor (mask));
11667 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11668 add_loc_descr (&op1, int_loc_descriptor (mask));
11669 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11671 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11673 HOST_WIDE_INT bias = 1;
11674 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11675 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11676 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11679 else if (GET_MODE_CLASS (mode) == MODE_INT
11680 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11682 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
11683 add_loc_descr (&op0, int_loc_descriptor (shift));
11684 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11685 add_loc_descr (&op1, int_loc_descriptor (shift));
11686 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11688 else if (GET_MODE_CLASS (mode) == MODE_INT
11689 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11691 dw_die_ref type_die = base_type_for_mode (mode, 0);
11692 dw_loc_descr_ref cvt;
11693 if (type_die == NULL)
11694 return NULL;
11695 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11696 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11697 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11698 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11699 add_loc_descr (&op0, cvt);
11700 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11701 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11702 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11703 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11704 add_loc_descr (&op1, cvt);
11707 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
11708 op = DW_OP_lt;
11709 else
11710 op = DW_OP_gt;
11711 ret = op0;
11712 add_loc_descr (&ret, op1);
11713 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11714 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11715 add_loc_descr (&ret, bra_node);
11716 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11717 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11718 add_loc_descr (&ret, drop_node);
11719 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11720 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11721 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
11722 && GET_MODE_CLASS (mode) == MODE_INT
11723 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11724 ret = convert_descriptor_to_mode (mode, ret);
11725 return ret;
11728 /* Helper function for mem_loc_descriptor. Perform OP binary op,
11729 but after converting arguments to type_die, afterwards
11730 convert back to unsigned. */
11732 static dw_loc_descr_ref
11733 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
11734 enum machine_mode mode, enum machine_mode mem_mode)
11736 dw_loc_descr_ref cvt, op0, op1;
11738 if (type_die == NULL)
11739 return NULL;
11740 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11741 VAR_INIT_STATUS_INITIALIZED);
11742 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11743 VAR_INIT_STATUS_INITIALIZED);
11744 if (op0 == NULL || op1 == NULL)
11745 return NULL;
11746 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11747 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11748 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11749 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11750 add_loc_descr (&op0, cvt);
11751 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11752 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11753 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11754 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11755 add_loc_descr (&op1, cvt);
11756 add_loc_descr (&op0, op1);
11757 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
11758 return convert_descriptor_to_mode (mode, op0);
11761 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
11762 const0 is DW_OP_lit0 or corresponding typed constant,
11763 const1 is DW_OP_lit1 or corresponding typed constant
11764 and constMSB is constant with just the MSB bit set
11765 for the mode):
11766 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11767 L1: const0 DW_OP_swap
11768 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
11769 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11770 L3: DW_OP_drop
11771 L4: DW_OP_nop
11773 CTZ is similar:
11774 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11775 L1: const0 DW_OP_swap
11776 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11777 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11778 L3: DW_OP_drop
11779 L4: DW_OP_nop
11781 FFS is similar:
11782 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
11783 L1: const1 DW_OP_swap
11784 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11785 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11786 L3: DW_OP_drop
11787 L4: DW_OP_nop */
11789 static dw_loc_descr_ref
11790 clz_loc_descriptor (rtx rtl, enum machine_mode mode,
11791 enum machine_mode mem_mode)
11793 dw_loc_descr_ref op0, ret, tmp;
11794 HOST_WIDE_INT valv;
11795 dw_loc_descr_ref l1jump, l1label;
11796 dw_loc_descr_ref l2jump, l2label;
11797 dw_loc_descr_ref l3jump, l3label;
11798 dw_loc_descr_ref l4jump, l4label;
11799 rtx msb;
11801 if (GET_MODE_CLASS (mode) != MODE_INT
11802 || GET_MODE (XEXP (rtl, 0)) != mode
11803 || (GET_CODE (rtl) == CLZ
11804 && GET_MODE_BITSIZE (mode) > HOST_BITS_PER_DOUBLE_INT))
11805 return NULL;
11807 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11808 VAR_INIT_STATUS_INITIALIZED);
11809 if (op0 == NULL)
11810 return NULL;
11811 ret = op0;
11812 if (GET_CODE (rtl) == CLZ)
11814 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11815 valv = GET_MODE_BITSIZE (mode);
11817 else if (GET_CODE (rtl) == FFS)
11818 valv = 0;
11819 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11820 valv = GET_MODE_BITSIZE (mode);
11821 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11822 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
11823 add_loc_descr (&ret, l1jump);
11824 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11825 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
11826 VAR_INIT_STATUS_INITIALIZED);
11827 if (tmp == NULL)
11828 return NULL;
11829 add_loc_descr (&ret, tmp);
11830 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
11831 add_loc_descr (&ret, l4jump);
11832 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
11833 ? const1_rtx : const0_rtx,
11834 mode, mem_mode,
11835 VAR_INIT_STATUS_INITIALIZED);
11836 if (l1label == NULL)
11837 return NULL;
11838 add_loc_descr (&ret, l1label);
11839 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11840 l2label = new_loc_descr (DW_OP_dup, 0, 0);
11841 add_loc_descr (&ret, l2label);
11842 if (GET_CODE (rtl) != CLZ)
11843 msb = const1_rtx;
11844 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11845 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
11846 << (GET_MODE_BITSIZE (mode) - 1));
11847 else
11848 msb = immed_double_const (0, (unsigned HOST_WIDE_INT) 1
11849 << (GET_MODE_BITSIZE (mode)
11850 - HOST_BITS_PER_WIDE_INT - 1), mode);
11851 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
11852 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11853 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
11854 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
11855 else
11856 tmp = mem_loc_descriptor (msb, mode, mem_mode,
11857 VAR_INIT_STATUS_INITIALIZED);
11858 if (tmp == NULL)
11859 return NULL;
11860 add_loc_descr (&ret, tmp);
11861 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11862 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
11863 add_loc_descr (&ret, l3jump);
11864 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11865 VAR_INIT_STATUS_INITIALIZED);
11866 if (tmp == NULL)
11867 return NULL;
11868 add_loc_descr (&ret, tmp);
11869 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
11870 ? DW_OP_shl : DW_OP_shr, 0, 0));
11871 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11872 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
11873 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11874 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
11875 add_loc_descr (&ret, l2jump);
11876 l3label = new_loc_descr (DW_OP_drop, 0, 0);
11877 add_loc_descr (&ret, l3label);
11878 l4label = new_loc_descr (DW_OP_nop, 0, 0);
11879 add_loc_descr (&ret, l4label);
11880 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11881 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11882 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11883 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11884 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11885 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
11886 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11887 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
11888 return ret;
11891 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
11892 const1 is DW_OP_lit1 or corresponding typed constant):
11893 const0 DW_OP_swap
11894 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11895 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11896 L2: DW_OP_drop
11898 PARITY is similar:
11899 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11900 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11901 L2: DW_OP_drop */
11903 static dw_loc_descr_ref
11904 popcount_loc_descriptor (rtx rtl, enum machine_mode mode,
11905 enum machine_mode mem_mode)
11907 dw_loc_descr_ref op0, ret, tmp;
11908 dw_loc_descr_ref l1jump, l1label;
11909 dw_loc_descr_ref l2jump, l2label;
11911 if (GET_MODE_CLASS (mode) != MODE_INT
11912 || GET_MODE (XEXP (rtl, 0)) != mode)
11913 return NULL;
11915 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11916 VAR_INIT_STATUS_INITIALIZED);
11917 if (op0 == NULL)
11918 return NULL;
11919 ret = op0;
11920 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11921 VAR_INIT_STATUS_INITIALIZED);
11922 if (tmp == NULL)
11923 return NULL;
11924 add_loc_descr (&ret, tmp);
11925 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11926 l1label = new_loc_descr (DW_OP_dup, 0, 0);
11927 add_loc_descr (&ret, l1label);
11928 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11929 add_loc_descr (&ret, l2jump);
11930 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11931 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11932 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11933 VAR_INIT_STATUS_INITIALIZED);
11934 if (tmp == NULL)
11935 return NULL;
11936 add_loc_descr (&ret, tmp);
11937 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11938 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
11939 ? DW_OP_plus : DW_OP_xor, 0, 0));
11940 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11941 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11942 VAR_INIT_STATUS_INITIALIZED);
11943 add_loc_descr (&ret, tmp);
11944 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11945 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11946 add_loc_descr (&ret, l1jump);
11947 l2label = new_loc_descr (DW_OP_drop, 0, 0);
11948 add_loc_descr (&ret, l2label);
11949 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11950 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11951 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11952 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11953 return ret;
11956 /* BSWAP (constS is initial shift count, either 56 or 24):
11957 constS const0
11958 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
11959 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
11960 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
11961 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
11962 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
11964 static dw_loc_descr_ref
11965 bswap_loc_descriptor (rtx rtl, enum machine_mode mode,
11966 enum machine_mode mem_mode)
11968 dw_loc_descr_ref op0, ret, tmp;
11969 dw_loc_descr_ref l1jump, l1label;
11970 dw_loc_descr_ref l2jump, l2label;
11972 if (GET_MODE_CLASS (mode) != MODE_INT
11973 || BITS_PER_UNIT != 8
11974 || (GET_MODE_BITSIZE (mode) != 32
11975 && GET_MODE_BITSIZE (mode) != 64))
11976 return NULL;
11978 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11979 VAR_INIT_STATUS_INITIALIZED);
11980 if (op0 == NULL)
11981 return NULL;
11983 ret = op0;
11984 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11985 mode, mem_mode,
11986 VAR_INIT_STATUS_INITIALIZED);
11987 if (tmp == NULL)
11988 return NULL;
11989 add_loc_descr (&ret, tmp);
11990 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11991 VAR_INIT_STATUS_INITIALIZED);
11992 if (tmp == NULL)
11993 return NULL;
11994 add_loc_descr (&ret, tmp);
11995 l1label = new_loc_descr (DW_OP_pick, 2, 0);
11996 add_loc_descr (&ret, l1label);
11997 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11998 mode, mem_mode,
11999 VAR_INIT_STATUS_INITIALIZED);
12000 add_loc_descr (&ret, tmp);
12001 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
12002 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12003 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12004 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
12005 VAR_INIT_STATUS_INITIALIZED);
12006 if (tmp == NULL)
12007 return NULL;
12008 add_loc_descr (&ret, tmp);
12009 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12010 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
12011 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12012 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 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_dup, 0, 0));
12015 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12016 VAR_INIT_STATUS_INITIALIZED);
12017 add_loc_descr (&ret, tmp);
12018 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
12019 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12020 add_loc_descr (&ret, l2jump);
12021 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
12022 VAR_INIT_STATUS_INITIALIZED);
12023 add_loc_descr (&ret, tmp);
12024 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12025 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12026 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12027 add_loc_descr (&ret, l1jump);
12028 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12029 add_loc_descr (&ret, l2label);
12030 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12031 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12032 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12033 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12034 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12035 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12036 return ret;
12039 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
12040 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12041 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
12042 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
12044 ROTATERT is similar:
12045 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
12046 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12047 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
12049 static dw_loc_descr_ref
12050 rotate_loc_descriptor (rtx rtl, enum machine_mode mode,
12051 enum machine_mode mem_mode)
12053 rtx rtlop1 = XEXP (rtl, 1);
12054 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
12055 int i;
12057 if (GET_MODE_CLASS (mode) != MODE_INT)
12058 return NULL;
12060 if (GET_MODE (rtlop1) != VOIDmode
12061 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
12062 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12063 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12064 VAR_INIT_STATUS_INITIALIZED);
12065 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12066 VAR_INIT_STATUS_INITIALIZED);
12067 if (op0 == NULL || op1 == NULL)
12068 return NULL;
12069 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12070 for (i = 0; i < 2; i++)
12072 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
12073 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
12074 mode, mem_mode,
12075 VAR_INIT_STATUS_INITIALIZED);
12076 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12077 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12078 ? DW_OP_const4u
12079 : HOST_BITS_PER_WIDE_INT == 64
12080 ? DW_OP_const8u : DW_OP_constu,
12081 GET_MODE_MASK (mode), 0);
12082 else
12083 mask[i] = NULL;
12084 if (mask[i] == NULL)
12085 return NULL;
12086 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
12088 ret = op0;
12089 add_loc_descr (&ret, op1);
12090 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12091 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12092 if (GET_CODE (rtl) == ROTATERT)
12094 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12095 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12096 GET_MODE_BITSIZE (mode), 0));
12098 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12099 if (mask[0] != NULL)
12100 add_loc_descr (&ret, mask[0]);
12101 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12102 if (mask[1] != NULL)
12104 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12105 add_loc_descr (&ret, mask[1]);
12106 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12108 if (GET_CODE (rtl) == ROTATE)
12110 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12111 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12112 GET_MODE_BITSIZE (mode), 0));
12114 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12115 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12116 return ret;
12119 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
12120 for DEBUG_PARAMETER_REF RTL. */
12122 static dw_loc_descr_ref
12123 parameter_ref_descriptor (rtx rtl)
12125 dw_loc_descr_ref ret;
12126 dw_die_ref ref;
12128 if (dwarf_strict)
12129 return NULL;
12130 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
12131 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
12132 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
12133 if (ref)
12135 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12136 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12137 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12139 else
12141 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12142 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
12144 return ret;
12147 /* The following routine converts the RTL for a variable or parameter
12148 (resident in memory) into an equivalent Dwarf representation of a
12149 mechanism for getting the address of that same variable onto the top of a
12150 hypothetical "address evaluation" stack.
12152 When creating memory location descriptors, we are effectively transforming
12153 the RTL for a memory-resident object into its Dwarf postfix expression
12154 equivalent. This routine recursively descends an RTL tree, turning
12155 it into Dwarf postfix code as it goes.
12157 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
12159 MEM_MODE is the mode of the memory reference, needed to handle some
12160 autoincrement addressing modes.
12162 Return 0 if we can't represent the location. */
12164 dw_loc_descr_ref
12165 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
12166 enum machine_mode mem_mode,
12167 enum var_init_status initialized)
12169 dw_loc_descr_ref mem_loc_result = NULL;
12170 enum dwarf_location_atom op;
12171 dw_loc_descr_ref op0, op1;
12172 rtx inner = NULL_RTX;
12174 if (mode == VOIDmode)
12175 mode = GET_MODE (rtl);
12177 /* Note that for a dynamically sized array, the location we will generate a
12178 description of here will be the lowest numbered location which is
12179 actually within the array. That's *not* necessarily the same as the
12180 zeroth element of the array. */
12182 rtl = targetm.delegitimize_address (rtl);
12184 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
12185 return NULL;
12187 switch (GET_CODE (rtl))
12189 case POST_INC:
12190 case POST_DEC:
12191 case POST_MODIFY:
12192 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
12194 case SUBREG:
12195 /* The case of a subreg may arise when we have a local (register)
12196 variable or a formal (register) parameter which doesn't quite fill
12197 up an entire register. For now, just assume that it is
12198 legitimate to make the Dwarf info refer to the whole register which
12199 contains the given subreg. */
12200 if (!subreg_lowpart_p (rtl))
12201 break;
12202 inner = SUBREG_REG (rtl);
12203 case TRUNCATE:
12204 if (inner == NULL_RTX)
12205 inner = XEXP (rtl, 0);
12206 if (GET_MODE_CLASS (mode) == MODE_INT
12207 && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
12208 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12209 #ifdef POINTERS_EXTEND_UNSIGNED
12210 || (mode == Pmode && mem_mode != VOIDmode)
12211 #endif
12213 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
12215 mem_loc_result = mem_loc_descriptor (inner,
12216 GET_MODE (inner),
12217 mem_mode, initialized);
12218 break;
12220 if (dwarf_strict)
12221 break;
12222 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
12223 break;
12224 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
12225 && (GET_MODE_CLASS (mode) != MODE_INT
12226 || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
12227 break;
12228 else
12230 dw_die_ref type_die;
12231 dw_loc_descr_ref cvt;
12233 mem_loc_result = mem_loc_descriptor (inner,
12234 GET_MODE (inner),
12235 mem_mode, initialized);
12236 if (mem_loc_result == NULL)
12237 break;
12238 type_die = base_type_for_mode (mode,
12239 GET_MODE_CLASS (mode) == MODE_INT);
12240 if (type_die == NULL)
12242 mem_loc_result = NULL;
12243 break;
12245 if (GET_MODE_SIZE (mode)
12246 != GET_MODE_SIZE (GET_MODE (inner)))
12247 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12248 else
12249 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
12250 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12251 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12252 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12253 add_loc_descr (&mem_loc_result, cvt);
12255 break;
12257 case REG:
12258 if (GET_MODE_CLASS (mode) != MODE_INT
12259 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12260 && rtl != arg_pointer_rtx
12261 && rtl != frame_pointer_rtx
12262 #ifdef POINTERS_EXTEND_UNSIGNED
12263 && (mode != Pmode || mem_mode == VOIDmode)
12264 #endif
12267 dw_die_ref type_die;
12268 unsigned int dbx_regnum;
12270 if (dwarf_strict)
12271 break;
12272 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12273 break;
12274 type_die = base_type_for_mode (mode,
12275 GET_MODE_CLASS (mode) == MODE_INT);
12276 if (type_die == NULL)
12277 break;
12279 dbx_regnum = dbx_reg_number (rtl);
12280 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12281 break;
12282 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12283 dbx_regnum, 0);
12284 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12285 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12286 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12287 break;
12289 /* Whenever a register number forms a part of the description of the
12290 method for calculating the (dynamic) address of a memory resident
12291 object, DWARF rules require the register number be referred to as
12292 a "base register". This distinction is not based in any way upon
12293 what category of register the hardware believes the given register
12294 belongs to. This is strictly DWARF terminology we're dealing with
12295 here. Note that in cases where the location of a memory-resident
12296 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12297 OP_CONST (0)) the actual DWARF location descriptor that we generate
12298 may just be OP_BASEREG (basereg). This may look deceptively like
12299 the object in question was allocated to a register (rather than in
12300 memory) so DWARF consumers need to be aware of the subtle
12301 distinction between OP_REG and OP_BASEREG. */
12302 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12303 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12304 else if (stack_realign_drap
12305 && crtl->drap_reg
12306 && crtl->args.internal_arg_pointer == rtl
12307 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12309 /* If RTL is internal_arg_pointer, which has been optimized
12310 out, use DRAP instead. */
12311 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12312 VAR_INIT_STATUS_INITIALIZED);
12314 break;
12316 case SIGN_EXTEND:
12317 case ZERO_EXTEND:
12318 if (GET_MODE_CLASS (mode) != MODE_INT)
12319 break;
12320 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12321 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12322 if (op0 == 0)
12323 break;
12324 else if (GET_CODE (rtl) == ZERO_EXTEND
12325 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12326 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12327 < HOST_BITS_PER_WIDE_INT
12328 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12329 to expand zero extend as two shifts instead of
12330 masking. */
12331 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12333 enum machine_mode imode = GET_MODE (XEXP (rtl, 0));
12334 mem_loc_result = op0;
12335 add_loc_descr (&mem_loc_result,
12336 int_loc_descriptor (GET_MODE_MASK (imode)));
12337 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12339 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12341 int shift = DWARF2_ADDR_SIZE
12342 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12343 shift *= BITS_PER_UNIT;
12344 if (GET_CODE (rtl) == SIGN_EXTEND)
12345 op = DW_OP_shra;
12346 else
12347 op = DW_OP_shr;
12348 mem_loc_result = op0;
12349 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12350 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12351 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12352 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12354 else if (!dwarf_strict)
12356 dw_die_ref type_die1, type_die2;
12357 dw_loc_descr_ref cvt;
12359 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12360 GET_CODE (rtl) == ZERO_EXTEND);
12361 if (type_die1 == NULL)
12362 break;
12363 type_die2 = base_type_for_mode (mode, 1);
12364 if (type_die2 == NULL)
12365 break;
12366 mem_loc_result = op0;
12367 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12368 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12369 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12370 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12371 add_loc_descr (&mem_loc_result, cvt);
12372 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12373 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12374 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12375 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12376 add_loc_descr (&mem_loc_result, cvt);
12378 break;
12380 case MEM:
12382 rtx new_rtl = avoid_constant_pool_reference (rtl);
12383 if (new_rtl != rtl)
12385 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12386 initialized);
12387 if (mem_loc_result != NULL)
12388 return mem_loc_result;
12391 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12392 get_address_mode (rtl), mode,
12393 VAR_INIT_STATUS_INITIALIZED);
12394 if (mem_loc_result == NULL)
12395 mem_loc_result = tls_mem_loc_descriptor (rtl);
12396 if (mem_loc_result != NULL)
12398 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12399 || GET_MODE_CLASS (mode) != MODE_INT)
12401 dw_die_ref type_die;
12402 dw_loc_descr_ref deref;
12404 if (dwarf_strict)
12405 return NULL;
12406 type_die
12407 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12408 if (type_die == NULL)
12409 return NULL;
12410 deref = new_loc_descr (DW_OP_GNU_deref_type,
12411 GET_MODE_SIZE (mode), 0);
12412 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12413 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12414 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12415 add_loc_descr (&mem_loc_result, deref);
12417 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12418 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12419 else
12420 add_loc_descr (&mem_loc_result,
12421 new_loc_descr (DW_OP_deref_size,
12422 GET_MODE_SIZE (mode), 0));
12424 break;
12426 case LO_SUM:
12427 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12429 case LABEL_REF:
12430 /* Some ports can transform a symbol ref into a label ref, because
12431 the symbol ref is too far away and has to be dumped into a constant
12432 pool. */
12433 case CONST:
12434 case SYMBOL_REF:
12435 if (GET_MODE_CLASS (mode) != MODE_INT
12436 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12437 #ifdef POINTERS_EXTEND_UNSIGNED
12438 && (mode != Pmode || mem_mode == VOIDmode)
12439 #endif
12441 break;
12442 if (GET_CODE (rtl) == SYMBOL_REF
12443 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12445 dw_loc_descr_ref temp;
12447 /* If this is not defined, we have no way to emit the data. */
12448 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12449 break;
12451 temp = new_addr_loc_descr (rtl, dtprel_true);
12453 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12454 add_loc_descr (&mem_loc_result, temp);
12456 break;
12459 if (!const_ok_for_output (rtl))
12460 break;
12462 symref:
12463 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12464 vec_safe_push (used_rtx_array, rtl);
12465 break;
12467 case CONCAT:
12468 case CONCATN:
12469 case VAR_LOCATION:
12470 case DEBUG_IMPLICIT_PTR:
12471 expansion_failed (NULL_TREE, rtl,
12472 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12473 return 0;
12475 case ENTRY_VALUE:
12476 if (dwarf_strict)
12477 return NULL;
12478 if (REG_P (ENTRY_VALUE_EXP (rtl)))
12480 if (GET_MODE_CLASS (mode) != MODE_INT
12481 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12482 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12483 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12484 else
12486 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12487 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12488 return NULL;
12489 op0 = one_reg_loc_descriptor (dbx_regnum,
12490 VAR_INIT_STATUS_INITIALIZED);
12493 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12494 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12496 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12497 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12498 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12499 return NULL;
12501 else
12502 gcc_unreachable ();
12503 if (op0 == NULL)
12504 return NULL;
12505 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12506 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12507 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12508 break;
12510 case DEBUG_PARAMETER_REF:
12511 mem_loc_result = parameter_ref_descriptor (rtl);
12512 break;
12514 case PRE_MODIFY:
12515 /* Extract the PLUS expression nested inside and fall into
12516 PLUS code below. */
12517 rtl = XEXP (rtl, 1);
12518 goto plus;
12520 case PRE_INC:
12521 case PRE_DEC:
12522 /* Turn these into a PLUS expression and fall into the PLUS code
12523 below. */
12524 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12525 gen_int_mode (GET_CODE (rtl) == PRE_INC
12526 ? GET_MODE_UNIT_SIZE (mem_mode)
12527 : -GET_MODE_UNIT_SIZE (mem_mode),
12528 mode));
12530 /* ... fall through ... */
12532 case PLUS:
12533 plus:
12534 if (is_based_loc (rtl)
12535 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12536 || XEXP (rtl, 0) == arg_pointer_rtx
12537 || XEXP (rtl, 0) == frame_pointer_rtx)
12538 && GET_MODE_CLASS (mode) == MODE_INT)
12539 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12540 INTVAL (XEXP (rtl, 1)),
12541 VAR_INIT_STATUS_INITIALIZED);
12542 else
12544 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12545 VAR_INIT_STATUS_INITIALIZED);
12546 if (mem_loc_result == 0)
12547 break;
12549 if (CONST_INT_P (XEXP (rtl, 1))
12550 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12551 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12552 else
12554 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12555 VAR_INIT_STATUS_INITIALIZED);
12556 if (op1 == 0)
12557 break;
12558 add_loc_descr (&mem_loc_result, op1);
12559 add_loc_descr (&mem_loc_result,
12560 new_loc_descr (DW_OP_plus, 0, 0));
12563 break;
12565 /* If a pseudo-reg is optimized away, it is possible for it to
12566 be replaced with a MEM containing a multiply or shift. */
12567 case MINUS:
12568 op = DW_OP_minus;
12569 goto do_binop;
12571 case MULT:
12572 op = DW_OP_mul;
12573 goto do_binop;
12575 case DIV:
12576 if (!dwarf_strict
12577 && GET_MODE_CLASS (mode) == MODE_INT
12578 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12580 mem_loc_result = typed_binop (DW_OP_div, rtl,
12581 base_type_for_mode (mode, 0),
12582 mode, mem_mode);
12583 break;
12585 op = DW_OP_div;
12586 goto do_binop;
12588 case UMOD:
12589 op = DW_OP_mod;
12590 goto do_binop;
12592 case ASHIFT:
12593 op = DW_OP_shl;
12594 goto do_shift;
12596 case ASHIFTRT:
12597 op = DW_OP_shra;
12598 goto do_shift;
12600 case LSHIFTRT:
12601 op = DW_OP_shr;
12602 goto do_shift;
12604 do_shift:
12605 if (GET_MODE_CLASS (mode) != MODE_INT)
12606 break;
12607 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12608 VAR_INIT_STATUS_INITIALIZED);
12610 rtx rtlop1 = XEXP (rtl, 1);
12611 if (GET_MODE (rtlop1) != VOIDmode
12612 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
12613 < GET_MODE_BITSIZE (mode))
12614 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12615 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12616 VAR_INIT_STATUS_INITIALIZED);
12619 if (op0 == 0 || op1 == 0)
12620 break;
12622 mem_loc_result = op0;
12623 add_loc_descr (&mem_loc_result, op1);
12624 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12625 break;
12627 case AND:
12628 op = DW_OP_and;
12629 goto do_binop;
12631 case IOR:
12632 op = DW_OP_or;
12633 goto do_binop;
12635 case XOR:
12636 op = DW_OP_xor;
12637 goto do_binop;
12639 do_binop:
12640 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12641 VAR_INIT_STATUS_INITIALIZED);
12642 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12643 VAR_INIT_STATUS_INITIALIZED);
12645 if (op0 == 0 || op1 == 0)
12646 break;
12648 mem_loc_result = op0;
12649 add_loc_descr (&mem_loc_result, op1);
12650 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12651 break;
12653 case MOD:
12654 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
12656 mem_loc_result = typed_binop (DW_OP_mod, rtl,
12657 base_type_for_mode (mode, 0),
12658 mode, mem_mode);
12659 break;
12662 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12663 VAR_INIT_STATUS_INITIALIZED);
12664 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12665 VAR_INIT_STATUS_INITIALIZED);
12667 if (op0 == 0 || op1 == 0)
12668 break;
12670 mem_loc_result = op0;
12671 add_loc_descr (&mem_loc_result, op1);
12672 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12673 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12674 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
12675 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
12676 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
12677 break;
12679 case UDIV:
12680 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
12682 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
12684 op = DW_OP_div;
12685 goto do_binop;
12687 mem_loc_result = typed_binop (DW_OP_div, rtl,
12688 base_type_for_mode (mode, 1),
12689 mode, mem_mode);
12691 break;
12693 case NOT:
12694 op = DW_OP_not;
12695 goto do_unop;
12697 case ABS:
12698 op = DW_OP_abs;
12699 goto do_unop;
12701 case NEG:
12702 op = DW_OP_neg;
12703 goto do_unop;
12705 do_unop:
12706 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12707 VAR_INIT_STATUS_INITIALIZED);
12709 if (op0 == 0)
12710 break;
12712 mem_loc_result = op0;
12713 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12714 break;
12716 case CONST_INT:
12717 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12718 #ifdef POINTERS_EXTEND_UNSIGNED
12719 || (mode == Pmode
12720 && mem_mode != VOIDmode
12721 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
12722 #endif
12725 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12726 break;
12728 if (!dwarf_strict
12729 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
12730 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
12732 dw_die_ref type_die = base_type_for_mode (mode, 1);
12733 enum machine_mode amode;
12734 if (type_die == NULL)
12735 return NULL;
12736 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
12737 MODE_INT, 0);
12738 if (INTVAL (rtl) >= 0
12739 && amode != BLKmode
12740 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
12741 /* const DW_OP_GNU_convert <XXX> vs.
12742 DW_OP_GNU_const_type <XXX, 1, const>. */
12743 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
12744 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
12746 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12747 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12748 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12749 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12750 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
12751 add_loc_descr (&mem_loc_result, op0);
12752 return mem_loc_result;
12754 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
12755 INTVAL (rtl));
12756 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12757 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12758 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12759 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12760 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12761 else
12763 mem_loc_result->dw_loc_oprnd2.val_class
12764 = dw_val_class_const_double;
12765 mem_loc_result->dw_loc_oprnd2.v.val_double
12766 = double_int::from_shwi (INTVAL (rtl));
12769 break;
12771 case CONST_DOUBLE:
12772 if (!dwarf_strict)
12774 dw_die_ref type_die;
12776 /* Note that a CONST_DOUBLE rtx could represent either an integer
12777 or a floating-point constant. A CONST_DOUBLE is used whenever
12778 the constant requires more than one word in order to be
12779 adequately represented. We output CONST_DOUBLEs as blocks. */
12780 if (mode == VOIDmode
12781 || (GET_MODE (rtl) == VOIDmode
12782 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
12783 break;
12784 type_die = base_type_for_mode (mode,
12785 GET_MODE_CLASS (mode) == MODE_INT);
12786 if (type_die == NULL)
12787 return NULL;
12788 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
12789 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12790 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12791 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12792 if (SCALAR_FLOAT_MODE_P (mode))
12794 unsigned int length = GET_MODE_SIZE (mode);
12795 unsigned char *array
12796 = (unsigned char*) ggc_alloc_atomic (length);
12798 insert_float (rtl, array);
12799 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12800 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12801 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12802 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12804 else
12806 mem_loc_result->dw_loc_oprnd2.val_class
12807 = dw_val_class_const_double;
12808 mem_loc_result->dw_loc_oprnd2.v.val_double
12809 = rtx_to_double_int (rtl);
12812 break;
12814 case EQ:
12815 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
12816 break;
12818 case GE:
12819 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12820 break;
12822 case GT:
12823 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12824 break;
12826 case LE:
12827 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12828 break;
12830 case LT:
12831 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12832 break;
12834 case NE:
12835 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
12836 break;
12838 case GEU:
12839 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12840 break;
12842 case GTU:
12843 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12844 break;
12846 case LEU:
12847 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12848 break;
12850 case LTU:
12851 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12852 break;
12854 case UMIN:
12855 case UMAX:
12856 if (GET_MODE_CLASS (mode) != MODE_INT)
12857 break;
12858 /* FALLTHRU */
12859 case SMIN:
12860 case SMAX:
12861 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
12862 break;
12864 case ZERO_EXTRACT:
12865 case SIGN_EXTRACT:
12866 if (CONST_INT_P (XEXP (rtl, 1))
12867 && CONST_INT_P (XEXP (rtl, 2))
12868 && ((unsigned) INTVAL (XEXP (rtl, 1))
12869 + (unsigned) INTVAL (XEXP (rtl, 2))
12870 <= GET_MODE_BITSIZE (mode))
12871 && GET_MODE_CLASS (mode) == MODE_INT
12872 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12873 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
12875 int shift, size;
12876 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12877 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12878 if (op0 == 0)
12879 break;
12880 if (GET_CODE (rtl) == SIGN_EXTRACT)
12881 op = DW_OP_shra;
12882 else
12883 op = DW_OP_shr;
12884 mem_loc_result = op0;
12885 size = INTVAL (XEXP (rtl, 1));
12886 shift = INTVAL (XEXP (rtl, 2));
12887 if (BITS_BIG_ENDIAN)
12888 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12889 - shift - size;
12890 if (shift + size != (int) DWARF2_ADDR_SIZE)
12892 add_loc_descr (&mem_loc_result,
12893 int_loc_descriptor (DWARF2_ADDR_SIZE
12894 - shift - size));
12895 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12897 if (size != (int) DWARF2_ADDR_SIZE)
12899 add_loc_descr (&mem_loc_result,
12900 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
12901 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12904 break;
12906 case IF_THEN_ELSE:
12908 dw_loc_descr_ref op2, bra_node, drop_node;
12909 op0 = mem_loc_descriptor (XEXP (rtl, 0),
12910 GET_MODE (XEXP (rtl, 0)) == VOIDmode
12911 ? word_mode : GET_MODE (XEXP (rtl, 0)),
12912 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12913 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12914 VAR_INIT_STATUS_INITIALIZED);
12915 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
12916 VAR_INIT_STATUS_INITIALIZED);
12917 if (op0 == NULL || op1 == NULL || op2 == NULL)
12918 break;
12920 mem_loc_result = op1;
12921 add_loc_descr (&mem_loc_result, op2);
12922 add_loc_descr (&mem_loc_result, op0);
12923 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12924 add_loc_descr (&mem_loc_result, bra_node);
12925 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
12926 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12927 add_loc_descr (&mem_loc_result, drop_node);
12928 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12929 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12931 break;
12933 case FLOAT_EXTEND:
12934 case FLOAT_TRUNCATE:
12935 case FLOAT:
12936 case UNSIGNED_FLOAT:
12937 case FIX:
12938 case UNSIGNED_FIX:
12939 if (!dwarf_strict)
12941 dw_die_ref type_die;
12942 dw_loc_descr_ref cvt;
12944 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12945 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12946 if (op0 == NULL)
12947 break;
12948 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
12949 && (GET_CODE (rtl) == FLOAT
12950 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
12951 <= DWARF2_ADDR_SIZE))
12953 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12954 GET_CODE (rtl) == UNSIGNED_FLOAT);
12955 if (type_die == NULL)
12956 break;
12957 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12958 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12959 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12960 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12961 add_loc_descr (&op0, cvt);
12963 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
12964 if (type_die == NULL)
12965 break;
12966 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12967 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12968 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12969 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12970 add_loc_descr (&op0, cvt);
12971 if (GET_MODE_CLASS (mode) == MODE_INT
12972 && (GET_CODE (rtl) == FIX
12973 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
12975 op0 = convert_descriptor_to_mode (mode, op0);
12976 if (op0 == NULL)
12977 break;
12979 mem_loc_result = op0;
12981 break;
12983 case CLZ:
12984 case CTZ:
12985 case FFS:
12986 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
12987 break;
12989 case POPCOUNT:
12990 case PARITY:
12991 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
12992 break;
12994 case BSWAP:
12995 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
12996 break;
12998 case ROTATE:
12999 case ROTATERT:
13000 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
13001 break;
13003 case COMPARE:
13004 /* In theory, we could implement the above. */
13005 /* DWARF cannot represent the unsigned compare operations
13006 natively. */
13007 case SS_MULT:
13008 case US_MULT:
13009 case SS_DIV:
13010 case US_DIV:
13011 case SS_PLUS:
13012 case US_PLUS:
13013 case SS_MINUS:
13014 case US_MINUS:
13015 case SS_NEG:
13016 case US_NEG:
13017 case SS_ABS:
13018 case SS_ASHIFT:
13019 case US_ASHIFT:
13020 case SS_TRUNCATE:
13021 case US_TRUNCATE:
13022 case UNORDERED:
13023 case ORDERED:
13024 case UNEQ:
13025 case UNGE:
13026 case UNGT:
13027 case UNLE:
13028 case UNLT:
13029 case LTGT:
13030 case FRACT_CONVERT:
13031 case UNSIGNED_FRACT_CONVERT:
13032 case SAT_FRACT:
13033 case UNSIGNED_SAT_FRACT:
13034 case SQRT:
13035 case ASM_OPERANDS:
13036 case VEC_MERGE:
13037 case VEC_SELECT:
13038 case VEC_CONCAT:
13039 case VEC_DUPLICATE:
13040 case UNSPEC:
13041 case HIGH:
13042 case FMA:
13043 case STRICT_LOW_PART:
13044 case CONST_VECTOR:
13045 case CONST_FIXED:
13046 case CLRSB:
13047 case CLOBBER:
13048 /* If delegitimize_address couldn't do anything with the UNSPEC, we
13049 can't express it in the debug info. This can happen e.g. with some
13050 TLS UNSPECs. */
13051 break;
13053 case CONST_STRING:
13054 resolve_one_addr (&rtl, NULL);
13055 goto symref;
13057 default:
13058 #ifdef ENABLE_CHECKING
13059 print_rtl (stderr, rtl);
13060 gcc_unreachable ();
13061 #else
13062 break;
13063 #endif
13066 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13067 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13069 return mem_loc_result;
13072 /* Return a descriptor that describes the concatenation of two locations.
13073 This is typically a complex variable. */
13075 static dw_loc_descr_ref
13076 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
13078 dw_loc_descr_ref cc_loc_result = NULL;
13079 dw_loc_descr_ref x0_ref
13080 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13081 dw_loc_descr_ref x1_ref
13082 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13084 if (x0_ref == 0 || x1_ref == 0)
13085 return 0;
13087 cc_loc_result = x0_ref;
13088 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
13090 add_loc_descr (&cc_loc_result, x1_ref);
13091 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
13093 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13094 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13096 return cc_loc_result;
13099 /* Return a descriptor that describes the concatenation of N
13100 locations. */
13102 static dw_loc_descr_ref
13103 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
13105 unsigned int i;
13106 dw_loc_descr_ref cc_loc_result = NULL;
13107 unsigned int n = XVECLEN (concatn, 0);
13109 for (i = 0; i < n; ++i)
13111 dw_loc_descr_ref ref;
13112 rtx x = XVECEXP (concatn, 0, i);
13114 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13115 if (ref == NULL)
13116 return NULL;
13118 add_loc_descr (&cc_loc_result, ref);
13119 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
13122 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13123 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13125 return cc_loc_result;
13128 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
13129 for DEBUG_IMPLICIT_PTR RTL. */
13131 static dw_loc_descr_ref
13132 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
13134 dw_loc_descr_ref ret;
13135 dw_die_ref ref;
13137 if (dwarf_strict)
13138 return NULL;
13139 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
13140 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
13141 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
13142 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
13143 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
13144 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
13145 if (ref)
13147 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13148 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13149 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13151 else
13153 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13154 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
13156 return ret;
13159 /* Output a proper Dwarf location descriptor for a variable or parameter
13160 which is either allocated in a register or in a memory location. For a
13161 register, we just generate an OP_REG and the register number. For a
13162 memory location we provide a Dwarf postfix expression describing how to
13163 generate the (dynamic) address of the object onto the address stack.
13165 MODE is mode of the decl if this loc_descriptor is going to be used in
13166 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
13167 allowed, VOIDmode otherwise.
13169 If we don't know how to describe it, return 0. */
13171 static dw_loc_descr_ref
13172 loc_descriptor (rtx rtl, enum machine_mode mode,
13173 enum var_init_status initialized)
13175 dw_loc_descr_ref loc_result = NULL;
13177 switch (GET_CODE (rtl))
13179 case SUBREG:
13180 /* The case of a subreg may arise when we have a local (register)
13181 variable or a formal (register) parameter which doesn't quite fill
13182 up an entire register. For now, just assume that it is
13183 legitimate to make the Dwarf info refer to the whole register which
13184 contains the given subreg. */
13185 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
13186 loc_result = loc_descriptor (SUBREG_REG (rtl),
13187 GET_MODE (SUBREG_REG (rtl)), initialized);
13188 else
13189 goto do_default;
13190 break;
13192 case REG:
13193 loc_result = reg_loc_descriptor (rtl, initialized);
13194 break;
13196 case MEM:
13197 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13198 GET_MODE (rtl), initialized);
13199 if (loc_result == NULL)
13200 loc_result = tls_mem_loc_descriptor (rtl);
13201 if (loc_result == NULL)
13203 rtx new_rtl = avoid_constant_pool_reference (rtl);
13204 if (new_rtl != rtl)
13205 loc_result = loc_descriptor (new_rtl, mode, initialized);
13207 break;
13209 case CONCAT:
13210 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
13211 initialized);
13212 break;
13214 case CONCATN:
13215 loc_result = concatn_loc_descriptor (rtl, initialized);
13216 break;
13218 case VAR_LOCATION:
13219 /* Single part. */
13220 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
13222 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
13223 if (GET_CODE (loc) == EXPR_LIST)
13224 loc = XEXP (loc, 0);
13225 loc_result = loc_descriptor (loc, mode, initialized);
13226 break;
13229 rtl = XEXP (rtl, 1);
13230 /* FALLTHRU */
13232 case PARALLEL:
13234 rtvec par_elems = XVEC (rtl, 0);
13235 int num_elem = GET_NUM_ELEM (par_elems);
13236 enum machine_mode mode;
13237 int i;
13239 /* Create the first one, so we have something to add to. */
13240 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13241 VOIDmode, initialized);
13242 if (loc_result == NULL)
13243 return NULL;
13244 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13245 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13246 for (i = 1; i < num_elem; i++)
13248 dw_loc_descr_ref temp;
13250 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13251 VOIDmode, initialized);
13252 if (temp == NULL)
13253 return NULL;
13254 add_loc_descr (&loc_result, temp);
13255 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13256 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13259 break;
13261 case CONST_INT:
13262 if (mode != VOIDmode && mode != BLKmode)
13263 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13264 INTVAL (rtl));
13265 break;
13267 case CONST_DOUBLE:
13268 if (mode == VOIDmode)
13269 mode = GET_MODE (rtl);
13271 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13273 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13275 /* Note that a CONST_DOUBLE rtx could represent either an integer
13276 or a floating-point constant. A CONST_DOUBLE is used whenever
13277 the constant requires more than one word in order to be
13278 adequately represented. We output CONST_DOUBLEs as blocks. */
13279 loc_result = new_loc_descr (DW_OP_implicit_value,
13280 GET_MODE_SIZE (mode), 0);
13281 if (SCALAR_FLOAT_MODE_P (mode))
13283 unsigned int length = GET_MODE_SIZE (mode);
13284 unsigned char *array
13285 = (unsigned char*) ggc_alloc_atomic (length);
13287 insert_float (rtl, array);
13288 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13289 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13290 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13291 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13293 else
13295 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13296 loc_result->dw_loc_oprnd2.v.val_double
13297 = rtx_to_double_int (rtl);
13300 break;
13302 case CONST_VECTOR:
13303 if (mode == VOIDmode)
13304 mode = GET_MODE (rtl);
13306 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13308 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13309 unsigned int length = CONST_VECTOR_NUNITS (rtl);
13310 unsigned char *array = (unsigned char *)
13311 ggc_alloc_atomic (length * elt_size);
13312 unsigned int i;
13313 unsigned char *p;
13315 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13316 switch (GET_MODE_CLASS (mode))
13318 case MODE_VECTOR_INT:
13319 for (i = 0, p = array; i < length; i++, p += elt_size)
13321 rtx elt = CONST_VECTOR_ELT (rtl, i);
13322 double_int val = rtx_to_double_int (elt);
13324 if (elt_size <= sizeof (HOST_WIDE_INT))
13325 insert_int (val.to_shwi (), elt_size, p);
13326 else
13328 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
13329 insert_double (val, p);
13332 break;
13334 case MODE_VECTOR_FLOAT:
13335 for (i = 0, p = array; i < length; i++, p += elt_size)
13337 rtx elt = CONST_VECTOR_ELT (rtl, i);
13338 insert_float (elt, p);
13340 break;
13342 default:
13343 gcc_unreachable ();
13346 loc_result = new_loc_descr (DW_OP_implicit_value,
13347 length * elt_size, 0);
13348 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13349 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13350 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13351 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13353 break;
13355 case CONST:
13356 if (mode == VOIDmode
13357 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13358 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13359 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13361 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13362 break;
13364 /* FALLTHROUGH */
13365 case SYMBOL_REF:
13366 if (!const_ok_for_output (rtl))
13367 break;
13368 case LABEL_REF:
13369 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13370 && (dwarf_version >= 4 || !dwarf_strict))
13372 loc_result = new_addr_loc_descr (rtl, dtprel_false);
13373 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13374 vec_safe_push (used_rtx_array, rtl);
13376 break;
13378 case DEBUG_IMPLICIT_PTR:
13379 loc_result = implicit_ptr_descriptor (rtl, 0);
13380 break;
13382 case PLUS:
13383 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13384 && CONST_INT_P (XEXP (rtl, 1)))
13386 loc_result
13387 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13388 break;
13390 /* FALLTHRU */
13391 do_default:
13392 default:
13393 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13394 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13395 && dwarf_version >= 4)
13396 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13398 /* Value expression. */
13399 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13400 if (loc_result)
13401 add_loc_descr (&loc_result,
13402 new_loc_descr (DW_OP_stack_value, 0, 0));
13404 break;
13407 return loc_result;
13410 /* We need to figure out what section we should use as the base for the
13411 address ranges where a given location is valid.
13412 1. If this particular DECL has a section associated with it, use that.
13413 2. If this function has a section associated with it, use that.
13414 3. Otherwise, use the text section.
13415 XXX: If you split a variable across multiple sections, we won't notice. */
13417 static const char *
13418 secname_for_decl (const_tree decl)
13420 const char *secname;
13422 if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
13424 tree sectree = DECL_SECTION_NAME (decl);
13425 secname = TREE_STRING_POINTER (sectree);
13427 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13429 tree sectree = DECL_SECTION_NAME (current_function_decl);
13430 secname = TREE_STRING_POINTER (sectree);
13432 else if (cfun && in_cold_section_p)
13433 secname = crtl->subsections.cold_section_label;
13434 else
13435 secname = text_section_label;
13437 return secname;
13440 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
13442 static bool
13443 decl_by_reference_p (tree decl)
13445 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13446 || TREE_CODE (decl) == VAR_DECL)
13447 && DECL_BY_REFERENCE (decl));
13450 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13451 for VARLOC. */
13453 static dw_loc_descr_ref
13454 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13455 enum var_init_status initialized)
13457 int have_address = 0;
13458 dw_loc_descr_ref descr;
13459 enum machine_mode mode;
13461 if (want_address != 2)
13463 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13464 /* Single part. */
13465 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13467 varloc = PAT_VAR_LOCATION_LOC (varloc);
13468 if (GET_CODE (varloc) == EXPR_LIST)
13469 varloc = XEXP (varloc, 0);
13470 mode = GET_MODE (varloc);
13471 if (MEM_P (varloc))
13473 rtx addr = XEXP (varloc, 0);
13474 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13475 mode, initialized);
13476 if (descr)
13477 have_address = 1;
13478 else
13480 rtx x = avoid_constant_pool_reference (varloc);
13481 if (x != varloc)
13482 descr = mem_loc_descriptor (x, mode, VOIDmode,
13483 initialized);
13486 else
13487 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13489 else
13490 return 0;
13492 else
13494 if (GET_CODE (varloc) == VAR_LOCATION)
13495 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13496 else
13497 mode = DECL_MODE (loc);
13498 descr = loc_descriptor (varloc, mode, initialized);
13499 have_address = 1;
13502 if (!descr)
13503 return 0;
13505 if (want_address == 2 && !have_address
13506 && (dwarf_version >= 4 || !dwarf_strict))
13508 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13510 expansion_failed (loc, NULL_RTX,
13511 "DWARF address size mismatch");
13512 return 0;
13514 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13515 have_address = 1;
13517 /* Show if we can't fill the request for an address. */
13518 if (want_address && !have_address)
13520 expansion_failed (loc, NULL_RTX,
13521 "Want address and only have value");
13522 return 0;
13525 /* If we've got an address and don't want one, dereference. */
13526 if (!want_address && have_address)
13528 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13529 enum dwarf_location_atom op;
13531 if (size > DWARF2_ADDR_SIZE || size == -1)
13533 expansion_failed (loc, NULL_RTX,
13534 "DWARF address size mismatch");
13535 return 0;
13537 else if (size == DWARF2_ADDR_SIZE)
13538 op = DW_OP_deref;
13539 else
13540 op = DW_OP_deref_size;
13542 add_loc_descr (&descr, new_loc_descr (op, size, 0));
13545 return descr;
13548 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
13549 if it is not possible. */
13551 static dw_loc_descr_ref
13552 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
13554 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
13555 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
13556 else if (dwarf_version >= 3 || !dwarf_strict)
13557 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
13558 else
13559 return NULL;
13562 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13563 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
13565 static dw_loc_descr_ref
13566 dw_sra_loc_expr (tree decl, rtx loc)
13568 rtx p;
13569 unsigned int padsize = 0;
13570 dw_loc_descr_ref descr, *descr_tail;
13571 unsigned HOST_WIDE_INT decl_size;
13572 rtx varloc;
13573 enum var_init_status initialized;
13575 if (DECL_SIZE (decl) == NULL
13576 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
13577 return NULL;
13579 decl_size = tree_to_uhwi (DECL_SIZE (decl));
13580 descr = NULL;
13581 descr_tail = &descr;
13583 for (p = loc; p; p = XEXP (p, 1))
13585 unsigned int bitsize = decl_piece_bitsize (p);
13586 rtx loc_note = *decl_piece_varloc_ptr (p);
13587 dw_loc_descr_ref cur_descr;
13588 dw_loc_descr_ref *tail, last = NULL;
13589 unsigned int opsize = 0;
13591 if (loc_note == NULL_RTX
13592 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
13594 padsize += bitsize;
13595 continue;
13597 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
13598 varloc = NOTE_VAR_LOCATION (loc_note);
13599 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
13600 if (cur_descr == NULL)
13602 padsize += bitsize;
13603 continue;
13606 /* Check that cur_descr either doesn't use
13607 DW_OP_*piece operations, or their sum is equal
13608 to bitsize. Otherwise we can't embed it. */
13609 for (tail = &cur_descr; *tail != NULL;
13610 tail = &(*tail)->dw_loc_next)
13611 if ((*tail)->dw_loc_opc == DW_OP_piece)
13613 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
13614 * BITS_PER_UNIT;
13615 last = *tail;
13617 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
13619 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
13620 last = *tail;
13623 if (last != NULL && opsize != bitsize)
13625 padsize += bitsize;
13626 /* Discard the current piece of the descriptor and release any
13627 addr_table entries it uses. */
13628 remove_loc_list_addr_table_entries (cur_descr);
13629 continue;
13632 /* If there is a hole, add DW_OP_*piece after empty DWARF
13633 expression, which means that those bits are optimized out. */
13634 if (padsize)
13636 if (padsize > decl_size)
13638 remove_loc_list_addr_table_entries (cur_descr);
13639 goto discard_descr;
13641 decl_size -= padsize;
13642 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
13643 if (*descr_tail == NULL)
13645 remove_loc_list_addr_table_entries (cur_descr);
13646 goto discard_descr;
13648 descr_tail = &(*descr_tail)->dw_loc_next;
13649 padsize = 0;
13651 *descr_tail = cur_descr;
13652 descr_tail = tail;
13653 if (bitsize > decl_size)
13654 goto discard_descr;
13655 decl_size -= bitsize;
13656 if (last == NULL)
13658 HOST_WIDE_INT offset = 0;
13659 if (GET_CODE (varloc) == VAR_LOCATION
13660 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13662 varloc = PAT_VAR_LOCATION_LOC (varloc);
13663 if (GET_CODE (varloc) == EXPR_LIST)
13664 varloc = XEXP (varloc, 0);
13668 if (GET_CODE (varloc) == CONST
13669 || GET_CODE (varloc) == SIGN_EXTEND
13670 || GET_CODE (varloc) == ZERO_EXTEND)
13671 varloc = XEXP (varloc, 0);
13672 else if (GET_CODE (varloc) == SUBREG)
13673 varloc = SUBREG_REG (varloc);
13674 else
13675 break;
13677 while (1);
13678 /* DW_OP_bit_size offset should be zero for register
13679 or implicit location descriptions and empty location
13680 descriptions, but for memory addresses needs big endian
13681 adjustment. */
13682 if (MEM_P (varloc))
13684 unsigned HOST_WIDE_INT memsize
13685 = MEM_SIZE (varloc) * BITS_PER_UNIT;
13686 if (memsize != bitsize)
13688 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
13689 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
13690 goto discard_descr;
13691 if (memsize < bitsize)
13692 goto discard_descr;
13693 if (BITS_BIG_ENDIAN)
13694 offset = memsize - bitsize;
13698 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
13699 if (*descr_tail == NULL)
13700 goto discard_descr;
13701 descr_tail = &(*descr_tail)->dw_loc_next;
13705 /* If there were any non-empty expressions, add padding till the end of
13706 the decl. */
13707 if (descr != NULL && decl_size != 0)
13709 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
13710 if (*descr_tail == NULL)
13711 goto discard_descr;
13713 return descr;
13715 discard_descr:
13716 /* Discard the descriptor and release any addr_table entries it uses. */
13717 remove_loc_list_addr_table_entries (descr);
13718 return NULL;
13721 /* Return the dwarf representation of the location list LOC_LIST of
13722 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
13723 function. */
13725 static dw_loc_list_ref
13726 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
13728 const char *endname, *secname;
13729 rtx varloc;
13730 enum var_init_status initialized;
13731 struct var_loc_node *node;
13732 dw_loc_descr_ref descr;
13733 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13734 dw_loc_list_ref list = NULL;
13735 dw_loc_list_ref *listp = &list;
13737 /* Now that we know what section we are using for a base,
13738 actually construct the list of locations.
13739 The first location information is what is passed to the
13740 function that creates the location list, and the remaining
13741 locations just get added on to that list.
13742 Note that we only know the start address for a location
13743 (IE location changes), so to build the range, we use
13744 the range [current location start, next location start].
13745 This means we have to special case the last node, and generate
13746 a range of [last location start, end of function label]. */
13748 secname = secname_for_decl (decl);
13750 for (node = loc_list->first; node; node = node->next)
13751 if (GET_CODE (node->loc) == EXPR_LIST
13752 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
13754 if (GET_CODE (node->loc) == EXPR_LIST)
13756 /* This requires DW_OP_{,bit_}piece, which is not usable
13757 inside DWARF expressions. */
13758 if (want_address != 2)
13759 continue;
13760 descr = dw_sra_loc_expr (decl, node->loc);
13761 if (descr == NULL)
13762 continue;
13764 else
13766 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13767 varloc = NOTE_VAR_LOCATION (node->loc);
13768 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
13770 if (descr)
13772 bool range_across_switch = false;
13773 /* If section switch happens in between node->label
13774 and node->next->label (or end of function) and
13775 we can't emit it as a single entry list,
13776 emit two ranges, first one ending at the end
13777 of first partition and second one starting at the
13778 beginning of second partition. */
13779 if (node == loc_list->last_before_switch
13780 && (node != loc_list->first || loc_list->first->next)
13781 && current_function_decl)
13783 endname = cfun->fde->dw_fde_end;
13784 range_across_switch = true;
13786 /* The variable has a location between NODE->LABEL and
13787 NODE->NEXT->LABEL. */
13788 else if (node->next)
13789 endname = node->next->label;
13790 /* If the variable has a location at the last label
13791 it keeps its location until the end of function. */
13792 else if (!current_function_decl)
13793 endname = text_end_label;
13794 else
13796 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13797 current_function_funcdef_no);
13798 endname = ggc_strdup (label_id);
13801 *listp = new_loc_list (descr, node->label, endname, secname);
13802 if (TREE_CODE (decl) == PARM_DECL
13803 && node == loc_list->first
13804 && NOTE_P (node->loc)
13805 && strcmp (node->label, endname) == 0)
13806 (*listp)->force = true;
13807 listp = &(*listp)->dw_loc_next;
13809 if (range_across_switch)
13811 if (GET_CODE (node->loc) == EXPR_LIST)
13812 descr = dw_sra_loc_expr (decl, node->loc);
13813 else
13815 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13816 varloc = NOTE_VAR_LOCATION (node->loc);
13817 descr = dw_loc_list_1 (decl, varloc, want_address,
13818 initialized);
13820 gcc_assert (descr);
13821 /* The variable has a location between NODE->LABEL and
13822 NODE->NEXT->LABEL. */
13823 if (node->next)
13824 endname = node->next->label;
13825 else
13826 endname = cfun->fde->dw_fde_second_end;
13827 *listp = new_loc_list (descr,
13828 cfun->fde->dw_fde_second_begin,
13829 endname, secname);
13830 listp = &(*listp)->dw_loc_next;
13835 /* Try to avoid the overhead of a location list emitting a location
13836 expression instead, but only if we didn't have more than one
13837 location entry in the first place. If some entries were not
13838 representable, we don't want to pretend a single entry that was
13839 applies to the entire scope in which the variable is
13840 available. */
13841 if (list && loc_list->first->next)
13842 gen_llsym (list);
13844 return list;
13847 /* Return if the loc_list has only single element and thus can be represented
13848 as location description. */
13850 static bool
13851 single_element_loc_list_p (dw_loc_list_ref list)
13853 gcc_assert (!list->dw_loc_next || list->ll_symbol);
13854 return !list->ll_symbol;
13857 /* To each location in list LIST add loc descr REF. */
13859 static void
13860 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
13862 dw_loc_descr_ref copy;
13863 add_loc_descr (&list->expr, ref);
13864 list = list->dw_loc_next;
13865 while (list)
13867 copy = ggc_alloc_dw_loc_descr_node ();
13868 memcpy (copy, ref, sizeof (dw_loc_descr_node));
13869 add_loc_descr (&list->expr, copy);
13870 while (copy->dw_loc_next)
13872 dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
13873 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
13874 copy->dw_loc_next = new_copy;
13875 copy = new_copy;
13877 list = list->dw_loc_next;
13881 /* Given two lists RET and LIST
13882 produce location list that is result of adding expression in LIST
13883 to expression in RET on each position in program.
13884 Might be destructive on both RET and LIST.
13886 TODO: We handle only simple cases of RET or LIST having at most one
13887 element. General case would inolve sorting the lists in program order
13888 and merging them that will need some additional work.
13889 Adding that will improve quality of debug info especially for SRA-ed
13890 structures. */
13892 static void
13893 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
13895 if (!list)
13896 return;
13897 if (!*ret)
13899 *ret = list;
13900 return;
13902 if (!list->dw_loc_next)
13904 add_loc_descr_to_each (*ret, list->expr);
13905 return;
13907 if (!(*ret)->dw_loc_next)
13909 add_loc_descr_to_each (list, (*ret)->expr);
13910 *ret = list;
13911 return;
13913 expansion_failed (NULL_TREE, NULL_RTX,
13914 "Don't know how to merge two non-trivial"
13915 " location lists.\n");
13916 *ret = NULL;
13917 return;
13920 /* LOC is constant expression. Try a luck, look it up in constant
13921 pool and return its loc_descr of its address. */
13923 static dw_loc_descr_ref
13924 cst_pool_loc_descr (tree loc)
13926 /* Get an RTL for this, if something has been emitted. */
13927 rtx rtl = lookup_constant_def (loc);
13929 if (!rtl || !MEM_P (rtl))
13931 gcc_assert (!rtl);
13932 return 0;
13934 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
13936 /* TODO: We might get more coverage if we was actually delaying expansion
13937 of all expressions till end of compilation when constant pools are fully
13938 populated. */
13939 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
13941 expansion_failed (loc, NULL_RTX,
13942 "CST value in contant pool but not marked.");
13943 return 0;
13945 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13946 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
13949 /* Return dw_loc_list representing address of addr_expr LOC
13950 by looking for inner INDIRECT_REF expression and turning
13951 it into simple arithmetics. */
13953 static dw_loc_list_ref
13954 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
13956 tree obj, offset;
13957 HOST_WIDE_INT bitsize, bitpos, bytepos;
13958 enum machine_mode mode;
13959 int unsignedp, volatilep = 0;
13960 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13962 obj = get_inner_reference (TREE_OPERAND (loc, 0),
13963 &bitsize, &bitpos, &offset, &mode,
13964 &unsignedp, &volatilep, false);
13965 STRIP_NOPS (obj);
13966 if (bitpos % BITS_PER_UNIT)
13968 expansion_failed (loc, NULL_RTX, "bitfield access");
13969 return 0;
13971 if (!INDIRECT_REF_P (obj))
13973 expansion_failed (obj,
13974 NULL_RTX, "no indirect ref in inner refrence");
13975 return 0;
13977 if (!offset && !bitpos)
13978 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
13979 else if (toplev
13980 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
13981 && (dwarf_version >= 4 || !dwarf_strict))
13983 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
13984 if (!list_ret)
13985 return 0;
13986 if (offset)
13988 /* Variable offset. */
13989 list_ret1 = loc_list_from_tree (offset, 0);
13990 if (list_ret1 == 0)
13991 return 0;
13992 add_loc_list (&list_ret, list_ret1);
13993 if (!list_ret)
13994 return 0;
13995 add_loc_descr_to_each (list_ret,
13996 new_loc_descr (DW_OP_plus, 0, 0));
13998 bytepos = bitpos / BITS_PER_UNIT;
13999 if (bytepos > 0)
14000 add_loc_descr_to_each (list_ret,
14001 new_loc_descr (DW_OP_plus_uconst,
14002 bytepos, 0));
14003 else if (bytepos < 0)
14004 loc_list_plus_const (list_ret, bytepos);
14005 add_loc_descr_to_each (list_ret,
14006 new_loc_descr (DW_OP_stack_value, 0, 0));
14008 return list_ret;
14012 /* Generate Dwarf location list representing LOC.
14013 If WANT_ADDRESS is false, expression computing LOC will be computed
14014 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
14015 if WANT_ADDRESS is 2, expression computing address useable in location
14016 will be returned (i.e. DW_OP_reg can be used
14017 to refer to register values). */
14019 static dw_loc_list_ref
14020 loc_list_from_tree (tree loc, int want_address)
14022 dw_loc_descr_ref ret = NULL, ret1 = NULL;
14023 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14024 int have_address = 0;
14025 enum dwarf_location_atom op;
14027 /* ??? Most of the time we do not take proper care for sign/zero
14028 extending the values properly. Hopefully this won't be a real
14029 problem... */
14031 switch (TREE_CODE (loc))
14033 case ERROR_MARK:
14034 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
14035 return 0;
14037 case PLACEHOLDER_EXPR:
14038 /* This case involves extracting fields from an object to determine the
14039 position of other fields. We don't try to encode this here. The
14040 only user of this is Ada, which encodes the needed information using
14041 the names of types. */
14042 expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
14043 return 0;
14045 case CALL_EXPR:
14046 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
14047 /* There are no opcodes for these operations. */
14048 return 0;
14050 case PREINCREMENT_EXPR:
14051 case PREDECREMENT_EXPR:
14052 case POSTINCREMENT_EXPR:
14053 case POSTDECREMENT_EXPR:
14054 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
14055 /* There are no opcodes for these operations. */
14056 return 0;
14058 case ADDR_EXPR:
14059 /* If we already want an address, see if there is INDIRECT_REF inside
14060 e.g. for &this->field. */
14061 if (want_address)
14063 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
14064 (loc, want_address == 2);
14065 if (list_ret)
14066 have_address = 1;
14067 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
14068 && (ret = cst_pool_loc_descr (loc)))
14069 have_address = 1;
14071 /* Otherwise, process the argument and look for the address. */
14072 if (!list_ret && !ret)
14073 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
14074 else
14076 if (want_address)
14077 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
14078 return NULL;
14080 break;
14082 case VAR_DECL:
14083 if (DECL_THREAD_LOCAL_P (loc))
14085 rtx rtl;
14086 enum dwarf_location_atom tls_op;
14087 enum dtprel_bool dtprel = dtprel_false;
14089 if (targetm.have_tls)
14091 /* If this is not defined, we have no way to emit the
14092 data. */
14093 if (!targetm.asm_out.output_dwarf_dtprel)
14094 return 0;
14096 /* The way DW_OP_GNU_push_tls_address is specified, we
14097 can only look up addresses of objects in the current
14098 module. We used DW_OP_addr as first op, but that's
14099 wrong, because DW_OP_addr is relocated by the debug
14100 info consumer, while DW_OP_GNU_push_tls_address
14101 operand shouldn't be. */
14102 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
14103 return 0;
14104 dtprel = dtprel_true;
14105 tls_op = DW_OP_GNU_push_tls_address;
14107 else
14109 if (!targetm.emutls.debug_form_tls_address
14110 || !(dwarf_version >= 3 || !dwarf_strict))
14111 return 0;
14112 /* We stuffed the control variable into the DECL_VALUE_EXPR
14113 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
14114 no longer appear in gimple code. We used the control
14115 variable in specific so that we could pick it up here. */
14116 loc = DECL_VALUE_EXPR (loc);
14117 tls_op = DW_OP_form_tls_address;
14120 rtl = rtl_for_decl_location (loc);
14121 if (rtl == NULL_RTX)
14122 return 0;
14124 if (!MEM_P (rtl))
14125 return 0;
14126 rtl = XEXP (rtl, 0);
14127 if (! CONSTANT_P (rtl))
14128 return 0;
14130 ret = new_addr_loc_descr (rtl, dtprel);
14131 ret1 = new_loc_descr (tls_op, 0, 0);
14132 add_loc_descr (&ret, ret1);
14134 have_address = 1;
14135 break;
14137 /* FALLTHRU */
14139 case PARM_DECL:
14140 case RESULT_DECL:
14141 if (DECL_HAS_VALUE_EXPR_P (loc))
14142 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
14143 want_address);
14144 /* FALLTHRU */
14146 case FUNCTION_DECL:
14148 rtx rtl;
14149 var_loc_list *loc_list = lookup_decl_loc (loc);
14151 if (loc_list && loc_list->first)
14153 list_ret = dw_loc_list (loc_list, loc, want_address);
14154 have_address = want_address != 0;
14155 break;
14157 rtl = rtl_for_decl_location (loc);
14158 if (rtl == NULL_RTX)
14160 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
14161 return 0;
14163 else if (CONST_INT_P (rtl))
14165 HOST_WIDE_INT val = INTVAL (rtl);
14166 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14167 val &= GET_MODE_MASK (DECL_MODE (loc));
14168 ret = int_loc_descriptor (val);
14170 else if (GET_CODE (rtl) == CONST_STRING)
14172 expansion_failed (loc, NULL_RTX, "CONST_STRING");
14173 return 0;
14175 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
14176 ret = new_addr_loc_descr (rtl, dtprel_false);
14177 else
14179 enum machine_mode mode, mem_mode;
14181 /* Certain constructs can only be represented at top-level. */
14182 if (want_address == 2)
14184 ret = loc_descriptor (rtl, VOIDmode,
14185 VAR_INIT_STATUS_INITIALIZED);
14186 have_address = 1;
14188 else
14190 mode = GET_MODE (rtl);
14191 mem_mode = VOIDmode;
14192 if (MEM_P (rtl))
14194 mem_mode = mode;
14195 mode = get_address_mode (rtl);
14196 rtl = XEXP (rtl, 0);
14197 have_address = 1;
14199 ret = mem_loc_descriptor (rtl, mode, mem_mode,
14200 VAR_INIT_STATUS_INITIALIZED);
14202 if (!ret)
14203 expansion_failed (loc, rtl,
14204 "failed to produce loc descriptor for rtl");
14207 break;
14209 case MEM_REF:
14210 /* ??? FIXME. */
14211 if (!integer_zerop (TREE_OPERAND (loc, 1)))
14212 return 0;
14213 /* Fallthru. */
14214 case INDIRECT_REF:
14215 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14216 have_address = 1;
14217 break;
14219 case COMPOUND_EXPR:
14220 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
14222 CASE_CONVERT:
14223 case VIEW_CONVERT_EXPR:
14224 case SAVE_EXPR:
14225 case MODIFY_EXPR:
14226 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
14228 case COMPONENT_REF:
14229 case BIT_FIELD_REF:
14230 case ARRAY_REF:
14231 case ARRAY_RANGE_REF:
14232 case REALPART_EXPR:
14233 case IMAGPART_EXPR:
14235 tree obj, offset;
14236 HOST_WIDE_INT bitsize, bitpos, bytepos;
14237 enum machine_mode mode;
14238 int unsignedp, volatilep = 0;
14240 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14241 &unsignedp, &volatilep, false);
14243 gcc_assert (obj != loc);
14245 list_ret = loc_list_from_tree (obj,
14246 want_address == 2
14247 && !bitpos && !offset ? 2 : 1);
14248 /* TODO: We can extract value of the small expression via shifting even
14249 for nonzero bitpos. */
14250 if (list_ret == 0)
14251 return 0;
14252 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14254 expansion_failed (loc, NULL_RTX,
14255 "bitfield access");
14256 return 0;
14259 if (offset != NULL_TREE)
14261 /* Variable offset. */
14262 list_ret1 = loc_list_from_tree (offset, 0);
14263 if (list_ret1 == 0)
14264 return 0;
14265 add_loc_list (&list_ret, list_ret1);
14266 if (!list_ret)
14267 return 0;
14268 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14271 bytepos = bitpos / BITS_PER_UNIT;
14272 if (bytepos > 0)
14273 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14274 else if (bytepos < 0)
14275 loc_list_plus_const (list_ret, bytepos);
14277 have_address = 1;
14278 break;
14281 case INTEGER_CST:
14282 if ((want_address || !tree_fits_shwi_p (loc))
14283 && (ret = cst_pool_loc_descr (loc)))
14284 have_address = 1;
14285 else if (want_address == 2
14286 && tree_fits_shwi_p (loc)
14287 && (ret = address_of_int_loc_descriptor
14288 (int_size_in_bytes (TREE_TYPE (loc)),
14289 tree_to_shwi (loc))))
14290 have_address = 1;
14291 else if (tree_fits_shwi_p (loc))
14292 ret = int_loc_descriptor (tree_to_shwi (loc));
14293 else
14295 expansion_failed (loc, NULL_RTX,
14296 "Integer operand is not host integer");
14297 return 0;
14299 break;
14301 case CONSTRUCTOR:
14302 case REAL_CST:
14303 case STRING_CST:
14304 case COMPLEX_CST:
14305 if ((ret = cst_pool_loc_descr (loc)))
14306 have_address = 1;
14307 else
14308 /* We can construct small constants here using int_loc_descriptor. */
14309 expansion_failed (loc, NULL_RTX,
14310 "constructor or constant not in constant pool");
14311 break;
14313 case TRUTH_AND_EXPR:
14314 case TRUTH_ANDIF_EXPR:
14315 case BIT_AND_EXPR:
14316 op = DW_OP_and;
14317 goto do_binop;
14319 case TRUTH_XOR_EXPR:
14320 case BIT_XOR_EXPR:
14321 op = DW_OP_xor;
14322 goto do_binop;
14324 case TRUTH_OR_EXPR:
14325 case TRUTH_ORIF_EXPR:
14326 case BIT_IOR_EXPR:
14327 op = DW_OP_or;
14328 goto do_binop;
14330 case FLOOR_DIV_EXPR:
14331 case CEIL_DIV_EXPR:
14332 case ROUND_DIV_EXPR:
14333 case TRUNC_DIV_EXPR:
14334 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14335 return 0;
14336 op = DW_OP_div;
14337 goto do_binop;
14339 case MINUS_EXPR:
14340 op = DW_OP_minus;
14341 goto do_binop;
14343 case FLOOR_MOD_EXPR:
14344 case CEIL_MOD_EXPR:
14345 case ROUND_MOD_EXPR:
14346 case TRUNC_MOD_EXPR:
14347 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14349 op = DW_OP_mod;
14350 goto do_binop;
14352 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14353 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14354 if (list_ret == 0 || list_ret1 == 0)
14355 return 0;
14357 add_loc_list (&list_ret, list_ret1);
14358 if (list_ret == 0)
14359 return 0;
14360 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14361 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14362 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14363 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14364 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14365 break;
14367 case MULT_EXPR:
14368 op = DW_OP_mul;
14369 goto do_binop;
14371 case LSHIFT_EXPR:
14372 op = DW_OP_shl;
14373 goto do_binop;
14375 case RSHIFT_EXPR:
14376 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14377 goto do_binop;
14379 case POINTER_PLUS_EXPR:
14380 case PLUS_EXPR:
14381 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
14383 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14384 if (list_ret == 0)
14385 return 0;
14387 loc_list_plus_const (list_ret, tree_to_shwi (TREE_OPERAND (loc, 1)));
14388 break;
14391 op = DW_OP_plus;
14392 goto do_binop;
14394 case LE_EXPR:
14395 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14396 return 0;
14398 op = DW_OP_le;
14399 goto do_binop;
14401 case GE_EXPR:
14402 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14403 return 0;
14405 op = DW_OP_ge;
14406 goto do_binop;
14408 case LT_EXPR:
14409 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14410 return 0;
14412 op = DW_OP_lt;
14413 goto do_binop;
14415 case GT_EXPR:
14416 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14417 return 0;
14419 op = DW_OP_gt;
14420 goto do_binop;
14422 case EQ_EXPR:
14423 op = DW_OP_eq;
14424 goto do_binop;
14426 case NE_EXPR:
14427 op = DW_OP_ne;
14428 goto do_binop;
14430 do_binop:
14431 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14432 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14433 if (list_ret == 0 || list_ret1 == 0)
14434 return 0;
14436 add_loc_list (&list_ret, list_ret1);
14437 if (list_ret == 0)
14438 return 0;
14439 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14440 break;
14442 case TRUTH_NOT_EXPR:
14443 case BIT_NOT_EXPR:
14444 op = DW_OP_not;
14445 goto do_unop;
14447 case ABS_EXPR:
14448 op = DW_OP_abs;
14449 goto do_unop;
14451 case NEGATE_EXPR:
14452 op = DW_OP_neg;
14453 goto do_unop;
14455 do_unop:
14456 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14457 if (list_ret == 0)
14458 return 0;
14460 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14461 break;
14463 case MIN_EXPR:
14464 case MAX_EXPR:
14466 const enum tree_code code =
14467 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14469 loc = build3 (COND_EXPR, TREE_TYPE (loc),
14470 build2 (code, integer_type_node,
14471 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14472 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
14475 /* ... fall through ... */
14477 case COND_EXPR:
14479 dw_loc_descr_ref lhs
14480 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
14481 dw_loc_list_ref rhs
14482 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
14483 dw_loc_descr_ref bra_node, jump_node, tmp;
14485 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14486 if (list_ret == 0 || lhs == 0 || rhs == 0)
14487 return 0;
14489 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14490 add_loc_descr_to_each (list_ret, bra_node);
14492 add_loc_list (&list_ret, rhs);
14493 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
14494 add_loc_descr_to_each (list_ret, jump_node);
14496 add_loc_descr_to_each (list_ret, lhs);
14497 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14498 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
14500 /* ??? Need a node to point the skip at. Use a nop. */
14501 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14502 add_loc_descr_to_each (list_ret, tmp);
14503 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14504 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
14506 break;
14508 case FIX_TRUNC_EXPR:
14509 return 0;
14511 default:
14512 /* Leave front-end specific codes as simply unknown. This comes
14513 up, for instance, with the C STMT_EXPR. */
14514 if ((unsigned int) TREE_CODE (loc)
14515 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
14517 expansion_failed (loc, NULL_RTX,
14518 "language specific tree node");
14519 return 0;
14522 #ifdef ENABLE_CHECKING
14523 /* Otherwise this is a generic code; we should just lists all of
14524 these explicitly. We forgot one. */
14525 gcc_unreachable ();
14526 #else
14527 /* In a release build, we want to degrade gracefully: better to
14528 generate incomplete debugging information than to crash. */
14529 return NULL;
14530 #endif
14533 if (!ret && !list_ret)
14534 return 0;
14536 if (want_address == 2 && !have_address
14537 && (dwarf_version >= 4 || !dwarf_strict))
14539 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14541 expansion_failed (loc, NULL_RTX,
14542 "DWARF address size mismatch");
14543 return 0;
14545 if (ret)
14546 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
14547 else
14548 add_loc_descr_to_each (list_ret,
14549 new_loc_descr (DW_OP_stack_value, 0, 0));
14550 have_address = 1;
14552 /* Show if we can't fill the request for an address. */
14553 if (want_address && !have_address)
14555 expansion_failed (loc, NULL_RTX,
14556 "Want address and only have value");
14557 return 0;
14560 gcc_assert (!ret || !list_ret);
14562 /* If we've got an address and don't want one, dereference. */
14563 if (!want_address && have_address)
14565 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14567 if (size > DWARF2_ADDR_SIZE || size == -1)
14569 expansion_failed (loc, NULL_RTX,
14570 "DWARF address size mismatch");
14571 return 0;
14573 else if (size == DWARF2_ADDR_SIZE)
14574 op = DW_OP_deref;
14575 else
14576 op = DW_OP_deref_size;
14578 if (ret)
14579 add_loc_descr (&ret, new_loc_descr (op, size, 0));
14580 else
14581 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
14583 if (ret)
14584 list_ret = new_loc_list (ret, NULL, NULL, NULL);
14586 return list_ret;
14589 /* Same as above but return only single location expression. */
14590 static dw_loc_descr_ref
14591 loc_descriptor_from_tree (tree loc, int want_address)
14593 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
14594 if (!ret)
14595 return NULL;
14596 if (ret->dw_loc_next)
14598 expansion_failed (loc, NULL_RTX,
14599 "Location list where only loc descriptor needed");
14600 return NULL;
14602 return ret->expr;
14605 /* Given a value, round it up to the lowest multiple of `boundary'
14606 which is not less than the value itself. */
14608 static inline HOST_WIDE_INT
14609 ceiling (HOST_WIDE_INT value, unsigned int boundary)
14611 return (((value + boundary - 1) / boundary) * boundary);
14614 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
14615 pointer to the declared type for the relevant field variable, or return
14616 `integer_type_node' if the given node turns out to be an
14617 ERROR_MARK node. */
14619 static inline tree
14620 field_type (const_tree decl)
14622 tree type;
14624 if (TREE_CODE (decl) == ERROR_MARK)
14625 return integer_type_node;
14627 type = DECL_BIT_FIELD_TYPE (decl);
14628 if (type == NULL_TREE)
14629 type = TREE_TYPE (decl);
14631 return type;
14634 /* Given a pointer to a tree node, return the alignment in bits for
14635 it, or else return BITS_PER_WORD if the node actually turns out to
14636 be an ERROR_MARK node. */
14638 static inline unsigned
14639 simple_type_align_in_bits (const_tree type)
14641 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
14644 static inline unsigned
14645 simple_decl_align_in_bits (const_tree decl)
14647 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
14650 /* Return the result of rounding T up to ALIGN. */
14652 static inline double_int
14653 round_up_to_align (double_int t, unsigned int align)
14655 double_int alignd = double_int::from_uhwi (align);
14656 t += alignd;
14657 t += double_int_minus_one;
14658 t = t.div (alignd, true, TRUNC_DIV_EXPR);
14659 t *= alignd;
14660 return t;
14663 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
14664 lowest addressed byte of the "containing object" for the given FIELD_DECL,
14665 or return 0 if we are unable to determine what that offset is, either
14666 because the argument turns out to be a pointer to an ERROR_MARK node, or
14667 because the offset is actually variable. (We can't handle the latter case
14668 just yet). */
14670 static HOST_WIDE_INT
14671 field_byte_offset (const_tree decl)
14673 double_int object_offset_in_bits;
14674 double_int object_offset_in_bytes;
14675 double_int bitpos_int;
14677 if (TREE_CODE (decl) == ERROR_MARK)
14678 return 0;
14680 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
14682 /* We cannot yet cope with fields whose positions are variable, so
14683 for now, when we see such things, we simply return 0. Someday, we may
14684 be able to handle such cases, but it will be damn difficult. */
14685 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
14686 return 0;
14688 bitpos_int = tree_to_double_int (bit_position (decl));
14690 #ifdef PCC_BITFIELD_TYPE_MATTERS
14691 if (PCC_BITFIELD_TYPE_MATTERS)
14693 tree type;
14694 tree field_size_tree;
14695 double_int deepest_bitpos;
14696 double_int field_size_in_bits;
14697 unsigned int type_align_in_bits;
14698 unsigned int decl_align_in_bits;
14699 double_int type_size_in_bits;
14701 type = field_type (decl);
14702 type_size_in_bits = double_int_type_size_in_bits (type);
14703 type_align_in_bits = simple_type_align_in_bits (type);
14705 field_size_tree = DECL_SIZE (decl);
14707 /* The size could be unspecified if there was an error, or for
14708 a flexible array member. */
14709 if (!field_size_tree)
14710 field_size_tree = bitsize_zero_node;
14712 /* If the size of the field is not constant, use the type size. */
14713 if (TREE_CODE (field_size_tree) == INTEGER_CST)
14714 field_size_in_bits = tree_to_double_int (field_size_tree);
14715 else
14716 field_size_in_bits = type_size_in_bits;
14718 decl_align_in_bits = simple_decl_align_in_bits (decl);
14720 /* The GCC front-end doesn't make any attempt to keep track of the
14721 starting bit offset (relative to the start of the containing
14722 structure type) of the hypothetical "containing object" for a
14723 bit-field. Thus, when computing the byte offset value for the
14724 start of the "containing object" of a bit-field, we must deduce
14725 this information on our own. This can be rather tricky to do in
14726 some cases. For example, handling the following structure type
14727 definition when compiling for an i386/i486 target (which only
14728 aligns long long's to 32-bit boundaries) can be very tricky:
14730 struct S { int field1; long long field2:31; };
14732 Fortunately, there is a simple rule-of-thumb which can be used
14733 in such cases. When compiling for an i386/i486, GCC will
14734 allocate 8 bytes for the structure shown above. It decides to
14735 do this based upon one simple rule for bit-field allocation.
14736 GCC allocates each "containing object" for each bit-field at
14737 the first (i.e. lowest addressed) legitimate alignment boundary
14738 (based upon the required minimum alignment for the declared
14739 type of the field) which it can possibly use, subject to the
14740 condition that there is still enough available space remaining
14741 in the containing object (when allocated at the selected point)
14742 to fully accommodate all of the bits of the bit-field itself.
14744 This simple rule makes it obvious why GCC allocates 8 bytes for
14745 each object of the structure type shown above. When looking
14746 for a place to allocate the "containing object" for `field2',
14747 the compiler simply tries to allocate a 64-bit "containing
14748 object" at each successive 32-bit boundary (starting at zero)
14749 until it finds a place to allocate that 64- bit field such that
14750 at least 31 contiguous (and previously unallocated) bits remain
14751 within that selected 64 bit field. (As it turns out, for the
14752 example above, the compiler finds it is OK to allocate the
14753 "containing object" 64-bit field at bit-offset zero within the
14754 structure type.)
14756 Here we attempt to work backwards from the limited set of facts
14757 we're given, and we try to deduce from those facts, where GCC
14758 must have believed that the containing object started (within
14759 the structure type). The value we deduce is then used (by the
14760 callers of this routine) to generate DW_AT_location and
14761 DW_AT_bit_offset attributes for fields (both bit-fields and, in
14762 the case of DW_AT_location, regular fields as well). */
14764 /* Figure out the bit-distance from the start of the structure to
14765 the "deepest" bit of the bit-field. */
14766 deepest_bitpos = bitpos_int + field_size_in_bits;
14768 /* This is the tricky part. Use some fancy footwork to deduce
14769 where the lowest addressed bit of the containing object must
14770 be. */
14771 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14773 /* Round up to type_align by default. This works best for
14774 bitfields. */
14775 object_offset_in_bits
14776 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
14778 if (object_offset_in_bits.ugt (bitpos_int))
14780 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14782 /* Round up to decl_align instead. */
14783 object_offset_in_bits
14784 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
14787 else
14788 #endif /* PCC_BITFIELD_TYPE_MATTERS */
14789 object_offset_in_bits = bitpos_int;
14791 object_offset_in_bytes
14792 = object_offset_in_bits.div (double_int::from_uhwi (BITS_PER_UNIT),
14793 true, TRUNC_DIV_EXPR);
14794 return object_offset_in_bytes.to_shwi ();
14797 /* The following routines define various Dwarf attributes and any data
14798 associated with them. */
14800 /* Add a location description attribute value to a DIE.
14802 This emits location attributes suitable for whole variables and
14803 whole parameters. Note that the location attributes for struct fields are
14804 generated by the routine `data_member_location_attribute' below. */
14806 static inline void
14807 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
14808 dw_loc_list_ref descr)
14810 if (descr == 0)
14811 return;
14812 if (single_element_loc_list_p (descr))
14813 add_AT_loc (die, attr_kind, descr->expr);
14814 else
14815 add_AT_loc_list (die, attr_kind, descr);
14818 /* Add DW_AT_accessibility attribute to DIE if needed. */
14820 static void
14821 add_accessibility_attribute (dw_die_ref die, tree decl)
14823 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
14824 children, otherwise the default is DW_ACCESS_public. In DWARF2
14825 the default has always been DW_ACCESS_public. */
14826 if (TREE_PROTECTED (decl))
14827 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
14828 else if (TREE_PRIVATE (decl))
14830 if (dwarf_version == 2
14831 || die->die_parent == NULL
14832 || die->die_parent->die_tag != DW_TAG_class_type)
14833 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
14835 else if (dwarf_version > 2
14836 && die->die_parent
14837 && die->die_parent->die_tag == DW_TAG_class_type)
14838 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
14841 /* Attach the specialized form of location attribute used for data members of
14842 struct and union types. In the special case of a FIELD_DECL node which
14843 represents a bit-field, the "offset" part of this special location
14844 descriptor must indicate the distance in bytes from the lowest-addressed
14845 byte of the containing struct or union type to the lowest-addressed byte of
14846 the "containing object" for the bit-field. (See the `field_byte_offset'
14847 function above).
14849 For any given bit-field, the "containing object" is a hypothetical object
14850 (of some integral or enum type) within which the given bit-field lives. The
14851 type of this hypothetical "containing object" is always the same as the
14852 declared type of the individual bit-field itself (for GCC anyway... the
14853 DWARF spec doesn't actually mandate this). Note that it is the size (in
14854 bytes) of the hypothetical "containing object" which will be given in the
14855 DW_AT_byte_size attribute for this bit-field. (See the
14856 `byte_size_attribute' function below.) It is also used when calculating the
14857 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
14858 function below.) */
14860 static void
14861 add_data_member_location_attribute (dw_die_ref die, tree decl)
14863 HOST_WIDE_INT offset;
14864 dw_loc_descr_ref loc_descr = 0;
14866 if (TREE_CODE (decl) == TREE_BINFO)
14868 /* We're working on the TAG_inheritance for a base class. */
14869 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
14871 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
14872 aren't at a fixed offset from all (sub)objects of the same
14873 type. We need to extract the appropriate offset from our
14874 vtable. The following dwarf expression means
14876 BaseAddr = ObAddr + *((*ObAddr) - Offset)
14878 This is specific to the V3 ABI, of course. */
14880 dw_loc_descr_ref tmp;
14882 /* Make a copy of the object address. */
14883 tmp = new_loc_descr (DW_OP_dup, 0, 0);
14884 add_loc_descr (&loc_descr, tmp);
14886 /* Extract the vtable address. */
14887 tmp = new_loc_descr (DW_OP_deref, 0, 0);
14888 add_loc_descr (&loc_descr, tmp);
14890 /* Calculate the address of the offset. */
14891 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
14892 gcc_assert (offset < 0);
14894 tmp = int_loc_descriptor (-offset);
14895 add_loc_descr (&loc_descr, tmp);
14896 tmp = new_loc_descr (DW_OP_minus, 0, 0);
14897 add_loc_descr (&loc_descr, tmp);
14899 /* Extract the offset. */
14900 tmp = new_loc_descr (DW_OP_deref, 0, 0);
14901 add_loc_descr (&loc_descr, tmp);
14903 /* Add it to the object address. */
14904 tmp = new_loc_descr (DW_OP_plus, 0, 0);
14905 add_loc_descr (&loc_descr, tmp);
14907 else
14908 offset = tree_to_shwi (BINFO_OFFSET (decl));
14910 else
14911 offset = field_byte_offset (decl);
14913 if (! loc_descr)
14915 if (dwarf_version > 2)
14917 /* Don't need to output a location expression, just the constant. */
14918 if (offset < 0)
14919 add_AT_int (die, DW_AT_data_member_location, offset);
14920 else
14921 add_AT_unsigned (die, DW_AT_data_member_location, offset);
14922 return;
14924 else
14926 enum dwarf_location_atom op;
14928 /* The DWARF2 standard says that we should assume that the structure
14929 address is already on the stack, so we can specify a structure
14930 field address by using DW_OP_plus_uconst. */
14931 op = DW_OP_plus_uconst;
14932 loc_descr = new_loc_descr (op, offset, 0);
14936 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
14939 /* Writes integer values to dw_vec_const array. */
14941 static void
14942 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
14944 while (size != 0)
14946 *dest++ = val & 0xff;
14947 val >>= 8;
14948 --size;
14952 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
14954 static HOST_WIDE_INT
14955 extract_int (const unsigned char *src, unsigned int size)
14957 HOST_WIDE_INT val = 0;
14959 src += size;
14960 while (size != 0)
14962 val <<= 8;
14963 val |= *--src & 0xff;
14964 --size;
14966 return val;
14969 /* Writes double_int values to dw_vec_const array. */
14971 static void
14972 insert_double (double_int val, unsigned char *dest)
14974 unsigned char *p0 = dest;
14975 unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
14977 if (WORDS_BIG_ENDIAN)
14979 p0 = p1;
14980 p1 = dest;
14983 insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
14984 insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
14987 /* Writes floating point values to dw_vec_const array. */
14989 static void
14990 insert_float (const_rtx rtl, unsigned char *array)
14992 REAL_VALUE_TYPE rv;
14993 long val[4];
14994 int i;
14996 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
14997 real_to_target (val, &rv, GET_MODE (rtl));
14999 /* real_to_target puts 32-bit pieces in each long. Pack them. */
15000 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
15002 insert_int (val[i], 4, array);
15003 array += 4;
15007 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
15008 does not have a "location" either in memory or in a register. These
15009 things can arise in GNU C when a constant is passed as an actual parameter
15010 to an inlined function. They can also arise in C++ where declared
15011 constants do not necessarily get memory "homes". */
15013 static bool
15014 add_const_value_attribute (dw_die_ref die, rtx rtl)
15016 switch (GET_CODE (rtl))
15018 case CONST_INT:
15020 HOST_WIDE_INT val = INTVAL (rtl);
15022 if (val < 0)
15023 add_AT_int (die, DW_AT_const_value, val);
15024 else
15025 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
15027 return true;
15029 case CONST_DOUBLE:
15030 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
15031 floating-point constant. A CONST_DOUBLE is used whenever the
15032 constant requires more than one word in order to be adequately
15033 represented. */
15035 enum machine_mode mode = GET_MODE (rtl);
15037 if (SCALAR_FLOAT_MODE_P (mode))
15039 unsigned int length = GET_MODE_SIZE (mode);
15040 unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
15042 insert_float (rtl, array);
15043 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
15045 else
15046 add_AT_double (die, DW_AT_const_value,
15047 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
15049 return true;
15051 case CONST_VECTOR:
15053 enum machine_mode mode = GET_MODE (rtl);
15054 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
15055 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15056 unsigned char *array = (unsigned char *) ggc_alloc_atomic
15057 (length * elt_size);
15058 unsigned int i;
15059 unsigned char *p;
15061 switch (GET_MODE_CLASS (mode))
15063 case MODE_VECTOR_INT:
15064 for (i = 0, p = array; i < length; i++, p += elt_size)
15066 rtx elt = CONST_VECTOR_ELT (rtl, i);
15067 double_int val = rtx_to_double_int (elt);
15069 if (elt_size <= sizeof (HOST_WIDE_INT))
15070 insert_int (val.to_shwi (), elt_size, p);
15071 else
15073 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
15074 insert_double (val, p);
15077 break;
15079 case MODE_VECTOR_FLOAT:
15080 for (i = 0, p = array; i < length; i++, p += elt_size)
15082 rtx elt = CONST_VECTOR_ELT (rtl, i);
15083 insert_float (elt, p);
15085 break;
15087 default:
15088 gcc_unreachable ();
15091 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
15093 return true;
15095 case CONST_STRING:
15096 if (dwarf_version >= 4 || !dwarf_strict)
15098 dw_loc_descr_ref loc_result;
15099 resolve_one_addr (&rtl, NULL);
15100 rtl_addr:
15101 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15102 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15103 add_AT_loc (die, DW_AT_location, loc_result);
15104 vec_safe_push (used_rtx_array, rtl);
15105 return true;
15107 return false;
15109 case CONST:
15110 if (CONSTANT_P (XEXP (rtl, 0)))
15111 return add_const_value_attribute (die, XEXP (rtl, 0));
15112 /* FALLTHROUGH */
15113 case SYMBOL_REF:
15114 if (!const_ok_for_output (rtl))
15115 return false;
15116 case LABEL_REF:
15117 if (dwarf_version >= 4 || !dwarf_strict)
15118 goto rtl_addr;
15119 return false;
15121 case PLUS:
15122 /* In cases where an inlined instance of an inline function is passed
15123 the address of an `auto' variable (which is local to the caller) we
15124 can get a situation where the DECL_RTL of the artificial local
15125 variable (for the inlining) which acts as a stand-in for the
15126 corresponding formal parameter (of the inline function) will look
15127 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
15128 exactly a compile-time constant expression, but it isn't the address
15129 of the (artificial) local variable either. Rather, it represents the
15130 *value* which the artificial local variable always has during its
15131 lifetime. We currently have no way to represent such quasi-constant
15132 values in Dwarf, so for now we just punt and generate nothing. */
15133 return false;
15135 case HIGH:
15136 case CONST_FIXED:
15137 return false;
15139 case MEM:
15140 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
15141 && MEM_READONLY_P (rtl)
15142 && GET_MODE (rtl) == BLKmode)
15144 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
15145 return true;
15147 return false;
15149 default:
15150 /* No other kinds of rtx should be possible here. */
15151 gcc_unreachable ();
15153 return false;
15156 /* Determine whether the evaluation of EXPR references any variables
15157 or functions which aren't otherwise used (and therefore may not be
15158 output). */
15159 static tree
15160 reference_to_unused (tree * tp, int * walk_subtrees,
15161 void * data ATTRIBUTE_UNUSED)
15163 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
15164 *walk_subtrees = 0;
15166 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
15167 && ! TREE_ASM_WRITTEN (*tp))
15168 return *tp;
15169 /* ??? The C++ FE emits debug information for using decls, so
15170 putting gcc_unreachable here falls over. See PR31899. For now
15171 be conservative. */
15172 else if (!cgraph_global_info_ready
15173 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
15174 return *tp;
15175 else if (TREE_CODE (*tp) == VAR_DECL)
15177 varpool_node *node = varpool_get_node (*tp);
15178 if (!node || !node->definition)
15179 return *tp;
15181 else if (TREE_CODE (*tp) == FUNCTION_DECL
15182 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
15184 /* The call graph machinery must have finished analyzing,
15185 optimizing and gimplifying the CU by now.
15186 So if *TP has no call graph node associated
15187 to it, it means *TP will not be emitted. */
15188 if (!cgraph_get_node (*tp))
15189 return *tp;
15191 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
15192 return *tp;
15194 return NULL_TREE;
15197 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
15198 for use in a later add_const_value_attribute call. */
15200 static rtx
15201 rtl_for_decl_init (tree init, tree type)
15203 rtx rtl = NULL_RTX;
15205 STRIP_NOPS (init);
15207 /* If a variable is initialized with a string constant without embedded
15208 zeros, build CONST_STRING. */
15209 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
15211 tree enttype = TREE_TYPE (type);
15212 tree domain = TYPE_DOMAIN (type);
15213 enum machine_mode mode = TYPE_MODE (enttype);
15215 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
15216 && domain
15217 && integer_zerop (TYPE_MIN_VALUE (domain))
15218 && compare_tree_int (TYPE_MAX_VALUE (domain),
15219 TREE_STRING_LENGTH (init) - 1) == 0
15220 && ((size_t) TREE_STRING_LENGTH (init)
15221 == strlen (TREE_STRING_POINTER (init)) + 1))
15223 rtl = gen_rtx_CONST_STRING (VOIDmode,
15224 ggc_strdup (TREE_STRING_POINTER (init)));
15225 rtl = gen_rtx_MEM (BLKmode, rtl);
15226 MEM_READONLY_P (rtl) = 1;
15229 /* Other aggregates, and complex values, could be represented using
15230 CONCAT: FIXME! */
15231 else if (AGGREGATE_TYPE_P (type)
15232 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
15233 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
15234 || TREE_CODE (type) == COMPLEX_TYPE)
15236 /* Vectors only work if their mode is supported by the target.
15237 FIXME: generic vectors ought to work too. */
15238 else if (TREE_CODE (type) == VECTOR_TYPE
15239 && !VECTOR_MODE_P (TYPE_MODE (type)))
15241 /* If the initializer is something that we know will expand into an
15242 immediate RTL constant, expand it now. We must be careful not to
15243 reference variables which won't be output. */
15244 else if (initializer_constant_valid_p (init, type)
15245 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15247 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15248 possible. */
15249 if (TREE_CODE (type) == VECTOR_TYPE)
15250 switch (TREE_CODE (init))
15252 case VECTOR_CST:
15253 break;
15254 case CONSTRUCTOR:
15255 if (TREE_CONSTANT (init))
15257 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
15258 bool constant_p = true;
15259 tree value;
15260 unsigned HOST_WIDE_INT ix;
15262 /* Even when ctor is constant, it might contain non-*_CST
15263 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15264 belong into VECTOR_CST nodes. */
15265 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15266 if (!CONSTANT_CLASS_P (value))
15268 constant_p = false;
15269 break;
15272 if (constant_p)
15274 init = build_vector_from_ctor (type, elts);
15275 break;
15278 /* FALLTHRU */
15280 default:
15281 return NULL;
15284 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15286 /* If expand_expr returns a MEM, it wasn't immediate. */
15287 gcc_assert (!rtl || !MEM_P (rtl));
15290 return rtl;
15293 /* Generate RTL for the variable DECL to represent its location. */
15295 static rtx
15296 rtl_for_decl_location (tree decl)
15298 rtx rtl;
15300 /* Here we have to decide where we are going to say the parameter "lives"
15301 (as far as the debugger is concerned). We only have a couple of
15302 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15304 DECL_RTL normally indicates where the parameter lives during most of the
15305 activation of the function. If optimization is enabled however, this
15306 could be either NULL or else a pseudo-reg. Both of those cases indicate
15307 that the parameter doesn't really live anywhere (as far as the code
15308 generation parts of GCC are concerned) during most of the function's
15309 activation. That will happen (for example) if the parameter is never
15310 referenced within the function.
15312 We could just generate a location descriptor here for all non-NULL
15313 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15314 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15315 where DECL_RTL is NULL or is a pseudo-reg.
15317 Note however that we can only get away with using DECL_INCOMING_RTL as
15318 a backup substitute for DECL_RTL in certain limited cases. In cases
15319 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15320 we can be sure that the parameter was passed using the same type as it is
15321 declared to have within the function, and that its DECL_INCOMING_RTL
15322 points us to a place where a value of that type is passed.
15324 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15325 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15326 because in these cases DECL_INCOMING_RTL points us to a value of some
15327 type which is *different* from the type of the parameter itself. Thus,
15328 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15329 such cases, the debugger would end up (for example) trying to fetch a
15330 `float' from a place which actually contains the first part of a
15331 `double'. That would lead to really incorrect and confusing
15332 output at debug-time.
15334 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15335 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
15336 are a couple of exceptions however. On little-endian machines we can
15337 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15338 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15339 an integral type that is smaller than TREE_TYPE (decl). These cases arise
15340 when (on a little-endian machine) a non-prototyped function has a
15341 parameter declared to be of type `short' or `char'. In such cases,
15342 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15343 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15344 passed `int' value. If the debugger then uses that address to fetch
15345 a `short' or a `char' (on a little-endian machine) the result will be
15346 the correct data, so we allow for such exceptional cases below.
15348 Note that our goal here is to describe the place where the given formal
15349 parameter lives during most of the function's activation (i.e. between the
15350 end of the prologue and the start of the epilogue). We'll do that as best
15351 as we can. Note however that if the given formal parameter is modified
15352 sometime during the execution of the function, then a stack backtrace (at
15353 debug-time) will show the function as having been called with the *new*
15354 value rather than the value which was originally passed in. This happens
15355 rarely enough that it is not a major problem, but it *is* a problem, and
15356 I'd like to fix it.
15358 A future version of dwarf2out.c may generate two additional attributes for
15359 any given DW_TAG_formal_parameter DIE which will describe the "passed
15360 type" and the "passed location" for the given formal parameter in addition
15361 to the attributes we now generate to indicate the "declared type" and the
15362 "active location" for each parameter. This additional set of attributes
15363 could be used by debuggers for stack backtraces. Separately, note that
15364 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15365 This happens (for example) for inlined-instances of inline function formal
15366 parameters which are never referenced. This really shouldn't be
15367 happening. All PARM_DECL nodes should get valid non-NULL
15368 DECL_INCOMING_RTL values. FIXME. */
15370 /* Use DECL_RTL as the "location" unless we find something better. */
15371 rtl = DECL_RTL_IF_SET (decl);
15373 /* When generating abstract instances, ignore everything except
15374 constants, symbols living in memory, and symbols living in
15375 fixed registers. */
15376 if (! reload_completed)
15378 if (rtl
15379 && (CONSTANT_P (rtl)
15380 || (MEM_P (rtl)
15381 && CONSTANT_P (XEXP (rtl, 0)))
15382 || (REG_P (rtl)
15383 && TREE_CODE (decl) == VAR_DECL
15384 && TREE_STATIC (decl))))
15386 rtl = targetm.delegitimize_address (rtl);
15387 return rtl;
15389 rtl = NULL_RTX;
15391 else if (TREE_CODE (decl) == PARM_DECL)
15393 if (rtl == NULL_RTX
15394 || is_pseudo_reg (rtl)
15395 || (MEM_P (rtl)
15396 && is_pseudo_reg (XEXP (rtl, 0))
15397 && DECL_INCOMING_RTL (decl)
15398 && MEM_P (DECL_INCOMING_RTL (decl))
15399 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15401 tree declared_type = TREE_TYPE (decl);
15402 tree passed_type = DECL_ARG_TYPE (decl);
15403 enum machine_mode dmode = TYPE_MODE (declared_type);
15404 enum machine_mode pmode = TYPE_MODE (passed_type);
15406 /* This decl represents a formal parameter which was optimized out.
15407 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15408 all cases where (rtl == NULL_RTX) just below. */
15409 if (dmode == pmode)
15410 rtl = DECL_INCOMING_RTL (decl);
15411 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15412 && SCALAR_INT_MODE_P (dmode)
15413 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15414 && DECL_INCOMING_RTL (decl))
15416 rtx inc = DECL_INCOMING_RTL (decl);
15417 if (REG_P (inc))
15418 rtl = inc;
15419 else if (MEM_P (inc))
15421 if (BYTES_BIG_ENDIAN)
15422 rtl = adjust_address_nv (inc, dmode,
15423 GET_MODE_SIZE (pmode)
15424 - GET_MODE_SIZE (dmode));
15425 else
15426 rtl = inc;
15431 /* If the parm was passed in registers, but lives on the stack, then
15432 make a big endian correction if the mode of the type of the
15433 parameter is not the same as the mode of the rtl. */
15434 /* ??? This is the same series of checks that are made in dbxout.c before
15435 we reach the big endian correction code there. It isn't clear if all
15436 of these checks are necessary here, but keeping them all is the safe
15437 thing to do. */
15438 else if (MEM_P (rtl)
15439 && XEXP (rtl, 0) != const0_rtx
15440 && ! CONSTANT_P (XEXP (rtl, 0))
15441 /* Not passed in memory. */
15442 && !MEM_P (DECL_INCOMING_RTL (decl))
15443 /* Not passed by invisible reference. */
15444 && (!REG_P (XEXP (rtl, 0))
15445 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15446 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15447 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15448 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15449 #endif
15451 /* Big endian correction check. */
15452 && BYTES_BIG_ENDIAN
15453 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15454 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15455 < UNITS_PER_WORD))
15457 enum machine_mode addr_mode = get_address_mode (rtl);
15458 int offset = (UNITS_PER_WORD
15459 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15461 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15462 plus_constant (addr_mode, XEXP (rtl, 0), offset));
15465 else if (TREE_CODE (decl) == VAR_DECL
15466 && rtl
15467 && MEM_P (rtl)
15468 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
15469 && BYTES_BIG_ENDIAN)
15471 enum machine_mode addr_mode = get_address_mode (rtl);
15472 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
15473 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
15475 /* If a variable is declared "register" yet is smaller than
15476 a register, then if we store the variable to memory, it
15477 looks like we're storing a register-sized value, when in
15478 fact we are not. We need to adjust the offset of the
15479 storage location to reflect the actual value's bytes,
15480 else gdb will not be able to display it. */
15481 if (rsize > dsize)
15482 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15483 plus_constant (addr_mode, XEXP (rtl, 0),
15484 rsize - dsize));
15487 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
15488 and will have been substituted directly into all expressions that use it.
15489 C does not have such a concept, but C++ and other languages do. */
15490 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
15491 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
15493 if (rtl)
15494 rtl = targetm.delegitimize_address (rtl);
15496 /* If we don't look past the constant pool, we risk emitting a
15497 reference to a constant pool entry that isn't referenced from
15498 code, and thus is not emitted. */
15499 if (rtl)
15500 rtl = avoid_constant_pool_reference (rtl);
15502 /* Try harder to get a rtl. If this symbol ends up not being emitted
15503 in the current CU, resolve_addr will remove the expression referencing
15504 it. */
15505 if (rtl == NULL_RTX
15506 && TREE_CODE (decl) == VAR_DECL
15507 && !DECL_EXTERNAL (decl)
15508 && TREE_STATIC (decl)
15509 && DECL_NAME (decl)
15510 && !DECL_HARD_REGISTER (decl)
15511 && DECL_MODE (decl) != VOIDmode)
15513 rtl = make_decl_rtl_for_debug (decl);
15514 if (!MEM_P (rtl)
15515 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
15516 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
15517 rtl = NULL_RTX;
15520 return rtl;
15523 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
15524 returned. If so, the decl for the COMMON block is returned, and the
15525 value is the offset into the common block for the symbol. */
15527 static tree
15528 fortran_common (tree decl, HOST_WIDE_INT *value)
15530 tree val_expr, cvar;
15531 enum machine_mode mode;
15532 HOST_WIDE_INT bitsize, bitpos;
15533 tree offset;
15534 int unsignedp, volatilep = 0;
15536 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
15537 it does not have a value (the offset into the common area), or if it
15538 is thread local (as opposed to global) then it isn't common, and shouldn't
15539 be handled as such. */
15540 if (TREE_CODE (decl) != VAR_DECL
15541 || !TREE_STATIC (decl)
15542 || !DECL_HAS_VALUE_EXPR_P (decl)
15543 || !is_fortran ())
15544 return NULL_TREE;
15546 val_expr = DECL_VALUE_EXPR (decl);
15547 if (TREE_CODE (val_expr) != COMPONENT_REF)
15548 return NULL_TREE;
15550 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
15551 &mode, &unsignedp, &volatilep, true);
15553 if (cvar == NULL_TREE
15554 || TREE_CODE (cvar) != VAR_DECL
15555 || DECL_ARTIFICIAL (cvar)
15556 || !TREE_PUBLIC (cvar))
15557 return NULL_TREE;
15559 *value = 0;
15560 if (offset != NULL)
15562 if (!tree_fits_shwi_p (offset))
15563 return NULL_TREE;
15564 *value = tree_to_shwi (offset);
15566 if (bitpos != 0)
15567 *value += bitpos / BITS_PER_UNIT;
15569 return cvar;
15572 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
15573 data attribute for a variable or a parameter. We generate the
15574 DW_AT_const_value attribute only in those cases where the given variable
15575 or parameter does not have a true "location" either in memory or in a
15576 register. This can happen (for example) when a constant is passed as an
15577 actual argument in a call to an inline function. (It's possible that
15578 these things can crop up in other ways also.) Note that one type of
15579 constant value which can be passed into an inlined function is a constant
15580 pointer. This can happen for example if an actual argument in an inlined
15581 function call evaluates to a compile-time constant address.
15583 CACHE_P is true if it is worth caching the location list for DECL,
15584 so that future calls can reuse it rather than regenerate it from scratch.
15585 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
15586 since we will need to refer to them each time the function is inlined. */
15588 static bool
15589 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
15590 enum dwarf_attribute attr)
15592 rtx rtl;
15593 dw_loc_list_ref list;
15594 var_loc_list *loc_list;
15595 cached_dw_loc_list *cache;
15596 void **slot;
15598 if (TREE_CODE (decl) == ERROR_MARK)
15599 return false;
15601 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
15602 || TREE_CODE (decl) == RESULT_DECL);
15604 /* Try to get some constant RTL for this decl, and use that as the value of
15605 the location. */
15607 rtl = rtl_for_decl_location (decl);
15608 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15609 && add_const_value_attribute (die, rtl))
15610 return true;
15612 /* See if we have single element location list that is equivalent to
15613 a constant value. That way we are better to use add_const_value_attribute
15614 rather than expanding constant value equivalent. */
15615 loc_list = lookup_decl_loc (decl);
15616 if (loc_list
15617 && loc_list->first
15618 && loc_list->first->next == NULL
15619 && NOTE_P (loc_list->first->loc)
15620 && NOTE_VAR_LOCATION (loc_list->first->loc)
15621 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
15623 struct var_loc_node *node;
15625 node = loc_list->first;
15626 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
15627 if (GET_CODE (rtl) == EXPR_LIST)
15628 rtl = XEXP (rtl, 0);
15629 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15630 && add_const_value_attribute (die, rtl))
15631 return true;
15633 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
15634 list several times. See if we've already cached the contents. */
15635 list = NULL;
15636 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
15637 cache_p = false;
15638 if (cache_p)
15640 cache = (cached_dw_loc_list *)
15641 htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
15642 if (cache)
15643 list = cache->loc_list;
15645 if (list == NULL)
15647 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
15648 /* It is usually worth caching this result if the decl is from
15649 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
15650 if (cache_p && list && list->dw_loc_next)
15652 slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
15653 DECL_UID (decl), INSERT);
15654 cache = ggc_alloc_cleared_cached_dw_loc_list ();
15655 cache->decl_id = DECL_UID (decl);
15656 cache->loc_list = list;
15657 *slot = cache;
15660 if (list)
15662 add_AT_location_description (die, attr, list);
15663 return true;
15665 /* None of that worked, so it must not really have a location;
15666 try adding a constant value attribute from the DECL_INITIAL. */
15667 return tree_add_const_value_attribute_for_decl (die, decl);
15670 /* Add VARIABLE and DIE into deferred locations list. */
15672 static void
15673 defer_location (tree variable, dw_die_ref die)
15675 deferred_locations entry;
15676 entry.variable = variable;
15677 entry.die = die;
15678 vec_safe_push (deferred_locations_list, entry);
15681 /* Helper function for tree_add_const_value_attribute. Natively encode
15682 initializer INIT into an array. Return true if successful. */
15684 static bool
15685 native_encode_initializer (tree init, unsigned char *array, int size)
15687 tree type;
15689 if (init == NULL_TREE)
15690 return false;
15692 STRIP_NOPS (init);
15693 switch (TREE_CODE (init))
15695 case STRING_CST:
15696 type = TREE_TYPE (init);
15697 if (TREE_CODE (type) == ARRAY_TYPE)
15699 tree enttype = TREE_TYPE (type);
15700 enum machine_mode mode = TYPE_MODE (enttype);
15702 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
15703 return false;
15704 if (int_size_in_bytes (type) != size)
15705 return false;
15706 if (size > TREE_STRING_LENGTH (init))
15708 memcpy (array, TREE_STRING_POINTER (init),
15709 TREE_STRING_LENGTH (init));
15710 memset (array + TREE_STRING_LENGTH (init),
15711 '\0', size - TREE_STRING_LENGTH (init));
15713 else
15714 memcpy (array, TREE_STRING_POINTER (init), size);
15715 return true;
15717 return false;
15718 case CONSTRUCTOR:
15719 type = TREE_TYPE (init);
15720 if (int_size_in_bytes (type) != size)
15721 return false;
15722 if (TREE_CODE (type) == ARRAY_TYPE)
15724 HOST_WIDE_INT min_index;
15725 unsigned HOST_WIDE_INT cnt;
15726 int curpos = 0, fieldsize;
15727 constructor_elt *ce;
15729 if (TYPE_DOMAIN (type) == NULL_TREE
15730 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
15731 return false;
15733 fieldsize = int_size_in_bytes (TREE_TYPE (type));
15734 if (fieldsize <= 0)
15735 return false;
15737 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
15738 memset (array, '\0', size);
15739 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15741 tree val = ce->value;
15742 tree index = ce->index;
15743 int pos = curpos;
15744 if (index && TREE_CODE (index) == RANGE_EXPR)
15745 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
15746 * fieldsize;
15747 else if (index)
15748 pos = (tree_to_shwi (index) - min_index) * fieldsize;
15750 if (val)
15752 STRIP_NOPS (val);
15753 if (!native_encode_initializer (val, array + pos, fieldsize))
15754 return false;
15756 curpos = pos + fieldsize;
15757 if (index && TREE_CODE (index) == RANGE_EXPR)
15759 int count = tree_to_shwi (TREE_OPERAND (index, 1))
15760 - tree_to_shwi (TREE_OPERAND (index, 0));
15761 while (count-- > 0)
15763 if (val)
15764 memcpy (array + curpos, array + pos, fieldsize);
15765 curpos += fieldsize;
15768 gcc_assert (curpos <= size);
15770 return true;
15772 else if (TREE_CODE (type) == RECORD_TYPE
15773 || TREE_CODE (type) == UNION_TYPE)
15775 tree field = NULL_TREE;
15776 unsigned HOST_WIDE_INT cnt;
15777 constructor_elt *ce;
15779 if (int_size_in_bytes (type) != size)
15780 return false;
15782 if (TREE_CODE (type) == RECORD_TYPE)
15783 field = TYPE_FIELDS (type);
15785 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15787 tree val = ce->value;
15788 int pos, fieldsize;
15790 if (ce->index != 0)
15791 field = ce->index;
15793 if (val)
15794 STRIP_NOPS (val);
15796 if (field == NULL_TREE || DECL_BIT_FIELD (field))
15797 return false;
15799 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
15800 && TYPE_DOMAIN (TREE_TYPE (field))
15801 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
15802 return false;
15803 else if (DECL_SIZE_UNIT (field) == NULL_TREE
15804 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
15805 return false;
15806 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
15807 pos = int_byte_position (field);
15808 gcc_assert (pos + fieldsize <= size);
15809 if (val
15810 && !native_encode_initializer (val, array + pos, fieldsize))
15811 return false;
15813 return true;
15815 return false;
15816 case VIEW_CONVERT_EXPR:
15817 case NON_LVALUE_EXPR:
15818 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
15819 default:
15820 return native_encode_expr (init, array, size) == size;
15824 /* Attach a DW_AT_const_value attribute to DIE. The value of the
15825 attribute is the const value T. */
15827 static bool
15828 tree_add_const_value_attribute (dw_die_ref die, tree t)
15830 tree init;
15831 tree type = TREE_TYPE (t);
15832 rtx rtl;
15834 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
15835 return false;
15837 init = t;
15838 gcc_assert (!DECL_P (init));
15840 rtl = rtl_for_decl_init (init, type);
15841 if (rtl)
15842 return add_const_value_attribute (die, rtl);
15843 /* If the host and target are sane, try harder. */
15844 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
15845 && initializer_constant_valid_p (init, type))
15847 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
15848 if (size > 0 && (int) size == size)
15850 unsigned char *array = (unsigned char *)
15851 ggc_alloc_cleared_atomic (size);
15853 if (native_encode_initializer (init, array, size))
15855 add_AT_vec (die, DW_AT_const_value, size, 1, array);
15856 return true;
15858 ggc_free (array);
15861 return false;
15864 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
15865 attribute is the const value of T, where T is an integral constant
15866 variable with static storage duration
15867 (so it can't be a PARM_DECL or a RESULT_DECL). */
15869 static bool
15870 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
15873 if (!decl
15874 || (TREE_CODE (decl) != VAR_DECL
15875 && TREE_CODE (decl) != CONST_DECL)
15876 || (TREE_CODE (decl) == VAR_DECL
15877 && !TREE_STATIC (decl)))
15878 return false;
15880 if (TREE_READONLY (decl)
15881 && ! TREE_THIS_VOLATILE (decl)
15882 && DECL_INITIAL (decl))
15883 /* OK */;
15884 else
15885 return false;
15887 /* Don't add DW_AT_const_value if abstract origin already has one. */
15888 if (get_AT (var_die, DW_AT_const_value))
15889 return false;
15891 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
15894 /* Convert the CFI instructions for the current function into a
15895 location list. This is used for DW_AT_frame_base when we targeting
15896 a dwarf2 consumer that does not support the dwarf3
15897 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
15898 expressions. */
15900 static dw_loc_list_ref
15901 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
15903 int ix;
15904 dw_fde_ref fde;
15905 dw_loc_list_ref list, *list_tail;
15906 dw_cfi_ref cfi;
15907 dw_cfa_location last_cfa, next_cfa;
15908 const char *start_label, *last_label, *section;
15909 dw_cfa_location remember;
15911 fde = cfun->fde;
15912 gcc_assert (fde != NULL);
15914 section = secname_for_decl (current_function_decl);
15915 list_tail = &list;
15916 list = NULL;
15918 memset (&next_cfa, 0, sizeof (next_cfa));
15919 next_cfa.reg = INVALID_REGNUM;
15920 remember = next_cfa;
15922 start_label = fde->dw_fde_begin;
15924 /* ??? Bald assumption that the CIE opcode list does not contain
15925 advance opcodes. */
15926 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
15927 lookup_cfa_1 (cfi, &next_cfa, &remember);
15929 last_cfa = next_cfa;
15930 last_label = start_label;
15932 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
15934 /* If the first partition contained no CFI adjustments, the
15935 CIE opcodes apply to the whole first partition. */
15936 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15937 fde->dw_fde_begin, fde->dw_fde_end, section);
15938 list_tail =&(*list_tail)->dw_loc_next;
15939 start_label = last_label = fde->dw_fde_second_begin;
15942 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
15944 switch (cfi->dw_cfi_opc)
15946 case DW_CFA_set_loc:
15947 case DW_CFA_advance_loc1:
15948 case DW_CFA_advance_loc2:
15949 case DW_CFA_advance_loc4:
15950 if (!cfa_equal_p (&last_cfa, &next_cfa))
15952 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15953 start_label, last_label, section);
15955 list_tail = &(*list_tail)->dw_loc_next;
15956 last_cfa = next_cfa;
15957 start_label = last_label;
15959 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
15960 break;
15962 case DW_CFA_advance_loc:
15963 /* The encoding is complex enough that we should never emit this. */
15964 gcc_unreachable ();
15966 default:
15967 lookup_cfa_1 (cfi, &next_cfa, &remember);
15968 break;
15970 if (ix + 1 == fde->dw_fde_switch_cfi_index)
15972 if (!cfa_equal_p (&last_cfa, &next_cfa))
15974 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15975 start_label, last_label, section);
15977 list_tail = &(*list_tail)->dw_loc_next;
15978 last_cfa = next_cfa;
15979 start_label = last_label;
15981 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15982 start_label, fde->dw_fde_end, section);
15983 list_tail = &(*list_tail)->dw_loc_next;
15984 start_label = last_label = fde->dw_fde_second_begin;
15988 if (!cfa_equal_p (&last_cfa, &next_cfa))
15990 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15991 start_label, last_label, section);
15992 list_tail = &(*list_tail)->dw_loc_next;
15993 start_label = last_label;
15996 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
15997 start_label,
15998 fde->dw_fde_second_begin
15999 ? fde->dw_fde_second_end : fde->dw_fde_end,
16000 section);
16002 if (list && list->dw_loc_next)
16003 gen_llsym (list);
16005 return list;
16008 /* Compute a displacement from the "steady-state frame pointer" to the
16009 frame base (often the same as the CFA), and store it in
16010 frame_pointer_fb_offset. OFFSET is added to the displacement
16011 before the latter is negated. */
16013 static void
16014 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
16016 rtx reg, elim;
16018 #ifdef FRAME_POINTER_CFA_OFFSET
16019 reg = frame_pointer_rtx;
16020 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
16021 #else
16022 reg = arg_pointer_rtx;
16023 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
16024 #endif
16026 elim = (ira_use_lra_p
16027 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
16028 : eliminate_regs (reg, VOIDmode, NULL_RTX));
16029 if (GET_CODE (elim) == PLUS)
16031 offset += INTVAL (XEXP (elim, 1));
16032 elim = XEXP (elim, 0);
16035 frame_pointer_fb_offset = -offset;
16037 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
16038 in which to eliminate. This is because it's stack pointer isn't
16039 directly accessible as a register within the ISA. To work around
16040 this, assume that while we cannot provide a proper value for
16041 frame_pointer_fb_offset, we won't need one either. */
16042 frame_pointer_fb_offset_valid
16043 = ((SUPPORTS_STACK_ALIGNMENT
16044 && (elim == hard_frame_pointer_rtx
16045 || elim == stack_pointer_rtx))
16046 || elim == (frame_pointer_needed
16047 ? hard_frame_pointer_rtx
16048 : stack_pointer_rtx));
16051 /* Generate a DW_AT_name attribute given some string value to be included as
16052 the value of the attribute. */
16054 static void
16055 add_name_attribute (dw_die_ref die, const char *name_string)
16057 if (name_string != NULL && *name_string != 0)
16059 if (demangle_name_func)
16060 name_string = (*demangle_name_func) (name_string);
16062 add_AT_string (die, DW_AT_name, name_string);
16066 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
16067 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
16068 of TYPE accordingly.
16070 ??? This is a temporary measure until after we're able to generate
16071 regular DWARF for the complex Ada type system. */
16073 static void
16074 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
16075 dw_die_ref context_die)
16077 tree dtype;
16078 dw_die_ref dtype_die;
16080 if (!lang_hooks.types.descriptive_type)
16081 return;
16083 dtype = lang_hooks.types.descriptive_type (type);
16084 if (!dtype)
16085 return;
16087 dtype_die = lookup_type_die (dtype);
16088 if (!dtype_die)
16090 gen_type_die (dtype, context_die);
16091 dtype_die = lookup_type_die (dtype);
16092 gcc_assert (dtype_die);
16095 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
16098 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
16100 static const char *
16101 comp_dir_string (void)
16103 const char *wd;
16104 char *wd1;
16105 static const char *cached_wd = NULL;
16107 if (cached_wd != NULL)
16108 return cached_wd;
16110 wd = get_src_pwd ();
16111 if (wd == NULL)
16112 return NULL;
16114 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
16116 int wdlen;
16118 wdlen = strlen (wd);
16119 wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
16120 strcpy (wd1, wd);
16121 wd1 [wdlen] = DIR_SEPARATOR;
16122 wd1 [wdlen + 1] = 0;
16123 wd = wd1;
16126 cached_wd = remap_debug_filename (wd);
16127 return cached_wd;
16130 /* Generate a DW_AT_comp_dir attribute for DIE. */
16132 static void
16133 add_comp_dir_attribute (dw_die_ref die)
16135 const char * wd = comp_dir_string ();
16136 if (wd != NULL)
16137 add_AT_string (die, DW_AT_comp_dir, wd);
16140 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
16141 default. */
16143 static int
16144 lower_bound_default (void)
16146 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16148 case DW_LANG_C:
16149 case DW_LANG_C89:
16150 case DW_LANG_C99:
16151 case DW_LANG_C_plus_plus:
16152 case DW_LANG_ObjC:
16153 case DW_LANG_ObjC_plus_plus:
16154 case DW_LANG_Java:
16155 return 0;
16156 case DW_LANG_Fortran77:
16157 case DW_LANG_Fortran90:
16158 case DW_LANG_Fortran95:
16159 return 1;
16160 case DW_LANG_UPC:
16161 case DW_LANG_D:
16162 case DW_LANG_Python:
16163 return dwarf_version >= 4 ? 0 : -1;
16164 case DW_LANG_Ada95:
16165 case DW_LANG_Ada83:
16166 case DW_LANG_Cobol74:
16167 case DW_LANG_Cobol85:
16168 case DW_LANG_Pascal83:
16169 case DW_LANG_Modula2:
16170 case DW_LANG_PLI:
16171 return dwarf_version >= 4 ? 1 : -1;
16172 default:
16173 return -1;
16177 /* Given a tree node describing an array bound (either lower or upper) output
16178 a representation for that bound. */
16180 static void
16181 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
16183 switch (TREE_CODE (bound))
16185 case ERROR_MARK:
16186 return;
16188 /* All fixed-bounds are represented by INTEGER_CST nodes. */
16189 case INTEGER_CST:
16191 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
16192 int dflt;
16194 /* Use the default if possible. */
16195 if (bound_attr == DW_AT_lower_bound
16196 && tree_fits_shwi_p (bound)
16197 && (dflt = lower_bound_default ()) != -1
16198 && tree_to_shwi (bound) == dflt)
16201 /* Otherwise represent the bound as an unsigned value with the
16202 precision of its type. The precision and signedness of the
16203 type will be necessary to re-interpret it unambiguously. */
16204 else if (prec < HOST_BITS_PER_WIDE_INT)
16206 unsigned HOST_WIDE_INT mask
16207 = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
16208 add_AT_unsigned (subrange_die, bound_attr,
16209 TREE_INT_CST_LOW (bound) & mask);
16211 else if (prec == HOST_BITS_PER_WIDE_INT
16212 || TREE_INT_CST_HIGH (bound) == 0)
16213 add_AT_unsigned (subrange_die, bound_attr,
16214 TREE_INT_CST_LOW (bound));
16215 else
16216 add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
16217 TREE_INT_CST_LOW (bound));
16219 break;
16221 CASE_CONVERT:
16222 case VIEW_CONVERT_EXPR:
16223 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
16224 break;
16226 case SAVE_EXPR:
16227 break;
16229 case VAR_DECL:
16230 case PARM_DECL:
16231 case RESULT_DECL:
16233 dw_die_ref decl_die = lookup_decl_die (bound);
16235 /* ??? Can this happen, or should the variable have been bound
16236 first? Probably it can, since I imagine that we try to create
16237 the types of parameters in the order in which they exist in
16238 the list, and won't have created a forward reference to a
16239 later parameter. */
16240 if (decl_die != NULL)
16242 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16243 break;
16246 /* FALLTHRU */
16248 default:
16250 /* Otherwise try to create a stack operation procedure to
16251 evaluate the value of the array bound. */
16253 dw_die_ref ctx, decl_die;
16254 dw_loc_list_ref list;
16256 list = loc_list_from_tree (bound, 2);
16257 if (list == NULL || single_element_loc_list_p (list))
16259 /* If DW_AT_*bound is not a reference nor constant, it is
16260 a DWARF expression rather than location description.
16261 For that loc_list_from_tree (bound, 0) is needed.
16262 If that fails to give a single element list,
16263 fall back to outputting this as a reference anyway. */
16264 dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
16265 if (list2 && single_element_loc_list_p (list2))
16267 add_AT_loc (subrange_die, bound_attr, list2->expr);
16268 break;
16271 if (list == NULL)
16272 break;
16274 if (current_function_decl == 0)
16275 ctx = comp_unit_die ();
16276 else
16277 ctx = lookup_decl_die (current_function_decl);
16279 decl_die = new_die (DW_TAG_variable, ctx, bound);
16280 add_AT_flag (decl_die, DW_AT_artificial, 1);
16281 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
16282 add_AT_location_description (decl_die, DW_AT_location, list);
16283 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16284 break;
16289 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16290 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16291 Note that the block of subscript information for an array type also
16292 includes information about the element type of the given array type. */
16294 static void
16295 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16297 unsigned dimension_number;
16298 tree lower, upper;
16299 dw_die_ref subrange_die;
16301 for (dimension_number = 0;
16302 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16303 type = TREE_TYPE (type), dimension_number++)
16305 tree domain = TYPE_DOMAIN (type);
16307 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16308 break;
16310 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16311 and (in GNU C only) variable bounds. Handle all three forms
16312 here. */
16313 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16314 if (domain)
16316 /* We have an array type with specified bounds. */
16317 lower = TYPE_MIN_VALUE (domain);
16318 upper = TYPE_MAX_VALUE (domain);
16320 /* Define the index type. */
16321 if (TREE_TYPE (domain))
16323 /* ??? This is probably an Ada unnamed subrange type. Ignore the
16324 TREE_TYPE field. We can't emit debug info for this
16325 because it is an unnamed integral type. */
16326 if (TREE_CODE (domain) == INTEGER_TYPE
16327 && TYPE_NAME (domain) == NULL_TREE
16328 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16329 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16331 else
16332 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
16333 type_die);
16336 /* ??? If upper is NULL, the array has unspecified length,
16337 but it does have a lower bound. This happens with Fortran
16338 dimension arr(N:*)
16339 Since the debugger is definitely going to need to know N
16340 to produce useful results, go ahead and output the lower
16341 bound solo, and hope the debugger can cope. */
16343 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
16344 if (upper)
16345 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
16348 /* Otherwise we have an array type with an unspecified length. The
16349 DWARF-2 spec does not say how to handle this; let's just leave out the
16350 bounds. */
16354 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
16356 static void
16357 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16359 dw_die_ref decl_die;
16360 HOST_WIDE_INT size;
16362 switch (TREE_CODE (tree_node))
16364 case ERROR_MARK:
16365 size = 0;
16366 break;
16367 case ENUMERAL_TYPE:
16368 case RECORD_TYPE:
16369 case UNION_TYPE:
16370 case QUAL_UNION_TYPE:
16371 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
16372 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
16374 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
16375 return;
16377 size = int_size_in_bytes (tree_node);
16378 break;
16379 case FIELD_DECL:
16380 /* For a data member of a struct or union, the DW_AT_byte_size is
16381 generally given as the number of bytes normally allocated for an
16382 object of the *declared* type of the member itself. This is true
16383 even for bit-fields. */
16384 size = int_size_in_bytes (field_type (tree_node));
16385 break;
16386 default:
16387 gcc_unreachable ();
16390 /* Note that `size' might be -1 when we get to this point. If it is, that
16391 indicates that the byte size of the entity in question is variable. We
16392 have no good way of expressing this fact in Dwarf at the present time,
16393 when location description was not used by the caller code instead. */
16394 if (size >= 0)
16395 add_AT_unsigned (die, DW_AT_byte_size, size);
16398 /* For a FIELD_DECL node which represents a bit-field, output an attribute
16399 which specifies the distance in bits from the highest order bit of the
16400 "containing object" for the bit-field to the highest order bit of the
16401 bit-field itself.
16403 For any given bit-field, the "containing object" is a hypothetical object
16404 (of some integral or enum type) within which the given bit-field lives. The
16405 type of this hypothetical "containing object" is always the same as the
16406 declared type of the individual bit-field itself. The determination of the
16407 exact location of the "containing object" for a bit-field is rather
16408 complicated. It's handled by the `field_byte_offset' function (above).
16410 Note that it is the size (in bytes) of the hypothetical "containing object"
16411 which will be given in the DW_AT_byte_size attribute for this bit-field.
16412 (See `byte_size_attribute' above). */
16414 static inline void
16415 add_bit_offset_attribute (dw_die_ref die, tree decl)
16417 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
16418 tree type = DECL_BIT_FIELD_TYPE (decl);
16419 HOST_WIDE_INT bitpos_int;
16420 HOST_WIDE_INT highest_order_object_bit_offset;
16421 HOST_WIDE_INT highest_order_field_bit_offset;
16422 HOST_WIDE_INT bit_offset;
16424 /* Must be a field and a bit field. */
16425 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
16427 /* We can't yet handle bit-fields whose offsets are variable, so if we
16428 encounter such things, just return without generating any attribute
16429 whatsoever. Likewise for variable or too large size. */
16430 if (! tree_fits_shwi_p (bit_position (decl))
16431 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
16432 return;
16434 bitpos_int = int_bit_position (decl);
16436 /* Note that the bit offset is always the distance (in bits) from the
16437 highest-order bit of the "containing object" to the highest-order bit of
16438 the bit-field itself. Since the "high-order end" of any object or field
16439 is different on big-endian and little-endian machines, the computation
16440 below must take account of these differences. */
16441 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
16442 highest_order_field_bit_offset = bitpos_int;
16444 if (! BYTES_BIG_ENDIAN)
16446 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
16447 highest_order_object_bit_offset += simple_type_size_in_bits (type);
16450 bit_offset
16451 = (! BYTES_BIG_ENDIAN
16452 ? highest_order_object_bit_offset - highest_order_field_bit_offset
16453 : highest_order_field_bit_offset - highest_order_object_bit_offset);
16455 if (bit_offset < 0)
16456 add_AT_int (die, DW_AT_bit_offset, bit_offset);
16457 else
16458 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
16461 /* For a FIELD_DECL node which represents a bit field, output an attribute
16462 which specifies the length in bits of the given field. */
16464 static inline void
16465 add_bit_size_attribute (dw_die_ref die, tree decl)
16467 /* Must be a field and a bit field. */
16468 gcc_assert (TREE_CODE (decl) == FIELD_DECL
16469 && DECL_BIT_FIELD_TYPE (decl));
16471 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
16472 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
16475 /* If the compiled language is ANSI C, then add a 'prototyped'
16476 attribute, if arg types are given for the parameters of a function. */
16478 static inline void
16479 add_prototyped_attribute (dw_die_ref die, tree func_type)
16481 if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
16482 && prototype_p (func_type))
16483 add_AT_flag (die, DW_AT_prototyped, 1);
16486 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
16487 by looking in either the type declaration or object declaration
16488 equate table. */
16490 static inline dw_die_ref
16491 add_abstract_origin_attribute (dw_die_ref die, tree origin)
16493 dw_die_ref origin_die = NULL;
16495 if (TREE_CODE (origin) != FUNCTION_DECL)
16497 /* We may have gotten separated from the block for the inlined
16498 function, if we're in an exception handler or some such; make
16499 sure that the abstract function has been written out.
16501 Doing this for nested functions is wrong, however; functions are
16502 distinct units, and our context might not even be inline. */
16503 tree fn = origin;
16505 if (TYPE_P (fn))
16506 fn = TYPE_STUB_DECL (fn);
16508 fn = decl_function_context (fn);
16509 if (fn)
16510 dwarf2out_abstract_function (fn);
16513 if (DECL_P (origin))
16514 origin_die = lookup_decl_die (origin);
16515 else if (TYPE_P (origin))
16516 origin_die = lookup_type_die (origin);
16518 /* XXX: Functions that are never lowered don't always have correct block
16519 trees (in the case of java, they simply have no block tree, in some other
16520 languages). For these functions, there is nothing we can really do to
16521 output correct debug info for inlined functions in all cases. Rather
16522 than die, we'll just produce deficient debug info now, in that we will
16523 have variables without a proper abstract origin. In the future, when all
16524 functions are lowered, we should re-add a gcc_assert (origin_die)
16525 here. */
16527 if (origin_die)
16528 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
16529 return origin_die;
16532 /* We do not currently support the pure_virtual attribute. */
16534 static inline void
16535 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
16537 if (DECL_VINDEX (func_decl))
16539 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
16541 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
16542 add_AT_loc (die, DW_AT_vtable_elem_location,
16543 new_loc_descr (DW_OP_constu,
16544 tree_to_shwi (DECL_VINDEX (func_decl)),
16545 0));
16547 /* GNU extension: Record what type this method came from originally. */
16548 if (debug_info_level > DINFO_LEVEL_TERSE
16549 && DECL_CONTEXT (func_decl))
16550 add_AT_die_ref (die, DW_AT_containing_type,
16551 lookup_type_die (DECL_CONTEXT (func_decl)));
16555 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
16556 given decl. This used to be a vendor extension until after DWARF 4
16557 standardized it. */
16559 static void
16560 add_linkage_attr (dw_die_ref die, tree decl)
16562 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
16564 /* Mimic what assemble_name_raw does with a leading '*'. */
16565 if (name[0] == '*')
16566 name = &name[1];
16568 if (dwarf_version >= 4)
16569 add_AT_string (die, DW_AT_linkage_name, name);
16570 else
16571 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
16574 /* Add source coordinate attributes for the given decl. */
16576 static void
16577 add_src_coords_attributes (dw_die_ref die, tree decl)
16579 expanded_location s;
16581 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
16582 return;
16583 s = expand_location (DECL_SOURCE_LOCATION (decl));
16584 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
16585 add_AT_unsigned (die, DW_AT_decl_line, s.line);
16588 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
16590 static void
16591 add_linkage_name (dw_die_ref die, tree decl)
16593 if (debug_info_level > DINFO_LEVEL_TERSE
16594 && (TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
16595 && TREE_PUBLIC (decl)
16596 && !DECL_ABSTRACT (decl)
16597 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
16598 && die->die_tag != DW_TAG_member)
16600 /* Defer until we have an assembler name set. */
16601 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
16603 limbo_die_node *asm_name;
16605 asm_name = ggc_alloc_cleared_limbo_die_node ();
16606 asm_name->die = die;
16607 asm_name->created_for = decl;
16608 asm_name->next = deferred_asm_name;
16609 deferred_asm_name = asm_name;
16611 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
16612 add_linkage_attr (die, decl);
16616 /* Add a DW_AT_name attribute and source coordinate attribute for the
16617 given decl, but only if it actually has a name. */
16619 static void
16620 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
16622 tree decl_name;
16624 decl_name = DECL_NAME (decl);
16625 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
16627 const char *name = dwarf2_name (decl, 0);
16628 if (name)
16629 add_name_attribute (die, name);
16630 if (! DECL_ARTIFICIAL (decl))
16631 add_src_coords_attributes (die, decl);
16633 add_linkage_name (die, decl);
16636 #ifdef VMS_DEBUGGING_INFO
16637 /* Get the function's name, as described by its RTL. This may be different
16638 from the DECL_NAME name used in the source file. */
16639 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
16641 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
16642 XEXP (DECL_RTL (decl), 0), false);
16643 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
16645 #endif /* VMS_DEBUGGING_INFO */
16648 #ifdef VMS_DEBUGGING_INFO
16649 /* Output the debug main pointer die for VMS */
16651 void
16652 dwarf2out_vms_debug_main_pointer (void)
16654 char label[MAX_ARTIFICIAL_LABEL_BYTES];
16655 dw_die_ref die;
16657 /* Allocate the VMS debug main subprogram die. */
16658 die = ggc_alloc_cleared_die_node ();
16659 die->die_tag = DW_TAG_subprogram;
16660 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
16661 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
16662 current_function_funcdef_no);
16663 add_AT_lbl_id (die, DW_AT_entry_pc, label);
16665 /* Make it the first child of comp_unit_die (). */
16666 die->die_parent = comp_unit_die ();
16667 if (comp_unit_die ()->die_child)
16669 die->die_sib = comp_unit_die ()->die_child->die_sib;
16670 comp_unit_die ()->die_child->die_sib = die;
16672 else
16674 die->die_sib = die;
16675 comp_unit_die ()->die_child = die;
16678 #endif /* VMS_DEBUGGING_INFO */
16680 /* Push a new declaration scope. */
16682 static void
16683 push_decl_scope (tree scope)
16685 vec_safe_push (decl_scope_table, scope);
16688 /* Pop a declaration scope. */
16690 static inline void
16691 pop_decl_scope (void)
16693 decl_scope_table->pop ();
16696 /* walk_tree helper function for uses_local_type, below. */
16698 static tree
16699 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
16701 if (!TYPE_P (*tp))
16702 *walk_subtrees = 0;
16703 else
16705 tree name = TYPE_NAME (*tp);
16706 if (name && DECL_P (name) && decl_function_context (name))
16707 return *tp;
16709 return NULL_TREE;
16712 /* If TYPE involves a function-local type (including a local typedef to a
16713 non-local type), returns that type; otherwise returns NULL_TREE. */
16715 static tree
16716 uses_local_type (tree type)
16718 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
16719 return used;
16722 /* Return the DIE for the scope that immediately contains this type.
16723 Non-named types that do not involve a function-local type get global
16724 scope. Named types nested in namespaces or other types get their
16725 containing scope. All other types (i.e. function-local named types) get
16726 the current active scope. */
16728 static dw_die_ref
16729 scope_die_for (tree t, dw_die_ref context_die)
16731 dw_die_ref scope_die = NULL;
16732 tree containing_scope;
16734 /* Non-types always go in the current scope. */
16735 gcc_assert (TYPE_P (t));
16737 /* Use the scope of the typedef, rather than the scope of the type
16738 it refers to. */
16739 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
16740 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
16741 else
16742 containing_scope = TYPE_CONTEXT (t);
16744 /* Use the containing namespace if there is one. */
16745 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
16747 if (context_die == lookup_decl_die (containing_scope))
16748 /* OK */;
16749 else if (debug_info_level > DINFO_LEVEL_TERSE)
16750 context_die = get_context_die (containing_scope);
16751 else
16752 containing_scope = NULL_TREE;
16755 /* Ignore function type "scopes" from the C frontend. They mean that
16756 a tagged type is local to a parmlist of a function declarator, but
16757 that isn't useful to DWARF. */
16758 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
16759 containing_scope = NULL_TREE;
16761 if (SCOPE_FILE_SCOPE_P (containing_scope))
16763 /* If T uses a local type keep it local as well, to avoid references
16764 to function-local DIEs from outside the function. */
16765 if (current_function_decl && uses_local_type (t))
16766 scope_die = context_die;
16767 else
16768 scope_die = comp_unit_die ();
16770 else if (TYPE_P (containing_scope))
16772 /* For types, we can just look up the appropriate DIE. */
16773 if (debug_info_level > DINFO_LEVEL_TERSE)
16774 scope_die = get_context_die (containing_scope);
16775 else
16777 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
16778 if (scope_die == NULL)
16779 scope_die = comp_unit_die ();
16782 else
16783 scope_die = context_die;
16785 return scope_die;
16788 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
16790 static inline int
16791 local_scope_p (dw_die_ref context_die)
16793 for (; context_die; context_die = context_die->die_parent)
16794 if (context_die->die_tag == DW_TAG_inlined_subroutine
16795 || context_die->die_tag == DW_TAG_subprogram)
16796 return 1;
16798 return 0;
16801 /* Returns nonzero if CONTEXT_DIE is a class. */
16803 static inline int
16804 class_scope_p (dw_die_ref context_die)
16806 return (context_die
16807 && (context_die->die_tag == DW_TAG_structure_type
16808 || context_die->die_tag == DW_TAG_class_type
16809 || context_die->die_tag == DW_TAG_interface_type
16810 || context_die->die_tag == DW_TAG_union_type));
16813 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
16814 whether or not to treat a DIE in this context as a declaration. */
16816 static inline int
16817 class_or_namespace_scope_p (dw_die_ref context_die)
16819 return (class_scope_p (context_die)
16820 || (context_die && context_die->die_tag == DW_TAG_namespace));
16823 /* Many forms of DIEs require a "type description" attribute. This
16824 routine locates the proper "type descriptor" die for the type given
16825 by 'type', and adds a DW_AT_type attribute below the given die. */
16827 static void
16828 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
16829 int decl_volatile, dw_die_ref context_die)
16831 enum tree_code code = TREE_CODE (type);
16832 dw_die_ref type_die = NULL;
16834 /* ??? If this type is an unnamed subrange type of an integral, floating-point
16835 or fixed-point type, use the inner type. This is because we have no
16836 support for unnamed types in base_type_die. This can happen if this is
16837 an Ada subrange type. Correct solution is emit a subrange type die. */
16838 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
16839 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
16840 type = TREE_TYPE (type), code = TREE_CODE (type);
16842 if (code == ERROR_MARK
16843 /* Handle a special case. For functions whose return type is void, we
16844 generate *no* type attribute. (Note that no object may have type
16845 `void', so this only applies to function return types). */
16846 || code == VOID_TYPE)
16847 return;
16849 type_die = modified_type_die (type,
16850 decl_const || TYPE_READONLY (type),
16851 decl_volatile || TYPE_VOLATILE (type),
16852 context_die);
16854 if (type_die != NULL)
16855 add_AT_die_ref (object_die, DW_AT_type, type_die);
16858 /* Given an object die, add the calling convention attribute for the
16859 function call type. */
16860 static void
16861 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
16863 enum dwarf_calling_convention value = DW_CC_normal;
16865 value = ((enum dwarf_calling_convention)
16866 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
16868 if (is_fortran ()
16869 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
16871 /* DWARF 2 doesn't provide a way to identify a program's source-level
16872 entry point. DW_AT_calling_convention attributes are only meant
16873 to describe functions' calling conventions. However, lacking a
16874 better way to signal the Fortran main program, we used this for
16875 a long time, following existing custom. Now, DWARF 4 has
16876 DW_AT_main_subprogram, which we add below, but some tools still
16877 rely on the old way, which we thus keep. */
16878 value = DW_CC_program;
16880 if (dwarf_version >= 4 || !dwarf_strict)
16881 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
16884 /* Only add the attribute if the backend requests it, and
16885 is not DW_CC_normal. */
16886 if (value && (value != DW_CC_normal))
16887 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
16890 /* Given a tree pointer to a struct, class, union, or enum type node, return
16891 a pointer to the (string) tag name for the given type, or zero if the type
16892 was declared without a tag. */
16894 static const char *
16895 type_tag (const_tree type)
16897 const char *name = 0;
16899 if (TYPE_NAME (type) != 0)
16901 tree t = 0;
16903 /* Find the IDENTIFIER_NODE for the type name. */
16904 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
16905 && !TYPE_NAMELESS (type))
16906 t = TYPE_NAME (type);
16908 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
16909 a TYPE_DECL node, regardless of whether or not a `typedef' was
16910 involved. */
16911 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
16912 && ! DECL_IGNORED_P (TYPE_NAME (type)))
16914 /* We want to be extra verbose. Don't call dwarf_name if
16915 DECL_NAME isn't set. The default hook for decl_printable_name
16916 doesn't like that, and in this context it's correct to return
16917 0, instead of "<anonymous>" or the like. */
16918 if (DECL_NAME (TYPE_NAME (type))
16919 && !DECL_NAMELESS (TYPE_NAME (type)))
16920 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
16923 /* Now get the name as a string, or invent one. */
16924 if (!name && t != 0)
16925 name = IDENTIFIER_POINTER (t);
16928 return (name == 0 || *name == '\0') ? 0 : name;
16931 /* Return the type associated with a data member, make a special check
16932 for bit field types. */
16934 static inline tree
16935 member_declared_type (const_tree member)
16937 return (DECL_BIT_FIELD_TYPE (member)
16938 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
16941 /* Get the decl's label, as described by its RTL. This may be different
16942 from the DECL_NAME name used in the source file. */
16944 #if 0
16945 static const char *
16946 decl_start_label (tree decl)
16948 rtx x;
16949 const char *fnname;
16951 x = DECL_RTL (decl);
16952 gcc_assert (MEM_P (x));
16954 x = XEXP (x, 0);
16955 gcc_assert (GET_CODE (x) == SYMBOL_REF);
16957 fnname = XSTR (x, 0);
16958 return fnname;
16960 #endif
16962 /* These routines generate the internal representation of the DIE's for
16963 the compilation unit. Debugging information is collected by walking
16964 the declaration trees passed in from dwarf2out_decl(). */
16966 static void
16967 gen_array_type_die (tree type, dw_die_ref context_die)
16969 dw_die_ref scope_die = scope_die_for (type, context_die);
16970 dw_die_ref array_die;
16972 /* GNU compilers represent multidimensional array types as sequences of one
16973 dimensional array types whose element types are themselves array types.
16974 We sometimes squish that down to a single array_type DIE with multiple
16975 subscripts in the Dwarf debugging info. The draft Dwarf specification
16976 say that we are allowed to do this kind of compression in C, because
16977 there is no difference between an array of arrays and a multidimensional
16978 array. We don't do this for Ada to remain as close as possible to the
16979 actual representation, which is especially important against the language
16980 flexibilty wrt arrays of variable size. */
16982 bool collapse_nested_arrays = !is_ada ();
16983 tree element_type;
16985 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
16986 DW_TAG_string_type doesn't have DW_AT_type attribute). */
16987 if (TYPE_STRING_FLAG (type)
16988 && TREE_CODE (type) == ARRAY_TYPE
16989 && is_fortran ()
16990 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
16992 HOST_WIDE_INT size;
16994 array_die = new_die (DW_TAG_string_type, scope_die, type);
16995 add_name_attribute (array_die, type_tag (type));
16996 equate_type_number_to_die (type, array_die);
16997 size = int_size_in_bytes (type);
16998 if (size >= 0)
16999 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17000 else if (TYPE_DOMAIN (type) != NULL_TREE
17001 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
17002 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
17004 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
17005 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
17007 size = int_size_in_bytes (TREE_TYPE (szdecl));
17008 if (loc && size > 0)
17010 add_AT_location_description (array_die, DW_AT_string_length, loc);
17011 if (size != DWARF2_ADDR_SIZE)
17012 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17015 return;
17018 array_die = new_die (DW_TAG_array_type, scope_die, type);
17019 add_name_attribute (array_die, type_tag (type));
17020 equate_type_number_to_die (type, array_die);
17022 if (TREE_CODE (type) == VECTOR_TYPE)
17023 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
17025 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17026 if (is_fortran ()
17027 && TREE_CODE (type) == ARRAY_TYPE
17028 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17029 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17030 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17032 #if 0
17033 /* We default the array ordering. SDB will probably do
17034 the right things even if DW_AT_ordering is not present. It's not even
17035 an issue until we start to get into multidimensional arrays anyway. If
17036 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17037 then we'll have to put the DW_AT_ordering attribute back in. (But if
17038 and when we find out that we need to put these in, we will only do so
17039 for multidimensional arrays. */
17040 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17041 #endif
17043 if (TREE_CODE (type) == VECTOR_TYPE)
17045 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
17046 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
17047 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
17048 add_bound_info (subrange_die, DW_AT_upper_bound,
17049 size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
17051 else
17052 add_subscript_info (array_die, type, collapse_nested_arrays);
17054 /* Add representation of the type of the elements of this array type and
17055 emit the corresponding DIE if we haven't done it already. */
17056 element_type = TREE_TYPE (type);
17057 if (collapse_nested_arrays)
17058 while (TREE_CODE (element_type) == ARRAY_TYPE)
17060 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17061 break;
17062 element_type = TREE_TYPE (element_type);
17065 add_type_attribute (array_die, element_type, 0, 0, context_die);
17067 add_gnat_descriptive_type_attribute (array_die, type, context_die);
17068 if (TYPE_ARTIFICIAL (type))
17069 add_AT_flag (array_die, DW_AT_artificial, 1);
17071 if (get_AT (array_die, DW_AT_name))
17072 add_pubtype (type, array_die);
17075 static dw_loc_descr_ref
17076 descr_info_loc (tree val, tree base_decl)
17078 HOST_WIDE_INT size;
17079 dw_loc_descr_ref loc, loc2;
17080 enum dwarf_location_atom op;
17082 if (val == base_decl)
17083 return new_loc_descr (DW_OP_push_object_address, 0, 0);
17085 switch (TREE_CODE (val))
17087 CASE_CONVERT:
17088 return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17089 case VAR_DECL:
17090 return loc_descriptor_from_tree (val, 0);
17091 case INTEGER_CST:
17092 if (tree_fits_shwi_p (val))
17093 return int_loc_descriptor (tree_to_shwi (val));
17094 break;
17095 case INDIRECT_REF:
17096 size = int_size_in_bytes (TREE_TYPE (val));
17097 if (size < 0)
17098 break;
17099 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17100 if (!loc)
17101 break;
17102 if (size == DWARF2_ADDR_SIZE)
17103 add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
17104 else
17105 add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
17106 return loc;
17107 case POINTER_PLUS_EXPR:
17108 case PLUS_EXPR:
17109 if (tree_fits_uhwi_p (TREE_OPERAND (val, 1))
17110 && tree_to_uhwi (TREE_OPERAND (val, 1)) < 16384)
17112 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17113 if (!loc)
17114 break;
17115 loc_descr_plus_const (&loc, tree_to_shwi (TREE_OPERAND (val, 1)));
17117 else
17119 op = DW_OP_plus;
17120 do_binop:
17121 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17122 if (!loc)
17123 break;
17124 loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
17125 if (!loc2)
17126 break;
17127 add_loc_descr (&loc, loc2);
17128 add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
17130 return loc;
17131 case MINUS_EXPR:
17132 op = DW_OP_minus;
17133 goto do_binop;
17134 case MULT_EXPR:
17135 op = DW_OP_mul;
17136 goto do_binop;
17137 case EQ_EXPR:
17138 op = DW_OP_eq;
17139 goto do_binop;
17140 case NE_EXPR:
17141 op = DW_OP_ne;
17142 goto do_binop;
17143 default:
17144 break;
17146 return NULL;
17149 static void
17150 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
17151 tree val, tree base_decl)
17153 dw_loc_descr_ref loc;
17155 if (tree_fits_shwi_p (val))
17157 add_AT_unsigned (die, attr, tree_to_shwi (val));
17158 return;
17161 loc = descr_info_loc (val, base_decl);
17162 if (!loc)
17163 return;
17165 add_AT_loc (die, attr, loc);
17168 /* This routine generates DIE for array with hidden descriptor, details
17169 are filled into *info by a langhook. */
17171 static void
17172 gen_descr_array_type_die (tree type, struct array_descr_info *info,
17173 dw_die_ref context_die)
17175 dw_die_ref scope_die = scope_die_for (type, context_die);
17176 dw_die_ref array_die;
17177 int dim;
17179 array_die = new_die (DW_TAG_array_type, scope_die, type);
17180 add_name_attribute (array_die, type_tag (type));
17181 equate_type_number_to_die (type, array_die);
17183 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17184 if (is_fortran ()
17185 && info->ndimensions >= 2)
17186 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17188 if (info->data_location)
17189 add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
17190 info->base_decl);
17191 if (info->associated)
17192 add_descr_info_field (array_die, DW_AT_associated, info->associated,
17193 info->base_decl);
17194 if (info->allocated)
17195 add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
17196 info->base_decl);
17198 for (dim = 0; dim < info->ndimensions; dim++)
17200 dw_die_ref subrange_die
17201 = new_die (DW_TAG_subrange_type, array_die, NULL);
17203 if (info->dimen[dim].lower_bound)
17205 /* If it is the default value, omit it. */
17206 int dflt;
17208 if (tree_fits_shwi_p (info->dimen[dim].lower_bound)
17209 && (dflt = lower_bound_default ()) != -1
17210 && tree_to_shwi (info->dimen[dim].lower_bound) == dflt)
17212 else
17213 add_descr_info_field (subrange_die, DW_AT_lower_bound,
17214 info->dimen[dim].lower_bound,
17215 info->base_decl);
17217 if (info->dimen[dim].upper_bound)
17218 add_descr_info_field (subrange_die, DW_AT_upper_bound,
17219 info->dimen[dim].upper_bound,
17220 info->base_decl);
17221 if (info->dimen[dim].stride)
17222 add_descr_info_field (subrange_die, DW_AT_byte_stride,
17223 info->dimen[dim].stride,
17224 info->base_decl);
17227 gen_type_die (info->element_type, context_die);
17228 add_type_attribute (array_die, info->element_type, 0, 0, context_die);
17230 if (get_AT (array_die, DW_AT_name))
17231 add_pubtype (type, array_die);
17234 #if 0
17235 static void
17236 gen_entry_point_die (tree decl, dw_die_ref context_die)
17238 tree origin = decl_ultimate_origin (decl);
17239 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
17241 if (origin != NULL)
17242 add_abstract_origin_attribute (decl_die, origin);
17243 else
17245 add_name_and_src_coords_attributes (decl_die, decl);
17246 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17247 0, 0, context_die);
17250 if (DECL_ABSTRACT (decl))
17251 equate_decl_number_to_die (decl, decl_die);
17252 else
17253 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17255 #endif
17257 /* Walk through the list of incomplete types again, trying once more to
17258 emit full debugging info for them. */
17260 static void
17261 retry_incomplete_types (void)
17263 int i;
17265 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
17266 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
17267 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
17270 /* Determine what tag to use for a record type. */
17272 static enum dwarf_tag
17273 record_type_tag (tree type)
17275 if (! lang_hooks.types.classify_record)
17276 return DW_TAG_structure_type;
17278 switch (lang_hooks.types.classify_record (type))
17280 case RECORD_IS_STRUCT:
17281 return DW_TAG_structure_type;
17283 case RECORD_IS_CLASS:
17284 return DW_TAG_class_type;
17286 case RECORD_IS_INTERFACE:
17287 if (dwarf_version >= 3 || !dwarf_strict)
17288 return DW_TAG_interface_type;
17289 return DW_TAG_structure_type;
17291 default:
17292 gcc_unreachable ();
17296 /* Generate a DIE to represent an enumeration type. Note that these DIEs
17297 include all of the information about the enumeration values also. Each
17298 enumerated type name/value is listed as a child of the enumerated type
17299 DIE. */
17301 static dw_die_ref
17302 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17304 dw_die_ref type_die = lookup_type_die (type);
17306 if (type_die == NULL)
17308 type_die = new_die (DW_TAG_enumeration_type,
17309 scope_die_for (type, context_die), type);
17310 equate_type_number_to_die (type, type_die);
17311 add_name_attribute (type_die, type_tag (type));
17312 if (dwarf_version >= 4 || !dwarf_strict)
17314 if (ENUM_IS_SCOPED (type))
17315 add_AT_flag (type_die, DW_AT_enum_class, 1);
17316 if (ENUM_IS_OPAQUE (type))
17317 add_AT_flag (type_die, DW_AT_declaration, 1);
17320 else if (! TYPE_SIZE (type))
17321 return type_die;
17322 else
17323 remove_AT (type_die, DW_AT_declaration);
17325 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
17326 given enum type is incomplete, do not generate the DW_AT_byte_size
17327 attribute or the DW_AT_element_list attribute. */
17328 if (TYPE_SIZE (type))
17330 tree link;
17332 TREE_ASM_WRITTEN (type) = 1;
17333 add_byte_size_attribute (type_die, type);
17334 if (TYPE_STUB_DECL (type) != NULL_TREE)
17336 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17337 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17340 /* If the first reference to this type was as the return type of an
17341 inline function, then it may not have a parent. Fix this now. */
17342 if (type_die->die_parent == NULL)
17343 add_child_die (scope_die_for (type, context_die), type_die);
17345 for (link = TYPE_VALUES (type);
17346 link != NULL; link = TREE_CHAIN (link))
17348 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17349 tree value = TREE_VALUE (link);
17351 add_name_attribute (enum_die,
17352 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17354 if (TREE_CODE (value) == CONST_DECL)
17355 value = DECL_INITIAL (value);
17357 if (simple_type_size_in_bits (TREE_TYPE (value))
17358 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
17359 /* DWARF2 does not provide a way of indicating whether or
17360 not enumeration constants are signed or unsigned. GDB
17361 always assumes the values are signed, so we output all
17362 values as if they were signed. That means that
17363 enumeration constants with very large unsigned values
17364 will appear to have negative values in the debugger.
17366 TODO: the above comment is wrong, DWARF2 does provide
17367 DW_FORM_sdata/DW_FORM_udata to represent signed/unsigned data.
17368 This should be re-worked to use correct signed/unsigned
17369 int/double tags for all cases, instead of always treating as
17370 signed. */
17371 add_AT_int (enum_die, DW_AT_const_value, TREE_INT_CST_LOW (value));
17372 else
17373 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
17374 that here. */
17375 add_AT_double (enum_die, DW_AT_const_value,
17376 TREE_INT_CST_HIGH (value), TREE_INT_CST_LOW (value));
17379 add_gnat_descriptive_type_attribute (type_die, type, context_die);
17380 if (TYPE_ARTIFICIAL (type))
17381 add_AT_flag (type_die, DW_AT_artificial, 1);
17383 else
17384 add_AT_flag (type_die, DW_AT_declaration, 1);
17386 add_pubtype (type, type_die);
17388 return type_die;
17391 /* Generate a DIE to represent either a real live formal parameter decl or to
17392 represent just the type of some formal parameter position in some function
17393 type.
17395 Note that this routine is a bit unusual because its argument may be a
17396 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17397 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17398 node. If it's the former then this function is being called to output a
17399 DIE to represent a formal parameter object (or some inlining thereof). If
17400 it's the latter, then this function is only being called to output a
17401 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17402 argument type of some subprogram type.
17403 If EMIT_NAME_P is true, name and source coordinate attributes
17404 are emitted. */
17406 static dw_die_ref
17407 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17408 dw_die_ref context_die)
17410 tree node_or_origin = node ? node : origin;
17411 tree ultimate_origin;
17412 dw_die_ref parm_die
17413 = new_die (DW_TAG_formal_parameter, context_die, node);
17415 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
17417 case tcc_declaration:
17418 ultimate_origin = decl_ultimate_origin (node_or_origin);
17419 if (node || ultimate_origin)
17420 origin = ultimate_origin;
17421 if (origin != NULL)
17422 add_abstract_origin_attribute (parm_die, origin);
17423 else if (emit_name_p)
17424 add_name_and_src_coords_attributes (parm_die, node);
17425 if (origin == NULL
17426 || (! DECL_ABSTRACT (node_or_origin)
17427 && variably_modified_type_p (TREE_TYPE (node_or_origin),
17428 decl_function_context
17429 (node_or_origin))))
17431 tree type = TREE_TYPE (node_or_origin);
17432 if (decl_by_reference_p (node_or_origin))
17433 add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
17434 context_die);
17435 else
17436 add_type_attribute (parm_die, type,
17437 TREE_READONLY (node_or_origin),
17438 TREE_THIS_VOLATILE (node_or_origin),
17439 context_die);
17441 if (origin == NULL && DECL_ARTIFICIAL (node))
17442 add_AT_flag (parm_die, DW_AT_artificial, 1);
17444 if (node && node != origin)
17445 equate_decl_number_to_die (node, parm_die);
17446 if (! DECL_ABSTRACT (node_or_origin))
17447 add_location_or_const_value_attribute (parm_die, node_or_origin,
17448 node == NULL, DW_AT_location);
17450 break;
17452 case tcc_type:
17453 /* We were called with some kind of a ..._TYPE node. */
17454 add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
17455 break;
17457 default:
17458 gcc_unreachable ();
17461 return parm_die;
17464 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
17465 children DW_TAG_formal_parameter DIEs representing the arguments of the
17466 parameter pack.
17468 PARM_PACK must be a function parameter pack.
17469 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
17470 must point to the subsequent arguments of the function PACK_ARG belongs to.
17471 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
17472 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
17473 following the last one for which a DIE was generated. */
17475 static dw_die_ref
17476 gen_formal_parameter_pack_die (tree parm_pack,
17477 tree pack_arg,
17478 dw_die_ref subr_die,
17479 tree *next_arg)
17481 tree arg;
17482 dw_die_ref parm_pack_die;
17484 gcc_assert (parm_pack
17485 && lang_hooks.function_parameter_pack_p (parm_pack)
17486 && subr_die);
17488 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
17489 add_src_coords_attributes (parm_pack_die, parm_pack);
17491 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
17493 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
17494 parm_pack))
17495 break;
17496 gen_formal_parameter_die (arg, NULL,
17497 false /* Don't emit name attribute. */,
17498 parm_pack_die);
17500 if (next_arg)
17501 *next_arg = arg;
17502 return parm_pack_die;
17505 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
17506 at the end of an (ANSI prototyped) formal parameters list. */
17508 static void
17509 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
17511 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
17514 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
17515 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
17516 parameters as specified in some function type specification (except for
17517 those which appear as part of a function *definition*). */
17519 static void
17520 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
17522 tree link;
17523 tree formal_type = NULL;
17524 tree first_parm_type;
17525 tree arg;
17527 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
17529 arg = DECL_ARGUMENTS (function_or_method_type);
17530 function_or_method_type = TREE_TYPE (function_or_method_type);
17532 else
17533 arg = NULL_TREE;
17535 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
17537 /* Make our first pass over the list of formal parameter types and output a
17538 DW_TAG_formal_parameter DIE for each one. */
17539 for (link = first_parm_type; link; )
17541 dw_die_ref parm_die;
17543 formal_type = TREE_VALUE (link);
17544 if (formal_type == void_type_node)
17545 break;
17547 /* Output a (nameless) DIE to represent the formal parameter itself. */
17548 parm_die = gen_formal_parameter_die (formal_type, NULL,
17549 true /* Emit name attribute. */,
17550 context_die);
17551 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
17552 && link == first_parm_type)
17554 add_AT_flag (parm_die, DW_AT_artificial, 1);
17555 if (dwarf_version >= 3 || !dwarf_strict)
17556 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
17558 else if (arg && DECL_ARTIFICIAL (arg))
17559 add_AT_flag (parm_die, DW_AT_artificial, 1);
17561 link = TREE_CHAIN (link);
17562 if (arg)
17563 arg = DECL_CHAIN (arg);
17566 /* If this function type has an ellipsis, add a
17567 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
17568 if (formal_type != void_type_node)
17569 gen_unspecified_parameters_die (function_or_method_type, context_die);
17571 /* Make our second (and final) pass over the list of formal parameter types
17572 and output DIEs to represent those types (as necessary). */
17573 for (link = TYPE_ARG_TYPES (function_or_method_type);
17574 link && TREE_VALUE (link);
17575 link = TREE_CHAIN (link))
17576 gen_type_die (TREE_VALUE (link), context_die);
17579 /* We want to generate the DIE for TYPE so that we can generate the
17580 die for MEMBER, which has been defined; we will need to refer back
17581 to the member declaration nested within TYPE. If we're trying to
17582 generate minimal debug info for TYPE, processing TYPE won't do the
17583 trick; we need to attach the member declaration by hand. */
17585 static void
17586 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
17588 gen_type_die (type, context_die);
17590 /* If we're trying to avoid duplicate debug info, we may not have
17591 emitted the member decl for this function. Emit it now. */
17592 if (TYPE_STUB_DECL (type)
17593 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
17594 && ! lookup_decl_die (member))
17596 dw_die_ref type_die;
17597 gcc_assert (!decl_ultimate_origin (member));
17599 push_decl_scope (type);
17600 type_die = lookup_type_die_strip_naming_typedef (type);
17601 if (TREE_CODE (member) == FUNCTION_DECL)
17602 gen_subprogram_die (member, type_die);
17603 else if (TREE_CODE (member) == FIELD_DECL)
17605 /* Ignore the nameless fields that are used to skip bits but handle
17606 C++ anonymous unions and structs. */
17607 if (DECL_NAME (member) != NULL_TREE
17608 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
17609 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
17611 gen_type_die (member_declared_type (member), type_die);
17612 gen_field_die (member, type_die);
17615 else
17616 gen_variable_die (member, NULL_TREE, type_die);
17618 pop_decl_scope ();
17622 /* Forward declare these functions, because they are mutually recursive
17623 with their set_block_* pairing functions. */
17624 static void set_decl_origin_self (tree);
17625 static void set_decl_abstract_flags (tree, int);
17627 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
17628 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
17629 that it points to the node itself, thus indicating that the node is its
17630 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
17631 the given node is NULL, recursively descend the decl/block tree which
17632 it is the root of, and for each other ..._DECL or BLOCK node contained
17633 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
17634 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
17635 values to point to themselves. */
17637 static void
17638 set_block_origin_self (tree stmt)
17640 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
17642 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
17645 tree local_decl;
17647 for (local_decl = BLOCK_VARS (stmt);
17648 local_decl != NULL_TREE;
17649 local_decl = DECL_CHAIN (local_decl))
17650 if (! DECL_EXTERNAL (local_decl))
17651 set_decl_origin_self (local_decl); /* Potential recursion. */
17655 tree subblock;
17657 for (subblock = BLOCK_SUBBLOCKS (stmt);
17658 subblock != NULL_TREE;
17659 subblock = BLOCK_CHAIN (subblock))
17660 set_block_origin_self (subblock); /* Recurse. */
17665 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
17666 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
17667 node to so that it points to the node itself, thus indicating that the
17668 node represents its own (abstract) origin. Additionally, if the
17669 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
17670 the decl/block tree of which the given node is the root of, and for
17671 each other ..._DECL or BLOCK node contained therein whose
17672 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
17673 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
17674 point to themselves. */
17676 static void
17677 set_decl_origin_self (tree decl)
17679 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
17681 DECL_ABSTRACT_ORIGIN (decl) = decl;
17682 if (TREE_CODE (decl) == FUNCTION_DECL)
17684 tree arg;
17686 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17687 DECL_ABSTRACT_ORIGIN (arg) = arg;
17688 if (DECL_INITIAL (decl) != NULL_TREE
17689 && DECL_INITIAL (decl) != error_mark_node)
17690 set_block_origin_self (DECL_INITIAL (decl));
17695 /* Given a pointer to some BLOCK node, and a boolean value to set the
17696 "abstract" flags to, set that value into the BLOCK_ABSTRACT flag for
17697 the given block, and for all local decls and all local sub-blocks
17698 (recursively) which are contained therein. */
17700 static void
17701 set_block_abstract_flags (tree stmt, int setting)
17703 tree local_decl;
17704 tree subblock;
17705 unsigned int i;
17707 BLOCK_ABSTRACT (stmt) = setting;
17709 for (local_decl = BLOCK_VARS (stmt);
17710 local_decl != NULL_TREE;
17711 local_decl = DECL_CHAIN (local_decl))
17712 if (! DECL_EXTERNAL (local_decl))
17713 set_decl_abstract_flags (local_decl, setting);
17715 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
17717 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
17718 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
17719 || TREE_CODE (local_decl) == PARM_DECL)
17720 set_decl_abstract_flags (local_decl, setting);
17723 for (subblock = BLOCK_SUBBLOCKS (stmt);
17724 subblock != NULL_TREE;
17725 subblock = BLOCK_CHAIN (subblock))
17726 set_block_abstract_flags (subblock, setting);
17729 /* Given a pointer to some ..._DECL node, and a boolean value to set the
17730 "abstract" flags to, set that value into the DECL_ABSTRACT flag for the
17731 given decl, and (in the case where the decl is a FUNCTION_DECL) also
17732 set the abstract flags for all of the parameters, local vars, local
17733 blocks and sub-blocks (recursively) to the same setting. */
17735 static void
17736 set_decl_abstract_flags (tree decl, int setting)
17738 DECL_ABSTRACT (decl) = setting;
17739 if (TREE_CODE (decl) == FUNCTION_DECL)
17741 tree arg;
17743 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17744 DECL_ABSTRACT (arg) = setting;
17745 if (DECL_INITIAL (decl) != NULL_TREE
17746 && DECL_INITIAL (decl) != error_mark_node)
17747 set_block_abstract_flags (DECL_INITIAL (decl), setting);
17751 /* Generate the DWARF2 info for the "abstract" instance of a function which we
17752 may later generate inlined and/or out-of-line instances of. */
17754 static void
17755 dwarf2out_abstract_function (tree decl)
17757 dw_die_ref old_die;
17758 tree save_fn;
17759 tree context;
17760 int was_abstract;
17761 htab_t old_decl_loc_table;
17762 htab_t old_cached_dw_loc_list_table;
17763 int old_call_site_count, old_tail_call_site_count;
17764 struct call_arg_loc_node *old_call_arg_locations;
17766 /* Make sure we have the actual abstract inline, not a clone. */
17767 decl = DECL_ORIGIN (decl);
17769 old_die = lookup_decl_die (decl);
17770 if (old_die && get_AT (old_die, DW_AT_inline))
17771 /* We've already generated the abstract instance. */
17772 return;
17774 /* We can be called while recursively when seeing block defining inlined subroutine
17775 DIE. Be sure to not clobber the outer location table nor use it or we would
17776 get locations in abstract instantces. */
17777 old_decl_loc_table = decl_loc_table;
17778 decl_loc_table = NULL;
17779 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
17780 cached_dw_loc_list_table = NULL;
17781 old_call_arg_locations = call_arg_locations;
17782 call_arg_locations = NULL;
17783 old_call_site_count = call_site_count;
17784 call_site_count = -1;
17785 old_tail_call_site_count = tail_call_site_count;
17786 tail_call_site_count = -1;
17788 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
17789 we don't get confused by DECL_ABSTRACT. */
17790 if (debug_info_level > DINFO_LEVEL_TERSE)
17792 context = decl_class_context (decl);
17793 if (context)
17794 gen_type_die_for_member
17795 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
17798 /* Pretend we've just finished compiling this function. */
17799 save_fn = current_function_decl;
17800 current_function_decl = decl;
17802 was_abstract = DECL_ABSTRACT (decl);
17803 set_decl_abstract_flags (decl, 1);
17804 dwarf2out_decl (decl);
17805 if (! was_abstract)
17806 set_decl_abstract_flags (decl, 0);
17808 current_function_decl = save_fn;
17809 decl_loc_table = old_decl_loc_table;
17810 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
17811 call_arg_locations = old_call_arg_locations;
17812 call_site_count = old_call_site_count;
17813 tail_call_site_count = old_tail_call_site_count;
17816 /* Helper function of premark_used_types() which gets called through
17817 htab_traverse.
17819 Marks the DIE of a given type in *SLOT as perennial, so it never gets
17820 marked as unused by prune_unused_types. */
17822 static int
17823 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
17825 tree type;
17826 dw_die_ref die;
17828 type = (tree) *slot;
17829 die = lookup_type_die (type);
17830 if (die != NULL)
17831 die->die_perennial_p = 1;
17832 return 1;
17835 /* Helper function of premark_types_used_by_global_vars which gets called
17836 through htab_traverse.
17838 Marks the DIE of a given type in *SLOT as perennial, so it never gets
17839 marked as unused by prune_unused_types. The DIE of the type is marked
17840 only if the global variable using the type will actually be emitted. */
17842 static int
17843 premark_types_used_by_global_vars_helper (void **slot,
17844 void *data ATTRIBUTE_UNUSED)
17846 struct types_used_by_vars_entry *entry;
17847 dw_die_ref die;
17849 entry = (struct types_used_by_vars_entry *) *slot;
17850 gcc_assert (entry->type != NULL
17851 && entry->var_decl != NULL);
17852 die = lookup_type_die (entry->type);
17853 if (die)
17855 /* Ask cgraph if the global variable really is to be emitted.
17856 If yes, then we'll keep the DIE of ENTRY->TYPE. */
17857 varpool_node *node = varpool_get_node (entry->var_decl);
17858 if (node && node->definition)
17860 die->die_perennial_p = 1;
17861 /* Keep the parent DIEs as well. */
17862 while ((die = die->die_parent) && die->die_perennial_p == 0)
17863 die->die_perennial_p = 1;
17866 return 1;
17869 /* Mark all members of used_types_hash as perennial. */
17871 static void
17872 premark_used_types (struct function *fun)
17874 if (fun && fun->used_types_hash)
17875 htab_traverse (fun->used_types_hash, premark_used_types_helper, NULL);
17878 /* Mark all members of types_used_by_vars_entry as perennial. */
17880 static void
17881 premark_types_used_by_global_vars (void)
17883 if (types_used_by_vars_hash)
17884 htab_traverse (types_used_by_vars_hash,
17885 premark_types_used_by_global_vars_helper, NULL);
17888 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
17889 for CA_LOC call arg loc node. */
17891 static dw_die_ref
17892 gen_call_site_die (tree decl, dw_die_ref subr_die,
17893 struct call_arg_loc_node *ca_loc)
17895 dw_die_ref stmt_die = NULL, die;
17896 tree block = ca_loc->block;
17898 while (block
17899 && block != DECL_INITIAL (decl)
17900 && TREE_CODE (block) == BLOCK)
17902 if (block_map.length () > BLOCK_NUMBER (block))
17903 stmt_die = block_map[BLOCK_NUMBER (block)];
17904 if (stmt_die)
17905 break;
17906 block = BLOCK_SUPERCONTEXT (block);
17908 if (stmt_die == NULL)
17909 stmt_die = subr_die;
17910 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
17911 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
17912 if (ca_loc->tail_call_p)
17913 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
17914 if (ca_loc->symbol_ref)
17916 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
17917 if (tdie)
17918 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
17919 else
17920 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
17922 return die;
17925 /* Generate a DIE to represent a declared function (either file-scope or
17926 block-local). */
17928 static void
17929 gen_subprogram_die (tree decl, dw_die_ref context_die)
17931 tree origin = decl_ultimate_origin (decl);
17932 dw_die_ref subr_die;
17933 tree outer_scope;
17934 dw_die_ref old_die = lookup_decl_die (decl);
17935 int declaration = (current_function_decl != decl
17936 || class_or_namespace_scope_p (context_die));
17938 premark_used_types (DECL_STRUCT_FUNCTION (decl));
17940 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
17941 started to generate the abstract instance of an inline, decided to output
17942 its containing class, and proceeded to emit the declaration of the inline
17943 from the member list for the class. If so, DECLARATION takes priority;
17944 we'll get back to the abstract instance when done with the class. */
17946 /* The class-scope declaration DIE must be the primary DIE. */
17947 if (origin && declaration && class_or_namespace_scope_p (context_die))
17949 origin = NULL;
17950 gcc_assert (!old_die);
17953 /* Now that the C++ front end lazily declares artificial member fns, we
17954 might need to retrofit the declaration into its class. */
17955 if (!declaration && !origin && !old_die
17956 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
17957 && !class_or_namespace_scope_p (context_die)
17958 && debug_info_level > DINFO_LEVEL_TERSE)
17959 old_die = force_decl_die (decl);
17961 if (origin != NULL)
17963 gcc_assert (!declaration || local_scope_p (context_die));
17965 /* Fixup die_parent for the abstract instance of a nested
17966 inline function. */
17967 if (old_die && old_die->die_parent == NULL)
17968 add_child_die (context_die, old_die);
17970 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17971 add_abstract_origin_attribute (subr_die, origin);
17972 /* This is where the actual code for a cloned function is.
17973 Let's emit linkage name attribute for it. This helps
17974 debuggers to e.g, set breakpoints into
17975 constructors/destructors when the user asks "break
17976 K::K". */
17977 add_linkage_name (subr_die, decl);
17979 else if (old_die)
17981 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17982 struct dwarf_file_data * file_index = lookup_filename (s.file);
17984 if (!get_AT_flag (old_die, DW_AT_declaration)
17985 /* We can have a normal definition following an inline one in the
17986 case of redefinition of GNU C extern inlines.
17987 It seems reasonable to use AT_specification in this case. */
17988 && !get_AT (old_die, DW_AT_inline))
17990 /* Detect and ignore this case, where we are trying to output
17991 something we have already output. */
17992 return;
17995 /* If the definition comes from the same place as the declaration,
17996 maybe use the old DIE. We always want the DIE for this function
17997 that has the *_pc attributes to be under comp_unit_die so the
17998 debugger can find it. We also need to do this for abstract
17999 instances of inlines, since the spec requires the out-of-line copy
18000 to have the same parent. For local class methods, this doesn't
18001 apply; we just use the old DIE. */
18002 if ((is_cu_die (old_die->die_parent) || context_die == NULL)
18003 && (DECL_ARTIFICIAL (decl)
18004 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18005 && (get_AT_unsigned (old_die, DW_AT_decl_line)
18006 == (unsigned) s.line))))
18008 subr_die = old_die;
18010 /* Clear out the declaration attribute and the formal parameters.
18011 Do not remove all children, because it is possible that this
18012 declaration die was forced using force_decl_die(). In such
18013 cases die that forced declaration die (e.g. TAG_imported_module)
18014 is one of the children that we do not want to remove. */
18015 remove_AT (subr_die, DW_AT_declaration);
18016 remove_AT (subr_die, DW_AT_object_pointer);
18017 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18019 else
18021 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18022 add_AT_specification (subr_die, old_die);
18023 add_pubname (decl, subr_die);
18024 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18025 add_AT_file (subr_die, DW_AT_decl_file, file_index);
18026 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18027 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18029 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
18030 emit the real type on the definition die. */
18031 if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE)
18033 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
18034 if (die == auto_die || die == decltype_auto_die)
18035 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18036 0, 0, context_die);
18040 else
18042 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18044 if (TREE_PUBLIC (decl))
18045 add_AT_flag (subr_die, DW_AT_external, 1);
18047 add_name_and_src_coords_attributes (subr_die, decl);
18048 add_pubname (decl, subr_die);
18049 if (debug_info_level > DINFO_LEVEL_TERSE)
18051 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18052 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18053 0, 0, context_die);
18056 add_pure_or_virtual_attribute (subr_die, decl);
18057 if (DECL_ARTIFICIAL (decl))
18058 add_AT_flag (subr_die, DW_AT_artificial, 1);
18060 add_accessibility_attribute (subr_die, decl);
18063 if (declaration)
18065 if (!old_die || !get_AT (old_die, DW_AT_inline))
18067 add_AT_flag (subr_die, DW_AT_declaration, 1);
18069 /* If this is an explicit function declaration then generate
18070 a DW_AT_explicit attribute. */
18071 if (lang_hooks.decls.function_decl_explicit_p (decl)
18072 && (dwarf_version >= 3 || !dwarf_strict))
18073 add_AT_flag (subr_die, DW_AT_explicit, 1);
18075 /* The first time we see a member function, it is in the context of
18076 the class to which it belongs. We make sure of this by emitting
18077 the class first. The next time is the definition, which is
18078 handled above. The two may come from the same source text.
18080 Note that force_decl_die() forces function declaration die. It is
18081 later reused to represent definition. */
18082 equate_decl_number_to_die (decl, subr_die);
18085 else if (DECL_ABSTRACT (decl))
18087 if (DECL_DECLARED_INLINE_P (decl))
18089 if (cgraph_function_possibly_inlined_p (decl))
18090 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18091 else
18092 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18094 else
18096 if (cgraph_function_possibly_inlined_p (decl))
18097 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18098 else
18099 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18102 if (DECL_DECLARED_INLINE_P (decl)
18103 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18104 add_AT_flag (subr_die, DW_AT_artificial, 1);
18106 equate_decl_number_to_die (decl, subr_die);
18108 else if (!DECL_EXTERNAL (decl))
18110 HOST_WIDE_INT cfa_fb_offset;
18111 struct function *fun = DECL_STRUCT_FUNCTION (decl);
18113 if (!old_die || !get_AT (old_die, DW_AT_inline))
18114 equate_decl_number_to_die (decl, subr_die);
18116 gcc_checking_assert (fun);
18117 if (!flag_reorder_blocks_and_partition)
18119 dw_fde_ref fde = fun->fde;
18120 if (fde->dw_fde_begin)
18122 /* We have already generated the labels. */
18123 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18124 fde->dw_fde_end, false);
18126 else
18128 /* Create start/end labels and add the range. */
18129 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
18130 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
18131 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
18132 current_function_funcdef_no);
18133 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
18134 current_function_funcdef_no);
18135 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
18136 false);
18139 #if VMS_DEBUGGING_INFO
18140 /* HP OpenVMS Industry Standard 64: DWARF Extensions
18141 Section 2.3 Prologue and Epilogue Attributes:
18142 When a breakpoint is set on entry to a function, it is generally
18143 desirable for execution to be suspended, not on the very first
18144 instruction of the function, but rather at a point after the
18145 function's frame has been set up, after any language defined local
18146 declaration processing has been completed, and before execution of
18147 the first statement of the function begins. Debuggers generally
18148 cannot properly determine where this point is. Similarly for a
18149 breakpoint set on exit from a function. The prologue and epilogue
18150 attributes allow a compiler to communicate the location(s) to use. */
18153 if (fde->dw_fde_vms_end_prologue)
18154 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18155 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18157 if (fde->dw_fde_vms_begin_epilogue)
18158 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18159 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18161 #endif
18164 else
18166 /* Generate pubnames entries for the split function code ranges. */
18167 dw_fde_ref fde = fun->fde;
18169 if (fde->dw_fde_second_begin)
18171 if (dwarf_version >= 3 || !dwarf_strict)
18173 /* We should use ranges for non-contiguous code section
18174 addresses. Use the actual code range for the initial
18175 section, since the HOT/COLD labels might precede an
18176 alignment offset. */
18177 bool range_list_added = false;
18178 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
18179 fde->dw_fde_end, &range_list_added,
18180 false);
18181 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
18182 fde->dw_fde_second_end,
18183 &range_list_added, false);
18184 if (range_list_added)
18185 add_ranges (NULL);
18187 else
18189 /* There is no real support in DW2 for this .. so we make
18190 a work-around. First, emit the pub name for the segment
18191 containing the function label. Then make and emit a
18192 simplified subprogram DIE for the second segment with the
18193 name pre-fixed by __hot/cold_sect_of_. We use the same
18194 linkage name for the second die so that gdb will find both
18195 sections when given "b foo". */
18196 const char *name = NULL;
18197 tree decl_name = DECL_NAME (decl);
18198 dw_die_ref seg_die;
18200 /* Do the 'primary' section. */
18201 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18202 fde->dw_fde_end, false);
18204 /* Build a minimal DIE for the secondary section. */
18205 seg_die = new_die (DW_TAG_subprogram,
18206 subr_die->die_parent, decl);
18208 if (TREE_PUBLIC (decl))
18209 add_AT_flag (seg_die, DW_AT_external, 1);
18211 if (decl_name != NULL
18212 && IDENTIFIER_POINTER (decl_name) != NULL)
18214 name = dwarf2_name (decl, 1);
18215 if (! DECL_ARTIFICIAL (decl))
18216 add_src_coords_attributes (seg_die, decl);
18218 add_linkage_name (seg_die, decl);
18220 gcc_assert (name != NULL);
18221 add_pure_or_virtual_attribute (seg_die, decl);
18222 if (DECL_ARTIFICIAL (decl))
18223 add_AT_flag (seg_die, DW_AT_artificial, 1);
18225 name = concat ("__second_sect_of_", name, NULL);
18226 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
18227 fde->dw_fde_second_end, false);
18228 add_name_attribute (seg_die, name);
18229 if (want_pubnames ())
18230 add_pubname_string (name, seg_die);
18233 else
18234 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
18235 false);
18238 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
18240 /* We define the "frame base" as the function's CFA. This is more
18241 convenient for several reasons: (1) It's stable across the prologue
18242 and epilogue, which makes it better than just a frame pointer,
18243 (2) With dwarf3, there exists a one-byte encoding that allows us
18244 to reference the .debug_frame data by proxy, but failing that,
18245 (3) We can at least reuse the code inspection and interpretation
18246 code that determines the CFA position at various points in the
18247 function. */
18248 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
18250 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
18251 add_AT_loc (subr_die, DW_AT_frame_base, op);
18253 else
18255 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
18256 if (list->dw_loc_next)
18257 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
18258 else
18259 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
18262 /* Compute a displacement from the "steady-state frame pointer" to
18263 the CFA. The former is what all stack slots and argument slots
18264 will reference in the rtl; the latter is what we've told the
18265 debugger about. We'll need to adjust all frame_base references
18266 by this displacement. */
18267 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
18269 if (fun->static_chain_decl)
18270 add_AT_location_description (subr_die, DW_AT_static_link,
18271 loc_list_from_tree (fun->static_chain_decl, 2));
18274 /* Generate child dies for template paramaters. */
18275 if (debug_info_level > DINFO_LEVEL_TERSE)
18276 gen_generic_params_dies (decl);
18278 /* Now output descriptions of the arguments for this function. This gets
18279 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
18280 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
18281 `...' at the end of the formal parameter list. In order to find out if
18282 there was a trailing ellipsis or not, we must instead look at the type
18283 associated with the FUNCTION_DECL. This will be a node of type
18284 FUNCTION_TYPE. If the chain of type nodes hanging off of this
18285 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
18286 an ellipsis at the end. */
18288 /* In the case where we are describing a mere function declaration, all we
18289 need to do here (and all we *can* do here) is to describe the *types* of
18290 its formal parameters. */
18291 if (debug_info_level <= DINFO_LEVEL_TERSE)
18293 else if (declaration)
18294 gen_formal_types_die (decl, subr_die);
18295 else
18297 /* Generate DIEs to represent all known formal parameters. */
18298 tree parm = DECL_ARGUMENTS (decl);
18299 tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
18300 tree generic_decl_parm = generic_decl
18301 ? DECL_ARGUMENTS (generic_decl)
18302 : NULL;
18304 /* Now we want to walk the list of parameters of the function and
18305 emit their relevant DIEs.
18307 We consider the case of DECL being an instance of a generic function
18308 as well as it being a normal function.
18310 If DECL is an instance of a generic function we walk the
18311 parameters of the generic function declaration _and_ the parameters of
18312 DECL itself. This is useful because we want to emit specific DIEs for
18313 function parameter packs and those are declared as part of the
18314 generic function declaration. In that particular case,
18315 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
18316 That DIE has children DIEs representing the set of arguments
18317 of the pack. Note that the set of pack arguments can be empty.
18318 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
18319 children DIE.
18321 Otherwise, we just consider the parameters of DECL. */
18322 while (generic_decl_parm || parm)
18324 if (generic_decl_parm
18325 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
18326 gen_formal_parameter_pack_die (generic_decl_parm,
18327 parm, subr_die,
18328 &parm);
18329 else if (parm)
18331 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
18333 if (parm == DECL_ARGUMENTS (decl)
18334 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
18335 && parm_die
18336 && (dwarf_version >= 3 || !dwarf_strict))
18337 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
18339 parm = DECL_CHAIN (parm);
18342 if (generic_decl_parm)
18343 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
18346 /* Decide whether we need an unspecified_parameters DIE at the end.
18347 There are 2 more cases to do this for: 1) the ansi ... declaration -
18348 this is detectable when the end of the arg list is not a
18349 void_type_node 2) an unprototyped function declaration (not a
18350 definition). This just means that we have no info about the
18351 parameters at all. */
18352 if (prototype_p (TREE_TYPE (decl)))
18354 /* This is the prototyped case, check for.... */
18355 if (stdarg_p (TREE_TYPE (decl)))
18356 gen_unspecified_parameters_die (decl, subr_die);
18358 else if (DECL_INITIAL (decl) == NULL_TREE)
18359 gen_unspecified_parameters_die (decl, subr_die);
18362 /* Output Dwarf info for all of the stuff within the body of the function
18363 (if it has one - it may be just a declaration). */
18364 outer_scope = DECL_INITIAL (decl);
18366 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
18367 a function. This BLOCK actually represents the outermost binding contour
18368 for the function, i.e. the contour in which the function's formal
18369 parameters and labels get declared. Curiously, it appears that the front
18370 end doesn't actually put the PARM_DECL nodes for the current function onto
18371 the BLOCK_VARS list for this outer scope, but are strung off of the
18372 DECL_ARGUMENTS list for the function instead.
18374 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
18375 the LABEL_DECL nodes for the function however, and we output DWARF info
18376 for those in decls_for_scope. Just within the `outer_scope' there will be
18377 a BLOCK node representing the function's outermost pair of curly braces,
18378 and any blocks used for the base and member initializers of a C++
18379 constructor function. */
18380 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
18382 int call_site_note_count = 0;
18383 int tail_call_site_note_count = 0;
18385 /* Emit a DW_TAG_variable DIE for a named return value. */
18386 if (DECL_NAME (DECL_RESULT (decl)))
18387 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
18389 current_function_has_inlines = 0;
18390 decls_for_scope (outer_scope, subr_die, 0);
18392 if (call_arg_locations && !dwarf_strict)
18394 struct call_arg_loc_node *ca_loc;
18395 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
18397 dw_die_ref die = NULL;
18398 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
18399 rtx arg, next_arg;
18401 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
18402 arg; arg = next_arg)
18404 dw_loc_descr_ref reg, val;
18405 enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
18406 dw_die_ref cdie, tdie = NULL;
18408 next_arg = XEXP (arg, 1);
18409 if (REG_P (XEXP (XEXP (arg, 0), 0))
18410 && next_arg
18411 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
18412 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
18413 && REGNO (XEXP (XEXP (arg, 0), 0))
18414 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
18415 next_arg = XEXP (next_arg, 1);
18416 if (mode == VOIDmode)
18418 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
18419 if (mode == VOIDmode)
18420 mode = GET_MODE (XEXP (arg, 0));
18422 if (mode == VOIDmode || mode == BLKmode)
18423 continue;
18424 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
18426 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18427 tloc = XEXP (XEXP (arg, 0), 1);
18428 continue;
18430 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
18431 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
18433 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18434 tlocc = XEXP (XEXP (arg, 0), 1);
18435 continue;
18437 reg = NULL;
18438 if (REG_P (XEXP (XEXP (arg, 0), 0)))
18439 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
18440 VAR_INIT_STATUS_INITIALIZED);
18441 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
18443 rtx mem = XEXP (XEXP (arg, 0), 0);
18444 reg = mem_loc_descriptor (XEXP (mem, 0),
18445 get_address_mode (mem),
18446 GET_MODE (mem),
18447 VAR_INIT_STATUS_INITIALIZED);
18449 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
18450 == DEBUG_PARAMETER_REF)
18452 tree tdecl
18453 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
18454 tdie = lookup_decl_die (tdecl);
18455 if (tdie == NULL)
18456 continue;
18458 else
18459 continue;
18460 if (reg == NULL
18461 && GET_CODE (XEXP (XEXP (arg, 0), 0))
18462 != DEBUG_PARAMETER_REF)
18463 continue;
18464 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
18465 VOIDmode,
18466 VAR_INIT_STATUS_INITIALIZED);
18467 if (val == NULL)
18468 continue;
18469 if (die == NULL)
18470 die = gen_call_site_die (decl, subr_die, ca_loc);
18471 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
18472 NULL_TREE);
18473 if (reg != NULL)
18474 add_AT_loc (cdie, DW_AT_location, reg);
18475 else if (tdie != NULL)
18476 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
18477 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
18478 if (next_arg != XEXP (arg, 1))
18480 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
18481 if (mode == VOIDmode)
18482 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
18483 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
18484 0), 1),
18485 mode, VOIDmode,
18486 VAR_INIT_STATUS_INITIALIZED);
18487 if (val != NULL)
18488 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
18491 if (die == NULL
18492 && (ca_loc->symbol_ref || tloc))
18493 die = gen_call_site_die (decl, subr_die, ca_loc);
18494 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
18496 dw_loc_descr_ref tval = NULL;
18498 if (tloc != NULL_RTX)
18499 tval = mem_loc_descriptor (tloc,
18500 GET_MODE (tloc) == VOIDmode
18501 ? Pmode : GET_MODE (tloc),
18502 VOIDmode,
18503 VAR_INIT_STATUS_INITIALIZED);
18504 if (tval)
18505 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
18506 else if (tlocc != NULL_RTX)
18508 tval = mem_loc_descriptor (tlocc,
18509 GET_MODE (tlocc) == VOIDmode
18510 ? Pmode : GET_MODE (tlocc),
18511 VOIDmode,
18512 VAR_INIT_STATUS_INITIALIZED);
18513 if (tval)
18514 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
18515 tval);
18518 if (die != NULL)
18520 call_site_note_count++;
18521 if (ca_loc->tail_call_p)
18522 tail_call_site_note_count++;
18526 call_arg_locations = NULL;
18527 call_arg_loc_last = NULL;
18528 if (tail_call_site_count >= 0
18529 && tail_call_site_count == tail_call_site_note_count
18530 && !dwarf_strict)
18532 if (call_site_count >= 0
18533 && call_site_count == call_site_note_count)
18534 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
18535 else
18536 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
18538 call_site_count = -1;
18539 tail_call_site_count = -1;
18542 if (subr_die != old_die)
18543 /* Add the calling convention attribute if requested. */
18544 add_calling_convention_attribute (subr_die, decl);
18547 /* Returns a hash value for X (which really is a die_struct). */
18549 static hashval_t
18550 common_block_die_table_hash (const void *x)
18552 const_dw_die_ref d = (const_dw_die_ref) x;
18553 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
18556 /* Return nonzero if decl_id and die_parent of die_struct X is the same
18557 as decl_id and die_parent of die_struct Y. */
18559 static int
18560 common_block_die_table_eq (const void *x, const void *y)
18562 const_dw_die_ref d = (const_dw_die_ref) x;
18563 const_dw_die_ref e = (const_dw_die_ref) y;
18564 return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
18567 /* Generate a DIE to represent a declared data object.
18568 Either DECL or ORIGIN must be non-null. */
18570 static void
18571 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
18573 HOST_WIDE_INT off = 0;
18574 tree com_decl;
18575 tree decl_or_origin = decl ? decl : origin;
18576 tree ultimate_origin;
18577 dw_die_ref var_die;
18578 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
18579 dw_die_ref origin_die;
18580 bool declaration = (DECL_EXTERNAL (decl_or_origin)
18581 || class_or_namespace_scope_p (context_die));
18582 bool specialization_p = false;
18584 ultimate_origin = decl_ultimate_origin (decl_or_origin);
18585 if (decl || ultimate_origin)
18586 origin = ultimate_origin;
18587 com_decl = fortran_common (decl_or_origin, &off);
18589 /* Symbol in common gets emitted as a child of the common block, in the form
18590 of a data member. */
18591 if (com_decl)
18593 dw_die_ref com_die;
18594 dw_loc_list_ref loc;
18595 die_node com_die_arg;
18597 var_die = lookup_decl_die (decl_or_origin);
18598 if (var_die)
18600 if (get_AT (var_die, DW_AT_location) == NULL)
18602 loc = loc_list_from_tree (com_decl, off ? 1 : 2);
18603 if (loc)
18605 if (off)
18607 /* Optimize the common case. */
18608 if (single_element_loc_list_p (loc)
18609 && loc->expr->dw_loc_opc == DW_OP_addr
18610 && loc->expr->dw_loc_next == NULL
18611 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
18612 == SYMBOL_REF)
18614 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18615 loc->expr->dw_loc_oprnd1.v.val_addr
18616 = plus_constant (GET_MODE (x), x , off);
18618 else
18619 loc_list_plus_const (loc, off);
18621 add_AT_location_description (var_die, DW_AT_location, loc);
18622 remove_AT (var_die, DW_AT_declaration);
18625 return;
18628 if (common_block_die_table == NULL)
18629 common_block_die_table
18630 = htab_create_ggc (10, common_block_die_table_hash,
18631 common_block_die_table_eq, NULL);
18633 com_die_arg.decl_id = DECL_UID (com_decl);
18634 com_die_arg.die_parent = context_die;
18635 com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
18636 loc = loc_list_from_tree (com_decl, 2);
18637 if (com_die == NULL)
18639 const char *cnam
18640 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
18641 void **slot;
18643 com_die = new_die (DW_TAG_common_block, context_die, decl);
18644 add_name_and_src_coords_attributes (com_die, com_decl);
18645 if (loc)
18647 add_AT_location_description (com_die, DW_AT_location, loc);
18648 /* Avoid sharing the same loc descriptor between
18649 DW_TAG_common_block and DW_TAG_variable. */
18650 loc = loc_list_from_tree (com_decl, 2);
18652 else if (DECL_EXTERNAL (decl))
18653 add_AT_flag (com_die, DW_AT_declaration, 1);
18654 if (want_pubnames ())
18655 add_pubname_string (cnam, com_die); /* ??? needed? */
18656 com_die->decl_id = DECL_UID (com_decl);
18657 slot = htab_find_slot (common_block_die_table, com_die, INSERT);
18658 *slot = (void *) com_die;
18660 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
18662 add_AT_location_description (com_die, DW_AT_location, loc);
18663 loc = loc_list_from_tree (com_decl, 2);
18664 remove_AT (com_die, DW_AT_declaration);
18666 var_die = new_die (DW_TAG_variable, com_die, decl);
18667 add_name_and_src_coords_attributes (var_die, decl);
18668 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
18669 TREE_THIS_VOLATILE (decl), context_die);
18670 add_AT_flag (var_die, DW_AT_external, 1);
18671 if (loc)
18673 if (off)
18675 /* Optimize the common case. */
18676 if (single_element_loc_list_p (loc)
18677 && loc->expr->dw_loc_opc == DW_OP_addr
18678 && loc->expr->dw_loc_next == NULL
18679 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
18681 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18682 loc->expr->dw_loc_oprnd1.v.val_addr
18683 = plus_constant (GET_MODE (x), x, off);
18685 else
18686 loc_list_plus_const (loc, off);
18688 add_AT_location_description (var_die, DW_AT_location, loc);
18690 else if (DECL_EXTERNAL (decl))
18691 add_AT_flag (var_die, DW_AT_declaration, 1);
18692 equate_decl_number_to_die (decl, var_die);
18693 return;
18696 /* If the compiler emitted a definition for the DECL declaration
18697 and if we already emitted a DIE for it, don't emit a second
18698 DIE for it again. Allow re-declarations of DECLs that are
18699 inside functions, though. */
18700 if (old_die && declaration && !local_scope_p (context_die))
18701 return;
18703 /* For static data members, the declaration in the class is supposed
18704 to have DW_TAG_member tag; the specification should still be
18705 DW_TAG_variable referencing the DW_TAG_member DIE. */
18706 if (declaration && class_scope_p (context_die))
18707 var_die = new_die (DW_TAG_member, context_die, decl);
18708 else
18709 var_die = new_die (DW_TAG_variable, context_die, decl);
18711 origin_die = NULL;
18712 if (origin != NULL)
18713 origin_die = add_abstract_origin_attribute (var_die, origin);
18715 /* Loop unrolling can create multiple blocks that refer to the same
18716 static variable, so we must test for the DW_AT_declaration flag.
18718 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
18719 copy decls and set the DECL_ABSTRACT flag on them instead of
18720 sharing them.
18722 ??? Duplicated blocks have been rewritten to use .debug_ranges.
18724 ??? The declare_in_namespace support causes us to get two DIEs for one
18725 variable, both of which are declarations. We want to avoid considering
18726 one to be a specification, so we must test that this DIE is not a
18727 declaration. */
18728 else if (old_die && TREE_STATIC (decl) && ! declaration
18729 && get_AT_flag (old_die, DW_AT_declaration) == 1)
18731 /* This is a definition of a C++ class level static. */
18732 add_AT_specification (var_die, old_die);
18733 specialization_p = true;
18734 if (DECL_NAME (decl))
18736 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18737 struct dwarf_file_data * file_index = lookup_filename (s.file);
18739 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18740 add_AT_file (var_die, DW_AT_decl_file, file_index);
18742 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18743 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
18745 if (old_die->die_tag == DW_TAG_member)
18746 add_linkage_name (var_die, decl);
18749 else
18750 add_name_and_src_coords_attributes (var_die, decl);
18752 if ((origin == NULL && !specialization_p)
18753 || (origin != NULL
18754 && !DECL_ABSTRACT (decl_or_origin)
18755 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
18756 decl_function_context
18757 (decl_or_origin))))
18759 tree type = TREE_TYPE (decl_or_origin);
18761 if (decl_by_reference_p (decl_or_origin))
18762 add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
18763 else
18764 add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
18765 TREE_THIS_VOLATILE (decl_or_origin), context_die);
18768 if (origin == NULL && !specialization_p)
18770 if (TREE_PUBLIC (decl))
18771 add_AT_flag (var_die, DW_AT_external, 1);
18773 if (DECL_ARTIFICIAL (decl))
18774 add_AT_flag (var_die, DW_AT_artificial, 1);
18776 add_accessibility_attribute (var_die, decl);
18779 if (declaration)
18780 add_AT_flag (var_die, DW_AT_declaration, 1);
18782 if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
18783 equate_decl_number_to_die (decl, var_die);
18785 if (! declaration
18786 && (! DECL_ABSTRACT (decl_or_origin)
18787 /* Local static vars are shared between all clones/inlines,
18788 so emit DW_AT_location on the abstract DIE if DECL_RTL is
18789 already set. */
18790 || (TREE_CODE (decl_or_origin) == VAR_DECL
18791 && TREE_STATIC (decl_or_origin)
18792 && DECL_RTL_SET_P (decl_or_origin)))
18793 /* When abstract origin already has DW_AT_location attribute, no need
18794 to add it again. */
18795 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
18797 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
18798 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
18799 defer_location (decl_or_origin, var_die);
18800 else
18801 add_location_or_const_value_attribute (var_die, decl_or_origin,
18802 decl == NULL, DW_AT_location);
18803 add_pubname (decl_or_origin, var_die);
18805 else
18806 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
18809 /* Generate a DIE to represent a named constant. */
18811 static void
18812 gen_const_die (tree decl, dw_die_ref context_die)
18814 dw_die_ref const_die;
18815 tree type = TREE_TYPE (decl);
18817 const_die = new_die (DW_TAG_constant, context_die, decl);
18818 add_name_and_src_coords_attributes (const_die, decl);
18819 add_type_attribute (const_die, type, 1, 0, context_die);
18820 if (TREE_PUBLIC (decl))
18821 add_AT_flag (const_die, DW_AT_external, 1);
18822 if (DECL_ARTIFICIAL (decl))
18823 add_AT_flag (const_die, DW_AT_artificial, 1);
18824 tree_add_const_value_attribute_for_decl (const_die, decl);
18827 /* Generate a DIE to represent a label identifier. */
18829 static void
18830 gen_label_die (tree decl, dw_die_ref context_die)
18832 tree origin = decl_ultimate_origin (decl);
18833 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
18834 rtx insn;
18835 char label[MAX_ARTIFICIAL_LABEL_BYTES];
18837 if (origin != NULL)
18838 add_abstract_origin_attribute (lbl_die, origin);
18839 else
18840 add_name_and_src_coords_attributes (lbl_die, decl);
18842 if (DECL_ABSTRACT (decl))
18843 equate_decl_number_to_die (decl, lbl_die);
18844 else
18846 insn = DECL_RTL_IF_SET (decl);
18848 /* Deleted labels are programmer specified labels which have been
18849 eliminated because of various optimizations. We still emit them
18850 here so that it is possible to put breakpoints on them. */
18851 if (insn
18852 && (LABEL_P (insn)
18853 || ((NOTE_P (insn)
18854 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
18856 /* When optimization is enabled (via -O) some parts of the compiler
18857 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
18858 represent source-level labels which were explicitly declared by
18859 the user. This really shouldn't be happening though, so catch
18860 it if it ever does happen. */
18861 gcc_assert (!INSN_DELETED_P (insn));
18863 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
18864 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18866 else if (insn
18867 && NOTE_P (insn)
18868 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
18869 && CODE_LABEL_NUMBER (insn) != -1)
18871 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
18872 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18877 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
18878 attributes to the DIE for a block STMT, to describe where the inlined
18879 function was called from. This is similar to add_src_coords_attributes. */
18881 static inline void
18882 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
18884 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
18886 if (dwarf_version >= 3 || !dwarf_strict)
18888 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
18889 add_AT_unsigned (die, DW_AT_call_line, s.line);
18894 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
18895 Add low_pc and high_pc attributes to the DIE for a block STMT. */
18897 static inline void
18898 add_high_low_attributes (tree stmt, dw_die_ref die)
18900 char label[MAX_ARTIFICIAL_LABEL_BYTES];
18902 if (BLOCK_FRAGMENT_CHAIN (stmt)
18903 && (dwarf_version >= 3 || !dwarf_strict))
18905 tree chain, superblock = NULL_TREE;
18906 dw_die_ref pdie;
18907 dw_attr_ref attr = NULL;
18909 if (inlined_function_outer_scope_p (stmt))
18911 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18912 BLOCK_NUMBER (stmt));
18913 add_AT_lbl_id (die, DW_AT_entry_pc, label);
18916 /* Optimize duplicate .debug_ranges lists or even tails of
18917 lists. If this BLOCK has same ranges as its supercontext,
18918 lookup DW_AT_ranges attribute in the supercontext (and
18919 recursively so), verify that the ranges_table contains the
18920 right values and use it instead of adding a new .debug_range. */
18921 for (chain = stmt, pdie = die;
18922 BLOCK_SAME_RANGE (chain);
18923 chain = BLOCK_SUPERCONTEXT (chain))
18925 dw_attr_ref new_attr;
18927 pdie = pdie->die_parent;
18928 if (pdie == NULL)
18929 break;
18930 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
18931 break;
18932 new_attr = get_AT (pdie, DW_AT_ranges);
18933 if (new_attr == NULL
18934 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
18935 break;
18936 attr = new_attr;
18937 superblock = BLOCK_SUPERCONTEXT (chain);
18939 if (attr != NULL
18940 && (ranges_table[attr->dw_attr_val.v.val_offset
18941 / 2 / DWARF2_ADDR_SIZE].num
18942 == BLOCK_NUMBER (superblock))
18943 && BLOCK_FRAGMENT_CHAIN (superblock))
18945 unsigned long off = attr->dw_attr_val.v.val_offset
18946 / 2 / DWARF2_ADDR_SIZE;
18947 unsigned long supercnt = 0, thiscnt = 0;
18948 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
18949 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
18951 ++supercnt;
18952 gcc_checking_assert (ranges_table[off + supercnt].num
18953 == BLOCK_NUMBER (chain));
18955 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
18956 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
18957 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
18958 ++thiscnt;
18959 gcc_assert (supercnt >= thiscnt);
18960 add_AT_range_list (die, DW_AT_ranges,
18961 ((off + supercnt - thiscnt)
18962 * 2 * DWARF2_ADDR_SIZE),
18963 false);
18964 return;
18967 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
18969 chain = BLOCK_FRAGMENT_CHAIN (stmt);
18972 add_ranges (chain);
18973 chain = BLOCK_FRAGMENT_CHAIN (chain);
18975 while (chain);
18976 add_ranges (NULL);
18978 else
18980 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
18981 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18982 BLOCK_NUMBER (stmt));
18983 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
18984 BLOCK_NUMBER (stmt));
18985 add_AT_low_high_pc (die, label, label_high, false);
18989 /* Generate a DIE for a lexical block. */
18991 static void
18992 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
18994 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
18996 if (call_arg_locations)
18998 if (block_map.length () <= BLOCK_NUMBER (stmt))
18999 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19000 block_map[BLOCK_NUMBER (stmt)] = stmt_die;
19003 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19004 add_high_low_attributes (stmt, stmt_die);
19006 decls_for_scope (stmt, stmt_die, depth);
19009 /* Generate a DIE for an inlined subprogram. */
19011 static void
19012 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
19014 tree decl;
19016 /* The instance of function that is effectively being inlined shall not
19017 be abstract. */
19018 gcc_assert (! BLOCK_ABSTRACT (stmt));
19020 decl = block_ultimate_origin (stmt);
19022 /* Emit info for the abstract instance first, if we haven't yet. We
19023 must emit this even if the block is abstract, otherwise when we
19024 emit the block below (or elsewhere), we may end up trying to emit
19025 a die whose origin die hasn't been emitted, and crashing. */
19026 dwarf2out_abstract_function (decl);
19028 if (! BLOCK_ABSTRACT (stmt))
19030 dw_die_ref subr_die
19031 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19033 if (call_arg_locations)
19035 if (block_map.length () <= BLOCK_NUMBER (stmt))
19036 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19037 block_map[BLOCK_NUMBER (stmt)] = subr_die;
19039 add_abstract_origin_attribute (subr_die, decl);
19040 if (TREE_ASM_WRITTEN (stmt))
19041 add_high_low_attributes (stmt, subr_die);
19042 add_call_src_coords_attributes (stmt, subr_die);
19044 decls_for_scope (stmt, subr_die, depth);
19045 current_function_has_inlines = 1;
19049 /* Generate a DIE for a field in a record, or structure. */
19051 static void
19052 gen_field_die (tree decl, dw_die_ref context_die)
19054 dw_die_ref decl_die;
19056 if (TREE_TYPE (decl) == error_mark_node)
19057 return;
19059 decl_die = new_die (DW_TAG_member, context_die, decl);
19060 add_name_and_src_coords_attributes (decl_die, decl);
19061 add_type_attribute (decl_die, member_declared_type (decl),
19062 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
19063 context_die);
19065 if (DECL_BIT_FIELD_TYPE (decl))
19067 add_byte_size_attribute (decl_die, decl);
19068 add_bit_size_attribute (decl_die, decl);
19069 add_bit_offset_attribute (decl_die, decl);
19072 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19073 add_data_member_location_attribute (decl_die, decl);
19075 if (DECL_ARTIFICIAL (decl))
19076 add_AT_flag (decl_die, DW_AT_artificial, 1);
19078 add_accessibility_attribute (decl_die, decl);
19080 /* Equate decl number to die, so that we can look up this decl later on. */
19081 equate_decl_number_to_die (decl, decl_die);
19084 #if 0
19085 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19086 Use modified_type_die instead.
19087 We keep this code here just in case these types of DIEs may be needed to
19088 represent certain things in other languages (e.g. Pascal) someday. */
19090 static void
19091 gen_pointer_type_die (tree type, dw_die_ref context_die)
19093 dw_die_ref ptr_die
19094 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19096 equate_type_number_to_die (type, ptr_die);
19097 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19098 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19101 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19102 Use modified_type_die instead.
19103 We keep this code here just in case these types of DIEs may be needed to
19104 represent certain things in other languages (e.g. Pascal) someday. */
19106 static void
19107 gen_reference_type_die (tree type, dw_die_ref context_die)
19109 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
19111 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
19112 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
19113 else
19114 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
19116 equate_type_number_to_die (type, ref_die);
19117 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
19118 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19120 #endif
19122 /* Generate a DIE for a pointer to a member type. */
19124 static void
19125 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
19127 dw_die_ref ptr_die
19128 = new_die (DW_TAG_ptr_to_member_type,
19129 scope_die_for (type, context_die), type);
19131 equate_type_number_to_die (type, ptr_die);
19132 add_AT_die_ref (ptr_die, DW_AT_containing_type,
19133 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
19134 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19137 typedef const char *dchar_p; /* For DEF_VEC_P. */
19139 static char *producer_string;
19141 /* Return a heap allocated producer string including command line options
19142 if -grecord-gcc-switches. */
19144 static char *
19145 gen_producer_string (void)
19147 size_t j;
19148 auto_vec<dchar_p> switches;
19149 const char *language_string = lang_hooks.name;
19150 char *producer, *tail;
19151 const char *p;
19152 size_t len = dwarf_record_gcc_switches ? 0 : 3;
19153 size_t plen = strlen (language_string) + 1 + strlen (version_string);
19155 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
19156 switch (save_decoded_options[j].opt_index)
19158 case OPT_o:
19159 case OPT_d:
19160 case OPT_dumpbase:
19161 case OPT_dumpdir:
19162 case OPT_auxbase:
19163 case OPT_auxbase_strip:
19164 case OPT_quiet:
19165 case OPT_version:
19166 case OPT_v:
19167 case OPT_w:
19168 case OPT_L:
19169 case OPT_D:
19170 case OPT_I:
19171 case OPT_U:
19172 case OPT_SPECIAL_unknown:
19173 case OPT_SPECIAL_ignore:
19174 case OPT_SPECIAL_program_name:
19175 case OPT_SPECIAL_input_file:
19176 case OPT_grecord_gcc_switches:
19177 case OPT_gno_record_gcc_switches:
19178 case OPT__output_pch_:
19179 case OPT_fdiagnostics_show_location_:
19180 case OPT_fdiagnostics_show_option:
19181 case OPT_fdiagnostics_show_caret:
19182 case OPT_fdiagnostics_color_:
19183 case OPT_fverbose_asm:
19184 case OPT____:
19185 case OPT__sysroot_:
19186 case OPT_nostdinc:
19187 case OPT_nostdinc__:
19188 /* Ignore these. */
19189 continue;
19190 default:
19191 if (cl_options[save_decoded_options[j].opt_index].flags
19192 & CL_NO_DWARF_RECORD)
19193 continue;
19194 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
19195 == '-');
19196 switch (save_decoded_options[j].canonical_option[0][1])
19198 case 'M':
19199 case 'i':
19200 case 'W':
19201 continue;
19202 case 'f':
19203 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
19204 "dump", 4) == 0)
19205 continue;
19206 break;
19207 default:
19208 break;
19210 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
19211 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
19212 break;
19215 producer = XNEWVEC (char, plen + 1 + len + 1);
19216 tail = producer;
19217 sprintf (tail, "%s %s", language_string, version_string);
19218 tail += plen;
19220 FOR_EACH_VEC_ELT (switches, j, p)
19222 len = strlen (p);
19223 *tail = ' ';
19224 memcpy (tail + 1, p, len);
19225 tail += len + 1;
19228 *tail = '\0';
19229 return producer;
19232 /* Generate the DIE for the compilation unit. */
19234 static dw_die_ref
19235 gen_compile_unit_die (const char *filename)
19237 dw_die_ref die;
19238 const char *language_string = lang_hooks.name;
19239 int language;
19241 die = new_die (DW_TAG_compile_unit, NULL, NULL);
19243 if (filename)
19245 add_name_attribute (die, filename);
19246 /* Don't add cwd for <built-in>. */
19247 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
19248 add_comp_dir_attribute (die);
19251 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
19253 /* If our producer is LTO try to figure out a common language to use
19254 from the global list of translation units. */
19255 if (strcmp (language_string, "GNU GIMPLE") == 0)
19257 unsigned i;
19258 tree t;
19259 const char *common_lang = NULL;
19261 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
19263 if (!TRANSLATION_UNIT_LANGUAGE (t))
19264 continue;
19265 if (!common_lang)
19266 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
19267 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
19269 else if (strncmp (common_lang, "GNU C", 5) == 0
19270 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
19271 /* Mixing C and C++ is ok, use C++ in that case. */
19272 common_lang = "GNU C++";
19273 else
19275 /* Fall back to C. */
19276 common_lang = NULL;
19277 break;
19281 if (common_lang)
19282 language_string = common_lang;
19285 language = DW_LANG_C89;
19286 if (strcmp (language_string, "GNU C++") == 0)
19287 language = DW_LANG_C_plus_plus;
19288 else if (strcmp (language_string, "GNU F77") == 0)
19289 language = DW_LANG_Fortran77;
19290 else if (strcmp (language_string, "GNU Pascal") == 0)
19291 language = DW_LANG_Pascal83;
19292 else if (dwarf_version >= 3 || !dwarf_strict)
19294 if (strcmp (language_string, "GNU Ada") == 0)
19295 language = DW_LANG_Ada95;
19296 else if (strcmp (language_string, "GNU Fortran") == 0)
19297 language = DW_LANG_Fortran95;
19298 else if (strcmp (language_string, "GNU Java") == 0)
19299 language = DW_LANG_Java;
19300 else if (strcmp (language_string, "GNU Objective-C") == 0)
19301 language = DW_LANG_ObjC;
19302 else if (strcmp (language_string, "GNU Objective-C++") == 0)
19303 language = DW_LANG_ObjC_plus_plus;
19304 else if (dwarf_version >= 5 || !dwarf_strict)
19306 if (strcmp (language_string, "GNU Go") == 0)
19307 language = DW_LANG_Go;
19310 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
19311 else if (strcmp (language_string, "GNU Fortran") == 0)
19312 language = DW_LANG_Fortran90;
19314 add_AT_unsigned (die, DW_AT_language, language);
19316 switch (language)
19318 case DW_LANG_Fortran77:
19319 case DW_LANG_Fortran90:
19320 case DW_LANG_Fortran95:
19321 /* Fortran has case insensitive identifiers and the front-end
19322 lowercases everything. */
19323 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
19324 break;
19325 default:
19326 /* The default DW_ID_case_sensitive doesn't need to be specified. */
19327 break;
19329 return die;
19332 /* Generate the DIE for a base class. */
19334 static void
19335 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
19337 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
19339 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
19340 add_data_member_location_attribute (die, binfo);
19342 if (BINFO_VIRTUAL_P (binfo))
19343 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
19345 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19346 children, otherwise the default is DW_ACCESS_public. In DWARF2
19347 the default has always been DW_ACCESS_private. */
19348 if (access == access_public_node)
19350 if (dwarf_version == 2
19351 || context_die->die_tag == DW_TAG_class_type)
19352 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19354 else if (access == access_protected_node)
19355 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19356 else if (dwarf_version > 2
19357 && context_die->die_tag != DW_TAG_class_type)
19358 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19361 /* Generate a DIE for a class member. */
19363 static void
19364 gen_member_die (tree type, dw_die_ref context_die)
19366 tree member;
19367 tree binfo = TYPE_BINFO (type);
19368 dw_die_ref child;
19370 /* If this is not an incomplete type, output descriptions of each of its
19371 members. Note that as we output the DIEs necessary to represent the
19372 members of this record or union type, we will also be trying to output
19373 DIEs to represent the *types* of those members. However the `type'
19374 function (above) will specifically avoid generating type DIEs for member
19375 types *within* the list of member DIEs for this (containing) type except
19376 for those types (of members) which are explicitly marked as also being
19377 members of this (containing) type themselves. The g++ front- end can
19378 force any given type to be treated as a member of some other (containing)
19379 type by setting the TYPE_CONTEXT of the given (member) type to point to
19380 the TREE node representing the appropriate (containing) type. */
19382 /* First output info about the base classes. */
19383 if (binfo)
19385 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
19386 int i;
19387 tree base;
19389 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19390 gen_inheritance_die (base,
19391 (accesses ? (*accesses)[i] : access_public_node),
19392 context_die);
19395 /* Now output info about the data members and type members. */
19396 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19398 /* If we thought we were generating minimal debug info for TYPE
19399 and then changed our minds, some of the member declarations
19400 may have already been defined. Don't define them again, but
19401 do put them in the right order. */
19403 child = lookup_decl_die (member);
19404 if (child)
19405 splice_child_die (context_die, child);
19406 else
19407 gen_decl_die (member, NULL, context_die);
19410 /* Now output info about the function members (if any). */
19411 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19413 /* Don't include clones in the member list. */
19414 if (DECL_ABSTRACT_ORIGIN (member))
19415 continue;
19417 child = lookup_decl_die (member);
19418 if (child)
19419 splice_child_die (context_die, child);
19420 else
19421 gen_decl_die (member, NULL, context_die);
19425 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
19426 is set, we pretend that the type was never defined, so we only get the
19427 member DIEs needed by later specification DIEs. */
19429 static void
19430 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19431 enum debug_info_usage usage)
19433 dw_die_ref type_die = lookup_type_die (type);
19434 dw_die_ref scope_die = 0;
19435 int nested = 0;
19436 int complete = (TYPE_SIZE (type)
19437 && (! TYPE_STUB_DECL (type)
19438 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19439 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19440 complete = complete && should_emit_struct_debug (type, usage);
19442 if (type_die && ! complete)
19443 return;
19445 if (TYPE_CONTEXT (type) != NULL_TREE
19446 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19447 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19448 nested = 1;
19450 scope_die = scope_die_for (type, context_die);
19452 /* Generate child dies for template paramaters. */
19453 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
19454 schedule_generic_params_dies_gen (type);
19456 if (! type_die || (nested && is_cu_die (scope_die)))
19457 /* First occurrence of type or toplevel definition of nested class. */
19459 dw_die_ref old_die = type_die;
19461 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
19462 ? record_type_tag (type) : DW_TAG_union_type,
19463 scope_die, type);
19464 equate_type_number_to_die (type, type_die);
19465 if (old_die)
19466 add_AT_specification (type_die, old_die);
19467 else
19468 add_name_attribute (type_die, type_tag (type));
19470 else
19471 remove_AT (type_die, DW_AT_declaration);
19473 /* If this type has been completed, then give it a byte_size attribute and
19474 then give a list of members. */
19475 if (complete && !ns_decl)
19477 /* Prevent infinite recursion in cases where the type of some member of
19478 this type is expressed in terms of this type itself. */
19479 TREE_ASM_WRITTEN (type) = 1;
19480 add_byte_size_attribute (type_die, type);
19481 if (TYPE_STUB_DECL (type) != NULL_TREE)
19483 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19484 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19487 /* If the first reference to this type was as the return type of an
19488 inline function, then it may not have a parent. Fix this now. */
19489 if (type_die->die_parent == NULL)
19490 add_child_die (scope_die, type_die);
19492 push_decl_scope (type);
19493 gen_member_die (type, type_die);
19494 pop_decl_scope ();
19496 add_gnat_descriptive_type_attribute (type_die, type, context_die);
19497 if (TYPE_ARTIFICIAL (type))
19498 add_AT_flag (type_die, DW_AT_artificial, 1);
19500 /* GNU extension: Record what type our vtable lives in. */
19501 if (TYPE_VFIELD (type))
19503 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
19505 gen_type_die (vtype, context_die);
19506 add_AT_die_ref (type_die, DW_AT_containing_type,
19507 lookup_type_die (vtype));
19510 else
19512 add_AT_flag (type_die, DW_AT_declaration, 1);
19514 /* We don't need to do this for function-local types. */
19515 if (TYPE_STUB_DECL (type)
19516 && ! decl_function_context (TYPE_STUB_DECL (type)))
19517 vec_safe_push (incomplete_types, type);
19520 if (get_AT (type_die, DW_AT_name))
19521 add_pubtype (type, type_die);
19524 /* Generate a DIE for a subroutine _type_. */
19526 static void
19527 gen_subroutine_type_die (tree type, dw_die_ref context_die)
19529 tree return_type = TREE_TYPE (type);
19530 dw_die_ref subr_die
19531 = new_die (DW_TAG_subroutine_type,
19532 scope_die_for (type, context_die), type);
19534 equate_type_number_to_die (type, subr_die);
19535 add_prototyped_attribute (subr_die, type);
19536 add_type_attribute (subr_die, return_type, 0, 0, context_die);
19537 gen_formal_types_die (type, subr_die);
19539 if (get_AT (subr_die, DW_AT_name))
19540 add_pubtype (type, subr_die);
19543 /* Generate a DIE for a type definition. */
19545 static void
19546 gen_typedef_die (tree decl, dw_die_ref context_die)
19548 dw_die_ref type_die;
19549 tree origin;
19551 if (TREE_ASM_WRITTEN (decl))
19552 return;
19554 TREE_ASM_WRITTEN (decl) = 1;
19555 type_die = new_die (DW_TAG_typedef, context_die, decl);
19556 origin = decl_ultimate_origin (decl);
19557 if (origin != NULL)
19558 add_abstract_origin_attribute (type_die, origin);
19559 else
19561 tree type;
19563 add_name_and_src_coords_attributes (type_die, decl);
19564 if (DECL_ORIGINAL_TYPE (decl))
19566 type = DECL_ORIGINAL_TYPE (decl);
19568 gcc_assert (type != TREE_TYPE (decl));
19569 equate_type_number_to_die (TREE_TYPE (decl), type_die);
19571 else
19573 type = TREE_TYPE (decl);
19575 if (is_naming_typedef_decl (TYPE_NAME (type)))
19577 /* Here, we are in the case of decl being a typedef naming
19578 an anonymous type, e.g:
19579 typedef struct {...} foo;
19580 In that case TREE_TYPE (decl) is not a typedef variant
19581 type and TYPE_NAME of the anonymous type is set to the
19582 TYPE_DECL of the typedef. This construct is emitted by
19583 the C++ FE.
19585 TYPE is the anonymous struct named by the typedef
19586 DECL. As we need the DW_AT_type attribute of the
19587 DW_TAG_typedef to point to the DIE of TYPE, let's
19588 generate that DIE right away. add_type_attribute
19589 called below will then pick (via lookup_type_die) that
19590 anonymous struct DIE. */
19591 if (!TREE_ASM_WRITTEN (type))
19592 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
19594 /* This is a GNU Extension. We are adding a
19595 DW_AT_linkage_name attribute to the DIE of the
19596 anonymous struct TYPE. The value of that attribute
19597 is the name of the typedef decl naming the anonymous
19598 struct. This greatly eases the work of consumers of
19599 this debug info. */
19600 add_linkage_attr (lookup_type_die (type), decl);
19604 add_type_attribute (type_die, type, TREE_READONLY (decl),
19605 TREE_THIS_VOLATILE (decl), context_die);
19607 if (is_naming_typedef_decl (decl))
19608 /* We want that all subsequent calls to lookup_type_die with
19609 TYPE in argument yield the DW_TAG_typedef we have just
19610 created. */
19611 equate_type_number_to_die (type, type_die);
19613 add_accessibility_attribute (type_die, decl);
19616 if (DECL_ABSTRACT (decl))
19617 equate_decl_number_to_die (decl, type_die);
19619 if (get_AT (type_die, DW_AT_name))
19620 add_pubtype (decl, type_die);
19623 /* Generate a DIE for a struct, class, enum or union type. */
19625 static void
19626 gen_tagged_type_die (tree type,
19627 dw_die_ref context_die,
19628 enum debug_info_usage usage)
19630 int need_pop;
19632 if (type == NULL_TREE
19633 || !is_tagged_type (type))
19634 return;
19636 /* If this is a nested type whose containing class hasn't been written
19637 out yet, writing it out will cover this one, too. This does not apply
19638 to instantiations of member class templates; they need to be added to
19639 the containing class as they are generated. FIXME: This hurts the
19640 idea of combining type decls from multiple TUs, since we can't predict
19641 what set of template instantiations we'll get. */
19642 if (TYPE_CONTEXT (type)
19643 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19644 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
19646 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
19648 if (TREE_ASM_WRITTEN (type))
19649 return;
19651 /* If that failed, attach ourselves to the stub. */
19652 push_decl_scope (TYPE_CONTEXT (type));
19653 context_die = lookup_type_die (TYPE_CONTEXT (type));
19654 need_pop = 1;
19656 else if (TYPE_CONTEXT (type) != NULL_TREE
19657 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
19659 /* If this type is local to a function that hasn't been written
19660 out yet, use a NULL context for now; it will be fixed up in
19661 decls_for_scope. */
19662 context_die = lookup_decl_die (TYPE_CONTEXT (type));
19663 /* A declaration DIE doesn't count; nested types need to go in the
19664 specification. */
19665 if (context_die && is_declaration_die (context_die))
19666 context_die = NULL;
19667 need_pop = 0;
19669 else
19671 context_die = declare_in_namespace (type, context_die);
19672 need_pop = 0;
19675 if (TREE_CODE (type) == ENUMERAL_TYPE)
19677 /* This might have been written out by the call to
19678 declare_in_namespace. */
19679 if (!TREE_ASM_WRITTEN (type))
19680 gen_enumeration_type_die (type, context_die);
19682 else
19683 gen_struct_or_union_type_die (type, context_die, usage);
19685 if (need_pop)
19686 pop_decl_scope ();
19688 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
19689 it up if it is ever completed. gen_*_type_die will set it for us
19690 when appropriate. */
19693 /* Generate a type description DIE. */
19695 static void
19696 gen_type_die_with_usage (tree type, dw_die_ref context_die,
19697 enum debug_info_usage usage)
19699 struct array_descr_info info;
19701 if (type == NULL_TREE || type == error_mark_node)
19702 return;
19704 if (TYPE_NAME (type) != NULL_TREE
19705 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
19706 && is_redundant_typedef (TYPE_NAME (type))
19707 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
19708 /* The DECL of this type is a typedef we don't want to emit debug
19709 info for but we want debug info for its underlying typedef.
19710 This can happen for e.g, the injected-class-name of a C++
19711 type. */
19712 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
19714 /* If TYPE is a typedef type variant, let's generate debug info
19715 for the parent typedef which TYPE is a type of. */
19716 if (typedef_variant_p (type))
19718 if (TREE_ASM_WRITTEN (type))
19719 return;
19721 /* Prevent broken recursion; we can't hand off to the same type. */
19722 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
19724 /* Give typedefs the right scope. */
19725 context_die = scope_die_for (type, context_die);
19727 TREE_ASM_WRITTEN (type) = 1;
19729 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19730 return;
19733 /* If type is an anonymous tagged type named by a typedef, let's
19734 generate debug info for the typedef. */
19735 if (is_naming_typedef_decl (TYPE_NAME (type)))
19737 /* Use the DIE of the containing namespace as the parent DIE of
19738 the type description DIE we want to generate. */
19739 if (DECL_CONTEXT (TYPE_NAME (type))
19740 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
19741 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
19743 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19744 return;
19747 /* If this is an array type with hidden descriptor, handle it first. */
19748 if (!TREE_ASM_WRITTEN (type)
19749 && lang_hooks.types.get_array_descr_info
19750 && lang_hooks.types.get_array_descr_info (type, &info)
19751 && (dwarf_version >= 3 || !dwarf_strict))
19753 gen_descr_array_type_die (type, &info, context_die);
19754 TREE_ASM_WRITTEN (type) = 1;
19755 return;
19758 /* We are going to output a DIE to represent the unqualified version
19759 of this type (i.e. without any const or volatile qualifiers) so
19760 get the main variant (i.e. the unqualified version) of this type
19761 now. (Vectors are special because the debugging info is in the
19762 cloned type itself). */
19763 if (TREE_CODE (type) != VECTOR_TYPE)
19764 type = type_main_variant (type);
19766 if (TREE_ASM_WRITTEN (type))
19767 return;
19769 switch (TREE_CODE (type))
19771 case ERROR_MARK:
19772 break;
19774 case POINTER_TYPE:
19775 case REFERENCE_TYPE:
19776 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
19777 ensures that the gen_type_die recursion will terminate even if the
19778 type is recursive. Recursive types are possible in Ada. */
19779 /* ??? We could perhaps do this for all types before the switch
19780 statement. */
19781 TREE_ASM_WRITTEN (type) = 1;
19783 /* For these types, all that is required is that we output a DIE (or a
19784 set of DIEs) to represent the "basis" type. */
19785 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19786 DINFO_USAGE_IND_USE);
19787 break;
19789 case OFFSET_TYPE:
19790 /* This code is used for C++ pointer-to-data-member types.
19791 Output a description of the relevant class type. */
19792 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
19793 DINFO_USAGE_IND_USE);
19795 /* Output a description of the type of the object pointed to. */
19796 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19797 DINFO_USAGE_IND_USE);
19799 /* Now output a DIE to represent this pointer-to-data-member type
19800 itself. */
19801 gen_ptr_to_mbr_type_die (type, context_die);
19802 break;
19804 case FUNCTION_TYPE:
19805 /* Force out return type (in case it wasn't forced out already). */
19806 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19807 DINFO_USAGE_DIR_USE);
19808 gen_subroutine_type_die (type, context_die);
19809 break;
19811 case METHOD_TYPE:
19812 /* Force out return type (in case it wasn't forced out already). */
19813 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19814 DINFO_USAGE_DIR_USE);
19815 gen_subroutine_type_die (type, context_die);
19816 break;
19818 case ARRAY_TYPE:
19819 gen_array_type_die (type, context_die);
19820 break;
19822 case VECTOR_TYPE:
19823 gen_array_type_die (type, context_die);
19824 break;
19826 case ENUMERAL_TYPE:
19827 case RECORD_TYPE:
19828 case UNION_TYPE:
19829 case QUAL_UNION_TYPE:
19830 gen_tagged_type_die (type, context_die, usage);
19831 return;
19833 case VOID_TYPE:
19834 case INTEGER_TYPE:
19835 case REAL_TYPE:
19836 case FIXED_POINT_TYPE:
19837 case COMPLEX_TYPE:
19838 case BOOLEAN_TYPE:
19839 /* No DIEs needed for fundamental types. */
19840 break;
19842 case NULLPTR_TYPE:
19843 case LANG_TYPE:
19844 /* Just use DW_TAG_unspecified_type. */
19846 dw_die_ref type_die = lookup_type_die (type);
19847 if (type_die == NULL)
19849 tree name = TYPE_NAME (type);
19850 if (TREE_CODE (name) == TYPE_DECL)
19851 name = DECL_NAME (name);
19852 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
19853 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
19854 equate_type_number_to_die (type, type_die);
19857 break;
19859 default:
19860 if (is_cxx_auto (type))
19862 tree name = TYPE_NAME (type);
19863 if (TREE_CODE (name) == TYPE_DECL)
19864 name = DECL_NAME (name);
19865 dw_die_ref *die = (name == get_identifier ("auto")
19866 ? &auto_die : &decltype_auto_die);
19867 if (!*die)
19869 *die = new_die (DW_TAG_unspecified_type,
19870 comp_unit_die (), NULL_TREE);
19871 add_name_attribute (*die, IDENTIFIER_POINTER (name));
19873 equate_type_number_to_die (type, *die);
19874 break;
19876 gcc_unreachable ();
19879 TREE_ASM_WRITTEN (type) = 1;
19882 static void
19883 gen_type_die (tree type, dw_die_ref context_die)
19885 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
19888 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
19889 things which are local to the given block. */
19891 static void
19892 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
19894 int must_output_die = 0;
19895 bool inlined_func;
19897 /* Ignore blocks that are NULL. */
19898 if (stmt == NULL_TREE)
19899 return;
19901 inlined_func = inlined_function_outer_scope_p (stmt);
19903 /* If the block is one fragment of a non-contiguous block, do not
19904 process the variables, since they will have been done by the
19905 origin block. Do process subblocks. */
19906 if (BLOCK_FRAGMENT_ORIGIN (stmt))
19908 tree sub;
19910 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
19911 gen_block_die (sub, context_die, depth + 1);
19913 return;
19916 /* Determine if we need to output any Dwarf DIEs at all to represent this
19917 block. */
19918 if (inlined_func)
19919 /* The outer scopes for inlinings *must* always be represented. We
19920 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
19921 must_output_die = 1;
19922 else
19924 /* Determine if this block directly contains any "significant"
19925 local declarations which we will need to output DIEs for. */
19926 if (debug_info_level > DINFO_LEVEL_TERSE)
19927 /* We are not in terse mode so *any* local declaration counts
19928 as being a "significant" one. */
19929 must_output_die = ((BLOCK_VARS (stmt) != NULL
19930 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
19931 && (TREE_USED (stmt)
19932 || TREE_ASM_WRITTEN (stmt)
19933 || BLOCK_ABSTRACT (stmt)));
19934 else if ((TREE_USED (stmt)
19935 || TREE_ASM_WRITTEN (stmt)
19936 || BLOCK_ABSTRACT (stmt))
19937 && !dwarf2out_ignore_block (stmt))
19938 must_output_die = 1;
19941 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
19942 DIE for any block which contains no significant local declarations at
19943 all. Rather, in such cases we just call `decls_for_scope' so that any
19944 needed Dwarf info for any sub-blocks will get properly generated. Note
19945 that in terse mode, our definition of what constitutes a "significant"
19946 local declaration gets restricted to include only inlined function
19947 instances and local (nested) function definitions. */
19948 if (must_output_die)
19950 if (inlined_func)
19952 /* If STMT block is abstract, that means we have been called
19953 indirectly from dwarf2out_abstract_function.
19954 That function rightfully marks the descendent blocks (of
19955 the abstract function it is dealing with) as being abstract,
19956 precisely to prevent us from emitting any
19957 DW_TAG_inlined_subroutine DIE as a descendent
19958 of an abstract function instance. So in that case, we should
19959 not call gen_inlined_subroutine_die.
19961 Later though, when cgraph asks dwarf2out to emit info
19962 for the concrete instance of the function decl into which
19963 the concrete instance of STMT got inlined, the later will lead
19964 to the generation of a DW_TAG_inlined_subroutine DIE. */
19965 if (! BLOCK_ABSTRACT (stmt))
19966 gen_inlined_subroutine_die (stmt, context_die, depth);
19968 else
19969 gen_lexical_block_die (stmt, context_die, depth);
19971 else
19972 decls_for_scope (stmt, context_die, depth);
19975 /* Process variable DECL (or variable with origin ORIGIN) within
19976 block STMT and add it to CONTEXT_DIE. */
19977 static void
19978 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
19980 dw_die_ref die;
19981 tree decl_or_origin = decl ? decl : origin;
19983 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
19984 die = lookup_decl_die (decl_or_origin);
19985 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
19986 && TYPE_DECL_IS_STUB (decl_or_origin))
19987 die = lookup_type_die (TREE_TYPE (decl_or_origin));
19988 else
19989 die = NULL;
19991 if (die != NULL && die->die_parent == NULL)
19992 add_child_die (context_die, die);
19993 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
19994 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
19995 stmt, context_die);
19996 else
19997 gen_decl_die (decl, origin, context_die);
20000 /* Generate all of the decls declared within a given scope and (recursively)
20001 all of its sub-blocks. */
20003 static void
20004 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
20006 tree decl;
20007 unsigned int i;
20008 tree subblocks;
20010 /* Ignore NULL blocks. */
20011 if (stmt == NULL_TREE)
20012 return;
20014 /* Output the DIEs to represent all of the data objects and typedefs
20015 declared directly within this block but not within any nested
20016 sub-blocks. Also, nested function and tag DIEs have been
20017 generated with a parent of NULL; fix that up now. We don't
20018 have to do this if we're at -g1. */
20019 if (debug_info_level > DINFO_LEVEL_TERSE)
20021 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
20022 process_scope_var (stmt, decl, NULL_TREE, context_die);
20023 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20024 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
20025 context_die);
20028 /* Even if we're at -g1, we need to process the subblocks in order to get
20029 inlined call information. */
20031 /* Output the DIEs to represent all sub-blocks (and the items declared
20032 therein) of this block. */
20033 for (subblocks = BLOCK_SUBBLOCKS (stmt);
20034 subblocks != NULL;
20035 subblocks = BLOCK_CHAIN (subblocks))
20036 gen_block_die (subblocks, context_die, depth + 1);
20039 /* Is this a typedef we can avoid emitting? */
20041 static inline int
20042 is_redundant_typedef (const_tree decl)
20044 if (TYPE_DECL_IS_STUB (decl))
20045 return 1;
20047 if (DECL_ARTIFICIAL (decl)
20048 && DECL_CONTEXT (decl)
20049 && is_tagged_type (DECL_CONTEXT (decl))
20050 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
20051 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
20052 /* Also ignore the artificial member typedef for the class name. */
20053 return 1;
20055 return 0;
20058 /* Return TRUE if TYPE is a typedef that names a type for linkage
20059 purposes. This kind of typedefs is produced by the C++ FE for
20060 constructs like:
20062 typedef struct {...} foo;
20064 In that case, there is no typedef variant type produced for foo.
20065 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20066 struct type. */
20068 static bool
20069 is_naming_typedef_decl (const_tree decl)
20071 if (decl == NULL_TREE
20072 || TREE_CODE (decl) != TYPE_DECL
20073 || !is_tagged_type (TREE_TYPE (decl))
20074 || DECL_IS_BUILTIN (decl)
20075 || is_redundant_typedef (decl)
20076 /* It looks like Ada produces TYPE_DECLs that are very similar
20077 to C++ naming typedefs but that have different
20078 semantics. Let's be specific to c++ for now. */
20079 || !is_cxx ())
20080 return FALSE;
20082 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20083 && TYPE_NAME (TREE_TYPE (decl)) == decl
20084 && (TYPE_STUB_DECL (TREE_TYPE (decl))
20085 != TYPE_NAME (TREE_TYPE (decl))));
20088 /* Returns the DIE for a context. */
20090 static inline dw_die_ref
20091 get_context_die (tree context)
20093 if (context)
20095 /* Find die that represents this context. */
20096 if (TYPE_P (context))
20098 context = TYPE_MAIN_VARIANT (context);
20099 return strip_naming_typedef (context, force_type_die (context));
20101 else
20102 return force_decl_die (context);
20104 return comp_unit_die ();
20107 /* Returns the DIE for decl. A DIE will always be returned. */
20109 static dw_die_ref
20110 force_decl_die (tree decl)
20112 dw_die_ref decl_die;
20113 unsigned saved_external_flag;
20114 tree save_fn = NULL_TREE;
20115 decl_die = lookup_decl_die (decl);
20116 if (!decl_die)
20118 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20120 decl_die = lookup_decl_die (decl);
20121 if (decl_die)
20122 return decl_die;
20124 switch (TREE_CODE (decl))
20126 case FUNCTION_DECL:
20127 /* Clear current_function_decl, so that gen_subprogram_die thinks
20128 that this is a declaration. At this point, we just want to force
20129 declaration die. */
20130 save_fn = current_function_decl;
20131 current_function_decl = NULL_TREE;
20132 gen_subprogram_die (decl, context_die);
20133 current_function_decl = save_fn;
20134 break;
20136 case VAR_DECL:
20137 /* Set external flag to force declaration die. Restore it after
20138 gen_decl_die() call. */
20139 saved_external_flag = DECL_EXTERNAL (decl);
20140 DECL_EXTERNAL (decl) = 1;
20141 gen_decl_die (decl, NULL, context_die);
20142 DECL_EXTERNAL (decl) = saved_external_flag;
20143 break;
20145 case NAMESPACE_DECL:
20146 if (dwarf_version >= 3 || !dwarf_strict)
20147 dwarf2out_decl (decl);
20148 else
20149 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
20150 decl_die = comp_unit_die ();
20151 break;
20153 case TRANSLATION_UNIT_DECL:
20154 decl_die = comp_unit_die ();
20155 break;
20157 default:
20158 gcc_unreachable ();
20161 /* We should be able to find the DIE now. */
20162 if (!decl_die)
20163 decl_die = lookup_decl_die (decl);
20164 gcc_assert (decl_die);
20167 return decl_die;
20170 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
20171 always returned. */
20173 static dw_die_ref
20174 force_type_die (tree type)
20176 dw_die_ref type_die;
20178 type_die = lookup_type_die (type);
20179 if (!type_die)
20181 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
20183 type_die = modified_type_die (type, TYPE_READONLY (type),
20184 TYPE_VOLATILE (type), context_die);
20185 gcc_assert (type_die);
20187 return type_die;
20190 /* Force out any required namespaces to be able to output DECL,
20191 and return the new context_die for it, if it's changed. */
20193 static dw_die_ref
20194 setup_namespace_context (tree thing, dw_die_ref context_die)
20196 tree context = (DECL_P (thing)
20197 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
20198 if (context && TREE_CODE (context) == NAMESPACE_DECL)
20199 /* Force out the namespace. */
20200 context_die = force_decl_die (context);
20202 return context_die;
20205 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
20206 type) within its namespace, if appropriate.
20208 For compatibility with older debuggers, namespace DIEs only contain
20209 declarations; all definitions are emitted at CU scope. */
20211 static dw_die_ref
20212 declare_in_namespace (tree thing, dw_die_ref context_die)
20214 dw_die_ref ns_context;
20216 if (debug_info_level <= DINFO_LEVEL_TERSE)
20217 return context_die;
20219 /* If this decl is from an inlined function, then don't try to emit it in its
20220 namespace, as we will get confused. It would have already been emitted
20221 when the abstract instance of the inline function was emitted anyways. */
20222 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
20223 return context_die;
20225 ns_context = setup_namespace_context (thing, context_die);
20227 if (ns_context != context_die)
20229 if (is_fortran ())
20230 return ns_context;
20231 if (DECL_P (thing))
20232 gen_decl_die (thing, NULL, ns_context);
20233 else
20234 gen_type_die (thing, ns_context);
20236 return context_die;
20239 /* Generate a DIE for a namespace or namespace alias. */
20241 static void
20242 gen_namespace_die (tree decl, dw_die_ref context_die)
20244 dw_die_ref namespace_die;
20246 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
20247 they are an alias of. */
20248 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
20250 /* Output a real namespace or module. */
20251 context_die = setup_namespace_context (decl, comp_unit_die ());
20252 namespace_die = new_die (is_fortran ()
20253 ? DW_TAG_module : DW_TAG_namespace,
20254 context_die, decl);
20255 /* For Fortran modules defined in different CU don't add src coords. */
20256 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
20258 const char *name = dwarf2_name (decl, 0);
20259 if (name)
20260 add_name_attribute (namespace_die, name);
20262 else
20263 add_name_and_src_coords_attributes (namespace_die, decl);
20264 if (DECL_EXTERNAL (decl))
20265 add_AT_flag (namespace_die, DW_AT_declaration, 1);
20266 equate_decl_number_to_die (decl, namespace_die);
20268 else
20270 /* Output a namespace alias. */
20272 /* Force out the namespace we are an alias of, if necessary. */
20273 dw_die_ref origin_die
20274 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
20276 if (DECL_FILE_SCOPE_P (decl)
20277 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
20278 context_die = setup_namespace_context (decl, comp_unit_die ());
20279 /* Now create the namespace alias DIE. */
20280 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
20281 add_name_and_src_coords_attributes (namespace_die, decl);
20282 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
20283 equate_decl_number_to_die (decl, namespace_die);
20285 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
20286 if (want_pubnames ())
20287 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
20290 /* Generate Dwarf debug information for a decl described by DECL.
20291 The return value is currently only meaningful for PARM_DECLs,
20292 for all other decls it returns NULL. */
20294 static dw_die_ref
20295 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20297 tree decl_or_origin = decl ? decl : origin;
20298 tree class_origin = NULL, ultimate_origin;
20300 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20301 return NULL;
20303 switch (TREE_CODE (decl_or_origin))
20305 case ERROR_MARK:
20306 break;
20308 case CONST_DECL:
20309 if (!is_fortran () && !is_ada ())
20311 /* The individual enumerators of an enum type get output when we output
20312 the Dwarf representation of the relevant enum type itself. */
20313 break;
20316 /* Emit its type. */
20317 gen_type_die (TREE_TYPE (decl), context_die);
20319 /* And its containing namespace. */
20320 context_die = declare_in_namespace (decl, context_die);
20322 gen_const_die (decl, context_die);
20323 break;
20325 case FUNCTION_DECL:
20326 /* Don't output any DIEs to represent mere function declarations,
20327 unless they are class members or explicit block externs. */
20328 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20329 && DECL_FILE_SCOPE_P (decl_or_origin)
20330 && (current_function_decl == NULL_TREE
20331 || DECL_ARTIFICIAL (decl_or_origin)))
20332 break;
20334 #if 0
20335 /* FIXME */
20336 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20337 on local redeclarations of global functions. That seems broken. */
20338 if (current_function_decl != decl)
20339 /* This is only a declaration. */;
20340 #endif
20342 /* If we're emitting a clone, emit info for the abstract instance. */
20343 if (origin || DECL_ORIGIN (decl) != decl)
20344 dwarf2out_abstract_function (origin
20345 ? DECL_ORIGIN (origin)
20346 : DECL_ABSTRACT_ORIGIN (decl));
20348 /* If we're emitting an out-of-line copy of an inline function,
20349 emit info for the abstract instance and set up to refer to it. */
20350 else if (cgraph_function_possibly_inlined_p (decl)
20351 && ! DECL_ABSTRACT (decl)
20352 && ! class_or_namespace_scope_p (context_die)
20353 /* dwarf2out_abstract_function won't emit a die if this is just
20354 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
20355 that case, because that works only if we have a die. */
20356 && DECL_INITIAL (decl) != NULL_TREE)
20358 dwarf2out_abstract_function (decl);
20359 set_decl_origin_self (decl);
20362 /* Otherwise we're emitting the primary DIE for this decl. */
20363 else if (debug_info_level > DINFO_LEVEL_TERSE)
20365 /* Before we describe the FUNCTION_DECL itself, make sure that we
20366 have its containing type. */
20367 if (!origin)
20368 origin = decl_class_context (decl);
20369 if (origin != NULL_TREE)
20370 gen_type_die (origin, context_die);
20372 /* And its return type. */
20373 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20375 /* And its virtual context. */
20376 if (DECL_VINDEX (decl) != NULL_TREE)
20377 gen_type_die (DECL_CONTEXT (decl), context_die);
20379 /* Make sure we have a member DIE for decl. */
20380 if (origin != NULL_TREE)
20381 gen_type_die_for_member (origin, decl, context_die);
20383 /* And its containing namespace. */
20384 context_die = declare_in_namespace (decl, context_die);
20387 /* Now output a DIE to represent the function itself. */
20388 if (decl)
20389 gen_subprogram_die (decl, context_die);
20390 break;
20392 case TYPE_DECL:
20393 /* If we are in terse mode, don't generate any DIEs to represent any
20394 actual typedefs. */
20395 if (debug_info_level <= DINFO_LEVEL_TERSE)
20396 break;
20398 /* In the special case of a TYPE_DECL node representing the declaration
20399 of some type tag, if the given TYPE_DECL is marked as having been
20400 instantiated from some other (original) TYPE_DECL node (e.g. one which
20401 was generated within the original definition of an inline function) we
20402 used to generate a special (abbreviated) DW_TAG_structure_type,
20403 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
20404 should be actually referencing those DIEs, as variable DIEs with that
20405 type would be emitted already in the abstract origin, so it was always
20406 removed during unused type prunning. Don't add anything in this
20407 case. */
20408 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20409 break;
20411 if (is_redundant_typedef (decl))
20412 gen_type_die (TREE_TYPE (decl), context_die);
20413 else
20414 /* Output a DIE to represent the typedef itself. */
20415 gen_typedef_die (decl, context_die);
20416 break;
20418 case LABEL_DECL:
20419 if (debug_info_level >= DINFO_LEVEL_NORMAL)
20420 gen_label_die (decl, context_die);
20421 break;
20423 case VAR_DECL:
20424 case RESULT_DECL:
20425 /* If we are in terse mode, don't generate any DIEs to represent any
20426 variable declarations or definitions. */
20427 if (debug_info_level <= DINFO_LEVEL_TERSE)
20428 break;
20430 /* Output any DIEs that are needed to specify the type of this data
20431 object. */
20432 if (decl_by_reference_p (decl_or_origin))
20433 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20434 else
20435 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20437 /* And its containing type. */
20438 class_origin = decl_class_context (decl_or_origin);
20439 if (class_origin != NULL_TREE)
20440 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
20442 /* And its containing namespace. */
20443 context_die = declare_in_namespace (decl_or_origin, context_die);
20445 /* Now output the DIE to represent the data object itself. This gets
20446 complicated because of the possibility that the VAR_DECL really
20447 represents an inlined instance of a formal parameter for an inline
20448 function. */
20449 ultimate_origin = decl_ultimate_origin (decl_or_origin);
20450 if (ultimate_origin != NULL_TREE
20451 && TREE_CODE (ultimate_origin) == PARM_DECL)
20452 gen_formal_parameter_die (decl, origin,
20453 true /* Emit name attribute. */,
20454 context_die);
20455 else
20456 gen_variable_die (decl, origin, context_die);
20457 break;
20459 case FIELD_DECL:
20460 /* Ignore the nameless fields that are used to skip bits but handle C++
20461 anonymous unions and structs. */
20462 if (DECL_NAME (decl) != NULL_TREE
20463 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
20464 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
20466 gen_type_die (member_declared_type (decl), context_die);
20467 gen_field_die (decl, context_die);
20469 break;
20471 case PARM_DECL:
20472 if (DECL_BY_REFERENCE (decl_or_origin))
20473 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20474 else
20475 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20476 return gen_formal_parameter_die (decl, origin,
20477 true /* Emit name attribute. */,
20478 context_die);
20480 case NAMESPACE_DECL:
20481 case IMPORTED_DECL:
20482 if (dwarf_version >= 3 || !dwarf_strict)
20483 gen_namespace_die (decl, context_die);
20484 break;
20486 case NAMELIST_DECL:
20487 gen_namelist_decl (DECL_NAME (decl), context_die,
20488 NAMELIST_DECL_ASSOCIATED_DECL (decl));
20489 break;
20491 default:
20492 /* Probably some frontend-internal decl. Assume we don't care. */
20493 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
20494 break;
20497 return NULL;
20500 /* Output debug information for global decl DECL. Called from toplev.c after
20501 compilation proper has finished. */
20503 static void
20504 dwarf2out_global_decl (tree decl)
20506 /* Output DWARF2 information for file-scope tentative data object
20507 declarations, file-scope (extern) function declarations (which
20508 had no corresponding body) and file-scope tagged type declarations
20509 and definitions which have not yet been forced out. */
20510 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
20511 dwarf2out_decl (decl);
20514 /* Output debug information for type decl DECL. Called from toplev.c
20515 and from language front ends (to record built-in types). */
20516 static void
20517 dwarf2out_type_decl (tree decl, int local)
20519 if (!local)
20520 dwarf2out_decl (decl);
20523 /* Output debug information for imported module or decl DECL.
20524 NAME is non-NULL name in the lexical block if the decl has been renamed.
20525 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
20526 that DECL belongs to.
20527 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
20528 static void
20529 dwarf2out_imported_module_or_decl_1 (tree decl,
20530 tree name,
20531 tree lexical_block,
20532 dw_die_ref lexical_block_die)
20534 expanded_location xloc;
20535 dw_die_ref imported_die = NULL;
20536 dw_die_ref at_import_die;
20538 if (TREE_CODE (decl) == IMPORTED_DECL)
20540 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
20541 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
20542 gcc_assert (decl);
20544 else
20545 xloc = expand_location (input_location);
20547 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
20549 at_import_die = force_type_die (TREE_TYPE (decl));
20550 /* For namespace N { typedef void T; } using N::T; base_type_die
20551 returns NULL, but DW_TAG_imported_declaration requires
20552 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
20553 if (!at_import_die)
20555 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
20556 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
20557 at_import_die = lookup_type_die (TREE_TYPE (decl));
20558 gcc_assert (at_import_die);
20561 else
20563 at_import_die = lookup_decl_die (decl);
20564 if (!at_import_die)
20566 /* If we're trying to avoid duplicate debug info, we may not have
20567 emitted the member decl for this field. Emit it now. */
20568 if (TREE_CODE (decl) == FIELD_DECL)
20570 tree type = DECL_CONTEXT (decl);
20572 if (TYPE_CONTEXT (type)
20573 && TYPE_P (TYPE_CONTEXT (type))
20574 && !should_emit_struct_debug (TYPE_CONTEXT (type),
20575 DINFO_USAGE_DIR_USE))
20576 return;
20577 gen_type_die_for_member (type, decl,
20578 get_context_die (TYPE_CONTEXT (type)));
20580 if (TREE_CODE (decl) == NAMELIST_DECL)
20581 at_import_die = gen_namelist_decl (DECL_NAME (decl),
20582 get_context_die (DECL_CONTEXT (decl)),
20583 NULL_TREE);
20584 else
20585 at_import_die = force_decl_die (decl);
20589 if (TREE_CODE (decl) == NAMESPACE_DECL)
20591 if (dwarf_version >= 3 || !dwarf_strict)
20592 imported_die = new_die (DW_TAG_imported_module,
20593 lexical_block_die,
20594 lexical_block);
20595 else
20596 return;
20598 else
20599 imported_die = new_die (DW_TAG_imported_declaration,
20600 lexical_block_die,
20601 lexical_block);
20603 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
20604 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
20605 if (name)
20606 add_AT_string (imported_die, DW_AT_name,
20607 IDENTIFIER_POINTER (name));
20608 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
20611 /* Output debug information for imported module or decl DECL.
20612 NAME is non-NULL name in context if the decl has been renamed.
20613 CHILD is true if decl is one of the renamed decls as part of
20614 importing whole module. */
20616 static void
20617 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
20618 bool child)
20620 /* dw_die_ref at_import_die; */
20621 dw_die_ref scope_die;
20623 if (debug_info_level <= DINFO_LEVEL_TERSE)
20624 return;
20626 gcc_assert (decl);
20628 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
20629 We need decl DIE for reference and scope die. First, get DIE for the decl
20630 itself. */
20632 /* Get the scope die for decl context. Use comp_unit_die for global module
20633 or decl. If die is not found for non globals, force new die. */
20634 if (context
20635 && TYPE_P (context)
20636 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
20637 return;
20639 if (!(dwarf_version >= 3 || !dwarf_strict))
20640 return;
20642 scope_die = get_context_die (context);
20644 if (child)
20646 gcc_assert (scope_die->die_child);
20647 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
20648 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
20649 scope_die = scope_die->die_child;
20652 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
20653 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
20657 /* Output debug information for namelists. */
20659 static dw_die_ref
20660 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
20662 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
20663 tree value;
20664 unsigned i;
20666 if (debug_info_level <= DINFO_LEVEL_TERSE)
20667 return NULL;
20669 gcc_assert (scope_die != NULL);
20670 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
20671 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
20673 /* If there are no item_decls, we have a nondefining namelist, e.g.
20674 with USE association; hence, set DW_AT_declaration. */
20675 if (item_decls == NULL_TREE)
20677 add_AT_flag (nml_die, DW_AT_declaration, 1);
20678 return nml_die;
20681 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
20683 nml_item_ref_die = lookup_decl_die (value);
20684 if (!nml_item_ref_die)
20685 nml_item_ref_die = force_decl_die (value);
20687 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
20688 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
20690 return nml_die;
20694 /* Write the debugging output for DECL. */
20696 void
20697 dwarf2out_decl (tree decl)
20699 dw_die_ref context_die = comp_unit_die ();
20701 switch (TREE_CODE (decl))
20703 case ERROR_MARK:
20704 return;
20706 case FUNCTION_DECL:
20707 /* What we would really like to do here is to filter out all mere
20708 file-scope declarations of file-scope functions which are never
20709 referenced later within this translation unit (and keep all of ones
20710 that *are* referenced later on) but we aren't clairvoyant, so we have
20711 no idea which functions will be referenced in the future (i.e. later
20712 on within the current translation unit). So here we just ignore all
20713 file-scope function declarations which are not also definitions. If
20714 and when the debugger needs to know something about these functions,
20715 it will have to hunt around and find the DWARF information associated
20716 with the definition of the function.
20718 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
20719 nodes represent definitions and which ones represent mere
20720 declarations. We have to check DECL_INITIAL instead. That's because
20721 the C front-end supports some weird semantics for "extern inline"
20722 function definitions. These can get inlined within the current
20723 translation unit (and thus, we need to generate Dwarf info for their
20724 abstract instances so that the Dwarf info for the concrete inlined
20725 instances can have something to refer to) but the compiler never
20726 generates any out-of-lines instances of such things (despite the fact
20727 that they *are* definitions).
20729 The important point is that the C front-end marks these "extern
20730 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
20731 them anyway. Note that the C++ front-end also plays some similar games
20732 for inline function definitions appearing within include files which
20733 also contain `#pragma interface' pragmas.
20735 If we are called from dwarf2out_abstract_function output a DIE
20736 anyway. We can end up here this way with early inlining and LTO
20737 where the inlined function is output in a different LTRANS unit
20738 or not at all. */
20739 if (DECL_INITIAL (decl) == NULL_TREE
20740 && ! DECL_ABSTRACT (decl))
20741 return;
20743 /* If we're a nested function, initially use a parent of NULL; if we're
20744 a plain function, this will be fixed up in decls_for_scope. If
20745 we're a method, it will be ignored, since we already have a DIE. */
20746 if (decl_function_context (decl)
20747 /* But if we're in terse mode, we don't care about scope. */
20748 && debug_info_level > DINFO_LEVEL_TERSE)
20749 context_die = NULL;
20750 break;
20752 case VAR_DECL:
20753 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
20754 declaration and if the declaration was never even referenced from
20755 within this entire compilation unit. We suppress these DIEs in
20756 order to save space in the .debug section (by eliminating entries
20757 which are probably useless). Note that we must not suppress
20758 block-local extern declarations (whether used or not) because that
20759 would screw-up the debugger's name lookup mechanism and cause it to
20760 miss things which really ought to be in scope at a given point. */
20761 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
20762 return;
20764 /* For local statics lookup proper context die. */
20765 if (TREE_STATIC (decl)
20766 && DECL_CONTEXT (decl)
20767 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
20768 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20770 /* If we are in terse mode, don't generate any DIEs to represent any
20771 variable declarations or definitions. */
20772 if (debug_info_level <= DINFO_LEVEL_TERSE)
20773 return;
20774 break;
20776 case CONST_DECL:
20777 if (debug_info_level <= DINFO_LEVEL_TERSE)
20778 return;
20779 if (!is_fortran () && !is_ada ())
20780 return;
20781 if (TREE_STATIC (decl) && decl_function_context (decl))
20782 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20783 break;
20785 case NAMESPACE_DECL:
20786 case IMPORTED_DECL:
20787 if (debug_info_level <= DINFO_LEVEL_TERSE)
20788 return;
20789 if (lookup_decl_die (decl) != NULL)
20790 return;
20791 break;
20793 case TYPE_DECL:
20794 /* Don't emit stubs for types unless they are needed by other DIEs. */
20795 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
20796 return;
20798 /* Don't bother trying to generate any DIEs to represent any of the
20799 normal built-in types for the language we are compiling. */
20800 if (DECL_IS_BUILTIN (decl))
20801 return;
20803 /* If we are in terse mode, don't generate any DIEs for types. */
20804 if (debug_info_level <= DINFO_LEVEL_TERSE)
20805 return;
20807 /* If we're a function-scope tag, initially use a parent of NULL;
20808 this will be fixed up in decls_for_scope. */
20809 if (decl_function_context (decl))
20810 context_die = NULL;
20812 break;
20814 case NAMELIST_DECL:
20815 break;
20817 default:
20818 return;
20821 gen_decl_die (decl, NULL, context_die);
20824 /* Write the debugging output for DECL. */
20826 static void
20827 dwarf2out_function_decl (tree decl)
20829 dwarf2out_decl (decl);
20830 call_arg_locations = NULL;
20831 call_arg_loc_last = NULL;
20832 call_site_count = -1;
20833 tail_call_site_count = -1;
20834 block_map.release ();
20835 htab_empty (decl_loc_table);
20836 htab_empty (cached_dw_loc_list_table);
20839 /* Output a marker (i.e. a label) for the beginning of the generated code for
20840 a lexical block. */
20842 static void
20843 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
20844 unsigned int blocknum)
20846 switch_to_section (current_function_section ());
20847 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
20850 /* Output a marker (i.e. a label) for the end of the generated code for a
20851 lexical block. */
20853 static void
20854 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
20856 switch_to_section (current_function_section ());
20857 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
20860 /* Returns nonzero if it is appropriate not to emit any debugging
20861 information for BLOCK, because it doesn't contain any instructions.
20863 Don't allow this for blocks with nested functions or local classes
20864 as we would end up with orphans, and in the presence of scheduling
20865 we may end up calling them anyway. */
20867 static bool
20868 dwarf2out_ignore_block (const_tree block)
20870 tree decl;
20871 unsigned int i;
20873 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
20874 if (TREE_CODE (decl) == FUNCTION_DECL
20875 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20876 return 0;
20877 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
20879 decl = BLOCK_NONLOCALIZED_VAR (block, i);
20880 if (TREE_CODE (decl) == FUNCTION_DECL
20881 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20882 return 0;
20885 return 1;
20888 /* Hash table routines for file_hash. */
20890 static int
20891 file_table_eq (const void *p1_p, const void *p2_p)
20893 const struct dwarf_file_data *const p1 =
20894 (const struct dwarf_file_data *) p1_p;
20895 const char *const p2 = (const char *) p2_p;
20896 return filename_cmp (p1->filename, p2) == 0;
20899 static hashval_t
20900 file_table_hash (const void *p_p)
20902 const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
20903 return htab_hash_string (p->filename);
20906 /* Lookup FILE_NAME (in the list of filenames that we know about here in
20907 dwarf2out.c) and return its "index". The index of each (known) filename is
20908 just a unique number which is associated with only that one filename. We
20909 need such numbers for the sake of generating labels (in the .debug_sfnames
20910 section) and references to those files numbers (in the .debug_srcinfo
20911 and.debug_macinfo sections). If the filename given as an argument is not
20912 found in our current list, add it to the list and assign it the next
20913 available unique index number. In order to speed up searches, we remember
20914 the index of the filename was looked up last. This handles the majority of
20915 all searches. */
20917 static struct dwarf_file_data *
20918 lookup_filename (const char *file_name)
20920 void ** slot;
20921 struct dwarf_file_data * created;
20923 /* Check to see if the file name that was searched on the previous
20924 call matches this file name. If so, return the index. */
20925 if (file_table_last_lookup
20926 && (file_name == file_table_last_lookup->filename
20927 || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
20928 return file_table_last_lookup;
20930 /* Didn't match the previous lookup, search the table. */
20931 slot = htab_find_slot_with_hash (file_table, file_name,
20932 htab_hash_string (file_name), INSERT);
20933 if (*slot)
20934 return (struct dwarf_file_data *) *slot;
20936 created = ggc_alloc_dwarf_file_data ();
20937 created->filename = file_name;
20938 created->emitted_number = 0;
20939 *slot = created;
20940 return created;
20943 /* If the assembler will construct the file table, then translate the compiler
20944 internal file table number into the assembler file table number, and emit
20945 a .file directive if we haven't already emitted one yet. The file table
20946 numbers are different because we prune debug info for unused variables and
20947 types, which may include filenames. */
20949 static int
20950 maybe_emit_file (struct dwarf_file_data * fd)
20952 if (! fd->emitted_number)
20954 if (last_emitted_file)
20955 fd->emitted_number = last_emitted_file->emitted_number + 1;
20956 else
20957 fd->emitted_number = 1;
20958 last_emitted_file = fd;
20960 if (DWARF2_ASM_LINE_DEBUG_INFO)
20962 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
20963 output_quoted_string (asm_out_file,
20964 remap_debug_filename (fd->filename));
20965 fputc ('\n', asm_out_file);
20969 return fd->emitted_number;
20972 /* Schedule generation of a DW_AT_const_value attribute to DIE.
20973 That generation should happen after function debug info has been
20974 generated. The value of the attribute is the constant value of ARG. */
20976 static void
20977 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
20979 die_arg_entry entry;
20981 if (!die || !arg)
20982 return;
20984 if (!tmpl_value_parm_die_table)
20985 vec_alloc (tmpl_value_parm_die_table, 32);
20987 entry.die = die;
20988 entry.arg = arg;
20989 vec_safe_push (tmpl_value_parm_die_table, entry);
20992 /* Return TRUE if T is an instance of generic type, FALSE
20993 otherwise. */
20995 static bool
20996 generic_type_p (tree t)
20998 if (t == NULL_TREE || !TYPE_P (t))
20999 return false;
21000 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
21003 /* Schedule the generation of the generic parameter dies for the
21004 instance of generic type T. The proper generation itself is later
21005 done by gen_scheduled_generic_parms_dies. */
21007 static void
21008 schedule_generic_params_dies_gen (tree t)
21010 if (!generic_type_p (t))
21011 return;
21013 if (!generic_type_instances)
21014 vec_alloc (generic_type_instances, 256);
21016 vec_safe_push (generic_type_instances, t);
21019 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
21020 by append_entry_to_tmpl_value_parm_die_table. This function must
21021 be called after function DIEs have been generated. */
21023 static void
21024 gen_remaining_tmpl_value_param_die_attribute (void)
21026 if (tmpl_value_parm_die_table)
21028 unsigned i;
21029 die_arg_entry *e;
21031 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
21032 tree_add_const_value_attribute (e->die, e->arg);
21036 /* Generate generic parameters DIEs for instances of generic types
21037 that have been previously scheduled by
21038 schedule_generic_params_dies_gen. This function must be called
21039 after all the types of the CU have been laid out. */
21041 static void
21042 gen_scheduled_generic_parms_dies (void)
21044 unsigned i;
21045 tree t;
21047 if (!generic_type_instances)
21048 return;
21050 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
21051 if (COMPLETE_TYPE_P (t))
21052 gen_generic_params_dies (t);
21056 /* Replace DW_AT_name for the decl with name. */
21058 static void
21059 dwarf2out_set_name (tree decl, tree name)
21061 dw_die_ref die;
21062 dw_attr_ref attr;
21063 const char *dname;
21065 die = TYPE_SYMTAB_DIE (decl);
21066 if (!die)
21067 return;
21069 dname = dwarf2_name (name, 0);
21070 if (!dname)
21071 return;
21073 attr = get_AT (die, DW_AT_name);
21074 if (attr)
21076 struct indirect_string_node *node;
21078 node = find_AT_string (dname);
21079 /* replace the string. */
21080 attr->dw_attr_val.v.val_str = node;
21083 else
21084 add_name_attribute (die, dname);
21087 /* True if before or during processing of the first function being emitted. */
21088 static bool in_first_function_p = true;
21089 /* True if loc_note during dwarf2out_var_location call might still be
21090 before first real instruction at address equal to .Ltext0. */
21091 static bool maybe_at_text_label_p = true;
21092 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
21093 static unsigned int first_loclabel_num_not_at_text_label;
21095 /* Called by the final INSN scan whenever we see a var location. We
21096 use it to drop labels in the right places, and throw the location in
21097 our lookup table. */
21099 static void
21100 dwarf2out_var_location (rtx loc_note)
21102 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21103 struct var_loc_node *newloc;
21104 rtx next_real, next_note;
21105 static const char *last_label;
21106 static const char *last_postcall_label;
21107 static bool last_in_cold_section_p;
21108 static rtx expected_next_loc_note;
21109 tree decl;
21110 bool var_loc_p;
21112 if (!NOTE_P (loc_note))
21114 if (CALL_P (loc_note))
21116 call_site_count++;
21117 if (SIBLING_CALL_P (loc_note))
21118 tail_call_site_count++;
21120 return;
21123 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
21124 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21125 return;
21127 /* Optimize processing a large consecutive sequence of location
21128 notes so we don't spend too much time in next_real_insn. If the
21129 next insn is another location note, remember the next_real_insn
21130 calculation for next time. */
21131 next_real = cached_next_real_insn;
21132 if (next_real)
21134 if (expected_next_loc_note != loc_note)
21135 next_real = NULL_RTX;
21138 next_note = NEXT_INSN (loc_note);
21139 if (! next_note
21140 || INSN_DELETED_P (next_note)
21141 || ! NOTE_P (next_note)
21142 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
21143 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
21144 next_note = NULL_RTX;
21146 if (! next_real)
21147 next_real = next_real_insn (loc_note);
21149 if (next_note)
21151 expected_next_loc_note = next_note;
21152 cached_next_real_insn = next_real;
21154 else
21155 cached_next_real_insn = NULL_RTX;
21157 /* If there are no instructions which would be affected by this note,
21158 don't do anything. */
21159 if (var_loc_p
21160 && next_real == NULL_RTX
21161 && !NOTE_DURING_CALL_P (loc_note))
21162 return;
21164 if (next_real == NULL_RTX)
21165 next_real = get_last_insn ();
21167 /* If there were any real insns between note we processed last time
21168 and this note (or if it is the first note), clear
21169 last_{,postcall_}label so that they are not reused this time. */
21170 if (last_var_location_insn == NULL_RTX
21171 || last_var_location_insn != next_real
21172 || last_in_cold_section_p != in_cold_section_p)
21174 last_label = NULL;
21175 last_postcall_label = NULL;
21178 if (var_loc_p)
21180 decl = NOTE_VAR_LOCATION_DECL (loc_note);
21181 newloc = add_var_loc_to_decl (decl, loc_note,
21182 NOTE_DURING_CALL_P (loc_note)
21183 ? last_postcall_label : last_label);
21184 if (newloc == NULL)
21185 return;
21187 else
21189 decl = NULL_TREE;
21190 newloc = NULL;
21193 /* If there were no real insns between note we processed last time
21194 and this note, use the label we emitted last time. Otherwise
21195 create a new label and emit it. */
21196 if (last_label == NULL)
21198 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21199 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21200 loclabel_num++;
21201 last_label = ggc_strdup (loclabel);
21202 /* See if loclabel might be equal to .Ltext0. If yes,
21203 bump first_loclabel_num_not_at_text_label. */
21204 if (!have_multiple_function_sections
21205 && in_first_function_p
21206 && maybe_at_text_label_p)
21208 static rtx last_start;
21209 rtx insn;
21210 for (insn = loc_note; insn; insn = previous_insn (insn))
21211 if (insn == last_start)
21212 break;
21213 else if (!NONDEBUG_INSN_P (insn))
21214 continue;
21215 else
21217 rtx body = PATTERN (insn);
21218 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
21219 continue;
21220 /* Inline asm could occupy zero bytes. */
21221 else if (GET_CODE (body) == ASM_INPUT
21222 || asm_noperands (body) >= 0)
21223 continue;
21224 #ifdef HAVE_attr_length
21225 else if (get_attr_min_length (insn) == 0)
21226 continue;
21227 #endif
21228 else
21230 /* Assume insn has non-zero length. */
21231 maybe_at_text_label_p = false;
21232 break;
21235 if (maybe_at_text_label_p)
21237 last_start = loc_note;
21238 first_loclabel_num_not_at_text_label = loclabel_num;
21243 if (!var_loc_p)
21245 struct call_arg_loc_node *ca_loc
21246 = ggc_alloc_cleared_call_arg_loc_node ();
21247 rtx prev = prev_real_insn (loc_note), x;
21248 ca_loc->call_arg_loc_note = loc_note;
21249 ca_loc->next = NULL;
21250 ca_loc->label = last_label;
21251 gcc_assert (prev
21252 && (CALL_P (prev)
21253 || (NONJUMP_INSN_P (prev)
21254 && GET_CODE (PATTERN (prev)) == SEQUENCE
21255 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
21256 if (!CALL_P (prev))
21257 prev = XVECEXP (PATTERN (prev), 0, 0);
21258 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
21259 x = get_call_rtx_from (PATTERN (prev));
21260 if (x)
21262 x = XEXP (XEXP (x, 0), 0);
21263 if (GET_CODE (x) == SYMBOL_REF
21264 && SYMBOL_REF_DECL (x)
21265 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
21266 ca_loc->symbol_ref = x;
21268 ca_loc->block = insn_scope (prev);
21269 if (call_arg_locations)
21270 call_arg_loc_last->next = ca_loc;
21271 else
21272 call_arg_locations = ca_loc;
21273 call_arg_loc_last = ca_loc;
21275 else if (!NOTE_DURING_CALL_P (loc_note))
21276 newloc->label = last_label;
21277 else
21279 if (!last_postcall_label)
21281 sprintf (loclabel, "%s-1", last_label);
21282 last_postcall_label = ggc_strdup (loclabel);
21284 newloc->label = last_postcall_label;
21287 last_var_location_insn = next_real;
21288 last_in_cold_section_p = in_cold_section_p;
21291 /* Note in one location list that text section has changed. */
21293 static int
21294 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
21296 var_loc_list *list = (var_loc_list *) *slot;
21297 if (list->first)
21298 list->last_before_switch
21299 = list->last->next ? list->last->next : list->last;
21300 return 1;
21303 /* Note in all location lists that text section has changed. */
21305 static void
21306 var_location_switch_text_section (void)
21308 if (decl_loc_table == NULL)
21309 return;
21311 htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
21314 /* Create a new line number table. */
21316 static dw_line_info_table *
21317 new_line_info_table (void)
21319 dw_line_info_table *table;
21321 table = ggc_alloc_cleared_dw_line_info_table_struct ();
21322 table->file_num = 1;
21323 table->line_num = 1;
21324 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
21326 return table;
21329 /* Lookup the "current" table into which we emit line info, so
21330 that we don't have to do it for every source line. */
21332 static void
21333 set_cur_line_info_table (section *sec)
21335 dw_line_info_table *table;
21337 if (sec == text_section)
21338 table = text_section_line_info;
21339 else if (sec == cold_text_section)
21341 table = cold_text_section_line_info;
21342 if (!table)
21344 cold_text_section_line_info = table = new_line_info_table ();
21345 table->end_label = cold_end_label;
21348 else
21350 const char *end_label;
21352 if (flag_reorder_blocks_and_partition)
21354 if (in_cold_section_p)
21355 end_label = crtl->subsections.cold_section_end_label;
21356 else
21357 end_label = crtl->subsections.hot_section_end_label;
21359 else
21361 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21362 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
21363 current_function_funcdef_no);
21364 end_label = ggc_strdup (label);
21367 table = new_line_info_table ();
21368 table->end_label = end_label;
21370 vec_safe_push (separate_line_info, table);
21373 if (DWARF2_ASM_LINE_DEBUG_INFO)
21374 table->is_stmt = (cur_line_info_table
21375 ? cur_line_info_table->is_stmt
21376 : DWARF_LINE_DEFAULT_IS_STMT_START);
21377 cur_line_info_table = table;
21381 /* We need to reset the locations at the beginning of each
21382 function. We can't do this in the end_function hook, because the
21383 declarations that use the locations won't have been output when
21384 that hook is called. Also compute have_multiple_function_sections here. */
21386 static void
21387 dwarf2out_begin_function (tree fun)
21389 section *sec = function_section (fun);
21391 if (sec != text_section)
21392 have_multiple_function_sections = true;
21394 if (flag_reorder_blocks_and_partition && !cold_text_section)
21396 gcc_assert (current_function_decl == fun);
21397 cold_text_section = unlikely_text_section ();
21398 switch_to_section (cold_text_section);
21399 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21400 switch_to_section (sec);
21403 dwarf2out_note_section_used ();
21404 call_site_count = 0;
21405 tail_call_site_count = 0;
21407 set_cur_line_info_table (sec);
21410 /* Helper function of dwarf2out_end_function, called only after emitting
21411 the very first function into assembly. Check if some .debug_loc range
21412 might end with a .LVL* label that could be equal to .Ltext0.
21413 In that case we must force using absolute addresses in .debug_loc ranges,
21414 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
21415 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
21416 list terminator.
21417 Set have_multiple_function_sections to true in that case and
21418 terminate htab traversal. */
21420 static int
21421 find_empty_loc_ranges_at_text_label (void **slot, void *)
21423 var_loc_list *entry;
21424 struct var_loc_node *node;
21426 entry = (var_loc_list *) *slot;
21427 node = entry->first;
21428 if (node && node->next && node->next->label)
21430 unsigned int i;
21431 const char *label = node->next->label;
21432 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
21434 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
21436 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
21437 if (strcmp (label, loclabel) == 0)
21439 have_multiple_function_sections = true;
21440 return 0;
21444 return 1;
21447 /* Hook called after emitting a function into assembly.
21448 This does something only for the very first function emitted. */
21450 static void
21451 dwarf2out_end_function (unsigned int)
21453 if (in_first_function_p
21454 && !have_multiple_function_sections
21455 && first_loclabel_num_not_at_text_label
21456 && decl_loc_table)
21457 htab_traverse (decl_loc_table, find_empty_loc_ranges_at_text_label,
21458 NULL);
21459 in_first_function_p = false;
21460 maybe_at_text_label_p = false;
21463 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
21465 static void
21466 push_dw_line_info_entry (dw_line_info_table *table,
21467 enum dw_line_info_opcode opcode, unsigned int val)
21469 dw_line_info_entry e;
21470 e.opcode = opcode;
21471 e.val = val;
21472 vec_safe_push (table->entries, e);
21475 /* Output a label to mark the beginning of a source code line entry
21476 and record information relating to this source line, in
21477 'line_info_table' for later output of the .debug_line section. */
21478 /* ??? The discriminator parameter ought to be unsigned. */
21480 static void
21481 dwarf2out_source_line (unsigned int line, const char *filename,
21482 int discriminator, bool is_stmt)
21484 unsigned int file_num;
21485 dw_line_info_table *table;
21487 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
21488 return;
21490 /* The discriminator column was added in dwarf4. Simplify the below
21491 by simply removing it if we're not supposed to output it. */
21492 if (dwarf_version < 4 && dwarf_strict)
21493 discriminator = 0;
21495 table = cur_line_info_table;
21496 file_num = maybe_emit_file (lookup_filename (filename));
21498 /* ??? TODO: Elide duplicate line number entries. Traditionally,
21499 the debugger has used the second (possibly duplicate) line number
21500 at the beginning of the function to mark the end of the prologue.
21501 We could eliminate any other duplicates within the function. For
21502 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
21503 that second line number entry. */
21504 /* Recall that this end-of-prologue indication is *not* the same thing
21505 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
21506 to which the hook corresponds, follows the last insn that was
21507 emitted by gen_prologue. What we need is to precede the first insn
21508 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
21509 insn that corresponds to something the user wrote. These may be
21510 very different locations once scheduling is enabled. */
21512 if (0 && file_num == table->file_num
21513 && line == table->line_num
21514 && discriminator == table->discrim_num
21515 && is_stmt == table->is_stmt)
21516 return;
21518 switch_to_section (current_function_section ());
21520 /* If requested, emit something human-readable. */
21521 if (flag_debug_asm)
21522 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
21524 if (DWARF2_ASM_LINE_DEBUG_INFO)
21526 /* Emit the .loc directive understood by GNU as. */
21527 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
21528 file_num, line, is_stmt, discriminator */
21529 fputs ("\t.loc ", asm_out_file);
21530 fprint_ul (asm_out_file, file_num);
21531 putc (' ', asm_out_file);
21532 fprint_ul (asm_out_file, line);
21533 putc (' ', asm_out_file);
21534 putc ('0', asm_out_file);
21536 if (is_stmt != table->is_stmt)
21538 fputs (" is_stmt ", asm_out_file);
21539 putc (is_stmt ? '1' : '0', asm_out_file);
21541 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
21543 gcc_assert (discriminator > 0);
21544 fputs (" discriminator ", asm_out_file);
21545 fprint_ul (asm_out_file, (unsigned long) discriminator);
21547 putc ('\n', asm_out_file);
21549 else
21551 unsigned int label_num = ++line_info_label_num;
21553 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
21555 push_dw_line_info_entry (table, LI_set_address, label_num);
21556 if (file_num != table->file_num)
21557 push_dw_line_info_entry (table, LI_set_file, file_num);
21558 if (discriminator != table->discrim_num)
21559 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
21560 if (is_stmt != table->is_stmt)
21561 push_dw_line_info_entry (table, LI_negate_stmt, 0);
21562 push_dw_line_info_entry (table, LI_set_line, line);
21565 table->file_num = file_num;
21566 table->line_num = line;
21567 table->discrim_num = discriminator;
21568 table->is_stmt = is_stmt;
21569 table->in_use = true;
21572 /* Record the beginning of a new source file. */
21574 static void
21575 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
21577 if (flag_eliminate_dwarf2_dups)
21579 /* Record the beginning of the file for break_out_includes. */
21580 dw_die_ref bincl_die;
21582 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
21583 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
21586 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21588 macinfo_entry e;
21589 e.code = DW_MACINFO_start_file;
21590 e.lineno = lineno;
21591 e.info = ggc_strdup (filename);
21592 vec_safe_push (macinfo_table, e);
21596 /* Record the end of a source file. */
21598 static void
21599 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
21601 if (flag_eliminate_dwarf2_dups)
21602 /* Record the end of the file for break_out_includes. */
21603 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
21605 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21607 macinfo_entry e;
21608 e.code = DW_MACINFO_end_file;
21609 e.lineno = lineno;
21610 e.info = NULL;
21611 vec_safe_push (macinfo_table, e);
21615 /* Called from debug_define in toplev.c. The `buffer' parameter contains
21616 the tail part of the directive line, i.e. the part which is past the
21617 initial whitespace, #, whitespace, directive-name, whitespace part. */
21619 static void
21620 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
21621 const char *buffer ATTRIBUTE_UNUSED)
21623 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21625 macinfo_entry e;
21626 /* Insert a dummy first entry to be able to optimize the whole
21627 predefined macro block using DW_MACRO_GNU_transparent_include. */
21628 if (macinfo_table->is_empty () && lineno <= 1)
21630 e.code = 0;
21631 e.lineno = 0;
21632 e.info = NULL;
21633 vec_safe_push (macinfo_table, e);
21635 e.code = DW_MACINFO_define;
21636 e.lineno = lineno;
21637 e.info = ggc_strdup (buffer);
21638 vec_safe_push (macinfo_table, e);
21642 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
21643 the tail part of the directive line, i.e. the part which is past the
21644 initial whitespace, #, whitespace, directive-name, whitespace part. */
21646 static void
21647 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
21648 const char *buffer ATTRIBUTE_UNUSED)
21650 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21652 macinfo_entry e;
21653 /* Insert a dummy first entry to be able to optimize the whole
21654 predefined macro block using DW_MACRO_GNU_transparent_include. */
21655 if (macinfo_table->is_empty () && lineno <= 1)
21657 e.code = 0;
21658 e.lineno = 0;
21659 e.info = NULL;
21660 vec_safe_push (macinfo_table, e);
21662 e.code = DW_MACINFO_undef;
21663 e.lineno = lineno;
21664 e.info = ggc_strdup (buffer);
21665 vec_safe_push (macinfo_table, e);
21669 /* Helpers to manipulate hash table of CUs. */
21671 struct macinfo_entry_hasher : typed_noop_remove <macinfo_entry>
21673 typedef macinfo_entry value_type;
21674 typedef macinfo_entry compare_type;
21675 static inline hashval_t hash (const value_type *);
21676 static inline bool equal (const value_type *, const compare_type *);
21679 inline hashval_t
21680 macinfo_entry_hasher::hash (const value_type *entry)
21682 return htab_hash_string (entry->info);
21685 inline bool
21686 macinfo_entry_hasher::equal (const value_type *entry1,
21687 const compare_type *entry2)
21689 return !strcmp (entry1->info, entry2->info);
21692 typedef hash_table <macinfo_entry_hasher> macinfo_hash_type;
21694 /* Output a single .debug_macinfo entry. */
21696 static void
21697 output_macinfo_op (macinfo_entry *ref)
21699 int file_num;
21700 size_t len;
21701 struct indirect_string_node *node;
21702 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21703 struct dwarf_file_data *fd;
21705 switch (ref->code)
21707 case DW_MACINFO_start_file:
21708 fd = lookup_filename (ref->info);
21709 file_num = maybe_emit_file (fd);
21710 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
21711 dw2_asm_output_data_uleb128 (ref->lineno,
21712 "Included from line number %lu",
21713 (unsigned long) ref->lineno);
21714 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
21715 break;
21716 case DW_MACINFO_end_file:
21717 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
21718 break;
21719 case DW_MACINFO_define:
21720 case DW_MACINFO_undef:
21721 len = strlen (ref->info) + 1;
21722 if (!dwarf_strict
21723 && len > DWARF_OFFSET_SIZE
21724 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21725 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21727 ref->code = ref->code == DW_MACINFO_define
21728 ? DW_MACRO_GNU_define_indirect
21729 : DW_MACRO_GNU_undef_indirect;
21730 output_macinfo_op (ref);
21731 return;
21733 dw2_asm_output_data (1, ref->code,
21734 ref->code == DW_MACINFO_define
21735 ? "Define macro" : "Undefine macro");
21736 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21737 (unsigned long) ref->lineno);
21738 dw2_asm_output_nstring (ref->info, -1, "The macro");
21739 break;
21740 case DW_MACRO_GNU_define_indirect:
21741 case DW_MACRO_GNU_undef_indirect:
21742 node = find_AT_string (ref->info);
21743 gcc_assert (node
21744 && ((node->form == DW_FORM_strp)
21745 || (node->form == DW_FORM_GNU_str_index)));
21746 dw2_asm_output_data (1, ref->code,
21747 ref->code == DW_MACRO_GNU_define_indirect
21748 ? "Define macro indirect"
21749 : "Undefine macro indirect");
21750 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21751 (unsigned long) ref->lineno);
21752 if (node->form == DW_FORM_strp)
21753 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
21754 debug_str_section, "The macro: \"%s\"",
21755 ref->info);
21756 else
21757 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
21758 ref->info);
21759 break;
21760 case DW_MACRO_GNU_transparent_include:
21761 dw2_asm_output_data (1, ref->code, "Transparent include");
21762 ASM_GENERATE_INTERNAL_LABEL (label,
21763 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
21764 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
21765 break;
21766 default:
21767 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
21768 ASM_COMMENT_START, (unsigned long) ref->code);
21769 break;
21773 /* Attempt to make a sequence of define/undef macinfo ops shareable with
21774 other compilation unit .debug_macinfo sections. IDX is the first
21775 index of a define/undef, return the number of ops that should be
21776 emitted in a comdat .debug_macinfo section and emit
21777 a DW_MACRO_GNU_transparent_include entry referencing it.
21778 If the define/undef entry should be emitted normally, return 0. */
21780 static unsigned
21781 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
21782 macinfo_hash_type *macinfo_htab)
21784 macinfo_entry *first, *second, *cur, *inc;
21785 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
21786 unsigned char checksum[16];
21787 struct md5_ctx ctx;
21788 char *grp_name, *tail;
21789 const char *base;
21790 unsigned int i, count, encoded_filename_len, linebuf_len;
21791 macinfo_entry **slot;
21793 first = &(*macinfo_table)[idx];
21794 second = &(*macinfo_table)[idx + 1];
21796 /* Optimize only if there are at least two consecutive define/undef ops,
21797 and either all of them are before first DW_MACINFO_start_file
21798 with lineno {0,1} (i.e. predefined macro block), or all of them are
21799 in some included header file. */
21800 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
21801 return 0;
21802 if (vec_safe_is_empty (files))
21804 if (first->lineno > 1 || second->lineno > 1)
21805 return 0;
21807 else if (first->lineno == 0)
21808 return 0;
21810 /* Find the last define/undef entry that can be grouped together
21811 with first and at the same time compute md5 checksum of their
21812 codes, linenumbers and strings. */
21813 md5_init_ctx (&ctx);
21814 for (i = idx; macinfo_table->iterate (i, &cur); i++)
21815 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
21816 break;
21817 else if (vec_safe_is_empty (files) && cur->lineno > 1)
21818 break;
21819 else
21821 unsigned char code = cur->code;
21822 md5_process_bytes (&code, 1, &ctx);
21823 checksum_uleb128 (cur->lineno, &ctx);
21824 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
21826 md5_finish_ctx (&ctx, checksum);
21827 count = i - idx;
21829 /* From the containing include filename (if any) pick up just
21830 usable characters from its basename. */
21831 if (vec_safe_is_empty (files))
21832 base = "";
21833 else
21834 base = lbasename (files->last ().info);
21835 for (encoded_filename_len = 0, i = 0; base[i]; i++)
21836 if (ISIDNUM (base[i]) || base[i] == '.')
21837 encoded_filename_len++;
21838 /* Count . at the end. */
21839 if (encoded_filename_len)
21840 encoded_filename_len++;
21842 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
21843 linebuf_len = strlen (linebuf);
21845 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
21846 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
21847 + 16 * 2 + 1);
21848 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
21849 tail = grp_name + 4;
21850 if (encoded_filename_len)
21852 for (i = 0; base[i]; i++)
21853 if (ISIDNUM (base[i]) || base[i] == '.')
21854 *tail++ = base[i];
21855 *tail++ = '.';
21857 memcpy (tail, linebuf, linebuf_len);
21858 tail += linebuf_len;
21859 *tail++ = '.';
21860 for (i = 0; i < 16; i++)
21861 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
21863 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
21864 in the empty vector entry before the first define/undef. */
21865 inc = &(*macinfo_table)[idx - 1];
21866 inc->code = DW_MACRO_GNU_transparent_include;
21867 inc->lineno = 0;
21868 inc->info = ggc_strdup (grp_name);
21869 if (!macinfo_htab->is_created ())
21870 macinfo_htab->create (10);
21871 /* Avoid emitting duplicates. */
21872 slot = macinfo_htab->find_slot (inc, INSERT);
21873 if (*slot != NULL)
21875 inc->code = 0;
21876 inc->info = NULL;
21877 /* If such an entry has been used before, just emit
21878 a DW_MACRO_GNU_transparent_include op. */
21879 inc = *slot;
21880 output_macinfo_op (inc);
21881 /* And clear all macinfo_entry in the range to avoid emitting them
21882 in the second pass. */
21883 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
21885 cur->code = 0;
21886 cur->info = NULL;
21889 else
21891 *slot = inc;
21892 inc->lineno = macinfo_htab->elements ();
21893 output_macinfo_op (inc);
21895 return count;
21898 /* Save any strings needed by the macinfo table in the debug str
21899 table. All strings must be collected into the table by the time
21900 index_string is called. */
21902 static void
21903 save_macinfo_strings (void)
21905 unsigned len;
21906 unsigned i;
21907 macinfo_entry *ref;
21909 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
21911 switch (ref->code)
21913 /* Match the logic in output_macinfo_op to decide on
21914 indirect strings. */
21915 case DW_MACINFO_define:
21916 case DW_MACINFO_undef:
21917 len = strlen (ref->info) + 1;
21918 if (!dwarf_strict
21919 && len > DWARF_OFFSET_SIZE
21920 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21921 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21922 set_indirect_string (find_AT_string (ref->info));
21923 break;
21924 case DW_MACRO_GNU_define_indirect:
21925 case DW_MACRO_GNU_undef_indirect:
21926 set_indirect_string (find_AT_string (ref->info));
21927 break;
21928 default:
21929 break;
21934 /* Output macinfo section(s). */
21936 static void
21937 output_macinfo (void)
21939 unsigned i;
21940 unsigned long length = vec_safe_length (macinfo_table);
21941 macinfo_entry *ref;
21942 vec<macinfo_entry, va_gc> *files = NULL;
21943 macinfo_hash_type macinfo_htab;
21945 if (! length)
21946 return;
21948 /* output_macinfo* uses these interchangeably. */
21949 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
21950 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
21951 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
21952 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
21954 /* For .debug_macro emit the section header. */
21955 if (!dwarf_strict)
21957 dw2_asm_output_data (2, 4, "DWARF macro version number");
21958 if (DWARF_OFFSET_SIZE == 8)
21959 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
21960 else
21961 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
21962 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
21963 (!dwarf_split_debug_info ? debug_line_section_label
21964 : debug_skeleton_line_section_label),
21965 debug_line_section, NULL);
21968 /* In the first loop, it emits the primary .debug_macinfo section
21969 and after each emitted op the macinfo_entry is cleared.
21970 If a longer range of define/undef ops can be optimized using
21971 DW_MACRO_GNU_transparent_include, the
21972 DW_MACRO_GNU_transparent_include op is emitted and kept in
21973 the vector before the first define/undef in the range and the
21974 whole range of define/undef ops is not emitted and kept. */
21975 for (i = 0; macinfo_table->iterate (i, &ref); i++)
21977 switch (ref->code)
21979 case DW_MACINFO_start_file:
21980 vec_safe_push (files, *ref);
21981 break;
21982 case DW_MACINFO_end_file:
21983 if (!vec_safe_is_empty (files))
21984 files->pop ();
21985 break;
21986 case DW_MACINFO_define:
21987 case DW_MACINFO_undef:
21988 if (!dwarf_strict
21989 && HAVE_COMDAT_GROUP
21990 && vec_safe_length (files) != 1
21991 && i > 0
21992 && i + 1 < length
21993 && (*macinfo_table)[i - 1].code == 0)
21995 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
21996 if (count)
21998 i += count - 1;
21999 continue;
22002 break;
22003 case 0:
22004 /* A dummy entry may be inserted at the beginning to be able
22005 to optimize the whole block of predefined macros. */
22006 if (i == 0)
22007 continue;
22008 default:
22009 break;
22011 output_macinfo_op (ref);
22012 ref->info = NULL;
22013 ref->code = 0;
22016 if (!macinfo_htab.is_created ())
22017 return;
22019 macinfo_htab.dispose ();
22021 /* If any DW_MACRO_GNU_transparent_include were used, on those
22022 DW_MACRO_GNU_transparent_include entries terminate the
22023 current chain and switch to a new comdat .debug_macinfo
22024 section and emit the define/undef entries within it. */
22025 for (i = 0; macinfo_table->iterate (i, &ref); i++)
22026 switch (ref->code)
22028 case 0:
22029 continue;
22030 case DW_MACRO_GNU_transparent_include:
22032 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22033 tree comdat_key = get_identifier (ref->info);
22034 /* Terminate the previous .debug_macinfo section. */
22035 dw2_asm_output_data (1, 0, "End compilation unit");
22036 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
22037 SECTION_DEBUG
22038 | SECTION_LINKONCE,
22039 comdat_key);
22040 ASM_GENERATE_INTERNAL_LABEL (label,
22041 DEBUG_MACRO_SECTION_LABEL,
22042 ref->lineno);
22043 ASM_OUTPUT_LABEL (asm_out_file, label);
22044 ref->code = 0;
22045 ref->info = NULL;
22046 dw2_asm_output_data (2, 4, "DWARF macro version number");
22047 if (DWARF_OFFSET_SIZE == 8)
22048 dw2_asm_output_data (1, 1, "Flags: 64-bit");
22049 else
22050 dw2_asm_output_data (1, 0, "Flags: 32-bit");
22052 break;
22053 case DW_MACINFO_define:
22054 case DW_MACINFO_undef:
22055 output_macinfo_op (ref);
22056 ref->code = 0;
22057 ref->info = NULL;
22058 break;
22059 default:
22060 gcc_unreachable ();
22064 /* Set up for Dwarf output at the start of compilation. */
22066 static void
22067 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
22069 /* Allocate the file_table. */
22070 file_table = htab_create_ggc (50, file_table_hash,
22071 file_table_eq, NULL);
22073 /* Allocate the decl_die_table. */
22074 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
22075 decl_die_table_eq, NULL);
22077 /* Allocate the decl_loc_table. */
22078 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
22079 decl_loc_table_eq, NULL);
22081 /* Allocate the cached_dw_loc_list_table. */
22082 cached_dw_loc_list_table
22083 = htab_create_ggc (10, cached_dw_loc_list_table_hash,
22084 cached_dw_loc_list_table_eq, NULL);
22086 /* Allocate the initial hunk of the decl_scope_table. */
22087 vec_alloc (decl_scope_table, 256);
22089 /* Allocate the initial hunk of the abbrev_die_table. */
22090 abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
22091 (ABBREV_DIE_TABLE_INCREMENT);
22092 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
22093 /* Zero-th entry is allocated, but unused. */
22094 abbrev_die_table_in_use = 1;
22096 /* Allocate the pubtypes and pubnames vectors. */
22097 vec_alloc (pubname_table, 32);
22098 vec_alloc (pubtype_table, 32);
22100 vec_alloc (incomplete_types, 64);
22102 vec_alloc (used_rtx_array, 32);
22104 if (!dwarf_split_debug_info)
22106 debug_info_section = get_section (DEBUG_INFO_SECTION,
22107 SECTION_DEBUG, NULL);
22108 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22109 SECTION_DEBUG, NULL);
22110 debug_loc_section = get_section (DEBUG_LOC_SECTION,
22111 SECTION_DEBUG, NULL);
22113 else
22115 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
22116 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22117 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
22118 SECTION_DEBUG | SECTION_EXCLUDE,
22119 NULL);
22120 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
22121 SECTION_DEBUG, NULL);
22122 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
22123 SECTION_DEBUG, NULL);
22124 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22125 SECTION_DEBUG, NULL);
22126 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
22127 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
22129 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
22130 the main .o, but the skeleton_line goes into the split off dwo. */
22131 debug_skeleton_line_section
22132 = get_section (DEBUG_DWO_LINE_SECTION,
22133 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22134 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
22135 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
22136 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
22137 SECTION_DEBUG | SECTION_EXCLUDE,
22138 NULL);
22139 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
22140 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
22141 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
22142 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22143 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
22144 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
22146 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
22147 SECTION_DEBUG, NULL);
22148 debug_macinfo_section = get_section (dwarf_strict
22149 ? DEBUG_MACINFO_SECTION
22150 : DEBUG_MACRO_SECTION,
22151 DEBUG_MACRO_SECTION_FLAGS, NULL);
22152 debug_line_section = get_section (DEBUG_LINE_SECTION,
22153 SECTION_DEBUG, NULL);
22154 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
22155 SECTION_DEBUG, NULL);
22156 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
22157 SECTION_DEBUG, NULL);
22158 debug_str_section = get_section (DEBUG_STR_SECTION,
22159 DEBUG_STR_SECTION_FLAGS, NULL);
22160 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
22161 SECTION_DEBUG, NULL);
22162 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
22163 SECTION_DEBUG, NULL);
22165 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
22166 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
22167 DEBUG_ABBREV_SECTION_LABEL, 0);
22168 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
22169 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
22170 COLD_TEXT_SECTION_LABEL, 0);
22171 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
22173 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
22174 DEBUG_INFO_SECTION_LABEL, 0);
22175 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
22176 DEBUG_LINE_SECTION_LABEL, 0);
22177 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
22178 DEBUG_RANGES_SECTION_LABEL, 0);
22179 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
22180 DEBUG_ADDR_SECTION_LABEL, 0);
22181 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
22182 dwarf_strict
22183 ? DEBUG_MACINFO_SECTION_LABEL
22184 : DEBUG_MACRO_SECTION_LABEL, 0);
22185 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
22187 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22188 vec_alloc (macinfo_table, 64);
22190 switch_to_section (text_section);
22191 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
22193 /* Make sure the line number table for .text always exists. */
22194 text_section_line_info = new_line_info_table ();
22195 text_section_line_info->end_label = text_end_label;
22198 /* Called before compile () starts outputtting functions, variables
22199 and toplevel asms into assembly. */
22201 static void
22202 dwarf2out_assembly_start (void)
22204 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
22205 && dwarf2out_do_cfi_asm ()
22206 && (!(flag_unwind_tables || flag_exceptions)
22207 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
22208 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
22211 /* A helper function for dwarf2out_finish called through
22212 htab_traverse. Assign a string its index. All strings must be
22213 collected into the table by the time index_string is called,
22214 because the indexing code relies on htab_traverse to traverse nodes
22215 in the same order for each run. */
22217 static int
22218 index_string (void **h, void *v)
22220 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22221 unsigned int *index = (unsigned int *) v;
22223 find_string_form (node);
22224 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22226 gcc_assert (node->index == NO_INDEX_ASSIGNED);
22227 node->index = *index;
22228 *index += 1;
22230 return 1;
22233 /* A helper function for output_indirect_strings called through
22234 htab_traverse. Output the offset to a string and update the
22235 current offset. */
22237 static int
22238 output_index_string_offset (void **h, void *v)
22240 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22241 unsigned int *offset = (unsigned int *) v;
22243 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22245 /* Assert that this node has been assigned an index. */
22246 gcc_assert (node->index != NO_INDEX_ASSIGNED
22247 && node->index != NOT_INDEXED);
22248 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
22249 "indexed string 0x%x: %s", node->index, node->str);
22250 *offset += strlen (node->str) + 1;
22252 return 1;
22255 /* A helper function for dwarf2out_finish called through
22256 htab_traverse. Output the indexed string. */
22258 static int
22259 output_index_string (void **h, void *v)
22261 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22262 unsigned int *cur_idx = (unsigned int *) v;
22264 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22266 /* Assert that the strings are output in the same order as their
22267 indexes were assigned. */
22268 gcc_assert (*cur_idx == node->index);
22269 assemble_string (node->str, strlen (node->str) + 1);
22270 *cur_idx += 1;
22272 return 1;
22275 /* A helper function for dwarf2out_finish called through
22276 htab_traverse. Emit one queued .debug_str string. */
22278 static int
22279 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
22281 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22283 node->form = find_string_form (node);
22284 if (node->form == DW_FORM_strp && node->refcount > 0)
22286 ASM_OUTPUT_LABEL (asm_out_file, node->label);
22287 assemble_string (node->str, strlen (node->str) + 1);
22290 return 1;
22293 /* Output the indexed string table. */
22295 static void
22296 output_indirect_strings (void)
22298 switch_to_section (debug_str_section);
22299 if (!dwarf_split_debug_info)
22300 htab_traverse (debug_str_hash, output_indirect_string, NULL);
22301 else
22303 unsigned int offset = 0;
22304 unsigned int cur_idx = 0;
22306 htab_traverse (skeleton_debug_str_hash, output_indirect_string, NULL);
22308 switch_to_section (debug_str_offsets_section);
22309 htab_traverse_noresize (debug_str_hash,
22310 output_index_string_offset,
22311 &offset);
22312 switch_to_section (debug_str_dwo_section);
22313 htab_traverse_noresize (debug_str_hash,
22314 output_index_string,
22315 &cur_idx);
22319 /* Callback for htab_traverse to assign an index to an entry in the
22320 table, and to write that entry to the .debug_addr section. */
22322 static int
22323 output_addr_table_entry (void **slot, void *data)
22325 addr_table_entry *entry = (addr_table_entry *) *slot;
22326 unsigned int *cur_index = (unsigned int *)data;
22328 if (entry->refcount == 0)
22330 gcc_assert (entry->index == NO_INDEX_ASSIGNED
22331 || entry->index == NOT_INDEXED);
22332 return 1;
22335 gcc_assert (entry->index == *cur_index);
22336 (*cur_index)++;
22338 switch (entry->kind)
22340 case ate_kind_rtx:
22341 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
22342 "0x%x", entry->index);
22343 break;
22344 case ate_kind_rtx_dtprel:
22345 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
22346 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
22347 DWARF2_ADDR_SIZE,
22348 entry->addr.rtl);
22349 fputc ('\n', asm_out_file);
22350 break;
22351 case ate_kind_label:
22352 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
22353 "0x%x", entry->index);
22354 break;
22355 default:
22356 gcc_unreachable ();
22358 return 1;
22361 /* Produce the .debug_addr section. */
22363 static void
22364 output_addr_table (void)
22366 unsigned int index = 0;
22367 if (addr_index_table == NULL || htab_size (addr_index_table) == 0)
22368 return;
22370 switch_to_section (debug_addr_section);
22371 htab_traverse_noresize (addr_index_table, output_addr_table_entry, &index);
22374 #if ENABLE_ASSERT_CHECKING
22375 /* Verify that all marks are clear. */
22377 static void
22378 verify_marks_clear (dw_die_ref die)
22380 dw_die_ref c;
22382 gcc_assert (! die->die_mark);
22383 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
22385 #endif /* ENABLE_ASSERT_CHECKING */
22387 /* Clear the marks for a die and its children.
22388 Be cool if the mark isn't set. */
22390 static void
22391 prune_unmark_dies (dw_die_ref die)
22393 dw_die_ref c;
22395 if (die->die_mark)
22396 die->die_mark = 0;
22397 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
22400 /* Given DIE that we're marking as used, find any other dies
22401 it references as attributes and mark them as used. */
22403 static void
22404 prune_unused_types_walk_attribs (dw_die_ref die)
22406 dw_attr_ref a;
22407 unsigned ix;
22409 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22411 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
22413 /* A reference to another DIE.
22414 Make sure that it will get emitted.
22415 If it was broken out into a comdat group, don't follow it. */
22416 if (! AT_ref (a)->comdat_type_p
22417 || a->dw_attr == DW_AT_specification)
22418 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
22420 /* Set the string's refcount to 0 so that prune_unused_types_mark
22421 accounts properly for it. */
22422 if (AT_class (a) == dw_val_class_str)
22423 a->dw_attr_val.v.val_str->refcount = 0;
22427 /* Mark the generic parameters and arguments children DIEs of DIE. */
22429 static void
22430 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
22432 dw_die_ref c;
22434 if (die == NULL || die->die_child == NULL)
22435 return;
22436 c = die->die_child;
22439 if (is_template_parameter (c))
22440 prune_unused_types_mark (c, 1);
22441 c = c->die_sib;
22442 } while (c && c != die->die_child);
22445 /* Mark DIE as being used. If DOKIDS is true, then walk down
22446 to DIE's children. */
22448 static void
22449 prune_unused_types_mark (dw_die_ref die, int dokids)
22451 dw_die_ref c;
22453 if (die->die_mark == 0)
22455 /* We haven't done this node yet. Mark it as used. */
22456 die->die_mark = 1;
22457 /* If this is the DIE of a generic type instantiation,
22458 mark the children DIEs that describe its generic parms and
22459 args. */
22460 prune_unused_types_mark_generic_parms_dies (die);
22462 /* We also have to mark its parents as used.
22463 (But we don't want to mark our parent's kids due to this,
22464 unless it is a class.) */
22465 if (die->die_parent)
22466 prune_unused_types_mark (die->die_parent,
22467 class_scope_p (die->die_parent));
22469 /* Mark any referenced nodes. */
22470 prune_unused_types_walk_attribs (die);
22472 /* If this node is a specification,
22473 also mark the definition, if it exists. */
22474 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
22475 prune_unused_types_mark (die->die_definition, 1);
22478 if (dokids && die->die_mark != 2)
22480 /* We need to walk the children, but haven't done so yet.
22481 Remember that we've walked the kids. */
22482 die->die_mark = 2;
22484 /* If this is an array type, we need to make sure our
22485 kids get marked, even if they're types. If we're
22486 breaking out types into comdat sections, do this
22487 for all type definitions. */
22488 if (die->die_tag == DW_TAG_array_type
22489 || (use_debug_types
22490 && is_type_die (die) && ! is_declaration_die (die)))
22491 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
22492 else
22493 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22497 /* For local classes, look if any static member functions were emitted
22498 and if so, mark them. */
22500 static void
22501 prune_unused_types_walk_local_classes (dw_die_ref die)
22503 dw_die_ref c;
22505 if (die->die_mark == 2)
22506 return;
22508 switch (die->die_tag)
22510 case DW_TAG_structure_type:
22511 case DW_TAG_union_type:
22512 case DW_TAG_class_type:
22513 break;
22515 case DW_TAG_subprogram:
22516 if (!get_AT_flag (die, DW_AT_declaration)
22517 || die->die_definition != NULL)
22518 prune_unused_types_mark (die, 1);
22519 return;
22521 default:
22522 return;
22525 /* Mark children. */
22526 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
22529 /* Walk the tree DIE and mark types that we actually use. */
22531 static void
22532 prune_unused_types_walk (dw_die_ref die)
22534 dw_die_ref c;
22536 /* Don't do anything if this node is already marked and
22537 children have been marked as well. */
22538 if (die->die_mark == 2)
22539 return;
22541 switch (die->die_tag)
22543 case DW_TAG_structure_type:
22544 case DW_TAG_union_type:
22545 case DW_TAG_class_type:
22546 if (die->die_perennial_p)
22547 break;
22549 for (c = die->die_parent; c; c = c->die_parent)
22550 if (c->die_tag == DW_TAG_subprogram)
22551 break;
22553 /* Finding used static member functions inside of classes
22554 is needed just for local classes, because for other classes
22555 static member function DIEs with DW_AT_specification
22556 are emitted outside of the DW_TAG_*_type. If we ever change
22557 it, we'd need to call this even for non-local classes. */
22558 if (c)
22559 prune_unused_types_walk_local_classes (die);
22561 /* It's a type node --- don't mark it. */
22562 return;
22564 case DW_TAG_const_type:
22565 case DW_TAG_packed_type:
22566 case DW_TAG_pointer_type:
22567 case DW_TAG_reference_type:
22568 case DW_TAG_rvalue_reference_type:
22569 case DW_TAG_volatile_type:
22570 case DW_TAG_typedef:
22571 case DW_TAG_array_type:
22572 case DW_TAG_interface_type:
22573 case DW_TAG_friend:
22574 case DW_TAG_variant_part:
22575 case DW_TAG_enumeration_type:
22576 case DW_TAG_subroutine_type:
22577 case DW_TAG_string_type:
22578 case DW_TAG_set_type:
22579 case DW_TAG_subrange_type:
22580 case DW_TAG_ptr_to_member_type:
22581 case DW_TAG_file_type:
22582 if (die->die_perennial_p)
22583 break;
22585 /* It's a type node --- don't mark it. */
22586 return;
22588 default:
22589 /* Mark everything else. */
22590 break;
22593 if (die->die_mark == 0)
22595 die->die_mark = 1;
22597 /* Now, mark any dies referenced from here. */
22598 prune_unused_types_walk_attribs (die);
22601 die->die_mark = 2;
22603 /* Mark children. */
22604 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22607 /* Increment the string counts on strings referred to from DIE's
22608 attributes. */
22610 static void
22611 prune_unused_types_update_strings (dw_die_ref die)
22613 dw_attr_ref a;
22614 unsigned ix;
22616 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22617 if (AT_class (a) == dw_val_class_str)
22619 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22620 s->refcount++;
22621 /* Avoid unnecessarily putting strings that are used less than
22622 twice in the hash table. */
22623 if (s->refcount
22624 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22626 void ** slot;
22627 slot = htab_find_slot_with_hash (debug_str_hash, s->str,
22628 htab_hash_string (s->str),
22629 INSERT);
22630 gcc_assert (*slot == NULL);
22631 *slot = s;
22636 /* Remove from the tree DIE any dies that aren't marked. */
22638 static void
22639 prune_unused_types_prune (dw_die_ref die)
22641 dw_die_ref c;
22643 gcc_assert (die->die_mark);
22644 prune_unused_types_update_strings (die);
22646 if (! die->die_child)
22647 return;
22649 c = die->die_child;
22650 do {
22651 dw_die_ref prev = c;
22652 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
22653 if (c == die->die_child)
22655 /* No marked children between 'prev' and the end of the list. */
22656 if (prev == c)
22657 /* No marked children at all. */
22658 die->die_child = NULL;
22659 else
22661 prev->die_sib = c->die_sib;
22662 die->die_child = prev;
22664 return;
22667 if (c != prev->die_sib)
22668 prev->die_sib = c;
22669 prune_unused_types_prune (c);
22670 } while (c != die->die_child);
22673 /* Remove dies representing declarations that we never use. */
22675 static void
22676 prune_unused_types (void)
22678 unsigned int i;
22679 limbo_die_node *node;
22680 comdat_type_node *ctnode;
22681 pubname_ref pub;
22682 dw_die_ref base_type;
22684 #if ENABLE_ASSERT_CHECKING
22685 /* All the marks should already be clear. */
22686 verify_marks_clear (comp_unit_die ());
22687 for (node = limbo_die_list; node; node = node->next)
22688 verify_marks_clear (node->die);
22689 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22690 verify_marks_clear (ctnode->root_die);
22691 #endif /* ENABLE_ASSERT_CHECKING */
22693 /* Mark types that are used in global variables. */
22694 premark_types_used_by_global_vars ();
22696 /* Set the mark on nodes that are actually used. */
22697 prune_unused_types_walk (comp_unit_die ());
22698 for (node = limbo_die_list; node; node = node->next)
22699 prune_unused_types_walk (node->die);
22700 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22702 prune_unused_types_walk (ctnode->root_die);
22703 prune_unused_types_mark (ctnode->type_die, 1);
22706 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
22707 are unusual in that they are pubnames that are the children of pubtypes.
22708 They should only be marked via their parent DW_TAG_enumeration_type die,
22709 not as roots in themselves. */
22710 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
22711 if (pub->die->die_tag != DW_TAG_enumerator)
22712 prune_unused_types_mark (pub->die, 1);
22713 for (i = 0; base_types.iterate (i, &base_type); i++)
22714 prune_unused_types_mark (base_type, 1);
22716 if (debug_str_hash)
22717 htab_empty (debug_str_hash);
22718 if (skeleton_debug_str_hash)
22719 htab_empty (skeleton_debug_str_hash);
22720 prune_unused_types_prune (comp_unit_die ());
22721 for (node = limbo_die_list; node; node = node->next)
22722 prune_unused_types_prune (node->die);
22723 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22724 prune_unused_types_prune (ctnode->root_die);
22726 /* Leave the marks clear. */
22727 prune_unmark_dies (comp_unit_die ());
22728 for (node = limbo_die_list; node; node = node->next)
22729 prune_unmark_dies (node->die);
22730 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22731 prune_unmark_dies (ctnode->root_die);
22734 /* Set the parameter to true if there are any relative pathnames in
22735 the file table. */
22736 static int
22737 file_table_relative_p (void ** slot, void *param)
22739 bool *p = (bool *) param;
22740 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
22741 if (!IS_ABSOLUTE_PATH (d->filename))
22743 *p = true;
22744 return 0;
22746 return 1;
22749 /* Helpers to manipulate hash table of comdat type units. */
22751 struct comdat_type_hasher : typed_noop_remove <comdat_type_node>
22753 typedef comdat_type_node value_type;
22754 typedef comdat_type_node compare_type;
22755 static inline hashval_t hash (const value_type *);
22756 static inline bool equal (const value_type *, const compare_type *);
22759 inline hashval_t
22760 comdat_type_hasher::hash (const value_type *type_node)
22762 hashval_t h;
22763 memcpy (&h, type_node->signature, sizeof (h));
22764 return h;
22767 inline bool
22768 comdat_type_hasher::equal (const value_type *type_node_1,
22769 const compare_type *type_node_2)
22771 return (! memcmp (type_node_1->signature, type_node_2->signature,
22772 DWARF_TYPE_SIGNATURE_SIZE));
22775 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
22776 to the location it would have been added, should we know its
22777 DECL_ASSEMBLER_NAME when we added other attributes. This will
22778 probably improve compactness of debug info, removing equivalent
22779 abbrevs, and hide any differences caused by deferring the
22780 computation of the assembler name, triggered by e.g. PCH. */
22782 static inline void
22783 move_linkage_attr (dw_die_ref die)
22785 unsigned ix = vec_safe_length (die->die_attr);
22786 dw_attr_node linkage = (*die->die_attr)[ix - 1];
22788 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
22789 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
22791 while (--ix > 0)
22793 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
22795 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
22796 break;
22799 if (ix != vec_safe_length (die->die_attr) - 1)
22801 die->die_attr->pop ();
22802 die->die_attr->quick_insert (ix, linkage);
22806 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
22807 referenced from typed stack ops and count how often they are used. */
22809 static void
22810 mark_base_types (dw_loc_descr_ref loc)
22812 dw_die_ref base_type = NULL;
22814 for (; loc; loc = loc->dw_loc_next)
22816 switch (loc->dw_loc_opc)
22818 case DW_OP_GNU_regval_type:
22819 case DW_OP_GNU_deref_type:
22820 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
22821 break;
22822 case DW_OP_GNU_convert:
22823 case DW_OP_GNU_reinterpret:
22824 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
22825 continue;
22826 /* FALLTHRU */
22827 case DW_OP_GNU_const_type:
22828 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
22829 break;
22830 case DW_OP_GNU_entry_value:
22831 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
22832 continue;
22833 default:
22834 continue;
22836 gcc_assert (base_type->die_parent == comp_unit_die ());
22837 if (base_type->die_mark)
22838 base_type->die_mark++;
22839 else
22841 base_types.safe_push (base_type);
22842 base_type->die_mark = 1;
22847 /* Comparison function for sorting marked base types. */
22849 static int
22850 base_type_cmp (const void *x, const void *y)
22852 dw_die_ref dx = *(const dw_die_ref *) x;
22853 dw_die_ref dy = *(const dw_die_ref *) y;
22854 unsigned int byte_size1, byte_size2;
22855 unsigned int encoding1, encoding2;
22856 if (dx->die_mark > dy->die_mark)
22857 return -1;
22858 if (dx->die_mark < dy->die_mark)
22859 return 1;
22860 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
22861 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
22862 if (byte_size1 < byte_size2)
22863 return 1;
22864 if (byte_size1 > byte_size2)
22865 return -1;
22866 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
22867 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
22868 if (encoding1 < encoding2)
22869 return 1;
22870 if (encoding1 > encoding2)
22871 return -1;
22872 return 0;
22875 /* Move base types marked by mark_base_types as early as possible
22876 in the CU, sorted by decreasing usage count both to make the
22877 uleb128 references as small as possible and to make sure they
22878 will have die_offset already computed by calc_die_sizes when
22879 sizes of typed stack loc ops is computed. */
22881 static void
22882 move_marked_base_types (void)
22884 unsigned int i;
22885 dw_die_ref base_type, die, c;
22887 if (base_types.is_empty ())
22888 return;
22890 /* Sort by decreasing usage count, they will be added again in that
22891 order later on. */
22892 base_types.qsort (base_type_cmp);
22893 die = comp_unit_die ();
22894 c = die->die_child;
22897 dw_die_ref prev = c;
22898 c = c->die_sib;
22899 while (c->die_mark)
22901 remove_child_with_prev (c, prev);
22902 /* As base types got marked, there must be at least
22903 one node other than DW_TAG_base_type. */
22904 gcc_assert (c != c->die_sib);
22905 c = c->die_sib;
22908 while (c != die->die_child);
22909 gcc_assert (die->die_child);
22910 c = die->die_child;
22911 for (i = 0; base_types.iterate (i, &base_type); i++)
22913 base_type->die_mark = 0;
22914 base_type->die_sib = c->die_sib;
22915 c->die_sib = base_type;
22916 c = base_type;
22920 /* Helper function for resolve_addr, attempt to resolve
22921 one CONST_STRING, return non-zero if not successful. Similarly verify that
22922 SYMBOL_REFs refer to variables emitted in the current CU. */
22924 static int
22925 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
22927 rtx rtl = *addr;
22929 if (GET_CODE (rtl) == CONST_STRING)
22931 size_t len = strlen (XSTR (rtl, 0)) + 1;
22932 tree t = build_string (len, XSTR (rtl, 0));
22933 tree tlen = size_int (len - 1);
22934 TREE_TYPE (t)
22935 = build_array_type (char_type_node, build_index_type (tlen));
22936 rtl = lookup_constant_def (t);
22937 if (!rtl || !MEM_P (rtl))
22938 return 1;
22939 rtl = XEXP (rtl, 0);
22940 if (GET_CODE (rtl) == SYMBOL_REF
22941 && SYMBOL_REF_DECL (rtl)
22942 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22943 return 1;
22944 vec_safe_push (used_rtx_array, rtl);
22945 *addr = rtl;
22946 return 0;
22949 if (GET_CODE (rtl) == SYMBOL_REF
22950 && SYMBOL_REF_DECL (rtl))
22952 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
22954 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
22955 return 1;
22957 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22958 return 1;
22961 if (GET_CODE (rtl) == CONST
22962 && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
22963 return 1;
22965 return 0;
22968 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
22969 if possible, and create DW_TAG_dwarf_procedure that can be referenced
22970 from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet. */
22972 static rtx
22973 string_cst_pool_decl (tree t)
22975 rtx rtl = output_constant_def (t, 1);
22976 unsigned char *array;
22977 dw_loc_descr_ref l;
22978 tree decl;
22979 size_t len;
22980 dw_die_ref ref;
22982 if (!rtl || !MEM_P (rtl))
22983 return NULL_RTX;
22984 rtl = XEXP (rtl, 0);
22985 if (GET_CODE (rtl) != SYMBOL_REF
22986 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
22987 return NULL_RTX;
22989 decl = SYMBOL_REF_DECL (rtl);
22990 if (!lookup_decl_die (decl))
22992 len = TREE_STRING_LENGTH (t);
22993 vec_safe_push (used_rtx_array, rtl);
22994 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
22995 array = (unsigned char *) ggc_alloc_atomic (len);
22996 memcpy (array, TREE_STRING_POINTER (t), len);
22997 l = new_loc_descr (DW_OP_implicit_value, len, 0);
22998 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
22999 l->dw_loc_oprnd2.v.val_vec.length = len;
23000 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
23001 l->dw_loc_oprnd2.v.val_vec.array = array;
23002 add_AT_loc (ref, DW_AT_location, l);
23003 equate_decl_number_to_die (decl, ref);
23005 return rtl;
23008 /* Helper function of resolve_addr_in_expr. LOC is
23009 a DW_OP_addr followed by DW_OP_stack_value, either at the start
23010 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
23011 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
23012 with DW_OP_GNU_implicit_pointer if possible
23013 and return true, if unsuccessful, return false. */
23015 static bool
23016 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
23018 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
23019 HOST_WIDE_INT offset = 0;
23020 dw_die_ref ref = NULL;
23021 tree decl;
23023 if (GET_CODE (rtl) == CONST
23024 && GET_CODE (XEXP (rtl, 0)) == PLUS
23025 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
23027 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
23028 rtl = XEXP (XEXP (rtl, 0), 0);
23030 if (GET_CODE (rtl) == CONST_STRING)
23032 size_t len = strlen (XSTR (rtl, 0)) + 1;
23033 tree t = build_string (len, XSTR (rtl, 0));
23034 tree tlen = size_int (len - 1);
23036 TREE_TYPE (t)
23037 = build_array_type (char_type_node, build_index_type (tlen));
23038 rtl = string_cst_pool_decl (t);
23039 if (!rtl)
23040 return false;
23042 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
23044 decl = SYMBOL_REF_DECL (rtl);
23045 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
23047 ref = lookup_decl_die (decl);
23048 if (ref && (get_AT (ref, DW_AT_location)
23049 || get_AT (ref, DW_AT_const_value)))
23051 loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
23052 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23053 loc->dw_loc_oprnd1.val_entry = NULL;
23054 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23055 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23056 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23057 loc->dw_loc_oprnd2.v.val_int = offset;
23058 return true;
23062 return false;
23065 /* Helper function for resolve_addr, handle one location
23066 expression, return false if at least one CONST_STRING or SYMBOL_REF in
23067 the location list couldn't be resolved. */
23069 static bool
23070 resolve_addr_in_expr (dw_loc_descr_ref loc)
23072 dw_loc_descr_ref keep = NULL;
23073 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
23074 switch (loc->dw_loc_opc)
23076 case DW_OP_addr:
23077 if (resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
23079 if ((prev == NULL
23080 || prev->dw_loc_opc == DW_OP_piece
23081 || prev->dw_loc_opc == DW_OP_bit_piece)
23082 && loc->dw_loc_next
23083 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
23084 && !dwarf_strict
23085 && optimize_one_addr_into_implicit_ptr (loc))
23086 break;
23087 return false;
23089 break;
23090 case DW_OP_GNU_addr_index:
23091 case DW_OP_GNU_const_index:
23092 if ((loc->dw_loc_opc == DW_OP_GNU_addr_index
23093 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
23094 && resolve_one_addr (&loc->dw_loc_oprnd1.val_entry->addr.rtl,
23095 NULL))
23096 return false;
23097 break;
23098 case DW_OP_const4u:
23099 case DW_OP_const8u:
23100 if (loc->dtprel
23101 && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
23102 return false;
23103 break;
23104 case DW_OP_plus_uconst:
23105 if (size_of_loc_descr (loc)
23106 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
23108 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
23110 dw_loc_descr_ref repl
23111 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
23112 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
23113 add_loc_descr (&repl, loc->dw_loc_next);
23114 *loc = *repl;
23116 break;
23117 case DW_OP_implicit_value:
23118 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
23119 && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL))
23120 return false;
23121 break;
23122 case DW_OP_GNU_implicit_pointer:
23123 case DW_OP_GNU_parameter_ref:
23124 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
23126 dw_die_ref ref
23127 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
23128 if (ref == NULL)
23129 return false;
23130 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23131 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23132 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23134 break;
23135 case DW_OP_GNU_const_type:
23136 case DW_OP_GNU_regval_type:
23137 case DW_OP_GNU_deref_type:
23138 case DW_OP_GNU_convert:
23139 case DW_OP_GNU_reinterpret:
23140 while (loc->dw_loc_next
23141 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
23143 dw_die_ref base1, base2;
23144 unsigned enc1, enc2, size1, size2;
23145 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23146 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23147 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
23148 else if (loc->dw_loc_oprnd1.val_class
23149 == dw_val_class_unsigned_const)
23150 break;
23151 else
23152 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
23153 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
23154 == dw_val_class_unsigned_const)
23155 break;
23156 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
23157 gcc_assert (base1->die_tag == DW_TAG_base_type
23158 && base2->die_tag == DW_TAG_base_type);
23159 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
23160 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
23161 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
23162 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
23163 if (size1 == size2
23164 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
23165 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
23166 && loc != keep)
23167 || enc1 == enc2))
23169 /* Optimize away next DW_OP_GNU_convert after
23170 adjusting LOC's base type die reference. */
23171 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23172 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23173 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
23174 else
23175 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
23176 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23177 continue;
23179 /* Don't change integer DW_OP_GNU_convert after e.g. floating
23180 point typed stack entry. */
23181 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
23182 keep = loc->dw_loc_next;
23183 break;
23185 break;
23186 default:
23187 break;
23189 return true;
23192 /* Helper function of resolve_addr. DIE had DW_AT_location of
23193 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
23194 and DW_OP_addr couldn't be resolved. resolve_addr has already
23195 removed the DW_AT_location attribute. This function attempts to
23196 add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
23197 to it or DW_AT_const_value attribute, if possible. */
23199 static void
23200 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
23202 if (TREE_CODE (decl) != VAR_DECL
23203 || lookup_decl_die (decl) != die
23204 || DECL_EXTERNAL (decl)
23205 || !TREE_STATIC (decl)
23206 || DECL_INITIAL (decl) == NULL_TREE
23207 || DECL_P (DECL_INITIAL (decl))
23208 || get_AT (die, DW_AT_const_value))
23209 return;
23211 tree init = DECL_INITIAL (decl);
23212 HOST_WIDE_INT offset = 0;
23213 /* For variables that have been optimized away and thus
23214 don't have a memory location, see if we can emit
23215 DW_AT_const_value instead. */
23216 if (tree_add_const_value_attribute (die, init))
23217 return;
23218 if (dwarf_strict)
23219 return;
23220 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
23221 and ADDR_EXPR refers to a decl that has DW_AT_location or
23222 DW_AT_const_value (but isn't addressable, otherwise
23223 resolving the original DW_OP_addr wouldn't fail), see if
23224 we can add DW_OP_GNU_implicit_pointer. */
23225 STRIP_NOPS (init);
23226 if (TREE_CODE (init) == POINTER_PLUS_EXPR
23227 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
23229 offset = tree_to_shwi (TREE_OPERAND (init, 1));
23230 init = TREE_OPERAND (init, 0);
23231 STRIP_NOPS (init);
23233 if (TREE_CODE (init) != ADDR_EXPR)
23234 return;
23235 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
23236 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
23237 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
23238 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
23239 && TREE_OPERAND (init, 0) != decl))
23241 dw_die_ref ref;
23242 dw_loc_descr_ref l;
23244 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
23246 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
23247 if (!rtl)
23248 return;
23249 decl = SYMBOL_REF_DECL (rtl);
23251 else
23252 decl = TREE_OPERAND (init, 0);
23253 ref = lookup_decl_die (decl);
23254 if (ref == NULL
23255 || (!get_AT (ref, DW_AT_location)
23256 && !get_AT (ref, DW_AT_const_value)))
23257 return;
23258 l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
23259 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23260 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
23261 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
23262 add_AT_loc (die, DW_AT_location, l);
23266 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
23267 an address in .rodata section if the string literal is emitted there,
23268 or remove the containing location list or replace DW_AT_const_value
23269 with DW_AT_location and empty location expression, if it isn't found
23270 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
23271 to something that has been emitted in the current CU. */
23273 static void
23274 resolve_addr (dw_die_ref die)
23276 dw_die_ref c;
23277 dw_attr_ref a;
23278 dw_loc_list_ref *curr, *start, loc;
23279 unsigned ix;
23281 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23282 switch (AT_class (a))
23284 case dw_val_class_loc_list:
23285 start = curr = AT_loc_list_ptr (a);
23286 loc = *curr;
23287 gcc_assert (loc);
23288 /* The same list can be referenced more than once. See if we have
23289 already recorded the result from a previous pass. */
23290 if (loc->replaced)
23291 *curr = loc->dw_loc_next;
23292 else if (!loc->resolved_addr)
23294 /* As things stand, we do not expect or allow one die to
23295 reference a suffix of another die's location list chain.
23296 References must be identical or completely separate.
23297 There is therefore no need to cache the result of this
23298 pass on any list other than the first; doing so
23299 would lead to unnecessary writes. */
23300 while (*curr)
23302 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
23303 if (!resolve_addr_in_expr ((*curr)->expr))
23305 dw_loc_list_ref next = (*curr)->dw_loc_next;
23306 dw_loc_descr_ref l = (*curr)->expr;
23308 if (next && (*curr)->ll_symbol)
23310 gcc_assert (!next->ll_symbol);
23311 next->ll_symbol = (*curr)->ll_symbol;
23313 if (dwarf_split_debug_info)
23314 remove_loc_list_addr_table_entries (l);
23315 *curr = next;
23317 else
23319 mark_base_types ((*curr)->expr);
23320 curr = &(*curr)->dw_loc_next;
23323 if (loc == *start)
23324 loc->resolved_addr = 1;
23325 else
23327 loc->replaced = 1;
23328 loc->dw_loc_next = *start;
23331 if (!*start)
23333 remove_AT (die, a->dw_attr);
23334 ix--;
23336 break;
23337 case dw_val_class_loc:
23339 dw_loc_descr_ref l = AT_loc (a);
23340 /* For -gdwarf-2 don't attempt to optimize
23341 DW_AT_data_member_location containing
23342 DW_OP_plus_uconst - older consumers might
23343 rely on it being that op instead of a more complex,
23344 but shorter, location description. */
23345 if ((dwarf_version > 2
23346 || a->dw_attr != DW_AT_data_member_location
23347 || l == NULL
23348 || l->dw_loc_opc != DW_OP_plus_uconst
23349 || l->dw_loc_next != NULL)
23350 && !resolve_addr_in_expr (l))
23352 if (dwarf_split_debug_info)
23353 remove_loc_list_addr_table_entries (l);
23354 if (l != NULL
23355 && l->dw_loc_next == NULL
23356 && l->dw_loc_opc == DW_OP_addr
23357 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
23358 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
23359 && a->dw_attr == DW_AT_location)
23361 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
23362 remove_AT (die, a->dw_attr);
23363 ix--;
23364 optimize_location_into_implicit_ptr (die, decl);
23365 break;
23367 remove_AT (die, a->dw_attr);
23368 ix--;
23370 else
23371 mark_base_types (l);
23373 break;
23374 case dw_val_class_addr:
23375 if (a->dw_attr == DW_AT_const_value
23376 && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
23378 if (AT_index (a) != NOT_INDEXED)
23379 remove_addr_table_entry (a->dw_attr_val.val_entry);
23380 remove_AT (die, a->dw_attr);
23381 ix--;
23383 if (die->die_tag == DW_TAG_GNU_call_site
23384 && a->dw_attr == DW_AT_abstract_origin)
23386 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
23387 dw_die_ref tdie = lookup_decl_die (tdecl);
23388 if (tdie == NULL
23389 && DECL_EXTERNAL (tdecl)
23390 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
23392 force_decl_die (tdecl);
23393 tdie = lookup_decl_die (tdecl);
23395 if (tdie)
23397 a->dw_attr_val.val_class = dw_val_class_die_ref;
23398 a->dw_attr_val.v.val_die_ref.die = tdie;
23399 a->dw_attr_val.v.val_die_ref.external = 0;
23401 else
23403 if (AT_index (a) != NOT_INDEXED)
23404 remove_addr_table_entry (a->dw_attr_val.val_entry);
23405 remove_AT (die, a->dw_attr);
23406 ix--;
23409 break;
23410 default:
23411 break;
23414 FOR_EACH_CHILD (die, c, resolve_addr (c));
23417 /* Helper routines for optimize_location_lists.
23418 This pass tries to share identical local lists in .debug_loc
23419 section. */
23421 /* Iteratively hash operands of LOC opcode. */
23423 static hashval_t
23424 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
23426 dw_val_ref val1 = &loc->dw_loc_oprnd1;
23427 dw_val_ref val2 = &loc->dw_loc_oprnd2;
23429 switch (loc->dw_loc_opc)
23431 case DW_OP_const4u:
23432 case DW_OP_const8u:
23433 if (loc->dtprel)
23434 goto hash_addr;
23435 /* FALLTHRU */
23436 case DW_OP_const1u:
23437 case DW_OP_const1s:
23438 case DW_OP_const2u:
23439 case DW_OP_const2s:
23440 case DW_OP_const4s:
23441 case DW_OP_const8s:
23442 case DW_OP_constu:
23443 case DW_OP_consts:
23444 case DW_OP_pick:
23445 case DW_OP_plus_uconst:
23446 case DW_OP_breg0:
23447 case DW_OP_breg1:
23448 case DW_OP_breg2:
23449 case DW_OP_breg3:
23450 case DW_OP_breg4:
23451 case DW_OP_breg5:
23452 case DW_OP_breg6:
23453 case DW_OP_breg7:
23454 case DW_OP_breg8:
23455 case DW_OP_breg9:
23456 case DW_OP_breg10:
23457 case DW_OP_breg11:
23458 case DW_OP_breg12:
23459 case DW_OP_breg13:
23460 case DW_OP_breg14:
23461 case DW_OP_breg15:
23462 case DW_OP_breg16:
23463 case DW_OP_breg17:
23464 case DW_OP_breg18:
23465 case DW_OP_breg19:
23466 case DW_OP_breg20:
23467 case DW_OP_breg21:
23468 case DW_OP_breg22:
23469 case DW_OP_breg23:
23470 case DW_OP_breg24:
23471 case DW_OP_breg25:
23472 case DW_OP_breg26:
23473 case DW_OP_breg27:
23474 case DW_OP_breg28:
23475 case DW_OP_breg29:
23476 case DW_OP_breg30:
23477 case DW_OP_breg31:
23478 case DW_OP_regx:
23479 case DW_OP_fbreg:
23480 case DW_OP_piece:
23481 case DW_OP_deref_size:
23482 case DW_OP_xderef_size:
23483 hash = iterative_hash_object (val1->v.val_int, hash);
23484 break;
23485 case DW_OP_skip:
23486 case DW_OP_bra:
23488 int offset;
23490 gcc_assert (val1->val_class == dw_val_class_loc);
23491 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
23492 hash = iterative_hash_object (offset, hash);
23494 break;
23495 case DW_OP_implicit_value:
23496 hash = iterative_hash_object (val1->v.val_unsigned, hash);
23497 switch (val2->val_class)
23499 case dw_val_class_const:
23500 hash = iterative_hash_object (val2->v.val_int, hash);
23501 break;
23502 case dw_val_class_vec:
23504 unsigned int elt_size = val2->v.val_vec.elt_size;
23505 unsigned int len = val2->v.val_vec.length;
23507 hash = iterative_hash_object (elt_size, hash);
23508 hash = iterative_hash_object (len, hash);
23509 hash = iterative_hash (val2->v.val_vec.array,
23510 len * elt_size, hash);
23512 break;
23513 case dw_val_class_const_double:
23514 hash = iterative_hash_object (val2->v.val_double.low, hash);
23515 hash = iterative_hash_object (val2->v.val_double.high, hash);
23516 break;
23517 case dw_val_class_addr:
23518 hash = iterative_hash_rtx (val2->v.val_addr, hash);
23519 break;
23520 default:
23521 gcc_unreachable ();
23523 break;
23524 case DW_OP_bregx:
23525 case DW_OP_bit_piece:
23526 hash = iterative_hash_object (val1->v.val_int, hash);
23527 hash = iterative_hash_object (val2->v.val_int, hash);
23528 break;
23529 case DW_OP_addr:
23530 hash_addr:
23531 if (loc->dtprel)
23533 unsigned char dtprel = 0xd1;
23534 hash = iterative_hash_object (dtprel, hash);
23536 hash = iterative_hash_rtx (val1->v.val_addr, hash);
23537 break;
23538 case DW_OP_GNU_addr_index:
23539 case DW_OP_GNU_const_index:
23541 if (loc->dtprel)
23543 unsigned char dtprel = 0xd1;
23544 hash = iterative_hash_object (dtprel, hash);
23546 hash = iterative_hash_rtx (val1->val_entry->addr.rtl, hash);
23548 break;
23549 case DW_OP_GNU_implicit_pointer:
23550 hash = iterative_hash_object (val2->v.val_int, hash);
23551 break;
23552 case DW_OP_GNU_entry_value:
23553 hash = hash_loc_operands (val1->v.val_loc, hash);
23554 break;
23555 case DW_OP_GNU_regval_type:
23556 case DW_OP_GNU_deref_type:
23558 unsigned int byte_size
23559 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
23560 unsigned int encoding
23561 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
23562 hash = iterative_hash_object (val1->v.val_int, hash);
23563 hash = iterative_hash_object (byte_size, hash);
23564 hash = iterative_hash_object (encoding, hash);
23566 break;
23567 case DW_OP_GNU_convert:
23568 case DW_OP_GNU_reinterpret:
23569 if (val1->val_class == dw_val_class_unsigned_const)
23571 hash = iterative_hash_object (val1->v.val_unsigned, hash);
23572 break;
23574 /* FALLTHRU */
23575 case DW_OP_GNU_const_type:
23577 unsigned int byte_size
23578 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
23579 unsigned int encoding
23580 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
23581 hash = iterative_hash_object (byte_size, hash);
23582 hash = iterative_hash_object (encoding, hash);
23583 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
23584 break;
23585 hash = iterative_hash_object (val2->val_class, hash);
23586 switch (val2->val_class)
23588 case dw_val_class_const:
23589 hash = iterative_hash_object (val2->v.val_int, hash);
23590 break;
23591 case dw_val_class_vec:
23593 unsigned int elt_size = val2->v.val_vec.elt_size;
23594 unsigned int len = val2->v.val_vec.length;
23596 hash = iterative_hash_object (elt_size, hash);
23597 hash = iterative_hash_object (len, hash);
23598 hash = iterative_hash (val2->v.val_vec.array,
23599 len * elt_size, hash);
23601 break;
23602 case dw_val_class_const_double:
23603 hash = iterative_hash_object (val2->v.val_double.low, hash);
23604 hash = iterative_hash_object (val2->v.val_double.high, hash);
23605 break;
23606 default:
23607 gcc_unreachable ();
23610 break;
23612 default:
23613 /* Other codes have no operands. */
23614 break;
23616 return hash;
23619 /* Iteratively hash the whole DWARF location expression LOC. */
23621 static inline hashval_t
23622 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
23624 dw_loc_descr_ref l;
23625 bool sizes_computed = false;
23626 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
23627 size_of_locs (loc);
23629 for (l = loc; l != NULL; l = l->dw_loc_next)
23631 enum dwarf_location_atom opc = l->dw_loc_opc;
23632 hash = iterative_hash_object (opc, hash);
23633 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
23635 size_of_locs (loc);
23636 sizes_computed = true;
23638 hash = hash_loc_operands (l, hash);
23640 return hash;
23643 /* Compute hash of the whole location list LIST_HEAD. */
23645 static inline void
23646 hash_loc_list (dw_loc_list_ref list_head)
23648 dw_loc_list_ref curr = list_head;
23649 hashval_t hash = 0;
23651 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
23653 hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
23654 hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
23655 if (curr->section)
23656 hash = iterative_hash (curr->section, strlen (curr->section) + 1,
23657 hash);
23658 hash = hash_locs (curr->expr, hash);
23660 list_head->hash = hash;
23663 /* Return true if X and Y opcodes have the same operands. */
23665 static inline bool
23666 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
23668 dw_val_ref valx1 = &x->dw_loc_oprnd1;
23669 dw_val_ref valx2 = &x->dw_loc_oprnd2;
23670 dw_val_ref valy1 = &y->dw_loc_oprnd1;
23671 dw_val_ref valy2 = &y->dw_loc_oprnd2;
23673 switch (x->dw_loc_opc)
23675 case DW_OP_const4u:
23676 case DW_OP_const8u:
23677 if (x->dtprel)
23678 goto hash_addr;
23679 /* FALLTHRU */
23680 case DW_OP_const1u:
23681 case DW_OP_const1s:
23682 case DW_OP_const2u:
23683 case DW_OP_const2s:
23684 case DW_OP_const4s:
23685 case DW_OP_const8s:
23686 case DW_OP_constu:
23687 case DW_OP_consts:
23688 case DW_OP_pick:
23689 case DW_OP_plus_uconst:
23690 case DW_OP_breg0:
23691 case DW_OP_breg1:
23692 case DW_OP_breg2:
23693 case DW_OP_breg3:
23694 case DW_OP_breg4:
23695 case DW_OP_breg5:
23696 case DW_OP_breg6:
23697 case DW_OP_breg7:
23698 case DW_OP_breg8:
23699 case DW_OP_breg9:
23700 case DW_OP_breg10:
23701 case DW_OP_breg11:
23702 case DW_OP_breg12:
23703 case DW_OP_breg13:
23704 case DW_OP_breg14:
23705 case DW_OP_breg15:
23706 case DW_OP_breg16:
23707 case DW_OP_breg17:
23708 case DW_OP_breg18:
23709 case DW_OP_breg19:
23710 case DW_OP_breg20:
23711 case DW_OP_breg21:
23712 case DW_OP_breg22:
23713 case DW_OP_breg23:
23714 case DW_OP_breg24:
23715 case DW_OP_breg25:
23716 case DW_OP_breg26:
23717 case DW_OP_breg27:
23718 case DW_OP_breg28:
23719 case DW_OP_breg29:
23720 case DW_OP_breg30:
23721 case DW_OP_breg31:
23722 case DW_OP_regx:
23723 case DW_OP_fbreg:
23724 case DW_OP_piece:
23725 case DW_OP_deref_size:
23726 case DW_OP_xderef_size:
23727 return valx1->v.val_int == valy1->v.val_int;
23728 case DW_OP_skip:
23729 case DW_OP_bra:
23730 /* If splitting debug info, the use of DW_OP_GNU_addr_index
23731 can cause irrelevant differences in dw_loc_addr. */
23732 gcc_assert (valx1->val_class == dw_val_class_loc
23733 && valy1->val_class == dw_val_class_loc
23734 && (dwarf_split_debug_info
23735 || x->dw_loc_addr == y->dw_loc_addr));
23736 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
23737 case DW_OP_implicit_value:
23738 if (valx1->v.val_unsigned != valy1->v.val_unsigned
23739 || valx2->val_class != valy2->val_class)
23740 return false;
23741 switch (valx2->val_class)
23743 case dw_val_class_const:
23744 return valx2->v.val_int == valy2->v.val_int;
23745 case dw_val_class_vec:
23746 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23747 && valx2->v.val_vec.length == valy2->v.val_vec.length
23748 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23749 valx2->v.val_vec.elt_size
23750 * valx2->v.val_vec.length) == 0;
23751 case dw_val_class_const_double:
23752 return valx2->v.val_double.low == valy2->v.val_double.low
23753 && valx2->v.val_double.high == valy2->v.val_double.high;
23754 case dw_val_class_addr:
23755 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
23756 default:
23757 gcc_unreachable ();
23759 case DW_OP_bregx:
23760 case DW_OP_bit_piece:
23761 return valx1->v.val_int == valy1->v.val_int
23762 && valx2->v.val_int == valy2->v.val_int;
23763 case DW_OP_addr:
23764 hash_addr:
23765 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
23766 case DW_OP_GNU_addr_index:
23767 case DW_OP_GNU_const_index:
23769 rtx ax1 = valx1->val_entry->addr.rtl;
23770 rtx ay1 = valy1->val_entry->addr.rtl;
23771 return rtx_equal_p (ax1, ay1);
23773 case DW_OP_GNU_implicit_pointer:
23774 return valx1->val_class == dw_val_class_die_ref
23775 && valx1->val_class == valy1->val_class
23776 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
23777 && valx2->v.val_int == valy2->v.val_int;
23778 case DW_OP_GNU_entry_value:
23779 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
23780 case DW_OP_GNU_const_type:
23781 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
23782 || valx2->val_class != valy2->val_class)
23783 return false;
23784 switch (valx2->val_class)
23786 case dw_val_class_const:
23787 return valx2->v.val_int == valy2->v.val_int;
23788 case dw_val_class_vec:
23789 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23790 && valx2->v.val_vec.length == valy2->v.val_vec.length
23791 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23792 valx2->v.val_vec.elt_size
23793 * valx2->v.val_vec.length) == 0;
23794 case dw_val_class_const_double:
23795 return valx2->v.val_double.low == valy2->v.val_double.low
23796 && valx2->v.val_double.high == valy2->v.val_double.high;
23797 default:
23798 gcc_unreachable ();
23800 case DW_OP_GNU_regval_type:
23801 case DW_OP_GNU_deref_type:
23802 return valx1->v.val_int == valy1->v.val_int
23803 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
23804 case DW_OP_GNU_convert:
23805 case DW_OP_GNU_reinterpret:
23806 if (valx1->val_class != valy1->val_class)
23807 return false;
23808 if (valx1->val_class == dw_val_class_unsigned_const)
23809 return valx1->v.val_unsigned == valy1->v.val_unsigned;
23810 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
23811 case DW_OP_GNU_parameter_ref:
23812 return valx1->val_class == dw_val_class_die_ref
23813 && valx1->val_class == valy1->val_class
23814 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
23815 default:
23816 /* Other codes have no operands. */
23817 return true;
23821 /* Return true if DWARF location expressions X and Y are the same. */
23823 static inline bool
23824 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
23826 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
23827 if (x->dw_loc_opc != y->dw_loc_opc
23828 || x->dtprel != y->dtprel
23829 || !compare_loc_operands (x, y))
23830 break;
23831 return x == NULL && y == NULL;
23834 /* Hashtable helpers. */
23836 struct loc_list_hasher : typed_noop_remove <dw_loc_list_struct>
23838 typedef dw_loc_list_struct value_type;
23839 typedef dw_loc_list_struct compare_type;
23840 static inline hashval_t hash (const value_type *);
23841 static inline bool equal (const value_type *, const compare_type *);
23844 /* Return precomputed hash of location list X. */
23846 inline hashval_t
23847 loc_list_hasher::hash (const value_type *x)
23849 return x->hash;
23852 /* Return true if location lists A and B are the same. */
23854 inline bool
23855 loc_list_hasher::equal (const value_type *a, const compare_type *b)
23857 if (a == b)
23858 return 1;
23859 if (a->hash != b->hash)
23860 return 0;
23861 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
23862 if (strcmp (a->begin, b->begin) != 0
23863 || strcmp (a->end, b->end) != 0
23864 || (a->section == NULL) != (b->section == NULL)
23865 || (a->section && strcmp (a->section, b->section) != 0)
23866 || !compare_locs (a->expr, b->expr))
23867 break;
23868 return a == NULL && b == NULL;
23871 typedef hash_table <loc_list_hasher> loc_list_hash_type;
23874 /* Recursively optimize location lists referenced from DIE
23875 children and share them whenever possible. */
23877 static void
23878 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type htab)
23880 dw_die_ref c;
23881 dw_attr_ref a;
23882 unsigned ix;
23883 dw_loc_list_struct **slot;
23885 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23886 if (AT_class (a) == dw_val_class_loc_list)
23888 dw_loc_list_ref list = AT_loc_list (a);
23889 /* TODO: perform some optimizations here, before hashing
23890 it and storing into the hash table. */
23891 hash_loc_list (list);
23892 slot = htab.find_slot_with_hash (list, list->hash, INSERT);
23893 if (*slot == NULL)
23894 *slot = list;
23895 else
23896 a->dw_attr_val.v.val_loc_list = *slot;
23899 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
23903 /* Recursively assign each location list a unique index into the debug_addr
23904 section. */
23906 static void
23907 index_location_lists (dw_die_ref die)
23909 dw_die_ref c;
23910 dw_attr_ref a;
23911 unsigned ix;
23913 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23914 if (AT_class (a) == dw_val_class_loc_list)
23916 dw_loc_list_ref list = AT_loc_list (a);
23917 dw_loc_list_ref curr;
23918 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
23920 /* Don't index an entry that has already been indexed
23921 or won't be output. */
23922 if (curr->begin_entry != NULL
23923 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
23924 continue;
23926 curr->begin_entry
23927 = add_addr_table_entry (xstrdup (curr->begin),
23928 ate_kind_label);
23932 FOR_EACH_CHILD (die, c, index_location_lists (c));
23935 /* Optimize location lists referenced from DIE
23936 children and share them whenever possible. */
23938 static void
23939 optimize_location_lists (dw_die_ref die)
23941 loc_list_hash_type htab;
23942 htab.create (500);
23943 optimize_location_lists_1 (die, htab);
23944 htab.dispose ();
23947 /* Output stuff that dwarf requires at the end of every file,
23948 and generate the DWARF-2 debugging info. */
23950 static void
23951 dwarf2out_finish (const char *filename)
23953 limbo_die_node *node, *next_node;
23954 comdat_type_node *ctnode;
23955 hash_table <comdat_type_hasher> comdat_type_table;
23956 unsigned int i;
23957 dw_die_ref main_comp_unit_die;
23959 /* PCH might result in DW_AT_producer string being restored from the
23960 header compilation, so always fill it with empty string initially
23961 and overwrite only here. */
23962 dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
23963 producer_string = gen_producer_string ();
23964 producer->dw_attr_val.v.val_str->refcount--;
23965 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
23967 gen_scheduled_generic_parms_dies ();
23968 gen_remaining_tmpl_value_param_die_attribute ();
23970 /* Add the name for the main input file now. We delayed this from
23971 dwarf2out_init to avoid complications with PCH. */
23972 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
23973 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
23974 add_comp_dir_attribute (comp_unit_die ());
23975 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
23977 bool p = false;
23978 htab_traverse (file_table, file_table_relative_p, &p);
23979 if (p)
23980 add_comp_dir_attribute (comp_unit_die ());
23983 if (deferred_locations_list)
23984 for (i = 0; i < deferred_locations_list->length (); i++)
23986 add_location_or_const_value_attribute (
23987 (*deferred_locations_list)[i].die,
23988 (*deferred_locations_list)[i].variable,
23989 false,
23990 DW_AT_location);
23993 /* Traverse the limbo die list, and add parent/child links. The only
23994 dies without parents that should be here are concrete instances of
23995 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
23996 For concrete instances, we can get the parent die from the abstract
23997 instance. */
23998 for (node = limbo_die_list; node; node = next_node)
24000 dw_die_ref die = node->die;
24001 next_node = node->next;
24003 if (die->die_parent == NULL)
24005 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
24007 if (origin && origin->die_parent)
24008 add_child_die (origin->die_parent, die);
24009 else if (is_cu_die (die))
24011 else if (seen_error ())
24012 /* It's OK to be confused by errors in the input. */
24013 add_child_die (comp_unit_die (), die);
24014 else
24016 /* In certain situations, the lexical block containing a
24017 nested function can be optimized away, which results
24018 in the nested function die being orphaned. Likewise
24019 with the return type of that nested function. Force
24020 this to be a child of the containing function.
24022 It may happen that even the containing function got fully
24023 inlined and optimized out. In that case we are lost and
24024 assign the empty child. This should not be big issue as
24025 the function is likely unreachable too. */
24026 gcc_assert (node->created_for);
24028 if (DECL_P (node->created_for))
24029 origin = get_context_die (DECL_CONTEXT (node->created_for));
24030 else if (TYPE_P (node->created_for))
24031 origin = scope_die_for (node->created_for, comp_unit_die ());
24032 else
24033 origin = comp_unit_die ();
24035 add_child_die (origin, die);
24040 limbo_die_list = NULL;
24042 #if ENABLE_ASSERT_CHECKING
24044 dw_die_ref die = comp_unit_die (), c;
24045 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
24047 #endif
24048 resolve_addr (comp_unit_die ());
24049 move_marked_base_types ();
24051 for (node = deferred_asm_name; node; node = node->next)
24053 tree decl = node->created_for;
24054 /* When generating LTO bytecode we can not generate new assembler
24055 names at this point and all important decls got theirs via
24056 free-lang-data. */
24057 if ((!flag_generate_lto || DECL_ASSEMBLER_NAME_SET_P (decl))
24058 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
24060 add_linkage_attr (node->die, decl);
24061 move_linkage_attr (node->die);
24065 deferred_asm_name = NULL;
24067 /* Walk through the list of incomplete types again, trying once more to
24068 emit full debugging info for them. */
24069 retry_incomplete_types ();
24071 if (flag_eliminate_unused_debug_types)
24072 prune_unused_types ();
24074 /* Generate separate COMDAT sections for type DIEs. */
24075 if (use_debug_types)
24077 break_out_comdat_types (comp_unit_die ());
24079 /* Each new type_unit DIE was added to the limbo die list when created.
24080 Since these have all been added to comdat_type_list, clear the
24081 limbo die list. */
24082 limbo_die_list = NULL;
24084 /* For each new comdat type unit, copy declarations for incomplete
24085 types to make the new unit self-contained (i.e., no direct
24086 references to the main compile unit). */
24087 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24088 copy_decls_for_unworthy_types (ctnode->root_die);
24089 copy_decls_for_unworthy_types (comp_unit_die ());
24091 /* In the process of copying declarations from one unit to another,
24092 we may have left some declarations behind that are no longer
24093 referenced. Prune them. */
24094 prune_unused_types ();
24097 /* Generate separate CUs for each of the include files we've seen.
24098 They will go into limbo_die_list. */
24099 if (flag_eliminate_dwarf2_dups)
24100 break_out_includes (comp_unit_die ());
24102 /* Traverse the DIE's and add add sibling attributes to those DIE's
24103 that have children. */
24104 add_sibling_attributes (comp_unit_die ());
24105 for (node = limbo_die_list; node; node = node->next)
24106 add_sibling_attributes (node->die);
24107 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24108 add_sibling_attributes (ctnode->root_die);
24110 /* When splitting DWARF info, we put some attributes in the
24111 skeleton compile_unit DIE that remains in the .o, while
24112 most attributes go in the DWO compile_unit_die. */
24113 if (dwarf_split_debug_info)
24114 main_comp_unit_die = gen_compile_unit_die (NULL);
24115 else
24116 main_comp_unit_die = comp_unit_die ();
24118 /* Output a terminator label for the .text section. */
24119 switch_to_section (text_section);
24120 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
24121 if (cold_text_section)
24123 switch_to_section (cold_text_section);
24124 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
24127 /* We can only use the low/high_pc attributes if all of the code was
24128 in .text. */
24129 if (!have_multiple_function_sections
24130 || (dwarf_version < 3 && dwarf_strict))
24132 /* Don't add if the CU has no associated code. */
24133 if (text_section_used)
24134 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
24135 text_end_label, true);
24137 else
24139 unsigned fde_idx;
24140 dw_fde_ref fde;
24141 bool range_list_added = false;
24143 if (text_section_used)
24144 add_ranges_by_labels (main_comp_unit_die, text_section_label,
24145 text_end_label, &range_list_added, true);
24146 if (cold_text_section_used)
24147 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
24148 cold_end_label, &range_list_added, true);
24150 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
24152 if (DECL_IGNORED_P (fde->decl))
24153 continue;
24154 if (!fde->in_std_section)
24155 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
24156 fde->dw_fde_end, &range_list_added,
24157 true);
24158 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
24159 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
24160 fde->dw_fde_second_end, &range_list_added,
24161 true);
24164 if (range_list_added)
24166 /* We need to give .debug_loc and .debug_ranges an appropriate
24167 "base address". Use zero so that these addresses become
24168 absolute. Historically, we've emitted the unexpected
24169 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
24170 Emit both to give time for other tools to adapt. */
24171 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
24172 if (! dwarf_strict && dwarf_version < 4)
24173 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
24175 add_ranges (NULL);
24179 if (debug_info_level >= DINFO_LEVEL_TERSE)
24180 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
24181 debug_line_section_label);
24183 if (have_macinfo)
24184 add_AT_macptr (comp_unit_die (),
24185 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
24186 macinfo_section_label);
24188 if (dwarf_split_debug_info && addr_index_table != NULL)
24190 /* optimize_location_lists calculates the size of the lists,
24191 so index them first, and assign indices to the entries.
24192 Although optimize_location_lists will remove entries from
24193 the table, it only does so for duplicates, and therefore
24194 only reduces ref_counts to 1. */
24195 unsigned int index = 0;
24196 index_location_lists (comp_unit_die ());
24197 htab_traverse_noresize (addr_index_table,
24198 index_addr_table_entry, &index);
24200 if (have_location_lists)
24201 optimize_location_lists (comp_unit_die ());
24203 save_macinfo_strings ();
24205 if (dwarf_split_debug_info)
24207 unsigned int index = 0;
24209 /* Add attributes common to skeleton compile_units and
24210 type_units. Because these attributes include strings, it
24211 must be done before freezing the string table. Top-level
24212 skeleton die attrs are added when the skeleton type unit is
24213 created, so ensure it is created by this point. */
24214 add_top_level_skeleton_die_attrs (main_comp_unit_die);
24215 (void) get_skeleton_type_unit ();
24216 htab_traverse_noresize (debug_str_hash, index_string, &index);
24219 /* Output all of the compilation units. We put the main one last so that
24220 the offsets are available to output_pubnames. */
24221 for (node = limbo_die_list; node; node = node->next)
24222 output_comp_unit (node->die, 0);
24224 comdat_type_table.create (100);
24225 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24227 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
24229 /* Don't output duplicate types. */
24230 if (*slot != HTAB_EMPTY_ENTRY)
24231 continue;
24233 /* Add a pointer to the line table for the main compilation unit
24234 so that the debugger can make sense of DW_AT_decl_file
24235 attributes. */
24236 if (debug_info_level >= DINFO_LEVEL_TERSE)
24237 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
24238 (!dwarf_split_debug_info
24239 ? debug_line_section_label
24240 : debug_skeleton_line_section_label));
24242 output_comdat_type_unit (ctnode);
24243 *slot = ctnode;
24245 comdat_type_table.dispose ();
24247 /* The AT_pubnames attribute needs to go in all skeleton dies, including
24248 both the main_cu and all skeleton TUs. Making this call unconditional
24249 would end up either adding a second copy of the AT_pubnames attribute, or
24250 requiring a special case in add_top_level_skeleton_die_attrs. */
24251 if (!dwarf_split_debug_info)
24252 add_AT_pubnames (comp_unit_die ());
24254 if (dwarf_split_debug_info)
24256 int mark;
24257 unsigned char checksum[16];
24258 struct md5_ctx ctx;
24260 /* Compute a checksum of the comp_unit to use as the dwo_id. */
24261 md5_init_ctx (&ctx);
24262 mark = 0;
24263 die_checksum (comp_unit_die (), &ctx, &mark);
24264 unmark_all_dies (comp_unit_die ());
24265 md5_finish_ctx (&ctx, checksum);
24267 /* Use the first 8 bytes of the checksum as the dwo_id,
24268 and add it to both comp-unit DIEs. */
24269 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
24270 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
24272 /* Add the base offset of the ranges table to the skeleton
24273 comp-unit DIE. */
24274 if (ranges_table_in_use)
24275 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
24276 ranges_section_label);
24278 switch_to_section (debug_addr_section);
24279 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
24280 output_addr_table ();
24283 /* Output the main compilation unit if non-empty or if .debug_macinfo
24284 or .debug_macro will be emitted. */
24285 output_comp_unit (comp_unit_die (), have_macinfo);
24287 if (dwarf_split_debug_info && info_section_emitted)
24288 output_skeleton_debug_sections (main_comp_unit_die);
24290 /* Output the abbreviation table. */
24291 if (abbrev_die_table_in_use != 1)
24293 switch_to_section (debug_abbrev_section);
24294 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
24295 output_abbrev_section ();
24298 /* Output location list section if necessary. */
24299 if (have_location_lists)
24301 /* Output the location lists info. */
24302 switch_to_section (debug_loc_section);
24303 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
24304 output_location_lists (comp_unit_die ());
24307 output_pubtables ();
24309 /* Output the address range information if a CU (.debug_info section)
24310 was emitted. We output an empty table even if we had no functions
24311 to put in it. This because the consumer has no way to tell the
24312 difference between an empty table that we omitted and failure to
24313 generate a table that would have contained data. */
24314 if (info_section_emitted)
24316 unsigned long aranges_length = size_of_aranges ();
24318 switch_to_section (debug_aranges_section);
24319 output_aranges (aranges_length);
24322 /* Output ranges section if necessary. */
24323 if (ranges_table_in_use)
24325 switch_to_section (debug_ranges_section);
24326 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
24327 output_ranges ();
24330 /* Have to end the macro section. */
24331 if (have_macinfo)
24333 switch_to_section (debug_macinfo_section);
24334 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
24335 output_macinfo ();
24336 dw2_asm_output_data (1, 0, "End compilation unit");
24339 /* Output the source line correspondence table. We must do this
24340 even if there is no line information. Otherwise, on an empty
24341 translation unit, we will generate a present, but empty,
24342 .debug_info section. IRIX 6.5 `nm' will then complain when
24343 examining the file. This is done late so that any filenames
24344 used by the debug_info section are marked as 'used'. */
24345 switch_to_section (debug_line_section);
24346 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
24347 if (! DWARF2_ASM_LINE_DEBUG_INFO)
24348 output_line_info (false);
24350 if (dwarf_split_debug_info && info_section_emitted)
24352 switch_to_section (debug_skeleton_line_section);
24353 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
24354 output_line_info (true);
24357 /* If we emitted any indirect strings, output the string table too. */
24358 if (debug_str_hash || skeleton_debug_str_hash)
24359 output_indirect_strings ();
24362 #include "gt-dwarf2out.h"