* lto-partition.c (add_symbol_to_partition_1,
[official-gcc.git] / gcc / dwarf2out.c
blobe202fa7357fbee218aac135e46aeb54a5a64198a
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 /* Forward declarations for functions defined in this file. */
255 static char *stripattributes (const char *);
256 static void output_call_frame_info (int);
257 static void dwarf2out_note_section_used (void);
259 /* Personality decl of current unit. Used only when assembler does not support
260 personality CFI. */
261 static GTY(()) rtx current_unit_personality;
263 /* Data and reference forms for relocatable data. */
264 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
265 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
267 #ifndef DEBUG_FRAME_SECTION
268 #define DEBUG_FRAME_SECTION ".debug_frame"
269 #endif
271 #ifndef FUNC_BEGIN_LABEL
272 #define FUNC_BEGIN_LABEL "LFB"
273 #endif
275 #ifndef FUNC_END_LABEL
276 #define FUNC_END_LABEL "LFE"
277 #endif
279 #ifndef PROLOGUE_END_LABEL
280 #define PROLOGUE_END_LABEL "LPE"
281 #endif
283 #ifndef EPILOGUE_BEGIN_LABEL
284 #define EPILOGUE_BEGIN_LABEL "LEB"
285 #endif
287 #ifndef FRAME_BEGIN_LABEL
288 #define FRAME_BEGIN_LABEL "Lframe"
289 #endif
290 #define CIE_AFTER_SIZE_LABEL "LSCIE"
291 #define CIE_END_LABEL "LECIE"
292 #define FDE_LABEL "LSFDE"
293 #define FDE_AFTER_SIZE_LABEL "LASFDE"
294 #define FDE_END_LABEL "LEFDE"
295 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
296 #define LINE_NUMBER_END_LABEL "LELT"
297 #define LN_PROLOG_AS_LABEL "LASLTP"
298 #define LN_PROLOG_END_LABEL "LELTP"
299 #define DIE_LABEL_PREFIX "DW"
301 /* Match the base name of a file to the base name of a compilation unit. */
303 static int
304 matches_main_base (const char *path)
306 /* Cache the last query. */
307 static const char *last_path = NULL;
308 static int last_match = 0;
309 if (path != last_path)
311 const char *base;
312 int length = base_of_path (path, &base);
313 last_path = path;
314 last_match = (length == main_input_baselength
315 && memcmp (base, main_input_basename, length) == 0);
317 return last_match;
320 #ifdef DEBUG_DEBUG_STRUCT
322 static int
323 dump_struct_debug (tree type, enum debug_info_usage usage,
324 enum debug_struct_file criterion, int generic,
325 int matches, int result)
327 /* Find the type name. */
328 tree type_decl = TYPE_STUB_DECL (type);
329 tree t = type_decl;
330 const char *name = 0;
331 if (TREE_CODE (t) == TYPE_DECL)
332 t = DECL_NAME (t);
333 if (t)
334 name = IDENTIFIER_POINTER (t);
336 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
337 criterion,
338 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
339 matches ? "bas" : "hdr",
340 generic ? "gen" : "ord",
341 usage == DINFO_USAGE_DFN ? ";" :
342 usage == DINFO_USAGE_DIR_USE ? "." : "*",
343 result,
344 (void*) type_decl, name);
345 return result;
347 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
348 dump_struct_debug (type, usage, criterion, generic, matches, result)
350 #else
352 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
353 (result)
355 #endif
357 static bool
358 should_emit_struct_debug (tree type, enum debug_info_usage usage)
360 enum debug_struct_file criterion;
361 tree type_decl;
362 bool generic = lang_hooks.types.generic_p (type);
364 if (generic)
365 criterion = debug_struct_generic[usage];
366 else
367 criterion = debug_struct_ordinary[usage];
369 if (criterion == DINFO_STRUCT_FILE_NONE)
370 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
371 if (criterion == DINFO_STRUCT_FILE_ANY)
372 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
374 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
376 if (type_decl != NULL)
378 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
379 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
381 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
382 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
385 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
388 /* Return a pointer to a copy of the section string name S with all
389 attributes stripped off, and an asterisk prepended (for assemble_name). */
391 static inline char *
392 stripattributes (const char *s)
394 char *stripped = XNEWVEC (char, strlen (s) + 2);
395 char *p = stripped;
397 *p++ = '*';
399 while (*s && *s != ',')
400 *p++ = *s++;
402 *p = '\0';
403 return stripped;
406 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
407 switch to the data section instead, and write out a synthetic start label
408 for collect2 the first time around. */
410 static void
411 switch_to_eh_frame_section (bool back)
413 tree label;
415 #ifdef EH_FRAME_SECTION_NAME
416 if (eh_frame_section == 0)
418 int flags;
420 if (EH_TABLES_CAN_BE_READ_ONLY)
422 int fde_encoding;
423 int per_encoding;
424 int lsda_encoding;
426 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
427 /*global=*/0);
428 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
429 /*global=*/1);
430 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
431 /*global=*/0);
432 flags = ((! flag_pic
433 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
434 && (fde_encoding & 0x70) != DW_EH_PE_aligned
435 && (per_encoding & 0x70) != DW_EH_PE_absptr
436 && (per_encoding & 0x70) != DW_EH_PE_aligned
437 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
438 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
439 ? 0 : SECTION_WRITE);
441 else
442 flags = SECTION_WRITE;
443 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
445 #endif /* EH_FRAME_SECTION_NAME */
447 if (eh_frame_section)
448 switch_to_section (eh_frame_section);
449 else
451 /* We have no special eh_frame section. Put the information in
452 the data section and emit special labels to guide collect2. */
453 switch_to_section (data_section);
455 if (!back)
457 label = get_file_function_name ("F");
458 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
459 targetm.asm_out.globalize_label (asm_out_file,
460 IDENTIFIER_POINTER (label));
461 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
466 /* Switch [BACK] to the eh or debug frame table section, depending on
467 FOR_EH. */
469 static void
470 switch_to_frame_table_section (int for_eh, bool back)
472 if (for_eh)
473 switch_to_eh_frame_section (back);
474 else
476 if (!debug_frame_section)
477 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
478 SECTION_DEBUG, NULL);
479 switch_to_section (debug_frame_section);
483 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
485 enum dw_cfi_oprnd_type
486 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
488 switch (cfi)
490 case DW_CFA_nop:
491 case DW_CFA_GNU_window_save:
492 case DW_CFA_remember_state:
493 case DW_CFA_restore_state:
494 return dw_cfi_oprnd_unused;
496 case DW_CFA_set_loc:
497 case DW_CFA_advance_loc1:
498 case DW_CFA_advance_loc2:
499 case DW_CFA_advance_loc4:
500 case DW_CFA_MIPS_advance_loc8:
501 return dw_cfi_oprnd_addr;
503 case DW_CFA_offset:
504 case DW_CFA_offset_extended:
505 case DW_CFA_def_cfa:
506 case DW_CFA_offset_extended_sf:
507 case DW_CFA_def_cfa_sf:
508 case DW_CFA_restore:
509 case DW_CFA_restore_extended:
510 case DW_CFA_undefined:
511 case DW_CFA_same_value:
512 case DW_CFA_def_cfa_register:
513 case DW_CFA_register:
514 case DW_CFA_expression:
515 return dw_cfi_oprnd_reg_num;
517 case DW_CFA_def_cfa_offset:
518 case DW_CFA_GNU_args_size:
519 case DW_CFA_def_cfa_offset_sf:
520 return dw_cfi_oprnd_offset;
522 case DW_CFA_def_cfa_expression:
523 return dw_cfi_oprnd_loc;
525 default:
526 gcc_unreachable ();
530 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
532 enum dw_cfi_oprnd_type
533 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
535 switch (cfi)
537 case DW_CFA_def_cfa:
538 case DW_CFA_def_cfa_sf:
539 case DW_CFA_offset:
540 case DW_CFA_offset_extended_sf:
541 case DW_CFA_offset_extended:
542 return dw_cfi_oprnd_offset;
544 case DW_CFA_register:
545 return dw_cfi_oprnd_reg_num;
547 case DW_CFA_expression:
548 return dw_cfi_oprnd_loc;
550 default:
551 return dw_cfi_oprnd_unused;
555 /* Output one FDE. */
557 static void
558 output_fde (dw_fde_ref fde, bool for_eh, bool second,
559 char *section_start_label, int fde_encoding, char *augmentation,
560 bool any_lsda_needed, int lsda_encoding)
562 const char *begin, *end;
563 static unsigned int j;
564 char l1[20], l2[20];
566 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
567 /* empty */ 0);
568 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
569 for_eh + j);
570 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
571 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
572 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
573 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
574 " indicating 64-bit DWARF extension");
575 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
576 "FDE Length");
577 ASM_OUTPUT_LABEL (asm_out_file, l1);
579 if (for_eh)
580 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
581 else
582 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
583 debug_frame_section, "FDE CIE offset");
585 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
586 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
588 if (for_eh)
590 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
591 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
592 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
593 "FDE initial location");
594 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
595 end, begin, "FDE address range");
597 else
599 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
600 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
603 if (augmentation[0])
605 if (any_lsda_needed)
607 int size = size_of_encoded_value (lsda_encoding);
609 if (lsda_encoding == DW_EH_PE_aligned)
611 int offset = ( 4 /* Length */
612 + 4 /* CIE offset */
613 + 2 * size_of_encoded_value (fde_encoding)
614 + 1 /* Augmentation size */ );
615 int pad = -offset & (PTR_SIZE - 1);
617 size += pad;
618 gcc_assert (size_of_uleb128 (size) == 1);
621 dw2_asm_output_data_uleb128 (size, "Augmentation size");
623 if (fde->uses_eh_lsda)
625 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
626 fde->funcdef_number);
627 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
628 gen_rtx_SYMBOL_REF (Pmode, l1),
629 false,
630 "Language Specific Data Area");
632 else
634 if (lsda_encoding == DW_EH_PE_aligned)
635 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
636 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
637 "Language Specific Data Area (none)");
640 else
641 dw2_asm_output_data_uleb128 (0, "Augmentation size");
644 /* Loop through the Call Frame Instructions associated with this FDE. */
645 fde->dw_fde_current_label = begin;
647 size_t from, until, i;
649 from = 0;
650 until = vec_safe_length (fde->dw_fde_cfi);
652 if (fde->dw_fde_second_begin == NULL)
654 else if (!second)
655 until = fde->dw_fde_switch_cfi_index;
656 else
657 from = fde->dw_fde_switch_cfi_index;
659 for (i = from; i < until; i++)
660 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
663 /* If we are to emit a ref/link from function bodies to their frame tables,
664 do it now. This is typically performed to make sure that tables
665 associated with functions are dragged with them and not discarded in
666 garbage collecting links. We need to do this on a per function basis to
667 cope with -ffunction-sections. */
669 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
670 /* Switch to the function section, emit the ref to the tables, and
671 switch *back* into the table section. */
672 switch_to_section (function_section (fde->decl));
673 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
674 switch_to_frame_table_section (for_eh, true);
675 #endif
677 /* Pad the FDE out to an address sized boundary. */
678 ASM_OUTPUT_ALIGN (asm_out_file,
679 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
680 ASM_OUTPUT_LABEL (asm_out_file, l2);
682 j += 2;
685 /* Return true if frame description entry FDE is needed for EH. */
687 static bool
688 fde_needed_for_eh_p (dw_fde_ref fde)
690 if (flag_asynchronous_unwind_tables)
691 return true;
693 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
694 return true;
696 if (fde->uses_eh_lsda)
697 return true;
699 /* If exceptions are enabled, we have collected nothrow info. */
700 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
701 return false;
703 return true;
706 /* Output the call frame information used to record information
707 that relates to calculating the frame pointer, and records the
708 location of saved registers. */
710 static void
711 output_call_frame_info (int for_eh)
713 unsigned int i;
714 dw_fde_ref fde;
715 dw_cfi_ref cfi;
716 char l1[20], l2[20], section_start_label[20];
717 bool any_lsda_needed = false;
718 char augmentation[6];
719 int augmentation_size;
720 int fde_encoding = DW_EH_PE_absptr;
721 int per_encoding = DW_EH_PE_absptr;
722 int lsda_encoding = DW_EH_PE_absptr;
723 int return_reg;
724 rtx personality = NULL;
725 int dw_cie_version;
727 /* Don't emit a CIE if there won't be any FDEs. */
728 if (!fde_vec)
729 return;
731 /* Nothing to do if the assembler's doing it all. */
732 if (dwarf2out_do_cfi_asm ())
733 return;
735 /* If we don't have any functions we'll want to unwind out of, don't emit
736 any EH unwind information. If we make FDEs linkonce, we may have to
737 emit an empty label for an FDE that wouldn't otherwise be emitted. We
738 want to avoid having an FDE kept around when the function it refers to
739 is discarded. Example where this matters: a primary function template
740 in C++ requires EH information, an explicit specialization doesn't. */
741 if (for_eh)
743 bool any_eh_needed = false;
745 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
747 if (fde->uses_eh_lsda)
748 any_eh_needed = any_lsda_needed = true;
749 else if (fde_needed_for_eh_p (fde))
750 any_eh_needed = true;
751 else if (TARGET_USES_WEAK_UNWIND_INFO)
752 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
755 if (!any_eh_needed)
756 return;
759 /* We're going to be generating comments, so turn on app. */
760 if (flag_debug_asm)
761 app_enable ();
763 /* Switch to the proper frame section, first time. */
764 switch_to_frame_table_section (for_eh, false);
766 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
767 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
769 /* Output the CIE. */
770 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
771 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
772 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
773 dw2_asm_output_data (4, 0xffffffff,
774 "Initial length escape value indicating 64-bit DWARF extension");
775 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
776 "Length of Common Information Entry");
777 ASM_OUTPUT_LABEL (asm_out_file, l1);
779 /* Now that the CIE pointer is PC-relative for EH,
780 use 0 to identify the CIE. */
781 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
782 (for_eh ? 0 : DWARF_CIE_ID),
783 "CIE Identifier Tag");
785 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
786 use CIE version 1, unless that would produce incorrect results
787 due to overflowing the return register column. */
788 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
789 dw_cie_version = 1;
790 if (return_reg >= 256 || dwarf_version > 2)
791 dw_cie_version = 3;
792 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
794 augmentation[0] = 0;
795 augmentation_size = 0;
797 personality = current_unit_personality;
798 if (for_eh)
800 char *p;
802 /* Augmentation:
803 z Indicates that a uleb128 is present to size the
804 augmentation section.
805 L Indicates the encoding (and thus presence) of
806 an LSDA pointer in the FDE augmentation.
807 R Indicates a non-default pointer encoding for
808 FDE code pointers.
809 P Indicates the presence of an encoding + language
810 personality routine in the CIE augmentation. */
812 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
813 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
814 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
816 p = augmentation + 1;
817 if (personality)
819 *p++ = 'P';
820 augmentation_size += 1 + size_of_encoded_value (per_encoding);
821 assemble_external_libcall (personality);
823 if (any_lsda_needed)
825 *p++ = 'L';
826 augmentation_size += 1;
828 if (fde_encoding != DW_EH_PE_absptr)
830 *p++ = 'R';
831 augmentation_size += 1;
833 if (p > augmentation + 1)
835 augmentation[0] = 'z';
836 *p = '\0';
839 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
840 if (personality && per_encoding == DW_EH_PE_aligned)
842 int offset = ( 4 /* Length */
843 + 4 /* CIE Id */
844 + 1 /* CIE version */
845 + strlen (augmentation) + 1 /* Augmentation */
846 + size_of_uleb128 (1) /* Code alignment */
847 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
848 + 1 /* RA column */
849 + 1 /* Augmentation size */
850 + 1 /* Personality encoding */ );
851 int pad = -offset & (PTR_SIZE - 1);
853 augmentation_size += pad;
855 /* Augmentations should be small, so there's scarce need to
856 iterate for a solution. Die if we exceed one uleb128 byte. */
857 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
861 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
862 if (dw_cie_version >= 4)
864 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
865 dw2_asm_output_data (1, 0, "CIE Segment Size");
867 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
868 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
869 "CIE Data Alignment Factor");
871 if (dw_cie_version == 1)
872 dw2_asm_output_data (1, return_reg, "CIE RA Column");
873 else
874 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
876 if (augmentation[0])
878 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
879 if (personality)
881 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
882 eh_data_format_name (per_encoding));
883 dw2_asm_output_encoded_addr_rtx (per_encoding,
884 personality,
885 true, NULL);
888 if (any_lsda_needed)
889 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
890 eh_data_format_name (lsda_encoding));
892 if (fde_encoding != DW_EH_PE_absptr)
893 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
894 eh_data_format_name (fde_encoding));
897 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
898 output_cfi (cfi, NULL, for_eh);
900 /* Pad the CIE out to an address sized boundary. */
901 ASM_OUTPUT_ALIGN (asm_out_file,
902 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
903 ASM_OUTPUT_LABEL (asm_out_file, l2);
905 /* Loop through all of the FDE's. */
906 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
908 unsigned int k;
910 /* Don't emit EH unwind info for leaf functions that don't need it. */
911 if (for_eh && !fde_needed_for_eh_p (fde))
912 continue;
914 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
915 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
916 augmentation, any_lsda_needed, lsda_encoding);
919 if (for_eh && targetm.terminate_dw2_eh_frame_info)
920 dw2_asm_output_data (4, 0, "End of Table");
922 /* Turn off app to make assembly quicker. */
923 if (flag_debug_asm)
924 app_disable ();
927 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
929 static void
930 dwarf2out_do_cfi_startproc (bool second)
932 int enc;
933 rtx ref;
934 rtx personality = get_personality_function (current_function_decl);
936 fprintf (asm_out_file, "\t.cfi_startproc\n");
938 if (personality)
940 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
941 ref = personality;
943 /* ??? The GAS support isn't entirely consistent. We have to
944 handle indirect support ourselves, but PC-relative is done
945 in the assembler. Further, the assembler can't handle any
946 of the weirder relocation types. */
947 if (enc & DW_EH_PE_indirect)
948 ref = dw2_force_const_mem (ref, true);
950 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
951 output_addr_const (asm_out_file, ref);
952 fputc ('\n', asm_out_file);
955 if (crtl->uses_eh_lsda)
957 char lab[20];
959 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
960 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
961 current_function_funcdef_no);
962 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
963 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
965 if (enc & DW_EH_PE_indirect)
966 ref = dw2_force_const_mem (ref, true);
968 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
969 output_addr_const (asm_out_file, ref);
970 fputc ('\n', asm_out_file);
974 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
975 this allocation may be done before pass_final. */
977 dw_fde_ref
978 dwarf2out_alloc_current_fde (void)
980 dw_fde_ref fde;
982 fde = ggc_alloc_cleared_dw_fde_node ();
983 fde->decl = current_function_decl;
984 fde->funcdef_number = current_function_funcdef_no;
985 fde->fde_index = vec_safe_length (fde_vec);
986 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
987 fde->uses_eh_lsda = crtl->uses_eh_lsda;
988 fde->nothrow = crtl->nothrow;
989 fde->drap_reg = INVALID_REGNUM;
990 fde->vdrap_reg = INVALID_REGNUM;
992 /* Record the FDE associated with this function. */
993 cfun->fde = fde;
994 vec_safe_push (fde_vec, fde);
996 return fde;
999 /* Output a marker (i.e. a label) for the beginning of a function, before
1000 the prologue. */
1002 void
1003 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1004 const char *file ATTRIBUTE_UNUSED)
1006 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1007 char * dup_label;
1008 dw_fde_ref fde;
1009 section *fnsec;
1010 bool do_frame;
1012 current_function_func_begin_label = NULL;
1014 do_frame = dwarf2out_do_frame ();
1016 /* ??? current_function_func_begin_label is also used by except.c for
1017 call-site information. We must emit this label if it might be used. */
1018 if (!do_frame
1019 && (!flag_exceptions
1020 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1021 return;
1023 fnsec = function_section (current_function_decl);
1024 switch_to_section (fnsec);
1025 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1026 current_function_funcdef_no);
1027 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1028 current_function_funcdef_no);
1029 dup_label = xstrdup (label);
1030 current_function_func_begin_label = dup_label;
1032 /* We can elide the fde allocation if we're not emitting debug info. */
1033 if (!do_frame)
1034 return;
1036 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1037 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1038 would include pass_dwarf2_frame. If we've not created the FDE yet,
1039 do so now. */
1040 fde = cfun->fde;
1041 if (fde == NULL)
1042 fde = dwarf2out_alloc_current_fde ();
1044 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1045 fde->dw_fde_begin = dup_label;
1046 fde->dw_fde_current_label = dup_label;
1047 fde->in_std_section = (fnsec == text_section
1048 || (cold_text_section && fnsec == cold_text_section));
1050 /* We only want to output line number information for the genuine dwarf2
1051 prologue case, not the eh frame case. */
1052 #ifdef DWARF2_DEBUGGING_INFO
1053 if (file)
1054 dwarf2out_source_line (line, file, 0, true);
1055 #endif
1057 if (dwarf2out_do_cfi_asm ())
1058 dwarf2out_do_cfi_startproc (false);
1059 else
1061 rtx personality = get_personality_function (current_function_decl);
1062 if (!current_unit_personality)
1063 current_unit_personality = personality;
1065 /* We cannot keep a current personality per function as without CFI
1066 asm, at the point where we emit the CFI data, there is no current
1067 function anymore. */
1068 if (personality && current_unit_personality != personality)
1069 sorry ("multiple EH personalities are supported only with assemblers "
1070 "supporting .cfi_personality directive");
1074 /* Output a marker (i.e. a label) for the end of the generated code
1075 for a function prologue. This gets called *after* the prologue code has
1076 been generated. */
1078 void
1079 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1080 const char *file ATTRIBUTE_UNUSED)
1082 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1084 /* Output a label to mark the endpoint of the code generated for this
1085 function. */
1086 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1087 current_function_funcdef_no);
1088 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1089 current_function_funcdef_no);
1090 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1093 /* Output a marker (i.e. a label) for the beginning of the generated code
1094 for a function epilogue. This gets called *before* the prologue code has
1095 been generated. */
1097 void
1098 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1099 const char *file ATTRIBUTE_UNUSED)
1101 dw_fde_ref fde = cfun->fde;
1102 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1104 if (fde->dw_fde_vms_begin_epilogue)
1105 return;
1107 /* Output a label to mark the endpoint of the code generated for this
1108 function. */
1109 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1110 current_function_funcdef_no);
1111 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1112 current_function_funcdef_no);
1113 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1116 /* Output a marker (i.e. a label) for the absolute end of the generated code
1117 for a function definition. This gets called *after* the epilogue code has
1118 been generated. */
1120 void
1121 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1122 const char *file ATTRIBUTE_UNUSED)
1124 dw_fde_ref fde;
1125 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1127 last_var_location_insn = NULL_RTX;
1128 cached_next_real_insn = NULL_RTX;
1130 if (dwarf2out_do_cfi_asm ())
1131 fprintf (asm_out_file, "\t.cfi_endproc\n");
1133 /* Output a label to mark the endpoint of the code generated for this
1134 function. */
1135 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1136 current_function_funcdef_no);
1137 ASM_OUTPUT_LABEL (asm_out_file, label);
1138 fde = cfun->fde;
1139 gcc_assert (fde != NULL);
1140 if (fde->dw_fde_second_begin == NULL)
1141 fde->dw_fde_end = xstrdup (label);
1144 void
1145 dwarf2out_frame_finish (void)
1147 /* Output call frame information. */
1148 if (targetm.debug_unwind_info () == UI_DWARF2)
1149 output_call_frame_info (0);
1151 /* Output another copy for the unwinder. */
1152 if ((flag_unwind_tables || flag_exceptions)
1153 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1154 output_call_frame_info (1);
1157 /* Note that the current function section is being used for code. */
1159 static void
1160 dwarf2out_note_section_used (void)
1162 section *sec = current_function_section ();
1163 if (sec == text_section)
1164 text_section_used = true;
1165 else if (sec == cold_text_section)
1166 cold_text_section_used = true;
1169 static void var_location_switch_text_section (void);
1170 static void set_cur_line_info_table (section *);
1172 void
1173 dwarf2out_switch_text_section (void)
1175 section *sect;
1176 dw_fde_ref fde = cfun->fde;
1178 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1180 if (!in_cold_section_p)
1182 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1183 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1184 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1186 else
1188 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1189 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1190 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1192 have_multiple_function_sections = true;
1194 /* There is no need to mark used sections when not debugging. */
1195 if (cold_text_section != NULL)
1196 dwarf2out_note_section_used ();
1198 if (dwarf2out_do_cfi_asm ())
1199 fprintf (asm_out_file, "\t.cfi_endproc\n");
1201 /* Now do the real section switch. */
1202 sect = current_function_section ();
1203 switch_to_section (sect);
1205 fde->second_in_std_section
1206 = (sect == text_section
1207 || (cold_text_section && sect == cold_text_section));
1209 if (dwarf2out_do_cfi_asm ())
1210 dwarf2out_do_cfi_startproc (true);
1212 var_location_switch_text_section ();
1214 if (cold_text_section != NULL)
1215 set_cur_line_info_table (sect);
1218 /* And now, the subset of the debugging information support code necessary
1219 for emitting location expressions. */
1221 /* Data about a single source file. */
1222 struct GTY(()) dwarf_file_data {
1223 const char * filename;
1224 int emitted_number;
1227 typedef struct GTY(()) deferred_locations_struct
1229 tree variable;
1230 dw_die_ref die;
1231 } deferred_locations;
1234 static GTY(()) vec<deferred_locations, va_gc> *deferred_locations_list;
1237 /* Describe an entry into the .debug_addr section. */
1239 enum ate_kind {
1240 ate_kind_rtx,
1241 ate_kind_rtx_dtprel,
1242 ate_kind_label
1245 typedef struct GTY(()) addr_table_entry_struct {
1246 enum ate_kind kind;
1247 unsigned int refcount;
1248 unsigned int index;
1249 union addr_table_entry_struct_union
1251 rtx GTY ((tag ("0"))) rtl;
1252 char * GTY ((tag ("1"))) label;
1254 GTY ((desc ("%1.kind"))) addr;
1256 addr_table_entry;
1258 /* Location lists are ranges + location descriptions for that range,
1259 so you can track variables that are in different places over
1260 their entire life. */
1261 typedef struct GTY(()) dw_loc_list_struct {
1262 dw_loc_list_ref dw_loc_next;
1263 const char *begin; /* Label and addr_entry for start of range */
1264 addr_table_entry *begin_entry;
1265 const char *end; /* Label for end of range */
1266 char *ll_symbol; /* Label for beginning of location list.
1267 Only on head of list */
1268 const char *section; /* Section this loclist is relative to */
1269 dw_loc_descr_ref expr;
1270 hashval_t hash;
1271 /* True if all addresses in this and subsequent lists are known to be
1272 resolved. */
1273 bool resolved_addr;
1274 /* True if this list has been replaced by dw_loc_next. */
1275 bool replaced;
1276 bool emitted;
1277 /* True if the range should be emitted even if begin and end
1278 are the same. */
1279 bool force;
1280 } dw_loc_list_node;
1282 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1284 /* Convert a DWARF stack opcode into its string name. */
1286 static const char *
1287 dwarf_stack_op_name (unsigned int op)
1289 const char *name = get_DW_OP_name (op);
1291 if (name != NULL)
1292 return name;
1294 return "OP_<unknown>";
1297 /* Return a pointer to a newly allocated location description. Location
1298 descriptions are simple expression terms that can be strung
1299 together to form more complicated location (address) descriptions. */
1301 static inline dw_loc_descr_ref
1302 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1303 unsigned HOST_WIDE_INT oprnd2)
1305 dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
1307 descr->dw_loc_opc = op;
1308 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1309 descr->dw_loc_oprnd1.val_entry = NULL;
1310 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1311 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1312 descr->dw_loc_oprnd2.val_entry = NULL;
1313 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1315 return descr;
1318 /* Return a pointer to a newly allocated location description for
1319 REG and OFFSET. */
1321 static inline dw_loc_descr_ref
1322 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1324 if (reg <= 31)
1325 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1326 offset, 0);
1327 else
1328 return new_loc_descr (DW_OP_bregx, reg, offset);
1331 /* Add a location description term to a location description expression. */
1333 static inline void
1334 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1336 dw_loc_descr_ref *d;
1338 /* Find the end of the chain. */
1339 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1342 *d = descr;
1345 /* Compare two location operands for exact equality. */
1347 static bool
1348 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1350 if (a->val_class != b->val_class)
1351 return false;
1352 switch (a->val_class)
1354 case dw_val_class_none:
1355 return true;
1356 case dw_val_class_addr:
1357 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1359 case dw_val_class_offset:
1360 case dw_val_class_unsigned_const:
1361 case dw_val_class_const:
1362 case dw_val_class_range_list:
1363 case dw_val_class_lineptr:
1364 case dw_val_class_macptr:
1365 /* These are all HOST_WIDE_INT, signed or unsigned. */
1366 return a->v.val_unsigned == b->v.val_unsigned;
1368 case dw_val_class_loc:
1369 return a->v.val_loc == b->v.val_loc;
1370 case dw_val_class_loc_list:
1371 return a->v.val_loc_list == b->v.val_loc_list;
1372 case dw_val_class_die_ref:
1373 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1374 case dw_val_class_fde_ref:
1375 return a->v.val_fde_index == b->v.val_fde_index;
1376 case dw_val_class_lbl_id:
1377 case dw_val_class_high_pc:
1378 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1379 case dw_val_class_str:
1380 return a->v.val_str == b->v.val_str;
1381 case dw_val_class_flag:
1382 return a->v.val_flag == b->v.val_flag;
1383 case dw_val_class_file:
1384 return a->v.val_file == b->v.val_file;
1385 case dw_val_class_decl_ref:
1386 return a->v.val_decl_ref == b->v.val_decl_ref;
1388 case dw_val_class_const_double:
1389 return (a->v.val_double.high == b->v.val_double.high
1390 && a->v.val_double.low == b->v.val_double.low);
1392 case dw_val_class_vec:
1394 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1395 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1397 return (a_len == b_len
1398 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1401 case dw_val_class_data8:
1402 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1404 case dw_val_class_vms_delta:
1405 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1406 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1408 gcc_unreachable ();
1411 /* Compare two location atoms for exact equality. */
1413 static bool
1414 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1416 if (a->dw_loc_opc != b->dw_loc_opc)
1417 return false;
1419 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1420 address size, but since we always allocate cleared storage it
1421 should be zero for other types of locations. */
1422 if (a->dtprel != b->dtprel)
1423 return false;
1425 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1426 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1429 /* Compare two complete location expressions for exact equality. */
1431 bool
1432 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1434 while (1)
1436 if (a == b)
1437 return true;
1438 if (a == NULL || b == NULL)
1439 return false;
1440 if (!loc_descr_equal_p_1 (a, b))
1441 return false;
1443 a = a->dw_loc_next;
1444 b = b->dw_loc_next;
1449 /* Add a constant OFFSET to a location expression. */
1451 static void
1452 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1454 dw_loc_descr_ref loc;
1455 HOST_WIDE_INT *p;
1457 gcc_assert (*list_head != NULL);
1459 if (!offset)
1460 return;
1462 /* Find the end of the chain. */
1463 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1466 p = NULL;
1467 if (loc->dw_loc_opc == DW_OP_fbreg
1468 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1469 p = &loc->dw_loc_oprnd1.v.val_int;
1470 else if (loc->dw_loc_opc == DW_OP_bregx)
1471 p = &loc->dw_loc_oprnd2.v.val_int;
1473 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1474 offset. Don't optimize if an signed integer overflow would happen. */
1475 if (p != NULL
1476 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1477 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1478 *p += offset;
1480 else if (offset > 0)
1481 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1483 else
1485 loc->dw_loc_next = int_loc_descriptor (-offset);
1486 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1490 /* Add a constant OFFSET to a location list. */
1492 static void
1493 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1495 dw_loc_list_ref d;
1496 for (d = list_head; d != NULL; d = d->dw_loc_next)
1497 loc_descr_plus_const (&d->expr, offset);
1500 #define DWARF_REF_SIZE \
1501 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1503 static unsigned long int get_base_type_offset (dw_die_ref);
1505 /* Return the size of a location descriptor. */
1507 static unsigned long
1508 size_of_loc_descr (dw_loc_descr_ref loc)
1510 unsigned long size = 1;
1512 switch (loc->dw_loc_opc)
1514 case DW_OP_addr:
1515 size += DWARF2_ADDR_SIZE;
1516 break;
1517 case DW_OP_GNU_addr_index:
1518 case DW_OP_GNU_const_index:
1519 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1520 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1521 break;
1522 case DW_OP_const1u:
1523 case DW_OP_const1s:
1524 size += 1;
1525 break;
1526 case DW_OP_const2u:
1527 case DW_OP_const2s:
1528 size += 2;
1529 break;
1530 case DW_OP_const4u:
1531 case DW_OP_const4s:
1532 size += 4;
1533 break;
1534 case DW_OP_const8u:
1535 case DW_OP_const8s:
1536 size += 8;
1537 break;
1538 case DW_OP_constu:
1539 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1540 break;
1541 case DW_OP_consts:
1542 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1543 break;
1544 case DW_OP_pick:
1545 size += 1;
1546 break;
1547 case DW_OP_plus_uconst:
1548 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1549 break;
1550 case DW_OP_skip:
1551 case DW_OP_bra:
1552 size += 2;
1553 break;
1554 case DW_OP_breg0:
1555 case DW_OP_breg1:
1556 case DW_OP_breg2:
1557 case DW_OP_breg3:
1558 case DW_OP_breg4:
1559 case DW_OP_breg5:
1560 case DW_OP_breg6:
1561 case DW_OP_breg7:
1562 case DW_OP_breg8:
1563 case DW_OP_breg9:
1564 case DW_OP_breg10:
1565 case DW_OP_breg11:
1566 case DW_OP_breg12:
1567 case DW_OP_breg13:
1568 case DW_OP_breg14:
1569 case DW_OP_breg15:
1570 case DW_OP_breg16:
1571 case DW_OP_breg17:
1572 case DW_OP_breg18:
1573 case DW_OP_breg19:
1574 case DW_OP_breg20:
1575 case DW_OP_breg21:
1576 case DW_OP_breg22:
1577 case DW_OP_breg23:
1578 case DW_OP_breg24:
1579 case DW_OP_breg25:
1580 case DW_OP_breg26:
1581 case DW_OP_breg27:
1582 case DW_OP_breg28:
1583 case DW_OP_breg29:
1584 case DW_OP_breg30:
1585 case DW_OP_breg31:
1586 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1587 break;
1588 case DW_OP_regx:
1589 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1590 break;
1591 case DW_OP_fbreg:
1592 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1593 break;
1594 case DW_OP_bregx:
1595 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1596 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1597 break;
1598 case DW_OP_piece:
1599 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1600 break;
1601 case DW_OP_bit_piece:
1602 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1603 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1604 break;
1605 case DW_OP_deref_size:
1606 case DW_OP_xderef_size:
1607 size += 1;
1608 break;
1609 case DW_OP_call2:
1610 size += 2;
1611 break;
1612 case DW_OP_call4:
1613 size += 4;
1614 break;
1615 case DW_OP_call_ref:
1616 size += DWARF_REF_SIZE;
1617 break;
1618 case DW_OP_implicit_value:
1619 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1620 + loc->dw_loc_oprnd1.v.val_unsigned;
1621 break;
1622 case DW_OP_GNU_implicit_pointer:
1623 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1624 break;
1625 case DW_OP_GNU_entry_value:
1627 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1628 size += size_of_uleb128 (op_size) + op_size;
1629 break;
1631 case DW_OP_GNU_const_type:
1633 unsigned long o
1634 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1635 size += size_of_uleb128 (o) + 1;
1636 switch (loc->dw_loc_oprnd2.val_class)
1638 case dw_val_class_vec:
1639 size += loc->dw_loc_oprnd2.v.val_vec.length
1640 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1641 break;
1642 case dw_val_class_const:
1643 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1644 break;
1645 case dw_val_class_const_double:
1646 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1647 break;
1648 default:
1649 gcc_unreachable ();
1651 break;
1653 case DW_OP_GNU_regval_type:
1655 unsigned long o
1656 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1657 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1658 + size_of_uleb128 (o);
1660 break;
1661 case DW_OP_GNU_deref_type:
1663 unsigned long o
1664 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1665 size += 1 + size_of_uleb128 (o);
1667 break;
1668 case DW_OP_GNU_convert:
1669 case DW_OP_GNU_reinterpret:
1670 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1671 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1672 else
1674 unsigned long o
1675 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1676 size += size_of_uleb128 (o);
1678 break;
1679 case DW_OP_GNU_parameter_ref:
1680 size += 4;
1681 break;
1682 default:
1683 break;
1686 return size;
1689 /* Return the size of a series of location descriptors. */
1691 unsigned long
1692 size_of_locs (dw_loc_descr_ref loc)
1694 dw_loc_descr_ref l;
1695 unsigned long size;
1697 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1698 field, to avoid writing to a PCH file. */
1699 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1701 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1702 break;
1703 size += size_of_loc_descr (l);
1705 if (! l)
1706 return size;
1708 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1710 l->dw_loc_addr = size;
1711 size += size_of_loc_descr (l);
1714 return size;
1717 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1718 static void get_ref_die_offset_label (char *, dw_die_ref);
1719 static unsigned long int get_ref_die_offset (dw_die_ref);
1721 /* Output location description stack opcode's operands (if any).
1722 The for_eh_or_skip parameter controls whether register numbers are
1723 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1724 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1725 info). This should be suppressed for the cases that have not been converted
1726 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1728 static void
1729 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1731 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1732 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1734 switch (loc->dw_loc_opc)
1736 #ifdef DWARF2_DEBUGGING_INFO
1737 case DW_OP_const2u:
1738 case DW_OP_const2s:
1739 dw2_asm_output_data (2, val1->v.val_int, NULL);
1740 break;
1741 case DW_OP_const4u:
1742 if (loc->dtprel)
1744 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1745 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1746 val1->v.val_addr);
1747 fputc ('\n', asm_out_file);
1748 break;
1750 /* FALLTHRU */
1751 case DW_OP_const4s:
1752 dw2_asm_output_data (4, val1->v.val_int, NULL);
1753 break;
1754 case DW_OP_const8u:
1755 if (loc->dtprel)
1757 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1758 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1759 val1->v.val_addr);
1760 fputc ('\n', asm_out_file);
1761 break;
1763 /* FALLTHRU */
1764 case DW_OP_const8s:
1765 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1766 dw2_asm_output_data (8, val1->v.val_int, NULL);
1767 break;
1768 case DW_OP_skip:
1769 case DW_OP_bra:
1771 int offset;
1773 gcc_assert (val1->val_class == dw_val_class_loc);
1774 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1776 dw2_asm_output_data (2, offset, NULL);
1778 break;
1779 case DW_OP_implicit_value:
1780 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1781 switch (val2->val_class)
1783 case dw_val_class_const:
1784 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1785 break;
1786 case dw_val_class_vec:
1788 unsigned int elt_size = val2->v.val_vec.elt_size;
1789 unsigned int len = val2->v.val_vec.length;
1790 unsigned int i;
1791 unsigned char *p;
1793 if (elt_size > sizeof (HOST_WIDE_INT))
1795 elt_size /= 2;
1796 len *= 2;
1798 for (i = 0, p = val2->v.val_vec.array;
1799 i < len;
1800 i++, p += elt_size)
1801 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1802 "fp or vector constant word %u", i);
1804 break;
1805 case dw_val_class_const_double:
1807 unsigned HOST_WIDE_INT first, second;
1809 if (WORDS_BIG_ENDIAN)
1811 first = val2->v.val_double.high;
1812 second = val2->v.val_double.low;
1814 else
1816 first = val2->v.val_double.low;
1817 second = val2->v.val_double.high;
1819 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1820 first, NULL);
1821 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1822 second, NULL);
1824 break;
1825 case dw_val_class_addr:
1826 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1827 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1828 break;
1829 default:
1830 gcc_unreachable ();
1832 break;
1833 #else
1834 case DW_OP_const2u:
1835 case DW_OP_const2s:
1836 case DW_OP_const4u:
1837 case DW_OP_const4s:
1838 case DW_OP_const8u:
1839 case DW_OP_const8s:
1840 case DW_OP_skip:
1841 case DW_OP_bra:
1842 case DW_OP_implicit_value:
1843 /* We currently don't make any attempt to make sure these are
1844 aligned properly like we do for the main unwind info, so
1845 don't support emitting things larger than a byte if we're
1846 only doing unwinding. */
1847 gcc_unreachable ();
1848 #endif
1849 case DW_OP_const1u:
1850 case DW_OP_const1s:
1851 dw2_asm_output_data (1, val1->v.val_int, NULL);
1852 break;
1853 case DW_OP_constu:
1854 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1855 break;
1856 case DW_OP_consts:
1857 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1858 break;
1859 case DW_OP_pick:
1860 dw2_asm_output_data (1, val1->v.val_int, NULL);
1861 break;
1862 case DW_OP_plus_uconst:
1863 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1864 break;
1865 case DW_OP_breg0:
1866 case DW_OP_breg1:
1867 case DW_OP_breg2:
1868 case DW_OP_breg3:
1869 case DW_OP_breg4:
1870 case DW_OP_breg5:
1871 case DW_OP_breg6:
1872 case DW_OP_breg7:
1873 case DW_OP_breg8:
1874 case DW_OP_breg9:
1875 case DW_OP_breg10:
1876 case DW_OP_breg11:
1877 case DW_OP_breg12:
1878 case DW_OP_breg13:
1879 case DW_OP_breg14:
1880 case DW_OP_breg15:
1881 case DW_OP_breg16:
1882 case DW_OP_breg17:
1883 case DW_OP_breg18:
1884 case DW_OP_breg19:
1885 case DW_OP_breg20:
1886 case DW_OP_breg21:
1887 case DW_OP_breg22:
1888 case DW_OP_breg23:
1889 case DW_OP_breg24:
1890 case DW_OP_breg25:
1891 case DW_OP_breg26:
1892 case DW_OP_breg27:
1893 case DW_OP_breg28:
1894 case DW_OP_breg29:
1895 case DW_OP_breg30:
1896 case DW_OP_breg31:
1897 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1898 break;
1899 case DW_OP_regx:
1901 unsigned r = val1->v.val_unsigned;
1902 if (for_eh_or_skip >= 0)
1903 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1904 gcc_assert (size_of_uleb128 (r)
1905 == size_of_uleb128 (val1->v.val_unsigned));
1906 dw2_asm_output_data_uleb128 (r, NULL);
1908 break;
1909 case DW_OP_fbreg:
1910 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1911 break;
1912 case DW_OP_bregx:
1914 unsigned r = val1->v.val_unsigned;
1915 if (for_eh_or_skip >= 0)
1916 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1917 gcc_assert (size_of_uleb128 (r)
1918 == size_of_uleb128 (val1->v.val_unsigned));
1919 dw2_asm_output_data_uleb128 (r, NULL);
1920 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1922 break;
1923 case DW_OP_piece:
1924 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1925 break;
1926 case DW_OP_bit_piece:
1927 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1928 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1929 break;
1930 case DW_OP_deref_size:
1931 case DW_OP_xderef_size:
1932 dw2_asm_output_data (1, val1->v.val_int, NULL);
1933 break;
1935 case DW_OP_addr:
1936 if (loc->dtprel)
1938 if (targetm.asm_out.output_dwarf_dtprel)
1940 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
1941 DWARF2_ADDR_SIZE,
1942 val1->v.val_addr);
1943 fputc ('\n', asm_out_file);
1945 else
1946 gcc_unreachable ();
1948 else
1950 #ifdef DWARF2_DEBUGGING_INFO
1951 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
1952 #else
1953 gcc_unreachable ();
1954 #endif
1956 break;
1958 case DW_OP_GNU_addr_index:
1959 case DW_OP_GNU_const_index:
1960 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1961 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
1962 "(index into .debug_addr)");
1963 break;
1965 case DW_OP_GNU_implicit_pointer:
1967 char label[MAX_ARTIFICIAL_LABEL_BYTES
1968 + HOST_BITS_PER_WIDE_INT / 2 + 2];
1969 gcc_assert (val1->val_class == dw_val_class_die_ref);
1970 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
1971 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
1972 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1974 break;
1976 case DW_OP_GNU_entry_value:
1977 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
1978 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
1979 break;
1981 case DW_OP_GNU_const_type:
1983 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
1984 gcc_assert (o);
1985 dw2_asm_output_data_uleb128 (o, NULL);
1986 switch (val2->val_class)
1988 case dw_val_class_const:
1989 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
1990 dw2_asm_output_data (1, l, NULL);
1991 dw2_asm_output_data (l, val2->v.val_int, NULL);
1992 break;
1993 case dw_val_class_vec:
1995 unsigned int elt_size = val2->v.val_vec.elt_size;
1996 unsigned int len = val2->v.val_vec.length;
1997 unsigned int i;
1998 unsigned char *p;
2000 l = len * elt_size;
2001 dw2_asm_output_data (1, l, NULL);
2002 if (elt_size > sizeof (HOST_WIDE_INT))
2004 elt_size /= 2;
2005 len *= 2;
2007 for (i = 0, p = val2->v.val_vec.array;
2008 i < len;
2009 i++, p += elt_size)
2010 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2011 "fp or vector constant word %u", i);
2013 break;
2014 case dw_val_class_const_double:
2016 unsigned HOST_WIDE_INT first, second;
2017 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2019 dw2_asm_output_data (1, 2 * l, NULL);
2020 if (WORDS_BIG_ENDIAN)
2022 first = val2->v.val_double.high;
2023 second = val2->v.val_double.low;
2025 else
2027 first = val2->v.val_double.low;
2028 second = val2->v.val_double.high;
2030 dw2_asm_output_data (l, first, NULL);
2031 dw2_asm_output_data (l, second, NULL);
2033 break;
2034 default:
2035 gcc_unreachable ();
2038 break;
2039 case DW_OP_GNU_regval_type:
2041 unsigned r = val1->v.val_unsigned;
2042 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2043 gcc_assert (o);
2044 if (for_eh_or_skip >= 0)
2046 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2047 gcc_assert (size_of_uleb128 (r)
2048 == size_of_uleb128 (val1->v.val_unsigned));
2050 dw2_asm_output_data_uleb128 (r, NULL);
2051 dw2_asm_output_data_uleb128 (o, NULL);
2053 break;
2054 case DW_OP_GNU_deref_type:
2056 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2057 gcc_assert (o);
2058 dw2_asm_output_data (1, val1->v.val_int, NULL);
2059 dw2_asm_output_data_uleb128 (o, NULL);
2061 break;
2062 case DW_OP_GNU_convert:
2063 case DW_OP_GNU_reinterpret:
2064 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2065 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2066 else
2068 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2069 gcc_assert (o);
2070 dw2_asm_output_data_uleb128 (o, NULL);
2072 break;
2074 case DW_OP_GNU_parameter_ref:
2076 unsigned long o;
2077 gcc_assert (val1->val_class == dw_val_class_die_ref);
2078 o = get_ref_die_offset (val1->v.val_die_ref.die);
2079 dw2_asm_output_data (4, o, NULL);
2081 break;
2083 default:
2084 /* Other codes have no operands. */
2085 break;
2089 /* Output a sequence of location operations.
2090 The for_eh_or_skip parameter controls whether register numbers are
2091 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2092 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2093 info). This should be suppressed for the cases that have not been converted
2094 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2096 void
2097 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2099 for (; loc != NULL; loc = loc->dw_loc_next)
2101 enum dwarf_location_atom opc = loc->dw_loc_opc;
2102 /* Output the opcode. */
2103 if (for_eh_or_skip >= 0
2104 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2106 unsigned r = (opc - DW_OP_breg0);
2107 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2108 gcc_assert (r <= 31);
2109 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2111 else if (for_eh_or_skip >= 0
2112 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2114 unsigned r = (opc - DW_OP_reg0);
2115 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2116 gcc_assert (r <= 31);
2117 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2120 dw2_asm_output_data (1, opc,
2121 "%s", dwarf_stack_op_name (opc));
2123 /* Output the operand(s) (if any). */
2124 output_loc_operands (loc, for_eh_or_skip);
2128 /* Output location description stack opcode's operands (if any).
2129 The output is single bytes on a line, suitable for .cfi_escape. */
2131 static void
2132 output_loc_operands_raw (dw_loc_descr_ref loc)
2134 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2135 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2137 switch (loc->dw_loc_opc)
2139 case DW_OP_addr:
2140 case DW_OP_GNU_addr_index:
2141 case DW_OP_GNU_const_index:
2142 case DW_OP_implicit_value:
2143 /* We cannot output addresses in .cfi_escape, only bytes. */
2144 gcc_unreachable ();
2146 case DW_OP_const1u:
2147 case DW_OP_const1s:
2148 case DW_OP_pick:
2149 case DW_OP_deref_size:
2150 case DW_OP_xderef_size:
2151 fputc (',', asm_out_file);
2152 dw2_asm_output_data_raw (1, val1->v.val_int);
2153 break;
2155 case DW_OP_const2u:
2156 case DW_OP_const2s:
2157 fputc (',', asm_out_file);
2158 dw2_asm_output_data_raw (2, val1->v.val_int);
2159 break;
2161 case DW_OP_const4u:
2162 case DW_OP_const4s:
2163 fputc (',', asm_out_file);
2164 dw2_asm_output_data_raw (4, val1->v.val_int);
2165 break;
2167 case DW_OP_const8u:
2168 case DW_OP_const8s:
2169 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2170 fputc (',', asm_out_file);
2171 dw2_asm_output_data_raw (8, val1->v.val_int);
2172 break;
2174 case DW_OP_skip:
2175 case DW_OP_bra:
2177 int offset;
2179 gcc_assert (val1->val_class == dw_val_class_loc);
2180 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2182 fputc (',', asm_out_file);
2183 dw2_asm_output_data_raw (2, offset);
2185 break;
2187 case DW_OP_regx:
2189 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2190 gcc_assert (size_of_uleb128 (r)
2191 == size_of_uleb128 (val1->v.val_unsigned));
2192 fputc (',', asm_out_file);
2193 dw2_asm_output_data_uleb128_raw (r);
2195 break;
2197 case DW_OP_constu:
2198 case DW_OP_plus_uconst:
2199 case DW_OP_piece:
2200 fputc (',', asm_out_file);
2201 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2202 break;
2204 case DW_OP_bit_piece:
2205 fputc (',', asm_out_file);
2206 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2207 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2208 break;
2210 case DW_OP_consts:
2211 case DW_OP_breg0:
2212 case DW_OP_breg1:
2213 case DW_OP_breg2:
2214 case DW_OP_breg3:
2215 case DW_OP_breg4:
2216 case DW_OP_breg5:
2217 case DW_OP_breg6:
2218 case DW_OP_breg7:
2219 case DW_OP_breg8:
2220 case DW_OP_breg9:
2221 case DW_OP_breg10:
2222 case DW_OP_breg11:
2223 case DW_OP_breg12:
2224 case DW_OP_breg13:
2225 case DW_OP_breg14:
2226 case DW_OP_breg15:
2227 case DW_OP_breg16:
2228 case DW_OP_breg17:
2229 case DW_OP_breg18:
2230 case DW_OP_breg19:
2231 case DW_OP_breg20:
2232 case DW_OP_breg21:
2233 case DW_OP_breg22:
2234 case DW_OP_breg23:
2235 case DW_OP_breg24:
2236 case DW_OP_breg25:
2237 case DW_OP_breg26:
2238 case DW_OP_breg27:
2239 case DW_OP_breg28:
2240 case DW_OP_breg29:
2241 case DW_OP_breg30:
2242 case DW_OP_breg31:
2243 case DW_OP_fbreg:
2244 fputc (',', asm_out_file);
2245 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2246 break;
2248 case DW_OP_bregx:
2250 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2251 gcc_assert (size_of_uleb128 (r)
2252 == size_of_uleb128 (val1->v.val_unsigned));
2253 fputc (',', asm_out_file);
2254 dw2_asm_output_data_uleb128_raw (r);
2255 fputc (',', asm_out_file);
2256 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2258 break;
2260 case DW_OP_GNU_implicit_pointer:
2261 case DW_OP_GNU_entry_value:
2262 case DW_OP_GNU_const_type:
2263 case DW_OP_GNU_regval_type:
2264 case DW_OP_GNU_deref_type:
2265 case DW_OP_GNU_convert:
2266 case DW_OP_GNU_reinterpret:
2267 case DW_OP_GNU_parameter_ref:
2268 gcc_unreachable ();
2269 break;
2271 default:
2272 /* Other codes have no operands. */
2273 break;
2277 void
2278 output_loc_sequence_raw (dw_loc_descr_ref loc)
2280 while (1)
2282 enum dwarf_location_atom opc = loc->dw_loc_opc;
2283 /* Output the opcode. */
2284 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2286 unsigned r = (opc - DW_OP_breg0);
2287 r = DWARF2_FRAME_REG_OUT (r, 1);
2288 gcc_assert (r <= 31);
2289 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2291 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2293 unsigned r = (opc - DW_OP_reg0);
2294 r = DWARF2_FRAME_REG_OUT (r, 1);
2295 gcc_assert (r <= 31);
2296 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2298 /* Output the opcode. */
2299 fprintf (asm_out_file, "%#x", opc);
2300 output_loc_operands_raw (loc);
2302 if (!loc->dw_loc_next)
2303 break;
2304 loc = loc->dw_loc_next;
2306 fputc (',', asm_out_file);
2310 /* This function builds a dwarf location descriptor sequence from a
2311 dw_cfa_location, adding the given OFFSET to the result of the
2312 expression. */
2314 struct dw_loc_descr_node *
2315 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2317 struct dw_loc_descr_node *head, *tmp;
2319 offset += cfa->offset;
2321 if (cfa->indirect)
2323 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2324 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2325 head->dw_loc_oprnd1.val_entry = NULL;
2326 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2327 add_loc_descr (&head, tmp);
2328 if (offset != 0)
2330 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2331 add_loc_descr (&head, tmp);
2334 else
2335 head = new_reg_loc_descr (cfa->reg, offset);
2337 return head;
2340 /* This function builds a dwarf location descriptor sequence for
2341 the address at OFFSET from the CFA when stack is aligned to
2342 ALIGNMENT byte. */
2344 struct dw_loc_descr_node *
2345 build_cfa_aligned_loc (dw_cfa_location *cfa,
2346 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2348 struct dw_loc_descr_node *head;
2349 unsigned int dwarf_fp
2350 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2352 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2353 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2355 head = new_reg_loc_descr (dwarf_fp, 0);
2356 add_loc_descr (&head, int_loc_descriptor (alignment));
2357 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2358 loc_descr_plus_const (&head, offset);
2360 else
2361 head = new_reg_loc_descr (dwarf_fp, offset);
2362 return head;
2365 /* And now, the support for symbolic debugging information. */
2367 /* .debug_str support. */
2368 static int output_indirect_string (void **, void *);
2370 static void dwarf2out_init (const char *);
2371 static void dwarf2out_finish (const char *);
2372 static void dwarf2out_assembly_start (void);
2373 static void dwarf2out_define (unsigned int, const char *);
2374 static void dwarf2out_undef (unsigned int, const char *);
2375 static void dwarf2out_start_source_file (unsigned, const char *);
2376 static void dwarf2out_end_source_file (unsigned);
2377 static void dwarf2out_function_decl (tree);
2378 static void dwarf2out_begin_block (unsigned, unsigned);
2379 static void dwarf2out_end_block (unsigned, unsigned);
2380 static bool dwarf2out_ignore_block (const_tree);
2381 static void dwarf2out_global_decl (tree);
2382 static void dwarf2out_type_decl (tree, int);
2383 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2384 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2385 dw_die_ref);
2386 static void dwarf2out_abstract_function (tree);
2387 static void dwarf2out_var_location (rtx);
2388 static void dwarf2out_begin_function (tree);
2389 static void dwarf2out_end_function (unsigned int);
2390 static void dwarf2out_set_name (tree, tree);
2392 /* The debug hooks structure. */
2394 const struct gcc_debug_hooks dwarf2_debug_hooks =
2396 dwarf2out_init,
2397 dwarf2out_finish,
2398 dwarf2out_assembly_start,
2399 dwarf2out_define,
2400 dwarf2out_undef,
2401 dwarf2out_start_source_file,
2402 dwarf2out_end_source_file,
2403 dwarf2out_begin_block,
2404 dwarf2out_end_block,
2405 dwarf2out_ignore_block,
2406 dwarf2out_source_line,
2407 dwarf2out_begin_prologue,
2408 #if VMS_DEBUGGING_INFO
2409 dwarf2out_vms_end_prologue,
2410 dwarf2out_vms_begin_epilogue,
2411 #else
2412 debug_nothing_int_charstar,
2413 debug_nothing_int_charstar,
2414 #endif
2415 dwarf2out_end_epilogue,
2416 dwarf2out_begin_function,
2417 dwarf2out_end_function, /* end_function */
2418 dwarf2out_function_decl, /* function_decl */
2419 dwarf2out_global_decl,
2420 dwarf2out_type_decl, /* type_decl */
2421 dwarf2out_imported_module_or_decl,
2422 debug_nothing_tree, /* deferred_inline_function */
2423 /* The DWARF 2 backend tries to reduce debugging bloat by not
2424 emitting the abstract description of inline functions until
2425 something tries to reference them. */
2426 dwarf2out_abstract_function, /* outlining_inline_function */
2427 debug_nothing_rtx, /* label */
2428 debug_nothing_int, /* handle_pch */
2429 dwarf2out_var_location,
2430 dwarf2out_switch_text_section,
2431 dwarf2out_set_name,
2432 1, /* start_end_main_source_file */
2433 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2436 /* NOTE: In the comments in this file, many references are made to
2437 "Debugging Information Entries". This term is abbreviated as `DIE'
2438 throughout the remainder of this file. */
2440 /* An internal representation of the DWARF output is built, and then
2441 walked to generate the DWARF debugging info. The walk of the internal
2442 representation is done after the entire program has been compiled.
2443 The types below are used to describe the internal representation. */
2445 /* Whether to put type DIEs into their own section .debug_types instead
2446 of making them part of the .debug_info section. Only supported for
2447 Dwarf V4 or higher and the user didn't disable them through
2448 -fno-debug-types-section. It is more efficient to put them in a
2449 separate comdat sections since the linker will then be able to
2450 remove duplicates. But not all tools support .debug_types sections
2451 yet. */
2453 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2455 /* Various DIE's use offsets relative to the beginning of the
2456 .debug_info section to refer to each other. */
2458 typedef long int dw_offset;
2460 /* Define typedefs here to avoid circular dependencies. */
2462 typedef struct dw_attr_struct *dw_attr_ref;
2463 typedef struct dw_line_info_struct *dw_line_info_ref;
2464 typedef struct pubname_struct *pubname_ref;
2465 typedef struct dw_ranges_struct *dw_ranges_ref;
2466 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
2467 typedef struct comdat_type_struct *comdat_type_node_ref;
2469 /* The entries in the line_info table more-or-less mirror the opcodes
2470 that are used in the real dwarf line table. Arrays of these entries
2471 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2472 supported. */
2474 enum dw_line_info_opcode {
2475 /* Emit DW_LNE_set_address; the operand is the label index. */
2476 LI_set_address,
2478 /* Emit a row to the matrix with the given line. This may be done
2479 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2480 special opcodes. */
2481 LI_set_line,
2483 /* Emit a DW_LNS_set_file. */
2484 LI_set_file,
2486 /* Emit a DW_LNS_set_column. */
2487 LI_set_column,
2489 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2490 LI_negate_stmt,
2492 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2493 LI_set_prologue_end,
2494 LI_set_epilogue_begin,
2496 /* Emit a DW_LNE_set_discriminator. */
2497 LI_set_discriminator
2500 typedef struct GTY(()) dw_line_info_struct {
2501 enum dw_line_info_opcode opcode;
2502 unsigned int val;
2503 } dw_line_info_entry;
2506 typedef struct GTY(()) dw_line_info_table_struct {
2507 /* The label that marks the end of this section. */
2508 const char *end_label;
2510 /* The values for the last row of the matrix, as collected in the table.
2511 These are used to minimize the changes to the next row. */
2512 unsigned int file_num;
2513 unsigned int line_num;
2514 unsigned int column_num;
2515 int discrim_num;
2516 bool is_stmt;
2517 bool in_use;
2519 vec<dw_line_info_entry, va_gc> *entries;
2520 } dw_line_info_table;
2522 typedef dw_line_info_table *dw_line_info_table_p;
2525 /* Each DIE attribute has a field specifying the attribute kind,
2526 a link to the next attribute in the chain, and an attribute value.
2527 Attributes are typically linked below the DIE they modify. */
2529 typedef struct GTY(()) dw_attr_struct {
2530 enum dwarf_attribute dw_attr;
2531 dw_val_node dw_attr_val;
2533 dw_attr_node;
2536 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2537 The children of each node form a circular list linked by
2538 die_sib. die_child points to the node *before* the "first" child node. */
2540 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
2541 union die_symbol_or_type_node
2543 const char * GTY ((tag ("0"))) die_symbol;
2544 comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
2546 GTY ((desc ("%0.comdat_type_p"))) die_id;
2547 vec<dw_attr_node, va_gc> *die_attr;
2548 dw_die_ref die_parent;
2549 dw_die_ref die_child;
2550 dw_die_ref die_sib;
2551 dw_die_ref die_definition; /* ref from a specification to its definition */
2552 dw_offset die_offset;
2553 unsigned long die_abbrev;
2554 int die_mark;
2555 unsigned int decl_id;
2556 enum dwarf_tag die_tag;
2557 /* Die is used and must not be pruned as unused. */
2558 BOOL_BITFIELD die_perennial_p : 1;
2559 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2560 /* Lots of spare bits. */
2562 die_node;
2564 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2565 #define FOR_EACH_CHILD(die, c, expr) do { \
2566 c = die->die_child; \
2567 if (c) do { \
2568 c = c->die_sib; \
2569 expr; \
2570 } while (c != die->die_child); \
2571 } while (0)
2573 /* The pubname structure */
2575 typedef struct GTY(()) pubname_struct {
2576 dw_die_ref die;
2577 const char *name;
2579 pubname_entry;
2582 struct GTY(()) dw_ranges_struct {
2583 /* If this is positive, it's a block number, otherwise it's a
2584 bitwise-negated index into dw_ranges_by_label. */
2585 int num;
2588 /* A structure to hold a macinfo entry. */
2590 typedef struct GTY(()) macinfo_struct {
2591 unsigned char code;
2592 unsigned HOST_WIDE_INT lineno;
2593 const char *info;
2595 macinfo_entry;
2598 struct GTY(()) dw_ranges_by_label_struct {
2599 const char *begin;
2600 const char *end;
2603 /* The comdat type node structure. */
2604 typedef struct GTY(()) comdat_type_struct
2606 dw_die_ref root_die;
2607 dw_die_ref type_die;
2608 dw_die_ref skeleton_die;
2609 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2610 struct comdat_type_struct *next;
2612 comdat_type_node;
2614 /* The limbo die list structure. */
2615 typedef struct GTY(()) limbo_die_struct {
2616 dw_die_ref die;
2617 tree created_for;
2618 struct limbo_die_struct *next;
2620 limbo_die_node;
2622 typedef struct skeleton_chain_struct
2624 dw_die_ref old_die;
2625 dw_die_ref new_die;
2626 struct skeleton_chain_struct *parent;
2628 skeleton_chain_node;
2630 /* Define a macro which returns nonzero for a TYPE_DECL which was
2631 implicitly generated for a type.
2633 Note that, unlike the C front-end (which generates a NULL named
2634 TYPE_DECL node for each complete tagged type, each array type,
2635 and each function type node created) the C++ front-end generates
2636 a _named_ TYPE_DECL node for each tagged type node created.
2637 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2638 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2639 front-end, but for each type, tagged or not. */
2641 #define TYPE_DECL_IS_STUB(decl) \
2642 (DECL_NAME (decl) == NULL_TREE \
2643 || (DECL_ARTIFICIAL (decl) \
2644 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
2645 /* This is necessary for stub decls that \
2646 appear in nested inline functions. */ \
2647 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2648 && (decl_ultimate_origin (decl) \
2649 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2651 /* Information concerning the compilation unit's programming
2652 language, and compiler version. */
2654 /* Fixed size portion of the DWARF compilation unit header. */
2655 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2656 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2658 /* Fixed size portion of the DWARF comdat type unit header. */
2659 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2660 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2661 + DWARF_OFFSET_SIZE)
2663 /* Fixed size portion of public names info. */
2664 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2666 /* Fixed size portion of the address range info. */
2667 #define DWARF_ARANGES_HEADER_SIZE \
2668 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2669 DWARF2_ADDR_SIZE * 2) \
2670 - DWARF_INITIAL_LENGTH_SIZE)
2672 /* Size of padding portion in the address range info. It must be
2673 aligned to twice the pointer size. */
2674 #define DWARF_ARANGES_PAD_SIZE \
2675 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2676 DWARF2_ADDR_SIZE * 2) \
2677 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2679 /* Use assembler line directives if available. */
2680 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2681 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2682 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2683 #else
2684 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2685 #endif
2686 #endif
2688 /* Minimum line offset in a special line info. opcode.
2689 This value was chosen to give a reasonable range of values. */
2690 #define DWARF_LINE_BASE -10
2692 /* First special line opcode - leave room for the standard opcodes. */
2693 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
2695 /* Range of line offsets in a special line info. opcode. */
2696 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
2698 /* Flag that indicates the initial value of the is_stmt_start flag.
2699 In the present implementation, we do not mark any lines as
2700 the beginning of a source statement, because that information
2701 is not made available by the GCC front-end. */
2702 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2704 /* Maximum number of operations per instruction bundle. */
2705 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2706 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2707 #endif
2709 /* This location is used by calc_die_sizes() to keep track
2710 the offset of each DIE within the .debug_info section. */
2711 static unsigned long next_die_offset;
2713 /* Record the root of the DIE's built for the current compilation unit. */
2714 static GTY(()) dw_die_ref single_comp_unit_die;
2716 /* A list of type DIEs that have been separated into comdat sections. */
2717 static GTY(()) comdat_type_node *comdat_type_list;
2719 /* A list of DIEs with a NULL parent waiting to be relocated. */
2720 static GTY(()) limbo_die_node *limbo_die_list;
2722 /* A list of DIEs for which we may have to generate
2723 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
2724 static GTY(()) limbo_die_node *deferred_asm_name;
2726 /* Filenames referenced by this compilation unit. */
2727 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
2729 /* A hash table of references to DIE's that describe declarations.
2730 The key is a DECL_UID() which is a unique number identifying each decl. */
2731 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
2733 /* A hash table of references to DIE's that describe COMMON blocks.
2734 The key is DECL_UID() ^ die_parent. */
2735 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
2737 typedef struct GTY(()) die_arg_entry_struct {
2738 dw_die_ref die;
2739 tree arg;
2740 } die_arg_entry;
2743 /* Node of the variable location list. */
2744 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2745 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2746 EXPR_LIST chain. For small bitsizes, bitsize is encoded
2747 in mode of the EXPR_LIST node and first EXPR_LIST operand
2748 is either NOTE_INSN_VAR_LOCATION for a piece with a known
2749 location or NULL for padding. For larger bitsizes,
2750 mode is 0 and first operand is a CONCAT with bitsize
2751 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2752 NULL as second operand. */
2753 rtx GTY (()) loc;
2754 const char * GTY (()) label;
2755 struct var_loc_node * GTY (()) next;
2758 /* Variable location list. */
2759 struct GTY (()) var_loc_list_def {
2760 struct var_loc_node * GTY (()) first;
2762 /* Pointer to the last but one or last element of the
2763 chained list. If the list is empty, both first and
2764 last are NULL, if the list contains just one node
2765 or the last node certainly is not redundant, it points
2766 to the last node, otherwise points to the last but one.
2767 Do not mark it for GC because it is marked through the chain. */
2768 struct var_loc_node * GTY ((skip ("%h"))) last;
2770 /* Pointer to the last element before section switch,
2771 if NULL, either sections weren't switched or first
2772 is after section switch. */
2773 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2775 /* DECL_UID of the variable decl. */
2776 unsigned int decl_id;
2778 typedef struct var_loc_list_def var_loc_list;
2780 /* Call argument location list. */
2781 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2782 rtx GTY (()) call_arg_loc_note;
2783 const char * GTY (()) label;
2784 tree GTY (()) block;
2785 bool tail_call_p;
2786 rtx GTY (()) symbol_ref;
2787 struct call_arg_loc_node * GTY (()) next;
2791 /* Table of decl location linked lists. */
2792 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
2794 /* Head and tail of call_arg_loc chain. */
2795 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2796 static struct call_arg_loc_node *call_arg_loc_last;
2798 /* Number of call sites in the current function. */
2799 static int call_site_count = -1;
2800 /* Number of tail call sites in the current function. */
2801 static int tail_call_site_count = -1;
2803 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
2804 DIEs. */
2805 static vec<dw_die_ref> block_map;
2807 /* A cached location list. */
2808 struct GTY (()) cached_dw_loc_list_def {
2809 /* The DECL_UID of the decl that this entry describes. */
2810 unsigned int decl_id;
2812 /* The cached location list. */
2813 dw_loc_list_ref loc_list;
2815 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2817 /* Table of cached location lists. */
2818 static GTY ((param_is (cached_dw_loc_list))) htab_t cached_dw_loc_list_table;
2820 /* A pointer to the base of a list of references to DIE's that
2821 are uniquely identified by their tag, presence/absence of
2822 children DIE's, and list of attribute/value pairs. */
2823 static GTY((length ("abbrev_die_table_allocated")))
2824 dw_die_ref *abbrev_die_table;
2826 /* Number of elements currently allocated for abbrev_die_table. */
2827 static GTY(()) unsigned abbrev_die_table_allocated;
2829 /* Number of elements in type_die_table currently in use. */
2830 static GTY(()) unsigned abbrev_die_table_in_use;
2832 /* Size (in elements) of increments by which we may expand the
2833 abbrev_die_table. */
2834 #define ABBREV_DIE_TABLE_INCREMENT 256
2836 /* A global counter for generating labels for line number data. */
2837 static unsigned int line_info_label_num;
2839 /* The current table to which we should emit line number information
2840 for the current function. This will be set up at the beginning of
2841 assembly for the function. */
2842 static dw_line_info_table *cur_line_info_table;
2844 /* The two default tables of line number info. */
2845 static GTY(()) dw_line_info_table *text_section_line_info;
2846 static GTY(()) dw_line_info_table *cold_text_section_line_info;
2848 /* The set of all non-default tables of line number info. */
2849 static GTY(()) vec<dw_line_info_table_p, va_gc> *separate_line_info;
2851 /* A flag to tell pubnames/types export if there is an info section to
2852 refer to. */
2853 static bool info_section_emitted;
2855 /* A pointer to the base of a table that contains a list of publicly
2856 accessible names. */
2857 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
2859 /* A pointer to the base of a table that contains a list of publicly
2860 accessible types. */
2861 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
2863 /* A pointer to the base of a table that contains a list of macro
2864 defines/undefines (and file start/end markers). */
2865 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
2867 /* True if .debug_macinfo or .debug_macros section is going to be
2868 emitted. */
2869 #define have_macinfo \
2870 (debug_info_level >= DINFO_LEVEL_VERBOSE \
2871 && !macinfo_table->is_empty ())
2873 /* Array of dies for which we should generate .debug_ranges info. */
2874 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
2876 /* Number of elements currently allocated for ranges_table. */
2877 static GTY(()) unsigned ranges_table_allocated;
2879 /* Number of elements in ranges_table currently in use. */
2880 static GTY(()) unsigned ranges_table_in_use;
2882 /* Array of pairs of labels referenced in ranges_table. */
2883 static GTY ((length ("ranges_by_label_allocated")))
2884 dw_ranges_by_label_ref ranges_by_label;
2886 /* Number of elements currently allocated for ranges_by_label. */
2887 static GTY(()) unsigned ranges_by_label_allocated;
2889 /* Number of elements in ranges_by_label currently in use. */
2890 static GTY(()) unsigned ranges_by_label_in_use;
2892 /* Size (in elements) of increments by which we may expand the
2893 ranges_table. */
2894 #define RANGES_TABLE_INCREMENT 64
2896 /* Whether we have location lists that need outputting */
2897 static GTY(()) bool have_location_lists;
2899 /* Unique label counter. */
2900 static GTY(()) unsigned int loclabel_num;
2902 /* Unique label counter for point-of-call tables. */
2903 static GTY(()) unsigned int poc_label_num;
2905 /* Record whether the function being analyzed contains inlined functions. */
2906 static int current_function_has_inlines;
2908 /* The last file entry emitted by maybe_emit_file(). */
2909 static GTY(()) struct dwarf_file_data * last_emitted_file;
2911 /* Number of internal labels generated by gen_internal_sym(). */
2912 static GTY(()) int label_num;
2914 /* Cached result of previous call to lookup_filename. */
2915 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
2917 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
2919 /* Instances of generic types for which we need to generate debug
2920 info that describe their generic parameters and arguments. That
2921 generation needs to happen once all types are properly laid out so
2922 we do it at the end of compilation. */
2923 static GTY(()) vec<tree, va_gc> *generic_type_instances;
2925 /* Offset from the "steady-state frame pointer" to the frame base,
2926 within the current function. */
2927 static HOST_WIDE_INT frame_pointer_fb_offset;
2928 static bool frame_pointer_fb_offset_valid;
2930 static vec<dw_die_ref> base_types;
2932 /* Forward declarations for functions defined in this file. */
2934 static int is_pseudo_reg (const_rtx);
2935 static tree type_main_variant (tree);
2936 static int is_tagged_type (const_tree);
2937 static const char *dwarf_tag_name (unsigned);
2938 static const char *dwarf_attr_name (unsigned);
2939 static const char *dwarf_form_name (unsigned);
2940 static tree decl_ultimate_origin (const_tree);
2941 static tree decl_class_context (tree);
2942 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
2943 static inline enum dw_val_class AT_class (dw_attr_ref);
2944 static inline unsigned int AT_index (dw_attr_ref);
2945 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
2946 static inline unsigned AT_flag (dw_attr_ref);
2947 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
2948 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
2949 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
2950 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
2951 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
2952 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2953 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
2954 unsigned int, unsigned char *);
2955 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
2956 static hashval_t debug_str_do_hash (const void *);
2957 static int debug_str_eq (const void *, const void *);
2958 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
2959 static inline const char *AT_string (dw_attr_ref);
2960 static enum dwarf_form AT_string_form (dw_attr_ref);
2961 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
2962 static void add_AT_specification (dw_die_ref, dw_die_ref);
2963 static inline dw_die_ref AT_ref (dw_attr_ref);
2964 static inline int AT_ref_external (dw_attr_ref);
2965 static inline void set_AT_ref_external (dw_attr_ref, int);
2966 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
2967 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
2968 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
2969 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
2970 dw_loc_list_ref);
2971 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
2972 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
2973 static void remove_addr_table_entry (addr_table_entry *);
2974 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
2975 static inline rtx AT_addr (dw_attr_ref);
2976 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
2977 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
2978 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
2979 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
2980 unsigned HOST_WIDE_INT);
2981 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
2982 unsigned long, bool);
2983 static inline const char *AT_lbl (dw_attr_ref);
2984 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
2985 static const char *get_AT_low_pc (dw_die_ref);
2986 static const char *get_AT_hi_pc (dw_die_ref);
2987 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
2988 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
2989 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
2990 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
2991 static bool is_cxx (void);
2992 static bool is_fortran (void);
2993 static bool is_ada (void);
2994 static void remove_AT (dw_die_ref, enum dwarf_attribute);
2995 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
2996 static void add_child_die (dw_die_ref, dw_die_ref);
2997 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
2998 static dw_die_ref lookup_type_die (tree);
2999 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3000 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3001 static void equate_type_number_to_die (tree, dw_die_ref);
3002 static hashval_t decl_die_table_hash (const void *);
3003 static int decl_die_table_eq (const void *, const void *);
3004 static dw_die_ref lookup_decl_die (tree);
3005 static hashval_t common_block_die_table_hash (const void *);
3006 static int common_block_die_table_eq (const void *, const void *);
3007 static hashval_t decl_loc_table_hash (const void *);
3008 static int decl_loc_table_eq (const void *, const void *);
3009 static var_loc_list *lookup_decl_loc (const_tree);
3010 static void equate_decl_number_to_die (tree, dw_die_ref);
3011 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3012 static void print_spaces (FILE *);
3013 static void print_die (dw_die_ref, FILE *);
3014 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3015 static dw_die_ref pop_compile_unit (dw_die_ref);
3016 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3017 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3018 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3019 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3020 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3021 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3022 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
3023 struct md5_ctx *, int *);
3024 struct checksum_attributes;
3025 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3026 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3027 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3028 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3029 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3030 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3031 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3032 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3033 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3034 static void compute_section_prefix (dw_die_ref);
3035 static int is_type_die (dw_die_ref);
3036 static int is_comdat_die (dw_die_ref);
3037 static int is_symbol_die (dw_die_ref);
3038 static inline bool is_template_instantiation (dw_die_ref);
3039 static void assign_symbol_names (dw_die_ref);
3040 static void break_out_includes (dw_die_ref);
3041 static int is_declaration_die (dw_die_ref);
3042 static int should_move_die_to_comdat (dw_die_ref);
3043 static dw_die_ref clone_as_declaration (dw_die_ref);
3044 static dw_die_ref clone_die (dw_die_ref);
3045 static dw_die_ref clone_tree (dw_die_ref);
3046 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3047 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3048 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3049 static dw_die_ref generate_skeleton (dw_die_ref);
3050 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3051 dw_die_ref,
3052 dw_die_ref);
3053 static void break_out_comdat_types (dw_die_ref);
3054 static void copy_decls_for_unworthy_types (dw_die_ref);
3056 static void add_sibling_attributes (dw_die_ref);
3057 static void output_location_lists (dw_die_ref);
3058 static int constant_size (unsigned HOST_WIDE_INT);
3059 static unsigned long size_of_die (dw_die_ref);
3060 static void calc_die_sizes (dw_die_ref);
3061 static void calc_base_type_die_sizes (void);
3062 static void mark_dies (dw_die_ref);
3063 static void unmark_dies (dw_die_ref);
3064 static void unmark_all_dies (dw_die_ref);
3065 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3066 static unsigned long size_of_aranges (void);
3067 static enum dwarf_form value_format (dw_attr_ref);
3068 static void output_value_format (dw_attr_ref);
3069 static void output_abbrev_section (void);
3070 static void output_die_abbrevs (unsigned long, dw_die_ref);
3071 static void output_die_symbol (dw_die_ref);
3072 static void output_die (dw_die_ref);
3073 static void output_compilation_unit_header (void);
3074 static void output_comp_unit (dw_die_ref, int);
3075 static void output_comdat_type_unit (comdat_type_node *);
3076 static const char *dwarf2_name (tree, int);
3077 static void add_pubname (tree, dw_die_ref);
3078 static void add_enumerator_pubname (const char *, dw_die_ref);
3079 static void add_pubname_string (const char *, dw_die_ref);
3080 static void add_pubtype (tree, dw_die_ref);
3081 static void output_pubnames (vec<pubname_entry, va_gc> *);
3082 static void output_aranges (unsigned long);
3083 static unsigned int add_ranges_num (int);
3084 static unsigned int add_ranges (const_tree);
3085 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3086 bool *, bool);
3087 static void output_ranges (void);
3088 static dw_line_info_table *new_line_info_table (void);
3089 static void output_line_info (bool);
3090 static void output_file_names (void);
3091 static dw_die_ref base_type_die (tree);
3092 static int is_base_type (tree);
3093 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3094 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
3095 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3096 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3097 static int type_is_enum (const_tree);
3098 static unsigned int dbx_reg_number (const_rtx);
3099 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3100 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3101 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3102 enum var_init_status);
3103 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3104 enum var_init_status);
3105 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3106 enum var_init_status);
3107 static int is_based_loc (const_rtx);
3108 static int resolve_one_addr (rtx *, void *);
3109 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3110 enum var_init_status);
3111 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
3112 enum var_init_status);
3113 static dw_loc_list_ref loc_list_from_tree (tree, int);
3114 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
3115 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3116 static tree field_type (const_tree);
3117 static unsigned int simple_type_align_in_bits (const_tree);
3118 static unsigned int simple_decl_align_in_bits (const_tree);
3119 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3120 static HOST_WIDE_INT field_byte_offset (const_tree);
3121 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3122 dw_loc_list_ref);
3123 static void add_data_member_location_attribute (dw_die_ref, tree);
3124 static bool add_const_value_attribute (dw_die_ref, rtx);
3125 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3126 static void insert_double (double_int, unsigned char *);
3127 static void insert_float (const_rtx, unsigned char *);
3128 static rtx rtl_for_decl_location (tree);
3129 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3130 enum dwarf_attribute);
3131 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3132 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3133 static void add_name_attribute (dw_die_ref, const char *);
3134 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3135 static void add_comp_dir_attribute (dw_die_ref);
3136 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
3137 static void add_subscript_info (dw_die_ref, tree, bool);
3138 static void add_byte_size_attribute (dw_die_ref, tree);
3139 static void add_bit_offset_attribute (dw_die_ref, tree);
3140 static void add_bit_size_attribute (dw_die_ref, tree);
3141 static void add_prototyped_attribute (dw_die_ref, tree);
3142 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3143 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3144 static void add_src_coords_attributes (dw_die_ref, tree);
3145 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3146 static void push_decl_scope (tree);
3147 static void pop_decl_scope (void);
3148 static dw_die_ref scope_die_for (tree, dw_die_ref);
3149 static inline int local_scope_p (dw_die_ref);
3150 static inline int class_scope_p (dw_die_ref);
3151 static inline int class_or_namespace_scope_p (dw_die_ref);
3152 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
3153 static void add_calling_convention_attribute (dw_die_ref, tree);
3154 static const char *type_tag (const_tree);
3155 static tree member_declared_type (const_tree);
3156 #if 0
3157 static const char *decl_start_label (tree);
3158 #endif
3159 static void gen_array_type_die (tree, dw_die_ref);
3160 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3161 #if 0
3162 static void gen_entry_point_die (tree, dw_die_ref);
3163 #endif
3164 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3165 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3166 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3167 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3168 static void gen_formal_types_die (tree, dw_die_ref);
3169 static void gen_subprogram_die (tree, dw_die_ref);
3170 static void gen_variable_die (tree, tree, dw_die_ref);
3171 static void gen_const_die (tree, dw_die_ref);
3172 static void gen_label_die (tree, dw_die_ref);
3173 static void gen_lexical_block_die (tree, dw_die_ref, int);
3174 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
3175 static void gen_field_die (tree, dw_die_ref);
3176 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3177 static dw_die_ref gen_compile_unit_die (const char *);
3178 static void gen_inheritance_die (tree, tree, dw_die_ref);
3179 static void gen_member_die (tree, dw_die_ref);
3180 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3181 enum debug_info_usage);
3182 static void gen_subroutine_type_die (tree, dw_die_ref);
3183 static void gen_typedef_die (tree, dw_die_ref);
3184 static void gen_type_die (tree, dw_die_ref);
3185 static void gen_block_die (tree, dw_die_ref, int);
3186 static void decls_for_scope (tree, dw_die_ref, int);
3187 static inline int is_redundant_typedef (const_tree);
3188 static bool is_naming_typedef_decl (const_tree);
3189 static inline dw_die_ref get_context_die (tree);
3190 static void gen_namespace_die (tree, dw_die_ref);
3191 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3192 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3193 static dw_die_ref force_decl_die (tree);
3194 static dw_die_ref force_type_die (tree);
3195 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3196 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3197 static struct dwarf_file_data * lookup_filename (const char *);
3198 static void retry_incomplete_types (void);
3199 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3200 static void gen_generic_params_dies (tree);
3201 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3202 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3203 static void splice_child_die (dw_die_ref, dw_die_ref);
3204 static int file_info_cmp (const void *, const void *);
3205 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3206 const char *, const char *);
3207 static void output_loc_list (dw_loc_list_ref);
3208 static char *gen_internal_sym (const char *);
3209 static bool want_pubnames (void);
3211 static void prune_unmark_dies (dw_die_ref);
3212 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3213 static void prune_unused_types_mark (dw_die_ref, int);
3214 static void prune_unused_types_walk (dw_die_ref);
3215 static void prune_unused_types_walk_attribs (dw_die_ref);
3216 static void prune_unused_types_prune (dw_die_ref);
3217 static void prune_unused_types (void);
3218 static int maybe_emit_file (struct dwarf_file_data *fd);
3219 static inline const char *AT_vms_delta1 (dw_attr_ref);
3220 static inline const char *AT_vms_delta2 (dw_attr_ref);
3221 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3222 const char *, const char *);
3223 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3224 static void gen_remaining_tmpl_value_param_die_attribute (void);
3225 static bool generic_type_p (tree);
3226 static void schedule_generic_params_dies_gen (tree t);
3227 static void gen_scheduled_generic_parms_dies (void);
3229 static const char *comp_dir_string (void);
3231 static hashval_t hash_loc_operands (dw_loc_descr_ref, hashval_t);
3233 /* enum for tracking thread-local variables whose address is really an offset
3234 relative to the TLS pointer, which will need link-time relocation, but will
3235 not need relocation by the DWARF consumer. */
3237 enum dtprel_bool
3239 dtprel_false = 0,
3240 dtprel_true = 1
3243 /* Return the operator to use for an address of a variable. For dtprel_true, we
3244 use DW_OP_const*. For regular variables, which need both link-time
3245 relocation and consumer-level relocation (e.g., to account for shared objects
3246 loaded at a random address), we use DW_OP_addr*. */
3248 static inline enum dwarf_location_atom
3249 dw_addr_op (enum dtprel_bool dtprel)
3251 if (dtprel == dtprel_true)
3252 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3253 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3254 else
3255 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3258 /* Return a pointer to a newly allocated address location description. If
3259 dwarf_split_debug_info is true, then record the address with the appropriate
3260 relocation. */
3261 static inline dw_loc_descr_ref
3262 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3264 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3266 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3267 ref->dw_loc_oprnd1.v.val_addr = addr;
3268 ref->dtprel = dtprel;
3269 if (dwarf_split_debug_info)
3270 ref->dw_loc_oprnd1.val_entry
3271 = add_addr_table_entry (addr,
3272 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3273 else
3274 ref->dw_loc_oprnd1.val_entry = NULL;
3276 return ref;
3279 /* Section names used to hold DWARF debugging information. */
3281 #ifndef DEBUG_INFO_SECTION
3282 #define DEBUG_INFO_SECTION ".debug_info"
3283 #endif
3284 #ifndef DEBUG_DWO_INFO_SECTION
3285 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3286 #endif
3287 #ifndef DEBUG_ABBREV_SECTION
3288 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3289 #endif
3290 #ifndef DEBUG_DWO_ABBREV_SECTION
3291 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3292 #endif
3293 #ifndef DEBUG_ARANGES_SECTION
3294 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3295 #endif
3296 #ifndef DEBUG_ADDR_SECTION
3297 #define DEBUG_ADDR_SECTION ".debug_addr"
3298 #endif
3299 #ifndef DEBUG_NORM_MACINFO_SECTION
3300 #define DEBUG_NORM_MACINFO_SECTION ".debug_macinfo"
3301 #endif
3302 #ifndef DEBUG_DWO_MACINFO_SECTION
3303 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3304 #endif
3305 #ifndef DEBUG_MACINFO_SECTION
3306 #define DEBUG_MACINFO_SECTION \
3307 (!dwarf_split_debug_info \
3308 ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3309 #endif
3310 #ifndef DEBUG_NORM_MACRO_SECTION
3311 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3312 #endif
3313 #ifndef DEBUG_DWO_MACRO_SECTION
3314 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3315 #endif
3316 #ifndef DEBUG_MACRO_SECTION
3317 #define DEBUG_MACRO_SECTION \
3318 (!dwarf_split_debug_info \
3319 ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3320 #endif
3321 #ifndef DEBUG_LINE_SECTION
3322 #define DEBUG_LINE_SECTION ".debug_line"
3323 #endif
3324 #ifndef DEBUG_DWO_LINE_SECTION
3325 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3326 #endif
3327 #ifndef DEBUG_LOC_SECTION
3328 #define DEBUG_LOC_SECTION ".debug_loc"
3329 #endif
3330 #ifndef DEBUG_DWO_LOC_SECTION
3331 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3332 #endif
3333 #ifndef DEBUG_PUBNAMES_SECTION
3334 #define DEBUG_PUBNAMES_SECTION \
3335 ((debug_generate_pub_sections == 2) \
3336 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3337 #endif
3338 #ifndef DEBUG_PUBTYPES_SECTION
3339 #define DEBUG_PUBTYPES_SECTION \
3340 ((debug_generate_pub_sections == 2) \
3341 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3342 #endif
3343 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3344 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3345 #ifndef DEBUG_STR_OFFSETS_SECTION
3346 #define DEBUG_STR_OFFSETS_SECTION \
3347 (!dwarf_split_debug_info \
3348 ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3349 #endif
3350 #ifndef DEBUG_STR_DWO_SECTION
3351 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3352 #endif
3353 #ifndef DEBUG_STR_SECTION
3354 #define DEBUG_STR_SECTION ".debug_str"
3355 #endif
3356 #ifndef DEBUG_RANGES_SECTION
3357 #define DEBUG_RANGES_SECTION ".debug_ranges"
3358 #endif
3360 /* Standard ELF section names for compiled code and data. */
3361 #ifndef TEXT_SECTION_NAME
3362 #define TEXT_SECTION_NAME ".text"
3363 #endif
3365 /* Section flags for .debug_macinfo/.debug_macro section. */
3366 #define DEBUG_MACRO_SECTION_FLAGS \
3367 (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3369 /* Section flags for .debug_str section. */
3370 #define DEBUG_STR_SECTION_FLAGS \
3371 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3372 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3373 : SECTION_DEBUG)
3375 /* Section flags for .debug_str.dwo section. */
3376 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3378 /* Labels we insert at beginning sections we can reference instead of
3379 the section names themselves. */
3381 #ifndef TEXT_SECTION_LABEL
3382 #define TEXT_SECTION_LABEL "Ltext"
3383 #endif
3384 #ifndef COLD_TEXT_SECTION_LABEL
3385 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3386 #endif
3387 #ifndef DEBUG_LINE_SECTION_LABEL
3388 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3389 #endif
3390 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3391 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3392 #endif
3393 #ifndef DEBUG_INFO_SECTION_LABEL
3394 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3395 #endif
3396 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3397 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3398 #endif
3399 #ifndef DEBUG_ABBREV_SECTION_LABEL
3400 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3401 #endif
3402 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3403 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3404 #endif
3405 #ifndef DEBUG_ADDR_SECTION_LABEL
3406 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3407 #endif
3408 #ifndef DEBUG_LOC_SECTION_LABEL
3409 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3410 #endif
3411 #ifndef DEBUG_RANGES_SECTION_LABEL
3412 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3413 #endif
3414 #ifndef DEBUG_MACINFO_SECTION_LABEL
3415 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3416 #endif
3417 #ifndef DEBUG_MACRO_SECTION_LABEL
3418 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3419 #endif
3420 #define SKELETON_COMP_DIE_ABBREV 1
3421 #define SKELETON_TYPE_DIE_ABBREV 2
3423 /* Definitions of defaults for formats and names of various special
3424 (artificial) labels which may be generated within this file (when the -g
3425 options is used and DWARF2_DEBUGGING_INFO is in effect.
3426 If necessary, these may be overridden from within the tm.h file, but
3427 typically, overriding these defaults is unnecessary. */
3429 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3430 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3431 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3432 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3433 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3434 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3435 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3436 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3437 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3438 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3439 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3440 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3441 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3442 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3444 #ifndef TEXT_END_LABEL
3445 #define TEXT_END_LABEL "Letext"
3446 #endif
3447 #ifndef COLD_END_LABEL
3448 #define COLD_END_LABEL "Letext_cold"
3449 #endif
3450 #ifndef BLOCK_BEGIN_LABEL
3451 #define BLOCK_BEGIN_LABEL "LBB"
3452 #endif
3453 #ifndef BLOCK_END_LABEL
3454 #define BLOCK_END_LABEL "LBE"
3455 #endif
3456 #ifndef LINE_CODE_LABEL
3457 #define LINE_CODE_LABEL "LM"
3458 #endif
3461 /* Return the root of the DIE's built for the current compilation unit. */
3462 static dw_die_ref
3463 comp_unit_die (void)
3465 if (!single_comp_unit_die)
3466 single_comp_unit_die = gen_compile_unit_die (NULL);
3467 return single_comp_unit_die;
3470 /* We allow a language front-end to designate a function that is to be
3471 called to "demangle" any name before it is put into a DIE. */
3473 static const char *(*demangle_name_func) (const char *);
3475 void
3476 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3478 demangle_name_func = func;
3481 /* Test if rtl node points to a pseudo register. */
3483 static inline int
3484 is_pseudo_reg (const_rtx rtl)
3486 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3487 || (GET_CODE (rtl) == SUBREG
3488 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3491 /* Return a reference to a type, with its const and volatile qualifiers
3492 removed. */
3494 static inline tree
3495 type_main_variant (tree type)
3497 type = TYPE_MAIN_VARIANT (type);
3499 /* ??? There really should be only one main variant among any group of
3500 variants of a given type (and all of the MAIN_VARIANT values for all
3501 members of the group should point to that one type) but sometimes the C
3502 front-end messes this up for array types, so we work around that bug
3503 here. */
3504 if (TREE_CODE (type) == ARRAY_TYPE)
3505 while (type != TYPE_MAIN_VARIANT (type))
3506 type = TYPE_MAIN_VARIANT (type);
3508 return type;
3511 /* Return nonzero if the given type node represents a tagged type. */
3513 static inline int
3514 is_tagged_type (const_tree type)
3516 enum tree_code code = TREE_CODE (type);
3518 return (code == RECORD_TYPE || code == UNION_TYPE
3519 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3522 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3524 static void
3525 get_ref_die_offset_label (char *label, dw_die_ref ref)
3527 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3530 /* Return die_offset of a DIE reference to a base type. */
3532 static unsigned long int
3533 get_base_type_offset (dw_die_ref ref)
3535 if (ref->die_offset)
3536 return ref->die_offset;
3537 if (comp_unit_die ()->die_abbrev)
3539 calc_base_type_die_sizes ();
3540 gcc_assert (ref->die_offset);
3542 return ref->die_offset;
3545 /* Return die_offset of a DIE reference other than base type. */
3547 static unsigned long int
3548 get_ref_die_offset (dw_die_ref ref)
3550 gcc_assert (ref->die_offset);
3551 return ref->die_offset;
3554 /* Convert a DIE tag into its string name. */
3556 static const char *
3557 dwarf_tag_name (unsigned int tag)
3559 const char *name = get_DW_TAG_name (tag);
3561 if (name != NULL)
3562 return name;
3564 return "DW_TAG_<unknown>";
3567 /* Convert a DWARF attribute code into its string name. */
3569 static const char *
3570 dwarf_attr_name (unsigned int attr)
3572 const char *name;
3574 switch (attr)
3576 #if VMS_DEBUGGING_INFO
3577 case DW_AT_HP_prologue:
3578 return "DW_AT_HP_prologue";
3579 #else
3580 case DW_AT_MIPS_loop_unroll_factor:
3581 return "DW_AT_MIPS_loop_unroll_factor";
3582 #endif
3584 #if VMS_DEBUGGING_INFO
3585 case DW_AT_HP_epilogue:
3586 return "DW_AT_HP_epilogue";
3587 #else
3588 case DW_AT_MIPS_stride:
3589 return "DW_AT_MIPS_stride";
3590 #endif
3593 name = get_DW_AT_name (attr);
3595 if (name != NULL)
3596 return name;
3598 return "DW_AT_<unknown>";
3601 /* Convert a DWARF value form code into its string name. */
3603 static const char *
3604 dwarf_form_name (unsigned int form)
3606 const char *name = get_DW_FORM_name (form);
3608 if (name != NULL)
3609 return name;
3611 return "DW_FORM_<unknown>";
3614 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3615 instance of an inlined instance of a decl which is local to an inline
3616 function, so we have to trace all of the way back through the origin chain
3617 to find out what sort of node actually served as the original seed for the
3618 given block. */
3620 static tree
3621 decl_ultimate_origin (const_tree decl)
3623 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3624 return NULL_TREE;
3626 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
3627 nodes in the function to point to themselves; ignore that if
3628 we're trying to output the abstract instance of this function. */
3629 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3630 return NULL_TREE;
3632 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3633 most distant ancestor, this should never happen. */
3634 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3636 return DECL_ABSTRACT_ORIGIN (decl);
3639 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
3640 of a virtual function may refer to a base class, so we check the 'this'
3641 parameter. */
3643 static tree
3644 decl_class_context (tree decl)
3646 tree context = NULL_TREE;
3648 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3649 context = DECL_CONTEXT (decl);
3650 else
3651 context = TYPE_MAIN_VARIANT
3652 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3654 if (context && !TYPE_P (context))
3655 context = NULL_TREE;
3657 return context;
3660 /* Add an attribute/value pair to a DIE. */
3662 static inline void
3663 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
3665 /* Maybe this should be an assert? */
3666 if (die == NULL)
3667 return;
3669 vec_safe_reserve (die->die_attr, 1);
3670 vec_safe_push (die->die_attr, *attr);
3673 static inline enum dw_val_class
3674 AT_class (dw_attr_ref a)
3676 return a->dw_attr_val.val_class;
3679 /* Return the index for any attribute that will be referenced with a
3680 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
3681 are stored in dw_attr_val.v.val_str for reference counting
3682 pruning. */
3684 static inline unsigned int
3685 AT_index (dw_attr_ref a)
3687 if (AT_class (a) == dw_val_class_str)
3688 return a->dw_attr_val.v.val_str->index;
3689 else if (a->dw_attr_val.val_entry != NULL)
3690 return a->dw_attr_val.val_entry->index;
3691 return NOT_INDEXED;
3694 /* Add a flag value attribute to a DIE. */
3696 static inline void
3697 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3699 dw_attr_node attr;
3701 attr.dw_attr = attr_kind;
3702 attr.dw_attr_val.val_class = dw_val_class_flag;
3703 attr.dw_attr_val.val_entry = NULL;
3704 attr.dw_attr_val.v.val_flag = flag;
3705 add_dwarf_attr (die, &attr);
3708 static inline unsigned
3709 AT_flag (dw_attr_ref a)
3711 gcc_assert (a && AT_class (a) == dw_val_class_flag);
3712 return a->dw_attr_val.v.val_flag;
3715 /* Add a signed integer attribute value to a DIE. */
3717 static inline void
3718 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3720 dw_attr_node attr;
3722 attr.dw_attr = attr_kind;
3723 attr.dw_attr_val.val_class = dw_val_class_const;
3724 attr.dw_attr_val.val_entry = NULL;
3725 attr.dw_attr_val.v.val_int = int_val;
3726 add_dwarf_attr (die, &attr);
3729 static inline HOST_WIDE_INT
3730 AT_int (dw_attr_ref a)
3732 gcc_assert (a && AT_class (a) == dw_val_class_const);
3733 return a->dw_attr_val.v.val_int;
3736 /* Add an unsigned integer attribute value to a DIE. */
3738 static inline void
3739 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3740 unsigned HOST_WIDE_INT unsigned_val)
3742 dw_attr_node attr;
3744 attr.dw_attr = attr_kind;
3745 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3746 attr.dw_attr_val.val_entry = NULL;
3747 attr.dw_attr_val.v.val_unsigned = unsigned_val;
3748 add_dwarf_attr (die, &attr);
3751 static inline unsigned HOST_WIDE_INT
3752 AT_unsigned (dw_attr_ref a)
3754 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3755 return a->dw_attr_val.v.val_unsigned;
3758 /* Add an unsigned double integer attribute value to a DIE. */
3760 static inline void
3761 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3762 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3764 dw_attr_node attr;
3766 attr.dw_attr = attr_kind;
3767 attr.dw_attr_val.val_class = dw_val_class_const_double;
3768 attr.dw_attr_val.val_entry = NULL;
3769 attr.dw_attr_val.v.val_double.high = high;
3770 attr.dw_attr_val.v.val_double.low = low;
3771 add_dwarf_attr (die, &attr);
3774 /* Add a floating point attribute value to a DIE and return it. */
3776 static inline void
3777 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3778 unsigned int length, unsigned int elt_size, unsigned char *array)
3780 dw_attr_node attr;
3782 attr.dw_attr = attr_kind;
3783 attr.dw_attr_val.val_class = dw_val_class_vec;
3784 attr.dw_attr_val.val_entry = NULL;
3785 attr.dw_attr_val.v.val_vec.length = length;
3786 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3787 attr.dw_attr_val.v.val_vec.array = array;
3788 add_dwarf_attr (die, &attr);
3791 /* Add an 8-byte data attribute value to a DIE. */
3793 static inline void
3794 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3795 unsigned char data8[8])
3797 dw_attr_node attr;
3799 attr.dw_attr = attr_kind;
3800 attr.dw_attr_val.val_class = dw_val_class_data8;
3801 attr.dw_attr_val.val_entry = NULL;
3802 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3803 add_dwarf_attr (die, &attr);
3806 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
3807 dwarf_split_debug_info, address attributes in dies destined for the
3808 final executable have force_direct set to avoid using indexed
3809 references. */
3811 static inline void
3812 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
3813 bool force_direct)
3815 dw_attr_node attr;
3816 char * lbl_id;
3818 lbl_id = xstrdup (lbl_low);
3819 attr.dw_attr = DW_AT_low_pc;
3820 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3821 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3822 if (dwarf_split_debug_info && !force_direct)
3823 attr.dw_attr_val.val_entry
3824 = add_addr_table_entry (lbl_id, ate_kind_label);
3825 else
3826 attr.dw_attr_val.val_entry = NULL;
3827 add_dwarf_attr (die, &attr);
3829 attr.dw_attr = DW_AT_high_pc;
3830 if (dwarf_version < 4)
3831 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3832 else
3833 attr.dw_attr_val.val_class = dw_val_class_high_pc;
3834 lbl_id = xstrdup (lbl_high);
3835 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3836 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
3837 && dwarf_split_debug_info && !force_direct)
3838 attr.dw_attr_val.val_entry
3839 = add_addr_table_entry (lbl_id, ate_kind_label);
3840 else
3841 attr.dw_attr_val.val_entry = NULL;
3842 add_dwarf_attr (die, &attr);
3845 /* Hash and equality functions for debug_str_hash. */
3847 static hashval_t
3848 debug_str_do_hash (const void *x)
3850 return htab_hash_string (((const struct indirect_string_node *)x)->str);
3853 static int
3854 debug_str_eq (const void *x1, const void *x2)
3856 return strcmp ((((const struct indirect_string_node *)x1)->str),
3857 (const char *)x2) == 0;
3860 /* Add STR to the given string hash table. */
3862 static struct indirect_string_node *
3863 find_AT_string_in_table (const char *str, htab_t table)
3865 struct indirect_string_node *node;
3866 void **slot;
3868 slot = htab_find_slot_with_hash (table, str,
3869 htab_hash_string (str), INSERT);
3870 if (*slot == NULL)
3872 node = ggc_alloc_cleared_indirect_string_node ();
3873 node->str = ggc_strdup (str);
3874 *slot = node;
3876 else
3877 node = (struct indirect_string_node *) *slot;
3879 node->refcount++;
3880 return node;
3883 /* Add STR to the indirect string hash table. */
3885 static struct indirect_string_node *
3886 find_AT_string (const char *str)
3888 if (! debug_str_hash)
3889 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
3890 debug_str_eq, NULL);
3892 return find_AT_string_in_table (str, debug_str_hash);
3895 /* Add a string attribute value to a DIE. */
3897 static inline void
3898 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
3900 dw_attr_node attr;
3901 struct indirect_string_node *node;
3903 node = find_AT_string (str);
3905 attr.dw_attr = attr_kind;
3906 attr.dw_attr_val.val_class = dw_val_class_str;
3907 attr.dw_attr_val.val_entry = NULL;
3908 attr.dw_attr_val.v.val_str = node;
3909 add_dwarf_attr (die, &attr);
3912 static inline const char *
3913 AT_string (dw_attr_ref a)
3915 gcc_assert (a && AT_class (a) == dw_val_class_str);
3916 return a->dw_attr_val.v.val_str->str;
3919 /* Call this function directly to bypass AT_string_form's logic to put
3920 the string inline in the die. */
3922 static void
3923 set_indirect_string (struct indirect_string_node *node)
3925 char label[32];
3926 /* Already indirect is a no op. */
3927 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
3929 gcc_assert (node->label);
3930 return;
3932 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
3933 ++dw2_string_counter;
3934 node->label = xstrdup (label);
3936 if (!dwarf_split_debug_info)
3938 node->form = DW_FORM_strp;
3939 node->index = NOT_INDEXED;
3941 else
3943 node->form = DW_FORM_GNU_str_index;
3944 node->index = NO_INDEX_ASSIGNED;
3948 /* Find out whether a string should be output inline in DIE
3949 or out-of-line in .debug_str section. */
3951 static enum dwarf_form
3952 find_string_form (struct indirect_string_node *node)
3954 unsigned int len;
3956 if (node->form)
3957 return node->form;
3959 len = strlen (node->str) + 1;
3961 /* If the string is shorter or equal to the size of the reference, it is
3962 always better to put it inline. */
3963 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
3964 return node->form = DW_FORM_string;
3966 /* If we cannot expect the linker to merge strings in .debug_str
3967 section, only put it into .debug_str if it is worth even in this
3968 single module. */
3969 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
3970 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
3971 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
3972 return node->form = DW_FORM_string;
3974 set_indirect_string (node);
3976 return node->form;
3979 /* Find out whether the string referenced from the attribute should be
3980 output inline in DIE or out-of-line in .debug_str section. */
3982 static enum dwarf_form
3983 AT_string_form (dw_attr_ref a)
3985 gcc_assert (a && AT_class (a) == dw_val_class_str);
3986 return find_string_form (a->dw_attr_val.v.val_str);
3989 /* Add a DIE reference attribute value to a DIE. */
3991 static inline void
3992 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
3994 dw_attr_node attr;
3996 #ifdef ENABLE_CHECKING
3997 gcc_assert (targ_die != NULL);
3998 #else
3999 /* With LTO we can end up trying to reference something we didn't create
4000 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4001 if (targ_die == NULL)
4002 return;
4003 #endif
4005 attr.dw_attr = attr_kind;
4006 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4007 attr.dw_attr_val.val_entry = NULL;
4008 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4009 attr.dw_attr_val.v.val_die_ref.external = 0;
4010 add_dwarf_attr (die, &attr);
4013 /* Change DIE reference REF to point to NEW_DIE instead. */
4015 static inline void
4016 change_AT_die_ref (dw_attr_ref ref, dw_die_ref new_die)
4018 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4019 ref->dw_attr_val.v.val_die_ref.die = new_die;
4020 ref->dw_attr_val.v.val_die_ref.external = 0;
4023 /* Add an AT_specification attribute to a DIE, and also make the back
4024 pointer from the specification to the definition. */
4026 static inline void
4027 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4029 add_AT_die_ref (die, DW_AT_specification, targ_die);
4030 gcc_assert (!targ_die->die_definition);
4031 targ_die->die_definition = die;
4034 static inline dw_die_ref
4035 AT_ref (dw_attr_ref a)
4037 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4038 return a->dw_attr_val.v.val_die_ref.die;
4041 static inline int
4042 AT_ref_external (dw_attr_ref a)
4044 if (a && AT_class (a) == dw_val_class_die_ref)
4045 return a->dw_attr_val.v.val_die_ref.external;
4047 return 0;
4050 static inline void
4051 set_AT_ref_external (dw_attr_ref a, int i)
4053 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4054 a->dw_attr_val.v.val_die_ref.external = i;
4057 /* Add an FDE reference attribute value to a DIE. */
4059 static inline void
4060 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4062 dw_attr_node attr;
4064 attr.dw_attr = attr_kind;
4065 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4066 attr.dw_attr_val.val_entry = NULL;
4067 attr.dw_attr_val.v.val_fde_index = targ_fde;
4068 add_dwarf_attr (die, &attr);
4071 /* Add a location description attribute value to a DIE. */
4073 static inline void
4074 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4076 dw_attr_node attr;
4078 attr.dw_attr = attr_kind;
4079 attr.dw_attr_val.val_class = dw_val_class_loc;
4080 attr.dw_attr_val.val_entry = NULL;
4081 attr.dw_attr_val.v.val_loc = loc;
4082 add_dwarf_attr (die, &attr);
4085 static inline dw_loc_descr_ref
4086 AT_loc (dw_attr_ref a)
4088 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4089 return a->dw_attr_val.v.val_loc;
4092 static inline void
4093 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4095 dw_attr_node attr;
4097 attr.dw_attr = attr_kind;
4098 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4099 attr.dw_attr_val.val_entry = NULL;
4100 attr.dw_attr_val.v.val_loc_list = loc_list;
4101 add_dwarf_attr (die, &attr);
4102 have_location_lists = true;
4105 static inline dw_loc_list_ref
4106 AT_loc_list (dw_attr_ref a)
4108 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4109 return a->dw_attr_val.v.val_loc_list;
4112 static inline dw_loc_list_ref *
4113 AT_loc_list_ptr (dw_attr_ref a)
4115 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4116 return &a->dw_attr_val.v.val_loc_list;
4119 /* Table of entries into the .debug_addr section. */
4121 static GTY ((param_is (addr_table_entry))) htab_t addr_index_table;
4123 /* Hash an address_table_entry. */
4125 static hashval_t
4126 addr_table_entry_do_hash (const void *x)
4128 const addr_table_entry *a = (const addr_table_entry *) x;
4129 switch (a->kind)
4131 case ate_kind_rtx:
4132 return iterative_hash_rtx (a->addr.rtl, 0);
4133 case ate_kind_rtx_dtprel:
4134 return iterative_hash_rtx (a->addr.rtl, 1);
4135 case ate_kind_label:
4136 return htab_hash_string (a->addr.label);
4137 default:
4138 gcc_unreachable ();
4142 /* Determine equality for two address_table_entries. */
4144 static int
4145 addr_table_entry_eq (const void *x1, const void *x2)
4147 const addr_table_entry *a1 = (const addr_table_entry *) x1;
4148 const addr_table_entry *a2 = (const addr_table_entry *) x2;
4150 if (a1->kind != a2->kind)
4151 return 0;
4152 switch (a1->kind)
4154 case ate_kind_rtx:
4155 case ate_kind_rtx_dtprel:
4156 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4157 case ate_kind_label:
4158 return strcmp (a1->addr.label, a2->addr.label) == 0;
4159 default:
4160 gcc_unreachable ();
4164 /* Initialize an addr_table_entry. */
4166 void
4167 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4169 e->kind = kind;
4170 switch (kind)
4172 case ate_kind_rtx:
4173 case ate_kind_rtx_dtprel:
4174 e->addr.rtl = (rtx) addr;
4175 break;
4176 case ate_kind_label:
4177 e->addr.label = (char *) addr;
4178 break;
4180 e->refcount = 0;
4181 e->index = NO_INDEX_ASSIGNED;
4184 /* Add attr to the address table entry to the table. Defer setting an
4185 index until output time. */
4187 static addr_table_entry *
4188 add_addr_table_entry (void *addr, enum ate_kind kind)
4190 addr_table_entry *node;
4191 addr_table_entry finder;
4192 void **slot;
4194 gcc_assert (dwarf_split_debug_info);
4195 if (! addr_index_table)
4196 addr_index_table = htab_create_ggc (10, addr_table_entry_do_hash,
4197 addr_table_entry_eq, NULL);
4198 init_addr_table_entry (&finder, kind, addr);
4199 slot = htab_find_slot (addr_index_table, &finder, INSERT);
4201 if (*slot == HTAB_EMPTY_ENTRY)
4203 node = ggc_alloc_cleared_addr_table_entry ();
4204 init_addr_table_entry (node, kind, addr);
4205 *slot = node;
4207 else
4208 node = (addr_table_entry *) *slot;
4210 node->refcount++;
4211 return node;
4214 /* Remove an entry from the addr table by decrementing its refcount.
4215 Strictly, decrementing the refcount would be enough, but the
4216 assertion that the entry is actually in the table has found
4217 bugs. */
4219 static void
4220 remove_addr_table_entry (addr_table_entry *entry)
4222 addr_table_entry *node;
4224 gcc_assert (dwarf_split_debug_info && addr_index_table);
4225 node = (addr_table_entry *) htab_find (addr_index_table, entry);
4226 /* After an index is assigned, the table is frozen. */
4227 gcc_assert (node->refcount > 0 && node->index == NO_INDEX_ASSIGNED);
4228 node->refcount--;
4231 /* Given a location list, remove all addresses it refers to from the
4232 address_table. */
4234 static void
4235 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4237 for (; descr; descr = descr->dw_loc_next)
4238 if (descr->dw_loc_oprnd1.val_entry != NULL)
4240 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4241 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4245 /* A helper function for dwarf2out_finish called through
4246 htab_traverse. Assign an addr_table_entry its index. All entries
4247 must be collected into the table when this function is called,
4248 because the indexing code relies on htab_traverse to traverse nodes
4249 in the same order for each run. */
4251 static int
4252 index_addr_table_entry (void **h, void *v)
4254 addr_table_entry *node = (addr_table_entry *) *h;
4255 unsigned int *index = (unsigned int *) v;
4257 /* Don't index unreferenced nodes. */
4258 if (node->refcount == 0)
4259 return 1;
4261 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4262 node->index = *index;
4263 *index += 1;
4265 return 1;
4268 /* Add an address constant attribute value to a DIE. When using
4269 dwarf_split_debug_info, address attributes in dies destined for the
4270 final executable should be direct references--setting the parameter
4271 force_direct ensures this behavior. */
4273 static inline void
4274 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4275 bool force_direct)
4277 dw_attr_node attr;
4279 attr.dw_attr = attr_kind;
4280 attr.dw_attr_val.val_class = dw_val_class_addr;
4281 attr.dw_attr_val.v.val_addr = addr;
4282 if (dwarf_split_debug_info && !force_direct)
4283 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4284 else
4285 attr.dw_attr_val.val_entry = NULL;
4286 add_dwarf_attr (die, &attr);
4289 /* Get the RTX from to an address DIE attribute. */
4291 static inline rtx
4292 AT_addr (dw_attr_ref a)
4294 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4295 return a->dw_attr_val.v.val_addr;
4298 /* Add a file attribute value to a DIE. */
4300 static inline void
4301 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4302 struct dwarf_file_data *fd)
4304 dw_attr_node attr;
4306 attr.dw_attr = attr_kind;
4307 attr.dw_attr_val.val_class = dw_val_class_file;
4308 attr.dw_attr_val.val_entry = NULL;
4309 attr.dw_attr_val.v.val_file = fd;
4310 add_dwarf_attr (die, &attr);
4313 /* Get the dwarf_file_data from a file DIE attribute. */
4315 static inline struct dwarf_file_data *
4316 AT_file (dw_attr_ref a)
4318 gcc_assert (a && AT_class (a) == dw_val_class_file);
4319 return a->dw_attr_val.v.val_file;
4322 /* Add a vms delta attribute value to a DIE. */
4324 static inline void
4325 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4326 const char *lbl1, const char *lbl2)
4328 dw_attr_node attr;
4330 attr.dw_attr = attr_kind;
4331 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4332 attr.dw_attr_val.val_entry = NULL;
4333 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4334 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4335 add_dwarf_attr (die, &attr);
4338 /* Add a label identifier attribute value to a DIE. */
4340 static inline void
4341 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4342 const char *lbl_id)
4344 dw_attr_node attr;
4346 attr.dw_attr = attr_kind;
4347 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4348 attr.dw_attr_val.val_entry = NULL;
4349 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4350 if (dwarf_split_debug_info)
4351 attr.dw_attr_val.val_entry
4352 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4353 ate_kind_label);
4354 add_dwarf_attr (die, &attr);
4357 /* Add a section offset attribute value to a DIE, an offset into the
4358 debug_line section. */
4360 static inline void
4361 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4362 const char *label)
4364 dw_attr_node attr;
4366 attr.dw_attr = attr_kind;
4367 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4368 attr.dw_attr_val.val_entry = NULL;
4369 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4370 add_dwarf_attr (die, &attr);
4373 /* Add a section offset attribute value to a DIE, an offset into the
4374 debug_macinfo section. */
4376 static inline void
4377 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4378 const char *label)
4380 dw_attr_node attr;
4382 attr.dw_attr = attr_kind;
4383 attr.dw_attr_val.val_class = dw_val_class_macptr;
4384 attr.dw_attr_val.val_entry = NULL;
4385 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4386 add_dwarf_attr (die, &attr);
4389 /* Add an offset attribute value to a DIE. */
4391 static inline void
4392 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4393 unsigned HOST_WIDE_INT offset)
4395 dw_attr_node attr;
4397 attr.dw_attr = attr_kind;
4398 attr.dw_attr_val.val_class = dw_val_class_offset;
4399 attr.dw_attr_val.val_entry = NULL;
4400 attr.dw_attr_val.v.val_offset = offset;
4401 add_dwarf_attr (die, &attr);
4404 /* Add a range_list attribute value to a DIE. When using
4405 dwarf_split_debug_info, address attributes in dies destined for the
4406 final executable should be direct references--setting the parameter
4407 force_direct ensures this behavior. */
4409 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4410 #define RELOCATED_OFFSET (NULL)
4412 static void
4413 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4414 long unsigned int offset, bool force_direct)
4416 dw_attr_node attr;
4418 attr.dw_attr = attr_kind;
4419 attr.dw_attr_val.val_class = dw_val_class_range_list;
4420 /* For the range_list attribute, use val_entry to store whether the
4421 offset should follow split-debug-info or normal semantics. This
4422 value is read in output_range_list_offset. */
4423 if (dwarf_split_debug_info && !force_direct)
4424 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4425 else
4426 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4427 attr.dw_attr_val.v.val_offset = offset;
4428 add_dwarf_attr (die, &attr);
4431 /* Return the start label of a delta attribute. */
4433 static inline const char *
4434 AT_vms_delta1 (dw_attr_ref a)
4436 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4437 return a->dw_attr_val.v.val_vms_delta.lbl1;
4440 /* Return the end label of a delta attribute. */
4442 static inline const char *
4443 AT_vms_delta2 (dw_attr_ref a)
4445 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4446 return a->dw_attr_val.v.val_vms_delta.lbl2;
4449 static inline const char *
4450 AT_lbl (dw_attr_ref a)
4452 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4453 || AT_class (a) == dw_val_class_lineptr
4454 || AT_class (a) == dw_val_class_macptr
4455 || AT_class (a) == dw_val_class_high_pc));
4456 return a->dw_attr_val.v.val_lbl_id;
4459 /* Get the attribute of type attr_kind. */
4461 static dw_attr_ref
4462 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4464 dw_attr_ref a;
4465 unsigned ix;
4466 dw_die_ref spec = NULL;
4468 if (! die)
4469 return NULL;
4471 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4472 if (a->dw_attr == attr_kind)
4473 return a;
4474 else if (a->dw_attr == DW_AT_specification
4475 || a->dw_attr == DW_AT_abstract_origin)
4476 spec = AT_ref (a);
4478 if (spec)
4479 return get_AT (spec, attr_kind);
4481 return NULL;
4484 /* Returns the parent of the declaration of DIE. */
4486 static dw_die_ref
4487 get_die_parent (dw_die_ref die)
4489 dw_die_ref t;
4491 if (!die)
4492 return NULL;
4494 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4495 || (t = get_AT_ref (die, DW_AT_specification)))
4496 die = t;
4498 return die->die_parent;
4501 /* Return the "low pc" attribute value, typically associated with a subprogram
4502 DIE. Return null if the "low pc" attribute is either not present, or if it
4503 cannot be represented as an assembler label identifier. */
4505 static inline const char *
4506 get_AT_low_pc (dw_die_ref die)
4508 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4510 return a ? AT_lbl (a) : NULL;
4513 /* Return the "high pc" attribute value, typically associated with a subprogram
4514 DIE. Return null if the "high pc" attribute is either not present, or if it
4515 cannot be represented as an assembler label identifier. */
4517 static inline const char *
4518 get_AT_hi_pc (dw_die_ref die)
4520 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4522 return a ? AT_lbl (a) : NULL;
4525 /* Return the value of the string attribute designated by ATTR_KIND, or
4526 NULL if it is not present. */
4528 static inline const char *
4529 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4531 dw_attr_ref a = get_AT (die, attr_kind);
4533 return a ? AT_string (a) : NULL;
4536 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4537 if it is not present. */
4539 static inline int
4540 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4542 dw_attr_ref a = get_AT (die, attr_kind);
4544 return a ? AT_flag (a) : 0;
4547 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4548 if it is not present. */
4550 static inline unsigned
4551 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4553 dw_attr_ref a = get_AT (die, attr_kind);
4555 return a ? AT_unsigned (a) : 0;
4558 static inline dw_die_ref
4559 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4561 dw_attr_ref a = get_AT (die, attr_kind);
4563 return a ? AT_ref (a) : NULL;
4566 static inline struct dwarf_file_data *
4567 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4569 dw_attr_ref a = get_AT (die, attr_kind);
4571 return a ? AT_file (a) : NULL;
4574 /* Return TRUE if the language is C++. */
4576 static inline bool
4577 is_cxx (void)
4579 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4581 return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
4584 /* Return TRUE if the language is Java. */
4586 static inline bool
4587 is_java (void)
4589 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4591 return lang == DW_LANG_Java;
4594 /* Return TRUE if the language is Fortran. */
4596 static inline bool
4597 is_fortran (void)
4599 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4601 return (lang == DW_LANG_Fortran77
4602 || lang == DW_LANG_Fortran90
4603 || lang == DW_LANG_Fortran95);
4606 /* Return TRUE if the language is Ada. */
4608 static inline bool
4609 is_ada (void)
4611 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4613 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4616 /* Remove the specified attribute if present. */
4618 static void
4619 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4621 dw_attr_ref a;
4622 unsigned ix;
4624 if (! die)
4625 return;
4627 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4628 if (a->dw_attr == attr_kind)
4630 if (AT_class (a) == dw_val_class_str)
4631 if (a->dw_attr_val.v.val_str->refcount)
4632 a->dw_attr_val.v.val_str->refcount--;
4634 /* vec::ordered_remove should help reduce the number of abbrevs
4635 that are needed. */
4636 die->die_attr->ordered_remove (ix);
4637 return;
4641 /* Remove CHILD from its parent. PREV must have the property that
4642 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4644 static void
4645 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4647 gcc_assert (child->die_parent == prev->die_parent);
4648 gcc_assert (prev->die_sib == child);
4649 if (prev == child)
4651 gcc_assert (child->die_parent->die_child == child);
4652 prev = NULL;
4654 else
4655 prev->die_sib = child->die_sib;
4656 if (child->die_parent->die_child == child)
4657 child->die_parent->die_child = prev;
4660 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4661 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4663 static void
4664 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4666 dw_die_ref parent = old_child->die_parent;
4668 gcc_assert (parent == prev->die_parent);
4669 gcc_assert (prev->die_sib == old_child);
4671 new_child->die_parent = parent;
4672 if (prev == old_child)
4674 gcc_assert (parent->die_child == old_child);
4675 new_child->die_sib = new_child;
4677 else
4679 prev->die_sib = new_child;
4680 new_child->die_sib = old_child->die_sib;
4682 if (old_child->die_parent->die_child == old_child)
4683 old_child->die_parent->die_child = new_child;
4686 /* Move all children from OLD_PARENT to NEW_PARENT. */
4688 static void
4689 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4691 dw_die_ref c;
4692 new_parent->die_child = old_parent->die_child;
4693 old_parent->die_child = NULL;
4694 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4697 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4698 matches TAG. */
4700 static void
4701 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4703 dw_die_ref c;
4705 c = die->die_child;
4706 if (c) do {
4707 dw_die_ref prev = c;
4708 c = c->die_sib;
4709 while (c->die_tag == tag)
4711 remove_child_with_prev (c, prev);
4712 /* Might have removed every child. */
4713 if (c == c->die_sib)
4714 return;
4715 c = c->die_sib;
4717 } while (c != die->die_child);
4720 /* Add a CHILD_DIE as the last child of DIE. */
4722 static void
4723 add_child_die (dw_die_ref die, dw_die_ref child_die)
4725 /* FIXME this should probably be an assert. */
4726 if (! die || ! child_die)
4727 return;
4728 gcc_assert (die != child_die);
4730 child_die->die_parent = die;
4731 if (die->die_child)
4733 child_die->die_sib = die->die_child->die_sib;
4734 die->die_child->die_sib = child_die;
4736 else
4737 child_die->die_sib = child_die;
4738 die->die_child = child_die;
4741 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4742 is the specification, to the end of PARENT's list of children.
4743 This is done by removing and re-adding it. */
4745 static void
4746 splice_child_die (dw_die_ref parent, dw_die_ref child)
4748 dw_die_ref p;
4750 /* We want the declaration DIE from inside the class, not the
4751 specification DIE at toplevel. */
4752 if (child->die_parent != parent)
4754 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4756 if (tmp)
4757 child = tmp;
4760 gcc_assert (child->die_parent == parent
4761 || (child->die_parent
4762 == get_AT_ref (parent, DW_AT_specification)));
4764 for (p = child->die_parent->die_child; ; p = p->die_sib)
4765 if (p->die_sib == child)
4767 remove_child_with_prev (child, p);
4768 break;
4771 add_child_die (parent, child);
4774 /* Return a pointer to a newly created DIE node. */
4776 static inline dw_die_ref
4777 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4779 dw_die_ref die = ggc_alloc_cleared_die_node ();
4781 die->die_tag = tag_value;
4783 if (parent_die != NULL)
4784 add_child_die (parent_die, die);
4785 else
4787 limbo_die_node *limbo_node;
4789 limbo_node = ggc_alloc_cleared_limbo_die_node ();
4790 limbo_node->die = die;
4791 limbo_node->created_for = t;
4792 limbo_node->next = limbo_die_list;
4793 limbo_die_list = limbo_node;
4796 return die;
4799 /* Return the DIE associated with the given type specifier. */
4801 static inline dw_die_ref
4802 lookup_type_die (tree type)
4804 return TYPE_SYMTAB_DIE (type);
4807 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4808 anonymous type named by the typedef TYPE_DIE, return the DIE of the
4809 anonymous type instead the one of the naming typedef. */
4811 static inline dw_die_ref
4812 strip_naming_typedef (tree type, dw_die_ref type_die)
4814 if (type
4815 && TREE_CODE (type) == RECORD_TYPE
4816 && type_die
4817 && type_die->die_tag == DW_TAG_typedef
4818 && is_naming_typedef_decl (TYPE_NAME (type)))
4819 type_die = get_AT_ref (type_die, DW_AT_type);
4820 return type_die;
4823 /* Like lookup_type_die, but if type is an anonymous type named by a
4824 typedef[1], return the DIE of the anonymous type instead the one of
4825 the naming typedef. This is because in gen_typedef_die, we did
4826 equate the anonymous struct named by the typedef with the DIE of
4827 the naming typedef. So by default, lookup_type_die on an anonymous
4828 struct yields the DIE of the naming typedef.
4830 [1]: Read the comment of is_naming_typedef_decl to learn about what
4831 a naming typedef is. */
4833 static inline dw_die_ref
4834 lookup_type_die_strip_naming_typedef (tree type)
4836 dw_die_ref die = lookup_type_die (type);
4837 return strip_naming_typedef (type, die);
4840 /* Equate a DIE to a given type specifier. */
4842 static inline void
4843 equate_type_number_to_die (tree type, dw_die_ref type_die)
4845 TYPE_SYMTAB_DIE (type) = type_die;
4848 /* Returns a hash value for X (which really is a die_struct). */
4850 static hashval_t
4851 decl_die_table_hash (const void *x)
4853 return (hashval_t) ((const_dw_die_ref) x)->decl_id;
4856 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
4858 static int
4859 decl_die_table_eq (const void *x, const void *y)
4861 return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
4864 /* Return the DIE associated with a given declaration. */
4866 static inline dw_die_ref
4867 lookup_decl_die (tree decl)
4869 return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
4872 /* Returns a hash value for X (which really is a var_loc_list). */
4874 static hashval_t
4875 decl_loc_table_hash (const void *x)
4877 return (hashval_t) ((const var_loc_list *) x)->decl_id;
4880 /* Return nonzero if decl_id of var_loc_list X is the same as
4881 UID of decl *Y. */
4883 static int
4884 decl_loc_table_eq (const void *x, const void *y)
4886 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
4889 /* Return the var_loc list associated with a given declaration. */
4891 static inline var_loc_list *
4892 lookup_decl_loc (const_tree decl)
4894 if (!decl_loc_table)
4895 return NULL;
4896 return (var_loc_list *)
4897 htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
4900 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
4902 static hashval_t
4903 cached_dw_loc_list_table_hash (const void *x)
4905 return (hashval_t) ((const cached_dw_loc_list *) x)->decl_id;
4908 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
4909 UID of decl *Y. */
4911 static int
4912 cached_dw_loc_list_table_eq (const void *x, const void *y)
4914 return (((const cached_dw_loc_list *) x)->decl_id
4915 == DECL_UID ((const_tree) y));
4918 /* Equate a DIE to a particular declaration. */
4920 static void
4921 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
4923 unsigned int decl_id = DECL_UID (decl);
4924 void **slot;
4926 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
4927 *slot = decl_die;
4928 decl_die->decl_id = decl_id;
4931 /* Return how many bits covers PIECE EXPR_LIST. */
4933 static int
4934 decl_piece_bitsize (rtx piece)
4936 int ret = (int) GET_MODE (piece);
4937 if (ret)
4938 return ret;
4939 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
4940 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
4941 return INTVAL (XEXP (XEXP (piece, 0), 0));
4944 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
4946 static rtx *
4947 decl_piece_varloc_ptr (rtx piece)
4949 if ((int) GET_MODE (piece))
4950 return &XEXP (piece, 0);
4951 else
4952 return &XEXP (XEXP (piece, 0), 1);
4955 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
4956 Next is the chain of following piece nodes. */
4958 static rtx
4959 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
4961 if (bitsize <= (int) MAX_MACHINE_MODE)
4962 return alloc_EXPR_LIST (bitsize, loc_note, next);
4963 else
4964 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
4965 GEN_INT (bitsize),
4966 loc_note), next);
4969 /* Return rtx that should be stored into loc field for
4970 LOC_NOTE and BITPOS/BITSIZE. */
4972 static rtx
4973 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
4974 HOST_WIDE_INT bitsize)
4976 if (bitsize != -1)
4978 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
4979 if (bitpos != 0)
4980 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
4982 return loc_note;
4985 /* This function either modifies location piece list *DEST in
4986 place (if SRC and INNER is NULL), or copies location piece list
4987 *SRC to *DEST while modifying it. Location BITPOS is modified
4988 to contain LOC_NOTE, any pieces overlapping it are removed resp.
4989 not copied and if needed some padding around it is added.
4990 When modifying in place, DEST should point to EXPR_LIST where
4991 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
4992 to the start of the whole list and INNER points to the EXPR_LIST
4993 where earlier pieces cover PIECE_BITPOS bits. */
4995 static void
4996 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
4997 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
4998 HOST_WIDE_INT bitsize, rtx loc_note)
5000 int diff;
5001 bool copy = inner != NULL;
5003 if (copy)
5005 /* First copy all nodes preceding the current bitpos. */
5006 while (src != inner)
5008 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5009 decl_piece_bitsize (*src), NULL_RTX);
5010 dest = &XEXP (*dest, 1);
5011 src = &XEXP (*src, 1);
5014 /* Add padding if needed. */
5015 if (bitpos != piece_bitpos)
5017 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5018 copy ? NULL_RTX : *dest);
5019 dest = &XEXP (*dest, 1);
5021 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5023 gcc_assert (!copy);
5024 /* A piece with correct bitpos and bitsize already exist,
5025 just update the location for it and return. */
5026 *decl_piece_varloc_ptr (*dest) = loc_note;
5027 return;
5029 /* Add the piece that changed. */
5030 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5031 dest = &XEXP (*dest, 1);
5032 /* Skip over pieces that overlap it. */
5033 diff = bitpos - piece_bitpos + bitsize;
5034 if (!copy)
5035 src = dest;
5036 while (diff > 0 && *src)
5038 rtx piece = *src;
5039 diff -= decl_piece_bitsize (piece);
5040 if (copy)
5041 src = &XEXP (piece, 1);
5042 else
5044 *src = XEXP (piece, 1);
5045 free_EXPR_LIST_node (piece);
5048 /* Add padding if needed. */
5049 if (diff < 0 && *src)
5051 if (!copy)
5052 dest = src;
5053 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5054 dest = &XEXP (*dest, 1);
5056 if (!copy)
5057 return;
5058 /* Finally copy all nodes following it. */
5059 while (*src)
5061 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5062 decl_piece_bitsize (*src), NULL_RTX);
5063 dest = &XEXP (*dest, 1);
5064 src = &XEXP (*src, 1);
5068 /* Add a variable location node to the linked list for DECL. */
5070 static struct var_loc_node *
5071 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5073 unsigned int decl_id;
5074 var_loc_list *temp;
5075 void **slot;
5076 struct var_loc_node *loc = NULL;
5077 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5079 if (TREE_CODE (decl) == VAR_DECL
5080 && DECL_HAS_DEBUG_EXPR_P (decl))
5082 tree realdecl = DECL_DEBUG_EXPR (decl);
5083 if (handled_component_p (realdecl)
5084 || (TREE_CODE (realdecl) == MEM_REF
5085 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5087 HOST_WIDE_INT maxsize;
5088 tree innerdecl;
5089 innerdecl
5090 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5091 if (!DECL_P (innerdecl)
5092 || DECL_IGNORED_P (innerdecl)
5093 || TREE_STATIC (innerdecl)
5094 || bitsize <= 0
5095 || bitpos + bitsize > 256
5096 || bitsize != maxsize)
5097 return NULL;
5098 decl = innerdecl;
5102 decl_id = DECL_UID (decl);
5103 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5104 if (*slot == NULL)
5106 temp = ggc_alloc_cleared_var_loc_list ();
5107 temp->decl_id = decl_id;
5108 *slot = temp;
5110 else
5111 temp = (var_loc_list *) *slot;
5113 /* For PARM_DECLs try to keep around the original incoming value,
5114 even if that means we'll emit a zero-range .debug_loc entry. */
5115 if (temp->last
5116 && temp->first == temp->last
5117 && TREE_CODE (decl) == PARM_DECL
5118 && NOTE_P (temp->first->loc)
5119 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5120 && DECL_INCOMING_RTL (decl)
5121 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5122 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5123 == GET_CODE (DECL_INCOMING_RTL (decl))
5124 && prev_real_insn (temp->first->loc) == NULL_RTX
5125 && (bitsize != -1
5126 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5127 NOTE_VAR_LOCATION_LOC (loc_note))
5128 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5129 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5131 loc = ggc_alloc_cleared_var_loc_node ();
5132 temp->first->next = loc;
5133 temp->last = loc;
5134 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5136 else if (temp->last)
5138 struct var_loc_node *last = temp->last, *unused = NULL;
5139 rtx *piece_loc = NULL, last_loc_note;
5140 int piece_bitpos = 0;
5141 if (last->next)
5143 last = last->next;
5144 gcc_assert (last->next == NULL);
5146 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5148 piece_loc = &last->loc;
5151 int cur_bitsize = decl_piece_bitsize (*piece_loc);
5152 if (piece_bitpos + cur_bitsize > bitpos)
5153 break;
5154 piece_bitpos += cur_bitsize;
5155 piece_loc = &XEXP (*piece_loc, 1);
5157 while (*piece_loc);
5159 /* TEMP->LAST here is either pointer to the last but one or
5160 last element in the chained list, LAST is pointer to the
5161 last element. */
5162 if (label && strcmp (last->label, label) == 0)
5164 /* For SRA optimized variables if there weren't any real
5165 insns since last note, just modify the last node. */
5166 if (piece_loc != NULL)
5168 adjust_piece_list (piece_loc, NULL, NULL,
5169 bitpos, piece_bitpos, bitsize, loc_note);
5170 return NULL;
5172 /* If the last note doesn't cover any instructions, remove it. */
5173 if (temp->last != last)
5175 temp->last->next = NULL;
5176 unused = last;
5177 last = temp->last;
5178 gcc_assert (strcmp (last->label, label) != 0);
5180 else
5182 gcc_assert (temp->first == temp->last
5183 || (temp->first->next == temp->last
5184 && TREE_CODE (decl) == PARM_DECL));
5185 memset (temp->last, '\0', sizeof (*temp->last));
5186 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5187 return temp->last;
5190 if (bitsize == -1 && NOTE_P (last->loc))
5191 last_loc_note = last->loc;
5192 else if (piece_loc != NULL
5193 && *piece_loc != NULL_RTX
5194 && piece_bitpos == bitpos
5195 && decl_piece_bitsize (*piece_loc) == bitsize)
5196 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5197 else
5198 last_loc_note = NULL_RTX;
5199 /* If the current location is the same as the end of the list,
5200 and either both or neither of the locations is uninitialized,
5201 we have nothing to do. */
5202 if (last_loc_note == NULL_RTX
5203 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5204 NOTE_VAR_LOCATION_LOC (loc_note)))
5205 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5206 != NOTE_VAR_LOCATION_STATUS (loc_note))
5207 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5208 == VAR_INIT_STATUS_UNINITIALIZED)
5209 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5210 == VAR_INIT_STATUS_UNINITIALIZED))))
5212 /* Add LOC to the end of list and update LAST. If the last
5213 element of the list has been removed above, reuse its
5214 memory for the new node, otherwise allocate a new one. */
5215 if (unused)
5217 loc = unused;
5218 memset (loc, '\0', sizeof (*loc));
5220 else
5221 loc = ggc_alloc_cleared_var_loc_node ();
5222 if (bitsize == -1 || piece_loc == NULL)
5223 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5224 else
5225 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5226 bitpos, piece_bitpos, bitsize, loc_note);
5227 last->next = loc;
5228 /* Ensure TEMP->LAST will point either to the new last but one
5229 element of the chain, or to the last element in it. */
5230 if (last != temp->last)
5231 temp->last = last;
5233 else if (unused)
5234 ggc_free (unused);
5236 else
5238 loc = ggc_alloc_cleared_var_loc_node ();
5239 temp->first = loc;
5240 temp->last = loc;
5241 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5243 return loc;
5246 /* Keep track of the number of spaces used to indent the
5247 output of the debugging routines that print the structure of
5248 the DIE internal representation. */
5249 static int print_indent;
5251 /* Indent the line the number of spaces given by print_indent. */
5253 static inline void
5254 print_spaces (FILE *outfile)
5256 fprintf (outfile, "%*s", print_indent, "");
5259 /* Print a type signature in hex. */
5261 static inline void
5262 print_signature (FILE *outfile, char *sig)
5264 int i;
5266 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5267 fprintf (outfile, "%02x", sig[i] & 0xff);
5270 /* Print the information associated with a given DIE, and its children.
5271 This routine is a debugging aid only. */
5273 static void
5274 print_die (dw_die_ref die, FILE *outfile)
5276 dw_attr_ref a;
5277 dw_die_ref c;
5278 unsigned ix;
5280 print_spaces (outfile);
5281 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5282 die->die_offset, dwarf_tag_name (die->die_tag),
5283 (void*) die);
5284 print_spaces (outfile);
5285 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5286 fprintf (outfile, " offset: %ld", die->die_offset);
5287 fprintf (outfile, " mark: %d\n", die->die_mark);
5289 if (die->comdat_type_p)
5291 print_spaces (outfile);
5292 fprintf (outfile, " signature: ");
5293 print_signature (outfile, die->die_id.die_type_node->signature);
5294 fprintf (outfile, "\n");
5297 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5299 print_spaces (outfile);
5300 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5302 switch (AT_class (a))
5304 case dw_val_class_addr:
5305 fprintf (outfile, "address");
5306 break;
5307 case dw_val_class_offset:
5308 fprintf (outfile, "offset");
5309 break;
5310 case dw_val_class_loc:
5311 fprintf (outfile, "location descriptor");
5312 break;
5313 case dw_val_class_loc_list:
5314 fprintf (outfile, "location list -> label:%s",
5315 AT_loc_list (a)->ll_symbol);
5316 break;
5317 case dw_val_class_range_list:
5318 fprintf (outfile, "range list");
5319 break;
5320 case dw_val_class_const:
5321 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5322 break;
5323 case dw_val_class_unsigned_const:
5324 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5325 break;
5326 case dw_val_class_const_double:
5327 fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
5328 HOST_WIDE_INT_PRINT_UNSIGNED")",
5329 a->dw_attr_val.v.val_double.high,
5330 a->dw_attr_val.v.val_double.low);
5331 break;
5332 case dw_val_class_vec:
5333 fprintf (outfile, "floating-point or vector constant");
5334 break;
5335 case dw_val_class_flag:
5336 fprintf (outfile, "%u", AT_flag (a));
5337 break;
5338 case dw_val_class_die_ref:
5339 if (AT_ref (a) != NULL)
5341 if (AT_ref (a)->comdat_type_p)
5343 fprintf (outfile, "die -> signature: ");
5344 print_signature (outfile,
5345 AT_ref (a)->die_id.die_type_node->signature);
5347 else if (AT_ref (a)->die_id.die_symbol)
5348 fprintf (outfile, "die -> label: %s",
5349 AT_ref (a)->die_id.die_symbol);
5350 else
5351 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5352 fprintf (outfile, " (%p)", (void *) AT_ref (a));
5354 else
5355 fprintf (outfile, "die -> <null>");
5356 break;
5357 case dw_val_class_vms_delta:
5358 fprintf (outfile, "delta: @slotcount(%s-%s)",
5359 AT_vms_delta2 (a), AT_vms_delta1 (a));
5360 break;
5361 case dw_val_class_lbl_id:
5362 case dw_val_class_lineptr:
5363 case dw_val_class_macptr:
5364 case dw_val_class_high_pc:
5365 fprintf (outfile, "label: %s", AT_lbl (a));
5366 break;
5367 case dw_val_class_str:
5368 if (AT_string (a) != NULL)
5369 fprintf (outfile, "\"%s\"", AT_string (a));
5370 else
5371 fprintf (outfile, "<null>");
5372 break;
5373 case dw_val_class_file:
5374 fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5375 AT_file (a)->emitted_number);
5376 break;
5377 case dw_val_class_data8:
5379 int i;
5381 for (i = 0; i < 8; i++)
5382 fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
5383 break;
5385 default:
5386 break;
5389 fprintf (outfile, "\n");
5392 if (die->die_child != NULL)
5394 print_indent += 4;
5395 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5396 print_indent -= 4;
5398 if (print_indent == 0)
5399 fprintf (outfile, "\n");
5402 /* Print the information collected for a given DIE. */
5404 DEBUG_FUNCTION void
5405 debug_dwarf_die (dw_die_ref die)
5407 print_die (die, stderr);
5410 DEBUG_FUNCTION void
5411 debug (die_struct &ref)
5413 print_die (&ref, stderr);
5416 DEBUG_FUNCTION void
5417 debug (die_struct *ptr)
5419 if (ptr)
5420 debug (*ptr);
5421 else
5422 fprintf (stderr, "<nil>\n");
5426 /* Print all DWARF information collected for the compilation unit.
5427 This routine is a debugging aid only. */
5429 DEBUG_FUNCTION void
5430 debug_dwarf (void)
5432 print_indent = 0;
5433 print_die (comp_unit_die (), stderr);
5436 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5437 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5438 DIE that marks the start of the DIEs for this include file. */
5440 static dw_die_ref
5441 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5443 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5444 dw_die_ref new_unit = gen_compile_unit_die (filename);
5446 new_unit->die_sib = old_unit;
5447 return new_unit;
5450 /* Close an include-file CU and reopen the enclosing one. */
5452 static dw_die_ref
5453 pop_compile_unit (dw_die_ref old_unit)
5455 dw_die_ref new_unit = old_unit->die_sib;
5457 old_unit->die_sib = NULL;
5458 return new_unit;
5461 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5462 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5463 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5465 /* Calculate the checksum of a location expression. */
5467 static inline void
5468 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5470 int tem;
5471 hashval_t hash = 0;
5473 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5474 CHECKSUM (tem);
5475 hash = hash_loc_operands (loc, hash);
5476 CHECKSUM (hash);
5479 /* Calculate the checksum of an attribute. */
5481 static void
5482 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5484 dw_loc_descr_ref loc;
5485 rtx r;
5487 CHECKSUM (at->dw_attr);
5489 /* We don't care that this was compiled with a different compiler
5490 snapshot; if the output is the same, that's what matters. */
5491 if (at->dw_attr == DW_AT_producer)
5492 return;
5494 switch (AT_class (at))
5496 case dw_val_class_const:
5497 CHECKSUM (at->dw_attr_val.v.val_int);
5498 break;
5499 case dw_val_class_unsigned_const:
5500 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5501 break;
5502 case dw_val_class_const_double:
5503 CHECKSUM (at->dw_attr_val.v.val_double);
5504 break;
5505 case dw_val_class_vec:
5506 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5507 (at->dw_attr_val.v.val_vec.length
5508 * at->dw_attr_val.v.val_vec.elt_size));
5509 break;
5510 case dw_val_class_flag:
5511 CHECKSUM (at->dw_attr_val.v.val_flag);
5512 break;
5513 case dw_val_class_str:
5514 CHECKSUM_STRING (AT_string (at));
5515 break;
5517 case dw_val_class_addr:
5518 r = AT_addr (at);
5519 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5520 CHECKSUM_STRING (XSTR (r, 0));
5521 break;
5523 case dw_val_class_offset:
5524 CHECKSUM (at->dw_attr_val.v.val_offset);
5525 break;
5527 case dw_val_class_loc:
5528 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5529 loc_checksum (loc, ctx);
5530 break;
5532 case dw_val_class_die_ref:
5533 die_checksum (AT_ref (at), ctx, mark);
5534 break;
5536 case dw_val_class_fde_ref:
5537 case dw_val_class_vms_delta:
5538 case dw_val_class_lbl_id:
5539 case dw_val_class_lineptr:
5540 case dw_val_class_macptr:
5541 case dw_val_class_high_pc:
5542 break;
5544 case dw_val_class_file:
5545 CHECKSUM_STRING (AT_file (at)->filename);
5546 break;
5548 case dw_val_class_data8:
5549 CHECKSUM (at->dw_attr_val.v.val_data8);
5550 break;
5552 default:
5553 break;
5557 /* Calculate the checksum of a DIE. */
5559 static void
5560 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5562 dw_die_ref c;
5563 dw_attr_ref a;
5564 unsigned ix;
5566 /* To avoid infinite recursion. */
5567 if (die->die_mark)
5569 CHECKSUM (die->die_mark);
5570 return;
5572 die->die_mark = ++(*mark);
5574 CHECKSUM (die->die_tag);
5576 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5577 attr_checksum (a, ctx, mark);
5579 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5582 #undef CHECKSUM
5583 #undef CHECKSUM_BLOCK
5584 #undef CHECKSUM_STRING
5586 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
5587 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5588 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5589 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5590 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5591 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5592 #define CHECKSUM_ATTR(FOO) \
5593 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5595 /* Calculate the checksum of a number in signed LEB128 format. */
5597 static void
5598 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5600 unsigned char byte;
5601 bool more;
5603 while (1)
5605 byte = (value & 0x7f);
5606 value >>= 7;
5607 more = !((value == 0 && (byte & 0x40) == 0)
5608 || (value == -1 && (byte & 0x40) != 0));
5609 if (more)
5610 byte |= 0x80;
5611 CHECKSUM (byte);
5612 if (!more)
5613 break;
5617 /* Calculate the checksum of a number in unsigned LEB128 format. */
5619 static void
5620 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5622 while (1)
5624 unsigned char byte = (value & 0x7f);
5625 value >>= 7;
5626 if (value != 0)
5627 /* More bytes to follow. */
5628 byte |= 0x80;
5629 CHECKSUM (byte);
5630 if (value == 0)
5631 break;
5635 /* Checksum the context of the DIE. This adds the names of any
5636 surrounding namespaces or structures to the checksum. */
5638 static void
5639 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5641 const char *name;
5642 dw_die_ref spec;
5643 int tag = die->die_tag;
5645 if (tag != DW_TAG_namespace
5646 && tag != DW_TAG_structure_type
5647 && tag != DW_TAG_class_type)
5648 return;
5650 name = get_AT_string (die, DW_AT_name);
5652 spec = get_AT_ref (die, DW_AT_specification);
5653 if (spec != NULL)
5654 die = spec;
5656 if (die->die_parent != NULL)
5657 checksum_die_context (die->die_parent, ctx);
5659 CHECKSUM_ULEB128 ('C');
5660 CHECKSUM_ULEB128 (tag);
5661 if (name != NULL)
5662 CHECKSUM_STRING (name);
5665 /* Calculate the checksum of a location expression. */
5667 static inline void
5668 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5670 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5671 were emitted as a DW_FORM_sdata instead of a location expression. */
5672 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5674 CHECKSUM_ULEB128 (DW_FORM_sdata);
5675 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5676 return;
5679 /* Otherwise, just checksum the raw location expression. */
5680 while (loc != NULL)
5682 hashval_t hash = 0;
5684 CHECKSUM_ULEB128 (loc->dtprel);
5685 CHECKSUM_ULEB128 (loc->dw_loc_opc);
5686 hash = hash_loc_operands (loc, hash);
5687 CHECKSUM (hash);
5688 loc = loc->dw_loc_next;
5692 /* Calculate the checksum of an attribute. */
5694 static void
5695 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5696 struct md5_ctx *ctx, int *mark)
5698 dw_loc_descr_ref loc;
5699 rtx r;
5701 if (AT_class (at) == dw_val_class_die_ref)
5703 dw_die_ref target_die = AT_ref (at);
5705 /* For pointer and reference types, we checksum only the (qualified)
5706 name of the target type (if there is a name). For friend entries,
5707 we checksum only the (qualified) name of the target type or function.
5708 This allows the checksum to remain the same whether the target type
5709 is complete or not. */
5710 if ((at->dw_attr == DW_AT_type
5711 && (tag == DW_TAG_pointer_type
5712 || tag == DW_TAG_reference_type
5713 || tag == DW_TAG_rvalue_reference_type
5714 || tag == DW_TAG_ptr_to_member_type))
5715 || (at->dw_attr == DW_AT_friend
5716 && tag == DW_TAG_friend))
5718 dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5720 if (name_attr != NULL)
5722 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5724 if (decl == NULL)
5725 decl = target_die;
5726 CHECKSUM_ULEB128 ('N');
5727 CHECKSUM_ULEB128 (at->dw_attr);
5728 if (decl->die_parent != NULL)
5729 checksum_die_context (decl->die_parent, ctx);
5730 CHECKSUM_ULEB128 ('E');
5731 CHECKSUM_STRING (AT_string (name_attr));
5732 return;
5736 /* For all other references to another DIE, we check to see if the
5737 target DIE has already been visited. If it has, we emit a
5738 backward reference; if not, we descend recursively. */
5739 if (target_die->die_mark > 0)
5741 CHECKSUM_ULEB128 ('R');
5742 CHECKSUM_ULEB128 (at->dw_attr);
5743 CHECKSUM_ULEB128 (target_die->die_mark);
5745 else
5747 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5749 if (decl == NULL)
5750 decl = target_die;
5751 target_die->die_mark = ++(*mark);
5752 CHECKSUM_ULEB128 ('T');
5753 CHECKSUM_ULEB128 (at->dw_attr);
5754 if (decl->die_parent != NULL)
5755 checksum_die_context (decl->die_parent, ctx);
5756 die_checksum_ordered (target_die, ctx, mark);
5758 return;
5761 CHECKSUM_ULEB128 ('A');
5762 CHECKSUM_ULEB128 (at->dw_attr);
5764 switch (AT_class (at))
5766 case dw_val_class_const:
5767 CHECKSUM_ULEB128 (DW_FORM_sdata);
5768 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
5769 break;
5771 case dw_val_class_unsigned_const:
5772 CHECKSUM_ULEB128 (DW_FORM_sdata);
5773 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
5774 break;
5776 case dw_val_class_const_double:
5777 CHECKSUM_ULEB128 (DW_FORM_block);
5778 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
5779 CHECKSUM (at->dw_attr_val.v.val_double);
5780 break;
5782 case dw_val_class_vec:
5783 CHECKSUM_ULEB128 (DW_FORM_block);
5784 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
5785 * at->dw_attr_val.v.val_vec.elt_size);
5786 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5787 (at->dw_attr_val.v.val_vec.length
5788 * at->dw_attr_val.v.val_vec.elt_size));
5789 break;
5791 case dw_val_class_flag:
5792 CHECKSUM_ULEB128 (DW_FORM_flag);
5793 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
5794 break;
5796 case dw_val_class_str:
5797 CHECKSUM_ULEB128 (DW_FORM_string);
5798 CHECKSUM_STRING (AT_string (at));
5799 break;
5801 case dw_val_class_addr:
5802 r = AT_addr (at);
5803 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5804 CHECKSUM_ULEB128 (DW_FORM_string);
5805 CHECKSUM_STRING (XSTR (r, 0));
5806 break;
5808 case dw_val_class_offset:
5809 CHECKSUM_ULEB128 (DW_FORM_sdata);
5810 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
5811 break;
5813 case dw_val_class_loc:
5814 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5815 loc_checksum_ordered (loc, ctx);
5816 break;
5818 case dw_val_class_fde_ref:
5819 case dw_val_class_lbl_id:
5820 case dw_val_class_lineptr:
5821 case dw_val_class_macptr:
5822 case dw_val_class_high_pc:
5823 break;
5825 case dw_val_class_file:
5826 CHECKSUM_ULEB128 (DW_FORM_string);
5827 CHECKSUM_STRING (AT_file (at)->filename);
5828 break;
5830 case dw_val_class_data8:
5831 CHECKSUM (at->dw_attr_val.v.val_data8);
5832 break;
5834 default:
5835 break;
5839 struct checksum_attributes
5841 dw_attr_ref at_name;
5842 dw_attr_ref at_type;
5843 dw_attr_ref at_friend;
5844 dw_attr_ref at_accessibility;
5845 dw_attr_ref at_address_class;
5846 dw_attr_ref at_allocated;
5847 dw_attr_ref at_artificial;
5848 dw_attr_ref at_associated;
5849 dw_attr_ref at_binary_scale;
5850 dw_attr_ref at_bit_offset;
5851 dw_attr_ref at_bit_size;
5852 dw_attr_ref at_bit_stride;
5853 dw_attr_ref at_byte_size;
5854 dw_attr_ref at_byte_stride;
5855 dw_attr_ref at_const_value;
5856 dw_attr_ref at_containing_type;
5857 dw_attr_ref at_count;
5858 dw_attr_ref at_data_location;
5859 dw_attr_ref at_data_member_location;
5860 dw_attr_ref at_decimal_scale;
5861 dw_attr_ref at_decimal_sign;
5862 dw_attr_ref at_default_value;
5863 dw_attr_ref at_digit_count;
5864 dw_attr_ref at_discr;
5865 dw_attr_ref at_discr_list;
5866 dw_attr_ref at_discr_value;
5867 dw_attr_ref at_encoding;
5868 dw_attr_ref at_endianity;
5869 dw_attr_ref at_explicit;
5870 dw_attr_ref at_is_optional;
5871 dw_attr_ref at_location;
5872 dw_attr_ref at_lower_bound;
5873 dw_attr_ref at_mutable;
5874 dw_attr_ref at_ordering;
5875 dw_attr_ref at_picture_string;
5876 dw_attr_ref at_prototyped;
5877 dw_attr_ref at_small;
5878 dw_attr_ref at_segment;
5879 dw_attr_ref at_string_length;
5880 dw_attr_ref at_threads_scaled;
5881 dw_attr_ref at_upper_bound;
5882 dw_attr_ref at_use_location;
5883 dw_attr_ref at_use_UTF8;
5884 dw_attr_ref at_variable_parameter;
5885 dw_attr_ref at_virtuality;
5886 dw_attr_ref at_visibility;
5887 dw_attr_ref at_vtable_elem_location;
5890 /* Collect the attributes that we will want to use for the checksum. */
5892 static void
5893 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
5895 dw_attr_ref a;
5896 unsigned ix;
5898 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5900 switch (a->dw_attr)
5902 case DW_AT_name:
5903 attrs->at_name = a;
5904 break;
5905 case DW_AT_type:
5906 attrs->at_type = a;
5907 break;
5908 case DW_AT_friend:
5909 attrs->at_friend = a;
5910 break;
5911 case DW_AT_accessibility:
5912 attrs->at_accessibility = a;
5913 break;
5914 case DW_AT_address_class:
5915 attrs->at_address_class = a;
5916 break;
5917 case DW_AT_allocated:
5918 attrs->at_allocated = a;
5919 break;
5920 case DW_AT_artificial:
5921 attrs->at_artificial = a;
5922 break;
5923 case DW_AT_associated:
5924 attrs->at_associated = a;
5925 break;
5926 case DW_AT_binary_scale:
5927 attrs->at_binary_scale = a;
5928 break;
5929 case DW_AT_bit_offset:
5930 attrs->at_bit_offset = a;
5931 break;
5932 case DW_AT_bit_size:
5933 attrs->at_bit_size = a;
5934 break;
5935 case DW_AT_bit_stride:
5936 attrs->at_bit_stride = a;
5937 break;
5938 case DW_AT_byte_size:
5939 attrs->at_byte_size = a;
5940 break;
5941 case DW_AT_byte_stride:
5942 attrs->at_byte_stride = a;
5943 break;
5944 case DW_AT_const_value:
5945 attrs->at_const_value = a;
5946 break;
5947 case DW_AT_containing_type:
5948 attrs->at_containing_type = a;
5949 break;
5950 case DW_AT_count:
5951 attrs->at_count = a;
5952 break;
5953 case DW_AT_data_location:
5954 attrs->at_data_location = a;
5955 break;
5956 case DW_AT_data_member_location:
5957 attrs->at_data_member_location = a;
5958 break;
5959 case DW_AT_decimal_scale:
5960 attrs->at_decimal_scale = a;
5961 break;
5962 case DW_AT_decimal_sign:
5963 attrs->at_decimal_sign = a;
5964 break;
5965 case DW_AT_default_value:
5966 attrs->at_default_value = a;
5967 break;
5968 case DW_AT_digit_count:
5969 attrs->at_digit_count = a;
5970 break;
5971 case DW_AT_discr:
5972 attrs->at_discr = a;
5973 break;
5974 case DW_AT_discr_list:
5975 attrs->at_discr_list = a;
5976 break;
5977 case DW_AT_discr_value:
5978 attrs->at_discr_value = a;
5979 break;
5980 case DW_AT_encoding:
5981 attrs->at_encoding = a;
5982 break;
5983 case DW_AT_endianity:
5984 attrs->at_endianity = a;
5985 break;
5986 case DW_AT_explicit:
5987 attrs->at_explicit = a;
5988 break;
5989 case DW_AT_is_optional:
5990 attrs->at_is_optional = a;
5991 break;
5992 case DW_AT_location:
5993 attrs->at_location = a;
5994 break;
5995 case DW_AT_lower_bound:
5996 attrs->at_lower_bound = a;
5997 break;
5998 case DW_AT_mutable:
5999 attrs->at_mutable = a;
6000 break;
6001 case DW_AT_ordering:
6002 attrs->at_ordering = a;
6003 break;
6004 case DW_AT_picture_string:
6005 attrs->at_picture_string = a;
6006 break;
6007 case DW_AT_prototyped:
6008 attrs->at_prototyped = a;
6009 break;
6010 case DW_AT_small:
6011 attrs->at_small = a;
6012 break;
6013 case DW_AT_segment:
6014 attrs->at_segment = a;
6015 break;
6016 case DW_AT_string_length:
6017 attrs->at_string_length = a;
6018 break;
6019 case DW_AT_threads_scaled:
6020 attrs->at_threads_scaled = a;
6021 break;
6022 case DW_AT_upper_bound:
6023 attrs->at_upper_bound = a;
6024 break;
6025 case DW_AT_use_location:
6026 attrs->at_use_location = a;
6027 break;
6028 case DW_AT_use_UTF8:
6029 attrs->at_use_UTF8 = a;
6030 break;
6031 case DW_AT_variable_parameter:
6032 attrs->at_variable_parameter = a;
6033 break;
6034 case DW_AT_virtuality:
6035 attrs->at_virtuality = a;
6036 break;
6037 case DW_AT_visibility:
6038 attrs->at_visibility = a;
6039 break;
6040 case DW_AT_vtable_elem_location:
6041 attrs->at_vtable_elem_location = a;
6042 break;
6043 default:
6044 break;
6049 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6051 static void
6052 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6054 dw_die_ref c;
6055 dw_die_ref decl;
6056 struct checksum_attributes attrs;
6058 CHECKSUM_ULEB128 ('D');
6059 CHECKSUM_ULEB128 (die->die_tag);
6061 memset (&attrs, 0, sizeof (attrs));
6063 decl = get_AT_ref (die, DW_AT_specification);
6064 if (decl != NULL)
6065 collect_checksum_attributes (&attrs, decl);
6066 collect_checksum_attributes (&attrs, die);
6068 CHECKSUM_ATTR (attrs.at_name);
6069 CHECKSUM_ATTR (attrs.at_accessibility);
6070 CHECKSUM_ATTR (attrs.at_address_class);
6071 CHECKSUM_ATTR (attrs.at_allocated);
6072 CHECKSUM_ATTR (attrs.at_artificial);
6073 CHECKSUM_ATTR (attrs.at_associated);
6074 CHECKSUM_ATTR (attrs.at_binary_scale);
6075 CHECKSUM_ATTR (attrs.at_bit_offset);
6076 CHECKSUM_ATTR (attrs.at_bit_size);
6077 CHECKSUM_ATTR (attrs.at_bit_stride);
6078 CHECKSUM_ATTR (attrs.at_byte_size);
6079 CHECKSUM_ATTR (attrs.at_byte_stride);
6080 CHECKSUM_ATTR (attrs.at_const_value);
6081 CHECKSUM_ATTR (attrs.at_containing_type);
6082 CHECKSUM_ATTR (attrs.at_count);
6083 CHECKSUM_ATTR (attrs.at_data_location);
6084 CHECKSUM_ATTR (attrs.at_data_member_location);
6085 CHECKSUM_ATTR (attrs.at_decimal_scale);
6086 CHECKSUM_ATTR (attrs.at_decimal_sign);
6087 CHECKSUM_ATTR (attrs.at_default_value);
6088 CHECKSUM_ATTR (attrs.at_digit_count);
6089 CHECKSUM_ATTR (attrs.at_discr);
6090 CHECKSUM_ATTR (attrs.at_discr_list);
6091 CHECKSUM_ATTR (attrs.at_discr_value);
6092 CHECKSUM_ATTR (attrs.at_encoding);
6093 CHECKSUM_ATTR (attrs.at_endianity);
6094 CHECKSUM_ATTR (attrs.at_explicit);
6095 CHECKSUM_ATTR (attrs.at_is_optional);
6096 CHECKSUM_ATTR (attrs.at_location);
6097 CHECKSUM_ATTR (attrs.at_lower_bound);
6098 CHECKSUM_ATTR (attrs.at_mutable);
6099 CHECKSUM_ATTR (attrs.at_ordering);
6100 CHECKSUM_ATTR (attrs.at_picture_string);
6101 CHECKSUM_ATTR (attrs.at_prototyped);
6102 CHECKSUM_ATTR (attrs.at_small);
6103 CHECKSUM_ATTR (attrs.at_segment);
6104 CHECKSUM_ATTR (attrs.at_string_length);
6105 CHECKSUM_ATTR (attrs.at_threads_scaled);
6106 CHECKSUM_ATTR (attrs.at_upper_bound);
6107 CHECKSUM_ATTR (attrs.at_use_location);
6108 CHECKSUM_ATTR (attrs.at_use_UTF8);
6109 CHECKSUM_ATTR (attrs.at_variable_parameter);
6110 CHECKSUM_ATTR (attrs.at_virtuality);
6111 CHECKSUM_ATTR (attrs.at_visibility);
6112 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6113 CHECKSUM_ATTR (attrs.at_type);
6114 CHECKSUM_ATTR (attrs.at_friend);
6116 /* Checksum the child DIEs. */
6117 c = die->die_child;
6118 if (c) do {
6119 dw_attr_ref name_attr;
6121 c = c->die_sib;
6122 name_attr = get_AT (c, DW_AT_name);
6123 if (is_template_instantiation (c))
6125 /* Ignore instantiations of member type and function templates. */
6127 else if (name_attr != NULL
6128 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6130 /* Use a shallow checksum for named nested types and member
6131 functions. */
6132 CHECKSUM_ULEB128 ('S');
6133 CHECKSUM_ULEB128 (c->die_tag);
6134 CHECKSUM_STRING (AT_string (name_attr));
6136 else
6138 /* Use a deep checksum for other children. */
6139 /* Mark this DIE so it gets processed when unmarking. */
6140 if (c->die_mark == 0)
6141 c->die_mark = -1;
6142 die_checksum_ordered (c, ctx, mark);
6144 } while (c != die->die_child);
6146 CHECKSUM_ULEB128 (0);
6149 /* Add a type name and tag to a hash. */
6150 static void
6151 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6153 CHECKSUM_ULEB128 (tag);
6154 CHECKSUM_STRING (name);
6157 #undef CHECKSUM
6158 #undef CHECKSUM_STRING
6159 #undef CHECKSUM_ATTR
6160 #undef CHECKSUM_LEB128
6161 #undef CHECKSUM_ULEB128
6163 /* Generate the type signature for DIE. This is computed by generating an
6164 MD5 checksum over the DIE's tag, its relevant attributes, and its
6165 children. Attributes that are references to other DIEs are processed
6166 by recursion, using the MARK field to prevent infinite recursion.
6167 If the DIE is nested inside a namespace or another type, we also
6168 need to include that context in the signature. The lower 64 bits
6169 of the resulting MD5 checksum comprise the signature. */
6171 static void
6172 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6174 int mark;
6175 const char *name;
6176 unsigned char checksum[16];
6177 struct md5_ctx ctx;
6178 dw_die_ref decl;
6179 dw_die_ref parent;
6181 name = get_AT_string (die, DW_AT_name);
6182 decl = get_AT_ref (die, DW_AT_specification);
6183 parent = get_die_parent (die);
6185 /* First, compute a signature for just the type name (and its surrounding
6186 context, if any. This is stored in the type unit DIE for link-time
6187 ODR (one-definition rule) checking. */
6189 if (is_cxx () && name != NULL)
6191 md5_init_ctx (&ctx);
6193 /* Checksum the names of surrounding namespaces and structures. */
6194 if (parent != NULL)
6195 checksum_die_context (parent, &ctx);
6197 /* Checksum the current DIE. */
6198 die_odr_checksum (die->die_tag, name, &ctx);
6199 md5_finish_ctx (&ctx, checksum);
6201 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6204 /* Next, compute the complete type signature. */
6206 md5_init_ctx (&ctx);
6207 mark = 1;
6208 die->die_mark = mark;
6210 /* Checksum the names of surrounding namespaces and structures. */
6211 if (parent != NULL)
6212 checksum_die_context (parent, &ctx);
6214 /* Checksum the DIE and its children. */
6215 die_checksum_ordered (die, &ctx, &mark);
6216 unmark_all_dies (die);
6217 md5_finish_ctx (&ctx, checksum);
6219 /* Store the signature in the type node and link the type DIE and the
6220 type node together. */
6221 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6222 DWARF_TYPE_SIGNATURE_SIZE);
6223 die->comdat_type_p = true;
6224 die->die_id.die_type_node = type_node;
6225 type_node->type_die = die;
6227 /* If the DIE is a specification, link its declaration to the type node
6228 as well. */
6229 if (decl != NULL)
6231 decl->comdat_type_p = true;
6232 decl->die_id.die_type_node = type_node;
6236 /* Do the location expressions look same? */
6237 static inline int
6238 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6240 return loc1->dw_loc_opc == loc2->dw_loc_opc
6241 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6242 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6245 /* Do the values look the same? */
6246 static int
6247 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6249 dw_loc_descr_ref loc1, loc2;
6250 rtx r1, r2;
6252 if (v1->val_class != v2->val_class)
6253 return 0;
6255 switch (v1->val_class)
6257 case dw_val_class_const:
6258 return v1->v.val_int == v2->v.val_int;
6259 case dw_val_class_unsigned_const:
6260 return v1->v.val_unsigned == v2->v.val_unsigned;
6261 case dw_val_class_const_double:
6262 return v1->v.val_double.high == v2->v.val_double.high
6263 && v1->v.val_double.low == v2->v.val_double.low;
6264 case dw_val_class_vec:
6265 if (v1->v.val_vec.length != v2->v.val_vec.length
6266 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6267 return 0;
6268 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6269 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6270 return 0;
6271 return 1;
6272 case dw_val_class_flag:
6273 return v1->v.val_flag == v2->v.val_flag;
6274 case dw_val_class_str:
6275 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6277 case dw_val_class_addr:
6278 r1 = v1->v.val_addr;
6279 r2 = v2->v.val_addr;
6280 if (GET_CODE (r1) != GET_CODE (r2))
6281 return 0;
6282 return !rtx_equal_p (r1, r2);
6284 case dw_val_class_offset:
6285 return v1->v.val_offset == v2->v.val_offset;
6287 case dw_val_class_loc:
6288 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6289 loc1 && loc2;
6290 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6291 if (!same_loc_p (loc1, loc2, mark))
6292 return 0;
6293 return !loc1 && !loc2;
6295 case dw_val_class_die_ref:
6296 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6298 case dw_val_class_fde_ref:
6299 case dw_val_class_vms_delta:
6300 case dw_val_class_lbl_id:
6301 case dw_val_class_lineptr:
6302 case dw_val_class_macptr:
6303 case dw_val_class_high_pc:
6304 return 1;
6306 case dw_val_class_file:
6307 return v1->v.val_file == v2->v.val_file;
6309 case dw_val_class_data8:
6310 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6312 default:
6313 return 1;
6317 /* Do the attributes look the same? */
6319 static int
6320 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6322 if (at1->dw_attr != at2->dw_attr)
6323 return 0;
6325 /* We don't care that this was compiled with a different compiler
6326 snapshot; if the output is the same, that's what matters. */
6327 if (at1->dw_attr == DW_AT_producer)
6328 return 1;
6330 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6333 /* Do the dies look the same? */
6335 static int
6336 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6338 dw_die_ref c1, c2;
6339 dw_attr_ref a1;
6340 unsigned ix;
6342 /* To avoid infinite recursion. */
6343 if (die1->die_mark)
6344 return die1->die_mark == die2->die_mark;
6345 die1->die_mark = die2->die_mark = ++(*mark);
6347 if (die1->die_tag != die2->die_tag)
6348 return 0;
6350 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6351 return 0;
6353 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6354 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6355 return 0;
6357 c1 = die1->die_child;
6358 c2 = die2->die_child;
6359 if (! c1)
6361 if (c2)
6362 return 0;
6364 else
6365 for (;;)
6367 if (!same_die_p (c1, c2, mark))
6368 return 0;
6369 c1 = c1->die_sib;
6370 c2 = c2->die_sib;
6371 if (c1 == die1->die_child)
6373 if (c2 == die2->die_child)
6374 break;
6375 else
6376 return 0;
6380 return 1;
6383 /* Do the dies look the same? Wrapper around same_die_p. */
6385 static int
6386 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6388 int mark = 0;
6389 int ret = same_die_p (die1, die2, &mark);
6391 unmark_all_dies (die1);
6392 unmark_all_dies (die2);
6394 return ret;
6397 /* The prefix to attach to symbols on DIEs in the current comdat debug
6398 info section. */
6399 static const char *comdat_symbol_id;
6401 /* The index of the current symbol within the current comdat CU. */
6402 static unsigned int comdat_symbol_number;
6404 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6405 children, and set comdat_symbol_id accordingly. */
6407 static void
6408 compute_section_prefix (dw_die_ref unit_die)
6410 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6411 const char *base = die_name ? lbasename (die_name) : "anonymous";
6412 char *name = XALLOCAVEC (char, strlen (base) + 64);
6413 char *p;
6414 int i, mark;
6415 unsigned char checksum[16];
6416 struct md5_ctx ctx;
6418 /* Compute the checksum of the DIE, then append part of it as hex digits to
6419 the name filename of the unit. */
6421 md5_init_ctx (&ctx);
6422 mark = 0;
6423 die_checksum (unit_die, &ctx, &mark);
6424 unmark_all_dies (unit_die);
6425 md5_finish_ctx (&ctx, checksum);
6427 sprintf (name, "%s.", base);
6428 clean_symbol_name (name);
6430 p = name + strlen (name);
6431 for (i = 0; i < 4; i++)
6433 sprintf (p, "%.2x", checksum[i]);
6434 p += 2;
6437 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6438 comdat_symbol_number = 0;
6441 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6443 static int
6444 is_type_die (dw_die_ref die)
6446 switch (die->die_tag)
6448 case DW_TAG_array_type:
6449 case DW_TAG_class_type:
6450 case DW_TAG_interface_type:
6451 case DW_TAG_enumeration_type:
6452 case DW_TAG_pointer_type:
6453 case DW_TAG_reference_type:
6454 case DW_TAG_rvalue_reference_type:
6455 case DW_TAG_string_type:
6456 case DW_TAG_structure_type:
6457 case DW_TAG_subroutine_type:
6458 case DW_TAG_union_type:
6459 case DW_TAG_ptr_to_member_type:
6460 case DW_TAG_set_type:
6461 case DW_TAG_subrange_type:
6462 case DW_TAG_base_type:
6463 case DW_TAG_const_type:
6464 case DW_TAG_file_type:
6465 case DW_TAG_packed_type:
6466 case DW_TAG_volatile_type:
6467 case DW_TAG_typedef:
6468 return 1;
6469 default:
6470 return 0;
6474 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6475 Basically, we want to choose the bits that are likely to be shared between
6476 compilations (types) and leave out the bits that are specific to individual
6477 compilations (functions). */
6479 static int
6480 is_comdat_die (dw_die_ref c)
6482 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6483 we do for stabs. The advantage is a greater likelihood of sharing between
6484 objects that don't include headers in the same order (and therefore would
6485 put the base types in a different comdat). jason 8/28/00 */
6487 if (c->die_tag == DW_TAG_base_type)
6488 return 0;
6490 if (c->die_tag == DW_TAG_pointer_type
6491 || c->die_tag == DW_TAG_reference_type
6492 || c->die_tag == DW_TAG_rvalue_reference_type
6493 || c->die_tag == DW_TAG_const_type
6494 || c->die_tag == DW_TAG_volatile_type)
6496 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6498 return t ? is_comdat_die (t) : 0;
6501 return is_type_die (c);
6504 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6505 compilation unit. */
6507 static int
6508 is_symbol_die (dw_die_ref c)
6510 return (is_type_die (c)
6511 || is_declaration_die (c)
6512 || c->die_tag == DW_TAG_namespace
6513 || c->die_tag == DW_TAG_module);
6516 /* Returns true iff C is a compile-unit DIE. */
6518 static inline bool
6519 is_cu_die (dw_die_ref c)
6521 return c && c->die_tag == DW_TAG_compile_unit;
6524 /* Returns true iff C is a unit DIE of some sort. */
6526 static inline bool
6527 is_unit_die (dw_die_ref c)
6529 return c && (c->die_tag == DW_TAG_compile_unit
6530 || c->die_tag == DW_TAG_partial_unit
6531 || c->die_tag == DW_TAG_type_unit);
6534 /* Returns true iff C is a namespace DIE. */
6536 static inline bool
6537 is_namespace_die (dw_die_ref c)
6539 return c && c->die_tag == DW_TAG_namespace;
6542 /* Returns true iff C is a class or structure DIE. */
6544 static inline bool
6545 is_class_die (dw_die_ref c)
6547 return c && (c->die_tag == DW_TAG_class_type
6548 || c->die_tag == DW_TAG_structure_type);
6551 /* Return non-zero if this DIE is a template parameter. */
6553 static inline bool
6554 is_template_parameter (dw_die_ref die)
6556 switch (die->die_tag)
6558 case DW_TAG_template_type_param:
6559 case DW_TAG_template_value_param:
6560 case DW_TAG_GNU_template_template_param:
6561 case DW_TAG_GNU_template_parameter_pack:
6562 return true;
6563 default:
6564 return false;
6568 /* Return non-zero if this DIE represents a template instantiation. */
6570 static inline bool
6571 is_template_instantiation (dw_die_ref die)
6573 dw_die_ref c;
6575 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
6576 return false;
6577 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
6578 return false;
6581 static char *
6582 gen_internal_sym (const char *prefix)
6584 char buf[256];
6586 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6587 return xstrdup (buf);
6590 /* Assign symbols to all worthy DIEs under DIE. */
6592 static void
6593 assign_symbol_names (dw_die_ref die)
6595 dw_die_ref c;
6597 if (is_symbol_die (die) && !die->comdat_type_p)
6599 if (comdat_symbol_id)
6601 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6603 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6604 comdat_symbol_id, comdat_symbol_number++);
6605 die->die_id.die_symbol = xstrdup (p);
6607 else
6608 die->die_id.die_symbol = gen_internal_sym ("LDIE");
6611 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6614 struct cu_hash_table_entry
6616 dw_die_ref cu;
6617 unsigned min_comdat_num, max_comdat_num;
6618 struct cu_hash_table_entry *next;
6621 /* Helpers to manipulate hash table of CUs. */
6623 struct cu_hash_table_entry_hasher
6625 typedef cu_hash_table_entry value_type;
6626 typedef die_struct compare_type;
6627 static inline hashval_t hash (const value_type *);
6628 static inline bool equal (const value_type *, const compare_type *);
6629 static inline void remove (value_type *);
6632 inline hashval_t
6633 cu_hash_table_entry_hasher::hash (const value_type *entry)
6635 return htab_hash_string (entry->cu->die_id.die_symbol);
6638 inline bool
6639 cu_hash_table_entry_hasher::equal (const value_type *entry1,
6640 const compare_type *entry2)
6642 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6645 inline void
6646 cu_hash_table_entry_hasher::remove (value_type *entry)
6648 struct cu_hash_table_entry *next;
6650 while (entry)
6652 next = entry->next;
6653 free (entry);
6654 entry = next;
6658 typedef hash_table <cu_hash_table_entry_hasher> cu_hash_type;
6660 /* Check whether we have already seen this CU and set up SYM_NUM
6661 accordingly. */
6662 static int
6663 check_duplicate_cu (dw_die_ref cu, cu_hash_type htable, unsigned int *sym_num)
6665 struct cu_hash_table_entry dummy;
6666 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6668 dummy.max_comdat_num = 0;
6670 slot = htable.find_slot_with_hash (cu,
6671 htab_hash_string (cu->die_id.die_symbol),
6672 INSERT);
6673 entry = *slot;
6675 for (; entry; last = entry, entry = entry->next)
6677 if (same_die_p_wrap (cu, entry->cu))
6678 break;
6681 if (entry)
6683 *sym_num = entry->min_comdat_num;
6684 return 1;
6687 entry = XCNEW (struct cu_hash_table_entry);
6688 entry->cu = cu;
6689 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6690 entry->next = *slot;
6691 *slot = entry;
6693 return 0;
6696 /* Record SYM_NUM to record of CU in HTABLE. */
6697 static void
6698 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type htable,
6699 unsigned int sym_num)
6701 struct cu_hash_table_entry **slot, *entry;
6703 slot = htable.find_slot_with_hash (cu,
6704 htab_hash_string (cu->die_id.die_symbol),
6705 NO_INSERT);
6706 entry = *slot;
6708 entry->max_comdat_num = sym_num;
6711 /* Traverse the DIE (which is always comp_unit_die), and set up
6712 additional compilation units for each of the include files we see
6713 bracketed by BINCL/EINCL. */
6715 static void
6716 break_out_includes (dw_die_ref die)
6718 dw_die_ref c;
6719 dw_die_ref unit = NULL;
6720 limbo_die_node *node, **pnode;
6721 cu_hash_type cu_hash_table;
6723 c = die->die_child;
6724 if (c) do {
6725 dw_die_ref prev = c;
6726 c = c->die_sib;
6727 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6728 || (unit && is_comdat_die (c)))
6730 dw_die_ref next = c->die_sib;
6732 /* This DIE is for a secondary CU; remove it from the main one. */
6733 remove_child_with_prev (c, prev);
6735 if (c->die_tag == DW_TAG_GNU_BINCL)
6736 unit = push_new_compile_unit (unit, c);
6737 else if (c->die_tag == DW_TAG_GNU_EINCL)
6738 unit = pop_compile_unit (unit);
6739 else
6740 add_child_die (unit, c);
6741 c = next;
6742 if (c == die->die_child)
6743 break;
6745 } while (c != die->die_child);
6747 #if 0
6748 /* We can only use this in debugging, since the frontend doesn't check
6749 to make sure that we leave every include file we enter. */
6750 gcc_assert (!unit);
6751 #endif
6753 assign_symbol_names (die);
6754 cu_hash_table.create (10);
6755 for (node = limbo_die_list, pnode = &limbo_die_list;
6756 node;
6757 node = node->next)
6759 int is_dupl;
6761 compute_section_prefix (node->die);
6762 is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6763 &comdat_symbol_number);
6764 assign_symbol_names (node->die);
6765 if (is_dupl)
6766 *pnode = node->next;
6767 else
6769 pnode = &node->next;
6770 record_comdat_symbol_number (node->die, cu_hash_table,
6771 comdat_symbol_number);
6774 cu_hash_table.dispose ();
6777 /* Return non-zero if this DIE is a declaration. */
6779 static int
6780 is_declaration_die (dw_die_ref die)
6782 dw_attr_ref a;
6783 unsigned ix;
6785 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6786 if (a->dw_attr == DW_AT_declaration)
6787 return 1;
6789 return 0;
6792 /* Return non-zero if this DIE is nested inside a subprogram. */
6794 static int
6795 is_nested_in_subprogram (dw_die_ref die)
6797 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
6799 if (decl == NULL)
6800 decl = die;
6801 return local_scope_p (decl);
6804 /* Return non-zero if this DIE contains a defining declaration of a
6805 subprogram. */
6807 static int
6808 contains_subprogram_definition (dw_die_ref die)
6810 dw_die_ref c;
6812 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
6813 return 1;
6814 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
6815 return 0;
6818 /* Return non-zero if this is a type DIE that should be moved to a
6819 COMDAT .debug_types section. */
6821 static int
6822 should_move_die_to_comdat (dw_die_ref die)
6824 switch (die->die_tag)
6826 case DW_TAG_class_type:
6827 case DW_TAG_structure_type:
6828 case DW_TAG_enumeration_type:
6829 case DW_TAG_union_type:
6830 /* Don't move declarations, inlined instances, or types nested in a
6831 subprogram. */
6832 if (is_declaration_die (die)
6833 || get_AT (die, DW_AT_abstract_origin)
6834 || is_nested_in_subprogram (die))
6835 return 0;
6836 /* A type definition should never contain a subprogram definition. */
6837 gcc_assert (!contains_subprogram_definition (die));
6838 return 1;
6839 case DW_TAG_array_type:
6840 case DW_TAG_interface_type:
6841 case DW_TAG_pointer_type:
6842 case DW_TAG_reference_type:
6843 case DW_TAG_rvalue_reference_type:
6844 case DW_TAG_string_type:
6845 case DW_TAG_subroutine_type:
6846 case DW_TAG_ptr_to_member_type:
6847 case DW_TAG_set_type:
6848 case DW_TAG_subrange_type:
6849 case DW_TAG_base_type:
6850 case DW_TAG_const_type:
6851 case DW_TAG_file_type:
6852 case DW_TAG_packed_type:
6853 case DW_TAG_volatile_type:
6854 case DW_TAG_typedef:
6855 default:
6856 return 0;
6860 /* Make a clone of DIE. */
6862 static dw_die_ref
6863 clone_die (dw_die_ref die)
6865 dw_die_ref clone;
6866 dw_attr_ref a;
6867 unsigned ix;
6869 clone = ggc_alloc_cleared_die_node ();
6870 clone->die_tag = die->die_tag;
6872 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6873 add_dwarf_attr (clone, a);
6875 return clone;
6878 /* Make a clone of the tree rooted at DIE. */
6880 static dw_die_ref
6881 clone_tree (dw_die_ref die)
6883 dw_die_ref c;
6884 dw_die_ref clone = clone_die (die);
6886 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
6888 return clone;
6891 /* Make a clone of DIE as a declaration. */
6893 static dw_die_ref
6894 clone_as_declaration (dw_die_ref die)
6896 dw_die_ref clone;
6897 dw_die_ref decl;
6898 dw_attr_ref a;
6899 unsigned ix;
6901 /* If the DIE is already a declaration, just clone it. */
6902 if (is_declaration_die (die))
6903 return clone_die (die);
6905 /* If the DIE is a specification, just clone its declaration DIE. */
6906 decl = get_AT_ref (die, DW_AT_specification);
6907 if (decl != NULL)
6909 clone = clone_die (decl);
6910 if (die->comdat_type_p)
6911 add_AT_die_ref (clone, DW_AT_signature, die);
6912 return clone;
6915 clone = ggc_alloc_cleared_die_node ();
6916 clone->die_tag = die->die_tag;
6918 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6920 /* We don't want to copy over all attributes.
6921 For example we don't want DW_AT_byte_size because otherwise we will no
6922 longer have a declaration and GDB will treat it as a definition. */
6924 switch (a->dw_attr)
6926 case DW_AT_artificial:
6927 case DW_AT_containing_type:
6928 case DW_AT_external:
6929 case DW_AT_name:
6930 case DW_AT_type:
6931 case DW_AT_virtuality:
6932 case DW_AT_linkage_name:
6933 case DW_AT_MIPS_linkage_name:
6934 add_dwarf_attr (clone, a);
6935 break;
6936 case DW_AT_byte_size:
6937 default:
6938 break;
6942 if (die->comdat_type_p)
6943 add_AT_die_ref (clone, DW_AT_signature, die);
6945 add_AT_flag (clone, DW_AT_declaration, 1);
6946 return clone;
6950 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
6952 struct decl_table_entry
6954 dw_die_ref orig;
6955 dw_die_ref copy;
6958 /* Helpers to manipulate hash table of copied declarations. */
6960 /* Hashtable helpers. */
6962 struct decl_table_entry_hasher : typed_free_remove <decl_table_entry>
6964 typedef decl_table_entry value_type;
6965 typedef die_struct compare_type;
6966 static inline hashval_t hash (const value_type *);
6967 static inline bool equal (const value_type *, const compare_type *);
6970 inline hashval_t
6971 decl_table_entry_hasher::hash (const value_type *entry)
6973 return htab_hash_pointer (entry->orig);
6976 inline bool
6977 decl_table_entry_hasher::equal (const value_type *entry1,
6978 const compare_type *entry2)
6980 return entry1->orig == entry2;
6983 typedef hash_table <decl_table_entry_hasher> decl_hash_type;
6985 /* Copy DIE and its ancestors, up to, but not including, the compile unit
6986 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
6987 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
6988 to check if the ancestor has already been copied into UNIT. */
6990 static dw_die_ref
6991 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, decl_hash_type decl_table)
6993 dw_die_ref parent = die->die_parent;
6994 dw_die_ref new_parent = unit;
6995 dw_die_ref copy;
6996 decl_table_entry **slot = NULL;
6997 struct decl_table_entry *entry = NULL;
6999 if (decl_table.is_created ())
7001 /* Check if the entry has already been copied to UNIT. */
7002 slot = decl_table.find_slot_with_hash (die, htab_hash_pointer (die),
7003 INSERT);
7004 if (*slot != HTAB_EMPTY_ENTRY)
7006 entry = *slot;
7007 return entry->copy;
7010 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7011 entry = XCNEW (struct decl_table_entry);
7012 entry->orig = die;
7013 entry->copy = NULL;
7014 *slot = entry;
7017 if (parent != NULL)
7019 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7020 if (spec != NULL)
7021 parent = spec;
7022 if (!is_unit_die (parent))
7023 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7026 copy = clone_as_declaration (die);
7027 add_child_die (new_parent, copy);
7029 if (decl_table.is_created ())
7031 /* Record the pointer to the copy. */
7032 entry->copy = copy;
7035 return copy;
7037 /* Copy the declaration context to the new type unit DIE. This includes
7038 any surrounding namespace or type declarations. If the DIE has an
7039 AT_specification attribute, it also includes attributes and children
7040 attached to the specification, and returns a pointer to the original
7041 parent of the declaration DIE. Returns NULL otherwise. */
7043 static dw_die_ref
7044 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7046 dw_die_ref decl;
7047 dw_die_ref new_decl;
7048 dw_die_ref orig_parent = NULL;
7050 decl = get_AT_ref (die, DW_AT_specification);
7051 if (decl == NULL)
7052 decl = die;
7053 else
7055 unsigned ix;
7056 dw_die_ref c;
7057 dw_attr_ref a;
7059 /* The original DIE will be changed to a declaration, and must
7060 be moved to be a child of the original declaration DIE. */
7061 orig_parent = decl->die_parent;
7063 /* Copy the type node pointer from the new DIE to the original
7064 declaration DIE so we can forward references later. */
7065 decl->comdat_type_p = true;
7066 decl->die_id.die_type_node = die->die_id.die_type_node;
7068 remove_AT (die, DW_AT_specification);
7070 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7072 if (a->dw_attr != DW_AT_name
7073 && a->dw_attr != DW_AT_declaration
7074 && a->dw_attr != DW_AT_external)
7075 add_dwarf_attr (die, a);
7078 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7081 if (decl->die_parent != NULL
7082 && !is_unit_die (decl->die_parent))
7084 new_decl = copy_ancestor_tree (unit, decl, decl_hash_type ());
7085 if (new_decl != NULL)
7087 remove_AT (new_decl, DW_AT_signature);
7088 add_AT_specification (die, new_decl);
7092 return orig_parent;
7095 /* Generate the skeleton ancestor tree for the given NODE, then clone
7096 the DIE and add the clone into the tree. */
7098 static void
7099 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7101 if (node->new_die != NULL)
7102 return;
7104 node->new_die = clone_as_declaration (node->old_die);
7106 if (node->parent != NULL)
7108 generate_skeleton_ancestor_tree (node->parent);
7109 add_child_die (node->parent->new_die, node->new_die);
7113 /* Generate a skeleton tree of DIEs containing any declarations that are
7114 found in the original tree. We traverse the tree looking for declaration
7115 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7117 static void
7118 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7120 skeleton_chain_node node;
7121 dw_die_ref c;
7122 dw_die_ref first;
7123 dw_die_ref prev = NULL;
7124 dw_die_ref next = NULL;
7126 node.parent = parent;
7128 first = c = parent->old_die->die_child;
7129 if (c)
7130 next = c->die_sib;
7131 if (c) do {
7132 if (prev == NULL || prev->die_sib == c)
7133 prev = c;
7134 c = next;
7135 next = (c == first ? NULL : c->die_sib);
7136 node.old_die = c;
7137 node.new_die = NULL;
7138 if (is_declaration_die (c))
7140 if (is_template_instantiation (c))
7142 /* Instantiated templates do not need to be cloned into the
7143 type unit. Just move the DIE and its children back to
7144 the skeleton tree (in the main CU). */
7145 remove_child_with_prev (c, prev);
7146 add_child_die (parent->new_die, c);
7147 c = prev;
7149 else
7151 /* Clone the existing DIE, move the original to the skeleton
7152 tree (which is in the main CU), and put the clone, with
7153 all the original's children, where the original came from
7154 (which is about to be moved to the type unit). */
7155 dw_die_ref clone = clone_die (c);
7156 move_all_children (c, clone);
7158 replace_child (c, clone, prev);
7159 generate_skeleton_ancestor_tree (parent);
7160 add_child_die (parent->new_die, c);
7161 node.new_die = c;
7162 c = clone;
7165 generate_skeleton_bottom_up (&node);
7166 } while (next != NULL);
7169 /* Wrapper function for generate_skeleton_bottom_up. */
7171 static dw_die_ref
7172 generate_skeleton (dw_die_ref die)
7174 skeleton_chain_node node;
7176 node.old_die = die;
7177 node.new_die = NULL;
7178 node.parent = NULL;
7180 /* If this type definition is nested inside another type,
7181 and is not an instantiation of a template, always leave
7182 at least a declaration in its place. */
7183 if (die->die_parent != NULL
7184 && is_type_die (die->die_parent)
7185 && !is_template_instantiation (die))
7186 node.new_die = clone_as_declaration (die);
7188 generate_skeleton_bottom_up (&node);
7189 return node.new_die;
7192 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7193 declaration. The original DIE is moved to a new compile unit so that
7194 existing references to it follow it to the new location. If any of the
7195 original DIE's descendants is a declaration, we need to replace the
7196 original DIE with a skeleton tree and move the declarations back into the
7197 skeleton tree. */
7199 static dw_die_ref
7200 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7201 dw_die_ref prev)
7203 dw_die_ref skeleton, orig_parent;
7205 /* Copy the declaration context to the type unit DIE. If the returned
7206 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7207 that DIE. */
7208 orig_parent = copy_declaration_context (unit, child);
7210 skeleton = generate_skeleton (child);
7211 if (skeleton == NULL)
7212 remove_child_with_prev (child, prev);
7213 else
7215 skeleton->comdat_type_p = true;
7216 skeleton->die_id.die_type_node = child->die_id.die_type_node;
7218 /* If the original DIE was a specification, we need to put
7219 the skeleton under the parent DIE of the declaration.
7220 This leaves the original declaration in the tree, but
7221 it will be pruned later since there are no longer any
7222 references to it. */
7223 if (orig_parent != NULL)
7225 remove_child_with_prev (child, prev);
7226 add_child_die (orig_parent, skeleton);
7228 else
7229 replace_child (child, skeleton, prev);
7232 return skeleton;
7235 /* Traverse the DIE and set up additional .debug_types sections for each
7236 type worthy of being placed in a COMDAT section. */
7238 static void
7239 break_out_comdat_types (dw_die_ref die)
7241 dw_die_ref c;
7242 dw_die_ref first;
7243 dw_die_ref prev = NULL;
7244 dw_die_ref next = NULL;
7245 dw_die_ref unit = NULL;
7247 first = c = die->die_child;
7248 if (c)
7249 next = c->die_sib;
7250 if (c) do {
7251 if (prev == NULL || prev->die_sib == c)
7252 prev = c;
7253 c = next;
7254 next = (c == first ? NULL : c->die_sib);
7255 if (should_move_die_to_comdat (c))
7257 dw_die_ref replacement;
7258 comdat_type_node_ref type_node;
7260 /* Break out nested types into their own type units. */
7261 break_out_comdat_types (c);
7263 /* Create a new type unit DIE as the root for the new tree, and
7264 add it to the list of comdat types. */
7265 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7266 add_AT_unsigned (unit, DW_AT_language,
7267 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7268 type_node = ggc_alloc_cleared_comdat_type_node ();
7269 type_node->root_die = unit;
7270 type_node->next = comdat_type_list;
7271 comdat_type_list = type_node;
7273 /* Generate the type signature. */
7274 generate_type_signature (c, type_node);
7276 /* Copy the declaration context, attributes, and children of the
7277 declaration into the new type unit DIE, then remove this DIE
7278 from the main CU (or replace it with a skeleton if necessary). */
7279 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7280 type_node->skeleton_die = replacement;
7282 /* Add the DIE to the new compunit. */
7283 add_child_die (unit, c);
7285 if (replacement != NULL)
7286 c = replacement;
7288 else if (c->die_tag == DW_TAG_namespace
7289 || c->die_tag == DW_TAG_class_type
7290 || c->die_tag == DW_TAG_structure_type
7291 || c->die_tag == DW_TAG_union_type)
7293 /* Look for nested types that can be broken out. */
7294 break_out_comdat_types (c);
7296 } while (next != NULL);
7299 /* Like clone_tree, but additionally enter all the children into
7300 the hash table decl_table. */
7302 static dw_die_ref
7303 clone_tree_hash (dw_die_ref die, decl_hash_type decl_table)
7305 dw_die_ref c;
7306 dw_die_ref clone = clone_die (die);
7307 struct decl_table_entry *entry;
7308 decl_table_entry **slot = decl_table.find_slot_with_hash (die,
7309 htab_hash_pointer (die), INSERT);
7310 /* Assert that DIE isn't in the hash table yet. If it would be there
7311 before, the ancestors would be necessarily there as well, therefore
7312 clone_tree_hash wouldn't be called. */
7313 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7314 entry = XCNEW (struct decl_table_entry);
7315 entry->orig = die;
7316 entry->copy = clone;
7317 *slot = entry;
7319 FOR_EACH_CHILD (die, c,
7320 add_child_die (clone, clone_tree_hash (c, decl_table)));
7322 return clone;
7325 /* Walk the DIE and its children, looking for references to incomplete
7326 or trivial types that are unmarked (i.e., that are not in the current
7327 type_unit). */
7329 static void
7330 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type decl_table)
7332 dw_die_ref c;
7333 dw_attr_ref a;
7334 unsigned ix;
7336 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7338 if (AT_class (a) == dw_val_class_die_ref)
7340 dw_die_ref targ = AT_ref (a);
7341 decl_table_entry **slot;
7342 struct decl_table_entry *entry;
7344 if (targ->die_mark != 0 || targ->comdat_type_p)
7345 continue;
7347 slot = decl_table.find_slot_with_hash (targ, htab_hash_pointer (targ),
7348 INSERT);
7350 if (*slot != HTAB_EMPTY_ENTRY)
7352 /* TARG has already been copied, so we just need to
7353 modify the reference to point to the copy. */
7354 entry = *slot;
7355 a->dw_attr_val.v.val_die_ref.die = entry->copy;
7357 else
7359 dw_die_ref parent = unit;
7360 dw_die_ref copy = clone_die (targ);
7362 /* Record in DECL_TABLE that TARG has been copied.
7363 Need to do this now, before the recursive call,
7364 because DECL_TABLE may be expanded and SLOT
7365 would no longer be a valid pointer. */
7366 entry = XCNEW (struct decl_table_entry);
7367 entry->orig = targ;
7368 entry->copy = copy;
7369 *slot = entry;
7371 FOR_EACH_CHILD (targ, c,
7372 add_child_die (copy,
7373 clone_tree_hash (c, decl_table)));
7375 /* Make sure the cloned tree is marked as part of the
7376 type unit. */
7377 mark_dies (copy);
7379 /* If TARG has surrounding context, copy its ancestor tree
7380 into the new type unit. */
7381 if (targ->die_parent != NULL
7382 && !is_unit_die (targ->die_parent))
7383 parent = copy_ancestor_tree (unit, targ->die_parent,
7384 decl_table);
7386 add_child_die (parent, copy);
7387 a->dw_attr_val.v.val_die_ref.die = copy;
7389 /* Make sure the newly-copied DIE is walked. If it was
7390 installed in a previously-added context, it won't
7391 get visited otherwise. */
7392 if (parent != unit)
7394 /* Find the highest point of the newly-added tree,
7395 mark each node along the way, and walk from there. */
7396 parent->die_mark = 1;
7397 while (parent->die_parent
7398 && parent->die_parent->die_mark == 0)
7400 parent = parent->die_parent;
7401 parent->die_mark = 1;
7403 copy_decls_walk (unit, parent, decl_table);
7409 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7412 /* Copy declarations for "unworthy" types into the new comdat section.
7413 Incomplete types, modified types, and certain other types aren't broken
7414 out into comdat sections of their own, so they don't have a signature,
7415 and we need to copy the declaration into the same section so that we
7416 don't have an external reference. */
7418 static void
7419 copy_decls_for_unworthy_types (dw_die_ref unit)
7421 decl_hash_type decl_table;
7423 mark_dies (unit);
7424 decl_table.create (10);
7425 copy_decls_walk (unit, unit, decl_table);
7426 decl_table.dispose ();
7427 unmark_dies (unit);
7430 /* Traverse the DIE and add a sibling attribute if it may have the
7431 effect of speeding up access to siblings. To save some space,
7432 avoid generating sibling attributes for DIE's without children. */
7434 static void
7435 add_sibling_attributes (dw_die_ref die)
7437 dw_die_ref c;
7439 if (! die->die_child)
7440 return;
7442 if (die->die_parent && die != die->die_parent->die_child)
7443 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7445 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7448 /* Output all location lists for the DIE and its children. */
7450 static void
7451 output_location_lists (dw_die_ref die)
7453 dw_die_ref c;
7454 dw_attr_ref a;
7455 unsigned ix;
7457 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7458 if (AT_class (a) == dw_val_class_loc_list)
7459 output_loc_list (AT_loc_list (a));
7461 FOR_EACH_CHILD (die, c, output_location_lists (c));
7464 /* We want to limit the number of external references, because they are
7465 larger than local references: a relocation takes multiple words, and
7466 even a sig8 reference is always eight bytes, whereas a local reference
7467 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7468 So if we encounter multiple external references to the same type DIE, we
7469 make a local typedef stub for it and redirect all references there.
7471 This is the element of the hash table for keeping track of these
7472 references. */
7474 struct external_ref
7476 dw_die_ref type;
7477 dw_die_ref stub;
7478 unsigned n_refs;
7481 /* Hashtable helpers. */
7483 struct external_ref_hasher : typed_free_remove <external_ref>
7485 typedef external_ref value_type;
7486 typedef external_ref compare_type;
7487 static inline hashval_t hash (const value_type *);
7488 static inline bool equal (const value_type *, const compare_type *);
7491 inline hashval_t
7492 external_ref_hasher::hash (const value_type *r)
7494 dw_die_ref die = r->type;
7495 hashval_t h = 0;
7497 /* We can't use the address of the DIE for hashing, because
7498 that will make the order of the stub DIEs non-deterministic. */
7499 if (! die->comdat_type_p)
7500 /* We have a symbol; use it to compute a hash. */
7501 h = htab_hash_string (die->die_id.die_symbol);
7502 else
7504 /* We have a type signature; use a subset of the bits as the hash.
7505 The 8-byte signature is at least as large as hashval_t. */
7506 comdat_type_node_ref type_node = die->die_id.die_type_node;
7507 memcpy (&h, type_node->signature, sizeof (h));
7509 return h;
7512 inline bool
7513 external_ref_hasher::equal (const value_type *r1, const compare_type *r2)
7515 return r1->type == r2->type;
7518 typedef hash_table <external_ref_hasher> external_ref_hash_type;
7520 /* Return a pointer to the external_ref for references to DIE. */
7522 static struct external_ref *
7523 lookup_external_ref (external_ref_hash_type map, dw_die_ref die)
7525 struct external_ref ref, *ref_p;
7526 external_ref **slot;
7528 ref.type = die;
7529 slot = map.find_slot (&ref, INSERT);
7530 if (*slot != HTAB_EMPTY_ENTRY)
7531 return *slot;
7533 ref_p = XCNEW (struct external_ref);
7534 ref_p->type = die;
7535 *slot = ref_p;
7536 return ref_p;
7539 /* Subroutine of optimize_external_refs, below.
7541 If we see a type skeleton, record it as our stub. If we see external
7542 references, remember how many we've seen. */
7544 static void
7545 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type map)
7547 dw_die_ref c;
7548 dw_attr_ref a;
7549 unsigned ix;
7550 struct external_ref *ref_p;
7552 if (is_type_die (die)
7553 && (c = get_AT_ref (die, DW_AT_signature)))
7555 /* This is a local skeleton; use it for local references. */
7556 ref_p = lookup_external_ref (map, c);
7557 ref_p->stub = die;
7560 /* Scan the DIE references, and remember any that refer to DIEs from
7561 other CUs (i.e. those which are not marked). */
7562 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7563 if (AT_class (a) == dw_val_class_die_ref
7564 && (c = AT_ref (a))->die_mark == 0
7565 && is_type_die (c))
7567 ref_p = lookup_external_ref (map, c);
7568 ref_p->n_refs++;
7571 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7574 /* htab_traverse callback function for optimize_external_refs, below. SLOT
7575 points to an external_ref, DATA is the CU we're processing. If we don't
7576 already have a local stub, and we have multiple refs, build a stub. */
7579 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
7581 struct external_ref *ref_p = *slot;
7583 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7585 /* We have multiple references to this type, so build a small stub.
7586 Both of these forms are a bit dodgy from the perspective of the
7587 DWARF standard, since technically they should have names. */
7588 dw_die_ref cu = data;
7589 dw_die_ref type = ref_p->type;
7590 dw_die_ref stub = NULL;
7592 if (type->comdat_type_p)
7594 /* If we refer to this type via sig8, use AT_signature. */
7595 stub = new_die (type->die_tag, cu, NULL_TREE);
7596 add_AT_die_ref (stub, DW_AT_signature, type);
7598 else
7600 /* Otherwise, use a typedef with no name. */
7601 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7602 add_AT_die_ref (stub, DW_AT_type, type);
7605 stub->die_mark++;
7606 ref_p->stub = stub;
7608 return 1;
7611 /* DIE is a unit; look through all the DIE references to see if there are
7612 any external references to types, and if so, create local stubs for
7613 them which will be applied in build_abbrev_table. This is useful because
7614 references to local DIEs are smaller. */
7616 static external_ref_hash_type
7617 optimize_external_refs (dw_die_ref die)
7619 external_ref_hash_type map;
7620 map.create (10);
7621 optimize_external_refs_1 (die, map);
7622 map.traverse <dw_die_ref, dwarf2_build_local_stub> (die);
7623 return map;
7626 /* The format of each DIE (and its attribute value pairs) is encoded in an
7627 abbreviation table. This routine builds the abbreviation table and assigns
7628 a unique abbreviation id for each abbreviation entry. The children of each
7629 die are visited recursively. */
7631 static void
7632 build_abbrev_table (dw_die_ref die, external_ref_hash_type extern_map)
7634 unsigned long abbrev_id;
7635 unsigned int n_alloc;
7636 dw_die_ref c;
7637 dw_attr_ref a;
7638 unsigned ix;
7640 /* Scan the DIE references, and replace any that refer to
7641 DIEs from other CUs (i.e. those which are not marked) with
7642 the local stubs we built in optimize_external_refs. */
7643 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7644 if (AT_class (a) == dw_val_class_die_ref
7645 && (c = AT_ref (a))->die_mark == 0)
7647 struct external_ref *ref_p;
7648 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
7650 ref_p = lookup_external_ref (extern_map, c);
7651 if (ref_p->stub && ref_p->stub != die)
7652 change_AT_die_ref (a, ref_p->stub);
7653 else
7654 /* We aren't changing this reference, so mark it external. */
7655 set_AT_ref_external (a, 1);
7658 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7660 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7661 dw_attr_ref die_a, abbrev_a;
7662 unsigned ix;
7663 bool ok = true;
7665 if (abbrev->die_tag != die->die_tag)
7666 continue;
7667 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7668 continue;
7670 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
7671 continue;
7673 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
7675 abbrev_a = &(*abbrev->die_attr)[ix];
7676 if ((abbrev_a->dw_attr != die_a->dw_attr)
7677 || (value_format (abbrev_a) != value_format (die_a)))
7679 ok = false;
7680 break;
7683 if (ok)
7684 break;
7687 if (abbrev_id >= abbrev_die_table_in_use)
7689 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7691 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7692 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7693 n_alloc);
7695 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7696 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7697 abbrev_die_table_allocated = n_alloc;
7700 ++abbrev_die_table_in_use;
7701 abbrev_die_table[abbrev_id] = die;
7704 die->die_abbrev = abbrev_id;
7705 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
7708 /* Return the power-of-two number of bytes necessary to represent VALUE. */
7710 static int
7711 constant_size (unsigned HOST_WIDE_INT value)
7713 int log;
7715 if (value == 0)
7716 log = 0;
7717 else
7718 log = floor_log2 (value);
7720 log = log / 8;
7721 log = 1 << (floor_log2 (log) + 1);
7723 return log;
7726 /* Return the size of a DIE as it is represented in the
7727 .debug_info section. */
7729 static unsigned long
7730 size_of_die (dw_die_ref die)
7732 unsigned long size = 0;
7733 dw_attr_ref a;
7734 unsigned ix;
7735 enum dwarf_form form;
7737 size += size_of_uleb128 (die->die_abbrev);
7738 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7740 switch (AT_class (a))
7742 case dw_val_class_addr:
7743 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7745 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7746 size += size_of_uleb128 (AT_index (a));
7748 else
7749 size += DWARF2_ADDR_SIZE;
7750 break;
7751 case dw_val_class_offset:
7752 size += DWARF_OFFSET_SIZE;
7753 break;
7754 case dw_val_class_loc:
7756 unsigned long lsize = size_of_locs (AT_loc (a));
7758 /* Block length. */
7759 if (dwarf_version >= 4)
7760 size += size_of_uleb128 (lsize);
7761 else
7762 size += constant_size (lsize);
7763 size += lsize;
7765 break;
7766 case dw_val_class_loc_list:
7767 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7769 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7770 size += size_of_uleb128 (AT_index (a));
7772 else
7773 size += DWARF_OFFSET_SIZE;
7774 break;
7775 case dw_val_class_range_list:
7776 size += DWARF_OFFSET_SIZE;
7777 break;
7778 case dw_val_class_const:
7779 size += size_of_sleb128 (AT_int (a));
7780 break;
7781 case dw_val_class_unsigned_const:
7783 int csize = constant_size (AT_unsigned (a));
7784 if (dwarf_version == 3
7785 && a->dw_attr == DW_AT_data_member_location
7786 && csize >= 4)
7787 size += size_of_uleb128 (AT_unsigned (a));
7788 else
7789 size += csize;
7791 break;
7792 case dw_val_class_const_double:
7793 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
7794 if (HOST_BITS_PER_WIDE_INT >= 64)
7795 size++; /* block */
7796 break;
7797 case dw_val_class_vec:
7798 size += constant_size (a->dw_attr_val.v.val_vec.length
7799 * a->dw_attr_val.v.val_vec.elt_size)
7800 + a->dw_attr_val.v.val_vec.length
7801 * a->dw_attr_val.v.val_vec.elt_size; /* block */
7802 break;
7803 case dw_val_class_flag:
7804 if (dwarf_version >= 4)
7805 /* Currently all add_AT_flag calls pass in 1 as last argument,
7806 so DW_FORM_flag_present can be used. If that ever changes,
7807 we'll need to use DW_FORM_flag and have some optimization
7808 in build_abbrev_table that will change those to
7809 DW_FORM_flag_present if it is set to 1 in all DIEs using
7810 the same abbrev entry. */
7811 gcc_assert (a->dw_attr_val.v.val_flag == 1);
7812 else
7813 size += 1;
7814 break;
7815 case dw_val_class_die_ref:
7816 if (AT_ref_external (a))
7818 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
7819 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
7820 is sized by target address length, whereas in DWARF3
7821 it's always sized as an offset. */
7822 if (use_debug_types)
7823 size += DWARF_TYPE_SIGNATURE_SIZE;
7824 else if (dwarf_version == 2)
7825 size += DWARF2_ADDR_SIZE;
7826 else
7827 size += DWARF_OFFSET_SIZE;
7829 else
7830 size += DWARF_OFFSET_SIZE;
7831 break;
7832 case dw_val_class_fde_ref:
7833 size += DWARF_OFFSET_SIZE;
7834 break;
7835 case dw_val_class_lbl_id:
7836 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7838 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7839 size += size_of_uleb128 (AT_index (a));
7841 else
7842 size += DWARF2_ADDR_SIZE;
7843 break;
7844 case dw_val_class_lineptr:
7845 case dw_val_class_macptr:
7846 size += DWARF_OFFSET_SIZE;
7847 break;
7848 case dw_val_class_str:
7849 form = AT_string_form (a);
7850 if (form == DW_FORM_strp)
7851 size += DWARF_OFFSET_SIZE;
7852 else if (form == DW_FORM_GNU_str_index)
7853 size += size_of_uleb128 (AT_index (a));
7854 else
7855 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7856 break;
7857 case dw_val_class_file:
7858 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7859 break;
7860 case dw_val_class_data8:
7861 size += 8;
7862 break;
7863 case dw_val_class_vms_delta:
7864 size += DWARF_OFFSET_SIZE;
7865 break;
7866 case dw_val_class_high_pc:
7867 size += DWARF2_ADDR_SIZE;
7868 break;
7869 default:
7870 gcc_unreachable ();
7874 return size;
7877 /* Size the debugging information associated with a given DIE. Visits the
7878 DIE's children recursively. Updates the global variable next_die_offset, on
7879 each time through. Uses the current value of next_die_offset to update the
7880 die_offset field in each DIE. */
7882 static void
7883 calc_die_sizes (dw_die_ref die)
7885 dw_die_ref c;
7887 gcc_assert (die->die_offset == 0
7888 || (unsigned long int) die->die_offset == next_die_offset);
7889 die->die_offset = next_die_offset;
7890 next_die_offset += size_of_die (die);
7892 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
7894 if (die->die_child != NULL)
7895 /* Count the null byte used to terminate sibling lists. */
7896 next_die_offset += 1;
7899 /* Size just the base type children at the start of the CU.
7900 This is needed because build_abbrev needs to size locs
7901 and sizing of type based stack ops needs to know die_offset
7902 values for the base types. */
7904 static void
7905 calc_base_type_die_sizes (void)
7907 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7908 unsigned int i;
7909 dw_die_ref base_type;
7910 #if ENABLE_ASSERT_CHECKING
7911 dw_die_ref prev = comp_unit_die ()->die_child;
7912 #endif
7914 die_offset += size_of_die (comp_unit_die ());
7915 for (i = 0; base_types.iterate (i, &base_type); i++)
7917 #if ENABLE_ASSERT_CHECKING
7918 gcc_assert (base_type->die_offset == 0
7919 && prev->die_sib == base_type
7920 && base_type->die_child == NULL
7921 && base_type->die_abbrev);
7922 prev = base_type;
7923 #endif
7924 base_type->die_offset = die_offset;
7925 die_offset += size_of_die (base_type);
7929 /* Set the marks for a die and its children. We do this so
7930 that we know whether or not a reference needs to use FORM_ref_addr; only
7931 DIEs in the same CU will be marked. We used to clear out the offset
7932 and use that as the flag, but ran into ordering problems. */
7934 static void
7935 mark_dies (dw_die_ref die)
7937 dw_die_ref c;
7939 gcc_assert (!die->die_mark);
7941 die->die_mark = 1;
7942 FOR_EACH_CHILD (die, c, mark_dies (c));
7945 /* Clear the marks for a die and its children. */
7947 static void
7948 unmark_dies (dw_die_ref die)
7950 dw_die_ref c;
7952 if (! use_debug_types)
7953 gcc_assert (die->die_mark);
7955 die->die_mark = 0;
7956 FOR_EACH_CHILD (die, c, unmark_dies (c));
7959 /* Clear the marks for a die, its children and referred dies. */
7961 static void
7962 unmark_all_dies (dw_die_ref die)
7964 dw_die_ref c;
7965 dw_attr_ref a;
7966 unsigned ix;
7968 if (!die->die_mark)
7969 return;
7970 die->die_mark = 0;
7972 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
7974 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7975 if (AT_class (a) == dw_val_class_die_ref)
7976 unmark_all_dies (AT_ref (a));
7979 /* Calculate if the entry should appear in the final output file. It may be
7980 from a pruned a type. */
7982 static bool
7983 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
7985 /* By limiting gnu pubnames to definitions only, gold can generate a
7986 gdb index without entries for declarations, which don't include
7987 enough information to be useful. */
7988 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
7989 return false;
7991 if (table == pubname_table)
7993 /* Enumerator names are part of the pubname table, but the
7994 parent DW_TAG_enumeration_type die may have been pruned.
7995 Don't output them if that is the case. */
7996 if (p->die->die_tag == DW_TAG_enumerator &&
7997 (p->die->die_parent == NULL
7998 || !p->die->die_parent->die_perennial_p))
7999 return false;
8001 /* Everything else in the pubname table is included. */
8002 return true;
8005 /* The pubtypes table shouldn't include types that have been
8006 pruned. */
8007 return (p->die->die_offset != 0
8008 || !flag_eliminate_unused_debug_types);
8011 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8012 generated for the compilation unit. */
8014 static unsigned long
8015 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8017 unsigned long size;
8018 unsigned i;
8019 pubname_ref p;
8020 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8022 size = DWARF_PUBNAMES_HEADER_SIZE;
8023 FOR_EACH_VEC_ELT (*names, i, p)
8024 if (include_pubname_in_output (names, p))
8025 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8027 size += DWARF_OFFSET_SIZE;
8028 return size;
8031 /* Return the size of the information in the .debug_aranges section. */
8033 static unsigned long
8034 size_of_aranges (void)
8036 unsigned long size;
8038 size = DWARF_ARANGES_HEADER_SIZE;
8040 /* Count the address/length pair for this compilation unit. */
8041 if (text_section_used)
8042 size += 2 * DWARF2_ADDR_SIZE;
8043 if (cold_text_section_used)
8044 size += 2 * DWARF2_ADDR_SIZE;
8045 if (have_multiple_function_sections)
8047 unsigned fde_idx;
8048 dw_fde_ref fde;
8050 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8052 if (DECL_IGNORED_P (fde->decl))
8053 continue;
8054 if (!fde->in_std_section)
8055 size += 2 * DWARF2_ADDR_SIZE;
8056 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8057 size += 2 * DWARF2_ADDR_SIZE;
8061 /* Count the two zero words used to terminated the address range table. */
8062 size += 2 * DWARF2_ADDR_SIZE;
8063 return size;
8066 /* Select the encoding of an attribute value. */
8068 static enum dwarf_form
8069 value_format (dw_attr_ref a)
8071 switch (AT_class (a))
8073 case dw_val_class_addr:
8074 /* Only very few attributes allow DW_FORM_addr. */
8075 switch (a->dw_attr)
8077 case DW_AT_low_pc:
8078 case DW_AT_high_pc:
8079 case DW_AT_entry_pc:
8080 case DW_AT_trampoline:
8081 return (AT_index (a) == NOT_INDEXED
8082 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8083 default:
8084 break;
8086 switch (DWARF2_ADDR_SIZE)
8088 case 1:
8089 return DW_FORM_data1;
8090 case 2:
8091 return DW_FORM_data2;
8092 case 4:
8093 return DW_FORM_data4;
8094 case 8:
8095 return DW_FORM_data8;
8096 default:
8097 gcc_unreachable ();
8099 case dw_val_class_range_list:
8100 case dw_val_class_loc_list:
8101 if (dwarf_version >= 4)
8102 return DW_FORM_sec_offset;
8103 /* FALLTHRU */
8104 case dw_val_class_vms_delta:
8105 case dw_val_class_offset:
8106 switch (DWARF_OFFSET_SIZE)
8108 case 4:
8109 return DW_FORM_data4;
8110 case 8:
8111 return DW_FORM_data8;
8112 default:
8113 gcc_unreachable ();
8115 case dw_val_class_loc:
8116 if (dwarf_version >= 4)
8117 return DW_FORM_exprloc;
8118 switch (constant_size (size_of_locs (AT_loc (a))))
8120 case 1:
8121 return DW_FORM_block1;
8122 case 2:
8123 return DW_FORM_block2;
8124 case 4:
8125 return DW_FORM_block4;
8126 default:
8127 gcc_unreachable ();
8129 case dw_val_class_const:
8130 return DW_FORM_sdata;
8131 case dw_val_class_unsigned_const:
8132 switch (constant_size (AT_unsigned (a)))
8134 case 1:
8135 return DW_FORM_data1;
8136 case 2:
8137 return DW_FORM_data2;
8138 case 4:
8139 /* In DWARF3 DW_AT_data_member_location with
8140 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8141 constant, so we need to use DW_FORM_udata if we need
8142 a large constant. */
8143 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8144 return DW_FORM_udata;
8145 return DW_FORM_data4;
8146 case 8:
8147 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8148 return DW_FORM_udata;
8149 return DW_FORM_data8;
8150 default:
8151 gcc_unreachable ();
8153 case dw_val_class_const_double:
8154 switch (HOST_BITS_PER_WIDE_INT)
8156 case 8:
8157 return DW_FORM_data2;
8158 case 16:
8159 return DW_FORM_data4;
8160 case 32:
8161 return DW_FORM_data8;
8162 case 64:
8163 default:
8164 return DW_FORM_block1;
8166 case dw_val_class_vec:
8167 switch (constant_size (a->dw_attr_val.v.val_vec.length
8168 * a->dw_attr_val.v.val_vec.elt_size))
8170 case 1:
8171 return DW_FORM_block1;
8172 case 2:
8173 return DW_FORM_block2;
8174 case 4:
8175 return DW_FORM_block4;
8176 default:
8177 gcc_unreachable ();
8179 case dw_val_class_flag:
8180 if (dwarf_version >= 4)
8182 /* Currently all add_AT_flag calls pass in 1 as last argument,
8183 so DW_FORM_flag_present can be used. If that ever changes,
8184 we'll need to use DW_FORM_flag and have some optimization
8185 in build_abbrev_table that will change those to
8186 DW_FORM_flag_present if it is set to 1 in all DIEs using
8187 the same abbrev entry. */
8188 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8189 return DW_FORM_flag_present;
8191 return DW_FORM_flag;
8192 case dw_val_class_die_ref:
8193 if (AT_ref_external (a))
8194 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8195 else
8196 return DW_FORM_ref;
8197 case dw_val_class_fde_ref:
8198 return DW_FORM_data;
8199 case dw_val_class_lbl_id:
8200 return (AT_index (a) == NOT_INDEXED
8201 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8202 case dw_val_class_lineptr:
8203 case dw_val_class_macptr:
8204 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8205 case dw_val_class_str:
8206 return AT_string_form (a);
8207 case dw_val_class_file:
8208 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8210 case 1:
8211 return DW_FORM_data1;
8212 case 2:
8213 return DW_FORM_data2;
8214 case 4:
8215 return DW_FORM_data4;
8216 default:
8217 gcc_unreachable ();
8220 case dw_val_class_data8:
8221 return DW_FORM_data8;
8223 case dw_val_class_high_pc:
8224 switch (DWARF2_ADDR_SIZE)
8226 case 1:
8227 return DW_FORM_data1;
8228 case 2:
8229 return DW_FORM_data2;
8230 case 4:
8231 return DW_FORM_data4;
8232 case 8:
8233 return DW_FORM_data8;
8234 default:
8235 gcc_unreachable ();
8238 default:
8239 gcc_unreachable ();
8243 /* Output the encoding of an attribute value. */
8245 static void
8246 output_value_format (dw_attr_ref a)
8248 enum dwarf_form form = value_format (a);
8250 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8253 /* Given a die and id, produce the appropriate abbreviations. */
8255 static void
8256 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8258 unsigned ix;
8259 dw_attr_ref a_attr;
8261 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8262 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8263 dwarf_tag_name (abbrev->die_tag));
8265 if (abbrev->die_child != NULL)
8266 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8267 else
8268 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8270 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8272 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8273 dwarf_attr_name (a_attr->dw_attr));
8274 output_value_format (a_attr);
8277 dw2_asm_output_data (1, 0, NULL);
8278 dw2_asm_output_data (1, 0, NULL);
8282 /* Output the .debug_abbrev section which defines the DIE abbreviation
8283 table. */
8285 static void
8286 output_abbrev_section (void)
8288 unsigned long abbrev_id;
8290 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8291 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8293 /* Terminate the table. */
8294 dw2_asm_output_data (1, 0, NULL);
8297 /* Output a symbol we can use to refer to this DIE from another CU. */
8299 static inline void
8300 output_die_symbol (dw_die_ref die)
8302 const char *sym = die->die_id.die_symbol;
8304 gcc_assert (!die->comdat_type_p);
8306 if (sym == 0)
8307 return;
8309 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8310 /* We make these global, not weak; if the target doesn't support
8311 .linkonce, it doesn't support combining the sections, so debugging
8312 will break. */
8313 targetm.asm_out.globalize_label (asm_out_file, sym);
8315 ASM_OUTPUT_LABEL (asm_out_file, sym);
8318 /* Return a new location list, given the begin and end range, and the
8319 expression. */
8321 static inline dw_loc_list_ref
8322 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8323 const char *section)
8325 dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
8327 retlist->begin = begin;
8328 retlist->begin_entry = NULL;
8329 retlist->end = end;
8330 retlist->expr = expr;
8331 retlist->section = section;
8333 return retlist;
8336 /* Generate a new internal symbol for this location list node, if it
8337 hasn't got one yet. */
8339 static inline void
8340 gen_llsym (dw_loc_list_ref list)
8342 gcc_assert (!list->ll_symbol);
8343 list->ll_symbol = gen_internal_sym ("LLST");
8346 /* Output the location list given to us. */
8348 static void
8349 output_loc_list (dw_loc_list_ref list_head)
8351 dw_loc_list_ref curr = list_head;
8353 if (list_head->emitted)
8354 return;
8355 list_head->emitted = true;
8357 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8359 /* Walk the location list, and output each range + expression. */
8360 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8362 unsigned long size;
8363 /* Don't output an entry that starts and ends at the same address. */
8364 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8365 continue;
8366 size = size_of_locs (curr->expr);
8367 /* If the expression is too large, drop it on the floor. We could
8368 perhaps put it into DW_TAG_dwarf_procedure and refer to that
8369 in the expression, but >= 64KB expressions for a single value
8370 in a single range are unlikely very useful. */
8371 if (size > 0xffff)
8372 continue;
8373 if (dwarf_split_debug_info)
8375 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8376 "Location list start/length entry (%s)",
8377 list_head->ll_symbol);
8378 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8379 "Location list range start index (%s)",
8380 curr->begin);
8381 /* The length field is 4 bytes. If we ever need to support
8382 an 8-byte length, we can add a new DW_LLE code or fall back
8383 to DW_LLE_GNU_start_end_entry. */
8384 dw2_asm_output_delta (4, curr->end, curr->begin,
8385 "Location list range length (%s)",
8386 list_head->ll_symbol);
8388 else if (!have_multiple_function_sections)
8390 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8391 "Location list begin address (%s)",
8392 list_head->ll_symbol);
8393 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8394 "Location list end address (%s)",
8395 list_head->ll_symbol);
8397 else
8399 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8400 "Location list begin address (%s)",
8401 list_head->ll_symbol);
8402 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8403 "Location list end address (%s)",
8404 list_head->ll_symbol);
8407 /* Output the block length for this list of location operations. */
8408 gcc_assert (size <= 0xffff);
8409 dw2_asm_output_data (2, size, "%s", "Location expression size");
8411 output_loc_sequence (curr->expr, -1);
8414 if (dwarf_split_debug_info)
8415 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8416 "Location list terminator (%s)",
8417 list_head->ll_symbol);
8418 else
8420 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8421 "Location list terminator begin (%s)",
8422 list_head->ll_symbol);
8423 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8424 "Location list terminator end (%s)",
8425 list_head->ll_symbol);
8429 /* Output a range_list offset into the debug_range section. Emit a
8430 relocated reference if val_entry is NULL, otherwise, emit an
8431 indirect reference. */
8433 static void
8434 output_range_list_offset (dw_attr_ref a)
8436 const char *name = dwarf_attr_name (a->dw_attr);
8438 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8440 char *p = strchr (ranges_section_label, '\0');
8441 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8442 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8443 debug_ranges_section, "%s", name);
8444 *p = '\0';
8446 else
8447 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8448 "%s (offset from %s)", name, ranges_section_label);
8451 /* Output the offset into the debug_loc section. */
8453 static void
8454 output_loc_list_offset (dw_attr_ref a)
8456 char *sym = AT_loc_list (a)->ll_symbol;
8458 gcc_assert (sym);
8459 if (dwarf_split_debug_info)
8460 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8461 "%s", dwarf_attr_name (a->dw_attr));
8462 else
8463 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8464 "%s", dwarf_attr_name (a->dw_attr));
8467 /* Output an attribute's index or value appropriately. */
8469 static void
8470 output_attr_index_or_value (dw_attr_ref a)
8472 const char *name = dwarf_attr_name (a->dw_attr);
8474 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8476 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8477 return;
8479 switch (AT_class (a))
8481 case dw_val_class_addr:
8482 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8483 break;
8484 case dw_val_class_high_pc:
8485 case dw_val_class_lbl_id:
8486 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8487 break;
8488 case dw_val_class_loc_list:
8489 output_loc_list_offset (a);
8490 break;
8491 default:
8492 gcc_unreachable ();
8496 /* Output a type signature. */
8498 static inline void
8499 output_signature (const char *sig, const char *name)
8501 int i;
8503 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8504 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8507 /* Output the DIE and its attributes. Called recursively to generate
8508 the definitions of each child DIE. */
8510 static void
8511 output_die (dw_die_ref die)
8513 dw_attr_ref a;
8514 dw_die_ref c;
8515 unsigned long size;
8516 unsigned ix;
8518 /* If someone in another CU might refer to us, set up a symbol for
8519 them to point to. */
8520 if (! die->comdat_type_p && die->die_id.die_symbol)
8521 output_die_symbol (die);
8523 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8524 (unsigned long)die->die_offset,
8525 dwarf_tag_name (die->die_tag));
8527 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8529 const char *name = dwarf_attr_name (a->dw_attr);
8531 switch (AT_class (a))
8533 case dw_val_class_addr:
8534 output_attr_index_or_value (a);
8535 break;
8537 case dw_val_class_offset:
8538 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8539 "%s", name);
8540 break;
8542 case dw_val_class_range_list:
8543 output_range_list_offset (a);
8544 break;
8546 case dw_val_class_loc:
8547 size = size_of_locs (AT_loc (a));
8549 /* Output the block length for this list of location operations. */
8550 if (dwarf_version >= 4)
8551 dw2_asm_output_data_uleb128 (size, "%s", name);
8552 else
8553 dw2_asm_output_data (constant_size (size), size, "%s", name);
8555 output_loc_sequence (AT_loc (a), -1);
8556 break;
8558 case dw_val_class_const:
8559 /* ??? It would be slightly more efficient to use a scheme like is
8560 used for unsigned constants below, but gdb 4.x does not sign
8561 extend. Gdb 5.x does sign extend. */
8562 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8563 break;
8565 case dw_val_class_unsigned_const:
8567 int csize = constant_size (AT_unsigned (a));
8568 if (dwarf_version == 3
8569 && a->dw_attr == DW_AT_data_member_location
8570 && csize >= 4)
8571 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8572 else
8573 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8575 break;
8577 case dw_val_class_const_double:
8579 unsigned HOST_WIDE_INT first, second;
8581 if (HOST_BITS_PER_WIDE_INT >= 64)
8582 dw2_asm_output_data (1,
8583 HOST_BITS_PER_DOUBLE_INT
8584 / HOST_BITS_PER_CHAR,
8585 NULL);
8587 if (WORDS_BIG_ENDIAN)
8589 first = a->dw_attr_val.v.val_double.high;
8590 second = a->dw_attr_val.v.val_double.low;
8592 else
8594 first = a->dw_attr_val.v.val_double.low;
8595 second = a->dw_attr_val.v.val_double.high;
8598 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8599 first, "%s", name);
8600 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8601 second, NULL);
8603 break;
8605 case dw_val_class_vec:
8607 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8608 unsigned int len = a->dw_attr_val.v.val_vec.length;
8609 unsigned int i;
8610 unsigned char *p;
8612 dw2_asm_output_data (constant_size (len * elt_size),
8613 len * elt_size, "%s", name);
8614 if (elt_size > sizeof (HOST_WIDE_INT))
8616 elt_size /= 2;
8617 len *= 2;
8619 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8620 i < len;
8621 i++, p += elt_size)
8622 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8623 "fp or vector constant word %u", i);
8624 break;
8627 case dw_val_class_flag:
8628 if (dwarf_version >= 4)
8630 /* Currently all add_AT_flag calls pass in 1 as last argument,
8631 so DW_FORM_flag_present can be used. If that ever changes,
8632 we'll need to use DW_FORM_flag and have some optimization
8633 in build_abbrev_table that will change those to
8634 DW_FORM_flag_present if it is set to 1 in all DIEs using
8635 the same abbrev entry. */
8636 gcc_assert (AT_flag (a) == 1);
8637 if (flag_debug_asm)
8638 fprintf (asm_out_file, "\t\t\t%s %s\n",
8639 ASM_COMMENT_START, name);
8640 break;
8642 dw2_asm_output_data (1, AT_flag (a), "%s", name);
8643 break;
8645 case dw_val_class_loc_list:
8646 output_attr_index_or_value (a);
8647 break;
8649 case dw_val_class_die_ref:
8650 if (AT_ref_external (a))
8652 if (AT_ref (a)->comdat_type_p)
8654 comdat_type_node_ref type_node =
8655 AT_ref (a)->die_id.die_type_node;
8657 gcc_assert (type_node);
8658 output_signature (type_node->signature, name);
8660 else
8662 const char *sym = AT_ref (a)->die_id.die_symbol;
8663 int size;
8665 gcc_assert (sym);
8666 /* In DWARF2, DW_FORM_ref_addr is sized by target address
8667 length, whereas in DWARF3 it's always sized as an
8668 offset. */
8669 if (dwarf_version == 2)
8670 size = DWARF2_ADDR_SIZE;
8671 else
8672 size = DWARF_OFFSET_SIZE;
8673 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8674 name);
8677 else
8679 gcc_assert (AT_ref (a)->die_offset);
8680 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8681 "%s", name);
8683 break;
8685 case dw_val_class_fde_ref:
8687 char l1[20];
8689 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8690 a->dw_attr_val.v.val_fde_index * 2);
8691 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8692 "%s", name);
8694 break;
8696 case dw_val_class_vms_delta:
8697 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
8698 AT_vms_delta2 (a), AT_vms_delta1 (a),
8699 "%s", name);
8700 break;
8702 case dw_val_class_lbl_id:
8703 output_attr_index_or_value (a);
8704 break;
8706 case dw_val_class_lineptr:
8707 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8708 debug_line_section, "%s", name);
8709 break;
8711 case dw_val_class_macptr:
8712 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8713 debug_macinfo_section, "%s", name);
8714 break;
8716 case dw_val_class_str:
8717 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
8718 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8719 a->dw_attr_val.v.val_str->label,
8720 debug_str_section,
8721 "%s: \"%s\"", name, AT_string (a));
8722 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
8723 dw2_asm_output_data_uleb128 (AT_index (a),
8724 "%s: \"%s\"", name, AT_string (a));
8725 else
8726 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8727 break;
8729 case dw_val_class_file:
8731 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8733 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8734 a->dw_attr_val.v.val_file->filename);
8735 break;
8738 case dw_val_class_data8:
8740 int i;
8742 for (i = 0; i < 8; i++)
8743 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
8744 i == 0 ? "%s" : NULL, name);
8745 break;
8748 case dw_val_class_high_pc:
8749 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
8750 get_AT_low_pc (die), "DW_AT_high_pc");
8751 break;
8753 default:
8754 gcc_unreachable ();
8758 FOR_EACH_CHILD (die, c, output_die (c));
8760 /* Add null byte to terminate sibling list. */
8761 if (die->die_child != NULL)
8762 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
8763 (unsigned long) die->die_offset);
8766 /* Output the compilation unit that appears at the beginning of the
8767 .debug_info section, and precedes the DIE descriptions. */
8769 static void
8770 output_compilation_unit_header (void)
8772 int ver = dwarf_version;
8774 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8775 dw2_asm_output_data (4, 0xffffffff,
8776 "Initial length escape value indicating 64-bit DWARF extension");
8777 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8778 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8779 "Length of Compilation Unit Info");
8780 dw2_asm_output_data (2, ver, "DWARF version number");
8781 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8782 debug_abbrev_section,
8783 "Offset Into Abbrev. Section");
8784 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8787 /* Output the compilation unit DIE and its children. */
8789 static void
8790 output_comp_unit (dw_die_ref die, int output_if_empty)
8792 const char *secname, *oldsym;
8793 char *tmp;
8794 external_ref_hash_type extern_map;
8796 /* Unless we are outputting main CU, we may throw away empty ones. */
8797 if (!output_if_empty && die->die_child == NULL)
8798 return;
8800 /* Even if there are no children of this DIE, we must output the information
8801 about the compilation unit. Otherwise, on an empty translation unit, we
8802 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
8803 will then complain when examining the file. First mark all the DIEs in
8804 this CU so we know which get local refs. */
8805 mark_dies (die);
8807 extern_map = optimize_external_refs (die);
8809 build_abbrev_table (die, extern_map);
8811 extern_map.dispose ();
8813 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8814 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8815 calc_die_sizes (die);
8817 oldsym = die->die_id.die_symbol;
8818 if (oldsym)
8820 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8822 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8823 secname = tmp;
8824 die->die_id.die_symbol = NULL;
8825 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8827 else
8829 switch_to_section (debug_info_section);
8830 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
8831 info_section_emitted = true;
8834 /* Output debugging information. */
8835 output_compilation_unit_header ();
8836 output_die (die);
8838 /* Leave the marks on the main CU, so we can check them in
8839 output_pubnames. */
8840 if (oldsym)
8842 unmark_dies (die);
8843 die->die_id.die_symbol = oldsym;
8847 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
8848 and .debug_pubtypes. This is configured per-target, but can be
8849 overridden by the -gpubnames or -gno-pubnames options. */
8851 static inline bool
8852 want_pubnames (void)
8854 if (debug_info_level <= DINFO_LEVEL_TERSE)
8855 return false;
8856 if (debug_generate_pub_sections != -1)
8857 return debug_generate_pub_sections;
8858 return targetm.want_debug_pub_sections;
8861 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
8863 static void
8864 add_AT_pubnames (dw_die_ref die)
8866 if (want_pubnames ())
8867 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
8870 /* Add a string attribute value to a skeleton DIE. */
8872 static inline void
8873 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
8874 const char *str)
8876 dw_attr_node attr;
8877 struct indirect_string_node *node;
8879 if (! skeleton_debug_str_hash)
8880 skeleton_debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
8881 debug_str_eq, NULL);
8883 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
8884 find_string_form (node);
8885 if (node->form == DW_FORM_GNU_str_index)
8886 node->form = DW_FORM_strp;
8888 attr.dw_attr = attr_kind;
8889 attr.dw_attr_val.val_class = dw_val_class_str;
8890 attr.dw_attr_val.val_entry = NULL;
8891 attr.dw_attr_val.v.val_str = node;
8892 add_dwarf_attr (die, &attr);
8895 /* Helper function to generate top-level dies for skeleton debug_info and
8896 debug_types. */
8898 static void
8899 add_top_level_skeleton_die_attrs (dw_die_ref die)
8901 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
8902 const char *comp_dir = comp_dir_string ();
8904 add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
8905 if (comp_dir != NULL)
8906 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
8907 add_AT_pubnames (die);
8908 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
8911 /* Return the single type-unit die for skeleton type units. */
8913 static dw_die_ref
8914 get_skeleton_type_unit (void)
8916 /* For dwarf_split_debug_sections with use_type info, all type units in the
8917 skeleton sections have identical dies (but different headers). This
8918 single die will be output many times. */
8920 static dw_die_ref skeleton_type_unit = NULL;
8922 if (skeleton_type_unit == NULL)
8924 skeleton_type_unit = new_die (DW_TAG_type_unit, NULL, NULL);
8925 add_top_level_skeleton_die_attrs (skeleton_type_unit);
8926 skeleton_type_unit->die_abbrev = SKELETON_TYPE_DIE_ABBREV;
8928 return skeleton_type_unit;
8931 /* Output skeleton debug sections that point to the dwo file. */
8933 static void
8934 output_skeleton_debug_sections (dw_die_ref comp_unit)
8936 /* These attributes will be found in the full debug_info section. */
8937 remove_AT (comp_unit, DW_AT_producer);
8938 remove_AT (comp_unit, DW_AT_language);
8940 switch_to_section (debug_skeleton_info_section);
8941 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
8943 /* Produce the skeleton compilation-unit header. This one differs enough from
8944 a normal CU header that it's better not to call output_compilation_unit
8945 header. */
8946 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8947 dw2_asm_output_data (4, 0xffffffff,
8948 "Initial length escape value indicating 64-bit DWARF extension");
8950 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8951 DWARF_COMPILE_UNIT_HEADER_SIZE
8952 - DWARF_INITIAL_LENGTH_SIZE
8953 + size_of_die (comp_unit),
8954 "Length of Compilation Unit Info");
8955 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
8956 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
8957 debug_abbrev_section,
8958 "Offset Into Abbrev. Section");
8959 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8961 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
8962 output_die (comp_unit);
8964 /* Build the skeleton debug_abbrev section. */
8965 switch_to_section (debug_skeleton_abbrev_section);
8966 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
8968 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
8969 if (use_debug_types)
8970 output_die_abbrevs (SKELETON_TYPE_DIE_ABBREV, get_skeleton_type_unit ());
8972 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
8975 /* Output a comdat type unit DIE and its children. */
8977 static void
8978 output_comdat_type_unit (comdat_type_node *node)
8980 const char *secname;
8981 char *tmp;
8982 int i;
8983 #if defined (OBJECT_FORMAT_ELF)
8984 tree comdat_key;
8985 #endif
8986 external_ref_hash_type extern_map;
8988 /* First mark all the DIEs in this CU so we know which get local refs. */
8989 mark_dies (node->root_die);
8991 extern_map = optimize_external_refs (node->root_die);
8993 build_abbrev_table (node->root_die, extern_map);
8995 extern_map.dispose ();
8997 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8998 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
8999 calc_die_sizes (node->root_die);
9001 #if defined (OBJECT_FORMAT_ELF)
9002 if (!dwarf_split_debug_info)
9003 secname = ".debug_types";
9004 else
9005 secname = ".debug_types.dwo";
9007 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9008 sprintf (tmp, "wt.");
9009 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9010 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9011 comdat_key = get_identifier (tmp);
9012 targetm.asm_out.named_section (secname,
9013 SECTION_DEBUG | SECTION_LINKONCE,
9014 comdat_key);
9015 #else
9016 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9017 sprintf (tmp, ".gnu.linkonce.wt.");
9018 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9019 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9020 secname = tmp;
9021 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9022 #endif
9024 /* Output debugging information. */
9025 output_compilation_unit_header ();
9026 output_signature (node->signature, "Type Signature");
9027 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9028 "Offset to Type DIE");
9029 output_die (node->root_die);
9031 unmark_dies (node->root_die);
9033 #if defined (OBJECT_FORMAT_ELF)
9034 if (dwarf_split_debug_info)
9036 /* Produce the skeleton type-unit header. */
9037 const char *secname = ".debug_types";
9039 targetm.asm_out.named_section (secname,
9040 SECTION_DEBUG | SECTION_LINKONCE,
9041 comdat_key);
9042 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9043 dw2_asm_output_data (4, 0xffffffff,
9044 "Initial length escape value indicating 64-bit DWARF extension");
9046 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9047 DWARF_COMPILE_UNIT_HEADER_SIZE
9048 - DWARF_INITIAL_LENGTH_SIZE
9049 + size_of_die (get_skeleton_type_unit ())
9050 + DWARF_TYPE_SIGNATURE_SIZE + DWARF_OFFSET_SIZE,
9051 "Length of Type Unit Info");
9052 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
9053 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
9054 debug_skeleton_abbrev_section_label,
9055 debug_abbrev_section,
9056 "Offset Into Abbrev. Section");
9057 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9058 output_signature (node->signature, "Type Signature");
9059 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, "Offset to Type DIE");
9061 output_die (get_skeleton_type_unit ());
9063 #endif
9066 /* Return the DWARF2/3 pubname associated with a decl. */
9068 static const char *
9069 dwarf2_name (tree decl, int scope)
9071 if (DECL_NAMELESS (decl))
9072 return NULL;
9073 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9076 /* Add a new entry to .debug_pubnames if appropriate. */
9078 static void
9079 add_pubname_string (const char *str, dw_die_ref die)
9081 pubname_entry e;
9083 e.die = die;
9084 e.name = xstrdup (str);
9085 vec_safe_push (pubname_table, e);
9088 static void
9089 add_pubname (tree decl, dw_die_ref die)
9091 if (!want_pubnames ())
9092 return;
9094 /* Don't add items to the table when we expect that the consumer will have
9095 just read the enclosing die. For example, if the consumer is looking at a
9096 class_member, it will either be inside the class already, or will have just
9097 looked up the class to find the member. Either way, searching the class is
9098 faster than searching the index. */
9099 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9100 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9102 const char *name = dwarf2_name (decl, 1);
9104 if (name)
9105 add_pubname_string (name, die);
9109 /* Add an enumerator to the pubnames section. */
9111 static void
9112 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9114 pubname_entry e;
9116 gcc_assert (scope_name);
9117 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9118 e.die = die;
9119 vec_safe_push (pubname_table, e);
9122 /* Add a new entry to .debug_pubtypes if appropriate. */
9124 static void
9125 add_pubtype (tree decl, dw_die_ref die)
9127 pubname_entry e;
9129 if (!want_pubnames ())
9130 return;
9132 if ((TREE_PUBLIC (decl)
9133 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9134 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9136 tree scope = NULL;
9137 const char *scope_name = "";
9138 const char *sep = is_cxx () ? "::" : ".";
9139 const char *name;
9141 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9142 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9144 scope_name = lang_hooks.dwarf_name (scope, 1);
9145 if (scope_name != NULL && scope_name[0] != '\0')
9146 scope_name = concat (scope_name, sep, NULL);
9147 else
9148 scope_name = "";
9151 if (TYPE_P (decl))
9152 name = type_tag (decl);
9153 else
9154 name = lang_hooks.dwarf_name (decl, 1);
9156 /* If we don't have a name for the type, there's no point in adding
9157 it to the table. */
9158 if (name != NULL && name[0] != '\0')
9160 e.die = die;
9161 e.name = concat (scope_name, name, NULL);
9162 vec_safe_push (pubtype_table, e);
9165 /* Although it might be more consistent to add the pubinfo for the
9166 enumerators as their dies are created, they should only be added if the
9167 enum type meets the criteria above. So rather than re-check the parent
9168 enum type whenever an enumerator die is created, just output them all
9169 here. This isn't protected by the name conditional because anonymous
9170 enums don't have names. */
9171 if (die->die_tag == DW_TAG_enumeration_type)
9173 dw_die_ref c;
9175 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9180 /* Output a single entry in the pubnames table. */
9182 static void
9183 output_pubname (dw_offset die_offset, pubname_entry *entry)
9185 dw_die_ref die = entry->die;
9186 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9188 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9190 if (debug_generate_pub_sections == 2)
9192 /* This logic follows gdb's method for determining the value of the flag
9193 byte. */
9194 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9195 switch (die->die_tag)
9197 case DW_TAG_typedef:
9198 case DW_TAG_base_type:
9199 case DW_TAG_subrange_type:
9200 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9201 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9202 break;
9203 case DW_TAG_enumerator:
9204 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9205 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9206 if (!is_cxx () && !is_java ())
9207 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9208 break;
9209 case DW_TAG_subprogram:
9210 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9211 GDB_INDEX_SYMBOL_KIND_FUNCTION);
9212 if (!is_ada ())
9213 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9214 break;
9215 case DW_TAG_constant:
9216 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9217 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9218 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9219 break;
9220 case DW_TAG_variable:
9221 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9222 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9223 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9224 break;
9225 case DW_TAG_namespace:
9226 case DW_TAG_imported_declaration:
9227 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9228 break;
9229 case DW_TAG_class_type:
9230 case DW_TAG_interface_type:
9231 case DW_TAG_structure_type:
9232 case DW_TAG_union_type:
9233 case DW_TAG_enumeration_type:
9234 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9235 if (!is_cxx () && !is_java ())
9236 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9237 break;
9238 default:
9239 /* An unusual tag. Leave the flag-byte empty. */
9240 break;
9242 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9243 "GDB-index flags");
9246 dw2_asm_output_nstring (entry->name, -1, "external name");
9250 /* Output the public names table used to speed up access to externally
9251 visible names; or the public types table used to find type definitions. */
9253 static void
9254 output_pubnames (vec<pubname_entry, va_gc> *names)
9256 unsigned i;
9257 unsigned long pubnames_length = size_of_pubnames (names);
9258 pubname_ref pub;
9260 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9261 dw2_asm_output_data (4, 0xffffffff,
9262 "Initial length escape value indicating 64-bit DWARF extension");
9263 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length, "Pub Info Length");
9265 /* Version number for pubnames/pubtypes is independent of dwarf version. */
9266 dw2_asm_output_data (2, 2, "DWARF Version");
9268 if (dwarf_split_debug_info)
9269 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9270 debug_skeleton_info_section,
9271 "Offset of Compilation Unit Info");
9272 else
9273 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9274 debug_info_section,
9275 "Offset of Compilation Unit Info");
9276 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9277 "Compilation Unit Length");
9279 FOR_EACH_VEC_ELT (*names, i, pub)
9281 if (include_pubname_in_output (names, pub))
9283 dw_offset die_offset = pub->die->die_offset;
9285 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9286 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9287 gcc_assert (pub->die->die_mark);
9289 /* If we're putting types in their own .debug_types sections,
9290 the .debug_pubtypes table will still point to the compile
9291 unit (not the type unit), so we want to use the offset of
9292 the skeleton DIE (if there is one). */
9293 if (pub->die->comdat_type_p && names == pubtype_table)
9295 comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
9297 if (type_node != NULL)
9298 die_offset = (type_node->skeleton_die != NULL
9299 ? type_node->skeleton_die->die_offset
9300 : comp_unit_die ()->die_offset);
9303 output_pubname (die_offset, pub);
9307 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9310 /* Output public names and types tables if necessary. */
9312 static void
9313 output_pubtables (void)
9315 if (!want_pubnames () || !info_section_emitted)
9316 return;
9318 switch_to_section (debug_pubnames_section);
9319 output_pubnames (pubname_table);
9320 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
9321 It shouldn't hurt to emit it always, since pure DWARF2 consumers
9322 simply won't look for the section. */
9323 switch_to_section (debug_pubtypes_section);
9324 output_pubnames (pubtype_table);
9328 /* Output the information that goes into the .debug_aranges table.
9329 Namely, define the beginning and ending address range of the
9330 text section generated for this compilation unit. */
9332 static void
9333 output_aranges (unsigned long aranges_length)
9335 unsigned i;
9337 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9338 dw2_asm_output_data (4, 0xffffffff,
9339 "Initial length escape value indicating 64-bit DWARF extension");
9340 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9341 "Length of Address Ranges Info");
9342 /* Version number for aranges is still 2, even in DWARF3. */
9343 dw2_asm_output_data (2, 2, "DWARF Version");
9344 if (dwarf_split_debug_info)
9345 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9346 debug_skeleton_info_section,
9347 "Offset of Compilation Unit Info");
9348 else
9349 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9350 debug_info_section,
9351 "Offset of Compilation Unit Info");
9352 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9353 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9355 /* We need to align to twice the pointer size here. */
9356 if (DWARF_ARANGES_PAD_SIZE)
9358 /* Pad using a 2 byte words so that padding is correct for any
9359 pointer size. */
9360 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9361 2 * DWARF2_ADDR_SIZE);
9362 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9363 dw2_asm_output_data (2, 0, NULL);
9366 /* It is necessary not to output these entries if the sections were
9367 not used; if the sections were not used, the length will be 0 and
9368 the address may end up as 0 if the section is discarded by ld
9369 --gc-sections, leaving an invalid (0, 0) entry that can be
9370 confused with the terminator. */
9371 if (text_section_used)
9373 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9374 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9375 text_section_label, "Length");
9377 if (cold_text_section_used)
9379 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9380 "Address");
9381 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9382 cold_text_section_label, "Length");
9385 if (have_multiple_function_sections)
9387 unsigned fde_idx;
9388 dw_fde_ref fde;
9390 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9392 if (DECL_IGNORED_P (fde->decl))
9393 continue;
9394 if (!fde->in_std_section)
9396 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9397 "Address");
9398 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9399 fde->dw_fde_begin, "Length");
9401 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9403 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9404 "Address");
9405 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9406 fde->dw_fde_second_begin, "Length");
9411 /* Output the terminator words. */
9412 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9413 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9416 /* Add a new entry to .debug_ranges. Return the offset at which it
9417 was placed. */
9419 static unsigned int
9420 add_ranges_num (int num)
9422 unsigned int in_use = ranges_table_in_use;
9424 if (in_use == ranges_table_allocated)
9426 ranges_table_allocated += RANGES_TABLE_INCREMENT;
9427 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9428 ranges_table_allocated);
9429 memset (ranges_table + ranges_table_in_use, 0,
9430 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9433 ranges_table[in_use].num = num;
9434 ranges_table_in_use = in_use + 1;
9436 return in_use * 2 * DWARF2_ADDR_SIZE;
9439 /* Add a new entry to .debug_ranges corresponding to a block, or a
9440 range terminator if BLOCK is NULL. */
9442 static unsigned int
9443 add_ranges (const_tree block)
9445 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9448 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9449 When using dwarf_split_debug_info, address attributes in dies destined
9450 for the final executable should be direct references--setting the
9451 parameter force_direct ensures this behavior. */
9453 static void
9454 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9455 bool *added, bool force_direct)
9457 unsigned int in_use = ranges_by_label_in_use;
9458 unsigned int offset;
9460 if (in_use == ranges_by_label_allocated)
9462 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9463 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9464 ranges_by_label,
9465 ranges_by_label_allocated);
9466 memset (ranges_by_label + ranges_by_label_in_use, 0,
9467 RANGES_TABLE_INCREMENT
9468 * sizeof (struct dw_ranges_by_label_struct));
9471 ranges_by_label[in_use].begin = begin;
9472 ranges_by_label[in_use].end = end;
9473 ranges_by_label_in_use = in_use + 1;
9475 offset = add_ranges_num (-(int)in_use - 1);
9476 if (!*added)
9478 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9479 *added = true;
9483 static void
9484 output_ranges (void)
9486 unsigned i;
9487 static const char *const start_fmt = "Offset %#x";
9488 const char *fmt = start_fmt;
9490 for (i = 0; i < ranges_table_in_use; i++)
9492 int block_num = ranges_table[i].num;
9494 if (block_num > 0)
9496 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9497 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9499 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9500 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9502 /* If all code is in the text section, then the compilation
9503 unit base address defaults to DW_AT_low_pc, which is the
9504 base of the text section. */
9505 if (!have_multiple_function_sections)
9507 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9508 text_section_label,
9509 fmt, i * 2 * DWARF2_ADDR_SIZE);
9510 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9511 text_section_label, NULL);
9514 /* Otherwise, the compilation unit base address is zero,
9515 which allows us to use absolute addresses, and not worry
9516 about whether the target supports cross-section
9517 arithmetic. */
9518 else
9520 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9521 fmt, i * 2 * DWARF2_ADDR_SIZE);
9522 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9525 fmt = NULL;
9528 /* Negative block_num stands for an index into ranges_by_label. */
9529 else if (block_num < 0)
9531 int lab_idx = - block_num - 1;
9533 if (!have_multiple_function_sections)
9535 gcc_unreachable ();
9536 #if 0
9537 /* If we ever use add_ranges_by_labels () for a single
9538 function section, all we have to do is to take out
9539 the #if 0 above. */
9540 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9541 ranges_by_label[lab_idx].begin,
9542 text_section_label,
9543 fmt, i * 2 * DWARF2_ADDR_SIZE);
9544 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9545 ranges_by_label[lab_idx].end,
9546 text_section_label, NULL);
9547 #endif
9549 else
9551 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9552 ranges_by_label[lab_idx].begin,
9553 fmt, i * 2 * DWARF2_ADDR_SIZE);
9554 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9555 ranges_by_label[lab_idx].end,
9556 NULL);
9559 else
9561 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9562 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9563 fmt = start_fmt;
9568 /* Data structure containing information about input files. */
9569 struct file_info
9571 const char *path; /* Complete file name. */
9572 const char *fname; /* File name part. */
9573 int length; /* Length of entire string. */
9574 struct dwarf_file_data * file_idx; /* Index in input file table. */
9575 int dir_idx; /* Index in directory table. */
9578 /* Data structure containing information about directories with source
9579 files. */
9580 struct dir_info
9582 const char *path; /* Path including directory name. */
9583 int length; /* Path length. */
9584 int prefix; /* Index of directory entry which is a prefix. */
9585 int count; /* Number of files in this directory. */
9586 int dir_idx; /* Index of directory used as base. */
9589 /* Callback function for file_info comparison. We sort by looking at
9590 the directories in the path. */
9592 static int
9593 file_info_cmp (const void *p1, const void *p2)
9595 const struct file_info *const s1 = (const struct file_info *) p1;
9596 const struct file_info *const s2 = (const struct file_info *) p2;
9597 const unsigned char *cp1;
9598 const unsigned char *cp2;
9600 /* Take care of file names without directories. We need to make sure that
9601 we return consistent values to qsort since some will get confused if
9602 we return the same value when identical operands are passed in opposite
9603 orders. So if neither has a directory, return 0 and otherwise return
9604 1 or -1 depending on which one has the directory. */
9605 if ((s1->path == s1->fname || s2->path == s2->fname))
9606 return (s2->path == s2->fname) - (s1->path == s1->fname);
9608 cp1 = (const unsigned char *) s1->path;
9609 cp2 = (const unsigned char *) s2->path;
9611 while (1)
9613 ++cp1;
9614 ++cp2;
9615 /* Reached the end of the first path? If so, handle like above. */
9616 if ((cp1 == (const unsigned char *) s1->fname)
9617 || (cp2 == (const unsigned char *) s2->fname))
9618 return ((cp2 == (const unsigned char *) s2->fname)
9619 - (cp1 == (const unsigned char *) s1->fname));
9621 /* Character of current path component the same? */
9622 else if (*cp1 != *cp2)
9623 return *cp1 - *cp2;
9627 struct file_name_acquire_data
9629 struct file_info *files;
9630 int used_files;
9631 int max_files;
9634 /* Traversal function for the hash table. */
9636 static int
9637 file_name_acquire (void ** slot, void *data)
9639 struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
9640 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
9641 struct file_info *fi;
9642 const char *f;
9644 gcc_assert (fnad->max_files >= d->emitted_number);
9646 if (! d->emitted_number)
9647 return 1;
9649 gcc_assert (fnad->max_files != fnad->used_files);
9651 fi = fnad->files + fnad->used_files++;
9653 /* Skip all leading "./". */
9654 f = d->filename;
9655 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9656 f += 2;
9658 /* Create a new array entry. */
9659 fi->path = f;
9660 fi->length = strlen (f);
9661 fi->file_idx = d;
9663 /* Search for the file name part. */
9664 f = strrchr (f, DIR_SEPARATOR);
9665 #if defined (DIR_SEPARATOR_2)
9667 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9669 if (g != NULL)
9671 if (f == NULL || f < g)
9672 f = g;
9675 #endif
9677 fi->fname = f == NULL ? fi->path : f + 1;
9678 return 1;
9681 /* Output the directory table and the file name table. We try to minimize
9682 the total amount of memory needed. A heuristic is used to avoid large
9683 slowdowns with many input files. */
9685 static void
9686 output_file_names (void)
9688 struct file_name_acquire_data fnad;
9689 int numfiles;
9690 struct file_info *files;
9691 struct dir_info *dirs;
9692 int *saved;
9693 int *savehere;
9694 int *backmap;
9695 int ndirs;
9696 int idx_offset;
9697 int i;
9699 if (!last_emitted_file)
9701 dw2_asm_output_data (1, 0, "End directory table");
9702 dw2_asm_output_data (1, 0, "End file name table");
9703 return;
9706 numfiles = last_emitted_file->emitted_number;
9708 /* Allocate the various arrays we need. */
9709 files = XALLOCAVEC (struct file_info, numfiles);
9710 dirs = XALLOCAVEC (struct dir_info, numfiles);
9712 fnad.files = files;
9713 fnad.used_files = 0;
9714 fnad.max_files = numfiles;
9715 htab_traverse (file_table, file_name_acquire, &fnad);
9716 gcc_assert (fnad.used_files == fnad.max_files);
9718 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9720 /* Find all the different directories used. */
9721 dirs[0].path = files[0].path;
9722 dirs[0].length = files[0].fname - files[0].path;
9723 dirs[0].prefix = -1;
9724 dirs[0].count = 1;
9725 dirs[0].dir_idx = 0;
9726 files[0].dir_idx = 0;
9727 ndirs = 1;
9729 for (i = 1; i < numfiles; i++)
9730 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9731 && memcmp (dirs[ndirs - 1].path, files[i].path,
9732 dirs[ndirs - 1].length) == 0)
9734 /* Same directory as last entry. */
9735 files[i].dir_idx = ndirs - 1;
9736 ++dirs[ndirs - 1].count;
9738 else
9740 int j;
9742 /* This is a new directory. */
9743 dirs[ndirs].path = files[i].path;
9744 dirs[ndirs].length = files[i].fname - files[i].path;
9745 dirs[ndirs].count = 1;
9746 dirs[ndirs].dir_idx = ndirs;
9747 files[i].dir_idx = ndirs;
9749 /* Search for a prefix. */
9750 dirs[ndirs].prefix = -1;
9751 for (j = 0; j < ndirs; j++)
9752 if (dirs[j].length < dirs[ndirs].length
9753 && dirs[j].length > 1
9754 && (dirs[ndirs].prefix == -1
9755 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9756 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9757 dirs[ndirs].prefix = j;
9759 ++ndirs;
9762 /* Now to the actual work. We have to find a subset of the directories which
9763 allow expressing the file name using references to the directory table
9764 with the least amount of characters. We do not do an exhaustive search
9765 where we would have to check out every combination of every single
9766 possible prefix. Instead we use a heuristic which provides nearly optimal
9767 results in most cases and never is much off. */
9768 saved = XALLOCAVEC (int, ndirs);
9769 savehere = XALLOCAVEC (int, ndirs);
9771 memset (saved, '\0', ndirs * sizeof (saved[0]));
9772 for (i = 0; i < ndirs; i++)
9774 int j;
9775 int total;
9777 /* We can always save some space for the current directory. But this
9778 does not mean it will be enough to justify adding the directory. */
9779 savehere[i] = dirs[i].length;
9780 total = (savehere[i] - saved[i]) * dirs[i].count;
9782 for (j = i + 1; j < ndirs; j++)
9784 savehere[j] = 0;
9785 if (saved[j] < dirs[i].length)
9787 /* Determine whether the dirs[i] path is a prefix of the
9788 dirs[j] path. */
9789 int k;
9791 k = dirs[j].prefix;
9792 while (k != -1 && k != (int) i)
9793 k = dirs[k].prefix;
9795 if (k == (int) i)
9797 /* Yes it is. We can possibly save some memory by
9798 writing the filenames in dirs[j] relative to
9799 dirs[i]. */
9800 savehere[j] = dirs[i].length;
9801 total += (savehere[j] - saved[j]) * dirs[j].count;
9806 /* Check whether we can save enough to justify adding the dirs[i]
9807 directory. */
9808 if (total > dirs[i].length + 1)
9810 /* It's worthwhile adding. */
9811 for (j = i; j < ndirs; j++)
9812 if (savehere[j] > 0)
9814 /* Remember how much we saved for this directory so far. */
9815 saved[j] = savehere[j];
9817 /* Remember the prefix directory. */
9818 dirs[j].dir_idx = i;
9823 /* Emit the directory name table. */
9824 idx_offset = dirs[0].length > 0 ? 1 : 0;
9825 for (i = 1 - idx_offset; i < ndirs; i++)
9826 dw2_asm_output_nstring (dirs[i].path,
9827 dirs[i].length
9828 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9829 "Directory Entry: %#x", i + idx_offset);
9831 dw2_asm_output_data (1, 0, "End directory table");
9833 /* We have to emit them in the order of emitted_number since that's
9834 used in the debug info generation. To do this efficiently we
9835 generate a back-mapping of the indices first. */
9836 backmap = XALLOCAVEC (int, numfiles);
9837 for (i = 0; i < numfiles; i++)
9838 backmap[files[i].file_idx->emitted_number - 1] = i;
9840 /* Now write all the file names. */
9841 for (i = 0; i < numfiles; i++)
9843 int file_idx = backmap[i];
9844 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9846 #ifdef VMS_DEBUGGING_INFO
9847 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
9849 /* Setting these fields can lead to debugger miscomparisons,
9850 but VMS Debug requires them to be set correctly. */
9852 int ver;
9853 long long cdt;
9854 long siz;
9855 int maxfilelen = strlen (files[file_idx].path)
9856 + dirs[dir_idx].length
9857 + MAX_VMS_VERSION_LEN + 1;
9858 char *filebuf = XALLOCAVEC (char, maxfilelen);
9860 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
9861 snprintf (filebuf, maxfilelen, "%s;%d",
9862 files[file_idx].path + dirs[dir_idx].length, ver);
9864 dw2_asm_output_nstring
9865 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
9867 /* Include directory index. */
9868 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9870 /* Modification time. */
9871 dw2_asm_output_data_uleb128
9872 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
9873 ? cdt : 0,
9874 NULL);
9876 /* File length in bytes. */
9877 dw2_asm_output_data_uleb128
9878 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
9879 ? siz : 0,
9880 NULL);
9881 #else
9882 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
9883 "File Entry: %#x", (unsigned) i + 1);
9885 /* Include directory index. */
9886 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9888 /* Modification time. */
9889 dw2_asm_output_data_uleb128 (0, NULL);
9891 /* File length in bytes. */
9892 dw2_asm_output_data_uleb128 (0, NULL);
9893 #endif /* VMS_DEBUGGING_INFO */
9896 dw2_asm_output_data (1, 0, "End file name table");
9900 /* Output one line number table into the .debug_line section. */
9902 static void
9903 output_one_line_info_table (dw_line_info_table *table)
9905 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9906 unsigned int current_line = 1;
9907 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
9908 dw_line_info_entry *ent;
9909 size_t i;
9911 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
9913 switch (ent->opcode)
9915 case LI_set_address:
9916 /* ??? Unfortunately, we have little choice here currently, and
9917 must always use the most general form. GCC does not know the
9918 address delta itself, so we can't use DW_LNS_advance_pc. Many
9919 ports do have length attributes which will give an upper bound
9920 on the address range. We could perhaps use length attributes
9921 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
9922 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
9924 /* This can handle any delta. This takes
9925 4+DWARF2_ADDR_SIZE bytes. */
9926 dw2_asm_output_data (1, 0, "set address %s", line_label);
9927 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9928 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9929 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9930 break;
9932 case LI_set_line:
9933 if (ent->val == current_line)
9935 /* We still need to start a new row, so output a copy insn. */
9936 dw2_asm_output_data (1, DW_LNS_copy,
9937 "copy line %u", current_line);
9939 else
9941 int line_offset = ent->val - current_line;
9942 int line_delta = line_offset - DWARF_LINE_BASE;
9944 current_line = ent->val;
9945 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9947 /* This can handle deltas from -10 to 234, using the current
9948 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
9949 This takes 1 byte. */
9950 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9951 "line %u", current_line);
9953 else
9955 /* This can handle any delta. This takes at least 4 bytes,
9956 depending on the value being encoded. */
9957 dw2_asm_output_data (1, DW_LNS_advance_line,
9958 "advance to line %u", current_line);
9959 dw2_asm_output_data_sleb128 (line_offset, NULL);
9960 dw2_asm_output_data (1, DW_LNS_copy, NULL);
9963 break;
9965 case LI_set_file:
9966 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
9967 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9968 break;
9970 case LI_set_column:
9971 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
9972 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9973 break;
9975 case LI_negate_stmt:
9976 current_is_stmt = !current_is_stmt;
9977 dw2_asm_output_data (1, DW_LNS_negate_stmt,
9978 "is_stmt %d", current_is_stmt);
9979 break;
9981 case LI_set_prologue_end:
9982 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
9983 "set prologue end");
9984 break;
9986 case LI_set_epilogue_begin:
9987 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
9988 "set epilogue begin");
9989 break;
9991 case LI_set_discriminator:
9992 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
9993 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
9994 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
9995 dw2_asm_output_data_uleb128 (ent->val, NULL);
9996 break;
10000 /* Emit debug info for the address of the end of the table. */
10001 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
10002 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10003 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10004 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
10006 dw2_asm_output_data (1, 0, "end sequence");
10007 dw2_asm_output_data_uleb128 (1, NULL);
10008 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10011 /* Output the source line number correspondence information. This
10012 information goes into the .debug_line section. */
10014 static void
10015 output_line_info (bool prologue_only)
10017 char l1[20], l2[20], p1[20], p2[20];
10018 int ver = dwarf_version;
10019 bool saw_one = false;
10020 int opc;
10022 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
10023 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
10024 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
10025 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
10027 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10028 dw2_asm_output_data (4, 0xffffffff,
10029 "Initial length escape value indicating 64-bit DWARF extension");
10030 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10031 "Length of Source Line Info");
10032 ASM_OUTPUT_LABEL (asm_out_file, l1);
10034 dw2_asm_output_data (2, ver, "DWARF Version");
10035 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10036 ASM_OUTPUT_LABEL (asm_out_file, p1);
10038 /* Define the architecture-dependent minimum instruction length (in bytes).
10039 In this implementation of DWARF, this field is used for information
10040 purposes only. Since GCC generates assembly language, we have no
10041 a priori knowledge of how many instruction bytes are generated for each
10042 source line, and therefore can use only the DW_LNE_set_address and
10043 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
10044 this as '1', which is "correct enough" for all architectures,
10045 and don't let the target override. */
10046 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10048 if (ver >= 4)
10049 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10050 "Maximum Operations Per Instruction");
10051 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10052 "Default is_stmt_start flag");
10053 dw2_asm_output_data (1, DWARF_LINE_BASE,
10054 "Line Base Value (Special Opcodes)");
10055 dw2_asm_output_data (1, DWARF_LINE_RANGE,
10056 "Line Range Value (Special Opcodes)");
10057 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10058 "Special Opcode Base");
10060 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10062 int n_op_args;
10063 switch (opc)
10065 case DW_LNS_advance_pc:
10066 case DW_LNS_advance_line:
10067 case DW_LNS_set_file:
10068 case DW_LNS_set_column:
10069 case DW_LNS_fixed_advance_pc:
10070 case DW_LNS_set_isa:
10071 n_op_args = 1;
10072 break;
10073 default:
10074 n_op_args = 0;
10075 break;
10078 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10079 opc, n_op_args);
10082 /* Write out the information about the files we use. */
10083 output_file_names ();
10084 ASM_OUTPUT_LABEL (asm_out_file, p2);
10085 if (prologue_only)
10087 /* Output the marker for the end of the line number info. */
10088 ASM_OUTPUT_LABEL (asm_out_file, l2);
10089 return;
10092 if (separate_line_info)
10094 dw_line_info_table *table;
10095 size_t i;
10097 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10098 if (table->in_use)
10100 output_one_line_info_table (table);
10101 saw_one = true;
10104 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10106 output_one_line_info_table (cold_text_section_line_info);
10107 saw_one = true;
10110 /* ??? Some Darwin linkers crash on a .debug_line section with no
10111 sequences. Further, merely a DW_LNE_end_sequence entry is not
10112 sufficient -- the address column must also be initialized.
10113 Make sure to output at least one set_address/end_sequence pair,
10114 choosing .text since that section is always present. */
10115 if (text_section_line_info->in_use || !saw_one)
10116 output_one_line_info_table (text_section_line_info);
10118 /* Output the marker for the end of the line number info. */
10119 ASM_OUTPUT_LABEL (asm_out_file, l2);
10122 /* Given a pointer to a tree node for some base type, return a pointer to
10123 a DIE that describes the given type.
10125 This routine must only be called for GCC type nodes that correspond to
10126 Dwarf base (fundamental) types. */
10128 static dw_die_ref
10129 base_type_die (tree type)
10131 dw_die_ref base_type_result;
10132 enum dwarf_type encoding;
10134 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10135 return 0;
10137 /* If this is a subtype that should not be emitted as a subrange type,
10138 use the base type. See subrange_type_for_debug_p. */
10139 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10140 type = TREE_TYPE (type);
10142 switch (TREE_CODE (type))
10144 case INTEGER_TYPE:
10145 if ((dwarf_version >= 4 || !dwarf_strict)
10146 && TYPE_NAME (type)
10147 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10148 && DECL_IS_BUILTIN (TYPE_NAME (type))
10149 && DECL_NAME (TYPE_NAME (type)))
10151 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10152 if (strcmp (name, "char16_t") == 0
10153 || strcmp (name, "char32_t") == 0)
10155 encoding = DW_ATE_UTF;
10156 break;
10159 if (TYPE_STRING_FLAG (type))
10161 if (TYPE_UNSIGNED (type))
10162 encoding = DW_ATE_unsigned_char;
10163 else
10164 encoding = DW_ATE_signed_char;
10166 else if (TYPE_UNSIGNED (type))
10167 encoding = DW_ATE_unsigned;
10168 else
10169 encoding = DW_ATE_signed;
10170 break;
10172 case REAL_TYPE:
10173 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10175 if (dwarf_version >= 3 || !dwarf_strict)
10176 encoding = DW_ATE_decimal_float;
10177 else
10178 encoding = DW_ATE_lo_user;
10180 else
10181 encoding = DW_ATE_float;
10182 break;
10184 case FIXED_POINT_TYPE:
10185 if (!(dwarf_version >= 3 || !dwarf_strict))
10186 encoding = DW_ATE_lo_user;
10187 else if (TYPE_UNSIGNED (type))
10188 encoding = DW_ATE_unsigned_fixed;
10189 else
10190 encoding = DW_ATE_signed_fixed;
10191 break;
10193 /* Dwarf2 doesn't know anything about complex ints, so use
10194 a user defined type for it. */
10195 case COMPLEX_TYPE:
10196 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10197 encoding = DW_ATE_complex_float;
10198 else
10199 encoding = DW_ATE_lo_user;
10200 break;
10202 case BOOLEAN_TYPE:
10203 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
10204 encoding = DW_ATE_boolean;
10205 break;
10207 default:
10208 /* No other TREE_CODEs are Dwarf fundamental types. */
10209 gcc_unreachable ();
10212 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10214 add_AT_unsigned (base_type_result, DW_AT_byte_size,
10215 int_size_in_bytes (type));
10216 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10217 add_pubtype (type, base_type_result);
10219 return base_type_result;
10222 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
10223 named 'auto' in its type: return true for it, false otherwise. */
10225 static inline bool
10226 is_cxx_auto (tree type)
10228 if (is_cxx ())
10230 tree name = TYPE_NAME (type);
10231 if (TREE_CODE (name) == TYPE_DECL)
10232 name = DECL_NAME (name);
10233 if (name == get_identifier ("auto"))
10234 return true;
10236 return false;
10239 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10240 given input type is a Dwarf "fundamental" type. Otherwise return null. */
10242 static inline int
10243 is_base_type (tree type)
10245 switch (TREE_CODE (type))
10247 case ERROR_MARK:
10248 case VOID_TYPE:
10249 case INTEGER_TYPE:
10250 case REAL_TYPE:
10251 case FIXED_POINT_TYPE:
10252 case COMPLEX_TYPE:
10253 case BOOLEAN_TYPE:
10254 return 1;
10256 case ARRAY_TYPE:
10257 case RECORD_TYPE:
10258 case UNION_TYPE:
10259 case QUAL_UNION_TYPE:
10260 case ENUMERAL_TYPE:
10261 case FUNCTION_TYPE:
10262 case METHOD_TYPE:
10263 case POINTER_TYPE:
10264 case REFERENCE_TYPE:
10265 case NULLPTR_TYPE:
10266 case OFFSET_TYPE:
10267 case LANG_TYPE:
10268 case VECTOR_TYPE:
10269 return 0;
10271 default:
10272 if (is_cxx_auto (type))
10273 return 0;
10274 gcc_unreachable ();
10277 return 0;
10280 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10281 node, return the size in bits for the type if it is a constant, or else
10282 return the alignment for the type if the type's size is not constant, or
10283 else return BITS_PER_WORD if the type actually turns out to be an
10284 ERROR_MARK node. */
10286 static inline unsigned HOST_WIDE_INT
10287 simple_type_size_in_bits (const_tree type)
10289 if (TREE_CODE (type) == ERROR_MARK)
10290 return BITS_PER_WORD;
10291 else if (TYPE_SIZE (type) == NULL_TREE)
10292 return 0;
10293 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
10294 return tree_to_uhwi (TYPE_SIZE (type));
10295 else
10296 return TYPE_ALIGN (type);
10299 /* Similarly, but return a double_int instead of UHWI. */
10301 static inline double_int
10302 double_int_type_size_in_bits (const_tree type)
10304 if (TREE_CODE (type) == ERROR_MARK)
10305 return double_int::from_uhwi (BITS_PER_WORD);
10306 else if (TYPE_SIZE (type) == NULL_TREE)
10307 return double_int_zero;
10308 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10309 return tree_to_double_int (TYPE_SIZE (type));
10310 else
10311 return double_int::from_uhwi (TYPE_ALIGN (type));
10314 /* Given a pointer to a tree node for a subrange type, return a pointer
10315 to a DIE that describes the given type. */
10317 static dw_die_ref
10318 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10320 dw_die_ref subrange_die;
10321 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10323 if (context_die == NULL)
10324 context_die = comp_unit_die ();
10326 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10328 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10330 /* The size of the subrange type and its base type do not match,
10331 so we need to generate a size attribute for the subrange type. */
10332 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10335 if (low)
10336 add_bound_info (subrange_die, DW_AT_lower_bound, low);
10337 if (high)
10338 add_bound_info (subrange_die, DW_AT_upper_bound, high);
10340 return subrange_die;
10343 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10344 entry that chains various modifiers in front of the given type. */
10346 static dw_die_ref
10347 modified_type_die (tree type, int is_const_type, int is_volatile_type,
10348 dw_die_ref context_die)
10350 enum tree_code code = TREE_CODE (type);
10351 dw_die_ref mod_type_die;
10352 dw_die_ref sub_die = NULL;
10353 tree item_type = NULL;
10354 tree qualified_type;
10355 tree name, low, high;
10356 dw_die_ref mod_scope;
10358 if (code == ERROR_MARK)
10359 return NULL;
10361 /* See if we already have the appropriately qualified variant of
10362 this type. */
10363 qualified_type
10364 = get_qualified_type (type,
10365 ((is_const_type ? TYPE_QUAL_CONST : 0)
10366 | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
10368 if (qualified_type == sizetype
10369 && TYPE_NAME (qualified_type)
10370 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10372 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10374 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10375 && TYPE_PRECISION (t)
10376 == TYPE_PRECISION (qualified_type)
10377 && TYPE_UNSIGNED (t)
10378 == TYPE_UNSIGNED (qualified_type));
10379 qualified_type = t;
10382 /* If we do, then we can just use its DIE, if it exists. */
10383 if (qualified_type)
10385 mod_type_die = lookup_type_die (qualified_type);
10386 if (mod_type_die)
10387 return mod_type_die;
10390 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10392 /* Handle C typedef types. */
10393 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10394 && !DECL_ARTIFICIAL (name))
10396 tree dtype = TREE_TYPE (name);
10398 if (qualified_type == dtype)
10400 /* For a named type, use the typedef. */
10401 gen_type_die (qualified_type, context_die);
10402 return lookup_type_die (qualified_type);
10404 else if (is_const_type < TYPE_READONLY (dtype)
10405 || is_volatile_type < TYPE_VOLATILE (dtype)
10406 || (is_const_type <= TYPE_READONLY (dtype)
10407 && is_volatile_type <= TYPE_VOLATILE (dtype)
10408 && DECL_ORIGINAL_TYPE (name) != type))
10409 /* cv-unqualified version of named type. Just use the unnamed
10410 type to which it refers. */
10411 return modified_type_die (DECL_ORIGINAL_TYPE (name),
10412 is_const_type, is_volatile_type,
10413 context_die);
10414 /* Else cv-qualified version of named type; fall through. */
10417 mod_scope = scope_die_for (type, context_die);
10419 if (is_const_type
10420 /* If both is_const_type and is_volatile_type, prefer the path
10421 which leads to a qualified type. */
10422 && (!is_volatile_type
10423 || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
10424 || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
10426 mod_type_die = new_die (DW_TAG_const_type, mod_scope, type);
10427 sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
10429 else if (is_volatile_type)
10431 mod_type_die = new_die (DW_TAG_volatile_type, mod_scope, type);
10432 sub_die = modified_type_die (type, is_const_type, 0, context_die);
10434 else if (code == POINTER_TYPE)
10436 mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10437 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10438 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10439 item_type = TREE_TYPE (type);
10440 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10441 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10442 TYPE_ADDR_SPACE (item_type));
10444 else if (code == REFERENCE_TYPE)
10446 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10447 mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10448 type);
10449 else
10450 mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10451 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10452 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10453 item_type = TREE_TYPE (type);
10454 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10455 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10456 TYPE_ADDR_SPACE (item_type));
10458 else if (code == INTEGER_TYPE
10459 && TREE_TYPE (type) != NULL_TREE
10460 && subrange_type_for_debug_p (type, &low, &high))
10462 mod_type_die = subrange_type_die (type, low, high, context_die);
10463 item_type = TREE_TYPE (type);
10465 else if (is_base_type (type))
10466 mod_type_die = base_type_die (type);
10467 else
10469 gen_type_die (type, context_die);
10471 /* We have to get the type_main_variant here (and pass that to the
10472 `lookup_type_die' routine) because the ..._TYPE node we have
10473 might simply be a *copy* of some original type node (where the
10474 copy was created to help us keep track of typedef names) and
10475 that copy might have a different TYPE_UID from the original
10476 ..._TYPE node. */
10477 if (TREE_CODE (type) != VECTOR_TYPE)
10478 return lookup_type_die (type_main_variant (type));
10479 else
10480 /* Vectors have the debugging information in the type,
10481 not the main variant. */
10482 return lookup_type_die (type);
10485 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
10486 don't output a DW_TAG_typedef, since there isn't one in the
10487 user's program; just attach a DW_AT_name to the type.
10488 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10489 if the base type already has the same name. */
10490 if (name
10491 && ((TREE_CODE (name) != TYPE_DECL
10492 && (qualified_type == TYPE_MAIN_VARIANT (type)
10493 || (!is_const_type && !is_volatile_type)))
10494 || (TREE_CODE (name) == TYPE_DECL
10495 && TREE_TYPE (name) == qualified_type
10496 && DECL_NAME (name))))
10498 if (TREE_CODE (name) == TYPE_DECL)
10499 /* Could just call add_name_and_src_coords_attributes here,
10500 but since this is a builtin type it doesn't have any
10501 useful source coordinates anyway. */
10502 name = DECL_NAME (name);
10503 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10505 /* This probably indicates a bug. */
10506 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10508 name = TYPE_NAME (type);
10509 if (name
10510 && TREE_CODE (name) == TYPE_DECL)
10511 name = DECL_NAME (name);
10512 add_name_attribute (mod_type_die,
10513 name ? IDENTIFIER_POINTER (name) : "__unknown__");
10516 if (qualified_type)
10517 equate_type_number_to_die (qualified_type, mod_type_die);
10519 if (item_type)
10520 /* We must do this after the equate_type_number_to_die call, in case
10521 this is a recursive type. This ensures that the modified_type_die
10522 recursion will terminate even if the type is recursive. Recursive
10523 types are possible in Ada. */
10524 sub_die = modified_type_die (item_type,
10525 TYPE_READONLY (item_type),
10526 TYPE_VOLATILE (item_type),
10527 context_die);
10529 if (sub_die != NULL)
10530 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10532 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10533 if (TYPE_ARTIFICIAL (type))
10534 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10536 return mod_type_die;
10539 /* Generate DIEs for the generic parameters of T.
10540 T must be either a generic type or a generic function.
10541 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
10543 static void
10544 gen_generic_params_dies (tree t)
10546 tree parms, args;
10547 int parms_num, i;
10548 dw_die_ref die = NULL;
10549 int non_default;
10551 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10552 return;
10554 if (TYPE_P (t))
10555 die = lookup_type_die (t);
10556 else if (DECL_P (t))
10557 die = lookup_decl_die (t);
10559 gcc_assert (die);
10561 parms = lang_hooks.get_innermost_generic_parms (t);
10562 if (!parms)
10563 /* T has no generic parameter. It means T is neither a generic type
10564 or function. End of story. */
10565 return;
10567 parms_num = TREE_VEC_LENGTH (parms);
10568 args = lang_hooks.get_innermost_generic_args (t);
10569 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
10570 non_default = int_cst_value (TREE_CHAIN (args));
10571 else
10572 non_default = TREE_VEC_LENGTH (args);
10573 for (i = 0; i < parms_num; i++)
10575 tree parm, arg, arg_pack_elems;
10576 dw_die_ref parm_die;
10578 parm = TREE_VEC_ELT (parms, i);
10579 arg = TREE_VEC_ELT (args, i);
10580 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
10581 gcc_assert (parm && TREE_VALUE (parm) && arg);
10583 if (parm && TREE_VALUE (parm) && arg)
10585 /* If PARM represents a template parameter pack,
10586 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
10587 by DW_TAG_template_*_parameter DIEs for the argument
10588 pack elements of ARG. Note that ARG would then be
10589 an argument pack. */
10590 if (arg_pack_elems)
10591 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
10592 arg_pack_elems,
10593 die);
10594 else
10595 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
10596 true /* emit name */, die);
10597 if (i >= non_default)
10598 add_AT_flag (parm_die, DW_AT_default_value, 1);
10603 /* Create and return a DIE for PARM which should be
10604 the representation of a generic type parameter.
10605 For instance, in the C++ front end, PARM would be a template parameter.
10606 ARG is the argument to PARM.
10607 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
10608 name of the PARM.
10609 PARENT_DIE is the parent DIE which the new created DIE should be added to,
10610 as a child node. */
10612 static dw_die_ref
10613 generic_parameter_die (tree parm, tree arg,
10614 bool emit_name_p,
10615 dw_die_ref parent_die)
10617 dw_die_ref tmpl_die = NULL;
10618 const char *name = NULL;
10620 if (!parm || !DECL_NAME (parm) || !arg)
10621 return NULL;
10623 /* We support non-type generic parameters and arguments,
10624 type generic parameters and arguments, as well as
10625 generic generic parameters (a.k.a. template template parameters in C++)
10626 and arguments. */
10627 if (TREE_CODE (parm) == PARM_DECL)
10628 /* PARM is a nontype generic parameter */
10629 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10630 else if (TREE_CODE (parm) == TYPE_DECL)
10631 /* PARM is a type generic parameter. */
10632 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10633 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10634 /* PARM is a generic generic parameter.
10635 Its DIE is a GNU extension. It shall have a
10636 DW_AT_name attribute to represent the name of the template template
10637 parameter, and a DW_AT_GNU_template_name attribute to represent the
10638 name of the template template argument. */
10639 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10640 parent_die, parm);
10641 else
10642 gcc_unreachable ();
10644 if (tmpl_die)
10646 tree tmpl_type;
10648 /* If PARM is a generic parameter pack, it means we are
10649 emitting debug info for a template argument pack element.
10650 In other terms, ARG is a template argument pack element.
10651 In that case, we don't emit any DW_AT_name attribute for
10652 the die. */
10653 if (emit_name_p)
10655 name = IDENTIFIER_POINTER (DECL_NAME (parm));
10656 gcc_assert (name);
10657 add_AT_string (tmpl_die, DW_AT_name, name);
10660 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10662 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10663 TMPL_DIE should have a child DW_AT_type attribute that is set
10664 to the type of the argument to PARM, which is ARG.
10665 If PARM is a type generic parameter, TMPL_DIE should have a
10666 child DW_AT_type that is set to ARG. */
10667 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10668 add_type_attribute (tmpl_die, tmpl_type, 0,
10669 TREE_THIS_VOLATILE (tmpl_type),
10670 parent_die);
10672 else
10674 /* So TMPL_DIE is a DIE representing a
10675 a generic generic template parameter, a.k.a template template
10676 parameter in C++ and arg is a template. */
10678 /* The DW_AT_GNU_template_name attribute of the DIE must be set
10679 to the name of the argument. */
10680 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
10681 if (name)
10682 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
10685 if (TREE_CODE (parm) == PARM_DECL)
10686 /* So PARM is a non-type generic parameter.
10687 DWARF3 5.6.8 says we must set a DW_AT_const_value child
10688 attribute of TMPL_DIE which value represents the value
10689 of ARG.
10690 We must be careful here:
10691 The value of ARG might reference some function decls.
10692 We might currently be emitting debug info for a generic
10693 type and types are emitted before function decls, we don't
10694 know if the function decls referenced by ARG will actually be
10695 emitted after cgraph computations.
10696 So must defer the generation of the DW_AT_const_value to
10697 after cgraph is ready. */
10698 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
10701 return tmpl_die;
10704 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
10705 PARM_PACK must be a template parameter pack. The returned DIE
10706 will be child DIE of PARENT_DIE. */
10708 static dw_die_ref
10709 template_parameter_pack_die (tree parm_pack,
10710 tree parm_pack_args,
10711 dw_die_ref parent_die)
10713 dw_die_ref die;
10714 int j;
10716 gcc_assert (parent_die && parm_pack);
10718 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
10719 add_name_and_src_coords_attributes (die, parm_pack);
10720 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
10721 generic_parameter_die (parm_pack,
10722 TREE_VEC_ELT (parm_pack_args, j),
10723 false /* Don't emit DW_AT_name */,
10724 die);
10725 return die;
10728 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
10729 an enumerated type. */
10731 static inline int
10732 type_is_enum (const_tree type)
10734 return TREE_CODE (type) == ENUMERAL_TYPE;
10737 /* Return the DBX register number described by a given RTL node. */
10739 static unsigned int
10740 dbx_reg_number (const_rtx rtl)
10742 unsigned regno = REGNO (rtl);
10744 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10746 #ifdef LEAF_REG_REMAP
10747 if (crtl->uses_only_leaf_regs)
10749 int leaf_reg = LEAF_REG_REMAP (regno);
10750 if (leaf_reg != -1)
10751 regno = (unsigned) leaf_reg;
10753 #endif
10755 regno = DBX_REGISTER_NUMBER (regno);
10756 gcc_assert (regno != INVALID_REGNUM);
10757 return regno;
10760 /* Optionally add a DW_OP_piece term to a location description expression.
10761 DW_OP_piece is only added if the location description expression already
10762 doesn't end with DW_OP_piece. */
10764 static void
10765 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10767 dw_loc_descr_ref loc;
10769 if (*list_head != NULL)
10771 /* Find the end of the chain. */
10772 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10775 if (loc->dw_loc_opc != DW_OP_piece)
10776 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10780 /* Return a location descriptor that designates a machine register or
10781 zero if there is none. */
10783 static dw_loc_descr_ref
10784 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10786 rtx regs;
10788 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10789 return 0;
10791 /* We only use "frame base" when we're sure we're talking about the
10792 post-prologue local stack frame. We do this by *not* running
10793 register elimination until this point, and recognizing the special
10794 argument pointer and soft frame pointer rtx's.
10795 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
10796 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
10797 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
10799 dw_loc_descr_ref result = NULL;
10801 if (dwarf_version >= 4 || !dwarf_strict)
10803 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
10804 initialized);
10805 if (result)
10806 add_loc_descr (&result,
10807 new_loc_descr (DW_OP_stack_value, 0, 0));
10809 return result;
10812 regs = targetm.dwarf_register_span (rtl);
10814 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
10815 return multiple_reg_loc_descriptor (rtl, regs, initialized);
10816 else
10818 unsigned int dbx_regnum = dbx_reg_number (rtl);
10819 if (dbx_regnum == IGNORED_DWARF_REGNUM)
10820 return 0;
10821 return one_reg_loc_descriptor (dbx_regnum, initialized);
10825 /* Return a location descriptor that designates a machine register for
10826 a given hard register number. */
10828 static dw_loc_descr_ref
10829 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
10831 dw_loc_descr_ref reg_loc_descr;
10833 if (regno <= 31)
10834 reg_loc_descr
10835 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
10836 else
10837 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
10839 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10840 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10842 return reg_loc_descr;
10845 /* Given an RTL of a register, return a location descriptor that
10846 designates a value that spans more than one register. */
10848 static dw_loc_descr_ref
10849 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
10850 enum var_init_status initialized)
10852 int size, i;
10853 dw_loc_descr_ref loc_result = NULL;
10855 /* Simple, contiguous registers. */
10856 if (regs == NULL_RTX)
10858 unsigned reg = REGNO (rtl);
10859 int nregs;
10861 #ifdef LEAF_REG_REMAP
10862 if (crtl->uses_only_leaf_regs)
10864 int leaf_reg = LEAF_REG_REMAP (reg);
10865 if (leaf_reg != -1)
10866 reg = (unsigned) leaf_reg;
10868 #endif
10870 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
10871 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
10873 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
10875 loc_result = NULL;
10876 while (nregs--)
10878 dw_loc_descr_ref t;
10880 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
10881 VAR_INIT_STATUS_INITIALIZED);
10882 add_loc_descr (&loc_result, t);
10883 add_loc_descr_op_piece (&loc_result, size);
10884 ++reg;
10886 return loc_result;
10889 /* Now onto stupid register sets in non contiguous locations. */
10891 gcc_assert (GET_CODE (regs) == PARALLEL);
10893 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10894 loc_result = NULL;
10896 for (i = 0; i < XVECLEN (regs, 0); ++i)
10898 dw_loc_descr_ref t;
10900 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
10901 VAR_INIT_STATUS_INITIALIZED);
10902 add_loc_descr (&loc_result, t);
10903 add_loc_descr_op_piece (&loc_result, size);
10906 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10907 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10908 return loc_result;
10911 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
10913 /* Return a location descriptor that designates a constant i,
10914 as a compound operation from constant (i >> shift), constant shift
10915 and DW_OP_shl. */
10917 static dw_loc_descr_ref
10918 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10920 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
10921 add_loc_descr (&ret, int_loc_descriptor (shift));
10922 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
10923 return ret;
10926 /* Return a location descriptor that designates a constant. */
10928 static dw_loc_descr_ref
10929 int_loc_descriptor (HOST_WIDE_INT i)
10931 enum dwarf_location_atom op;
10933 /* Pick the smallest representation of a constant, rather than just
10934 defaulting to the LEB encoding. */
10935 if (i >= 0)
10937 int clz = clz_hwi (i);
10938 int ctz = ctz_hwi (i);
10939 if (i <= 31)
10940 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
10941 else if (i <= 0xff)
10942 op = DW_OP_const1u;
10943 else if (i <= 0xffff)
10944 op = DW_OP_const2u;
10945 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10946 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10947 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
10948 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
10949 while DW_OP_const4u is 5 bytes. */
10950 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
10951 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10952 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10953 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
10954 while DW_OP_const4u is 5 bytes. */
10955 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10956 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10957 op = DW_OP_const4u;
10958 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10959 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10960 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
10961 while DW_OP_constu of constant >= 0x100000000 takes at least
10962 6 bytes. */
10963 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10964 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10965 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
10966 >= HOST_BITS_PER_WIDE_INT)
10967 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
10968 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
10969 while DW_OP_constu takes in this case at least 6 bytes. */
10970 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
10971 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10972 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10973 && size_of_uleb128 (i) > 6)
10974 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
10975 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
10976 else
10977 op = DW_OP_constu;
10979 else
10981 if (i >= -0x80)
10982 op = DW_OP_const1s;
10983 else if (i >= -0x8000)
10984 op = DW_OP_const2s;
10985 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10987 if (size_of_int_loc_descriptor (i) < 5)
10989 dw_loc_descr_ref ret = int_loc_descriptor (-i);
10990 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10991 return ret;
10993 op = DW_OP_const4s;
10995 else
10997 if (size_of_int_loc_descriptor (i)
10998 < (unsigned long) 1 + size_of_sleb128 (i))
11000 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11001 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11002 return ret;
11004 op = DW_OP_consts;
11008 return new_loc_descr (op, i, 0);
11011 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
11012 without actually allocating it. */
11014 static unsigned long
11015 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11017 return size_of_int_loc_descriptor (i >> shift)
11018 + size_of_int_loc_descriptor (shift)
11019 + 1;
11022 /* Return size_of_locs (int_loc_descriptor (i)) without
11023 actually allocating it. */
11025 static unsigned long
11026 size_of_int_loc_descriptor (HOST_WIDE_INT i)
11028 unsigned long s;
11030 if (i >= 0)
11032 int clz, ctz;
11033 if (i <= 31)
11034 return 1;
11035 else if (i <= 0xff)
11036 return 2;
11037 else if (i <= 0xffff)
11038 return 3;
11039 clz = clz_hwi (i);
11040 ctz = ctz_hwi (i);
11041 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11042 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11043 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11044 - clz - 5);
11045 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11046 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11047 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11048 - clz - 8);
11049 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11050 return 5;
11051 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
11052 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11053 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11054 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11055 - clz - 8);
11056 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11057 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
11058 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11059 - clz - 16);
11060 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11061 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11062 && s > 6)
11063 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11064 - clz - 32);
11065 else
11066 return 1 + s;
11068 else
11070 if (i >= -0x80)
11071 return 2;
11072 else if (i >= -0x8000)
11073 return 3;
11074 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11076 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11078 s = size_of_int_loc_descriptor (-i) + 1;
11079 if (s < 5)
11080 return s;
11082 return 5;
11084 else
11086 unsigned long r = 1 + size_of_sleb128 (i);
11087 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11089 s = size_of_int_loc_descriptor (-i) + 1;
11090 if (s < r)
11091 return s;
11093 return r;
11098 /* Return loc description representing "address" of integer value.
11099 This can appear only as toplevel expression. */
11101 static dw_loc_descr_ref
11102 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
11104 int litsize;
11105 dw_loc_descr_ref loc_result = NULL;
11107 if (!(dwarf_version >= 4 || !dwarf_strict))
11108 return NULL;
11110 litsize = size_of_int_loc_descriptor (i);
11111 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
11112 is more compact. For DW_OP_stack_value we need:
11113 litsize + 1 (DW_OP_stack_value)
11114 and for DW_OP_implicit_value:
11115 1 (DW_OP_implicit_value) + 1 (length) + size. */
11116 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
11118 loc_result = int_loc_descriptor (i);
11119 add_loc_descr (&loc_result,
11120 new_loc_descr (DW_OP_stack_value, 0, 0));
11121 return loc_result;
11124 loc_result = new_loc_descr (DW_OP_implicit_value,
11125 size, 0);
11126 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11127 loc_result->dw_loc_oprnd2.v.val_int = i;
11128 return loc_result;
11131 /* Return a location descriptor that designates a base+offset location. */
11133 static dw_loc_descr_ref
11134 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
11135 enum var_init_status initialized)
11137 unsigned int regno;
11138 dw_loc_descr_ref result;
11139 dw_fde_ref fde = cfun->fde;
11141 /* We only use "frame base" when we're sure we're talking about the
11142 post-prologue local stack frame. We do this by *not* running
11143 register elimination until this point, and recognizing the special
11144 argument pointer and soft frame pointer rtx's. */
11145 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
11147 rtx elim = (ira_use_lra_p
11148 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
11149 : eliminate_regs (reg, VOIDmode, NULL_RTX));
11151 if (elim != reg)
11153 if (GET_CODE (elim) == PLUS)
11155 offset += INTVAL (XEXP (elim, 1));
11156 elim = XEXP (elim, 0);
11158 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11159 && (elim == hard_frame_pointer_rtx
11160 || elim == stack_pointer_rtx))
11161 || elim == (frame_pointer_needed
11162 ? hard_frame_pointer_rtx
11163 : stack_pointer_rtx));
11165 /* If drap register is used to align stack, use frame
11166 pointer + offset to access stack variables. If stack
11167 is aligned without drap, use stack pointer + offset to
11168 access stack variables. */
11169 if (crtl->stack_realign_tried
11170 && reg == frame_pointer_rtx)
11172 int base_reg
11173 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
11174 ? HARD_FRAME_POINTER_REGNUM
11175 : REGNO (elim));
11176 return new_reg_loc_descr (base_reg, offset);
11179 gcc_assert (frame_pointer_fb_offset_valid);
11180 offset += frame_pointer_fb_offset;
11181 return new_loc_descr (DW_OP_fbreg, offset, 0);
11185 regno = REGNO (reg);
11186 #ifdef LEAF_REG_REMAP
11187 if (crtl->uses_only_leaf_regs)
11189 int leaf_reg = LEAF_REG_REMAP (regno);
11190 if (leaf_reg != -1)
11191 regno = (unsigned) leaf_reg;
11193 #endif
11194 regno = DWARF_FRAME_REGNUM (regno);
11196 if (!optimize && fde
11197 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
11199 /* Use cfa+offset to represent the location of arguments passed
11200 on the stack when drap is used to align stack.
11201 Only do this when not optimizing, for optimized code var-tracking
11202 is supposed to track where the arguments live and the register
11203 used as vdrap or drap in some spot might be used for something
11204 else in other part of the routine. */
11205 return new_loc_descr (DW_OP_fbreg, offset, 0);
11208 if (regno <= 31)
11209 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
11210 offset, 0);
11211 else
11212 result = new_loc_descr (DW_OP_bregx, regno, offset);
11214 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11215 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11217 return result;
11220 /* Return true if this RTL expression describes a base+offset calculation. */
11222 static inline int
11223 is_based_loc (const_rtx rtl)
11225 return (GET_CODE (rtl) == PLUS
11226 && ((REG_P (XEXP (rtl, 0))
11227 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11228 && CONST_INT_P (XEXP (rtl, 1)))));
11231 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11232 failed. */
11234 static dw_loc_descr_ref
11235 tls_mem_loc_descriptor (rtx mem)
11237 tree base;
11238 dw_loc_descr_ref loc_result;
11240 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
11241 return NULL;
11243 base = get_base_address (MEM_EXPR (mem));
11244 if (base == NULL
11245 || TREE_CODE (base) != VAR_DECL
11246 || !DECL_THREAD_LOCAL_P (base))
11247 return NULL;
11249 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
11250 if (loc_result == NULL)
11251 return NULL;
11253 if (MEM_OFFSET (mem))
11254 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
11256 return loc_result;
11259 /* Output debug info about reason why we failed to expand expression as dwarf
11260 expression. */
11262 static void
11263 expansion_failed (tree expr, rtx rtl, char const *reason)
11265 if (dump_file && (dump_flags & TDF_DETAILS))
11267 fprintf (dump_file, "Failed to expand as dwarf: ");
11268 if (expr)
11269 print_generic_expr (dump_file, expr, dump_flags);
11270 if (rtl)
11272 fprintf (dump_file, "\n");
11273 print_rtl (dump_file, rtl);
11275 fprintf (dump_file, "\nReason: %s\n", reason);
11279 /* Helper function for const_ok_for_output, called either directly
11280 or via for_each_rtx. */
11282 static int
11283 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
11285 rtx rtl = *rtlp;
11287 if (GET_CODE (rtl) == UNSPEC)
11289 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11290 we can't express it in the debug info. */
11291 #ifdef ENABLE_CHECKING
11292 /* Don't complain about TLS UNSPECs, those are just too hard to
11293 delegitimize. Note this could be a non-decl SYMBOL_REF such as
11294 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11295 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
11296 if (XVECLEN (rtl, 0) == 0
11297 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11298 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
11299 inform (current_function_decl
11300 ? DECL_SOURCE_LOCATION (current_function_decl)
11301 : UNKNOWN_LOCATION,
11302 #if NUM_UNSPEC_VALUES > 0
11303 "non-delegitimized UNSPEC %s (%d) found in variable location",
11304 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11305 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11306 XINT (rtl, 1));
11307 #else
11308 "non-delegitimized UNSPEC %d found in variable location",
11309 XINT (rtl, 1));
11310 #endif
11311 #endif
11312 expansion_failed (NULL_TREE, rtl,
11313 "UNSPEC hasn't been delegitimized.\n");
11314 return 1;
11317 if (targetm.const_not_ok_for_debug_p (rtl))
11319 expansion_failed (NULL_TREE, rtl,
11320 "Expression rejected for debug by the backend.\n");
11321 return 1;
11324 if (GET_CODE (rtl) != SYMBOL_REF)
11325 return 0;
11327 if (CONSTANT_POOL_ADDRESS_P (rtl))
11329 bool marked;
11330 get_pool_constant_mark (rtl, &marked);
11331 /* If all references to this pool constant were optimized away,
11332 it was not output and thus we can't represent it. */
11333 if (!marked)
11335 expansion_failed (NULL_TREE, rtl,
11336 "Constant was removed from constant pool.\n");
11337 return 1;
11341 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11342 return 1;
11344 /* Avoid references to external symbols in debug info, on several targets
11345 the linker might even refuse to link when linking a shared library,
11346 and in many other cases the relocations for .debug_info/.debug_loc are
11347 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
11348 to be defined within the same shared library or executable are fine. */
11349 if (SYMBOL_REF_EXTERNAL_P (rtl))
11351 tree decl = SYMBOL_REF_DECL (rtl);
11353 if (decl == NULL || !targetm.binds_local_p (decl))
11355 expansion_failed (NULL_TREE, rtl,
11356 "Symbol not defined in current TU.\n");
11357 return 1;
11361 return 0;
11364 /* Return true if constant RTL can be emitted in DW_OP_addr or
11365 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
11366 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
11368 static bool
11369 const_ok_for_output (rtx rtl)
11371 if (GET_CODE (rtl) == SYMBOL_REF)
11372 return const_ok_for_output_1 (&rtl, NULL) == 0;
11374 if (GET_CODE (rtl) == CONST)
11375 return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
11377 return true;
11380 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11381 if possible, NULL otherwise. */
11383 static dw_die_ref
11384 base_type_for_mode (enum machine_mode mode, bool unsignedp)
11386 dw_die_ref type_die;
11387 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11389 if (type == NULL)
11390 return NULL;
11391 switch (TREE_CODE (type))
11393 case INTEGER_TYPE:
11394 case REAL_TYPE:
11395 break;
11396 default:
11397 return NULL;
11399 type_die = lookup_type_die (type);
11400 if (!type_die)
11401 type_die = modified_type_die (type, false, false, comp_unit_die ());
11402 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11403 return NULL;
11404 return type_die;
11407 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11408 type matching MODE, or, if MODE is narrower than or as wide as
11409 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
11410 possible. */
11412 static dw_loc_descr_ref
11413 convert_descriptor_to_mode (enum machine_mode mode, dw_loc_descr_ref op)
11415 enum machine_mode outer_mode = mode;
11416 dw_die_ref type_die;
11417 dw_loc_descr_ref cvt;
11419 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11421 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11422 return op;
11424 type_die = base_type_for_mode (outer_mode, 1);
11425 if (type_die == NULL)
11426 return NULL;
11427 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11428 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11429 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11430 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11431 add_loc_descr (&op, cvt);
11432 return op;
11435 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
11437 static dw_loc_descr_ref
11438 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11439 dw_loc_descr_ref op1)
11441 dw_loc_descr_ref ret = op0;
11442 add_loc_descr (&ret, op1);
11443 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11444 if (STORE_FLAG_VALUE != 1)
11446 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11447 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11449 return ret;
11452 /* Return location descriptor for signed comparison OP RTL. */
11454 static dw_loc_descr_ref
11455 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11456 enum machine_mode mem_mode)
11458 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11459 dw_loc_descr_ref op0, op1;
11460 int shift;
11462 if (op_mode == VOIDmode)
11463 op_mode = GET_MODE (XEXP (rtl, 1));
11464 if (op_mode == VOIDmode)
11465 return NULL;
11467 if (dwarf_strict
11468 && (GET_MODE_CLASS (op_mode) != MODE_INT
11469 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11470 return NULL;
11472 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11473 VAR_INIT_STATUS_INITIALIZED);
11474 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11475 VAR_INIT_STATUS_INITIALIZED);
11477 if (op0 == NULL || op1 == NULL)
11478 return NULL;
11480 if (GET_MODE_CLASS (op_mode) != MODE_INT
11481 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11482 return compare_loc_descriptor (op, op0, op1);
11484 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11486 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11487 dw_loc_descr_ref cvt;
11489 if (type_die == NULL)
11490 return NULL;
11491 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11492 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11493 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11494 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11495 add_loc_descr (&op0, cvt);
11496 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11497 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11498 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11499 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11500 add_loc_descr (&op1, cvt);
11501 return compare_loc_descriptor (op, op0, op1);
11504 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11505 /* For eq/ne, if the operands are known to be zero-extended,
11506 there is no need to do the fancy shifting up. */
11507 if (op == DW_OP_eq || op == DW_OP_ne)
11509 dw_loc_descr_ref last0, last1;
11510 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11512 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11514 /* deref_size zero extends, and for constants we can check
11515 whether they are zero extended or not. */
11516 if (((last0->dw_loc_opc == DW_OP_deref_size
11517 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11518 || (CONST_INT_P (XEXP (rtl, 0))
11519 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11520 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11521 && ((last1->dw_loc_opc == DW_OP_deref_size
11522 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11523 || (CONST_INT_P (XEXP (rtl, 1))
11524 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11525 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11526 return compare_loc_descriptor (op, op0, op1);
11528 /* EQ/NE comparison against constant in narrower type than
11529 DWARF2_ADDR_SIZE can be performed either as
11530 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11531 DW_OP_{eq,ne}
11533 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11534 DW_OP_{eq,ne}. Pick whatever is shorter. */
11535 if (CONST_INT_P (XEXP (rtl, 1))
11536 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11537 && (size_of_int_loc_descriptor (shift) + 1
11538 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11539 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11540 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11541 & GET_MODE_MASK (op_mode))))
11543 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
11544 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11545 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11546 & GET_MODE_MASK (op_mode));
11547 return compare_loc_descriptor (op, op0, op1);
11550 add_loc_descr (&op0, int_loc_descriptor (shift));
11551 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11552 if (CONST_INT_P (XEXP (rtl, 1)))
11553 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11554 else
11556 add_loc_descr (&op1, int_loc_descriptor (shift));
11557 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11559 return compare_loc_descriptor (op, op0, op1);
11562 /* Return location descriptor for unsigned comparison OP RTL. */
11564 static dw_loc_descr_ref
11565 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11566 enum machine_mode mem_mode)
11568 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11569 dw_loc_descr_ref op0, op1;
11571 if (op_mode == VOIDmode)
11572 op_mode = GET_MODE (XEXP (rtl, 1));
11573 if (op_mode == VOIDmode)
11574 return NULL;
11575 if (GET_MODE_CLASS (op_mode) != MODE_INT)
11576 return NULL;
11578 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11579 return NULL;
11581 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11582 VAR_INIT_STATUS_INITIALIZED);
11583 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11584 VAR_INIT_STATUS_INITIALIZED);
11586 if (op0 == NULL || op1 == NULL)
11587 return NULL;
11589 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
11591 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
11592 dw_loc_descr_ref last0, last1;
11593 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11595 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11597 if (CONST_INT_P (XEXP (rtl, 0)))
11598 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
11599 /* deref_size zero extends, so no need to mask it again. */
11600 else if (last0->dw_loc_opc != DW_OP_deref_size
11601 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11603 add_loc_descr (&op0, int_loc_descriptor (mask));
11604 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11606 if (CONST_INT_P (XEXP (rtl, 1)))
11607 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11608 /* deref_size zero extends, so no need to mask it again. */
11609 else if (last1->dw_loc_opc != DW_OP_deref_size
11610 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11612 add_loc_descr (&op1, int_loc_descriptor (mask));
11613 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11616 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11618 HOST_WIDE_INT bias = 1;
11619 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11620 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11621 if (CONST_INT_P (XEXP (rtl, 1)))
11622 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
11623 + INTVAL (XEXP (rtl, 1)));
11624 else
11625 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
11626 bias, 0));
11628 return compare_loc_descriptor (op, op0, op1);
11631 /* Return location descriptor for {U,S}{MIN,MAX}. */
11633 static dw_loc_descr_ref
11634 minmax_loc_descriptor (rtx rtl, enum machine_mode mode,
11635 enum machine_mode mem_mode)
11637 enum dwarf_location_atom op;
11638 dw_loc_descr_ref op0, op1, ret;
11639 dw_loc_descr_ref bra_node, drop_node;
11641 if (dwarf_strict
11642 && (GET_MODE_CLASS (mode) != MODE_INT
11643 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
11644 return NULL;
11646 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11647 VAR_INIT_STATUS_INITIALIZED);
11648 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11649 VAR_INIT_STATUS_INITIALIZED);
11651 if (op0 == NULL || op1 == NULL)
11652 return NULL;
11654 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
11655 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
11656 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
11657 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
11659 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11661 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11662 add_loc_descr (&op0, int_loc_descriptor (mask));
11663 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11664 add_loc_descr (&op1, int_loc_descriptor (mask));
11665 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11667 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11669 HOST_WIDE_INT bias = 1;
11670 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11671 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11672 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11675 else if (GET_MODE_CLASS (mode) == MODE_INT
11676 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11678 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
11679 add_loc_descr (&op0, int_loc_descriptor (shift));
11680 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11681 add_loc_descr (&op1, int_loc_descriptor (shift));
11682 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11684 else if (GET_MODE_CLASS (mode) == MODE_INT
11685 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11687 dw_die_ref type_die = base_type_for_mode (mode, 0);
11688 dw_loc_descr_ref cvt;
11689 if (type_die == NULL)
11690 return NULL;
11691 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11692 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11693 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11694 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11695 add_loc_descr (&op0, cvt);
11696 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11697 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11698 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11699 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11700 add_loc_descr (&op1, cvt);
11703 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
11704 op = DW_OP_lt;
11705 else
11706 op = DW_OP_gt;
11707 ret = op0;
11708 add_loc_descr (&ret, op1);
11709 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11710 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11711 add_loc_descr (&ret, bra_node);
11712 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11713 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11714 add_loc_descr (&ret, drop_node);
11715 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11716 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11717 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
11718 && GET_MODE_CLASS (mode) == MODE_INT
11719 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11720 ret = convert_descriptor_to_mode (mode, ret);
11721 return ret;
11724 /* Helper function for mem_loc_descriptor. Perform OP binary op,
11725 but after converting arguments to type_die, afterwards
11726 convert back to unsigned. */
11728 static dw_loc_descr_ref
11729 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
11730 enum machine_mode mode, enum machine_mode mem_mode)
11732 dw_loc_descr_ref cvt, op0, op1;
11734 if (type_die == NULL)
11735 return NULL;
11736 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11737 VAR_INIT_STATUS_INITIALIZED);
11738 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11739 VAR_INIT_STATUS_INITIALIZED);
11740 if (op0 == NULL || op1 == NULL)
11741 return NULL;
11742 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11743 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11744 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11745 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11746 add_loc_descr (&op0, cvt);
11747 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11748 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11749 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11750 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11751 add_loc_descr (&op1, cvt);
11752 add_loc_descr (&op0, op1);
11753 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
11754 return convert_descriptor_to_mode (mode, op0);
11757 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
11758 const0 is DW_OP_lit0 or corresponding typed constant,
11759 const1 is DW_OP_lit1 or corresponding typed constant
11760 and constMSB is constant with just the MSB bit set
11761 for the mode):
11762 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11763 L1: const0 DW_OP_swap
11764 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
11765 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11766 L3: DW_OP_drop
11767 L4: DW_OP_nop
11769 CTZ is similar:
11770 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11771 L1: const0 DW_OP_swap
11772 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11773 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11774 L3: DW_OP_drop
11775 L4: DW_OP_nop
11777 FFS is similar:
11778 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
11779 L1: const1 DW_OP_swap
11780 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11781 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11782 L3: DW_OP_drop
11783 L4: DW_OP_nop */
11785 static dw_loc_descr_ref
11786 clz_loc_descriptor (rtx rtl, enum machine_mode mode,
11787 enum machine_mode mem_mode)
11789 dw_loc_descr_ref op0, ret, tmp;
11790 HOST_WIDE_INT valv;
11791 dw_loc_descr_ref l1jump, l1label;
11792 dw_loc_descr_ref l2jump, l2label;
11793 dw_loc_descr_ref l3jump, l3label;
11794 dw_loc_descr_ref l4jump, l4label;
11795 rtx msb;
11797 if (GET_MODE_CLASS (mode) != MODE_INT
11798 || GET_MODE (XEXP (rtl, 0)) != mode
11799 || (GET_CODE (rtl) == CLZ
11800 && GET_MODE_BITSIZE (mode) > HOST_BITS_PER_DOUBLE_INT))
11801 return NULL;
11803 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11804 VAR_INIT_STATUS_INITIALIZED);
11805 if (op0 == NULL)
11806 return NULL;
11807 ret = op0;
11808 if (GET_CODE (rtl) == CLZ)
11810 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11811 valv = GET_MODE_BITSIZE (mode);
11813 else if (GET_CODE (rtl) == FFS)
11814 valv = 0;
11815 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11816 valv = GET_MODE_BITSIZE (mode);
11817 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11818 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
11819 add_loc_descr (&ret, l1jump);
11820 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11821 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
11822 VAR_INIT_STATUS_INITIALIZED);
11823 if (tmp == NULL)
11824 return NULL;
11825 add_loc_descr (&ret, tmp);
11826 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
11827 add_loc_descr (&ret, l4jump);
11828 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
11829 ? const1_rtx : const0_rtx,
11830 mode, mem_mode,
11831 VAR_INIT_STATUS_INITIALIZED);
11832 if (l1label == NULL)
11833 return NULL;
11834 add_loc_descr (&ret, l1label);
11835 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11836 l2label = new_loc_descr (DW_OP_dup, 0, 0);
11837 add_loc_descr (&ret, l2label);
11838 if (GET_CODE (rtl) != CLZ)
11839 msb = const1_rtx;
11840 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11841 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
11842 << (GET_MODE_BITSIZE (mode) - 1));
11843 else
11844 msb = immed_double_const (0, (unsigned HOST_WIDE_INT) 1
11845 << (GET_MODE_BITSIZE (mode)
11846 - HOST_BITS_PER_WIDE_INT - 1), mode);
11847 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
11848 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11849 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
11850 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
11851 else
11852 tmp = mem_loc_descriptor (msb, mode, mem_mode,
11853 VAR_INIT_STATUS_INITIALIZED);
11854 if (tmp == NULL)
11855 return NULL;
11856 add_loc_descr (&ret, tmp);
11857 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11858 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
11859 add_loc_descr (&ret, l3jump);
11860 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11861 VAR_INIT_STATUS_INITIALIZED);
11862 if (tmp == NULL)
11863 return NULL;
11864 add_loc_descr (&ret, tmp);
11865 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
11866 ? DW_OP_shl : DW_OP_shr, 0, 0));
11867 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11868 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
11869 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11870 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
11871 add_loc_descr (&ret, l2jump);
11872 l3label = new_loc_descr (DW_OP_drop, 0, 0);
11873 add_loc_descr (&ret, l3label);
11874 l4label = new_loc_descr (DW_OP_nop, 0, 0);
11875 add_loc_descr (&ret, l4label);
11876 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11877 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11878 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11879 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11880 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11881 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
11882 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11883 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
11884 return ret;
11887 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
11888 const1 is DW_OP_lit1 or corresponding typed constant):
11889 const0 DW_OP_swap
11890 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11891 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11892 L2: DW_OP_drop
11894 PARITY is similar:
11895 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11896 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11897 L2: DW_OP_drop */
11899 static dw_loc_descr_ref
11900 popcount_loc_descriptor (rtx rtl, enum machine_mode mode,
11901 enum machine_mode mem_mode)
11903 dw_loc_descr_ref op0, ret, tmp;
11904 dw_loc_descr_ref l1jump, l1label;
11905 dw_loc_descr_ref l2jump, l2label;
11907 if (GET_MODE_CLASS (mode) != MODE_INT
11908 || GET_MODE (XEXP (rtl, 0)) != mode)
11909 return NULL;
11911 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11912 VAR_INIT_STATUS_INITIALIZED);
11913 if (op0 == NULL)
11914 return NULL;
11915 ret = op0;
11916 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11917 VAR_INIT_STATUS_INITIALIZED);
11918 if (tmp == NULL)
11919 return NULL;
11920 add_loc_descr (&ret, tmp);
11921 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11922 l1label = new_loc_descr (DW_OP_dup, 0, 0);
11923 add_loc_descr (&ret, l1label);
11924 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11925 add_loc_descr (&ret, l2jump);
11926 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11927 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11928 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11929 VAR_INIT_STATUS_INITIALIZED);
11930 if (tmp == NULL)
11931 return NULL;
11932 add_loc_descr (&ret, tmp);
11933 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11934 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
11935 ? DW_OP_plus : DW_OP_xor, 0, 0));
11936 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11937 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11938 VAR_INIT_STATUS_INITIALIZED);
11939 add_loc_descr (&ret, tmp);
11940 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11941 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11942 add_loc_descr (&ret, l1jump);
11943 l2label = new_loc_descr (DW_OP_drop, 0, 0);
11944 add_loc_descr (&ret, l2label);
11945 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11946 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11947 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11948 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11949 return ret;
11952 /* BSWAP (constS is initial shift count, either 56 or 24):
11953 constS const0
11954 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
11955 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
11956 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
11957 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
11958 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
11960 static dw_loc_descr_ref
11961 bswap_loc_descriptor (rtx rtl, enum machine_mode mode,
11962 enum machine_mode mem_mode)
11964 dw_loc_descr_ref op0, ret, tmp;
11965 dw_loc_descr_ref l1jump, l1label;
11966 dw_loc_descr_ref l2jump, l2label;
11968 if (GET_MODE_CLASS (mode) != MODE_INT
11969 || BITS_PER_UNIT != 8
11970 || (GET_MODE_BITSIZE (mode) != 32
11971 && GET_MODE_BITSIZE (mode) != 64))
11972 return NULL;
11974 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11975 VAR_INIT_STATUS_INITIALIZED);
11976 if (op0 == NULL)
11977 return NULL;
11979 ret = op0;
11980 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11981 mode, mem_mode,
11982 VAR_INIT_STATUS_INITIALIZED);
11983 if (tmp == NULL)
11984 return NULL;
11985 add_loc_descr (&ret, tmp);
11986 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11987 VAR_INIT_STATUS_INITIALIZED);
11988 if (tmp == NULL)
11989 return NULL;
11990 add_loc_descr (&ret, tmp);
11991 l1label = new_loc_descr (DW_OP_pick, 2, 0);
11992 add_loc_descr (&ret, l1label);
11993 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11994 mode, mem_mode,
11995 VAR_INIT_STATUS_INITIALIZED);
11996 add_loc_descr (&ret, tmp);
11997 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
11998 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11999 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12000 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
12001 VAR_INIT_STATUS_INITIALIZED);
12002 if (tmp == NULL)
12003 return NULL;
12004 add_loc_descr (&ret, tmp);
12005 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12006 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
12007 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12008 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12009 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12010 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12011 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12012 VAR_INIT_STATUS_INITIALIZED);
12013 add_loc_descr (&ret, tmp);
12014 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
12015 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12016 add_loc_descr (&ret, l2jump);
12017 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
12018 VAR_INIT_STATUS_INITIALIZED);
12019 add_loc_descr (&ret, tmp);
12020 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12021 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12022 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12023 add_loc_descr (&ret, l1jump);
12024 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12025 add_loc_descr (&ret, l2label);
12026 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12027 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12028 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12029 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12030 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12031 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12032 return ret;
12035 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
12036 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12037 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
12038 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
12040 ROTATERT is similar:
12041 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
12042 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12043 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
12045 static dw_loc_descr_ref
12046 rotate_loc_descriptor (rtx rtl, enum machine_mode mode,
12047 enum machine_mode mem_mode)
12049 rtx rtlop1 = XEXP (rtl, 1);
12050 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
12051 int i;
12053 if (GET_MODE_CLASS (mode) != MODE_INT)
12054 return NULL;
12056 if (GET_MODE (rtlop1) != VOIDmode
12057 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
12058 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12059 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12060 VAR_INIT_STATUS_INITIALIZED);
12061 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12062 VAR_INIT_STATUS_INITIALIZED);
12063 if (op0 == NULL || op1 == NULL)
12064 return NULL;
12065 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12066 for (i = 0; i < 2; i++)
12068 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
12069 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
12070 mode, mem_mode,
12071 VAR_INIT_STATUS_INITIALIZED);
12072 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12073 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12074 ? DW_OP_const4u
12075 : HOST_BITS_PER_WIDE_INT == 64
12076 ? DW_OP_const8u : DW_OP_constu,
12077 GET_MODE_MASK (mode), 0);
12078 else
12079 mask[i] = NULL;
12080 if (mask[i] == NULL)
12081 return NULL;
12082 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
12084 ret = op0;
12085 add_loc_descr (&ret, op1);
12086 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12087 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12088 if (GET_CODE (rtl) == ROTATERT)
12090 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12091 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12092 GET_MODE_BITSIZE (mode), 0));
12094 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12095 if (mask[0] != NULL)
12096 add_loc_descr (&ret, mask[0]);
12097 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12098 if (mask[1] != NULL)
12100 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12101 add_loc_descr (&ret, mask[1]);
12102 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12104 if (GET_CODE (rtl) == ROTATE)
12106 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12107 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12108 GET_MODE_BITSIZE (mode), 0));
12110 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12111 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12112 return ret;
12115 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
12116 for DEBUG_PARAMETER_REF RTL. */
12118 static dw_loc_descr_ref
12119 parameter_ref_descriptor (rtx rtl)
12121 dw_loc_descr_ref ret;
12122 dw_die_ref ref;
12124 if (dwarf_strict)
12125 return NULL;
12126 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
12127 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
12128 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
12129 if (ref)
12131 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12132 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12133 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12135 else
12137 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12138 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
12140 return ret;
12143 /* The following routine converts the RTL for a variable or parameter
12144 (resident in memory) into an equivalent Dwarf representation of a
12145 mechanism for getting the address of that same variable onto the top of a
12146 hypothetical "address evaluation" stack.
12148 When creating memory location descriptors, we are effectively transforming
12149 the RTL for a memory-resident object into its Dwarf postfix expression
12150 equivalent. This routine recursively descends an RTL tree, turning
12151 it into Dwarf postfix code as it goes.
12153 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
12155 MEM_MODE is the mode of the memory reference, needed to handle some
12156 autoincrement addressing modes.
12158 Return 0 if we can't represent the location. */
12160 dw_loc_descr_ref
12161 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
12162 enum machine_mode mem_mode,
12163 enum var_init_status initialized)
12165 dw_loc_descr_ref mem_loc_result = NULL;
12166 enum dwarf_location_atom op;
12167 dw_loc_descr_ref op0, op1;
12168 rtx inner = NULL_RTX;
12170 if (mode == VOIDmode)
12171 mode = GET_MODE (rtl);
12173 /* Note that for a dynamically sized array, the location we will generate a
12174 description of here will be the lowest numbered location which is
12175 actually within the array. That's *not* necessarily the same as the
12176 zeroth element of the array. */
12178 rtl = targetm.delegitimize_address (rtl);
12180 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
12181 return NULL;
12183 switch (GET_CODE (rtl))
12185 case POST_INC:
12186 case POST_DEC:
12187 case POST_MODIFY:
12188 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
12190 case SUBREG:
12191 /* The case of a subreg may arise when we have a local (register)
12192 variable or a formal (register) parameter which doesn't quite fill
12193 up an entire register. For now, just assume that it is
12194 legitimate to make the Dwarf info refer to the whole register which
12195 contains the given subreg. */
12196 if (!subreg_lowpart_p (rtl))
12197 break;
12198 inner = SUBREG_REG (rtl);
12199 case TRUNCATE:
12200 if (inner == NULL_RTX)
12201 inner = XEXP (rtl, 0);
12202 if (GET_MODE_CLASS (mode) == MODE_INT
12203 && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
12204 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12205 #ifdef POINTERS_EXTEND_UNSIGNED
12206 || (mode == Pmode && mem_mode != VOIDmode)
12207 #endif
12209 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
12211 mem_loc_result = mem_loc_descriptor (inner,
12212 GET_MODE (inner),
12213 mem_mode, initialized);
12214 break;
12216 if (dwarf_strict)
12217 break;
12218 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
12219 break;
12220 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
12221 && (GET_MODE_CLASS (mode) != MODE_INT
12222 || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
12223 break;
12224 else
12226 dw_die_ref type_die;
12227 dw_loc_descr_ref cvt;
12229 mem_loc_result = mem_loc_descriptor (inner,
12230 GET_MODE (inner),
12231 mem_mode, initialized);
12232 if (mem_loc_result == NULL)
12233 break;
12234 type_die = base_type_for_mode (mode,
12235 GET_MODE_CLASS (mode) == MODE_INT);
12236 if (type_die == NULL)
12238 mem_loc_result = NULL;
12239 break;
12241 if (GET_MODE_SIZE (mode)
12242 != GET_MODE_SIZE (GET_MODE (inner)))
12243 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12244 else
12245 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
12246 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12247 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12248 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12249 add_loc_descr (&mem_loc_result, cvt);
12251 break;
12253 case REG:
12254 if (GET_MODE_CLASS (mode) != MODE_INT
12255 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12256 && rtl != arg_pointer_rtx
12257 && rtl != frame_pointer_rtx
12258 #ifdef POINTERS_EXTEND_UNSIGNED
12259 && (mode != Pmode || mem_mode == VOIDmode)
12260 #endif
12263 dw_die_ref type_die;
12264 unsigned int dbx_regnum;
12266 if (dwarf_strict)
12267 break;
12268 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12269 break;
12270 type_die = base_type_for_mode (mode,
12271 GET_MODE_CLASS (mode) == MODE_INT);
12272 if (type_die == NULL)
12273 break;
12275 dbx_regnum = dbx_reg_number (rtl);
12276 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12277 break;
12278 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12279 dbx_regnum, 0);
12280 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12281 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12282 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12283 break;
12285 /* Whenever a register number forms a part of the description of the
12286 method for calculating the (dynamic) address of a memory resident
12287 object, DWARF rules require the register number be referred to as
12288 a "base register". This distinction is not based in any way upon
12289 what category of register the hardware believes the given register
12290 belongs to. This is strictly DWARF terminology we're dealing with
12291 here. Note that in cases where the location of a memory-resident
12292 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12293 OP_CONST (0)) the actual DWARF location descriptor that we generate
12294 may just be OP_BASEREG (basereg). This may look deceptively like
12295 the object in question was allocated to a register (rather than in
12296 memory) so DWARF consumers need to be aware of the subtle
12297 distinction between OP_REG and OP_BASEREG. */
12298 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12299 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12300 else if (stack_realign_drap
12301 && crtl->drap_reg
12302 && crtl->args.internal_arg_pointer == rtl
12303 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12305 /* If RTL is internal_arg_pointer, which has been optimized
12306 out, use DRAP instead. */
12307 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12308 VAR_INIT_STATUS_INITIALIZED);
12310 break;
12312 case SIGN_EXTEND:
12313 case ZERO_EXTEND:
12314 if (GET_MODE_CLASS (mode) != MODE_INT)
12315 break;
12316 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12317 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12318 if (op0 == 0)
12319 break;
12320 else if (GET_CODE (rtl) == ZERO_EXTEND
12321 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12322 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12323 < HOST_BITS_PER_WIDE_INT
12324 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12325 to expand zero extend as two shifts instead of
12326 masking. */
12327 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12329 enum machine_mode imode = GET_MODE (XEXP (rtl, 0));
12330 mem_loc_result = op0;
12331 add_loc_descr (&mem_loc_result,
12332 int_loc_descriptor (GET_MODE_MASK (imode)));
12333 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12335 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12337 int shift = DWARF2_ADDR_SIZE
12338 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12339 shift *= BITS_PER_UNIT;
12340 if (GET_CODE (rtl) == SIGN_EXTEND)
12341 op = DW_OP_shra;
12342 else
12343 op = DW_OP_shr;
12344 mem_loc_result = op0;
12345 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12346 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12347 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12348 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12350 else if (!dwarf_strict)
12352 dw_die_ref type_die1, type_die2;
12353 dw_loc_descr_ref cvt;
12355 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12356 GET_CODE (rtl) == ZERO_EXTEND);
12357 if (type_die1 == NULL)
12358 break;
12359 type_die2 = base_type_for_mode (mode, 1);
12360 if (type_die2 == NULL)
12361 break;
12362 mem_loc_result = op0;
12363 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12364 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12365 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12366 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12367 add_loc_descr (&mem_loc_result, cvt);
12368 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12369 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12370 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12371 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12372 add_loc_descr (&mem_loc_result, cvt);
12374 break;
12376 case MEM:
12378 rtx new_rtl = avoid_constant_pool_reference (rtl);
12379 if (new_rtl != rtl)
12381 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12382 initialized);
12383 if (mem_loc_result != NULL)
12384 return mem_loc_result;
12387 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12388 get_address_mode (rtl), mode,
12389 VAR_INIT_STATUS_INITIALIZED);
12390 if (mem_loc_result == NULL)
12391 mem_loc_result = tls_mem_loc_descriptor (rtl);
12392 if (mem_loc_result != NULL)
12394 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12395 || GET_MODE_CLASS (mode) != MODE_INT)
12397 dw_die_ref type_die;
12398 dw_loc_descr_ref deref;
12400 if (dwarf_strict)
12401 return NULL;
12402 type_die
12403 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12404 if (type_die == NULL)
12405 return NULL;
12406 deref = new_loc_descr (DW_OP_GNU_deref_type,
12407 GET_MODE_SIZE (mode), 0);
12408 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12409 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12410 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12411 add_loc_descr (&mem_loc_result, deref);
12413 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12414 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12415 else
12416 add_loc_descr (&mem_loc_result,
12417 new_loc_descr (DW_OP_deref_size,
12418 GET_MODE_SIZE (mode), 0));
12420 break;
12422 case LO_SUM:
12423 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12425 case LABEL_REF:
12426 /* Some ports can transform a symbol ref into a label ref, because
12427 the symbol ref is too far away and has to be dumped into a constant
12428 pool. */
12429 case CONST:
12430 case SYMBOL_REF:
12431 if (GET_MODE_CLASS (mode) != MODE_INT
12432 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12433 #ifdef POINTERS_EXTEND_UNSIGNED
12434 && (mode != Pmode || mem_mode == VOIDmode)
12435 #endif
12437 break;
12438 if (GET_CODE (rtl) == SYMBOL_REF
12439 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12441 dw_loc_descr_ref temp;
12443 /* If this is not defined, we have no way to emit the data. */
12444 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12445 break;
12447 temp = new_addr_loc_descr (rtl, dtprel_true);
12449 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12450 add_loc_descr (&mem_loc_result, temp);
12452 break;
12455 if (!const_ok_for_output (rtl))
12456 break;
12458 symref:
12459 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12460 vec_safe_push (used_rtx_array, rtl);
12461 break;
12463 case CONCAT:
12464 case CONCATN:
12465 case VAR_LOCATION:
12466 case DEBUG_IMPLICIT_PTR:
12467 expansion_failed (NULL_TREE, rtl,
12468 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12469 return 0;
12471 case ENTRY_VALUE:
12472 if (dwarf_strict)
12473 return NULL;
12474 if (REG_P (ENTRY_VALUE_EXP (rtl)))
12476 if (GET_MODE_CLASS (mode) != MODE_INT
12477 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12478 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12479 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12480 else
12482 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12483 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12484 return NULL;
12485 op0 = one_reg_loc_descriptor (dbx_regnum,
12486 VAR_INIT_STATUS_INITIALIZED);
12489 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12490 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12492 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12493 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12494 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12495 return NULL;
12497 else
12498 gcc_unreachable ();
12499 if (op0 == NULL)
12500 return NULL;
12501 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12502 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12503 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12504 break;
12506 case DEBUG_PARAMETER_REF:
12507 mem_loc_result = parameter_ref_descriptor (rtl);
12508 break;
12510 case PRE_MODIFY:
12511 /* Extract the PLUS expression nested inside and fall into
12512 PLUS code below. */
12513 rtl = XEXP (rtl, 1);
12514 goto plus;
12516 case PRE_INC:
12517 case PRE_DEC:
12518 /* Turn these into a PLUS expression and fall into the PLUS code
12519 below. */
12520 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12521 gen_int_mode (GET_CODE (rtl) == PRE_INC
12522 ? GET_MODE_UNIT_SIZE (mem_mode)
12523 : -GET_MODE_UNIT_SIZE (mem_mode),
12524 mode));
12526 /* ... fall through ... */
12528 case PLUS:
12529 plus:
12530 if (is_based_loc (rtl)
12531 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12532 || XEXP (rtl, 0) == arg_pointer_rtx
12533 || XEXP (rtl, 0) == frame_pointer_rtx)
12534 && GET_MODE_CLASS (mode) == MODE_INT)
12535 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12536 INTVAL (XEXP (rtl, 1)),
12537 VAR_INIT_STATUS_INITIALIZED);
12538 else
12540 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12541 VAR_INIT_STATUS_INITIALIZED);
12542 if (mem_loc_result == 0)
12543 break;
12545 if (CONST_INT_P (XEXP (rtl, 1))
12546 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12547 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12548 else
12550 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12551 VAR_INIT_STATUS_INITIALIZED);
12552 if (op1 == 0)
12553 break;
12554 add_loc_descr (&mem_loc_result, op1);
12555 add_loc_descr (&mem_loc_result,
12556 new_loc_descr (DW_OP_plus, 0, 0));
12559 break;
12561 /* If a pseudo-reg is optimized away, it is possible for it to
12562 be replaced with a MEM containing a multiply or shift. */
12563 case MINUS:
12564 op = DW_OP_minus;
12565 goto do_binop;
12567 case MULT:
12568 op = DW_OP_mul;
12569 goto do_binop;
12571 case DIV:
12572 if (!dwarf_strict
12573 && GET_MODE_CLASS (mode) == MODE_INT
12574 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12576 mem_loc_result = typed_binop (DW_OP_div, rtl,
12577 base_type_for_mode (mode, 0),
12578 mode, mem_mode);
12579 break;
12581 op = DW_OP_div;
12582 goto do_binop;
12584 case UMOD:
12585 op = DW_OP_mod;
12586 goto do_binop;
12588 case ASHIFT:
12589 op = DW_OP_shl;
12590 goto do_shift;
12592 case ASHIFTRT:
12593 op = DW_OP_shra;
12594 goto do_shift;
12596 case LSHIFTRT:
12597 op = DW_OP_shr;
12598 goto do_shift;
12600 do_shift:
12601 if (GET_MODE_CLASS (mode) != MODE_INT)
12602 break;
12603 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12604 VAR_INIT_STATUS_INITIALIZED);
12606 rtx rtlop1 = XEXP (rtl, 1);
12607 if (GET_MODE (rtlop1) != VOIDmode
12608 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
12609 < GET_MODE_BITSIZE (mode))
12610 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12611 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12612 VAR_INIT_STATUS_INITIALIZED);
12615 if (op0 == 0 || op1 == 0)
12616 break;
12618 mem_loc_result = op0;
12619 add_loc_descr (&mem_loc_result, op1);
12620 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12621 break;
12623 case AND:
12624 op = DW_OP_and;
12625 goto do_binop;
12627 case IOR:
12628 op = DW_OP_or;
12629 goto do_binop;
12631 case XOR:
12632 op = DW_OP_xor;
12633 goto do_binop;
12635 do_binop:
12636 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12637 VAR_INIT_STATUS_INITIALIZED);
12638 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12639 VAR_INIT_STATUS_INITIALIZED);
12641 if (op0 == 0 || op1 == 0)
12642 break;
12644 mem_loc_result = op0;
12645 add_loc_descr (&mem_loc_result, op1);
12646 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12647 break;
12649 case MOD:
12650 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
12652 mem_loc_result = typed_binop (DW_OP_mod, rtl,
12653 base_type_for_mode (mode, 0),
12654 mode, mem_mode);
12655 break;
12658 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12659 VAR_INIT_STATUS_INITIALIZED);
12660 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12661 VAR_INIT_STATUS_INITIALIZED);
12663 if (op0 == 0 || op1 == 0)
12664 break;
12666 mem_loc_result = op0;
12667 add_loc_descr (&mem_loc_result, op1);
12668 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12669 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12670 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
12671 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
12672 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
12673 break;
12675 case UDIV:
12676 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
12678 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
12680 op = DW_OP_div;
12681 goto do_binop;
12683 mem_loc_result = typed_binop (DW_OP_div, rtl,
12684 base_type_for_mode (mode, 1),
12685 mode, mem_mode);
12687 break;
12689 case NOT:
12690 op = DW_OP_not;
12691 goto do_unop;
12693 case ABS:
12694 op = DW_OP_abs;
12695 goto do_unop;
12697 case NEG:
12698 op = DW_OP_neg;
12699 goto do_unop;
12701 do_unop:
12702 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12703 VAR_INIT_STATUS_INITIALIZED);
12705 if (op0 == 0)
12706 break;
12708 mem_loc_result = op0;
12709 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12710 break;
12712 case CONST_INT:
12713 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12714 #ifdef POINTERS_EXTEND_UNSIGNED
12715 || (mode == Pmode
12716 && mem_mode != VOIDmode
12717 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
12718 #endif
12721 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12722 break;
12724 if (!dwarf_strict
12725 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
12726 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
12728 dw_die_ref type_die = base_type_for_mode (mode, 1);
12729 enum machine_mode amode;
12730 if (type_die == NULL)
12731 return NULL;
12732 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
12733 MODE_INT, 0);
12734 if (INTVAL (rtl) >= 0
12735 && amode != BLKmode
12736 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
12737 /* const DW_OP_GNU_convert <XXX> vs.
12738 DW_OP_GNU_const_type <XXX, 1, const>. */
12739 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
12740 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
12742 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12743 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12744 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12745 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12746 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
12747 add_loc_descr (&mem_loc_result, op0);
12748 return mem_loc_result;
12750 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
12751 INTVAL (rtl));
12752 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12753 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12754 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12755 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12756 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12757 else
12759 mem_loc_result->dw_loc_oprnd2.val_class
12760 = dw_val_class_const_double;
12761 mem_loc_result->dw_loc_oprnd2.v.val_double
12762 = double_int::from_shwi (INTVAL (rtl));
12765 break;
12767 case CONST_DOUBLE:
12768 if (!dwarf_strict)
12770 dw_die_ref type_die;
12772 /* Note that a CONST_DOUBLE rtx could represent either an integer
12773 or a floating-point constant. A CONST_DOUBLE is used whenever
12774 the constant requires more than one word in order to be
12775 adequately represented. We output CONST_DOUBLEs as blocks. */
12776 if (mode == VOIDmode
12777 || (GET_MODE (rtl) == VOIDmode
12778 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
12779 break;
12780 type_die = base_type_for_mode (mode,
12781 GET_MODE_CLASS (mode) == MODE_INT);
12782 if (type_die == NULL)
12783 return NULL;
12784 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
12785 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12786 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12787 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12788 if (SCALAR_FLOAT_MODE_P (mode))
12790 unsigned int length = GET_MODE_SIZE (mode);
12791 unsigned char *array
12792 = (unsigned char*) ggc_alloc_atomic (length);
12794 insert_float (rtl, array);
12795 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12796 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12797 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12798 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12800 else
12802 mem_loc_result->dw_loc_oprnd2.val_class
12803 = dw_val_class_const_double;
12804 mem_loc_result->dw_loc_oprnd2.v.val_double
12805 = rtx_to_double_int (rtl);
12808 break;
12810 case EQ:
12811 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
12812 break;
12814 case GE:
12815 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12816 break;
12818 case GT:
12819 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12820 break;
12822 case LE:
12823 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12824 break;
12826 case LT:
12827 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12828 break;
12830 case NE:
12831 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
12832 break;
12834 case GEU:
12835 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12836 break;
12838 case GTU:
12839 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12840 break;
12842 case LEU:
12843 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12844 break;
12846 case LTU:
12847 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12848 break;
12850 case UMIN:
12851 case UMAX:
12852 if (GET_MODE_CLASS (mode) != MODE_INT)
12853 break;
12854 /* FALLTHRU */
12855 case SMIN:
12856 case SMAX:
12857 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
12858 break;
12860 case ZERO_EXTRACT:
12861 case SIGN_EXTRACT:
12862 if (CONST_INT_P (XEXP (rtl, 1))
12863 && CONST_INT_P (XEXP (rtl, 2))
12864 && ((unsigned) INTVAL (XEXP (rtl, 1))
12865 + (unsigned) INTVAL (XEXP (rtl, 2))
12866 <= GET_MODE_BITSIZE (mode))
12867 && GET_MODE_CLASS (mode) == MODE_INT
12868 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12869 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
12871 int shift, size;
12872 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12873 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12874 if (op0 == 0)
12875 break;
12876 if (GET_CODE (rtl) == SIGN_EXTRACT)
12877 op = DW_OP_shra;
12878 else
12879 op = DW_OP_shr;
12880 mem_loc_result = op0;
12881 size = INTVAL (XEXP (rtl, 1));
12882 shift = INTVAL (XEXP (rtl, 2));
12883 if (BITS_BIG_ENDIAN)
12884 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12885 - shift - size;
12886 if (shift + size != (int) DWARF2_ADDR_SIZE)
12888 add_loc_descr (&mem_loc_result,
12889 int_loc_descriptor (DWARF2_ADDR_SIZE
12890 - shift - size));
12891 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12893 if (size != (int) DWARF2_ADDR_SIZE)
12895 add_loc_descr (&mem_loc_result,
12896 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
12897 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12900 break;
12902 case IF_THEN_ELSE:
12904 dw_loc_descr_ref op2, bra_node, drop_node;
12905 op0 = mem_loc_descriptor (XEXP (rtl, 0),
12906 GET_MODE (XEXP (rtl, 0)) == VOIDmode
12907 ? word_mode : GET_MODE (XEXP (rtl, 0)),
12908 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12909 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12910 VAR_INIT_STATUS_INITIALIZED);
12911 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
12912 VAR_INIT_STATUS_INITIALIZED);
12913 if (op0 == NULL || op1 == NULL || op2 == NULL)
12914 break;
12916 mem_loc_result = op1;
12917 add_loc_descr (&mem_loc_result, op2);
12918 add_loc_descr (&mem_loc_result, op0);
12919 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12920 add_loc_descr (&mem_loc_result, bra_node);
12921 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
12922 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12923 add_loc_descr (&mem_loc_result, drop_node);
12924 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12925 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12927 break;
12929 case FLOAT_EXTEND:
12930 case FLOAT_TRUNCATE:
12931 case FLOAT:
12932 case UNSIGNED_FLOAT:
12933 case FIX:
12934 case UNSIGNED_FIX:
12935 if (!dwarf_strict)
12937 dw_die_ref type_die;
12938 dw_loc_descr_ref cvt;
12940 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12941 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12942 if (op0 == NULL)
12943 break;
12944 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
12945 && (GET_CODE (rtl) == FLOAT
12946 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
12947 <= DWARF2_ADDR_SIZE))
12949 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12950 GET_CODE (rtl) == UNSIGNED_FLOAT);
12951 if (type_die == NULL)
12952 break;
12953 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12954 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12955 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12956 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12957 add_loc_descr (&op0, cvt);
12959 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
12960 if (type_die == NULL)
12961 break;
12962 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12963 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12964 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12965 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12966 add_loc_descr (&op0, cvt);
12967 if (GET_MODE_CLASS (mode) == MODE_INT
12968 && (GET_CODE (rtl) == FIX
12969 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
12971 op0 = convert_descriptor_to_mode (mode, op0);
12972 if (op0 == NULL)
12973 break;
12975 mem_loc_result = op0;
12977 break;
12979 case CLZ:
12980 case CTZ:
12981 case FFS:
12982 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
12983 break;
12985 case POPCOUNT:
12986 case PARITY:
12987 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
12988 break;
12990 case BSWAP:
12991 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
12992 break;
12994 case ROTATE:
12995 case ROTATERT:
12996 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
12997 break;
12999 case COMPARE:
13000 /* In theory, we could implement the above. */
13001 /* DWARF cannot represent the unsigned compare operations
13002 natively. */
13003 case SS_MULT:
13004 case US_MULT:
13005 case SS_DIV:
13006 case US_DIV:
13007 case SS_PLUS:
13008 case US_PLUS:
13009 case SS_MINUS:
13010 case US_MINUS:
13011 case SS_NEG:
13012 case US_NEG:
13013 case SS_ABS:
13014 case SS_ASHIFT:
13015 case US_ASHIFT:
13016 case SS_TRUNCATE:
13017 case US_TRUNCATE:
13018 case UNORDERED:
13019 case ORDERED:
13020 case UNEQ:
13021 case UNGE:
13022 case UNGT:
13023 case UNLE:
13024 case UNLT:
13025 case LTGT:
13026 case FRACT_CONVERT:
13027 case UNSIGNED_FRACT_CONVERT:
13028 case SAT_FRACT:
13029 case UNSIGNED_SAT_FRACT:
13030 case SQRT:
13031 case ASM_OPERANDS:
13032 case VEC_MERGE:
13033 case VEC_SELECT:
13034 case VEC_CONCAT:
13035 case VEC_DUPLICATE:
13036 case UNSPEC:
13037 case HIGH:
13038 case FMA:
13039 case STRICT_LOW_PART:
13040 case CONST_VECTOR:
13041 case CONST_FIXED:
13042 case CLRSB:
13043 case CLOBBER:
13044 /* If delegitimize_address couldn't do anything with the UNSPEC, we
13045 can't express it in the debug info. This can happen e.g. with some
13046 TLS UNSPECs. */
13047 break;
13049 case CONST_STRING:
13050 resolve_one_addr (&rtl, NULL);
13051 goto symref;
13053 default:
13054 #ifdef ENABLE_CHECKING
13055 print_rtl (stderr, rtl);
13056 gcc_unreachable ();
13057 #else
13058 break;
13059 #endif
13062 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13063 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13065 return mem_loc_result;
13068 /* Return a descriptor that describes the concatenation of two locations.
13069 This is typically a complex variable. */
13071 static dw_loc_descr_ref
13072 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
13074 dw_loc_descr_ref cc_loc_result = NULL;
13075 dw_loc_descr_ref x0_ref
13076 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13077 dw_loc_descr_ref x1_ref
13078 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13080 if (x0_ref == 0 || x1_ref == 0)
13081 return 0;
13083 cc_loc_result = x0_ref;
13084 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
13086 add_loc_descr (&cc_loc_result, x1_ref);
13087 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
13089 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13090 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13092 return cc_loc_result;
13095 /* Return a descriptor that describes the concatenation of N
13096 locations. */
13098 static dw_loc_descr_ref
13099 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
13101 unsigned int i;
13102 dw_loc_descr_ref cc_loc_result = NULL;
13103 unsigned int n = XVECLEN (concatn, 0);
13105 for (i = 0; i < n; ++i)
13107 dw_loc_descr_ref ref;
13108 rtx x = XVECEXP (concatn, 0, i);
13110 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13111 if (ref == NULL)
13112 return NULL;
13114 add_loc_descr (&cc_loc_result, ref);
13115 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
13118 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13119 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13121 return cc_loc_result;
13124 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
13125 for DEBUG_IMPLICIT_PTR RTL. */
13127 static dw_loc_descr_ref
13128 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
13130 dw_loc_descr_ref ret;
13131 dw_die_ref ref;
13133 if (dwarf_strict)
13134 return NULL;
13135 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
13136 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
13137 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
13138 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
13139 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
13140 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
13141 if (ref)
13143 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13144 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13145 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13147 else
13149 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13150 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
13152 return ret;
13155 /* Output a proper Dwarf location descriptor for a variable or parameter
13156 which is either allocated in a register or in a memory location. For a
13157 register, we just generate an OP_REG and the register number. For a
13158 memory location we provide a Dwarf postfix expression describing how to
13159 generate the (dynamic) address of the object onto the address stack.
13161 MODE is mode of the decl if this loc_descriptor is going to be used in
13162 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
13163 allowed, VOIDmode otherwise.
13165 If we don't know how to describe it, return 0. */
13167 static dw_loc_descr_ref
13168 loc_descriptor (rtx rtl, enum machine_mode mode,
13169 enum var_init_status initialized)
13171 dw_loc_descr_ref loc_result = NULL;
13173 switch (GET_CODE (rtl))
13175 case SUBREG:
13176 /* The case of a subreg may arise when we have a local (register)
13177 variable or a formal (register) parameter which doesn't quite fill
13178 up an entire register. For now, just assume that it is
13179 legitimate to make the Dwarf info refer to the whole register which
13180 contains the given subreg. */
13181 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
13182 loc_result = loc_descriptor (SUBREG_REG (rtl),
13183 GET_MODE (SUBREG_REG (rtl)), initialized);
13184 else
13185 goto do_default;
13186 break;
13188 case REG:
13189 loc_result = reg_loc_descriptor (rtl, initialized);
13190 break;
13192 case MEM:
13193 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13194 GET_MODE (rtl), initialized);
13195 if (loc_result == NULL)
13196 loc_result = tls_mem_loc_descriptor (rtl);
13197 if (loc_result == NULL)
13199 rtx new_rtl = avoid_constant_pool_reference (rtl);
13200 if (new_rtl != rtl)
13201 loc_result = loc_descriptor (new_rtl, mode, initialized);
13203 break;
13205 case CONCAT:
13206 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
13207 initialized);
13208 break;
13210 case CONCATN:
13211 loc_result = concatn_loc_descriptor (rtl, initialized);
13212 break;
13214 case VAR_LOCATION:
13215 /* Single part. */
13216 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
13218 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
13219 if (GET_CODE (loc) == EXPR_LIST)
13220 loc = XEXP (loc, 0);
13221 loc_result = loc_descriptor (loc, mode, initialized);
13222 break;
13225 rtl = XEXP (rtl, 1);
13226 /* FALLTHRU */
13228 case PARALLEL:
13230 rtvec par_elems = XVEC (rtl, 0);
13231 int num_elem = GET_NUM_ELEM (par_elems);
13232 enum machine_mode mode;
13233 int i;
13235 /* Create the first one, so we have something to add to. */
13236 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13237 VOIDmode, initialized);
13238 if (loc_result == NULL)
13239 return NULL;
13240 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13241 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13242 for (i = 1; i < num_elem; i++)
13244 dw_loc_descr_ref temp;
13246 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13247 VOIDmode, initialized);
13248 if (temp == NULL)
13249 return NULL;
13250 add_loc_descr (&loc_result, temp);
13251 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13252 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13255 break;
13257 case CONST_INT:
13258 if (mode != VOIDmode && mode != BLKmode)
13259 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13260 INTVAL (rtl));
13261 break;
13263 case CONST_DOUBLE:
13264 if (mode == VOIDmode)
13265 mode = GET_MODE (rtl);
13267 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13269 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13271 /* Note that a CONST_DOUBLE rtx could represent either an integer
13272 or a floating-point constant. A CONST_DOUBLE is used whenever
13273 the constant requires more than one word in order to be
13274 adequately represented. We output CONST_DOUBLEs as blocks. */
13275 loc_result = new_loc_descr (DW_OP_implicit_value,
13276 GET_MODE_SIZE (mode), 0);
13277 if (SCALAR_FLOAT_MODE_P (mode))
13279 unsigned int length = GET_MODE_SIZE (mode);
13280 unsigned char *array
13281 = (unsigned char*) ggc_alloc_atomic (length);
13283 insert_float (rtl, array);
13284 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13285 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13286 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13287 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13289 else
13291 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13292 loc_result->dw_loc_oprnd2.v.val_double
13293 = rtx_to_double_int (rtl);
13296 break;
13298 case CONST_VECTOR:
13299 if (mode == VOIDmode)
13300 mode = GET_MODE (rtl);
13302 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13304 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13305 unsigned int length = CONST_VECTOR_NUNITS (rtl);
13306 unsigned char *array = (unsigned char *)
13307 ggc_alloc_atomic (length * elt_size);
13308 unsigned int i;
13309 unsigned char *p;
13311 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13312 switch (GET_MODE_CLASS (mode))
13314 case MODE_VECTOR_INT:
13315 for (i = 0, p = array; i < length; i++, p += elt_size)
13317 rtx elt = CONST_VECTOR_ELT (rtl, i);
13318 double_int val = rtx_to_double_int (elt);
13320 if (elt_size <= sizeof (HOST_WIDE_INT))
13321 insert_int (val.to_shwi (), elt_size, p);
13322 else
13324 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
13325 insert_double (val, p);
13328 break;
13330 case MODE_VECTOR_FLOAT:
13331 for (i = 0, p = array; i < length; i++, p += elt_size)
13333 rtx elt = CONST_VECTOR_ELT (rtl, i);
13334 insert_float (elt, p);
13336 break;
13338 default:
13339 gcc_unreachable ();
13342 loc_result = new_loc_descr (DW_OP_implicit_value,
13343 length * elt_size, 0);
13344 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13345 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13346 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13347 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13349 break;
13351 case CONST:
13352 if (mode == VOIDmode
13353 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13354 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13355 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13357 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13358 break;
13360 /* FALLTHROUGH */
13361 case SYMBOL_REF:
13362 if (!const_ok_for_output (rtl))
13363 break;
13364 case LABEL_REF:
13365 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13366 && (dwarf_version >= 4 || !dwarf_strict))
13368 loc_result = new_addr_loc_descr (rtl, dtprel_false);
13369 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13370 vec_safe_push (used_rtx_array, rtl);
13372 break;
13374 case DEBUG_IMPLICIT_PTR:
13375 loc_result = implicit_ptr_descriptor (rtl, 0);
13376 break;
13378 case PLUS:
13379 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13380 && CONST_INT_P (XEXP (rtl, 1)))
13382 loc_result
13383 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13384 break;
13386 /* FALLTHRU */
13387 do_default:
13388 default:
13389 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13390 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13391 && dwarf_version >= 4)
13392 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13394 /* Value expression. */
13395 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13396 if (loc_result)
13397 add_loc_descr (&loc_result,
13398 new_loc_descr (DW_OP_stack_value, 0, 0));
13400 break;
13403 return loc_result;
13406 /* We need to figure out what section we should use as the base for the
13407 address ranges where a given location is valid.
13408 1. If this particular DECL has a section associated with it, use that.
13409 2. If this function has a section associated with it, use that.
13410 3. Otherwise, use the text section.
13411 XXX: If you split a variable across multiple sections, we won't notice. */
13413 static const char *
13414 secname_for_decl (const_tree decl)
13416 const char *secname;
13418 if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
13420 tree sectree = DECL_SECTION_NAME (decl);
13421 secname = TREE_STRING_POINTER (sectree);
13423 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13425 tree sectree = DECL_SECTION_NAME (current_function_decl);
13426 secname = TREE_STRING_POINTER (sectree);
13428 else if (cfun && in_cold_section_p)
13429 secname = crtl->subsections.cold_section_label;
13430 else
13431 secname = text_section_label;
13433 return secname;
13436 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
13438 static bool
13439 decl_by_reference_p (tree decl)
13441 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13442 || TREE_CODE (decl) == VAR_DECL)
13443 && DECL_BY_REFERENCE (decl));
13446 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13447 for VARLOC. */
13449 static dw_loc_descr_ref
13450 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13451 enum var_init_status initialized)
13453 int have_address = 0;
13454 dw_loc_descr_ref descr;
13455 enum machine_mode mode;
13457 if (want_address != 2)
13459 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13460 /* Single part. */
13461 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13463 varloc = PAT_VAR_LOCATION_LOC (varloc);
13464 if (GET_CODE (varloc) == EXPR_LIST)
13465 varloc = XEXP (varloc, 0);
13466 mode = GET_MODE (varloc);
13467 if (MEM_P (varloc))
13469 rtx addr = XEXP (varloc, 0);
13470 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13471 mode, initialized);
13472 if (descr)
13473 have_address = 1;
13474 else
13476 rtx x = avoid_constant_pool_reference (varloc);
13477 if (x != varloc)
13478 descr = mem_loc_descriptor (x, mode, VOIDmode,
13479 initialized);
13482 else
13483 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13485 else
13486 return 0;
13488 else
13490 if (GET_CODE (varloc) == VAR_LOCATION)
13491 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13492 else
13493 mode = DECL_MODE (loc);
13494 descr = loc_descriptor (varloc, mode, initialized);
13495 have_address = 1;
13498 if (!descr)
13499 return 0;
13501 if (want_address == 2 && !have_address
13502 && (dwarf_version >= 4 || !dwarf_strict))
13504 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13506 expansion_failed (loc, NULL_RTX,
13507 "DWARF address size mismatch");
13508 return 0;
13510 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13511 have_address = 1;
13513 /* Show if we can't fill the request for an address. */
13514 if (want_address && !have_address)
13516 expansion_failed (loc, NULL_RTX,
13517 "Want address and only have value");
13518 return 0;
13521 /* If we've got an address and don't want one, dereference. */
13522 if (!want_address && have_address)
13524 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13525 enum dwarf_location_atom op;
13527 if (size > DWARF2_ADDR_SIZE || size == -1)
13529 expansion_failed (loc, NULL_RTX,
13530 "DWARF address size mismatch");
13531 return 0;
13533 else if (size == DWARF2_ADDR_SIZE)
13534 op = DW_OP_deref;
13535 else
13536 op = DW_OP_deref_size;
13538 add_loc_descr (&descr, new_loc_descr (op, size, 0));
13541 return descr;
13544 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
13545 if it is not possible. */
13547 static dw_loc_descr_ref
13548 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
13550 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
13551 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
13552 else if (dwarf_version >= 3 || !dwarf_strict)
13553 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
13554 else
13555 return NULL;
13558 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13559 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
13561 static dw_loc_descr_ref
13562 dw_sra_loc_expr (tree decl, rtx loc)
13564 rtx p;
13565 unsigned int padsize = 0;
13566 dw_loc_descr_ref descr, *descr_tail;
13567 unsigned HOST_WIDE_INT decl_size;
13568 rtx varloc;
13569 enum var_init_status initialized;
13571 if (DECL_SIZE (decl) == NULL
13572 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
13573 return NULL;
13575 decl_size = tree_to_uhwi (DECL_SIZE (decl));
13576 descr = NULL;
13577 descr_tail = &descr;
13579 for (p = loc; p; p = XEXP (p, 1))
13581 unsigned int bitsize = decl_piece_bitsize (p);
13582 rtx loc_note = *decl_piece_varloc_ptr (p);
13583 dw_loc_descr_ref cur_descr;
13584 dw_loc_descr_ref *tail, last = NULL;
13585 unsigned int opsize = 0;
13587 if (loc_note == NULL_RTX
13588 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
13590 padsize += bitsize;
13591 continue;
13593 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
13594 varloc = NOTE_VAR_LOCATION (loc_note);
13595 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
13596 if (cur_descr == NULL)
13598 padsize += bitsize;
13599 continue;
13602 /* Check that cur_descr either doesn't use
13603 DW_OP_*piece operations, or their sum is equal
13604 to bitsize. Otherwise we can't embed it. */
13605 for (tail = &cur_descr; *tail != NULL;
13606 tail = &(*tail)->dw_loc_next)
13607 if ((*tail)->dw_loc_opc == DW_OP_piece)
13609 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
13610 * BITS_PER_UNIT;
13611 last = *tail;
13613 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
13615 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
13616 last = *tail;
13619 if (last != NULL && opsize != bitsize)
13621 padsize += bitsize;
13622 /* Discard the current piece of the descriptor and release any
13623 addr_table entries it uses. */
13624 remove_loc_list_addr_table_entries (cur_descr);
13625 continue;
13628 /* If there is a hole, add DW_OP_*piece after empty DWARF
13629 expression, which means that those bits are optimized out. */
13630 if (padsize)
13632 if (padsize > decl_size)
13634 remove_loc_list_addr_table_entries (cur_descr);
13635 goto discard_descr;
13637 decl_size -= padsize;
13638 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
13639 if (*descr_tail == NULL)
13641 remove_loc_list_addr_table_entries (cur_descr);
13642 goto discard_descr;
13644 descr_tail = &(*descr_tail)->dw_loc_next;
13645 padsize = 0;
13647 *descr_tail = cur_descr;
13648 descr_tail = tail;
13649 if (bitsize > decl_size)
13650 goto discard_descr;
13651 decl_size -= bitsize;
13652 if (last == NULL)
13654 HOST_WIDE_INT offset = 0;
13655 if (GET_CODE (varloc) == VAR_LOCATION
13656 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13658 varloc = PAT_VAR_LOCATION_LOC (varloc);
13659 if (GET_CODE (varloc) == EXPR_LIST)
13660 varloc = XEXP (varloc, 0);
13664 if (GET_CODE (varloc) == CONST
13665 || GET_CODE (varloc) == SIGN_EXTEND
13666 || GET_CODE (varloc) == ZERO_EXTEND)
13667 varloc = XEXP (varloc, 0);
13668 else if (GET_CODE (varloc) == SUBREG)
13669 varloc = SUBREG_REG (varloc);
13670 else
13671 break;
13673 while (1);
13674 /* DW_OP_bit_size offset should be zero for register
13675 or implicit location descriptions and empty location
13676 descriptions, but for memory addresses needs big endian
13677 adjustment. */
13678 if (MEM_P (varloc))
13680 unsigned HOST_WIDE_INT memsize
13681 = MEM_SIZE (varloc) * BITS_PER_UNIT;
13682 if (memsize != bitsize)
13684 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
13685 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
13686 goto discard_descr;
13687 if (memsize < bitsize)
13688 goto discard_descr;
13689 if (BITS_BIG_ENDIAN)
13690 offset = memsize - bitsize;
13694 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
13695 if (*descr_tail == NULL)
13696 goto discard_descr;
13697 descr_tail = &(*descr_tail)->dw_loc_next;
13701 /* If there were any non-empty expressions, add padding till the end of
13702 the decl. */
13703 if (descr != NULL && decl_size != 0)
13705 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
13706 if (*descr_tail == NULL)
13707 goto discard_descr;
13709 return descr;
13711 discard_descr:
13712 /* Discard the descriptor and release any addr_table entries it uses. */
13713 remove_loc_list_addr_table_entries (descr);
13714 return NULL;
13717 /* Return the dwarf representation of the location list LOC_LIST of
13718 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
13719 function. */
13721 static dw_loc_list_ref
13722 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
13724 const char *endname, *secname;
13725 rtx varloc;
13726 enum var_init_status initialized;
13727 struct var_loc_node *node;
13728 dw_loc_descr_ref descr;
13729 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13730 dw_loc_list_ref list = NULL;
13731 dw_loc_list_ref *listp = &list;
13733 /* Now that we know what section we are using for a base,
13734 actually construct the list of locations.
13735 The first location information is what is passed to the
13736 function that creates the location list, and the remaining
13737 locations just get added on to that list.
13738 Note that we only know the start address for a location
13739 (IE location changes), so to build the range, we use
13740 the range [current location start, next location start].
13741 This means we have to special case the last node, and generate
13742 a range of [last location start, end of function label]. */
13744 secname = secname_for_decl (decl);
13746 for (node = loc_list->first; node; node = node->next)
13747 if (GET_CODE (node->loc) == EXPR_LIST
13748 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
13750 if (GET_CODE (node->loc) == EXPR_LIST)
13752 /* This requires DW_OP_{,bit_}piece, which is not usable
13753 inside DWARF expressions. */
13754 if (want_address != 2)
13755 continue;
13756 descr = dw_sra_loc_expr (decl, node->loc);
13757 if (descr == NULL)
13758 continue;
13760 else
13762 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13763 varloc = NOTE_VAR_LOCATION (node->loc);
13764 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
13766 if (descr)
13768 bool range_across_switch = false;
13769 /* If section switch happens in between node->label
13770 and node->next->label (or end of function) and
13771 we can't emit it as a single entry list,
13772 emit two ranges, first one ending at the end
13773 of first partition and second one starting at the
13774 beginning of second partition. */
13775 if (node == loc_list->last_before_switch
13776 && (node != loc_list->first || loc_list->first->next)
13777 && current_function_decl)
13779 endname = cfun->fde->dw_fde_end;
13780 range_across_switch = true;
13782 /* The variable has a location between NODE->LABEL and
13783 NODE->NEXT->LABEL. */
13784 else if (node->next)
13785 endname = node->next->label;
13786 /* If the variable has a location at the last label
13787 it keeps its location until the end of function. */
13788 else if (!current_function_decl)
13789 endname = text_end_label;
13790 else
13792 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13793 current_function_funcdef_no);
13794 endname = ggc_strdup (label_id);
13797 *listp = new_loc_list (descr, node->label, endname, secname);
13798 if (TREE_CODE (decl) == PARM_DECL
13799 && node == loc_list->first
13800 && NOTE_P (node->loc)
13801 && strcmp (node->label, endname) == 0)
13802 (*listp)->force = true;
13803 listp = &(*listp)->dw_loc_next;
13805 if (range_across_switch)
13807 if (GET_CODE (node->loc) == EXPR_LIST)
13808 descr = dw_sra_loc_expr (decl, node->loc);
13809 else
13811 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13812 varloc = NOTE_VAR_LOCATION (node->loc);
13813 descr = dw_loc_list_1 (decl, varloc, want_address,
13814 initialized);
13816 gcc_assert (descr);
13817 /* The variable has a location between NODE->LABEL and
13818 NODE->NEXT->LABEL. */
13819 if (node->next)
13820 endname = node->next->label;
13821 else
13822 endname = cfun->fde->dw_fde_second_end;
13823 *listp = new_loc_list (descr,
13824 cfun->fde->dw_fde_second_begin,
13825 endname, secname);
13826 listp = &(*listp)->dw_loc_next;
13831 /* Try to avoid the overhead of a location list emitting a location
13832 expression instead, but only if we didn't have more than one
13833 location entry in the first place. If some entries were not
13834 representable, we don't want to pretend a single entry that was
13835 applies to the entire scope in which the variable is
13836 available. */
13837 if (list && loc_list->first->next)
13838 gen_llsym (list);
13840 return list;
13843 /* Return if the loc_list has only single element and thus can be represented
13844 as location description. */
13846 static bool
13847 single_element_loc_list_p (dw_loc_list_ref list)
13849 gcc_assert (!list->dw_loc_next || list->ll_symbol);
13850 return !list->ll_symbol;
13853 /* To each location in list LIST add loc descr REF. */
13855 static void
13856 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
13858 dw_loc_descr_ref copy;
13859 add_loc_descr (&list->expr, ref);
13860 list = list->dw_loc_next;
13861 while (list)
13863 copy = ggc_alloc_dw_loc_descr_node ();
13864 memcpy (copy, ref, sizeof (dw_loc_descr_node));
13865 add_loc_descr (&list->expr, copy);
13866 while (copy->dw_loc_next)
13868 dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
13869 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
13870 copy->dw_loc_next = new_copy;
13871 copy = new_copy;
13873 list = list->dw_loc_next;
13877 /* Given two lists RET and LIST
13878 produce location list that is result of adding expression in LIST
13879 to expression in RET on each position in program.
13880 Might be destructive on both RET and LIST.
13882 TODO: We handle only simple cases of RET or LIST having at most one
13883 element. General case would inolve sorting the lists in program order
13884 and merging them that will need some additional work.
13885 Adding that will improve quality of debug info especially for SRA-ed
13886 structures. */
13888 static void
13889 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
13891 if (!list)
13892 return;
13893 if (!*ret)
13895 *ret = list;
13896 return;
13898 if (!list->dw_loc_next)
13900 add_loc_descr_to_each (*ret, list->expr);
13901 return;
13903 if (!(*ret)->dw_loc_next)
13905 add_loc_descr_to_each (list, (*ret)->expr);
13906 *ret = list;
13907 return;
13909 expansion_failed (NULL_TREE, NULL_RTX,
13910 "Don't know how to merge two non-trivial"
13911 " location lists.\n");
13912 *ret = NULL;
13913 return;
13916 /* LOC is constant expression. Try a luck, look it up in constant
13917 pool and return its loc_descr of its address. */
13919 static dw_loc_descr_ref
13920 cst_pool_loc_descr (tree loc)
13922 /* Get an RTL for this, if something has been emitted. */
13923 rtx rtl = lookup_constant_def (loc);
13925 if (!rtl || !MEM_P (rtl))
13927 gcc_assert (!rtl);
13928 return 0;
13930 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
13932 /* TODO: We might get more coverage if we was actually delaying expansion
13933 of all expressions till end of compilation when constant pools are fully
13934 populated. */
13935 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
13937 expansion_failed (loc, NULL_RTX,
13938 "CST value in contant pool but not marked.");
13939 return 0;
13941 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13942 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
13945 /* Return dw_loc_list representing address of addr_expr LOC
13946 by looking for inner INDIRECT_REF expression and turning
13947 it into simple arithmetics. */
13949 static dw_loc_list_ref
13950 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
13952 tree obj, offset;
13953 HOST_WIDE_INT bitsize, bitpos, bytepos;
13954 enum machine_mode mode;
13955 int unsignedp, volatilep = 0;
13956 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13958 obj = get_inner_reference (TREE_OPERAND (loc, 0),
13959 &bitsize, &bitpos, &offset, &mode,
13960 &unsignedp, &volatilep, false);
13961 STRIP_NOPS (obj);
13962 if (bitpos % BITS_PER_UNIT)
13964 expansion_failed (loc, NULL_RTX, "bitfield access");
13965 return 0;
13967 if (!INDIRECT_REF_P (obj))
13969 expansion_failed (obj,
13970 NULL_RTX, "no indirect ref in inner refrence");
13971 return 0;
13973 if (!offset && !bitpos)
13974 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
13975 else if (toplev
13976 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
13977 && (dwarf_version >= 4 || !dwarf_strict))
13979 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
13980 if (!list_ret)
13981 return 0;
13982 if (offset)
13984 /* Variable offset. */
13985 list_ret1 = loc_list_from_tree (offset, 0);
13986 if (list_ret1 == 0)
13987 return 0;
13988 add_loc_list (&list_ret, list_ret1);
13989 if (!list_ret)
13990 return 0;
13991 add_loc_descr_to_each (list_ret,
13992 new_loc_descr (DW_OP_plus, 0, 0));
13994 bytepos = bitpos / BITS_PER_UNIT;
13995 if (bytepos > 0)
13996 add_loc_descr_to_each (list_ret,
13997 new_loc_descr (DW_OP_plus_uconst,
13998 bytepos, 0));
13999 else if (bytepos < 0)
14000 loc_list_plus_const (list_ret, bytepos);
14001 add_loc_descr_to_each (list_ret,
14002 new_loc_descr (DW_OP_stack_value, 0, 0));
14004 return list_ret;
14008 /* Generate Dwarf location list representing LOC.
14009 If WANT_ADDRESS is false, expression computing LOC will be computed
14010 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
14011 if WANT_ADDRESS is 2, expression computing address useable in location
14012 will be returned (i.e. DW_OP_reg can be used
14013 to refer to register values). */
14015 static dw_loc_list_ref
14016 loc_list_from_tree (tree loc, int want_address)
14018 dw_loc_descr_ref ret = NULL, ret1 = NULL;
14019 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14020 int have_address = 0;
14021 enum dwarf_location_atom op;
14023 /* ??? Most of the time we do not take proper care for sign/zero
14024 extending the values properly. Hopefully this won't be a real
14025 problem... */
14027 switch (TREE_CODE (loc))
14029 case ERROR_MARK:
14030 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
14031 return 0;
14033 case PLACEHOLDER_EXPR:
14034 /* This case involves extracting fields from an object to determine the
14035 position of other fields. We don't try to encode this here. The
14036 only user of this is Ada, which encodes the needed information using
14037 the names of types. */
14038 expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
14039 return 0;
14041 case CALL_EXPR:
14042 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
14043 /* There are no opcodes for these operations. */
14044 return 0;
14046 case PREINCREMENT_EXPR:
14047 case PREDECREMENT_EXPR:
14048 case POSTINCREMENT_EXPR:
14049 case POSTDECREMENT_EXPR:
14050 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
14051 /* There are no opcodes for these operations. */
14052 return 0;
14054 case ADDR_EXPR:
14055 /* If we already want an address, see if there is INDIRECT_REF inside
14056 e.g. for &this->field. */
14057 if (want_address)
14059 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
14060 (loc, want_address == 2);
14061 if (list_ret)
14062 have_address = 1;
14063 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
14064 && (ret = cst_pool_loc_descr (loc)))
14065 have_address = 1;
14067 /* Otherwise, process the argument and look for the address. */
14068 if (!list_ret && !ret)
14069 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
14070 else
14072 if (want_address)
14073 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
14074 return NULL;
14076 break;
14078 case VAR_DECL:
14079 if (DECL_THREAD_LOCAL_P (loc))
14081 rtx rtl;
14082 enum dwarf_location_atom tls_op;
14083 enum dtprel_bool dtprel = dtprel_false;
14085 if (targetm.have_tls)
14087 /* If this is not defined, we have no way to emit the
14088 data. */
14089 if (!targetm.asm_out.output_dwarf_dtprel)
14090 return 0;
14092 /* The way DW_OP_GNU_push_tls_address is specified, we
14093 can only look up addresses of objects in the current
14094 module. We used DW_OP_addr as first op, but that's
14095 wrong, because DW_OP_addr is relocated by the debug
14096 info consumer, while DW_OP_GNU_push_tls_address
14097 operand shouldn't be. */
14098 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
14099 return 0;
14100 dtprel = dtprel_true;
14101 tls_op = DW_OP_GNU_push_tls_address;
14103 else
14105 if (!targetm.emutls.debug_form_tls_address
14106 || !(dwarf_version >= 3 || !dwarf_strict))
14107 return 0;
14108 /* We stuffed the control variable into the DECL_VALUE_EXPR
14109 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
14110 no longer appear in gimple code. We used the control
14111 variable in specific so that we could pick it up here. */
14112 loc = DECL_VALUE_EXPR (loc);
14113 tls_op = DW_OP_form_tls_address;
14116 rtl = rtl_for_decl_location (loc);
14117 if (rtl == NULL_RTX)
14118 return 0;
14120 if (!MEM_P (rtl))
14121 return 0;
14122 rtl = XEXP (rtl, 0);
14123 if (! CONSTANT_P (rtl))
14124 return 0;
14126 ret = new_addr_loc_descr (rtl, dtprel);
14127 ret1 = new_loc_descr (tls_op, 0, 0);
14128 add_loc_descr (&ret, ret1);
14130 have_address = 1;
14131 break;
14133 /* FALLTHRU */
14135 case PARM_DECL:
14136 case RESULT_DECL:
14137 if (DECL_HAS_VALUE_EXPR_P (loc))
14138 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
14139 want_address);
14140 /* FALLTHRU */
14142 case FUNCTION_DECL:
14144 rtx rtl;
14145 var_loc_list *loc_list = lookup_decl_loc (loc);
14147 if (loc_list && loc_list->first)
14149 list_ret = dw_loc_list (loc_list, loc, want_address);
14150 have_address = want_address != 0;
14151 break;
14153 rtl = rtl_for_decl_location (loc);
14154 if (rtl == NULL_RTX)
14156 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
14157 return 0;
14159 else if (CONST_INT_P (rtl))
14161 HOST_WIDE_INT val = INTVAL (rtl);
14162 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14163 val &= GET_MODE_MASK (DECL_MODE (loc));
14164 ret = int_loc_descriptor (val);
14166 else if (GET_CODE (rtl) == CONST_STRING)
14168 expansion_failed (loc, NULL_RTX, "CONST_STRING");
14169 return 0;
14171 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
14172 ret = new_addr_loc_descr (rtl, dtprel_false);
14173 else
14175 enum machine_mode mode, mem_mode;
14177 /* Certain constructs can only be represented at top-level. */
14178 if (want_address == 2)
14180 ret = loc_descriptor (rtl, VOIDmode,
14181 VAR_INIT_STATUS_INITIALIZED);
14182 have_address = 1;
14184 else
14186 mode = GET_MODE (rtl);
14187 mem_mode = VOIDmode;
14188 if (MEM_P (rtl))
14190 mem_mode = mode;
14191 mode = get_address_mode (rtl);
14192 rtl = XEXP (rtl, 0);
14193 have_address = 1;
14195 ret = mem_loc_descriptor (rtl, mode, mem_mode,
14196 VAR_INIT_STATUS_INITIALIZED);
14198 if (!ret)
14199 expansion_failed (loc, rtl,
14200 "failed to produce loc descriptor for rtl");
14203 break;
14205 case MEM_REF:
14206 /* ??? FIXME. */
14207 if (!integer_zerop (TREE_OPERAND (loc, 1)))
14208 return 0;
14209 /* Fallthru. */
14210 case INDIRECT_REF:
14211 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14212 have_address = 1;
14213 break;
14215 case COMPOUND_EXPR:
14216 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
14218 CASE_CONVERT:
14219 case VIEW_CONVERT_EXPR:
14220 case SAVE_EXPR:
14221 case MODIFY_EXPR:
14222 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
14224 case COMPONENT_REF:
14225 case BIT_FIELD_REF:
14226 case ARRAY_REF:
14227 case ARRAY_RANGE_REF:
14228 case REALPART_EXPR:
14229 case IMAGPART_EXPR:
14231 tree obj, offset;
14232 HOST_WIDE_INT bitsize, bitpos, bytepos;
14233 enum machine_mode mode;
14234 int unsignedp, volatilep = 0;
14236 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14237 &unsignedp, &volatilep, false);
14239 gcc_assert (obj != loc);
14241 list_ret = loc_list_from_tree (obj,
14242 want_address == 2
14243 && !bitpos && !offset ? 2 : 1);
14244 /* TODO: We can extract value of the small expression via shifting even
14245 for nonzero bitpos. */
14246 if (list_ret == 0)
14247 return 0;
14248 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14250 expansion_failed (loc, NULL_RTX,
14251 "bitfield access");
14252 return 0;
14255 if (offset != NULL_TREE)
14257 /* Variable offset. */
14258 list_ret1 = loc_list_from_tree (offset, 0);
14259 if (list_ret1 == 0)
14260 return 0;
14261 add_loc_list (&list_ret, list_ret1);
14262 if (!list_ret)
14263 return 0;
14264 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14267 bytepos = bitpos / BITS_PER_UNIT;
14268 if (bytepos > 0)
14269 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14270 else if (bytepos < 0)
14271 loc_list_plus_const (list_ret, bytepos);
14273 have_address = 1;
14274 break;
14277 case INTEGER_CST:
14278 if ((want_address || !tree_fits_shwi_p (loc))
14279 && (ret = cst_pool_loc_descr (loc)))
14280 have_address = 1;
14281 else if (want_address == 2
14282 && tree_fits_shwi_p (loc)
14283 && (ret = address_of_int_loc_descriptor
14284 (int_size_in_bytes (TREE_TYPE (loc)),
14285 tree_to_shwi (loc))))
14286 have_address = 1;
14287 else if (tree_fits_shwi_p (loc))
14288 ret = int_loc_descriptor (tree_to_shwi (loc));
14289 else
14291 expansion_failed (loc, NULL_RTX,
14292 "Integer operand is not host integer");
14293 return 0;
14295 break;
14297 case CONSTRUCTOR:
14298 case REAL_CST:
14299 case STRING_CST:
14300 case COMPLEX_CST:
14301 if ((ret = cst_pool_loc_descr (loc)))
14302 have_address = 1;
14303 else
14304 /* We can construct small constants here using int_loc_descriptor. */
14305 expansion_failed (loc, NULL_RTX,
14306 "constructor or constant not in constant pool");
14307 break;
14309 case TRUTH_AND_EXPR:
14310 case TRUTH_ANDIF_EXPR:
14311 case BIT_AND_EXPR:
14312 op = DW_OP_and;
14313 goto do_binop;
14315 case TRUTH_XOR_EXPR:
14316 case BIT_XOR_EXPR:
14317 op = DW_OP_xor;
14318 goto do_binop;
14320 case TRUTH_OR_EXPR:
14321 case TRUTH_ORIF_EXPR:
14322 case BIT_IOR_EXPR:
14323 op = DW_OP_or;
14324 goto do_binop;
14326 case FLOOR_DIV_EXPR:
14327 case CEIL_DIV_EXPR:
14328 case ROUND_DIV_EXPR:
14329 case TRUNC_DIV_EXPR:
14330 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14331 return 0;
14332 op = DW_OP_div;
14333 goto do_binop;
14335 case MINUS_EXPR:
14336 op = DW_OP_minus;
14337 goto do_binop;
14339 case FLOOR_MOD_EXPR:
14340 case CEIL_MOD_EXPR:
14341 case ROUND_MOD_EXPR:
14342 case TRUNC_MOD_EXPR:
14343 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14345 op = DW_OP_mod;
14346 goto do_binop;
14348 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14349 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14350 if (list_ret == 0 || list_ret1 == 0)
14351 return 0;
14353 add_loc_list (&list_ret, list_ret1);
14354 if (list_ret == 0)
14355 return 0;
14356 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14357 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14358 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14359 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14360 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14361 break;
14363 case MULT_EXPR:
14364 op = DW_OP_mul;
14365 goto do_binop;
14367 case LSHIFT_EXPR:
14368 op = DW_OP_shl;
14369 goto do_binop;
14371 case RSHIFT_EXPR:
14372 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14373 goto do_binop;
14375 case POINTER_PLUS_EXPR:
14376 case PLUS_EXPR:
14377 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
14379 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14380 if (list_ret == 0)
14381 return 0;
14383 loc_list_plus_const (list_ret, tree_to_shwi (TREE_OPERAND (loc, 1)));
14384 break;
14387 op = DW_OP_plus;
14388 goto do_binop;
14390 case LE_EXPR:
14391 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14392 return 0;
14394 op = DW_OP_le;
14395 goto do_binop;
14397 case GE_EXPR:
14398 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14399 return 0;
14401 op = DW_OP_ge;
14402 goto do_binop;
14404 case LT_EXPR:
14405 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14406 return 0;
14408 op = DW_OP_lt;
14409 goto do_binop;
14411 case GT_EXPR:
14412 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14413 return 0;
14415 op = DW_OP_gt;
14416 goto do_binop;
14418 case EQ_EXPR:
14419 op = DW_OP_eq;
14420 goto do_binop;
14422 case NE_EXPR:
14423 op = DW_OP_ne;
14424 goto do_binop;
14426 do_binop:
14427 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14428 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14429 if (list_ret == 0 || list_ret1 == 0)
14430 return 0;
14432 add_loc_list (&list_ret, list_ret1);
14433 if (list_ret == 0)
14434 return 0;
14435 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14436 break;
14438 case TRUTH_NOT_EXPR:
14439 case BIT_NOT_EXPR:
14440 op = DW_OP_not;
14441 goto do_unop;
14443 case ABS_EXPR:
14444 op = DW_OP_abs;
14445 goto do_unop;
14447 case NEGATE_EXPR:
14448 op = DW_OP_neg;
14449 goto do_unop;
14451 do_unop:
14452 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14453 if (list_ret == 0)
14454 return 0;
14456 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14457 break;
14459 case MIN_EXPR:
14460 case MAX_EXPR:
14462 const enum tree_code code =
14463 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14465 loc = build3 (COND_EXPR, TREE_TYPE (loc),
14466 build2 (code, integer_type_node,
14467 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14468 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
14471 /* ... fall through ... */
14473 case COND_EXPR:
14475 dw_loc_descr_ref lhs
14476 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
14477 dw_loc_list_ref rhs
14478 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
14479 dw_loc_descr_ref bra_node, jump_node, tmp;
14481 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14482 if (list_ret == 0 || lhs == 0 || rhs == 0)
14483 return 0;
14485 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14486 add_loc_descr_to_each (list_ret, bra_node);
14488 add_loc_list (&list_ret, rhs);
14489 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
14490 add_loc_descr_to_each (list_ret, jump_node);
14492 add_loc_descr_to_each (list_ret, lhs);
14493 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14494 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
14496 /* ??? Need a node to point the skip at. Use a nop. */
14497 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14498 add_loc_descr_to_each (list_ret, tmp);
14499 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14500 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
14502 break;
14504 case FIX_TRUNC_EXPR:
14505 return 0;
14507 default:
14508 /* Leave front-end specific codes as simply unknown. This comes
14509 up, for instance, with the C STMT_EXPR. */
14510 if ((unsigned int) TREE_CODE (loc)
14511 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
14513 expansion_failed (loc, NULL_RTX,
14514 "language specific tree node");
14515 return 0;
14518 #ifdef ENABLE_CHECKING
14519 /* Otherwise this is a generic code; we should just lists all of
14520 these explicitly. We forgot one. */
14521 gcc_unreachable ();
14522 #else
14523 /* In a release build, we want to degrade gracefully: better to
14524 generate incomplete debugging information than to crash. */
14525 return NULL;
14526 #endif
14529 if (!ret && !list_ret)
14530 return 0;
14532 if (want_address == 2 && !have_address
14533 && (dwarf_version >= 4 || !dwarf_strict))
14535 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14537 expansion_failed (loc, NULL_RTX,
14538 "DWARF address size mismatch");
14539 return 0;
14541 if (ret)
14542 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
14543 else
14544 add_loc_descr_to_each (list_ret,
14545 new_loc_descr (DW_OP_stack_value, 0, 0));
14546 have_address = 1;
14548 /* Show if we can't fill the request for an address. */
14549 if (want_address && !have_address)
14551 expansion_failed (loc, NULL_RTX,
14552 "Want address and only have value");
14553 return 0;
14556 gcc_assert (!ret || !list_ret);
14558 /* If we've got an address and don't want one, dereference. */
14559 if (!want_address && have_address)
14561 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14563 if (size > DWARF2_ADDR_SIZE || size == -1)
14565 expansion_failed (loc, NULL_RTX,
14566 "DWARF address size mismatch");
14567 return 0;
14569 else if (size == DWARF2_ADDR_SIZE)
14570 op = DW_OP_deref;
14571 else
14572 op = DW_OP_deref_size;
14574 if (ret)
14575 add_loc_descr (&ret, new_loc_descr (op, size, 0));
14576 else
14577 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
14579 if (ret)
14580 list_ret = new_loc_list (ret, NULL, NULL, NULL);
14582 return list_ret;
14585 /* Same as above but return only single location expression. */
14586 static dw_loc_descr_ref
14587 loc_descriptor_from_tree (tree loc, int want_address)
14589 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
14590 if (!ret)
14591 return NULL;
14592 if (ret->dw_loc_next)
14594 expansion_failed (loc, NULL_RTX,
14595 "Location list where only loc descriptor needed");
14596 return NULL;
14598 return ret->expr;
14601 /* Given a value, round it up to the lowest multiple of `boundary'
14602 which is not less than the value itself. */
14604 static inline HOST_WIDE_INT
14605 ceiling (HOST_WIDE_INT value, unsigned int boundary)
14607 return (((value + boundary - 1) / boundary) * boundary);
14610 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
14611 pointer to the declared type for the relevant field variable, or return
14612 `integer_type_node' if the given node turns out to be an
14613 ERROR_MARK node. */
14615 static inline tree
14616 field_type (const_tree decl)
14618 tree type;
14620 if (TREE_CODE (decl) == ERROR_MARK)
14621 return integer_type_node;
14623 type = DECL_BIT_FIELD_TYPE (decl);
14624 if (type == NULL_TREE)
14625 type = TREE_TYPE (decl);
14627 return type;
14630 /* Given a pointer to a tree node, return the alignment in bits for
14631 it, or else return BITS_PER_WORD if the node actually turns out to
14632 be an ERROR_MARK node. */
14634 static inline unsigned
14635 simple_type_align_in_bits (const_tree type)
14637 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
14640 static inline unsigned
14641 simple_decl_align_in_bits (const_tree decl)
14643 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
14646 /* Return the result of rounding T up to ALIGN. */
14648 static inline double_int
14649 round_up_to_align (double_int t, unsigned int align)
14651 double_int alignd = double_int::from_uhwi (align);
14652 t += alignd;
14653 t += double_int_minus_one;
14654 t = t.div (alignd, true, TRUNC_DIV_EXPR);
14655 t *= alignd;
14656 return t;
14659 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
14660 lowest addressed byte of the "containing object" for the given FIELD_DECL,
14661 or return 0 if we are unable to determine what that offset is, either
14662 because the argument turns out to be a pointer to an ERROR_MARK node, or
14663 because the offset is actually variable. (We can't handle the latter case
14664 just yet). */
14666 static HOST_WIDE_INT
14667 field_byte_offset (const_tree decl)
14669 double_int object_offset_in_bits;
14670 double_int object_offset_in_bytes;
14671 double_int bitpos_int;
14673 if (TREE_CODE (decl) == ERROR_MARK)
14674 return 0;
14676 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
14678 /* We cannot yet cope with fields whose positions are variable, so
14679 for now, when we see such things, we simply return 0. Someday, we may
14680 be able to handle such cases, but it will be damn difficult. */
14681 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
14682 return 0;
14684 bitpos_int = tree_to_double_int (bit_position (decl));
14686 #ifdef PCC_BITFIELD_TYPE_MATTERS
14687 if (PCC_BITFIELD_TYPE_MATTERS)
14689 tree type;
14690 tree field_size_tree;
14691 double_int deepest_bitpos;
14692 double_int field_size_in_bits;
14693 unsigned int type_align_in_bits;
14694 unsigned int decl_align_in_bits;
14695 double_int type_size_in_bits;
14697 type = field_type (decl);
14698 type_size_in_bits = double_int_type_size_in_bits (type);
14699 type_align_in_bits = simple_type_align_in_bits (type);
14701 field_size_tree = DECL_SIZE (decl);
14703 /* The size could be unspecified if there was an error, or for
14704 a flexible array member. */
14705 if (!field_size_tree)
14706 field_size_tree = bitsize_zero_node;
14708 /* If the size of the field is not constant, use the type size. */
14709 if (TREE_CODE (field_size_tree) == INTEGER_CST)
14710 field_size_in_bits = tree_to_double_int (field_size_tree);
14711 else
14712 field_size_in_bits = type_size_in_bits;
14714 decl_align_in_bits = simple_decl_align_in_bits (decl);
14716 /* The GCC front-end doesn't make any attempt to keep track of the
14717 starting bit offset (relative to the start of the containing
14718 structure type) of the hypothetical "containing object" for a
14719 bit-field. Thus, when computing the byte offset value for the
14720 start of the "containing object" of a bit-field, we must deduce
14721 this information on our own. This can be rather tricky to do in
14722 some cases. For example, handling the following structure type
14723 definition when compiling for an i386/i486 target (which only
14724 aligns long long's to 32-bit boundaries) can be very tricky:
14726 struct S { int field1; long long field2:31; };
14728 Fortunately, there is a simple rule-of-thumb which can be used
14729 in such cases. When compiling for an i386/i486, GCC will
14730 allocate 8 bytes for the structure shown above. It decides to
14731 do this based upon one simple rule for bit-field allocation.
14732 GCC allocates each "containing object" for each bit-field at
14733 the first (i.e. lowest addressed) legitimate alignment boundary
14734 (based upon the required minimum alignment for the declared
14735 type of the field) which it can possibly use, subject to the
14736 condition that there is still enough available space remaining
14737 in the containing object (when allocated at the selected point)
14738 to fully accommodate all of the bits of the bit-field itself.
14740 This simple rule makes it obvious why GCC allocates 8 bytes for
14741 each object of the structure type shown above. When looking
14742 for a place to allocate the "containing object" for `field2',
14743 the compiler simply tries to allocate a 64-bit "containing
14744 object" at each successive 32-bit boundary (starting at zero)
14745 until it finds a place to allocate that 64- bit field such that
14746 at least 31 contiguous (and previously unallocated) bits remain
14747 within that selected 64 bit field. (As it turns out, for the
14748 example above, the compiler finds it is OK to allocate the
14749 "containing object" 64-bit field at bit-offset zero within the
14750 structure type.)
14752 Here we attempt to work backwards from the limited set of facts
14753 we're given, and we try to deduce from those facts, where GCC
14754 must have believed that the containing object started (within
14755 the structure type). The value we deduce is then used (by the
14756 callers of this routine) to generate DW_AT_location and
14757 DW_AT_bit_offset attributes for fields (both bit-fields and, in
14758 the case of DW_AT_location, regular fields as well). */
14760 /* Figure out the bit-distance from the start of the structure to
14761 the "deepest" bit of the bit-field. */
14762 deepest_bitpos = bitpos_int + field_size_in_bits;
14764 /* This is the tricky part. Use some fancy footwork to deduce
14765 where the lowest addressed bit of the containing object must
14766 be. */
14767 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14769 /* Round up to type_align by default. This works best for
14770 bitfields. */
14771 object_offset_in_bits
14772 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
14774 if (object_offset_in_bits.ugt (bitpos_int))
14776 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14778 /* Round up to decl_align instead. */
14779 object_offset_in_bits
14780 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
14783 else
14784 #endif /* PCC_BITFIELD_TYPE_MATTERS */
14785 object_offset_in_bits = bitpos_int;
14787 object_offset_in_bytes
14788 = object_offset_in_bits.div (double_int::from_uhwi (BITS_PER_UNIT),
14789 true, TRUNC_DIV_EXPR);
14790 return object_offset_in_bytes.to_shwi ();
14793 /* The following routines define various Dwarf attributes and any data
14794 associated with them. */
14796 /* Add a location description attribute value to a DIE.
14798 This emits location attributes suitable for whole variables and
14799 whole parameters. Note that the location attributes for struct fields are
14800 generated by the routine `data_member_location_attribute' below. */
14802 static inline void
14803 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
14804 dw_loc_list_ref descr)
14806 if (descr == 0)
14807 return;
14808 if (single_element_loc_list_p (descr))
14809 add_AT_loc (die, attr_kind, descr->expr);
14810 else
14811 add_AT_loc_list (die, attr_kind, descr);
14814 /* Add DW_AT_accessibility attribute to DIE if needed. */
14816 static void
14817 add_accessibility_attribute (dw_die_ref die, tree decl)
14819 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
14820 children, otherwise the default is DW_ACCESS_public. In DWARF2
14821 the default has always been DW_ACCESS_public. */
14822 if (TREE_PROTECTED (decl))
14823 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
14824 else if (TREE_PRIVATE (decl))
14826 if (dwarf_version == 2
14827 || die->die_parent == NULL
14828 || die->die_parent->die_tag != DW_TAG_class_type)
14829 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
14831 else if (dwarf_version > 2
14832 && die->die_parent
14833 && die->die_parent->die_tag == DW_TAG_class_type)
14834 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
14837 /* Attach the specialized form of location attribute used for data members of
14838 struct and union types. In the special case of a FIELD_DECL node which
14839 represents a bit-field, the "offset" part of this special location
14840 descriptor must indicate the distance in bytes from the lowest-addressed
14841 byte of the containing struct or union type to the lowest-addressed byte of
14842 the "containing object" for the bit-field. (See the `field_byte_offset'
14843 function above).
14845 For any given bit-field, the "containing object" is a hypothetical object
14846 (of some integral or enum type) within which the given bit-field lives. The
14847 type of this hypothetical "containing object" is always the same as the
14848 declared type of the individual bit-field itself (for GCC anyway... the
14849 DWARF spec doesn't actually mandate this). Note that it is the size (in
14850 bytes) of the hypothetical "containing object" which will be given in the
14851 DW_AT_byte_size attribute for this bit-field. (See the
14852 `byte_size_attribute' function below.) It is also used when calculating the
14853 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
14854 function below.) */
14856 static void
14857 add_data_member_location_attribute (dw_die_ref die, tree decl)
14859 HOST_WIDE_INT offset;
14860 dw_loc_descr_ref loc_descr = 0;
14862 if (TREE_CODE (decl) == TREE_BINFO)
14864 /* We're working on the TAG_inheritance for a base class. */
14865 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
14867 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
14868 aren't at a fixed offset from all (sub)objects of the same
14869 type. We need to extract the appropriate offset from our
14870 vtable. The following dwarf expression means
14872 BaseAddr = ObAddr + *((*ObAddr) - Offset)
14874 This is specific to the V3 ABI, of course. */
14876 dw_loc_descr_ref tmp;
14878 /* Make a copy of the object address. */
14879 tmp = new_loc_descr (DW_OP_dup, 0, 0);
14880 add_loc_descr (&loc_descr, tmp);
14882 /* Extract the vtable address. */
14883 tmp = new_loc_descr (DW_OP_deref, 0, 0);
14884 add_loc_descr (&loc_descr, tmp);
14886 /* Calculate the address of the offset. */
14887 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
14888 gcc_assert (offset < 0);
14890 tmp = int_loc_descriptor (-offset);
14891 add_loc_descr (&loc_descr, tmp);
14892 tmp = new_loc_descr (DW_OP_minus, 0, 0);
14893 add_loc_descr (&loc_descr, tmp);
14895 /* Extract the offset. */
14896 tmp = new_loc_descr (DW_OP_deref, 0, 0);
14897 add_loc_descr (&loc_descr, tmp);
14899 /* Add it to the object address. */
14900 tmp = new_loc_descr (DW_OP_plus, 0, 0);
14901 add_loc_descr (&loc_descr, tmp);
14903 else
14904 offset = tree_to_shwi (BINFO_OFFSET (decl));
14906 else
14907 offset = field_byte_offset (decl);
14909 if (! loc_descr)
14911 if (dwarf_version > 2)
14913 /* Don't need to output a location expression, just the constant. */
14914 if (offset < 0)
14915 add_AT_int (die, DW_AT_data_member_location, offset);
14916 else
14917 add_AT_unsigned (die, DW_AT_data_member_location, offset);
14918 return;
14920 else
14922 enum dwarf_location_atom op;
14924 /* The DWARF2 standard says that we should assume that the structure
14925 address is already on the stack, so we can specify a structure
14926 field address by using DW_OP_plus_uconst. */
14927 op = DW_OP_plus_uconst;
14928 loc_descr = new_loc_descr (op, offset, 0);
14932 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
14935 /* Writes integer values to dw_vec_const array. */
14937 static void
14938 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
14940 while (size != 0)
14942 *dest++ = val & 0xff;
14943 val >>= 8;
14944 --size;
14948 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
14950 static HOST_WIDE_INT
14951 extract_int (const unsigned char *src, unsigned int size)
14953 HOST_WIDE_INT val = 0;
14955 src += size;
14956 while (size != 0)
14958 val <<= 8;
14959 val |= *--src & 0xff;
14960 --size;
14962 return val;
14965 /* Writes double_int values to dw_vec_const array. */
14967 static void
14968 insert_double (double_int val, unsigned char *dest)
14970 unsigned char *p0 = dest;
14971 unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
14973 if (WORDS_BIG_ENDIAN)
14975 p0 = p1;
14976 p1 = dest;
14979 insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
14980 insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
14983 /* Writes floating point values to dw_vec_const array. */
14985 static void
14986 insert_float (const_rtx rtl, unsigned char *array)
14988 REAL_VALUE_TYPE rv;
14989 long val[4];
14990 int i;
14992 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
14993 real_to_target (val, &rv, GET_MODE (rtl));
14995 /* real_to_target puts 32-bit pieces in each long. Pack them. */
14996 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
14998 insert_int (val[i], 4, array);
14999 array += 4;
15003 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
15004 does not have a "location" either in memory or in a register. These
15005 things can arise in GNU C when a constant is passed as an actual parameter
15006 to an inlined function. They can also arise in C++ where declared
15007 constants do not necessarily get memory "homes". */
15009 static bool
15010 add_const_value_attribute (dw_die_ref die, rtx rtl)
15012 switch (GET_CODE (rtl))
15014 case CONST_INT:
15016 HOST_WIDE_INT val = INTVAL (rtl);
15018 if (val < 0)
15019 add_AT_int (die, DW_AT_const_value, val);
15020 else
15021 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
15023 return true;
15025 case CONST_DOUBLE:
15026 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
15027 floating-point constant. A CONST_DOUBLE is used whenever the
15028 constant requires more than one word in order to be adequately
15029 represented. */
15031 enum machine_mode mode = GET_MODE (rtl);
15033 if (SCALAR_FLOAT_MODE_P (mode))
15035 unsigned int length = GET_MODE_SIZE (mode);
15036 unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
15038 insert_float (rtl, array);
15039 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
15041 else
15042 add_AT_double (die, DW_AT_const_value,
15043 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
15045 return true;
15047 case CONST_VECTOR:
15049 enum machine_mode mode = GET_MODE (rtl);
15050 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
15051 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15052 unsigned char *array = (unsigned char *) ggc_alloc_atomic
15053 (length * elt_size);
15054 unsigned int i;
15055 unsigned char *p;
15057 switch (GET_MODE_CLASS (mode))
15059 case MODE_VECTOR_INT:
15060 for (i = 0, p = array; i < length; i++, p += elt_size)
15062 rtx elt = CONST_VECTOR_ELT (rtl, i);
15063 double_int val = rtx_to_double_int (elt);
15065 if (elt_size <= sizeof (HOST_WIDE_INT))
15066 insert_int (val.to_shwi (), elt_size, p);
15067 else
15069 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
15070 insert_double (val, p);
15073 break;
15075 case MODE_VECTOR_FLOAT:
15076 for (i = 0, p = array; i < length; i++, p += elt_size)
15078 rtx elt = CONST_VECTOR_ELT (rtl, i);
15079 insert_float (elt, p);
15081 break;
15083 default:
15084 gcc_unreachable ();
15087 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
15089 return true;
15091 case CONST_STRING:
15092 if (dwarf_version >= 4 || !dwarf_strict)
15094 dw_loc_descr_ref loc_result;
15095 resolve_one_addr (&rtl, NULL);
15096 rtl_addr:
15097 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15098 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15099 add_AT_loc (die, DW_AT_location, loc_result);
15100 vec_safe_push (used_rtx_array, rtl);
15101 return true;
15103 return false;
15105 case CONST:
15106 if (CONSTANT_P (XEXP (rtl, 0)))
15107 return add_const_value_attribute (die, XEXP (rtl, 0));
15108 /* FALLTHROUGH */
15109 case SYMBOL_REF:
15110 if (!const_ok_for_output (rtl))
15111 return false;
15112 case LABEL_REF:
15113 if (dwarf_version >= 4 || !dwarf_strict)
15114 goto rtl_addr;
15115 return false;
15117 case PLUS:
15118 /* In cases where an inlined instance of an inline function is passed
15119 the address of an `auto' variable (which is local to the caller) we
15120 can get a situation where the DECL_RTL of the artificial local
15121 variable (for the inlining) which acts as a stand-in for the
15122 corresponding formal parameter (of the inline function) will look
15123 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
15124 exactly a compile-time constant expression, but it isn't the address
15125 of the (artificial) local variable either. Rather, it represents the
15126 *value* which the artificial local variable always has during its
15127 lifetime. We currently have no way to represent such quasi-constant
15128 values in Dwarf, so for now we just punt and generate nothing. */
15129 return false;
15131 case HIGH:
15132 case CONST_FIXED:
15133 return false;
15135 case MEM:
15136 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
15137 && MEM_READONLY_P (rtl)
15138 && GET_MODE (rtl) == BLKmode)
15140 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
15141 return true;
15143 return false;
15145 default:
15146 /* No other kinds of rtx should be possible here. */
15147 gcc_unreachable ();
15149 return false;
15152 /* Determine whether the evaluation of EXPR references any variables
15153 or functions which aren't otherwise used (and therefore may not be
15154 output). */
15155 static tree
15156 reference_to_unused (tree * tp, int * walk_subtrees,
15157 void * data ATTRIBUTE_UNUSED)
15159 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
15160 *walk_subtrees = 0;
15162 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
15163 && ! TREE_ASM_WRITTEN (*tp))
15164 return *tp;
15165 /* ??? The C++ FE emits debug information for using decls, so
15166 putting gcc_unreachable here falls over. See PR31899. For now
15167 be conservative. */
15168 else if (!cgraph_global_info_ready
15169 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
15170 return *tp;
15171 else if (TREE_CODE (*tp) == VAR_DECL)
15173 varpool_node *node = varpool_get_node (*tp);
15174 if (!node || !node->definition)
15175 return *tp;
15177 else if (TREE_CODE (*tp) == FUNCTION_DECL
15178 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
15180 /* The call graph machinery must have finished analyzing,
15181 optimizing and gimplifying the CU by now.
15182 So if *TP has no call graph node associated
15183 to it, it means *TP will not be emitted. */
15184 if (!cgraph_get_node (*tp))
15185 return *tp;
15187 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
15188 return *tp;
15190 return NULL_TREE;
15193 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
15194 for use in a later add_const_value_attribute call. */
15196 static rtx
15197 rtl_for_decl_init (tree init, tree type)
15199 rtx rtl = NULL_RTX;
15201 STRIP_NOPS (init);
15203 /* If a variable is initialized with a string constant without embedded
15204 zeros, build CONST_STRING. */
15205 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
15207 tree enttype = TREE_TYPE (type);
15208 tree domain = TYPE_DOMAIN (type);
15209 enum machine_mode mode = TYPE_MODE (enttype);
15211 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
15212 && domain
15213 && integer_zerop (TYPE_MIN_VALUE (domain))
15214 && compare_tree_int (TYPE_MAX_VALUE (domain),
15215 TREE_STRING_LENGTH (init) - 1) == 0
15216 && ((size_t) TREE_STRING_LENGTH (init)
15217 == strlen (TREE_STRING_POINTER (init)) + 1))
15219 rtl = gen_rtx_CONST_STRING (VOIDmode,
15220 ggc_strdup (TREE_STRING_POINTER (init)));
15221 rtl = gen_rtx_MEM (BLKmode, rtl);
15222 MEM_READONLY_P (rtl) = 1;
15225 /* Other aggregates, and complex values, could be represented using
15226 CONCAT: FIXME! */
15227 else if (AGGREGATE_TYPE_P (type)
15228 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
15229 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
15230 || TREE_CODE (type) == COMPLEX_TYPE)
15232 /* Vectors only work if their mode is supported by the target.
15233 FIXME: generic vectors ought to work too. */
15234 else if (TREE_CODE (type) == VECTOR_TYPE
15235 && !VECTOR_MODE_P (TYPE_MODE (type)))
15237 /* If the initializer is something that we know will expand into an
15238 immediate RTL constant, expand it now. We must be careful not to
15239 reference variables which won't be output. */
15240 else if (initializer_constant_valid_p (init, type)
15241 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15243 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15244 possible. */
15245 if (TREE_CODE (type) == VECTOR_TYPE)
15246 switch (TREE_CODE (init))
15248 case VECTOR_CST:
15249 break;
15250 case CONSTRUCTOR:
15251 if (TREE_CONSTANT (init))
15253 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
15254 bool constant_p = true;
15255 tree value;
15256 unsigned HOST_WIDE_INT ix;
15258 /* Even when ctor is constant, it might contain non-*_CST
15259 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15260 belong into VECTOR_CST nodes. */
15261 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15262 if (!CONSTANT_CLASS_P (value))
15264 constant_p = false;
15265 break;
15268 if (constant_p)
15270 init = build_vector_from_ctor (type, elts);
15271 break;
15274 /* FALLTHRU */
15276 default:
15277 return NULL;
15280 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15282 /* If expand_expr returns a MEM, it wasn't immediate. */
15283 gcc_assert (!rtl || !MEM_P (rtl));
15286 return rtl;
15289 /* Generate RTL for the variable DECL to represent its location. */
15291 static rtx
15292 rtl_for_decl_location (tree decl)
15294 rtx rtl;
15296 /* Here we have to decide where we are going to say the parameter "lives"
15297 (as far as the debugger is concerned). We only have a couple of
15298 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15300 DECL_RTL normally indicates where the parameter lives during most of the
15301 activation of the function. If optimization is enabled however, this
15302 could be either NULL or else a pseudo-reg. Both of those cases indicate
15303 that the parameter doesn't really live anywhere (as far as the code
15304 generation parts of GCC are concerned) during most of the function's
15305 activation. That will happen (for example) if the parameter is never
15306 referenced within the function.
15308 We could just generate a location descriptor here for all non-NULL
15309 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15310 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15311 where DECL_RTL is NULL or is a pseudo-reg.
15313 Note however that we can only get away with using DECL_INCOMING_RTL as
15314 a backup substitute for DECL_RTL in certain limited cases. In cases
15315 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15316 we can be sure that the parameter was passed using the same type as it is
15317 declared to have within the function, and that its DECL_INCOMING_RTL
15318 points us to a place where a value of that type is passed.
15320 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15321 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15322 because in these cases DECL_INCOMING_RTL points us to a value of some
15323 type which is *different* from the type of the parameter itself. Thus,
15324 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15325 such cases, the debugger would end up (for example) trying to fetch a
15326 `float' from a place which actually contains the first part of a
15327 `double'. That would lead to really incorrect and confusing
15328 output at debug-time.
15330 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15331 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
15332 are a couple of exceptions however. On little-endian machines we can
15333 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15334 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15335 an integral type that is smaller than TREE_TYPE (decl). These cases arise
15336 when (on a little-endian machine) a non-prototyped function has a
15337 parameter declared to be of type `short' or `char'. In such cases,
15338 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15339 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15340 passed `int' value. If the debugger then uses that address to fetch
15341 a `short' or a `char' (on a little-endian machine) the result will be
15342 the correct data, so we allow for such exceptional cases below.
15344 Note that our goal here is to describe the place where the given formal
15345 parameter lives during most of the function's activation (i.e. between the
15346 end of the prologue and the start of the epilogue). We'll do that as best
15347 as we can. Note however that if the given formal parameter is modified
15348 sometime during the execution of the function, then a stack backtrace (at
15349 debug-time) will show the function as having been called with the *new*
15350 value rather than the value which was originally passed in. This happens
15351 rarely enough that it is not a major problem, but it *is* a problem, and
15352 I'd like to fix it.
15354 A future version of dwarf2out.c may generate two additional attributes for
15355 any given DW_TAG_formal_parameter DIE which will describe the "passed
15356 type" and the "passed location" for the given formal parameter in addition
15357 to the attributes we now generate to indicate the "declared type" and the
15358 "active location" for each parameter. This additional set of attributes
15359 could be used by debuggers for stack backtraces. Separately, note that
15360 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15361 This happens (for example) for inlined-instances of inline function formal
15362 parameters which are never referenced. This really shouldn't be
15363 happening. All PARM_DECL nodes should get valid non-NULL
15364 DECL_INCOMING_RTL values. FIXME. */
15366 /* Use DECL_RTL as the "location" unless we find something better. */
15367 rtl = DECL_RTL_IF_SET (decl);
15369 /* When generating abstract instances, ignore everything except
15370 constants, symbols living in memory, and symbols living in
15371 fixed registers. */
15372 if (! reload_completed)
15374 if (rtl
15375 && (CONSTANT_P (rtl)
15376 || (MEM_P (rtl)
15377 && CONSTANT_P (XEXP (rtl, 0)))
15378 || (REG_P (rtl)
15379 && TREE_CODE (decl) == VAR_DECL
15380 && TREE_STATIC (decl))))
15382 rtl = targetm.delegitimize_address (rtl);
15383 return rtl;
15385 rtl = NULL_RTX;
15387 else if (TREE_CODE (decl) == PARM_DECL)
15389 if (rtl == NULL_RTX
15390 || is_pseudo_reg (rtl)
15391 || (MEM_P (rtl)
15392 && is_pseudo_reg (XEXP (rtl, 0))
15393 && DECL_INCOMING_RTL (decl)
15394 && MEM_P (DECL_INCOMING_RTL (decl))
15395 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15397 tree declared_type = TREE_TYPE (decl);
15398 tree passed_type = DECL_ARG_TYPE (decl);
15399 enum machine_mode dmode = TYPE_MODE (declared_type);
15400 enum machine_mode pmode = TYPE_MODE (passed_type);
15402 /* This decl represents a formal parameter which was optimized out.
15403 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15404 all cases where (rtl == NULL_RTX) just below. */
15405 if (dmode == pmode)
15406 rtl = DECL_INCOMING_RTL (decl);
15407 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15408 && SCALAR_INT_MODE_P (dmode)
15409 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15410 && DECL_INCOMING_RTL (decl))
15412 rtx inc = DECL_INCOMING_RTL (decl);
15413 if (REG_P (inc))
15414 rtl = inc;
15415 else if (MEM_P (inc))
15417 if (BYTES_BIG_ENDIAN)
15418 rtl = adjust_address_nv (inc, dmode,
15419 GET_MODE_SIZE (pmode)
15420 - GET_MODE_SIZE (dmode));
15421 else
15422 rtl = inc;
15427 /* If the parm was passed in registers, but lives on the stack, then
15428 make a big endian correction if the mode of the type of the
15429 parameter is not the same as the mode of the rtl. */
15430 /* ??? This is the same series of checks that are made in dbxout.c before
15431 we reach the big endian correction code there. It isn't clear if all
15432 of these checks are necessary here, but keeping them all is the safe
15433 thing to do. */
15434 else if (MEM_P (rtl)
15435 && XEXP (rtl, 0) != const0_rtx
15436 && ! CONSTANT_P (XEXP (rtl, 0))
15437 /* Not passed in memory. */
15438 && !MEM_P (DECL_INCOMING_RTL (decl))
15439 /* Not passed by invisible reference. */
15440 && (!REG_P (XEXP (rtl, 0))
15441 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15442 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15443 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15444 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15445 #endif
15447 /* Big endian correction check. */
15448 && BYTES_BIG_ENDIAN
15449 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15450 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15451 < UNITS_PER_WORD))
15453 enum machine_mode addr_mode = get_address_mode (rtl);
15454 int offset = (UNITS_PER_WORD
15455 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15457 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15458 plus_constant (addr_mode, XEXP (rtl, 0), offset));
15461 else if (TREE_CODE (decl) == VAR_DECL
15462 && rtl
15463 && MEM_P (rtl)
15464 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
15465 && BYTES_BIG_ENDIAN)
15467 enum machine_mode addr_mode = get_address_mode (rtl);
15468 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
15469 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
15471 /* If a variable is declared "register" yet is smaller than
15472 a register, then if we store the variable to memory, it
15473 looks like we're storing a register-sized value, when in
15474 fact we are not. We need to adjust the offset of the
15475 storage location to reflect the actual value's bytes,
15476 else gdb will not be able to display it. */
15477 if (rsize > dsize)
15478 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15479 plus_constant (addr_mode, XEXP (rtl, 0),
15480 rsize - dsize));
15483 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
15484 and will have been substituted directly into all expressions that use it.
15485 C does not have such a concept, but C++ and other languages do. */
15486 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
15487 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
15489 if (rtl)
15490 rtl = targetm.delegitimize_address (rtl);
15492 /* If we don't look past the constant pool, we risk emitting a
15493 reference to a constant pool entry that isn't referenced from
15494 code, and thus is not emitted. */
15495 if (rtl)
15496 rtl = avoid_constant_pool_reference (rtl);
15498 /* Try harder to get a rtl. If this symbol ends up not being emitted
15499 in the current CU, resolve_addr will remove the expression referencing
15500 it. */
15501 if (rtl == NULL_RTX
15502 && TREE_CODE (decl) == VAR_DECL
15503 && !DECL_EXTERNAL (decl)
15504 && TREE_STATIC (decl)
15505 && DECL_NAME (decl)
15506 && !DECL_HARD_REGISTER (decl)
15507 && DECL_MODE (decl) != VOIDmode)
15509 rtl = make_decl_rtl_for_debug (decl);
15510 if (!MEM_P (rtl)
15511 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
15512 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
15513 rtl = NULL_RTX;
15516 return rtl;
15519 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
15520 returned. If so, the decl for the COMMON block is returned, and the
15521 value is the offset into the common block for the symbol. */
15523 static tree
15524 fortran_common (tree decl, HOST_WIDE_INT *value)
15526 tree val_expr, cvar;
15527 enum machine_mode mode;
15528 HOST_WIDE_INT bitsize, bitpos;
15529 tree offset;
15530 int unsignedp, volatilep = 0;
15532 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
15533 it does not have a value (the offset into the common area), or if it
15534 is thread local (as opposed to global) then it isn't common, and shouldn't
15535 be handled as such. */
15536 if (TREE_CODE (decl) != VAR_DECL
15537 || !TREE_STATIC (decl)
15538 || !DECL_HAS_VALUE_EXPR_P (decl)
15539 || !is_fortran ())
15540 return NULL_TREE;
15542 val_expr = DECL_VALUE_EXPR (decl);
15543 if (TREE_CODE (val_expr) != COMPONENT_REF)
15544 return NULL_TREE;
15546 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
15547 &mode, &unsignedp, &volatilep, true);
15549 if (cvar == NULL_TREE
15550 || TREE_CODE (cvar) != VAR_DECL
15551 || DECL_ARTIFICIAL (cvar)
15552 || !TREE_PUBLIC (cvar))
15553 return NULL_TREE;
15555 *value = 0;
15556 if (offset != NULL)
15558 if (!tree_fits_shwi_p (offset))
15559 return NULL_TREE;
15560 *value = tree_to_shwi (offset);
15562 if (bitpos != 0)
15563 *value += bitpos / BITS_PER_UNIT;
15565 return cvar;
15568 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
15569 data attribute for a variable or a parameter. We generate the
15570 DW_AT_const_value attribute only in those cases where the given variable
15571 or parameter does not have a true "location" either in memory or in a
15572 register. This can happen (for example) when a constant is passed as an
15573 actual argument in a call to an inline function. (It's possible that
15574 these things can crop up in other ways also.) Note that one type of
15575 constant value which can be passed into an inlined function is a constant
15576 pointer. This can happen for example if an actual argument in an inlined
15577 function call evaluates to a compile-time constant address.
15579 CACHE_P is true if it is worth caching the location list for DECL,
15580 so that future calls can reuse it rather than regenerate it from scratch.
15581 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
15582 since we will need to refer to them each time the function is inlined. */
15584 static bool
15585 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
15586 enum dwarf_attribute attr)
15588 rtx rtl;
15589 dw_loc_list_ref list;
15590 var_loc_list *loc_list;
15591 cached_dw_loc_list *cache;
15592 void **slot;
15594 if (TREE_CODE (decl) == ERROR_MARK)
15595 return false;
15597 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
15598 || TREE_CODE (decl) == RESULT_DECL);
15600 /* Try to get some constant RTL for this decl, and use that as the value of
15601 the location. */
15603 rtl = rtl_for_decl_location (decl);
15604 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15605 && add_const_value_attribute (die, rtl))
15606 return true;
15608 /* See if we have single element location list that is equivalent to
15609 a constant value. That way we are better to use add_const_value_attribute
15610 rather than expanding constant value equivalent. */
15611 loc_list = lookup_decl_loc (decl);
15612 if (loc_list
15613 && loc_list->first
15614 && loc_list->first->next == NULL
15615 && NOTE_P (loc_list->first->loc)
15616 && NOTE_VAR_LOCATION (loc_list->first->loc)
15617 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
15619 struct var_loc_node *node;
15621 node = loc_list->first;
15622 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
15623 if (GET_CODE (rtl) == EXPR_LIST)
15624 rtl = XEXP (rtl, 0);
15625 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15626 && add_const_value_attribute (die, rtl))
15627 return true;
15629 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
15630 list several times. See if we've already cached the contents. */
15631 list = NULL;
15632 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
15633 cache_p = false;
15634 if (cache_p)
15636 cache = (cached_dw_loc_list *)
15637 htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
15638 if (cache)
15639 list = cache->loc_list;
15641 if (list == NULL)
15643 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
15644 /* It is usually worth caching this result if the decl is from
15645 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
15646 if (cache_p && list && list->dw_loc_next)
15648 slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
15649 DECL_UID (decl), INSERT);
15650 cache = ggc_alloc_cleared_cached_dw_loc_list ();
15651 cache->decl_id = DECL_UID (decl);
15652 cache->loc_list = list;
15653 *slot = cache;
15656 if (list)
15658 add_AT_location_description (die, attr, list);
15659 return true;
15661 /* None of that worked, so it must not really have a location;
15662 try adding a constant value attribute from the DECL_INITIAL. */
15663 return tree_add_const_value_attribute_for_decl (die, decl);
15666 /* Add VARIABLE and DIE into deferred locations list. */
15668 static void
15669 defer_location (tree variable, dw_die_ref die)
15671 deferred_locations entry;
15672 entry.variable = variable;
15673 entry.die = die;
15674 vec_safe_push (deferred_locations_list, entry);
15677 /* Helper function for tree_add_const_value_attribute. Natively encode
15678 initializer INIT into an array. Return true if successful. */
15680 static bool
15681 native_encode_initializer (tree init, unsigned char *array, int size)
15683 tree type;
15685 if (init == NULL_TREE)
15686 return false;
15688 STRIP_NOPS (init);
15689 switch (TREE_CODE (init))
15691 case STRING_CST:
15692 type = TREE_TYPE (init);
15693 if (TREE_CODE (type) == ARRAY_TYPE)
15695 tree enttype = TREE_TYPE (type);
15696 enum machine_mode mode = TYPE_MODE (enttype);
15698 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
15699 return false;
15700 if (int_size_in_bytes (type) != size)
15701 return false;
15702 if (size > TREE_STRING_LENGTH (init))
15704 memcpy (array, TREE_STRING_POINTER (init),
15705 TREE_STRING_LENGTH (init));
15706 memset (array + TREE_STRING_LENGTH (init),
15707 '\0', size - TREE_STRING_LENGTH (init));
15709 else
15710 memcpy (array, TREE_STRING_POINTER (init), size);
15711 return true;
15713 return false;
15714 case CONSTRUCTOR:
15715 type = TREE_TYPE (init);
15716 if (int_size_in_bytes (type) != size)
15717 return false;
15718 if (TREE_CODE (type) == ARRAY_TYPE)
15720 HOST_WIDE_INT min_index;
15721 unsigned HOST_WIDE_INT cnt;
15722 int curpos = 0, fieldsize;
15723 constructor_elt *ce;
15725 if (TYPE_DOMAIN (type) == NULL_TREE
15726 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
15727 return false;
15729 fieldsize = int_size_in_bytes (TREE_TYPE (type));
15730 if (fieldsize <= 0)
15731 return false;
15733 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
15734 memset (array, '\0', size);
15735 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15737 tree val = ce->value;
15738 tree index = ce->index;
15739 int pos = curpos;
15740 if (index && TREE_CODE (index) == RANGE_EXPR)
15741 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
15742 * fieldsize;
15743 else if (index)
15744 pos = (tree_to_shwi (index) - min_index) * fieldsize;
15746 if (val)
15748 STRIP_NOPS (val);
15749 if (!native_encode_initializer (val, array + pos, fieldsize))
15750 return false;
15752 curpos = pos + fieldsize;
15753 if (index && TREE_CODE (index) == RANGE_EXPR)
15755 int count = tree_to_shwi (TREE_OPERAND (index, 1))
15756 - tree_to_shwi (TREE_OPERAND (index, 0));
15757 while (count-- > 0)
15759 if (val)
15760 memcpy (array + curpos, array + pos, fieldsize);
15761 curpos += fieldsize;
15764 gcc_assert (curpos <= size);
15766 return true;
15768 else if (TREE_CODE (type) == RECORD_TYPE
15769 || TREE_CODE (type) == UNION_TYPE)
15771 tree field = NULL_TREE;
15772 unsigned HOST_WIDE_INT cnt;
15773 constructor_elt *ce;
15775 if (int_size_in_bytes (type) != size)
15776 return false;
15778 if (TREE_CODE (type) == RECORD_TYPE)
15779 field = TYPE_FIELDS (type);
15781 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15783 tree val = ce->value;
15784 int pos, fieldsize;
15786 if (ce->index != 0)
15787 field = ce->index;
15789 if (val)
15790 STRIP_NOPS (val);
15792 if (field == NULL_TREE || DECL_BIT_FIELD (field))
15793 return false;
15795 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
15796 && TYPE_DOMAIN (TREE_TYPE (field))
15797 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
15798 return false;
15799 else if (DECL_SIZE_UNIT (field) == NULL_TREE
15800 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
15801 return false;
15802 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
15803 pos = int_byte_position (field);
15804 gcc_assert (pos + fieldsize <= size);
15805 if (val
15806 && !native_encode_initializer (val, array + pos, fieldsize))
15807 return false;
15809 return true;
15811 return false;
15812 case VIEW_CONVERT_EXPR:
15813 case NON_LVALUE_EXPR:
15814 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
15815 default:
15816 return native_encode_expr (init, array, size) == size;
15820 /* Attach a DW_AT_const_value attribute to DIE. The value of the
15821 attribute is the const value T. */
15823 static bool
15824 tree_add_const_value_attribute (dw_die_ref die, tree t)
15826 tree init;
15827 tree type = TREE_TYPE (t);
15828 rtx rtl;
15830 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
15831 return false;
15833 init = t;
15834 gcc_assert (!DECL_P (init));
15836 rtl = rtl_for_decl_init (init, type);
15837 if (rtl)
15838 return add_const_value_attribute (die, rtl);
15839 /* If the host and target are sane, try harder. */
15840 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
15841 && initializer_constant_valid_p (init, type))
15843 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
15844 if (size > 0 && (int) size == size)
15846 unsigned char *array = (unsigned char *)
15847 ggc_alloc_cleared_atomic (size);
15849 if (native_encode_initializer (init, array, size))
15851 add_AT_vec (die, DW_AT_const_value, size, 1, array);
15852 return true;
15854 ggc_free (array);
15857 return false;
15860 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
15861 attribute is the const value of T, where T is an integral constant
15862 variable with static storage duration
15863 (so it can't be a PARM_DECL or a RESULT_DECL). */
15865 static bool
15866 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
15869 if (!decl
15870 || (TREE_CODE (decl) != VAR_DECL
15871 && TREE_CODE (decl) != CONST_DECL)
15872 || (TREE_CODE (decl) == VAR_DECL
15873 && !TREE_STATIC (decl)))
15874 return false;
15876 if (TREE_READONLY (decl)
15877 && ! TREE_THIS_VOLATILE (decl)
15878 && DECL_INITIAL (decl))
15879 /* OK */;
15880 else
15881 return false;
15883 /* Don't add DW_AT_const_value if abstract origin already has one. */
15884 if (get_AT (var_die, DW_AT_const_value))
15885 return false;
15887 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
15890 /* Convert the CFI instructions for the current function into a
15891 location list. This is used for DW_AT_frame_base when we targeting
15892 a dwarf2 consumer that does not support the dwarf3
15893 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
15894 expressions. */
15896 static dw_loc_list_ref
15897 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
15899 int ix;
15900 dw_fde_ref fde;
15901 dw_loc_list_ref list, *list_tail;
15902 dw_cfi_ref cfi;
15903 dw_cfa_location last_cfa, next_cfa;
15904 const char *start_label, *last_label, *section;
15905 dw_cfa_location remember;
15907 fde = cfun->fde;
15908 gcc_assert (fde != NULL);
15910 section = secname_for_decl (current_function_decl);
15911 list_tail = &list;
15912 list = NULL;
15914 memset (&next_cfa, 0, sizeof (next_cfa));
15915 next_cfa.reg = INVALID_REGNUM;
15916 remember = next_cfa;
15918 start_label = fde->dw_fde_begin;
15920 /* ??? Bald assumption that the CIE opcode list does not contain
15921 advance opcodes. */
15922 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
15923 lookup_cfa_1 (cfi, &next_cfa, &remember);
15925 last_cfa = next_cfa;
15926 last_label = start_label;
15928 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
15930 /* If the first partition contained no CFI adjustments, the
15931 CIE opcodes apply to the whole first partition. */
15932 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15933 fde->dw_fde_begin, fde->dw_fde_end, section);
15934 list_tail =&(*list_tail)->dw_loc_next;
15935 start_label = last_label = fde->dw_fde_second_begin;
15938 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
15940 switch (cfi->dw_cfi_opc)
15942 case DW_CFA_set_loc:
15943 case DW_CFA_advance_loc1:
15944 case DW_CFA_advance_loc2:
15945 case DW_CFA_advance_loc4:
15946 if (!cfa_equal_p (&last_cfa, &next_cfa))
15948 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15949 start_label, last_label, section);
15951 list_tail = &(*list_tail)->dw_loc_next;
15952 last_cfa = next_cfa;
15953 start_label = last_label;
15955 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
15956 break;
15958 case DW_CFA_advance_loc:
15959 /* The encoding is complex enough that we should never emit this. */
15960 gcc_unreachable ();
15962 default:
15963 lookup_cfa_1 (cfi, &next_cfa, &remember);
15964 break;
15966 if (ix + 1 == fde->dw_fde_switch_cfi_index)
15968 if (!cfa_equal_p (&last_cfa, &next_cfa))
15970 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15971 start_label, last_label, section);
15973 list_tail = &(*list_tail)->dw_loc_next;
15974 last_cfa = next_cfa;
15975 start_label = last_label;
15977 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15978 start_label, fde->dw_fde_end, section);
15979 list_tail = &(*list_tail)->dw_loc_next;
15980 start_label = last_label = fde->dw_fde_second_begin;
15984 if (!cfa_equal_p (&last_cfa, &next_cfa))
15986 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15987 start_label, last_label, section);
15988 list_tail = &(*list_tail)->dw_loc_next;
15989 start_label = last_label;
15992 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
15993 start_label,
15994 fde->dw_fde_second_begin
15995 ? fde->dw_fde_second_end : fde->dw_fde_end,
15996 section);
15998 if (list && list->dw_loc_next)
15999 gen_llsym (list);
16001 return list;
16004 /* Compute a displacement from the "steady-state frame pointer" to the
16005 frame base (often the same as the CFA), and store it in
16006 frame_pointer_fb_offset. OFFSET is added to the displacement
16007 before the latter is negated. */
16009 static void
16010 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
16012 rtx reg, elim;
16014 #ifdef FRAME_POINTER_CFA_OFFSET
16015 reg = frame_pointer_rtx;
16016 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
16017 #else
16018 reg = arg_pointer_rtx;
16019 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
16020 #endif
16022 elim = (ira_use_lra_p
16023 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
16024 : eliminate_regs (reg, VOIDmode, NULL_RTX));
16025 if (GET_CODE (elim) == PLUS)
16027 offset += INTVAL (XEXP (elim, 1));
16028 elim = XEXP (elim, 0);
16031 frame_pointer_fb_offset = -offset;
16033 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
16034 in which to eliminate. This is because it's stack pointer isn't
16035 directly accessible as a register within the ISA. To work around
16036 this, assume that while we cannot provide a proper value for
16037 frame_pointer_fb_offset, we won't need one either. */
16038 frame_pointer_fb_offset_valid
16039 = ((SUPPORTS_STACK_ALIGNMENT
16040 && (elim == hard_frame_pointer_rtx
16041 || elim == stack_pointer_rtx))
16042 || elim == (frame_pointer_needed
16043 ? hard_frame_pointer_rtx
16044 : stack_pointer_rtx));
16047 /* Generate a DW_AT_name attribute given some string value to be included as
16048 the value of the attribute. */
16050 static void
16051 add_name_attribute (dw_die_ref die, const char *name_string)
16053 if (name_string != NULL && *name_string != 0)
16055 if (demangle_name_func)
16056 name_string = (*demangle_name_func) (name_string);
16058 add_AT_string (die, DW_AT_name, name_string);
16062 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
16063 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
16064 of TYPE accordingly.
16066 ??? This is a temporary measure until after we're able to generate
16067 regular DWARF for the complex Ada type system. */
16069 static void
16070 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
16071 dw_die_ref context_die)
16073 tree dtype;
16074 dw_die_ref dtype_die;
16076 if (!lang_hooks.types.descriptive_type)
16077 return;
16079 dtype = lang_hooks.types.descriptive_type (type);
16080 if (!dtype)
16081 return;
16083 dtype_die = lookup_type_die (dtype);
16084 if (!dtype_die)
16086 gen_type_die (dtype, context_die);
16087 dtype_die = lookup_type_die (dtype);
16088 gcc_assert (dtype_die);
16091 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
16094 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
16096 static const char *
16097 comp_dir_string (void)
16099 const char *wd;
16100 char *wd1;
16101 static const char *cached_wd = NULL;
16103 if (cached_wd != NULL)
16104 return cached_wd;
16106 wd = get_src_pwd ();
16107 if (wd == NULL)
16108 return NULL;
16110 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
16112 int wdlen;
16114 wdlen = strlen (wd);
16115 wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
16116 strcpy (wd1, wd);
16117 wd1 [wdlen] = DIR_SEPARATOR;
16118 wd1 [wdlen + 1] = 0;
16119 wd = wd1;
16122 cached_wd = remap_debug_filename (wd);
16123 return cached_wd;
16126 /* Generate a DW_AT_comp_dir attribute for DIE. */
16128 static void
16129 add_comp_dir_attribute (dw_die_ref die)
16131 const char * wd = comp_dir_string ();
16132 if (wd != NULL)
16133 add_AT_string (die, DW_AT_comp_dir, wd);
16136 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
16137 default. */
16139 static int
16140 lower_bound_default (void)
16142 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16144 case DW_LANG_C:
16145 case DW_LANG_C89:
16146 case DW_LANG_C99:
16147 case DW_LANG_C_plus_plus:
16148 case DW_LANG_ObjC:
16149 case DW_LANG_ObjC_plus_plus:
16150 case DW_LANG_Java:
16151 return 0;
16152 case DW_LANG_Fortran77:
16153 case DW_LANG_Fortran90:
16154 case DW_LANG_Fortran95:
16155 return 1;
16156 case DW_LANG_UPC:
16157 case DW_LANG_D:
16158 case DW_LANG_Python:
16159 return dwarf_version >= 4 ? 0 : -1;
16160 case DW_LANG_Ada95:
16161 case DW_LANG_Ada83:
16162 case DW_LANG_Cobol74:
16163 case DW_LANG_Cobol85:
16164 case DW_LANG_Pascal83:
16165 case DW_LANG_Modula2:
16166 case DW_LANG_PLI:
16167 return dwarf_version >= 4 ? 1 : -1;
16168 default:
16169 return -1;
16173 /* Given a tree node describing an array bound (either lower or upper) output
16174 a representation for that bound. */
16176 static void
16177 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
16179 switch (TREE_CODE (bound))
16181 case ERROR_MARK:
16182 return;
16184 /* All fixed-bounds are represented by INTEGER_CST nodes. */
16185 case INTEGER_CST:
16187 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
16188 int dflt;
16190 /* Use the default if possible. */
16191 if (bound_attr == DW_AT_lower_bound
16192 && tree_fits_shwi_p (bound)
16193 && (dflt = lower_bound_default ()) != -1
16194 && tree_to_shwi (bound) == dflt)
16197 /* Otherwise represent the bound as an unsigned value with the
16198 precision of its type. The precision and signedness of the
16199 type will be necessary to re-interpret it unambiguously. */
16200 else if (prec < HOST_BITS_PER_WIDE_INT)
16202 unsigned HOST_WIDE_INT mask
16203 = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
16204 add_AT_unsigned (subrange_die, bound_attr,
16205 TREE_INT_CST_LOW (bound) & mask);
16207 else if (prec == HOST_BITS_PER_WIDE_INT
16208 || TREE_INT_CST_HIGH (bound) == 0)
16209 add_AT_unsigned (subrange_die, bound_attr,
16210 TREE_INT_CST_LOW (bound));
16211 else
16212 add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
16213 TREE_INT_CST_LOW (bound));
16215 break;
16217 CASE_CONVERT:
16218 case VIEW_CONVERT_EXPR:
16219 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
16220 break;
16222 case SAVE_EXPR:
16223 break;
16225 case VAR_DECL:
16226 case PARM_DECL:
16227 case RESULT_DECL:
16229 dw_die_ref decl_die = lookup_decl_die (bound);
16231 /* ??? Can this happen, or should the variable have been bound
16232 first? Probably it can, since I imagine that we try to create
16233 the types of parameters in the order in which they exist in
16234 the list, and won't have created a forward reference to a
16235 later parameter. */
16236 if (decl_die != NULL)
16238 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16239 break;
16242 /* FALLTHRU */
16244 default:
16246 /* Otherwise try to create a stack operation procedure to
16247 evaluate the value of the array bound. */
16249 dw_die_ref ctx, decl_die;
16250 dw_loc_list_ref list;
16252 list = loc_list_from_tree (bound, 2);
16253 if (list == NULL || single_element_loc_list_p (list))
16255 /* If DW_AT_*bound is not a reference nor constant, it is
16256 a DWARF expression rather than location description.
16257 For that loc_list_from_tree (bound, 0) is needed.
16258 If that fails to give a single element list,
16259 fall back to outputting this as a reference anyway. */
16260 dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
16261 if (list2 && single_element_loc_list_p (list2))
16263 add_AT_loc (subrange_die, bound_attr, list2->expr);
16264 break;
16267 if (list == NULL)
16268 break;
16270 if (current_function_decl == 0)
16271 ctx = comp_unit_die ();
16272 else
16273 ctx = lookup_decl_die (current_function_decl);
16275 decl_die = new_die (DW_TAG_variable, ctx, bound);
16276 add_AT_flag (decl_die, DW_AT_artificial, 1);
16277 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
16278 add_AT_location_description (decl_die, DW_AT_location, list);
16279 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16280 break;
16285 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16286 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16287 Note that the block of subscript information for an array type also
16288 includes information about the element type of the given array type. */
16290 static void
16291 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16293 unsigned dimension_number;
16294 tree lower, upper;
16295 dw_die_ref subrange_die;
16297 for (dimension_number = 0;
16298 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16299 type = TREE_TYPE (type), dimension_number++)
16301 tree domain = TYPE_DOMAIN (type);
16303 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16304 break;
16306 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16307 and (in GNU C only) variable bounds. Handle all three forms
16308 here. */
16309 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16310 if (domain)
16312 /* We have an array type with specified bounds. */
16313 lower = TYPE_MIN_VALUE (domain);
16314 upper = TYPE_MAX_VALUE (domain);
16316 /* Define the index type. */
16317 if (TREE_TYPE (domain))
16319 /* ??? This is probably an Ada unnamed subrange type. Ignore the
16320 TREE_TYPE field. We can't emit debug info for this
16321 because it is an unnamed integral type. */
16322 if (TREE_CODE (domain) == INTEGER_TYPE
16323 && TYPE_NAME (domain) == NULL_TREE
16324 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16325 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16327 else
16328 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
16329 type_die);
16332 /* ??? If upper is NULL, the array has unspecified length,
16333 but it does have a lower bound. This happens with Fortran
16334 dimension arr(N:*)
16335 Since the debugger is definitely going to need to know N
16336 to produce useful results, go ahead and output the lower
16337 bound solo, and hope the debugger can cope. */
16339 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
16340 if (upper)
16341 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
16344 /* Otherwise we have an array type with an unspecified length. The
16345 DWARF-2 spec does not say how to handle this; let's just leave out the
16346 bounds. */
16350 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
16352 static void
16353 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16355 dw_die_ref decl_die;
16356 HOST_WIDE_INT size;
16358 switch (TREE_CODE (tree_node))
16360 case ERROR_MARK:
16361 size = 0;
16362 break;
16363 case ENUMERAL_TYPE:
16364 case RECORD_TYPE:
16365 case UNION_TYPE:
16366 case QUAL_UNION_TYPE:
16367 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
16368 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
16370 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
16371 return;
16373 size = int_size_in_bytes (tree_node);
16374 break;
16375 case FIELD_DECL:
16376 /* For a data member of a struct or union, the DW_AT_byte_size is
16377 generally given as the number of bytes normally allocated for an
16378 object of the *declared* type of the member itself. This is true
16379 even for bit-fields. */
16380 size = int_size_in_bytes (field_type (tree_node));
16381 break;
16382 default:
16383 gcc_unreachable ();
16386 /* Note that `size' might be -1 when we get to this point. If it is, that
16387 indicates that the byte size of the entity in question is variable. We
16388 have no good way of expressing this fact in Dwarf at the present time,
16389 when location description was not used by the caller code instead. */
16390 if (size >= 0)
16391 add_AT_unsigned (die, DW_AT_byte_size, size);
16394 /* For a FIELD_DECL node which represents a bit-field, output an attribute
16395 which specifies the distance in bits from the highest order bit of the
16396 "containing object" for the bit-field to the highest order bit of the
16397 bit-field itself.
16399 For any given bit-field, the "containing object" is a hypothetical object
16400 (of some integral or enum type) within which the given bit-field lives. The
16401 type of this hypothetical "containing object" is always the same as the
16402 declared type of the individual bit-field itself. The determination of the
16403 exact location of the "containing object" for a bit-field is rather
16404 complicated. It's handled by the `field_byte_offset' function (above).
16406 Note that it is the size (in bytes) of the hypothetical "containing object"
16407 which will be given in the DW_AT_byte_size attribute for this bit-field.
16408 (See `byte_size_attribute' above). */
16410 static inline void
16411 add_bit_offset_attribute (dw_die_ref die, tree decl)
16413 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
16414 tree type = DECL_BIT_FIELD_TYPE (decl);
16415 HOST_WIDE_INT bitpos_int;
16416 HOST_WIDE_INT highest_order_object_bit_offset;
16417 HOST_WIDE_INT highest_order_field_bit_offset;
16418 HOST_WIDE_INT bit_offset;
16420 /* Must be a field and a bit field. */
16421 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
16423 /* We can't yet handle bit-fields whose offsets are variable, so if we
16424 encounter such things, just return without generating any attribute
16425 whatsoever. Likewise for variable or too large size. */
16426 if (! tree_fits_shwi_p (bit_position (decl))
16427 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
16428 return;
16430 bitpos_int = int_bit_position (decl);
16432 /* Note that the bit offset is always the distance (in bits) from the
16433 highest-order bit of the "containing object" to the highest-order bit of
16434 the bit-field itself. Since the "high-order end" of any object or field
16435 is different on big-endian and little-endian machines, the computation
16436 below must take account of these differences. */
16437 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
16438 highest_order_field_bit_offset = bitpos_int;
16440 if (! BYTES_BIG_ENDIAN)
16442 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
16443 highest_order_object_bit_offset += simple_type_size_in_bits (type);
16446 bit_offset
16447 = (! BYTES_BIG_ENDIAN
16448 ? highest_order_object_bit_offset - highest_order_field_bit_offset
16449 : highest_order_field_bit_offset - highest_order_object_bit_offset);
16451 if (bit_offset < 0)
16452 add_AT_int (die, DW_AT_bit_offset, bit_offset);
16453 else
16454 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
16457 /* For a FIELD_DECL node which represents a bit field, output an attribute
16458 which specifies the length in bits of the given field. */
16460 static inline void
16461 add_bit_size_attribute (dw_die_ref die, tree decl)
16463 /* Must be a field and a bit field. */
16464 gcc_assert (TREE_CODE (decl) == FIELD_DECL
16465 && DECL_BIT_FIELD_TYPE (decl));
16467 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
16468 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
16471 /* If the compiled language is ANSI C, then add a 'prototyped'
16472 attribute, if arg types are given for the parameters of a function. */
16474 static inline void
16475 add_prototyped_attribute (dw_die_ref die, tree func_type)
16477 if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
16478 && prototype_p (func_type))
16479 add_AT_flag (die, DW_AT_prototyped, 1);
16482 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
16483 by looking in either the type declaration or object declaration
16484 equate table. */
16486 static inline dw_die_ref
16487 add_abstract_origin_attribute (dw_die_ref die, tree origin)
16489 dw_die_ref origin_die = NULL;
16491 if (TREE_CODE (origin) != FUNCTION_DECL)
16493 /* We may have gotten separated from the block for the inlined
16494 function, if we're in an exception handler or some such; make
16495 sure that the abstract function has been written out.
16497 Doing this for nested functions is wrong, however; functions are
16498 distinct units, and our context might not even be inline. */
16499 tree fn = origin;
16501 if (TYPE_P (fn))
16502 fn = TYPE_STUB_DECL (fn);
16504 fn = decl_function_context (fn);
16505 if (fn)
16506 dwarf2out_abstract_function (fn);
16509 if (DECL_P (origin))
16510 origin_die = lookup_decl_die (origin);
16511 else if (TYPE_P (origin))
16512 origin_die = lookup_type_die (origin);
16514 /* XXX: Functions that are never lowered don't always have correct block
16515 trees (in the case of java, they simply have no block tree, in some other
16516 languages). For these functions, there is nothing we can really do to
16517 output correct debug info for inlined functions in all cases. Rather
16518 than die, we'll just produce deficient debug info now, in that we will
16519 have variables without a proper abstract origin. In the future, when all
16520 functions are lowered, we should re-add a gcc_assert (origin_die)
16521 here. */
16523 if (origin_die)
16524 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
16525 return origin_die;
16528 /* We do not currently support the pure_virtual attribute. */
16530 static inline void
16531 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
16533 if (DECL_VINDEX (func_decl))
16535 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
16537 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
16538 add_AT_loc (die, DW_AT_vtable_elem_location,
16539 new_loc_descr (DW_OP_constu,
16540 tree_to_shwi (DECL_VINDEX (func_decl)),
16541 0));
16543 /* GNU extension: Record what type this method came from originally. */
16544 if (debug_info_level > DINFO_LEVEL_TERSE
16545 && DECL_CONTEXT (func_decl))
16546 add_AT_die_ref (die, DW_AT_containing_type,
16547 lookup_type_die (DECL_CONTEXT (func_decl)));
16551 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
16552 given decl. This used to be a vendor extension until after DWARF 4
16553 standardized it. */
16555 static void
16556 add_linkage_attr (dw_die_ref die, tree decl)
16558 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
16560 /* Mimic what assemble_name_raw does with a leading '*'. */
16561 if (name[0] == '*')
16562 name = &name[1];
16564 if (dwarf_version >= 4)
16565 add_AT_string (die, DW_AT_linkage_name, name);
16566 else
16567 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
16570 /* Add source coordinate attributes for the given decl. */
16572 static void
16573 add_src_coords_attributes (dw_die_ref die, tree decl)
16575 expanded_location s;
16577 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
16578 return;
16579 s = expand_location (DECL_SOURCE_LOCATION (decl));
16580 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
16581 add_AT_unsigned (die, DW_AT_decl_line, s.line);
16584 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
16586 static void
16587 add_linkage_name (dw_die_ref die, tree decl)
16589 if (debug_info_level > DINFO_LEVEL_TERSE
16590 && (TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
16591 && TREE_PUBLIC (decl)
16592 && !DECL_ABSTRACT (decl)
16593 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
16594 && die->die_tag != DW_TAG_member)
16596 /* Defer until we have an assembler name set. */
16597 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
16599 limbo_die_node *asm_name;
16601 asm_name = ggc_alloc_cleared_limbo_die_node ();
16602 asm_name->die = die;
16603 asm_name->created_for = decl;
16604 asm_name->next = deferred_asm_name;
16605 deferred_asm_name = asm_name;
16607 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
16608 add_linkage_attr (die, decl);
16612 /* Add a DW_AT_name attribute and source coordinate attribute for the
16613 given decl, but only if it actually has a name. */
16615 static void
16616 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
16618 tree decl_name;
16620 decl_name = DECL_NAME (decl);
16621 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
16623 const char *name = dwarf2_name (decl, 0);
16624 if (name)
16625 add_name_attribute (die, name);
16626 if (! DECL_ARTIFICIAL (decl))
16627 add_src_coords_attributes (die, decl);
16629 add_linkage_name (die, decl);
16632 #ifdef VMS_DEBUGGING_INFO
16633 /* Get the function's name, as described by its RTL. This may be different
16634 from the DECL_NAME name used in the source file. */
16635 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
16637 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
16638 XEXP (DECL_RTL (decl), 0), false);
16639 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
16641 #endif /* VMS_DEBUGGING_INFO */
16644 #ifdef VMS_DEBUGGING_INFO
16645 /* Output the debug main pointer die for VMS */
16647 void
16648 dwarf2out_vms_debug_main_pointer (void)
16650 char label[MAX_ARTIFICIAL_LABEL_BYTES];
16651 dw_die_ref die;
16653 /* Allocate the VMS debug main subprogram die. */
16654 die = ggc_alloc_cleared_die_node ();
16655 die->die_tag = DW_TAG_subprogram;
16656 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
16657 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
16658 current_function_funcdef_no);
16659 add_AT_lbl_id (die, DW_AT_entry_pc, label);
16661 /* Make it the first child of comp_unit_die (). */
16662 die->die_parent = comp_unit_die ();
16663 if (comp_unit_die ()->die_child)
16665 die->die_sib = comp_unit_die ()->die_child->die_sib;
16666 comp_unit_die ()->die_child->die_sib = die;
16668 else
16670 die->die_sib = die;
16671 comp_unit_die ()->die_child = die;
16674 #endif /* VMS_DEBUGGING_INFO */
16676 /* Push a new declaration scope. */
16678 static void
16679 push_decl_scope (tree scope)
16681 vec_safe_push (decl_scope_table, scope);
16684 /* Pop a declaration scope. */
16686 static inline void
16687 pop_decl_scope (void)
16689 decl_scope_table->pop ();
16692 /* walk_tree helper function for uses_local_type, below. */
16694 static tree
16695 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
16697 if (!TYPE_P (*tp))
16698 *walk_subtrees = 0;
16699 else
16701 tree name = TYPE_NAME (*tp);
16702 if (name && DECL_P (name) && decl_function_context (name))
16703 return *tp;
16705 return NULL_TREE;
16708 /* If TYPE involves a function-local type (including a local typedef to a
16709 non-local type), returns that type; otherwise returns NULL_TREE. */
16711 static tree
16712 uses_local_type (tree type)
16714 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
16715 return used;
16718 /* Return the DIE for the scope that immediately contains this type.
16719 Non-named types that do not involve a function-local type get global
16720 scope. Named types nested in namespaces or other types get their
16721 containing scope. All other types (i.e. function-local named types) get
16722 the current active scope. */
16724 static dw_die_ref
16725 scope_die_for (tree t, dw_die_ref context_die)
16727 dw_die_ref scope_die = NULL;
16728 tree containing_scope;
16730 /* Non-types always go in the current scope. */
16731 gcc_assert (TYPE_P (t));
16733 /* Use the scope of the typedef, rather than the scope of the type
16734 it refers to. */
16735 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
16736 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
16737 else
16738 containing_scope = TYPE_CONTEXT (t);
16740 /* Use the containing namespace if there is one. */
16741 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
16743 if (context_die == lookup_decl_die (containing_scope))
16744 /* OK */;
16745 else if (debug_info_level > DINFO_LEVEL_TERSE)
16746 context_die = get_context_die (containing_scope);
16747 else
16748 containing_scope = NULL_TREE;
16751 /* Ignore function type "scopes" from the C frontend. They mean that
16752 a tagged type is local to a parmlist of a function declarator, but
16753 that isn't useful to DWARF. */
16754 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
16755 containing_scope = NULL_TREE;
16757 if (SCOPE_FILE_SCOPE_P (containing_scope))
16759 /* If T uses a local type keep it local as well, to avoid references
16760 to function-local DIEs from outside the function. */
16761 if (current_function_decl && uses_local_type (t))
16762 scope_die = context_die;
16763 else
16764 scope_die = comp_unit_die ();
16766 else if (TYPE_P (containing_scope))
16768 /* For types, we can just look up the appropriate DIE. */
16769 if (debug_info_level > DINFO_LEVEL_TERSE)
16770 scope_die = get_context_die (containing_scope);
16771 else
16773 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
16774 if (scope_die == NULL)
16775 scope_die = comp_unit_die ();
16778 else
16779 scope_die = context_die;
16781 return scope_die;
16784 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
16786 static inline int
16787 local_scope_p (dw_die_ref context_die)
16789 for (; context_die; context_die = context_die->die_parent)
16790 if (context_die->die_tag == DW_TAG_inlined_subroutine
16791 || context_die->die_tag == DW_TAG_subprogram)
16792 return 1;
16794 return 0;
16797 /* Returns nonzero if CONTEXT_DIE is a class. */
16799 static inline int
16800 class_scope_p (dw_die_ref context_die)
16802 return (context_die
16803 && (context_die->die_tag == DW_TAG_structure_type
16804 || context_die->die_tag == DW_TAG_class_type
16805 || context_die->die_tag == DW_TAG_interface_type
16806 || context_die->die_tag == DW_TAG_union_type));
16809 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
16810 whether or not to treat a DIE in this context as a declaration. */
16812 static inline int
16813 class_or_namespace_scope_p (dw_die_ref context_die)
16815 return (class_scope_p (context_die)
16816 || (context_die && context_die->die_tag == DW_TAG_namespace));
16819 /* Many forms of DIEs require a "type description" attribute. This
16820 routine locates the proper "type descriptor" die for the type given
16821 by 'type', and adds a DW_AT_type attribute below the given die. */
16823 static void
16824 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
16825 int decl_volatile, dw_die_ref context_die)
16827 enum tree_code code = TREE_CODE (type);
16828 dw_die_ref type_die = NULL;
16830 /* ??? If this type is an unnamed subrange type of an integral, floating-point
16831 or fixed-point type, use the inner type. This is because we have no
16832 support for unnamed types in base_type_die. This can happen if this is
16833 an Ada subrange type. Correct solution is emit a subrange type die. */
16834 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
16835 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
16836 type = TREE_TYPE (type), code = TREE_CODE (type);
16838 if (code == ERROR_MARK
16839 /* Handle a special case. For functions whose return type is void, we
16840 generate *no* type attribute. (Note that no object may have type
16841 `void', so this only applies to function return types). */
16842 || code == VOID_TYPE)
16843 return;
16845 type_die = modified_type_die (type,
16846 decl_const || TYPE_READONLY (type),
16847 decl_volatile || TYPE_VOLATILE (type),
16848 context_die);
16850 if (type_die != NULL)
16851 add_AT_die_ref (object_die, DW_AT_type, type_die);
16854 /* Given an object die, add the calling convention attribute for the
16855 function call type. */
16856 static void
16857 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
16859 enum dwarf_calling_convention value = DW_CC_normal;
16861 value = ((enum dwarf_calling_convention)
16862 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
16864 if (is_fortran ()
16865 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
16867 /* DWARF 2 doesn't provide a way to identify a program's source-level
16868 entry point. DW_AT_calling_convention attributes are only meant
16869 to describe functions' calling conventions. However, lacking a
16870 better way to signal the Fortran main program, we used this for
16871 a long time, following existing custom. Now, DWARF 4 has
16872 DW_AT_main_subprogram, which we add below, but some tools still
16873 rely on the old way, which we thus keep. */
16874 value = DW_CC_program;
16876 if (dwarf_version >= 4 || !dwarf_strict)
16877 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
16880 /* Only add the attribute if the backend requests it, and
16881 is not DW_CC_normal. */
16882 if (value && (value != DW_CC_normal))
16883 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
16886 /* Given a tree pointer to a struct, class, union, or enum type node, return
16887 a pointer to the (string) tag name for the given type, or zero if the type
16888 was declared without a tag. */
16890 static const char *
16891 type_tag (const_tree type)
16893 const char *name = 0;
16895 if (TYPE_NAME (type) != 0)
16897 tree t = 0;
16899 /* Find the IDENTIFIER_NODE for the type name. */
16900 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
16901 && !TYPE_NAMELESS (type))
16902 t = TYPE_NAME (type);
16904 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
16905 a TYPE_DECL node, regardless of whether or not a `typedef' was
16906 involved. */
16907 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
16908 && ! DECL_IGNORED_P (TYPE_NAME (type)))
16910 /* We want to be extra verbose. Don't call dwarf_name if
16911 DECL_NAME isn't set. The default hook for decl_printable_name
16912 doesn't like that, and in this context it's correct to return
16913 0, instead of "<anonymous>" or the like. */
16914 if (DECL_NAME (TYPE_NAME (type))
16915 && !DECL_NAMELESS (TYPE_NAME (type)))
16916 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
16919 /* Now get the name as a string, or invent one. */
16920 if (!name && t != 0)
16921 name = IDENTIFIER_POINTER (t);
16924 return (name == 0 || *name == '\0') ? 0 : name;
16927 /* Return the type associated with a data member, make a special check
16928 for bit field types. */
16930 static inline tree
16931 member_declared_type (const_tree member)
16933 return (DECL_BIT_FIELD_TYPE (member)
16934 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
16937 /* Get the decl's label, as described by its RTL. This may be different
16938 from the DECL_NAME name used in the source file. */
16940 #if 0
16941 static const char *
16942 decl_start_label (tree decl)
16944 rtx x;
16945 const char *fnname;
16947 x = DECL_RTL (decl);
16948 gcc_assert (MEM_P (x));
16950 x = XEXP (x, 0);
16951 gcc_assert (GET_CODE (x) == SYMBOL_REF);
16953 fnname = XSTR (x, 0);
16954 return fnname;
16956 #endif
16958 /* These routines generate the internal representation of the DIE's for
16959 the compilation unit. Debugging information is collected by walking
16960 the declaration trees passed in from dwarf2out_decl(). */
16962 static void
16963 gen_array_type_die (tree type, dw_die_ref context_die)
16965 dw_die_ref scope_die = scope_die_for (type, context_die);
16966 dw_die_ref array_die;
16968 /* GNU compilers represent multidimensional array types as sequences of one
16969 dimensional array types whose element types are themselves array types.
16970 We sometimes squish that down to a single array_type DIE with multiple
16971 subscripts in the Dwarf debugging info. The draft Dwarf specification
16972 say that we are allowed to do this kind of compression in C, because
16973 there is no difference between an array of arrays and a multidimensional
16974 array. We don't do this for Ada to remain as close as possible to the
16975 actual representation, which is especially important against the language
16976 flexibilty wrt arrays of variable size. */
16978 bool collapse_nested_arrays = !is_ada ();
16979 tree element_type;
16981 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
16982 DW_TAG_string_type doesn't have DW_AT_type attribute). */
16983 if (TYPE_STRING_FLAG (type)
16984 && TREE_CODE (type) == ARRAY_TYPE
16985 && is_fortran ()
16986 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
16988 HOST_WIDE_INT size;
16990 array_die = new_die (DW_TAG_string_type, scope_die, type);
16991 add_name_attribute (array_die, type_tag (type));
16992 equate_type_number_to_die (type, array_die);
16993 size = int_size_in_bytes (type);
16994 if (size >= 0)
16995 add_AT_unsigned (array_die, DW_AT_byte_size, size);
16996 else if (TYPE_DOMAIN (type) != NULL_TREE
16997 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
16998 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
17000 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
17001 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
17003 size = int_size_in_bytes (TREE_TYPE (szdecl));
17004 if (loc && size > 0)
17006 add_AT_location_description (array_die, DW_AT_string_length, loc);
17007 if (size != DWARF2_ADDR_SIZE)
17008 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17011 return;
17014 array_die = new_die (DW_TAG_array_type, scope_die, type);
17015 add_name_attribute (array_die, type_tag (type));
17016 equate_type_number_to_die (type, array_die);
17018 if (TREE_CODE (type) == VECTOR_TYPE)
17019 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
17021 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17022 if (is_fortran ()
17023 && TREE_CODE (type) == ARRAY_TYPE
17024 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17025 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17026 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17028 #if 0
17029 /* We default the array ordering. SDB will probably do
17030 the right things even if DW_AT_ordering is not present. It's not even
17031 an issue until we start to get into multidimensional arrays anyway. If
17032 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17033 then we'll have to put the DW_AT_ordering attribute back in. (But if
17034 and when we find out that we need to put these in, we will only do so
17035 for multidimensional arrays. */
17036 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17037 #endif
17039 if (TREE_CODE (type) == VECTOR_TYPE)
17041 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
17042 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
17043 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
17044 add_bound_info (subrange_die, DW_AT_upper_bound,
17045 size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
17047 else
17048 add_subscript_info (array_die, type, collapse_nested_arrays);
17050 /* Add representation of the type of the elements of this array type and
17051 emit the corresponding DIE if we haven't done it already. */
17052 element_type = TREE_TYPE (type);
17053 if (collapse_nested_arrays)
17054 while (TREE_CODE (element_type) == ARRAY_TYPE)
17056 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17057 break;
17058 element_type = TREE_TYPE (element_type);
17061 add_type_attribute (array_die, element_type, 0, 0, context_die);
17063 add_gnat_descriptive_type_attribute (array_die, type, context_die);
17064 if (TYPE_ARTIFICIAL (type))
17065 add_AT_flag (array_die, DW_AT_artificial, 1);
17067 if (get_AT (array_die, DW_AT_name))
17068 add_pubtype (type, array_die);
17071 static dw_loc_descr_ref
17072 descr_info_loc (tree val, tree base_decl)
17074 HOST_WIDE_INT size;
17075 dw_loc_descr_ref loc, loc2;
17076 enum dwarf_location_atom op;
17078 if (val == base_decl)
17079 return new_loc_descr (DW_OP_push_object_address, 0, 0);
17081 switch (TREE_CODE (val))
17083 CASE_CONVERT:
17084 return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17085 case VAR_DECL:
17086 return loc_descriptor_from_tree (val, 0);
17087 case INTEGER_CST:
17088 if (tree_fits_shwi_p (val))
17089 return int_loc_descriptor (tree_to_shwi (val));
17090 break;
17091 case INDIRECT_REF:
17092 size = int_size_in_bytes (TREE_TYPE (val));
17093 if (size < 0)
17094 break;
17095 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17096 if (!loc)
17097 break;
17098 if (size == DWARF2_ADDR_SIZE)
17099 add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
17100 else
17101 add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
17102 return loc;
17103 case POINTER_PLUS_EXPR:
17104 case PLUS_EXPR:
17105 if (tree_fits_uhwi_p (TREE_OPERAND (val, 1))
17106 && tree_to_uhwi (TREE_OPERAND (val, 1)) < 16384)
17108 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17109 if (!loc)
17110 break;
17111 loc_descr_plus_const (&loc, tree_to_shwi (TREE_OPERAND (val, 1)));
17113 else
17115 op = DW_OP_plus;
17116 do_binop:
17117 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17118 if (!loc)
17119 break;
17120 loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
17121 if (!loc2)
17122 break;
17123 add_loc_descr (&loc, loc2);
17124 add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
17126 return loc;
17127 case MINUS_EXPR:
17128 op = DW_OP_minus;
17129 goto do_binop;
17130 case MULT_EXPR:
17131 op = DW_OP_mul;
17132 goto do_binop;
17133 case EQ_EXPR:
17134 op = DW_OP_eq;
17135 goto do_binop;
17136 case NE_EXPR:
17137 op = DW_OP_ne;
17138 goto do_binop;
17139 default:
17140 break;
17142 return NULL;
17145 static void
17146 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
17147 tree val, tree base_decl)
17149 dw_loc_descr_ref loc;
17151 if (tree_fits_shwi_p (val))
17153 add_AT_unsigned (die, attr, tree_to_shwi (val));
17154 return;
17157 loc = descr_info_loc (val, base_decl);
17158 if (!loc)
17159 return;
17161 add_AT_loc (die, attr, loc);
17164 /* This routine generates DIE for array with hidden descriptor, details
17165 are filled into *info by a langhook. */
17167 static void
17168 gen_descr_array_type_die (tree type, struct array_descr_info *info,
17169 dw_die_ref context_die)
17171 dw_die_ref scope_die = scope_die_for (type, context_die);
17172 dw_die_ref array_die;
17173 int dim;
17175 array_die = new_die (DW_TAG_array_type, scope_die, type);
17176 add_name_attribute (array_die, type_tag (type));
17177 equate_type_number_to_die (type, array_die);
17179 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17180 if (is_fortran ()
17181 && info->ndimensions >= 2)
17182 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17184 if (info->data_location)
17185 add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
17186 info->base_decl);
17187 if (info->associated)
17188 add_descr_info_field (array_die, DW_AT_associated, info->associated,
17189 info->base_decl);
17190 if (info->allocated)
17191 add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
17192 info->base_decl);
17194 for (dim = 0; dim < info->ndimensions; dim++)
17196 dw_die_ref subrange_die
17197 = new_die (DW_TAG_subrange_type, array_die, NULL);
17199 if (info->dimen[dim].lower_bound)
17201 /* If it is the default value, omit it. */
17202 int dflt;
17204 if (tree_fits_shwi_p (info->dimen[dim].lower_bound)
17205 && (dflt = lower_bound_default ()) != -1
17206 && tree_to_shwi (info->dimen[dim].lower_bound) == dflt)
17208 else
17209 add_descr_info_field (subrange_die, DW_AT_lower_bound,
17210 info->dimen[dim].lower_bound,
17211 info->base_decl);
17213 if (info->dimen[dim].upper_bound)
17214 add_descr_info_field (subrange_die, DW_AT_upper_bound,
17215 info->dimen[dim].upper_bound,
17216 info->base_decl);
17217 if (info->dimen[dim].stride)
17218 add_descr_info_field (subrange_die, DW_AT_byte_stride,
17219 info->dimen[dim].stride,
17220 info->base_decl);
17223 gen_type_die (info->element_type, context_die);
17224 add_type_attribute (array_die, info->element_type, 0, 0, context_die);
17226 if (get_AT (array_die, DW_AT_name))
17227 add_pubtype (type, array_die);
17230 #if 0
17231 static void
17232 gen_entry_point_die (tree decl, dw_die_ref context_die)
17234 tree origin = decl_ultimate_origin (decl);
17235 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
17237 if (origin != NULL)
17238 add_abstract_origin_attribute (decl_die, origin);
17239 else
17241 add_name_and_src_coords_attributes (decl_die, decl);
17242 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17243 0, 0, context_die);
17246 if (DECL_ABSTRACT (decl))
17247 equate_decl_number_to_die (decl, decl_die);
17248 else
17249 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17251 #endif
17253 /* Walk through the list of incomplete types again, trying once more to
17254 emit full debugging info for them. */
17256 static void
17257 retry_incomplete_types (void)
17259 int i;
17261 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
17262 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
17263 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
17266 /* Determine what tag to use for a record type. */
17268 static enum dwarf_tag
17269 record_type_tag (tree type)
17271 if (! lang_hooks.types.classify_record)
17272 return DW_TAG_structure_type;
17274 switch (lang_hooks.types.classify_record (type))
17276 case RECORD_IS_STRUCT:
17277 return DW_TAG_structure_type;
17279 case RECORD_IS_CLASS:
17280 return DW_TAG_class_type;
17282 case RECORD_IS_INTERFACE:
17283 if (dwarf_version >= 3 || !dwarf_strict)
17284 return DW_TAG_interface_type;
17285 return DW_TAG_structure_type;
17287 default:
17288 gcc_unreachable ();
17292 /* Generate a DIE to represent an enumeration type. Note that these DIEs
17293 include all of the information about the enumeration values also. Each
17294 enumerated type name/value is listed as a child of the enumerated type
17295 DIE. */
17297 static dw_die_ref
17298 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17300 dw_die_ref type_die = lookup_type_die (type);
17302 if (type_die == NULL)
17304 type_die = new_die (DW_TAG_enumeration_type,
17305 scope_die_for (type, context_die), type);
17306 equate_type_number_to_die (type, type_die);
17307 add_name_attribute (type_die, type_tag (type));
17308 if (dwarf_version >= 4 || !dwarf_strict)
17310 if (ENUM_IS_SCOPED (type))
17311 add_AT_flag (type_die, DW_AT_enum_class, 1);
17312 if (ENUM_IS_OPAQUE (type))
17313 add_AT_flag (type_die, DW_AT_declaration, 1);
17316 else if (! TYPE_SIZE (type))
17317 return type_die;
17318 else
17319 remove_AT (type_die, DW_AT_declaration);
17321 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
17322 given enum type is incomplete, do not generate the DW_AT_byte_size
17323 attribute or the DW_AT_element_list attribute. */
17324 if (TYPE_SIZE (type))
17326 tree link;
17328 TREE_ASM_WRITTEN (type) = 1;
17329 add_byte_size_attribute (type_die, type);
17330 if (TYPE_STUB_DECL (type) != NULL_TREE)
17332 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17333 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17336 /* If the first reference to this type was as the return type of an
17337 inline function, then it may not have a parent. Fix this now. */
17338 if (type_die->die_parent == NULL)
17339 add_child_die (scope_die_for (type, context_die), type_die);
17341 for (link = TYPE_VALUES (type);
17342 link != NULL; link = TREE_CHAIN (link))
17344 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17345 tree value = TREE_VALUE (link);
17347 add_name_attribute (enum_die,
17348 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17350 if (TREE_CODE (value) == CONST_DECL)
17351 value = DECL_INITIAL (value);
17353 if (simple_type_size_in_bits (TREE_TYPE (value))
17354 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
17355 /* DWARF2 does not provide a way of indicating whether or
17356 not enumeration constants are signed or unsigned. GDB
17357 always assumes the values are signed, so we output all
17358 values as if they were signed. That means that
17359 enumeration constants with very large unsigned values
17360 will appear to have negative values in the debugger.
17362 TODO: the above comment is wrong, DWARF2 does provide
17363 DW_FORM_sdata/DW_FORM_udata to represent signed/unsigned data.
17364 This should be re-worked to use correct signed/unsigned
17365 int/double tags for all cases, instead of always treating as
17366 signed. */
17367 add_AT_int (enum_die, DW_AT_const_value, TREE_INT_CST_LOW (value));
17368 else
17369 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
17370 that here. */
17371 add_AT_double (enum_die, DW_AT_const_value,
17372 TREE_INT_CST_HIGH (value), TREE_INT_CST_LOW (value));
17375 add_gnat_descriptive_type_attribute (type_die, type, context_die);
17376 if (TYPE_ARTIFICIAL (type))
17377 add_AT_flag (type_die, DW_AT_artificial, 1);
17379 else
17380 add_AT_flag (type_die, DW_AT_declaration, 1);
17382 add_pubtype (type, type_die);
17384 return type_die;
17387 /* Generate a DIE to represent either a real live formal parameter decl or to
17388 represent just the type of some formal parameter position in some function
17389 type.
17391 Note that this routine is a bit unusual because its argument may be a
17392 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17393 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17394 node. If it's the former then this function is being called to output a
17395 DIE to represent a formal parameter object (or some inlining thereof). If
17396 it's the latter, then this function is only being called to output a
17397 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17398 argument type of some subprogram type.
17399 If EMIT_NAME_P is true, name and source coordinate attributes
17400 are emitted. */
17402 static dw_die_ref
17403 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17404 dw_die_ref context_die)
17406 tree node_or_origin = node ? node : origin;
17407 tree ultimate_origin;
17408 dw_die_ref parm_die
17409 = new_die (DW_TAG_formal_parameter, context_die, node);
17411 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
17413 case tcc_declaration:
17414 ultimate_origin = decl_ultimate_origin (node_or_origin);
17415 if (node || ultimate_origin)
17416 origin = ultimate_origin;
17417 if (origin != NULL)
17418 add_abstract_origin_attribute (parm_die, origin);
17419 else if (emit_name_p)
17420 add_name_and_src_coords_attributes (parm_die, node);
17421 if (origin == NULL
17422 || (! DECL_ABSTRACT (node_or_origin)
17423 && variably_modified_type_p (TREE_TYPE (node_or_origin),
17424 decl_function_context
17425 (node_or_origin))))
17427 tree type = TREE_TYPE (node_or_origin);
17428 if (decl_by_reference_p (node_or_origin))
17429 add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
17430 context_die);
17431 else
17432 add_type_attribute (parm_die, type,
17433 TREE_READONLY (node_or_origin),
17434 TREE_THIS_VOLATILE (node_or_origin),
17435 context_die);
17437 if (origin == NULL && DECL_ARTIFICIAL (node))
17438 add_AT_flag (parm_die, DW_AT_artificial, 1);
17440 if (node && node != origin)
17441 equate_decl_number_to_die (node, parm_die);
17442 if (! DECL_ABSTRACT (node_or_origin))
17443 add_location_or_const_value_attribute (parm_die, node_or_origin,
17444 node == NULL, DW_AT_location);
17446 break;
17448 case tcc_type:
17449 /* We were called with some kind of a ..._TYPE node. */
17450 add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
17451 break;
17453 default:
17454 gcc_unreachable ();
17457 return parm_die;
17460 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
17461 children DW_TAG_formal_parameter DIEs representing the arguments of the
17462 parameter pack.
17464 PARM_PACK must be a function parameter pack.
17465 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
17466 must point to the subsequent arguments of the function PACK_ARG belongs to.
17467 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
17468 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
17469 following the last one for which a DIE was generated. */
17471 static dw_die_ref
17472 gen_formal_parameter_pack_die (tree parm_pack,
17473 tree pack_arg,
17474 dw_die_ref subr_die,
17475 tree *next_arg)
17477 tree arg;
17478 dw_die_ref parm_pack_die;
17480 gcc_assert (parm_pack
17481 && lang_hooks.function_parameter_pack_p (parm_pack)
17482 && subr_die);
17484 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
17485 add_src_coords_attributes (parm_pack_die, parm_pack);
17487 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
17489 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
17490 parm_pack))
17491 break;
17492 gen_formal_parameter_die (arg, NULL,
17493 false /* Don't emit name attribute. */,
17494 parm_pack_die);
17496 if (next_arg)
17497 *next_arg = arg;
17498 return parm_pack_die;
17501 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
17502 at the end of an (ANSI prototyped) formal parameters list. */
17504 static void
17505 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
17507 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
17510 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
17511 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
17512 parameters as specified in some function type specification (except for
17513 those which appear as part of a function *definition*). */
17515 static void
17516 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
17518 tree link;
17519 tree formal_type = NULL;
17520 tree first_parm_type;
17521 tree arg;
17523 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
17525 arg = DECL_ARGUMENTS (function_or_method_type);
17526 function_or_method_type = TREE_TYPE (function_or_method_type);
17528 else
17529 arg = NULL_TREE;
17531 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
17533 /* Make our first pass over the list of formal parameter types and output a
17534 DW_TAG_formal_parameter DIE for each one. */
17535 for (link = first_parm_type; link; )
17537 dw_die_ref parm_die;
17539 formal_type = TREE_VALUE (link);
17540 if (formal_type == void_type_node)
17541 break;
17543 /* Output a (nameless) DIE to represent the formal parameter itself. */
17544 parm_die = gen_formal_parameter_die (formal_type, NULL,
17545 true /* Emit name attribute. */,
17546 context_die);
17547 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
17548 && link == first_parm_type)
17550 add_AT_flag (parm_die, DW_AT_artificial, 1);
17551 if (dwarf_version >= 3 || !dwarf_strict)
17552 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
17554 else if (arg && DECL_ARTIFICIAL (arg))
17555 add_AT_flag (parm_die, DW_AT_artificial, 1);
17557 link = TREE_CHAIN (link);
17558 if (arg)
17559 arg = DECL_CHAIN (arg);
17562 /* If this function type has an ellipsis, add a
17563 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
17564 if (formal_type != void_type_node)
17565 gen_unspecified_parameters_die (function_or_method_type, context_die);
17567 /* Make our second (and final) pass over the list of formal parameter types
17568 and output DIEs to represent those types (as necessary). */
17569 for (link = TYPE_ARG_TYPES (function_or_method_type);
17570 link && TREE_VALUE (link);
17571 link = TREE_CHAIN (link))
17572 gen_type_die (TREE_VALUE (link), context_die);
17575 /* We want to generate the DIE for TYPE so that we can generate the
17576 die for MEMBER, which has been defined; we will need to refer back
17577 to the member declaration nested within TYPE. If we're trying to
17578 generate minimal debug info for TYPE, processing TYPE won't do the
17579 trick; we need to attach the member declaration by hand. */
17581 static void
17582 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
17584 gen_type_die (type, context_die);
17586 /* If we're trying to avoid duplicate debug info, we may not have
17587 emitted the member decl for this function. Emit it now. */
17588 if (TYPE_STUB_DECL (type)
17589 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
17590 && ! lookup_decl_die (member))
17592 dw_die_ref type_die;
17593 gcc_assert (!decl_ultimate_origin (member));
17595 push_decl_scope (type);
17596 type_die = lookup_type_die_strip_naming_typedef (type);
17597 if (TREE_CODE (member) == FUNCTION_DECL)
17598 gen_subprogram_die (member, type_die);
17599 else if (TREE_CODE (member) == FIELD_DECL)
17601 /* Ignore the nameless fields that are used to skip bits but handle
17602 C++ anonymous unions and structs. */
17603 if (DECL_NAME (member) != NULL_TREE
17604 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
17605 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
17607 gen_type_die (member_declared_type (member), type_die);
17608 gen_field_die (member, type_die);
17611 else
17612 gen_variable_die (member, NULL_TREE, type_die);
17614 pop_decl_scope ();
17618 /* Forward declare these functions, because they are mutually recursive
17619 with their set_block_* pairing functions. */
17620 static void set_decl_origin_self (tree);
17621 static void set_decl_abstract_flags (tree, int);
17623 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
17624 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
17625 that it points to the node itself, thus indicating that the node is its
17626 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
17627 the given node is NULL, recursively descend the decl/block tree which
17628 it is the root of, and for each other ..._DECL or BLOCK node contained
17629 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
17630 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
17631 values to point to themselves. */
17633 static void
17634 set_block_origin_self (tree stmt)
17636 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
17638 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
17641 tree local_decl;
17643 for (local_decl = BLOCK_VARS (stmt);
17644 local_decl != NULL_TREE;
17645 local_decl = DECL_CHAIN (local_decl))
17646 if (! DECL_EXTERNAL (local_decl))
17647 set_decl_origin_self (local_decl); /* Potential recursion. */
17651 tree subblock;
17653 for (subblock = BLOCK_SUBBLOCKS (stmt);
17654 subblock != NULL_TREE;
17655 subblock = BLOCK_CHAIN (subblock))
17656 set_block_origin_self (subblock); /* Recurse. */
17661 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
17662 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
17663 node to so that it points to the node itself, thus indicating that the
17664 node represents its own (abstract) origin. Additionally, if the
17665 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
17666 the decl/block tree of which the given node is the root of, and for
17667 each other ..._DECL or BLOCK node contained therein whose
17668 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
17669 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
17670 point to themselves. */
17672 static void
17673 set_decl_origin_self (tree decl)
17675 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
17677 DECL_ABSTRACT_ORIGIN (decl) = decl;
17678 if (TREE_CODE (decl) == FUNCTION_DECL)
17680 tree arg;
17682 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17683 DECL_ABSTRACT_ORIGIN (arg) = arg;
17684 if (DECL_INITIAL (decl) != NULL_TREE
17685 && DECL_INITIAL (decl) != error_mark_node)
17686 set_block_origin_self (DECL_INITIAL (decl));
17691 /* Given a pointer to some BLOCK node, and a boolean value to set the
17692 "abstract" flags to, set that value into the BLOCK_ABSTRACT flag for
17693 the given block, and for all local decls and all local sub-blocks
17694 (recursively) which are contained therein. */
17696 static void
17697 set_block_abstract_flags (tree stmt, int setting)
17699 tree local_decl;
17700 tree subblock;
17701 unsigned int i;
17703 BLOCK_ABSTRACT (stmt) = setting;
17705 for (local_decl = BLOCK_VARS (stmt);
17706 local_decl != NULL_TREE;
17707 local_decl = DECL_CHAIN (local_decl))
17708 if (! DECL_EXTERNAL (local_decl))
17709 set_decl_abstract_flags (local_decl, setting);
17711 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
17713 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
17714 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
17715 || TREE_CODE (local_decl) == PARM_DECL)
17716 set_decl_abstract_flags (local_decl, setting);
17719 for (subblock = BLOCK_SUBBLOCKS (stmt);
17720 subblock != NULL_TREE;
17721 subblock = BLOCK_CHAIN (subblock))
17722 set_block_abstract_flags (subblock, setting);
17725 /* Given a pointer to some ..._DECL node, and a boolean value to set the
17726 "abstract" flags to, set that value into the DECL_ABSTRACT flag for the
17727 given decl, and (in the case where the decl is a FUNCTION_DECL) also
17728 set the abstract flags for all of the parameters, local vars, local
17729 blocks and sub-blocks (recursively) to the same setting. */
17731 static void
17732 set_decl_abstract_flags (tree decl, int setting)
17734 DECL_ABSTRACT (decl) = setting;
17735 if (TREE_CODE (decl) == FUNCTION_DECL)
17737 tree arg;
17739 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17740 DECL_ABSTRACT (arg) = setting;
17741 if (DECL_INITIAL (decl) != NULL_TREE
17742 && DECL_INITIAL (decl) != error_mark_node)
17743 set_block_abstract_flags (DECL_INITIAL (decl), setting);
17747 /* Generate the DWARF2 info for the "abstract" instance of a function which we
17748 may later generate inlined and/or out-of-line instances of. */
17750 static void
17751 dwarf2out_abstract_function (tree decl)
17753 dw_die_ref old_die;
17754 tree save_fn;
17755 tree context;
17756 int was_abstract;
17757 htab_t old_decl_loc_table;
17758 htab_t old_cached_dw_loc_list_table;
17759 int old_call_site_count, old_tail_call_site_count;
17760 struct call_arg_loc_node *old_call_arg_locations;
17762 /* Make sure we have the actual abstract inline, not a clone. */
17763 decl = DECL_ORIGIN (decl);
17765 old_die = lookup_decl_die (decl);
17766 if (old_die && get_AT (old_die, DW_AT_inline))
17767 /* We've already generated the abstract instance. */
17768 return;
17770 /* We can be called while recursively when seeing block defining inlined subroutine
17771 DIE. Be sure to not clobber the outer location table nor use it or we would
17772 get locations in abstract instantces. */
17773 old_decl_loc_table = decl_loc_table;
17774 decl_loc_table = NULL;
17775 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
17776 cached_dw_loc_list_table = NULL;
17777 old_call_arg_locations = call_arg_locations;
17778 call_arg_locations = NULL;
17779 old_call_site_count = call_site_count;
17780 call_site_count = -1;
17781 old_tail_call_site_count = tail_call_site_count;
17782 tail_call_site_count = -1;
17784 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
17785 we don't get confused by DECL_ABSTRACT. */
17786 if (debug_info_level > DINFO_LEVEL_TERSE)
17788 context = decl_class_context (decl);
17789 if (context)
17790 gen_type_die_for_member
17791 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
17794 /* Pretend we've just finished compiling this function. */
17795 save_fn = current_function_decl;
17796 current_function_decl = decl;
17798 was_abstract = DECL_ABSTRACT (decl);
17799 set_decl_abstract_flags (decl, 1);
17800 dwarf2out_decl (decl);
17801 if (! was_abstract)
17802 set_decl_abstract_flags (decl, 0);
17804 current_function_decl = save_fn;
17805 decl_loc_table = old_decl_loc_table;
17806 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
17807 call_arg_locations = old_call_arg_locations;
17808 call_site_count = old_call_site_count;
17809 tail_call_site_count = old_tail_call_site_count;
17812 /* Helper function of premark_used_types() which gets called through
17813 htab_traverse.
17815 Marks the DIE of a given type in *SLOT as perennial, so it never gets
17816 marked as unused by prune_unused_types. */
17818 static int
17819 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
17821 tree type;
17822 dw_die_ref die;
17824 type = (tree) *slot;
17825 die = lookup_type_die (type);
17826 if (die != NULL)
17827 die->die_perennial_p = 1;
17828 return 1;
17831 /* Helper function of premark_types_used_by_global_vars which gets called
17832 through htab_traverse.
17834 Marks the DIE of a given type in *SLOT as perennial, so it never gets
17835 marked as unused by prune_unused_types. The DIE of the type is marked
17836 only if the global variable using the type will actually be emitted. */
17838 static int
17839 premark_types_used_by_global_vars_helper (void **slot,
17840 void *data ATTRIBUTE_UNUSED)
17842 struct types_used_by_vars_entry *entry;
17843 dw_die_ref die;
17845 entry = (struct types_used_by_vars_entry *) *slot;
17846 gcc_assert (entry->type != NULL
17847 && entry->var_decl != NULL);
17848 die = lookup_type_die (entry->type);
17849 if (die)
17851 /* Ask cgraph if the global variable really is to be emitted.
17852 If yes, then we'll keep the DIE of ENTRY->TYPE. */
17853 varpool_node *node = varpool_get_node (entry->var_decl);
17854 if (node && node->definition)
17856 die->die_perennial_p = 1;
17857 /* Keep the parent DIEs as well. */
17858 while ((die = die->die_parent) && die->die_perennial_p == 0)
17859 die->die_perennial_p = 1;
17862 return 1;
17865 /* Mark all members of used_types_hash as perennial. */
17867 static void
17868 premark_used_types (struct function *fun)
17870 if (fun && fun->used_types_hash)
17871 htab_traverse (fun->used_types_hash, premark_used_types_helper, NULL);
17874 /* Mark all members of types_used_by_vars_entry as perennial. */
17876 static void
17877 premark_types_used_by_global_vars (void)
17879 if (types_used_by_vars_hash)
17880 htab_traverse (types_used_by_vars_hash,
17881 premark_types_used_by_global_vars_helper, NULL);
17884 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
17885 for CA_LOC call arg loc node. */
17887 static dw_die_ref
17888 gen_call_site_die (tree decl, dw_die_ref subr_die,
17889 struct call_arg_loc_node *ca_loc)
17891 dw_die_ref stmt_die = NULL, die;
17892 tree block = ca_loc->block;
17894 while (block
17895 && block != DECL_INITIAL (decl)
17896 && TREE_CODE (block) == BLOCK)
17898 if (block_map.length () > BLOCK_NUMBER (block))
17899 stmt_die = block_map[BLOCK_NUMBER (block)];
17900 if (stmt_die)
17901 break;
17902 block = BLOCK_SUPERCONTEXT (block);
17904 if (stmt_die == NULL)
17905 stmt_die = subr_die;
17906 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
17907 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
17908 if (ca_loc->tail_call_p)
17909 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
17910 if (ca_loc->symbol_ref)
17912 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
17913 if (tdie)
17914 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
17915 else
17916 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
17918 return die;
17921 /* Generate a DIE to represent a declared function (either file-scope or
17922 block-local). */
17924 static void
17925 gen_subprogram_die (tree decl, dw_die_ref context_die)
17927 tree origin = decl_ultimate_origin (decl);
17928 dw_die_ref subr_die;
17929 tree outer_scope;
17930 dw_die_ref old_die = lookup_decl_die (decl);
17931 int declaration = (current_function_decl != decl
17932 || class_or_namespace_scope_p (context_die));
17934 premark_used_types (DECL_STRUCT_FUNCTION (decl));
17936 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
17937 started to generate the abstract instance of an inline, decided to output
17938 its containing class, and proceeded to emit the declaration of the inline
17939 from the member list for the class. If so, DECLARATION takes priority;
17940 we'll get back to the abstract instance when done with the class. */
17942 /* The class-scope declaration DIE must be the primary DIE. */
17943 if (origin && declaration && class_or_namespace_scope_p (context_die))
17945 origin = NULL;
17946 gcc_assert (!old_die);
17949 /* Now that the C++ front end lazily declares artificial member fns, we
17950 might need to retrofit the declaration into its class. */
17951 if (!declaration && !origin && !old_die
17952 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
17953 && !class_or_namespace_scope_p (context_die)
17954 && debug_info_level > DINFO_LEVEL_TERSE)
17955 old_die = force_decl_die (decl);
17957 if (origin != NULL)
17959 gcc_assert (!declaration || local_scope_p (context_die));
17961 /* Fixup die_parent for the abstract instance of a nested
17962 inline function. */
17963 if (old_die && old_die->die_parent == NULL)
17964 add_child_die (context_die, old_die);
17966 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17967 add_abstract_origin_attribute (subr_die, origin);
17968 /* This is where the actual code for a cloned function is.
17969 Let's emit linkage name attribute for it. This helps
17970 debuggers to e.g, set breakpoints into
17971 constructors/destructors when the user asks "break
17972 K::K". */
17973 add_linkage_name (subr_die, decl);
17975 else if (old_die)
17977 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17978 struct dwarf_file_data * file_index = lookup_filename (s.file);
17980 if (!get_AT_flag (old_die, DW_AT_declaration)
17981 /* We can have a normal definition following an inline one in the
17982 case of redefinition of GNU C extern inlines.
17983 It seems reasonable to use AT_specification in this case. */
17984 && !get_AT (old_die, DW_AT_inline))
17986 /* Detect and ignore this case, where we are trying to output
17987 something we have already output. */
17988 return;
17991 /* If the definition comes from the same place as the declaration,
17992 maybe use the old DIE. We always want the DIE for this function
17993 that has the *_pc attributes to be under comp_unit_die so the
17994 debugger can find it. We also need to do this for abstract
17995 instances of inlines, since the spec requires the out-of-line copy
17996 to have the same parent. For local class methods, this doesn't
17997 apply; we just use the old DIE. */
17998 if ((is_cu_die (old_die->die_parent) || context_die == NULL)
17999 && (DECL_ARTIFICIAL (decl)
18000 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18001 && (get_AT_unsigned (old_die, DW_AT_decl_line)
18002 == (unsigned) s.line))))
18004 subr_die = old_die;
18006 /* Clear out the declaration attribute and the formal parameters.
18007 Do not remove all children, because it is possible that this
18008 declaration die was forced using force_decl_die(). In such
18009 cases die that forced declaration die (e.g. TAG_imported_module)
18010 is one of the children that we do not want to remove. */
18011 remove_AT (subr_die, DW_AT_declaration);
18012 remove_AT (subr_die, DW_AT_object_pointer);
18013 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18015 else
18017 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18018 add_AT_specification (subr_die, old_die);
18019 add_pubname (decl, subr_die);
18020 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18021 add_AT_file (subr_die, DW_AT_decl_file, file_index);
18022 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18023 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18025 /* If the prototype had an 'auto' return type, emit the real
18026 type on the definition die. */
18027 if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE
18028 && get_AT_ref (old_die, DW_AT_type) == auto_die)
18029 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18030 0, 0, context_die);
18033 else
18035 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18037 if (TREE_PUBLIC (decl))
18038 add_AT_flag (subr_die, DW_AT_external, 1);
18040 add_name_and_src_coords_attributes (subr_die, decl);
18041 add_pubname (decl, subr_die);
18042 if (debug_info_level > DINFO_LEVEL_TERSE)
18044 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18045 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18046 0, 0, context_die);
18049 add_pure_or_virtual_attribute (subr_die, decl);
18050 if (DECL_ARTIFICIAL (decl))
18051 add_AT_flag (subr_die, DW_AT_artificial, 1);
18053 add_accessibility_attribute (subr_die, decl);
18056 if (declaration)
18058 if (!old_die || !get_AT (old_die, DW_AT_inline))
18060 add_AT_flag (subr_die, DW_AT_declaration, 1);
18062 /* If this is an explicit function declaration then generate
18063 a DW_AT_explicit attribute. */
18064 if (lang_hooks.decls.function_decl_explicit_p (decl)
18065 && (dwarf_version >= 3 || !dwarf_strict))
18066 add_AT_flag (subr_die, DW_AT_explicit, 1);
18068 /* The first time we see a member function, it is in the context of
18069 the class to which it belongs. We make sure of this by emitting
18070 the class first. The next time is the definition, which is
18071 handled above. The two may come from the same source text.
18073 Note that force_decl_die() forces function declaration die. It is
18074 later reused to represent definition. */
18075 equate_decl_number_to_die (decl, subr_die);
18078 else if (DECL_ABSTRACT (decl))
18080 if (DECL_DECLARED_INLINE_P (decl))
18082 if (cgraph_function_possibly_inlined_p (decl))
18083 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18084 else
18085 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18087 else
18089 if (cgraph_function_possibly_inlined_p (decl))
18090 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18091 else
18092 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18095 if (DECL_DECLARED_INLINE_P (decl)
18096 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18097 add_AT_flag (subr_die, DW_AT_artificial, 1);
18099 equate_decl_number_to_die (decl, subr_die);
18101 else if (!DECL_EXTERNAL (decl))
18103 HOST_WIDE_INT cfa_fb_offset;
18104 struct function *fun = DECL_STRUCT_FUNCTION (decl);
18106 if (!old_die || !get_AT (old_die, DW_AT_inline))
18107 equate_decl_number_to_die (decl, subr_die);
18109 gcc_checking_assert (fun);
18110 if (!flag_reorder_blocks_and_partition)
18112 dw_fde_ref fde = fun->fde;
18113 if (fde->dw_fde_begin)
18115 /* We have already generated the labels. */
18116 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18117 fde->dw_fde_end, false);
18119 else
18121 /* Create start/end labels and add the range. */
18122 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
18123 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
18124 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
18125 current_function_funcdef_no);
18126 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
18127 current_function_funcdef_no);
18128 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
18129 false);
18132 #if VMS_DEBUGGING_INFO
18133 /* HP OpenVMS Industry Standard 64: DWARF Extensions
18134 Section 2.3 Prologue and Epilogue Attributes:
18135 When a breakpoint is set on entry to a function, it is generally
18136 desirable for execution to be suspended, not on the very first
18137 instruction of the function, but rather at a point after the
18138 function's frame has been set up, after any language defined local
18139 declaration processing has been completed, and before execution of
18140 the first statement of the function begins. Debuggers generally
18141 cannot properly determine where this point is. Similarly for a
18142 breakpoint set on exit from a function. The prologue and epilogue
18143 attributes allow a compiler to communicate the location(s) to use. */
18146 if (fde->dw_fde_vms_end_prologue)
18147 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18148 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18150 if (fde->dw_fde_vms_begin_epilogue)
18151 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18152 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18154 #endif
18157 else
18159 /* Generate pubnames entries for the split function code ranges. */
18160 dw_fde_ref fde = fun->fde;
18162 if (fde->dw_fde_second_begin)
18164 if (dwarf_version >= 3 || !dwarf_strict)
18166 /* We should use ranges for non-contiguous code section
18167 addresses. Use the actual code range for the initial
18168 section, since the HOT/COLD labels might precede an
18169 alignment offset. */
18170 bool range_list_added = false;
18171 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
18172 fde->dw_fde_end, &range_list_added,
18173 false);
18174 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
18175 fde->dw_fde_second_end,
18176 &range_list_added, false);
18177 if (range_list_added)
18178 add_ranges (NULL);
18180 else
18182 /* There is no real support in DW2 for this .. so we make
18183 a work-around. First, emit the pub name for the segment
18184 containing the function label. Then make and emit a
18185 simplified subprogram DIE for the second segment with the
18186 name pre-fixed by __hot/cold_sect_of_. We use the same
18187 linkage name for the second die so that gdb will find both
18188 sections when given "b foo". */
18189 const char *name = NULL;
18190 tree decl_name = DECL_NAME (decl);
18191 dw_die_ref seg_die;
18193 /* Do the 'primary' section. */
18194 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18195 fde->dw_fde_end, false);
18197 /* Build a minimal DIE for the secondary section. */
18198 seg_die = new_die (DW_TAG_subprogram,
18199 subr_die->die_parent, decl);
18201 if (TREE_PUBLIC (decl))
18202 add_AT_flag (seg_die, DW_AT_external, 1);
18204 if (decl_name != NULL
18205 && IDENTIFIER_POINTER (decl_name) != NULL)
18207 name = dwarf2_name (decl, 1);
18208 if (! DECL_ARTIFICIAL (decl))
18209 add_src_coords_attributes (seg_die, decl);
18211 add_linkage_name (seg_die, decl);
18213 gcc_assert (name != NULL);
18214 add_pure_or_virtual_attribute (seg_die, decl);
18215 if (DECL_ARTIFICIAL (decl))
18216 add_AT_flag (seg_die, DW_AT_artificial, 1);
18218 name = concat ("__second_sect_of_", name, NULL);
18219 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
18220 fde->dw_fde_second_end, false);
18221 add_name_attribute (seg_die, name);
18222 if (want_pubnames ())
18223 add_pubname_string (name, seg_die);
18226 else
18227 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
18228 false);
18231 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
18233 /* We define the "frame base" as the function's CFA. This is more
18234 convenient for several reasons: (1) It's stable across the prologue
18235 and epilogue, which makes it better than just a frame pointer,
18236 (2) With dwarf3, there exists a one-byte encoding that allows us
18237 to reference the .debug_frame data by proxy, but failing that,
18238 (3) We can at least reuse the code inspection and interpretation
18239 code that determines the CFA position at various points in the
18240 function. */
18241 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
18243 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
18244 add_AT_loc (subr_die, DW_AT_frame_base, op);
18246 else
18248 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
18249 if (list->dw_loc_next)
18250 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
18251 else
18252 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
18255 /* Compute a displacement from the "steady-state frame pointer" to
18256 the CFA. The former is what all stack slots and argument slots
18257 will reference in the rtl; the latter is what we've told the
18258 debugger about. We'll need to adjust all frame_base references
18259 by this displacement. */
18260 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
18262 if (fun->static_chain_decl)
18263 add_AT_location_description (subr_die, DW_AT_static_link,
18264 loc_list_from_tree (fun->static_chain_decl, 2));
18267 /* Generate child dies for template paramaters. */
18268 if (debug_info_level > DINFO_LEVEL_TERSE)
18269 gen_generic_params_dies (decl);
18271 /* Now output descriptions of the arguments for this function. This gets
18272 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
18273 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
18274 `...' at the end of the formal parameter list. In order to find out if
18275 there was a trailing ellipsis or not, we must instead look at the type
18276 associated with the FUNCTION_DECL. This will be a node of type
18277 FUNCTION_TYPE. If the chain of type nodes hanging off of this
18278 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
18279 an ellipsis at the end. */
18281 /* In the case where we are describing a mere function declaration, all we
18282 need to do here (and all we *can* do here) is to describe the *types* of
18283 its formal parameters. */
18284 if (debug_info_level <= DINFO_LEVEL_TERSE)
18286 else if (declaration)
18287 gen_formal_types_die (decl, subr_die);
18288 else
18290 /* Generate DIEs to represent all known formal parameters. */
18291 tree parm = DECL_ARGUMENTS (decl);
18292 tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
18293 tree generic_decl_parm = generic_decl
18294 ? DECL_ARGUMENTS (generic_decl)
18295 : NULL;
18297 /* Now we want to walk the list of parameters of the function and
18298 emit their relevant DIEs.
18300 We consider the case of DECL being an instance of a generic function
18301 as well as it being a normal function.
18303 If DECL is an instance of a generic function we walk the
18304 parameters of the generic function declaration _and_ the parameters of
18305 DECL itself. This is useful because we want to emit specific DIEs for
18306 function parameter packs and those are declared as part of the
18307 generic function declaration. In that particular case,
18308 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
18309 That DIE has children DIEs representing the set of arguments
18310 of the pack. Note that the set of pack arguments can be empty.
18311 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
18312 children DIE.
18314 Otherwise, we just consider the parameters of DECL. */
18315 while (generic_decl_parm || parm)
18317 if (generic_decl_parm
18318 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
18319 gen_formal_parameter_pack_die (generic_decl_parm,
18320 parm, subr_die,
18321 &parm);
18322 else if (parm)
18324 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
18326 if (parm == DECL_ARGUMENTS (decl)
18327 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
18328 && parm_die
18329 && (dwarf_version >= 3 || !dwarf_strict))
18330 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
18332 parm = DECL_CHAIN (parm);
18335 if (generic_decl_parm)
18336 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
18339 /* Decide whether we need an unspecified_parameters DIE at the end.
18340 There are 2 more cases to do this for: 1) the ansi ... declaration -
18341 this is detectable when the end of the arg list is not a
18342 void_type_node 2) an unprototyped function declaration (not a
18343 definition). This just means that we have no info about the
18344 parameters at all. */
18345 if (prototype_p (TREE_TYPE (decl)))
18347 /* This is the prototyped case, check for.... */
18348 if (stdarg_p (TREE_TYPE (decl)))
18349 gen_unspecified_parameters_die (decl, subr_die);
18351 else if (DECL_INITIAL (decl) == NULL_TREE)
18352 gen_unspecified_parameters_die (decl, subr_die);
18355 /* Output Dwarf info for all of the stuff within the body of the function
18356 (if it has one - it may be just a declaration). */
18357 outer_scope = DECL_INITIAL (decl);
18359 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
18360 a function. This BLOCK actually represents the outermost binding contour
18361 for the function, i.e. the contour in which the function's formal
18362 parameters and labels get declared. Curiously, it appears that the front
18363 end doesn't actually put the PARM_DECL nodes for the current function onto
18364 the BLOCK_VARS list for this outer scope, but are strung off of the
18365 DECL_ARGUMENTS list for the function instead.
18367 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
18368 the LABEL_DECL nodes for the function however, and we output DWARF info
18369 for those in decls_for_scope. Just within the `outer_scope' there will be
18370 a BLOCK node representing the function's outermost pair of curly braces,
18371 and any blocks used for the base and member initializers of a C++
18372 constructor function. */
18373 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
18375 int call_site_note_count = 0;
18376 int tail_call_site_note_count = 0;
18378 /* Emit a DW_TAG_variable DIE for a named return value. */
18379 if (DECL_NAME (DECL_RESULT (decl)))
18380 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
18382 current_function_has_inlines = 0;
18383 decls_for_scope (outer_scope, subr_die, 0);
18385 if (call_arg_locations && !dwarf_strict)
18387 struct call_arg_loc_node *ca_loc;
18388 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
18390 dw_die_ref die = NULL;
18391 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
18392 rtx arg, next_arg;
18394 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
18395 arg; arg = next_arg)
18397 dw_loc_descr_ref reg, val;
18398 enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
18399 dw_die_ref cdie, tdie = NULL;
18401 next_arg = XEXP (arg, 1);
18402 if (REG_P (XEXP (XEXP (arg, 0), 0))
18403 && next_arg
18404 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
18405 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
18406 && REGNO (XEXP (XEXP (arg, 0), 0))
18407 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
18408 next_arg = XEXP (next_arg, 1);
18409 if (mode == VOIDmode)
18411 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
18412 if (mode == VOIDmode)
18413 mode = GET_MODE (XEXP (arg, 0));
18415 if (mode == VOIDmode || mode == BLKmode)
18416 continue;
18417 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
18419 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18420 tloc = XEXP (XEXP (arg, 0), 1);
18421 continue;
18423 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
18424 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
18426 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18427 tlocc = XEXP (XEXP (arg, 0), 1);
18428 continue;
18430 reg = NULL;
18431 if (REG_P (XEXP (XEXP (arg, 0), 0)))
18432 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
18433 VAR_INIT_STATUS_INITIALIZED);
18434 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
18436 rtx mem = XEXP (XEXP (arg, 0), 0);
18437 reg = mem_loc_descriptor (XEXP (mem, 0),
18438 get_address_mode (mem),
18439 GET_MODE (mem),
18440 VAR_INIT_STATUS_INITIALIZED);
18442 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
18443 == DEBUG_PARAMETER_REF)
18445 tree tdecl
18446 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
18447 tdie = lookup_decl_die (tdecl);
18448 if (tdie == NULL)
18449 continue;
18451 else
18452 continue;
18453 if (reg == NULL
18454 && GET_CODE (XEXP (XEXP (arg, 0), 0))
18455 != DEBUG_PARAMETER_REF)
18456 continue;
18457 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
18458 VOIDmode,
18459 VAR_INIT_STATUS_INITIALIZED);
18460 if (val == NULL)
18461 continue;
18462 if (die == NULL)
18463 die = gen_call_site_die (decl, subr_die, ca_loc);
18464 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
18465 NULL_TREE);
18466 if (reg != NULL)
18467 add_AT_loc (cdie, DW_AT_location, reg);
18468 else if (tdie != NULL)
18469 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
18470 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
18471 if (next_arg != XEXP (arg, 1))
18473 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
18474 if (mode == VOIDmode)
18475 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
18476 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
18477 0), 1),
18478 mode, VOIDmode,
18479 VAR_INIT_STATUS_INITIALIZED);
18480 if (val != NULL)
18481 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
18484 if (die == NULL
18485 && (ca_loc->symbol_ref || tloc))
18486 die = gen_call_site_die (decl, subr_die, ca_loc);
18487 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
18489 dw_loc_descr_ref tval = NULL;
18491 if (tloc != NULL_RTX)
18492 tval = mem_loc_descriptor (tloc,
18493 GET_MODE (tloc) == VOIDmode
18494 ? Pmode : GET_MODE (tloc),
18495 VOIDmode,
18496 VAR_INIT_STATUS_INITIALIZED);
18497 if (tval)
18498 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
18499 else if (tlocc != NULL_RTX)
18501 tval = mem_loc_descriptor (tlocc,
18502 GET_MODE (tlocc) == VOIDmode
18503 ? Pmode : GET_MODE (tlocc),
18504 VOIDmode,
18505 VAR_INIT_STATUS_INITIALIZED);
18506 if (tval)
18507 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
18508 tval);
18511 if (die != NULL)
18513 call_site_note_count++;
18514 if (ca_loc->tail_call_p)
18515 tail_call_site_note_count++;
18519 call_arg_locations = NULL;
18520 call_arg_loc_last = NULL;
18521 if (tail_call_site_count >= 0
18522 && tail_call_site_count == tail_call_site_note_count
18523 && !dwarf_strict)
18525 if (call_site_count >= 0
18526 && call_site_count == call_site_note_count)
18527 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
18528 else
18529 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
18531 call_site_count = -1;
18532 tail_call_site_count = -1;
18535 if (subr_die != old_die)
18536 /* Add the calling convention attribute if requested. */
18537 add_calling_convention_attribute (subr_die, decl);
18540 /* Returns a hash value for X (which really is a die_struct). */
18542 static hashval_t
18543 common_block_die_table_hash (const void *x)
18545 const_dw_die_ref d = (const_dw_die_ref) x;
18546 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
18549 /* Return nonzero if decl_id and die_parent of die_struct X is the same
18550 as decl_id and die_parent of die_struct Y. */
18552 static int
18553 common_block_die_table_eq (const void *x, const void *y)
18555 const_dw_die_ref d = (const_dw_die_ref) x;
18556 const_dw_die_ref e = (const_dw_die_ref) y;
18557 return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
18560 /* Generate a DIE to represent a declared data object.
18561 Either DECL or ORIGIN must be non-null. */
18563 static void
18564 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
18566 HOST_WIDE_INT off = 0;
18567 tree com_decl;
18568 tree decl_or_origin = decl ? decl : origin;
18569 tree ultimate_origin;
18570 dw_die_ref var_die;
18571 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
18572 dw_die_ref origin_die;
18573 bool declaration = (DECL_EXTERNAL (decl_or_origin)
18574 || class_or_namespace_scope_p (context_die));
18575 bool specialization_p = false;
18577 ultimate_origin = decl_ultimate_origin (decl_or_origin);
18578 if (decl || ultimate_origin)
18579 origin = ultimate_origin;
18580 com_decl = fortran_common (decl_or_origin, &off);
18582 /* Symbol in common gets emitted as a child of the common block, in the form
18583 of a data member. */
18584 if (com_decl)
18586 dw_die_ref com_die;
18587 dw_loc_list_ref loc;
18588 die_node com_die_arg;
18590 var_die = lookup_decl_die (decl_or_origin);
18591 if (var_die)
18593 if (get_AT (var_die, DW_AT_location) == NULL)
18595 loc = loc_list_from_tree (com_decl, off ? 1 : 2);
18596 if (loc)
18598 if (off)
18600 /* Optimize the common case. */
18601 if (single_element_loc_list_p (loc)
18602 && loc->expr->dw_loc_opc == DW_OP_addr
18603 && loc->expr->dw_loc_next == NULL
18604 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
18605 == SYMBOL_REF)
18607 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18608 loc->expr->dw_loc_oprnd1.v.val_addr
18609 = plus_constant (GET_MODE (x), x , off);
18611 else
18612 loc_list_plus_const (loc, off);
18614 add_AT_location_description (var_die, DW_AT_location, loc);
18615 remove_AT (var_die, DW_AT_declaration);
18618 return;
18621 if (common_block_die_table == NULL)
18622 common_block_die_table
18623 = htab_create_ggc (10, common_block_die_table_hash,
18624 common_block_die_table_eq, NULL);
18626 com_die_arg.decl_id = DECL_UID (com_decl);
18627 com_die_arg.die_parent = context_die;
18628 com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
18629 loc = loc_list_from_tree (com_decl, 2);
18630 if (com_die == NULL)
18632 const char *cnam
18633 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
18634 void **slot;
18636 com_die = new_die (DW_TAG_common_block, context_die, decl);
18637 add_name_and_src_coords_attributes (com_die, com_decl);
18638 if (loc)
18640 add_AT_location_description (com_die, DW_AT_location, loc);
18641 /* Avoid sharing the same loc descriptor between
18642 DW_TAG_common_block and DW_TAG_variable. */
18643 loc = loc_list_from_tree (com_decl, 2);
18645 else if (DECL_EXTERNAL (decl))
18646 add_AT_flag (com_die, DW_AT_declaration, 1);
18647 if (want_pubnames ())
18648 add_pubname_string (cnam, com_die); /* ??? needed? */
18649 com_die->decl_id = DECL_UID (com_decl);
18650 slot = htab_find_slot (common_block_die_table, com_die, INSERT);
18651 *slot = (void *) com_die;
18653 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
18655 add_AT_location_description (com_die, DW_AT_location, loc);
18656 loc = loc_list_from_tree (com_decl, 2);
18657 remove_AT (com_die, DW_AT_declaration);
18659 var_die = new_die (DW_TAG_variable, com_die, decl);
18660 add_name_and_src_coords_attributes (var_die, decl);
18661 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
18662 TREE_THIS_VOLATILE (decl), context_die);
18663 add_AT_flag (var_die, DW_AT_external, 1);
18664 if (loc)
18666 if (off)
18668 /* Optimize the common case. */
18669 if (single_element_loc_list_p (loc)
18670 && loc->expr->dw_loc_opc == DW_OP_addr
18671 && loc->expr->dw_loc_next == NULL
18672 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
18674 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18675 loc->expr->dw_loc_oprnd1.v.val_addr
18676 = plus_constant (GET_MODE (x), x, off);
18678 else
18679 loc_list_plus_const (loc, off);
18681 add_AT_location_description (var_die, DW_AT_location, loc);
18683 else if (DECL_EXTERNAL (decl))
18684 add_AT_flag (var_die, DW_AT_declaration, 1);
18685 equate_decl_number_to_die (decl, var_die);
18686 return;
18689 /* If the compiler emitted a definition for the DECL declaration
18690 and if we already emitted a DIE for it, don't emit a second
18691 DIE for it again. Allow re-declarations of DECLs that are
18692 inside functions, though. */
18693 if (old_die && declaration && !local_scope_p (context_die))
18694 return;
18696 /* For static data members, the declaration in the class is supposed
18697 to have DW_TAG_member tag; the specification should still be
18698 DW_TAG_variable referencing the DW_TAG_member DIE. */
18699 if (declaration && class_scope_p (context_die))
18700 var_die = new_die (DW_TAG_member, context_die, decl);
18701 else
18702 var_die = new_die (DW_TAG_variable, context_die, decl);
18704 origin_die = NULL;
18705 if (origin != NULL)
18706 origin_die = add_abstract_origin_attribute (var_die, origin);
18708 /* Loop unrolling can create multiple blocks that refer to the same
18709 static variable, so we must test for the DW_AT_declaration flag.
18711 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
18712 copy decls and set the DECL_ABSTRACT flag on them instead of
18713 sharing them.
18715 ??? Duplicated blocks have been rewritten to use .debug_ranges.
18717 ??? The declare_in_namespace support causes us to get two DIEs for one
18718 variable, both of which are declarations. We want to avoid considering
18719 one to be a specification, so we must test that this DIE is not a
18720 declaration. */
18721 else if (old_die && TREE_STATIC (decl) && ! declaration
18722 && get_AT_flag (old_die, DW_AT_declaration) == 1)
18724 /* This is a definition of a C++ class level static. */
18725 add_AT_specification (var_die, old_die);
18726 specialization_p = true;
18727 if (DECL_NAME (decl))
18729 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18730 struct dwarf_file_data * file_index = lookup_filename (s.file);
18732 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18733 add_AT_file (var_die, DW_AT_decl_file, file_index);
18735 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18736 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
18738 if (old_die->die_tag == DW_TAG_member)
18739 add_linkage_name (var_die, decl);
18742 else
18743 add_name_and_src_coords_attributes (var_die, decl);
18745 if ((origin == NULL && !specialization_p)
18746 || (origin != NULL
18747 && !DECL_ABSTRACT (decl_or_origin)
18748 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
18749 decl_function_context
18750 (decl_or_origin))))
18752 tree type = TREE_TYPE (decl_or_origin);
18754 if (decl_by_reference_p (decl_or_origin))
18755 add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
18756 else
18757 add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
18758 TREE_THIS_VOLATILE (decl_or_origin), context_die);
18761 if (origin == NULL && !specialization_p)
18763 if (TREE_PUBLIC (decl))
18764 add_AT_flag (var_die, DW_AT_external, 1);
18766 if (DECL_ARTIFICIAL (decl))
18767 add_AT_flag (var_die, DW_AT_artificial, 1);
18769 add_accessibility_attribute (var_die, decl);
18772 if (declaration)
18773 add_AT_flag (var_die, DW_AT_declaration, 1);
18775 if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
18776 equate_decl_number_to_die (decl, var_die);
18778 if (! declaration
18779 && (! DECL_ABSTRACT (decl_or_origin)
18780 /* Local static vars are shared between all clones/inlines,
18781 so emit DW_AT_location on the abstract DIE if DECL_RTL is
18782 already set. */
18783 || (TREE_CODE (decl_or_origin) == VAR_DECL
18784 && TREE_STATIC (decl_or_origin)
18785 && DECL_RTL_SET_P (decl_or_origin)))
18786 /* When abstract origin already has DW_AT_location attribute, no need
18787 to add it again. */
18788 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
18790 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
18791 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
18792 defer_location (decl_or_origin, var_die);
18793 else
18794 add_location_or_const_value_attribute (var_die, decl_or_origin,
18795 decl == NULL, DW_AT_location);
18796 add_pubname (decl_or_origin, var_die);
18798 else
18799 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
18802 /* Generate a DIE to represent a named constant. */
18804 static void
18805 gen_const_die (tree decl, dw_die_ref context_die)
18807 dw_die_ref const_die;
18808 tree type = TREE_TYPE (decl);
18810 const_die = new_die (DW_TAG_constant, context_die, decl);
18811 add_name_and_src_coords_attributes (const_die, decl);
18812 add_type_attribute (const_die, type, 1, 0, context_die);
18813 if (TREE_PUBLIC (decl))
18814 add_AT_flag (const_die, DW_AT_external, 1);
18815 if (DECL_ARTIFICIAL (decl))
18816 add_AT_flag (const_die, DW_AT_artificial, 1);
18817 tree_add_const_value_attribute_for_decl (const_die, decl);
18820 /* Generate a DIE to represent a label identifier. */
18822 static void
18823 gen_label_die (tree decl, dw_die_ref context_die)
18825 tree origin = decl_ultimate_origin (decl);
18826 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
18827 rtx insn;
18828 char label[MAX_ARTIFICIAL_LABEL_BYTES];
18830 if (origin != NULL)
18831 add_abstract_origin_attribute (lbl_die, origin);
18832 else
18833 add_name_and_src_coords_attributes (lbl_die, decl);
18835 if (DECL_ABSTRACT (decl))
18836 equate_decl_number_to_die (decl, lbl_die);
18837 else
18839 insn = DECL_RTL_IF_SET (decl);
18841 /* Deleted labels are programmer specified labels which have been
18842 eliminated because of various optimizations. We still emit them
18843 here so that it is possible to put breakpoints on them. */
18844 if (insn
18845 && (LABEL_P (insn)
18846 || ((NOTE_P (insn)
18847 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
18849 /* When optimization is enabled (via -O) some parts of the compiler
18850 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
18851 represent source-level labels which were explicitly declared by
18852 the user. This really shouldn't be happening though, so catch
18853 it if it ever does happen. */
18854 gcc_assert (!INSN_DELETED_P (insn));
18856 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
18857 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18859 else if (insn
18860 && NOTE_P (insn)
18861 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
18862 && CODE_LABEL_NUMBER (insn) != -1)
18864 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
18865 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18870 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
18871 attributes to the DIE for a block STMT, to describe where the inlined
18872 function was called from. This is similar to add_src_coords_attributes. */
18874 static inline void
18875 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
18877 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
18879 if (dwarf_version >= 3 || !dwarf_strict)
18881 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
18882 add_AT_unsigned (die, DW_AT_call_line, s.line);
18887 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
18888 Add low_pc and high_pc attributes to the DIE for a block STMT. */
18890 static inline void
18891 add_high_low_attributes (tree stmt, dw_die_ref die)
18893 char label[MAX_ARTIFICIAL_LABEL_BYTES];
18895 if (BLOCK_FRAGMENT_CHAIN (stmt)
18896 && (dwarf_version >= 3 || !dwarf_strict))
18898 tree chain, superblock = NULL_TREE;
18899 dw_die_ref pdie;
18900 dw_attr_ref attr = NULL;
18902 if (inlined_function_outer_scope_p (stmt))
18904 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18905 BLOCK_NUMBER (stmt));
18906 add_AT_lbl_id (die, DW_AT_entry_pc, label);
18909 /* Optimize duplicate .debug_ranges lists or even tails of
18910 lists. If this BLOCK has same ranges as its supercontext,
18911 lookup DW_AT_ranges attribute in the supercontext (and
18912 recursively so), verify that the ranges_table contains the
18913 right values and use it instead of adding a new .debug_range. */
18914 for (chain = stmt, pdie = die;
18915 BLOCK_SAME_RANGE (chain);
18916 chain = BLOCK_SUPERCONTEXT (chain))
18918 dw_attr_ref new_attr;
18920 pdie = pdie->die_parent;
18921 if (pdie == NULL)
18922 break;
18923 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
18924 break;
18925 new_attr = get_AT (pdie, DW_AT_ranges);
18926 if (new_attr == NULL
18927 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
18928 break;
18929 attr = new_attr;
18930 superblock = BLOCK_SUPERCONTEXT (chain);
18932 if (attr != NULL
18933 && (ranges_table[attr->dw_attr_val.v.val_offset
18934 / 2 / DWARF2_ADDR_SIZE].num
18935 == BLOCK_NUMBER (superblock))
18936 && BLOCK_FRAGMENT_CHAIN (superblock))
18938 unsigned long off = attr->dw_attr_val.v.val_offset
18939 / 2 / DWARF2_ADDR_SIZE;
18940 unsigned long supercnt = 0, thiscnt = 0;
18941 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
18942 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
18944 ++supercnt;
18945 gcc_checking_assert (ranges_table[off + supercnt].num
18946 == BLOCK_NUMBER (chain));
18948 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
18949 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
18950 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
18951 ++thiscnt;
18952 gcc_assert (supercnt >= thiscnt);
18953 add_AT_range_list (die, DW_AT_ranges,
18954 ((off + supercnt - thiscnt)
18955 * 2 * DWARF2_ADDR_SIZE),
18956 false);
18957 return;
18960 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
18962 chain = BLOCK_FRAGMENT_CHAIN (stmt);
18965 add_ranges (chain);
18966 chain = BLOCK_FRAGMENT_CHAIN (chain);
18968 while (chain);
18969 add_ranges (NULL);
18971 else
18973 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
18974 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18975 BLOCK_NUMBER (stmt));
18976 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
18977 BLOCK_NUMBER (stmt));
18978 add_AT_low_high_pc (die, label, label_high, false);
18982 /* Generate a DIE for a lexical block. */
18984 static void
18985 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
18987 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
18989 if (call_arg_locations)
18991 if (block_map.length () <= BLOCK_NUMBER (stmt))
18992 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
18993 block_map[BLOCK_NUMBER (stmt)] = stmt_die;
18996 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
18997 add_high_low_attributes (stmt, stmt_die);
18999 decls_for_scope (stmt, stmt_die, depth);
19002 /* Generate a DIE for an inlined subprogram. */
19004 static void
19005 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
19007 tree decl;
19009 /* The instance of function that is effectively being inlined shall not
19010 be abstract. */
19011 gcc_assert (! BLOCK_ABSTRACT (stmt));
19013 decl = block_ultimate_origin (stmt);
19015 /* Emit info for the abstract instance first, if we haven't yet. We
19016 must emit this even if the block is abstract, otherwise when we
19017 emit the block below (or elsewhere), we may end up trying to emit
19018 a die whose origin die hasn't been emitted, and crashing. */
19019 dwarf2out_abstract_function (decl);
19021 if (! BLOCK_ABSTRACT (stmt))
19023 dw_die_ref subr_die
19024 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19026 if (call_arg_locations)
19028 if (block_map.length () <= BLOCK_NUMBER (stmt))
19029 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19030 block_map[BLOCK_NUMBER (stmt)] = subr_die;
19032 add_abstract_origin_attribute (subr_die, decl);
19033 if (TREE_ASM_WRITTEN (stmt))
19034 add_high_low_attributes (stmt, subr_die);
19035 add_call_src_coords_attributes (stmt, subr_die);
19037 decls_for_scope (stmt, subr_die, depth);
19038 current_function_has_inlines = 1;
19042 /* Generate a DIE for a field in a record, or structure. */
19044 static void
19045 gen_field_die (tree decl, dw_die_ref context_die)
19047 dw_die_ref decl_die;
19049 if (TREE_TYPE (decl) == error_mark_node)
19050 return;
19052 decl_die = new_die (DW_TAG_member, context_die, decl);
19053 add_name_and_src_coords_attributes (decl_die, decl);
19054 add_type_attribute (decl_die, member_declared_type (decl),
19055 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
19056 context_die);
19058 if (DECL_BIT_FIELD_TYPE (decl))
19060 add_byte_size_attribute (decl_die, decl);
19061 add_bit_size_attribute (decl_die, decl);
19062 add_bit_offset_attribute (decl_die, decl);
19065 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19066 add_data_member_location_attribute (decl_die, decl);
19068 if (DECL_ARTIFICIAL (decl))
19069 add_AT_flag (decl_die, DW_AT_artificial, 1);
19071 add_accessibility_attribute (decl_die, decl);
19073 /* Equate decl number to die, so that we can look up this decl later on. */
19074 equate_decl_number_to_die (decl, decl_die);
19077 #if 0
19078 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19079 Use modified_type_die instead.
19080 We keep this code here just in case these types of DIEs may be needed to
19081 represent certain things in other languages (e.g. Pascal) someday. */
19083 static void
19084 gen_pointer_type_die (tree type, dw_die_ref context_die)
19086 dw_die_ref ptr_die
19087 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19089 equate_type_number_to_die (type, ptr_die);
19090 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19091 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19094 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19095 Use modified_type_die instead.
19096 We keep this code here just in case these types of DIEs may be needed to
19097 represent certain things in other languages (e.g. Pascal) someday. */
19099 static void
19100 gen_reference_type_die (tree type, dw_die_ref context_die)
19102 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
19104 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
19105 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
19106 else
19107 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
19109 equate_type_number_to_die (type, ref_die);
19110 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
19111 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19113 #endif
19115 /* Generate a DIE for a pointer to a member type. */
19117 static void
19118 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
19120 dw_die_ref ptr_die
19121 = new_die (DW_TAG_ptr_to_member_type,
19122 scope_die_for (type, context_die), type);
19124 equate_type_number_to_die (type, ptr_die);
19125 add_AT_die_ref (ptr_die, DW_AT_containing_type,
19126 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
19127 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19130 typedef const char *dchar_p; /* For DEF_VEC_P. */
19132 static char *producer_string;
19134 /* Return a heap allocated producer string including command line options
19135 if -grecord-gcc-switches. */
19137 static char *
19138 gen_producer_string (void)
19140 size_t j;
19141 auto_vec<dchar_p> switches;
19142 const char *language_string = lang_hooks.name;
19143 char *producer, *tail;
19144 const char *p;
19145 size_t len = dwarf_record_gcc_switches ? 0 : 3;
19146 size_t plen = strlen (language_string) + 1 + strlen (version_string);
19148 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
19149 switch (save_decoded_options[j].opt_index)
19151 case OPT_o:
19152 case OPT_d:
19153 case OPT_dumpbase:
19154 case OPT_dumpdir:
19155 case OPT_auxbase:
19156 case OPT_auxbase_strip:
19157 case OPT_quiet:
19158 case OPT_version:
19159 case OPT_v:
19160 case OPT_w:
19161 case OPT_L:
19162 case OPT_D:
19163 case OPT_I:
19164 case OPT_U:
19165 case OPT_SPECIAL_unknown:
19166 case OPT_SPECIAL_ignore:
19167 case OPT_SPECIAL_program_name:
19168 case OPT_SPECIAL_input_file:
19169 case OPT_grecord_gcc_switches:
19170 case OPT_gno_record_gcc_switches:
19171 case OPT__output_pch_:
19172 case OPT_fdiagnostics_show_location_:
19173 case OPT_fdiagnostics_show_option:
19174 case OPT_fdiagnostics_show_caret:
19175 case OPT_fdiagnostics_color_:
19176 case OPT_fverbose_asm:
19177 case OPT____:
19178 case OPT__sysroot_:
19179 case OPT_nostdinc:
19180 case OPT_nostdinc__:
19181 /* Ignore these. */
19182 continue;
19183 default:
19184 if (cl_options[save_decoded_options[j].opt_index].flags
19185 & CL_NO_DWARF_RECORD)
19186 continue;
19187 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
19188 == '-');
19189 switch (save_decoded_options[j].canonical_option[0][1])
19191 case 'M':
19192 case 'i':
19193 case 'W':
19194 continue;
19195 case 'f':
19196 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
19197 "dump", 4) == 0)
19198 continue;
19199 break;
19200 default:
19201 break;
19203 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
19204 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
19205 break;
19208 producer = XNEWVEC (char, plen + 1 + len + 1);
19209 tail = producer;
19210 sprintf (tail, "%s %s", language_string, version_string);
19211 tail += plen;
19213 FOR_EACH_VEC_ELT (switches, j, p)
19215 len = strlen (p);
19216 *tail = ' ';
19217 memcpy (tail + 1, p, len);
19218 tail += len + 1;
19221 *tail = '\0';
19222 return producer;
19225 /* Generate the DIE for the compilation unit. */
19227 static dw_die_ref
19228 gen_compile_unit_die (const char *filename)
19230 dw_die_ref die;
19231 const char *language_string = lang_hooks.name;
19232 int language;
19234 die = new_die (DW_TAG_compile_unit, NULL, NULL);
19236 if (filename)
19238 add_name_attribute (die, filename);
19239 /* Don't add cwd for <built-in>. */
19240 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
19241 add_comp_dir_attribute (die);
19244 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
19246 /* If our producer is LTO try to figure out a common language to use
19247 from the global list of translation units. */
19248 if (strcmp (language_string, "GNU GIMPLE") == 0)
19250 unsigned i;
19251 tree t;
19252 const char *common_lang = NULL;
19254 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
19256 if (!TRANSLATION_UNIT_LANGUAGE (t))
19257 continue;
19258 if (!common_lang)
19259 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
19260 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
19262 else if (strncmp (common_lang, "GNU C", 5) == 0
19263 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
19264 /* Mixing C and C++ is ok, use C++ in that case. */
19265 common_lang = "GNU C++";
19266 else
19268 /* Fall back to C. */
19269 common_lang = NULL;
19270 break;
19274 if (common_lang)
19275 language_string = common_lang;
19278 language = DW_LANG_C89;
19279 if (strcmp (language_string, "GNU C++") == 0)
19280 language = DW_LANG_C_plus_plus;
19281 else if (strcmp (language_string, "GNU F77") == 0)
19282 language = DW_LANG_Fortran77;
19283 else if (strcmp (language_string, "GNU Pascal") == 0)
19284 language = DW_LANG_Pascal83;
19285 else if (dwarf_version >= 3 || !dwarf_strict)
19287 if (strcmp (language_string, "GNU Ada") == 0)
19288 language = DW_LANG_Ada95;
19289 else if (strcmp (language_string, "GNU Fortran") == 0)
19290 language = DW_LANG_Fortran95;
19291 else if (strcmp (language_string, "GNU Java") == 0)
19292 language = DW_LANG_Java;
19293 else if (strcmp (language_string, "GNU Objective-C") == 0)
19294 language = DW_LANG_ObjC;
19295 else if (strcmp (language_string, "GNU Objective-C++") == 0)
19296 language = DW_LANG_ObjC_plus_plus;
19297 else if (dwarf_version >= 5 || !dwarf_strict)
19299 if (strcmp (language_string, "GNU Go") == 0)
19300 language = DW_LANG_Go;
19303 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
19304 else if (strcmp (language_string, "GNU Fortran") == 0)
19305 language = DW_LANG_Fortran90;
19307 add_AT_unsigned (die, DW_AT_language, language);
19309 switch (language)
19311 case DW_LANG_Fortran77:
19312 case DW_LANG_Fortran90:
19313 case DW_LANG_Fortran95:
19314 /* Fortran has case insensitive identifiers and the front-end
19315 lowercases everything. */
19316 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
19317 break;
19318 default:
19319 /* The default DW_ID_case_sensitive doesn't need to be specified. */
19320 break;
19322 return die;
19325 /* Generate the DIE for a base class. */
19327 static void
19328 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
19330 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
19332 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
19333 add_data_member_location_attribute (die, binfo);
19335 if (BINFO_VIRTUAL_P (binfo))
19336 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
19338 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19339 children, otherwise the default is DW_ACCESS_public. In DWARF2
19340 the default has always been DW_ACCESS_private. */
19341 if (access == access_public_node)
19343 if (dwarf_version == 2
19344 || context_die->die_tag == DW_TAG_class_type)
19345 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19347 else if (access == access_protected_node)
19348 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19349 else if (dwarf_version > 2
19350 && context_die->die_tag != DW_TAG_class_type)
19351 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19354 /* Generate a DIE for a class member. */
19356 static void
19357 gen_member_die (tree type, dw_die_ref context_die)
19359 tree member;
19360 tree binfo = TYPE_BINFO (type);
19361 dw_die_ref child;
19363 /* If this is not an incomplete type, output descriptions of each of its
19364 members. Note that as we output the DIEs necessary to represent the
19365 members of this record or union type, we will also be trying to output
19366 DIEs to represent the *types* of those members. However the `type'
19367 function (above) will specifically avoid generating type DIEs for member
19368 types *within* the list of member DIEs for this (containing) type except
19369 for those types (of members) which are explicitly marked as also being
19370 members of this (containing) type themselves. The g++ front- end can
19371 force any given type to be treated as a member of some other (containing)
19372 type by setting the TYPE_CONTEXT of the given (member) type to point to
19373 the TREE node representing the appropriate (containing) type. */
19375 /* First output info about the base classes. */
19376 if (binfo)
19378 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
19379 int i;
19380 tree base;
19382 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19383 gen_inheritance_die (base,
19384 (accesses ? (*accesses)[i] : access_public_node),
19385 context_die);
19388 /* Now output info about the data members and type members. */
19389 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19391 /* If we thought we were generating minimal debug info for TYPE
19392 and then changed our minds, some of the member declarations
19393 may have already been defined. Don't define them again, but
19394 do put them in the right order. */
19396 child = lookup_decl_die (member);
19397 if (child)
19398 splice_child_die (context_die, child);
19399 else
19400 gen_decl_die (member, NULL, context_die);
19403 /* Now output info about the function members (if any). */
19404 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19406 /* Don't include clones in the member list. */
19407 if (DECL_ABSTRACT_ORIGIN (member))
19408 continue;
19410 child = lookup_decl_die (member);
19411 if (child)
19412 splice_child_die (context_die, child);
19413 else
19414 gen_decl_die (member, NULL, context_die);
19418 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
19419 is set, we pretend that the type was never defined, so we only get the
19420 member DIEs needed by later specification DIEs. */
19422 static void
19423 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19424 enum debug_info_usage usage)
19426 dw_die_ref type_die = lookup_type_die (type);
19427 dw_die_ref scope_die = 0;
19428 int nested = 0;
19429 int complete = (TYPE_SIZE (type)
19430 && (! TYPE_STUB_DECL (type)
19431 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19432 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19433 complete = complete && should_emit_struct_debug (type, usage);
19435 if (type_die && ! complete)
19436 return;
19438 if (TYPE_CONTEXT (type) != NULL_TREE
19439 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19440 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19441 nested = 1;
19443 scope_die = scope_die_for (type, context_die);
19445 /* Generate child dies for template paramaters. */
19446 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
19447 schedule_generic_params_dies_gen (type);
19449 if (! type_die || (nested && is_cu_die (scope_die)))
19450 /* First occurrence of type or toplevel definition of nested class. */
19452 dw_die_ref old_die = type_die;
19454 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
19455 ? record_type_tag (type) : DW_TAG_union_type,
19456 scope_die, type);
19457 equate_type_number_to_die (type, type_die);
19458 if (old_die)
19459 add_AT_specification (type_die, old_die);
19460 else
19461 add_name_attribute (type_die, type_tag (type));
19463 else
19464 remove_AT (type_die, DW_AT_declaration);
19466 /* If this type has been completed, then give it a byte_size attribute and
19467 then give a list of members. */
19468 if (complete && !ns_decl)
19470 /* Prevent infinite recursion in cases where the type of some member of
19471 this type is expressed in terms of this type itself. */
19472 TREE_ASM_WRITTEN (type) = 1;
19473 add_byte_size_attribute (type_die, type);
19474 if (TYPE_STUB_DECL (type) != NULL_TREE)
19476 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19477 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19480 /* If the first reference to this type was as the return type of an
19481 inline function, then it may not have a parent. Fix this now. */
19482 if (type_die->die_parent == NULL)
19483 add_child_die (scope_die, type_die);
19485 push_decl_scope (type);
19486 gen_member_die (type, type_die);
19487 pop_decl_scope ();
19489 add_gnat_descriptive_type_attribute (type_die, type, context_die);
19490 if (TYPE_ARTIFICIAL (type))
19491 add_AT_flag (type_die, DW_AT_artificial, 1);
19493 /* GNU extension: Record what type our vtable lives in. */
19494 if (TYPE_VFIELD (type))
19496 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
19498 gen_type_die (vtype, context_die);
19499 add_AT_die_ref (type_die, DW_AT_containing_type,
19500 lookup_type_die (vtype));
19503 else
19505 add_AT_flag (type_die, DW_AT_declaration, 1);
19507 /* We don't need to do this for function-local types. */
19508 if (TYPE_STUB_DECL (type)
19509 && ! decl_function_context (TYPE_STUB_DECL (type)))
19510 vec_safe_push (incomplete_types, type);
19513 if (get_AT (type_die, DW_AT_name))
19514 add_pubtype (type, type_die);
19517 /* Generate a DIE for a subroutine _type_. */
19519 static void
19520 gen_subroutine_type_die (tree type, dw_die_ref context_die)
19522 tree return_type = TREE_TYPE (type);
19523 dw_die_ref subr_die
19524 = new_die (DW_TAG_subroutine_type,
19525 scope_die_for (type, context_die), type);
19527 equate_type_number_to_die (type, subr_die);
19528 add_prototyped_attribute (subr_die, type);
19529 add_type_attribute (subr_die, return_type, 0, 0, context_die);
19530 gen_formal_types_die (type, subr_die);
19532 if (get_AT (subr_die, DW_AT_name))
19533 add_pubtype (type, subr_die);
19536 /* Generate a DIE for a type definition. */
19538 static void
19539 gen_typedef_die (tree decl, dw_die_ref context_die)
19541 dw_die_ref type_die;
19542 tree origin;
19544 if (TREE_ASM_WRITTEN (decl))
19545 return;
19547 TREE_ASM_WRITTEN (decl) = 1;
19548 type_die = new_die (DW_TAG_typedef, context_die, decl);
19549 origin = decl_ultimate_origin (decl);
19550 if (origin != NULL)
19551 add_abstract_origin_attribute (type_die, origin);
19552 else
19554 tree type;
19556 add_name_and_src_coords_attributes (type_die, decl);
19557 if (DECL_ORIGINAL_TYPE (decl))
19559 type = DECL_ORIGINAL_TYPE (decl);
19561 gcc_assert (type != TREE_TYPE (decl));
19562 equate_type_number_to_die (TREE_TYPE (decl), type_die);
19564 else
19566 type = TREE_TYPE (decl);
19568 if (is_naming_typedef_decl (TYPE_NAME (type)))
19570 /* Here, we are in the case of decl being a typedef naming
19571 an anonymous type, e.g:
19572 typedef struct {...} foo;
19573 In that case TREE_TYPE (decl) is not a typedef variant
19574 type and TYPE_NAME of the anonymous type is set to the
19575 TYPE_DECL of the typedef. This construct is emitted by
19576 the C++ FE.
19578 TYPE is the anonymous struct named by the typedef
19579 DECL. As we need the DW_AT_type attribute of the
19580 DW_TAG_typedef to point to the DIE of TYPE, let's
19581 generate that DIE right away. add_type_attribute
19582 called below will then pick (via lookup_type_die) that
19583 anonymous struct DIE. */
19584 if (!TREE_ASM_WRITTEN (type))
19585 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
19587 /* This is a GNU Extension. We are adding a
19588 DW_AT_linkage_name attribute to the DIE of the
19589 anonymous struct TYPE. The value of that attribute
19590 is the name of the typedef decl naming the anonymous
19591 struct. This greatly eases the work of consumers of
19592 this debug info. */
19593 add_linkage_attr (lookup_type_die (type), decl);
19597 add_type_attribute (type_die, type, TREE_READONLY (decl),
19598 TREE_THIS_VOLATILE (decl), context_die);
19600 if (is_naming_typedef_decl (decl))
19601 /* We want that all subsequent calls to lookup_type_die with
19602 TYPE in argument yield the DW_TAG_typedef we have just
19603 created. */
19604 equate_type_number_to_die (type, type_die);
19606 add_accessibility_attribute (type_die, decl);
19609 if (DECL_ABSTRACT (decl))
19610 equate_decl_number_to_die (decl, type_die);
19612 if (get_AT (type_die, DW_AT_name))
19613 add_pubtype (decl, type_die);
19616 /* Generate a DIE for a struct, class, enum or union type. */
19618 static void
19619 gen_tagged_type_die (tree type,
19620 dw_die_ref context_die,
19621 enum debug_info_usage usage)
19623 int need_pop;
19625 if (type == NULL_TREE
19626 || !is_tagged_type (type))
19627 return;
19629 /* If this is a nested type whose containing class hasn't been written
19630 out yet, writing it out will cover this one, too. This does not apply
19631 to instantiations of member class templates; they need to be added to
19632 the containing class as they are generated. FIXME: This hurts the
19633 idea of combining type decls from multiple TUs, since we can't predict
19634 what set of template instantiations we'll get. */
19635 if (TYPE_CONTEXT (type)
19636 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19637 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
19639 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
19641 if (TREE_ASM_WRITTEN (type))
19642 return;
19644 /* If that failed, attach ourselves to the stub. */
19645 push_decl_scope (TYPE_CONTEXT (type));
19646 context_die = lookup_type_die (TYPE_CONTEXT (type));
19647 need_pop = 1;
19649 else if (TYPE_CONTEXT (type) != NULL_TREE
19650 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
19652 /* If this type is local to a function that hasn't been written
19653 out yet, use a NULL context for now; it will be fixed up in
19654 decls_for_scope. */
19655 context_die = lookup_decl_die (TYPE_CONTEXT (type));
19656 /* A declaration DIE doesn't count; nested types need to go in the
19657 specification. */
19658 if (context_die && is_declaration_die (context_die))
19659 context_die = NULL;
19660 need_pop = 0;
19662 else
19664 context_die = declare_in_namespace (type, context_die);
19665 need_pop = 0;
19668 if (TREE_CODE (type) == ENUMERAL_TYPE)
19670 /* This might have been written out by the call to
19671 declare_in_namespace. */
19672 if (!TREE_ASM_WRITTEN (type))
19673 gen_enumeration_type_die (type, context_die);
19675 else
19676 gen_struct_or_union_type_die (type, context_die, usage);
19678 if (need_pop)
19679 pop_decl_scope ();
19681 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
19682 it up if it is ever completed. gen_*_type_die will set it for us
19683 when appropriate. */
19686 /* Generate a type description DIE. */
19688 static void
19689 gen_type_die_with_usage (tree type, dw_die_ref context_die,
19690 enum debug_info_usage usage)
19692 struct array_descr_info info;
19694 if (type == NULL_TREE || type == error_mark_node)
19695 return;
19697 if (TYPE_NAME (type) != NULL_TREE
19698 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
19699 && is_redundant_typedef (TYPE_NAME (type))
19700 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
19701 /* The DECL of this type is a typedef we don't want to emit debug
19702 info for but we want debug info for its underlying typedef.
19703 This can happen for e.g, the injected-class-name of a C++
19704 type. */
19705 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
19707 /* If TYPE is a typedef type variant, let's generate debug info
19708 for the parent typedef which TYPE is a type of. */
19709 if (typedef_variant_p (type))
19711 if (TREE_ASM_WRITTEN (type))
19712 return;
19714 /* Prevent broken recursion; we can't hand off to the same type. */
19715 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
19717 /* Give typedefs the right scope. */
19718 context_die = scope_die_for (type, context_die);
19720 TREE_ASM_WRITTEN (type) = 1;
19722 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19723 return;
19726 /* If type is an anonymous tagged type named by a typedef, let's
19727 generate debug info for the typedef. */
19728 if (is_naming_typedef_decl (TYPE_NAME (type)))
19730 /* Use the DIE of the containing namespace as the parent DIE of
19731 the type description DIE we want to generate. */
19732 if (DECL_CONTEXT (TYPE_NAME (type))
19733 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
19734 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
19736 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19737 return;
19740 /* If this is an array type with hidden descriptor, handle it first. */
19741 if (!TREE_ASM_WRITTEN (type)
19742 && lang_hooks.types.get_array_descr_info
19743 && lang_hooks.types.get_array_descr_info (type, &info)
19744 && (dwarf_version >= 3 || !dwarf_strict))
19746 gen_descr_array_type_die (type, &info, context_die);
19747 TREE_ASM_WRITTEN (type) = 1;
19748 return;
19751 /* We are going to output a DIE to represent the unqualified version
19752 of this type (i.e. without any const or volatile qualifiers) so
19753 get the main variant (i.e. the unqualified version) of this type
19754 now. (Vectors are special because the debugging info is in the
19755 cloned type itself). */
19756 if (TREE_CODE (type) != VECTOR_TYPE)
19757 type = type_main_variant (type);
19759 if (TREE_ASM_WRITTEN (type))
19760 return;
19762 switch (TREE_CODE (type))
19764 case ERROR_MARK:
19765 break;
19767 case POINTER_TYPE:
19768 case REFERENCE_TYPE:
19769 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
19770 ensures that the gen_type_die recursion will terminate even if the
19771 type is recursive. Recursive types are possible in Ada. */
19772 /* ??? We could perhaps do this for all types before the switch
19773 statement. */
19774 TREE_ASM_WRITTEN (type) = 1;
19776 /* For these types, all that is required is that we output a DIE (or a
19777 set of DIEs) to represent the "basis" type. */
19778 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19779 DINFO_USAGE_IND_USE);
19780 break;
19782 case OFFSET_TYPE:
19783 /* This code is used for C++ pointer-to-data-member types.
19784 Output a description of the relevant class type. */
19785 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
19786 DINFO_USAGE_IND_USE);
19788 /* Output a description of the type of the object pointed to. */
19789 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19790 DINFO_USAGE_IND_USE);
19792 /* Now output a DIE to represent this pointer-to-data-member type
19793 itself. */
19794 gen_ptr_to_mbr_type_die (type, context_die);
19795 break;
19797 case FUNCTION_TYPE:
19798 /* Force out return type (in case it wasn't forced out already). */
19799 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19800 DINFO_USAGE_DIR_USE);
19801 gen_subroutine_type_die (type, context_die);
19802 break;
19804 case METHOD_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 ARRAY_TYPE:
19812 gen_array_type_die (type, context_die);
19813 break;
19815 case VECTOR_TYPE:
19816 gen_array_type_die (type, context_die);
19817 break;
19819 case ENUMERAL_TYPE:
19820 case RECORD_TYPE:
19821 case UNION_TYPE:
19822 case QUAL_UNION_TYPE:
19823 gen_tagged_type_die (type, context_die, usage);
19824 return;
19826 case VOID_TYPE:
19827 case INTEGER_TYPE:
19828 case REAL_TYPE:
19829 case FIXED_POINT_TYPE:
19830 case COMPLEX_TYPE:
19831 case BOOLEAN_TYPE:
19832 /* No DIEs needed for fundamental types. */
19833 break;
19835 case NULLPTR_TYPE:
19836 case LANG_TYPE:
19837 /* Just use DW_TAG_unspecified_type. */
19839 dw_die_ref type_die = lookup_type_die (type);
19840 if (type_die == NULL)
19842 tree name = TYPE_NAME (type);
19843 if (TREE_CODE (name) == TYPE_DECL)
19844 name = DECL_NAME (name);
19845 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
19846 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
19847 equate_type_number_to_die (type, type_die);
19850 break;
19852 default:
19853 if (is_cxx_auto (type))
19855 if (!auto_die)
19857 auto_die = new_die (DW_TAG_unspecified_type,
19858 comp_unit_die (), NULL_TREE);
19859 add_name_attribute (auto_die, "auto");
19861 equate_type_number_to_die (type, auto_die);
19862 break;
19864 gcc_unreachable ();
19867 TREE_ASM_WRITTEN (type) = 1;
19870 static void
19871 gen_type_die (tree type, dw_die_ref context_die)
19873 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
19876 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
19877 things which are local to the given block. */
19879 static void
19880 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
19882 int must_output_die = 0;
19883 bool inlined_func;
19885 /* Ignore blocks that are NULL. */
19886 if (stmt == NULL_TREE)
19887 return;
19889 inlined_func = inlined_function_outer_scope_p (stmt);
19891 /* If the block is one fragment of a non-contiguous block, do not
19892 process the variables, since they will have been done by the
19893 origin block. Do process subblocks. */
19894 if (BLOCK_FRAGMENT_ORIGIN (stmt))
19896 tree sub;
19898 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
19899 gen_block_die (sub, context_die, depth + 1);
19901 return;
19904 /* Determine if we need to output any Dwarf DIEs at all to represent this
19905 block. */
19906 if (inlined_func)
19907 /* The outer scopes for inlinings *must* always be represented. We
19908 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
19909 must_output_die = 1;
19910 else
19912 /* Determine if this block directly contains any "significant"
19913 local declarations which we will need to output DIEs for. */
19914 if (debug_info_level > DINFO_LEVEL_TERSE)
19915 /* We are not in terse mode so *any* local declaration counts
19916 as being a "significant" one. */
19917 must_output_die = ((BLOCK_VARS (stmt) != NULL
19918 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
19919 && (TREE_USED (stmt)
19920 || TREE_ASM_WRITTEN (stmt)
19921 || BLOCK_ABSTRACT (stmt)));
19922 else if ((TREE_USED (stmt)
19923 || TREE_ASM_WRITTEN (stmt)
19924 || BLOCK_ABSTRACT (stmt))
19925 && !dwarf2out_ignore_block (stmt))
19926 must_output_die = 1;
19929 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
19930 DIE for any block which contains no significant local declarations at
19931 all. Rather, in such cases we just call `decls_for_scope' so that any
19932 needed Dwarf info for any sub-blocks will get properly generated. Note
19933 that in terse mode, our definition of what constitutes a "significant"
19934 local declaration gets restricted to include only inlined function
19935 instances and local (nested) function definitions. */
19936 if (must_output_die)
19938 if (inlined_func)
19940 /* If STMT block is abstract, that means we have been called
19941 indirectly from dwarf2out_abstract_function.
19942 That function rightfully marks the descendent blocks (of
19943 the abstract function it is dealing with) as being abstract,
19944 precisely to prevent us from emitting any
19945 DW_TAG_inlined_subroutine DIE as a descendent
19946 of an abstract function instance. So in that case, we should
19947 not call gen_inlined_subroutine_die.
19949 Later though, when cgraph asks dwarf2out to emit info
19950 for the concrete instance of the function decl into which
19951 the concrete instance of STMT got inlined, the later will lead
19952 to the generation of a DW_TAG_inlined_subroutine DIE. */
19953 if (! BLOCK_ABSTRACT (stmt))
19954 gen_inlined_subroutine_die (stmt, context_die, depth);
19956 else
19957 gen_lexical_block_die (stmt, context_die, depth);
19959 else
19960 decls_for_scope (stmt, context_die, depth);
19963 /* Process variable DECL (or variable with origin ORIGIN) within
19964 block STMT and add it to CONTEXT_DIE. */
19965 static void
19966 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
19968 dw_die_ref die;
19969 tree decl_or_origin = decl ? decl : origin;
19971 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
19972 die = lookup_decl_die (decl_or_origin);
19973 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
19974 && TYPE_DECL_IS_STUB (decl_or_origin))
19975 die = lookup_type_die (TREE_TYPE (decl_or_origin));
19976 else
19977 die = NULL;
19979 if (die != NULL && die->die_parent == NULL)
19980 add_child_die (context_die, die);
19981 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
19982 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
19983 stmt, context_die);
19984 else
19985 gen_decl_die (decl, origin, context_die);
19988 /* Generate all of the decls declared within a given scope and (recursively)
19989 all of its sub-blocks. */
19991 static void
19992 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
19994 tree decl;
19995 unsigned int i;
19996 tree subblocks;
19998 /* Ignore NULL blocks. */
19999 if (stmt == NULL_TREE)
20000 return;
20002 /* Output the DIEs to represent all of the data objects and typedefs
20003 declared directly within this block but not within any nested
20004 sub-blocks. Also, nested function and tag DIEs have been
20005 generated with a parent of NULL; fix that up now. We don't
20006 have to do this if we're at -g1. */
20007 if (debug_info_level > DINFO_LEVEL_TERSE)
20009 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
20010 process_scope_var (stmt, decl, NULL_TREE, context_die);
20011 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20012 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
20013 context_die);
20016 /* Even if we're at -g1, we need to process the subblocks in order to get
20017 inlined call information. */
20019 /* Output the DIEs to represent all sub-blocks (and the items declared
20020 therein) of this block. */
20021 for (subblocks = BLOCK_SUBBLOCKS (stmt);
20022 subblocks != NULL;
20023 subblocks = BLOCK_CHAIN (subblocks))
20024 gen_block_die (subblocks, context_die, depth + 1);
20027 /* Is this a typedef we can avoid emitting? */
20029 static inline int
20030 is_redundant_typedef (const_tree decl)
20032 if (TYPE_DECL_IS_STUB (decl))
20033 return 1;
20035 if (DECL_ARTIFICIAL (decl)
20036 && DECL_CONTEXT (decl)
20037 && is_tagged_type (DECL_CONTEXT (decl))
20038 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
20039 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
20040 /* Also ignore the artificial member typedef for the class name. */
20041 return 1;
20043 return 0;
20046 /* Return TRUE if TYPE is a typedef that names a type for linkage
20047 purposes. This kind of typedefs is produced by the C++ FE for
20048 constructs like:
20050 typedef struct {...} foo;
20052 In that case, there is no typedef variant type produced for foo.
20053 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20054 struct type. */
20056 static bool
20057 is_naming_typedef_decl (const_tree decl)
20059 if (decl == NULL_TREE
20060 || TREE_CODE (decl) != TYPE_DECL
20061 || !is_tagged_type (TREE_TYPE (decl))
20062 || DECL_IS_BUILTIN (decl)
20063 || is_redundant_typedef (decl)
20064 /* It looks like Ada produces TYPE_DECLs that are very similar
20065 to C++ naming typedefs but that have different
20066 semantics. Let's be specific to c++ for now. */
20067 || !is_cxx ())
20068 return FALSE;
20070 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20071 && TYPE_NAME (TREE_TYPE (decl)) == decl
20072 && (TYPE_STUB_DECL (TREE_TYPE (decl))
20073 != TYPE_NAME (TREE_TYPE (decl))));
20076 /* Returns the DIE for a context. */
20078 static inline dw_die_ref
20079 get_context_die (tree context)
20081 if (context)
20083 /* Find die that represents this context. */
20084 if (TYPE_P (context))
20086 context = TYPE_MAIN_VARIANT (context);
20087 return strip_naming_typedef (context, force_type_die (context));
20089 else
20090 return force_decl_die (context);
20092 return comp_unit_die ();
20095 /* Returns the DIE for decl. A DIE will always be returned. */
20097 static dw_die_ref
20098 force_decl_die (tree decl)
20100 dw_die_ref decl_die;
20101 unsigned saved_external_flag;
20102 tree save_fn = NULL_TREE;
20103 decl_die = lookup_decl_die (decl);
20104 if (!decl_die)
20106 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20108 decl_die = lookup_decl_die (decl);
20109 if (decl_die)
20110 return decl_die;
20112 switch (TREE_CODE (decl))
20114 case FUNCTION_DECL:
20115 /* Clear current_function_decl, so that gen_subprogram_die thinks
20116 that this is a declaration. At this point, we just want to force
20117 declaration die. */
20118 save_fn = current_function_decl;
20119 current_function_decl = NULL_TREE;
20120 gen_subprogram_die (decl, context_die);
20121 current_function_decl = save_fn;
20122 break;
20124 case VAR_DECL:
20125 /* Set external flag to force declaration die. Restore it after
20126 gen_decl_die() call. */
20127 saved_external_flag = DECL_EXTERNAL (decl);
20128 DECL_EXTERNAL (decl) = 1;
20129 gen_decl_die (decl, NULL, context_die);
20130 DECL_EXTERNAL (decl) = saved_external_flag;
20131 break;
20133 case NAMESPACE_DECL:
20134 if (dwarf_version >= 3 || !dwarf_strict)
20135 dwarf2out_decl (decl);
20136 else
20137 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
20138 decl_die = comp_unit_die ();
20139 break;
20141 case TRANSLATION_UNIT_DECL:
20142 decl_die = comp_unit_die ();
20143 break;
20145 default:
20146 gcc_unreachable ();
20149 /* We should be able to find the DIE now. */
20150 if (!decl_die)
20151 decl_die = lookup_decl_die (decl);
20152 gcc_assert (decl_die);
20155 return decl_die;
20158 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
20159 always returned. */
20161 static dw_die_ref
20162 force_type_die (tree type)
20164 dw_die_ref type_die;
20166 type_die = lookup_type_die (type);
20167 if (!type_die)
20169 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
20171 type_die = modified_type_die (type, TYPE_READONLY (type),
20172 TYPE_VOLATILE (type), context_die);
20173 gcc_assert (type_die);
20175 return type_die;
20178 /* Force out any required namespaces to be able to output DECL,
20179 and return the new context_die for it, if it's changed. */
20181 static dw_die_ref
20182 setup_namespace_context (tree thing, dw_die_ref context_die)
20184 tree context = (DECL_P (thing)
20185 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
20186 if (context && TREE_CODE (context) == NAMESPACE_DECL)
20187 /* Force out the namespace. */
20188 context_die = force_decl_die (context);
20190 return context_die;
20193 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
20194 type) within its namespace, if appropriate.
20196 For compatibility with older debuggers, namespace DIEs only contain
20197 declarations; all definitions are emitted at CU scope. */
20199 static dw_die_ref
20200 declare_in_namespace (tree thing, dw_die_ref context_die)
20202 dw_die_ref ns_context;
20204 if (debug_info_level <= DINFO_LEVEL_TERSE)
20205 return context_die;
20207 /* If this decl is from an inlined function, then don't try to emit it in its
20208 namespace, as we will get confused. It would have already been emitted
20209 when the abstract instance of the inline function was emitted anyways. */
20210 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
20211 return context_die;
20213 ns_context = setup_namespace_context (thing, context_die);
20215 if (ns_context != context_die)
20217 if (is_fortran ())
20218 return ns_context;
20219 if (DECL_P (thing))
20220 gen_decl_die (thing, NULL, ns_context);
20221 else
20222 gen_type_die (thing, ns_context);
20224 return context_die;
20227 /* Generate a DIE for a namespace or namespace alias. */
20229 static void
20230 gen_namespace_die (tree decl, dw_die_ref context_die)
20232 dw_die_ref namespace_die;
20234 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
20235 they are an alias of. */
20236 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
20238 /* Output a real namespace or module. */
20239 context_die = setup_namespace_context (decl, comp_unit_die ());
20240 namespace_die = new_die (is_fortran ()
20241 ? DW_TAG_module : DW_TAG_namespace,
20242 context_die, decl);
20243 /* For Fortran modules defined in different CU don't add src coords. */
20244 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
20246 const char *name = dwarf2_name (decl, 0);
20247 if (name)
20248 add_name_attribute (namespace_die, name);
20250 else
20251 add_name_and_src_coords_attributes (namespace_die, decl);
20252 if (DECL_EXTERNAL (decl))
20253 add_AT_flag (namespace_die, DW_AT_declaration, 1);
20254 equate_decl_number_to_die (decl, namespace_die);
20256 else
20258 /* Output a namespace alias. */
20260 /* Force out the namespace we are an alias of, if necessary. */
20261 dw_die_ref origin_die
20262 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
20264 if (DECL_FILE_SCOPE_P (decl)
20265 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
20266 context_die = setup_namespace_context (decl, comp_unit_die ());
20267 /* Now create the namespace alias DIE. */
20268 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
20269 add_name_and_src_coords_attributes (namespace_die, decl);
20270 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
20271 equate_decl_number_to_die (decl, namespace_die);
20273 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
20274 if (want_pubnames ())
20275 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
20278 /* Generate Dwarf debug information for a decl described by DECL.
20279 The return value is currently only meaningful for PARM_DECLs,
20280 for all other decls it returns NULL. */
20282 static dw_die_ref
20283 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20285 tree decl_or_origin = decl ? decl : origin;
20286 tree class_origin = NULL, ultimate_origin;
20288 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20289 return NULL;
20291 switch (TREE_CODE (decl_or_origin))
20293 case ERROR_MARK:
20294 break;
20296 case CONST_DECL:
20297 if (!is_fortran () && !is_ada ())
20299 /* The individual enumerators of an enum type get output when we output
20300 the Dwarf representation of the relevant enum type itself. */
20301 break;
20304 /* Emit its type. */
20305 gen_type_die (TREE_TYPE (decl), context_die);
20307 /* And its containing namespace. */
20308 context_die = declare_in_namespace (decl, context_die);
20310 gen_const_die (decl, context_die);
20311 break;
20313 case FUNCTION_DECL:
20314 /* Don't output any DIEs to represent mere function declarations,
20315 unless they are class members or explicit block externs. */
20316 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20317 && DECL_FILE_SCOPE_P (decl_or_origin)
20318 && (current_function_decl == NULL_TREE
20319 || DECL_ARTIFICIAL (decl_or_origin)))
20320 break;
20322 #if 0
20323 /* FIXME */
20324 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20325 on local redeclarations of global functions. That seems broken. */
20326 if (current_function_decl != decl)
20327 /* This is only a declaration. */;
20328 #endif
20330 /* If we're emitting a clone, emit info for the abstract instance. */
20331 if (origin || DECL_ORIGIN (decl) != decl)
20332 dwarf2out_abstract_function (origin
20333 ? DECL_ORIGIN (origin)
20334 : DECL_ABSTRACT_ORIGIN (decl));
20336 /* If we're emitting an out-of-line copy of an inline function,
20337 emit info for the abstract instance and set up to refer to it. */
20338 else if (cgraph_function_possibly_inlined_p (decl)
20339 && ! DECL_ABSTRACT (decl)
20340 && ! class_or_namespace_scope_p (context_die)
20341 /* dwarf2out_abstract_function won't emit a die if this is just
20342 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
20343 that case, because that works only if we have a die. */
20344 && DECL_INITIAL (decl) != NULL_TREE)
20346 dwarf2out_abstract_function (decl);
20347 set_decl_origin_self (decl);
20350 /* Otherwise we're emitting the primary DIE for this decl. */
20351 else if (debug_info_level > DINFO_LEVEL_TERSE)
20353 /* Before we describe the FUNCTION_DECL itself, make sure that we
20354 have its containing type. */
20355 if (!origin)
20356 origin = decl_class_context (decl);
20357 if (origin != NULL_TREE)
20358 gen_type_die (origin, context_die);
20360 /* And its return type. */
20361 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20363 /* And its virtual context. */
20364 if (DECL_VINDEX (decl) != NULL_TREE)
20365 gen_type_die (DECL_CONTEXT (decl), context_die);
20367 /* Make sure we have a member DIE for decl. */
20368 if (origin != NULL_TREE)
20369 gen_type_die_for_member (origin, decl, context_die);
20371 /* And its containing namespace. */
20372 context_die = declare_in_namespace (decl, context_die);
20375 /* Now output a DIE to represent the function itself. */
20376 if (decl)
20377 gen_subprogram_die (decl, context_die);
20378 break;
20380 case TYPE_DECL:
20381 /* If we are in terse mode, don't generate any DIEs to represent any
20382 actual typedefs. */
20383 if (debug_info_level <= DINFO_LEVEL_TERSE)
20384 break;
20386 /* In the special case of a TYPE_DECL node representing the declaration
20387 of some type tag, if the given TYPE_DECL is marked as having been
20388 instantiated from some other (original) TYPE_DECL node (e.g. one which
20389 was generated within the original definition of an inline function) we
20390 used to generate a special (abbreviated) DW_TAG_structure_type,
20391 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
20392 should be actually referencing those DIEs, as variable DIEs with that
20393 type would be emitted already in the abstract origin, so it was always
20394 removed during unused type prunning. Don't add anything in this
20395 case. */
20396 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20397 break;
20399 if (is_redundant_typedef (decl))
20400 gen_type_die (TREE_TYPE (decl), context_die);
20401 else
20402 /* Output a DIE to represent the typedef itself. */
20403 gen_typedef_die (decl, context_die);
20404 break;
20406 case LABEL_DECL:
20407 if (debug_info_level >= DINFO_LEVEL_NORMAL)
20408 gen_label_die (decl, context_die);
20409 break;
20411 case VAR_DECL:
20412 case RESULT_DECL:
20413 /* If we are in terse mode, don't generate any DIEs to represent any
20414 variable declarations or definitions. */
20415 if (debug_info_level <= DINFO_LEVEL_TERSE)
20416 break;
20418 /* Output any DIEs that are needed to specify the type of this data
20419 object. */
20420 if (decl_by_reference_p (decl_or_origin))
20421 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20422 else
20423 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20425 /* And its containing type. */
20426 class_origin = decl_class_context (decl_or_origin);
20427 if (class_origin != NULL_TREE)
20428 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
20430 /* And its containing namespace. */
20431 context_die = declare_in_namespace (decl_or_origin, context_die);
20433 /* Now output the DIE to represent the data object itself. This gets
20434 complicated because of the possibility that the VAR_DECL really
20435 represents an inlined instance of a formal parameter for an inline
20436 function. */
20437 ultimate_origin = decl_ultimate_origin (decl_or_origin);
20438 if (ultimate_origin != NULL_TREE
20439 && TREE_CODE (ultimate_origin) == PARM_DECL)
20440 gen_formal_parameter_die (decl, origin,
20441 true /* Emit name attribute. */,
20442 context_die);
20443 else
20444 gen_variable_die (decl, origin, context_die);
20445 break;
20447 case FIELD_DECL:
20448 /* Ignore the nameless fields that are used to skip bits but handle C++
20449 anonymous unions and structs. */
20450 if (DECL_NAME (decl) != NULL_TREE
20451 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
20452 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
20454 gen_type_die (member_declared_type (decl), context_die);
20455 gen_field_die (decl, context_die);
20457 break;
20459 case PARM_DECL:
20460 if (DECL_BY_REFERENCE (decl_or_origin))
20461 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20462 else
20463 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20464 return gen_formal_parameter_die (decl, origin,
20465 true /* Emit name attribute. */,
20466 context_die);
20468 case NAMESPACE_DECL:
20469 case IMPORTED_DECL:
20470 if (dwarf_version >= 3 || !dwarf_strict)
20471 gen_namespace_die (decl, context_die);
20472 break;
20474 case NAMELIST_DECL:
20475 gen_namelist_decl (DECL_NAME (decl), context_die,
20476 NAMELIST_DECL_ASSOCIATED_DECL (decl));
20477 break;
20479 default:
20480 /* Probably some frontend-internal decl. Assume we don't care. */
20481 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
20482 break;
20485 return NULL;
20488 /* Output debug information for global decl DECL. Called from toplev.c after
20489 compilation proper has finished. */
20491 static void
20492 dwarf2out_global_decl (tree decl)
20494 /* Output DWARF2 information for file-scope tentative data object
20495 declarations, file-scope (extern) function declarations (which
20496 had no corresponding body) and file-scope tagged type declarations
20497 and definitions which have not yet been forced out. */
20498 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
20499 dwarf2out_decl (decl);
20502 /* Output debug information for type decl DECL. Called from toplev.c
20503 and from language front ends (to record built-in types). */
20504 static void
20505 dwarf2out_type_decl (tree decl, int local)
20507 if (!local)
20508 dwarf2out_decl (decl);
20511 /* Output debug information for imported module or decl DECL.
20512 NAME is non-NULL name in the lexical block if the decl has been renamed.
20513 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
20514 that DECL belongs to.
20515 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
20516 static void
20517 dwarf2out_imported_module_or_decl_1 (tree decl,
20518 tree name,
20519 tree lexical_block,
20520 dw_die_ref lexical_block_die)
20522 expanded_location xloc;
20523 dw_die_ref imported_die = NULL;
20524 dw_die_ref at_import_die;
20526 if (TREE_CODE (decl) == IMPORTED_DECL)
20528 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
20529 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
20530 gcc_assert (decl);
20532 else
20533 xloc = expand_location (input_location);
20535 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
20537 at_import_die = force_type_die (TREE_TYPE (decl));
20538 /* For namespace N { typedef void T; } using N::T; base_type_die
20539 returns NULL, but DW_TAG_imported_declaration requires
20540 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
20541 if (!at_import_die)
20543 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
20544 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
20545 at_import_die = lookup_type_die (TREE_TYPE (decl));
20546 gcc_assert (at_import_die);
20549 else
20551 at_import_die = lookup_decl_die (decl);
20552 if (!at_import_die)
20554 /* If we're trying to avoid duplicate debug info, we may not have
20555 emitted the member decl for this field. Emit it now. */
20556 if (TREE_CODE (decl) == FIELD_DECL)
20558 tree type = DECL_CONTEXT (decl);
20560 if (TYPE_CONTEXT (type)
20561 && TYPE_P (TYPE_CONTEXT (type))
20562 && !should_emit_struct_debug (TYPE_CONTEXT (type),
20563 DINFO_USAGE_DIR_USE))
20564 return;
20565 gen_type_die_for_member (type, decl,
20566 get_context_die (TYPE_CONTEXT (type)));
20568 if (TREE_CODE (decl) == NAMELIST_DECL)
20569 at_import_die = gen_namelist_decl (DECL_NAME (decl),
20570 get_context_die (DECL_CONTEXT (decl)),
20571 NULL_TREE);
20572 else
20573 at_import_die = force_decl_die (decl);
20577 if (TREE_CODE (decl) == NAMESPACE_DECL)
20579 if (dwarf_version >= 3 || !dwarf_strict)
20580 imported_die = new_die (DW_TAG_imported_module,
20581 lexical_block_die,
20582 lexical_block);
20583 else
20584 return;
20586 else
20587 imported_die = new_die (DW_TAG_imported_declaration,
20588 lexical_block_die,
20589 lexical_block);
20591 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
20592 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
20593 if (name)
20594 add_AT_string (imported_die, DW_AT_name,
20595 IDENTIFIER_POINTER (name));
20596 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
20599 /* Output debug information for imported module or decl DECL.
20600 NAME is non-NULL name in context if the decl has been renamed.
20601 CHILD is true if decl is one of the renamed decls as part of
20602 importing whole module. */
20604 static void
20605 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
20606 bool child)
20608 /* dw_die_ref at_import_die; */
20609 dw_die_ref scope_die;
20611 if (debug_info_level <= DINFO_LEVEL_TERSE)
20612 return;
20614 gcc_assert (decl);
20616 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
20617 We need decl DIE for reference and scope die. First, get DIE for the decl
20618 itself. */
20620 /* Get the scope die for decl context. Use comp_unit_die for global module
20621 or decl. If die is not found for non globals, force new die. */
20622 if (context
20623 && TYPE_P (context)
20624 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
20625 return;
20627 if (!(dwarf_version >= 3 || !dwarf_strict))
20628 return;
20630 scope_die = get_context_die (context);
20632 if (child)
20634 gcc_assert (scope_die->die_child);
20635 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
20636 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
20637 scope_die = scope_die->die_child;
20640 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
20641 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
20645 /* Output debug information for namelists. */
20647 static dw_die_ref
20648 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
20650 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
20651 tree value;
20652 unsigned i;
20654 if (debug_info_level <= DINFO_LEVEL_TERSE)
20655 return NULL;
20657 gcc_assert (scope_die != NULL);
20658 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
20659 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
20661 /* If there are no item_decls, we have a nondefining namelist, e.g.
20662 with USE association; hence, set DW_AT_declaration. */
20663 if (item_decls == NULL_TREE)
20665 add_AT_flag (nml_die, DW_AT_declaration, 1);
20666 return nml_die;
20669 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
20671 nml_item_ref_die = lookup_decl_die (value);
20672 if (!nml_item_ref_die)
20673 nml_item_ref_die = force_decl_die (value);
20675 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
20676 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
20678 return nml_die;
20682 /* Write the debugging output for DECL. */
20684 void
20685 dwarf2out_decl (tree decl)
20687 dw_die_ref context_die = comp_unit_die ();
20689 switch (TREE_CODE (decl))
20691 case ERROR_MARK:
20692 return;
20694 case FUNCTION_DECL:
20695 /* What we would really like to do here is to filter out all mere
20696 file-scope declarations of file-scope functions which are never
20697 referenced later within this translation unit (and keep all of ones
20698 that *are* referenced later on) but we aren't clairvoyant, so we have
20699 no idea which functions will be referenced in the future (i.e. later
20700 on within the current translation unit). So here we just ignore all
20701 file-scope function declarations which are not also definitions. If
20702 and when the debugger needs to know something about these functions,
20703 it will have to hunt around and find the DWARF information associated
20704 with the definition of the function.
20706 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
20707 nodes represent definitions and which ones represent mere
20708 declarations. We have to check DECL_INITIAL instead. That's because
20709 the C front-end supports some weird semantics for "extern inline"
20710 function definitions. These can get inlined within the current
20711 translation unit (and thus, we need to generate Dwarf info for their
20712 abstract instances so that the Dwarf info for the concrete inlined
20713 instances can have something to refer to) but the compiler never
20714 generates any out-of-lines instances of such things (despite the fact
20715 that they *are* definitions).
20717 The important point is that the C front-end marks these "extern
20718 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
20719 them anyway. Note that the C++ front-end also plays some similar games
20720 for inline function definitions appearing within include files which
20721 also contain `#pragma interface' pragmas.
20723 If we are called from dwarf2out_abstract_function output a DIE
20724 anyway. We can end up here this way with early inlining and LTO
20725 where the inlined function is output in a different LTRANS unit
20726 or not at all. */
20727 if (DECL_INITIAL (decl) == NULL_TREE
20728 && ! DECL_ABSTRACT (decl))
20729 return;
20731 /* If we're a nested function, initially use a parent of NULL; if we're
20732 a plain function, this will be fixed up in decls_for_scope. If
20733 we're a method, it will be ignored, since we already have a DIE. */
20734 if (decl_function_context (decl)
20735 /* But if we're in terse mode, we don't care about scope. */
20736 && debug_info_level > DINFO_LEVEL_TERSE)
20737 context_die = NULL;
20738 break;
20740 case VAR_DECL:
20741 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
20742 declaration and if the declaration was never even referenced from
20743 within this entire compilation unit. We suppress these DIEs in
20744 order to save space in the .debug section (by eliminating entries
20745 which are probably useless). Note that we must not suppress
20746 block-local extern declarations (whether used or not) because that
20747 would screw-up the debugger's name lookup mechanism and cause it to
20748 miss things which really ought to be in scope at a given point. */
20749 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
20750 return;
20752 /* For local statics lookup proper context die. */
20753 if (TREE_STATIC (decl)
20754 && DECL_CONTEXT (decl)
20755 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
20756 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20758 /* If we are in terse mode, don't generate any DIEs to represent any
20759 variable declarations or definitions. */
20760 if (debug_info_level <= DINFO_LEVEL_TERSE)
20761 return;
20762 break;
20764 case CONST_DECL:
20765 if (debug_info_level <= DINFO_LEVEL_TERSE)
20766 return;
20767 if (!is_fortran () && !is_ada ())
20768 return;
20769 if (TREE_STATIC (decl) && decl_function_context (decl))
20770 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20771 break;
20773 case NAMESPACE_DECL:
20774 case IMPORTED_DECL:
20775 if (debug_info_level <= DINFO_LEVEL_TERSE)
20776 return;
20777 if (lookup_decl_die (decl) != NULL)
20778 return;
20779 break;
20781 case TYPE_DECL:
20782 /* Don't emit stubs for types unless they are needed by other DIEs. */
20783 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
20784 return;
20786 /* Don't bother trying to generate any DIEs to represent any of the
20787 normal built-in types for the language we are compiling. */
20788 if (DECL_IS_BUILTIN (decl))
20789 return;
20791 /* If we are in terse mode, don't generate any DIEs for types. */
20792 if (debug_info_level <= DINFO_LEVEL_TERSE)
20793 return;
20795 /* If we're a function-scope tag, initially use a parent of NULL;
20796 this will be fixed up in decls_for_scope. */
20797 if (decl_function_context (decl))
20798 context_die = NULL;
20800 break;
20802 case NAMELIST_DECL:
20803 break;
20805 default:
20806 return;
20809 gen_decl_die (decl, NULL, context_die);
20812 /* Write the debugging output for DECL. */
20814 static void
20815 dwarf2out_function_decl (tree decl)
20817 dwarf2out_decl (decl);
20818 call_arg_locations = NULL;
20819 call_arg_loc_last = NULL;
20820 call_site_count = -1;
20821 tail_call_site_count = -1;
20822 block_map.release ();
20823 htab_empty (decl_loc_table);
20824 htab_empty (cached_dw_loc_list_table);
20827 /* Output a marker (i.e. a label) for the beginning of the generated code for
20828 a lexical block. */
20830 static void
20831 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
20832 unsigned int blocknum)
20834 switch_to_section (current_function_section ());
20835 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
20838 /* Output a marker (i.e. a label) for the end of the generated code for a
20839 lexical block. */
20841 static void
20842 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
20844 switch_to_section (current_function_section ());
20845 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
20848 /* Returns nonzero if it is appropriate not to emit any debugging
20849 information for BLOCK, because it doesn't contain any instructions.
20851 Don't allow this for blocks with nested functions or local classes
20852 as we would end up with orphans, and in the presence of scheduling
20853 we may end up calling them anyway. */
20855 static bool
20856 dwarf2out_ignore_block (const_tree block)
20858 tree decl;
20859 unsigned int i;
20861 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
20862 if (TREE_CODE (decl) == FUNCTION_DECL
20863 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20864 return 0;
20865 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
20867 decl = BLOCK_NONLOCALIZED_VAR (block, i);
20868 if (TREE_CODE (decl) == FUNCTION_DECL
20869 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20870 return 0;
20873 return 1;
20876 /* Hash table routines for file_hash. */
20878 static int
20879 file_table_eq (const void *p1_p, const void *p2_p)
20881 const struct dwarf_file_data *const p1 =
20882 (const struct dwarf_file_data *) p1_p;
20883 const char *const p2 = (const char *) p2_p;
20884 return filename_cmp (p1->filename, p2) == 0;
20887 static hashval_t
20888 file_table_hash (const void *p_p)
20890 const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
20891 return htab_hash_string (p->filename);
20894 /* Lookup FILE_NAME (in the list of filenames that we know about here in
20895 dwarf2out.c) and return its "index". The index of each (known) filename is
20896 just a unique number which is associated with only that one filename. We
20897 need such numbers for the sake of generating labels (in the .debug_sfnames
20898 section) and references to those files numbers (in the .debug_srcinfo
20899 and.debug_macinfo sections). If the filename given as an argument is not
20900 found in our current list, add it to the list and assign it the next
20901 available unique index number. In order to speed up searches, we remember
20902 the index of the filename was looked up last. This handles the majority of
20903 all searches. */
20905 static struct dwarf_file_data *
20906 lookup_filename (const char *file_name)
20908 void ** slot;
20909 struct dwarf_file_data * created;
20911 /* Check to see if the file name that was searched on the previous
20912 call matches this file name. If so, return the index. */
20913 if (file_table_last_lookup
20914 && (file_name == file_table_last_lookup->filename
20915 || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
20916 return file_table_last_lookup;
20918 /* Didn't match the previous lookup, search the table. */
20919 slot = htab_find_slot_with_hash (file_table, file_name,
20920 htab_hash_string (file_name), INSERT);
20921 if (*slot)
20922 return (struct dwarf_file_data *) *slot;
20924 created = ggc_alloc_dwarf_file_data ();
20925 created->filename = file_name;
20926 created->emitted_number = 0;
20927 *slot = created;
20928 return created;
20931 /* If the assembler will construct the file table, then translate the compiler
20932 internal file table number into the assembler file table number, and emit
20933 a .file directive if we haven't already emitted one yet. The file table
20934 numbers are different because we prune debug info for unused variables and
20935 types, which may include filenames. */
20937 static int
20938 maybe_emit_file (struct dwarf_file_data * fd)
20940 if (! fd->emitted_number)
20942 if (last_emitted_file)
20943 fd->emitted_number = last_emitted_file->emitted_number + 1;
20944 else
20945 fd->emitted_number = 1;
20946 last_emitted_file = fd;
20948 if (DWARF2_ASM_LINE_DEBUG_INFO)
20950 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
20951 output_quoted_string (asm_out_file,
20952 remap_debug_filename (fd->filename));
20953 fputc ('\n', asm_out_file);
20957 return fd->emitted_number;
20960 /* Schedule generation of a DW_AT_const_value attribute to DIE.
20961 That generation should happen after function debug info has been
20962 generated. The value of the attribute is the constant value of ARG. */
20964 static void
20965 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
20967 die_arg_entry entry;
20969 if (!die || !arg)
20970 return;
20972 if (!tmpl_value_parm_die_table)
20973 vec_alloc (tmpl_value_parm_die_table, 32);
20975 entry.die = die;
20976 entry.arg = arg;
20977 vec_safe_push (tmpl_value_parm_die_table, entry);
20980 /* Return TRUE if T is an instance of generic type, FALSE
20981 otherwise. */
20983 static bool
20984 generic_type_p (tree t)
20986 if (t == NULL_TREE || !TYPE_P (t))
20987 return false;
20988 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
20991 /* Schedule the generation of the generic parameter dies for the
20992 instance of generic type T. The proper generation itself is later
20993 done by gen_scheduled_generic_parms_dies. */
20995 static void
20996 schedule_generic_params_dies_gen (tree t)
20998 if (!generic_type_p (t))
20999 return;
21001 if (!generic_type_instances)
21002 vec_alloc (generic_type_instances, 256);
21004 vec_safe_push (generic_type_instances, t);
21007 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
21008 by append_entry_to_tmpl_value_parm_die_table. This function must
21009 be called after function DIEs have been generated. */
21011 static void
21012 gen_remaining_tmpl_value_param_die_attribute (void)
21014 if (tmpl_value_parm_die_table)
21016 unsigned i;
21017 die_arg_entry *e;
21019 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
21020 tree_add_const_value_attribute (e->die, e->arg);
21024 /* Generate generic parameters DIEs for instances of generic types
21025 that have been previously scheduled by
21026 schedule_generic_params_dies_gen. This function must be called
21027 after all the types of the CU have been laid out. */
21029 static void
21030 gen_scheduled_generic_parms_dies (void)
21032 unsigned i;
21033 tree t;
21035 if (!generic_type_instances)
21036 return;
21038 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
21039 if (COMPLETE_TYPE_P (t))
21040 gen_generic_params_dies (t);
21044 /* Replace DW_AT_name for the decl with name. */
21046 static void
21047 dwarf2out_set_name (tree decl, tree name)
21049 dw_die_ref die;
21050 dw_attr_ref attr;
21051 const char *dname;
21053 die = TYPE_SYMTAB_DIE (decl);
21054 if (!die)
21055 return;
21057 dname = dwarf2_name (name, 0);
21058 if (!dname)
21059 return;
21061 attr = get_AT (die, DW_AT_name);
21062 if (attr)
21064 struct indirect_string_node *node;
21066 node = find_AT_string (dname);
21067 /* replace the string. */
21068 attr->dw_attr_val.v.val_str = node;
21071 else
21072 add_name_attribute (die, dname);
21075 /* True if before or during processing of the first function being emitted. */
21076 static bool in_first_function_p = true;
21077 /* True if loc_note during dwarf2out_var_location call might still be
21078 before first real instruction at address equal to .Ltext0. */
21079 static bool maybe_at_text_label_p = true;
21080 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
21081 static unsigned int first_loclabel_num_not_at_text_label;
21083 /* Called by the final INSN scan whenever we see a var location. We
21084 use it to drop labels in the right places, and throw the location in
21085 our lookup table. */
21087 static void
21088 dwarf2out_var_location (rtx loc_note)
21090 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21091 struct var_loc_node *newloc;
21092 rtx next_real, next_note;
21093 static const char *last_label;
21094 static const char *last_postcall_label;
21095 static bool last_in_cold_section_p;
21096 static rtx expected_next_loc_note;
21097 tree decl;
21098 bool var_loc_p;
21100 if (!NOTE_P (loc_note))
21102 if (CALL_P (loc_note))
21104 call_site_count++;
21105 if (SIBLING_CALL_P (loc_note))
21106 tail_call_site_count++;
21108 return;
21111 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
21112 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21113 return;
21115 /* Optimize processing a large consecutive sequence of location
21116 notes so we don't spend too much time in next_real_insn. If the
21117 next insn is another location note, remember the next_real_insn
21118 calculation for next time. */
21119 next_real = cached_next_real_insn;
21120 if (next_real)
21122 if (expected_next_loc_note != loc_note)
21123 next_real = NULL_RTX;
21126 next_note = NEXT_INSN (loc_note);
21127 if (! next_note
21128 || INSN_DELETED_P (next_note)
21129 || ! NOTE_P (next_note)
21130 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
21131 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
21132 next_note = NULL_RTX;
21134 if (! next_real)
21135 next_real = next_real_insn (loc_note);
21137 if (next_note)
21139 expected_next_loc_note = next_note;
21140 cached_next_real_insn = next_real;
21142 else
21143 cached_next_real_insn = NULL_RTX;
21145 /* If there are no instructions which would be affected by this note,
21146 don't do anything. */
21147 if (var_loc_p
21148 && next_real == NULL_RTX
21149 && !NOTE_DURING_CALL_P (loc_note))
21150 return;
21152 if (next_real == NULL_RTX)
21153 next_real = get_last_insn ();
21155 /* If there were any real insns between note we processed last time
21156 and this note (or if it is the first note), clear
21157 last_{,postcall_}label so that they are not reused this time. */
21158 if (last_var_location_insn == NULL_RTX
21159 || last_var_location_insn != next_real
21160 || last_in_cold_section_p != in_cold_section_p)
21162 last_label = NULL;
21163 last_postcall_label = NULL;
21166 if (var_loc_p)
21168 decl = NOTE_VAR_LOCATION_DECL (loc_note);
21169 newloc = add_var_loc_to_decl (decl, loc_note,
21170 NOTE_DURING_CALL_P (loc_note)
21171 ? last_postcall_label : last_label);
21172 if (newloc == NULL)
21173 return;
21175 else
21177 decl = NULL_TREE;
21178 newloc = NULL;
21181 /* If there were no real insns between note we processed last time
21182 and this note, use the label we emitted last time. Otherwise
21183 create a new label and emit it. */
21184 if (last_label == NULL)
21186 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21187 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21188 loclabel_num++;
21189 last_label = ggc_strdup (loclabel);
21190 /* See if loclabel might be equal to .Ltext0. If yes,
21191 bump first_loclabel_num_not_at_text_label. */
21192 if (!have_multiple_function_sections
21193 && in_first_function_p
21194 && maybe_at_text_label_p)
21196 static rtx last_start;
21197 rtx insn;
21198 for (insn = loc_note; insn; insn = previous_insn (insn))
21199 if (insn == last_start)
21200 break;
21201 else if (!NONDEBUG_INSN_P (insn))
21202 continue;
21203 else
21205 rtx body = PATTERN (insn);
21206 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
21207 continue;
21208 /* Inline asm could occupy zero bytes. */
21209 else if (GET_CODE (body) == ASM_INPUT
21210 || asm_noperands (body) >= 0)
21211 continue;
21212 #ifdef HAVE_attr_length
21213 else if (get_attr_min_length (insn) == 0)
21214 continue;
21215 #endif
21216 else
21218 /* Assume insn has non-zero length. */
21219 maybe_at_text_label_p = false;
21220 break;
21223 if (maybe_at_text_label_p)
21225 last_start = loc_note;
21226 first_loclabel_num_not_at_text_label = loclabel_num;
21231 if (!var_loc_p)
21233 struct call_arg_loc_node *ca_loc
21234 = ggc_alloc_cleared_call_arg_loc_node ();
21235 rtx prev = prev_real_insn (loc_note), x;
21236 ca_loc->call_arg_loc_note = loc_note;
21237 ca_loc->next = NULL;
21238 ca_loc->label = last_label;
21239 gcc_assert (prev
21240 && (CALL_P (prev)
21241 || (NONJUMP_INSN_P (prev)
21242 && GET_CODE (PATTERN (prev)) == SEQUENCE
21243 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
21244 if (!CALL_P (prev))
21245 prev = XVECEXP (PATTERN (prev), 0, 0);
21246 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
21247 x = get_call_rtx_from (PATTERN (prev));
21248 if (x)
21250 x = XEXP (XEXP (x, 0), 0);
21251 if (GET_CODE (x) == SYMBOL_REF
21252 && SYMBOL_REF_DECL (x)
21253 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
21254 ca_loc->symbol_ref = x;
21256 ca_loc->block = insn_scope (prev);
21257 if (call_arg_locations)
21258 call_arg_loc_last->next = ca_loc;
21259 else
21260 call_arg_locations = ca_loc;
21261 call_arg_loc_last = ca_loc;
21263 else if (!NOTE_DURING_CALL_P (loc_note))
21264 newloc->label = last_label;
21265 else
21267 if (!last_postcall_label)
21269 sprintf (loclabel, "%s-1", last_label);
21270 last_postcall_label = ggc_strdup (loclabel);
21272 newloc->label = last_postcall_label;
21275 last_var_location_insn = next_real;
21276 last_in_cold_section_p = in_cold_section_p;
21279 /* Note in one location list that text section has changed. */
21281 static int
21282 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
21284 var_loc_list *list = (var_loc_list *) *slot;
21285 if (list->first)
21286 list->last_before_switch
21287 = list->last->next ? list->last->next : list->last;
21288 return 1;
21291 /* Note in all location lists that text section has changed. */
21293 static void
21294 var_location_switch_text_section (void)
21296 if (decl_loc_table == NULL)
21297 return;
21299 htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
21302 /* Create a new line number table. */
21304 static dw_line_info_table *
21305 new_line_info_table (void)
21307 dw_line_info_table *table;
21309 table = ggc_alloc_cleared_dw_line_info_table_struct ();
21310 table->file_num = 1;
21311 table->line_num = 1;
21312 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
21314 return table;
21317 /* Lookup the "current" table into which we emit line info, so
21318 that we don't have to do it for every source line. */
21320 static void
21321 set_cur_line_info_table (section *sec)
21323 dw_line_info_table *table;
21325 if (sec == text_section)
21326 table = text_section_line_info;
21327 else if (sec == cold_text_section)
21329 table = cold_text_section_line_info;
21330 if (!table)
21332 cold_text_section_line_info = table = new_line_info_table ();
21333 table->end_label = cold_end_label;
21336 else
21338 const char *end_label;
21340 if (flag_reorder_blocks_and_partition)
21342 if (in_cold_section_p)
21343 end_label = crtl->subsections.cold_section_end_label;
21344 else
21345 end_label = crtl->subsections.hot_section_end_label;
21347 else
21349 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21350 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
21351 current_function_funcdef_no);
21352 end_label = ggc_strdup (label);
21355 table = new_line_info_table ();
21356 table->end_label = end_label;
21358 vec_safe_push (separate_line_info, table);
21361 if (DWARF2_ASM_LINE_DEBUG_INFO)
21362 table->is_stmt = (cur_line_info_table
21363 ? cur_line_info_table->is_stmt
21364 : DWARF_LINE_DEFAULT_IS_STMT_START);
21365 cur_line_info_table = table;
21369 /* We need to reset the locations at the beginning of each
21370 function. We can't do this in the end_function hook, because the
21371 declarations that use the locations won't have been output when
21372 that hook is called. Also compute have_multiple_function_sections here. */
21374 static void
21375 dwarf2out_begin_function (tree fun)
21377 section *sec = function_section (fun);
21379 if (sec != text_section)
21380 have_multiple_function_sections = true;
21382 if (flag_reorder_blocks_and_partition && !cold_text_section)
21384 gcc_assert (current_function_decl == fun);
21385 cold_text_section = unlikely_text_section ();
21386 switch_to_section (cold_text_section);
21387 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21388 switch_to_section (sec);
21391 dwarf2out_note_section_used ();
21392 call_site_count = 0;
21393 tail_call_site_count = 0;
21395 set_cur_line_info_table (sec);
21398 /* Helper function of dwarf2out_end_function, called only after emitting
21399 the very first function into assembly. Check if some .debug_loc range
21400 might end with a .LVL* label that could be equal to .Ltext0.
21401 In that case we must force using absolute addresses in .debug_loc ranges,
21402 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
21403 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
21404 list terminator.
21405 Set have_multiple_function_sections to true in that case and
21406 terminate htab traversal. */
21408 static int
21409 find_empty_loc_ranges_at_text_label (void **slot, void *)
21411 var_loc_list *entry;
21412 struct var_loc_node *node;
21414 entry = (var_loc_list *) *slot;
21415 node = entry->first;
21416 if (node && node->next && node->next->label)
21418 unsigned int i;
21419 const char *label = node->next->label;
21420 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
21422 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
21424 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
21425 if (strcmp (label, loclabel) == 0)
21427 have_multiple_function_sections = true;
21428 return 0;
21432 return 1;
21435 /* Hook called after emitting a function into assembly.
21436 This does something only for the very first function emitted. */
21438 static void
21439 dwarf2out_end_function (unsigned int)
21441 if (in_first_function_p
21442 && !have_multiple_function_sections
21443 && first_loclabel_num_not_at_text_label
21444 && decl_loc_table)
21445 htab_traverse (decl_loc_table, find_empty_loc_ranges_at_text_label,
21446 NULL);
21447 in_first_function_p = false;
21448 maybe_at_text_label_p = false;
21451 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
21453 static void
21454 push_dw_line_info_entry (dw_line_info_table *table,
21455 enum dw_line_info_opcode opcode, unsigned int val)
21457 dw_line_info_entry e;
21458 e.opcode = opcode;
21459 e.val = val;
21460 vec_safe_push (table->entries, e);
21463 /* Output a label to mark the beginning of a source code line entry
21464 and record information relating to this source line, in
21465 'line_info_table' for later output of the .debug_line section. */
21466 /* ??? The discriminator parameter ought to be unsigned. */
21468 static void
21469 dwarf2out_source_line (unsigned int line, const char *filename,
21470 int discriminator, bool is_stmt)
21472 unsigned int file_num;
21473 dw_line_info_table *table;
21475 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
21476 return;
21478 /* The discriminator column was added in dwarf4. Simplify the below
21479 by simply removing it if we're not supposed to output it. */
21480 if (dwarf_version < 4 && dwarf_strict)
21481 discriminator = 0;
21483 table = cur_line_info_table;
21484 file_num = maybe_emit_file (lookup_filename (filename));
21486 /* ??? TODO: Elide duplicate line number entries. Traditionally,
21487 the debugger has used the second (possibly duplicate) line number
21488 at the beginning of the function to mark the end of the prologue.
21489 We could eliminate any other duplicates within the function. For
21490 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
21491 that second line number entry. */
21492 /* Recall that this end-of-prologue indication is *not* the same thing
21493 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
21494 to which the hook corresponds, follows the last insn that was
21495 emitted by gen_prologue. What we need is to precede the first insn
21496 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
21497 insn that corresponds to something the user wrote. These may be
21498 very different locations once scheduling is enabled. */
21500 if (0 && file_num == table->file_num
21501 && line == table->line_num
21502 && discriminator == table->discrim_num
21503 && is_stmt == table->is_stmt)
21504 return;
21506 switch_to_section (current_function_section ());
21508 /* If requested, emit something human-readable. */
21509 if (flag_debug_asm)
21510 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
21512 if (DWARF2_ASM_LINE_DEBUG_INFO)
21514 /* Emit the .loc directive understood by GNU as. */
21515 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
21516 file_num, line, is_stmt, discriminator */
21517 fputs ("\t.loc ", asm_out_file);
21518 fprint_ul (asm_out_file, file_num);
21519 putc (' ', asm_out_file);
21520 fprint_ul (asm_out_file, line);
21521 putc (' ', asm_out_file);
21522 putc ('0', asm_out_file);
21524 if (is_stmt != table->is_stmt)
21526 fputs (" is_stmt ", asm_out_file);
21527 putc (is_stmt ? '1' : '0', asm_out_file);
21529 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
21531 gcc_assert (discriminator > 0);
21532 fputs (" discriminator ", asm_out_file);
21533 fprint_ul (asm_out_file, (unsigned long) discriminator);
21535 putc ('\n', asm_out_file);
21537 else
21539 unsigned int label_num = ++line_info_label_num;
21541 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
21543 push_dw_line_info_entry (table, LI_set_address, label_num);
21544 if (file_num != table->file_num)
21545 push_dw_line_info_entry (table, LI_set_file, file_num);
21546 if (discriminator != table->discrim_num)
21547 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
21548 if (is_stmt != table->is_stmt)
21549 push_dw_line_info_entry (table, LI_negate_stmt, 0);
21550 push_dw_line_info_entry (table, LI_set_line, line);
21553 table->file_num = file_num;
21554 table->line_num = line;
21555 table->discrim_num = discriminator;
21556 table->is_stmt = is_stmt;
21557 table->in_use = true;
21560 /* Record the beginning of a new source file. */
21562 static void
21563 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
21565 if (flag_eliminate_dwarf2_dups)
21567 /* Record the beginning of the file for break_out_includes. */
21568 dw_die_ref bincl_die;
21570 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
21571 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
21574 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21576 macinfo_entry e;
21577 e.code = DW_MACINFO_start_file;
21578 e.lineno = lineno;
21579 e.info = ggc_strdup (filename);
21580 vec_safe_push (macinfo_table, e);
21584 /* Record the end of a source file. */
21586 static void
21587 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
21589 if (flag_eliminate_dwarf2_dups)
21590 /* Record the end of the file for break_out_includes. */
21591 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
21593 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21595 macinfo_entry e;
21596 e.code = DW_MACINFO_end_file;
21597 e.lineno = lineno;
21598 e.info = NULL;
21599 vec_safe_push (macinfo_table, e);
21603 /* Called from debug_define in toplev.c. The `buffer' parameter contains
21604 the tail part of the directive line, i.e. the part which is past the
21605 initial whitespace, #, whitespace, directive-name, whitespace part. */
21607 static void
21608 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
21609 const char *buffer ATTRIBUTE_UNUSED)
21611 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21613 macinfo_entry e;
21614 /* Insert a dummy first entry to be able to optimize the whole
21615 predefined macro block using DW_MACRO_GNU_transparent_include. */
21616 if (macinfo_table->is_empty () && lineno <= 1)
21618 e.code = 0;
21619 e.lineno = 0;
21620 e.info = NULL;
21621 vec_safe_push (macinfo_table, e);
21623 e.code = DW_MACINFO_define;
21624 e.lineno = lineno;
21625 e.info = ggc_strdup (buffer);
21626 vec_safe_push (macinfo_table, e);
21630 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
21631 the tail part of the directive line, i.e. the part which is past the
21632 initial whitespace, #, whitespace, directive-name, whitespace part. */
21634 static void
21635 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
21636 const char *buffer ATTRIBUTE_UNUSED)
21638 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21640 macinfo_entry e;
21641 /* Insert a dummy first entry to be able to optimize the whole
21642 predefined macro block using DW_MACRO_GNU_transparent_include. */
21643 if (macinfo_table->is_empty () && lineno <= 1)
21645 e.code = 0;
21646 e.lineno = 0;
21647 e.info = NULL;
21648 vec_safe_push (macinfo_table, e);
21650 e.code = DW_MACINFO_undef;
21651 e.lineno = lineno;
21652 e.info = ggc_strdup (buffer);
21653 vec_safe_push (macinfo_table, e);
21657 /* Helpers to manipulate hash table of CUs. */
21659 struct macinfo_entry_hasher : typed_noop_remove <macinfo_entry>
21661 typedef macinfo_entry value_type;
21662 typedef macinfo_entry compare_type;
21663 static inline hashval_t hash (const value_type *);
21664 static inline bool equal (const value_type *, const compare_type *);
21667 inline hashval_t
21668 macinfo_entry_hasher::hash (const value_type *entry)
21670 return htab_hash_string (entry->info);
21673 inline bool
21674 macinfo_entry_hasher::equal (const value_type *entry1,
21675 const compare_type *entry2)
21677 return !strcmp (entry1->info, entry2->info);
21680 typedef hash_table <macinfo_entry_hasher> macinfo_hash_type;
21682 /* Output a single .debug_macinfo entry. */
21684 static void
21685 output_macinfo_op (macinfo_entry *ref)
21687 int file_num;
21688 size_t len;
21689 struct indirect_string_node *node;
21690 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21691 struct dwarf_file_data *fd;
21693 switch (ref->code)
21695 case DW_MACINFO_start_file:
21696 fd = lookup_filename (ref->info);
21697 file_num = maybe_emit_file (fd);
21698 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
21699 dw2_asm_output_data_uleb128 (ref->lineno,
21700 "Included from line number %lu",
21701 (unsigned long) ref->lineno);
21702 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
21703 break;
21704 case DW_MACINFO_end_file:
21705 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
21706 break;
21707 case DW_MACINFO_define:
21708 case DW_MACINFO_undef:
21709 len = strlen (ref->info) + 1;
21710 if (!dwarf_strict
21711 && len > DWARF_OFFSET_SIZE
21712 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21713 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21715 ref->code = ref->code == DW_MACINFO_define
21716 ? DW_MACRO_GNU_define_indirect
21717 : DW_MACRO_GNU_undef_indirect;
21718 output_macinfo_op (ref);
21719 return;
21721 dw2_asm_output_data (1, ref->code,
21722 ref->code == DW_MACINFO_define
21723 ? "Define macro" : "Undefine macro");
21724 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21725 (unsigned long) ref->lineno);
21726 dw2_asm_output_nstring (ref->info, -1, "The macro");
21727 break;
21728 case DW_MACRO_GNU_define_indirect:
21729 case DW_MACRO_GNU_undef_indirect:
21730 node = find_AT_string (ref->info);
21731 gcc_assert (node
21732 && ((node->form == DW_FORM_strp)
21733 || (node->form == DW_FORM_GNU_str_index)));
21734 dw2_asm_output_data (1, ref->code,
21735 ref->code == DW_MACRO_GNU_define_indirect
21736 ? "Define macro indirect"
21737 : "Undefine macro indirect");
21738 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21739 (unsigned long) ref->lineno);
21740 if (node->form == DW_FORM_strp)
21741 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
21742 debug_str_section, "The macro: \"%s\"",
21743 ref->info);
21744 else
21745 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
21746 ref->info);
21747 break;
21748 case DW_MACRO_GNU_transparent_include:
21749 dw2_asm_output_data (1, ref->code, "Transparent include");
21750 ASM_GENERATE_INTERNAL_LABEL (label,
21751 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
21752 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
21753 break;
21754 default:
21755 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
21756 ASM_COMMENT_START, (unsigned long) ref->code);
21757 break;
21761 /* Attempt to make a sequence of define/undef macinfo ops shareable with
21762 other compilation unit .debug_macinfo sections. IDX is the first
21763 index of a define/undef, return the number of ops that should be
21764 emitted in a comdat .debug_macinfo section and emit
21765 a DW_MACRO_GNU_transparent_include entry referencing it.
21766 If the define/undef entry should be emitted normally, return 0. */
21768 static unsigned
21769 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
21770 macinfo_hash_type *macinfo_htab)
21772 macinfo_entry *first, *second, *cur, *inc;
21773 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
21774 unsigned char checksum[16];
21775 struct md5_ctx ctx;
21776 char *grp_name, *tail;
21777 const char *base;
21778 unsigned int i, count, encoded_filename_len, linebuf_len;
21779 macinfo_entry **slot;
21781 first = &(*macinfo_table)[idx];
21782 second = &(*macinfo_table)[idx + 1];
21784 /* Optimize only if there are at least two consecutive define/undef ops,
21785 and either all of them are before first DW_MACINFO_start_file
21786 with lineno {0,1} (i.e. predefined macro block), or all of them are
21787 in some included header file. */
21788 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
21789 return 0;
21790 if (vec_safe_is_empty (files))
21792 if (first->lineno > 1 || second->lineno > 1)
21793 return 0;
21795 else if (first->lineno == 0)
21796 return 0;
21798 /* Find the last define/undef entry that can be grouped together
21799 with first and at the same time compute md5 checksum of their
21800 codes, linenumbers and strings. */
21801 md5_init_ctx (&ctx);
21802 for (i = idx; macinfo_table->iterate (i, &cur); i++)
21803 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
21804 break;
21805 else if (vec_safe_is_empty (files) && cur->lineno > 1)
21806 break;
21807 else
21809 unsigned char code = cur->code;
21810 md5_process_bytes (&code, 1, &ctx);
21811 checksum_uleb128 (cur->lineno, &ctx);
21812 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
21814 md5_finish_ctx (&ctx, checksum);
21815 count = i - idx;
21817 /* From the containing include filename (if any) pick up just
21818 usable characters from its basename. */
21819 if (vec_safe_is_empty (files))
21820 base = "";
21821 else
21822 base = lbasename (files->last ().info);
21823 for (encoded_filename_len = 0, i = 0; base[i]; i++)
21824 if (ISIDNUM (base[i]) || base[i] == '.')
21825 encoded_filename_len++;
21826 /* Count . at the end. */
21827 if (encoded_filename_len)
21828 encoded_filename_len++;
21830 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
21831 linebuf_len = strlen (linebuf);
21833 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
21834 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
21835 + 16 * 2 + 1);
21836 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
21837 tail = grp_name + 4;
21838 if (encoded_filename_len)
21840 for (i = 0; base[i]; i++)
21841 if (ISIDNUM (base[i]) || base[i] == '.')
21842 *tail++ = base[i];
21843 *tail++ = '.';
21845 memcpy (tail, linebuf, linebuf_len);
21846 tail += linebuf_len;
21847 *tail++ = '.';
21848 for (i = 0; i < 16; i++)
21849 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
21851 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
21852 in the empty vector entry before the first define/undef. */
21853 inc = &(*macinfo_table)[idx - 1];
21854 inc->code = DW_MACRO_GNU_transparent_include;
21855 inc->lineno = 0;
21856 inc->info = ggc_strdup (grp_name);
21857 if (!macinfo_htab->is_created ())
21858 macinfo_htab->create (10);
21859 /* Avoid emitting duplicates. */
21860 slot = macinfo_htab->find_slot (inc, INSERT);
21861 if (*slot != NULL)
21863 inc->code = 0;
21864 inc->info = NULL;
21865 /* If such an entry has been used before, just emit
21866 a DW_MACRO_GNU_transparent_include op. */
21867 inc = *slot;
21868 output_macinfo_op (inc);
21869 /* And clear all macinfo_entry in the range to avoid emitting them
21870 in the second pass. */
21871 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
21873 cur->code = 0;
21874 cur->info = NULL;
21877 else
21879 *slot = inc;
21880 inc->lineno = macinfo_htab->elements ();
21881 output_macinfo_op (inc);
21883 return count;
21886 /* Save any strings needed by the macinfo table in the debug str
21887 table. All strings must be collected into the table by the time
21888 index_string is called. */
21890 static void
21891 save_macinfo_strings (void)
21893 unsigned len;
21894 unsigned i;
21895 macinfo_entry *ref;
21897 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
21899 switch (ref->code)
21901 /* Match the logic in output_macinfo_op to decide on
21902 indirect strings. */
21903 case DW_MACINFO_define:
21904 case DW_MACINFO_undef:
21905 len = strlen (ref->info) + 1;
21906 if (!dwarf_strict
21907 && len > DWARF_OFFSET_SIZE
21908 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21909 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21910 set_indirect_string (find_AT_string (ref->info));
21911 break;
21912 case DW_MACRO_GNU_define_indirect:
21913 case DW_MACRO_GNU_undef_indirect:
21914 set_indirect_string (find_AT_string (ref->info));
21915 break;
21916 default:
21917 break;
21922 /* Output macinfo section(s). */
21924 static void
21925 output_macinfo (void)
21927 unsigned i;
21928 unsigned long length = vec_safe_length (macinfo_table);
21929 macinfo_entry *ref;
21930 vec<macinfo_entry, va_gc> *files = NULL;
21931 macinfo_hash_type macinfo_htab;
21933 if (! length)
21934 return;
21936 /* output_macinfo* uses these interchangeably. */
21937 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
21938 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
21939 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
21940 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
21942 /* For .debug_macro emit the section header. */
21943 if (!dwarf_strict)
21945 dw2_asm_output_data (2, 4, "DWARF macro version number");
21946 if (DWARF_OFFSET_SIZE == 8)
21947 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
21948 else
21949 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
21950 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
21951 (!dwarf_split_debug_info ? debug_line_section_label
21952 : debug_skeleton_line_section_label),
21953 debug_line_section, NULL);
21956 /* In the first loop, it emits the primary .debug_macinfo section
21957 and after each emitted op the macinfo_entry is cleared.
21958 If a longer range of define/undef ops can be optimized using
21959 DW_MACRO_GNU_transparent_include, the
21960 DW_MACRO_GNU_transparent_include op is emitted and kept in
21961 the vector before the first define/undef in the range and the
21962 whole range of define/undef ops is not emitted and kept. */
21963 for (i = 0; macinfo_table->iterate (i, &ref); i++)
21965 switch (ref->code)
21967 case DW_MACINFO_start_file:
21968 vec_safe_push (files, *ref);
21969 break;
21970 case DW_MACINFO_end_file:
21971 if (!vec_safe_is_empty (files))
21972 files->pop ();
21973 break;
21974 case DW_MACINFO_define:
21975 case DW_MACINFO_undef:
21976 if (!dwarf_strict
21977 && HAVE_COMDAT_GROUP
21978 && vec_safe_length (files) != 1
21979 && i > 0
21980 && i + 1 < length
21981 && (*macinfo_table)[i - 1].code == 0)
21983 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
21984 if (count)
21986 i += count - 1;
21987 continue;
21990 break;
21991 case 0:
21992 /* A dummy entry may be inserted at the beginning to be able
21993 to optimize the whole block of predefined macros. */
21994 if (i == 0)
21995 continue;
21996 default:
21997 break;
21999 output_macinfo_op (ref);
22000 ref->info = NULL;
22001 ref->code = 0;
22004 if (!macinfo_htab.is_created ())
22005 return;
22007 macinfo_htab.dispose ();
22009 /* If any DW_MACRO_GNU_transparent_include were used, on those
22010 DW_MACRO_GNU_transparent_include entries terminate the
22011 current chain and switch to a new comdat .debug_macinfo
22012 section and emit the define/undef entries within it. */
22013 for (i = 0; macinfo_table->iterate (i, &ref); i++)
22014 switch (ref->code)
22016 case 0:
22017 continue;
22018 case DW_MACRO_GNU_transparent_include:
22020 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22021 tree comdat_key = get_identifier (ref->info);
22022 /* Terminate the previous .debug_macinfo section. */
22023 dw2_asm_output_data (1, 0, "End compilation unit");
22024 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
22025 SECTION_DEBUG
22026 | SECTION_LINKONCE,
22027 comdat_key);
22028 ASM_GENERATE_INTERNAL_LABEL (label,
22029 DEBUG_MACRO_SECTION_LABEL,
22030 ref->lineno);
22031 ASM_OUTPUT_LABEL (asm_out_file, label);
22032 ref->code = 0;
22033 ref->info = NULL;
22034 dw2_asm_output_data (2, 4, "DWARF macro version number");
22035 if (DWARF_OFFSET_SIZE == 8)
22036 dw2_asm_output_data (1, 1, "Flags: 64-bit");
22037 else
22038 dw2_asm_output_data (1, 0, "Flags: 32-bit");
22040 break;
22041 case DW_MACINFO_define:
22042 case DW_MACINFO_undef:
22043 output_macinfo_op (ref);
22044 ref->code = 0;
22045 ref->info = NULL;
22046 break;
22047 default:
22048 gcc_unreachable ();
22052 /* Set up for Dwarf output at the start of compilation. */
22054 static void
22055 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
22057 /* Allocate the file_table. */
22058 file_table = htab_create_ggc (50, file_table_hash,
22059 file_table_eq, NULL);
22061 /* Allocate the decl_die_table. */
22062 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
22063 decl_die_table_eq, NULL);
22065 /* Allocate the decl_loc_table. */
22066 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
22067 decl_loc_table_eq, NULL);
22069 /* Allocate the cached_dw_loc_list_table. */
22070 cached_dw_loc_list_table
22071 = htab_create_ggc (10, cached_dw_loc_list_table_hash,
22072 cached_dw_loc_list_table_eq, NULL);
22074 /* Allocate the initial hunk of the decl_scope_table. */
22075 vec_alloc (decl_scope_table, 256);
22077 /* Allocate the initial hunk of the abbrev_die_table. */
22078 abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
22079 (ABBREV_DIE_TABLE_INCREMENT);
22080 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
22081 /* Zero-th entry is allocated, but unused. */
22082 abbrev_die_table_in_use = 1;
22084 /* Allocate the pubtypes and pubnames vectors. */
22085 vec_alloc (pubname_table, 32);
22086 vec_alloc (pubtype_table, 32);
22088 vec_alloc (incomplete_types, 64);
22090 vec_alloc (used_rtx_array, 32);
22092 if (!dwarf_split_debug_info)
22094 debug_info_section = get_section (DEBUG_INFO_SECTION,
22095 SECTION_DEBUG, NULL);
22096 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22097 SECTION_DEBUG, NULL);
22098 debug_loc_section = get_section (DEBUG_LOC_SECTION,
22099 SECTION_DEBUG, NULL);
22101 else
22103 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
22104 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22105 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
22106 SECTION_DEBUG | SECTION_EXCLUDE,
22107 NULL);
22108 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
22109 SECTION_DEBUG, NULL);
22110 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
22111 SECTION_DEBUG, NULL);
22112 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22113 SECTION_DEBUG, NULL);
22114 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
22115 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
22117 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
22118 the main .o, but the skeleton_line goes into the split off dwo. */
22119 debug_skeleton_line_section
22120 = get_section (DEBUG_DWO_LINE_SECTION,
22121 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22122 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
22123 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
22124 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
22125 SECTION_DEBUG | SECTION_EXCLUDE,
22126 NULL);
22127 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
22128 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
22129 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
22130 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22131 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
22132 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
22134 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
22135 SECTION_DEBUG, NULL);
22136 debug_macinfo_section = get_section (dwarf_strict
22137 ? DEBUG_MACINFO_SECTION
22138 : DEBUG_MACRO_SECTION,
22139 DEBUG_MACRO_SECTION_FLAGS, NULL);
22140 debug_line_section = get_section (DEBUG_LINE_SECTION,
22141 SECTION_DEBUG, NULL);
22142 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
22143 SECTION_DEBUG, NULL);
22144 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
22145 SECTION_DEBUG, NULL);
22146 debug_str_section = get_section (DEBUG_STR_SECTION,
22147 DEBUG_STR_SECTION_FLAGS, NULL);
22148 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
22149 SECTION_DEBUG, NULL);
22150 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
22151 SECTION_DEBUG, NULL);
22153 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
22154 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
22155 DEBUG_ABBREV_SECTION_LABEL, 0);
22156 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
22157 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
22158 COLD_TEXT_SECTION_LABEL, 0);
22159 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
22161 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
22162 DEBUG_INFO_SECTION_LABEL, 0);
22163 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
22164 DEBUG_LINE_SECTION_LABEL, 0);
22165 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
22166 DEBUG_RANGES_SECTION_LABEL, 0);
22167 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
22168 DEBUG_ADDR_SECTION_LABEL, 0);
22169 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
22170 dwarf_strict
22171 ? DEBUG_MACINFO_SECTION_LABEL
22172 : DEBUG_MACRO_SECTION_LABEL, 0);
22173 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
22175 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22176 vec_alloc (macinfo_table, 64);
22178 switch_to_section (text_section);
22179 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
22181 /* Make sure the line number table for .text always exists. */
22182 text_section_line_info = new_line_info_table ();
22183 text_section_line_info->end_label = text_end_label;
22186 /* Called before compile () starts outputtting functions, variables
22187 and toplevel asms into assembly. */
22189 static void
22190 dwarf2out_assembly_start (void)
22192 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
22193 && dwarf2out_do_cfi_asm ()
22194 && (!(flag_unwind_tables || flag_exceptions)
22195 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
22196 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
22199 /* A helper function for dwarf2out_finish called through
22200 htab_traverse. Assign a string its index. All strings must be
22201 collected into the table by the time index_string is called,
22202 because the indexing code relies on htab_traverse to traverse nodes
22203 in the same order for each run. */
22205 static int
22206 index_string (void **h, void *v)
22208 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22209 unsigned int *index = (unsigned int *) v;
22211 find_string_form (node);
22212 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22214 gcc_assert (node->index == NO_INDEX_ASSIGNED);
22215 node->index = *index;
22216 *index += 1;
22218 return 1;
22221 /* A helper function for output_indirect_strings called through
22222 htab_traverse. Output the offset to a string and update the
22223 current offset. */
22225 static int
22226 output_index_string_offset (void **h, void *v)
22228 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22229 unsigned int *offset = (unsigned int *) v;
22231 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22233 /* Assert that this node has been assigned an index. */
22234 gcc_assert (node->index != NO_INDEX_ASSIGNED
22235 && node->index != NOT_INDEXED);
22236 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
22237 "indexed string 0x%x: %s", node->index, node->str);
22238 *offset += strlen (node->str) + 1;
22240 return 1;
22243 /* A helper function for dwarf2out_finish called through
22244 htab_traverse. Output the indexed string. */
22246 static int
22247 output_index_string (void **h, void *v)
22249 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22250 unsigned int *cur_idx = (unsigned int *) v;
22252 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22254 /* Assert that the strings are output in the same order as their
22255 indexes were assigned. */
22256 gcc_assert (*cur_idx == node->index);
22257 assemble_string (node->str, strlen (node->str) + 1);
22258 *cur_idx += 1;
22260 return 1;
22263 /* A helper function for dwarf2out_finish called through
22264 htab_traverse. Emit one queued .debug_str string. */
22266 static int
22267 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
22269 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22271 node->form = find_string_form (node);
22272 if (node->form == DW_FORM_strp && node->refcount > 0)
22274 ASM_OUTPUT_LABEL (asm_out_file, node->label);
22275 assemble_string (node->str, strlen (node->str) + 1);
22278 return 1;
22281 /* Output the indexed string table. */
22283 static void
22284 output_indirect_strings (void)
22286 switch_to_section (debug_str_section);
22287 if (!dwarf_split_debug_info)
22288 htab_traverse (debug_str_hash, output_indirect_string, NULL);
22289 else
22291 unsigned int offset = 0;
22292 unsigned int cur_idx = 0;
22294 htab_traverse (skeleton_debug_str_hash, output_indirect_string, NULL);
22296 switch_to_section (debug_str_offsets_section);
22297 htab_traverse_noresize (debug_str_hash,
22298 output_index_string_offset,
22299 &offset);
22300 switch_to_section (debug_str_dwo_section);
22301 htab_traverse_noresize (debug_str_hash,
22302 output_index_string,
22303 &cur_idx);
22307 /* Callback for htab_traverse to assign an index to an entry in the
22308 table, and to write that entry to the .debug_addr section. */
22310 static int
22311 output_addr_table_entry (void **slot, void *data)
22313 addr_table_entry *entry = (addr_table_entry *) *slot;
22314 unsigned int *cur_index = (unsigned int *)data;
22316 if (entry->refcount == 0)
22318 gcc_assert (entry->index == NO_INDEX_ASSIGNED
22319 || entry->index == NOT_INDEXED);
22320 return 1;
22323 gcc_assert (entry->index == *cur_index);
22324 (*cur_index)++;
22326 switch (entry->kind)
22328 case ate_kind_rtx:
22329 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
22330 "0x%x", entry->index);
22331 break;
22332 case ate_kind_rtx_dtprel:
22333 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
22334 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
22335 DWARF2_ADDR_SIZE,
22336 entry->addr.rtl);
22337 fputc ('\n', asm_out_file);
22338 break;
22339 case ate_kind_label:
22340 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
22341 "0x%x", entry->index);
22342 break;
22343 default:
22344 gcc_unreachable ();
22346 return 1;
22349 /* Produce the .debug_addr section. */
22351 static void
22352 output_addr_table (void)
22354 unsigned int index = 0;
22355 if (addr_index_table == NULL || htab_size (addr_index_table) == 0)
22356 return;
22358 switch_to_section (debug_addr_section);
22359 htab_traverse_noresize (addr_index_table, output_addr_table_entry, &index);
22362 #if ENABLE_ASSERT_CHECKING
22363 /* Verify that all marks are clear. */
22365 static void
22366 verify_marks_clear (dw_die_ref die)
22368 dw_die_ref c;
22370 gcc_assert (! die->die_mark);
22371 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
22373 #endif /* ENABLE_ASSERT_CHECKING */
22375 /* Clear the marks for a die and its children.
22376 Be cool if the mark isn't set. */
22378 static void
22379 prune_unmark_dies (dw_die_ref die)
22381 dw_die_ref c;
22383 if (die->die_mark)
22384 die->die_mark = 0;
22385 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
22388 /* Given DIE that we're marking as used, find any other dies
22389 it references as attributes and mark them as used. */
22391 static void
22392 prune_unused_types_walk_attribs (dw_die_ref die)
22394 dw_attr_ref a;
22395 unsigned ix;
22397 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22399 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
22401 /* A reference to another DIE.
22402 Make sure that it will get emitted.
22403 If it was broken out into a comdat group, don't follow it. */
22404 if (! AT_ref (a)->comdat_type_p
22405 || a->dw_attr == DW_AT_specification)
22406 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
22408 /* Set the string's refcount to 0 so that prune_unused_types_mark
22409 accounts properly for it. */
22410 if (AT_class (a) == dw_val_class_str)
22411 a->dw_attr_val.v.val_str->refcount = 0;
22415 /* Mark the generic parameters and arguments children DIEs of DIE. */
22417 static void
22418 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
22420 dw_die_ref c;
22422 if (die == NULL || die->die_child == NULL)
22423 return;
22424 c = die->die_child;
22427 if (is_template_parameter (c))
22428 prune_unused_types_mark (c, 1);
22429 c = c->die_sib;
22430 } while (c && c != die->die_child);
22433 /* Mark DIE as being used. If DOKIDS is true, then walk down
22434 to DIE's children. */
22436 static void
22437 prune_unused_types_mark (dw_die_ref die, int dokids)
22439 dw_die_ref c;
22441 if (die->die_mark == 0)
22443 /* We haven't done this node yet. Mark it as used. */
22444 die->die_mark = 1;
22445 /* If this is the DIE of a generic type instantiation,
22446 mark the children DIEs that describe its generic parms and
22447 args. */
22448 prune_unused_types_mark_generic_parms_dies (die);
22450 /* We also have to mark its parents as used.
22451 (But we don't want to mark our parent's kids due to this,
22452 unless it is a class.) */
22453 if (die->die_parent)
22454 prune_unused_types_mark (die->die_parent,
22455 class_scope_p (die->die_parent));
22457 /* Mark any referenced nodes. */
22458 prune_unused_types_walk_attribs (die);
22460 /* If this node is a specification,
22461 also mark the definition, if it exists. */
22462 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
22463 prune_unused_types_mark (die->die_definition, 1);
22466 if (dokids && die->die_mark != 2)
22468 /* We need to walk the children, but haven't done so yet.
22469 Remember that we've walked the kids. */
22470 die->die_mark = 2;
22472 /* If this is an array type, we need to make sure our
22473 kids get marked, even if they're types. If we're
22474 breaking out types into comdat sections, do this
22475 for all type definitions. */
22476 if (die->die_tag == DW_TAG_array_type
22477 || (use_debug_types
22478 && is_type_die (die) && ! is_declaration_die (die)))
22479 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
22480 else
22481 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22485 /* For local classes, look if any static member functions were emitted
22486 and if so, mark them. */
22488 static void
22489 prune_unused_types_walk_local_classes (dw_die_ref die)
22491 dw_die_ref c;
22493 if (die->die_mark == 2)
22494 return;
22496 switch (die->die_tag)
22498 case DW_TAG_structure_type:
22499 case DW_TAG_union_type:
22500 case DW_TAG_class_type:
22501 break;
22503 case DW_TAG_subprogram:
22504 if (!get_AT_flag (die, DW_AT_declaration)
22505 || die->die_definition != NULL)
22506 prune_unused_types_mark (die, 1);
22507 return;
22509 default:
22510 return;
22513 /* Mark children. */
22514 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
22517 /* Walk the tree DIE and mark types that we actually use. */
22519 static void
22520 prune_unused_types_walk (dw_die_ref die)
22522 dw_die_ref c;
22524 /* Don't do anything if this node is already marked and
22525 children have been marked as well. */
22526 if (die->die_mark == 2)
22527 return;
22529 switch (die->die_tag)
22531 case DW_TAG_structure_type:
22532 case DW_TAG_union_type:
22533 case DW_TAG_class_type:
22534 if (die->die_perennial_p)
22535 break;
22537 for (c = die->die_parent; c; c = c->die_parent)
22538 if (c->die_tag == DW_TAG_subprogram)
22539 break;
22541 /* Finding used static member functions inside of classes
22542 is needed just for local classes, because for other classes
22543 static member function DIEs with DW_AT_specification
22544 are emitted outside of the DW_TAG_*_type. If we ever change
22545 it, we'd need to call this even for non-local classes. */
22546 if (c)
22547 prune_unused_types_walk_local_classes (die);
22549 /* It's a type node --- don't mark it. */
22550 return;
22552 case DW_TAG_const_type:
22553 case DW_TAG_packed_type:
22554 case DW_TAG_pointer_type:
22555 case DW_TAG_reference_type:
22556 case DW_TAG_rvalue_reference_type:
22557 case DW_TAG_volatile_type:
22558 case DW_TAG_typedef:
22559 case DW_TAG_array_type:
22560 case DW_TAG_interface_type:
22561 case DW_TAG_friend:
22562 case DW_TAG_variant_part:
22563 case DW_TAG_enumeration_type:
22564 case DW_TAG_subroutine_type:
22565 case DW_TAG_string_type:
22566 case DW_TAG_set_type:
22567 case DW_TAG_subrange_type:
22568 case DW_TAG_ptr_to_member_type:
22569 case DW_TAG_file_type:
22570 if (die->die_perennial_p)
22571 break;
22573 /* It's a type node --- don't mark it. */
22574 return;
22576 default:
22577 /* Mark everything else. */
22578 break;
22581 if (die->die_mark == 0)
22583 die->die_mark = 1;
22585 /* Now, mark any dies referenced from here. */
22586 prune_unused_types_walk_attribs (die);
22589 die->die_mark = 2;
22591 /* Mark children. */
22592 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22595 /* Increment the string counts on strings referred to from DIE's
22596 attributes. */
22598 static void
22599 prune_unused_types_update_strings (dw_die_ref die)
22601 dw_attr_ref a;
22602 unsigned ix;
22604 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22605 if (AT_class (a) == dw_val_class_str)
22607 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22608 s->refcount++;
22609 /* Avoid unnecessarily putting strings that are used less than
22610 twice in the hash table. */
22611 if (s->refcount
22612 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22614 void ** slot;
22615 slot = htab_find_slot_with_hash (debug_str_hash, s->str,
22616 htab_hash_string (s->str),
22617 INSERT);
22618 gcc_assert (*slot == NULL);
22619 *slot = s;
22624 /* Remove from the tree DIE any dies that aren't marked. */
22626 static void
22627 prune_unused_types_prune (dw_die_ref die)
22629 dw_die_ref c;
22631 gcc_assert (die->die_mark);
22632 prune_unused_types_update_strings (die);
22634 if (! die->die_child)
22635 return;
22637 c = die->die_child;
22638 do {
22639 dw_die_ref prev = c;
22640 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
22641 if (c == die->die_child)
22643 /* No marked children between 'prev' and the end of the list. */
22644 if (prev == c)
22645 /* No marked children at all. */
22646 die->die_child = NULL;
22647 else
22649 prev->die_sib = c->die_sib;
22650 die->die_child = prev;
22652 return;
22655 if (c != prev->die_sib)
22656 prev->die_sib = c;
22657 prune_unused_types_prune (c);
22658 } while (c != die->die_child);
22661 /* Remove dies representing declarations that we never use. */
22663 static void
22664 prune_unused_types (void)
22666 unsigned int i;
22667 limbo_die_node *node;
22668 comdat_type_node *ctnode;
22669 pubname_ref pub;
22670 dw_die_ref base_type;
22672 #if ENABLE_ASSERT_CHECKING
22673 /* All the marks should already be clear. */
22674 verify_marks_clear (comp_unit_die ());
22675 for (node = limbo_die_list; node; node = node->next)
22676 verify_marks_clear (node->die);
22677 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22678 verify_marks_clear (ctnode->root_die);
22679 #endif /* ENABLE_ASSERT_CHECKING */
22681 /* Mark types that are used in global variables. */
22682 premark_types_used_by_global_vars ();
22684 /* Set the mark on nodes that are actually used. */
22685 prune_unused_types_walk (comp_unit_die ());
22686 for (node = limbo_die_list; node; node = node->next)
22687 prune_unused_types_walk (node->die);
22688 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22690 prune_unused_types_walk (ctnode->root_die);
22691 prune_unused_types_mark (ctnode->type_die, 1);
22694 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
22695 are unusual in that they are pubnames that are the children of pubtypes.
22696 They should only be marked via their parent DW_TAG_enumeration_type die,
22697 not as roots in themselves. */
22698 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
22699 if (pub->die->die_tag != DW_TAG_enumerator)
22700 prune_unused_types_mark (pub->die, 1);
22701 for (i = 0; base_types.iterate (i, &base_type); i++)
22702 prune_unused_types_mark (base_type, 1);
22704 if (debug_str_hash)
22705 htab_empty (debug_str_hash);
22706 if (skeleton_debug_str_hash)
22707 htab_empty (skeleton_debug_str_hash);
22708 prune_unused_types_prune (comp_unit_die ());
22709 for (node = limbo_die_list; node; node = node->next)
22710 prune_unused_types_prune (node->die);
22711 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22712 prune_unused_types_prune (ctnode->root_die);
22714 /* Leave the marks clear. */
22715 prune_unmark_dies (comp_unit_die ());
22716 for (node = limbo_die_list; node; node = node->next)
22717 prune_unmark_dies (node->die);
22718 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22719 prune_unmark_dies (ctnode->root_die);
22722 /* Set the parameter to true if there are any relative pathnames in
22723 the file table. */
22724 static int
22725 file_table_relative_p (void ** slot, void *param)
22727 bool *p = (bool *) param;
22728 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
22729 if (!IS_ABSOLUTE_PATH (d->filename))
22731 *p = true;
22732 return 0;
22734 return 1;
22737 /* Helpers to manipulate hash table of comdat type units. */
22739 struct comdat_type_hasher : typed_noop_remove <comdat_type_node>
22741 typedef comdat_type_node value_type;
22742 typedef comdat_type_node compare_type;
22743 static inline hashval_t hash (const value_type *);
22744 static inline bool equal (const value_type *, const compare_type *);
22747 inline hashval_t
22748 comdat_type_hasher::hash (const value_type *type_node)
22750 hashval_t h;
22751 memcpy (&h, type_node->signature, sizeof (h));
22752 return h;
22755 inline bool
22756 comdat_type_hasher::equal (const value_type *type_node_1,
22757 const compare_type *type_node_2)
22759 return (! memcmp (type_node_1->signature, type_node_2->signature,
22760 DWARF_TYPE_SIGNATURE_SIZE));
22763 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
22764 to the location it would have been added, should we know its
22765 DECL_ASSEMBLER_NAME when we added other attributes. This will
22766 probably improve compactness of debug info, removing equivalent
22767 abbrevs, and hide any differences caused by deferring the
22768 computation of the assembler name, triggered by e.g. PCH. */
22770 static inline void
22771 move_linkage_attr (dw_die_ref die)
22773 unsigned ix = vec_safe_length (die->die_attr);
22774 dw_attr_node linkage = (*die->die_attr)[ix - 1];
22776 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
22777 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
22779 while (--ix > 0)
22781 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
22783 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
22784 break;
22787 if (ix != vec_safe_length (die->die_attr) - 1)
22789 die->die_attr->pop ();
22790 die->die_attr->quick_insert (ix, linkage);
22794 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
22795 referenced from typed stack ops and count how often they are used. */
22797 static void
22798 mark_base_types (dw_loc_descr_ref loc)
22800 dw_die_ref base_type = NULL;
22802 for (; loc; loc = loc->dw_loc_next)
22804 switch (loc->dw_loc_opc)
22806 case DW_OP_GNU_regval_type:
22807 case DW_OP_GNU_deref_type:
22808 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
22809 break;
22810 case DW_OP_GNU_convert:
22811 case DW_OP_GNU_reinterpret:
22812 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
22813 continue;
22814 /* FALLTHRU */
22815 case DW_OP_GNU_const_type:
22816 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
22817 break;
22818 case DW_OP_GNU_entry_value:
22819 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
22820 continue;
22821 default:
22822 continue;
22824 gcc_assert (base_type->die_parent == comp_unit_die ());
22825 if (base_type->die_mark)
22826 base_type->die_mark++;
22827 else
22829 base_types.safe_push (base_type);
22830 base_type->die_mark = 1;
22835 /* Comparison function for sorting marked base types. */
22837 static int
22838 base_type_cmp (const void *x, const void *y)
22840 dw_die_ref dx = *(const dw_die_ref *) x;
22841 dw_die_ref dy = *(const dw_die_ref *) y;
22842 unsigned int byte_size1, byte_size2;
22843 unsigned int encoding1, encoding2;
22844 if (dx->die_mark > dy->die_mark)
22845 return -1;
22846 if (dx->die_mark < dy->die_mark)
22847 return 1;
22848 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
22849 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
22850 if (byte_size1 < byte_size2)
22851 return 1;
22852 if (byte_size1 > byte_size2)
22853 return -1;
22854 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
22855 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
22856 if (encoding1 < encoding2)
22857 return 1;
22858 if (encoding1 > encoding2)
22859 return -1;
22860 return 0;
22863 /* Move base types marked by mark_base_types as early as possible
22864 in the CU, sorted by decreasing usage count both to make the
22865 uleb128 references as small as possible and to make sure they
22866 will have die_offset already computed by calc_die_sizes when
22867 sizes of typed stack loc ops is computed. */
22869 static void
22870 move_marked_base_types (void)
22872 unsigned int i;
22873 dw_die_ref base_type, die, c;
22875 if (base_types.is_empty ())
22876 return;
22878 /* Sort by decreasing usage count, they will be added again in that
22879 order later on. */
22880 base_types.qsort (base_type_cmp);
22881 die = comp_unit_die ();
22882 c = die->die_child;
22885 dw_die_ref prev = c;
22886 c = c->die_sib;
22887 while (c->die_mark)
22889 remove_child_with_prev (c, prev);
22890 /* As base types got marked, there must be at least
22891 one node other than DW_TAG_base_type. */
22892 gcc_assert (c != c->die_sib);
22893 c = c->die_sib;
22896 while (c != die->die_child);
22897 gcc_assert (die->die_child);
22898 c = die->die_child;
22899 for (i = 0; base_types.iterate (i, &base_type); i++)
22901 base_type->die_mark = 0;
22902 base_type->die_sib = c->die_sib;
22903 c->die_sib = base_type;
22904 c = base_type;
22908 /* Helper function for resolve_addr, attempt to resolve
22909 one CONST_STRING, return non-zero if not successful. Similarly verify that
22910 SYMBOL_REFs refer to variables emitted in the current CU. */
22912 static int
22913 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
22915 rtx rtl = *addr;
22917 if (GET_CODE (rtl) == CONST_STRING)
22919 size_t len = strlen (XSTR (rtl, 0)) + 1;
22920 tree t = build_string (len, XSTR (rtl, 0));
22921 tree tlen = size_int (len - 1);
22922 TREE_TYPE (t)
22923 = build_array_type (char_type_node, build_index_type (tlen));
22924 rtl = lookup_constant_def (t);
22925 if (!rtl || !MEM_P (rtl))
22926 return 1;
22927 rtl = XEXP (rtl, 0);
22928 if (GET_CODE (rtl) == SYMBOL_REF
22929 && SYMBOL_REF_DECL (rtl)
22930 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22931 return 1;
22932 vec_safe_push (used_rtx_array, rtl);
22933 *addr = rtl;
22934 return 0;
22937 if (GET_CODE (rtl) == SYMBOL_REF
22938 && SYMBOL_REF_DECL (rtl))
22940 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
22942 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
22943 return 1;
22945 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22946 return 1;
22949 if (GET_CODE (rtl) == CONST
22950 && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
22951 return 1;
22953 return 0;
22956 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
22957 if possible, and create DW_TAG_dwarf_procedure that can be referenced
22958 from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet. */
22960 static rtx
22961 string_cst_pool_decl (tree t)
22963 rtx rtl = output_constant_def (t, 1);
22964 unsigned char *array;
22965 dw_loc_descr_ref l;
22966 tree decl;
22967 size_t len;
22968 dw_die_ref ref;
22970 if (!rtl || !MEM_P (rtl))
22971 return NULL_RTX;
22972 rtl = XEXP (rtl, 0);
22973 if (GET_CODE (rtl) != SYMBOL_REF
22974 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
22975 return NULL_RTX;
22977 decl = SYMBOL_REF_DECL (rtl);
22978 if (!lookup_decl_die (decl))
22980 len = TREE_STRING_LENGTH (t);
22981 vec_safe_push (used_rtx_array, rtl);
22982 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
22983 array = (unsigned char *) ggc_alloc_atomic (len);
22984 memcpy (array, TREE_STRING_POINTER (t), len);
22985 l = new_loc_descr (DW_OP_implicit_value, len, 0);
22986 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
22987 l->dw_loc_oprnd2.v.val_vec.length = len;
22988 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
22989 l->dw_loc_oprnd2.v.val_vec.array = array;
22990 add_AT_loc (ref, DW_AT_location, l);
22991 equate_decl_number_to_die (decl, ref);
22993 return rtl;
22996 /* Helper function of resolve_addr_in_expr. LOC is
22997 a DW_OP_addr followed by DW_OP_stack_value, either at the start
22998 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
22999 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
23000 with DW_OP_GNU_implicit_pointer if possible
23001 and return true, if unsuccessful, return false. */
23003 static bool
23004 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
23006 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
23007 HOST_WIDE_INT offset = 0;
23008 dw_die_ref ref = NULL;
23009 tree decl;
23011 if (GET_CODE (rtl) == CONST
23012 && GET_CODE (XEXP (rtl, 0)) == PLUS
23013 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
23015 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
23016 rtl = XEXP (XEXP (rtl, 0), 0);
23018 if (GET_CODE (rtl) == CONST_STRING)
23020 size_t len = strlen (XSTR (rtl, 0)) + 1;
23021 tree t = build_string (len, XSTR (rtl, 0));
23022 tree tlen = size_int (len - 1);
23024 TREE_TYPE (t)
23025 = build_array_type (char_type_node, build_index_type (tlen));
23026 rtl = string_cst_pool_decl (t);
23027 if (!rtl)
23028 return false;
23030 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
23032 decl = SYMBOL_REF_DECL (rtl);
23033 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
23035 ref = lookup_decl_die (decl);
23036 if (ref && (get_AT (ref, DW_AT_location)
23037 || get_AT (ref, DW_AT_const_value)))
23039 loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
23040 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23041 loc->dw_loc_oprnd1.val_entry = NULL;
23042 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23043 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23044 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23045 loc->dw_loc_oprnd2.v.val_int = offset;
23046 return true;
23050 return false;
23053 /* Helper function for resolve_addr, handle one location
23054 expression, return false if at least one CONST_STRING or SYMBOL_REF in
23055 the location list couldn't be resolved. */
23057 static bool
23058 resolve_addr_in_expr (dw_loc_descr_ref loc)
23060 dw_loc_descr_ref keep = NULL;
23061 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
23062 switch (loc->dw_loc_opc)
23064 case DW_OP_addr:
23065 if (resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
23067 if ((prev == NULL
23068 || prev->dw_loc_opc == DW_OP_piece
23069 || prev->dw_loc_opc == DW_OP_bit_piece)
23070 && loc->dw_loc_next
23071 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
23072 && !dwarf_strict
23073 && optimize_one_addr_into_implicit_ptr (loc))
23074 break;
23075 return false;
23077 break;
23078 case DW_OP_GNU_addr_index:
23079 case DW_OP_GNU_const_index:
23080 if ((loc->dw_loc_opc == DW_OP_GNU_addr_index
23081 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
23082 && resolve_one_addr (&loc->dw_loc_oprnd1.val_entry->addr.rtl,
23083 NULL))
23084 return false;
23085 break;
23086 case DW_OP_const4u:
23087 case DW_OP_const8u:
23088 if (loc->dtprel
23089 && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
23090 return false;
23091 break;
23092 case DW_OP_plus_uconst:
23093 if (size_of_loc_descr (loc)
23094 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
23096 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
23098 dw_loc_descr_ref repl
23099 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
23100 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
23101 add_loc_descr (&repl, loc->dw_loc_next);
23102 *loc = *repl;
23104 break;
23105 case DW_OP_implicit_value:
23106 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
23107 && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL))
23108 return false;
23109 break;
23110 case DW_OP_GNU_implicit_pointer:
23111 case DW_OP_GNU_parameter_ref:
23112 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
23114 dw_die_ref ref
23115 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
23116 if (ref == NULL)
23117 return false;
23118 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23119 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23120 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23122 break;
23123 case DW_OP_GNU_const_type:
23124 case DW_OP_GNU_regval_type:
23125 case DW_OP_GNU_deref_type:
23126 case DW_OP_GNU_convert:
23127 case DW_OP_GNU_reinterpret:
23128 while (loc->dw_loc_next
23129 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
23131 dw_die_ref base1, base2;
23132 unsigned enc1, enc2, size1, size2;
23133 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23134 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23135 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
23136 else if (loc->dw_loc_oprnd1.val_class
23137 == dw_val_class_unsigned_const)
23138 break;
23139 else
23140 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
23141 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
23142 == dw_val_class_unsigned_const)
23143 break;
23144 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
23145 gcc_assert (base1->die_tag == DW_TAG_base_type
23146 && base2->die_tag == DW_TAG_base_type);
23147 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
23148 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
23149 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
23150 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
23151 if (size1 == size2
23152 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
23153 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
23154 && loc != keep)
23155 || enc1 == enc2))
23157 /* Optimize away next DW_OP_GNU_convert after
23158 adjusting LOC's base type die reference. */
23159 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23160 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23161 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
23162 else
23163 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
23164 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23165 continue;
23167 /* Don't change integer DW_OP_GNU_convert after e.g. floating
23168 point typed stack entry. */
23169 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
23170 keep = loc->dw_loc_next;
23171 break;
23173 break;
23174 default:
23175 break;
23177 return true;
23180 /* Helper function of resolve_addr. DIE had DW_AT_location of
23181 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
23182 and DW_OP_addr couldn't be resolved. resolve_addr has already
23183 removed the DW_AT_location attribute. This function attempts to
23184 add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
23185 to it or DW_AT_const_value attribute, if possible. */
23187 static void
23188 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
23190 if (TREE_CODE (decl) != VAR_DECL
23191 || lookup_decl_die (decl) != die
23192 || DECL_EXTERNAL (decl)
23193 || !TREE_STATIC (decl)
23194 || DECL_INITIAL (decl) == NULL_TREE
23195 || DECL_P (DECL_INITIAL (decl))
23196 || get_AT (die, DW_AT_const_value))
23197 return;
23199 tree init = DECL_INITIAL (decl);
23200 HOST_WIDE_INT offset = 0;
23201 /* For variables that have been optimized away and thus
23202 don't have a memory location, see if we can emit
23203 DW_AT_const_value instead. */
23204 if (tree_add_const_value_attribute (die, init))
23205 return;
23206 if (dwarf_strict)
23207 return;
23208 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
23209 and ADDR_EXPR refers to a decl that has DW_AT_location or
23210 DW_AT_const_value (but isn't addressable, otherwise
23211 resolving the original DW_OP_addr wouldn't fail), see if
23212 we can add DW_OP_GNU_implicit_pointer. */
23213 STRIP_NOPS (init);
23214 if (TREE_CODE (init) == POINTER_PLUS_EXPR
23215 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
23217 offset = tree_to_shwi (TREE_OPERAND (init, 1));
23218 init = TREE_OPERAND (init, 0);
23219 STRIP_NOPS (init);
23221 if (TREE_CODE (init) != ADDR_EXPR)
23222 return;
23223 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
23224 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
23225 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
23226 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
23227 && TREE_OPERAND (init, 0) != decl))
23229 dw_die_ref ref;
23230 dw_loc_descr_ref l;
23232 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
23234 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
23235 if (!rtl)
23236 return;
23237 decl = SYMBOL_REF_DECL (rtl);
23239 else
23240 decl = TREE_OPERAND (init, 0);
23241 ref = lookup_decl_die (decl);
23242 if (ref == NULL
23243 || (!get_AT (ref, DW_AT_location)
23244 && !get_AT (ref, DW_AT_const_value)))
23245 return;
23246 l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
23247 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23248 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
23249 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
23250 add_AT_loc (die, DW_AT_location, l);
23254 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
23255 an address in .rodata section if the string literal is emitted there,
23256 or remove the containing location list or replace DW_AT_const_value
23257 with DW_AT_location and empty location expression, if it isn't found
23258 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
23259 to something that has been emitted in the current CU. */
23261 static void
23262 resolve_addr (dw_die_ref die)
23264 dw_die_ref c;
23265 dw_attr_ref a;
23266 dw_loc_list_ref *curr, *start, loc;
23267 unsigned ix;
23269 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23270 switch (AT_class (a))
23272 case dw_val_class_loc_list:
23273 start = curr = AT_loc_list_ptr (a);
23274 loc = *curr;
23275 gcc_assert (loc);
23276 /* The same list can be referenced more than once. See if we have
23277 already recorded the result from a previous pass. */
23278 if (loc->replaced)
23279 *curr = loc->dw_loc_next;
23280 else if (!loc->resolved_addr)
23282 /* As things stand, we do not expect or allow one die to
23283 reference a suffix of another die's location list chain.
23284 References must be identical or completely separate.
23285 There is therefore no need to cache the result of this
23286 pass on any list other than the first; doing so
23287 would lead to unnecessary writes. */
23288 while (*curr)
23290 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
23291 if (!resolve_addr_in_expr ((*curr)->expr))
23293 dw_loc_list_ref next = (*curr)->dw_loc_next;
23294 dw_loc_descr_ref l = (*curr)->expr;
23296 if (next && (*curr)->ll_symbol)
23298 gcc_assert (!next->ll_symbol);
23299 next->ll_symbol = (*curr)->ll_symbol;
23301 if (dwarf_split_debug_info)
23302 remove_loc_list_addr_table_entries (l);
23303 *curr = next;
23305 else
23307 mark_base_types ((*curr)->expr);
23308 curr = &(*curr)->dw_loc_next;
23311 if (loc == *start)
23312 loc->resolved_addr = 1;
23313 else
23315 loc->replaced = 1;
23316 loc->dw_loc_next = *start;
23319 if (!*start)
23321 remove_AT (die, a->dw_attr);
23322 ix--;
23324 break;
23325 case dw_val_class_loc:
23327 dw_loc_descr_ref l = AT_loc (a);
23328 /* For -gdwarf-2 don't attempt to optimize
23329 DW_AT_data_member_location containing
23330 DW_OP_plus_uconst - older consumers might
23331 rely on it being that op instead of a more complex,
23332 but shorter, location description. */
23333 if ((dwarf_version > 2
23334 || a->dw_attr != DW_AT_data_member_location
23335 || l == NULL
23336 || l->dw_loc_opc != DW_OP_plus_uconst
23337 || l->dw_loc_next != NULL)
23338 && !resolve_addr_in_expr (l))
23340 if (dwarf_split_debug_info)
23341 remove_loc_list_addr_table_entries (l);
23342 if (l != NULL
23343 && l->dw_loc_next == NULL
23344 && l->dw_loc_opc == DW_OP_addr
23345 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
23346 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
23347 && a->dw_attr == DW_AT_location)
23349 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
23350 remove_AT (die, a->dw_attr);
23351 ix--;
23352 optimize_location_into_implicit_ptr (die, decl);
23353 break;
23355 remove_AT (die, a->dw_attr);
23356 ix--;
23358 else
23359 mark_base_types (l);
23361 break;
23362 case dw_val_class_addr:
23363 if (a->dw_attr == DW_AT_const_value
23364 && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
23366 if (AT_index (a) != NOT_INDEXED)
23367 remove_addr_table_entry (a->dw_attr_val.val_entry);
23368 remove_AT (die, a->dw_attr);
23369 ix--;
23371 if (die->die_tag == DW_TAG_GNU_call_site
23372 && a->dw_attr == DW_AT_abstract_origin)
23374 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
23375 dw_die_ref tdie = lookup_decl_die (tdecl);
23376 if (tdie == NULL
23377 && DECL_EXTERNAL (tdecl)
23378 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
23380 force_decl_die (tdecl);
23381 tdie = lookup_decl_die (tdecl);
23383 if (tdie)
23385 a->dw_attr_val.val_class = dw_val_class_die_ref;
23386 a->dw_attr_val.v.val_die_ref.die = tdie;
23387 a->dw_attr_val.v.val_die_ref.external = 0;
23389 else
23391 if (AT_index (a) != NOT_INDEXED)
23392 remove_addr_table_entry (a->dw_attr_val.val_entry);
23393 remove_AT (die, a->dw_attr);
23394 ix--;
23397 break;
23398 default:
23399 break;
23402 FOR_EACH_CHILD (die, c, resolve_addr (c));
23405 /* Helper routines for optimize_location_lists.
23406 This pass tries to share identical local lists in .debug_loc
23407 section. */
23409 /* Iteratively hash operands of LOC opcode. */
23411 static hashval_t
23412 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
23414 dw_val_ref val1 = &loc->dw_loc_oprnd1;
23415 dw_val_ref val2 = &loc->dw_loc_oprnd2;
23417 switch (loc->dw_loc_opc)
23419 case DW_OP_const4u:
23420 case DW_OP_const8u:
23421 if (loc->dtprel)
23422 goto hash_addr;
23423 /* FALLTHRU */
23424 case DW_OP_const1u:
23425 case DW_OP_const1s:
23426 case DW_OP_const2u:
23427 case DW_OP_const2s:
23428 case DW_OP_const4s:
23429 case DW_OP_const8s:
23430 case DW_OP_constu:
23431 case DW_OP_consts:
23432 case DW_OP_pick:
23433 case DW_OP_plus_uconst:
23434 case DW_OP_breg0:
23435 case DW_OP_breg1:
23436 case DW_OP_breg2:
23437 case DW_OP_breg3:
23438 case DW_OP_breg4:
23439 case DW_OP_breg5:
23440 case DW_OP_breg6:
23441 case DW_OP_breg7:
23442 case DW_OP_breg8:
23443 case DW_OP_breg9:
23444 case DW_OP_breg10:
23445 case DW_OP_breg11:
23446 case DW_OP_breg12:
23447 case DW_OP_breg13:
23448 case DW_OP_breg14:
23449 case DW_OP_breg15:
23450 case DW_OP_breg16:
23451 case DW_OP_breg17:
23452 case DW_OP_breg18:
23453 case DW_OP_breg19:
23454 case DW_OP_breg20:
23455 case DW_OP_breg21:
23456 case DW_OP_breg22:
23457 case DW_OP_breg23:
23458 case DW_OP_breg24:
23459 case DW_OP_breg25:
23460 case DW_OP_breg26:
23461 case DW_OP_breg27:
23462 case DW_OP_breg28:
23463 case DW_OP_breg29:
23464 case DW_OP_breg30:
23465 case DW_OP_breg31:
23466 case DW_OP_regx:
23467 case DW_OP_fbreg:
23468 case DW_OP_piece:
23469 case DW_OP_deref_size:
23470 case DW_OP_xderef_size:
23471 hash = iterative_hash_object (val1->v.val_int, hash);
23472 break;
23473 case DW_OP_skip:
23474 case DW_OP_bra:
23476 int offset;
23478 gcc_assert (val1->val_class == dw_val_class_loc);
23479 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
23480 hash = iterative_hash_object (offset, hash);
23482 break;
23483 case DW_OP_implicit_value:
23484 hash = iterative_hash_object (val1->v.val_unsigned, hash);
23485 switch (val2->val_class)
23487 case dw_val_class_const:
23488 hash = iterative_hash_object (val2->v.val_int, hash);
23489 break;
23490 case dw_val_class_vec:
23492 unsigned int elt_size = val2->v.val_vec.elt_size;
23493 unsigned int len = val2->v.val_vec.length;
23495 hash = iterative_hash_object (elt_size, hash);
23496 hash = iterative_hash_object (len, hash);
23497 hash = iterative_hash (val2->v.val_vec.array,
23498 len * elt_size, hash);
23500 break;
23501 case dw_val_class_const_double:
23502 hash = iterative_hash_object (val2->v.val_double.low, hash);
23503 hash = iterative_hash_object (val2->v.val_double.high, hash);
23504 break;
23505 case dw_val_class_addr:
23506 hash = iterative_hash_rtx (val2->v.val_addr, hash);
23507 break;
23508 default:
23509 gcc_unreachable ();
23511 break;
23512 case DW_OP_bregx:
23513 case DW_OP_bit_piece:
23514 hash = iterative_hash_object (val1->v.val_int, hash);
23515 hash = iterative_hash_object (val2->v.val_int, hash);
23516 break;
23517 case DW_OP_addr:
23518 hash_addr:
23519 if (loc->dtprel)
23521 unsigned char dtprel = 0xd1;
23522 hash = iterative_hash_object (dtprel, hash);
23524 hash = iterative_hash_rtx (val1->v.val_addr, hash);
23525 break;
23526 case DW_OP_GNU_addr_index:
23527 case DW_OP_GNU_const_index:
23529 if (loc->dtprel)
23531 unsigned char dtprel = 0xd1;
23532 hash = iterative_hash_object (dtprel, hash);
23534 hash = iterative_hash_rtx (val1->val_entry->addr.rtl, hash);
23536 break;
23537 case DW_OP_GNU_implicit_pointer:
23538 hash = iterative_hash_object (val2->v.val_int, hash);
23539 break;
23540 case DW_OP_GNU_entry_value:
23541 hash = hash_loc_operands (val1->v.val_loc, hash);
23542 break;
23543 case DW_OP_GNU_regval_type:
23544 case DW_OP_GNU_deref_type:
23546 unsigned int byte_size
23547 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
23548 unsigned int encoding
23549 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
23550 hash = iterative_hash_object (val1->v.val_int, hash);
23551 hash = iterative_hash_object (byte_size, hash);
23552 hash = iterative_hash_object (encoding, hash);
23554 break;
23555 case DW_OP_GNU_convert:
23556 case DW_OP_GNU_reinterpret:
23557 if (val1->val_class == dw_val_class_unsigned_const)
23559 hash = iterative_hash_object (val1->v.val_unsigned, hash);
23560 break;
23562 /* FALLTHRU */
23563 case DW_OP_GNU_const_type:
23565 unsigned int byte_size
23566 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
23567 unsigned int encoding
23568 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
23569 hash = iterative_hash_object (byte_size, hash);
23570 hash = iterative_hash_object (encoding, hash);
23571 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
23572 break;
23573 hash = iterative_hash_object (val2->val_class, hash);
23574 switch (val2->val_class)
23576 case dw_val_class_const:
23577 hash = iterative_hash_object (val2->v.val_int, hash);
23578 break;
23579 case dw_val_class_vec:
23581 unsigned int elt_size = val2->v.val_vec.elt_size;
23582 unsigned int len = val2->v.val_vec.length;
23584 hash = iterative_hash_object (elt_size, hash);
23585 hash = iterative_hash_object (len, hash);
23586 hash = iterative_hash (val2->v.val_vec.array,
23587 len * elt_size, hash);
23589 break;
23590 case dw_val_class_const_double:
23591 hash = iterative_hash_object (val2->v.val_double.low, hash);
23592 hash = iterative_hash_object (val2->v.val_double.high, hash);
23593 break;
23594 default:
23595 gcc_unreachable ();
23598 break;
23600 default:
23601 /* Other codes have no operands. */
23602 break;
23604 return hash;
23607 /* Iteratively hash the whole DWARF location expression LOC. */
23609 static inline hashval_t
23610 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
23612 dw_loc_descr_ref l;
23613 bool sizes_computed = false;
23614 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
23615 size_of_locs (loc);
23617 for (l = loc; l != NULL; l = l->dw_loc_next)
23619 enum dwarf_location_atom opc = l->dw_loc_opc;
23620 hash = iterative_hash_object (opc, hash);
23621 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
23623 size_of_locs (loc);
23624 sizes_computed = true;
23626 hash = hash_loc_operands (l, hash);
23628 return hash;
23631 /* Compute hash of the whole location list LIST_HEAD. */
23633 static inline void
23634 hash_loc_list (dw_loc_list_ref list_head)
23636 dw_loc_list_ref curr = list_head;
23637 hashval_t hash = 0;
23639 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
23641 hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
23642 hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
23643 if (curr->section)
23644 hash = iterative_hash (curr->section, strlen (curr->section) + 1,
23645 hash);
23646 hash = hash_locs (curr->expr, hash);
23648 list_head->hash = hash;
23651 /* Return true if X and Y opcodes have the same operands. */
23653 static inline bool
23654 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
23656 dw_val_ref valx1 = &x->dw_loc_oprnd1;
23657 dw_val_ref valx2 = &x->dw_loc_oprnd2;
23658 dw_val_ref valy1 = &y->dw_loc_oprnd1;
23659 dw_val_ref valy2 = &y->dw_loc_oprnd2;
23661 switch (x->dw_loc_opc)
23663 case DW_OP_const4u:
23664 case DW_OP_const8u:
23665 if (x->dtprel)
23666 goto hash_addr;
23667 /* FALLTHRU */
23668 case DW_OP_const1u:
23669 case DW_OP_const1s:
23670 case DW_OP_const2u:
23671 case DW_OP_const2s:
23672 case DW_OP_const4s:
23673 case DW_OP_const8s:
23674 case DW_OP_constu:
23675 case DW_OP_consts:
23676 case DW_OP_pick:
23677 case DW_OP_plus_uconst:
23678 case DW_OP_breg0:
23679 case DW_OP_breg1:
23680 case DW_OP_breg2:
23681 case DW_OP_breg3:
23682 case DW_OP_breg4:
23683 case DW_OP_breg5:
23684 case DW_OP_breg6:
23685 case DW_OP_breg7:
23686 case DW_OP_breg8:
23687 case DW_OP_breg9:
23688 case DW_OP_breg10:
23689 case DW_OP_breg11:
23690 case DW_OP_breg12:
23691 case DW_OP_breg13:
23692 case DW_OP_breg14:
23693 case DW_OP_breg15:
23694 case DW_OP_breg16:
23695 case DW_OP_breg17:
23696 case DW_OP_breg18:
23697 case DW_OP_breg19:
23698 case DW_OP_breg20:
23699 case DW_OP_breg21:
23700 case DW_OP_breg22:
23701 case DW_OP_breg23:
23702 case DW_OP_breg24:
23703 case DW_OP_breg25:
23704 case DW_OP_breg26:
23705 case DW_OP_breg27:
23706 case DW_OP_breg28:
23707 case DW_OP_breg29:
23708 case DW_OP_breg30:
23709 case DW_OP_breg31:
23710 case DW_OP_regx:
23711 case DW_OP_fbreg:
23712 case DW_OP_piece:
23713 case DW_OP_deref_size:
23714 case DW_OP_xderef_size:
23715 return valx1->v.val_int == valy1->v.val_int;
23716 case DW_OP_skip:
23717 case DW_OP_bra:
23718 /* If splitting debug info, the use of DW_OP_GNU_addr_index
23719 can cause irrelevant differences in dw_loc_addr. */
23720 gcc_assert (valx1->val_class == dw_val_class_loc
23721 && valy1->val_class == dw_val_class_loc
23722 && (dwarf_split_debug_info
23723 || x->dw_loc_addr == y->dw_loc_addr));
23724 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
23725 case DW_OP_implicit_value:
23726 if (valx1->v.val_unsigned != valy1->v.val_unsigned
23727 || valx2->val_class != valy2->val_class)
23728 return false;
23729 switch (valx2->val_class)
23731 case dw_val_class_const:
23732 return valx2->v.val_int == valy2->v.val_int;
23733 case dw_val_class_vec:
23734 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23735 && valx2->v.val_vec.length == valy2->v.val_vec.length
23736 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23737 valx2->v.val_vec.elt_size
23738 * valx2->v.val_vec.length) == 0;
23739 case dw_val_class_const_double:
23740 return valx2->v.val_double.low == valy2->v.val_double.low
23741 && valx2->v.val_double.high == valy2->v.val_double.high;
23742 case dw_val_class_addr:
23743 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
23744 default:
23745 gcc_unreachable ();
23747 case DW_OP_bregx:
23748 case DW_OP_bit_piece:
23749 return valx1->v.val_int == valy1->v.val_int
23750 && valx2->v.val_int == valy2->v.val_int;
23751 case DW_OP_addr:
23752 hash_addr:
23753 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
23754 case DW_OP_GNU_addr_index:
23755 case DW_OP_GNU_const_index:
23757 rtx ax1 = valx1->val_entry->addr.rtl;
23758 rtx ay1 = valy1->val_entry->addr.rtl;
23759 return rtx_equal_p (ax1, ay1);
23761 case DW_OP_GNU_implicit_pointer:
23762 return valx1->val_class == dw_val_class_die_ref
23763 && valx1->val_class == valy1->val_class
23764 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
23765 && valx2->v.val_int == valy2->v.val_int;
23766 case DW_OP_GNU_entry_value:
23767 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
23768 case DW_OP_GNU_const_type:
23769 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
23770 || valx2->val_class != valy2->val_class)
23771 return false;
23772 switch (valx2->val_class)
23774 case dw_val_class_const:
23775 return valx2->v.val_int == valy2->v.val_int;
23776 case dw_val_class_vec:
23777 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23778 && valx2->v.val_vec.length == valy2->v.val_vec.length
23779 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23780 valx2->v.val_vec.elt_size
23781 * valx2->v.val_vec.length) == 0;
23782 case dw_val_class_const_double:
23783 return valx2->v.val_double.low == valy2->v.val_double.low
23784 && valx2->v.val_double.high == valy2->v.val_double.high;
23785 default:
23786 gcc_unreachable ();
23788 case DW_OP_GNU_regval_type:
23789 case DW_OP_GNU_deref_type:
23790 return valx1->v.val_int == valy1->v.val_int
23791 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
23792 case DW_OP_GNU_convert:
23793 case DW_OP_GNU_reinterpret:
23794 if (valx1->val_class != valy1->val_class)
23795 return false;
23796 if (valx1->val_class == dw_val_class_unsigned_const)
23797 return valx1->v.val_unsigned == valy1->v.val_unsigned;
23798 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
23799 case DW_OP_GNU_parameter_ref:
23800 return valx1->val_class == dw_val_class_die_ref
23801 && valx1->val_class == valy1->val_class
23802 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
23803 default:
23804 /* Other codes have no operands. */
23805 return true;
23809 /* Return true if DWARF location expressions X and Y are the same. */
23811 static inline bool
23812 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
23814 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
23815 if (x->dw_loc_opc != y->dw_loc_opc
23816 || x->dtprel != y->dtprel
23817 || !compare_loc_operands (x, y))
23818 break;
23819 return x == NULL && y == NULL;
23822 /* Hashtable helpers. */
23824 struct loc_list_hasher : typed_noop_remove <dw_loc_list_struct>
23826 typedef dw_loc_list_struct value_type;
23827 typedef dw_loc_list_struct compare_type;
23828 static inline hashval_t hash (const value_type *);
23829 static inline bool equal (const value_type *, const compare_type *);
23832 /* Return precomputed hash of location list X. */
23834 inline hashval_t
23835 loc_list_hasher::hash (const value_type *x)
23837 return x->hash;
23840 /* Return true if location lists A and B are the same. */
23842 inline bool
23843 loc_list_hasher::equal (const value_type *a, const compare_type *b)
23845 if (a == b)
23846 return 1;
23847 if (a->hash != b->hash)
23848 return 0;
23849 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
23850 if (strcmp (a->begin, b->begin) != 0
23851 || strcmp (a->end, b->end) != 0
23852 || (a->section == NULL) != (b->section == NULL)
23853 || (a->section && strcmp (a->section, b->section) != 0)
23854 || !compare_locs (a->expr, b->expr))
23855 break;
23856 return a == NULL && b == NULL;
23859 typedef hash_table <loc_list_hasher> loc_list_hash_type;
23862 /* Recursively optimize location lists referenced from DIE
23863 children and share them whenever possible. */
23865 static void
23866 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type htab)
23868 dw_die_ref c;
23869 dw_attr_ref a;
23870 unsigned ix;
23871 dw_loc_list_struct **slot;
23873 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23874 if (AT_class (a) == dw_val_class_loc_list)
23876 dw_loc_list_ref list = AT_loc_list (a);
23877 /* TODO: perform some optimizations here, before hashing
23878 it and storing into the hash table. */
23879 hash_loc_list (list);
23880 slot = htab.find_slot_with_hash (list, list->hash, INSERT);
23881 if (*slot == NULL)
23882 *slot = list;
23883 else
23884 a->dw_attr_val.v.val_loc_list = *slot;
23887 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
23891 /* Recursively assign each location list a unique index into the debug_addr
23892 section. */
23894 static void
23895 index_location_lists (dw_die_ref die)
23897 dw_die_ref c;
23898 dw_attr_ref a;
23899 unsigned ix;
23901 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23902 if (AT_class (a) == dw_val_class_loc_list)
23904 dw_loc_list_ref list = AT_loc_list (a);
23905 dw_loc_list_ref curr;
23906 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
23908 /* Don't index an entry that has already been indexed
23909 or won't be output. */
23910 if (curr->begin_entry != NULL
23911 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
23912 continue;
23914 curr->begin_entry
23915 = add_addr_table_entry (xstrdup (curr->begin),
23916 ate_kind_label);
23920 FOR_EACH_CHILD (die, c, index_location_lists (c));
23923 /* Optimize location lists referenced from DIE
23924 children and share them whenever possible. */
23926 static void
23927 optimize_location_lists (dw_die_ref die)
23929 loc_list_hash_type htab;
23930 htab.create (500);
23931 optimize_location_lists_1 (die, htab);
23932 htab.dispose ();
23935 /* Output stuff that dwarf requires at the end of every file,
23936 and generate the DWARF-2 debugging info. */
23938 static void
23939 dwarf2out_finish (const char *filename)
23941 limbo_die_node *node, *next_node;
23942 comdat_type_node *ctnode;
23943 hash_table <comdat_type_hasher> comdat_type_table;
23944 unsigned int i;
23945 dw_die_ref main_comp_unit_die;
23947 /* PCH might result in DW_AT_producer string being restored from the
23948 header compilation, so always fill it with empty string initially
23949 and overwrite only here. */
23950 dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
23951 producer_string = gen_producer_string ();
23952 producer->dw_attr_val.v.val_str->refcount--;
23953 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
23955 gen_scheduled_generic_parms_dies ();
23956 gen_remaining_tmpl_value_param_die_attribute ();
23958 /* Add the name for the main input file now. We delayed this from
23959 dwarf2out_init to avoid complications with PCH. */
23960 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
23961 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
23962 add_comp_dir_attribute (comp_unit_die ());
23963 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
23965 bool p = false;
23966 htab_traverse (file_table, file_table_relative_p, &p);
23967 if (p)
23968 add_comp_dir_attribute (comp_unit_die ());
23971 if (deferred_locations_list)
23972 for (i = 0; i < deferred_locations_list->length (); i++)
23974 add_location_or_const_value_attribute (
23975 (*deferred_locations_list)[i].die,
23976 (*deferred_locations_list)[i].variable,
23977 false,
23978 DW_AT_location);
23981 /* Traverse the limbo die list, and add parent/child links. The only
23982 dies without parents that should be here are concrete instances of
23983 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
23984 For concrete instances, we can get the parent die from the abstract
23985 instance. */
23986 for (node = limbo_die_list; node; node = next_node)
23988 dw_die_ref die = node->die;
23989 next_node = node->next;
23991 if (die->die_parent == NULL)
23993 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
23995 if (origin && origin->die_parent)
23996 add_child_die (origin->die_parent, die);
23997 else if (is_cu_die (die))
23999 else if (seen_error ())
24000 /* It's OK to be confused by errors in the input. */
24001 add_child_die (comp_unit_die (), die);
24002 else
24004 /* In certain situations, the lexical block containing a
24005 nested function can be optimized away, which results
24006 in the nested function die being orphaned. Likewise
24007 with the return type of that nested function. Force
24008 this to be a child of the containing function.
24010 It may happen that even the containing function got fully
24011 inlined and optimized out. In that case we are lost and
24012 assign the empty child. This should not be big issue as
24013 the function is likely unreachable too. */
24014 gcc_assert (node->created_for);
24016 if (DECL_P (node->created_for))
24017 origin = get_context_die (DECL_CONTEXT (node->created_for));
24018 else if (TYPE_P (node->created_for))
24019 origin = scope_die_for (node->created_for, comp_unit_die ());
24020 else
24021 origin = comp_unit_die ();
24023 add_child_die (origin, die);
24028 limbo_die_list = NULL;
24030 #if ENABLE_ASSERT_CHECKING
24032 dw_die_ref die = comp_unit_die (), c;
24033 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
24035 #endif
24036 resolve_addr (comp_unit_die ());
24037 move_marked_base_types ();
24039 for (node = deferred_asm_name; node; node = node->next)
24041 tree decl = node->created_for;
24042 /* When generating LTO bytecode we can not generate new assembler
24043 names at this point and all important decls got theirs via
24044 free-lang-data. */
24045 if ((!flag_generate_lto || DECL_ASSEMBLER_NAME_SET_P (decl))
24046 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
24048 add_linkage_attr (node->die, decl);
24049 move_linkage_attr (node->die);
24053 deferred_asm_name = NULL;
24055 /* Walk through the list of incomplete types again, trying once more to
24056 emit full debugging info for them. */
24057 retry_incomplete_types ();
24059 if (flag_eliminate_unused_debug_types)
24060 prune_unused_types ();
24062 /* Generate separate COMDAT sections for type DIEs. */
24063 if (use_debug_types)
24065 break_out_comdat_types (comp_unit_die ());
24067 /* Each new type_unit DIE was added to the limbo die list when created.
24068 Since these have all been added to comdat_type_list, clear the
24069 limbo die list. */
24070 limbo_die_list = NULL;
24072 /* For each new comdat type unit, copy declarations for incomplete
24073 types to make the new unit self-contained (i.e., no direct
24074 references to the main compile unit). */
24075 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24076 copy_decls_for_unworthy_types (ctnode->root_die);
24077 copy_decls_for_unworthy_types (comp_unit_die ());
24079 /* In the process of copying declarations from one unit to another,
24080 we may have left some declarations behind that are no longer
24081 referenced. Prune them. */
24082 prune_unused_types ();
24085 /* Generate separate CUs for each of the include files we've seen.
24086 They will go into limbo_die_list. */
24087 if (flag_eliminate_dwarf2_dups)
24088 break_out_includes (comp_unit_die ());
24090 /* Traverse the DIE's and add add sibling attributes to those DIE's
24091 that have children. */
24092 add_sibling_attributes (comp_unit_die ());
24093 for (node = limbo_die_list; node; node = node->next)
24094 add_sibling_attributes (node->die);
24095 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24096 add_sibling_attributes (ctnode->root_die);
24098 /* When splitting DWARF info, we put some attributes in the
24099 skeleton compile_unit DIE that remains in the .o, while
24100 most attributes go in the DWO compile_unit_die. */
24101 if (dwarf_split_debug_info)
24102 main_comp_unit_die = gen_compile_unit_die (NULL);
24103 else
24104 main_comp_unit_die = comp_unit_die ();
24106 /* Output a terminator label for the .text section. */
24107 switch_to_section (text_section);
24108 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
24109 if (cold_text_section)
24111 switch_to_section (cold_text_section);
24112 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
24115 /* We can only use the low/high_pc attributes if all of the code was
24116 in .text. */
24117 if (!have_multiple_function_sections
24118 || (dwarf_version < 3 && dwarf_strict))
24120 /* Don't add if the CU has no associated code. */
24121 if (text_section_used)
24122 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
24123 text_end_label, true);
24125 else
24127 unsigned fde_idx;
24128 dw_fde_ref fde;
24129 bool range_list_added = false;
24131 if (text_section_used)
24132 add_ranges_by_labels (main_comp_unit_die, text_section_label,
24133 text_end_label, &range_list_added, true);
24134 if (cold_text_section_used)
24135 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
24136 cold_end_label, &range_list_added, true);
24138 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
24140 if (DECL_IGNORED_P (fde->decl))
24141 continue;
24142 if (!fde->in_std_section)
24143 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
24144 fde->dw_fde_end, &range_list_added,
24145 true);
24146 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
24147 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
24148 fde->dw_fde_second_end, &range_list_added,
24149 true);
24152 if (range_list_added)
24154 /* We need to give .debug_loc and .debug_ranges an appropriate
24155 "base address". Use zero so that these addresses become
24156 absolute. Historically, we've emitted the unexpected
24157 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
24158 Emit both to give time for other tools to adapt. */
24159 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
24160 if (! dwarf_strict && dwarf_version < 4)
24161 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
24163 add_ranges (NULL);
24167 if (debug_info_level >= DINFO_LEVEL_TERSE)
24168 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
24169 debug_line_section_label);
24171 if (have_macinfo)
24172 add_AT_macptr (comp_unit_die (),
24173 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
24174 macinfo_section_label);
24176 if (dwarf_split_debug_info && addr_index_table != NULL)
24178 /* optimize_location_lists calculates the size of the lists,
24179 so index them first, and assign indices to the entries.
24180 Although optimize_location_lists will remove entries from
24181 the table, it only does so for duplicates, and therefore
24182 only reduces ref_counts to 1. */
24183 unsigned int index = 0;
24184 index_location_lists (comp_unit_die ());
24185 htab_traverse_noresize (addr_index_table,
24186 index_addr_table_entry, &index);
24188 if (have_location_lists)
24189 optimize_location_lists (comp_unit_die ());
24191 save_macinfo_strings ();
24193 if (dwarf_split_debug_info)
24195 unsigned int index = 0;
24197 /* Add attributes common to skeleton compile_units and
24198 type_units. Because these attributes include strings, it
24199 must be done before freezing the string table. Top-level
24200 skeleton die attrs are added when the skeleton type unit is
24201 created, so ensure it is created by this point. */
24202 add_top_level_skeleton_die_attrs (main_comp_unit_die);
24203 (void) get_skeleton_type_unit ();
24204 htab_traverse_noresize (debug_str_hash, index_string, &index);
24207 /* Output all of the compilation units. We put the main one last so that
24208 the offsets are available to output_pubnames. */
24209 for (node = limbo_die_list; node; node = node->next)
24210 output_comp_unit (node->die, 0);
24212 comdat_type_table.create (100);
24213 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24215 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
24217 /* Don't output duplicate types. */
24218 if (*slot != HTAB_EMPTY_ENTRY)
24219 continue;
24221 /* Add a pointer to the line table for the main compilation unit
24222 so that the debugger can make sense of DW_AT_decl_file
24223 attributes. */
24224 if (debug_info_level >= DINFO_LEVEL_TERSE)
24225 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
24226 (!dwarf_split_debug_info
24227 ? debug_line_section_label
24228 : debug_skeleton_line_section_label));
24230 output_comdat_type_unit (ctnode);
24231 *slot = ctnode;
24233 comdat_type_table.dispose ();
24235 /* The AT_pubnames attribute needs to go in all skeleton dies, including
24236 both the main_cu and all skeleton TUs. Making this call unconditional
24237 would end up either adding a second copy of the AT_pubnames attribute, or
24238 requiring a special case in add_top_level_skeleton_die_attrs. */
24239 if (!dwarf_split_debug_info)
24240 add_AT_pubnames (comp_unit_die ());
24242 if (dwarf_split_debug_info)
24244 int mark;
24245 unsigned char checksum[16];
24246 struct md5_ctx ctx;
24248 /* Compute a checksum of the comp_unit to use as the dwo_id. */
24249 md5_init_ctx (&ctx);
24250 mark = 0;
24251 die_checksum (comp_unit_die (), &ctx, &mark);
24252 unmark_all_dies (comp_unit_die ());
24253 md5_finish_ctx (&ctx, checksum);
24255 /* Use the first 8 bytes of the checksum as the dwo_id,
24256 and add it to both comp-unit DIEs. */
24257 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
24258 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
24260 /* Add the base offset of the ranges table to the skeleton
24261 comp-unit DIE. */
24262 if (ranges_table_in_use)
24263 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
24264 ranges_section_label);
24266 switch_to_section (debug_addr_section);
24267 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
24268 output_addr_table ();
24271 /* Output the main compilation unit if non-empty or if .debug_macinfo
24272 or .debug_macro will be emitted. */
24273 output_comp_unit (comp_unit_die (), have_macinfo);
24275 if (dwarf_split_debug_info && info_section_emitted)
24276 output_skeleton_debug_sections (main_comp_unit_die);
24278 /* Output the abbreviation table. */
24279 if (abbrev_die_table_in_use != 1)
24281 switch_to_section (debug_abbrev_section);
24282 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
24283 output_abbrev_section ();
24286 /* Output location list section if necessary. */
24287 if (have_location_lists)
24289 /* Output the location lists info. */
24290 switch_to_section (debug_loc_section);
24291 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
24292 output_location_lists (comp_unit_die ());
24295 output_pubtables ();
24297 /* Output the address range information if a CU (.debug_info section)
24298 was emitted. We output an empty table even if we had no functions
24299 to put in it. This because the consumer has no way to tell the
24300 difference between an empty table that we omitted and failure to
24301 generate a table that would have contained data. */
24302 if (info_section_emitted)
24304 unsigned long aranges_length = size_of_aranges ();
24306 switch_to_section (debug_aranges_section);
24307 output_aranges (aranges_length);
24310 /* Output ranges section if necessary. */
24311 if (ranges_table_in_use)
24313 switch_to_section (debug_ranges_section);
24314 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
24315 output_ranges ();
24318 /* Have to end the macro section. */
24319 if (have_macinfo)
24321 switch_to_section (debug_macinfo_section);
24322 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
24323 output_macinfo ();
24324 dw2_asm_output_data (1, 0, "End compilation unit");
24327 /* Output the source line correspondence table. We must do this
24328 even if there is no line information. Otherwise, on an empty
24329 translation unit, we will generate a present, but empty,
24330 .debug_info section. IRIX 6.5 `nm' will then complain when
24331 examining the file. This is done late so that any filenames
24332 used by the debug_info section are marked as 'used'. */
24333 switch_to_section (debug_line_section);
24334 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
24335 if (! DWARF2_ASM_LINE_DEBUG_INFO)
24336 output_line_info (false);
24338 if (dwarf_split_debug_info && info_section_emitted)
24340 switch_to_section (debug_skeleton_line_section);
24341 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
24342 output_line_info (true);
24345 /* If we emitted any indirect strings, output the string table too. */
24346 if (debug_str_hash || skeleton_debug_str_hash)
24347 output_indirect_strings ();
24350 #include "gt-dwarf2out.h"