* varasm.c (bss_initializer_p): Remove static.
[official-gcc.git] / gcc / dwarf2out.c
bloba86525094e85fff55bd873fde9a6efc294afd195
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992, 1993, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
4 Free Software Foundation, Inc.
5 Contributed by Gary Funck (gary@intrepid.com).
6 Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
7 Extensively modified by Jason Merrill (jason@cygnus.com).
9 This file is part of GCC.
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
14 version.
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 for more details.
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3. If not see
23 <http://www.gnu.org/licenses/>. */
25 /* TODO: Emit .debug_line header even when there are no functions, since
26 the file numbers are used by .debug_info. Alternately, leave
27 out locations for types and decls.
28 Avoid talking about ctors and op= for PODs.
29 Factor out common prologue sequences into multiple CIEs. */
31 /* The first part of this file deals with the DWARF 2 frame unwind
32 information, which is also used by the GCC efficient exception handling
33 mechanism. The second part, controlled only by an #ifdef
34 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
35 information. */
37 /* DWARF2 Abbreviation Glossary:
39 CFA = Canonical Frame Address
40 a fixed address on the stack which identifies a call frame.
41 We define it to be the value of SP just before the call insn.
42 The CFA register and offset, which may change during the course
43 of the function, are used to calculate its value at runtime.
45 CFI = Call Frame Instruction
46 an instruction for the DWARF2 abstract machine
48 CIE = Common Information Entry
49 information describing information common to one or more FDEs
51 DIE = Debugging Information Entry
53 FDE = Frame Description Entry
54 information describing the stack call frame, in particular,
55 how to restore registers
57 DW_CFA_... = DWARF2 CFA call frame instruction
58 DW_TAG_... = DWARF2 DIE tag */
60 #include "config.h"
61 #include "system.h"
62 #include "coretypes.h"
63 #include "tm.h"
64 #include "tree.h"
65 #include "version.h"
66 #include "flags.h"
67 #include "rtl.h"
68 #include "hard-reg-set.h"
69 #include "regs.h"
70 #include "insn-config.h"
71 #include "reload.h"
72 #include "function.h"
73 #include "output.h"
74 #include "expr.h"
75 #include "except.h"
76 #include "dwarf2.h"
77 #include "dwarf2out.h"
78 #include "dwarf2asm.h"
79 #include "toplev.h"
80 #include "ggc.h"
81 #include "md5.h"
82 #include "tm_p.h"
83 #include "diagnostic.h"
84 #include "tree-pretty-print.h"
85 #include "debug.h"
86 #include "target.h"
87 #include "common/common-target.h"
88 #include "langhooks.h"
89 #include "hashtab.h"
90 #include "cgraph.h"
91 #include "input.h"
92 #include "gimple.h"
93 #include "ira.h"
94 #include "lra.h"
95 #include "dumpfile.h"
96 #include "opts.h"
98 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
99 static rtx last_var_location_insn;
100 static rtx cached_next_real_insn;
102 #ifdef VMS_DEBUGGING_INFO
103 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
105 /* Define this macro to be a nonzero value if the directory specifications
106 which are output in the debug info should end with a separator. */
107 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
108 /* Define this macro to evaluate to a nonzero value if GCC should refrain
109 from generating indirect strings in DWARF2 debug information, for instance
110 if your target is stuck with an old version of GDB that is unable to
111 process them properly or uses VMS Debug. */
112 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
113 #else
114 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
115 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
116 #endif
118 /* ??? Poison these here until it can be done generically. They've been
119 totally replaced in this file; make sure it stays that way. */
120 #undef DWARF2_UNWIND_INFO
121 #undef DWARF2_FRAME_INFO
122 #if (GCC_VERSION >= 3000)
123 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
124 #endif
126 /* The size of the target's pointer type. */
127 #ifndef PTR_SIZE
128 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
129 #endif
131 /* Array of RTXes referenced by the debugging information, which therefore
132 must be kept around forever. */
133 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
135 /* A pointer to the base of a list of incomplete types which might be
136 completed at some later time. incomplete_types_list needs to be a
137 vec<tree, va_gc> *because we want to tell the garbage collector about
138 it. */
139 static GTY(()) vec<tree, va_gc> *incomplete_types;
141 /* A pointer to the base of a table of references to declaration
142 scopes. This table is a display which tracks the nesting
143 of declaration scopes at the current scope and containing
144 scopes. This table is used to find the proper place to
145 define type declaration DIE's. */
146 static GTY(()) vec<tree, va_gc> *decl_scope_table;
148 /* Pointers to various DWARF2 sections. */
149 static GTY(()) section *debug_info_section;
150 static GTY(()) section *debug_skeleton_info_section;
151 static GTY(()) section *debug_abbrev_section;
152 static GTY(()) section *debug_skeleton_abbrev_section;
153 static GTY(()) section *debug_aranges_section;
154 static GTY(()) section *debug_addr_section;
155 static GTY(()) section *debug_macinfo_section;
156 static GTY(()) section *debug_line_section;
157 static GTY(()) section *debug_skeleton_line_section;
158 static GTY(()) section *debug_loc_section;
159 static GTY(()) section *debug_pubnames_section;
160 static GTY(()) section *debug_pubtypes_section;
161 static GTY(()) section *debug_str_section;
162 static GTY(()) section *debug_str_offsets_section;
163 static GTY(()) section *debug_ranges_section;
164 static GTY(()) section *debug_frame_section;
166 /* Maximum size (in bytes) of an artificially generated label. */
167 #define MAX_ARTIFICIAL_LABEL_BYTES 30
169 /* According to the (draft) DWARF 3 specification, the initial length
170 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
171 bytes are 0xffffffff, followed by the length stored in the next 8
172 bytes.
174 However, the SGI/MIPS ABI uses an initial length which is equal to
175 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
177 #ifndef DWARF_INITIAL_LENGTH_SIZE
178 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
179 #endif
181 /* Round SIZE up to the nearest BOUNDARY. */
182 #define DWARF_ROUND(SIZE,BOUNDARY) \
183 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
185 /* CIE identifier. */
186 #if HOST_BITS_PER_WIDE_INT >= 64
187 #define DWARF_CIE_ID \
188 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
189 #else
190 #define DWARF_CIE_ID DW_CIE_ID
191 #endif
194 /* A vector for a table that contains frame description
195 information for each routine. */
196 #define NOT_INDEXED (-1U)
197 #define NO_INDEX_ASSIGNED (-2U)
199 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
201 struct GTY(()) indirect_string_node {
202 const char *str;
203 unsigned int refcount;
204 enum dwarf_form form;
205 char *label;
206 unsigned int index;
209 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
211 static GTY(()) int dw2_string_counter;
213 /* True if the compilation unit places functions in more than one section. */
214 static GTY(()) bool have_multiple_function_sections = false;
216 /* Whether the default text and cold text sections have been used at all. */
218 static GTY(()) bool text_section_used = false;
219 static GTY(()) bool cold_text_section_used = false;
221 /* The default cold text section. */
222 static GTY(()) section *cold_text_section;
224 /* Forward declarations for functions defined in this file. */
226 static char *stripattributes (const char *);
227 static void output_call_frame_info (int);
228 static void dwarf2out_note_section_used (void);
230 /* Personality decl of current unit. Used only when assembler does not support
231 personality CFI. */
232 static GTY(()) rtx current_unit_personality;
234 /* Data and reference forms for relocatable data. */
235 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
236 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
238 #ifndef DEBUG_FRAME_SECTION
239 #define DEBUG_FRAME_SECTION ".debug_frame"
240 #endif
242 #ifndef FUNC_BEGIN_LABEL
243 #define FUNC_BEGIN_LABEL "LFB"
244 #endif
246 #ifndef FUNC_END_LABEL
247 #define FUNC_END_LABEL "LFE"
248 #endif
250 #ifndef PROLOGUE_END_LABEL
251 #define PROLOGUE_END_LABEL "LPE"
252 #endif
254 #ifndef EPILOGUE_BEGIN_LABEL
255 #define EPILOGUE_BEGIN_LABEL "LEB"
256 #endif
258 #ifndef FRAME_BEGIN_LABEL
259 #define FRAME_BEGIN_LABEL "Lframe"
260 #endif
261 #define CIE_AFTER_SIZE_LABEL "LSCIE"
262 #define CIE_END_LABEL "LECIE"
263 #define FDE_LABEL "LSFDE"
264 #define FDE_AFTER_SIZE_LABEL "LASFDE"
265 #define FDE_END_LABEL "LEFDE"
266 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
267 #define LINE_NUMBER_END_LABEL "LELT"
268 #define LN_PROLOG_AS_LABEL "LASLTP"
269 #define LN_PROLOG_END_LABEL "LELTP"
270 #define DIE_LABEL_PREFIX "DW"
272 /* Match the base name of a file to the base name of a compilation unit. */
274 static int
275 matches_main_base (const char *path)
277 /* Cache the last query. */
278 static const char *last_path = NULL;
279 static int last_match = 0;
280 if (path != last_path)
282 const char *base;
283 int length = base_of_path (path, &base);
284 last_path = path;
285 last_match = (length == main_input_baselength
286 && memcmp (base, main_input_basename, length) == 0);
288 return last_match;
291 #ifdef DEBUG_DEBUG_STRUCT
293 static int
294 dump_struct_debug (tree type, enum debug_info_usage usage,
295 enum debug_struct_file criterion, int generic,
296 int matches, int result)
298 /* Find the type name. */
299 tree type_decl = TYPE_STUB_DECL (type);
300 tree t = type_decl;
301 const char *name = 0;
302 if (TREE_CODE (t) == TYPE_DECL)
303 t = DECL_NAME (t);
304 if (t)
305 name = IDENTIFIER_POINTER (t);
307 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
308 criterion,
309 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
310 matches ? "bas" : "hdr",
311 generic ? "gen" : "ord",
312 usage == DINFO_USAGE_DFN ? ";" :
313 usage == DINFO_USAGE_DIR_USE ? "." : "*",
314 result,
315 (void*) type_decl, name);
316 return result;
318 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
319 dump_struct_debug (type, usage, criterion, generic, matches, result)
321 #else
323 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
324 (result)
326 #endif
328 static bool
329 should_emit_struct_debug (tree type, enum debug_info_usage usage)
331 enum debug_struct_file criterion;
332 tree type_decl;
333 bool generic = lang_hooks.types.generic_p (type);
335 if (generic)
336 criterion = debug_struct_generic[usage];
337 else
338 criterion = debug_struct_ordinary[usage];
340 if (criterion == DINFO_STRUCT_FILE_NONE)
341 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
342 if (criterion == DINFO_STRUCT_FILE_ANY)
343 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
345 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
347 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
348 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
350 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
351 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
352 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
355 /* Return a pointer to a copy of the section string name S with all
356 attributes stripped off, and an asterisk prepended (for assemble_name). */
358 static inline char *
359 stripattributes (const char *s)
361 char *stripped = XNEWVEC (char, strlen (s) + 2);
362 char *p = stripped;
364 *p++ = '*';
366 while (*s && *s != ',')
367 *p++ = *s++;
369 *p = '\0';
370 return stripped;
373 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
374 switch to the data section instead, and write out a synthetic start label
375 for collect2 the first time around. */
377 static void
378 switch_to_eh_frame_section (bool back)
380 tree label;
382 #ifdef EH_FRAME_SECTION_NAME
383 if (eh_frame_section == 0)
385 int flags;
387 if (EH_TABLES_CAN_BE_READ_ONLY)
389 int fde_encoding;
390 int per_encoding;
391 int lsda_encoding;
393 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
394 /*global=*/0);
395 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
396 /*global=*/1);
397 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
398 /*global=*/0);
399 flags = ((! flag_pic
400 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
401 && (fde_encoding & 0x70) != DW_EH_PE_aligned
402 && (per_encoding & 0x70) != DW_EH_PE_absptr
403 && (per_encoding & 0x70) != DW_EH_PE_aligned
404 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
405 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
406 ? 0 : SECTION_WRITE);
408 else
409 flags = SECTION_WRITE;
410 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
412 #endif /* EH_FRAME_SECTION_NAME */
414 if (eh_frame_section)
415 switch_to_section (eh_frame_section);
416 else
418 /* We have no special eh_frame section. Put the information in
419 the data section and emit special labels to guide collect2. */
420 switch_to_section (data_section);
422 if (!back)
424 label = get_file_function_name ("F");
425 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
426 targetm.asm_out.globalize_label (asm_out_file,
427 IDENTIFIER_POINTER (label));
428 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
433 /* Switch [BACK] to the eh or debug frame table section, depending on
434 FOR_EH. */
436 static void
437 switch_to_frame_table_section (int for_eh, bool back)
439 if (for_eh)
440 switch_to_eh_frame_section (back);
441 else
443 if (!debug_frame_section)
444 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
445 SECTION_DEBUG, NULL);
446 switch_to_section (debug_frame_section);
450 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
452 enum dw_cfi_oprnd_type
453 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
455 switch (cfi)
457 case DW_CFA_nop:
458 case DW_CFA_GNU_window_save:
459 case DW_CFA_remember_state:
460 case DW_CFA_restore_state:
461 return dw_cfi_oprnd_unused;
463 case DW_CFA_set_loc:
464 case DW_CFA_advance_loc1:
465 case DW_CFA_advance_loc2:
466 case DW_CFA_advance_loc4:
467 case DW_CFA_MIPS_advance_loc8:
468 return dw_cfi_oprnd_addr;
470 case DW_CFA_offset:
471 case DW_CFA_offset_extended:
472 case DW_CFA_def_cfa:
473 case DW_CFA_offset_extended_sf:
474 case DW_CFA_def_cfa_sf:
475 case DW_CFA_restore:
476 case DW_CFA_restore_extended:
477 case DW_CFA_undefined:
478 case DW_CFA_same_value:
479 case DW_CFA_def_cfa_register:
480 case DW_CFA_register:
481 case DW_CFA_expression:
482 return dw_cfi_oprnd_reg_num;
484 case DW_CFA_def_cfa_offset:
485 case DW_CFA_GNU_args_size:
486 case DW_CFA_def_cfa_offset_sf:
487 return dw_cfi_oprnd_offset;
489 case DW_CFA_def_cfa_expression:
490 return dw_cfi_oprnd_loc;
492 default:
493 gcc_unreachable ();
497 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
499 enum dw_cfi_oprnd_type
500 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
502 switch (cfi)
504 case DW_CFA_def_cfa:
505 case DW_CFA_def_cfa_sf:
506 case DW_CFA_offset:
507 case DW_CFA_offset_extended_sf:
508 case DW_CFA_offset_extended:
509 return dw_cfi_oprnd_offset;
511 case DW_CFA_register:
512 return dw_cfi_oprnd_reg_num;
514 case DW_CFA_expression:
515 return dw_cfi_oprnd_loc;
517 default:
518 return dw_cfi_oprnd_unused;
522 /* Output one FDE. */
524 static void
525 output_fde (dw_fde_ref fde, bool for_eh, bool second,
526 char *section_start_label, int fde_encoding, char *augmentation,
527 bool any_lsda_needed, int lsda_encoding)
529 const char *begin, *end;
530 static unsigned int j;
531 char l1[20], l2[20];
533 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
534 /* empty */ 0);
535 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
536 for_eh + j);
537 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
538 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
539 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
540 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
541 " indicating 64-bit DWARF extension");
542 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
543 "FDE Length");
544 ASM_OUTPUT_LABEL (asm_out_file, l1);
546 if (for_eh)
547 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
548 else
549 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
550 debug_frame_section, "FDE CIE offset");
552 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
553 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
555 if (for_eh)
557 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
558 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
559 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
560 "FDE initial location");
561 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
562 end, begin, "FDE address range");
564 else
566 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
567 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
570 if (augmentation[0])
572 if (any_lsda_needed)
574 int size = size_of_encoded_value (lsda_encoding);
576 if (lsda_encoding == DW_EH_PE_aligned)
578 int offset = ( 4 /* Length */
579 + 4 /* CIE offset */
580 + 2 * size_of_encoded_value (fde_encoding)
581 + 1 /* Augmentation size */ );
582 int pad = -offset & (PTR_SIZE - 1);
584 size += pad;
585 gcc_assert (size_of_uleb128 (size) == 1);
588 dw2_asm_output_data_uleb128 (size, "Augmentation size");
590 if (fde->uses_eh_lsda)
592 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
593 fde->funcdef_number);
594 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
595 gen_rtx_SYMBOL_REF (Pmode, l1),
596 false,
597 "Language Specific Data Area");
599 else
601 if (lsda_encoding == DW_EH_PE_aligned)
602 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
603 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
604 "Language Specific Data Area (none)");
607 else
608 dw2_asm_output_data_uleb128 (0, "Augmentation size");
611 /* Loop through the Call Frame Instructions associated with this FDE. */
612 fde->dw_fde_current_label = begin;
614 size_t from, until, i;
616 from = 0;
617 until = vec_safe_length (fde->dw_fde_cfi);
619 if (fde->dw_fde_second_begin == NULL)
621 else if (!second)
622 until = fde->dw_fde_switch_cfi_index;
623 else
624 from = fde->dw_fde_switch_cfi_index;
626 for (i = from; i < until; i++)
627 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
630 /* If we are to emit a ref/link from function bodies to their frame tables,
631 do it now. This is typically performed to make sure that tables
632 associated with functions are dragged with them and not discarded in
633 garbage collecting links. We need to do this on a per function basis to
634 cope with -ffunction-sections. */
636 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
637 /* Switch to the function section, emit the ref to the tables, and
638 switch *back* into the table section. */
639 switch_to_section (function_section (fde->decl));
640 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
641 switch_to_frame_table_section (for_eh, true);
642 #endif
644 /* Pad the FDE out to an address sized boundary. */
645 ASM_OUTPUT_ALIGN (asm_out_file,
646 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
647 ASM_OUTPUT_LABEL (asm_out_file, l2);
649 j += 2;
652 /* Return true if frame description entry FDE is needed for EH. */
654 static bool
655 fde_needed_for_eh_p (dw_fde_ref fde)
657 if (flag_asynchronous_unwind_tables)
658 return true;
660 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
661 return true;
663 if (fde->uses_eh_lsda)
664 return true;
666 /* If exceptions are enabled, we have collected nothrow info. */
667 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
668 return false;
670 return true;
673 /* Output the call frame information used to record information
674 that relates to calculating the frame pointer, and records the
675 location of saved registers. */
677 static void
678 output_call_frame_info (int for_eh)
680 unsigned int i;
681 dw_fde_ref fde;
682 dw_cfi_ref cfi;
683 char l1[20], l2[20], section_start_label[20];
684 bool any_lsda_needed = false;
685 char augmentation[6];
686 int augmentation_size;
687 int fde_encoding = DW_EH_PE_absptr;
688 int per_encoding = DW_EH_PE_absptr;
689 int lsda_encoding = DW_EH_PE_absptr;
690 int return_reg;
691 rtx personality = NULL;
692 int dw_cie_version;
694 /* Don't emit a CIE if there won't be any FDEs. */
695 if (!fde_vec)
696 return;
698 /* Nothing to do if the assembler's doing it all. */
699 if (dwarf2out_do_cfi_asm ())
700 return;
702 /* If we don't have any functions we'll want to unwind out of, don't emit
703 any EH unwind information. If we make FDEs linkonce, we may have to
704 emit an empty label for an FDE that wouldn't otherwise be emitted. We
705 want to avoid having an FDE kept around when the function it refers to
706 is discarded. Example where this matters: a primary function template
707 in C++ requires EH information, an explicit specialization doesn't. */
708 if (for_eh)
710 bool any_eh_needed = false;
712 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
714 if (fde->uses_eh_lsda)
715 any_eh_needed = any_lsda_needed = true;
716 else if (fde_needed_for_eh_p (fde))
717 any_eh_needed = true;
718 else if (TARGET_USES_WEAK_UNWIND_INFO)
719 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
722 if (!any_eh_needed)
723 return;
726 /* We're going to be generating comments, so turn on app. */
727 if (flag_debug_asm)
728 app_enable ();
730 /* Switch to the proper frame section, first time. */
731 switch_to_frame_table_section (for_eh, false);
733 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
734 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
736 /* Output the CIE. */
737 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
738 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
739 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
740 dw2_asm_output_data (4, 0xffffffff,
741 "Initial length escape value indicating 64-bit DWARF extension");
742 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
743 "Length of Common Information Entry");
744 ASM_OUTPUT_LABEL (asm_out_file, l1);
746 /* Now that the CIE pointer is PC-relative for EH,
747 use 0 to identify the CIE. */
748 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
749 (for_eh ? 0 : DWARF_CIE_ID),
750 "CIE Identifier Tag");
752 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
753 use CIE version 1, unless that would produce incorrect results
754 due to overflowing the return register column. */
755 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
756 dw_cie_version = 1;
757 if (return_reg >= 256 || dwarf_version > 2)
758 dw_cie_version = 3;
759 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
761 augmentation[0] = 0;
762 augmentation_size = 0;
764 personality = current_unit_personality;
765 if (for_eh)
767 char *p;
769 /* Augmentation:
770 z Indicates that a uleb128 is present to size the
771 augmentation section.
772 L Indicates the encoding (and thus presence) of
773 an LSDA pointer in the FDE augmentation.
774 R Indicates a non-default pointer encoding for
775 FDE code pointers.
776 P Indicates the presence of an encoding + language
777 personality routine in the CIE augmentation. */
779 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
780 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
781 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
783 p = augmentation + 1;
784 if (personality)
786 *p++ = 'P';
787 augmentation_size += 1 + size_of_encoded_value (per_encoding);
788 assemble_external_libcall (personality);
790 if (any_lsda_needed)
792 *p++ = 'L';
793 augmentation_size += 1;
795 if (fde_encoding != DW_EH_PE_absptr)
797 *p++ = 'R';
798 augmentation_size += 1;
800 if (p > augmentation + 1)
802 augmentation[0] = 'z';
803 *p = '\0';
806 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
807 if (personality && per_encoding == DW_EH_PE_aligned)
809 int offset = ( 4 /* Length */
810 + 4 /* CIE Id */
811 + 1 /* CIE version */
812 + strlen (augmentation) + 1 /* Augmentation */
813 + size_of_uleb128 (1) /* Code alignment */
814 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
815 + 1 /* RA column */
816 + 1 /* Augmentation size */
817 + 1 /* Personality encoding */ );
818 int pad = -offset & (PTR_SIZE - 1);
820 augmentation_size += pad;
822 /* Augmentations should be small, so there's scarce need to
823 iterate for a solution. Die if we exceed one uleb128 byte. */
824 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
828 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
829 if (dw_cie_version >= 4)
831 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
832 dw2_asm_output_data (1, 0, "CIE Segment Size");
834 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
835 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
836 "CIE Data Alignment Factor");
838 if (dw_cie_version == 1)
839 dw2_asm_output_data (1, return_reg, "CIE RA Column");
840 else
841 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
843 if (augmentation[0])
845 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
846 if (personality)
848 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
849 eh_data_format_name (per_encoding));
850 dw2_asm_output_encoded_addr_rtx (per_encoding,
851 personality,
852 true, NULL);
855 if (any_lsda_needed)
856 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
857 eh_data_format_name (lsda_encoding));
859 if (fde_encoding != DW_EH_PE_absptr)
860 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
861 eh_data_format_name (fde_encoding));
864 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
865 output_cfi (cfi, NULL, for_eh);
867 /* Pad the CIE out to an address sized boundary. */
868 ASM_OUTPUT_ALIGN (asm_out_file,
869 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
870 ASM_OUTPUT_LABEL (asm_out_file, l2);
872 /* Loop through all of the FDE's. */
873 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
875 unsigned int k;
877 /* Don't emit EH unwind info for leaf functions that don't need it. */
878 if (for_eh && !fde_needed_for_eh_p (fde))
879 continue;
881 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
882 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
883 augmentation, any_lsda_needed, lsda_encoding);
886 if (for_eh && targetm.terminate_dw2_eh_frame_info)
887 dw2_asm_output_data (4, 0, "End of Table");
889 /* Turn off app to make assembly quicker. */
890 if (flag_debug_asm)
891 app_disable ();
894 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
896 static void
897 dwarf2out_do_cfi_startproc (bool second)
899 int enc;
900 rtx ref;
901 rtx personality = get_personality_function (current_function_decl);
903 fprintf (asm_out_file, "\t.cfi_startproc\n");
905 if (personality)
907 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
908 ref = personality;
910 /* ??? The GAS support isn't entirely consistent. We have to
911 handle indirect support ourselves, but PC-relative is done
912 in the assembler. Further, the assembler can't handle any
913 of the weirder relocation types. */
914 if (enc & DW_EH_PE_indirect)
915 ref = dw2_force_const_mem (ref, true);
917 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
918 output_addr_const (asm_out_file, ref);
919 fputc ('\n', asm_out_file);
922 if (crtl->uses_eh_lsda)
924 char lab[20];
926 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
927 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
928 current_function_funcdef_no);
929 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
930 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
932 if (enc & DW_EH_PE_indirect)
933 ref = dw2_force_const_mem (ref, true);
935 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
936 output_addr_const (asm_out_file, ref);
937 fputc ('\n', asm_out_file);
941 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
942 this allocation may be done before pass_final. */
944 dw_fde_ref
945 dwarf2out_alloc_current_fde (void)
947 dw_fde_ref fde;
949 fde = ggc_alloc_cleared_dw_fde_node ();
950 fde->decl = current_function_decl;
951 fde->funcdef_number = current_function_funcdef_no;
952 fde->fde_index = vec_safe_length (fde_vec);
953 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
954 fde->uses_eh_lsda = crtl->uses_eh_lsda;
955 fde->nothrow = crtl->nothrow;
956 fde->drap_reg = INVALID_REGNUM;
957 fde->vdrap_reg = INVALID_REGNUM;
959 /* Record the FDE associated with this function. */
960 cfun->fde = fde;
961 vec_safe_push (fde_vec, fde);
963 return fde;
966 /* Output a marker (i.e. a label) for the beginning of a function, before
967 the prologue. */
969 void
970 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
971 const char *file ATTRIBUTE_UNUSED)
973 char label[MAX_ARTIFICIAL_LABEL_BYTES];
974 char * dup_label;
975 dw_fde_ref fde;
976 section *fnsec;
977 bool do_frame;
979 current_function_func_begin_label = NULL;
981 do_frame = dwarf2out_do_frame ();
983 /* ??? current_function_func_begin_label is also used by except.c for
984 call-site information. We must emit this label if it might be used. */
985 if (!do_frame
986 && (!flag_exceptions
987 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
988 return;
990 fnsec = function_section (current_function_decl);
991 switch_to_section (fnsec);
992 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
993 current_function_funcdef_no);
994 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
995 current_function_funcdef_no);
996 dup_label = xstrdup (label);
997 current_function_func_begin_label = dup_label;
999 /* We can elide the fde allocation if we're not emitting debug info. */
1000 if (!do_frame)
1001 return;
1003 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1004 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1005 would include pass_dwarf2_frame. If we've not created the FDE yet,
1006 do so now. */
1007 fde = cfun->fde;
1008 if (fde == NULL)
1009 fde = dwarf2out_alloc_current_fde ();
1011 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1012 fde->dw_fde_begin = dup_label;
1013 fde->dw_fde_current_label = dup_label;
1014 fde->in_std_section = (fnsec == text_section
1015 || (cold_text_section && fnsec == cold_text_section));
1017 /* We only want to output line number information for the genuine dwarf2
1018 prologue case, not the eh frame case. */
1019 #ifdef DWARF2_DEBUGGING_INFO
1020 if (file)
1021 dwarf2out_source_line (line, file, 0, true);
1022 #endif
1024 if (dwarf2out_do_cfi_asm ())
1025 dwarf2out_do_cfi_startproc (false);
1026 else
1028 rtx personality = get_personality_function (current_function_decl);
1029 if (!current_unit_personality)
1030 current_unit_personality = personality;
1032 /* We cannot keep a current personality per function as without CFI
1033 asm, at the point where we emit the CFI data, there is no current
1034 function anymore. */
1035 if (personality && current_unit_personality != personality)
1036 sorry ("multiple EH personalities are supported only with assemblers "
1037 "supporting .cfi_personality directive");
1041 /* Output a marker (i.e. a label) for the end of the generated code
1042 for a function prologue. This gets called *after* the prologue code has
1043 been generated. */
1045 void
1046 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1047 const char *file ATTRIBUTE_UNUSED)
1049 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1051 /* Output a label to mark the endpoint of the code generated for this
1052 function. */
1053 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1054 current_function_funcdef_no);
1055 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1056 current_function_funcdef_no);
1057 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1060 /* Output a marker (i.e. a label) for the beginning of the generated code
1061 for a function epilogue. This gets called *before* the prologue code has
1062 been generated. */
1064 void
1065 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1066 const char *file ATTRIBUTE_UNUSED)
1068 dw_fde_ref fde = cfun->fde;
1069 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1071 if (fde->dw_fde_vms_begin_epilogue)
1072 return;
1074 /* Output a label to mark the endpoint of the code generated for this
1075 function. */
1076 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1077 current_function_funcdef_no);
1078 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1079 current_function_funcdef_no);
1080 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1083 /* Output a marker (i.e. a label) for the absolute end of the generated code
1084 for a function definition. This gets called *after* the epilogue code has
1085 been generated. */
1087 void
1088 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1089 const char *file ATTRIBUTE_UNUSED)
1091 dw_fde_ref fde;
1092 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1094 last_var_location_insn = NULL_RTX;
1095 cached_next_real_insn = NULL_RTX;
1097 if (dwarf2out_do_cfi_asm ())
1098 fprintf (asm_out_file, "\t.cfi_endproc\n");
1100 /* Output a label to mark the endpoint of the code generated for this
1101 function. */
1102 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1103 current_function_funcdef_no);
1104 ASM_OUTPUT_LABEL (asm_out_file, label);
1105 fde = cfun->fde;
1106 gcc_assert (fde != NULL);
1107 if (fde->dw_fde_second_begin == NULL)
1108 fde->dw_fde_end = xstrdup (label);
1111 void
1112 dwarf2out_frame_finish (void)
1114 /* Output call frame information. */
1115 if (targetm.debug_unwind_info () == UI_DWARF2)
1116 output_call_frame_info (0);
1118 /* Output another copy for the unwinder. */
1119 if ((flag_unwind_tables || flag_exceptions)
1120 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1121 output_call_frame_info (1);
1124 /* Note that the current function section is being used for code. */
1126 static void
1127 dwarf2out_note_section_used (void)
1129 section *sec = current_function_section ();
1130 if (sec == text_section)
1131 text_section_used = true;
1132 else if (sec == cold_text_section)
1133 cold_text_section_used = true;
1136 static void var_location_switch_text_section (void);
1137 static void set_cur_line_info_table (section *);
1139 void
1140 dwarf2out_switch_text_section (void)
1142 section *sect;
1143 dw_fde_ref fde = cfun->fde;
1145 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1147 if (!in_cold_section_p)
1149 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1150 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1151 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1153 else
1155 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1156 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1157 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1159 have_multiple_function_sections = true;
1161 /* There is no need to mark used sections when not debugging. */
1162 if (cold_text_section != NULL)
1163 dwarf2out_note_section_used ();
1165 if (dwarf2out_do_cfi_asm ())
1166 fprintf (asm_out_file, "\t.cfi_endproc\n");
1168 /* Now do the real section switch. */
1169 sect = current_function_section ();
1170 switch_to_section (sect);
1172 fde->second_in_std_section
1173 = (sect == text_section
1174 || (cold_text_section && sect == cold_text_section));
1176 if (dwarf2out_do_cfi_asm ())
1177 dwarf2out_do_cfi_startproc (true);
1179 var_location_switch_text_section ();
1181 if (cold_text_section != NULL)
1182 set_cur_line_info_table (sect);
1185 /* And now, the subset of the debugging information support code necessary
1186 for emitting location expressions. */
1188 /* Data about a single source file. */
1189 struct GTY(()) dwarf_file_data {
1190 const char * filename;
1191 int emitted_number;
1194 typedef struct GTY(()) deferred_locations_struct
1196 tree variable;
1197 dw_die_ref die;
1198 } deferred_locations;
1201 static GTY(()) vec<deferred_locations, va_gc> *deferred_locations_list;
1204 /* Describe an entry into the .debug_addr section. */
1206 enum ate_kind {
1207 ate_kind_rtx,
1208 ate_kind_rtx_dtprel,
1209 ate_kind_label
1212 typedef struct GTY(()) addr_table_entry_struct {
1213 enum ate_kind kind;
1214 unsigned int refcount;
1215 unsigned int index;
1216 union addr_table_entry_struct_union
1218 rtx GTY ((tag ("0"))) rtl;
1219 char * GTY ((tag ("1"))) label;
1221 GTY ((desc ("%1.kind"))) addr;
1223 addr_table_entry;
1225 /* Location lists are ranges + location descriptions for that range,
1226 so you can track variables that are in different places over
1227 their entire life. */
1228 typedef struct GTY(()) dw_loc_list_struct {
1229 dw_loc_list_ref dw_loc_next;
1230 const char *begin; /* Label and addr_entry for start of range */
1231 addr_table_entry *begin_entry;
1232 const char *end; /* Label for end of range */
1233 char *ll_symbol; /* Label for beginning of location list.
1234 Only on head of list */
1235 const char *section; /* Section this loclist is relative to */
1236 dw_loc_descr_ref expr;
1237 hashval_t hash;
1238 /* True if all addresses in this and subsequent lists are known to be
1239 resolved. */
1240 bool resolved_addr;
1241 /* True if this list has been replaced by dw_loc_next. */
1242 bool replaced;
1243 bool emitted;
1244 /* True if the range should be emitted even if begin and end
1245 are the same. */
1246 bool force;
1247 } dw_loc_list_node;
1249 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1251 /* Convert a DWARF stack opcode into its string name. */
1253 static const char *
1254 dwarf_stack_op_name (unsigned int op)
1256 const char *name = get_DW_OP_name (op);
1258 if (name != NULL)
1259 return name;
1261 return "OP_<unknown>";
1264 /* Return a pointer to a newly allocated location description. Location
1265 descriptions are simple expression terms that can be strung
1266 together to form more complicated location (address) descriptions. */
1268 static inline dw_loc_descr_ref
1269 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1270 unsigned HOST_WIDE_INT oprnd2)
1272 dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
1274 descr->dw_loc_opc = op;
1275 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1276 descr->dw_loc_oprnd1.val_entry = NULL;
1277 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1278 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1279 descr->dw_loc_oprnd2.val_entry = NULL;
1280 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1282 return descr;
1285 /* Return a pointer to a newly allocated location description for
1286 REG and OFFSET. */
1288 static inline dw_loc_descr_ref
1289 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1291 if (reg <= 31)
1292 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1293 offset, 0);
1294 else
1295 return new_loc_descr (DW_OP_bregx, reg, offset);
1298 /* Add a location description term to a location description expression. */
1300 static inline void
1301 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1303 dw_loc_descr_ref *d;
1305 /* Find the end of the chain. */
1306 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1309 *d = descr;
1312 /* Compare two location operands for exact equality. */
1314 static bool
1315 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1317 if (a->val_class != b->val_class)
1318 return false;
1319 switch (a->val_class)
1321 case dw_val_class_none:
1322 return true;
1323 case dw_val_class_addr:
1324 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1326 case dw_val_class_offset:
1327 case dw_val_class_unsigned_const:
1328 case dw_val_class_const:
1329 case dw_val_class_range_list:
1330 case dw_val_class_lineptr:
1331 case dw_val_class_macptr:
1332 /* These are all HOST_WIDE_INT, signed or unsigned. */
1333 return a->v.val_unsigned == b->v.val_unsigned;
1335 case dw_val_class_loc:
1336 return a->v.val_loc == b->v.val_loc;
1337 case dw_val_class_loc_list:
1338 return a->v.val_loc_list == b->v.val_loc_list;
1339 case dw_val_class_die_ref:
1340 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1341 case dw_val_class_fde_ref:
1342 return a->v.val_fde_index == b->v.val_fde_index;
1343 case dw_val_class_lbl_id:
1344 case dw_val_class_high_pc:
1345 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1346 case dw_val_class_str:
1347 return a->v.val_str == b->v.val_str;
1348 case dw_val_class_flag:
1349 return a->v.val_flag == b->v.val_flag;
1350 case dw_val_class_file:
1351 return a->v.val_file == b->v.val_file;
1352 case dw_val_class_decl_ref:
1353 return a->v.val_decl_ref == b->v.val_decl_ref;
1355 case dw_val_class_const_double:
1356 return (a->v.val_double.high == b->v.val_double.high
1357 && a->v.val_double.low == b->v.val_double.low);
1359 case dw_val_class_vec:
1361 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1362 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1364 return (a_len == b_len
1365 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1368 case dw_val_class_data8:
1369 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1371 case dw_val_class_vms_delta:
1372 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1373 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1375 gcc_unreachable ();
1378 /* Compare two location atoms for exact equality. */
1380 static bool
1381 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1383 if (a->dw_loc_opc != b->dw_loc_opc)
1384 return false;
1386 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1387 address size, but since we always allocate cleared storage it
1388 should be zero for other types of locations. */
1389 if (a->dtprel != b->dtprel)
1390 return false;
1392 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1393 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1396 /* Compare two complete location expressions for exact equality. */
1398 bool
1399 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1401 while (1)
1403 if (a == b)
1404 return true;
1405 if (a == NULL || b == NULL)
1406 return false;
1407 if (!loc_descr_equal_p_1 (a, b))
1408 return false;
1410 a = a->dw_loc_next;
1411 b = b->dw_loc_next;
1416 /* Add a constant OFFSET to a location expression. */
1418 static void
1419 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1421 dw_loc_descr_ref loc;
1422 HOST_WIDE_INT *p;
1424 gcc_assert (*list_head != NULL);
1426 if (!offset)
1427 return;
1429 /* Find the end of the chain. */
1430 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1433 p = NULL;
1434 if (loc->dw_loc_opc == DW_OP_fbreg
1435 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1436 p = &loc->dw_loc_oprnd1.v.val_int;
1437 else if (loc->dw_loc_opc == DW_OP_bregx)
1438 p = &loc->dw_loc_oprnd2.v.val_int;
1440 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1441 offset. Don't optimize if an signed integer overflow would happen. */
1442 if (p != NULL
1443 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1444 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1445 *p += offset;
1447 else if (offset > 0)
1448 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1450 else
1452 loc->dw_loc_next = int_loc_descriptor (-offset);
1453 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1457 /* Add a constant OFFSET to a location list. */
1459 static void
1460 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1462 dw_loc_list_ref d;
1463 for (d = list_head; d != NULL; d = d->dw_loc_next)
1464 loc_descr_plus_const (&d->expr, offset);
1467 #define DWARF_REF_SIZE \
1468 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1470 static unsigned long int get_base_type_offset (dw_die_ref);
1472 /* Return the size of a location descriptor. */
1474 static unsigned long
1475 size_of_loc_descr (dw_loc_descr_ref loc)
1477 unsigned long size = 1;
1479 switch (loc->dw_loc_opc)
1481 case DW_OP_addr:
1482 size += DWARF2_ADDR_SIZE;
1483 break;
1484 case DW_OP_GNU_addr_index:
1485 case DW_OP_GNU_const_index:
1486 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1487 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1488 break;
1489 case DW_OP_const1u:
1490 case DW_OP_const1s:
1491 size += 1;
1492 break;
1493 case DW_OP_const2u:
1494 case DW_OP_const2s:
1495 size += 2;
1496 break;
1497 case DW_OP_const4u:
1498 case DW_OP_const4s:
1499 size += 4;
1500 break;
1501 case DW_OP_const8u:
1502 case DW_OP_const8s:
1503 size += 8;
1504 break;
1505 case DW_OP_constu:
1506 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1507 break;
1508 case DW_OP_consts:
1509 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1510 break;
1511 case DW_OP_pick:
1512 size += 1;
1513 break;
1514 case DW_OP_plus_uconst:
1515 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1516 break;
1517 case DW_OP_skip:
1518 case DW_OP_bra:
1519 size += 2;
1520 break;
1521 case DW_OP_breg0:
1522 case DW_OP_breg1:
1523 case DW_OP_breg2:
1524 case DW_OP_breg3:
1525 case DW_OP_breg4:
1526 case DW_OP_breg5:
1527 case DW_OP_breg6:
1528 case DW_OP_breg7:
1529 case DW_OP_breg8:
1530 case DW_OP_breg9:
1531 case DW_OP_breg10:
1532 case DW_OP_breg11:
1533 case DW_OP_breg12:
1534 case DW_OP_breg13:
1535 case DW_OP_breg14:
1536 case DW_OP_breg15:
1537 case DW_OP_breg16:
1538 case DW_OP_breg17:
1539 case DW_OP_breg18:
1540 case DW_OP_breg19:
1541 case DW_OP_breg20:
1542 case DW_OP_breg21:
1543 case DW_OP_breg22:
1544 case DW_OP_breg23:
1545 case DW_OP_breg24:
1546 case DW_OP_breg25:
1547 case DW_OP_breg26:
1548 case DW_OP_breg27:
1549 case DW_OP_breg28:
1550 case DW_OP_breg29:
1551 case DW_OP_breg30:
1552 case DW_OP_breg31:
1553 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1554 break;
1555 case DW_OP_regx:
1556 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1557 break;
1558 case DW_OP_fbreg:
1559 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1560 break;
1561 case DW_OP_bregx:
1562 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1563 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1564 break;
1565 case DW_OP_piece:
1566 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1567 break;
1568 case DW_OP_bit_piece:
1569 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1570 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1571 break;
1572 case DW_OP_deref_size:
1573 case DW_OP_xderef_size:
1574 size += 1;
1575 break;
1576 case DW_OP_call2:
1577 size += 2;
1578 break;
1579 case DW_OP_call4:
1580 size += 4;
1581 break;
1582 case DW_OP_call_ref:
1583 size += DWARF_REF_SIZE;
1584 break;
1585 case DW_OP_implicit_value:
1586 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1587 + loc->dw_loc_oprnd1.v.val_unsigned;
1588 break;
1589 case DW_OP_GNU_implicit_pointer:
1590 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1591 break;
1592 case DW_OP_GNU_entry_value:
1594 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1595 size += size_of_uleb128 (op_size) + op_size;
1596 break;
1598 case DW_OP_GNU_const_type:
1600 unsigned long o
1601 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1602 size += size_of_uleb128 (o) + 1;
1603 switch (loc->dw_loc_oprnd2.val_class)
1605 case dw_val_class_vec:
1606 size += loc->dw_loc_oprnd2.v.val_vec.length
1607 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1608 break;
1609 case dw_val_class_const:
1610 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1611 break;
1612 case dw_val_class_const_double:
1613 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1614 break;
1615 default:
1616 gcc_unreachable ();
1618 break;
1620 case DW_OP_GNU_regval_type:
1622 unsigned long o
1623 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1624 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1625 + size_of_uleb128 (o);
1627 break;
1628 case DW_OP_GNU_deref_type:
1630 unsigned long o
1631 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1632 size += 1 + size_of_uleb128 (o);
1634 break;
1635 case DW_OP_GNU_convert:
1636 case DW_OP_GNU_reinterpret:
1637 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1638 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1639 else
1641 unsigned long o
1642 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1643 size += size_of_uleb128 (o);
1645 break;
1646 case DW_OP_GNU_parameter_ref:
1647 size += 4;
1648 break;
1649 default:
1650 break;
1653 return size;
1656 /* Return the size of a series of location descriptors. */
1658 unsigned long
1659 size_of_locs (dw_loc_descr_ref loc)
1661 dw_loc_descr_ref l;
1662 unsigned long size;
1664 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1665 field, to avoid writing to a PCH file. */
1666 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1668 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1669 break;
1670 size += size_of_loc_descr (l);
1672 if (! l)
1673 return size;
1675 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1677 l->dw_loc_addr = size;
1678 size += size_of_loc_descr (l);
1681 return size;
1684 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1685 static void get_ref_die_offset_label (char *, dw_die_ref);
1686 static unsigned long int get_ref_die_offset (dw_die_ref);
1688 /* Output location description stack opcode's operands (if any).
1689 The for_eh_or_skip parameter controls whether register numbers are
1690 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1691 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1692 info). This should be suppressed for the cases that have not been converted
1693 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1695 static void
1696 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1698 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1699 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1701 switch (loc->dw_loc_opc)
1703 #ifdef DWARF2_DEBUGGING_INFO
1704 case DW_OP_const2u:
1705 case DW_OP_const2s:
1706 dw2_asm_output_data (2, val1->v.val_int, NULL);
1707 break;
1708 case DW_OP_const4u:
1709 if (loc->dtprel)
1711 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1712 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1713 val1->v.val_addr);
1714 fputc ('\n', asm_out_file);
1715 break;
1717 /* FALLTHRU */
1718 case DW_OP_const4s:
1719 dw2_asm_output_data (4, val1->v.val_int, NULL);
1720 break;
1721 case DW_OP_const8u:
1722 if (loc->dtprel)
1724 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1725 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1726 val1->v.val_addr);
1727 fputc ('\n', asm_out_file);
1728 break;
1730 /* FALLTHRU */
1731 case DW_OP_const8s:
1732 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1733 dw2_asm_output_data (8, val1->v.val_int, NULL);
1734 break;
1735 case DW_OP_skip:
1736 case DW_OP_bra:
1738 int offset;
1740 gcc_assert (val1->val_class == dw_val_class_loc);
1741 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1743 dw2_asm_output_data (2, offset, NULL);
1745 break;
1746 case DW_OP_implicit_value:
1747 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1748 switch (val2->val_class)
1750 case dw_val_class_const:
1751 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1752 break;
1753 case dw_val_class_vec:
1755 unsigned int elt_size = val2->v.val_vec.elt_size;
1756 unsigned int len = val2->v.val_vec.length;
1757 unsigned int i;
1758 unsigned char *p;
1760 if (elt_size > sizeof (HOST_WIDE_INT))
1762 elt_size /= 2;
1763 len *= 2;
1765 for (i = 0, p = val2->v.val_vec.array;
1766 i < len;
1767 i++, p += elt_size)
1768 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1769 "fp or vector constant word %u", i);
1771 break;
1772 case dw_val_class_const_double:
1774 unsigned HOST_WIDE_INT first, second;
1776 if (WORDS_BIG_ENDIAN)
1778 first = val2->v.val_double.high;
1779 second = val2->v.val_double.low;
1781 else
1783 first = val2->v.val_double.low;
1784 second = val2->v.val_double.high;
1786 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1787 first, NULL);
1788 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1789 second, NULL);
1791 break;
1792 case dw_val_class_addr:
1793 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1794 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1795 break;
1796 default:
1797 gcc_unreachable ();
1799 break;
1800 #else
1801 case DW_OP_const2u:
1802 case DW_OP_const2s:
1803 case DW_OP_const4u:
1804 case DW_OP_const4s:
1805 case DW_OP_const8u:
1806 case DW_OP_const8s:
1807 case DW_OP_skip:
1808 case DW_OP_bra:
1809 case DW_OP_implicit_value:
1810 /* We currently don't make any attempt to make sure these are
1811 aligned properly like we do for the main unwind info, so
1812 don't support emitting things larger than a byte if we're
1813 only doing unwinding. */
1814 gcc_unreachable ();
1815 #endif
1816 case DW_OP_const1u:
1817 case DW_OP_const1s:
1818 dw2_asm_output_data (1, val1->v.val_int, NULL);
1819 break;
1820 case DW_OP_constu:
1821 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1822 break;
1823 case DW_OP_consts:
1824 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1825 break;
1826 case DW_OP_pick:
1827 dw2_asm_output_data (1, val1->v.val_int, NULL);
1828 break;
1829 case DW_OP_plus_uconst:
1830 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1831 break;
1832 case DW_OP_breg0:
1833 case DW_OP_breg1:
1834 case DW_OP_breg2:
1835 case DW_OP_breg3:
1836 case DW_OP_breg4:
1837 case DW_OP_breg5:
1838 case DW_OP_breg6:
1839 case DW_OP_breg7:
1840 case DW_OP_breg8:
1841 case DW_OP_breg9:
1842 case DW_OP_breg10:
1843 case DW_OP_breg11:
1844 case DW_OP_breg12:
1845 case DW_OP_breg13:
1846 case DW_OP_breg14:
1847 case DW_OP_breg15:
1848 case DW_OP_breg16:
1849 case DW_OP_breg17:
1850 case DW_OP_breg18:
1851 case DW_OP_breg19:
1852 case DW_OP_breg20:
1853 case DW_OP_breg21:
1854 case DW_OP_breg22:
1855 case DW_OP_breg23:
1856 case DW_OP_breg24:
1857 case DW_OP_breg25:
1858 case DW_OP_breg26:
1859 case DW_OP_breg27:
1860 case DW_OP_breg28:
1861 case DW_OP_breg29:
1862 case DW_OP_breg30:
1863 case DW_OP_breg31:
1864 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1865 break;
1866 case DW_OP_regx:
1868 unsigned r = val1->v.val_unsigned;
1869 if (for_eh_or_skip >= 0)
1870 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1871 gcc_assert (size_of_uleb128 (r)
1872 == size_of_uleb128 (val1->v.val_unsigned));
1873 dw2_asm_output_data_uleb128 (r, NULL);
1875 break;
1876 case DW_OP_fbreg:
1877 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1878 break;
1879 case DW_OP_bregx:
1881 unsigned r = val1->v.val_unsigned;
1882 if (for_eh_or_skip >= 0)
1883 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1884 gcc_assert (size_of_uleb128 (r)
1885 == size_of_uleb128 (val1->v.val_unsigned));
1886 dw2_asm_output_data_uleb128 (r, NULL);
1887 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1889 break;
1890 case DW_OP_piece:
1891 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1892 break;
1893 case DW_OP_bit_piece:
1894 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1895 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1896 break;
1897 case DW_OP_deref_size:
1898 case DW_OP_xderef_size:
1899 dw2_asm_output_data (1, val1->v.val_int, NULL);
1900 break;
1902 case DW_OP_addr:
1903 if (loc->dtprel)
1905 if (targetm.asm_out.output_dwarf_dtprel)
1907 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
1908 DWARF2_ADDR_SIZE,
1909 val1->v.val_addr);
1910 fputc ('\n', asm_out_file);
1912 else
1913 gcc_unreachable ();
1915 else
1917 #ifdef DWARF2_DEBUGGING_INFO
1918 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
1919 #else
1920 gcc_unreachable ();
1921 #endif
1923 break;
1925 case DW_OP_GNU_addr_index:
1926 case DW_OP_GNU_const_index:
1927 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1928 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
1929 "(index into .debug_addr)");
1930 break;
1932 case DW_OP_GNU_implicit_pointer:
1934 char label[MAX_ARTIFICIAL_LABEL_BYTES
1935 + HOST_BITS_PER_WIDE_INT / 2 + 2];
1936 gcc_assert (val1->val_class == dw_val_class_die_ref);
1937 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
1938 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
1939 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1941 break;
1943 case DW_OP_GNU_entry_value:
1944 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
1945 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
1946 break;
1948 case DW_OP_GNU_const_type:
1950 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
1951 gcc_assert (o);
1952 dw2_asm_output_data_uleb128 (o, NULL);
1953 switch (val2->val_class)
1955 case dw_val_class_const:
1956 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
1957 dw2_asm_output_data (1, l, NULL);
1958 dw2_asm_output_data (l, val2->v.val_int, NULL);
1959 break;
1960 case dw_val_class_vec:
1962 unsigned int elt_size = val2->v.val_vec.elt_size;
1963 unsigned int len = val2->v.val_vec.length;
1964 unsigned int i;
1965 unsigned char *p;
1967 l = len * elt_size;
1968 dw2_asm_output_data (1, l, NULL);
1969 if (elt_size > sizeof (HOST_WIDE_INT))
1971 elt_size /= 2;
1972 len *= 2;
1974 for (i = 0, p = val2->v.val_vec.array;
1975 i < len;
1976 i++, p += elt_size)
1977 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1978 "fp or vector constant word %u", i);
1980 break;
1981 case dw_val_class_const_double:
1983 unsigned HOST_WIDE_INT first, second;
1984 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
1986 dw2_asm_output_data (1, 2 * l, NULL);
1987 if (WORDS_BIG_ENDIAN)
1989 first = val2->v.val_double.high;
1990 second = val2->v.val_double.low;
1992 else
1994 first = val2->v.val_double.low;
1995 second = val2->v.val_double.high;
1997 dw2_asm_output_data (l, first, NULL);
1998 dw2_asm_output_data (l, second, NULL);
2000 break;
2001 default:
2002 gcc_unreachable ();
2005 break;
2006 case DW_OP_GNU_regval_type:
2008 unsigned r = val1->v.val_unsigned;
2009 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2010 gcc_assert (o);
2011 if (for_eh_or_skip >= 0)
2013 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2014 gcc_assert (size_of_uleb128 (r)
2015 == size_of_uleb128 (val1->v.val_unsigned));
2017 dw2_asm_output_data_uleb128 (r, NULL);
2018 dw2_asm_output_data_uleb128 (o, NULL);
2020 break;
2021 case DW_OP_GNU_deref_type:
2023 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2024 gcc_assert (o);
2025 dw2_asm_output_data (1, val1->v.val_int, NULL);
2026 dw2_asm_output_data_uleb128 (o, NULL);
2028 break;
2029 case DW_OP_GNU_convert:
2030 case DW_OP_GNU_reinterpret:
2031 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2032 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2033 else
2035 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2036 gcc_assert (o);
2037 dw2_asm_output_data_uleb128 (o, NULL);
2039 break;
2041 case DW_OP_GNU_parameter_ref:
2043 unsigned long o;
2044 gcc_assert (val1->val_class == dw_val_class_die_ref);
2045 o = get_ref_die_offset (val1->v.val_die_ref.die);
2046 dw2_asm_output_data (4, o, NULL);
2048 break;
2050 default:
2051 /* Other codes have no operands. */
2052 break;
2056 /* Output a sequence of location operations.
2057 The for_eh_or_skip parameter controls whether register numbers are
2058 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2059 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2060 info). This should be suppressed for the cases that have not been converted
2061 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2063 void
2064 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2066 for (; loc != NULL; loc = loc->dw_loc_next)
2068 enum dwarf_location_atom opc = loc->dw_loc_opc;
2069 /* Output the opcode. */
2070 if (for_eh_or_skip >= 0
2071 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2073 unsigned r = (opc - DW_OP_breg0);
2074 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2075 gcc_assert (r <= 31);
2076 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2078 else if (for_eh_or_skip >= 0
2079 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2081 unsigned r = (opc - DW_OP_reg0);
2082 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2083 gcc_assert (r <= 31);
2084 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2087 dw2_asm_output_data (1, opc,
2088 "%s", dwarf_stack_op_name (opc));
2090 /* Output the operand(s) (if any). */
2091 output_loc_operands (loc, for_eh_or_skip);
2095 /* Output location description stack opcode's operands (if any).
2096 The output is single bytes on a line, suitable for .cfi_escape. */
2098 static void
2099 output_loc_operands_raw (dw_loc_descr_ref loc)
2101 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2102 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2104 switch (loc->dw_loc_opc)
2106 case DW_OP_addr:
2107 case DW_OP_GNU_addr_index:
2108 case DW_OP_GNU_const_index:
2109 case DW_OP_implicit_value:
2110 /* We cannot output addresses in .cfi_escape, only bytes. */
2111 gcc_unreachable ();
2113 case DW_OP_const1u:
2114 case DW_OP_const1s:
2115 case DW_OP_pick:
2116 case DW_OP_deref_size:
2117 case DW_OP_xderef_size:
2118 fputc (',', asm_out_file);
2119 dw2_asm_output_data_raw (1, val1->v.val_int);
2120 break;
2122 case DW_OP_const2u:
2123 case DW_OP_const2s:
2124 fputc (',', asm_out_file);
2125 dw2_asm_output_data_raw (2, val1->v.val_int);
2126 break;
2128 case DW_OP_const4u:
2129 case DW_OP_const4s:
2130 fputc (',', asm_out_file);
2131 dw2_asm_output_data_raw (4, val1->v.val_int);
2132 break;
2134 case DW_OP_const8u:
2135 case DW_OP_const8s:
2136 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2137 fputc (',', asm_out_file);
2138 dw2_asm_output_data_raw (8, val1->v.val_int);
2139 break;
2141 case DW_OP_skip:
2142 case DW_OP_bra:
2144 int offset;
2146 gcc_assert (val1->val_class == dw_val_class_loc);
2147 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2149 fputc (',', asm_out_file);
2150 dw2_asm_output_data_raw (2, offset);
2152 break;
2154 case DW_OP_regx:
2156 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2157 gcc_assert (size_of_uleb128 (r)
2158 == size_of_uleb128 (val1->v.val_unsigned));
2159 fputc (',', asm_out_file);
2160 dw2_asm_output_data_uleb128_raw (r);
2162 break;
2164 case DW_OP_constu:
2165 case DW_OP_plus_uconst:
2166 case DW_OP_piece:
2167 fputc (',', asm_out_file);
2168 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2169 break;
2171 case DW_OP_bit_piece:
2172 fputc (',', asm_out_file);
2173 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2174 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2175 break;
2177 case DW_OP_consts:
2178 case DW_OP_breg0:
2179 case DW_OP_breg1:
2180 case DW_OP_breg2:
2181 case DW_OP_breg3:
2182 case DW_OP_breg4:
2183 case DW_OP_breg5:
2184 case DW_OP_breg6:
2185 case DW_OP_breg7:
2186 case DW_OP_breg8:
2187 case DW_OP_breg9:
2188 case DW_OP_breg10:
2189 case DW_OP_breg11:
2190 case DW_OP_breg12:
2191 case DW_OP_breg13:
2192 case DW_OP_breg14:
2193 case DW_OP_breg15:
2194 case DW_OP_breg16:
2195 case DW_OP_breg17:
2196 case DW_OP_breg18:
2197 case DW_OP_breg19:
2198 case DW_OP_breg20:
2199 case DW_OP_breg21:
2200 case DW_OP_breg22:
2201 case DW_OP_breg23:
2202 case DW_OP_breg24:
2203 case DW_OP_breg25:
2204 case DW_OP_breg26:
2205 case DW_OP_breg27:
2206 case DW_OP_breg28:
2207 case DW_OP_breg29:
2208 case DW_OP_breg30:
2209 case DW_OP_breg31:
2210 case DW_OP_fbreg:
2211 fputc (',', asm_out_file);
2212 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2213 break;
2215 case DW_OP_bregx:
2217 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2218 gcc_assert (size_of_uleb128 (r)
2219 == size_of_uleb128 (val1->v.val_unsigned));
2220 fputc (',', asm_out_file);
2221 dw2_asm_output_data_uleb128_raw (r);
2222 fputc (',', asm_out_file);
2223 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2225 break;
2227 case DW_OP_GNU_implicit_pointer:
2228 case DW_OP_GNU_entry_value:
2229 case DW_OP_GNU_const_type:
2230 case DW_OP_GNU_regval_type:
2231 case DW_OP_GNU_deref_type:
2232 case DW_OP_GNU_convert:
2233 case DW_OP_GNU_reinterpret:
2234 case DW_OP_GNU_parameter_ref:
2235 gcc_unreachable ();
2236 break;
2238 default:
2239 /* Other codes have no operands. */
2240 break;
2244 void
2245 output_loc_sequence_raw (dw_loc_descr_ref loc)
2247 while (1)
2249 enum dwarf_location_atom opc = loc->dw_loc_opc;
2250 /* Output the opcode. */
2251 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2253 unsigned r = (opc - DW_OP_breg0);
2254 r = DWARF2_FRAME_REG_OUT (r, 1);
2255 gcc_assert (r <= 31);
2256 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2258 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2260 unsigned r = (opc - DW_OP_reg0);
2261 r = DWARF2_FRAME_REG_OUT (r, 1);
2262 gcc_assert (r <= 31);
2263 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2265 /* Output the opcode. */
2266 fprintf (asm_out_file, "%#x", opc);
2267 output_loc_operands_raw (loc);
2269 if (!loc->dw_loc_next)
2270 break;
2271 loc = loc->dw_loc_next;
2273 fputc (',', asm_out_file);
2277 /* This function builds a dwarf location descriptor sequence from a
2278 dw_cfa_location, adding the given OFFSET to the result of the
2279 expression. */
2281 struct dw_loc_descr_struct *
2282 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2284 struct dw_loc_descr_struct *head, *tmp;
2286 offset += cfa->offset;
2288 if (cfa->indirect)
2290 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2291 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2292 head->dw_loc_oprnd1.val_entry = NULL;
2293 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2294 add_loc_descr (&head, tmp);
2295 if (offset != 0)
2297 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2298 add_loc_descr (&head, tmp);
2301 else
2302 head = new_reg_loc_descr (cfa->reg, offset);
2304 return head;
2307 /* This function builds a dwarf location descriptor sequence for
2308 the address at OFFSET from the CFA when stack is aligned to
2309 ALIGNMENT byte. */
2311 struct dw_loc_descr_struct *
2312 build_cfa_aligned_loc (dw_cfa_location *cfa,
2313 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2315 struct dw_loc_descr_struct *head;
2316 unsigned int dwarf_fp
2317 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2319 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2320 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2322 head = new_reg_loc_descr (dwarf_fp, 0);
2323 add_loc_descr (&head, int_loc_descriptor (alignment));
2324 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2325 loc_descr_plus_const (&head, offset);
2327 else
2328 head = new_reg_loc_descr (dwarf_fp, offset);
2329 return head;
2332 /* And now, the support for symbolic debugging information. */
2334 /* .debug_str support. */
2335 static int output_indirect_string (void **, void *);
2337 static void dwarf2out_init (const char *);
2338 static void dwarf2out_finish (const char *);
2339 static void dwarf2out_assembly_start (void);
2340 static void dwarf2out_define (unsigned int, const char *);
2341 static void dwarf2out_undef (unsigned int, const char *);
2342 static void dwarf2out_start_source_file (unsigned, const char *);
2343 static void dwarf2out_end_source_file (unsigned);
2344 static void dwarf2out_function_decl (tree);
2345 static void dwarf2out_begin_block (unsigned, unsigned);
2346 static void dwarf2out_end_block (unsigned, unsigned);
2347 static bool dwarf2out_ignore_block (const_tree);
2348 static void dwarf2out_global_decl (tree);
2349 static void dwarf2out_type_decl (tree, int);
2350 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2351 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2352 dw_die_ref);
2353 static void dwarf2out_abstract_function (tree);
2354 static void dwarf2out_var_location (rtx);
2355 static void dwarf2out_begin_function (tree);
2356 static void dwarf2out_set_name (tree, tree);
2358 /* The debug hooks structure. */
2360 const struct gcc_debug_hooks dwarf2_debug_hooks =
2362 dwarf2out_init,
2363 dwarf2out_finish,
2364 dwarf2out_assembly_start,
2365 dwarf2out_define,
2366 dwarf2out_undef,
2367 dwarf2out_start_source_file,
2368 dwarf2out_end_source_file,
2369 dwarf2out_begin_block,
2370 dwarf2out_end_block,
2371 dwarf2out_ignore_block,
2372 dwarf2out_source_line,
2373 dwarf2out_begin_prologue,
2374 #if VMS_DEBUGGING_INFO
2375 dwarf2out_vms_end_prologue,
2376 dwarf2out_vms_begin_epilogue,
2377 #else
2378 debug_nothing_int_charstar,
2379 debug_nothing_int_charstar,
2380 #endif
2381 dwarf2out_end_epilogue,
2382 dwarf2out_begin_function,
2383 debug_nothing_int, /* end_function */
2384 dwarf2out_function_decl, /* function_decl */
2385 dwarf2out_global_decl,
2386 dwarf2out_type_decl, /* type_decl */
2387 dwarf2out_imported_module_or_decl,
2388 debug_nothing_tree, /* deferred_inline_function */
2389 /* The DWARF 2 backend tries to reduce debugging bloat by not
2390 emitting the abstract description of inline functions until
2391 something tries to reference them. */
2392 dwarf2out_abstract_function, /* outlining_inline_function */
2393 debug_nothing_rtx, /* label */
2394 debug_nothing_int, /* handle_pch */
2395 dwarf2out_var_location,
2396 dwarf2out_switch_text_section,
2397 dwarf2out_set_name,
2398 1, /* start_end_main_source_file */
2399 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2402 /* NOTE: In the comments in this file, many references are made to
2403 "Debugging Information Entries". This term is abbreviated as `DIE'
2404 throughout the remainder of this file. */
2406 /* An internal representation of the DWARF output is built, and then
2407 walked to generate the DWARF debugging info. The walk of the internal
2408 representation is done after the entire program has been compiled.
2409 The types below are used to describe the internal representation. */
2411 /* Whether to put type DIEs into their own section .debug_types instead
2412 of making them part of the .debug_info section. Only supported for
2413 Dwarf V4 or higher and the user didn't disable them through
2414 -fno-debug-types-section. It is more efficient to put them in a
2415 separate comdat sections since the linker will then be able to
2416 remove duplicates. But not all tools support .debug_types sections
2417 yet. */
2419 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2421 /* Various DIE's use offsets relative to the beginning of the
2422 .debug_info section to refer to each other. */
2424 typedef long int dw_offset;
2426 /* Define typedefs here to avoid circular dependencies. */
2428 typedef struct dw_attr_struct *dw_attr_ref;
2429 typedef struct dw_line_info_struct *dw_line_info_ref;
2430 typedef struct pubname_struct *pubname_ref;
2431 typedef struct dw_ranges_struct *dw_ranges_ref;
2432 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
2433 typedef struct comdat_type_struct *comdat_type_node_ref;
2435 /* The entries in the line_info table more-or-less mirror the opcodes
2436 that are used in the real dwarf line table. Arrays of these entries
2437 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2438 supported. */
2440 enum dw_line_info_opcode {
2441 /* Emit DW_LNE_set_address; the operand is the label index. */
2442 LI_set_address,
2444 /* Emit a row to the matrix with the given line. This may be done
2445 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2446 special opcodes. */
2447 LI_set_line,
2449 /* Emit a DW_LNS_set_file. */
2450 LI_set_file,
2452 /* Emit a DW_LNS_set_column. */
2453 LI_set_column,
2455 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2456 LI_negate_stmt,
2458 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2459 LI_set_prologue_end,
2460 LI_set_epilogue_begin,
2462 /* Emit a DW_LNE_set_discriminator. */
2463 LI_set_discriminator
2466 typedef struct GTY(()) dw_line_info_struct {
2467 enum dw_line_info_opcode opcode;
2468 unsigned int val;
2469 } dw_line_info_entry;
2472 typedef struct GTY(()) dw_line_info_table_struct {
2473 /* The label that marks the end of this section. */
2474 const char *end_label;
2476 /* The values for the last row of the matrix, as collected in the table.
2477 These are used to minimize the changes to the next row. */
2478 unsigned int file_num;
2479 unsigned int line_num;
2480 unsigned int column_num;
2481 int discrim_num;
2482 bool is_stmt;
2483 bool in_use;
2485 vec<dw_line_info_entry, va_gc> *entries;
2486 } dw_line_info_table;
2488 typedef dw_line_info_table *dw_line_info_table_p;
2491 /* Each DIE attribute has a field specifying the attribute kind,
2492 a link to the next attribute in the chain, and an attribute value.
2493 Attributes are typically linked below the DIE they modify. */
2495 typedef struct GTY(()) dw_attr_struct {
2496 enum dwarf_attribute dw_attr;
2497 dw_val_node dw_attr_val;
2499 dw_attr_node;
2502 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2503 The children of each node form a circular list linked by
2504 die_sib. die_child points to the node *before* the "first" child node. */
2506 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
2507 union die_symbol_or_type_node
2509 const char * GTY ((tag ("0"))) die_symbol;
2510 comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
2512 GTY ((desc ("%0.comdat_type_p"))) die_id;
2513 vec<dw_attr_node, va_gc> *die_attr;
2514 dw_die_ref die_parent;
2515 dw_die_ref die_child;
2516 dw_die_ref die_sib;
2517 dw_die_ref die_definition; /* ref from a specification to its definition */
2518 dw_offset die_offset;
2519 unsigned long die_abbrev;
2520 int die_mark;
2521 unsigned int decl_id;
2522 enum dwarf_tag die_tag;
2523 /* Die is used and must not be pruned as unused. */
2524 BOOL_BITFIELD die_perennial_p : 1;
2525 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2526 /* Lots of spare bits. */
2528 die_node;
2530 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2531 #define FOR_EACH_CHILD(die, c, expr) do { \
2532 c = die->die_child; \
2533 if (c) do { \
2534 c = c->die_sib; \
2535 expr; \
2536 } while (c != die->die_child); \
2537 } while (0)
2539 /* The pubname structure */
2541 typedef struct GTY(()) pubname_struct {
2542 dw_die_ref die;
2543 const char *name;
2545 pubname_entry;
2548 struct GTY(()) dw_ranges_struct {
2549 /* If this is positive, it's a block number, otherwise it's a
2550 bitwise-negated index into dw_ranges_by_label. */
2551 int num;
2554 /* A structure to hold a macinfo entry. */
2556 typedef struct GTY(()) macinfo_struct {
2557 unsigned char code;
2558 unsigned HOST_WIDE_INT lineno;
2559 const char *info;
2561 macinfo_entry;
2564 struct GTY(()) dw_ranges_by_label_struct {
2565 const char *begin;
2566 const char *end;
2569 /* The comdat type node structure. */
2570 typedef struct GTY(()) comdat_type_struct
2572 dw_die_ref root_die;
2573 dw_die_ref type_die;
2574 dw_die_ref skeleton_die;
2575 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2576 struct comdat_type_struct *next;
2578 comdat_type_node;
2580 /* The limbo die list structure. */
2581 typedef struct GTY(()) limbo_die_struct {
2582 dw_die_ref die;
2583 tree created_for;
2584 struct limbo_die_struct *next;
2586 limbo_die_node;
2588 typedef struct skeleton_chain_struct
2590 dw_die_ref old_die;
2591 dw_die_ref new_die;
2592 struct skeleton_chain_struct *parent;
2594 skeleton_chain_node;
2596 /* Define a macro which returns nonzero for a TYPE_DECL which was
2597 implicitly generated for a type.
2599 Note that, unlike the C front-end (which generates a NULL named
2600 TYPE_DECL node for each complete tagged type, each array type,
2601 and each function type node created) the C++ front-end generates
2602 a _named_ TYPE_DECL node for each tagged type node created.
2603 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2604 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2605 front-end, but for each type, tagged or not. */
2607 #define TYPE_DECL_IS_STUB(decl) \
2608 (DECL_NAME (decl) == NULL_TREE \
2609 || (DECL_ARTIFICIAL (decl) \
2610 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
2611 /* This is necessary for stub decls that \
2612 appear in nested inline functions. */ \
2613 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2614 && (decl_ultimate_origin (decl) \
2615 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2617 /* Information concerning the compilation unit's programming
2618 language, and compiler version. */
2620 /* Fixed size portion of the DWARF compilation unit header. */
2621 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2622 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2624 /* Fixed size portion of the DWARF comdat type unit header. */
2625 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2626 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2627 + DWARF_OFFSET_SIZE)
2629 /* Fixed size portion of public names info. */
2630 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2632 /* Fixed size portion of the address range info. */
2633 #define DWARF_ARANGES_HEADER_SIZE \
2634 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2635 DWARF2_ADDR_SIZE * 2) \
2636 - DWARF_INITIAL_LENGTH_SIZE)
2638 /* Size of padding portion in the address range info. It must be
2639 aligned to twice the pointer size. */
2640 #define DWARF_ARANGES_PAD_SIZE \
2641 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2642 DWARF2_ADDR_SIZE * 2) \
2643 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2645 /* Use assembler line directives if available. */
2646 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2647 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2648 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2649 #else
2650 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2651 #endif
2652 #endif
2654 /* Minimum line offset in a special line info. opcode.
2655 This value was chosen to give a reasonable range of values. */
2656 #define DWARF_LINE_BASE -10
2658 /* First special line opcode - leave room for the standard opcodes. */
2659 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
2661 /* Range of line offsets in a special line info. opcode. */
2662 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
2664 /* Flag that indicates the initial value of the is_stmt_start flag.
2665 In the present implementation, we do not mark any lines as
2666 the beginning of a source statement, because that information
2667 is not made available by the GCC front-end. */
2668 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2670 /* Maximum number of operations per instruction bundle. */
2671 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2672 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2673 #endif
2675 /* This location is used by calc_die_sizes() to keep track
2676 the offset of each DIE within the .debug_info section. */
2677 static unsigned long next_die_offset;
2679 /* Record the root of the DIE's built for the current compilation unit. */
2680 static GTY(()) dw_die_ref single_comp_unit_die;
2682 /* A list of type DIEs that have been separated into comdat sections. */
2683 static GTY(()) comdat_type_node *comdat_type_list;
2685 /* A list of DIEs with a NULL parent waiting to be relocated. */
2686 static GTY(()) limbo_die_node *limbo_die_list;
2688 /* A list of DIEs for which we may have to generate
2689 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
2690 static GTY(()) limbo_die_node *deferred_asm_name;
2692 /* Filenames referenced by this compilation unit. */
2693 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
2695 /* A hash table of references to DIE's that describe declarations.
2696 The key is a DECL_UID() which is a unique number identifying each decl. */
2697 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
2699 /* A hash table of references to DIE's that describe COMMON blocks.
2700 The key is DECL_UID() ^ die_parent. */
2701 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
2703 typedef struct GTY(()) die_arg_entry_struct {
2704 dw_die_ref die;
2705 tree arg;
2706 } die_arg_entry;
2709 /* Node of the variable location list. */
2710 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2711 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2712 EXPR_LIST chain. For small bitsizes, bitsize is encoded
2713 in mode of the EXPR_LIST node and first EXPR_LIST operand
2714 is either NOTE_INSN_VAR_LOCATION for a piece with a known
2715 location or NULL for padding. For larger bitsizes,
2716 mode is 0 and first operand is a CONCAT with bitsize
2717 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2718 NULL as second operand. */
2719 rtx GTY (()) loc;
2720 const char * GTY (()) label;
2721 struct var_loc_node * GTY (()) next;
2724 /* Variable location list. */
2725 struct GTY (()) var_loc_list_def {
2726 struct var_loc_node * GTY (()) first;
2728 /* Pointer to the last but one or last element of the
2729 chained list. If the list is empty, both first and
2730 last are NULL, if the list contains just one node
2731 or the last node certainly is not redundant, it points
2732 to the last node, otherwise points to the last but one.
2733 Do not mark it for GC because it is marked through the chain. */
2734 struct var_loc_node * GTY ((skip ("%h"))) last;
2736 /* Pointer to the last element before section switch,
2737 if NULL, either sections weren't switched or first
2738 is after section switch. */
2739 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2741 /* DECL_UID of the variable decl. */
2742 unsigned int decl_id;
2744 typedef struct var_loc_list_def var_loc_list;
2746 /* Call argument location list. */
2747 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2748 rtx GTY (()) call_arg_loc_note;
2749 const char * GTY (()) label;
2750 tree GTY (()) block;
2751 bool tail_call_p;
2752 rtx GTY (()) symbol_ref;
2753 struct call_arg_loc_node * GTY (()) next;
2757 /* Table of decl location linked lists. */
2758 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
2760 /* Head and tail of call_arg_loc chain. */
2761 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2762 static struct call_arg_loc_node *call_arg_loc_last;
2764 /* Number of call sites in the current function. */
2765 static int call_site_count = -1;
2766 /* Number of tail call sites in the current function. */
2767 static int tail_call_site_count = -1;
2769 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
2770 DIEs. */
2771 static vec<dw_die_ref> block_map;
2773 /* A cached location list. */
2774 struct GTY (()) cached_dw_loc_list_def {
2775 /* The DECL_UID of the decl that this entry describes. */
2776 unsigned int decl_id;
2778 /* The cached location list. */
2779 dw_loc_list_ref loc_list;
2781 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2783 /* Table of cached location lists. */
2784 static GTY ((param_is (cached_dw_loc_list))) htab_t cached_dw_loc_list_table;
2786 /* A pointer to the base of a list of references to DIE's that
2787 are uniquely identified by their tag, presence/absence of
2788 children DIE's, and list of attribute/value pairs. */
2789 static GTY((length ("abbrev_die_table_allocated")))
2790 dw_die_ref *abbrev_die_table;
2792 /* Number of elements currently allocated for abbrev_die_table. */
2793 static GTY(()) unsigned abbrev_die_table_allocated;
2795 /* Number of elements in type_die_table currently in use. */
2796 static GTY(()) unsigned abbrev_die_table_in_use;
2798 /* Size (in elements) of increments by which we may expand the
2799 abbrev_die_table. */
2800 #define ABBREV_DIE_TABLE_INCREMENT 256
2802 /* A global counter for generating labels for line number data. */
2803 static unsigned int line_info_label_num;
2805 /* The current table to which we should emit line number information
2806 for the current function. This will be set up at the beginning of
2807 assembly for the function. */
2808 static dw_line_info_table *cur_line_info_table;
2810 /* The two default tables of line number info. */
2811 static GTY(()) dw_line_info_table *text_section_line_info;
2812 static GTY(()) dw_line_info_table *cold_text_section_line_info;
2814 /* The set of all non-default tables of line number info. */
2815 static GTY(()) vec<dw_line_info_table_p, va_gc> *separate_line_info;
2817 /* A flag to tell pubnames/types export if there is an info section to
2818 refer to. */
2819 static bool info_section_emitted;
2821 /* A pointer to the base of a table that contains a list of publicly
2822 accessible names. */
2823 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
2825 /* A pointer to the base of a table that contains a list of publicly
2826 accessible types. */
2827 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
2829 /* A pointer to the base of a table that contains a list of macro
2830 defines/undefines (and file start/end markers). */
2831 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
2833 /* True if .debug_macinfo or .debug_macros section is going to be
2834 emitted. */
2835 #define have_macinfo \
2836 (debug_info_level >= DINFO_LEVEL_VERBOSE \
2837 && !macinfo_table->is_empty ())
2839 /* Array of dies for which we should generate .debug_ranges info. */
2840 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
2842 /* Number of elements currently allocated for ranges_table. */
2843 static GTY(()) unsigned ranges_table_allocated;
2845 /* Number of elements in ranges_table currently in use. */
2846 static GTY(()) unsigned ranges_table_in_use;
2848 /* Array of pairs of labels referenced in ranges_table. */
2849 static GTY ((length ("ranges_by_label_allocated")))
2850 dw_ranges_by_label_ref ranges_by_label;
2852 /* Number of elements currently allocated for ranges_by_label. */
2853 static GTY(()) unsigned ranges_by_label_allocated;
2855 /* Number of elements in ranges_by_label currently in use. */
2856 static GTY(()) unsigned ranges_by_label_in_use;
2858 /* Size (in elements) of increments by which we may expand the
2859 ranges_table. */
2860 #define RANGES_TABLE_INCREMENT 64
2862 /* Whether we have location lists that need outputting */
2863 static GTY(()) bool have_location_lists;
2865 /* Unique label counter. */
2866 static GTY(()) unsigned int loclabel_num;
2868 /* Unique label counter for point-of-call tables. */
2869 static GTY(()) unsigned int poc_label_num;
2871 /* Record whether the function being analyzed contains inlined functions. */
2872 static int current_function_has_inlines;
2874 /* The last file entry emitted by maybe_emit_file(). */
2875 static GTY(()) struct dwarf_file_data * last_emitted_file;
2877 /* Number of internal labels generated by gen_internal_sym(). */
2878 static GTY(()) int label_num;
2880 /* Cached result of previous call to lookup_filename. */
2881 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
2883 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
2885 /* Instances of generic types for which we need to generate debug
2886 info that describe their generic parameters and arguments. That
2887 generation needs to happen once all types are properly laid out so
2888 we do it at the end of compilation. */
2889 static GTY(()) vec<tree, va_gc> *generic_type_instances;
2891 /* Offset from the "steady-state frame pointer" to the frame base,
2892 within the current function. */
2893 static HOST_WIDE_INT frame_pointer_fb_offset;
2894 static bool frame_pointer_fb_offset_valid;
2896 static vec<dw_die_ref> base_types;
2898 /* Forward declarations for functions defined in this file. */
2900 static int is_pseudo_reg (const_rtx);
2901 static tree type_main_variant (tree);
2902 static int is_tagged_type (const_tree);
2903 static const char *dwarf_tag_name (unsigned);
2904 static const char *dwarf_attr_name (unsigned);
2905 static const char *dwarf_form_name (unsigned);
2906 static tree decl_ultimate_origin (const_tree);
2907 static tree decl_class_context (tree);
2908 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
2909 static inline enum dw_val_class AT_class (dw_attr_ref);
2910 static inline unsigned int AT_index (dw_attr_ref);
2911 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
2912 static inline unsigned AT_flag (dw_attr_ref);
2913 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
2914 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
2915 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
2916 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
2917 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
2918 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2919 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
2920 unsigned int, unsigned char *);
2921 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
2922 static hashval_t debug_str_do_hash (const void *);
2923 static int debug_str_eq (const void *, const void *);
2924 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
2925 static inline const char *AT_string (dw_attr_ref);
2926 static enum dwarf_form AT_string_form (dw_attr_ref);
2927 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
2928 static void add_AT_specification (dw_die_ref, dw_die_ref);
2929 static inline dw_die_ref AT_ref (dw_attr_ref);
2930 static inline int AT_ref_external (dw_attr_ref);
2931 static inline void set_AT_ref_external (dw_attr_ref, int);
2932 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
2933 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
2934 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
2935 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
2936 dw_loc_list_ref);
2937 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
2938 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
2939 static void remove_addr_table_entry (addr_table_entry *);
2940 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
2941 static inline rtx AT_addr (dw_attr_ref);
2942 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
2943 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
2944 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
2945 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
2946 unsigned HOST_WIDE_INT);
2947 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
2948 unsigned long, bool);
2949 static inline const char *AT_lbl (dw_attr_ref);
2950 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
2951 static const char *get_AT_low_pc (dw_die_ref);
2952 static const char *get_AT_hi_pc (dw_die_ref);
2953 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
2954 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
2955 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
2956 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
2957 static bool is_cxx (void);
2958 static bool is_fortran (void);
2959 static bool is_ada (void);
2960 static void remove_AT (dw_die_ref, enum dwarf_attribute);
2961 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
2962 static void add_child_die (dw_die_ref, dw_die_ref);
2963 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
2964 static dw_die_ref lookup_type_die (tree);
2965 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
2966 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
2967 static void equate_type_number_to_die (tree, dw_die_ref);
2968 static hashval_t decl_die_table_hash (const void *);
2969 static int decl_die_table_eq (const void *, const void *);
2970 static dw_die_ref lookup_decl_die (tree);
2971 static hashval_t common_block_die_table_hash (const void *);
2972 static int common_block_die_table_eq (const void *, const void *);
2973 static hashval_t decl_loc_table_hash (const void *);
2974 static int decl_loc_table_eq (const void *, const void *);
2975 static var_loc_list *lookup_decl_loc (const_tree);
2976 static void equate_decl_number_to_die (tree, dw_die_ref);
2977 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
2978 static void print_spaces (FILE *);
2979 static void print_die (dw_die_ref, FILE *);
2980 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
2981 static dw_die_ref pop_compile_unit (dw_die_ref);
2982 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
2983 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
2984 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
2985 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
2986 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
2987 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
2988 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
2989 struct md5_ctx *, int *);
2990 struct checksum_attributes;
2991 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
2992 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
2993 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
2994 static void generate_type_signature (dw_die_ref, comdat_type_node *);
2995 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
2996 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
2997 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
2998 static int same_die_p (dw_die_ref, dw_die_ref, int *);
2999 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3000 static void compute_section_prefix (dw_die_ref);
3001 static int is_type_die (dw_die_ref);
3002 static int is_comdat_die (dw_die_ref);
3003 static int is_symbol_die (dw_die_ref);
3004 static void assign_symbol_names (dw_die_ref);
3005 static void break_out_includes (dw_die_ref);
3006 static int is_declaration_die (dw_die_ref);
3007 static int should_move_die_to_comdat (dw_die_ref);
3008 static dw_die_ref clone_as_declaration (dw_die_ref);
3009 static dw_die_ref clone_die (dw_die_ref);
3010 static dw_die_ref clone_tree (dw_die_ref);
3011 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3012 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3013 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3014 static dw_die_ref generate_skeleton (dw_die_ref);
3015 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3016 dw_die_ref,
3017 dw_die_ref);
3018 static void break_out_comdat_types (dw_die_ref);
3019 static dw_die_ref copy_ancestor_tree (dw_die_ref, dw_die_ref, htab_t);
3020 static void copy_decls_walk (dw_die_ref, dw_die_ref, htab_t);
3021 static void copy_decls_for_unworthy_types (dw_die_ref);
3023 static hashval_t htab_cu_hash (const void *);
3024 static int htab_cu_eq (const void *, const void *);
3025 static void htab_cu_del (void *);
3026 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
3027 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
3028 static void add_sibling_attributes (dw_die_ref);
3029 static void build_abbrev_table (dw_die_ref, htab_t);
3030 static void output_location_lists (dw_die_ref);
3031 static int constant_size (unsigned HOST_WIDE_INT);
3032 static unsigned long size_of_die (dw_die_ref);
3033 static void calc_die_sizes (dw_die_ref);
3034 static void calc_base_type_die_sizes (void);
3035 static void mark_dies (dw_die_ref);
3036 static void unmark_dies (dw_die_ref);
3037 static void unmark_all_dies (dw_die_ref);
3038 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3039 static unsigned long size_of_aranges (void);
3040 static enum dwarf_form value_format (dw_attr_ref);
3041 static void output_value_format (dw_attr_ref);
3042 static void output_abbrev_section (void);
3043 static void output_die_abbrevs (unsigned long, dw_die_ref);
3044 static void output_die_symbol (dw_die_ref);
3045 static void output_die (dw_die_ref);
3046 static void output_compilation_unit_header (void);
3047 static void output_comp_unit (dw_die_ref, int);
3048 static void output_comdat_type_unit (comdat_type_node *);
3049 static const char *dwarf2_name (tree, int);
3050 static void add_pubname (tree, dw_die_ref);
3051 static void add_enumerator_pubname (const char *, dw_die_ref);
3052 static void add_pubname_string (const char *, dw_die_ref);
3053 static void add_pubtype (tree, dw_die_ref);
3054 static void output_pubnames (vec<pubname_entry, va_gc> *);
3055 static void output_aranges (unsigned long);
3056 static unsigned int add_ranges_num (int);
3057 static unsigned int add_ranges (const_tree);
3058 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3059 bool *, bool);
3060 static void output_ranges (void);
3061 static dw_line_info_table *new_line_info_table (void);
3062 static void output_line_info (bool);
3063 static void output_file_names (void);
3064 static dw_die_ref base_type_die (tree);
3065 static int is_base_type (tree);
3066 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3067 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
3068 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3069 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3070 static int type_is_enum (const_tree);
3071 static unsigned int dbx_reg_number (const_rtx);
3072 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3073 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3074 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3075 enum var_init_status);
3076 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3077 enum var_init_status);
3078 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3079 enum var_init_status);
3080 static int is_based_loc (const_rtx);
3081 static int resolve_one_addr (rtx *, void *);
3082 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3083 enum var_init_status);
3084 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
3085 enum var_init_status);
3086 static dw_loc_list_ref loc_list_from_tree (tree, int);
3087 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
3088 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3089 static tree field_type (const_tree);
3090 static unsigned int simple_type_align_in_bits (const_tree);
3091 static unsigned int simple_decl_align_in_bits (const_tree);
3092 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3093 static HOST_WIDE_INT field_byte_offset (const_tree);
3094 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3095 dw_loc_list_ref);
3096 static void add_data_member_location_attribute (dw_die_ref, tree);
3097 static bool add_const_value_attribute (dw_die_ref, rtx);
3098 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3099 static void insert_double (double_int, unsigned char *);
3100 static void insert_float (const_rtx, unsigned char *);
3101 static rtx rtl_for_decl_location (tree);
3102 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3103 enum dwarf_attribute);
3104 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3105 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3106 static void add_name_attribute (dw_die_ref, const char *);
3107 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3108 static void add_comp_dir_attribute (dw_die_ref);
3109 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
3110 static void add_subscript_info (dw_die_ref, tree, bool);
3111 static void add_byte_size_attribute (dw_die_ref, tree);
3112 static void add_bit_offset_attribute (dw_die_ref, tree);
3113 static void add_bit_size_attribute (dw_die_ref, tree);
3114 static void add_prototyped_attribute (dw_die_ref, tree);
3115 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3116 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3117 static void add_src_coords_attributes (dw_die_ref, tree);
3118 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3119 static void push_decl_scope (tree);
3120 static void pop_decl_scope (void);
3121 static dw_die_ref scope_die_for (tree, dw_die_ref);
3122 static inline int local_scope_p (dw_die_ref);
3123 static inline int class_scope_p (dw_die_ref);
3124 static inline int class_or_namespace_scope_p (dw_die_ref);
3125 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
3126 static void add_calling_convention_attribute (dw_die_ref, tree);
3127 static const char *type_tag (const_tree);
3128 static tree member_declared_type (const_tree);
3129 #if 0
3130 static const char *decl_start_label (tree);
3131 #endif
3132 static void gen_array_type_die (tree, dw_die_ref);
3133 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3134 #if 0
3135 static void gen_entry_point_die (tree, dw_die_ref);
3136 #endif
3137 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3138 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3139 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3140 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3141 static void gen_formal_types_die (tree, dw_die_ref);
3142 static void gen_subprogram_die (tree, dw_die_ref);
3143 static void gen_variable_die (tree, tree, dw_die_ref);
3144 static void gen_const_die (tree, dw_die_ref);
3145 static void gen_label_die (tree, dw_die_ref);
3146 static void gen_lexical_block_die (tree, dw_die_ref, int);
3147 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
3148 static void gen_field_die (tree, dw_die_ref);
3149 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3150 static dw_die_ref gen_compile_unit_die (const char *);
3151 static void gen_inheritance_die (tree, tree, dw_die_ref);
3152 static void gen_member_die (tree, dw_die_ref);
3153 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3154 enum debug_info_usage);
3155 static void gen_subroutine_type_die (tree, dw_die_ref);
3156 static void gen_typedef_die (tree, dw_die_ref);
3157 static void gen_type_die (tree, dw_die_ref);
3158 static void gen_block_die (tree, dw_die_ref, int);
3159 static void decls_for_scope (tree, dw_die_ref, int);
3160 static inline int is_redundant_typedef (const_tree);
3161 static bool is_naming_typedef_decl (const_tree);
3162 static inline dw_die_ref get_context_die (tree);
3163 static void gen_namespace_die (tree, dw_die_ref);
3164 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3165 static dw_die_ref force_decl_die (tree);
3166 static dw_die_ref force_type_die (tree);
3167 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3168 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3169 static struct dwarf_file_data * lookup_filename (const char *);
3170 static void retry_incomplete_types (void);
3171 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3172 static void gen_generic_params_dies (tree);
3173 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3174 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3175 static void splice_child_die (dw_die_ref, dw_die_ref);
3176 static int file_info_cmp (const void *, const void *);
3177 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3178 const char *, const char *);
3179 static void output_loc_list (dw_loc_list_ref);
3180 static char *gen_internal_sym (const char *);
3181 static bool want_pubnames (void);
3183 static void prune_unmark_dies (dw_die_ref);
3184 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3185 static void prune_unused_types_mark (dw_die_ref, int);
3186 static void prune_unused_types_walk (dw_die_ref);
3187 static void prune_unused_types_walk_attribs (dw_die_ref);
3188 static void prune_unused_types_prune (dw_die_ref);
3189 static void prune_unused_types (void);
3190 static int maybe_emit_file (struct dwarf_file_data *fd);
3191 static inline const char *AT_vms_delta1 (dw_attr_ref);
3192 static inline const char *AT_vms_delta2 (dw_attr_ref);
3193 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3194 const char *, const char *);
3195 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3196 static void gen_remaining_tmpl_value_param_die_attribute (void);
3197 static bool generic_type_p (tree);
3198 static void schedule_generic_params_dies_gen (tree t);
3199 static void gen_scheduled_generic_parms_dies (void);
3201 /* enum for tracking thread-local variables whose address is really an offset
3202 relative to the TLS pointer, which will need link-time relocation, but will
3203 not need relocation by the DWARF consumer. */
3205 enum dtprel_bool
3207 dtprel_false = 0,
3208 dtprel_true = 1
3211 /* Return the operator to use for an address of a variable. For dtprel_true, we
3212 use DW_OP_const*. For regular variables, which need both link-time
3213 relocation and consumer-level relocation (e.g., to account for shared objects
3214 loaded at a random address), we use DW_OP_addr*. */
3216 static inline enum dwarf_location_atom
3217 dw_addr_op (enum dtprel_bool dtprel)
3219 if (dtprel == dtprel_true)
3220 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3221 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3222 else
3223 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3226 /* Return a pointer to a newly allocated address location description. If
3227 dwarf_split_debug_info is true, then record the address with the appropriate
3228 relocation. */
3229 static inline dw_loc_descr_ref
3230 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3232 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3234 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3235 ref->dw_loc_oprnd1.v.val_addr = addr;
3236 ref->dtprel = dtprel;
3237 if (dwarf_split_debug_info)
3238 ref->dw_loc_oprnd1.val_entry
3239 = add_addr_table_entry (addr,
3240 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3241 else
3242 ref->dw_loc_oprnd1.val_entry = NULL;
3244 return ref;
3247 /* Section names used to hold DWARF debugging information. */
3249 #ifndef DEBUG_INFO_SECTION
3250 #define DEBUG_INFO_SECTION ".debug_info"
3251 #endif
3252 #ifndef DEBUG_DWO_INFO_SECTION
3253 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3254 #endif
3255 #ifndef DEBUG_ABBREV_SECTION
3256 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3257 #endif
3258 #ifndef DEBUG_DWO_ABBREV_SECTION
3259 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3260 #endif
3261 #ifndef DEBUG_ARANGES_SECTION
3262 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3263 #endif
3264 #ifndef DEBUG_ADDR_SECTION
3265 #define DEBUG_ADDR_SECTION ".debug_addr"
3266 #endif
3267 #ifndef DEBUG_NORM_MACINFO_SECTION
3268 #define DEBUG_NORM_MACINFO_SECTION ".debug_macinfo"
3269 #endif
3270 #ifndef DEBUG_DWO_MACINFO_SECTION
3271 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3272 #endif
3273 #ifndef DEBUG_MACINFO_SECTION
3274 #define DEBUG_MACINFO_SECTION \
3275 (!dwarf_split_debug_info \
3276 ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3277 #endif
3278 #ifndef DEBUG_NORM_MACRO_SECTION
3279 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3280 #endif
3281 #ifndef DEBUG_DWO_MACRO_SECTION
3282 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3283 #endif
3284 #ifndef DEBUG_MACRO_SECTION
3285 #define DEBUG_MACRO_SECTION \
3286 (!dwarf_split_debug_info \
3287 ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3288 #endif
3289 #ifndef DEBUG_LINE_SECTION
3290 #define DEBUG_LINE_SECTION ".debug_line"
3291 #endif
3292 #ifndef DEBUG_DWO_LINE_SECTION
3293 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3294 #endif
3295 #ifndef DEBUG_LOC_SECTION
3296 #define DEBUG_LOC_SECTION ".debug_loc"
3297 #endif
3298 #ifndef DEBUG_DWO_LOC_SECTION
3299 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3300 #endif
3301 #ifndef DEBUG_PUBNAMES_SECTION
3302 #define DEBUG_PUBNAMES_SECTION ".debug_pubnames"
3303 #endif
3304 #ifndef DEBUG_PUBTYPES_SECTION
3305 #define DEBUG_PUBTYPES_SECTION ".debug_pubtypes"
3306 #endif
3307 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3308 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3309 #ifndef DEBUG_STR_OFFSETS_SECTION
3310 #define DEBUG_STR_OFFSETS_SECTION \
3311 (!dwarf_split_debug_info \
3312 ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3313 #endif
3314 #define DEBUG_DWO_STR_SECTION ".debug_str.dwo"
3315 #define DEBUG_NORM_STR_SECTION ".debug_str"
3316 #ifndef DEBUG_STR_SECTION
3317 #define DEBUG_STR_SECTION \
3318 (!dwarf_split_debug_info ? (DEBUG_NORM_STR_SECTION) : (DEBUG_DWO_STR_SECTION))
3319 #endif
3320 #ifndef DEBUG_RANGES_SECTION
3321 #define DEBUG_RANGES_SECTION ".debug_ranges"
3322 #endif
3324 /* Standard ELF section names for compiled code and data. */
3325 #ifndef TEXT_SECTION_NAME
3326 #define TEXT_SECTION_NAME ".text"
3327 #endif
3329 /* Section flags for .debug_macinfo/.debug_macro section. */
3330 #define DEBUG_MACRO_SECTION_FLAGS \
3331 (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3333 /* Section flags for .debug_str section. */
3334 #define DEBUG_STR_SECTION_FLAGS \
3335 (dwarf_split_debug_info \
3336 ? SECTION_DEBUG | SECTION_EXCLUDE \
3337 : (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3338 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3339 : SECTION_DEBUG))
3341 /* Labels we insert at beginning sections we can reference instead of
3342 the section names themselves. */
3344 #ifndef TEXT_SECTION_LABEL
3345 #define TEXT_SECTION_LABEL "Ltext"
3346 #endif
3347 #ifndef COLD_TEXT_SECTION_LABEL
3348 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3349 #endif
3350 #ifndef DEBUG_LINE_SECTION_LABEL
3351 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3352 #endif
3353 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3354 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3355 #endif
3356 #ifndef DEBUG_INFO_SECTION_LABEL
3357 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3358 #endif
3359 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3360 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3361 #endif
3362 #ifndef DEBUG_ABBREV_SECTION_LABEL
3363 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3364 #endif
3365 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3366 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3367 #endif
3368 #ifndef DEBUG_ADDR_SECTION_LABEL
3369 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3370 #endif
3371 #ifndef DEBUG_LOC_SECTION_LABEL
3372 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3373 #endif
3374 #ifndef DEBUG_RANGES_SECTION_LABEL
3375 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3376 #endif
3377 #ifndef DEBUG_MACINFO_SECTION_LABEL
3378 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3379 #endif
3380 #ifndef DEBUG_MACRO_SECTION_LABEL
3381 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3382 #endif
3383 #define SKELETON_COMP_DIE_ABBREV 1
3384 #define SKELETON_TYPE_DIE_ABBREV 2
3386 /* Definitions of defaults for formats and names of various special
3387 (artificial) labels which may be generated within this file (when the -g
3388 options is used and DWARF2_DEBUGGING_INFO is in effect.
3389 If necessary, these may be overridden from within the tm.h file, but
3390 typically, overriding these defaults is unnecessary. */
3392 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3393 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3394 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3395 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3396 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3397 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3398 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3399 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3400 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3401 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3402 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3403 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3404 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3405 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3407 #ifndef TEXT_END_LABEL
3408 #define TEXT_END_LABEL "Letext"
3409 #endif
3410 #ifndef COLD_END_LABEL
3411 #define COLD_END_LABEL "Letext_cold"
3412 #endif
3413 #ifndef BLOCK_BEGIN_LABEL
3414 #define BLOCK_BEGIN_LABEL "LBB"
3415 #endif
3416 #ifndef BLOCK_END_LABEL
3417 #define BLOCK_END_LABEL "LBE"
3418 #endif
3419 #ifndef LINE_CODE_LABEL
3420 #define LINE_CODE_LABEL "LM"
3421 #endif
3424 /* Return the root of the DIE's built for the current compilation unit. */
3425 static dw_die_ref
3426 comp_unit_die (void)
3428 if (!single_comp_unit_die)
3429 single_comp_unit_die = gen_compile_unit_die (NULL);
3430 return single_comp_unit_die;
3433 /* We allow a language front-end to designate a function that is to be
3434 called to "demangle" any name before it is put into a DIE. */
3436 static const char *(*demangle_name_func) (const char *);
3438 void
3439 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3441 demangle_name_func = func;
3444 /* Test if rtl node points to a pseudo register. */
3446 static inline int
3447 is_pseudo_reg (const_rtx rtl)
3449 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3450 || (GET_CODE (rtl) == SUBREG
3451 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3454 /* Return a reference to a type, with its const and volatile qualifiers
3455 removed. */
3457 static inline tree
3458 type_main_variant (tree type)
3460 type = TYPE_MAIN_VARIANT (type);
3462 /* ??? There really should be only one main variant among any group of
3463 variants of a given type (and all of the MAIN_VARIANT values for all
3464 members of the group should point to that one type) but sometimes the C
3465 front-end messes this up for array types, so we work around that bug
3466 here. */
3467 if (TREE_CODE (type) == ARRAY_TYPE)
3468 while (type != TYPE_MAIN_VARIANT (type))
3469 type = TYPE_MAIN_VARIANT (type);
3471 return type;
3474 /* Return nonzero if the given type node represents a tagged type. */
3476 static inline int
3477 is_tagged_type (const_tree type)
3479 enum tree_code code = TREE_CODE (type);
3481 return (code == RECORD_TYPE || code == UNION_TYPE
3482 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3485 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3487 static void
3488 get_ref_die_offset_label (char *label, dw_die_ref ref)
3490 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3493 /* Return die_offset of a DIE reference to a base type. */
3495 static unsigned long int
3496 get_base_type_offset (dw_die_ref ref)
3498 if (ref->die_offset)
3499 return ref->die_offset;
3500 if (comp_unit_die ()->die_abbrev)
3502 calc_base_type_die_sizes ();
3503 gcc_assert (ref->die_offset);
3505 return ref->die_offset;
3508 /* Return die_offset of a DIE reference other than base type. */
3510 static unsigned long int
3511 get_ref_die_offset (dw_die_ref ref)
3513 gcc_assert (ref->die_offset);
3514 return ref->die_offset;
3517 /* Convert a DIE tag into its string name. */
3519 static const char *
3520 dwarf_tag_name (unsigned int tag)
3522 const char *name = get_DW_TAG_name (tag);
3524 if (name != NULL)
3525 return name;
3527 return "DW_TAG_<unknown>";
3530 /* Convert a DWARF attribute code into its string name. */
3532 static const char *
3533 dwarf_attr_name (unsigned int attr)
3535 const char *name;
3537 switch (attr)
3539 #if VMS_DEBUGGING_INFO
3540 case DW_AT_HP_prologue:
3541 return "DW_AT_HP_prologue";
3542 #else
3543 case DW_AT_MIPS_loop_unroll_factor:
3544 return "DW_AT_MIPS_loop_unroll_factor";
3545 #endif
3547 #if VMS_DEBUGGING_INFO
3548 case DW_AT_HP_epilogue:
3549 return "DW_AT_HP_epilogue";
3550 #else
3551 case DW_AT_MIPS_stride:
3552 return "DW_AT_MIPS_stride";
3553 #endif
3556 name = get_DW_AT_name (attr);
3558 if (name != NULL)
3559 return name;
3561 return "DW_AT_<unknown>";
3564 /* Convert a DWARF value form code into its string name. */
3566 static const char *
3567 dwarf_form_name (unsigned int form)
3569 const char *name = get_DW_FORM_name (form);
3571 if (name != NULL)
3572 return name;
3574 return "DW_FORM_<unknown>";
3577 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3578 instance of an inlined instance of a decl which is local to an inline
3579 function, so we have to trace all of the way back through the origin chain
3580 to find out what sort of node actually served as the original seed for the
3581 given block. */
3583 static tree
3584 decl_ultimate_origin (const_tree decl)
3586 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3587 return NULL_TREE;
3589 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
3590 nodes in the function to point to themselves; ignore that if
3591 we're trying to output the abstract instance of this function. */
3592 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3593 return NULL_TREE;
3595 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3596 most distant ancestor, this should never happen. */
3597 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3599 return DECL_ABSTRACT_ORIGIN (decl);
3602 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
3603 of a virtual function may refer to a base class, so we check the 'this'
3604 parameter. */
3606 static tree
3607 decl_class_context (tree decl)
3609 tree context = NULL_TREE;
3611 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3612 context = DECL_CONTEXT (decl);
3613 else
3614 context = TYPE_MAIN_VARIANT
3615 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3617 if (context && !TYPE_P (context))
3618 context = NULL_TREE;
3620 return context;
3623 /* Add an attribute/value pair to a DIE. */
3625 static inline void
3626 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
3628 /* Maybe this should be an assert? */
3629 if (die == NULL)
3630 return;
3632 vec_safe_reserve (die->die_attr, 1);
3633 vec_safe_push (die->die_attr, *attr);
3636 static inline enum dw_val_class
3637 AT_class (dw_attr_ref a)
3639 return a->dw_attr_val.val_class;
3642 /* Return the index for any attribute that will be referenced with a
3643 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
3644 are stored in dw_attr_val.v.val_str for reference counting
3645 pruning. */
3647 static inline unsigned int
3648 AT_index (dw_attr_ref a)
3650 if (AT_class (a) == dw_val_class_str)
3651 return a->dw_attr_val.v.val_str->index;
3652 else if (a->dw_attr_val.val_entry != NULL)
3653 return a->dw_attr_val.val_entry->index;
3654 return NOT_INDEXED;
3657 /* Add a flag value attribute to a DIE. */
3659 static inline void
3660 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3662 dw_attr_node attr;
3664 attr.dw_attr = attr_kind;
3665 attr.dw_attr_val.val_class = dw_val_class_flag;
3666 attr.dw_attr_val.val_entry = NULL;
3667 attr.dw_attr_val.v.val_flag = flag;
3668 add_dwarf_attr (die, &attr);
3671 static inline unsigned
3672 AT_flag (dw_attr_ref a)
3674 gcc_assert (a && AT_class (a) == dw_val_class_flag);
3675 return a->dw_attr_val.v.val_flag;
3678 /* Add a signed integer attribute value to a DIE. */
3680 static inline void
3681 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3683 dw_attr_node attr;
3685 attr.dw_attr = attr_kind;
3686 attr.dw_attr_val.val_class = dw_val_class_const;
3687 attr.dw_attr_val.val_entry = NULL;
3688 attr.dw_attr_val.v.val_int = int_val;
3689 add_dwarf_attr (die, &attr);
3692 static inline HOST_WIDE_INT
3693 AT_int (dw_attr_ref a)
3695 gcc_assert (a && AT_class (a) == dw_val_class_const);
3696 return a->dw_attr_val.v.val_int;
3699 /* Add an unsigned integer attribute value to a DIE. */
3701 static inline void
3702 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3703 unsigned HOST_WIDE_INT unsigned_val)
3705 dw_attr_node attr;
3707 attr.dw_attr = attr_kind;
3708 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3709 attr.dw_attr_val.val_entry = NULL;
3710 attr.dw_attr_val.v.val_unsigned = unsigned_val;
3711 add_dwarf_attr (die, &attr);
3714 static inline unsigned HOST_WIDE_INT
3715 AT_unsigned (dw_attr_ref a)
3717 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3718 return a->dw_attr_val.v.val_unsigned;
3721 /* Add an unsigned double integer attribute value to a DIE. */
3723 static inline void
3724 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3725 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3727 dw_attr_node attr;
3729 attr.dw_attr = attr_kind;
3730 attr.dw_attr_val.val_class = dw_val_class_const_double;
3731 attr.dw_attr_val.val_entry = NULL;
3732 attr.dw_attr_val.v.val_double.high = high;
3733 attr.dw_attr_val.v.val_double.low = low;
3734 add_dwarf_attr (die, &attr);
3737 /* Add a floating point attribute value to a DIE and return it. */
3739 static inline void
3740 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3741 unsigned int length, unsigned int elt_size, unsigned char *array)
3743 dw_attr_node attr;
3745 attr.dw_attr = attr_kind;
3746 attr.dw_attr_val.val_class = dw_val_class_vec;
3747 attr.dw_attr_val.val_entry = NULL;
3748 attr.dw_attr_val.v.val_vec.length = length;
3749 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3750 attr.dw_attr_val.v.val_vec.array = array;
3751 add_dwarf_attr (die, &attr);
3754 /* Add an 8-byte data attribute value to a DIE. */
3756 static inline void
3757 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3758 unsigned char data8[8])
3760 dw_attr_node attr;
3762 attr.dw_attr = attr_kind;
3763 attr.dw_attr_val.val_class = dw_val_class_data8;
3764 attr.dw_attr_val.val_entry = NULL;
3765 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3766 add_dwarf_attr (die, &attr);
3769 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
3770 dwarf_split_debug_info, address attributes in dies destined for the
3771 final executable have force_direct set to avoid using indexed
3772 references. */
3774 static inline void
3775 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
3776 bool force_direct)
3778 dw_attr_node attr;
3779 char * lbl_id;
3781 lbl_id = xstrdup (lbl_low);
3782 attr.dw_attr = DW_AT_low_pc;
3783 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3784 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3785 if (dwarf_split_debug_info && !force_direct)
3786 attr.dw_attr_val.val_entry
3787 = add_addr_table_entry (lbl_id, ate_kind_label);
3788 else
3789 attr.dw_attr_val.val_entry = NULL;
3790 add_dwarf_attr (die, &attr);
3792 attr.dw_attr = DW_AT_high_pc;
3793 if (dwarf_version < 4)
3794 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3795 else
3796 attr.dw_attr_val.val_class = dw_val_class_high_pc;
3797 lbl_id = xstrdup (lbl_high);
3798 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3799 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
3800 && dwarf_split_debug_info && !force_direct)
3801 attr.dw_attr_val.val_entry
3802 = add_addr_table_entry (lbl_id, ate_kind_label);
3803 else
3804 attr.dw_attr_val.val_entry = NULL;
3805 add_dwarf_attr (die, &attr);
3808 /* Hash and equality functions for debug_str_hash. */
3810 static hashval_t
3811 debug_str_do_hash (const void *x)
3813 return htab_hash_string (((const struct indirect_string_node *)x)->str);
3816 static int
3817 debug_str_eq (const void *x1, const void *x2)
3819 return strcmp ((((const struct indirect_string_node *)x1)->str),
3820 (const char *)x2) == 0;
3823 /* Add STR to the indirect string hash table. */
3825 static struct indirect_string_node *
3826 find_AT_string (const char *str)
3828 struct indirect_string_node *node;
3829 void **slot;
3831 if (! debug_str_hash)
3832 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
3833 debug_str_eq, NULL);
3835 slot = htab_find_slot_with_hash (debug_str_hash, str,
3836 htab_hash_string (str), INSERT);
3837 if (*slot == NULL)
3839 node = ggc_alloc_cleared_indirect_string_node ();
3840 node->str = ggc_strdup (str);
3841 *slot = node;
3843 else
3844 node = (struct indirect_string_node *) *slot;
3846 node->refcount++;
3847 return node;
3850 /* Add a string attribute value to a DIE. */
3852 static inline void
3853 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
3855 dw_attr_node attr;
3856 struct indirect_string_node *node;
3858 node = find_AT_string (str);
3860 attr.dw_attr = attr_kind;
3861 attr.dw_attr_val.val_class = dw_val_class_str;
3862 attr.dw_attr_val.val_entry = NULL;
3863 attr.dw_attr_val.v.val_str = node;
3864 add_dwarf_attr (die, &attr);
3867 static inline const char *
3868 AT_string (dw_attr_ref a)
3870 gcc_assert (a && AT_class (a) == dw_val_class_str);
3871 return a->dw_attr_val.v.val_str->str;
3874 /* Call this function directly to bypass AT_string_form's logic to put
3875 the string inline in the die. */
3877 static void
3878 set_indirect_string (struct indirect_string_node *node)
3880 char label[32];
3881 /* Already indirect is a no op. */
3882 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
3884 gcc_assert (node->label);
3885 return;
3887 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
3888 ++dw2_string_counter;
3889 node->label = xstrdup (label);
3891 if (!dwarf_split_debug_info)
3893 node->form = DW_FORM_strp;
3894 node->index = NOT_INDEXED;
3896 else
3898 node->form = DW_FORM_GNU_str_index;
3899 node->index = NO_INDEX_ASSIGNED;
3903 /* Find out whether a string should be output inline in DIE
3904 or out-of-line in .debug_str section. */
3906 static enum dwarf_form
3907 find_string_form (struct indirect_string_node *node)
3909 unsigned int len;
3911 if (node->form)
3912 return node->form;
3914 len = strlen (node->str) + 1;
3916 /* If the string is shorter or equal to the size of the reference, it is
3917 always better to put it inline. */
3918 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
3919 return node->form = DW_FORM_string;
3921 /* If we cannot expect the linker to merge strings in .debug_str
3922 section, only put it into .debug_str if it is worth even in this
3923 single module. */
3924 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
3925 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
3926 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
3927 return node->form = DW_FORM_string;
3929 set_indirect_string (node);
3931 return node->form;
3934 /* Find out whether the string referenced from the attribute should be
3935 output inline in DIE or out-of-line in .debug_str section. */
3937 static enum dwarf_form
3938 AT_string_form (dw_attr_ref a)
3940 gcc_assert (a && AT_class (a) == dw_val_class_str);
3941 return find_string_form (a->dw_attr_val.v.val_str);
3944 /* Add a DIE reference attribute value to a DIE. */
3946 static inline void
3947 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
3949 dw_attr_node attr;
3951 #ifdef ENABLE_CHECKING
3952 gcc_assert (targ_die != NULL);
3953 #else
3954 /* With LTO we can end up trying to reference something we didn't create
3955 a DIE for. Avoid crashing later on a NULL referenced DIE. */
3956 if (targ_die == NULL)
3957 return;
3958 #endif
3960 attr.dw_attr = attr_kind;
3961 attr.dw_attr_val.val_class = dw_val_class_die_ref;
3962 attr.dw_attr_val.val_entry = NULL;
3963 attr.dw_attr_val.v.val_die_ref.die = targ_die;
3964 attr.dw_attr_val.v.val_die_ref.external = 0;
3965 add_dwarf_attr (die, &attr);
3968 /* Change DIE reference REF to point to NEW_DIE instead. */
3970 static inline void
3971 change_AT_die_ref (dw_attr_ref ref, dw_die_ref new_die)
3973 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
3974 ref->dw_attr_val.v.val_die_ref.die = new_die;
3975 ref->dw_attr_val.v.val_die_ref.external = 0;
3978 /* Add an AT_specification attribute to a DIE, and also make the back
3979 pointer from the specification to the definition. */
3981 static inline void
3982 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
3984 add_AT_die_ref (die, DW_AT_specification, targ_die);
3985 gcc_assert (!targ_die->die_definition);
3986 targ_die->die_definition = die;
3989 static inline dw_die_ref
3990 AT_ref (dw_attr_ref a)
3992 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
3993 return a->dw_attr_val.v.val_die_ref.die;
3996 static inline int
3997 AT_ref_external (dw_attr_ref a)
3999 if (a && AT_class (a) == dw_val_class_die_ref)
4000 return a->dw_attr_val.v.val_die_ref.external;
4002 return 0;
4005 static inline void
4006 set_AT_ref_external (dw_attr_ref a, int i)
4008 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4009 a->dw_attr_val.v.val_die_ref.external = i;
4012 /* Add an FDE reference attribute value to a DIE. */
4014 static inline void
4015 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4017 dw_attr_node attr;
4019 attr.dw_attr = attr_kind;
4020 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4021 attr.dw_attr_val.val_entry = NULL;
4022 attr.dw_attr_val.v.val_fde_index = targ_fde;
4023 add_dwarf_attr (die, &attr);
4026 /* Add a location description attribute value to a DIE. */
4028 static inline void
4029 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4031 dw_attr_node attr;
4033 attr.dw_attr = attr_kind;
4034 attr.dw_attr_val.val_class = dw_val_class_loc;
4035 attr.dw_attr_val.val_entry = NULL;
4036 attr.dw_attr_val.v.val_loc = loc;
4037 add_dwarf_attr (die, &attr);
4040 static inline dw_loc_descr_ref
4041 AT_loc (dw_attr_ref a)
4043 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4044 return a->dw_attr_val.v.val_loc;
4047 static inline void
4048 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4050 dw_attr_node attr;
4052 attr.dw_attr = attr_kind;
4053 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4054 attr.dw_attr_val.val_entry = NULL;
4055 attr.dw_attr_val.v.val_loc_list = loc_list;
4056 add_dwarf_attr (die, &attr);
4057 have_location_lists = true;
4060 static inline dw_loc_list_ref
4061 AT_loc_list (dw_attr_ref a)
4063 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4064 return a->dw_attr_val.v.val_loc_list;
4067 static inline dw_loc_list_ref *
4068 AT_loc_list_ptr (dw_attr_ref a)
4070 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4071 return &a->dw_attr_val.v.val_loc_list;
4074 /* Table of entries into the .debug_addr section. */
4076 static GTY ((param_is (addr_table_entry))) htab_t addr_index_table;
4078 /* Hash an address_table_entry. */
4080 static hashval_t
4081 addr_table_entry_do_hash (const void *x)
4083 const addr_table_entry *a = (const addr_table_entry *) x;
4084 switch (a->kind)
4086 case ate_kind_rtx:
4087 return iterative_hash_rtx (a->addr.rtl, 0);
4088 case ate_kind_rtx_dtprel:
4089 return iterative_hash_rtx (a->addr.rtl, 1);
4090 case ate_kind_label:
4091 return htab_hash_string (a->addr.label);
4092 default:
4093 gcc_unreachable ();
4097 /* Determine equality for two address_table_entries. */
4099 static int
4100 addr_table_entry_eq (const void *x1, const void *x2)
4102 const addr_table_entry *a1 = (const addr_table_entry *) x1;
4103 const addr_table_entry *a2 = (const addr_table_entry *) x2;
4105 if (a1->kind != a2->kind)
4106 return 0;
4107 switch (a1->kind)
4109 case ate_kind_rtx:
4110 case ate_kind_rtx_dtprel:
4111 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4112 case ate_kind_label:
4113 return strcmp (a1->addr.label, a2->addr.label) == 0;
4114 default:
4115 gcc_unreachable ();
4119 /* Initialize an addr_table_entry. */
4121 void
4122 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4124 e->kind = kind;
4125 switch (kind)
4127 case ate_kind_rtx:
4128 case ate_kind_rtx_dtprel:
4129 e->addr.rtl = (rtx) addr;
4130 break;
4131 case ate_kind_label:
4132 e->addr.label = (char *) addr;
4133 break;
4135 e->refcount = 0;
4136 e->index = NO_INDEX_ASSIGNED;
4139 /* Add attr to the address table entry to the table. Defer setting an
4140 index until output time. */
4142 static addr_table_entry *
4143 add_addr_table_entry (void *addr, enum ate_kind kind)
4145 addr_table_entry *node;
4146 addr_table_entry finder;
4147 void **slot;
4149 gcc_assert (dwarf_split_debug_info);
4150 if (! addr_index_table)
4151 addr_index_table = htab_create_ggc (10, addr_table_entry_do_hash,
4152 addr_table_entry_eq, NULL);
4153 init_addr_table_entry (&finder, kind, addr);
4154 slot = htab_find_slot (addr_index_table, &finder, INSERT);
4156 if (*slot == HTAB_EMPTY_ENTRY)
4158 node = ggc_alloc_cleared_addr_table_entry ();
4159 init_addr_table_entry (node, kind, addr);
4160 *slot = node;
4162 else
4163 node = (addr_table_entry *) *slot;
4165 node->refcount++;
4166 return node;
4169 /* Remove an entry from the addr table by decrementing its refcount.
4170 Strictly, decrementing the refcount would be enough, but the
4171 assertion that the entry is actually in the table has found
4172 bugs. */
4174 static void
4175 remove_addr_table_entry (addr_table_entry *entry)
4177 addr_table_entry *node;
4179 gcc_assert (dwarf_split_debug_info && addr_index_table);
4180 node = (addr_table_entry *) htab_find (addr_index_table, entry);
4181 /* After an index is assigned, the table is frozen. */
4182 gcc_assert (node->refcount > 0 && node->index == NO_INDEX_ASSIGNED);
4183 node->refcount--;
4186 /* Given a location list, remove all addresses it refers to from the
4187 address_table. */
4189 static void
4190 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4192 for (; descr; descr = descr->dw_loc_next)
4193 if (descr->dw_loc_oprnd1.val_entry != NULL)
4195 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4196 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4200 /* A helper function for dwarf2out_finish called through
4201 htab_traverse. Assign an addr_table_entry its index. All entries
4202 must be collected into the table when this function is called,
4203 because the indexing code relies on htab_traverse to traverse nodes
4204 in the same order for each run. */
4206 static int
4207 index_addr_table_entry (void **h, void *v)
4209 addr_table_entry *node = (addr_table_entry *) *h;
4210 unsigned int *index = (unsigned int *) v;
4212 /* Don't index unreferenced nodes. */
4213 if (node->refcount == 0)
4214 return 1;
4216 gcc_assert(node->index == NO_INDEX_ASSIGNED);
4217 node->index = *index;
4218 *index += 1;
4220 return 1;
4223 /* Add an address constant attribute value to a DIE. When using
4224 dwarf_split_debug_info, address attributes in dies destined for the
4225 final executable should be direct references--setting the parameter
4226 force_direct ensures this behavior. */
4228 static inline void
4229 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4230 bool force_direct)
4232 dw_attr_node attr;
4234 attr.dw_attr = attr_kind;
4235 attr.dw_attr_val.val_class = dw_val_class_addr;
4236 attr.dw_attr_val.v.val_addr = addr;
4237 if (dwarf_split_debug_info && !force_direct)
4238 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4239 else
4240 attr.dw_attr_val.val_entry = NULL;
4241 add_dwarf_attr (die, &attr);
4244 /* Get the RTX from to an address DIE attribute. */
4246 static inline rtx
4247 AT_addr (dw_attr_ref a)
4249 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4250 return a->dw_attr_val.v.val_addr;
4253 /* Add a file attribute value to a DIE. */
4255 static inline void
4256 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4257 struct dwarf_file_data *fd)
4259 dw_attr_node attr;
4261 attr.dw_attr = attr_kind;
4262 attr.dw_attr_val.val_class = dw_val_class_file;
4263 attr.dw_attr_val.val_entry = NULL;
4264 attr.dw_attr_val.v.val_file = fd;
4265 add_dwarf_attr (die, &attr);
4268 /* Get the dwarf_file_data from a file DIE attribute. */
4270 static inline struct dwarf_file_data *
4271 AT_file (dw_attr_ref a)
4273 gcc_assert (a && AT_class (a) == dw_val_class_file);
4274 return a->dw_attr_val.v.val_file;
4277 /* Add a vms delta attribute value to a DIE. */
4279 static inline void
4280 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4281 const char *lbl1, const char *lbl2)
4283 dw_attr_node attr;
4285 attr.dw_attr = attr_kind;
4286 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4287 attr.dw_attr_val.val_entry = NULL;
4288 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4289 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4290 add_dwarf_attr (die, &attr);
4293 /* Add a label identifier attribute value to a DIE. */
4295 static inline void
4296 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4297 const char *lbl_id)
4299 dw_attr_node attr;
4301 attr.dw_attr = attr_kind;
4302 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4303 attr.dw_attr_val.val_entry = NULL;
4304 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4305 if (dwarf_split_debug_info)
4306 attr.dw_attr_val.val_entry
4307 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4308 ate_kind_label);
4309 add_dwarf_attr (die, &attr);
4312 /* Add a section offset attribute value to a DIE, an offset into the
4313 debug_line section. */
4315 static inline void
4316 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4317 const char *label)
4319 dw_attr_node attr;
4321 attr.dw_attr = attr_kind;
4322 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4323 attr.dw_attr_val.val_entry = NULL;
4324 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4325 add_dwarf_attr (die, &attr);
4328 /* Add a section offset attribute value to a DIE, an offset into the
4329 debug_macinfo section. */
4331 static inline void
4332 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4333 const char *label)
4335 dw_attr_node attr;
4337 attr.dw_attr = attr_kind;
4338 attr.dw_attr_val.val_class = dw_val_class_macptr;
4339 attr.dw_attr_val.val_entry = NULL;
4340 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4341 add_dwarf_attr (die, &attr);
4344 /* Add an offset attribute value to a DIE. */
4346 static inline void
4347 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4348 unsigned HOST_WIDE_INT offset)
4350 dw_attr_node attr;
4352 attr.dw_attr = attr_kind;
4353 attr.dw_attr_val.val_class = dw_val_class_offset;
4354 attr.dw_attr_val.val_entry = NULL;
4355 attr.dw_attr_val.v.val_offset = offset;
4356 add_dwarf_attr (die, &attr);
4359 /* Add a range_list attribute value to a DIE. When using
4360 dwarf_split_debug_info, address attributes in dies destined for the
4361 final executable should be direct references--setting the parameter
4362 force_direct ensures this behavior. */
4364 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4365 #define RELOCATED_OFFSET (NULL)
4367 static void
4368 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4369 long unsigned int offset, bool force_direct)
4371 dw_attr_node attr;
4373 attr.dw_attr = attr_kind;
4374 attr.dw_attr_val.val_class = dw_val_class_range_list;
4375 /* For the range_list attribute, use val_entry to store whether the
4376 offset should follow split-debug-info or normal semantics. This
4377 value is read in output_range_list_offset. */
4378 if (dwarf_split_debug_info && !force_direct)
4379 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4380 else
4381 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4382 attr.dw_attr_val.v.val_offset = offset;
4383 add_dwarf_attr (die, &attr);
4386 /* Return the start label of a delta attribute. */
4388 static inline const char *
4389 AT_vms_delta1 (dw_attr_ref a)
4391 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4392 return a->dw_attr_val.v.val_vms_delta.lbl1;
4395 /* Return the end label of a delta attribute. */
4397 static inline const char *
4398 AT_vms_delta2 (dw_attr_ref a)
4400 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4401 return a->dw_attr_val.v.val_vms_delta.lbl2;
4404 static inline const char *
4405 AT_lbl (dw_attr_ref a)
4407 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4408 || AT_class (a) == dw_val_class_lineptr
4409 || AT_class (a) == dw_val_class_macptr
4410 || AT_class (a) == dw_val_class_high_pc));
4411 return a->dw_attr_val.v.val_lbl_id;
4414 /* Get the attribute of type attr_kind. */
4416 static dw_attr_ref
4417 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4419 dw_attr_ref a;
4420 unsigned ix;
4421 dw_die_ref spec = NULL;
4423 if (! die)
4424 return NULL;
4426 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4427 if (a->dw_attr == attr_kind)
4428 return a;
4429 else if (a->dw_attr == DW_AT_specification
4430 || a->dw_attr == DW_AT_abstract_origin)
4431 spec = AT_ref (a);
4433 if (spec)
4434 return get_AT (spec, attr_kind);
4436 return NULL;
4439 /* Returns the parent of the declaration of DIE. */
4441 static dw_die_ref
4442 get_die_parent (dw_die_ref die)
4444 dw_die_ref t;
4446 if (!die)
4447 return NULL;
4449 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4450 || (t = get_AT_ref (die, DW_AT_specification)))
4451 die = t;
4453 return die->die_parent;
4456 /* Return the "low pc" attribute value, typically associated with a subprogram
4457 DIE. Return null if the "low pc" attribute is either not present, or if it
4458 cannot be represented as an assembler label identifier. */
4460 static inline const char *
4461 get_AT_low_pc (dw_die_ref die)
4463 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4465 return a ? AT_lbl (a) : NULL;
4468 /* Return the "high pc" attribute value, typically associated with a subprogram
4469 DIE. Return null if the "high pc" attribute is either not present, or if it
4470 cannot be represented as an assembler label identifier. */
4472 static inline const char *
4473 get_AT_hi_pc (dw_die_ref die)
4475 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4477 return a ? AT_lbl (a) : NULL;
4480 /* Return the value of the string attribute designated by ATTR_KIND, or
4481 NULL if it is not present. */
4483 static inline const char *
4484 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4486 dw_attr_ref a = get_AT (die, attr_kind);
4488 return a ? AT_string (a) : NULL;
4491 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4492 if it is not present. */
4494 static inline int
4495 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4497 dw_attr_ref a = get_AT (die, attr_kind);
4499 return a ? AT_flag (a) : 0;
4502 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4503 if it is not present. */
4505 static inline unsigned
4506 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4508 dw_attr_ref a = get_AT (die, attr_kind);
4510 return a ? AT_unsigned (a) : 0;
4513 static inline dw_die_ref
4514 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4516 dw_attr_ref a = get_AT (die, attr_kind);
4518 return a ? AT_ref (a) : NULL;
4521 static inline struct dwarf_file_data *
4522 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4524 dw_attr_ref a = get_AT (die, attr_kind);
4526 return a ? AT_file (a) : NULL;
4529 /* Return TRUE if the language is C++. */
4531 static inline bool
4532 is_cxx (void)
4534 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4536 return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
4539 /* Return TRUE if the language is Fortran. */
4541 static inline bool
4542 is_fortran (void)
4544 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4546 return (lang == DW_LANG_Fortran77
4547 || lang == DW_LANG_Fortran90
4548 || lang == DW_LANG_Fortran95);
4551 /* Return TRUE if the language is Ada. */
4553 static inline bool
4554 is_ada (void)
4556 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4558 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4561 /* Remove the specified attribute if present. */
4563 static void
4564 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4566 dw_attr_ref a;
4567 unsigned ix;
4569 if (! die)
4570 return;
4572 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4573 if (a->dw_attr == attr_kind)
4575 if (AT_class (a) == dw_val_class_str)
4576 if (a->dw_attr_val.v.val_str->refcount)
4577 a->dw_attr_val.v.val_str->refcount--;
4579 /* vec::ordered_remove should help reduce the number of abbrevs
4580 that are needed. */
4581 die->die_attr->ordered_remove (ix);
4582 return;
4586 /* Remove CHILD from its parent. PREV must have the property that
4587 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4589 static void
4590 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4592 gcc_assert (child->die_parent == prev->die_parent);
4593 gcc_assert (prev->die_sib == child);
4594 if (prev == child)
4596 gcc_assert (child->die_parent->die_child == child);
4597 prev = NULL;
4599 else
4600 prev->die_sib = child->die_sib;
4601 if (child->die_parent->die_child == child)
4602 child->die_parent->die_child = prev;
4605 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4606 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4608 static void
4609 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4611 dw_die_ref parent = old_child->die_parent;
4613 gcc_assert (parent == prev->die_parent);
4614 gcc_assert (prev->die_sib == old_child);
4616 new_child->die_parent = parent;
4617 if (prev == old_child)
4619 gcc_assert (parent->die_child == old_child);
4620 new_child->die_sib = new_child;
4622 else
4624 prev->die_sib = new_child;
4625 new_child->die_sib = old_child->die_sib;
4627 if (old_child->die_parent->die_child == old_child)
4628 old_child->die_parent->die_child = new_child;
4631 /* Move all children from OLD_PARENT to NEW_PARENT. */
4633 static void
4634 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4636 dw_die_ref c;
4637 new_parent->die_child = old_parent->die_child;
4638 old_parent->die_child = NULL;
4639 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4642 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4643 matches TAG. */
4645 static void
4646 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4648 dw_die_ref c;
4650 c = die->die_child;
4651 if (c) do {
4652 dw_die_ref prev = c;
4653 c = c->die_sib;
4654 while (c->die_tag == tag)
4656 remove_child_with_prev (c, prev);
4657 /* Might have removed every child. */
4658 if (c == c->die_sib)
4659 return;
4660 c = c->die_sib;
4662 } while (c != die->die_child);
4665 /* Add a CHILD_DIE as the last child of DIE. */
4667 static void
4668 add_child_die (dw_die_ref die, dw_die_ref child_die)
4670 /* FIXME this should probably be an assert. */
4671 if (! die || ! child_die)
4672 return;
4673 gcc_assert (die != child_die);
4675 child_die->die_parent = die;
4676 if (die->die_child)
4678 child_die->die_sib = die->die_child->die_sib;
4679 die->die_child->die_sib = child_die;
4681 else
4682 child_die->die_sib = child_die;
4683 die->die_child = child_die;
4686 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4687 is the specification, to the end of PARENT's list of children.
4688 This is done by removing and re-adding it. */
4690 static void
4691 splice_child_die (dw_die_ref parent, dw_die_ref child)
4693 dw_die_ref p;
4695 /* We want the declaration DIE from inside the class, not the
4696 specification DIE at toplevel. */
4697 if (child->die_parent != parent)
4699 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4701 if (tmp)
4702 child = tmp;
4705 gcc_assert (child->die_parent == parent
4706 || (child->die_parent
4707 == get_AT_ref (parent, DW_AT_specification)));
4709 for (p = child->die_parent->die_child; ; p = p->die_sib)
4710 if (p->die_sib == child)
4712 remove_child_with_prev (child, p);
4713 break;
4716 add_child_die (parent, child);
4719 /* Return a pointer to a newly created DIE node. */
4721 static inline dw_die_ref
4722 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4724 dw_die_ref die = ggc_alloc_cleared_die_node ();
4726 die->die_tag = tag_value;
4728 if (parent_die != NULL)
4729 add_child_die (parent_die, die);
4730 else
4732 limbo_die_node *limbo_node;
4734 limbo_node = ggc_alloc_cleared_limbo_die_node ();
4735 limbo_node->die = die;
4736 limbo_node->created_for = t;
4737 limbo_node->next = limbo_die_list;
4738 limbo_die_list = limbo_node;
4741 return die;
4744 /* Return the DIE associated with the given type specifier. */
4746 static inline dw_die_ref
4747 lookup_type_die (tree type)
4749 return TYPE_SYMTAB_DIE (type);
4752 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4753 anonymous type named by the typedef TYPE_DIE, return the DIE of the
4754 anonymous type instead the one of the naming typedef. */
4756 static inline dw_die_ref
4757 strip_naming_typedef (tree type, dw_die_ref type_die)
4759 if (type
4760 && TREE_CODE (type) == RECORD_TYPE
4761 && type_die
4762 && type_die->die_tag == DW_TAG_typedef
4763 && is_naming_typedef_decl (TYPE_NAME (type)))
4764 type_die = get_AT_ref (type_die, DW_AT_type);
4765 return type_die;
4768 /* Like lookup_type_die, but if type is an anonymous type named by a
4769 typedef[1], return the DIE of the anonymous type instead the one of
4770 the naming typedef. This is because in gen_typedef_die, we did
4771 equate the anonymous struct named by the typedef with the DIE of
4772 the naming typedef. So by default, lookup_type_die on an anonymous
4773 struct yields the DIE of the naming typedef.
4775 [1]: Read the comment of is_naming_typedef_decl to learn about what
4776 a naming typedef is. */
4778 static inline dw_die_ref
4779 lookup_type_die_strip_naming_typedef (tree type)
4781 dw_die_ref die = lookup_type_die (type);
4782 return strip_naming_typedef (type, die);
4785 /* Equate a DIE to a given type specifier. */
4787 static inline void
4788 equate_type_number_to_die (tree type, dw_die_ref type_die)
4790 TYPE_SYMTAB_DIE (type) = type_die;
4793 /* Returns a hash value for X (which really is a die_struct). */
4795 static hashval_t
4796 decl_die_table_hash (const void *x)
4798 return (hashval_t) ((const_dw_die_ref) x)->decl_id;
4801 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
4803 static int
4804 decl_die_table_eq (const void *x, const void *y)
4806 return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
4809 /* Return the DIE associated with a given declaration. */
4811 static inline dw_die_ref
4812 lookup_decl_die (tree decl)
4814 return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
4817 /* Returns a hash value for X (which really is a var_loc_list). */
4819 static hashval_t
4820 decl_loc_table_hash (const void *x)
4822 return (hashval_t) ((const var_loc_list *) x)->decl_id;
4825 /* Return nonzero if decl_id of var_loc_list X is the same as
4826 UID of decl *Y. */
4828 static int
4829 decl_loc_table_eq (const void *x, const void *y)
4831 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
4834 /* Return the var_loc list associated with a given declaration. */
4836 static inline var_loc_list *
4837 lookup_decl_loc (const_tree decl)
4839 if (!decl_loc_table)
4840 return NULL;
4841 return (var_loc_list *)
4842 htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
4845 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
4847 static hashval_t
4848 cached_dw_loc_list_table_hash (const void *x)
4850 return (hashval_t) ((const cached_dw_loc_list *) x)->decl_id;
4853 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
4854 UID of decl *Y. */
4856 static int
4857 cached_dw_loc_list_table_eq (const void *x, const void *y)
4859 return (((const cached_dw_loc_list *) x)->decl_id
4860 == DECL_UID ((const_tree) y));
4863 /* Equate a DIE to a particular declaration. */
4865 static void
4866 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
4868 unsigned int decl_id = DECL_UID (decl);
4869 void **slot;
4871 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
4872 *slot = decl_die;
4873 decl_die->decl_id = decl_id;
4876 /* Return how many bits covers PIECE EXPR_LIST. */
4878 static int
4879 decl_piece_bitsize (rtx piece)
4881 int ret = (int) GET_MODE (piece);
4882 if (ret)
4883 return ret;
4884 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
4885 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
4886 return INTVAL (XEXP (XEXP (piece, 0), 0));
4889 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
4891 static rtx *
4892 decl_piece_varloc_ptr (rtx piece)
4894 if ((int) GET_MODE (piece))
4895 return &XEXP (piece, 0);
4896 else
4897 return &XEXP (XEXP (piece, 0), 1);
4900 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
4901 Next is the chain of following piece nodes. */
4903 static rtx
4904 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
4906 if (bitsize <= (int) MAX_MACHINE_MODE)
4907 return alloc_EXPR_LIST (bitsize, loc_note, next);
4908 else
4909 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
4910 GEN_INT (bitsize),
4911 loc_note), next);
4914 /* Return rtx that should be stored into loc field for
4915 LOC_NOTE and BITPOS/BITSIZE. */
4917 static rtx
4918 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
4919 HOST_WIDE_INT bitsize)
4921 if (bitsize != -1)
4923 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
4924 if (bitpos != 0)
4925 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
4927 return loc_note;
4930 /* This function either modifies location piece list *DEST in
4931 place (if SRC and INNER is NULL), or copies location piece list
4932 *SRC to *DEST while modifying it. Location BITPOS is modified
4933 to contain LOC_NOTE, any pieces overlapping it are removed resp.
4934 not copied and if needed some padding around it is added.
4935 When modifying in place, DEST should point to EXPR_LIST where
4936 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
4937 to the start of the whole list and INNER points to the EXPR_LIST
4938 where earlier pieces cover PIECE_BITPOS bits. */
4940 static void
4941 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
4942 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
4943 HOST_WIDE_INT bitsize, rtx loc_note)
4945 int diff;
4946 bool copy = inner != NULL;
4948 if (copy)
4950 /* First copy all nodes preceding the current bitpos. */
4951 while (src != inner)
4953 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
4954 decl_piece_bitsize (*src), NULL_RTX);
4955 dest = &XEXP (*dest, 1);
4956 src = &XEXP (*src, 1);
4959 /* Add padding if needed. */
4960 if (bitpos != piece_bitpos)
4962 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
4963 copy ? NULL_RTX : *dest);
4964 dest = &XEXP (*dest, 1);
4966 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
4968 gcc_assert (!copy);
4969 /* A piece with correct bitpos and bitsize already exist,
4970 just update the location for it and return. */
4971 *decl_piece_varloc_ptr (*dest) = loc_note;
4972 return;
4974 /* Add the piece that changed. */
4975 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
4976 dest = &XEXP (*dest, 1);
4977 /* Skip over pieces that overlap it. */
4978 diff = bitpos - piece_bitpos + bitsize;
4979 if (!copy)
4980 src = dest;
4981 while (diff > 0 && *src)
4983 rtx piece = *src;
4984 diff -= decl_piece_bitsize (piece);
4985 if (copy)
4986 src = &XEXP (piece, 1);
4987 else
4989 *src = XEXP (piece, 1);
4990 free_EXPR_LIST_node (piece);
4993 /* Add padding if needed. */
4994 if (diff < 0 && *src)
4996 if (!copy)
4997 dest = src;
4998 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
4999 dest = &XEXP (*dest, 1);
5001 if (!copy)
5002 return;
5003 /* Finally copy all nodes following it. */
5004 while (*src)
5006 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5007 decl_piece_bitsize (*src), NULL_RTX);
5008 dest = &XEXP (*dest, 1);
5009 src = &XEXP (*src, 1);
5013 /* Add a variable location node to the linked list for DECL. */
5015 static struct var_loc_node *
5016 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5018 unsigned int decl_id;
5019 var_loc_list *temp;
5020 void **slot;
5021 struct var_loc_node *loc = NULL;
5022 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5024 if (DECL_DEBUG_EXPR_IS_FROM (decl))
5026 tree realdecl = DECL_DEBUG_EXPR (decl);
5027 if (realdecl
5028 && (handled_component_p (realdecl)
5029 || (TREE_CODE (realdecl) == MEM_REF
5030 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR)))
5032 HOST_WIDE_INT maxsize;
5033 tree innerdecl;
5034 innerdecl
5035 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5036 if (!DECL_P (innerdecl)
5037 || DECL_IGNORED_P (innerdecl)
5038 || TREE_STATIC (innerdecl)
5039 || bitsize <= 0
5040 || bitpos + bitsize > 256
5041 || bitsize != maxsize)
5042 return NULL;
5043 decl = innerdecl;
5047 decl_id = DECL_UID (decl);
5048 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5049 if (*slot == NULL)
5051 temp = ggc_alloc_cleared_var_loc_list ();
5052 temp->decl_id = decl_id;
5053 *slot = temp;
5055 else
5056 temp = (var_loc_list *) *slot;
5058 /* For PARM_DECLs try to keep around the original incoming value,
5059 even if that means we'll emit a zero-range .debug_loc entry. */
5060 if (temp->last
5061 && temp->first == temp->last
5062 && TREE_CODE (decl) == PARM_DECL
5063 && GET_CODE (temp->first->loc) == NOTE
5064 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5065 && DECL_INCOMING_RTL (decl)
5066 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5067 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5068 == GET_CODE (DECL_INCOMING_RTL (decl))
5069 && prev_real_insn (temp->first->loc) == NULL_RTX
5070 && (bitsize != -1
5071 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5072 NOTE_VAR_LOCATION_LOC (loc_note))
5073 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5074 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5076 loc = ggc_alloc_cleared_var_loc_node ();
5077 temp->first->next = loc;
5078 temp->last = loc;
5079 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5081 else if (temp->last)
5083 struct var_loc_node *last = temp->last, *unused = NULL;
5084 rtx *piece_loc = NULL, last_loc_note;
5085 int piece_bitpos = 0;
5086 if (last->next)
5088 last = last->next;
5089 gcc_assert (last->next == NULL);
5091 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5093 piece_loc = &last->loc;
5096 int cur_bitsize = decl_piece_bitsize (*piece_loc);
5097 if (piece_bitpos + cur_bitsize > bitpos)
5098 break;
5099 piece_bitpos += cur_bitsize;
5100 piece_loc = &XEXP (*piece_loc, 1);
5102 while (*piece_loc);
5104 /* TEMP->LAST here is either pointer to the last but one or
5105 last element in the chained list, LAST is pointer to the
5106 last element. */
5107 if (label && strcmp (last->label, label) == 0)
5109 /* For SRA optimized variables if there weren't any real
5110 insns since last note, just modify the last node. */
5111 if (piece_loc != NULL)
5113 adjust_piece_list (piece_loc, NULL, NULL,
5114 bitpos, piece_bitpos, bitsize, loc_note);
5115 return NULL;
5117 /* If the last note doesn't cover any instructions, remove it. */
5118 if (temp->last != last)
5120 temp->last->next = NULL;
5121 unused = last;
5122 last = temp->last;
5123 gcc_assert (strcmp (last->label, label) != 0);
5125 else
5127 gcc_assert (temp->first == temp->last
5128 || (temp->first->next == temp->last
5129 && TREE_CODE (decl) == PARM_DECL));
5130 memset (temp->last, '\0', sizeof (*temp->last));
5131 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5132 return temp->last;
5135 if (bitsize == -1 && NOTE_P (last->loc))
5136 last_loc_note = last->loc;
5137 else if (piece_loc != NULL
5138 && *piece_loc != NULL_RTX
5139 && piece_bitpos == bitpos
5140 && decl_piece_bitsize (*piece_loc) == bitsize)
5141 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5142 else
5143 last_loc_note = NULL_RTX;
5144 /* If the current location is the same as the end of the list,
5145 and either both or neither of the locations is uninitialized,
5146 we have nothing to do. */
5147 if (last_loc_note == NULL_RTX
5148 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5149 NOTE_VAR_LOCATION_LOC (loc_note)))
5150 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5151 != NOTE_VAR_LOCATION_STATUS (loc_note))
5152 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5153 == VAR_INIT_STATUS_UNINITIALIZED)
5154 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5155 == VAR_INIT_STATUS_UNINITIALIZED))))
5157 /* Add LOC to the end of list and update LAST. If the last
5158 element of the list has been removed above, reuse its
5159 memory for the new node, otherwise allocate a new one. */
5160 if (unused)
5162 loc = unused;
5163 memset (loc, '\0', sizeof (*loc));
5165 else
5166 loc = ggc_alloc_cleared_var_loc_node ();
5167 if (bitsize == -1 || piece_loc == NULL)
5168 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5169 else
5170 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5171 bitpos, piece_bitpos, bitsize, loc_note);
5172 last->next = loc;
5173 /* Ensure TEMP->LAST will point either to the new last but one
5174 element of the chain, or to the last element in it. */
5175 if (last != temp->last)
5176 temp->last = last;
5178 else if (unused)
5179 ggc_free (unused);
5181 else
5183 loc = ggc_alloc_cleared_var_loc_node ();
5184 temp->first = loc;
5185 temp->last = loc;
5186 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5188 return loc;
5191 /* Keep track of the number of spaces used to indent the
5192 output of the debugging routines that print the structure of
5193 the DIE internal representation. */
5194 static int print_indent;
5196 /* Indent the line the number of spaces given by print_indent. */
5198 static inline void
5199 print_spaces (FILE *outfile)
5201 fprintf (outfile, "%*s", print_indent, "");
5204 /* Print a type signature in hex. */
5206 static inline void
5207 print_signature (FILE *outfile, char *sig)
5209 int i;
5211 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5212 fprintf (outfile, "%02x", sig[i] & 0xff);
5215 /* Print the information associated with a given DIE, and its children.
5216 This routine is a debugging aid only. */
5218 static void
5219 print_die (dw_die_ref die, FILE *outfile)
5221 dw_attr_ref a;
5222 dw_die_ref c;
5223 unsigned ix;
5225 print_spaces (outfile);
5226 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5227 die->die_offset, dwarf_tag_name (die->die_tag),
5228 (void*) die);
5229 print_spaces (outfile);
5230 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5231 fprintf (outfile, " offset: %ld", die->die_offset);
5232 fprintf (outfile, " mark: %d\n", die->die_mark);
5234 if (die->comdat_type_p)
5236 print_spaces (outfile);
5237 fprintf (outfile, " signature: ");
5238 print_signature (outfile, die->die_id.die_type_node->signature);
5239 fprintf (outfile, "\n");
5242 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5244 print_spaces (outfile);
5245 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5247 switch (AT_class (a))
5249 case dw_val_class_addr:
5250 fprintf (outfile, "address");
5251 break;
5252 case dw_val_class_offset:
5253 fprintf (outfile, "offset");
5254 break;
5255 case dw_val_class_loc:
5256 fprintf (outfile, "location descriptor");
5257 break;
5258 case dw_val_class_loc_list:
5259 fprintf (outfile, "location list -> label:%s",
5260 AT_loc_list (a)->ll_symbol);
5261 break;
5262 case dw_val_class_range_list:
5263 fprintf (outfile, "range list");
5264 break;
5265 case dw_val_class_const:
5266 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5267 break;
5268 case dw_val_class_unsigned_const:
5269 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5270 break;
5271 case dw_val_class_const_double:
5272 fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
5273 HOST_WIDE_INT_PRINT_UNSIGNED")",
5274 a->dw_attr_val.v.val_double.high,
5275 a->dw_attr_val.v.val_double.low);
5276 break;
5277 case dw_val_class_vec:
5278 fprintf (outfile, "floating-point or vector constant");
5279 break;
5280 case dw_val_class_flag:
5281 fprintf (outfile, "%u", AT_flag (a));
5282 break;
5283 case dw_val_class_die_ref:
5284 if (AT_ref (a) != NULL)
5286 if (AT_ref (a)->comdat_type_p)
5288 fprintf (outfile, "die -> signature: ");
5289 print_signature (outfile,
5290 AT_ref (a)->die_id.die_type_node->signature);
5292 else if (AT_ref (a)->die_id.die_symbol)
5293 fprintf (outfile, "die -> label: %s",
5294 AT_ref (a)->die_id.die_symbol);
5295 else
5296 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5297 fprintf (outfile, " (%p)", (void *) AT_ref (a));
5299 else
5300 fprintf (outfile, "die -> <null>");
5301 break;
5302 case dw_val_class_vms_delta:
5303 fprintf (outfile, "delta: @slotcount(%s-%s)",
5304 AT_vms_delta2 (a), AT_vms_delta1 (a));
5305 break;
5306 case dw_val_class_lbl_id:
5307 case dw_val_class_lineptr:
5308 case dw_val_class_macptr:
5309 case dw_val_class_high_pc:
5310 fprintf (outfile, "label: %s", AT_lbl (a));
5311 break;
5312 case dw_val_class_str:
5313 if (AT_string (a) != NULL)
5314 fprintf (outfile, "\"%s\"", AT_string (a));
5315 else
5316 fprintf (outfile, "<null>");
5317 break;
5318 case dw_val_class_file:
5319 fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5320 AT_file (a)->emitted_number);
5321 break;
5322 case dw_val_class_data8:
5324 int i;
5326 for (i = 0; i < 8; i++)
5327 fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
5328 break;
5330 default:
5331 break;
5334 fprintf (outfile, "\n");
5337 if (die->die_child != NULL)
5339 print_indent += 4;
5340 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5341 print_indent -= 4;
5343 if (print_indent == 0)
5344 fprintf (outfile, "\n");
5347 /* Print the information collected for a given DIE. */
5349 DEBUG_FUNCTION void
5350 debug_dwarf_die (dw_die_ref die)
5352 print_die (die, stderr);
5355 /* Print all DWARF information collected for the compilation unit.
5356 This routine is a debugging aid only. */
5358 DEBUG_FUNCTION void
5359 debug_dwarf (void)
5361 print_indent = 0;
5362 print_die (comp_unit_die (), stderr);
5365 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5366 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5367 DIE that marks the start of the DIEs for this include file. */
5369 static dw_die_ref
5370 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5372 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5373 dw_die_ref new_unit = gen_compile_unit_die (filename);
5375 new_unit->die_sib = old_unit;
5376 return new_unit;
5379 /* Close an include-file CU and reopen the enclosing one. */
5381 static dw_die_ref
5382 pop_compile_unit (dw_die_ref old_unit)
5384 dw_die_ref new_unit = old_unit->die_sib;
5386 old_unit->die_sib = NULL;
5387 return new_unit;
5390 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5391 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5393 /* Calculate the checksum of a location expression. */
5395 static inline void
5396 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5398 int tem;
5400 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5401 CHECKSUM (tem);
5402 CHECKSUM (loc->dw_loc_oprnd1);
5403 CHECKSUM (loc->dw_loc_oprnd2);
5406 /* Calculate the checksum of an attribute. */
5408 static void
5409 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5411 dw_loc_descr_ref loc;
5412 rtx r;
5414 CHECKSUM (at->dw_attr);
5416 /* We don't care that this was compiled with a different compiler
5417 snapshot; if the output is the same, that's what matters. */
5418 if (at->dw_attr == DW_AT_producer)
5419 return;
5421 switch (AT_class (at))
5423 case dw_val_class_const:
5424 CHECKSUM (at->dw_attr_val.v.val_int);
5425 break;
5426 case dw_val_class_unsigned_const:
5427 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5428 break;
5429 case dw_val_class_const_double:
5430 CHECKSUM (at->dw_attr_val.v.val_double);
5431 break;
5432 case dw_val_class_vec:
5433 CHECKSUM (at->dw_attr_val.v.val_vec);
5434 break;
5435 case dw_val_class_flag:
5436 CHECKSUM (at->dw_attr_val.v.val_flag);
5437 break;
5438 case dw_val_class_str:
5439 CHECKSUM_STRING (AT_string (at));
5440 break;
5442 case dw_val_class_addr:
5443 r = AT_addr (at);
5444 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5445 CHECKSUM_STRING (XSTR (r, 0));
5446 break;
5448 case dw_val_class_offset:
5449 CHECKSUM (at->dw_attr_val.v.val_offset);
5450 break;
5452 case dw_val_class_loc:
5453 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5454 loc_checksum (loc, ctx);
5455 break;
5457 case dw_val_class_die_ref:
5458 die_checksum (AT_ref (at), ctx, mark);
5459 break;
5461 case dw_val_class_fde_ref:
5462 case dw_val_class_vms_delta:
5463 case dw_val_class_lbl_id:
5464 case dw_val_class_lineptr:
5465 case dw_val_class_macptr:
5466 case dw_val_class_high_pc:
5467 break;
5469 case dw_val_class_file:
5470 CHECKSUM_STRING (AT_file (at)->filename);
5471 break;
5473 case dw_val_class_data8:
5474 CHECKSUM (at->dw_attr_val.v.val_data8);
5475 break;
5477 default:
5478 break;
5482 /* Calculate the checksum of a DIE. */
5484 static void
5485 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5487 dw_die_ref c;
5488 dw_attr_ref a;
5489 unsigned ix;
5491 /* To avoid infinite recursion. */
5492 if (die->die_mark)
5494 CHECKSUM (die->die_mark);
5495 return;
5497 die->die_mark = ++(*mark);
5499 CHECKSUM (die->die_tag);
5501 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5502 attr_checksum (a, ctx, mark);
5504 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5507 #undef CHECKSUM
5508 #undef CHECKSUM_STRING
5510 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
5511 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5512 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5513 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5514 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5515 #define CHECKSUM_ATTR(FOO) \
5516 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5518 /* Calculate the checksum of a number in signed LEB128 format. */
5520 static void
5521 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5523 unsigned char byte;
5524 bool more;
5526 while (1)
5528 byte = (value & 0x7f);
5529 value >>= 7;
5530 more = !((value == 0 && (byte & 0x40) == 0)
5531 || (value == -1 && (byte & 0x40) != 0));
5532 if (more)
5533 byte |= 0x80;
5534 CHECKSUM (byte);
5535 if (!more)
5536 break;
5540 /* Calculate the checksum of a number in unsigned LEB128 format. */
5542 static void
5543 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5545 while (1)
5547 unsigned char byte = (value & 0x7f);
5548 value >>= 7;
5549 if (value != 0)
5550 /* More bytes to follow. */
5551 byte |= 0x80;
5552 CHECKSUM (byte);
5553 if (value == 0)
5554 break;
5558 /* Checksum the context of the DIE. This adds the names of any
5559 surrounding namespaces or structures to the checksum. */
5561 static void
5562 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5564 const char *name;
5565 dw_die_ref spec;
5566 int tag = die->die_tag;
5568 if (tag != DW_TAG_namespace
5569 && tag != DW_TAG_structure_type
5570 && tag != DW_TAG_class_type)
5571 return;
5573 name = get_AT_string (die, DW_AT_name);
5575 spec = get_AT_ref (die, DW_AT_specification);
5576 if (spec != NULL)
5577 die = spec;
5579 if (die->die_parent != NULL)
5580 checksum_die_context (die->die_parent, ctx);
5582 CHECKSUM_ULEB128 ('C');
5583 CHECKSUM_ULEB128 (tag);
5584 if (name != NULL)
5585 CHECKSUM_STRING (name);
5588 /* Calculate the checksum of a location expression. */
5590 static inline void
5591 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5593 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5594 were emitted as a DW_FORM_sdata instead of a location expression. */
5595 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5597 CHECKSUM_ULEB128 (DW_FORM_sdata);
5598 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5599 return;
5602 /* Otherwise, just checksum the raw location expression. */
5603 while (loc != NULL)
5605 CHECKSUM_ULEB128 (loc->dw_loc_opc);
5606 CHECKSUM (loc->dw_loc_oprnd1);
5607 CHECKSUM (loc->dw_loc_oprnd2);
5608 loc = loc->dw_loc_next;
5612 /* Calculate the checksum of an attribute. */
5614 static void
5615 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5616 struct md5_ctx *ctx, int *mark)
5618 dw_loc_descr_ref loc;
5619 rtx r;
5621 if (AT_class (at) == dw_val_class_die_ref)
5623 dw_die_ref target_die = AT_ref (at);
5625 /* For pointer and reference types, we checksum only the (qualified)
5626 name of the target type (if there is a name). For friend entries,
5627 we checksum only the (qualified) name of the target type or function.
5628 This allows the checksum to remain the same whether the target type
5629 is complete or not. */
5630 if ((at->dw_attr == DW_AT_type
5631 && (tag == DW_TAG_pointer_type
5632 || tag == DW_TAG_reference_type
5633 || tag == DW_TAG_rvalue_reference_type
5634 || tag == DW_TAG_ptr_to_member_type))
5635 || (at->dw_attr == DW_AT_friend
5636 && tag == DW_TAG_friend))
5638 dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5640 if (name_attr != NULL)
5642 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5644 if (decl == NULL)
5645 decl = target_die;
5646 CHECKSUM_ULEB128 ('N');
5647 CHECKSUM_ULEB128 (at->dw_attr);
5648 if (decl->die_parent != NULL)
5649 checksum_die_context (decl->die_parent, ctx);
5650 CHECKSUM_ULEB128 ('E');
5651 CHECKSUM_STRING (AT_string (name_attr));
5652 return;
5656 /* For all other references to another DIE, we check to see if the
5657 target DIE has already been visited. If it has, we emit a
5658 backward reference; if not, we descend recursively. */
5659 if (target_die->die_mark > 0)
5661 CHECKSUM_ULEB128 ('R');
5662 CHECKSUM_ULEB128 (at->dw_attr);
5663 CHECKSUM_ULEB128 (target_die->die_mark);
5665 else
5667 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5669 if (decl == NULL)
5670 decl = target_die;
5671 target_die->die_mark = ++(*mark);
5672 CHECKSUM_ULEB128 ('T');
5673 CHECKSUM_ULEB128 (at->dw_attr);
5674 if (decl->die_parent != NULL)
5675 checksum_die_context (decl->die_parent, ctx);
5676 die_checksum_ordered (target_die, ctx, mark);
5678 return;
5681 CHECKSUM_ULEB128 ('A');
5682 CHECKSUM_ULEB128 (at->dw_attr);
5684 switch (AT_class (at))
5686 case dw_val_class_const:
5687 CHECKSUM_ULEB128 (DW_FORM_sdata);
5688 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
5689 break;
5691 case dw_val_class_unsigned_const:
5692 CHECKSUM_ULEB128 (DW_FORM_sdata);
5693 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
5694 break;
5696 case dw_val_class_const_double:
5697 CHECKSUM_ULEB128 (DW_FORM_block);
5698 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
5699 CHECKSUM (at->dw_attr_val.v.val_double);
5700 break;
5702 case dw_val_class_vec:
5703 CHECKSUM_ULEB128 (DW_FORM_block);
5704 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
5705 CHECKSUM (at->dw_attr_val.v.val_vec);
5706 break;
5708 case dw_val_class_flag:
5709 CHECKSUM_ULEB128 (DW_FORM_flag);
5710 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
5711 break;
5713 case dw_val_class_str:
5714 CHECKSUM_ULEB128 (DW_FORM_string);
5715 CHECKSUM_STRING (AT_string (at));
5716 break;
5718 case dw_val_class_addr:
5719 r = AT_addr (at);
5720 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5721 CHECKSUM_ULEB128 (DW_FORM_string);
5722 CHECKSUM_STRING (XSTR (r, 0));
5723 break;
5725 case dw_val_class_offset:
5726 CHECKSUM_ULEB128 (DW_FORM_sdata);
5727 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
5728 break;
5730 case dw_val_class_loc:
5731 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5732 loc_checksum_ordered (loc, ctx);
5733 break;
5735 case dw_val_class_fde_ref:
5736 case dw_val_class_lbl_id:
5737 case dw_val_class_lineptr:
5738 case dw_val_class_macptr:
5739 case dw_val_class_high_pc:
5740 break;
5742 case dw_val_class_file:
5743 CHECKSUM_ULEB128 (DW_FORM_string);
5744 CHECKSUM_STRING (AT_file (at)->filename);
5745 break;
5747 case dw_val_class_data8:
5748 CHECKSUM (at->dw_attr_val.v.val_data8);
5749 break;
5751 default:
5752 break;
5756 struct checksum_attributes
5758 dw_attr_ref at_name;
5759 dw_attr_ref at_type;
5760 dw_attr_ref at_friend;
5761 dw_attr_ref at_accessibility;
5762 dw_attr_ref at_address_class;
5763 dw_attr_ref at_allocated;
5764 dw_attr_ref at_artificial;
5765 dw_attr_ref at_associated;
5766 dw_attr_ref at_binary_scale;
5767 dw_attr_ref at_bit_offset;
5768 dw_attr_ref at_bit_size;
5769 dw_attr_ref at_bit_stride;
5770 dw_attr_ref at_byte_size;
5771 dw_attr_ref at_byte_stride;
5772 dw_attr_ref at_const_value;
5773 dw_attr_ref at_containing_type;
5774 dw_attr_ref at_count;
5775 dw_attr_ref at_data_location;
5776 dw_attr_ref at_data_member_location;
5777 dw_attr_ref at_decimal_scale;
5778 dw_attr_ref at_decimal_sign;
5779 dw_attr_ref at_default_value;
5780 dw_attr_ref at_digit_count;
5781 dw_attr_ref at_discr;
5782 dw_attr_ref at_discr_list;
5783 dw_attr_ref at_discr_value;
5784 dw_attr_ref at_encoding;
5785 dw_attr_ref at_endianity;
5786 dw_attr_ref at_explicit;
5787 dw_attr_ref at_is_optional;
5788 dw_attr_ref at_location;
5789 dw_attr_ref at_lower_bound;
5790 dw_attr_ref at_mutable;
5791 dw_attr_ref at_ordering;
5792 dw_attr_ref at_picture_string;
5793 dw_attr_ref at_prototyped;
5794 dw_attr_ref at_small;
5795 dw_attr_ref at_segment;
5796 dw_attr_ref at_string_length;
5797 dw_attr_ref at_threads_scaled;
5798 dw_attr_ref at_upper_bound;
5799 dw_attr_ref at_use_location;
5800 dw_attr_ref at_use_UTF8;
5801 dw_attr_ref at_variable_parameter;
5802 dw_attr_ref at_virtuality;
5803 dw_attr_ref at_visibility;
5804 dw_attr_ref at_vtable_elem_location;
5807 /* Collect the attributes that we will want to use for the checksum. */
5809 static void
5810 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
5812 dw_attr_ref a;
5813 unsigned ix;
5815 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5817 switch (a->dw_attr)
5819 case DW_AT_name:
5820 attrs->at_name = a;
5821 break;
5822 case DW_AT_type:
5823 attrs->at_type = a;
5824 break;
5825 case DW_AT_friend:
5826 attrs->at_friend = a;
5827 break;
5828 case DW_AT_accessibility:
5829 attrs->at_accessibility = a;
5830 break;
5831 case DW_AT_address_class:
5832 attrs->at_address_class = a;
5833 break;
5834 case DW_AT_allocated:
5835 attrs->at_allocated = a;
5836 break;
5837 case DW_AT_artificial:
5838 attrs->at_artificial = a;
5839 break;
5840 case DW_AT_associated:
5841 attrs->at_associated = a;
5842 break;
5843 case DW_AT_binary_scale:
5844 attrs->at_binary_scale = a;
5845 break;
5846 case DW_AT_bit_offset:
5847 attrs->at_bit_offset = a;
5848 break;
5849 case DW_AT_bit_size:
5850 attrs->at_bit_size = a;
5851 break;
5852 case DW_AT_bit_stride:
5853 attrs->at_bit_stride = a;
5854 break;
5855 case DW_AT_byte_size:
5856 attrs->at_byte_size = a;
5857 break;
5858 case DW_AT_byte_stride:
5859 attrs->at_byte_stride = a;
5860 break;
5861 case DW_AT_const_value:
5862 attrs->at_const_value = a;
5863 break;
5864 case DW_AT_containing_type:
5865 attrs->at_containing_type = a;
5866 break;
5867 case DW_AT_count:
5868 attrs->at_count = a;
5869 break;
5870 case DW_AT_data_location:
5871 attrs->at_data_location = a;
5872 break;
5873 case DW_AT_data_member_location:
5874 attrs->at_data_member_location = a;
5875 break;
5876 case DW_AT_decimal_scale:
5877 attrs->at_decimal_scale = a;
5878 break;
5879 case DW_AT_decimal_sign:
5880 attrs->at_decimal_sign = a;
5881 break;
5882 case DW_AT_default_value:
5883 attrs->at_default_value = a;
5884 break;
5885 case DW_AT_digit_count:
5886 attrs->at_digit_count = a;
5887 break;
5888 case DW_AT_discr:
5889 attrs->at_discr = a;
5890 break;
5891 case DW_AT_discr_list:
5892 attrs->at_discr_list = a;
5893 break;
5894 case DW_AT_discr_value:
5895 attrs->at_discr_value = a;
5896 break;
5897 case DW_AT_encoding:
5898 attrs->at_encoding = a;
5899 break;
5900 case DW_AT_endianity:
5901 attrs->at_endianity = a;
5902 break;
5903 case DW_AT_explicit:
5904 attrs->at_explicit = a;
5905 break;
5906 case DW_AT_is_optional:
5907 attrs->at_is_optional = a;
5908 break;
5909 case DW_AT_location:
5910 attrs->at_location = a;
5911 break;
5912 case DW_AT_lower_bound:
5913 attrs->at_lower_bound = a;
5914 break;
5915 case DW_AT_mutable:
5916 attrs->at_mutable = a;
5917 break;
5918 case DW_AT_ordering:
5919 attrs->at_ordering = a;
5920 break;
5921 case DW_AT_picture_string:
5922 attrs->at_picture_string = a;
5923 break;
5924 case DW_AT_prototyped:
5925 attrs->at_prototyped = a;
5926 break;
5927 case DW_AT_small:
5928 attrs->at_small = a;
5929 break;
5930 case DW_AT_segment:
5931 attrs->at_segment = a;
5932 break;
5933 case DW_AT_string_length:
5934 attrs->at_string_length = a;
5935 break;
5936 case DW_AT_threads_scaled:
5937 attrs->at_threads_scaled = a;
5938 break;
5939 case DW_AT_upper_bound:
5940 attrs->at_upper_bound = a;
5941 break;
5942 case DW_AT_use_location:
5943 attrs->at_use_location = a;
5944 break;
5945 case DW_AT_use_UTF8:
5946 attrs->at_use_UTF8 = a;
5947 break;
5948 case DW_AT_variable_parameter:
5949 attrs->at_variable_parameter = a;
5950 break;
5951 case DW_AT_virtuality:
5952 attrs->at_virtuality = a;
5953 break;
5954 case DW_AT_visibility:
5955 attrs->at_visibility = a;
5956 break;
5957 case DW_AT_vtable_elem_location:
5958 attrs->at_vtable_elem_location = a;
5959 break;
5960 default:
5961 break;
5966 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
5968 static void
5969 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5971 dw_die_ref c;
5972 dw_die_ref decl;
5973 struct checksum_attributes attrs;
5975 CHECKSUM_ULEB128 ('D');
5976 CHECKSUM_ULEB128 (die->die_tag);
5978 memset (&attrs, 0, sizeof (attrs));
5980 decl = get_AT_ref (die, DW_AT_specification);
5981 if (decl != NULL)
5982 collect_checksum_attributes (&attrs, decl);
5983 collect_checksum_attributes (&attrs, die);
5985 CHECKSUM_ATTR (attrs.at_name);
5986 CHECKSUM_ATTR (attrs.at_accessibility);
5987 CHECKSUM_ATTR (attrs.at_address_class);
5988 CHECKSUM_ATTR (attrs.at_allocated);
5989 CHECKSUM_ATTR (attrs.at_artificial);
5990 CHECKSUM_ATTR (attrs.at_associated);
5991 CHECKSUM_ATTR (attrs.at_binary_scale);
5992 CHECKSUM_ATTR (attrs.at_bit_offset);
5993 CHECKSUM_ATTR (attrs.at_bit_size);
5994 CHECKSUM_ATTR (attrs.at_bit_stride);
5995 CHECKSUM_ATTR (attrs.at_byte_size);
5996 CHECKSUM_ATTR (attrs.at_byte_stride);
5997 CHECKSUM_ATTR (attrs.at_const_value);
5998 CHECKSUM_ATTR (attrs.at_containing_type);
5999 CHECKSUM_ATTR (attrs.at_count);
6000 CHECKSUM_ATTR (attrs.at_data_location);
6001 CHECKSUM_ATTR (attrs.at_data_member_location);
6002 CHECKSUM_ATTR (attrs.at_decimal_scale);
6003 CHECKSUM_ATTR (attrs.at_decimal_sign);
6004 CHECKSUM_ATTR (attrs.at_default_value);
6005 CHECKSUM_ATTR (attrs.at_digit_count);
6006 CHECKSUM_ATTR (attrs.at_discr);
6007 CHECKSUM_ATTR (attrs.at_discr_list);
6008 CHECKSUM_ATTR (attrs.at_discr_value);
6009 CHECKSUM_ATTR (attrs.at_encoding);
6010 CHECKSUM_ATTR (attrs.at_endianity);
6011 CHECKSUM_ATTR (attrs.at_explicit);
6012 CHECKSUM_ATTR (attrs.at_is_optional);
6013 CHECKSUM_ATTR (attrs.at_location);
6014 CHECKSUM_ATTR (attrs.at_lower_bound);
6015 CHECKSUM_ATTR (attrs.at_mutable);
6016 CHECKSUM_ATTR (attrs.at_ordering);
6017 CHECKSUM_ATTR (attrs.at_picture_string);
6018 CHECKSUM_ATTR (attrs.at_prototyped);
6019 CHECKSUM_ATTR (attrs.at_small);
6020 CHECKSUM_ATTR (attrs.at_segment);
6021 CHECKSUM_ATTR (attrs.at_string_length);
6022 CHECKSUM_ATTR (attrs.at_threads_scaled);
6023 CHECKSUM_ATTR (attrs.at_upper_bound);
6024 CHECKSUM_ATTR (attrs.at_use_location);
6025 CHECKSUM_ATTR (attrs.at_use_UTF8);
6026 CHECKSUM_ATTR (attrs.at_variable_parameter);
6027 CHECKSUM_ATTR (attrs.at_virtuality);
6028 CHECKSUM_ATTR (attrs.at_visibility);
6029 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6030 CHECKSUM_ATTR (attrs.at_type);
6031 CHECKSUM_ATTR (attrs.at_friend);
6033 /* Checksum the child DIEs, except for nested types and member functions. */
6034 c = die->die_child;
6035 if (c) do {
6036 dw_attr_ref name_attr;
6038 c = c->die_sib;
6039 name_attr = get_AT (c, DW_AT_name);
6040 if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
6041 && name_attr != NULL)
6043 CHECKSUM_ULEB128 ('S');
6044 CHECKSUM_ULEB128 (c->die_tag);
6045 CHECKSUM_STRING (AT_string (name_attr));
6047 else
6049 /* Mark this DIE so it gets processed when unmarking. */
6050 if (c->die_mark == 0)
6051 c->die_mark = -1;
6052 die_checksum_ordered (c, ctx, mark);
6054 } while (c != die->die_child);
6056 CHECKSUM_ULEB128 (0);
6059 #undef CHECKSUM
6060 #undef CHECKSUM_STRING
6061 #undef CHECKSUM_ATTR
6062 #undef CHECKSUM_LEB128
6063 #undef CHECKSUM_ULEB128
6065 /* Generate the type signature for DIE. This is computed by generating an
6066 MD5 checksum over the DIE's tag, its relevant attributes, and its
6067 children. Attributes that are references to other DIEs are processed
6068 by recursion, using the MARK field to prevent infinite recursion.
6069 If the DIE is nested inside a namespace or another type, we also
6070 need to include that context in the signature. The lower 64 bits
6071 of the resulting MD5 checksum comprise the signature. */
6073 static void
6074 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6076 int mark;
6077 const char *name;
6078 unsigned char checksum[16];
6079 struct md5_ctx ctx;
6080 dw_die_ref decl;
6081 dw_die_ref parent;
6083 name = get_AT_string (die, DW_AT_name);
6084 decl = get_AT_ref (die, DW_AT_specification);
6085 parent = get_die_parent (die);
6087 /* First, compute a signature for just the type name (and its surrounding
6088 context, if any. This is stored in the type unit DIE for link-time
6089 ODR (one-definition rule) checking. */
6091 if (is_cxx() && name != NULL)
6093 md5_init_ctx (&ctx);
6095 /* Checksum the names of surrounding namespaces and structures. */
6096 if (parent != NULL)
6097 checksum_die_context (parent, &ctx);
6099 md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
6100 md5_process_bytes (name, strlen (name) + 1, &ctx);
6101 md5_finish_ctx (&ctx, checksum);
6103 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6106 /* Next, compute the complete type signature. */
6108 md5_init_ctx (&ctx);
6109 mark = 1;
6110 die->die_mark = mark;
6112 /* Checksum the names of surrounding namespaces and structures. */
6113 if (parent != NULL)
6114 checksum_die_context (parent, &ctx);
6116 /* Checksum the DIE and its children. */
6117 die_checksum_ordered (die, &ctx, &mark);
6118 unmark_all_dies (die);
6119 md5_finish_ctx (&ctx, checksum);
6121 /* Store the signature in the type node and link the type DIE and the
6122 type node together. */
6123 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6124 DWARF_TYPE_SIGNATURE_SIZE);
6125 die->comdat_type_p = true;
6126 die->die_id.die_type_node = type_node;
6127 type_node->type_die = die;
6129 /* If the DIE is a specification, link its declaration to the type node
6130 as well. */
6131 if (decl != NULL)
6133 decl->comdat_type_p = true;
6134 decl->die_id.die_type_node = type_node;
6138 /* Do the location expressions look same? */
6139 static inline int
6140 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6142 return loc1->dw_loc_opc == loc2->dw_loc_opc
6143 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6144 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6147 /* Do the values look the same? */
6148 static int
6149 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6151 dw_loc_descr_ref loc1, loc2;
6152 rtx r1, r2;
6154 if (v1->val_class != v2->val_class)
6155 return 0;
6157 switch (v1->val_class)
6159 case dw_val_class_const:
6160 return v1->v.val_int == v2->v.val_int;
6161 case dw_val_class_unsigned_const:
6162 return v1->v.val_unsigned == v2->v.val_unsigned;
6163 case dw_val_class_const_double:
6164 return v1->v.val_double.high == v2->v.val_double.high
6165 && v1->v.val_double.low == v2->v.val_double.low;
6166 case dw_val_class_vec:
6167 if (v1->v.val_vec.length != v2->v.val_vec.length
6168 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6169 return 0;
6170 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6171 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6172 return 0;
6173 return 1;
6174 case dw_val_class_flag:
6175 return v1->v.val_flag == v2->v.val_flag;
6176 case dw_val_class_str:
6177 return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
6179 case dw_val_class_addr:
6180 r1 = v1->v.val_addr;
6181 r2 = v2->v.val_addr;
6182 if (GET_CODE (r1) != GET_CODE (r2))
6183 return 0;
6184 return !rtx_equal_p (r1, r2);
6186 case dw_val_class_offset:
6187 return v1->v.val_offset == v2->v.val_offset;
6189 case dw_val_class_loc:
6190 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6191 loc1 && loc2;
6192 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6193 if (!same_loc_p (loc1, loc2, mark))
6194 return 0;
6195 return !loc1 && !loc2;
6197 case dw_val_class_die_ref:
6198 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6200 case dw_val_class_fde_ref:
6201 case dw_val_class_vms_delta:
6202 case dw_val_class_lbl_id:
6203 case dw_val_class_lineptr:
6204 case dw_val_class_macptr:
6205 case dw_val_class_high_pc:
6206 return 1;
6208 case dw_val_class_file:
6209 return v1->v.val_file == v2->v.val_file;
6211 case dw_val_class_data8:
6212 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6214 default:
6215 return 1;
6219 /* Do the attributes look the same? */
6221 static int
6222 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6224 if (at1->dw_attr != at2->dw_attr)
6225 return 0;
6227 /* We don't care that this was compiled with a different compiler
6228 snapshot; if the output is the same, that's what matters. */
6229 if (at1->dw_attr == DW_AT_producer)
6230 return 1;
6232 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6235 /* Do the dies look the same? */
6237 static int
6238 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6240 dw_die_ref c1, c2;
6241 dw_attr_ref a1;
6242 unsigned ix;
6244 /* To avoid infinite recursion. */
6245 if (die1->die_mark)
6246 return die1->die_mark == die2->die_mark;
6247 die1->die_mark = die2->die_mark = ++(*mark);
6249 if (die1->die_tag != die2->die_tag)
6250 return 0;
6252 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6253 return 0;
6255 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6256 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6257 return 0;
6259 c1 = die1->die_child;
6260 c2 = die2->die_child;
6261 if (! c1)
6263 if (c2)
6264 return 0;
6266 else
6267 for (;;)
6269 if (!same_die_p (c1, c2, mark))
6270 return 0;
6271 c1 = c1->die_sib;
6272 c2 = c2->die_sib;
6273 if (c1 == die1->die_child)
6275 if (c2 == die2->die_child)
6276 break;
6277 else
6278 return 0;
6282 return 1;
6285 /* Do the dies look the same? Wrapper around same_die_p. */
6287 static int
6288 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6290 int mark = 0;
6291 int ret = same_die_p (die1, die2, &mark);
6293 unmark_all_dies (die1);
6294 unmark_all_dies (die2);
6296 return ret;
6299 /* The prefix to attach to symbols on DIEs in the current comdat debug
6300 info section. */
6301 static const char *comdat_symbol_id;
6303 /* The index of the current symbol within the current comdat CU. */
6304 static unsigned int comdat_symbol_number;
6306 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6307 children, and set comdat_symbol_id accordingly. */
6309 static void
6310 compute_section_prefix (dw_die_ref unit_die)
6312 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6313 const char *base = die_name ? lbasename (die_name) : "anonymous";
6314 char *name = XALLOCAVEC (char, strlen (base) + 64);
6315 char *p;
6316 int i, mark;
6317 unsigned char checksum[16];
6318 struct md5_ctx ctx;
6320 /* Compute the checksum of the DIE, then append part of it as hex digits to
6321 the name filename of the unit. */
6323 md5_init_ctx (&ctx);
6324 mark = 0;
6325 die_checksum (unit_die, &ctx, &mark);
6326 unmark_all_dies (unit_die);
6327 md5_finish_ctx (&ctx, checksum);
6329 sprintf (name, "%s.", base);
6330 clean_symbol_name (name);
6332 p = name + strlen (name);
6333 for (i = 0; i < 4; i++)
6335 sprintf (p, "%.2x", checksum[i]);
6336 p += 2;
6339 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6340 comdat_symbol_number = 0;
6343 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6345 static int
6346 is_type_die (dw_die_ref die)
6348 switch (die->die_tag)
6350 case DW_TAG_array_type:
6351 case DW_TAG_class_type:
6352 case DW_TAG_interface_type:
6353 case DW_TAG_enumeration_type:
6354 case DW_TAG_pointer_type:
6355 case DW_TAG_reference_type:
6356 case DW_TAG_rvalue_reference_type:
6357 case DW_TAG_string_type:
6358 case DW_TAG_structure_type:
6359 case DW_TAG_subroutine_type:
6360 case DW_TAG_union_type:
6361 case DW_TAG_ptr_to_member_type:
6362 case DW_TAG_set_type:
6363 case DW_TAG_subrange_type:
6364 case DW_TAG_base_type:
6365 case DW_TAG_const_type:
6366 case DW_TAG_file_type:
6367 case DW_TAG_packed_type:
6368 case DW_TAG_volatile_type:
6369 case DW_TAG_typedef:
6370 return 1;
6371 default:
6372 return 0;
6376 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6377 Basically, we want to choose the bits that are likely to be shared between
6378 compilations (types) and leave out the bits that are specific to individual
6379 compilations (functions). */
6381 static int
6382 is_comdat_die (dw_die_ref c)
6384 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6385 we do for stabs. The advantage is a greater likelihood of sharing between
6386 objects that don't include headers in the same order (and therefore would
6387 put the base types in a different comdat). jason 8/28/00 */
6389 if (c->die_tag == DW_TAG_base_type)
6390 return 0;
6392 if (c->die_tag == DW_TAG_pointer_type
6393 || c->die_tag == DW_TAG_reference_type
6394 || c->die_tag == DW_TAG_rvalue_reference_type
6395 || c->die_tag == DW_TAG_const_type
6396 || c->die_tag == DW_TAG_volatile_type)
6398 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6400 return t ? is_comdat_die (t) : 0;
6403 return is_type_die (c);
6406 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6407 compilation unit. */
6409 static int
6410 is_symbol_die (dw_die_ref c)
6412 return (is_type_die (c)
6413 || is_declaration_die (c)
6414 || c->die_tag == DW_TAG_namespace
6415 || c->die_tag == DW_TAG_module);
6418 /* Returns true iff C is a compile-unit DIE. */
6420 static inline bool
6421 is_cu_die (dw_die_ref c)
6423 return c && c->die_tag == DW_TAG_compile_unit;
6426 /* Returns true iff C is a unit DIE of some sort. */
6428 static inline bool
6429 is_unit_die (dw_die_ref c)
6431 return c && (c->die_tag == DW_TAG_compile_unit
6432 || c->die_tag == DW_TAG_partial_unit
6433 || c->die_tag == DW_TAG_type_unit);
6436 /* Returns true iff C is a namespace DIE. */
6438 static inline bool
6439 is_namespace_die (dw_die_ref c)
6441 return c && c->die_tag == DW_TAG_namespace;
6444 /* Returns true iff C is a class or structure DIE. */
6446 static inline bool
6447 is_class_die (dw_die_ref c)
6449 return c && (c->die_tag == DW_TAG_class_type
6450 || c->die_tag == DW_TAG_structure_type);
6453 static char *
6454 gen_internal_sym (const char *prefix)
6456 char buf[256];
6458 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6459 return xstrdup (buf);
6462 /* Assign symbols to all worthy DIEs under DIE. */
6464 static void
6465 assign_symbol_names (dw_die_ref die)
6467 dw_die_ref c;
6469 if (is_symbol_die (die) && !die->comdat_type_p)
6471 if (comdat_symbol_id)
6473 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6475 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6476 comdat_symbol_id, comdat_symbol_number++);
6477 die->die_id.die_symbol = xstrdup (p);
6479 else
6480 die->die_id.die_symbol = gen_internal_sym ("LDIE");
6483 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6486 struct cu_hash_table_entry
6488 dw_die_ref cu;
6489 unsigned min_comdat_num, max_comdat_num;
6490 struct cu_hash_table_entry *next;
6493 /* Routines to manipulate hash table of CUs. */
6494 static hashval_t
6495 htab_cu_hash (const void *of)
6497 const struct cu_hash_table_entry *const entry =
6498 (const struct cu_hash_table_entry *) of;
6500 return htab_hash_string (entry->cu->die_id.die_symbol);
6503 static int
6504 htab_cu_eq (const void *of1, const void *of2)
6506 const struct cu_hash_table_entry *const entry1 =
6507 (const struct cu_hash_table_entry *) of1;
6508 const struct die_struct *const entry2 = (const struct die_struct *) of2;
6510 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6513 static void
6514 htab_cu_del (void *what)
6516 struct cu_hash_table_entry *next,
6517 *entry = (struct cu_hash_table_entry *) what;
6519 while (entry)
6521 next = entry->next;
6522 free (entry);
6523 entry = next;
6527 /* Check whether we have already seen this CU and set up SYM_NUM
6528 accordingly. */
6529 static int
6530 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
6532 struct cu_hash_table_entry dummy;
6533 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6535 dummy.max_comdat_num = 0;
6537 slot = (struct cu_hash_table_entry **)
6538 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
6539 INSERT);
6540 entry = *slot;
6542 for (; entry; last = entry, entry = entry->next)
6544 if (same_die_p_wrap (cu, entry->cu))
6545 break;
6548 if (entry)
6550 *sym_num = entry->min_comdat_num;
6551 return 1;
6554 entry = XCNEW (struct cu_hash_table_entry);
6555 entry->cu = cu;
6556 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6557 entry->next = *slot;
6558 *slot = entry;
6560 return 0;
6563 /* Record SYM_NUM to record of CU in HTABLE. */
6564 static void
6565 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
6567 struct cu_hash_table_entry **slot, *entry;
6569 slot = (struct cu_hash_table_entry **)
6570 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
6571 NO_INSERT);
6572 entry = *slot;
6574 entry->max_comdat_num = sym_num;
6577 /* Traverse the DIE (which is always comp_unit_die), and set up
6578 additional compilation units for each of the include files we see
6579 bracketed by BINCL/EINCL. */
6581 static void
6582 break_out_includes (dw_die_ref die)
6584 dw_die_ref c;
6585 dw_die_ref unit = NULL;
6586 limbo_die_node *node, **pnode;
6587 htab_t cu_hash_table;
6589 c = die->die_child;
6590 if (c) do {
6591 dw_die_ref prev = c;
6592 c = c->die_sib;
6593 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6594 || (unit && is_comdat_die (c)))
6596 dw_die_ref next = c->die_sib;
6598 /* This DIE is for a secondary CU; remove it from the main one. */
6599 remove_child_with_prev (c, prev);
6601 if (c->die_tag == DW_TAG_GNU_BINCL)
6602 unit = push_new_compile_unit (unit, c);
6603 else if (c->die_tag == DW_TAG_GNU_EINCL)
6604 unit = pop_compile_unit (unit);
6605 else
6606 add_child_die (unit, c);
6607 c = next;
6608 if (c == die->die_child)
6609 break;
6611 } while (c != die->die_child);
6613 #if 0
6614 /* We can only use this in debugging, since the frontend doesn't check
6615 to make sure that we leave every include file we enter. */
6616 gcc_assert (!unit);
6617 #endif
6619 assign_symbol_names (die);
6620 cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6621 for (node = limbo_die_list, pnode = &limbo_die_list;
6622 node;
6623 node = node->next)
6625 int is_dupl;
6627 compute_section_prefix (node->die);
6628 is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6629 &comdat_symbol_number);
6630 assign_symbol_names (node->die);
6631 if (is_dupl)
6632 *pnode = node->next;
6633 else
6635 pnode = &node->next;
6636 record_comdat_symbol_number (node->die, cu_hash_table,
6637 comdat_symbol_number);
6640 htab_delete (cu_hash_table);
6643 /* Return non-zero if this DIE is a declaration. */
6645 static int
6646 is_declaration_die (dw_die_ref die)
6648 dw_attr_ref a;
6649 unsigned ix;
6651 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6652 if (a->dw_attr == DW_AT_declaration)
6653 return 1;
6655 return 0;
6658 /* Return non-zero if this DIE is nested inside a subprogram. */
6660 static int
6661 is_nested_in_subprogram (dw_die_ref die)
6663 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
6665 if (decl == NULL)
6666 decl = die;
6667 return local_scope_p (decl);
6670 /* Return non-zero if this DIE contains a defining declaration of a
6671 subprogram. */
6673 static int
6674 contains_subprogram_definition (dw_die_ref die)
6676 dw_die_ref c;
6678 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
6679 return 1;
6680 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition(c)) return 1);
6681 return 0;
6684 /* Return non-zero if this is a type DIE that should be moved to a
6685 COMDAT .debug_types section. */
6687 static int
6688 should_move_die_to_comdat (dw_die_ref die)
6690 switch (die->die_tag)
6692 case DW_TAG_class_type:
6693 case DW_TAG_structure_type:
6694 case DW_TAG_enumeration_type:
6695 case DW_TAG_union_type:
6696 /* Don't move declarations, inlined instances, or types nested in a
6697 subprogram. */
6698 if (is_declaration_die (die)
6699 || get_AT (die, DW_AT_abstract_origin)
6700 || is_nested_in_subprogram (die))
6701 return 0;
6702 /* A type definition should never contain a subprogram definition. */
6703 gcc_assert (!contains_subprogram_definition (die));
6704 return 1;
6705 case DW_TAG_array_type:
6706 case DW_TAG_interface_type:
6707 case DW_TAG_pointer_type:
6708 case DW_TAG_reference_type:
6709 case DW_TAG_rvalue_reference_type:
6710 case DW_TAG_string_type:
6711 case DW_TAG_subroutine_type:
6712 case DW_TAG_ptr_to_member_type:
6713 case DW_TAG_set_type:
6714 case DW_TAG_subrange_type:
6715 case DW_TAG_base_type:
6716 case DW_TAG_const_type:
6717 case DW_TAG_file_type:
6718 case DW_TAG_packed_type:
6719 case DW_TAG_volatile_type:
6720 case DW_TAG_typedef:
6721 default:
6722 return 0;
6726 /* Make a clone of DIE. */
6728 static dw_die_ref
6729 clone_die (dw_die_ref die)
6731 dw_die_ref clone;
6732 dw_attr_ref a;
6733 unsigned ix;
6735 clone = ggc_alloc_cleared_die_node ();
6736 clone->die_tag = die->die_tag;
6738 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6739 add_dwarf_attr (clone, a);
6741 return clone;
6744 /* Make a clone of the tree rooted at DIE. */
6746 static dw_die_ref
6747 clone_tree (dw_die_ref die)
6749 dw_die_ref c;
6750 dw_die_ref clone = clone_die (die);
6752 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
6754 return clone;
6757 /* Make a clone of DIE as a declaration. */
6759 static dw_die_ref
6760 clone_as_declaration (dw_die_ref die)
6762 dw_die_ref clone;
6763 dw_die_ref decl;
6764 dw_attr_ref a;
6765 unsigned ix;
6767 /* If the DIE is already a declaration, just clone it. */
6768 if (is_declaration_die (die))
6769 return clone_die (die);
6771 /* If the DIE is a specification, just clone its declaration DIE. */
6772 decl = get_AT_ref (die, DW_AT_specification);
6773 if (decl != NULL)
6775 clone = clone_die (decl);
6776 if (die->comdat_type_p)
6777 add_AT_die_ref (clone, DW_AT_signature, die);
6778 return clone;
6781 clone = ggc_alloc_cleared_die_node ();
6782 clone->die_tag = die->die_tag;
6784 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6786 /* We don't want to copy over all attributes.
6787 For example we don't want DW_AT_byte_size because otherwise we will no
6788 longer have a declaration and GDB will treat it as a definition. */
6790 switch (a->dw_attr)
6792 case DW_AT_artificial:
6793 case DW_AT_containing_type:
6794 case DW_AT_external:
6795 case DW_AT_name:
6796 case DW_AT_type:
6797 case DW_AT_virtuality:
6798 case DW_AT_linkage_name:
6799 case DW_AT_MIPS_linkage_name:
6800 add_dwarf_attr (clone, a);
6801 break;
6802 case DW_AT_byte_size:
6803 default:
6804 break;
6808 if (die->comdat_type_p)
6809 add_AT_die_ref (clone, DW_AT_signature, die);
6811 add_AT_flag (clone, DW_AT_declaration, 1);
6812 return clone;
6815 /* Copy the declaration context to the new type unit DIE. This includes
6816 any surrounding namespace or type declarations. If the DIE has an
6817 AT_specification attribute, it also includes attributes and children
6818 attached to the specification, and returns a pointer to the original
6819 parent of the declaration DIE. Returns NULL otherwise. */
6821 static dw_die_ref
6822 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
6824 dw_die_ref decl;
6825 dw_die_ref new_decl;
6826 dw_die_ref orig_parent = NULL;
6828 decl = get_AT_ref (die, DW_AT_specification);
6829 if (decl == NULL)
6830 decl = die;
6831 else
6833 unsigned ix;
6834 dw_die_ref c;
6835 dw_attr_ref a;
6837 /* The original DIE will be changed to a declaration, and must
6838 be moved to be a child of the original declaration DIE. */
6839 orig_parent = decl->die_parent;
6841 /* Copy the type node pointer from the new DIE to the original
6842 declaration DIE so we can forward references later. */
6843 decl->comdat_type_p = true;
6844 decl->die_id.die_type_node = die->die_id.die_type_node;
6846 remove_AT (die, DW_AT_specification);
6848 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
6850 if (a->dw_attr != DW_AT_name
6851 && a->dw_attr != DW_AT_declaration
6852 && a->dw_attr != DW_AT_external)
6853 add_dwarf_attr (die, a);
6856 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
6859 if (decl->die_parent != NULL
6860 && !is_unit_die (decl->die_parent))
6862 new_decl = copy_ancestor_tree (unit, decl, NULL);
6863 if (new_decl != NULL)
6865 remove_AT (new_decl, DW_AT_signature);
6866 add_AT_specification (die, new_decl);
6870 return orig_parent;
6873 /* Generate the skeleton ancestor tree for the given NODE, then clone
6874 the DIE and add the clone into the tree. */
6876 static void
6877 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
6879 if (node->new_die != NULL)
6880 return;
6882 node->new_die = clone_as_declaration (node->old_die);
6884 if (node->parent != NULL)
6886 generate_skeleton_ancestor_tree (node->parent);
6887 add_child_die (node->parent->new_die, node->new_die);
6891 /* Generate a skeleton tree of DIEs containing any declarations that are
6892 found in the original tree. We traverse the tree looking for declaration
6893 DIEs, and construct the skeleton from the bottom up whenever we find one. */
6895 static void
6896 generate_skeleton_bottom_up (skeleton_chain_node *parent)
6898 skeleton_chain_node node;
6899 dw_die_ref c;
6900 dw_die_ref first;
6901 dw_die_ref prev = NULL;
6902 dw_die_ref next = NULL;
6904 node.parent = parent;
6906 first = c = parent->old_die->die_child;
6907 if (c)
6908 next = c->die_sib;
6909 if (c) do {
6910 if (prev == NULL || prev->die_sib == c)
6911 prev = c;
6912 c = next;
6913 next = (c == first ? NULL : c->die_sib);
6914 node.old_die = c;
6915 node.new_die = NULL;
6916 if (is_declaration_die (c))
6918 /* Clone the existing DIE, move the original to the skeleton
6919 tree (which is in the main CU), and put the clone, with
6920 all the original's children, where the original came from. */
6921 dw_die_ref clone = clone_die (c);
6922 move_all_children (c, clone);
6924 replace_child (c, clone, prev);
6925 generate_skeleton_ancestor_tree (parent);
6926 add_child_die (parent->new_die, c);
6927 node.new_die = c;
6928 c = clone;
6930 generate_skeleton_bottom_up (&node);
6931 } while (next != NULL);
6934 /* Wrapper function for generate_skeleton_bottom_up. */
6936 static dw_die_ref
6937 generate_skeleton (dw_die_ref die)
6939 skeleton_chain_node node;
6941 node.old_die = die;
6942 node.new_die = NULL;
6943 node.parent = NULL;
6945 /* If this type definition is nested inside another type,
6946 always leave at least a declaration in its place. */
6947 if (die->die_parent != NULL && is_type_die (die->die_parent))
6948 node.new_die = clone_as_declaration (die);
6950 generate_skeleton_bottom_up (&node);
6951 return node.new_die;
6954 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
6955 declaration. The original DIE is moved to a new compile unit so that
6956 existing references to it follow it to the new location. If any of the
6957 original DIE's descendants is a declaration, we need to replace the
6958 original DIE with a skeleton tree and move the declarations back into the
6959 skeleton tree. */
6961 static dw_die_ref
6962 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
6963 dw_die_ref prev)
6965 dw_die_ref skeleton, orig_parent;
6967 /* Copy the declaration context to the type unit DIE. If the returned
6968 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
6969 that DIE. */
6970 orig_parent = copy_declaration_context (unit, child);
6972 skeleton = generate_skeleton (child);
6973 if (skeleton == NULL)
6974 remove_child_with_prev (child, prev);
6975 else
6977 skeleton->comdat_type_p = true;
6978 skeleton->die_id.die_type_node = child->die_id.die_type_node;
6980 /* If the original DIE was a specification, we need to put
6981 the skeleton under the parent DIE of the declaration.
6982 This leaves the original declaration in the tree, but
6983 it will be pruned later since there are no longer any
6984 references to it. */
6985 if (orig_parent != NULL)
6987 remove_child_with_prev (child, prev);
6988 add_child_die (orig_parent, skeleton);
6990 else
6991 replace_child (child, skeleton, prev);
6994 return skeleton;
6997 /* Traverse the DIE and set up additional .debug_types sections for each
6998 type worthy of being placed in a COMDAT section. */
7000 static void
7001 break_out_comdat_types (dw_die_ref die)
7003 dw_die_ref c;
7004 dw_die_ref first;
7005 dw_die_ref prev = NULL;
7006 dw_die_ref next = NULL;
7007 dw_die_ref unit = NULL;
7009 first = c = die->die_child;
7010 if (c)
7011 next = c->die_sib;
7012 if (c) do {
7013 if (prev == NULL || prev->die_sib == c)
7014 prev = c;
7015 c = next;
7016 next = (c == first ? NULL : c->die_sib);
7017 if (should_move_die_to_comdat (c))
7019 dw_die_ref replacement;
7020 comdat_type_node_ref type_node;
7022 /* Create a new type unit DIE as the root for the new tree, and
7023 add it to the list of comdat types. */
7024 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7025 add_AT_unsigned (unit, DW_AT_language,
7026 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7027 type_node = ggc_alloc_cleared_comdat_type_node ();
7028 type_node->root_die = unit;
7029 type_node->next = comdat_type_list;
7030 comdat_type_list = type_node;
7032 /* Generate the type signature. */
7033 generate_type_signature (c, type_node);
7035 /* Copy the declaration context, attributes, and children of the
7036 declaration into the new type unit DIE, then remove this DIE
7037 from the main CU (or replace it with a skeleton if necessary). */
7038 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7039 type_node->skeleton_die = replacement;
7041 /* Break out nested types into their own type units. */
7042 break_out_comdat_types (c);
7044 /* Add the DIE to the new compunit. */
7045 add_child_die (unit, c);
7047 if (replacement != NULL)
7048 c = replacement;
7050 else if (c->die_tag == DW_TAG_namespace
7051 || c->die_tag == DW_TAG_class_type
7052 || c->die_tag == DW_TAG_structure_type
7053 || c->die_tag == DW_TAG_union_type)
7055 /* Look for nested types that can be broken out. */
7056 break_out_comdat_types (c);
7058 } while (next != NULL);
7061 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7063 struct decl_table_entry
7065 dw_die_ref orig;
7066 dw_die_ref copy;
7069 /* Routines to manipulate hash table of copied declarations. */
7071 static hashval_t
7072 htab_decl_hash (const void *of)
7074 const struct decl_table_entry *const entry =
7075 (const struct decl_table_entry *) of;
7077 return htab_hash_pointer (entry->orig);
7080 static int
7081 htab_decl_eq (const void *of1, const void *of2)
7083 const struct decl_table_entry *const entry1 =
7084 (const struct decl_table_entry *) of1;
7085 const struct die_struct *const entry2 = (const struct die_struct *) of2;
7087 return entry1->orig == entry2;
7090 static void
7091 htab_decl_del (void *what)
7093 struct decl_table_entry *entry = (struct decl_table_entry *) what;
7095 free (entry);
7098 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7099 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7100 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7101 to check if the ancestor has already been copied into UNIT. */
7103 static dw_die_ref
7104 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
7106 dw_die_ref parent = die->die_parent;
7107 dw_die_ref new_parent = unit;
7108 dw_die_ref copy;
7109 void **slot = NULL;
7110 struct decl_table_entry *entry = NULL;
7112 if (decl_table)
7114 /* Check if the entry has already been copied to UNIT. */
7115 slot = htab_find_slot_with_hash (decl_table, die,
7116 htab_hash_pointer (die), INSERT);
7117 if (*slot != HTAB_EMPTY_ENTRY)
7119 entry = (struct decl_table_entry *) *slot;
7120 return entry->copy;
7123 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7124 entry = XCNEW (struct decl_table_entry);
7125 entry->orig = die;
7126 entry->copy = NULL;
7127 *slot = entry;
7130 if (parent != NULL)
7132 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7133 if (spec != NULL)
7134 parent = spec;
7135 if (!is_unit_die (parent))
7136 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7139 copy = clone_as_declaration (die);
7140 add_child_die (new_parent, copy);
7142 if (decl_table != NULL)
7144 /* Record the pointer to the copy. */
7145 entry->copy = copy;
7148 return copy;
7151 /* Like clone_tree, but additionally enter all the children into
7152 the hash table decl_table. */
7154 static dw_die_ref
7155 clone_tree_hash (dw_die_ref die, htab_t decl_table)
7157 dw_die_ref c;
7158 dw_die_ref clone = clone_die (die);
7159 struct decl_table_entry *entry;
7160 void **slot = htab_find_slot_with_hash (decl_table, die,
7161 htab_hash_pointer (die), INSERT);
7162 /* Assert that DIE isn't in the hash table yet. If it would be there
7163 before, the ancestors would be necessarily there as well, therefore
7164 clone_tree_hash wouldn't be called. */
7165 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7166 entry = XCNEW (struct decl_table_entry);
7167 entry->orig = die;
7168 entry->copy = clone;
7169 *slot = entry;
7171 FOR_EACH_CHILD (die, c,
7172 add_child_die (clone, clone_tree_hash (c, decl_table)));
7174 return clone;
7177 /* Walk the DIE and its children, looking for references to incomplete
7178 or trivial types that are unmarked (i.e., that are not in the current
7179 type_unit). */
7181 static void
7182 copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
7184 dw_die_ref c;
7185 dw_attr_ref a;
7186 unsigned ix;
7188 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7190 if (AT_class (a) == dw_val_class_die_ref)
7192 dw_die_ref targ = AT_ref (a);
7193 void **slot;
7194 struct decl_table_entry *entry;
7196 if (targ->die_mark != 0 || targ->comdat_type_p)
7197 continue;
7199 slot = htab_find_slot_with_hash (decl_table, targ,
7200 htab_hash_pointer (targ), INSERT);
7202 if (*slot != HTAB_EMPTY_ENTRY)
7204 /* TARG has already been copied, so we just need to
7205 modify the reference to point to the copy. */
7206 entry = (struct decl_table_entry *) *slot;
7207 a->dw_attr_val.v.val_die_ref.die = entry->copy;
7209 else
7211 dw_die_ref parent = unit;
7212 dw_die_ref copy = clone_die (targ);
7214 /* Record in DECL_TABLE that TARG has been copied.
7215 Need to do this now, before the recursive call,
7216 because DECL_TABLE may be expanded and SLOT
7217 would no longer be a valid pointer. */
7218 entry = XCNEW (struct decl_table_entry);
7219 entry->orig = targ;
7220 entry->copy = copy;
7221 *slot = entry;
7223 FOR_EACH_CHILD (targ, c,
7224 add_child_die (copy,
7225 clone_tree_hash (c, decl_table)));
7227 /* Make sure the cloned tree is marked as part of the
7228 type unit. */
7229 mark_dies (copy);
7231 /* If TARG has surrounding context, copy its ancestor tree
7232 into the new type unit. */
7233 if (targ->die_parent != NULL
7234 && !is_unit_die (targ->die_parent))
7235 parent = copy_ancestor_tree (unit, targ->die_parent,
7236 decl_table);
7238 add_child_die (parent, copy);
7239 a->dw_attr_val.v.val_die_ref.die = copy;
7241 /* Make sure the newly-copied DIE is walked. If it was
7242 installed in a previously-added context, it won't
7243 get visited otherwise. */
7244 if (parent != unit)
7246 /* Find the highest point of the newly-added tree,
7247 mark each node along the way, and walk from there. */
7248 parent->die_mark = 1;
7249 while (parent->die_parent
7250 && parent->die_parent->die_mark == 0)
7252 parent = parent->die_parent;
7253 parent->die_mark = 1;
7255 copy_decls_walk (unit, parent, decl_table);
7261 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7264 /* Copy declarations for "unworthy" types into the new comdat section.
7265 Incomplete types, modified types, and certain other types aren't broken
7266 out into comdat sections of their own, so they don't have a signature,
7267 and we need to copy the declaration into the same section so that we
7268 don't have an external reference. */
7270 static void
7271 copy_decls_for_unworthy_types (dw_die_ref unit)
7273 htab_t decl_table;
7275 mark_dies (unit);
7276 decl_table = htab_create (10, htab_decl_hash, htab_decl_eq, htab_decl_del);
7277 copy_decls_walk (unit, unit, decl_table);
7278 htab_delete (decl_table);
7279 unmark_dies (unit);
7282 /* Traverse the DIE and add a sibling attribute if it may have the
7283 effect of speeding up access to siblings. To save some space,
7284 avoid generating sibling attributes for DIE's without children. */
7286 static void
7287 add_sibling_attributes (dw_die_ref die)
7289 dw_die_ref c;
7291 if (! die->die_child)
7292 return;
7294 if (die->die_parent && die != die->die_parent->die_child)
7295 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7297 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7300 /* Output all location lists for the DIE and its children. */
7302 static void
7303 output_location_lists (dw_die_ref die)
7305 dw_die_ref c;
7306 dw_attr_ref a;
7307 unsigned ix;
7309 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7310 if (AT_class (a) == dw_val_class_loc_list)
7311 output_loc_list (AT_loc_list (a));
7313 FOR_EACH_CHILD (die, c, output_location_lists (c));
7316 /* We want to limit the number of external references, because they are
7317 larger than local references: a relocation takes multiple words, and
7318 even a sig8 reference is always eight bytes, whereas a local reference
7319 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7320 So if we encounter multiple external references to the same type DIE, we
7321 make a local typedef stub for it and redirect all references there.
7323 This is the element of the hash table for keeping track of these
7324 references. */
7326 struct external_ref
7328 dw_die_ref type;
7329 dw_die_ref stub;
7330 unsigned n_refs;
7333 /* Hash an external_ref. */
7335 static hashval_t
7336 hash_external_ref (const void *p)
7338 const struct external_ref *r = (const struct external_ref *)p;
7339 return htab_hash_pointer (r->type);
7342 /* Compare external_refs. */
7344 static int
7345 external_ref_eq (const void *p1, const void *p2)
7347 const struct external_ref *r1 = (const struct external_ref *)p1;
7348 const struct external_ref *r2 = (const struct external_ref *)p2;
7349 return r1->type == r2->type;
7352 /* Return a pointer to the external_ref for references to DIE. */
7354 static struct external_ref *
7355 lookup_external_ref (htab_t map, dw_die_ref die)
7357 struct external_ref ref, *ref_p;
7358 void ** slot;
7360 ref.type = die;
7361 slot = htab_find_slot (map, &ref, INSERT);
7362 if (*slot != HTAB_EMPTY_ENTRY)
7363 return (struct external_ref *) *slot;
7365 ref_p = XCNEW (struct external_ref);
7366 ref_p->type = die;
7367 *slot = ref_p;
7368 return ref_p;
7371 /* Subroutine of optimize_external_refs, below.
7373 If we see a type skeleton, record it as our stub. If we see external
7374 references, remember how many we've seen. */
7376 static void
7377 optimize_external_refs_1 (dw_die_ref die, htab_t map)
7379 dw_die_ref c;
7380 dw_attr_ref a;
7381 unsigned ix;
7382 struct external_ref *ref_p;
7384 if (is_type_die (die)
7385 && (c = get_AT_ref (die, DW_AT_signature)))
7387 /* This is a local skeleton; use it for local references. */
7388 ref_p = lookup_external_ref (map, c);
7389 ref_p->stub = die;
7392 /* Scan the DIE references, and remember any that refer to DIEs from
7393 other CUs (i.e. those which are not marked). */
7394 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7395 if (AT_class (a) == dw_val_class_die_ref
7396 && (c = AT_ref (a))->die_mark == 0
7397 && is_type_die (c))
7399 ref_p = lookup_external_ref (map, c);
7400 ref_p->n_refs++;
7403 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7406 /* htab_traverse callback function for optimize_external_refs, below. SLOT
7407 points to an external_ref, DATA is the CU we're processing. If we don't
7408 already have a local stub, and we have multiple refs, build a stub. */
7410 static int
7411 build_local_stub (void **slot, void *data)
7413 struct external_ref *ref_p = (struct external_ref *)*slot;
7415 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7417 /* We have multiple references to this type, so build a small stub.
7418 Both of these forms are a bit dodgy from the perspective of the
7419 DWARF standard, since technically they should have names. */
7420 dw_die_ref cu = (dw_die_ref) data;
7421 dw_die_ref type = ref_p->type;
7422 dw_die_ref stub = NULL;
7424 if (type->comdat_type_p)
7426 /* If we refer to this type via sig8, use AT_signature. */
7427 stub = new_die (type->die_tag, cu, NULL_TREE);
7428 add_AT_die_ref (stub, DW_AT_signature, type);
7430 else
7432 /* Otherwise, use a typedef with no name. */
7433 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7434 add_AT_die_ref (stub, DW_AT_type, type);
7437 stub->die_mark++;
7438 ref_p->stub = stub;
7440 return 1;
7443 /* DIE is a unit; look through all the DIE references to see if there are
7444 any external references to types, and if so, create local stubs for
7445 them which will be applied in build_abbrev_table. This is useful because
7446 references to local DIEs are smaller. */
7448 static htab_t
7449 optimize_external_refs (dw_die_ref die)
7451 htab_t map = htab_create (10, hash_external_ref, external_ref_eq, free);
7452 optimize_external_refs_1 (die, map);
7453 htab_traverse (map, build_local_stub, die);
7454 return map;
7457 /* The format of each DIE (and its attribute value pairs) is encoded in an
7458 abbreviation table. This routine builds the abbreviation table and assigns
7459 a unique abbreviation id for each abbreviation entry. The children of each
7460 die are visited recursively. */
7462 static void
7463 build_abbrev_table (dw_die_ref die, htab_t extern_map)
7465 unsigned long abbrev_id;
7466 unsigned int n_alloc;
7467 dw_die_ref c;
7468 dw_attr_ref a;
7469 unsigned ix;
7471 /* Scan the DIE references, and replace any that refer to
7472 DIEs from other CUs (i.e. those which are not marked) with
7473 the local stubs we built in optimize_external_refs. */
7474 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7475 if (AT_class (a) == dw_val_class_die_ref
7476 && (c = AT_ref (a))->die_mark == 0)
7478 struct external_ref *ref_p;
7479 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
7481 ref_p = lookup_external_ref (extern_map, c);
7482 if (ref_p->stub && ref_p->stub != die)
7483 change_AT_die_ref (a, ref_p->stub);
7484 else
7485 /* We aren't changing this reference, so mark it external. */
7486 set_AT_ref_external (a, 1);
7489 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7491 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7492 dw_attr_ref die_a, abbrev_a;
7493 unsigned ix;
7494 bool ok = true;
7496 if (abbrev->die_tag != die->die_tag)
7497 continue;
7498 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7499 continue;
7501 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
7502 continue;
7504 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
7506 abbrev_a = &(*abbrev->die_attr)[ix];
7507 if ((abbrev_a->dw_attr != die_a->dw_attr)
7508 || (value_format (abbrev_a) != value_format (die_a)))
7510 ok = false;
7511 break;
7514 if (ok)
7515 break;
7518 if (abbrev_id >= abbrev_die_table_in_use)
7520 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7522 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7523 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7524 n_alloc);
7526 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7527 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7528 abbrev_die_table_allocated = n_alloc;
7531 ++abbrev_die_table_in_use;
7532 abbrev_die_table[abbrev_id] = die;
7535 die->die_abbrev = abbrev_id;
7536 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
7539 /* Return the power-of-two number of bytes necessary to represent VALUE. */
7541 static int
7542 constant_size (unsigned HOST_WIDE_INT value)
7544 int log;
7546 if (value == 0)
7547 log = 0;
7548 else
7549 log = floor_log2 (value);
7551 log = log / 8;
7552 log = 1 << (floor_log2 (log) + 1);
7554 return log;
7557 /* Return the size of a DIE as it is represented in the
7558 .debug_info section. */
7560 static unsigned long
7561 size_of_die (dw_die_ref die)
7563 unsigned long size = 0;
7564 dw_attr_ref a;
7565 unsigned ix;
7566 enum dwarf_form form;
7568 size += size_of_uleb128 (die->die_abbrev);
7569 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7571 switch (AT_class (a))
7573 case dw_val_class_addr:
7574 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7576 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7577 size += size_of_uleb128 (AT_index (a));
7579 else
7580 size += DWARF2_ADDR_SIZE;
7581 break;
7582 case dw_val_class_offset:
7583 size += DWARF_OFFSET_SIZE;
7584 break;
7585 case dw_val_class_loc:
7587 unsigned long lsize = size_of_locs (AT_loc (a));
7589 /* Block length. */
7590 if (dwarf_version >= 4)
7591 size += size_of_uleb128 (lsize);
7592 else
7593 size += constant_size (lsize);
7594 size += lsize;
7596 break;
7597 case dw_val_class_loc_list:
7598 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7600 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7601 size += size_of_uleb128 (AT_index (a));
7603 else
7604 size += DWARF_OFFSET_SIZE;
7605 break;
7606 case dw_val_class_range_list:
7607 size += DWARF_OFFSET_SIZE;
7608 break;
7609 case dw_val_class_const:
7610 size += size_of_sleb128 (AT_int (a));
7611 break;
7612 case dw_val_class_unsigned_const:
7614 int csize = constant_size (AT_unsigned (a));
7615 if (dwarf_version == 3
7616 && a->dw_attr == DW_AT_data_member_location
7617 && csize >= 4)
7618 size += size_of_uleb128 (AT_unsigned (a));
7619 else
7620 size += csize;
7622 break;
7623 case dw_val_class_const_double:
7624 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
7625 if (HOST_BITS_PER_WIDE_INT >= 64)
7626 size++; /* block */
7627 break;
7628 case dw_val_class_vec:
7629 size += constant_size (a->dw_attr_val.v.val_vec.length
7630 * a->dw_attr_val.v.val_vec.elt_size)
7631 + a->dw_attr_val.v.val_vec.length
7632 * a->dw_attr_val.v.val_vec.elt_size; /* block */
7633 break;
7634 case dw_val_class_flag:
7635 if (dwarf_version >= 4)
7636 /* Currently all add_AT_flag calls pass in 1 as last argument,
7637 so DW_FORM_flag_present can be used. If that ever changes,
7638 we'll need to use DW_FORM_flag and have some optimization
7639 in build_abbrev_table that will change those to
7640 DW_FORM_flag_present if it is set to 1 in all DIEs using
7641 the same abbrev entry. */
7642 gcc_assert (a->dw_attr_val.v.val_flag == 1);
7643 else
7644 size += 1;
7645 break;
7646 case dw_val_class_die_ref:
7647 if (AT_ref_external (a))
7649 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
7650 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
7651 is sized by target address length, whereas in DWARF3
7652 it's always sized as an offset. */
7653 if (use_debug_types)
7654 size += DWARF_TYPE_SIGNATURE_SIZE;
7655 else if (dwarf_version == 2)
7656 size += DWARF2_ADDR_SIZE;
7657 else
7658 size += DWARF_OFFSET_SIZE;
7660 else
7661 size += DWARF_OFFSET_SIZE;
7662 break;
7663 case dw_val_class_fde_ref:
7664 size += DWARF_OFFSET_SIZE;
7665 break;
7666 case dw_val_class_lbl_id:
7667 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7669 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7670 size += size_of_uleb128 (AT_index (a));
7672 else
7673 size += DWARF2_ADDR_SIZE;
7674 break;
7675 case dw_val_class_lineptr:
7676 case dw_val_class_macptr:
7677 size += DWARF_OFFSET_SIZE;
7678 break;
7679 case dw_val_class_str:
7680 form = AT_string_form (a);
7681 if (form == DW_FORM_strp)
7682 size += DWARF_OFFSET_SIZE;
7683 else if (form == DW_FORM_GNU_str_index)
7684 size += size_of_uleb128 (AT_index (a));
7685 else
7686 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7687 break;
7688 case dw_val_class_file:
7689 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7690 break;
7691 case dw_val_class_data8:
7692 size += 8;
7693 break;
7694 case dw_val_class_vms_delta:
7695 size += DWARF_OFFSET_SIZE;
7696 break;
7697 case dw_val_class_high_pc:
7698 size += DWARF2_ADDR_SIZE;
7699 break;
7700 default:
7701 gcc_unreachable ();
7705 return size;
7708 /* Size the debugging information associated with a given DIE. Visits the
7709 DIE's children recursively. Updates the global variable next_die_offset, on
7710 each time through. Uses the current value of next_die_offset to update the
7711 die_offset field in each DIE. */
7713 static void
7714 calc_die_sizes (dw_die_ref die)
7716 dw_die_ref c;
7718 gcc_assert (die->die_offset == 0
7719 || (unsigned long int) die->die_offset == next_die_offset);
7720 die->die_offset = next_die_offset;
7721 next_die_offset += size_of_die (die);
7723 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
7725 if (die->die_child != NULL)
7726 /* Count the null byte used to terminate sibling lists. */
7727 next_die_offset += 1;
7730 /* Size just the base type children at the start of the CU.
7731 This is needed because build_abbrev needs to size locs
7732 and sizing of type based stack ops needs to know die_offset
7733 values for the base types. */
7735 static void
7736 calc_base_type_die_sizes (void)
7738 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7739 unsigned int i;
7740 dw_die_ref base_type;
7741 #if ENABLE_ASSERT_CHECKING
7742 dw_die_ref prev = comp_unit_die ()->die_child;
7743 #endif
7745 die_offset += size_of_die (comp_unit_die ());
7746 for (i = 0; base_types.iterate (i, &base_type); i++)
7748 #if ENABLE_ASSERT_CHECKING
7749 gcc_assert (base_type->die_offset == 0
7750 && prev->die_sib == base_type
7751 && base_type->die_child == NULL
7752 && base_type->die_abbrev);
7753 prev = base_type;
7754 #endif
7755 base_type->die_offset = die_offset;
7756 die_offset += size_of_die (base_type);
7760 /* Set the marks for a die and its children. We do this so
7761 that we know whether or not a reference needs to use FORM_ref_addr; only
7762 DIEs in the same CU will be marked. We used to clear out the offset
7763 and use that as the flag, but ran into ordering problems. */
7765 static void
7766 mark_dies (dw_die_ref die)
7768 dw_die_ref c;
7770 gcc_assert (!die->die_mark);
7772 die->die_mark = 1;
7773 FOR_EACH_CHILD (die, c, mark_dies (c));
7776 /* Clear the marks for a die and its children. */
7778 static void
7779 unmark_dies (dw_die_ref die)
7781 dw_die_ref c;
7783 if (! use_debug_types)
7784 gcc_assert (die->die_mark);
7786 die->die_mark = 0;
7787 FOR_EACH_CHILD (die, c, unmark_dies (c));
7790 /* Clear the marks for a die, its children and referred dies. */
7792 static void
7793 unmark_all_dies (dw_die_ref die)
7795 dw_die_ref c;
7796 dw_attr_ref a;
7797 unsigned ix;
7799 if (!die->die_mark)
7800 return;
7801 die->die_mark = 0;
7803 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
7805 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7806 if (AT_class (a) == dw_val_class_die_ref)
7807 unmark_all_dies (AT_ref (a));
7810 /* Return the size of the .debug_pubnames or .debug_pubtypes table
7811 generated for the compilation unit. */
7813 static unsigned long
7814 size_of_pubnames (vec<pubname_entry, va_gc> *names)
7816 unsigned long size;
7817 unsigned i;
7818 pubname_ref p;
7820 size = DWARF_PUBNAMES_HEADER_SIZE;
7821 FOR_EACH_VEC_ELT (*names, i, p)
7822 if (names != pubtype_table
7823 || p->die->die_offset != 0
7824 || !flag_eliminate_unused_debug_types)
7825 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
7827 size += DWARF_OFFSET_SIZE;
7828 return size;
7831 /* Return the size of the information in the .debug_aranges section. */
7833 static unsigned long
7834 size_of_aranges (void)
7836 unsigned long size;
7838 size = DWARF_ARANGES_HEADER_SIZE;
7840 /* Count the address/length pair for this compilation unit. */
7841 if (text_section_used)
7842 size += 2 * DWARF2_ADDR_SIZE;
7843 if (cold_text_section_used)
7844 size += 2 * DWARF2_ADDR_SIZE;
7845 if (have_multiple_function_sections)
7847 unsigned fde_idx;
7848 dw_fde_ref fde;
7850 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
7852 if (!fde->in_std_section)
7853 size += 2 * DWARF2_ADDR_SIZE;
7854 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
7855 size += 2 * DWARF2_ADDR_SIZE;
7859 /* Count the two zero words used to terminated the address range table. */
7860 size += 2 * DWARF2_ADDR_SIZE;
7861 return size;
7864 /* Select the encoding of an attribute value. */
7866 static enum dwarf_form
7867 value_format (dw_attr_ref a)
7869 switch (AT_class (a))
7871 case dw_val_class_addr:
7872 /* Only very few attributes allow DW_FORM_addr. */
7873 switch (a->dw_attr)
7875 case DW_AT_low_pc:
7876 case DW_AT_high_pc:
7877 case DW_AT_entry_pc:
7878 case DW_AT_trampoline:
7879 return (AT_index (a) == NOT_INDEXED
7880 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
7881 default:
7882 break;
7884 switch (DWARF2_ADDR_SIZE)
7886 case 1:
7887 return DW_FORM_data1;
7888 case 2:
7889 return DW_FORM_data2;
7890 case 4:
7891 return DW_FORM_data4;
7892 case 8:
7893 return DW_FORM_data8;
7894 default:
7895 gcc_unreachable ();
7897 case dw_val_class_range_list:
7898 case dw_val_class_loc_list:
7899 if (dwarf_version >= 4)
7900 return DW_FORM_sec_offset;
7901 /* FALLTHRU */
7902 case dw_val_class_vms_delta:
7903 case dw_val_class_offset:
7904 switch (DWARF_OFFSET_SIZE)
7906 case 4:
7907 return DW_FORM_data4;
7908 case 8:
7909 return DW_FORM_data8;
7910 default:
7911 gcc_unreachable ();
7913 case dw_val_class_loc:
7914 if (dwarf_version >= 4)
7915 return DW_FORM_exprloc;
7916 switch (constant_size (size_of_locs (AT_loc (a))))
7918 case 1:
7919 return DW_FORM_block1;
7920 case 2:
7921 return DW_FORM_block2;
7922 case 4:
7923 return DW_FORM_block4;
7924 default:
7925 gcc_unreachable ();
7927 case dw_val_class_const:
7928 return DW_FORM_sdata;
7929 case dw_val_class_unsigned_const:
7930 switch (constant_size (AT_unsigned (a)))
7932 case 1:
7933 return DW_FORM_data1;
7934 case 2:
7935 return DW_FORM_data2;
7936 case 4:
7937 /* In DWARF3 DW_AT_data_member_location with
7938 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
7939 constant, so we need to use DW_FORM_udata if we need
7940 a large constant. */
7941 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
7942 return DW_FORM_udata;
7943 return DW_FORM_data4;
7944 case 8:
7945 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
7946 return DW_FORM_udata;
7947 return DW_FORM_data8;
7948 default:
7949 gcc_unreachable ();
7951 case dw_val_class_const_double:
7952 switch (HOST_BITS_PER_WIDE_INT)
7954 case 8:
7955 return DW_FORM_data2;
7956 case 16:
7957 return DW_FORM_data4;
7958 case 32:
7959 return DW_FORM_data8;
7960 case 64:
7961 default:
7962 return DW_FORM_block1;
7964 case dw_val_class_vec:
7965 switch (constant_size (a->dw_attr_val.v.val_vec.length
7966 * a->dw_attr_val.v.val_vec.elt_size))
7968 case 1:
7969 return DW_FORM_block1;
7970 case 2:
7971 return DW_FORM_block2;
7972 case 4:
7973 return DW_FORM_block4;
7974 default:
7975 gcc_unreachable ();
7977 case dw_val_class_flag:
7978 if (dwarf_version >= 4)
7980 /* Currently all add_AT_flag calls pass in 1 as last argument,
7981 so DW_FORM_flag_present can be used. If that ever changes,
7982 we'll need to use DW_FORM_flag and have some optimization
7983 in build_abbrev_table that will change those to
7984 DW_FORM_flag_present if it is set to 1 in all DIEs using
7985 the same abbrev entry. */
7986 gcc_assert (a->dw_attr_val.v.val_flag == 1);
7987 return DW_FORM_flag_present;
7989 return DW_FORM_flag;
7990 case dw_val_class_die_ref:
7991 if (AT_ref_external (a))
7992 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
7993 else
7994 return DW_FORM_ref;
7995 case dw_val_class_fde_ref:
7996 return DW_FORM_data;
7997 case dw_val_class_lbl_id:
7998 return (AT_index (a) == NOT_INDEXED
7999 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8000 case dw_val_class_lineptr:
8001 case dw_val_class_macptr:
8002 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8003 case dw_val_class_str:
8004 return AT_string_form (a);
8005 case dw_val_class_file:
8006 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8008 case 1:
8009 return DW_FORM_data1;
8010 case 2:
8011 return DW_FORM_data2;
8012 case 4:
8013 return DW_FORM_data4;
8014 default:
8015 gcc_unreachable ();
8018 case dw_val_class_data8:
8019 return DW_FORM_data8;
8021 case dw_val_class_high_pc:
8022 switch (DWARF2_ADDR_SIZE)
8024 case 1:
8025 return DW_FORM_data1;
8026 case 2:
8027 return DW_FORM_data2;
8028 case 4:
8029 return DW_FORM_data4;
8030 case 8:
8031 return DW_FORM_data8;
8032 default:
8033 gcc_unreachable ();
8036 default:
8037 gcc_unreachable ();
8041 /* Output the encoding of an attribute value. */
8043 static void
8044 output_value_format (dw_attr_ref a)
8046 enum dwarf_form form = value_format (a);
8048 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8051 /* Given a die and id, produce the appropriate abbreviations. */
8053 static void
8054 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8056 unsigned ix;
8057 dw_attr_ref a_attr;
8059 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8060 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8061 dwarf_tag_name (abbrev->die_tag));
8063 if (abbrev->die_child != NULL)
8064 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8065 else
8066 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8068 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8070 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8071 dwarf_attr_name (a_attr->dw_attr));
8072 output_value_format (a_attr);
8075 dw2_asm_output_data (1, 0, NULL);
8076 dw2_asm_output_data (1, 0, NULL);
8080 /* Output the .debug_abbrev section which defines the DIE abbreviation
8081 table. */
8083 static void
8084 output_abbrev_section (void)
8086 unsigned long abbrev_id;
8088 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8089 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8091 /* Terminate the table. */
8092 dw2_asm_output_data (1, 0, NULL);
8095 /* Output a symbol we can use to refer to this DIE from another CU. */
8097 static inline void
8098 output_die_symbol (dw_die_ref die)
8100 const char *sym = die->die_id.die_symbol;
8102 gcc_assert (!die->comdat_type_p);
8104 if (sym == 0)
8105 return;
8107 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8108 /* We make these global, not weak; if the target doesn't support
8109 .linkonce, it doesn't support combining the sections, so debugging
8110 will break. */
8111 targetm.asm_out.globalize_label (asm_out_file, sym);
8113 ASM_OUTPUT_LABEL (asm_out_file, sym);
8116 /* Return a new location list, given the begin and end range, and the
8117 expression. */
8119 static inline dw_loc_list_ref
8120 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8121 const char *section)
8123 dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
8125 retlist->begin = begin;
8126 retlist->begin_entry = NULL;
8127 retlist->end = end;
8128 retlist->expr = expr;
8129 retlist->section = section;
8131 return retlist;
8134 /* Generate a new internal symbol for this location list node, if it
8135 hasn't got one yet. */
8137 static inline void
8138 gen_llsym (dw_loc_list_ref list)
8140 gcc_assert (!list->ll_symbol);
8141 list->ll_symbol = gen_internal_sym ("LLST");
8144 /* Output the location list given to us. */
8146 static void
8147 output_loc_list (dw_loc_list_ref list_head)
8149 dw_loc_list_ref curr = list_head;
8151 if (list_head->emitted)
8152 return;
8153 list_head->emitted = true;
8155 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8157 /* Walk the location list, and output each range + expression. */
8158 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8160 unsigned long size;
8161 /* Don't output an entry that starts and ends at the same address. */
8162 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8163 continue;
8164 size = size_of_locs (curr->expr);
8165 /* If the expression is too large, drop it on the floor. We could
8166 perhaps put it into DW_TAG_dwarf_procedure and refer to that
8167 in the expression, but >= 64KB expressions for a single value
8168 in a single range are unlikely very useful. */
8169 if (size > 0xffff)
8170 continue;
8171 if (dwarf_split_debug_info)
8173 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8174 "Location list start/length entry (%s)",
8175 list_head->ll_symbol);
8176 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8177 "Location list range start index (%s)",
8178 curr->begin);
8179 /* The length field is 4 bytes. If we ever need to support
8180 an 8-byte length, we can add a new DW_LLE code or fall back
8181 to DW_LLE_GNU_start_end_entry. */
8182 dw2_asm_output_delta (4, curr->end, curr->begin,
8183 "Location list range length (%s)",
8184 list_head->ll_symbol);
8186 else if (!have_multiple_function_sections)
8188 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8189 "Location list begin address (%s)",
8190 list_head->ll_symbol);
8191 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8192 "Location list end address (%s)",
8193 list_head->ll_symbol);
8195 else
8197 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8198 "Location list begin address (%s)",
8199 list_head->ll_symbol);
8200 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8201 "Location list end address (%s)",
8202 list_head->ll_symbol);
8205 /* Output the block length for this list of location operations. */
8206 gcc_assert (size <= 0xffff);
8207 dw2_asm_output_data (2, size, "%s", "Location expression size");
8209 output_loc_sequence (curr->expr, -1);
8212 if (dwarf_split_debug_info)
8213 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8214 "Location list terminator (%s)",
8215 list_head->ll_symbol);
8216 else
8218 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8219 "Location list terminator begin (%s)",
8220 list_head->ll_symbol);
8221 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8222 "Location list terminator end (%s)",
8223 list_head->ll_symbol);
8227 /* Output a range_list offset into the debug_range section. Emit a
8228 relocated reference if val_entry is NULL, otherwise, emit an
8229 indirect reference. */
8231 static void
8232 output_range_list_offset (dw_attr_ref a)
8234 const char *name = dwarf_attr_name (a->dw_attr);
8236 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8238 char *p = strchr (ranges_section_label, '\0');
8239 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8240 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8241 debug_ranges_section, "%s", name);
8242 *p = '\0';
8244 else
8245 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8246 "%s (offset from %s)", name, ranges_section_label);
8249 /* Output the offset into the debug_loc section. */
8251 static void
8252 output_loc_list_offset (dw_attr_ref a)
8254 char *sym = AT_loc_list (a)->ll_symbol;
8256 gcc_assert (sym);
8257 if (dwarf_split_debug_info)
8258 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8259 "%s", dwarf_attr_name (a->dw_attr));
8260 else
8261 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8262 "%s", dwarf_attr_name (a->dw_attr));
8265 /* Output an attribute's index or value appropriately. */
8267 static void
8268 output_attr_index_or_value (dw_attr_ref a)
8270 const char *name = dwarf_attr_name (a->dw_attr);
8272 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8274 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8275 return;
8277 switch (AT_class (a))
8279 case dw_val_class_addr:
8280 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8281 break;
8282 case dw_val_class_high_pc:
8283 case dw_val_class_lbl_id:
8284 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8285 break;
8286 case dw_val_class_loc_list:
8287 output_loc_list_offset (a);
8288 break;
8289 default:
8290 gcc_unreachable ();
8294 /* Output a type signature. */
8296 static inline void
8297 output_signature (const char *sig, const char *name)
8299 int i;
8301 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8302 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8305 /* Output the DIE and its attributes. Called recursively to generate
8306 the definitions of each child DIE. */
8308 static void
8309 output_die (dw_die_ref die)
8311 dw_attr_ref a;
8312 dw_die_ref c;
8313 unsigned long size;
8314 unsigned ix;
8316 /* If someone in another CU might refer to us, set up a symbol for
8317 them to point to. */
8318 if (! die->comdat_type_p && die->die_id.die_symbol)
8319 output_die_symbol (die);
8321 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8322 (unsigned long)die->die_offset,
8323 dwarf_tag_name (die->die_tag));
8325 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8327 const char *name = dwarf_attr_name (a->dw_attr);
8329 switch (AT_class (a))
8331 case dw_val_class_addr:
8332 output_attr_index_or_value (a);
8333 break;
8335 case dw_val_class_offset:
8336 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8337 "%s", name);
8338 break;
8340 case dw_val_class_range_list:
8341 output_range_list_offset (a);
8342 break;
8344 case dw_val_class_loc:
8345 size = size_of_locs (AT_loc (a));
8347 /* Output the block length for this list of location operations. */
8348 if (dwarf_version >= 4)
8349 dw2_asm_output_data_uleb128 (size, "%s", name);
8350 else
8351 dw2_asm_output_data (constant_size (size), size, "%s", name);
8353 output_loc_sequence (AT_loc (a), -1);
8354 break;
8356 case dw_val_class_const:
8357 /* ??? It would be slightly more efficient to use a scheme like is
8358 used for unsigned constants below, but gdb 4.x does not sign
8359 extend. Gdb 5.x does sign extend. */
8360 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8361 break;
8363 case dw_val_class_unsigned_const:
8365 int csize = constant_size (AT_unsigned (a));
8366 if (dwarf_version == 3
8367 && a->dw_attr == DW_AT_data_member_location
8368 && csize >= 4)
8369 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8370 else
8371 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8373 break;
8375 case dw_val_class_const_double:
8377 unsigned HOST_WIDE_INT first, second;
8379 if (HOST_BITS_PER_WIDE_INT >= 64)
8380 dw2_asm_output_data (1,
8381 HOST_BITS_PER_DOUBLE_INT
8382 / HOST_BITS_PER_CHAR,
8383 NULL);
8385 if (WORDS_BIG_ENDIAN)
8387 first = a->dw_attr_val.v.val_double.high;
8388 second = a->dw_attr_val.v.val_double.low;
8390 else
8392 first = a->dw_attr_val.v.val_double.low;
8393 second = a->dw_attr_val.v.val_double.high;
8396 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8397 first, "%s", name);
8398 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8399 second, NULL);
8401 break;
8403 case dw_val_class_vec:
8405 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8406 unsigned int len = a->dw_attr_val.v.val_vec.length;
8407 unsigned int i;
8408 unsigned char *p;
8410 dw2_asm_output_data (constant_size (len * elt_size),
8411 len * elt_size, "%s", name);
8412 if (elt_size > sizeof (HOST_WIDE_INT))
8414 elt_size /= 2;
8415 len *= 2;
8417 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8418 i < len;
8419 i++, p += elt_size)
8420 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8421 "fp or vector constant word %u", i);
8422 break;
8425 case dw_val_class_flag:
8426 if (dwarf_version >= 4)
8428 /* Currently all add_AT_flag calls pass in 1 as last argument,
8429 so DW_FORM_flag_present can be used. If that ever changes,
8430 we'll need to use DW_FORM_flag and have some optimization
8431 in build_abbrev_table that will change those to
8432 DW_FORM_flag_present if it is set to 1 in all DIEs using
8433 the same abbrev entry. */
8434 gcc_assert (AT_flag (a) == 1);
8435 if (flag_debug_asm)
8436 fprintf (asm_out_file, "\t\t\t%s %s\n",
8437 ASM_COMMENT_START, name);
8438 break;
8440 dw2_asm_output_data (1, AT_flag (a), "%s", name);
8441 break;
8443 case dw_val_class_loc_list:
8444 output_attr_index_or_value (a);
8445 break;
8447 case dw_val_class_die_ref:
8448 if (AT_ref_external (a))
8450 if (AT_ref (a)->comdat_type_p)
8452 comdat_type_node_ref type_node =
8453 AT_ref (a)->die_id.die_type_node;
8455 gcc_assert (type_node);
8456 output_signature (type_node->signature, name);
8458 else
8460 const char *sym = AT_ref (a)->die_id.die_symbol;
8461 int size;
8463 gcc_assert (sym);
8464 /* In DWARF2, DW_FORM_ref_addr is sized by target address
8465 length, whereas in DWARF3 it's always sized as an
8466 offset. */
8467 if (dwarf_version == 2)
8468 size = DWARF2_ADDR_SIZE;
8469 else
8470 size = DWARF_OFFSET_SIZE;
8471 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8472 name);
8475 else
8477 gcc_assert (AT_ref (a)->die_offset);
8478 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8479 "%s", name);
8481 break;
8483 case dw_val_class_fde_ref:
8485 char l1[20];
8487 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8488 a->dw_attr_val.v.val_fde_index * 2);
8489 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8490 "%s", name);
8492 break;
8494 case dw_val_class_vms_delta:
8495 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
8496 AT_vms_delta2 (a), AT_vms_delta1 (a),
8497 "%s", name);
8498 break;
8500 case dw_val_class_lbl_id:
8501 output_attr_index_or_value (a);
8502 break;
8504 case dw_val_class_lineptr:
8505 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8506 debug_line_section, "%s", name);
8507 break;
8509 case dw_val_class_macptr:
8510 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8511 debug_macinfo_section, "%s", name);
8512 break;
8514 case dw_val_class_str:
8515 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
8516 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8517 a->dw_attr_val.v.val_str->label,
8518 debug_str_section,
8519 "%s: \"%s\"", name, AT_string (a));
8520 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
8521 dw2_asm_output_data_uleb128 (AT_index (a),
8522 "%s: \"%s\"", name, AT_string (a));
8523 else
8524 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8525 break;
8527 case dw_val_class_file:
8529 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8531 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8532 a->dw_attr_val.v.val_file->filename);
8533 break;
8536 case dw_val_class_data8:
8538 int i;
8540 for (i = 0; i < 8; i++)
8541 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
8542 i == 0 ? "%s" : NULL, name);
8543 break;
8546 case dw_val_class_high_pc:
8547 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
8548 get_AT_low_pc (die), "DW_AT_high_pc");
8549 break;
8551 default:
8552 gcc_unreachable ();
8556 FOR_EACH_CHILD (die, c, output_die (c));
8558 /* Add null byte to terminate sibling list. */
8559 if (die->die_child != NULL)
8560 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
8561 (unsigned long) die->die_offset);
8564 /* Output the compilation unit that appears at the beginning of the
8565 .debug_info section, and precedes the DIE descriptions. */
8567 static void
8568 output_compilation_unit_header (void)
8570 int ver = dwarf_version;
8572 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8573 dw2_asm_output_data (4, 0xffffffff,
8574 "Initial length escape value indicating 64-bit DWARF extension");
8575 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8576 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8577 "Length of Compilation Unit Info");
8578 dw2_asm_output_data (2, ver, "DWARF version number");
8579 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8580 debug_abbrev_section,
8581 "Offset Into Abbrev. Section");
8582 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8585 /* Output the compilation unit DIE and its children. */
8587 static void
8588 output_comp_unit (dw_die_ref die, int output_if_empty)
8590 const char *secname, *oldsym;
8591 char *tmp;
8592 htab_t extern_map;
8594 /* Unless we are outputting main CU, we may throw away empty ones. */
8595 if (!output_if_empty && die->die_child == NULL)
8596 return;
8598 /* Even if there are no children of this DIE, we must output the information
8599 about the compilation unit. Otherwise, on an empty translation unit, we
8600 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
8601 will then complain when examining the file. First mark all the DIEs in
8602 this CU so we know which get local refs. */
8603 mark_dies (die);
8605 extern_map = optimize_external_refs (die);
8607 build_abbrev_table (die, extern_map);
8609 htab_delete (extern_map);
8611 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8612 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8613 calc_die_sizes (die);
8615 oldsym = die->die_id.die_symbol;
8616 if (oldsym)
8618 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8620 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8621 secname = tmp;
8622 die->die_id.die_symbol = NULL;
8623 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8625 else
8627 switch_to_section (debug_info_section);
8628 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
8629 info_section_emitted = true;
8632 /* Output debugging information. */
8633 output_compilation_unit_header ();
8634 output_die (die);
8636 /* Leave the marks on the main CU, so we can check them in
8637 output_pubnames. */
8638 if (oldsym)
8640 unmark_dies (die);
8641 die->die_id.die_symbol = oldsym;
8645 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
8646 and .debug_pubtypes. This is configured per-target, but can be
8647 overridden by the -gpubnames or -gno-pubnames options. */
8649 static inline bool
8650 want_pubnames (void)
8652 return (debug_generate_pub_sections != -1
8653 ? debug_generate_pub_sections
8654 : targetm.want_debug_pub_sections);
8657 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
8659 static void
8660 add_AT_pubnames (dw_die_ref die)
8662 if (want_pubnames ())
8663 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
8666 /* Helper function to generate top-level dies for skeleton debug_info and
8667 debug_types. */
8669 static void
8670 add_top_level_skeleton_die_attrs (dw_die_ref die)
8672 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
8673 dw_attr_ref attr;
8675 add_comp_dir_attribute (die);
8676 add_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
8677 /* The specification suggests that these attributes be inline to avoid
8678 having a .debug_str section. We know that they exist in the die because
8679 we just added them. */
8680 attr = get_AT (die, DW_AT_GNU_dwo_name);
8681 attr->dw_attr_val.v.val_str->form = DW_FORM_string;
8682 attr = get_AT (die, DW_AT_comp_dir);
8683 attr->dw_attr_val.v.val_str->form = DW_FORM_string;
8685 add_AT_pubnames (die);
8686 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
8689 /* Return the single type-unit die for skeleton type units. */
8691 static dw_die_ref
8692 get_skeleton_type_unit (void)
8694 /* For dwarf_split_debug_sections with use_type info, all type units in the
8695 skeleton sections have identical dies (but different headers). This
8696 single die will be output many times. */
8698 static dw_die_ref skeleton_type_unit = NULL;
8700 if (skeleton_type_unit == NULL)
8702 skeleton_type_unit = new_die (DW_TAG_type_unit, NULL, NULL);
8703 add_top_level_skeleton_die_attrs (skeleton_type_unit);
8704 skeleton_type_unit->die_abbrev = SKELETON_TYPE_DIE_ABBREV;
8706 return skeleton_type_unit;
8709 /* Output skeleton debug sections that point to the dwo file. */
8711 static void
8712 output_skeleton_debug_sections (dw_die_ref comp_unit)
8714 /* These attributes will be found in the full debug_info section. */
8715 remove_AT (comp_unit, DW_AT_producer);
8716 remove_AT (comp_unit, DW_AT_language);
8718 /* Add attributes common to skeleton compile_units and type_units. */
8719 add_top_level_skeleton_die_attrs (comp_unit);
8721 switch_to_section (debug_skeleton_info_section);
8722 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
8724 /* Produce the skeleton compilation-unit header. This one differs enough from
8725 a normal CU header that it's better not to call output_compilation_unit
8726 header. */
8727 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8728 dw2_asm_output_data (4, 0xffffffff,
8729 "Initial length escape value indicating 64-bit DWARF extension");
8731 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8732 DWARF_COMPILE_UNIT_HEADER_SIZE
8733 - DWARF_INITIAL_LENGTH_SIZE
8734 + size_of_die (comp_unit),
8735 "Length of Compilation Unit Info");
8736 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
8737 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
8738 debug_abbrev_section,
8739 "Offset Into Abbrev. Section");
8740 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8742 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
8743 output_die (comp_unit);
8745 /* Build the skeleton debug_abbrev section. */
8746 switch_to_section (debug_skeleton_abbrev_section);
8747 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
8749 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
8750 if (use_debug_types)
8751 output_die_abbrevs (SKELETON_TYPE_DIE_ABBREV, get_skeleton_type_unit ());
8753 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
8756 /* Output a comdat type unit DIE and its children. */
8758 static void
8759 output_comdat_type_unit (comdat_type_node *node)
8761 const char *secname;
8762 char *tmp;
8763 int i;
8764 #if defined (OBJECT_FORMAT_ELF)
8765 tree comdat_key;
8766 #endif
8767 htab_t extern_map;
8769 /* First mark all the DIEs in this CU so we know which get local refs. */
8770 mark_dies (node->root_die);
8772 extern_map = optimize_external_refs (node->root_die);
8774 build_abbrev_table (node->root_die, extern_map);
8776 htab_delete (extern_map);
8778 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8779 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
8780 calc_die_sizes (node->root_die);
8782 #if defined (OBJECT_FORMAT_ELF)
8783 if (!dwarf_split_debug_info)
8784 secname = ".debug_types";
8785 else
8786 secname = ".debug_types.dwo";
8788 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
8789 sprintf (tmp, "wt.");
8790 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8791 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
8792 comdat_key = get_identifier (tmp);
8793 targetm.asm_out.named_section (secname,
8794 SECTION_DEBUG | SECTION_LINKONCE,
8795 comdat_key);
8796 #else
8797 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
8798 sprintf (tmp, ".gnu.linkonce.wt.");
8799 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8800 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
8801 secname = tmp;
8802 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8803 #endif
8805 /* Output debugging information. */
8806 output_compilation_unit_header ();
8807 output_signature (node->signature, "Type Signature");
8808 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
8809 "Offset to Type DIE");
8810 output_die (node->root_die);
8812 unmark_dies (node->root_die);
8814 #if defined (OBJECT_FORMAT_ELF)
8815 if (dwarf_split_debug_info)
8817 /* Produce the skeleton type-unit header. */
8818 const char *secname = ".debug_types";
8820 targetm.asm_out.named_section (secname,
8821 SECTION_DEBUG | SECTION_LINKONCE,
8822 comdat_key);
8823 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8824 dw2_asm_output_data (4, 0xffffffff,
8825 "Initial length escape value indicating 64-bit DWARF extension");
8827 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8828 DWARF_COMPILE_UNIT_HEADER_SIZE
8829 - DWARF_INITIAL_LENGTH_SIZE
8830 + size_of_die (get_skeleton_type_unit ())
8831 + DWARF_TYPE_SIGNATURE_SIZE + DWARF_OFFSET_SIZE,
8832 "Length of Type Unit Info");
8833 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
8834 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8835 debug_skeleton_abbrev_section_label,
8836 debug_abbrev_section,
8837 "Offset Into Abbrev. Section");
8838 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8839 output_signature (node->signature, "Type Signature");
8840 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, "Offset to Type DIE");
8842 output_die (get_skeleton_type_unit ());
8844 #endif
8847 /* Return the DWARF2/3 pubname associated with a decl. */
8849 static const char *
8850 dwarf2_name (tree decl, int scope)
8852 if (DECL_NAMELESS (decl))
8853 return NULL;
8854 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
8857 /* Add a new entry to .debug_pubnames if appropriate. */
8859 static void
8860 add_pubname_string (const char *str, dw_die_ref die)
8862 pubname_entry e;
8864 e.die = die;
8865 e.name = xstrdup (str);
8866 vec_safe_push (pubname_table, e);
8869 static void
8870 add_pubname (tree decl, dw_die_ref die)
8872 if (!want_pubnames ())
8873 return;
8875 /* Don't add items to the table when we expect that the consumer will have
8876 just read the enclosing die. For example, if the consumer is looking at a
8877 class_member, it will either be inside the class already, or will have just
8878 looked up the class to find the member. Either way, searching the class is
8879 faster than searching the index. */
8880 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
8881 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
8883 const char *name = dwarf2_name (decl, 1);
8885 if (name)
8886 add_pubname_string (name, die);
8890 /* Add an enumerator to the pubnames section. */
8892 static void
8893 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
8895 pubname_entry e;
8897 gcc_assert (scope_name);
8898 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
8899 e.die = die;
8900 vec_safe_push (pubname_table, e);
8903 /* Add a new entry to .debug_pubtypes if appropriate. */
8905 static void
8906 add_pubtype (tree decl, dw_die_ref die)
8908 pubname_entry e;
8910 if (!want_pubnames ())
8911 return;
8913 if ((TREE_PUBLIC (decl)
8914 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
8915 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
8917 tree scope = NULL;
8918 const char *scope_name = "";
8919 const char *sep = is_cxx () ? "::" : ".";
8920 const char *name;
8922 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
8923 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
8925 scope_name = lang_hooks.dwarf_name (scope, 1);
8926 if (scope_name != NULL && scope_name[0] != '\0')
8927 scope_name = concat (scope_name, sep, NULL);
8928 else
8929 scope_name = "";
8932 if (TYPE_P (decl))
8933 name = type_tag (decl);
8934 else
8935 name = lang_hooks.dwarf_name (decl, 1);
8937 /* If we don't have a name for the type, there's no point in adding
8938 it to the table. */
8939 if (name != NULL && name[0] != '\0')
8941 e.die = die;
8942 e.name = concat (scope_name, name, NULL);
8943 vec_safe_push (pubtype_table, e);
8946 /* Although it might be more consistent to add the pubinfo for the
8947 enumerators as their dies are created, they should only be added if the
8948 enum type meets the criteria above. So rather than re-check the parent
8949 enum type whenever an enumerator die is created, just output them all
8950 here. This isn't protected by the name conditional because anonymous
8951 enums don't have names. */
8952 if (die->die_tag == DW_TAG_enumeration_type)
8954 dw_die_ref c;
8956 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
8961 /* Output the public names table used to speed up access to externally
8962 visible names; or the public types table used to find type definitions. */
8964 static void
8965 output_pubnames (vec<pubname_entry, va_gc> *names)
8967 unsigned i;
8968 unsigned long pubnames_length = size_of_pubnames (names);
8969 pubname_ref pub;
8971 if (!want_pubnames () || !info_section_emitted)
8972 return;
8973 if (names == pubname_table)
8974 switch_to_section (debug_pubnames_section);
8975 else
8976 switch_to_section (debug_pubtypes_section);
8977 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8978 dw2_asm_output_data (4, 0xffffffff,
8979 "Initial length escape value indicating 64-bit DWARF extension");
8980 if (names == pubname_table)
8981 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8982 "Length of Public Names Info");
8983 else
8984 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8985 "Length of Public Type Names Info");
8986 /* Version number for pubnames/pubtypes is still 2, even in DWARF3. */
8987 dw2_asm_output_data (2, 2, "DWARF Version");
8988 if (dwarf_split_debug_info)
8989 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
8990 debug_skeleton_info_section,
8991 "Offset of Compilation Unit Info");
8992 else
8993 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8994 debug_info_section,
8995 "Offset of Compilation Unit Info");
8996 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
8997 "Compilation Unit Length");
8999 FOR_EACH_VEC_ELT (*names, i, pub)
9001 /* Enumerator names are part of the pubname table, but the parent
9002 DW_TAG_enumeration_type die may have been pruned. Don't output
9003 them if that is the case. */
9004 if (pub->die->die_tag == DW_TAG_enumerator && !pub->die->die_mark)
9005 continue;
9007 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9008 if (names == pubname_table)
9009 gcc_assert (pub->die->die_mark);
9011 if (names != pubtype_table
9012 || pub->die->die_offset != 0
9013 || !flag_eliminate_unused_debug_types)
9015 dw_offset die_offset = pub->die->die_offset;
9017 /* If we're putting types in their own .debug_types sections,
9018 the .debug_pubtypes table will still point to the compile
9019 unit (not the type unit), so we want to use the offset of
9020 the skeleton DIE (if there is one). */
9021 if (pub->die->comdat_type_p && names == pubtype_table)
9023 comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
9025 if (type_node != NULL)
9026 die_offset = (type_node->skeleton_die != NULL
9027 ? type_node->skeleton_die->die_offset
9028 : 0);
9031 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9033 dw2_asm_output_nstring (pub->name, -1, "external name");
9037 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9040 /* Output the information that goes into the .debug_aranges table.
9041 Namely, define the beginning and ending address range of the
9042 text section generated for this compilation unit. */
9044 static void
9045 output_aranges (unsigned long aranges_length)
9047 unsigned i;
9049 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9050 dw2_asm_output_data (4, 0xffffffff,
9051 "Initial length escape value indicating 64-bit DWARF extension");
9052 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9053 "Length of Address Ranges Info");
9054 /* Version number for aranges is still 2, even in DWARF3. */
9055 dw2_asm_output_data (2, 2, "DWARF Version");
9056 if (dwarf_split_debug_info)
9057 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9058 debug_skeleton_info_section,
9059 "Offset of Compilation Unit Info");
9060 else
9061 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9062 debug_info_section,
9063 "Offset of Compilation Unit Info");
9064 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9065 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9067 /* We need to align to twice the pointer size here. */
9068 if (DWARF_ARANGES_PAD_SIZE)
9070 /* Pad using a 2 byte words so that padding is correct for any
9071 pointer size. */
9072 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9073 2 * DWARF2_ADDR_SIZE);
9074 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9075 dw2_asm_output_data (2, 0, NULL);
9078 /* It is necessary not to output these entries if the sections were
9079 not used; if the sections were not used, the length will be 0 and
9080 the address may end up as 0 if the section is discarded by ld
9081 --gc-sections, leaving an invalid (0, 0) entry that can be
9082 confused with the terminator. */
9083 if (text_section_used)
9085 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9086 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9087 text_section_label, "Length");
9089 if (cold_text_section_used)
9091 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9092 "Address");
9093 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9094 cold_text_section_label, "Length");
9097 if (have_multiple_function_sections)
9099 unsigned fde_idx;
9100 dw_fde_ref fde;
9102 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9104 if (DECL_IGNORED_P (fde->decl))
9105 continue;
9106 if (!fde->in_std_section)
9108 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9109 "Address");
9110 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9111 fde->dw_fde_begin, "Length");
9113 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9115 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9116 "Address");
9117 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9118 fde->dw_fde_second_begin, "Length");
9123 /* Output the terminator words. */
9124 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9125 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9128 /* Add a new entry to .debug_ranges. Return the offset at which it
9129 was placed. */
9131 static unsigned int
9132 add_ranges_num (int num)
9134 unsigned int in_use = ranges_table_in_use;
9136 if (in_use == ranges_table_allocated)
9138 ranges_table_allocated += RANGES_TABLE_INCREMENT;
9139 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9140 ranges_table_allocated);
9141 memset (ranges_table + ranges_table_in_use, 0,
9142 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9145 ranges_table[in_use].num = num;
9146 ranges_table_in_use = in_use + 1;
9148 return in_use * 2 * DWARF2_ADDR_SIZE;
9151 /* Add a new entry to .debug_ranges corresponding to a block, or a
9152 range terminator if BLOCK is NULL. */
9154 static unsigned int
9155 add_ranges (const_tree block)
9157 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9160 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9161 When using dwarf_split_debug_info, address attributes in dies destined
9162 for the final executable should be direct references--setting the
9163 parameter force_direct ensures this behavior. */
9165 static void
9166 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9167 bool *added, bool force_direct)
9169 unsigned int in_use = ranges_by_label_in_use;
9170 unsigned int offset;
9172 if (in_use == ranges_by_label_allocated)
9174 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9175 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9176 ranges_by_label,
9177 ranges_by_label_allocated);
9178 memset (ranges_by_label + ranges_by_label_in_use, 0,
9179 RANGES_TABLE_INCREMENT
9180 * sizeof (struct dw_ranges_by_label_struct));
9183 ranges_by_label[in_use].begin = begin;
9184 ranges_by_label[in_use].end = end;
9185 ranges_by_label_in_use = in_use + 1;
9187 offset = add_ranges_num (-(int)in_use - 1);
9188 if (!*added)
9190 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9191 *added = true;
9195 static void
9196 output_ranges (void)
9198 unsigned i;
9199 static const char *const start_fmt = "Offset %#x";
9200 const char *fmt = start_fmt;
9202 for (i = 0; i < ranges_table_in_use; i++)
9204 int block_num = ranges_table[i].num;
9206 if (block_num > 0)
9208 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9209 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9211 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9212 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9214 /* If all code is in the text section, then the compilation
9215 unit base address defaults to DW_AT_low_pc, which is the
9216 base of the text section. */
9217 if (!have_multiple_function_sections)
9219 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9220 text_section_label,
9221 fmt, i * 2 * DWARF2_ADDR_SIZE);
9222 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9223 text_section_label, NULL);
9226 /* Otherwise, the compilation unit base address is zero,
9227 which allows us to use absolute addresses, and not worry
9228 about whether the target supports cross-section
9229 arithmetic. */
9230 else
9232 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9233 fmt, i * 2 * DWARF2_ADDR_SIZE);
9234 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9237 fmt = NULL;
9240 /* Negative block_num stands for an index into ranges_by_label. */
9241 else if (block_num < 0)
9243 int lab_idx = - block_num - 1;
9245 if (!have_multiple_function_sections)
9247 gcc_unreachable ();
9248 #if 0
9249 /* If we ever use add_ranges_by_labels () for a single
9250 function section, all we have to do is to take out
9251 the #if 0 above. */
9252 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9253 ranges_by_label[lab_idx].begin,
9254 text_section_label,
9255 fmt, i * 2 * DWARF2_ADDR_SIZE);
9256 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9257 ranges_by_label[lab_idx].end,
9258 text_section_label, NULL);
9259 #endif
9261 else
9263 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9264 ranges_by_label[lab_idx].begin,
9265 fmt, i * 2 * DWARF2_ADDR_SIZE);
9266 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9267 ranges_by_label[lab_idx].end,
9268 NULL);
9271 else
9273 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9274 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9275 fmt = start_fmt;
9280 /* Data structure containing information about input files. */
9281 struct file_info
9283 const char *path; /* Complete file name. */
9284 const char *fname; /* File name part. */
9285 int length; /* Length of entire string. */
9286 struct dwarf_file_data * file_idx; /* Index in input file table. */
9287 int dir_idx; /* Index in directory table. */
9290 /* Data structure containing information about directories with source
9291 files. */
9292 struct dir_info
9294 const char *path; /* Path including directory name. */
9295 int length; /* Path length. */
9296 int prefix; /* Index of directory entry which is a prefix. */
9297 int count; /* Number of files in this directory. */
9298 int dir_idx; /* Index of directory used as base. */
9301 /* Callback function for file_info comparison. We sort by looking at
9302 the directories in the path. */
9304 static int
9305 file_info_cmp (const void *p1, const void *p2)
9307 const struct file_info *const s1 = (const struct file_info *) p1;
9308 const struct file_info *const s2 = (const struct file_info *) p2;
9309 const unsigned char *cp1;
9310 const unsigned char *cp2;
9312 /* Take care of file names without directories. We need to make sure that
9313 we return consistent values to qsort since some will get confused if
9314 we return the same value when identical operands are passed in opposite
9315 orders. So if neither has a directory, return 0 and otherwise return
9316 1 or -1 depending on which one has the directory. */
9317 if ((s1->path == s1->fname || s2->path == s2->fname))
9318 return (s2->path == s2->fname) - (s1->path == s1->fname);
9320 cp1 = (const unsigned char *) s1->path;
9321 cp2 = (const unsigned char *) s2->path;
9323 while (1)
9325 ++cp1;
9326 ++cp2;
9327 /* Reached the end of the first path? If so, handle like above. */
9328 if ((cp1 == (const unsigned char *) s1->fname)
9329 || (cp2 == (const unsigned char *) s2->fname))
9330 return ((cp2 == (const unsigned char *) s2->fname)
9331 - (cp1 == (const unsigned char *) s1->fname));
9333 /* Character of current path component the same? */
9334 else if (*cp1 != *cp2)
9335 return *cp1 - *cp2;
9339 struct file_name_acquire_data
9341 struct file_info *files;
9342 int used_files;
9343 int max_files;
9346 /* Traversal function for the hash table. */
9348 static int
9349 file_name_acquire (void ** slot, void *data)
9351 struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
9352 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
9353 struct file_info *fi;
9354 const char *f;
9356 gcc_assert (fnad->max_files >= d->emitted_number);
9358 if (! d->emitted_number)
9359 return 1;
9361 gcc_assert (fnad->max_files != fnad->used_files);
9363 fi = fnad->files + fnad->used_files++;
9365 /* Skip all leading "./". */
9366 f = d->filename;
9367 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9368 f += 2;
9370 /* Create a new array entry. */
9371 fi->path = f;
9372 fi->length = strlen (f);
9373 fi->file_idx = d;
9375 /* Search for the file name part. */
9376 f = strrchr (f, DIR_SEPARATOR);
9377 #if defined (DIR_SEPARATOR_2)
9379 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9381 if (g != NULL)
9383 if (f == NULL || f < g)
9384 f = g;
9387 #endif
9389 fi->fname = f == NULL ? fi->path : f + 1;
9390 return 1;
9393 /* Output the directory table and the file name table. We try to minimize
9394 the total amount of memory needed. A heuristic is used to avoid large
9395 slowdowns with many input files. */
9397 static void
9398 output_file_names (void)
9400 struct file_name_acquire_data fnad;
9401 int numfiles;
9402 struct file_info *files;
9403 struct dir_info *dirs;
9404 int *saved;
9405 int *savehere;
9406 int *backmap;
9407 int ndirs;
9408 int idx_offset;
9409 int i;
9411 if (!last_emitted_file)
9413 dw2_asm_output_data (1, 0, "End directory table");
9414 dw2_asm_output_data (1, 0, "End file name table");
9415 return;
9418 numfiles = last_emitted_file->emitted_number;
9420 /* Allocate the various arrays we need. */
9421 files = XALLOCAVEC (struct file_info, numfiles);
9422 dirs = XALLOCAVEC (struct dir_info, numfiles);
9424 fnad.files = files;
9425 fnad.used_files = 0;
9426 fnad.max_files = numfiles;
9427 htab_traverse (file_table, file_name_acquire, &fnad);
9428 gcc_assert (fnad.used_files == fnad.max_files);
9430 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9432 /* Find all the different directories used. */
9433 dirs[0].path = files[0].path;
9434 dirs[0].length = files[0].fname - files[0].path;
9435 dirs[0].prefix = -1;
9436 dirs[0].count = 1;
9437 dirs[0].dir_idx = 0;
9438 files[0].dir_idx = 0;
9439 ndirs = 1;
9441 for (i = 1; i < numfiles; i++)
9442 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9443 && memcmp (dirs[ndirs - 1].path, files[i].path,
9444 dirs[ndirs - 1].length) == 0)
9446 /* Same directory as last entry. */
9447 files[i].dir_idx = ndirs - 1;
9448 ++dirs[ndirs - 1].count;
9450 else
9452 int j;
9454 /* This is a new directory. */
9455 dirs[ndirs].path = files[i].path;
9456 dirs[ndirs].length = files[i].fname - files[i].path;
9457 dirs[ndirs].count = 1;
9458 dirs[ndirs].dir_idx = ndirs;
9459 files[i].dir_idx = ndirs;
9461 /* Search for a prefix. */
9462 dirs[ndirs].prefix = -1;
9463 for (j = 0; j < ndirs; j++)
9464 if (dirs[j].length < dirs[ndirs].length
9465 && dirs[j].length > 1
9466 && (dirs[ndirs].prefix == -1
9467 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9468 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9469 dirs[ndirs].prefix = j;
9471 ++ndirs;
9474 /* Now to the actual work. We have to find a subset of the directories which
9475 allow expressing the file name using references to the directory table
9476 with the least amount of characters. We do not do an exhaustive search
9477 where we would have to check out every combination of every single
9478 possible prefix. Instead we use a heuristic which provides nearly optimal
9479 results in most cases and never is much off. */
9480 saved = XALLOCAVEC (int, ndirs);
9481 savehere = XALLOCAVEC (int, ndirs);
9483 memset (saved, '\0', ndirs * sizeof (saved[0]));
9484 for (i = 0; i < ndirs; i++)
9486 int j;
9487 int total;
9489 /* We can always save some space for the current directory. But this
9490 does not mean it will be enough to justify adding the directory. */
9491 savehere[i] = dirs[i].length;
9492 total = (savehere[i] - saved[i]) * dirs[i].count;
9494 for (j = i + 1; j < ndirs; j++)
9496 savehere[j] = 0;
9497 if (saved[j] < dirs[i].length)
9499 /* Determine whether the dirs[i] path is a prefix of the
9500 dirs[j] path. */
9501 int k;
9503 k = dirs[j].prefix;
9504 while (k != -1 && k != (int) i)
9505 k = dirs[k].prefix;
9507 if (k == (int) i)
9509 /* Yes it is. We can possibly save some memory by
9510 writing the filenames in dirs[j] relative to
9511 dirs[i]. */
9512 savehere[j] = dirs[i].length;
9513 total += (savehere[j] - saved[j]) * dirs[j].count;
9518 /* Check whether we can save enough to justify adding the dirs[i]
9519 directory. */
9520 if (total > dirs[i].length + 1)
9522 /* It's worthwhile adding. */
9523 for (j = i; j < ndirs; j++)
9524 if (savehere[j] > 0)
9526 /* Remember how much we saved for this directory so far. */
9527 saved[j] = savehere[j];
9529 /* Remember the prefix directory. */
9530 dirs[j].dir_idx = i;
9535 /* Emit the directory name table. */
9536 idx_offset = dirs[0].length > 0 ? 1 : 0;
9537 for (i = 1 - idx_offset; i < ndirs; i++)
9538 dw2_asm_output_nstring (dirs[i].path,
9539 dirs[i].length
9540 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9541 "Directory Entry: %#x", i + idx_offset);
9543 dw2_asm_output_data (1, 0, "End directory table");
9545 /* We have to emit them in the order of emitted_number since that's
9546 used in the debug info generation. To do this efficiently we
9547 generate a back-mapping of the indices first. */
9548 backmap = XALLOCAVEC (int, numfiles);
9549 for (i = 0; i < numfiles; i++)
9550 backmap[files[i].file_idx->emitted_number - 1] = i;
9552 /* Now write all the file names. */
9553 for (i = 0; i < numfiles; i++)
9555 int file_idx = backmap[i];
9556 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9558 #ifdef VMS_DEBUGGING_INFO
9559 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
9561 /* Setting these fields can lead to debugger miscomparisons,
9562 but VMS Debug requires them to be set correctly. */
9564 int ver;
9565 long long cdt;
9566 long siz;
9567 int maxfilelen = strlen (files[file_idx].path)
9568 + dirs[dir_idx].length
9569 + MAX_VMS_VERSION_LEN + 1;
9570 char *filebuf = XALLOCAVEC (char, maxfilelen);
9572 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
9573 snprintf (filebuf, maxfilelen, "%s;%d",
9574 files[file_idx].path + dirs[dir_idx].length, ver);
9576 dw2_asm_output_nstring
9577 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
9579 /* Include directory index. */
9580 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9582 /* Modification time. */
9583 dw2_asm_output_data_uleb128
9584 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
9585 ? cdt : 0,
9586 NULL);
9588 /* File length in bytes. */
9589 dw2_asm_output_data_uleb128
9590 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
9591 ? siz : 0,
9592 NULL);
9593 #else
9594 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
9595 "File Entry: %#x", (unsigned) i + 1);
9597 /* Include directory index. */
9598 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9600 /* Modification time. */
9601 dw2_asm_output_data_uleb128 (0, NULL);
9603 /* File length in bytes. */
9604 dw2_asm_output_data_uleb128 (0, NULL);
9605 #endif /* VMS_DEBUGGING_INFO */
9608 dw2_asm_output_data (1, 0, "End file name table");
9612 /* Output one line number table into the .debug_line section. */
9614 static void
9615 output_one_line_info_table (dw_line_info_table *table)
9617 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9618 unsigned int current_line = 1;
9619 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
9620 dw_line_info_entry *ent;
9621 size_t i;
9623 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
9625 switch (ent->opcode)
9627 case LI_set_address:
9628 /* ??? Unfortunately, we have little choice here currently, and
9629 must always use the most general form. GCC does not know the
9630 address delta itself, so we can't use DW_LNS_advance_pc. Many
9631 ports do have length attributes which will give an upper bound
9632 on the address range. We could perhaps use length attributes
9633 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
9634 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
9636 /* This can handle any delta. This takes
9637 4+DWARF2_ADDR_SIZE bytes. */
9638 dw2_asm_output_data (1, 0, "set address %s", line_label);
9639 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9640 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9641 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9642 break;
9644 case LI_set_line:
9645 if (ent->val == current_line)
9647 /* We still need to start a new row, so output a copy insn. */
9648 dw2_asm_output_data (1, DW_LNS_copy,
9649 "copy line %u", current_line);
9651 else
9653 int line_offset = ent->val - current_line;
9654 int line_delta = line_offset - DWARF_LINE_BASE;
9656 current_line = ent->val;
9657 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9659 /* This can handle deltas from -10 to 234, using the current
9660 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
9661 This takes 1 byte. */
9662 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9663 "line %u", current_line);
9665 else
9667 /* This can handle any delta. This takes at least 4 bytes,
9668 depending on the value being encoded. */
9669 dw2_asm_output_data (1, DW_LNS_advance_line,
9670 "advance to line %u", current_line);
9671 dw2_asm_output_data_sleb128 (line_offset, NULL);
9672 dw2_asm_output_data (1, DW_LNS_copy, NULL);
9675 break;
9677 case LI_set_file:
9678 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
9679 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9680 break;
9682 case LI_set_column:
9683 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
9684 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9685 break;
9687 case LI_negate_stmt:
9688 current_is_stmt = !current_is_stmt;
9689 dw2_asm_output_data (1, DW_LNS_negate_stmt,
9690 "is_stmt %d", current_is_stmt);
9691 break;
9693 case LI_set_prologue_end:
9694 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
9695 "set prologue end");
9696 break;
9698 case LI_set_epilogue_begin:
9699 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
9700 "set epilogue begin");
9701 break;
9703 case LI_set_discriminator:
9704 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
9705 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
9706 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
9707 dw2_asm_output_data_uleb128 (ent->val, NULL);
9708 break;
9712 /* Emit debug info for the address of the end of the table. */
9713 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
9714 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9715 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9716 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
9718 dw2_asm_output_data (1, 0, "end sequence");
9719 dw2_asm_output_data_uleb128 (1, NULL);
9720 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9723 /* Output the source line number correspondence information. This
9724 information goes into the .debug_line section. */
9726 static void
9727 output_line_info (bool prologue_only)
9729 char l1[20], l2[20], p1[20], p2[20];
9730 int ver = dwarf_version;
9731 bool saw_one = false;
9732 int opc;
9734 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
9735 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
9736 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
9737 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
9739 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9740 dw2_asm_output_data (4, 0xffffffff,
9741 "Initial length escape value indicating 64-bit DWARF extension");
9742 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
9743 "Length of Source Line Info");
9744 ASM_OUTPUT_LABEL (asm_out_file, l1);
9746 dw2_asm_output_data (2, ver, "DWARF Version");
9747 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
9748 ASM_OUTPUT_LABEL (asm_out_file, p1);
9750 /* Define the architecture-dependent minimum instruction length (in bytes).
9751 In this implementation of DWARF, this field is used for information
9752 purposes only. Since GCC generates assembly language, we have no
9753 a priori knowledge of how many instruction bytes are generated for each
9754 source line, and therefore can use only the DW_LNE_set_address and
9755 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
9756 this as '1', which is "correct enough" for all architectures,
9757 and don't let the target override. */
9758 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
9760 if (ver >= 4)
9761 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
9762 "Maximum Operations Per Instruction");
9763 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
9764 "Default is_stmt_start flag");
9765 dw2_asm_output_data (1, DWARF_LINE_BASE,
9766 "Line Base Value (Special Opcodes)");
9767 dw2_asm_output_data (1, DWARF_LINE_RANGE,
9768 "Line Range Value (Special Opcodes)");
9769 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
9770 "Special Opcode Base");
9772 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
9774 int n_op_args;
9775 switch (opc)
9777 case DW_LNS_advance_pc:
9778 case DW_LNS_advance_line:
9779 case DW_LNS_set_file:
9780 case DW_LNS_set_column:
9781 case DW_LNS_fixed_advance_pc:
9782 case DW_LNS_set_isa:
9783 n_op_args = 1;
9784 break;
9785 default:
9786 n_op_args = 0;
9787 break;
9790 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
9791 opc, n_op_args);
9794 /* Write out the information about the files we use. */
9795 output_file_names ();
9796 ASM_OUTPUT_LABEL (asm_out_file, p2);
9797 if (prologue_only)
9799 /* Output the marker for the end of the line number info. */
9800 ASM_OUTPUT_LABEL (asm_out_file, l2);
9801 return;
9804 if (separate_line_info)
9806 dw_line_info_table *table;
9807 size_t i;
9809 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
9810 if (table->in_use)
9812 output_one_line_info_table (table);
9813 saw_one = true;
9816 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
9818 output_one_line_info_table (cold_text_section_line_info);
9819 saw_one = true;
9822 /* ??? Some Darwin linkers crash on a .debug_line section with no
9823 sequences. Further, merely a DW_LNE_end_sequence entry is not
9824 sufficient -- the address column must also be initialized.
9825 Make sure to output at least one set_address/end_sequence pair,
9826 choosing .text since that section is always present. */
9827 if (text_section_line_info->in_use || !saw_one)
9828 output_one_line_info_table (text_section_line_info);
9830 /* Output the marker for the end of the line number info. */
9831 ASM_OUTPUT_LABEL (asm_out_file, l2);
9834 /* Given a pointer to a tree node for some base type, return a pointer to
9835 a DIE that describes the given type.
9837 This routine must only be called for GCC type nodes that correspond to
9838 Dwarf base (fundamental) types. */
9840 static dw_die_ref
9841 base_type_die (tree type)
9843 dw_die_ref base_type_result;
9844 enum dwarf_type encoding;
9846 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
9847 return 0;
9849 /* If this is a subtype that should not be emitted as a subrange type,
9850 use the base type. See subrange_type_for_debug_p. */
9851 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
9852 type = TREE_TYPE (type);
9854 switch (TREE_CODE (type))
9856 case INTEGER_TYPE:
9857 if ((dwarf_version >= 4 || !dwarf_strict)
9858 && TYPE_NAME (type)
9859 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
9860 && DECL_IS_BUILTIN (TYPE_NAME (type))
9861 && DECL_NAME (TYPE_NAME (type)))
9863 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
9864 if (strcmp (name, "char16_t") == 0
9865 || strcmp (name, "char32_t") == 0)
9867 encoding = DW_ATE_UTF;
9868 break;
9871 if (TYPE_STRING_FLAG (type))
9873 if (TYPE_UNSIGNED (type))
9874 encoding = DW_ATE_unsigned_char;
9875 else
9876 encoding = DW_ATE_signed_char;
9878 else if (TYPE_UNSIGNED (type))
9879 encoding = DW_ATE_unsigned;
9880 else
9881 encoding = DW_ATE_signed;
9882 break;
9884 case REAL_TYPE:
9885 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
9887 if (dwarf_version >= 3 || !dwarf_strict)
9888 encoding = DW_ATE_decimal_float;
9889 else
9890 encoding = DW_ATE_lo_user;
9892 else
9893 encoding = DW_ATE_float;
9894 break;
9896 case FIXED_POINT_TYPE:
9897 if (!(dwarf_version >= 3 || !dwarf_strict))
9898 encoding = DW_ATE_lo_user;
9899 else if (TYPE_UNSIGNED (type))
9900 encoding = DW_ATE_unsigned_fixed;
9901 else
9902 encoding = DW_ATE_signed_fixed;
9903 break;
9905 /* Dwarf2 doesn't know anything about complex ints, so use
9906 a user defined type for it. */
9907 case COMPLEX_TYPE:
9908 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
9909 encoding = DW_ATE_complex_float;
9910 else
9911 encoding = DW_ATE_lo_user;
9912 break;
9914 case BOOLEAN_TYPE:
9915 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
9916 encoding = DW_ATE_boolean;
9917 break;
9919 default:
9920 /* No other TREE_CODEs are Dwarf fundamental types. */
9921 gcc_unreachable ();
9924 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
9926 add_AT_unsigned (base_type_result, DW_AT_byte_size,
9927 int_size_in_bytes (type));
9928 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
9929 add_pubtype (type, base_type_result);
9931 return base_type_result;
9934 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
9935 given input type is a Dwarf "fundamental" type. Otherwise return null. */
9937 static inline int
9938 is_base_type (tree type)
9940 switch (TREE_CODE (type))
9942 case ERROR_MARK:
9943 case VOID_TYPE:
9944 case INTEGER_TYPE:
9945 case REAL_TYPE:
9946 case FIXED_POINT_TYPE:
9947 case COMPLEX_TYPE:
9948 case BOOLEAN_TYPE:
9949 return 1;
9951 case ARRAY_TYPE:
9952 case RECORD_TYPE:
9953 case UNION_TYPE:
9954 case QUAL_UNION_TYPE:
9955 case ENUMERAL_TYPE:
9956 case FUNCTION_TYPE:
9957 case METHOD_TYPE:
9958 case POINTER_TYPE:
9959 case REFERENCE_TYPE:
9960 case NULLPTR_TYPE:
9961 case OFFSET_TYPE:
9962 case LANG_TYPE:
9963 case VECTOR_TYPE:
9964 return 0;
9966 default:
9967 gcc_unreachable ();
9970 return 0;
9973 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
9974 node, return the size in bits for the type if it is a constant, or else
9975 return the alignment for the type if the type's size is not constant, or
9976 else return BITS_PER_WORD if the type actually turns out to be an
9977 ERROR_MARK node. */
9979 static inline unsigned HOST_WIDE_INT
9980 simple_type_size_in_bits (const_tree type)
9982 if (TREE_CODE (type) == ERROR_MARK)
9983 return BITS_PER_WORD;
9984 else if (TYPE_SIZE (type) == NULL_TREE)
9985 return 0;
9986 else if (host_integerp (TYPE_SIZE (type), 1))
9987 return tree_low_cst (TYPE_SIZE (type), 1);
9988 else
9989 return TYPE_ALIGN (type);
9992 /* Similarly, but return a double_int instead of UHWI. */
9994 static inline double_int
9995 double_int_type_size_in_bits (const_tree type)
9997 if (TREE_CODE (type) == ERROR_MARK)
9998 return double_int::from_uhwi (BITS_PER_WORD);
9999 else if (TYPE_SIZE (type) == NULL_TREE)
10000 return double_int_zero;
10001 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10002 return tree_to_double_int (TYPE_SIZE (type));
10003 else
10004 return double_int::from_uhwi (TYPE_ALIGN (type));
10007 /* Given a pointer to a tree node for a subrange type, return a pointer
10008 to a DIE that describes the given type. */
10010 static dw_die_ref
10011 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10013 dw_die_ref subrange_die;
10014 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10016 if (context_die == NULL)
10017 context_die = comp_unit_die ();
10019 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10021 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10023 /* The size of the subrange type and its base type do not match,
10024 so we need to generate a size attribute for the subrange type. */
10025 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10028 if (low)
10029 add_bound_info (subrange_die, DW_AT_lower_bound, low);
10030 if (high)
10031 add_bound_info (subrange_die, DW_AT_upper_bound, high);
10033 return subrange_die;
10036 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10037 entry that chains various modifiers in front of the given type. */
10039 static dw_die_ref
10040 modified_type_die (tree type, int is_const_type, int is_volatile_type,
10041 dw_die_ref context_die)
10043 enum tree_code code = TREE_CODE (type);
10044 dw_die_ref mod_type_die;
10045 dw_die_ref sub_die = NULL;
10046 tree item_type = NULL;
10047 tree qualified_type;
10048 tree name, low, high;
10049 dw_die_ref mod_scope;
10051 if (code == ERROR_MARK)
10052 return NULL;
10054 /* See if we already have the appropriately qualified variant of
10055 this type. */
10056 qualified_type
10057 = get_qualified_type (type,
10058 ((is_const_type ? TYPE_QUAL_CONST : 0)
10059 | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
10061 if (qualified_type == sizetype
10062 && TYPE_NAME (qualified_type)
10063 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10065 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10067 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10068 && TYPE_PRECISION (t)
10069 == TYPE_PRECISION (qualified_type)
10070 && TYPE_UNSIGNED (t)
10071 == TYPE_UNSIGNED (qualified_type));
10072 qualified_type = t;
10075 /* If we do, then we can just use its DIE, if it exists. */
10076 if (qualified_type)
10078 mod_type_die = lookup_type_die (qualified_type);
10079 if (mod_type_die)
10080 return mod_type_die;
10083 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10085 /* Handle C typedef types. */
10086 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10087 && !DECL_ARTIFICIAL (name))
10089 tree dtype = TREE_TYPE (name);
10091 if (qualified_type == dtype)
10093 /* For a named type, use the typedef. */
10094 gen_type_die (qualified_type, context_die);
10095 return lookup_type_die (qualified_type);
10097 else if (is_const_type < TYPE_READONLY (dtype)
10098 || is_volatile_type < TYPE_VOLATILE (dtype)
10099 || (is_const_type <= TYPE_READONLY (dtype)
10100 && is_volatile_type <= TYPE_VOLATILE (dtype)
10101 && DECL_ORIGINAL_TYPE (name) != type))
10102 /* cv-unqualified version of named type. Just use the unnamed
10103 type to which it refers. */
10104 return modified_type_die (DECL_ORIGINAL_TYPE (name),
10105 is_const_type, is_volatile_type,
10106 context_die);
10107 /* Else cv-qualified version of named type; fall through. */
10110 mod_scope = scope_die_for (type, context_die);
10112 if (is_const_type
10113 /* If both is_const_type and is_volatile_type, prefer the path
10114 which leads to a qualified type. */
10115 && (!is_volatile_type
10116 || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
10117 || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
10119 mod_type_die = new_die (DW_TAG_const_type, mod_scope, type);
10120 sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
10122 else if (is_volatile_type)
10124 mod_type_die = new_die (DW_TAG_volatile_type, mod_scope, type);
10125 sub_die = modified_type_die (type, is_const_type, 0, context_die);
10127 else if (code == POINTER_TYPE)
10129 mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10130 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10131 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10132 item_type = TREE_TYPE (type);
10133 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10134 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10135 TYPE_ADDR_SPACE (item_type));
10137 else if (code == REFERENCE_TYPE)
10139 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10140 mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10141 type);
10142 else
10143 mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10144 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10145 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10146 item_type = TREE_TYPE (type);
10147 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10148 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10149 TYPE_ADDR_SPACE (item_type));
10151 else if (code == INTEGER_TYPE
10152 && TREE_TYPE (type) != NULL_TREE
10153 && subrange_type_for_debug_p (type, &low, &high))
10155 mod_type_die = subrange_type_die (type, low, high, context_die);
10156 item_type = TREE_TYPE (type);
10158 else if (is_base_type (type))
10159 mod_type_die = base_type_die (type);
10160 else
10162 gen_type_die (type, context_die);
10164 /* We have to get the type_main_variant here (and pass that to the
10165 `lookup_type_die' routine) because the ..._TYPE node we have
10166 might simply be a *copy* of some original type node (where the
10167 copy was created to help us keep track of typedef names) and
10168 that copy might have a different TYPE_UID from the original
10169 ..._TYPE node. */
10170 if (TREE_CODE (type) != VECTOR_TYPE)
10171 return lookup_type_die (type_main_variant (type));
10172 else
10173 /* Vectors have the debugging information in the type,
10174 not the main variant. */
10175 return lookup_type_die (type);
10178 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
10179 don't output a DW_TAG_typedef, since there isn't one in the
10180 user's program; just attach a DW_AT_name to the type.
10181 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10182 if the base type already has the same name. */
10183 if (name
10184 && ((TREE_CODE (name) != TYPE_DECL
10185 && (qualified_type == TYPE_MAIN_VARIANT (type)
10186 || (!is_const_type && !is_volatile_type)))
10187 || (TREE_CODE (name) == TYPE_DECL
10188 && TREE_TYPE (name) == qualified_type
10189 && DECL_NAME (name))))
10191 if (TREE_CODE (name) == TYPE_DECL)
10192 /* Could just call add_name_and_src_coords_attributes here,
10193 but since this is a builtin type it doesn't have any
10194 useful source coordinates anyway. */
10195 name = DECL_NAME (name);
10196 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10198 /* This probably indicates a bug. */
10199 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10201 name = TYPE_NAME (type);
10202 if (name
10203 && TREE_CODE (name) == TYPE_DECL)
10204 name = DECL_NAME (name);
10205 add_name_attribute (mod_type_die,
10206 name ? IDENTIFIER_POINTER (name) : "__unknown__");
10209 if (qualified_type)
10210 equate_type_number_to_die (qualified_type, mod_type_die);
10212 if (item_type)
10213 /* We must do this after the equate_type_number_to_die call, in case
10214 this is a recursive type. This ensures that the modified_type_die
10215 recursion will terminate even if the type is recursive. Recursive
10216 types are possible in Ada. */
10217 sub_die = modified_type_die (item_type,
10218 TYPE_READONLY (item_type),
10219 TYPE_VOLATILE (item_type),
10220 context_die);
10222 if (sub_die != NULL)
10223 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10225 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10226 if (TYPE_ARTIFICIAL (type))
10227 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10229 return mod_type_die;
10232 /* Generate DIEs for the generic parameters of T.
10233 T must be either a generic type or a generic function.
10234 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
10236 static void
10237 gen_generic_params_dies (tree t)
10239 tree parms, args;
10240 int parms_num, i;
10241 dw_die_ref die = NULL;
10243 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10244 return;
10246 if (TYPE_P (t))
10247 die = lookup_type_die (t);
10248 else if (DECL_P (t))
10249 die = lookup_decl_die (t);
10251 gcc_assert (die);
10253 parms = lang_hooks.get_innermost_generic_parms (t);
10254 if (!parms)
10255 /* T has no generic parameter. It means T is neither a generic type
10256 or function. End of story. */
10257 return;
10259 parms_num = TREE_VEC_LENGTH (parms);
10260 args = lang_hooks.get_innermost_generic_args (t);
10261 for (i = 0; i < parms_num; i++)
10263 tree parm, arg, arg_pack_elems;
10265 parm = TREE_VEC_ELT (parms, i);
10266 arg = TREE_VEC_ELT (args, i);
10267 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
10268 gcc_assert (parm && TREE_VALUE (parm) && arg);
10270 if (parm && TREE_VALUE (parm) && arg)
10272 /* If PARM represents a template parameter pack,
10273 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
10274 by DW_TAG_template_*_parameter DIEs for the argument
10275 pack elements of ARG. Note that ARG would then be
10276 an argument pack. */
10277 if (arg_pack_elems)
10278 template_parameter_pack_die (TREE_VALUE (parm),
10279 arg_pack_elems,
10280 die);
10281 else
10282 generic_parameter_die (TREE_VALUE (parm), arg,
10283 true /* Emit DW_AT_name */, die);
10288 /* Create and return a DIE for PARM which should be
10289 the representation of a generic type parameter.
10290 For instance, in the C++ front end, PARM would be a template parameter.
10291 ARG is the argument to PARM.
10292 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
10293 name of the PARM.
10294 PARENT_DIE is the parent DIE which the new created DIE should be added to,
10295 as a child node. */
10297 static dw_die_ref
10298 generic_parameter_die (tree parm, tree arg,
10299 bool emit_name_p,
10300 dw_die_ref parent_die)
10302 dw_die_ref tmpl_die = NULL;
10303 const char *name = NULL;
10305 if (!parm || !DECL_NAME (parm) || !arg)
10306 return NULL;
10308 /* We support non-type generic parameters and arguments,
10309 type generic parameters and arguments, as well as
10310 generic generic parameters (a.k.a. template template parameters in C++)
10311 and arguments. */
10312 if (TREE_CODE (parm) == PARM_DECL)
10313 /* PARM is a nontype generic parameter */
10314 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10315 else if (TREE_CODE (parm) == TYPE_DECL)
10316 /* PARM is a type generic parameter. */
10317 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10318 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10319 /* PARM is a generic generic parameter.
10320 Its DIE is a GNU extension. It shall have a
10321 DW_AT_name attribute to represent the name of the template template
10322 parameter, and a DW_AT_GNU_template_name attribute to represent the
10323 name of the template template argument. */
10324 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10325 parent_die, parm);
10326 else
10327 gcc_unreachable ();
10329 if (tmpl_die)
10331 tree tmpl_type;
10333 /* If PARM is a generic parameter pack, it means we are
10334 emitting debug info for a template argument pack element.
10335 In other terms, ARG is a template argument pack element.
10336 In that case, we don't emit any DW_AT_name attribute for
10337 the die. */
10338 if (emit_name_p)
10340 name = IDENTIFIER_POINTER (DECL_NAME (parm));
10341 gcc_assert (name);
10342 add_AT_string (tmpl_die, DW_AT_name, name);
10345 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10347 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10348 TMPL_DIE should have a child DW_AT_type attribute that is set
10349 to the type of the argument to PARM, which is ARG.
10350 If PARM is a type generic parameter, TMPL_DIE should have a
10351 child DW_AT_type that is set to ARG. */
10352 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10353 add_type_attribute (tmpl_die, tmpl_type, 0,
10354 TREE_THIS_VOLATILE (tmpl_type),
10355 parent_die);
10357 else
10359 /* So TMPL_DIE is a DIE representing a
10360 a generic generic template parameter, a.k.a template template
10361 parameter in C++ and arg is a template. */
10363 /* The DW_AT_GNU_template_name attribute of the DIE must be set
10364 to the name of the argument. */
10365 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
10366 if (name)
10367 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
10370 if (TREE_CODE (parm) == PARM_DECL)
10371 /* So PARM is a non-type generic parameter.
10372 DWARF3 5.6.8 says we must set a DW_AT_const_value child
10373 attribute of TMPL_DIE which value represents the value
10374 of ARG.
10375 We must be careful here:
10376 The value of ARG might reference some function decls.
10377 We might currently be emitting debug info for a generic
10378 type and types are emitted before function decls, we don't
10379 know if the function decls referenced by ARG will actually be
10380 emitted after cgraph computations.
10381 So must defer the generation of the DW_AT_const_value to
10382 after cgraph is ready. */
10383 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
10386 return tmpl_die;
10389 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
10390 PARM_PACK must be a template parameter pack. The returned DIE
10391 will be child DIE of PARENT_DIE. */
10393 static dw_die_ref
10394 template_parameter_pack_die (tree parm_pack,
10395 tree parm_pack_args,
10396 dw_die_ref parent_die)
10398 dw_die_ref die;
10399 int j;
10401 gcc_assert (parent_die && parm_pack);
10403 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
10404 add_name_and_src_coords_attributes (die, parm_pack);
10405 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
10406 generic_parameter_die (parm_pack,
10407 TREE_VEC_ELT (parm_pack_args, j),
10408 false /* Don't emit DW_AT_name */,
10409 die);
10410 return die;
10413 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
10414 an enumerated type. */
10416 static inline int
10417 type_is_enum (const_tree type)
10419 return TREE_CODE (type) == ENUMERAL_TYPE;
10422 /* Return the DBX register number described by a given RTL node. */
10424 static unsigned int
10425 dbx_reg_number (const_rtx rtl)
10427 unsigned regno = REGNO (rtl);
10429 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10431 #ifdef LEAF_REG_REMAP
10432 if (crtl->uses_only_leaf_regs)
10434 int leaf_reg = LEAF_REG_REMAP (regno);
10435 if (leaf_reg != -1)
10436 regno = (unsigned) leaf_reg;
10438 #endif
10440 regno = DBX_REGISTER_NUMBER (regno);
10441 gcc_assert (regno != INVALID_REGNUM);
10442 return regno;
10445 /* Optionally add a DW_OP_piece term to a location description expression.
10446 DW_OP_piece is only added if the location description expression already
10447 doesn't end with DW_OP_piece. */
10449 static void
10450 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10452 dw_loc_descr_ref loc;
10454 if (*list_head != NULL)
10456 /* Find the end of the chain. */
10457 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10460 if (loc->dw_loc_opc != DW_OP_piece)
10461 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10465 /* Return a location descriptor that designates a machine register or
10466 zero if there is none. */
10468 static dw_loc_descr_ref
10469 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10471 rtx regs;
10473 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10474 return 0;
10476 /* We only use "frame base" when we're sure we're talking about the
10477 post-prologue local stack frame. We do this by *not* running
10478 register elimination until this point, and recognizing the special
10479 argument pointer and soft frame pointer rtx's.
10480 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
10481 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
10482 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
10484 dw_loc_descr_ref result = NULL;
10486 if (dwarf_version >= 4 || !dwarf_strict)
10488 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
10489 initialized);
10490 if (result)
10491 add_loc_descr (&result,
10492 new_loc_descr (DW_OP_stack_value, 0, 0));
10494 return result;
10497 regs = targetm.dwarf_register_span (rtl);
10499 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
10500 return multiple_reg_loc_descriptor (rtl, regs, initialized);
10501 else
10503 unsigned int dbx_regnum = dbx_reg_number (rtl);
10504 if (dbx_regnum == IGNORED_DWARF_REGNUM)
10505 return 0;
10506 return one_reg_loc_descriptor (dbx_regnum, initialized);
10510 /* Return a location descriptor that designates a machine register for
10511 a given hard register number. */
10513 static dw_loc_descr_ref
10514 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
10516 dw_loc_descr_ref reg_loc_descr;
10518 if (regno <= 31)
10519 reg_loc_descr
10520 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
10521 else
10522 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
10524 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10525 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10527 return reg_loc_descr;
10530 /* Given an RTL of a register, return a location descriptor that
10531 designates a value that spans more than one register. */
10533 static dw_loc_descr_ref
10534 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
10535 enum var_init_status initialized)
10537 int nregs, size, i;
10538 unsigned reg;
10539 dw_loc_descr_ref loc_result = NULL;
10541 reg = REGNO (rtl);
10542 #ifdef LEAF_REG_REMAP
10543 if (crtl->uses_only_leaf_regs)
10545 int leaf_reg = LEAF_REG_REMAP (reg);
10546 if (leaf_reg != -1)
10547 reg = (unsigned) leaf_reg;
10549 #endif
10550 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
10551 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
10553 /* Simple, contiguous registers. */
10554 if (regs == NULL_RTX)
10556 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
10558 loc_result = NULL;
10559 while (nregs--)
10561 dw_loc_descr_ref t;
10563 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
10564 VAR_INIT_STATUS_INITIALIZED);
10565 add_loc_descr (&loc_result, t);
10566 add_loc_descr_op_piece (&loc_result, size);
10567 ++reg;
10569 return loc_result;
10572 /* Now onto stupid register sets in non contiguous locations. */
10574 gcc_assert (GET_CODE (regs) == PARALLEL);
10576 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10577 loc_result = NULL;
10579 for (i = 0; i < XVECLEN (regs, 0); ++i)
10581 dw_loc_descr_ref t;
10583 t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
10584 VAR_INIT_STATUS_INITIALIZED);
10585 add_loc_descr (&loc_result, t);
10586 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10587 add_loc_descr_op_piece (&loc_result, size);
10590 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10591 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10592 return loc_result;
10595 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
10597 /* Return a location descriptor that designates a constant i,
10598 as a compound operation from constant (i >> shift), constant shift
10599 and DW_OP_shl. */
10601 static dw_loc_descr_ref
10602 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10604 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
10605 add_loc_descr (&ret, int_loc_descriptor (shift));
10606 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
10607 return ret;
10610 /* Return a location descriptor that designates a constant. */
10612 static dw_loc_descr_ref
10613 int_loc_descriptor (HOST_WIDE_INT i)
10615 enum dwarf_location_atom op;
10617 /* Pick the smallest representation of a constant, rather than just
10618 defaulting to the LEB encoding. */
10619 if (i >= 0)
10621 int clz = clz_hwi (i);
10622 int ctz = ctz_hwi (i);
10623 if (i <= 31)
10624 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
10625 else if (i <= 0xff)
10626 op = DW_OP_const1u;
10627 else if (i <= 0xffff)
10628 op = DW_OP_const2u;
10629 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10630 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10631 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
10632 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
10633 while DW_OP_const4u is 5 bytes. */
10634 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
10635 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10636 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10637 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
10638 while DW_OP_const4u is 5 bytes. */
10639 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10640 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10641 op = DW_OP_const4u;
10642 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10643 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10644 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
10645 while DW_OP_constu of constant >= 0x100000000 takes at least
10646 6 bytes. */
10647 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10648 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10649 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
10650 >= HOST_BITS_PER_WIDE_INT)
10651 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
10652 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
10653 while DW_OP_constu takes in this case at least 6 bytes. */
10654 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
10655 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10656 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10657 && size_of_uleb128 (i) > 6)
10658 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
10659 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
10660 else
10661 op = DW_OP_constu;
10663 else
10665 if (i >= -0x80)
10666 op = DW_OP_const1s;
10667 else if (i >= -0x8000)
10668 op = DW_OP_const2s;
10669 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10671 if (size_of_int_loc_descriptor (i) < 5)
10673 dw_loc_descr_ref ret = int_loc_descriptor (-i);
10674 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10675 return ret;
10677 op = DW_OP_const4s;
10679 else
10681 if (size_of_int_loc_descriptor (i)
10682 < (unsigned long) 1 + size_of_sleb128 (i))
10684 dw_loc_descr_ref ret = int_loc_descriptor (-i);
10685 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10686 return ret;
10688 op = DW_OP_consts;
10692 return new_loc_descr (op, i, 0);
10695 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
10696 without actually allocating it. */
10698 static unsigned long
10699 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10701 return size_of_int_loc_descriptor (i >> shift)
10702 + size_of_int_loc_descriptor (shift)
10703 + 1;
10706 /* Return size_of_locs (int_loc_descriptor (i)) without
10707 actually allocating it. */
10709 static unsigned long
10710 size_of_int_loc_descriptor (HOST_WIDE_INT i)
10712 unsigned long s;
10714 if (i >= 0)
10716 int clz, ctz;
10717 if (i <= 31)
10718 return 1;
10719 else if (i <= 0xff)
10720 return 2;
10721 else if (i <= 0xffff)
10722 return 3;
10723 clz = clz_hwi (i);
10724 ctz = ctz_hwi (i);
10725 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10726 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10727 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10728 - clz - 5);
10729 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10730 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10731 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10732 - clz - 8);
10733 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10734 return 5;
10735 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
10736 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10737 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10738 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10739 - clz - 8);
10740 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10741 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
10742 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10743 - clz - 16);
10744 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10745 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10746 && s > 6)
10747 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10748 - clz - 32);
10749 else
10750 return 1 + s;
10752 else
10754 if (i >= -0x80)
10755 return 2;
10756 else if (i >= -0x8000)
10757 return 3;
10758 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10760 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
10762 s = size_of_int_loc_descriptor (-i) + 1;
10763 if (s < 5)
10764 return s;
10766 return 5;
10768 else
10770 unsigned long r = 1 + size_of_sleb128 (i);
10771 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
10773 s = size_of_int_loc_descriptor (-i) + 1;
10774 if (s < r)
10775 return s;
10777 return r;
10782 /* Return loc description representing "address" of integer value.
10783 This can appear only as toplevel expression. */
10785 static dw_loc_descr_ref
10786 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
10788 int litsize;
10789 dw_loc_descr_ref loc_result = NULL;
10791 if (!(dwarf_version >= 4 || !dwarf_strict))
10792 return NULL;
10794 litsize = size_of_int_loc_descriptor (i);
10795 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
10796 is more compact. For DW_OP_stack_value we need:
10797 litsize + 1 (DW_OP_stack_value)
10798 and for DW_OP_implicit_value:
10799 1 (DW_OP_implicit_value) + 1 (length) + size. */
10800 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
10802 loc_result = int_loc_descriptor (i);
10803 add_loc_descr (&loc_result,
10804 new_loc_descr (DW_OP_stack_value, 0, 0));
10805 return loc_result;
10808 loc_result = new_loc_descr (DW_OP_implicit_value,
10809 size, 0);
10810 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
10811 loc_result->dw_loc_oprnd2.v.val_int = i;
10812 return loc_result;
10815 /* Return a location descriptor that designates a base+offset location. */
10817 static dw_loc_descr_ref
10818 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
10819 enum var_init_status initialized)
10821 unsigned int regno;
10822 dw_loc_descr_ref result;
10823 dw_fde_ref fde = cfun->fde;
10825 /* We only use "frame base" when we're sure we're talking about the
10826 post-prologue local stack frame. We do this by *not* running
10827 register elimination until this point, and recognizing the special
10828 argument pointer and soft frame pointer rtx's. */
10829 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
10831 rtx elim = (ira_use_lra_p
10832 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
10833 : eliminate_regs (reg, VOIDmode, NULL_RTX));
10835 if (elim != reg)
10837 if (GET_CODE (elim) == PLUS)
10839 offset += INTVAL (XEXP (elim, 1));
10840 elim = XEXP (elim, 0);
10842 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
10843 && (elim == hard_frame_pointer_rtx
10844 || elim == stack_pointer_rtx))
10845 || elim == (frame_pointer_needed
10846 ? hard_frame_pointer_rtx
10847 : stack_pointer_rtx));
10849 /* If drap register is used to align stack, use frame
10850 pointer + offset to access stack variables. If stack
10851 is aligned without drap, use stack pointer + offset to
10852 access stack variables. */
10853 if (crtl->stack_realign_tried
10854 && reg == frame_pointer_rtx)
10856 int base_reg
10857 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
10858 ? HARD_FRAME_POINTER_REGNUM
10859 : REGNO (elim));
10860 return new_reg_loc_descr (base_reg, offset);
10863 gcc_assert (frame_pointer_fb_offset_valid);
10864 offset += frame_pointer_fb_offset;
10865 return new_loc_descr (DW_OP_fbreg, offset, 0);
10869 regno = DWARF_FRAME_REGNUM (REGNO (reg));
10871 if (!optimize && fde
10872 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
10874 /* Use cfa+offset to represent the location of arguments passed
10875 on the stack when drap is used to align stack.
10876 Only do this when not optimizing, for optimized code var-tracking
10877 is supposed to track where the arguments live and the register
10878 used as vdrap or drap in some spot might be used for something
10879 else in other part of the routine. */
10880 return new_loc_descr (DW_OP_fbreg, offset, 0);
10883 if (regno <= 31)
10884 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
10885 offset, 0);
10886 else
10887 result = new_loc_descr (DW_OP_bregx, regno, offset);
10889 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10890 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10892 return result;
10895 /* Return true if this RTL expression describes a base+offset calculation. */
10897 static inline int
10898 is_based_loc (const_rtx rtl)
10900 return (GET_CODE (rtl) == PLUS
10901 && ((REG_P (XEXP (rtl, 0))
10902 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
10903 && CONST_INT_P (XEXP (rtl, 1)))));
10906 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
10907 failed. */
10909 static dw_loc_descr_ref
10910 tls_mem_loc_descriptor (rtx mem)
10912 tree base;
10913 dw_loc_descr_ref loc_result;
10915 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
10916 return NULL;
10918 base = get_base_address (MEM_EXPR (mem));
10919 if (base == NULL
10920 || TREE_CODE (base) != VAR_DECL
10921 || !DECL_THREAD_LOCAL_P (base))
10922 return NULL;
10924 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
10925 if (loc_result == NULL)
10926 return NULL;
10928 if (MEM_OFFSET (mem))
10929 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
10931 return loc_result;
10934 /* Output debug info about reason why we failed to expand expression as dwarf
10935 expression. */
10937 static void
10938 expansion_failed (tree expr, rtx rtl, char const *reason)
10940 if (dump_file && (dump_flags & TDF_DETAILS))
10942 fprintf (dump_file, "Failed to expand as dwarf: ");
10943 if (expr)
10944 print_generic_expr (dump_file, expr, dump_flags);
10945 if (rtl)
10947 fprintf (dump_file, "\n");
10948 print_rtl (dump_file, rtl);
10950 fprintf (dump_file, "\nReason: %s\n", reason);
10954 /* Helper function for const_ok_for_output, called either directly
10955 or via for_each_rtx. */
10957 static int
10958 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
10960 rtx rtl = *rtlp;
10962 if (GET_CODE (rtl) == UNSPEC)
10964 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
10965 we can't express it in the debug info. */
10966 #ifdef ENABLE_CHECKING
10967 /* Don't complain about TLS UNSPECs, those are just too hard to
10968 delegitimize. Note this could be a non-decl SYMBOL_REF such as
10969 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
10970 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
10971 if (XVECLEN (rtl, 0) == 0
10972 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
10973 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
10974 inform (current_function_decl
10975 ? DECL_SOURCE_LOCATION (current_function_decl)
10976 : UNKNOWN_LOCATION,
10977 #if NUM_UNSPEC_VALUES > 0
10978 "non-delegitimized UNSPEC %s (%d) found in variable location",
10979 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
10980 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
10981 XINT (rtl, 1));
10982 #else
10983 "non-delegitimized UNSPEC %d found in variable location",
10984 XINT (rtl, 1));
10985 #endif
10986 #endif
10987 expansion_failed (NULL_TREE, rtl,
10988 "UNSPEC hasn't been delegitimized.\n");
10989 return 1;
10992 if (targetm.const_not_ok_for_debug_p (rtl))
10994 expansion_failed (NULL_TREE, rtl,
10995 "Expression rejected for debug by the backend.\n");
10996 return 1;
10999 if (GET_CODE (rtl) != SYMBOL_REF)
11000 return 0;
11002 if (CONSTANT_POOL_ADDRESS_P (rtl))
11004 bool marked;
11005 get_pool_constant_mark (rtl, &marked);
11006 /* If all references to this pool constant were optimized away,
11007 it was not output and thus we can't represent it. */
11008 if (!marked)
11010 expansion_failed (NULL_TREE, rtl,
11011 "Constant was removed from constant pool.\n");
11012 return 1;
11016 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11017 return 1;
11019 /* Avoid references to external symbols in debug info, on several targets
11020 the linker might even refuse to link when linking a shared library,
11021 and in many other cases the relocations for .debug_info/.debug_loc are
11022 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
11023 to be defined within the same shared library or executable are fine. */
11024 if (SYMBOL_REF_EXTERNAL_P (rtl))
11026 tree decl = SYMBOL_REF_DECL (rtl);
11028 if (decl == NULL || !targetm.binds_local_p (decl))
11030 expansion_failed (NULL_TREE, rtl,
11031 "Symbol not defined in current TU.\n");
11032 return 1;
11036 return 0;
11039 /* Return true if constant RTL can be emitted in DW_OP_addr or
11040 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
11041 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
11043 static bool
11044 const_ok_for_output (rtx rtl)
11046 if (GET_CODE (rtl) == SYMBOL_REF)
11047 return const_ok_for_output_1 (&rtl, NULL) == 0;
11049 if (GET_CODE (rtl) == CONST)
11050 return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
11052 return true;
11055 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11056 if possible, NULL otherwise. */
11058 static dw_die_ref
11059 base_type_for_mode (enum machine_mode mode, bool unsignedp)
11061 dw_die_ref type_die;
11062 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11064 if (type == NULL)
11065 return NULL;
11066 switch (TREE_CODE (type))
11068 case INTEGER_TYPE:
11069 case REAL_TYPE:
11070 break;
11071 default:
11072 return NULL;
11074 type_die = lookup_type_die (type);
11075 if (!type_die)
11076 type_die = modified_type_die (type, false, false, comp_unit_die ());
11077 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11078 return NULL;
11079 return type_die;
11082 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11083 type matching MODE, or, if MODE is narrower than or as wide as
11084 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
11085 possible. */
11087 static dw_loc_descr_ref
11088 convert_descriptor_to_mode (enum machine_mode mode, dw_loc_descr_ref op)
11090 enum machine_mode outer_mode = mode;
11091 dw_die_ref type_die;
11092 dw_loc_descr_ref cvt;
11094 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11096 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11097 return op;
11099 type_die = base_type_for_mode (outer_mode, 1);
11100 if (type_die == NULL)
11101 return NULL;
11102 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11103 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11104 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11105 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11106 add_loc_descr (&op, cvt);
11107 return op;
11110 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
11112 static dw_loc_descr_ref
11113 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11114 dw_loc_descr_ref op1)
11116 dw_loc_descr_ref ret = op0;
11117 add_loc_descr (&ret, op1);
11118 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11119 if (STORE_FLAG_VALUE != 1)
11121 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11122 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11124 return ret;
11127 /* Return location descriptor for signed comparison OP RTL. */
11129 static dw_loc_descr_ref
11130 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11131 enum machine_mode mem_mode)
11133 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11134 dw_loc_descr_ref op0, op1;
11135 int shift;
11137 if (op_mode == VOIDmode)
11138 op_mode = GET_MODE (XEXP (rtl, 1));
11139 if (op_mode == VOIDmode)
11140 return NULL;
11142 if (dwarf_strict
11143 && (GET_MODE_CLASS (op_mode) != MODE_INT
11144 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11145 return NULL;
11147 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11148 VAR_INIT_STATUS_INITIALIZED);
11149 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11150 VAR_INIT_STATUS_INITIALIZED);
11152 if (op0 == NULL || op1 == NULL)
11153 return NULL;
11155 if (GET_MODE_CLASS (op_mode) != MODE_INT
11156 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11157 return compare_loc_descriptor (op, op0, op1);
11159 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11161 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11162 dw_loc_descr_ref cvt;
11164 if (type_die == NULL)
11165 return NULL;
11166 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11167 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11168 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11169 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11170 add_loc_descr (&op0, cvt);
11171 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11172 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11173 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11174 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11175 add_loc_descr (&op1, cvt);
11176 return compare_loc_descriptor (op, op0, op1);
11179 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11180 /* For eq/ne, if the operands are known to be zero-extended,
11181 there is no need to do the fancy shifting up. */
11182 if (op == DW_OP_eq || op == DW_OP_ne)
11184 dw_loc_descr_ref last0, last1;
11185 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11187 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11189 /* deref_size zero extends, and for constants we can check
11190 whether they are zero extended or not. */
11191 if (((last0->dw_loc_opc == DW_OP_deref_size
11192 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11193 || (CONST_INT_P (XEXP (rtl, 0))
11194 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11195 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11196 && ((last1->dw_loc_opc == DW_OP_deref_size
11197 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11198 || (CONST_INT_P (XEXP (rtl, 1))
11199 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11200 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11201 return compare_loc_descriptor (op, op0, op1);
11203 /* EQ/NE comparison against constant in narrower type than
11204 DWARF2_ADDR_SIZE can be performed either as
11205 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11206 DW_OP_{eq,ne}
11208 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11209 DW_OP_{eq,ne}. Pick whatever is shorter. */
11210 if (CONST_INT_P (XEXP (rtl, 1))
11211 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11212 && (size_of_int_loc_descriptor (shift) + 1
11213 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11214 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11215 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11216 & GET_MODE_MASK (op_mode))))
11218 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
11219 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11220 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11221 & GET_MODE_MASK (op_mode));
11222 return compare_loc_descriptor (op, op0, op1);
11225 add_loc_descr (&op0, int_loc_descriptor (shift));
11226 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11227 if (CONST_INT_P (XEXP (rtl, 1)))
11228 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11229 else
11231 add_loc_descr (&op1, int_loc_descriptor (shift));
11232 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11234 return compare_loc_descriptor (op, op0, op1);
11237 /* Return location descriptor for unsigned comparison OP RTL. */
11239 static dw_loc_descr_ref
11240 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11241 enum machine_mode mem_mode)
11243 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11244 dw_loc_descr_ref op0, op1;
11246 if (op_mode == VOIDmode)
11247 op_mode = GET_MODE (XEXP (rtl, 1));
11248 if (op_mode == VOIDmode)
11249 return NULL;
11250 if (GET_MODE_CLASS (op_mode) != MODE_INT)
11251 return NULL;
11253 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11254 return NULL;
11256 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11257 VAR_INIT_STATUS_INITIALIZED);
11258 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11259 VAR_INIT_STATUS_INITIALIZED);
11261 if (op0 == NULL || op1 == NULL)
11262 return NULL;
11264 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
11266 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
11267 dw_loc_descr_ref last0, last1;
11268 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11270 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11272 if (CONST_INT_P (XEXP (rtl, 0)))
11273 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
11274 /* deref_size zero extends, so no need to mask it again. */
11275 else if (last0->dw_loc_opc != DW_OP_deref_size
11276 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11278 add_loc_descr (&op0, int_loc_descriptor (mask));
11279 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11281 if (CONST_INT_P (XEXP (rtl, 1)))
11282 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11283 /* deref_size zero extends, so no need to mask it again. */
11284 else if (last1->dw_loc_opc != DW_OP_deref_size
11285 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11287 add_loc_descr (&op1, int_loc_descriptor (mask));
11288 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11291 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11293 HOST_WIDE_INT bias = 1;
11294 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11295 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11296 if (CONST_INT_P (XEXP (rtl, 1)))
11297 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
11298 + INTVAL (XEXP (rtl, 1)));
11299 else
11300 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
11301 bias, 0));
11303 return compare_loc_descriptor (op, op0, op1);
11306 /* Return location descriptor for {U,S}{MIN,MAX}. */
11308 static dw_loc_descr_ref
11309 minmax_loc_descriptor (rtx rtl, enum machine_mode mode,
11310 enum machine_mode mem_mode)
11312 enum dwarf_location_atom op;
11313 dw_loc_descr_ref op0, op1, ret;
11314 dw_loc_descr_ref bra_node, drop_node;
11316 if (dwarf_strict
11317 && (GET_MODE_CLASS (mode) != MODE_INT
11318 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
11319 return NULL;
11321 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11322 VAR_INIT_STATUS_INITIALIZED);
11323 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11324 VAR_INIT_STATUS_INITIALIZED);
11326 if (op0 == NULL || op1 == NULL)
11327 return NULL;
11329 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
11330 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
11331 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
11332 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
11334 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11336 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11337 add_loc_descr (&op0, int_loc_descriptor (mask));
11338 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11339 add_loc_descr (&op1, int_loc_descriptor (mask));
11340 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11342 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11344 HOST_WIDE_INT bias = 1;
11345 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11346 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11347 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11350 else if (GET_MODE_CLASS (mode) == MODE_INT
11351 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11353 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
11354 add_loc_descr (&op0, int_loc_descriptor (shift));
11355 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11356 add_loc_descr (&op1, int_loc_descriptor (shift));
11357 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11359 else if (GET_MODE_CLASS (mode) == MODE_INT
11360 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11362 dw_die_ref type_die = base_type_for_mode (mode, 0);
11363 dw_loc_descr_ref cvt;
11364 if (type_die == NULL)
11365 return NULL;
11366 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11367 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11368 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11369 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11370 add_loc_descr (&op0, cvt);
11371 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11372 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11373 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11374 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11375 add_loc_descr (&op1, cvt);
11378 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
11379 op = DW_OP_lt;
11380 else
11381 op = DW_OP_gt;
11382 ret = op0;
11383 add_loc_descr (&ret, op1);
11384 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11385 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11386 add_loc_descr (&ret, bra_node);
11387 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11388 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11389 add_loc_descr (&ret, drop_node);
11390 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11391 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11392 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
11393 && GET_MODE_CLASS (mode) == MODE_INT
11394 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11395 ret = convert_descriptor_to_mode (mode, ret);
11396 return ret;
11399 /* Helper function for mem_loc_descriptor. Perform OP binary op,
11400 but after converting arguments to type_die, afterwards
11401 convert back to unsigned. */
11403 static dw_loc_descr_ref
11404 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
11405 enum machine_mode mode, enum machine_mode mem_mode)
11407 dw_loc_descr_ref cvt, op0, op1;
11409 if (type_die == NULL)
11410 return NULL;
11411 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11412 VAR_INIT_STATUS_INITIALIZED);
11413 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11414 VAR_INIT_STATUS_INITIALIZED);
11415 if (op0 == NULL || op1 == NULL)
11416 return NULL;
11417 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11418 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11419 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11420 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11421 add_loc_descr (&op0, cvt);
11422 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11423 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11424 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11425 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11426 add_loc_descr (&op1, cvt);
11427 add_loc_descr (&op0, op1);
11428 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
11429 return convert_descriptor_to_mode (mode, op0);
11432 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
11433 const0 is DW_OP_lit0 or corresponding typed constant,
11434 const1 is DW_OP_lit1 or corresponding typed constant
11435 and constMSB is constant with just the MSB bit set
11436 for the mode):
11437 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11438 L1: const0 DW_OP_swap
11439 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
11440 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11441 L3: DW_OP_drop
11442 L4: DW_OP_nop
11444 CTZ is similar:
11445 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11446 L1: const0 DW_OP_swap
11447 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11448 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11449 L3: DW_OP_drop
11450 L4: DW_OP_nop
11452 FFS is similar:
11453 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
11454 L1: const1 DW_OP_swap
11455 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11456 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11457 L3: DW_OP_drop
11458 L4: DW_OP_nop */
11460 static dw_loc_descr_ref
11461 clz_loc_descriptor (rtx rtl, enum machine_mode mode,
11462 enum machine_mode mem_mode)
11464 dw_loc_descr_ref op0, ret, tmp;
11465 HOST_WIDE_INT valv;
11466 dw_loc_descr_ref l1jump, l1label;
11467 dw_loc_descr_ref l2jump, l2label;
11468 dw_loc_descr_ref l3jump, l3label;
11469 dw_loc_descr_ref l4jump, l4label;
11470 rtx msb;
11472 if (GET_MODE_CLASS (mode) != MODE_INT
11473 || GET_MODE (XEXP (rtl, 0)) != mode
11474 || (GET_CODE (rtl) == CLZ
11475 && GET_MODE_BITSIZE (mode) > HOST_BITS_PER_DOUBLE_INT))
11476 return NULL;
11478 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11479 VAR_INIT_STATUS_INITIALIZED);
11480 if (op0 == NULL)
11481 return NULL;
11482 ret = op0;
11483 if (GET_CODE (rtl) == CLZ)
11485 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11486 valv = GET_MODE_BITSIZE (mode);
11488 else if (GET_CODE (rtl) == FFS)
11489 valv = 0;
11490 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11491 valv = GET_MODE_BITSIZE (mode);
11492 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11493 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
11494 add_loc_descr (&ret, l1jump);
11495 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11496 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
11497 VAR_INIT_STATUS_INITIALIZED);
11498 if (tmp == NULL)
11499 return NULL;
11500 add_loc_descr (&ret, tmp);
11501 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
11502 add_loc_descr (&ret, l4jump);
11503 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
11504 ? const1_rtx : const0_rtx,
11505 mode, mem_mode,
11506 VAR_INIT_STATUS_INITIALIZED);
11507 if (l1label == NULL)
11508 return NULL;
11509 add_loc_descr (&ret, l1label);
11510 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11511 l2label = new_loc_descr (DW_OP_dup, 0, 0);
11512 add_loc_descr (&ret, l2label);
11513 if (GET_CODE (rtl) != CLZ)
11514 msb = const1_rtx;
11515 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11516 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
11517 << (GET_MODE_BITSIZE (mode) - 1));
11518 else
11519 msb = immed_double_const (0, (unsigned HOST_WIDE_INT) 1
11520 << (GET_MODE_BITSIZE (mode)
11521 - HOST_BITS_PER_WIDE_INT - 1), mode);
11522 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
11523 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11524 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
11525 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
11526 else
11527 tmp = mem_loc_descriptor (msb, mode, mem_mode,
11528 VAR_INIT_STATUS_INITIALIZED);
11529 if (tmp == NULL)
11530 return NULL;
11531 add_loc_descr (&ret, tmp);
11532 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11533 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
11534 add_loc_descr (&ret, l3jump);
11535 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11536 VAR_INIT_STATUS_INITIALIZED);
11537 if (tmp == NULL)
11538 return NULL;
11539 add_loc_descr (&ret, tmp);
11540 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
11541 ? DW_OP_shl : DW_OP_shr, 0, 0));
11542 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11543 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
11544 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11545 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
11546 add_loc_descr (&ret, l2jump);
11547 l3label = new_loc_descr (DW_OP_drop, 0, 0);
11548 add_loc_descr (&ret, l3label);
11549 l4label = new_loc_descr (DW_OP_nop, 0, 0);
11550 add_loc_descr (&ret, l4label);
11551 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11552 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11553 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11554 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11555 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11556 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
11557 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11558 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
11559 return ret;
11562 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
11563 const1 is DW_OP_lit1 or corresponding typed constant):
11564 const0 DW_OP_swap
11565 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11566 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11567 L2: DW_OP_drop
11569 PARITY is similar:
11570 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11571 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11572 L2: DW_OP_drop */
11574 static dw_loc_descr_ref
11575 popcount_loc_descriptor (rtx rtl, enum machine_mode mode,
11576 enum machine_mode mem_mode)
11578 dw_loc_descr_ref op0, ret, tmp;
11579 dw_loc_descr_ref l1jump, l1label;
11580 dw_loc_descr_ref l2jump, l2label;
11582 if (GET_MODE_CLASS (mode) != MODE_INT
11583 || GET_MODE (XEXP (rtl, 0)) != mode)
11584 return NULL;
11586 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11587 VAR_INIT_STATUS_INITIALIZED);
11588 if (op0 == NULL)
11589 return NULL;
11590 ret = op0;
11591 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11592 VAR_INIT_STATUS_INITIALIZED);
11593 if (tmp == NULL)
11594 return NULL;
11595 add_loc_descr (&ret, tmp);
11596 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11597 l1label = new_loc_descr (DW_OP_dup, 0, 0);
11598 add_loc_descr (&ret, l1label);
11599 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11600 add_loc_descr (&ret, l2jump);
11601 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11602 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11603 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11604 VAR_INIT_STATUS_INITIALIZED);
11605 if (tmp == NULL)
11606 return NULL;
11607 add_loc_descr (&ret, tmp);
11608 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11609 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
11610 ? DW_OP_plus : DW_OP_xor, 0, 0));
11611 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11612 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11613 VAR_INIT_STATUS_INITIALIZED);
11614 add_loc_descr (&ret, tmp);
11615 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11616 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11617 add_loc_descr (&ret, l1jump);
11618 l2label = new_loc_descr (DW_OP_drop, 0, 0);
11619 add_loc_descr (&ret, l2label);
11620 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11621 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11622 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11623 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11624 return ret;
11627 /* BSWAP (constS is initial shift count, either 56 or 24):
11628 constS const0
11629 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
11630 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
11631 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
11632 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
11633 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
11635 static dw_loc_descr_ref
11636 bswap_loc_descriptor (rtx rtl, enum machine_mode mode,
11637 enum machine_mode mem_mode)
11639 dw_loc_descr_ref op0, ret, tmp;
11640 dw_loc_descr_ref l1jump, l1label;
11641 dw_loc_descr_ref l2jump, l2label;
11643 if (GET_MODE_CLASS (mode) != MODE_INT
11644 || BITS_PER_UNIT != 8
11645 || (GET_MODE_BITSIZE (mode) != 32
11646 && GET_MODE_BITSIZE (mode) != 64))
11647 return NULL;
11649 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11650 VAR_INIT_STATUS_INITIALIZED);
11651 if (op0 == NULL)
11652 return NULL;
11654 ret = op0;
11655 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11656 mode, mem_mode,
11657 VAR_INIT_STATUS_INITIALIZED);
11658 if (tmp == NULL)
11659 return NULL;
11660 add_loc_descr (&ret, tmp);
11661 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11662 VAR_INIT_STATUS_INITIALIZED);
11663 if (tmp == NULL)
11664 return NULL;
11665 add_loc_descr (&ret, tmp);
11666 l1label = new_loc_descr (DW_OP_pick, 2, 0);
11667 add_loc_descr (&ret, l1label);
11668 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11669 mode, mem_mode,
11670 VAR_INIT_STATUS_INITIALIZED);
11671 add_loc_descr (&ret, tmp);
11672 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
11673 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11674 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11675 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
11676 VAR_INIT_STATUS_INITIALIZED);
11677 if (tmp == NULL)
11678 return NULL;
11679 add_loc_descr (&ret, tmp);
11680 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11681 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
11682 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11683 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11684 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11685 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11686 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11687 VAR_INIT_STATUS_INITIALIZED);
11688 add_loc_descr (&ret, tmp);
11689 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
11690 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11691 add_loc_descr (&ret, l2jump);
11692 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
11693 VAR_INIT_STATUS_INITIALIZED);
11694 add_loc_descr (&ret, tmp);
11695 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11696 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11697 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11698 add_loc_descr (&ret, l1jump);
11699 l2label = new_loc_descr (DW_OP_drop, 0, 0);
11700 add_loc_descr (&ret, l2label);
11701 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11702 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11703 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11704 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11705 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11706 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11707 return ret;
11710 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
11711 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11712 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
11713 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
11715 ROTATERT is similar:
11716 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
11717 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11718 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
11720 static dw_loc_descr_ref
11721 rotate_loc_descriptor (rtx rtl, enum machine_mode mode,
11722 enum machine_mode mem_mode)
11724 rtx rtlop1 = XEXP (rtl, 1);
11725 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
11726 int i;
11728 if (GET_MODE_CLASS (mode) != MODE_INT)
11729 return NULL;
11731 if (GET_MODE (rtlop1) != VOIDmode
11732 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
11733 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
11734 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11735 VAR_INIT_STATUS_INITIALIZED);
11736 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
11737 VAR_INIT_STATUS_INITIALIZED);
11738 if (op0 == NULL || op1 == NULL)
11739 return NULL;
11740 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11741 for (i = 0; i < 2; i++)
11743 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
11744 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
11745 mode, mem_mode,
11746 VAR_INIT_STATUS_INITIALIZED);
11747 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
11748 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11749 ? DW_OP_const4u
11750 : HOST_BITS_PER_WIDE_INT == 64
11751 ? DW_OP_const8u : DW_OP_constu,
11752 GET_MODE_MASK (mode), 0);
11753 else
11754 mask[i] = NULL;
11755 if (mask[i] == NULL)
11756 return NULL;
11757 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
11759 ret = op0;
11760 add_loc_descr (&ret, op1);
11761 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11762 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11763 if (GET_CODE (rtl) == ROTATERT)
11765 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11766 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11767 GET_MODE_BITSIZE (mode), 0));
11769 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11770 if (mask[0] != NULL)
11771 add_loc_descr (&ret, mask[0]);
11772 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11773 if (mask[1] != NULL)
11775 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11776 add_loc_descr (&ret, mask[1]);
11777 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11779 if (GET_CODE (rtl) == ROTATE)
11781 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11782 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11783 GET_MODE_BITSIZE (mode), 0));
11785 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11786 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11787 return ret;
11790 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
11791 for DEBUG_PARAMETER_REF RTL. */
11793 static dw_loc_descr_ref
11794 parameter_ref_descriptor (rtx rtl)
11796 dw_loc_descr_ref ret;
11797 dw_die_ref ref;
11799 if (dwarf_strict)
11800 return NULL;
11801 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
11802 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
11803 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
11804 if (ref)
11806 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11807 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
11808 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
11810 else
11812 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
11813 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
11815 return ret;
11818 /* The following routine converts the RTL for a variable or parameter
11819 (resident in memory) into an equivalent Dwarf representation of a
11820 mechanism for getting the address of that same variable onto the top of a
11821 hypothetical "address evaluation" stack.
11823 When creating memory location descriptors, we are effectively transforming
11824 the RTL for a memory-resident object into its Dwarf postfix expression
11825 equivalent. This routine recursively descends an RTL tree, turning
11826 it into Dwarf postfix code as it goes.
11828 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
11830 MEM_MODE is the mode of the memory reference, needed to handle some
11831 autoincrement addressing modes.
11833 Return 0 if we can't represent the location. */
11835 dw_loc_descr_ref
11836 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
11837 enum machine_mode mem_mode,
11838 enum var_init_status initialized)
11840 dw_loc_descr_ref mem_loc_result = NULL;
11841 enum dwarf_location_atom op;
11842 dw_loc_descr_ref op0, op1;
11843 rtx inner = NULL_RTX;
11845 if (mode == VOIDmode)
11846 mode = GET_MODE (rtl);
11848 /* Note that for a dynamically sized array, the location we will generate a
11849 description of here will be the lowest numbered location which is
11850 actually within the array. That's *not* necessarily the same as the
11851 zeroth element of the array. */
11853 rtl = targetm.delegitimize_address (rtl);
11855 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
11856 return NULL;
11858 switch (GET_CODE (rtl))
11860 case POST_INC:
11861 case POST_DEC:
11862 case POST_MODIFY:
11863 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
11865 case SUBREG:
11866 /* The case of a subreg may arise when we have a local (register)
11867 variable or a formal (register) parameter which doesn't quite fill
11868 up an entire register. For now, just assume that it is
11869 legitimate to make the Dwarf info refer to the whole register which
11870 contains the given subreg. */
11871 if (!subreg_lowpart_p (rtl))
11872 break;
11873 inner = SUBREG_REG (rtl);
11874 case TRUNCATE:
11875 if (inner == NULL_RTX)
11876 inner = XEXP (rtl, 0);
11877 if (GET_MODE_CLASS (mode) == MODE_INT
11878 && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
11879 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11880 #ifdef POINTERS_EXTEND_UNSIGNED
11881 || (mode == Pmode && mem_mode != VOIDmode)
11882 #endif
11884 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
11886 mem_loc_result = mem_loc_descriptor (inner,
11887 GET_MODE (inner),
11888 mem_mode, initialized);
11889 break;
11891 if (dwarf_strict)
11892 break;
11893 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
11894 break;
11895 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
11896 && (GET_MODE_CLASS (mode) != MODE_INT
11897 || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
11898 break;
11899 else
11901 dw_die_ref type_die;
11902 dw_loc_descr_ref cvt;
11904 mem_loc_result = mem_loc_descriptor (inner,
11905 GET_MODE (inner),
11906 mem_mode, initialized);
11907 if (mem_loc_result == NULL)
11908 break;
11909 type_die = base_type_for_mode (mode,
11910 GET_MODE_CLASS (mode) == MODE_INT);
11911 if (type_die == NULL)
11913 mem_loc_result = NULL;
11914 break;
11916 if (GET_MODE_SIZE (mode)
11917 != GET_MODE_SIZE (GET_MODE (inner)))
11918 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11919 else
11920 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
11921 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11922 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11923 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11924 add_loc_descr (&mem_loc_result, cvt);
11926 break;
11928 case REG:
11929 if (GET_MODE_CLASS (mode) != MODE_INT
11930 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
11931 && rtl != arg_pointer_rtx
11932 && rtl != frame_pointer_rtx
11933 #ifdef POINTERS_EXTEND_UNSIGNED
11934 && (mode != Pmode || mem_mode == VOIDmode)
11935 #endif
11938 dw_die_ref type_die;
11939 unsigned int dbx_regnum;
11941 if (dwarf_strict)
11942 break;
11943 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
11944 break;
11945 type_die = base_type_for_mode (mode,
11946 GET_MODE_CLASS (mode) == MODE_INT);
11947 if (type_die == NULL)
11948 break;
11950 dbx_regnum = dbx_reg_number (rtl);
11951 if (dbx_regnum == IGNORED_DWARF_REGNUM)
11952 break;
11953 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
11954 dbx_regnum, 0);
11955 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
11956 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
11957 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
11958 break;
11960 /* Whenever a register number forms a part of the description of the
11961 method for calculating the (dynamic) address of a memory resident
11962 object, DWARF rules require the register number be referred to as
11963 a "base register". This distinction is not based in any way upon
11964 what category of register the hardware believes the given register
11965 belongs to. This is strictly DWARF terminology we're dealing with
11966 here. Note that in cases where the location of a memory-resident
11967 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
11968 OP_CONST (0)) the actual DWARF location descriptor that we generate
11969 may just be OP_BASEREG (basereg). This may look deceptively like
11970 the object in question was allocated to a register (rather than in
11971 memory) so DWARF consumers need to be aware of the subtle
11972 distinction between OP_REG and OP_BASEREG. */
11973 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
11974 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
11975 else if (stack_realign_drap
11976 && crtl->drap_reg
11977 && crtl->args.internal_arg_pointer == rtl
11978 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
11980 /* If RTL is internal_arg_pointer, which has been optimized
11981 out, use DRAP instead. */
11982 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
11983 VAR_INIT_STATUS_INITIALIZED);
11985 break;
11987 case SIGN_EXTEND:
11988 case ZERO_EXTEND:
11989 if (GET_MODE_CLASS (mode) != MODE_INT)
11990 break;
11991 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
11992 mem_mode, VAR_INIT_STATUS_INITIALIZED);
11993 if (op0 == 0)
11994 break;
11995 else if (GET_CODE (rtl) == ZERO_EXTEND
11996 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11997 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
11998 < HOST_BITS_PER_WIDE_INT
11999 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12000 to expand zero extend as two shifts instead of
12001 masking. */
12002 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12004 enum machine_mode imode = GET_MODE (XEXP (rtl, 0));
12005 mem_loc_result = op0;
12006 add_loc_descr (&mem_loc_result,
12007 int_loc_descriptor (GET_MODE_MASK (imode)));
12008 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12010 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12012 int shift = DWARF2_ADDR_SIZE
12013 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12014 shift *= BITS_PER_UNIT;
12015 if (GET_CODE (rtl) == SIGN_EXTEND)
12016 op = DW_OP_shra;
12017 else
12018 op = DW_OP_shr;
12019 mem_loc_result = op0;
12020 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12021 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12022 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12023 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12025 else if (!dwarf_strict)
12027 dw_die_ref type_die1, type_die2;
12028 dw_loc_descr_ref cvt;
12030 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12031 GET_CODE (rtl) == ZERO_EXTEND);
12032 if (type_die1 == NULL)
12033 break;
12034 type_die2 = base_type_for_mode (mode, 1);
12035 if (type_die2 == NULL)
12036 break;
12037 mem_loc_result = op0;
12038 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12039 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12040 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12041 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12042 add_loc_descr (&mem_loc_result, cvt);
12043 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12044 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12045 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12046 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12047 add_loc_descr (&mem_loc_result, cvt);
12049 break;
12051 case MEM:
12053 rtx new_rtl = avoid_constant_pool_reference (rtl);
12054 if (new_rtl != rtl)
12056 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12057 initialized);
12058 if (mem_loc_result != NULL)
12059 return mem_loc_result;
12062 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12063 get_address_mode (rtl), mode,
12064 VAR_INIT_STATUS_INITIALIZED);
12065 if (mem_loc_result == NULL)
12066 mem_loc_result = tls_mem_loc_descriptor (rtl);
12067 if (mem_loc_result != NULL)
12069 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12070 || GET_MODE_CLASS (mode) != MODE_INT)
12072 dw_die_ref type_die;
12073 dw_loc_descr_ref deref;
12075 if (dwarf_strict)
12076 return NULL;
12077 type_die
12078 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12079 if (type_die == NULL)
12080 return NULL;
12081 deref = new_loc_descr (DW_OP_GNU_deref_type,
12082 GET_MODE_SIZE (mode), 0);
12083 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12084 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12085 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12086 add_loc_descr (&mem_loc_result, deref);
12088 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12089 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12090 else
12091 add_loc_descr (&mem_loc_result,
12092 new_loc_descr (DW_OP_deref_size,
12093 GET_MODE_SIZE (mode), 0));
12095 break;
12097 case LO_SUM:
12098 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12100 case LABEL_REF:
12101 /* Some ports can transform a symbol ref into a label ref, because
12102 the symbol ref is too far away and has to be dumped into a constant
12103 pool. */
12104 case CONST:
12105 case SYMBOL_REF:
12106 if (GET_MODE_CLASS (mode) != MODE_INT
12107 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12108 #ifdef POINTERS_EXTEND_UNSIGNED
12109 && (mode != Pmode || mem_mode == VOIDmode)
12110 #endif
12112 break;
12113 if (GET_CODE (rtl) == SYMBOL_REF
12114 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12116 dw_loc_descr_ref temp;
12118 /* If this is not defined, we have no way to emit the data. */
12119 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12120 break;
12122 temp = new_addr_loc_descr (rtl, dtprel_true);
12124 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12125 add_loc_descr (&mem_loc_result, temp);
12127 break;
12130 if (!const_ok_for_output (rtl))
12131 break;
12133 symref:
12134 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12135 vec_safe_push (used_rtx_array, rtl);
12136 break;
12138 case CONCAT:
12139 case CONCATN:
12140 case VAR_LOCATION:
12141 case DEBUG_IMPLICIT_PTR:
12142 expansion_failed (NULL_TREE, rtl,
12143 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12144 return 0;
12146 case ENTRY_VALUE:
12147 if (dwarf_strict)
12148 return NULL;
12149 if (REG_P (ENTRY_VALUE_EXP (rtl)))
12151 if (GET_MODE_CLASS (mode) != MODE_INT
12152 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12153 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12154 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12155 else
12157 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12158 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12159 return NULL;
12160 op0 = one_reg_loc_descriptor (dbx_regnum,
12161 VAR_INIT_STATUS_INITIALIZED);
12164 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12165 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12167 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12168 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12169 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12170 return NULL;
12172 else
12173 gcc_unreachable ();
12174 if (op0 == NULL)
12175 return NULL;
12176 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12177 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12178 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12179 break;
12181 case DEBUG_PARAMETER_REF:
12182 mem_loc_result = parameter_ref_descriptor (rtl);
12183 break;
12185 case PRE_MODIFY:
12186 /* Extract the PLUS expression nested inside and fall into
12187 PLUS code below. */
12188 rtl = XEXP (rtl, 1);
12189 goto plus;
12191 case PRE_INC:
12192 case PRE_DEC:
12193 /* Turn these into a PLUS expression and fall into the PLUS code
12194 below. */
12195 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12196 GEN_INT (GET_CODE (rtl) == PRE_INC
12197 ? GET_MODE_UNIT_SIZE (mem_mode)
12198 : -GET_MODE_UNIT_SIZE (mem_mode)));
12200 /* ... fall through ... */
12202 case PLUS:
12203 plus:
12204 if (is_based_loc (rtl)
12205 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12206 || XEXP (rtl, 0) == arg_pointer_rtx
12207 || XEXP (rtl, 0) == frame_pointer_rtx)
12208 && GET_MODE_CLASS (mode) == MODE_INT)
12209 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12210 INTVAL (XEXP (rtl, 1)),
12211 VAR_INIT_STATUS_INITIALIZED);
12212 else
12214 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12215 VAR_INIT_STATUS_INITIALIZED);
12216 if (mem_loc_result == 0)
12217 break;
12219 if (CONST_INT_P (XEXP (rtl, 1))
12220 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12221 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12222 else
12224 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12225 VAR_INIT_STATUS_INITIALIZED);
12226 if (op1 == 0)
12227 break;
12228 add_loc_descr (&mem_loc_result, op1);
12229 add_loc_descr (&mem_loc_result,
12230 new_loc_descr (DW_OP_plus, 0, 0));
12233 break;
12235 /* If a pseudo-reg is optimized away, it is possible for it to
12236 be replaced with a MEM containing a multiply or shift. */
12237 case MINUS:
12238 op = DW_OP_minus;
12239 goto do_binop;
12241 case MULT:
12242 op = DW_OP_mul;
12243 goto do_binop;
12245 case DIV:
12246 if (!dwarf_strict
12247 && GET_MODE_CLASS (mode) == MODE_INT
12248 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12250 mem_loc_result = typed_binop (DW_OP_div, rtl,
12251 base_type_for_mode (mode, 0),
12252 mode, mem_mode);
12253 break;
12255 op = DW_OP_div;
12256 goto do_binop;
12258 case UMOD:
12259 op = DW_OP_mod;
12260 goto do_binop;
12262 case ASHIFT:
12263 op = DW_OP_shl;
12264 goto do_shift;
12266 case ASHIFTRT:
12267 op = DW_OP_shra;
12268 goto do_shift;
12270 case LSHIFTRT:
12271 op = DW_OP_shr;
12272 goto do_shift;
12274 do_shift:
12275 if (GET_MODE_CLASS (mode) != MODE_INT)
12276 break;
12277 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12278 VAR_INIT_STATUS_INITIALIZED);
12280 rtx rtlop1 = XEXP (rtl, 1);
12281 if (GET_MODE (rtlop1) != VOIDmode
12282 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
12283 < GET_MODE_BITSIZE (mode))
12284 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12285 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12286 VAR_INIT_STATUS_INITIALIZED);
12289 if (op0 == 0 || op1 == 0)
12290 break;
12292 mem_loc_result = op0;
12293 add_loc_descr (&mem_loc_result, op1);
12294 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12295 break;
12297 case AND:
12298 op = DW_OP_and;
12299 goto do_binop;
12301 case IOR:
12302 op = DW_OP_or;
12303 goto do_binop;
12305 case XOR:
12306 op = DW_OP_xor;
12307 goto do_binop;
12309 do_binop:
12310 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12311 VAR_INIT_STATUS_INITIALIZED);
12312 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12313 VAR_INIT_STATUS_INITIALIZED);
12315 if (op0 == 0 || op1 == 0)
12316 break;
12318 mem_loc_result = op0;
12319 add_loc_descr (&mem_loc_result, op1);
12320 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12321 break;
12323 case MOD:
12324 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
12326 mem_loc_result = typed_binop (DW_OP_mod, rtl,
12327 base_type_for_mode (mode, 0),
12328 mode, mem_mode);
12329 break;
12332 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12333 VAR_INIT_STATUS_INITIALIZED);
12334 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12335 VAR_INIT_STATUS_INITIALIZED);
12337 if (op0 == 0 || op1 == 0)
12338 break;
12340 mem_loc_result = op0;
12341 add_loc_descr (&mem_loc_result, op1);
12342 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12343 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12344 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
12345 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
12346 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
12347 break;
12349 case UDIV:
12350 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
12352 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
12354 op = DW_OP_div;
12355 goto do_binop;
12357 mem_loc_result = typed_binop (DW_OP_div, rtl,
12358 base_type_for_mode (mode, 1),
12359 mode, mem_mode);
12361 break;
12363 case NOT:
12364 op = DW_OP_not;
12365 goto do_unop;
12367 case ABS:
12368 op = DW_OP_abs;
12369 goto do_unop;
12371 case NEG:
12372 op = DW_OP_neg;
12373 goto do_unop;
12375 do_unop:
12376 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12377 VAR_INIT_STATUS_INITIALIZED);
12379 if (op0 == 0)
12380 break;
12382 mem_loc_result = op0;
12383 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12384 break;
12386 case CONST_INT:
12387 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12388 #ifdef POINTERS_EXTEND_UNSIGNED
12389 || (mode == Pmode
12390 && mem_mode != VOIDmode
12391 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
12392 #endif
12395 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12396 break;
12398 if (!dwarf_strict
12399 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
12400 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
12402 dw_die_ref type_die = base_type_for_mode (mode, 1);
12403 enum machine_mode amode;
12404 if (type_die == NULL)
12405 return NULL;
12406 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
12407 MODE_INT, 0);
12408 if (INTVAL (rtl) >= 0
12409 && amode != BLKmode
12410 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
12411 /* const DW_OP_GNU_convert <XXX> vs.
12412 DW_OP_GNU_const_type <XXX, 1, const>. */
12413 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
12414 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
12416 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12417 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12418 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12419 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12420 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
12421 add_loc_descr (&mem_loc_result, op0);
12422 return mem_loc_result;
12424 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
12425 INTVAL (rtl));
12426 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12427 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12428 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12429 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12430 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12431 else
12433 mem_loc_result->dw_loc_oprnd2.val_class
12434 = dw_val_class_const_double;
12435 mem_loc_result->dw_loc_oprnd2.v.val_double
12436 = double_int::from_shwi (INTVAL (rtl));
12439 break;
12441 case CONST_DOUBLE:
12442 if (!dwarf_strict)
12444 dw_die_ref type_die;
12446 /* Note that a CONST_DOUBLE rtx could represent either an integer
12447 or a floating-point constant. A CONST_DOUBLE is used whenever
12448 the constant requires more than one word in order to be
12449 adequately represented. We output CONST_DOUBLEs as blocks. */
12450 if (mode == VOIDmode
12451 || (GET_MODE (rtl) == VOIDmode
12452 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
12453 break;
12454 type_die = base_type_for_mode (mode,
12455 GET_MODE_CLASS (mode) == MODE_INT);
12456 if (type_die == NULL)
12457 return NULL;
12458 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
12459 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12460 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12461 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12462 if (SCALAR_FLOAT_MODE_P (mode))
12464 unsigned int length = GET_MODE_SIZE (mode);
12465 unsigned char *array
12466 = (unsigned char*) ggc_alloc_atomic (length);
12468 insert_float (rtl, array);
12469 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12470 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12471 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12472 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12474 else
12476 mem_loc_result->dw_loc_oprnd2.val_class
12477 = dw_val_class_const_double;
12478 mem_loc_result->dw_loc_oprnd2.v.val_double
12479 = rtx_to_double_int (rtl);
12482 break;
12484 case EQ:
12485 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
12486 break;
12488 case GE:
12489 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12490 break;
12492 case GT:
12493 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12494 break;
12496 case LE:
12497 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12498 break;
12500 case LT:
12501 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12502 break;
12504 case NE:
12505 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
12506 break;
12508 case GEU:
12509 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12510 break;
12512 case GTU:
12513 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12514 break;
12516 case LEU:
12517 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12518 break;
12520 case LTU:
12521 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12522 break;
12524 case UMIN:
12525 case UMAX:
12526 if (GET_MODE_CLASS (mode) != MODE_INT)
12527 break;
12528 /* FALLTHRU */
12529 case SMIN:
12530 case SMAX:
12531 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
12532 break;
12534 case ZERO_EXTRACT:
12535 case SIGN_EXTRACT:
12536 if (CONST_INT_P (XEXP (rtl, 1))
12537 && CONST_INT_P (XEXP (rtl, 2))
12538 && ((unsigned) INTVAL (XEXP (rtl, 1))
12539 + (unsigned) INTVAL (XEXP (rtl, 2))
12540 <= GET_MODE_BITSIZE (mode))
12541 && GET_MODE_CLASS (mode) == MODE_INT
12542 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12543 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
12545 int shift, size;
12546 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12547 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12548 if (op0 == 0)
12549 break;
12550 if (GET_CODE (rtl) == SIGN_EXTRACT)
12551 op = DW_OP_shra;
12552 else
12553 op = DW_OP_shr;
12554 mem_loc_result = op0;
12555 size = INTVAL (XEXP (rtl, 1));
12556 shift = INTVAL (XEXP (rtl, 2));
12557 if (BITS_BIG_ENDIAN)
12558 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12559 - shift - size;
12560 if (shift + size != (int) DWARF2_ADDR_SIZE)
12562 add_loc_descr (&mem_loc_result,
12563 int_loc_descriptor (DWARF2_ADDR_SIZE
12564 - shift - size));
12565 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12567 if (size != (int) DWARF2_ADDR_SIZE)
12569 add_loc_descr (&mem_loc_result,
12570 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
12571 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12574 break;
12576 case IF_THEN_ELSE:
12578 dw_loc_descr_ref op2, bra_node, drop_node;
12579 op0 = mem_loc_descriptor (XEXP (rtl, 0),
12580 GET_MODE (XEXP (rtl, 0)) == VOIDmode
12581 ? word_mode : GET_MODE (XEXP (rtl, 0)),
12582 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12583 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12584 VAR_INIT_STATUS_INITIALIZED);
12585 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
12586 VAR_INIT_STATUS_INITIALIZED);
12587 if (op0 == NULL || op1 == NULL || op2 == NULL)
12588 break;
12590 mem_loc_result = op1;
12591 add_loc_descr (&mem_loc_result, op2);
12592 add_loc_descr (&mem_loc_result, op0);
12593 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12594 add_loc_descr (&mem_loc_result, bra_node);
12595 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
12596 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12597 add_loc_descr (&mem_loc_result, drop_node);
12598 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12599 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12601 break;
12603 case FLOAT_EXTEND:
12604 case FLOAT_TRUNCATE:
12605 case FLOAT:
12606 case UNSIGNED_FLOAT:
12607 case FIX:
12608 case UNSIGNED_FIX:
12609 if (!dwarf_strict)
12611 dw_die_ref type_die;
12612 dw_loc_descr_ref cvt;
12614 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12615 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12616 if (op0 == NULL)
12617 break;
12618 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
12619 && (GET_CODE (rtl) == FLOAT
12620 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
12621 <= DWARF2_ADDR_SIZE))
12623 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12624 GET_CODE (rtl) == UNSIGNED_FLOAT);
12625 if (type_die == NULL)
12626 break;
12627 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12628 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12629 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12630 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12631 add_loc_descr (&op0, cvt);
12633 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
12634 if (type_die == NULL)
12635 break;
12636 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12637 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12638 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12639 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12640 add_loc_descr (&op0, cvt);
12641 if (GET_MODE_CLASS (mode) == MODE_INT
12642 && (GET_CODE (rtl) == FIX
12643 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
12645 op0 = convert_descriptor_to_mode (mode, op0);
12646 if (op0 == NULL)
12647 break;
12649 mem_loc_result = op0;
12651 break;
12653 case CLZ:
12654 case CTZ:
12655 case FFS:
12656 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
12657 break;
12659 case POPCOUNT:
12660 case PARITY:
12661 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
12662 break;
12664 case BSWAP:
12665 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
12666 break;
12668 case ROTATE:
12669 case ROTATERT:
12670 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
12671 break;
12673 case COMPARE:
12674 /* In theory, we could implement the above. */
12675 /* DWARF cannot represent the unsigned compare operations
12676 natively. */
12677 case SS_MULT:
12678 case US_MULT:
12679 case SS_DIV:
12680 case US_DIV:
12681 case SS_PLUS:
12682 case US_PLUS:
12683 case SS_MINUS:
12684 case US_MINUS:
12685 case SS_NEG:
12686 case US_NEG:
12687 case SS_ABS:
12688 case SS_ASHIFT:
12689 case US_ASHIFT:
12690 case SS_TRUNCATE:
12691 case US_TRUNCATE:
12692 case UNORDERED:
12693 case ORDERED:
12694 case UNEQ:
12695 case UNGE:
12696 case UNGT:
12697 case UNLE:
12698 case UNLT:
12699 case LTGT:
12700 case FRACT_CONVERT:
12701 case UNSIGNED_FRACT_CONVERT:
12702 case SAT_FRACT:
12703 case UNSIGNED_SAT_FRACT:
12704 case SQRT:
12705 case ASM_OPERANDS:
12706 case VEC_MERGE:
12707 case VEC_SELECT:
12708 case VEC_CONCAT:
12709 case VEC_DUPLICATE:
12710 case UNSPEC:
12711 case HIGH:
12712 case FMA:
12713 case STRICT_LOW_PART:
12714 case CONST_VECTOR:
12715 case CONST_FIXED:
12716 case CLRSB:
12717 case CLOBBER:
12718 /* If delegitimize_address couldn't do anything with the UNSPEC, we
12719 can't express it in the debug info. This can happen e.g. with some
12720 TLS UNSPECs. */
12721 break;
12723 case CONST_STRING:
12724 resolve_one_addr (&rtl, NULL);
12725 goto symref;
12727 default:
12728 #ifdef ENABLE_CHECKING
12729 print_rtl (stderr, rtl);
12730 gcc_unreachable ();
12731 #else
12732 break;
12733 #endif
12736 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12737 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12739 return mem_loc_result;
12742 /* Return a descriptor that describes the concatenation of two locations.
12743 This is typically a complex variable. */
12745 static dw_loc_descr_ref
12746 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
12748 dw_loc_descr_ref cc_loc_result = NULL;
12749 dw_loc_descr_ref x0_ref
12750 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12751 dw_loc_descr_ref x1_ref
12752 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12754 if (x0_ref == 0 || x1_ref == 0)
12755 return 0;
12757 cc_loc_result = x0_ref;
12758 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
12760 add_loc_descr (&cc_loc_result, x1_ref);
12761 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
12763 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
12764 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12766 return cc_loc_result;
12769 /* Return a descriptor that describes the concatenation of N
12770 locations. */
12772 static dw_loc_descr_ref
12773 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
12775 unsigned int i;
12776 dw_loc_descr_ref cc_loc_result = NULL;
12777 unsigned int n = XVECLEN (concatn, 0);
12779 for (i = 0; i < n; ++i)
12781 dw_loc_descr_ref ref;
12782 rtx x = XVECEXP (concatn, 0, i);
12784 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12785 if (ref == NULL)
12786 return NULL;
12788 add_loc_descr (&cc_loc_result, ref);
12789 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
12792 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12793 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12795 return cc_loc_result;
12798 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
12799 for DEBUG_IMPLICIT_PTR RTL. */
12801 static dw_loc_descr_ref
12802 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
12804 dw_loc_descr_ref ret;
12805 dw_die_ref ref;
12807 if (dwarf_strict)
12808 return NULL;
12809 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
12810 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
12811 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
12812 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
12813 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
12814 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
12815 if (ref)
12817 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12818 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12819 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12821 else
12823 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12824 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
12826 return ret;
12829 /* Output a proper Dwarf location descriptor for a variable or parameter
12830 which is either allocated in a register or in a memory location. For a
12831 register, we just generate an OP_REG and the register number. For a
12832 memory location we provide a Dwarf postfix expression describing how to
12833 generate the (dynamic) address of the object onto the address stack.
12835 MODE is mode of the decl if this loc_descriptor is going to be used in
12836 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
12837 allowed, VOIDmode otherwise.
12839 If we don't know how to describe it, return 0. */
12841 static dw_loc_descr_ref
12842 loc_descriptor (rtx rtl, enum machine_mode mode,
12843 enum var_init_status initialized)
12845 dw_loc_descr_ref loc_result = NULL;
12847 switch (GET_CODE (rtl))
12849 case SUBREG:
12850 /* The case of a subreg may arise when we have a local (register)
12851 variable or a formal (register) parameter which doesn't quite fill
12852 up an entire register. For now, just assume that it is
12853 legitimate to make the Dwarf info refer to the whole register which
12854 contains the given subreg. */
12855 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
12856 loc_result = loc_descriptor (SUBREG_REG (rtl),
12857 GET_MODE (SUBREG_REG (rtl)), initialized);
12858 else
12859 goto do_default;
12860 break;
12862 case REG:
12863 loc_result = reg_loc_descriptor (rtl, initialized);
12864 break;
12866 case MEM:
12867 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
12868 GET_MODE (rtl), initialized);
12869 if (loc_result == NULL)
12870 loc_result = tls_mem_loc_descriptor (rtl);
12871 if (loc_result == NULL)
12873 rtx new_rtl = avoid_constant_pool_reference (rtl);
12874 if (new_rtl != rtl)
12875 loc_result = loc_descriptor (new_rtl, mode, initialized);
12877 break;
12879 case CONCAT:
12880 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
12881 initialized);
12882 break;
12884 case CONCATN:
12885 loc_result = concatn_loc_descriptor (rtl, initialized);
12886 break;
12888 case VAR_LOCATION:
12889 /* Single part. */
12890 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
12892 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
12893 if (GET_CODE (loc) == EXPR_LIST)
12894 loc = XEXP (loc, 0);
12895 loc_result = loc_descriptor (loc, mode, initialized);
12896 break;
12899 rtl = XEXP (rtl, 1);
12900 /* FALLTHRU */
12902 case PARALLEL:
12904 rtvec par_elems = XVEC (rtl, 0);
12905 int num_elem = GET_NUM_ELEM (par_elems);
12906 enum machine_mode mode;
12907 int i;
12909 /* Create the first one, so we have something to add to. */
12910 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
12911 VOIDmode, initialized);
12912 if (loc_result == NULL)
12913 return NULL;
12914 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
12915 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
12916 for (i = 1; i < num_elem; i++)
12918 dw_loc_descr_ref temp;
12920 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
12921 VOIDmode, initialized);
12922 if (temp == NULL)
12923 return NULL;
12924 add_loc_descr (&loc_result, temp);
12925 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
12926 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
12929 break;
12931 case CONST_INT:
12932 if (mode != VOIDmode && mode != BLKmode)
12933 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
12934 INTVAL (rtl));
12935 break;
12937 case CONST_DOUBLE:
12938 if (mode == VOIDmode)
12939 mode = GET_MODE (rtl);
12941 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
12943 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
12945 /* Note that a CONST_DOUBLE rtx could represent either an integer
12946 or a floating-point constant. A CONST_DOUBLE is used whenever
12947 the constant requires more than one word in order to be
12948 adequately represented. We output CONST_DOUBLEs as blocks. */
12949 loc_result = new_loc_descr (DW_OP_implicit_value,
12950 GET_MODE_SIZE (mode), 0);
12951 if (SCALAR_FLOAT_MODE_P (mode))
12953 unsigned int length = GET_MODE_SIZE (mode);
12954 unsigned char *array
12955 = (unsigned char*) ggc_alloc_atomic (length);
12957 insert_float (rtl, array);
12958 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12959 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12960 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12961 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12963 else
12965 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
12966 loc_result->dw_loc_oprnd2.v.val_double
12967 = rtx_to_double_int (rtl);
12970 break;
12972 case CONST_VECTOR:
12973 if (mode == VOIDmode)
12974 mode = GET_MODE (rtl);
12976 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
12978 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
12979 unsigned int length = CONST_VECTOR_NUNITS (rtl);
12980 unsigned char *array = (unsigned char *)
12981 ggc_alloc_atomic (length * elt_size);
12982 unsigned int i;
12983 unsigned char *p;
12985 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
12986 switch (GET_MODE_CLASS (mode))
12988 case MODE_VECTOR_INT:
12989 for (i = 0, p = array; i < length; i++, p += elt_size)
12991 rtx elt = CONST_VECTOR_ELT (rtl, i);
12992 double_int val = rtx_to_double_int (elt);
12994 if (elt_size <= sizeof (HOST_WIDE_INT))
12995 insert_int (val.to_shwi (), elt_size, p);
12996 else
12998 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
12999 insert_double (val, p);
13002 break;
13004 case MODE_VECTOR_FLOAT:
13005 for (i = 0, p = array; i < length; i++, p += elt_size)
13007 rtx elt = CONST_VECTOR_ELT (rtl, i);
13008 insert_float (elt, p);
13010 break;
13012 default:
13013 gcc_unreachable ();
13016 loc_result = new_loc_descr (DW_OP_implicit_value,
13017 length * elt_size, 0);
13018 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13019 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13020 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13021 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13023 break;
13025 case CONST:
13026 if (mode == VOIDmode
13027 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13028 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13029 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13031 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13032 break;
13034 /* FALLTHROUGH */
13035 case SYMBOL_REF:
13036 if (!const_ok_for_output (rtl))
13037 break;
13038 case LABEL_REF:
13039 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13040 && (dwarf_version >= 4 || !dwarf_strict))
13042 loc_result = new_addr_loc_descr (rtl, dtprel_false);
13043 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13044 vec_safe_push (used_rtx_array, rtl);
13046 break;
13048 case DEBUG_IMPLICIT_PTR:
13049 loc_result = implicit_ptr_descriptor (rtl, 0);
13050 break;
13052 case PLUS:
13053 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13054 && CONST_INT_P (XEXP (rtl, 1)))
13056 loc_result
13057 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13058 break;
13060 /* FALLTHRU */
13061 do_default:
13062 default:
13063 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13064 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13065 && dwarf_version >= 4)
13066 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13068 /* Value expression. */
13069 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13070 if (loc_result)
13071 add_loc_descr (&loc_result,
13072 new_loc_descr (DW_OP_stack_value, 0, 0));
13074 break;
13077 return loc_result;
13080 /* We need to figure out what section we should use as the base for the
13081 address ranges where a given location is valid.
13082 1. If this particular DECL has a section associated with it, use that.
13083 2. If this function has a section associated with it, use that.
13084 3. Otherwise, use the text section.
13085 XXX: If you split a variable across multiple sections, we won't notice. */
13087 static const char *
13088 secname_for_decl (const_tree decl)
13090 const char *secname;
13092 if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
13094 tree sectree = DECL_SECTION_NAME (decl);
13095 secname = TREE_STRING_POINTER (sectree);
13097 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13099 tree sectree = DECL_SECTION_NAME (current_function_decl);
13100 secname = TREE_STRING_POINTER (sectree);
13102 else if (cfun && in_cold_section_p)
13103 secname = crtl->subsections.cold_section_label;
13104 else
13105 secname = text_section_label;
13107 return secname;
13110 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
13112 static bool
13113 decl_by_reference_p (tree decl)
13115 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13116 || TREE_CODE (decl) == VAR_DECL)
13117 && DECL_BY_REFERENCE (decl));
13120 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13121 for VARLOC. */
13123 static dw_loc_descr_ref
13124 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13125 enum var_init_status initialized)
13127 int have_address = 0;
13128 dw_loc_descr_ref descr;
13129 enum machine_mode mode;
13131 if (want_address != 2)
13133 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13134 /* Single part. */
13135 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13137 varloc = PAT_VAR_LOCATION_LOC (varloc);
13138 if (GET_CODE (varloc) == EXPR_LIST)
13139 varloc = XEXP (varloc, 0);
13140 mode = GET_MODE (varloc);
13141 if (MEM_P (varloc))
13143 rtx addr = XEXP (varloc, 0);
13144 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13145 mode, initialized);
13146 if (descr)
13147 have_address = 1;
13148 else
13150 rtx x = avoid_constant_pool_reference (varloc);
13151 if (x != varloc)
13152 descr = mem_loc_descriptor (x, mode, VOIDmode,
13153 initialized);
13156 else
13157 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13159 else
13160 return 0;
13162 else
13164 if (GET_CODE (varloc) == VAR_LOCATION)
13165 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13166 else
13167 mode = DECL_MODE (loc);
13168 descr = loc_descriptor (varloc, mode, initialized);
13169 have_address = 1;
13172 if (!descr)
13173 return 0;
13175 if (want_address == 2 && !have_address
13176 && (dwarf_version >= 4 || !dwarf_strict))
13178 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13180 expansion_failed (loc, NULL_RTX,
13181 "DWARF address size mismatch");
13182 return 0;
13184 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13185 have_address = 1;
13187 /* Show if we can't fill the request for an address. */
13188 if (want_address && !have_address)
13190 expansion_failed (loc, NULL_RTX,
13191 "Want address and only have value");
13192 return 0;
13195 /* If we've got an address and don't want one, dereference. */
13196 if (!want_address && have_address)
13198 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13199 enum dwarf_location_atom op;
13201 if (size > DWARF2_ADDR_SIZE || size == -1)
13203 expansion_failed (loc, NULL_RTX,
13204 "DWARF address size mismatch");
13205 return 0;
13207 else if (size == DWARF2_ADDR_SIZE)
13208 op = DW_OP_deref;
13209 else
13210 op = DW_OP_deref_size;
13212 add_loc_descr (&descr, new_loc_descr (op, size, 0));
13215 return descr;
13218 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
13219 if it is not possible. */
13221 static dw_loc_descr_ref
13222 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
13224 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
13225 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
13226 else if (dwarf_version >= 3 || !dwarf_strict)
13227 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
13228 else
13229 return NULL;
13232 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13233 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
13235 static dw_loc_descr_ref
13236 dw_sra_loc_expr (tree decl, rtx loc)
13238 rtx p;
13239 unsigned int padsize = 0;
13240 dw_loc_descr_ref descr, *descr_tail;
13241 unsigned HOST_WIDE_INT decl_size;
13242 rtx varloc;
13243 enum var_init_status initialized;
13245 if (DECL_SIZE (decl) == NULL
13246 || !host_integerp (DECL_SIZE (decl), 1))
13247 return NULL;
13249 decl_size = tree_low_cst (DECL_SIZE (decl), 1);
13250 descr = NULL;
13251 descr_tail = &descr;
13253 for (p = loc; p; p = XEXP (p, 1))
13255 unsigned int bitsize = decl_piece_bitsize (p);
13256 rtx loc_note = *decl_piece_varloc_ptr (p);
13257 dw_loc_descr_ref cur_descr;
13258 dw_loc_descr_ref *tail, last = NULL;
13259 unsigned int opsize = 0;
13261 if (loc_note == NULL_RTX
13262 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
13264 padsize += bitsize;
13265 continue;
13267 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
13268 varloc = NOTE_VAR_LOCATION (loc_note);
13269 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
13270 if (cur_descr == NULL)
13272 padsize += bitsize;
13273 continue;
13276 /* Check that cur_descr either doesn't use
13277 DW_OP_*piece operations, or their sum is equal
13278 to bitsize. Otherwise we can't embed it. */
13279 for (tail = &cur_descr; *tail != NULL;
13280 tail = &(*tail)->dw_loc_next)
13281 if ((*tail)->dw_loc_opc == DW_OP_piece)
13283 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
13284 * BITS_PER_UNIT;
13285 last = *tail;
13287 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
13289 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
13290 last = *tail;
13293 if (last != NULL && opsize != bitsize)
13295 padsize += bitsize;
13296 continue;
13299 /* If there is a hole, add DW_OP_*piece after empty DWARF
13300 expression, which means that those bits are optimized out. */
13301 if (padsize)
13303 if (padsize > decl_size)
13304 return NULL;
13305 decl_size -= padsize;
13306 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
13307 if (*descr_tail == NULL)
13308 return NULL;
13309 descr_tail = &(*descr_tail)->dw_loc_next;
13310 padsize = 0;
13312 *descr_tail = cur_descr;
13313 descr_tail = tail;
13314 if (bitsize > decl_size)
13315 return NULL;
13316 decl_size -= bitsize;
13317 if (last == NULL)
13319 HOST_WIDE_INT offset = 0;
13320 if (GET_CODE (varloc) == VAR_LOCATION
13321 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13323 varloc = PAT_VAR_LOCATION_LOC (varloc);
13324 if (GET_CODE (varloc) == EXPR_LIST)
13325 varloc = XEXP (varloc, 0);
13329 if (GET_CODE (varloc) == CONST
13330 || GET_CODE (varloc) == SIGN_EXTEND
13331 || GET_CODE (varloc) == ZERO_EXTEND)
13332 varloc = XEXP (varloc, 0);
13333 else if (GET_CODE (varloc) == SUBREG)
13334 varloc = SUBREG_REG (varloc);
13335 else
13336 break;
13338 while (1);
13339 /* DW_OP_bit_size offset should be zero for register
13340 or implicit location descriptions and empty location
13341 descriptions, but for memory addresses needs big endian
13342 adjustment. */
13343 if (MEM_P (varloc))
13345 unsigned HOST_WIDE_INT memsize
13346 = MEM_SIZE (varloc) * BITS_PER_UNIT;
13347 if (memsize != bitsize)
13349 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
13350 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
13351 return NULL;
13352 if (memsize < bitsize)
13353 return NULL;
13354 if (BITS_BIG_ENDIAN)
13355 offset = memsize - bitsize;
13359 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
13360 if (*descr_tail == NULL)
13361 return NULL;
13362 descr_tail = &(*descr_tail)->dw_loc_next;
13366 /* If there were any non-empty expressions, add padding till the end of
13367 the decl. */
13368 if (descr != NULL && decl_size != 0)
13370 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
13371 if (*descr_tail == NULL)
13372 return NULL;
13374 return descr;
13377 /* Return the dwarf representation of the location list LOC_LIST of
13378 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
13379 function. */
13381 static dw_loc_list_ref
13382 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
13384 const char *endname, *secname;
13385 rtx varloc;
13386 enum var_init_status initialized;
13387 struct var_loc_node *node;
13388 dw_loc_descr_ref descr;
13389 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13390 dw_loc_list_ref list = NULL;
13391 dw_loc_list_ref *listp = &list;
13393 /* Now that we know what section we are using for a base,
13394 actually construct the list of locations.
13395 The first location information is what is passed to the
13396 function that creates the location list, and the remaining
13397 locations just get added on to that list.
13398 Note that we only know the start address for a location
13399 (IE location changes), so to build the range, we use
13400 the range [current location start, next location start].
13401 This means we have to special case the last node, and generate
13402 a range of [last location start, end of function label]. */
13404 secname = secname_for_decl (decl);
13406 for (node = loc_list->first; node; node = node->next)
13407 if (GET_CODE (node->loc) == EXPR_LIST
13408 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
13410 if (GET_CODE (node->loc) == EXPR_LIST)
13412 /* This requires DW_OP_{,bit_}piece, which is not usable
13413 inside DWARF expressions. */
13414 if (want_address != 2)
13415 continue;
13416 descr = dw_sra_loc_expr (decl, node->loc);
13417 if (descr == NULL)
13418 continue;
13420 else
13422 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13423 varloc = NOTE_VAR_LOCATION (node->loc);
13424 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
13426 if (descr)
13428 bool range_across_switch = false;
13429 /* If section switch happens in between node->label
13430 and node->next->label (or end of function) and
13431 we can't emit it as a single entry list,
13432 emit two ranges, first one ending at the end
13433 of first partition and second one starting at the
13434 beginning of second partition. */
13435 if (node == loc_list->last_before_switch
13436 && (node != loc_list->first || loc_list->first->next)
13437 && current_function_decl)
13439 endname = cfun->fde->dw_fde_end;
13440 range_across_switch = true;
13442 /* The variable has a location between NODE->LABEL and
13443 NODE->NEXT->LABEL. */
13444 else if (node->next)
13445 endname = node->next->label;
13446 /* If the variable has a location at the last label
13447 it keeps its location until the end of function. */
13448 else if (!current_function_decl)
13449 endname = text_end_label;
13450 else
13452 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13453 current_function_funcdef_no);
13454 endname = ggc_strdup (label_id);
13457 *listp = new_loc_list (descr, node->label, endname, secname);
13458 if (TREE_CODE (decl) == PARM_DECL
13459 && node == loc_list->first
13460 && GET_CODE (node->loc) == NOTE
13461 && strcmp (node->label, endname) == 0)
13462 (*listp)->force = true;
13463 listp = &(*listp)->dw_loc_next;
13465 if (range_across_switch)
13467 if (GET_CODE (node->loc) == EXPR_LIST)
13468 descr = dw_sra_loc_expr (decl, node->loc);
13469 else
13471 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13472 varloc = NOTE_VAR_LOCATION (node->loc);
13473 descr = dw_loc_list_1 (decl, varloc, want_address,
13474 initialized);
13476 gcc_assert (descr);
13477 /* The variable has a location between NODE->LABEL and
13478 NODE->NEXT->LABEL. */
13479 if (node->next)
13480 endname = node->next->label;
13481 else
13482 endname = cfun->fde->dw_fde_second_end;
13483 *listp = new_loc_list (descr,
13484 cfun->fde->dw_fde_second_begin,
13485 endname, secname);
13486 listp = &(*listp)->dw_loc_next;
13491 /* Try to avoid the overhead of a location list emitting a location
13492 expression instead, but only if we didn't have more than one
13493 location entry in the first place. If some entries were not
13494 representable, we don't want to pretend a single entry that was
13495 applies to the entire scope in which the variable is
13496 available. */
13497 if (list && loc_list->first->next)
13498 gen_llsym (list);
13500 return list;
13503 /* Return if the loc_list has only single element and thus can be represented
13504 as location description. */
13506 static bool
13507 single_element_loc_list_p (dw_loc_list_ref list)
13509 gcc_assert (!list->dw_loc_next || list->ll_symbol);
13510 return !list->ll_symbol;
13513 /* To each location in list LIST add loc descr REF. */
13515 static void
13516 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
13518 dw_loc_descr_ref copy;
13519 add_loc_descr (&list->expr, ref);
13520 list = list->dw_loc_next;
13521 while (list)
13523 copy = ggc_alloc_dw_loc_descr_node ();
13524 memcpy (copy, ref, sizeof (dw_loc_descr_node));
13525 add_loc_descr (&list->expr, copy);
13526 while (copy->dw_loc_next)
13528 dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
13529 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
13530 copy->dw_loc_next = new_copy;
13531 copy = new_copy;
13533 list = list->dw_loc_next;
13537 /* Given two lists RET and LIST
13538 produce location list that is result of adding expression in LIST
13539 to expression in RET on each position in program.
13540 Might be destructive on both RET and LIST.
13542 TODO: We handle only simple cases of RET or LIST having at most one
13543 element. General case would inolve sorting the lists in program order
13544 and merging them that will need some additional work.
13545 Adding that will improve quality of debug info especially for SRA-ed
13546 structures. */
13548 static void
13549 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
13551 if (!list)
13552 return;
13553 if (!*ret)
13555 *ret = list;
13556 return;
13558 if (!list->dw_loc_next)
13560 add_loc_descr_to_each (*ret, list->expr);
13561 return;
13563 if (!(*ret)->dw_loc_next)
13565 add_loc_descr_to_each (list, (*ret)->expr);
13566 *ret = list;
13567 return;
13569 expansion_failed (NULL_TREE, NULL_RTX,
13570 "Don't know how to merge two non-trivial"
13571 " location lists.\n");
13572 *ret = NULL;
13573 return;
13576 /* LOC is constant expression. Try a luck, look it up in constant
13577 pool and return its loc_descr of its address. */
13579 static dw_loc_descr_ref
13580 cst_pool_loc_descr (tree loc)
13582 /* Get an RTL for this, if something has been emitted. */
13583 rtx rtl = lookup_constant_def (loc);
13585 if (!rtl || !MEM_P (rtl))
13587 gcc_assert (!rtl);
13588 return 0;
13590 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
13592 /* TODO: We might get more coverage if we was actually delaying expansion
13593 of all expressions till end of compilation when constant pools are fully
13594 populated. */
13595 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
13597 expansion_failed (loc, NULL_RTX,
13598 "CST value in contant pool but not marked.");
13599 return 0;
13601 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13602 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
13605 /* Return dw_loc_list representing address of addr_expr LOC
13606 by looking for inner INDIRECT_REF expression and turning
13607 it into simple arithmetics. */
13609 static dw_loc_list_ref
13610 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
13612 tree obj, offset;
13613 HOST_WIDE_INT bitsize, bitpos, bytepos;
13614 enum machine_mode mode;
13615 int unsignedp, volatilep = 0;
13616 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13618 obj = get_inner_reference (TREE_OPERAND (loc, 0),
13619 &bitsize, &bitpos, &offset, &mode,
13620 &unsignedp, &volatilep, false);
13621 STRIP_NOPS (obj);
13622 if (bitpos % BITS_PER_UNIT)
13624 expansion_failed (loc, NULL_RTX, "bitfield access");
13625 return 0;
13627 if (!INDIRECT_REF_P (obj))
13629 expansion_failed (obj,
13630 NULL_RTX, "no indirect ref in inner refrence");
13631 return 0;
13633 if (!offset && !bitpos)
13634 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
13635 else if (toplev
13636 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
13637 && (dwarf_version >= 4 || !dwarf_strict))
13639 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
13640 if (!list_ret)
13641 return 0;
13642 if (offset)
13644 /* Variable offset. */
13645 list_ret1 = loc_list_from_tree (offset, 0);
13646 if (list_ret1 == 0)
13647 return 0;
13648 add_loc_list (&list_ret, list_ret1);
13649 if (!list_ret)
13650 return 0;
13651 add_loc_descr_to_each (list_ret,
13652 new_loc_descr (DW_OP_plus, 0, 0));
13654 bytepos = bitpos / BITS_PER_UNIT;
13655 if (bytepos > 0)
13656 add_loc_descr_to_each (list_ret,
13657 new_loc_descr (DW_OP_plus_uconst,
13658 bytepos, 0));
13659 else if (bytepos < 0)
13660 loc_list_plus_const (list_ret, bytepos);
13661 add_loc_descr_to_each (list_ret,
13662 new_loc_descr (DW_OP_stack_value, 0, 0));
13664 return list_ret;
13668 /* Generate Dwarf location list representing LOC.
13669 If WANT_ADDRESS is false, expression computing LOC will be computed
13670 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
13671 if WANT_ADDRESS is 2, expression computing address useable in location
13672 will be returned (i.e. DW_OP_reg can be used
13673 to refer to register values). */
13675 static dw_loc_list_ref
13676 loc_list_from_tree (tree loc, int want_address)
13678 dw_loc_descr_ref ret = NULL, ret1 = NULL;
13679 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13680 int have_address = 0;
13681 enum dwarf_location_atom op;
13683 /* ??? Most of the time we do not take proper care for sign/zero
13684 extending the values properly. Hopefully this won't be a real
13685 problem... */
13687 switch (TREE_CODE (loc))
13689 case ERROR_MARK:
13690 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
13691 return 0;
13693 case PLACEHOLDER_EXPR:
13694 /* This case involves extracting fields from an object to determine the
13695 position of other fields. We don't try to encode this here. The
13696 only user of this is Ada, which encodes the needed information using
13697 the names of types. */
13698 expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
13699 return 0;
13701 case CALL_EXPR:
13702 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
13703 /* There are no opcodes for these operations. */
13704 return 0;
13706 case PREINCREMENT_EXPR:
13707 case PREDECREMENT_EXPR:
13708 case POSTINCREMENT_EXPR:
13709 case POSTDECREMENT_EXPR:
13710 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
13711 /* There are no opcodes for these operations. */
13712 return 0;
13714 case ADDR_EXPR:
13715 /* If we already want an address, see if there is INDIRECT_REF inside
13716 e.g. for &this->field. */
13717 if (want_address)
13719 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
13720 (loc, want_address == 2);
13721 if (list_ret)
13722 have_address = 1;
13723 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
13724 && (ret = cst_pool_loc_descr (loc)))
13725 have_address = 1;
13727 /* Otherwise, process the argument and look for the address. */
13728 if (!list_ret && !ret)
13729 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
13730 else
13732 if (want_address)
13733 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
13734 return NULL;
13736 break;
13738 case VAR_DECL:
13739 if (DECL_THREAD_LOCAL_P (loc))
13741 rtx rtl;
13742 enum dwarf_location_atom tls_op;
13743 enum dtprel_bool dtprel = dtprel_false;
13745 if (targetm.have_tls)
13747 /* If this is not defined, we have no way to emit the
13748 data. */
13749 if (!targetm.asm_out.output_dwarf_dtprel)
13750 return 0;
13752 /* The way DW_OP_GNU_push_tls_address is specified, we
13753 can only look up addresses of objects in the current
13754 module. We used DW_OP_addr as first op, but that's
13755 wrong, because DW_OP_addr is relocated by the debug
13756 info consumer, while DW_OP_GNU_push_tls_address
13757 operand shouldn't be. */
13758 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
13759 return 0;
13760 dtprel = dtprel_true;
13761 tls_op = DW_OP_GNU_push_tls_address;
13763 else
13765 if (!targetm.emutls.debug_form_tls_address
13766 || !(dwarf_version >= 3 || !dwarf_strict))
13767 return 0;
13768 /* We stuffed the control variable into the DECL_VALUE_EXPR
13769 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
13770 no longer appear in gimple code. We used the control
13771 variable in specific so that we could pick it up here. */
13772 loc = DECL_VALUE_EXPR (loc);
13773 tls_op = DW_OP_form_tls_address;
13776 rtl = rtl_for_decl_location (loc);
13777 if (rtl == NULL_RTX)
13778 return 0;
13780 if (!MEM_P (rtl))
13781 return 0;
13782 rtl = XEXP (rtl, 0);
13783 if (! CONSTANT_P (rtl))
13784 return 0;
13786 ret = new_addr_loc_descr (rtl, dtprel);
13787 ret1 = new_loc_descr (tls_op, 0, 0);
13788 add_loc_descr (&ret, ret1);
13790 have_address = 1;
13791 break;
13793 /* FALLTHRU */
13795 case PARM_DECL:
13796 case RESULT_DECL:
13797 if (DECL_HAS_VALUE_EXPR_P (loc))
13798 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
13799 want_address);
13800 /* FALLTHRU */
13802 case FUNCTION_DECL:
13804 rtx rtl;
13805 var_loc_list *loc_list = lookup_decl_loc (loc);
13807 if (loc_list && loc_list->first)
13809 list_ret = dw_loc_list (loc_list, loc, want_address);
13810 have_address = want_address != 0;
13811 break;
13813 rtl = rtl_for_decl_location (loc);
13814 if (rtl == NULL_RTX)
13816 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
13817 return 0;
13819 else if (CONST_INT_P (rtl))
13821 HOST_WIDE_INT val = INTVAL (rtl);
13822 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13823 val &= GET_MODE_MASK (DECL_MODE (loc));
13824 ret = int_loc_descriptor (val);
13826 else if (GET_CODE (rtl) == CONST_STRING)
13828 expansion_failed (loc, NULL_RTX, "CONST_STRING");
13829 return 0;
13831 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
13832 ret = new_addr_loc_descr (rtl, dtprel_false);
13833 else
13835 enum machine_mode mode, mem_mode;
13837 /* Certain constructs can only be represented at top-level. */
13838 if (want_address == 2)
13840 ret = loc_descriptor (rtl, VOIDmode,
13841 VAR_INIT_STATUS_INITIALIZED);
13842 have_address = 1;
13844 else
13846 mode = GET_MODE (rtl);
13847 mem_mode = VOIDmode;
13848 if (MEM_P (rtl))
13850 mem_mode = mode;
13851 mode = get_address_mode (rtl);
13852 rtl = XEXP (rtl, 0);
13853 have_address = 1;
13855 ret = mem_loc_descriptor (rtl, mode, mem_mode,
13856 VAR_INIT_STATUS_INITIALIZED);
13858 if (!ret)
13859 expansion_failed (loc, rtl,
13860 "failed to produce loc descriptor for rtl");
13863 break;
13865 case MEM_REF:
13866 /* ??? FIXME. */
13867 if (!integer_zerop (TREE_OPERAND (loc, 1)))
13868 return 0;
13869 /* Fallthru. */
13870 case INDIRECT_REF:
13871 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13872 have_address = 1;
13873 break;
13875 case COMPOUND_EXPR:
13876 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
13878 CASE_CONVERT:
13879 case VIEW_CONVERT_EXPR:
13880 case SAVE_EXPR:
13881 case MODIFY_EXPR:
13882 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
13884 case COMPONENT_REF:
13885 case BIT_FIELD_REF:
13886 case ARRAY_REF:
13887 case ARRAY_RANGE_REF:
13888 case REALPART_EXPR:
13889 case IMAGPART_EXPR:
13891 tree obj, offset;
13892 HOST_WIDE_INT bitsize, bitpos, bytepos;
13893 enum machine_mode mode;
13894 int unsignedp, volatilep = 0;
13896 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
13897 &unsignedp, &volatilep, false);
13899 gcc_assert (obj != loc);
13901 list_ret = loc_list_from_tree (obj,
13902 want_address == 2
13903 && !bitpos && !offset ? 2 : 1);
13904 /* TODO: We can extract value of the small expression via shifting even
13905 for nonzero bitpos. */
13906 if (list_ret == 0)
13907 return 0;
13908 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
13910 expansion_failed (loc, NULL_RTX,
13911 "bitfield access");
13912 return 0;
13915 if (offset != NULL_TREE)
13917 /* Variable offset. */
13918 list_ret1 = loc_list_from_tree (offset, 0);
13919 if (list_ret1 == 0)
13920 return 0;
13921 add_loc_list (&list_ret, list_ret1);
13922 if (!list_ret)
13923 return 0;
13924 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
13927 bytepos = bitpos / BITS_PER_UNIT;
13928 if (bytepos > 0)
13929 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
13930 else if (bytepos < 0)
13931 loc_list_plus_const (list_ret, bytepos);
13933 have_address = 1;
13934 break;
13937 case INTEGER_CST:
13938 if ((want_address || !host_integerp (loc, 0))
13939 && (ret = cst_pool_loc_descr (loc)))
13940 have_address = 1;
13941 else if (want_address == 2
13942 && host_integerp (loc, 0)
13943 && (ret = address_of_int_loc_descriptor
13944 (int_size_in_bytes (TREE_TYPE (loc)),
13945 tree_low_cst (loc, 0))))
13946 have_address = 1;
13947 else if (host_integerp (loc, 0))
13948 ret = int_loc_descriptor (tree_low_cst (loc, 0));
13949 else
13951 expansion_failed (loc, NULL_RTX,
13952 "Integer operand is not host integer");
13953 return 0;
13955 break;
13957 case CONSTRUCTOR:
13958 case REAL_CST:
13959 case STRING_CST:
13960 case COMPLEX_CST:
13961 if ((ret = cst_pool_loc_descr (loc)))
13962 have_address = 1;
13963 else
13964 /* We can construct small constants here using int_loc_descriptor. */
13965 expansion_failed (loc, NULL_RTX,
13966 "constructor or constant not in constant pool");
13967 break;
13969 case TRUTH_AND_EXPR:
13970 case TRUTH_ANDIF_EXPR:
13971 case BIT_AND_EXPR:
13972 op = DW_OP_and;
13973 goto do_binop;
13975 case TRUTH_XOR_EXPR:
13976 case BIT_XOR_EXPR:
13977 op = DW_OP_xor;
13978 goto do_binop;
13980 case TRUTH_OR_EXPR:
13981 case TRUTH_ORIF_EXPR:
13982 case BIT_IOR_EXPR:
13983 op = DW_OP_or;
13984 goto do_binop;
13986 case FLOOR_DIV_EXPR:
13987 case CEIL_DIV_EXPR:
13988 case ROUND_DIV_EXPR:
13989 case TRUNC_DIV_EXPR:
13990 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13991 return 0;
13992 op = DW_OP_div;
13993 goto do_binop;
13995 case MINUS_EXPR:
13996 op = DW_OP_minus;
13997 goto do_binop;
13999 case FLOOR_MOD_EXPR:
14000 case CEIL_MOD_EXPR:
14001 case ROUND_MOD_EXPR:
14002 case TRUNC_MOD_EXPR:
14003 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14005 op = DW_OP_mod;
14006 goto do_binop;
14008 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14009 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14010 if (list_ret == 0 || list_ret1 == 0)
14011 return 0;
14013 add_loc_list (&list_ret, list_ret1);
14014 if (list_ret == 0)
14015 return 0;
14016 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14017 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14018 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14019 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14020 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14021 break;
14023 case MULT_EXPR:
14024 op = DW_OP_mul;
14025 goto do_binop;
14027 case LSHIFT_EXPR:
14028 op = DW_OP_shl;
14029 goto do_binop;
14031 case RSHIFT_EXPR:
14032 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14033 goto do_binop;
14035 case POINTER_PLUS_EXPR:
14036 case PLUS_EXPR:
14037 if (host_integerp (TREE_OPERAND (loc, 1), 0))
14039 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14040 if (list_ret == 0)
14041 return 0;
14043 loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
14044 break;
14047 op = DW_OP_plus;
14048 goto do_binop;
14050 case LE_EXPR:
14051 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14052 return 0;
14054 op = DW_OP_le;
14055 goto do_binop;
14057 case GE_EXPR:
14058 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14059 return 0;
14061 op = DW_OP_ge;
14062 goto do_binop;
14064 case LT_EXPR:
14065 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14066 return 0;
14068 op = DW_OP_lt;
14069 goto do_binop;
14071 case GT_EXPR:
14072 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14073 return 0;
14075 op = DW_OP_gt;
14076 goto do_binop;
14078 case EQ_EXPR:
14079 op = DW_OP_eq;
14080 goto do_binop;
14082 case NE_EXPR:
14083 op = DW_OP_ne;
14084 goto do_binop;
14086 do_binop:
14087 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14088 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14089 if (list_ret == 0 || list_ret1 == 0)
14090 return 0;
14092 add_loc_list (&list_ret, list_ret1);
14093 if (list_ret == 0)
14094 return 0;
14095 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14096 break;
14098 case TRUTH_NOT_EXPR:
14099 case BIT_NOT_EXPR:
14100 op = DW_OP_not;
14101 goto do_unop;
14103 case ABS_EXPR:
14104 op = DW_OP_abs;
14105 goto do_unop;
14107 case NEGATE_EXPR:
14108 op = DW_OP_neg;
14109 goto do_unop;
14111 do_unop:
14112 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14113 if (list_ret == 0)
14114 return 0;
14116 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14117 break;
14119 case MIN_EXPR:
14120 case MAX_EXPR:
14122 const enum tree_code code =
14123 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14125 loc = build3 (COND_EXPR, TREE_TYPE (loc),
14126 build2 (code, integer_type_node,
14127 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14128 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
14131 /* ... fall through ... */
14133 case COND_EXPR:
14135 dw_loc_descr_ref lhs
14136 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
14137 dw_loc_list_ref rhs
14138 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
14139 dw_loc_descr_ref bra_node, jump_node, tmp;
14141 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14142 if (list_ret == 0 || lhs == 0 || rhs == 0)
14143 return 0;
14145 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14146 add_loc_descr_to_each (list_ret, bra_node);
14148 add_loc_list (&list_ret, rhs);
14149 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
14150 add_loc_descr_to_each (list_ret, jump_node);
14152 add_loc_descr_to_each (list_ret, lhs);
14153 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14154 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
14156 /* ??? Need a node to point the skip at. Use a nop. */
14157 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14158 add_loc_descr_to_each (list_ret, tmp);
14159 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14160 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
14162 break;
14164 case FIX_TRUNC_EXPR:
14165 return 0;
14167 default:
14168 /* Leave front-end specific codes as simply unknown. This comes
14169 up, for instance, with the C STMT_EXPR. */
14170 if ((unsigned int) TREE_CODE (loc)
14171 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
14173 expansion_failed (loc, NULL_RTX,
14174 "language specific tree node");
14175 return 0;
14178 #ifdef ENABLE_CHECKING
14179 /* Otherwise this is a generic code; we should just lists all of
14180 these explicitly. We forgot one. */
14181 gcc_unreachable ();
14182 #else
14183 /* In a release build, we want to degrade gracefully: better to
14184 generate incomplete debugging information than to crash. */
14185 return NULL;
14186 #endif
14189 if (!ret && !list_ret)
14190 return 0;
14192 if (want_address == 2 && !have_address
14193 && (dwarf_version >= 4 || !dwarf_strict))
14195 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14197 expansion_failed (loc, NULL_RTX,
14198 "DWARF address size mismatch");
14199 return 0;
14201 if (ret)
14202 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
14203 else
14204 add_loc_descr_to_each (list_ret,
14205 new_loc_descr (DW_OP_stack_value, 0, 0));
14206 have_address = 1;
14208 /* Show if we can't fill the request for an address. */
14209 if (want_address && !have_address)
14211 expansion_failed (loc, NULL_RTX,
14212 "Want address and only have value");
14213 return 0;
14216 gcc_assert (!ret || !list_ret);
14218 /* If we've got an address and don't want one, dereference. */
14219 if (!want_address && have_address)
14221 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14223 if (size > DWARF2_ADDR_SIZE || size == -1)
14225 expansion_failed (loc, NULL_RTX,
14226 "DWARF address size mismatch");
14227 return 0;
14229 else if (size == DWARF2_ADDR_SIZE)
14230 op = DW_OP_deref;
14231 else
14232 op = DW_OP_deref_size;
14234 if (ret)
14235 add_loc_descr (&ret, new_loc_descr (op, size, 0));
14236 else
14237 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
14239 if (ret)
14240 list_ret = new_loc_list (ret, NULL, NULL, NULL);
14242 return list_ret;
14245 /* Same as above but return only single location expression. */
14246 static dw_loc_descr_ref
14247 loc_descriptor_from_tree (tree loc, int want_address)
14249 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
14250 if (!ret)
14251 return NULL;
14252 if (ret->dw_loc_next)
14254 expansion_failed (loc, NULL_RTX,
14255 "Location list where only loc descriptor needed");
14256 return NULL;
14258 return ret->expr;
14261 /* Given a value, round it up to the lowest multiple of `boundary'
14262 which is not less than the value itself. */
14264 static inline HOST_WIDE_INT
14265 ceiling (HOST_WIDE_INT value, unsigned int boundary)
14267 return (((value + boundary - 1) / boundary) * boundary);
14270 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
14271 pointer to the declared type for the relevant field variable, or return
14272 `integer_type_node' if the given node turns out to be an
14273 ERROR_MARK node. */
14275 static inline tree
14276 field_type (const_tree decl)
14278 tree type;
14280 if (TREE_CODE (decl) == ERROR_MARK)
14281 return integer_type_node;
14283 type = DECL_BIT_FIELD_TYPE (decl);
14284 if (type == NULL_TREE)
14285 type = TREE_TYPE (decl);
14287 return type;
14290 /* Given a pointer to a tree node, return the alignment in bits for
14291 it, or else return BITS_PER_WORD if the node actually turns out to
14292 be an ERROR_MARK node. */
14294 static inline unsigned
14295 simple_type_align_in_bits (const_tree type)
14297 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
14300 static inline unsigned
14301 simple_decl_align_in_bits (const_tree decl)
14303 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
14306 /* Return the result of rounding T up to ALIGN. */
14308 static inline double_int
14309 round_up_to_align (double_int t, unsigned int align)
14311 double_int alignd = double_int::from_uhwi (align);
14312 t += alignd;
14313 t += double_int_minus_one;
14314 t = t.div (alignd, true, TRUNC_DIV_EXPR);
14315 t *= alignd;
14316 return t;
14319 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
14320 lowest addressed byte of the "containing object" for the given FIELD_DECL,
14321 or return 0 if we are unable to determine what that offset is, either
14322 because the argument turns out to be a pointer to an ERROR_MARK node, or
14323 because the offset is actually variable. (We can't handle the latter case
14324 just yet). */
14326 static HOST_WIDE_INT
14327 field_byte_offset (const_tree decl)
14329 double_int object_offset_in_bits;
14330 double_int object_offset_in_bytes;
14331 double_int bitpos_int;
14333 if (TREE_CODE (decl) == ERROR_MARK)
14334 return 0;
14336 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
14338 /* We cannot yet cope with fields whose positions are variable, so
14339 for now, when we see such things, we simply return 0. Someday, we may
14340 be able to handle such cases, but it will be damn difficult. */
14341 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
14342 return 0;
14344 bitpos_int = tree_to_double_int (bit_position (decl));
14346 #ifdef PCC_BITFIELD_TYPE_MATTERS
14347 if (PCC_BITFIELD_TYPE_MATTERS)
14349 tree type;
14350 tree field_size_tree;
14351 double_int deepest_bitpos;
14352 double_int field_size_in_bits;
14353 unsigned int type_align_in_bits;
14354 unsigned int decl_align_in_bits;
14355 double_int type_size_in_bits;
14357 type = field_type (decl);
14358 type_size_in_bits = double_int_type_size_in_bits (type);
14359 type_align_in_bits = simple_type_align_in_bits (type);
14361 field_size_tree = DECL_SIZE (decl);
14363 /* The size could be unspecified if there was an error, or for
14364 a flexible array member. */
14365 if (!field_size_tree)
14366 field_size_tree = bitsize_zero_node;
14368 /* If the size of the field is not constant, use the type size. */
14369 if (TREE_CODE (field_size_tree) == INTEGER_CST)
14370 field_size_in_bits = tree_to_double_int (field_size_tree);
14371 else
14372 field_size_in_bits = type_size_in_bits;
14374 decl_align_in_bits = simple_decl_align_in_bits (decl);
14376 /* The GCC front-end doesn't make any attempt to keep track of the
14377 starting bit offset (relative to the start of the containing
14378 structure type) of the hypothetical "containing object" for a
14379 bit-field. Thus, when computing the byte offset value for the
14380 start of the "containing object" of a bit-field, we must deduce
14381 this information on our own. This can be rather tricky to do in
14382 some cases. For example, handling the following structure type
14383 definition when compiling for an i386/i486 target (which only
14384 aligns long long's to 32-bit boundaries) can be very tricky:
14386 struct S { int field1; long long field2:31; };
14388 Fortunately, there is a simple rule-of-thumb which can be used
14389 in such cases. When compiling for an i386/i486, GCC will
14390 allocate 8 bytes for the structure shown above. It decides to
14391 do this based upon one simple rule for bit-field allocation.
14392 GCC allocates each "containing object" for each bit-field at
14393 the first (i.e. lowest addressed) legitimate alignment boundary
14394 (based upon the required minimum alignment for the declared
14395 type of the field) which it can possibly use, subject to the
14396 condition that there is still enough available space remaining
14397 in the containing object (when allocated at the selected point)
14398 to fully accommodate all of the bits of the bit-field itself.
14400 This simple rule makes it obvious why GCC allocates 8 bytes for
14401 each object of the structure type shown above. When looking
14402 for a place to allocate the "containing object" for `field2',
14403 the compiler simply tries to allocate a 64-bit "containing
14404 object" at each successive 32-bit boundary (starting at zero)
14405 until it finds a place to allocate that 64- bit field such that
14406 at least 31 contiguous (and previously unallocated) bits remain
14407 within that selected 64 bit field. (As it turns out, for the
14408 example above, the compiler finds it is OK to allocate the
14409 "containing object" 64-bit field at bit-offset zero within the
14410 structure type.)
14412 Here we attempt to work backwards from the limited set of facts
14413 we're given, and we try to deduce from those facts, where GCC
14414 must have believed that the containing object started (within
14415 the structure type). The value we deduce is then used (by the
14416 callers of this routine) to generate DW_AT_location and
14417 DW_AT_bit_offset attributes for fields (both bit-fields and, in
14418 the case of DW_AT_location, regular fields as well). */
14420 /* Figure out the bit-distance from the start of the structure to
14421 the "deepest" bit of the bit-field. */
14422 deepest_bitpos = bitpos_int + field_size_in_bits;
14424 /* This is the tricky part. Use some fancy footwork to deduce
14425 where the lowest addressed bit of the containing object must
14426 be. */
14427 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14429 /* Round up to type_align by default. This works best for
14430 bitfields. */
14431 object_offset_in_bits
14432 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
14434 if (object_offset_in_bits.ugt (bitpos_int))
14436 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14438 /* Round up to decl_align instead. */
14439 object_offset_in_bits
14440 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
14443 else
14444 #endif /* PCC_BITFIELD_TYPE_MATTERS */
14445 object_offset_in_bits = bitpos_int;
14447 object_offset_in_bytes
14448 = object_offset_in_bits.div (double_int::from_uhwi (BITS_PER_UNIT),
14449 true, TRUNC_DIV_EXPR);
14450 return object_offset_in_bytes.to_shwi ();
14453 /* The following routines define various Dwarf attributes and any data
14454 associated with them. */
14456 /* Add a location description attribute value to a DIE.
14458 This emits location attributes suitable for whole variables and
14459 whole parameters. Note that the location attributes for struct fields are
14460 generated by the routine `data_member_location_attribute' below. */
14462 static inline void
14463 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
14464 dw_loc_list_ref descr)
14466 if (descr == 0)
14467 return;
14468 if (single_element_loc_list_p (descr))
14469 add_AT_loc (die, attr_kind, descr->expr);
14470 else
14471 add_AT_loc_list (die, attr_kind, descr);
14474 /* Add DW_AT_accessibility attribute to DIE if needed. */
14476 static void
14477 add_accessibility_attribute (dw_die_ref die, tree decl)
14479 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
14480 children, otherwise the default is DW_ACCESS_public. In DWARF2
14481 the default has always been DW_ACCESS_public. */
14482 if (TREE_PROTECTED (decl))
14483 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
14484 else if (TREE_PRIVATE (decl))
14486 if (dwarf_version == 2
14487 || die->die_parent == NULL
14488 || die->die_parent->die_tag != DW_TAG_class_type)
14489 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
14491 else if (dwarf_version > 2
14492 && die->die_parent
14493 && die->die_parent->die_tag == DW_TAG_class_type)
14494 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
14497 /* Attach the specialized form of location attribute used for data members of
14498 struct and union types. In the special case of a FIELD_DECL node which
14499 represents a bit-field, the "offset" part of this special location
14500 descriptor must indicate the distance in bytes from the lowest-addressed
14501 byte of the containing struct or union type to the lowest-addressed byte of
14502 the "containing object" for the bit-field. (See the `field_byte_offset'
14503 function above).
14505 For any given bit-field, the "containing object" is a hypothetical object
14506 (of some integral or enum type) within which the given bit-field lives. The
14507 type of this hypothetical "containing object" is always the same as the
14508 declared type of the individual bit-field itself (for GCC anyway... the
14509 DWARF spec doesn't actually mandate this). Note that it is the size (in
14510 bytes) of the hypothetical "containing object" which will be given in the
14511 DW_AT_byte_size attribute for this bit-field. (See the
14512 `byte_size_attribute' function below.) It is also used when calculating the
14513 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
14514 function below.) */
14516 static void
14517 add_data_member_location_attribute (dw_die_ref die, tree decl)
14519 HOST_WIDE_INT offset;
14520 dw_loc_descr_ref loc_descr = 0;
14522 if (TREE_CODE (decl) == TREE_BINFO)
14524 /* We're working on the TAG_inheritance for a base class. */
14525 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
14527 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
14528 aren't at a fixed offset from all (sub)objects of the same
14529 type. We need to extract the appropriate offset from our
14530 vtable. The following dwarf expression means
14532 BaseAddr = ObAddr + *((*ObAddr) - Offset)
14534 This is specific to the V3 ABI, of course. */
14536 dw_loc_descr_ref tmp;
14538 /* Make a copy of the object address. */
14539 tmp = new_loc_descr (DW_OP_dup, 0, 0);
14540 add_loc_descr (&loc_descr, tmp);
14542 /* Extract the vtable address. */
14543 tmp = new_loc_descr (DW_OP_deref, 0, 0);
14544 add_loc_descr (&loc_descr, tmp);
14546 /* Calculate the address of the offset. */
14547 offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
14548 gcc_assert (offset < 0);
14550 tmp = int_loc_descriptor (-offset);
14551 add_loc_descr (&loc_descr, tmp);
14552 tmp = new_loc_descr (DW_OP_minus, 0, 0);
14553 add_loc_descr (&loc_descr, tmp);
14555 /* Extract the offset. */
14556 tmp = new_loc_descr (DW_OP_deref, 0, 0);
14557 add_loc_descr (&loc_descr, tmp);
14559 /* Add it to the object address. */
14560 tmp = new_loc_descr (DW_OP_plus, 0, 0);
14561 add_loc_descr (&loc_descr, tmp);
14563 else
14564 offset = tree_low_cst (BINFO_OFFSET (decl), 0);
14566 else
14567 offset = field_byte_offset (decl);
14569 if (! loc_descr)
14571 if (dwarf_version > 2)
14573 /* Don't need to output a location expression, just the constant. */
14574 if (offset < 0)
14575 add_AT_int (die, DW_AT_data_member_location, offset);
14576 else
14577 add_AT_unsigned (die, DW_AT_data_member_location, offset);
14578 return;
14580 else
14582 enum dwarf_location_atom op;
14584 /* The DWARF2 standard says that we should assume that the structure
14585 address is already on the stack, so we can specify a structure
14586 field address by using DW_OP_plus_uconst. */
14587 op = DW_OP_plus_uconst;
14588 loc_descr = new_loc_descr (op, offset, 0);
14592 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
14595 /* Writes integer values to dw_vec_const array. */
14597 static void
14598 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
14600 while (size != 0)
14602 *dest++ = val & 0xff;
14603 val >>= 8;
14604 --size;
14608 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
14610 static HOST_WIDE_INT
14611 extract_int (const unsigned char *src, unsigned int size)
14613 HOST_WIDE_INT val = 0;
14615 src += size;
14616 while (size != 0)
14618 val <<= 8;
14619 val |= *--src & 0xff;
14620 --size;
14622 return val;
14625 /* Writes double_int values to dw_vec_const array. */
14627 static void
14628 insert_double (double_int val, unsigned char *dest)
14630 unsigned char *p0 = dest;
14631 unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
14633 if (WORDS_BIG_ENDIAN)
14635 p0 = p1;
14636 p1 = dest;
14639 insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
14640 insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
14643 /* Writes floating point values to dw_vec_const array. */
14645 static void
14646 insert_float (const_rtx rtl, unsigned char *array)
14648 REAL_VALUE_TYPE rv;
14649 long val[4];
14650 int i;
14652 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
14653 real_to_target (val, &rv, GET_MODE (rtl));
14655 /* real_to_target puts 32-bit pieces in each long. Pack them. */
14656 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
14658 insert_int (val[i], 4, array);
14659 array += 4;
14663 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
14664 does not have a "location" either in memory or in a register. These
14665 things can arise in GNU C when a constant is passed as an actual parameter
14666 to an inlined function. They can also arise in C++ where declared
14667 constants do not necessarily get memory "homes". */
14669 static bool
14670 add_const_value_attribute (dw_die_ref die, rtx rtl)
14672 switch (GET_CODE (rtl))
14674 case CONST_INT:
14676 HOST_WIDE_INT val = INTVAL (rtl);
14678 if (val < 0)
14679 add_AT_int (die, DW_AT_const_value, val);
14680 else
14681 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
14683 return true;
14685 case CONST_DOUBLE:
14686 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
14687 floating-point constant. A CONST_DOUBLE is used whenever the
14688 constant requires more than one word in order to be adequately
14689 represented. */
14691 enum machine_mode mode = GET_MODE (rtl);
14693 if (SCALAR_FLOAT_MODE_P (mode))
14695 unsigned int length = GET_MODE_SIZE (mode);
14696 unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
14698 insert_float (rtl, array);
14699 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
14701 else
14702 add_AT_double (die, DW_AT_const_value,
14703 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
14705 return true;
14707 case CONST_VECTOR:
14709 enum machine_mode mode = GET_MODE (rtl);
14710 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
14711 unsigned int length = CONST_VECTOR_NUNITS (rtl);
14712 unsigned char *array = (unsigned char *) ggc_alloc_atomic
14713 (length * elt_size);
14714 unsigned int i;
14715 unsigned char *p;
14717 switch (GET_MODE_CLASS (mode))
14719 case MODE_VECTOR_INT:
14720 for (i = 0, p = array; i < length; i++, p += elt_size)
14722 rtx elt = CONST_VECTOR_ELT (rtl, i);
14723 double_int val = rtx_to_double_int (elt);
14725 if (elt_size <= sizeof (HOST_WIDE_INT))
14726 insert_int (val.to_shwi (), elt_size, p);
14727 else
14729 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
14730 insert_double (val, p);
14733 break;
14735 case MODE_VECTOR_FLOAT:
14736 for (i = 0, p = array; i < length; i++, p += elt_size)
14738 rtx elt = CONST_VECTOR_ELT (rtl, i);
14739 insert_float (elt, p);
14741 break;
14743 default:
14744 gcc_unreachable ();
14747 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
14749 return true;
14751 case CONST_STRING:
14752 if (dwarf_version >= 4 || !dwarf_strict)
14754 dw_loc_descr_ref loc_result;
14755 resolve_one_addr (&rtl, NULL);
14756 rtl_addr:
14757 loc_result = new_addr_loc_descr (rtl, dtprel_false);
14758 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14759 add_AT_loc (die, DW_AT_location, loc_result);
14760 vec_safe_push (used_rtx_array, rtl);
14761 return true;
14763 return false;
14765 case CONST:
14766 if (CONSTANT_P (XEXP (rtl, 0)))
14767 return add_const_value_attribute (die, XEXP (rtl, 0));
14768 /* FALLTHROUGH */
14769 case SYMBOL_REF:
14770 if (!const_ok_for_output (rtl))
14771 return false;
14772 case LABEL_REF:
14773 if (dwarf_version >= 4 || !dwarf_strict)
14774 goto rtl_addr;
14775 return false;
14777 case PLUS:
14778 /* In cases where an inlined instance of an inline function is passed
14779 the address of an `auto' variable (which is local to the caller) we
14780 can get a situation where the DECL_RTL of the artificial local
14781 variable (for the inlining) which acts as a stand-in for the
14782 corresponding formal parameter (of the inline function) will look
14783 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
14784 exactly a compile-time constant expression, but it isn't the address
14785 of the (artificial) local variable either. Rather, it represents the
14786 *value* which the artificial local variable always has during its
14787 lifetime. We currently have no way to represent such quasi-constant
14788 values in Dwarf, so for now we just punt and generate nothing. */
14789 return false;
14791 case HIGH:
14792 case CONST_FIXED:
14793 return false;
14795 case MEM:
14796 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
14797 && MEM_READONLY_P (rtl)
14798 && GET_MODE (rtl) == BLKmode)
14800 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
14801 return true;
14803 return false;
14805 default:
14806 /* No other kinds of rtx should be possible here. */
14807 gcc_unreachable ();
14809 return false;
14812 /* Determine whether the evaluation of EXPR references any variables
14813 or functions which aren't otherwise used (and therefore may not be
14814 output). */
14815 static tree
14816 reference_to_unused (tree * tp, int * walk_subtrees,
14817 void * data ATTRIBUTE_UNUSED)
14819 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
14820 *walk_subtrees = 0;
14822 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
14823 && ! TREE_ASM_WRITTEN (*tp))
14824 return *tp;
14825 /* ??? The C++ FE emits debug information for using decls, so
14826 putting gcc_unreachable here falls over. See PR31899. For now
14827 be conservative. */
14828 else if (!cgraph_global_info_ready
14829 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
14830 return *tp;
14831 else if (TREE_CODE (*tp) == VAR_DECL)
14833 struct varpool_node *node = varpool_get_node (*tp);
14834 if (!node || !node->analyzed)
14835 return *tp;
14837 else if (TREE_CODE (*tp) == FUNCTION_DECL
14838 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
14840 /* The call graph machinery must have finished analyzing,
14841 optimizing and gimplifying the CU by now.
14842 So if *TP has no call graph node associated
14843 to it, it means *TP will not be emitted. */
14844 if (!cgraph_get_node (*tp))
14845 return *tp;
14847 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
14848 return *tp;
14850 return NULL_TREE;
14853 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
14854 for use in a later add_const_value_attribute call. */
14856 static rtx
14857 rtl_for_decl_init (tree init, tree type)
14859 rtx rtl = NULL_RTX;
14861 STRIP_NOPS (init);
14863 /* If a variable is initialized with a string constant without embedded
14864 zeros, build CONST_STRING. */
14865 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
14867 tree enttype = TREE_TYPE (type);
14868 tree domain = TYPE_DOMAIN (type);
14869 enum machine_mode mode = TYPE_MODE (enttype);
14871 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
14872 && domain
14873 && integer_zerop (TYPE_MIN_VALUE (domain))
14874 && compare_tree_int (TYPE_MAX_VALUE (domain),
14875 TREE_STRING_LENGTH (init) - 1) == 0
14876 && ((size_t) TREE_STRING_LENGTH (init)
14877 == strlen (TREE_STRING_POINTER (init)) + 1))
14879 rtl = gen_rtx_CONST_STRING (VOIDmode,
14880 ggc_strdup (TREE_STRING_POINTER (init)));
14881 rtl = gen_rtx_MEM (BLKmode, rtl);
14882 MEM_READONLY_P (rtl) = 1;
14885 /* Other aggregates, and complex values, could be represented using
14886 CONCAT: FIXME! */
14887 else if (AGGREGATE_TYPE_P (type)
14888 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
14889 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
14890 || TREE_CODE (type) == COMPLEX_TYPE)
14892 /* Vectors only work if their mode is supported by the target.
14893 FIXME: generic vectors ought to work too. */
14894 else if (TREE_CODE (type) == VECTOR_TYPE
14895 && !VECTOR_MODE_P (TYPE_MODE (type)))
14897 /* If the initializer is something that we know will expand into an
14898 immediate RTL constant, expand it now. We must be careful not to
14899 reference variables which won't be output. */
14900 else if (initializer_constant_valid_p (init, type)
14901 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
14903 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
14904 possible. */
14905 if (TREE_CODE (type) == VECTOR_TYPE)
14906 switch (TREE_CODE (init))
14908 case VECTOR_CST:
14909 break;
14910 case CONSTRUCTOR:
14911 if (TREE_CONSTANT (init))
14913 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
14914 bool constant_p = true;
14915 tree value;
14916 unsigned HOST_WIDE_INT ix;
14918 /* Even when ctor is constant, it might contain non-*_CST
14919 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
14920 belong into VECTOR_CST nodes. */
14921 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
14922 if (!CONSTANT_CLASS_P (value))
14924 constant_p = false;
14925 break;
14928 if (constant_p)
14930 init = build_vector_from_ctor (type, elts);
14931 break;
14934 /* FALLTHRU */
14936 default:
14937 return NULL;
14940 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
14942 /* If expand_expr returns a MEM, it wasn't immediate. */
14943 gcc_assert (!rtl || !MEM_P (rtl));
14946 return rtl;
14949 /* Generate RTL for the variable DECL to represent its location. */
14951 static rtx
14952 rtl_for_decl_location (tree decl)
14954 rtx rtl;
14956 /* Here we have to decide where we are going to say the parameter "lives"
14957 (as far as the debugger is concerned). We only have a couple of
14958 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
14960 DECL_RTL normally indicates where the parameter lives during most of the
14961 activation of the function. If optimization is enabled however, this
14962 could be either NULL or else a pseudo-reg. Both of those cases indicate
14963 that the parameter doesn't really live anywhere (as far as the code
14964 generation parts of GCC are concerned) during most of the function's
14965 activation. That will happen (for example) if the parameter is never
14966 referenced within the function.
14968 We could just generate a location descriptor here for all non-NULL
14969 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
14970 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
14971 where DECL_RTL is NULL or is a pseudo-reg.
14973 Note however that we can only get away with using DECL_INCOMING_RTL as
14974 a backup substitute for DECL_RTL in certain limited cases. In cases
14975 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
14976 we can be sure that the parameter was passed using the same type as it is
14977 declared to have within the function, and that its DECL_INCOMING_RTL
14978 points us to a place where a value of that type is passed.
14980 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
14981 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
14982 because in these cases DECL_INCOMING_RTL points us to a value of some
14983 type which is *different* from the type of the parameter itself. Thus,
14984 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
14985 such cases, the debugger would end up (for example) trying to fetch a
14986 `float' from a place which actually contains the first part of a
14987 `double'. That would lead to really incorrect and confusing
14988 output at debug-time.
14990 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
14991 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
14992 are a couple of exceptions however. On little-endian machines we can
14993 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
14994 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
14995 an integral type that is smaller than TREE_TYPE (decl). These cases arise
14996 when (on a little-endian machine) a non-prototyped function has a
14997 parameter declared to be of type `short' or `char'. In such cases,
14998 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
14999 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15000 passed `int' value. If the debugger then uses that address to fetch
15001 a `short' or a `char' (on a little-endian machine) the result will be
15002 the correct data, so we allow for such exceptional cases below.
15004 Note that our goal here is to describe the place where the given formal
15005 parameter lives during most of the function's activation (i.e. between the
15006 end of the prologue and the start of the epilogue). We'll do that as best
15007 as we can. Note however that if the given formal parameter is modified
15008 sometime during the execution of the function, then a stack backtrace (at
15009 debug-time) will show the function as having been called with the *new*
15010 value rather than the value which was originally passed in. This happens
15011 rarely enough that it is not a major problem, but it *is* a problem, and
15012 I'd like to fix it.
15014 A future version of dwarf2out.c may generate two additional attributes for
15015 any given DW_TAG_formal_parameter DIE which will describe the "passed
15016 type" and the "passed location" for the given formal parameter in addition
15017 to the attributes we now generate to indicate the "declared type" and the
15018 "active location" for each parameter. This additional set of attributes
15019 could be used by debuggers for stack backtraces. Separately, note that
15020 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15021 This happens (for example) for inlined-instances of inline function formal
15022 parameters which are never referenced. This really shouldn't be
15023 happening. All PARM_DECL nodes should get valid non-NULL
15024 DECL_INCOMING_RTL values. FIXME. */
15026 /* Use DECL_RTL as the "location" unless we find something better. */
15027 rtl = DECL_RTL_IF_SET (decl);
15029 /* When generating abstract instances, ignore everything except
15030 constants, symbols living in memory, and symbols living in
15031 fixed registers. */
15032 if (! reload_completed)
15034 if (rtl
15035 && (CONSTANT_P (rtl)
15036 || (MEM_P (rtl)
15037 && CONSTANT_P (XEXP (rtl, 0)))
15038 || (REG_P (rtl)
15039 && TREE_CODE (decl) == VAR_DECL
15040 && TREE_STATIC (decl))))
15042 rtl = targetm.delegitimize_address (rtl);
15043 return rtl;
15045 rtl = NULL_RTX;
15047 else if (TREE_CODE (decl) == PARM_DECL)
15049 if (rtl == NULL_RTX
15050 || is_pseudo_reg (rtl)
15051 || (MEM_P (rtl)
15052 && is_pseudo_reg (XEXP (rtl, 0))
15053 && DECL_INCOMING_RTL (decl)
15054 && MEM_P (DECL_INCOMING_RTL (decl))
15055 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15057 tree declared_type = TREE_TYPE (decl);
15058 tree passed_type = DECL_ARG_TYPE (decl);
15059 enum machine_mode dmode = TYPE_MODE (declared_type);
15060 enum machine_mode pmode = TYPE_MODE (passed_type);
15062 /* This decl represents a formal parameter which was optimized out.
15063 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15064 all cases where (rtl == NULL_RTX) just below. */
15065 if (dmode == pmode)
15066 rtl = DECL_INCOMING_RTL (decl);
15067 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15068 && SCALAR_INT_MODE_P (dmode)
15069 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15070 && DECL_INCOMING_RTL (decl))
15072 rtx inc = DECL_INCOMING_RTL (decl);
15073 if (REG_P (inc))
15074 rtl = inc;
15075 else if (MEM_P (inc))
15077 if (BYTES_BIG_ENDIAN)
15078 rtl = adjust_address_nv (inc, dmode,
15079 GET_MODE_SIZE (pmode)
15080 - GET_MODE_SIZE (dmode));
15081 else
15082 rtl = inc;
15087 /* If the parm was passed in registers, but lives on the stack, then
15088 make a big endian correction if the mode of the type of the
15089 parameter is not the same as the mode of the rtl. */
15090 /* ??? This is the same series of checks that are made in dbxout.c before
15091 we reach the big endian correction code there. It isn't clear if all
15092 of these checks are necessary here, but keeping them all is the safe
15093 thing to do. */
15094 else if (MEM_P (rtl)
15095 && XEXP (rtl, 0) != const0_rtx
15096 && ! CONSTANT_P (XEXP (rtl, 0))
15097 /* Not passed in memory. */
15098 && !MEM_P (DECL_INCOMING_RTL (decl))
15099 /* Not passed by invisible reference. */
15100 && (!REG_P (XEXP (rtl, 0))
15101 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15102 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15103 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15104 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15105 #endif
15107 /* Big endian correction check. */
15108 && BYTES_BIG_ENDIAN
15109 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15110 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15111 < UNITS_PER_WORD))
15113 enum machine_mode addr_mode = get_address_mode (rtl);
15114 int offset = (UNITS_PER_WORD
15115 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15117 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15118 plus_constant (addr_mode, XEXP (rtl, 0), offset));
15121 else if (TREE_CODE (decl) == VAR_DECL
15122 && rtl
15123 && MEM_P (rtl)
15124 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
15125 && BYTES_BIG_ENDIAN)
15127 enum machine_mode addr_mode = get_address_mode (rtl);
15128 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
15129 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
15131 /* If a variable is declared "register" yet is smaller than
15132 a register, then if we store the variable to memory, it
15133 looks like we're storing a register-sized value, when in
15134 fact we are not. We need to adjust the offset of the
15135 storage location to reflect the actual value's bytes,
15136 else gdb will not be able to display it. */
15137 if (rsize > dsize)
15138 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15139 plus_constant (addr_mode, XEXP (rtl, 0),
15140 rsize - dsize));
15143 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
15144 and will have been substituted directly into all expressions that use it.
15145 C does not have such a concept, but C++ and other languages do. */
15146 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
15147 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
15149 if (rtl)
15150 rtl = targetm.delegitimize_address (rtl);
15152 /* If we don't look past the constant pool, we risk emitting a
15153 reference to a constant pool entry that isn't referenced from
15154 code, and thus is not emitted. */
15155 if (rtl)
15156 rtl = avoid_constant_pool_reference (rtl);
15158 /* Try harder to get a rtl. If this symbol ends up not being emitted
15159 in the current CU, resolve_addr will remove the expression referencing
15160 it. */
15161 if (rtl == NULL_RTX
15162 && TREE_CODE (decl) == VAR_DECL
15163 && !DECL_EXTERNAL (decl)
15164 && TREE_STATIC (decl)
15165 && DECL_NAME (decl)
15166 && !DECL_HARD_REGISTER (decl)
15167 && DECL_MODE (decl) != VOIDmode)
15169 rtl = make_decl_rtl_for_debug (decl);
15170 if (!MEM_P (rtl)
15171 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
15172 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
15173 rtl = NULL_RTX;
15176 return rtl;
15179 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
15180 returned. If so, the decl for the COMMON block is returned, and the
15181 value is the offset into the common block for the symbol. */
15183 static tree
15184 fortran_common (tree decl, HOST_WIDE_INT *value)
15186 tree val_expr, cvar;
15187 enum machine_mode mode;
15188 HOST_WIDE_INT bitsize, bitpos;
15189 tree offset;
15190 int unsignedp, volatilep = 0;
15192 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
15193 it does not have a value (the offset into the common area), or if it
15194 is thread local (as opposed to global) then it isn't common, and shouldn't
15195 be handled as such. */
15196 if (TREE_CODE (decl) != VAR_DECL
15197 || !TREE_STATIC (decl)
15198 || !DECL_HAS_VALUE_EXPR_P (decl)
15199 || !is_fortran ())
15200 return NULL_TREE;
15202 val_expr = DECL_VALUE_EXPR (decl);
15203 if (TREE_CODE (val_expr) != COMPONENT_REF)
15204 return NULL_TREE;
15206 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
15207 &mode, &unsignedp, &volatilep, true);
15209 if (cvar == NULL_TREE
15210 || TREE_CODE (cvar) != VAR_DECL
15211 || DECL_ARTIFICIAL (cvar)
15212 || !TREE_PUBLIC (cvar))
15213 return NULL_TREE;
15215 *value = 0;
15216 if (offset != NULL)
15218 if (!host_integerp (offset, 0))
15219 return NULL_TREE;
15220 *value = tree_low_cst (offset, 0);
15222 if (bitpos != 0)
15223 *value += bitpos / BITS_PER_UNIT;
15225 return cvar;
15228 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
15229 data attribute for a variable or a parameter. We generate the
15230 DW_AT_const_value attribute only in those cases where the given variable
15231 or parameter does not have a true "location" either in memory or in a
15232 register. This can happen (for example) when a constant is passed as an
15233 actual argument in a call to an inline function. (It's possible that
15234 these things can crop up in other ways also.) Note that one type of
15235 constant value which can be passed into an inlined function is a constant
15236 pointer. This can happen for example if an actual argument in an inlined
15237 function call evaluates to a compile-time constant address.
15239 CACHE_P is true if it is worth caching the location list for DECL,
15240 so that future calls can reuse it rather than regenerate it from scratch.
15241 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
15242 since we will need to refer to them each time the function is inlined. */
15244 static bool
15245 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
15246 enum dwarf_attribute attr)
15248 rtx rtl;
15249 dw_loc_list_ref list;
15250 var_loc_list *loc_list;
15251 cached_dw_loc_list *cache;
15252 void **slot;
15254 if (TREE_CODE (decl) == ERROR_MARK)
15255 return false;
15257 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
15258 || TREE_CODE (decl) == RESULT_DECL);
15260 /* Try to get some constant RTL for this decl, and use that as the value of
15261 the location. */
15263 rtl = rtl_for_decl_location (decl);
15264 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15265 && add_const_value_attribute (die, rtl))
15266 return true;
15268 /* See if we have single element location list that is equivalent to
15269 a constant value. That way we are better to use add_const_value_attribute
15270 rather than expanding constant value equivalent. */
15271 loc_list = lookup_decl_loc (decl);
15272 if (loc_list
15273 && loc_list->first
15274 && loc_list->first->next == NULL
15275 && NOTE_P (loc_list->first->loc)
15276 && NOTE_VAR_LOCATION (loc_list->first->loc)
15277 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
15279 struct var_loc_node *node;
15281 node = loc_list->first;
15282 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
15283 if (GET_CODE (rtl) == EXPR_LIST)
15284 rtl = XEXP (rtl, 0);
15285 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15286 && add_const_value_attribute (die, rtl))
15287 return true;
15289 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
15290 list several times. See if we've already cached the contents. */
15291 list = NULL;
15292 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
15293 cache_p = false;
15294 if (cache_p)
15296 cache = (cached_dw_loc_list *)
15297 htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
15298 if (cache)
15299 list = cache->loc_list;
15301 if (list == NULL)
15303 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
15304 /* It is usually worth caching this result if the decl is from
15305 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
15306 if (cache_p && list && list->dw_loc_next)
15308 slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
15309 DECL_UID (decl), INSERT);
15310 cache = ggc_alloc_cleared_cached_dw_loc_list ();
15311 cache->decl_id = DECL_UID (decl);
15312 cache->loc_list = list;
15313 *slot = cache;
15316 if (list)
15318 add_AT_location_description (die, attr, list);
15319 return true;
15321 /* None of that worked, so it must not really have a location;
15322 try adding a constant value attribute from the DECL_INITIAL. */
15323 return tree_add_const_value_attribute_for_decl (die, decl);
15326 /* Add VARIABLE and DIE into deferred locations list. */
15328 static void
15329 defer_location (tree variable, dw_die_ref die)
15331 deferred_locations entry;
15332 entry.variable = variable;
15333 entry.die = die;
15334 vec_safe_push (deferred_locations_list, entry);
15337 /* Helper function for tree_add_const_value_attribute. Natively encode
15338 initializer INIT into an array. Return true if successful. */
15340 static bool
15341 native_encode_initializer (tree init, unsigned char *array, int size)
15343 tree type;
15345 if (init == NULL_TREE)
15346 return false;
15348 STRIP_NOPS (init);
15349 switch (TREE_CODE (init))
15351 case STRING_CST:
15352 type = TREE_TYPE (init);
15353 if (TREE_CODE (type) == ARRAY_TYPE)
15355 tree enttype = TREE_TYPE (type);
15356 enum machine_mode mode = TYPE_MODE (enttype);
15358 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
15359 return false;
15360 if (int_size_in_bytes (type) != size)
15361 return false;
15362 if (size > TREE_STRING_LENGTH (init))
15364 memcpy (array, TREE_STRING_POINTER (init),
15365 TREE_STRING_LENGTH (init));
15366 memset (array + TREE_STRING_LENGTH (init),
15367 '\0', size - TREE_STRING_LENGTH (init));
15369 else
15370 memcpy (array, TREE_STRING_POINTER (init), size);
15371 return true;
15373 return false;
15374 case CONSTRUCTOR:
15375 type = TREE_TYPE (init);
15376 if (int_size_in_bytes (type) != size)
15377 return false;
15378 if (TREE_CODE (type) == ARRAY_TYPE)
15380 HOST_WIDE_INT min_index;
15381 unsigned HOST_WIDE_INT cnt;
15382 int curpos = 0, fieldsize;
15383 constructor_elt *ce;
15385 if (TYPE_DOMAIN (type) == NULL_TREE
15386 || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
15387 return false;
15389 fieldsize = int_size_in_bytes (TREE_TYPE (type));
15390 if (fieldsize <= 0)
15391 return false;
15393 min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
15394 memset (array, '\0', size);
15395 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15397 tree val = ce->value;
15398 tree index = ce->index;
15399 int pos = curpos;
15400 if (index && TREE_CODE (index) == RANGE_EXPR)
15401 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
15402 * fieldsize;
15403 else if (index)
15404 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
15406 if (val)
15408 STRIP_NOPS (val);
15409 if (!native_encode_initializer (val, array + pos, fieldsize))
15410 return false;
15412 curpos = pos + fieldsize;
15413 if (index && TREE_CODE (index) == RANGE_EXPR)
15415 int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
15416 - tree_low_cst (TREE_OPERAND (index, 0), 0);
15417 while (count-- > 0)
15419 if (val)
15420 memcpy (array + curpos, array + pos, fieldsize);
15421 curpos += fieldsize;
15424 gcc_assert (curpos <= size);
15426 return true;
15428 else if (TREE_CODE (type) == RECORD_TYPE
15429 || TREE_CODE (type) == UNION_TYPE)
15431 tree field = NULL_TREE;
15432 unsigned HOST_WIDE_INT cnt;
15433 constructor_elt *ce;
15435 if (int_size_in_bytes (type) != size)
15436 return false;
15438 if (TREE_CODE (type) == RECORD_TYPE)
15439 field = TYPE_FIELDS (type);
15441 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15443 tree val = ce->value;
15444 int pos, fieldsize;
15446 if (ce->index != 0)
15447 field = ce->index;
15449 if (val)
15450 STRIP_NOPS (val);
15452 if (field == NULL_TREE || DECL_BIT_FIELD (field))
15453 return false;
15455 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
15456 && TYPE_DOMAIN (TREE_TYPE (field))
15457 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
15458 return false;
15459 else if (DECL_SIZE_UNIT (field) == NULL_TREE
15460 || !host_integerp (DECL_SIZE_UNIT (field), 0))
15461 return false;
15462 fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
15463 pos = int_byte_position (field);
15464 gcc_assert (pos + fieldsize <= size);
15465 if (val
15466 && !native_encode_initializer (val, array + pos, fieldsize))
15467 return false;
15469 return true;
15471 return false;
15472 case VIEW_CONVERT_EXPR:
15473 case NON_LVALUE_EXPR:
15474 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
15475 default:
15476 return native_encode_expr (init, array, size) == size;
15480 /* Attach a DW_AT_const_value attribute to DIE. The value of the
15481 attribute is the const value T. */
15483 static bool
15484 tree_add_const_value_attribute (dw_die_ref die, tree t)
15486 tree init;
15487 tree type = TREE_TYPE (t);
15488 rtx rtl;
15490 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
15491 return false;
15493 init = t;
15494 gcc_assert (!DECL_P (init));
15496 rtl = rtl_for_decl_init (init, type);
15497 if (rtl)
15498 return add_const_value_attribute (die, rtl);
15499 /* If the host and target are sane, try harder. */
15500 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
15501 && initializer_constant_valid_p (init, type))
15503 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
15504 if (size > 0 && (int) size == size)
15506 unsigned char *array = (unsigned char *)
15507 ggc_alloc_cleared_atomic (size);
15509 if (native_encode_initializer (init, array, size))
15511 add_AT_vec (die, DW_AT_const_value, size, 1, array);
15512 return true;
15516 return false;
15519 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
15520 attribute is the const value of T, where T is an integral constant
15521 variable with static storage duration
15522 (so it can't be a PARM_DECL or a RESULT_DECL). */
15524 static bool
15525 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
15528 if (!decl
15529 || (TREE_CODE (decl) != VAR_DECL
15530 && TREE_CODE (decl) != CONST_DECL)
15531 || (TREE_CODE (decl) == VAR_DECL
15532 && !TREE_STATIC (decl)))
15533 return false;
15535 if (TREE_READONLY (decl)
15536 && ! TREE_THIS_VOLATILE (decl)
15537 && DECL_INITIAL (decl))
15538 /* OK */;
15539 else
15540 return false;
15542 /* Don't add DW_AT_const_value if abstract origin already has one. */
15543 if (get_AT (var_die, DW_AT_const_value))
15544 return false;
15546 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
15549 /* Convert the CFI instructions for the current function into a
15550 location list. This is used for DW_AT_frame_base when we targeting
15551 a dwarf2 consumer that does not support the dwarf3
15552 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
15553 expressions. */
15555 static dw_loc_list_ref
15556 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
15558 int ix;
15559 dw_fde_ref fde;
15560 dw_loc_list_ref list, *list_tail;
15561 dw_cfi_ref cfi;
15562 dw_cfa_location last_cfa, next_cfa;
15563 const char *start_label, *last_label, *section;
15564 dw_cfa_location remember;
15566 fde = cfun->fde;
15567 gcc_assert (fde != NULL);
15569 section = secname_for_decl (current_function_decl);
15570 list_tail = &list;
15571 list = NULL;
15573 memset (&next_cfa, 0, sizeof (next_cfa));
15574 next_cfa.reg = INVALID_REGNUM;
15575 remember = next_cfa;
15577 start_label = fde->dw_fde_begin;
15579 /* ??? Bald assumption that the CIE opcode list does not contain
15580 advance opcodes. */
15581 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
15582 lookup_cfa_1 (cfi, &next_cfa, &remember);
15584 last_cfa = next_cfa;
15585 last_label = start_label;
15587 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
15589 /* If the first partition contained no CFI adjustments, the
15590 CIE opcodes apply to the whole first partition. */
15591 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15592 fde->dw_fde_begin, fde->dw_fde_end, section);
15593 list_tail =&(*list_tail)->dw_loc_next;
15594 start_label = last_label = fde->dw_fde_second_begin;
15597 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
15599 switch (cfi->dw_cfi_opc)
15601 case DW_CFA_set_loc:
15602 case DW_CFA_advance_loc1:
15603 case DW_CFA_advance_loc2:
15604 case DW_CFA_advance_loc4:
15605 if (!cfa_equal_p (&last_cfa, &next_cfa))
15607 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15608 start_label, last_label, section);
15610 list_tail = &(*list_tail)->dw_loc_next;
15611 last_cfa = next_cfa;
15612 start_label = last_label;
15614 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
15615 break;
15617 case DW_CFA_advance_loc:
15618 /* The encoding is complex enough that we should never emit this. */
15619 gcc_unreachable ();
15621 default:
15622 lookup_cfa_1 (cfi, &next_cfa, &remember);
15623 break;
15625 if (ix + 1 == fde->dw_fde_switch_cfi_index)
15627 if (!cfa_equal_p (&last_cfa, &next_cfa))
15629 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15630 start_label, last_label, section);
15632 list_tail = &(*list_tail)->dw_loc_next;
15633 last_cfa = next_cfa;
15634 start_label = last_label;
15636 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15637 start_label, fde->dw_fde_end, section);
15638 list_tail = &(*list_tail)->dw_loc_next;
15639 start_label = last_label = fde->dw_fde_second_begin;
15643 if (!cfa_equal_p (&last_cfa, &next_cfa))
15645 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15646 start_label, last_label, section);
15647 list_tail = &(*list_tail)->dw_loc_next;
15648 start_label = last_label;
15651 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
15652 start_label,
15653 fde->dw_fde_second_begin
15654 ? fde->dw_fde_second_end : fde->dw_fde_end,
15655 section);
15657 if (list && list->dw_loc_next)
15658 gen_llsym (list);
15660 return list;
15663 /* Compute a displacement from the "steady-state frame pointer" to the
15664 frame base (often the same as the CFA), and store it in
15665 frame_pointer_fb_offset. OFFSET is added to the displacement
15666 before the latter is negated. */
15668 static void
15669 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
15671 rtx reg, elim;
15673 #ifdef FRAME_POINTER_CFA_OFFSET
15674 reg = frame_pointer_rtx;
15675 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
15676 #else
15677 reg = arg_pointer_rtx;
15678 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
15679 #endif
15681 elim = (ira_use_lra_p
15682 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
15683 : eliminate_regs (reg, VOIDmode, NULL_RTX));
15684 if (GET_CODE (elim) == PLUS)
15686 offset += INTVAL (XEXP (elim, 1));
15687 elim = XEXP (elim, 0);
15690 frame_pointer_fb_offset = -offset;
15692 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
15693 in which to eliminate. This is because it's stack pointer isn't
15694 directly accessible as a register within the ISA. To work around
15695 this, assume that while we cannot provide a proper value for
15696 frame_pointer_fb_offset, we won't need one either. */
15697 frame_pointer_fb_offset_valid
15698 = ((SUPPORTS_STACK_ALIGNMENT
15699 && (elim == hard_frame_pointer_rtx
15700 || elim == stack_pointer_rtx))
15701 || elim == (frame_pointer_needed
15702 ? hard_frame_pointer_rtx
15703 : stack_pointer_rtx));
15706 /* Generate a DW_AT_name attribute given some string value to be included as
15707 the value of the attribute. */
15709 static void
15710 add_name_attribute (dw_die_ref die, const char *name_string)
15712 if (name_string != NULL && *name_string != 0)
15714 if (demangle_name_func)
15715 name_string = (*demangle_name_func) (name_string);
15717 add_AT_string (die, DW_AT_name, name_string);
15721 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
15722 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
15723 of TYPE accordingly.
15725 ??? This is a temporary measure until after we're able to generate
15726 regular DWARF for the complex Ada type system. */
15728 static void
15729 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
15730 dw_die_ref context_die)
15732 tree dtype;
15733 dw_die_ref dtype_die;
15735 if (!lang_hooks.types.descriptive_type)
15736 return;
15738 dtype = lang_hooks.types.descriptive_type (type);
15739 if (!dtype)
15740 return;
15742 dtype_die = lookup_type_die (dtype);
15743 if (!dtype_die)
15745 gen_type_die (dtype, context_die);
15746 dtype_die = lookup_type_die (dtype);
15747 gcc_assert (dtype_die);
15750 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
15753 /* Generate a DW_AT_comp_dir attribute for DIE. */
15755 static void
15756 add_comp_dir_attribute (dw_die_ref die)
15758 const char *wd = get_src_pwd ();
15759 char *wd1;
15761 if (wd == NULL)
15762 return;
15764 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
15766 int wdlen;
15768 wdlen = strlen (wd);
15769 wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
15770 strcpy (wd1, wd);
15771 wd1 [wdlen] = DIR_SEPARATOR;
15772 wd1 [wdlen + 1] = 0;
15773 wd = wd1;
15776 add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
15779 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
15780 default. */
15782 static int
15783 lower_bound_default (void)
15785 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
15787 case DW_LANG_C:
15788 case DW_LANG_C89:
15789 case DW_LANG_C99:
15790 case DW_LANG_C_plus_plus:
15791 case DW_LANG_ObjC:
15792 case DW_LANG_ObjC_plus_plus:
15793 case DW_LANG_Java:
15794 return 0;
15795 case DW_LANG_Fortran77:
15796 case DW_LANG_Fortran90:
15797 case DW_LANG_Fortran95:
15798 return 1;
15799 case DW_LANG_UPC:
15800 case DW_LANG_D:
15801 case DW_LANG_Python:
15802 return dwarf_version >= 4 ? 0 : -1;
15803 case DW_LANG_Ada95:
15804 case DW_LANG_Ada83:
15805 case DW_LANG_Cobol74:
15806 case DW_LANG_Cobol85:
15807 case DW_LANG_Pascal83:
15808 case DW_LANG_Modula2:
15809 case DW_LANG_PLI:
15810 return dwarf_version >= 4 ? 1 : -1;
15811 default:
15812 return -1;
15816 /* Given a tree node describing an array bound (either lower or upper) output
15817 a representation for that bound. */
15819 static void
15820 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
15822 switch (TREE_CODE (bound))
15824 case ERROR_MARK:
15825 return;
15827 /* All fixed-bounds are represented by INTEGER_CST nodes. */
15828 case INTEGER_CST:
15830 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
15831 int dflt;
15833 /* Use the default if possible. */
15834 if (bound_attr == DW_AT_lower_bound
15835 && host_integerp (bound, 0)
15836 && (dflt = lower_bound_default ()) != -1
15837 && tree_low_cst (bound, 0) == dflt)
15840 /* Otherwise represent the bound as an unsigned value with the
15841 precision of its type. The precision and signedness of the
15842 type will be necessary to re-interpret it unambiguously. */
15843 else if (prec < HOST_BITS_PER_WIDE_INT)
15845 unsigned HOST_WIDE_INT mask
15846 = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
15847 add_AT_unsigned (subrange_die, bound_attr,
15848 TREE_INT_CST_LOW (bound) & mask);
15850 else if (prec == HOST_BITS_PER_WIDE_INT
15851 || TREE_INT_CST_HIGH (bound) == 0)
15852 add_AT_unsigned (subrange_die, bound_attr,
15853 TREE_INT_CST_LOW (bound));
15854 else
15855 add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
15856 TREE_INT_CST_LOW (bound));
15858 break;
15860 CASE_CONVERT:
15861 case VIEW_CONVERT_EXPR:
15862 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
15863 break;
15865 case SAVE_EXPR:
15866 break;
15868 case VAR_DECL:
15869 case PARM_DECL:
15870 case RESULT_DECL:
15872 dw_die_ref decl_die = lookup_decl_die (bound);
15874 /* ??? Can this happen, or should the variable have been bound
15875 first? Probably it can, since I imagine that we try to create
15876 the types of parameters in the order in which they exist in
15877 the list, and won't have created a forward reference to a
15878 later parameter. */
15879 if (decl_die != NULL)
15881 add_AT_die_ref (subrange_die, bound_attr, decl_die);
15882 break;
15885 /* FALLTHRU */
15887 default:
15889 /* Otherwise try to create a stack operation procedure to
15890 evaluate the value of the array bound. */
15892 dw_die_ref ctx, decl_die;
15893 dw_loc_list_ref list;
15895 list = loc_list_from_tree (bound, 2);
15896 if (list == NULL || single_element_loc_list_p (list))
15898 /* If DW_AT_*bound is not a reference nor constant, it is
15899 a DWARF expression rather than location description.
15900 For that loc_list_from_tree (bound, 0) is needed.
15901 If that fails to give a single element list,
15902 fall back to outputting this as a reference anyway. */
15903 dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
15904 if (list2 && single_element_loc_list_p (list2))
15906 add_AT_loc (subrange_die, bound_attr, list2->expr);
15907 break;
15910 if (list == NULL)
15911 break;
15913 if (current_function_decl == 0)
15914 ctx = comp_unit_die ();
15915 else
15916 ctx = lookup_decl_die (current_function_decl);
15918 decl_die = new_die (DW_TAG_variable, ctx, bound);
15919 add_AT_flag (decl_die, DW_AT_artificial, 1);
15920 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
15921 add_AT_location_description (decl_die, DW_AT_location, list);
15922 add_AT_die_ref (subrange_die, bound_attr, decl_die);
15923 break;
15928 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
15929 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
15930 Note that the block of subscript information for an array type also
15931 includes information about the element type of the given array type. */
15933 static void
15934 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
15936 unsigned dimension_number;
15937 tree lower, upper;
15938 dw_die_ref subrange_die;
15940 for (dimension_number = 0;
15941 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
15942 type = TREE_TYPE (type), dimension_number++)
15944 tree domain = TYPE_DOMAIN (type);
15946 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
15947 break;
15949 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
15950 and (in GNU C only) variable bounds. Handle all three forms
15951 here. */
15952 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
15953 if (domain)
15955 /* We have an array type with specified bounds. */
15956 lower = TYPE_MIN_VALUE (domain);
15957 upper = TYPE_MAX_VALUE (domain);
15959 /* Define the index type. */
15960 if (TREE_TYPE (domain))
15962 /* ??? This is probably an Ada unnamed subrange type. Ignore the
15963 TREE_TYPE field. We can't emit debug info for this
15964 because it is an unnamed integral type. */
15965 if (TREE_CODE (domain) == INTEGER_TYPE
15966 && TYPE_NAME (domain) == NULL_TREE
15967 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
15968 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
15970 else
15971 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
15972 type_die);
15975 /* ??? If upper is NULL, the array has unspecified length,
15976 but it does have a lower bound. This happens with Fortran
15977 dimension arr(N:*)
15978 Since the debugger is definitely going to need to know N
15979 to produce useful results, go ahead and output the lower
15980 bound solo, and hope the debugger can cope. */
15982 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
15983 if (upper)
15984 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
15987 /* Otherwise we have an array type with an unspecified length. The
15988 DWARF-2 spec does not say how to handle this; let's just leave out the
15989 bounds. */
15993 static void
15994 add_byte_size_attribute (dw_die_ref die, tree tree_node)
15996 dw_die_ref decl_die;
15997 unsigned size;
15999 switch (TREE_CODE (tree_node))
16001 case ERROR_MARK:
16002 size = 0;
16003 break;
16004 case ENUMERAL_TYPE:
16005 case RECORD_TYPE:
16006 case UNION_TYPE:
16007 case QUAL_UNION_TYPE:
16008 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
16009 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
16011 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
16012 return;
16014 size = int_size_in_bytes (tree_node);
16015 break;
16016 case FIELD_DECL:
16017 /* For a data member of a struct or union, the DW_AT_byte_size is
16018 generally given as the number of bytes normally allocated for an
16019 object of the *declared* type of the member itself. This is true
16020 even for bit-fields. */
16021 size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
16022 break;
16023 default:
16024 gcc_unreachable ();
16027 /* Note that `size' might be -1 when we get to this point. If it is, that
16028 indicates that the byte size of the entity in question is variable. We
16029 have no good way of expressing this fact in Dwarf at the present time,
16030 so just let the -1 pass on through. */
16031 add_AT_unsigned (die, DW_AT_byte_size, size);
16034 /* For a FIELD_DECL node which represents a bit-field, output an attribute
16035 which specifies the distance in bits from the highest order bit of the
16036 "containing object" for the bit-field to the highest order bit of the
16037 bit-field itself.
16039 For any given bit-field, the "containing object" is a hypothetical object
16040 (of some integral or enum type) within which the given bit-field lives. The
16041 type of this hypothetical "containing object" is always the same as the
16042 declared type of the individual bit-field itself. The determination of the
16043 exact location of the "containing object" for a bit-field is rather
16044 complicated. It's handled by the `field_byte_offset' function (above).
16046 Note that it is the size (in bytes) of the hypothetical "containing object"
16047 which will be given in the DW_AT_byte_size attribute for this bit-field.
16048 (See `byte_size_attribute' above). */
16050 static inline void
16051 add_bit_offset_attribute (dw_die_ref die, tree decl)
16053 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
16054 tree type = DECL_BIT_FIELD_TYPE (decl);
16055 HOST_WIDE_INT bitpos_int;
16056 HOST_WIDE_INT highest_order_object_bit_offset;
16057 HOST_WIDE_INT highest_order_field_bit_offset;
16058 HOST_WIDE_INT bit_offset;
16060 /* Must be a field and a bit field. */
16061 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
16063 /* We can't yet handle bit-fields whose offsets are variable, so if we
16064 encounter such things, just return without generating any attribute
16065 whatsoever. Likewise for variable or too large size. */
16066 if (! host_integerp (bit_position (decl), 0)
16067 || ! host_integerp (DECL_SIZE (decl), 1))
16068 return;
16070 bitpos_int = int_bit_position (decl);
16072 /* Note that the bit offset is always the distance (in bits) from the
16073 highest-order bit of the "containing object" to the highest-order bit of
16074 the bit-field itself. Since the "high-order end" of any object or field
16075 is different on big-endian and little-endian machines, the computation
16076 below must take account of these differences. */
16077 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
16078 highest_order_field_bit_offset = bitpos_int;
16080 if (! BYTES_BIG_ENDIAN)
16082 highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
16083 highest_order_object_bit_offset += simple_type_size_in_bits (type);
16086 bit_offset
16087 = (! BYTES_BIG_ENDIAN
16088 ? highest_order_object_bit_offset - highest_order_field_bit_offset
16089 : highest_order_field_bit_offset - highest_order_object_bit_offset);
16091 if (bit_offset < 0)
16092 add_AT_int (die, DW_AT_bit_offset, bit_offset);
16093 else
16094 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
16097 /* For a FIELD_DECL node which represents a bit field, output an attribute
16098 which specifies the length in bits of the given field. */
16100 static inline void
16101 add_bit_size_attribute (dw_die_ref die, tree decl)
16103 /* Must be a field and a bit field. */
16104 gcc_assert (TREE_CODE (decl) == FIELD_DECL
16105 && DECL_BIT_FIELD_TYPE (decl));
16107 if (host_integerp (DECL_SIZE (decl), 1))
16108 add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
16111 /* If the compiled language is ANSI C, then add a 'prototyped'
16112 attribute, if arg types are given for the parameters of a function. */
16114 static inline void
16115 add_prototyped_attribute (dw_die_ref die, tree func_type)
16117 if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
16118 && prototype_p (func_type))
16119 add_AT_flag (die, DW_AT_prototyped, 1);
16122 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
16123 by looking in either the type declaration or object declaration
16124 equate table. */
16126 static inline dw_die_ref
16127 add_abstract_origin_attribute (dw_die_ref die, tree origin)
16129 dw_die_ref origin_die = NULL;
16131 if (TREE_CODE (origin) != FUNCTION_DECL)
16133 /* We may have gotten separated from the block for the inlined
16134 function, if we're in an exception handler or some such; make
16135 sure that the abstract function has been written out.
16137 Doing this for nested functions is wrong, however; functions are
16138 distinct units, and our context might not even be inline. */
16139 tree fn = origin;
16141 if (TYPE_P (fn))
16142 fn = TYPE_STUB_DECL (fn);
16144 fn = decl_function_context (fn);
16145 if (fn)
16146 dwarf2out_abstract_function (fn);
16149 if (DECL_P (origin))
16150 origin_die = lookup_decl_die (origin);
16151 else if (TYPE_P (origin))
16152 origin_die = lookup_type_die (origin);
16154 /* XXX: Functions that are never lowered don't always have correct block
16155 trees (in the case of java, they simply have no block tree, in some other
16156 languages). For these functions, there is nothing we can really do to
16157 output correct debug info for inlined functions in all cases. Rather
16158 than die, we'll just produce deficient debug info now, in that we will
16159 have variables without a proper abstract origin. In the future, when all
16160 functions are lowered, we should re-add a gcc_assert (origin_die)
16161 here. */
16163 if (origin_die)
16164 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
16165 return origin_die;
16168 /* We do not currently support the pure_virtual attribute. */
16170 static inline void
16171 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
16173 if (DECL_VINDEX (func_decl))
16175 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
16177 if (host_integerp (DECL_VINDEX (func_decl), 0))
16178 add_AT_loc (die, DW_AT_vtable_elem_location,
16179 new_loc_descr (DW_OP_constu,
16180 tree_low_cst (DECL_VINDEX (func_decl), 0),
16181 0));
16183 /* GNU extension: Record what type this method came from originally. */
16184 if (debug_info_level > DINFO_LEVEL_TERSE
16185 && DECL_CONTEXT (func_decl))
16186 add_AT_die_ref (die, DW_AT_containing_type,
16187 lookup_type_die (DECL_CONTEXT (func_decl)));
16191 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
16192 given decl. This used to be a vendor extension until after DWARF 4
16193 standardized it. */
16195 static void
16196 add_linkage_attr (dw_die_ref die, tree decl)
16198 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
16200 /* Mimic what assemble_name_raw does with a leading '*'. */
16201 if (name[0] == '*')
16202 name = &name[1];
16204 if (dwarf_version >= 4)
16205 add_AT_string (die, DW_AT_linkage_name, name);
16206 else
16207 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
16210 /* Add source coordinate attributes for the given decl. */
16212 static void
16213 add_src_coords_attributes (dw_die_ref die, tree decl)
16215 expanded_location s;
16217 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
16218 return;
16219 s = expand_location (DECL_SOURCE_LOCATION (decl));
16220 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
16221 add_AT_unsigned (die, DW_AT_decl_line, s.line);
16224 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
16226 static void
16227 add_linkage_name (dw_die_ref die, tree decl)
16229 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
16230 && TREE_PUBLIC (decl)
16231 && !DECL_ABSTRACT (decl)
16232 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
16233 && die->die_tag != DW_TAG_member)
16235 /* Defer until we have an assembler name set. */
16236 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
16238 limbo_die_node *asm_name;
16240 asm_name = ggc_alloc_cleared_limbo_die_node ();
16241 asm_name->die = die;
16242 asm_name->created_for = decl;
16243 asm_name->next = deferred_asm_name;
16244 deferred_asm_name = asm_name;
16246 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
16247 add_linkage_attr (die, decl);
16251 /* Add a DW_AT_name attribute and source coordinate attribute for the
16252 given decl, but only if it actually has a name. */
16254 static void
16255 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
16257 tree decl_name;
16259 decl_name = DECL_NAME (decl);
16260 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
16262 const char *name = dwarf2_name (decl, 0);
16263 if (name)
16264 add_name_attribute (die, name);
16265 if (! DECL_ARTIFICIAL (decl))
16266 add_src_coords_attributes (die, decl);
16268 add_linkage_name (die, decl);
16271 #ifdef VMS_DEBUGGING_INFO
16272 /* Get the function's name, as described by its RTL. This may be different
16273 from the DECL_NAME name used in the source file. */
16274 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
16276 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
16277 XEXP (DECL_RTL (decl), 0), false);
16278 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
16280 #endif /* VMS_DEBUGGING_INFO */
16283 #ifdef VMS_DEBUGGING_INFO
16284 /* Output the debug main pointer die for VMS */
16286 void
16287 dwarf2out_vms_debug_main_pointer (void)
16289 char label[MAX_ARTIFICIAL_LABEL_BYTES];
16290 dw_die_ref die;
16292 /* Allocate the VMS debug main subprogram die. */
16293 die = ggc_alloc_cleared_die_node ();
16294 die->die_tag = DW_TAG_subprogram;
16295 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
16296 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
16297 current_function_funcdef_no);
16298 add_AT_lbl_id (die, DW_AT_entry_pc, label);
16300 /* Make it the first child of comp_unit_die (). */
16301 die->die_parent = comp_unit_die ();
16302 if (comp_unit_die ()->die_child)
16304 die->die_sib = comp_unit_die ()->die_child->die_sib;
16305 comp_unit_die ()->die_child->die_sib = die;
16307 else
16309 die->die_sib = die;
16310 comp_unit_die ()->die_child = die;
16313 #endif /* VMS_DEBUGGING_INFO */
16315 /* Push a new declaration scope. */
16317 static void
16318 push_decl_scope (tree scope)
16320 vec_safe_push (decl_scope_table, scope);
16323 /* Pop a declaration scope. */
16325 static inline void
16326 pop_decl_scope (void)
16328 decl_scope_table->pop ();
16331 /* walk_tree helper function for uses_local_type, below. */
16333 static tree
16334 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
16336 if (!TYPE_P (*tp))
16337 *walk_subtrees = 0;
16338 else
16340 tree name = TYPE_NAME (*tp);
16341 if (name && DECL_P (name) && decl_function_context (name))
16342 return *tp;
16344 return NULL_TREE;
16347 /* If TYPE involves a function-local type (including a local typedef to a
16348 non-local type), returns that type; otherwise returns NULL_TREE. */
16350 static tree
16351 uses_local_type (tree type)
16353 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
16354 return used;
16357 /* Return the DIE for the scope that immediately contains this type.
16358 Non-named types that do not involve a function-local type get global
16359 scope. Named types nested in namespaces or other types get their
16360 containing scope. All other types (i.e. function-local named types) get
16361 the current active scope. */
16363 static dw_die_ref
16364 scope_die_for (tree t, dw_die_ref context_die)
16366 dw_die_ref scope_die = NULL;
16367 tree containing_scope;
16369 /* Non-types always go in the current scope. */
16370 gcc_assert (TYPE_P (t));
16372 /* Use the scope of the typedef, rather than the scope of the type
16373 it refers to. */
16374 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
16375 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
16376 else
16377 containing_scope = TYPE_CONTEXT (t);
16379 /* Use the containing namespace if there is one. */
16380 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
16382 if (context_die == lookup_decl_die (containing_scope))
16383 /* OK */;
16384 else if (debug_info_level > DINFO_LEVEL_TERSE)
16385 context_die = get_context_die (containing_scope);
16386 else
16387 containing_scope = NULL_TREE;
16390 /* Ignore function type "scopes" from the C frontend. They mean that
16391 a tagged type is local to a parmlist of a function declarator, but
16392 that isn't useful to DWARF. */
16393 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
16394 containing_scope = NULL_TREE;
16396 if (SCOPE_FILE_SCOPE_P (containing_scope))
16398 /* If T uses a local type keep it local as well, to avoid references
16399 to function-local DIEs from outside the function. */
16400 if (current_function_decl && uses_local_type (t))
16401 scope_die = context_die;
16402 else
16403 scope_die = comp_unit_die ();
16405 else if (TYPE_P (containing_scope))
16407 /* For types, we can just look up the appropriate DIE. */
16408 if (debug_info_level > DINFO_LEVEL_TERSE)
16409 scope_die = get_context_die (containing_scope);
16410 else
16412 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
16413 if (scope_die == NULL)
16414 scope_die = comp_unit_die ();
16417 else
16418 scope_die = context_die;
16420 return scope_die;
16423 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
16425 static inline int
16426 local_scope_p (dw_die_ref context_die)
16428 for (; context_die; context_die = context_die->die_parent)
16429 if (context_die->die_tag == DW_TAG_inlined_subroutine
16430 || context_die->die_tag == DW_TAG_subprogram)
16431 return 1;
16433 return 0;
16436 /* Returns nonzero if CONTEXT_DIE is a class. */
16438 static inline int
16439 class_scope_p (dw_die_ref context_die)
16441 return (context_die
16442 && (context_die->die_tag == DW_TAG_structure_type
16443 || context_die->die_tag == DW_TAG_class_type
16444 || context_die->die_tag == DW_TAG_interface_type
16445 || context_die->die_tag == DW_TAG_union_type));
16448 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
16449 whether or not to treat a DIE in this context as a declaration. */
16451 static inline int
16452 class_or_namespace_scope_p (dw_die_ref context_die)
16454 return (class_scope_p (context_die)
16455 || (context_die && context_die->die_tag == DW_TAG_namespace));
16458 /* Many forms of DIEs require a "type description" attribute. This
16459 routine locates the proper "type descriptor" die for the type given
16460 by 'type', and adds a DW_AT_type attribute below the given die. */
16462 static void
16463 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
16464 int decl_volatile, dw_die_ref context_die)
16466 enum tree_code code = TREE_CODE (type);
16467 dw_die_ref type_die = NULL;
16469 /* ??? If this type is an unnamed subrange type of an integral, floating-point
16470 or fixed-point type, use the inner type. This is because we have no
16471 support for unnamed types in base_type_die. This can happen if this is
16472 an Ada subrange type. Correct solution is emit a subrange type die. */
16473 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
16474 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
16475 type = TREE_TYPE (type), code = TREE_CODE (type);
16477 if (code == ERROR_MARK
16478 /* Handle a special case. For functions whose return type is void, we
16479 generate *no* type attribute. (Note that no object may have type
16480 `void', so this only applies to function return types). */
16481 || code == VOID_TYPE)
16482 return;
16484 type_die = modified_type_die (type,
16485 decl_const || TYPE_READONLY (type),
16486 decl_volatile || TYPE_VOLATILE (type),
16487 context_die);
16489 if (type_die != NULL)
16490 add_AT_die_ref (object_die, DW_AT_type, type_die);
16493 /* Given an object die, add the calling convention attribute for the
16494 function call type. */
16495 static void
16496 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
16498 enum dwarf_calling_convention value = DW_CC_normal;
16500 value = ((enum dwarf_calling_convention)
16501 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
16503 if (is_fortran ()
16504 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
16506 /* DWARF 2 doesn't provide a way to identify a program's source-level
16507 entry point. DW_AT_calling_convention attributes are only meant
16508 to describe functions' calling conventions. However, lacking a
16509 better way to signal the Fortran main program, we used this for
16510 a long time, following existing custom. Now, DWARF 4 has
16511 DW_AT_main_subprogram, which we add below, but some tools still
16512 rely on the old way, which we thus keep. */
16513 value = DW_CC_program;
16515 if (dwarf_version >= 4 || !dwarf_strict)
16516 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
16519 /* Only add the attribute if the backend requests it, and
16520 is not DW_CC_normal. */
16521 if (value && (value != DW_CC_normal))
16522 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
16525 /* Given a tree pointer to a struct, class, union, or enum type node, return
16526 a pointer to the (string) tag name for the given type, or zero if the type
16527 was declared without a tag. */
16529 static const char *
16530 type_tag (const_tree type)
16532 const char *name = 0;
16534 if (TYPE_NAME (type) != 0)
16536 tree t = 0;
16538 /* Find the IDENTIFIER_NODE for the type name. */
16539 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
16540 && !TYPE_NAMELESS (type))
16541 t = TYPE_NAME (type);
16543 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
16544 a TYPE_DECL node, regardless of whether or not a `typedef' was
16545 involved. */
16546 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
16547 && ! DECL_IGNORED_P (TYPE_NAME (type)))
16549 /* We want to be extra verbose. Don't call dwarf_name if
16550 DECL_NAME isn't set. The default hook for decl_printable_name
16551 doesn't like that, and in this context it's correct to return
16552 0, instead of "<anonymous>" or the like. */
16553 if (DECL_NAME (TYPE_NAME (type))
16554 && !DECL_NAMELESS (TYPE_NAME (type)))
16555 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
16558 /* Now get the name as a string, or invent one. */
16559 if (!name && t != 0)
16560 name = IDENTIFIER_POINTER (t);
16563 return (name == 0 || *name == '\0') ? 0 : name;
16566 /* Return the type associated with a data member, make a special check
16567 for bit field types. */
16569 static inline tree
16570 member_declared_type (const_tree member)
16572 return (DECL_BIT_FIELD_TYPE (member)
16573 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
16576 /* Get the decl's label, as described by its RTL. This may be different
16577 from the DECL_NAME name used in the source file. */
16579 #if 0
16580 static const char *
16581 decl_start_label (tree decl)
16583 rtx x;
16584 const char *fnname;
16586 x = DECL_RTL (decl);
16587 gcc_assert (MEM_P (x));
16589 x = XEXP (x, 0);
16590 gcc_assert (GET_CODE (x) == SYMBOL_REF);
16592 fnname = XSTR (x, 0);
16593 return fnname;
16595 #endif
16597 /* These routines generate the internal representation of the DIE's for
16598 the compilation unit. Debugging information is collected by walking
16599 the declaration trees passed in from dwarf2out_decl(). */
16601 static void
16602 gen_array_type_die (tree type, dw_die_ref context_die)
16604 dw_die_ref scope_die = scope_die_for (type, context_die);
16605 dw_die_ref array_die;
16607 /* GNU compilers represent multidimensional array types as sequences of one
16608 dimensional array types whose element types are themselves array types.
16609 We sometimes squish that down to a single array_type DIE with multiple
16610 subscripts in the Dwarf debugging info. The draft Dwarf specification
16611 say that we are allowed to do this kind of compression in C, because
16612 there is no difference between an array of arrays and a multidimensional
16613 array. We don't do this for Ada to remain as close as possible to the
16614 actual representation, which is especially important against the language
16615 flexibilty wrt arrays of variable size. */
16617 bool collapse_nested_arrays = !is_ada ();
16618 tree element_type;
16620 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
16621 DW_TAG_string_type doesn't have DW_AT_type attribute). */
16622 if (TYPE_STRING_FLAG (type)
16623 && TREE_CODE (type) == ARRAY_TYPE
16624 && is_fortran ()
16625 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
16627 HOST_WIDE_INT size;
16629 array_die = new_die (DW_TAG_string_type, scope_die, type);
16630 add_name_attribute (array_die, type_tag (type));
16631 equate_type_number_to_die (type, array_die);
16632 size = int_size_in_bytes (type);
16633 if (size >= 0)
16634 add_AT_unsigned (array_die, DW_AT_byte_size, size);
16635 else if (TYPE_DOMAIN (type) != NULL_TREE
16636 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
16637 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
16639 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
16640 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
16642 size = int_size_in_bytes (TREE_TYPE (szdecl));
16643 if (loc && size > 0)
16645 add_AT_location_description (array_die, DW_AT_string_length, loc);
16646 if (size != DWARF2_ADDR_SIZE)
16647 add_AT_unsigned (array_die, DW_AT_byte_size, size);
16650 return;
16653 array_die = new_die (DW_TAG_array_type, scope_die, type);
16654 add_name_attribute (array_die, type_tag (type));
16655 equate_type_number_to_die (type, array_die);
16657 if (TREE_CODE (type) == VECTOR_TYPE)
16658 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
16660 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
16661 if (is_fortran ()
16662 && TREE_CODE (type) == ARRAY_TYPE
16663 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
16664 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
16665 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16667 #if 0
16668 /* We default the array ordering. SDB will probably do
16669 the right things even if DW_AT_ordering is not present. It's not even
16670 an issue until we start to get into multidimensional arrays anyway. If
16671 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
16672 then we'll have to put the DW_AT_ordering attribute back in. (But if
16673 and when we find out that we need to put these in, we will only do so
16674 for multidimensional arrays. */
16675 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
16676 #endif
16678 if (TREE_CODE (type) == VECTOR_TYPE)
16680 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
16681 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
16682 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
16683 add_bound_info (subrange_die, DW_AT_upper_bound,
16684 size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
16686 else
16687 add_subscript_info (array_die, type, collapse_nested_arrays);
16689 /* Add representation of the type of the elements of this array type and
16690 emit the corresponding DIE if we haven't done it already. */
16691 element_type = TREE_TYPE (type);
16692 if (collapse_nested_arrays)
16693 while (TREE_CODE (element_type) == ARRAY_TYPE)
16695 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
16696 break;
16697 element_type = TREE_TYPE (element_type);
16700 add_type_attribute (array_die, element_type, 0, 0, context_die);
16702 add_gnat_descriptive_type_attribute (array_die, type, context_die);
16703 if (TYPE_ARTIFICIAL (type))
16704 add_AT_flag (array_die, DW_AT_artificial, 1);
16706 if (get_AT (array_die, DW_AT_name))
16707 add_pubtype (type, array_die);
16710 static dw_loc_descr_ref
16711 descr_info_loc (tree val, tree base_decl)
16713 HOST_WIDE_INT size;
16714 dw_loc_descr_ref loc, loc2;
16715 enum dwarf_location_atom op;
16717 if (val == base_decl)
16718 return new_loc_descr (DW_OP_push_object_address, 0, 0);
16720 switch (TREE_CODE (val))
16722 CASE_CONVERT:
16723 return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16724 case VAR_DECL:
16725 return loc_descriptor_from_tree (val, 0);
16726 case INTEGER_CST:
16727 if (host_integerp (val, 0))
16728 return int_loc_descriptor (tree_low_cst (val, 0));
16729 break;
16730 case INDIRECT_REF:
16731 size = int_size_in_bytes (TREE_TYPE (val));
16732 if (size < 0)
16733 break;
16734 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16735 if (!loc)
16736 break;
16737 if (size == DWARF2_ADDR_SIZE)
16738 add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
16739 else
16740 add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
16741 return loc;
16742 case POINTER_PLUS_EXPR:
16743 case PLUS_EXPR:
16744 if (host_integerp (TREE_OPERAND (val, 1), 1)
16745 && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
16746 < 16384)
16748 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16749 if (!loc)
16750 break;
16751 loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
16753 else
16755 op = DW_OP_plus;
16756 do_binop:
16757 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16758 if (!loc)
16759 break;
16760 loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
16761 if (!loc2)
16762 break;
16763 add_loc_descr (&loc, loc2);
16764 add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
16766 return loc;
16767 case MINUS_EXPR:
16768 op = DW_OP_minus;
16769 goto do_binop;
16770 case MULT_EXPR:
16771 op = DW_OP_mul;
16772 goto do_binop;
16773 case EQ_EXPR:
16774 op = DW_OP_eq;
16775 goto do_binop;
16776 case NE_EXPR:
16777 op = DW_OP_ne;
16778 goto do_binop;
16779 default:
16780 break;
16782 return NULL;
16785 static void
16786 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
16787 tree val, tree base_decl)
16789 dw_loc_descr_ref loc;
16791 if (host_integerp (val, 0))
16793 add_AT_unsigned (die, attr, tree_low_cst (val, 0));
16794 return;
16797 loc = descr_info_loc (val, base_decl);
16798 if (!loc)
16799 return;
16801 add_AT_loc (die, attr, loc);
16804 /* This routine generates DIE for array with hidden descriptor, details
16805 are filled into *info by a langhook. */
16807 static void
16808 gen_descr_array_type_die (tree type, struct array_descr_info *info,
16809 dw_die_ref context_die)
16811 dw_die_ref scope_die = scope_die_for (type, context_die);
16812 dw_die_ref array_die;
16813 int dim;
16815 array_die = new_die (DW_TAG_array_type, scope_die, type);
16816 add_name_attribute (array_die, type_tag (type));
16817 equate_type_number_to_die (type, array_die);
16819 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
16820 if (is_fortran ()
16821 && info->ndimensions >= 2)
16822 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16824 if (info->data_location)
16825 add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
16826 info->base_decl);
16827 if (info->associated)
16828 add_descr_info_field (array_die, DW_AT_associated, info->associated,
16829 info->base_decl);
16830 if (info->allocated)
16831 add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
16832 info->base_decl);
16834 for (dim = 0; dim < info->ndimensions; dim++)
16836 dw_die_ref subrange_die
16837 = new_die (DW_TAG_subrange_type, array_die, NULL);
16839 if (info->dimen[dim].lower_bound)
16841 /* If it is the default value, omit it. */
16842 int dflt;
16844 if (host_integerp (info->dimen[dim].lower_bound, 0)
16845 && (dflt = lower_bound_default ()) != -1
16846 && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
16848 else
16849 add_descr_info_field (subrange_die, DW_AT_lower_bound,
16850 info->dimen[dim].lower_bound,
16851 info->base_decl);
16853 if (info->dimen[dim].upper_bound)
16854 add_descr_info_field (subrange_die, DW_AT_upper_bound,
16855 info->dimen[dim].upper_bound,
16856 info->base_decl);
16857 if (info->dimen[dim].stride)
16858 add_descr_info_field (subrange_die, DW_AT_byte_stride,
16859 info->dimen[dim].stride,
16860 info->base_decl);
16863 gen_type_die (info->element_type, context_die);
16864 add_type_attribute (array_die, info->element_type, 0, 0, context_die);
16866 if (get_AT (array_die, DW_AT_name))
16867 add_pubtype (type, array_die);
16870 #if 0
16871 static void
16872 gen_entry_point_die (tree decl, dw_die_ref context_die)
16874 tree origin = decl_ultimate_origin (decl);
16875 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
16877 if (origin != NULL)
16878 add_abstract_origin_attribute (decl_die, origin);
16879 else
16881 add_name_and_src_coords_attributes (decl_die, decl);
16882 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
16883 0, 0, context_die);
16886 if (DECL_ABSTRACT (decl))
16887 equate_decl_number_to_die (decl, decl_die);
16888 else
16889 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
16891 #endif
16893 /* Walk through the list of incomplete types again, trying once more to
16894 emit full debugging info for them. */
16896 static void
16897 retry_incomplete_types (void)
16899 int i;
16901 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
16902 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
16903 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
16906 /* Determine what tag to use for a record type. */
16908 static enum dwarf_tag
16909 record_type_tag (tree type)
16911 if (! lang_hooks.types.classify_record)
16912 return DW_TAG_structure_type;
16914 switch (lang_hooks.types.classify_record (type))
16916 case RECORD_IS_STRUCT:
16917 return DW_TAG_structure_type;
16919 case RECORD_IS_CLASS:
16920 return DW_TAG_class_type;
16922 case RECORD_IS_INTERFACE:
16923 if (dwarf_version >= 3 || !dwarf_strict)
16924 return DW_TAG_interface_type;
16925 return DW_TAG_structure_type;
16927 default:
16928 gcc_unreachable ();
16932 /* Generate a DIE to represent an enumeration type. Note that these DIEs
16933 include all of the information about the enumeration values also. Each
16934 enumerated type name/value is listed as a child of the enumerated type
16935 DIE. */
16937 static dw_die_ref
16938 gen_enumeration_type_die (tree type, dw_die_ref context_die)
16940 dw_die_ref type_die = lookup_type_die (type);
16942 if (type_die == NULL)
16944 type_die = new_die (DW_TAG_enumeration_type,
16945 scope_die_for (type, context_die), type);
16946 equate_type_number_to_die (type, type_die);
16947 add_name_attribute (type_die, type_tag (type));
16948 if (dwarf_version >= 4 || !dwarf_strict)
16950 if (ENUM_IS_SCOPED (type))
16951 add_AT_flag (type_die, DW_AT_enum_class, 1);
16952 if (ENUM_IS_OPAQUE (type))
16953 add_AT_flag (type_die, DW_AT_declaration, 1);
16956 else if (! TYPE_SIZE (type))
16957 return type_die;
16958 else
16959 remove_AT (type_die, DW_AT_declaration);
16961 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
16962 given enum type is incomplete, do not generate the DW_AT_byte_size
16963 attribute or the DW_AT_element_list attribute. */
16964 if (TYPE_SIZE (type))
16966 tree link;
16968 TREE_ASM_WRITTEN (type) = 1;
16969 add_byte_size_attribute (type_die, type);
16970 if (TYPE_STUB_DECL (type) != NULL_TREE)
16972 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
16973 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
16976 /* If the first reference to this type was as the return type of an
16977 inline function, then it may not have a parent. Fix this now. */
16978 if (type_die->die_parent == NULL)
16979 add_child_die (scope_die_for (type, context_die), type_die);
16981 for (link = TYPE_VALUES (type);
16982 link != NULL; link = TREE_CHAIN (link))
16984 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
16985 tree value = TREE_VALUE (link);
16987 add_name_attribute (enum_die,
16988 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
16990 if (TREE_CODE (value) == CONST_DECL)
16991 value = DECL_INITIAL (value);
16993 if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
16994 /* DWARF2 does not provide a way of indicating whether or
16995 not enumeration constants are signed or unsigned. GDB
16996 always assumes the values are signed, so we output all
16997 values as if they were signed. That means that
16998 enumeration constants with very large unsigned values
16999 will appear to have negative values in the debugger. */
17000 add_AT_int (enum_die, DW_AT_const_value,
17001 tree_low_cst (value, tree_int_cst_sgn (value) > 0));
17004 add_gnat_descriptive_type_attribute (type_die, type, context_die);
17005 if (TYPE_ARTIFICIAL (type))
17006 add_AT_flag (type_die, DW_AT_artificial, 1);
17008 else
17009 add_AT_flag (type_die, DW_AT_declaration, 1);
17011 add_pubtype (type, type_die);
17013 return type_die;
17016 /* Generate a DIE to represent either a real live formal parameter decl or to
17017 represent just the type of some formal parameter position in some function
17018 type.
17020 Note that this routine is a bit unusual because its argument may be a
17021 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17022 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17023 node. If it's the former then this function is being called to output a
17024 DIE to represent a formal parameter object (or some inlining thereof). If
17025 it's the latter, then this function is only being called to output a
17026 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17027 argument type of some subprogram type.
17028 If EMIT_NAME_P is true, name and source coordinate attributes
17029 are emitted. */
17031 static dw_die_ref
17032 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17033 dw_die_ref context_die)
17035 tree node_or_origin = node ? node : origin;
17036 tree ultimate_origin;
17037 dw_die_ref parm_die
17038 = new_die (DW_TAG_formal_parameter, context_die, node);
17040 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
17042 case tcc_declaration:
17043 ultimate_origin = decl_ultimate_origin (node_or_origin);
17044 if (node || ultimate_origin)
17045 origin = ultimate_origin;
17046 if (origin != NULL)
17047 add_abstract_origin_attribute (parm_die, origin);
17048 else if (emit_name_p)
17049 add_name_and_src_coords_attributes (parm_die, node);
17050 if (origin == NULL
17051 || (! DECL_ABSTRACT (node_or_origin)
17052 && variably_modified_type_p (TREE_TYPE (node_or_origin),
17053 decl_function_context
17054 (node_or_origin))))
17056 tree type = TREE_TYPE (node_or_origin);
17057 if (decl_by_reference_p (node_or_origin))
17058 add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
17059 context_die);
17060 else
17061 add_type_attribute (parm_die, type,
17062 TREE_READONLY (node_or_origin),
17063 TREE_THIS_VOLATILE (node_or_origin),
17064 context_die);
17066 if (origin == NULL && DECL_ARTIFICIAL (node))
17067 add_AT_flag (parm_die, DW_AT_artificial, 1);
17069 if (node && node != origin)
17070 equate_decl_number_to_die (node, parm_die);
17071 if (! DECL_ABSTRACT (node_or_origin))
17072 add_location_or_const_value_attribute (parm_die, node_or_origin,
17073 node == NULL, DW_AT_location);
17075 break;
17077 case tcc_type:
17078 /* We were called with some kind of a ..._TYPE node. */
17079 add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
17080 break;
17082 default:
17083 gcc_unreachable ();
17086 return parm_die;
17089 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
17090 children DW_TAG_formal_parameter DIEs representing the arguments of the
17091 parameter pack.
17093 PARM_PACK must be a function parameter pack.
17094 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
17095 must point to the subsequent arguments of the function PACK_ARG belongs to.
17096 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
17097 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
17098 following the last one for which a DIE was generated. */
17100 static dw_die_ref
17101 gen_formal_parameter_pack_die (tree parm_pack,
17102 tree pack_arg,
17103 dw_die_ref subr_die,
17104 tree *next_arg)
17106 tree arg;
17107 dw_die_ref parm_pack_die;
17109 gcc_assert (parm_pack
17110 && lang_hooks.function_parameter_pack_p (parm_pack)
17111 && subr_die);
17113 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
17114 add_src_coords_attributes (parm_pack_die, parm_pack);
17116 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
17118 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
17119 parm_pack))
17120 break;
17121 gen_formal_parameter_die (arg, NULL,
17122 false /* Don't emit name attribute. */,
17123 parm_pack_die);
17125 if (next_arg)
17126 *next_arg = arg;
17127 return parm_pack_die;
17130 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
17131 at the end of an (ANSI prototyped) formal parameters list. */
17133 static void
17134 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
17136 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
17139 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
17140 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
17141 parameters as specified in some function type specification (except for
17142 those which appear as part of a function *definition*). */
17144 static void
17145 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
17147 tree link;
17148 tree formal_type = NULL;
17149 tree first_parm_type;
17150 tree arg;
17152 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
17154 arg = DECL_ARGUMENTS (function_or_method_type);
17155 function_or_method_type = TREE_TYPE (function_or_method_type);
17157 else
17158 arg = NULL_TREE;
17160 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
17162 /* Make our first pass over the list of formal parameter types and output a
17163 DW_TAG_formal_parameter DIE for each one. */
17164 for (link = first_parm_type; link; )
17166 dw_die_ref parm_die;
17168 formal_type = TREE_VALUE (link);
17169 if (formal_type == void_type_node)
17170 break;
17172 /* Output a (nameless) DIE to represent the formal parameter itself. */
17173 parm_die = gen_formal_parameter_die (formal_type, NULL,
17174 true /* Emit name attribute. */,
17175 context_die);
17176 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
17177 && link == first_parm_type)
17179 add_AT_flag (parm_die, DW_AT_artificial, 1);
17180 if (dwarf_version >= 3 || !dwarf_strict)
17181 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
17183 else if (arg && DECL_ARTIFICIAL (arg))
17184 add_AT_flag (parm_die, DW_AT_artificial, 1);
17186 link = TREE_CHAIN (link);
17187 if (arg)
17188 arg = DECL_CHAIN (arg);
17191 /* If this function type has an ellipsis, add a
17192 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
17193 if (formal_type != void_type_node)
17194 gen_unspecified_parameters_die (function_or_method_type, context_die);
17196 /* Make our second (and final) pass over the list of formal parameter types
17197 and output DIEs to represent those types (as necessary). */
17198 for (link = TYPE_ARG_TYPES (function_or_method_type);
17199 link && TREE_VALUE (link);
17200 link = TREE_CHAIN (link))
17201 gen_type_die (TREE_VALUE (link), context_die);
17204 /* We want to generate the DIE for TYPE so that we can generate the
17205 die for MEMBER, which has been defined; we will need to refer back
17206 to the member declaration nested within TYPE. If we're trying to
17207 generate minimal debug info for TYPE, processing TYPE won't do the
17208 trick; we need to attach the member declaration by hand. */
17210 static void
17211 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
17213 gen_type_die (type, context_die);
17215 /* If we're trying to avoid duplicate debug info, we may not have
17216 emitted the member decl for this function. Emit it now. */
17217 if (TYPE_STUB_DECL (type)
17218 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
17219 && ! lookup_decl_die (member))
17221 dw_die_ref type_die;
17222 gcc_assert (!decl_ultimate_origin (member));
17224 push_decl_scope (type);
17225 type_die = lookup_type_die_strip_naming_typedef (type);
17226 if (TREE_CODE (member) == FUNCTION_DECL)
17227 gen_subprogram_die (member, type_die);
17228 else if (TREE_CODE (member) == FIELD_DECL)
17230 /* Ignore the nameless fields that are used to skip bits but handle
17231 C++ anonymous unions and structs. */
17232 if (DECL_NAME (member) != NULL_TREE
17233 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
17234 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
17236 gen_type_die (member_declared_type (member), type_die);
17237 gen_field_die (member, type_die);
17240 else
17241 gen_variable_die (member, NULL_TREE, type_die);
17243 pop_decl_scope ();
17247 /* Forward declare these functions, because they are mutually recursive
17248 with their set_block_* pairing functions. */
17249 static void set_decl_origin_self (tree);
17250 static void set_decl_abstract_flags (tree, int);
17252 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
17253 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
17254 that it points to the node itself, thus indicating that the node is its
17255 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
17256 the given node is NULL, recursively descend the decl/block tree which
17257 it is the root of, and for each other ..._DECL or BLOCK node contained
17258 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
17259 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
17260 values to point to themselves. */
17262 static void
17263 set_block_origin_self (tree stmt)
17265 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
17267 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
17270 tree local_decl;
17272 for (local_decl = BLOCK_VARS (stmt);
17273 local_decl != NULL_TREE;
17274 local_decl = DECL_CHAIN (local_decl))
17275 if (! DECL_EXTERNAL (local_decl))
17276 set_decl_origin_self (local_decl); /* Potential recursion. */
17280 tree subblock;
17282 for (subblock = BLOCK_SUBBLOCKS (stmt);
17283 subblock != NULL_TREE;
17284 subblock = BLOCK_CHAIN (subblock))
17285 set_block_origin_self (subblock); /* Recurse. */
17290 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
17291 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
17292 node to so that it points to the node itself, thus indicating that the
17293 node represents its own (abstract) origin. Additionally, if the
17294 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
17295 the decl/block tree of which the given node is the root of, and for
17296 each other ..._DECL or BLOCK node contained therein whose
17297 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
17298 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
17299 point to themselves. */
17301 static void
17302 set_decl_origin_self (tree decl)
17304 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
17306 DECL_ABSTRACT_ORIGIN (decl) = decl;
17307 if (TREE_CODE (decl) == FUNCTION_DECL)
17309 tree arg;
17311 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17312 DECL_ABSTRACT_ORIGIN (arg) = arg;
17313 if (DECL_INITIAL (decl) != NULL_TREE
17314 && DECL_INITIAL (decl) != error_mark_node)
17315 set_block_origin_self (DECL_INITIAL (decl));
17320 /* Given a pointer to some BLOCK node, and a boolean value to set the
17321 "abstract" flags to, set that value into the BLOCK_ABSTRACT flag for
17322 the given block, and for all local decls and all local sub-blocks
17323 (recursively) which are contained therein. */
17325 static void
17326 set_block_abstract_flags (tree stmt, int setting)
17328 tree local_decl;
17329 tree subblock;
17330 unsigned int i;
17332 BLOCK_ABSTRACT (stmt) = setting;
17334 for (local_decl = BLOCK_VARS (stmt);
17335 local_decl != NULL_TREE;
17336 local_decl = DECL_CHAIN (local_decl))
17337 if (! DECL_EXTERNAL (local_decl))
17338 set_decl_abstract_flags (local_decl, setting);
17340 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
17342 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
17343 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
17344 || TREE_CODE (local_decl) == PARM_DECL)
17345 set_decl_abstract_flags (local_decl, setting);
17348 for (subblock = BLOCK_SUBBLOCKS (stmt);
17349 subblock != NULL_TREE;
17350 subblock = BLOCK_CHAIN (subblock))
17351 set_block_abstract_flags (subblock, setting);
17354 /* Given a pointer to some ..._DECL node, and a boolean value to set the
17355 "abstract" flags to, set that value into the DECL_ABSTRACT flag for the
17356 given decl, and (in the case where the decl is a FUNCTION_DECL) also
17357 set the abstract flags for all of the parameters, local vars, local
17358 blocks and sub-blocks (recursively) to the same setting. */
17360 static void
17361 set_decl_abstract_flags (tree decl, int setting)
17363 DECL_ABSTRACT (decl) = setting;
17364 if (TREE_CODE (decl) == FUNCTION_DECL)
17366 tree arg;
17368 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17369 DECL_ABSTRACT (arg) = setting;
17370 if (DECL_INITIAL (decl) != NULL_TREE
17371 && DECL_INITIAL (decl) != error_mark_node)
17372 set_block_abstract_flags (DECL_INITIAL (decl), setting);
17376 /* Generate the DWARF2 info for the "abstract" instance of a function which we
17377 may later generate inlined and/or out-of-line instances of. */
17379 static void
17380 dwarf2out_abstract_function (tree decl)
17382 dw_die_ref old_die;
17383 tree save_fn;
17384 tree context;
17385 int was_abstract;
17386 htab_t old_decl_loc_table;
17387 htab_t old_cached_dw_loc_list_table;
17388 int old_call_site_count, old_tail_call_site_count;
17389 struct call_arg_loc_node *old_call_arg_locations;
17391 /* Make sure we have the actual abstract inline, not a clone. */
17392 decl = DECL_ORIGIN (decl);
17394 old_die = lookup_decl_die (decl);
17395 if (old_die && get_AT (old_die, DW_AT_inline))
17396 /* We've already generated the abstract instance. */
17397 return;
17399 /* We can be called while recursively when seeing block defining inlined subroutine
17400 DIE. Be sure to not clobber the outer location table nor use it or we would
17401 get locations in abstract instantces. */
17402 old_decl_loc_table = decl_loc_table;
17403 decl_loc_table = NULL;
17404 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
17405 cached_dw_loc_list_table = NULL;
17406 old_call_arg_locations = call_arg_locations;
17407 call_arg_locations = NULL;
17408 old_call_site_count = call_site_count;
17409 call_site_count = -1;
17410 old_tail_call_site_count = tail_call_site_count;
17411 tail_call_site_count = -1;
17413 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
17414 we don't get confused by DECL_ABSTRACT. */
17415 if (debug_info_level > DINFO_LEVEL_TERSE)
17417 context = decl_class_context (decl);
17418 if (context)
17419 gen_type_die_for_member
17420 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
17423 /* Pretend we've just finished compiling this function. */
17424 save_fn = current_function_decl;
17425 current_function_decl = decl;
17427 was_abstract = DECL_ABSTRACT (decl);
17428 set_decl_abstract_flags (decl, 1);
17429 dwarf2out_decl (decl);
17430 if (! was_abstract)
17431 set_decl_abstract_flags (decl, 0);
17433 current_function_decl = save_fn;
17434 decl_loc_table = old_decl_loc_table;
17435 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
17436 call_arg_locations = old_call_arg_locations;
17437 call_site_count = old_call_site_count;
17438 tail_call_site_count = old_tail_call_site_count;
17441 /* Helper function of premark_used_types() which gets called through
17442 htab_traverse.
17444 Marks the DIE of a given type in *SLOT as perennial, so it never gets
17445 marked as unused by prune_unused_types. */
17447 static int
17448 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
17450 tree type;
17451 dw_die_ref die;
17453 type = (tree) *slot;
17454 die = lookup_type_die (type);
17455 if (die != NULL)
17456 die->die_perennial_p = 1;
17457 return 1;
17460 /* Helper function of premark_types_used_by_global_vars which gets called
17461 through htab_traverse.
17463 Marks the DIE of a given type in *SLOT as perennial, so it never gets
17464 marked as unused by prune_unused_types. The DIE of the type is marked
17465 only if the global variable using the type will actually be emitted. */
17467 static int
17468 premark_types_used_by_global_vars_helper (void **slot,
17469 void *data ATTRIBUTE_UNUSED)
17471 struct types_used_by_vars_entry *entry;
17472 dw_die_ref die;
17474 entry = (struct types_used_by_vars_entry *) *slot;
17475 gcc_assert (entry->type != NULL
17476 && entry->var_decl != NULL);
17477 die = lookup_type_die (entry->type);
17478 if (die)
17480 /* Ask cgraph if the global variable really is to be emitted.
17481 If yes, then we'll keep the DIE of ENTRY->TYPE. */
17482 struct varpool_node *node = varpool_get_node (entry->var_decl);
17483 if (node && node->analyzed)
17485 die->die_perennial_p = 1;
17486 /* Keep the parent DIEs as well. */
17487 while ((die = die->die_parent) && die->die_perennial_p == 0)
17488 die->die_perennial_p = 1;
17491 return 1;
17494 /* Mark all members of used_types_hash as perennial. */
17496 static void
17497 premark_used_types (struct function *fun)
17499 if (fun && fun->used_types_hash)
17500 htab_traverse (fun->used_types_hash, premark_used_types_helper, NULL);
17503 /* Mark all members of types_used_by_vars_entry as perennial. */
17505 static void
17506 premark_types_used_by_global_vars (void)
17508 if (types_used_by_vars_hash)
17509 htab_traverse (types_used_by_vars_hash,
17510 premark_types_used_by_global_vars_helper, NULL);
17513 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
17514 for CA_LOC call arg loc node. */
17516 static dw_die_ref
17517 gen_call_site_die (tree decl, dw_die_ref subr_die,
17518 struct call_arg_loc_node *ca_loc)
17520 dw_die_ref stmt_die = NULL, die;
17521 tree block = ca_loc->block;
17523 while (block
17524 && block != DECL_INITIAL (decl)
17525 && TREE_CODE (block) == BLOCK)
17527 if (block_map.length () > BLOCK_NUMBER (block))
17528 stmt_die = block_map[BLOCK_NUMBER (block)];
17529 if (stmt_die)
17530 break;
17531 block = BLOCK_SUPERCONTEXT (block);
17533 if (stmt_die == NULL)
17534 stmt_die = subr_die;
17535 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
17536 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
17537 if (ca_loc->tail_call_p)
17538 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
17539 if (ca_loc->symbol_ref)
17541 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
17542 if (tdie)
17543 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
17544 else
17545 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
17547 return die;
17550 /* Generate a DIE to represent a declared function (either file-scope or
17551 block-local). */
17553 static void
17554 gen_subprogram_die (tree decl, dw_die_ref context_die)
17556 tree origin = decl_ultimate_origin (decl);
17557 dw_die_ref subr_die;
17558 tree outer_scope;
17559 dw_die_ref old_die = lookup_decl_die (decl);
17560 int declaration = (current_function_decl != decl
17561 || class_or_namespace_scope_p (context_die));
17563 premark_used_types (DECL_STRUCT_FUNCTION (decl));
17565 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
17566 started to generate the abstract instance of an inline, decided to output
17567 its containing class, and proceeded to emit the declaration of the inline
17568 from the member list for the class. If so, DECLARATION takes priority;
17569 we'll get back to the abstract instance when done with the class. */
17571 /* The class-scope declaration DIE must be the primary DIE. */
17572 if (origin && declaration && class_or_namespace_scope_p (context_die))
17574 origin = NULL;
17575 gcc_assert (!old_die);
17578 /* Now that the C++ front end lazily declares artificial member fns, we
17579 might need to retrofit the declaration into its class. */
17580 if (!declaration && !origin && !old_die
17581 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
17582 && !class_or_namespace_scope_p (context_die)
17583 && debug_info_level > DINFO_LEVEL_TERSE)
17584 old_die = force_decl_die (decl);
17586 if (origin != NULL)
17588 gcc_assert (!declaration || local_scope_p (context_die));
17590 /* Fixup die_parent for the abstract instance of a nested
17591 inline function. */
17592 if (old_die && old_die->die_parent == NULL)
17593 add_child_die (context_die, old_die);
17595 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17596 add_abstract_origin_attribute (subr_die, origin);
17597 /* This is where the actual code for a cloned function is.
17598 Let's emit linkage name attribute for it. This helps
17599 debuggers to e.g, set breakpoints into
17600 constructors/destructors when the user asks "break
17601 K::K". */
17602 add_linkage_name (subr_die, decl);
17604 else if (old_die)
17606 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17607 struct dwarf_file_data * file_index = lookup_filename (s.file);
17609 if (!get_AT_flag (old_die, DW_AT_declaration)
17610 /* We can have a normal definition following an inline one in the
17611 case of redefinition of GNU C extern inlines.
17612 It seems reasonable to use AT_specification in this case. */
17613 && !get_AT (old_die, DW_AT_inline))
17615 /* Detect and ignore this case, where we are trying to output
17616 something we have already output. */
17617 return;
17620 /* If the definition comes from the same place as the declaration,
17621 maybe use the old DIE. We always want the DIE for this function
17622 that has the *_pc attributes to be under comp_unit_die so the
17623 debugger can find it. We also need to do this for abstract
17624 instances of inlines, since the spec requires the out-of-line copy
17625 to have the same parent. For local class methods, this doesn't
17626 apply; we just use the old DIE. */
17627 if ((is_cu_die (old_die->die_parent) || context_die == NULL)
17628 && (DECL_ARTIFICIAL (decl)
17629 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
17630 && (get_AT_unsigned (old_die, DW_AT_decl_line)
17631 == (unsigned) s.line))))
17633 subr_die = old_die;
17635 /* Clear out the declaration attribute and the formal parameters.
17636 Do not remove all children, because it is possible that this
17637 declaration die was forced using force_decl_die(). In such
17638 cases die that forced declaration die (e.g. TAG_imported_module)
17639 is one of the children that we do not want to remove. */
17640 remove_AT (subr_die, DW_AT_declaration);
17641 remove_AT (subr_die, DW_AT_object_pointer);
17642 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
17644 else
17646 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17647 add_AT_specification (subr_die, old_die);
17648 add_pubname (decl, subr_die);
17649 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
17650 add_AT_file (subr_die, DW_AT_decl_file, file_index);
17651 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
17652 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
17655 else
17657 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17659 if (TREE_PUBLIC (decl))
17660 add_AT_flag (subr_die, DW_AT_external, 1);
17662 add_name_and_src_coords_attributes (subr_die, decl);
17663 add_pubname (decl, subr_die);
17664 if (debug_info_level > DINFO_LEVEL_TERSE)
17666 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
17667 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
17668 0, 0, context_die);
17671 add_pure_or_virtual_attribute (subr_die, decl);
17672 if (DECL_ARTIFICIAL (decl))
17673 add_AT_flag (subr_die, DW_AT_artificial, 1);
17675 add_accessibility_attribute (subr_die, decl);
17678 if (declaration)
17680 if (!old_die || !get_AT (old_die, DW_AT_inline))
17682 add_AT_flag (subr_die, DW_AT_declaration, 1);
17684 /* If this is an explicit function declaration then generate
17685 a DW_AT_explicit attribute. */
17686 if (lang_hooks.decls.function_decl_explicit_p (decl)
17687 && (dwarf_version >= 3 || !dwarf_strict))
17688 add_AT_flag (subr_die, DW_AT_explicit, 1);
17690 /* The first time we see a member function, it is in the context of
17691 the class to which it belongs. We make sure of this by emitting
17692 the class first. The next time is the definition, which is
17693 handled above. The two may come from the same source text.
17695 Note that force_decl_die() forces function declaration die. It is
17696 later reused to represent definition. */
17697 equate_decl_number_to_die (decl, subr_die);
17700 else if (DECL_ABSTRACT (decl))
17702 if (DECL_DECLARED_INLINE_P (decl))
17704 if (cgraph_function_possibly_inlined_p (decl))
17705 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
17706 else
17707 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
17709 else
17711 if (cgraph_function_possibly_inlined_p (decl))
17712 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
17713 else
17714 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
17717 if (DECL_DECLARED_INLINE_P (decl)
17718 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
17719 add_AT_flag (subr_die, DW_AT_artificial, 1);
17721 equate_decl_number_to_die (decl, subr_die);
17723 else if (!DECL_EXTERNAL (decl))
17725 HOST_WIDE_INT cfa_fb_offset;
17726 struct function *fun = DECL_STRUCT_FUNCTION (decl);
17728 if (!old_die || !get_AT (old_die, DW_AT_inline))
17729 equate_decl_number_to_die (decl, subr_die);
17731 gcc_checking_assert (fun);
17732 if (!flag_reorder_blocks_and_partition)
17734 dw_fde_ref fde = fun->fde;
17735 if (fde->dw_fde_begin)
17737 /* We have already generated the labels. */
17738 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
17739 fde->dw_fde_end, false);
17741 else
17743 /* Create start/end labels and add the range. */
17744 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
17745 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
17746 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
17747 current_function_funcdef_no);
17748 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
17749 current_function_funcdef_no);
17750 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
17751 false);
17754 #if VMS_DEBUGGING_INFO
17755 /* HP OpenVMS Industry Standard 64: DWARF Extensions
17756 Section 2.3 Prologue and Epilogue Attributes:
17757 When a breakpoint is set on entry to a function, it is generally
17758 desirable for execution to be suspended, not on the very first
17759 instruction of the function, but rather at a point after the
17760 function's frame has been set up, after any language defined local
17761 declaration processing has been completed, and before execution of
17762 the first statement of the function begins. Debuggers generally
17763 cannot properly determine where this point is. Similarly for a
17764 breakpoint set on exit from a function. The prologue and epilogue
17765 attributes allow a compiler to communicate the location(s) to use. */
17768 if (fde->dw_fde_vms_end_prologue)
17769 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
17770 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
17772 if (fde->dw_fde_vms_begin_epilogue)
17773 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
17774 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
17776 #endif
17779 else
17781 /* Generate pubnames entries for the split function code ranges. */
17782 dw_fde_ref fde = fun->fde;
17784 if (fde->dw_fde_second_begin)
17786 if (dwarf_version >= 3 || !dwarf_strict)
17788 /* We should use ranges for non-contiguous code section
17789 addresses. Use the actual code range for the initial
17790 section, since the HOT/COLD labels might precede an
17791 alignment offset. */
17792 bool range_list_added = false;
17793 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
17794 fde->dw_fde_end, &range_list_added,
17795 false);
17796 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
17797 fde->dw_fde_second_end,
17798 &range_list_added, false);
17799 if (range_list_added)
17800 add_ranges (NULL);
17802 else
17804 /* There is no real support in DW2 for this .. so we make
17805 a work-around. First, emit the pub name for the segment
17806 containing the function label. Then make and emit a
17807 simplified subprogram DIE for the second segment with the
17808 name pre-fixed by __hot/cold_sect_of_. We use the same
17809 linkage name for the second die so that gdb will find both
17810 sections when given "b foo". */
17811 const char *name = NULL;
17812 tree decl_name = DECL_NAME (decl);
17813 dw_die_ref seg_die;
17815 /* Do the 'primary' section. */
17816 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
17817 fde->dw_fde_end, false);
17819 /* Build a minimal DIE for the secondary section. */
17820 seg_die = new_die (DW_TAG_subprogram,
17821 subr_die->die_parent, decl);
17823 if (TREE_PUBLIC (decl))
17824 add_AT_flag (seg_die, DW_AT_external, 1);
17826 if (decl_name != NULL
17827 && IDENTIFIER_POINTER (decl_name) != NULL)
17829 name = dwarf2_name (decl, 1);
17830 if (! DECL_ARTIFICIAL (decl))
17831 add_src_coords_attributes (seg_die, decl);
17833 add_linkage_name (seg_die, decl);
17835 gcc_assert (name != NULL);
17836 add_pure_or_virtual_attribute (seg_die, decl);
17837 if (DECL_ARTIFICIAL (decl))
17838 add_AT_flag (seg_die, DW_AT_artificial, 1);
17840 name = concat ("__second_sect_of_", name, NULL);
17841 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
17842 fde->dw_fde_second_end, false);
17843 add_name_attribute (seg_die, name);
17844 if (want_pubnames ())
17845 add_pubname_string (name, seg_die);
17848 else
17849 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
17850 false);
17853 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
17855 /* We define the "frame base" as the function's CFA. This is more
17856 convenient for several reasons: (1) It's stable across the prologue
17857 and epilogue, which makes it better than just a frame pointer,
17858 (2) With dwarf3, there exists a one-byte encoding that allows us
17859 to reference the .debug_frame data by proxy, but failing that,
17860 (3) We can at least reuse the code inspection and interpretation
17861 code that determines the CFA position at various points in the
17862 function. */
17863 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
17865 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
17866 add_AT_loc (subr_die, DW_AT_frame_base, op);
17868 else
17870 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
17871 if (list->dw_loc_next)
17872 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
17873 else
17874 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
17877 /* Compute a displacement from the "steady-state frame pointer" to
17878 the CFA. The former is what all stack slots and argument slots
17879 will reference in the rtl; the later is what we've told the
17880 debugger about. We'll need to adjust all frame_base references
17881 by this displacement. */
17882 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
17884 if (fun->static_chain_decl)
17885 add_AT_location_description (subr_die, DW_AT_static_link,
17886 loc_list_from_tree (fun->static_chain_decl, 2));
17889 /* Generate child dies for template paramaters. */
17890 if (debug_info_level > DINFO_LEVEL_TERSE)
17891 gen_generic_params_dies (decl);
17893 /* Now output descriptions of the arguments for this function. This gets
17894 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
17895 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
17896 `...' at the end of the formal parameter list. In order to find out if
17897 there was a trailing ellipsis or not, we must instead look at the type
17898 associated with the FUNCTION_DECL. This will be a node of type
17899 FUNCTION_TYPE. If the chain of type nodes hanging off of this
17900 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
17901 an ellipsis at the end. */
17903 /* In the case where we are describing a mere function declaration, all we
17904 need to do here (and all we *can* do here) is to describe the *types* of
17905 its formal parameters. */
17906 if (debug_info_level <= DINFO_LEVEL_TERSE)
17908 else if (declaration)
17909 gen_formal_types_die (decl, subr_die);
17910 else
17912 /* Generate DIEs to represent all known formal parameters. */
17913 tree parm = DECL_ARGUMENTS (decl);
17914 tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
17915 tree generic_decl_parm = generic_decl
17916 ? DECL_ARGUMENTS (generic_decl)
17917 : NULL;
17919 /* Now we want to walk the list of parameters of the function and
17920 emit their relevant DIEs.
17922 We consider the case of DECL being an instance of a generic function
17923 as well as it being a normal function.
17925 If DECL is an instance of a generic function we walk the
17926 parameters of the generic function declaration _and_ the parameters of
17927 DECL itself. This is useful because we want to emit specific DIEs for
17928 function parameter packs and those are declared as part of the
17929 generic function declaration. In that particular case,
17930 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
17931 That DIE has children DIEs representing the set of arguments
17932 of the pack. Note that the set of pack arguments can be empty.
17933 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
17934 children DIE.
17936 Otherwise, we just consider the parameters of DECL. */
17937 while (generic_decl_parm || parm)
17939 if (generic_decl_parm
17940 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
17941 gen_formal_parameter_pack_die (generic_decl_parm,
17942 parm, subr_die,
17943 &parm);
17944 else if (parm)
17946 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
17948 if (parm == DECL_ARGUMENTS (decl)
17949 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
17950 && parm_die
17951 && (dwarf_version >= 3 || !dwarf_strict))
17952 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
17954 parm = DECL_CHAIN (parm);
17957 if (generic_decl_parm)
17958 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
17961 /* Decide whether we need an unspecified_parameters DIE at the end.
17962 There are 2 more cases to do this for: 1) the ansi ... declaration -
17963 this is detectable when the end of the arg list is not a
17964 void_type_node 2) an unprototyped function declaration (not a
17965 definition). This just means that we have no info about the
17966 parameters at all. */
17967 if (prototype_p (TREE_TYPE (decl)))
17969 /* This is the prototyped case, check for.... */
17970 if (stdarg_p (TREE_TYPE (decl)))
17971 gen_unspecified_parameters_die (decl, subr_die);
17973 else if (DECL_INITIAL (decl) == NULL_TREE)
17974 gen_unspecified_parameters_die (decl, subr_die);
17977 /* Output Dwarf info for all of the stuff within the body of the function
17978 (if it has one - it may be just a declaration). */
17979 outer_scope = DECL_INITIAL (decl);
17981 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
17982 a function. This BLOCK actually represents the outermost binding contour
17983 for the function, i.e. the contour in which the function's formal
17984 parameters and labels get declared. Curiously, it appears that the front
17985 end doesn't actually put the PARM_DECL nodes for the current function onto
17986 the BLOCK_VARS list for this outer scope, but are strung off of the
17987 DECL_ARGUMENTS list for the function instead.
17989 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
17990 the LABEL_DECL nodes for the function however, and we output DWARF info
17991 for those in decls_for_scope. Just within the `outer_scope' there will be
17992 a BLOCK node representing the function's outermost pair of curly braces,
17993 and any blocks used for the base and member initializers of a C++
17994 constructor function. */
17995 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
17997 int call_site_note_count = 0;
17998 int tail_call_site_note_count = 0;
18000 /* Emit a DW_TAG_variable DIE for a named return value. */
18001 if (DECL_NAME (DECL_RESULT (decl)))
18002 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
18004 current_function_has_inlines = 0;
18005 decls_for_scope (outer_scope, subr_die, 0);
18007 if (call_arg_locations && !dwarf_strict)
18009 struct call_arg_loc_node *ca_loc;
18010 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
18012 dw_die_ref die = NULL;
18013 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
18014 rtx arg, next_arg;
18016 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
18017 arg; arg = next_arg)
18019 dw_loc_descr_ref reg, val;
18020 enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
18021 dw_die_ref cdie, tdie = NULL;
18023 next_arg = XEXP (arg, 1);
18024 if (REG_P (XEXP (XEXP (arg, 0), 0))
18025 && next_arg
18026 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
18027 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
18028 && REGNO (XEXP (XEXP (arg, 0), 0))
18029 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
18030 next_arg = XEXP (next_arg, 1);
18031 if (mode == VOIDmode)
18033 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
18034 if (mode == VOIDmode)
18035 mode = GET_MODE (XEXP (arg, 0));
18037 if (mode == VOIDmode || mode == BLKmode)
18038 continue;
18039 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
18041 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18042 tloc = XEXP (XEXP (arg, 0), 1);
18043 continue;
18045 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
18046 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
18048 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18049 tlocc = XEXP (XEXP (arg, 0), 1);
18050 continue;
18052 reg = NULL;
18053 if (REG_P (XEXP (XEXP (arg, 0), 0)))
18054 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
18055 VAR_INIT_STATUS_INITIALIZED);
18056 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
18058 rtx mem = XEXP (XEXP (arg, 0), 0);
18059 reg = mem_loc_descriptor (XEXP (mem, 0),
18060 get_address_mode (mem),
18061 GET_MODE (mem),
18062 VAR_INIT_STATUS_INITIALIZED);
18064 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
18065 == DEBUG_PARAMETER_REF)
18067 tree tdecl
18068 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
18069 tdie = lookup_decl_die (tdecl);
18070 if (tdie == NULL)
18071 continue;
18073 else
18074 continue;
18075 if (reg == NULL
18076 && GET_CODE (XEXP (XEXP (arg, 0), 0))
18077 != DEBUG_PARAMETER_REF)
18078 continue;
18079 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
18080 VOIDmode,
18081 VAR_INIT_STATUS_INITIALIZED);
18082 if (val == NULL)
18083 continue;
18084 if (die == NULL)
18085 die = gen_call_site_die (decl, subr_die, ca_loc);
18086 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
18087 NULL_TREE);
18088 if (reg != NULL)
18089 add_AT_loc (cdie, DW_AT_location, reg);
18090 else if (tdie != NULL)
18091 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
18092 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
18093 if (next_arg != XEXP (arg, 1))
18095 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
18096 if (mode == VOIDmode)
18097 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
18098 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
18099 0), 1),
18100 mode, VOIDmode,
18101 VAR_INIT_STATUS_INITIALIZED);
18102 if (val != NULL)
18103 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
18106 if (die == NULL
18107 && (ca_loc->symbol_ref || tloc))
18108 die = gen_call_site_die (decl, subr_die, ca_loc);
18109 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
18111 dw_loc_descr_ref tval = NULL;
18113 if (tloc != NULL_RTX)
18114 tval = mem_loc_descriptor (tloc,
18115 GET_MODE (tloc) == VOIDmode
18116 ? Pmode : GET_MODE (tloc),
18117 VOIDmode,
18118 VAR_INIT_STATUS_INITIALIZED);
18119 if (tval)
18120 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
18121 else if (tlocc != NULL_RTX)
18123 tval = mem_loc_descriptor (tlocc,
18124 GET_MODE (tlocc) == VOIDmode
18125 ? Pmode : GET_MODE (tlocc),
18126 VOIDmode,
18127 VAR_INIT_STATUS_INITIALIZED);
18128 if (tval)
18129 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
18130 tval);
18133 if (die != NULL)
18135 call_site_note_count++;
18136 if (ca_loc->tail_call_p)
18137 tail_call_site_note_count++;
18141 call_arg_locations = NULL;
18142 call_arg_loc_last = NULL;
18143 if (tail_call_site_count >= 0
18144 && tail_call_site_count == tail_call_site_note_count
18145 && !dwarf_strict)
18147 if (call_site_count >= 0
18148 && call_site_count == call_site_note_count)
18149 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
18150 else
18151 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
18153 call_site_count = -1;
18154 tail_call_site_count = -1;
18156 /* Add the calling convention attribute if requested. */
18157 add_calling_convention_attribute (subr_die, decl);
18161 /* Returns a hash value for X (which really is a die_struct). */
18163 static hashval_t
18164 common_block_die_table_hash (const void *x)
18166 const_dw_die_ref d = (const_dw_die_ref) x;
18167 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
18170 /* Return nonzero if decl_id and die_parent of die_struct X is the same
18171 as decl_id and die_parent of die_struct Y. */
18173 static int
18174 common_block_die_table_eq (const void *x, const void *y)
18176 const_dw_die_ref d = (const_dw_die_ref) x;
18177 const_dw_die_ref e = (const_dw_die_ref) y;
18178 return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
18181 /* Generate a DIE to represent a declared data object.
18182 Either DECL or ORIGIN must be non-null. */
18184 static void
18185 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
18187 HOST_WIDE_INT off = 0;
18188 tree com_decl;
18189 tree decl_or_origin = decl ? decl : origin;
18190 tree ultimate_origin;
18191 dw_die_ref var_die;
18192 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
18193 dw_die_ref origin_die;
18194 bool declaration = (DECL_EXTERNAL (decl_or_origin)
18195 || class_or_namespace_scope_p (context_die));
18196 bool specialization_p = false;
18198 ultimate_origin = decl_ultimate_origin (decl_or_origin);
18199 if (decl || ultimate_origin)
18200 origin = ultimate_origin;
18201 com_decl = fortran_common (decl_or_origin, &off);
18203 /* Symbol in common gets emitted as a child of the common block, in the form
18204 of a data member. */
18205 if (com_decl)
18207 dw_die_ref com_die;
18208 dw_loc_list_ref loc;
18209 die_node com_die_arg;
18211 var_die = lookup_decl_die (decl_or_origin);
18212 if (var_die)
18214 if (get_AT (var_die, DW_AT_location) == NULL)
18216 loc = loc_list_from_tree (com_decl, off ? 1 : 2);
18217 if (loc)
18219 if (off)
18221 /* Optimize the common case. */
18222 if (single_element_loc_list_p (loc)
18223 && loc->expr->dw_loc_opc == DW_OP_addr
18224 && loc->expr->dw_loc_next == NULL
18225 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
18226 == SYMBOL_REF)
18228 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18229 loc->expr->dw_loc_oprnd1.v.val_addr
18230 = plus_constant (GET_MODE (x), x , off);
18232 else
18233 loc_list_plus_const (loc, off);
18235 add_AT_location_description (var_die, DW_AT_location, loc);
18236 remove_AT (var_die, DW_AT_declaration);
18239 return;
18242 if (common_block_die_table == NULL)
18243 common_block_die_table
18244 = htab_create_ggc (10, common_block_die_table_hash,
18245 common_block_die_table_eq, NULL);
18247 com_die_arg.decl_id = DECL_UID (com_decl);
18248 com_die_arg.die_parent = context_die;
18249 com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
18250 loc = loc_list_from_tree (com_decl, 2);
18251 if (com_die == NULL)
18253 const char *cnam
18254 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
18255 void **slot;
18257 com_die = new_die (DW_TAG_common_block, context_die, decl);
18258 add_name_and_src_coords_attributes (com_die, com_decl);
18259 if (loc)
18261 add_AT_location_description (com_die, DW_AT_location, loc);
18262 /* Avoid sharing the same loc descriptor between
18263 DW_TAG_common_block and DW_TAG_variable. */
18264 loc = loc_list_from_tree (com_decl, 2);
18266 else if (DECL_EXTERNAL (decl))
18267 add_AT_flag (com_die, DW_AT_declaration, 1);
18268 if (want_pubnames ())
18269 add_pubname_string (cnam, com_die); /* ??? needed? */
18270 com_die->decl_id = DECL_UID (com_decl);
18271 slot = htab_find_slot (common_block_die_table, com_die, INSERT);
18272 *slot = (void *) com_die;
18274 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
18276 add_AT_location_description (com_die, DW_AT_location, loc);
18277 loc = loc_list_from_tree (com_decl, 2);
18278 remove_AT (com_die, DW_AT_declaration);
18280 var_die = new_die (DW_TAG_variable, com_die, decl);
18281 add_name_and_src_coords_attributes (var_die, decl);
18282 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
18283 TREE_THIS_VOLATILE (decl), context_die);
18284 add_AT_flag (var_die, DW_AT_external, 1);
18285 if (loc)
18287 if (off)
18289 /* Optimize the common case. */
18290 if (single_element_loc_list_p (loc)
18291 && loc->expr->dw_loc_opc == DW_OP_addr
18292 && loc->expr->dw_loc_next == NULL
18293 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
18295 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18296 loc->expr->dw_loc_oprnd1.v.val_addr
18297 = plus_constant (GET_MODE (x), x, off);
18299 else
18300 loc_list_plus_const (loc, off);
18302 add_AT_location_description (var_die, DW_AT_location, loc);
18304 else if (DECL_EXTERNAL (decl))
18305 add_AT_flag (var_die, DW_AT_declaration, 1);
18306 equate_decl_number_to_die (decl, var_die);
18307 return;
18310 /* If the compiler emitted a definition for the DECL declaration
18311 and if we already emitted a DIE for it, don't emit a second
18312 DIE for it again. Allow re-declarations of DECLs that are
18313 inside functions, though. */
18314 if (old_die && declaration && !local_scope_p (context_die))
18315 return;
18317 /* For static data members, the declaration in the class is supposed
18318 to have DW_TAG_member tag; the specification should still be
18319 DW_TAG_variable referencing the DW_TAG_member DIE. */
18320 if (declaration && class_scope_p (context_die))
18321 var_die = new_die (DW_TAG_member, context_die, decl);
18322 else
18323 var_die = new_die (DW_TAG_variable, context_die, decl);
18325 origin_die = NULL;
18326 if (origin != NULL)
18327 origin_die = add_abstract_origin_attribute (var_die, origin);
18329 /* Loop unrolling can create multiple blocks that refer to the same
18330 static variable, so we must test for the DW_AT_declaration flag.
18332 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
18333 copy decls and set the DECL_ABSTRACT flag on them instead of
18334 sharing them.
18336 ??? Duplicated blocks have been rewritten to use .debug_ranges.
18338 ??? The declare_in_namespace support causes us to get two DIEs for one
18339 variable, both of which are declarations. We want to avoid considering
18340 one to be a specification, so we must test that this DIE is not a
18341 declaration. */
18342 else if (old_die && TREE_STATIC (decl) && ! declaration
18343 && get_AT_flag (old_die, DW_AT_declaration) == 1)
18345 /* This is a definition of a C++ class level static. */
18346 add_AT_specification (var_die, old_die);
18347 specialization_p = true;
18348 if (DECL_NAME (decl))
18350 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18351 struct dwarf_file_data * file_index = lookup_filename (s.file);
18353 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18354 add_AT_file (var_die, DW_AT_decl_file, file_index);
18356 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18357 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
18359 if (old_die->die_tag == DW_TAG_member)
18360 add_linkage_name (var_die, decl);
18363 else
18364 add_name_and_src_coords_attributes (var_die, decl);
18366 if ((origin == NULL && !specialization_p)
18367 || (origin != NULL
18368 && !DECL_ABSTRACT (decl_or_origin)
18369 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
18370 decl_function_context
18371 (decl_or_origin))))
18373 tree type = TREE_TYPE (decl_or_origin);
18375 if (decl_by_reference_p (decl_or_origin))
18376 add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
18377 else
18378 add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
18379 TREE_THIS_VOLATILE (decl_or_origin), context_die);
18382 if (origin == NULL && !specialization_p)
18384 if (TREE_PUBLIC (decl))
18385 add_AT_flag (var_die, DW_AT_external, 1);
18387 if (DECL_ARTIFICIAL (decl))
18388 add_AT_flag (var_die, DW_AT_artificial, 1);
18390 add_accessibility_attribute (var_die, decl);
18393 if (declaration)
18394 add_AT_flag (var_die, DW_AT_declaration, 1);
18396 if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
18397 equate_decl_number_to_die (decl, var_die);
18399 if (! declaration
18400 && (! DECL_ABSTRACT (decl_or_origin)
18401 /* Local static vars are shared between all clones/inlines,
18402 so emit DW_AT_location on the abstract DIE if DECL_RTL is
18403 already set. */
18404 || (TREE_CODE (decl_or_origin) == VAR_DECL
18405 && TREE_STATIC (decl_or_origin)
18406 && DECL_RTL_SET_P (decl_or_origin)))
18407 /* When abstract origin already has DW_AT_location attribute, no need
18408 to add it again. */
18409 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
18411 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
18412 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
18413 defer_location (decl_or_origin, var_die);
18414 else
18415 add_location_or_const_value_attribute (var_die, decl_or_origin,
18416 decl == NULL, DW_AT_location);
18417 add_pubname (decl_or_origin, var_die);
18419 else
18420 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
18423 /* Generate a DIE to represent a named constant. */
18425 static void
18426 gen_const_die (tree decl, dw_die_ref context_die)
18428 dw_die_ref const_die;
18429 tree type = TREE_TYPE (decl);
18431 const_die = new_die (DW_TAG_constant, context_die, decl);
18432 add_name_and_src_coords_attributes (const_die, decl);
18433 add_type_attribute (const_die, type, 1, 0, context_die);
18434 if (TREE_PUBLIC (decl))
18435 add_AT_flag (const_die, DW_AT_external, 1);
18436 if (DECL_ARTIFICIAL (decl))
18437 add_AT_flag (const_die, DW_AT_artificial, 1);
18438 tree_add_const_value_attribute_for_decl (const_die, decl);
18441 /* Generate a DIE to represent a label identifier. */
18443 static void
18444 gen_label_die (tree decl, dw_die_ref context_die)
18446 tree origin = decl_ultimate_origin (decl);
18447 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
18448 rtx insn;
18449 char label[MAX_ARTIFICIAL_LABEL_BYTES];
18451 if (origin != NULL)
18452 add_abstract_origin_attribute (lbl_die, origin);
18453 else
18454 add_name_and_src_coords_attributes (lbl_die, decl);
18456 if (DECL_ABSTRACT (decl))
18457 equate_decl_number_to_die (decl, lbl_die);
18458 else
18460 insn = DECL_RTL_IF_SET (decl);
18462 /* Deleted labels are programmer specified labels which have been
18463 eliminated because of various optimizations. We still emit them
18464 here so that it is possible to put breakpoints on them. */
18465 if (insn
18466 && (LABEL_P (insn)
18467 || ((NOTE_P (insn)
18468 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
18470 /* When optimization is enabled (via -O) some parts of the compiler
18471 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
18472 represent source-level labels which were explicitly declared by
18473 the user. This really shouldn't be happening though, so catch
18474 it if it ever does happen. */
18475 gcc_assert (!INSN_DELETED_P (insn));
18477 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
18478 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18480 else if (insn
18481 && NOTE_P (insn)
18482 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
18483 && CODE_LABEL_NUMBER (insn) != -1)
18485 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
18486 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18491 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
18492 attributes to the DIE for a block STMT, to describe where the inlined
18493 function was called from. This is similar to add_src_coords_attributes. */
18495 static inline void
18496 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
18498 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
18500 if (dwarf_version >= 3 || !dwarf_strict)
18502 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
18503 add_AT_unsigned (die, DW_AT_call_line, s.line);
18508 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
18509 Add low_pc and high_pc attributes to the DIE for a block STMT. */
18511 static inline void
18512 add_high_low_attributes (tree stmt, dw_die_ref die)
18514 char label[MAX_ARTIFICIAL_LABEL_BYTES];
18516 if (BLOCK_FRAGMENT_CHAIN (stmt)
18517 && (dwarf_version >= 3 || !dwarf_strict))
18519 tree chain, superblock = NULL_TREE;
18520 dw_die_ref pdie;
18521 dw_attr_ref attr = NULL;
18523 if (inlined_function_outer_scope_p (stmt))
18525 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18526 BLOCK_NUMBER (stmt));
18527 add_AT_lbl_id (die, DW_AT_entry_pc, label);
18530 /* Optimize duplicate .debug_ranges lists or even tails of
18531 lists. If this BLOCK has same ranges as its supercontext,
18532 lookup DW_AT_ranges attribute in the supercontext (and
18533 recursively so), verify that the ranges_table contains the
18534 right values and use it instead of adding a new .debug_range. */
18535 for (chain = stmt, pdie = die;
18536 BLOCK_SAME_RANGE (chain);
18537 chain = BLOCK_SUPERCONTEXT (chain))
18539 dw_attr_ref new_attr;
18541 pdie = pdie->die_parent;
18542 if (pdie == NULL)
18543 break;
18544 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
18545 break;
18546 new_attr = get_AT (pdie, DW_AT_ranges);
18547 if (new_attr == NULL
18548 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
18549 break;
18550 attr = new_attr;
18551 superblock = BLOCK_SUPERCONTEXT (chain);
18553 if (attr != NULL
18554 && (ranges_table[attr->dw_attr_val.v.val_offset
18555 / 2 / DWARF2_ADDR_SIZE].num
18556 == BLOCK_NUMBER (superblock))
18557 && BLOCK_FRAGMENT_CHAIN (superblock))
18559 unsigned long off = attr->dw_attr_val.v.val_offset
18560 / 2 / DWARF2_ADDR_SIZE;
18561 unsigned long supercnt = 0, thiscnt = 0;
18562 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
18563 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
18565 ++supercnt;
18566 gcc_checking_assert (ranges_table[off + supercnt].num
18567 == BLOCK_NUMBER (chain));
18569 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
18570 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
18571 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
18572 ++thiscnt;
18573 gcc_assert (supercnt >= thiscnt);
18574 add_AT_range_list (die, DW_AT_ranges,
18575 ((off + supercnt - thiscnt)
18576 * 2 * DWARF2_ADDR_SIZE),
18577 false);
18578 return;
18581 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
18583 chain = BLOCK_FRAGMENT_CHAIN (stmt);
18586 add_ranges (chain);
18587 chain = BLOCK_FRAGMENT_CHAIN (chain);
18589 while (chain);
18590 add_ranges (NULL);
18592 else
18594 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
18595 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18596 BLOCK_NUMBER (stmt));
18597 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
18598 BLOCK_NUMBER (stmt));
18599 add_AT_low_high_pc (die, label, label_high, false);
18603 /* Generate a DIE for a lexical block. */
18605 static void
18606 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
18608 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
18610 if (call_arg_locations)
18612 if (block_map.length () <= BLOCK_NUMBER (stmt))
18613 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
18614 block_map[BLOCK_NUMBER (stmt)] = stmt_die;
18617 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
18618 add_high_low_attributes (stmt, stmt_die);
18620 decls_for_scope (stmt, stmt_die, depth);
18623 /* Generate a DIE for an inlined subprogram. */
18625 static void
18626 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
18628 tree decl;
18630 /* The instance of function that is effectively being inlined shall not
18631 be abstract. */
18632 gcc_assert (! BLOCK_ABSTRACT (stmt));
18634 decl = block_ultimate_origin (stmt);
18636 /* Emit info for the abstract instance first, if we haven't yet. We
18637 must emit this even if the block is abstract, otherwise when we
18638 emit the block below (or elsewhere), we may end up trying to emit
18639 a die whose origin die hasn't been emitted, and crashing. */
18640 dwarf2out_abstract_function (decl);
18642 if (! BLOCK_ABSTRACT (stmt))
18644 dw_die_ref subr_die
18645 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
18647 if (call_arg_locations)
18649 if (block_map.length () <= BLOCK_NUMBER (stmt))
18650 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
18651 block_map[BLOCK_NUMBER (stmt)] = subr_die;
18653 add_abstract_origin_attribute (subr_die, decl);
18654 if (TREE_ASM_WRITTEN (stmt))
18655 add_high_low_attributes (stmt, subr_die);
18656 add_call_src_coords_attributes (stmt, subr_die);
18658 decls_for_scope (stmt, subr_die, depth);
18659 current_function_has_inlines = 1;
18663 /* Generate a DIE for a field in a record, or structure. */
18665 static void
18666 gen_field_die (tree decl, dw_die_ref context_die)
18668 dw_die_ref decl_die;
18670 if (TREE_TYPE (decl) == error_mark_node)
18671 return;
18673 decl_die = new_die (DW_TAG_member, context_die, decl);
18674 add_name_and_src_coords_attributes (decl_die, decl);
18675 add_type_attribute (decl_die, member_declared_type (decl),
18676 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
18677 context_die);
18679 if (DECL_BIT_FIELD_TYPE (decl))
18681 add_byte_size_attribute (decl_die, decl);
18682 add_bit_size_attribute (decl_die, decl);
18683 add_bit_offset_attribute (decl_die, decl);
18686 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
18687 add_data_member_location_attribute (decl_die, decl);
18689 if (DECL_ARTIFICIAL (decl))
18690 add_AT_flag (decl_die, DW_AT_artificial, 1);
18692 add_accessibility_attribute (decl_die, decl);
18694 /* Equate decl number to die, so that we can look up this decl later on. */
18695 equate_decl_number_to_die (decl, decl_die);
18698 #if 0
18699 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18700 Use modified_type_die instead.
18701 We keep this code here just in case these types of DIEs may be needed to
18702 represent certain things in other languages (e.g. Pascal) someday. */
18704 static void
18705 gen_pointer_type_die (tree type, dw_die_ref context_die)
18707 dw_die_ref ptr_die
18708 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
18710 equate_type_number_to_die (type, ptr_die);
18711 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18712 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18715 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18716 Use modified_type_die instead.
18717 We keep this code here just in case these types of DIEs may be needed to
18718 represent certain things in other languages (e.g. Pascal) someday. */
18720 static void
18721 gen_reference_type_die (tree type, dw_die_ref context_die)
18723 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
18725 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
18726 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
18727 else
18728 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
18730 equate_type_number_to_die (type, ref_die);
18731 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
18732 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18734 #endif
18736 /* Generate a DIE for a pointer to a member type. */
18738 static void
18739 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
18741 dw_die_ref ptr_die
18742 = new_die (DW_TAG_ptr_to_member_type,
18743 scope_die_for (type, context_die), type);
18745 equate_type_number_to_die (type, ptr_die);
18746 add_AT_die_ref (ptr_die, DW_AT_containing_type,
18747 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
18748 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18751 typedef const char *dchar_p; /* For DEF_VEC_P. */
18753 static char *producer_string;
18755 /* Return a heap allocated producer string including command line options
18756 if -grecord-gcc-switches. */
18758 static char *
18759 gen_producer_string (void)
18761 size_t j;
18762 vec<dchar_p> switches = vNULL;
18763 const char *language_string = lang_hooks.name;
18764 char *producer, *tail;
18765 const char *p;
18766 size_t len = dwarf_record_gcc_switches ? 0 : 3;
18767 size_t plen = strlen (language_string) + 1 + strlen (version_string);
18769 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
18770 switch (save_decoded_options[j].opt_index)
18772 case OPT_o:
18773 case OPT_d:
18774 case OPT_dumpbase:
18775 case OPT_dumpdir:
18776 case OPT_auxbase:
18777 case OPT_auxbase_strip:
18778 case OPT_quiet:
18779 case OPT_version:
18780 case OPT_v:
18781 case OPT_w:
18782 case OPT_L:
18783 case OPT_D:
18784 case OPT_I:
18785 case OPT_U:
18786 case OPT_SPECIAL_unknown:
18787 case OPT_SPECIAL_ignore:
18788 case OPT_SPECIAL_program_name:
18789 case OPT_SPECIAL_input_file:
18790 case OPT_grecord_gcc_switches:
18791 case OPT_gno_record_gcc_switches:
18792 case OPT__output_pch_:
18793 case OPT_fdiagnostics_show_location_:
18794 case OPT_fdiagnostics_show_option:
18795 case OPT_fdiagnostics_show_caret:
18796 case OPT_fverbose_asm:
18797 case OPT____:
18798 case OPT__sysroot_:
18799 case OPT_nostdinc:
18800 case OPT_nostdinc__:
18801 /* Ignore these. */
18802 continue;
18803 default:
18804 if (cl_options[save_decoded_options[j].opt_index].flags
18805 & CL_NO_DWARF_RECORD)
18806 continue;
18807 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
18808 == '-');
18809 switch (save_decoded_options[j].canonical_option[0][1])
18811 case 'M':
18812 case 'i':
18813 case 'W':
18814 continue;
18815 case 'f':
18816 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
18817 "dump", 4) == 0)
18818 continue;
18819 break;
18820 default:
18821 break;
18823 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
18824 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
18825 break;
18828 producer = XNEWVEC (char, plen + 1 + len + 1);
18829 tail = producer;
18830 sprintf (tail, "%s %s", language_string, version_string);
18831 tail += plen;
18833 FOR_EACH_VEC_ELT (switches, j, p)
18835 len = strlen (p);
18836 *tail = ' ';
18837 memcpy (tail + 1, p, len);
18838 tail += len + 1;
18841 *tail = '\0';
18842 switches.release ();
18843 return producer;
18846 /* Generate the DIE for the compilation unit. */
18848 static dw_die_ref
18849 gen_compile_unit_die (const char *filename)
18851 dw_die_ref die;
18852 const char *language_string = lang_hooks.name;
18853 int language;
18855 die = new_die (DW_TAG_compile_unit, NULL, NULL);
18857 if (filename)
18859 add_name_attribute (die, filename);
18860 /* Don't add cwd for <built-in>. */
18861 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
18862 add_comp_dir_attribute (die);
18865 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
18867 /* If our producer is LTO try to figure out a common language to use
18868 from the global list of translation units. */
18869 if (strcmp (language_string, "GNU GIMPLE") == 0)
18871 unsigned i;
18872 tree t;
18873 const char *common_lang = NULL;
18875 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
18877 if (!TRANSLATION_UNIT_LANGUAGE (t))
18878 continue;
18879 if (!common_lang)
18880 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
18881 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
18883 else if (strncmp (common_lang, "GNU C", 5) == 0
18884 && strncmp(TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
18885 /* Mixing C and C++ is ok, use C++ in that case. */
18886 common_lang = "GNU C++";
18887 else
18889 /* Fall back to C. */
18890 common_lang = NULL;
18891 break;
18895 if (common_lang)
18896 language_string = common_lang;
18899 language = DW_LANG_C89;
18900 if (strcmp (language_string, "GNU C++") == 0)
18901 language = DW_LANG_C_plus_plus;
18902 else if (strcmp (language_string, "GNU F77") == 0)
18903 language = DW_LANG_Fortran77;
18904 else if (strcmp (language_string, "GNU Pascal") == 0)
18905 language = DW_LANG_Pascal83;
18906 else if (dwarf_version >= 3 || !dwarf_strict)
18908 if (strcmp (language_string, "GNU Ada") == 0)
18909 language = DW_LANG_Ada95;
18910 else if (strcmp (language_string, "GNU Fortran") == 0)
18911 language = DW_LANG_Fortran95;
18912 else if (strcmp (language_string, "GNU Java") == 0)
18913 language = DW_LANG_Java;
18914 else if (strcmp (language_string, "GNU Objective-C") == 0)
18915 language = DW_LANG_ObjC;
18916 else if (strcmp (language_string, "GNU Objective-C++") == 0)
18917 language = DW_LANG_ObjC_plus_plus;
18918 else if (dwarf_version >= 5 || !dwarf_strict)
18920 if (strcmp (language_string, "GNU Go") == 0)
18921 language = DW_LANG_Go;
18924 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
18925 else if (strcmp (language_string, "GNU Fortran") == 0)
18926 language = DW_LANG_Fortran90;
18928 add_AT_unsigned (die, DW_AT_language, language);
18930 switch (language)
18932 case DW_LANG_Fortran77:
18933 case DW_LANG_Fortran90:
18934 case DW_LANG_Fortran95:
18935 /* Fortran has case insensitive identifiers and the front-end
18936 lowercases everything. */
18937 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
18938 break;
18939 default:
18940 /* The default DW_ID_case_sensitive doesn't need to be specified. */
18941 break;
18943 return die;
18946 /* Generate the DIE for a base class. */
18948 static void
18949 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
18951 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
18953 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
18954 add_data_member_location_attribute (die, binfo);
18956 if (BINFO_VIRTUAL_P (binfo))
18957 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
18959 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
18960 children, otherwise the default is DW_ACCESS_public. In DWARF2
18961 the default has always been DW_ACCESS_private. */
18962 if (access == access_public_node)
18964 if (dwarf_version == 2
18965 || context_die->die_tag == DW_TAG_class_type)
18966 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
18968 else if (access == access_protected_node)
18969 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
18970 else if (dwarf_version > 2
18971 && context_die->die_tag != DW_TAG_class_type)
18972 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
18975 /* Generate a DIE for a class member. */
18977 static void
18978 gen_member_die (tree type, dw_die_ref context_die)
18980 tree member;
18981 tree binfo = TYPE_BINFO (type);
18982 dw_die_ref child;
18984 /* If this is not an incomplete type, output descriptions of each of its
18985 members. Note that as we output the DIEs necessary to represent the
18986 members of this record or union type, we will also be trying to output
18987 DIEs to represent the *types* of those members. However the `type'
18988 function (above) will specifically avoid generating type DIEs for member
18989 types *within* the list of member DIEs for this (containing) type except
18990 for those types (of members) which are explicitly marked as also being
18991 members of this (containing) type themselves. The g++ front- end can
18992 force any given type to be treated as a member of some other (containing)
18993 type by setting the TYPE_CONTEXT of the given (member) type to point to
18994 the TREE node representing the appropriate (containing) type. */
18996 /* First output info about the base classes. */
18997 if (binfo)
18999 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
19000 int i;
19001 tree base;
19003 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19004 gen_inheritance_die (base,
19005 (accesses ? (*accesses)[i] : access_public_node),
19006 context_die);
19009 /* Now output info about the data members and type members. */
19010 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19012 /* If we thought we were generating minimal debug info for TYPE
19013 and then changed our minds, some of the member declarations
19014 may have already been defined. Don't define them again, but
19015 do put them in the right order. */
19017 child = lookup_decl_die (member);
19018 if (child)
19019 splice_child_die (context_die, child);
19020 else
19021 gen_decl_die (member, NULL, context_die);
19024 /* Now output info about the function members (if any). */
19025 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19027 /* Don't include clones in the member list. */
19028 if (DECL_ABSTRACT_ORIGIN (member))
19029 continue;
19031 child = lookup_decl_die (member);
19032 if (child)
19033 splice_child_die (context_die, child);
19034 else
19035 gen_decl_die (member, NULL, context_die);
19039 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
19040 is set, we pretend that the type was never defined, so we only get the
19041 member DIEs needed by later specification DIEs. */
19043 static void
19044 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19045 enum debug_info_usage usage)
19047 dw_die_ref type_die = lookup_type_die (type);
19048 dw_die_ref scope_die = 0;
19049 int nested = 0;
19050 int complete = (TYPE_SIZE (type)
19051 && (! TYPE_STUB_DECL (type)
19052 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19053 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19054 complete = complete && should_emit_struct_debug (type, usage);
19056 if (type_die && ! complete)
19057 return;
19059 if (TYPE_CONTEXT (type) != NULL_TREE
19060 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19061 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19062 nested = 1;
19064 scope_die = scope_die_for (type, context_die);
19066 if (! type_die || (nested && is_cu_die (scope_die)))
19067 /* First occurrence of type or toplevel definition of nested class. */
19069 dw_die_ref old_die = type_die;
19071 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
19072 ? record_type_tag (type) : DW_TAG_union_type,
19073 scope_die, type);
19074 equate_type_number_to_die (type, type_die);
19075 if (old_die)
19076 add_AT_specification (type_die, old_die);
19077 else
19078 add_name_attribute (type_die, type_tag (type));
19080 else
19081 remove_AT (type_die, DW_AT_declaration);
19083 /* Generate child dies for template paramaters. */
19084 if (debug_info_level > DINFO_LEVEL_TERSE
19085 && COMPLETE_TYPE_P (type))
19086 schedule_generic_params_dies_gen (type);
19088 /* If this type has been completed, then give it a byte_size attribute and
19089 then give a list of members. */
19090 if (complete && !ns_decl)
19092 /* Prevent infinite recursion in cases where the type of some member of
19093 this type is expressed in terms of this type itself. */
19094 TREE_ASM_WRITTEN (type) = 1;
19095 add_byte_size_attribute (type_die, type);
19096 if (TYPE_STUB_DECL (type) != NULL_TREE)
19098 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19099 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19102 /* If the first reference to this type was as the return type of an
19103 inline function, then it may not have a parent. Fix this now. */
19104 if (type_die->die_parent == NULL)
19105 add_child_die (scope_die, type_die);
19107 push_decl_scope (type);
19108 gen_member_die (type, type_die);
19109 pop_decl_scope ();
19111 add_gnat_descriptive_type_attribute (type_die, type, context_die);
19112 if (TYPE_ARTIFICIAL (type))
19113 add_AT_flag (type_die, DW_AT_artificial, 1);
19115 /* GNU extension: Record what type our vtable lives in. */
19116 if (TYPE_VFIELD (type))
19118 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
19120 gen_type_die (vtype, context_die);
19121 add_AT_die_ref (type_die, DW_AT_containing_type,
19122 lookup_type_die (vtype));
19125 else
19127 add_AT_flag (type_die, DW_AT_declaration, 1);
19129 /* We don't need to do this for function-local types. */
19130 if (TYPE_STUB_DECL (type)
19131 && ! decl_function_context (TYPE_STUB_DECL (type)))
19132 vec_safe_push (incomplete_types, type);
19135 if (get_AT (type_die, DW_AT_name))
19136 add_pubtype (type, type_die);
19139 /* Generate a DIE for a subroutine _type_. */
19141 static void
19142 gen_subroutine_type_die (tree type, dw_die_ref context_die)
19144 tree return_type = TREE_TYPE (type);
19145 dw_die_ref subr_die
19146 = new_die (DW_TAG_subroutine_type,
19147 scope_die_for (type, context_die), type);
19149 equate_type_number_to_die (type, subr_die);
19150 add_prototyped_attribute (subr_die, type);
19151 add_type_attribute (subr_die, return_type, 0, 0, context_die);
19152 gen_formal_types_die (type, subr_die);
19154 if (get_AT (subr_die, DW_AT_name))
19155 add_pubtype (type, subr_die);
19158 /* Generate a DIE for a type definition. */
19160 static void
19161 gen_typedef_die (tree decl, dw_die_ref context_die)
19163 dw_die_ref type_die;
19164 tree origin;
19166 if (TREE_ASM_WRITTEN (decl))
19167 return;
19169 TREE_ASM_WRITTEN (decl) = 1;
19170 type_die = new_die (DW_TAG_typedef, context_die, decl);
19171 origin = decl_ultimate_origin (decl);
19172 if (origin != NULL)
19173 add_abstract_origin_attribute (type_die, origin);
19174 else
19176 tree type;
19178 add_name_and_src_coords_attributes (type_die, decl);
19179 if (DECL_ORIGINAL_TYPE (decl))
19181 type = DECL_ORIGINAL_TYPE (decl);
19183 gcc_assert (type != TREE_TYPE (decl));
19184 equate_type_number_to_die (TREE_TYPE (decl), type_die);
19186 else
19188 type = TREE_TYPE (decl);
19190 if (is_naming_typedef_decl (TYPE_NAME (type)))
19192 /* Here, we are in the case of decl being a typedef naming
19193 an anonymous type, e.g:
19194 typedef struct {...} foo;
19195 In that case TREE_TYPE (decl) is not a typedef variant
19196 type and TYPE_NAME of the anonymous type is set to the
19197 TYPE_DECL of the typedef. This construct is emitted by
19198 the C++ FE.
19200 TYPE is the anonymous struct named by the typedef
19201 DECL. As we need the DW_AT_type attribute of the
19202 DW_TAG_typedef to point to the DIE of TYPE, let's
19203 generate that DIE right away. add_type_attribute
19204 called below will then pick (via lookup_type_die) that
19205 anonymous struct DIE. */
19206 if (!TREE_ASM_WRITTEN (type))
19207 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
19209 /* This is a GNU Extension. We are adding a
19210 DW_AT_linkage_name attribute to the DIE of the
19211 anonymous struct TYPE. The value of that attribute
19212 is the name of the typedef decl naming the anonymous
19213 struct. This greatly eases the work of consumers of
19214 this debug info. */
19215 add_linkage_attr (lookup_type_die (type), decl);
19219 add_type_attribute (type_die, type, TREE_READONLY (decl),
19220 TREE_THIS_VOLATILE (decl), context_die);
19222 if (is_naming_typedef_decl (decl))
19223 /* We want that all subsequent calls to lookup_type_die with
19224 TYPE in argument yield the DW_TAG_typedef we have just
19225 created. */
19226 equate_type_number_to_die (type, type_die);
19228 add_accessibility_attribute (type_die, decl);
19231 if (DECL_ABSTRACT (decl))
19232 equate_decl_number_to_die (decl, type_die);
19234 if (get_AT (type_die, DW_AT_name))
19235 add_pubtype (decl, type_die);
19238 /* Generate a DIE for a struct, class, enum or union type. */
19240 static void
19241 gen_tagged_type_die (tree type,
19242 dw_die_ref context_die,
19243 enum debug_info_usage usage)
19245 int need_pop;
19247 if (type == NULL_TREE
19248 || !is_tagged_type (type))
19249 return;
19251 /* If this is a nested type whose containing class hasn't been written
19252 out yet, writing it out will cover this one, too. This does not apply
19253 to instantiations of member class templates; they need to be added to
19254 the containing class as they are generated. FIXME: This hurts the
19255 idea of combining type decls from multiple TUs, since we can't predict
19256 what set of template instantiations we'll get. */
19257 if (TYPE_CONTEXT (type)
19258 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19259 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
19261 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
19263 if (TREE_ASM_WRITTEN (type))
19264 return;
19266 /* If that failed, attach ourselves to the stub. */
19267 push_decl_scope (TYPE_CONTEXT (type));
19268 context_die = lookup_type_die (TYPE_CONTEXT (type));
19269 need_pop = 1;
19271 else if (TYPE_CONTEXT (type) != NULL_TREE
19272 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
19274 /* If this type is local to a function that hasn't been written
19275 out yet, use a NULL context for now; it will be fixed up in
19276 decls_for_scope. */
19277 context_die = lookup_decl_die (TYPE_CONTEXT (type));
19278 /* A declaration DIE doesn't count; nested types need to go in the
19279 specification. */
19280 if (context_die && is_declaration_die (context_die))
19281 context_die = NULL;
19282 need_pop = 0;
19284 else
19286 context_die = declare_in_namespace (type, context_die);
19287 need_pop = 0;
19290 if (TREE_CODE (type) == ENUMERAL_TYPE)
19292 /* This might have been written out by the call to
19293 declare_in_namespace. */
19294 if (!TREE_ASM_WRITTEN (type))
19295 gen_enumeration_type_die (type, context_die);
19297 else
19298 gen_struct_or_union_type_die (type, context_die, usage);
19300 if (need_pop)
19301 pop_decl_scope ();
19303 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
19304 it up if it is ever completed. gen_*_type_die will set it for us
19305 when appropriate. */
19308 /* Generate a type description DIE. */
19310 static void
19311 gen_type_die_with_usage (tree type, dw_die_ref context_die,
19312 enum debug_info_usage usage)
19314 struct array_descr_info info;
19316 if (type == NULL_TREE || type == error_mark_node)
19317 return;
19319 if (TYPE_NAME (type) != NULL_TREE
19320 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
19321 && is_redundant_typedef (TYPE_NAME (type))
19322 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
19323 /* The DECL of this type is a typedef we don't want to emit debug
19324 info for but we want debug info for its underlying typedef.
19325 This can happen for e.g, the injected-class-name of a C++
19326 type. */
19327 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
19329 /* If TYPE is a typedef type variant, let's generate debug info
19330 for the parent typedef which TYPE is a type of. */
19331 if (typedef_variant_p (type))
19333 if (TREE_ASM_WRITTEN (type))
19334 return;
19336 /* Prevent broken recursion; we can't hand off to the same type. */
19337 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
19339 /* Give typedefs the right scope. */
19340 context_die = scope_die_for (type, context_die);
19342 TREE_ASM_WRITTEN (type) = 1;
19344 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19345 return;
19348 /* If type is an anonymous tagged type named by a typedef, let's
19349 generate debug info for the typedef. */
19350 if (is_naming_typedef_decl (TYPE_NAME (type)))
19352 /* Use the DIE of the containing namespace as the parent DIE of
19353 the type description DIE we want to generate. */
19354 if (DECL_CONTEXT (TYPE_NAME (type))
19355 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
19356 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
19358 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19359 return;
19362 /* If this is an array type with hidden descriptor, handle it first. */
19363 if (!TREE_ASM_WRITTEN (type)
19364 && lang_hooks.types.get_array_descr_info
19365 && lang_hooks.types.get_array_descr_info (type, &info)
19366 && (dwarf_version >= 3 || !dwarf_strict))
19368 gen_descr_array_type_die (type, &info, context_die);
19369 TREE_ASM_WRITTEN (type) = 1;
19370 return;
19373 /* We are going to output a DIE to represent the unqualified version
19374 of this type (i.e. without any const or volatile qualifiers) so
19375 get the main variant (i.e. the unqualified version) of this type
19376 now. (Vectors are special because the debugging info is in the
19377 cloned type itself). */
19378 if (TREE_CODE (type) != VECTOR_TYPE)
19379 type = type_main_variant (type);
19381 if (TREE_ASM_WRITTEN (type))
19382 return;
19384 switch (TREE_CODE (type))
19386 case ERROR_MARK:
19387 break;
19389 case POINTER_TYPE:
19390 case REFERENCE_TYPE:
19391 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
19392 ensures that the gen_type_die recursion will terminate even if the
19393 type is recursive. Recursive types are possible in Ada. */
19394 /* ??? We could perhaps do this for all types before the switch
19395 statement. */
19396 TREE_ASM_WRITTEN (type) = 1;
19398 /* For these types, all that is required is that we output a DIE (or a
19399 set of DIEs) to represent the "basis" type. */
19400 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19401 DINFO_USAGE_IND_USE);
19402 break;
19404 case OFFSET_TYPE:
19405 /* This code is used for C++ pointer-to-data-member types.
19406 Output a description of the relevant class type. */
19407 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
19408 DINFO_USAGE_IND_USE);
19410 /* Output a description of the type of the object pointed to. */
19411 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19412 DINFO_USAGE_IND_USE);
19414 /* Now output a DIE to represent this pointer-to-data-member type
19415 itself. */
19416 gen_ptr_to_mbr_type_die (type, context_die);
19417 break;
19419 case FUNCTION_TYPE:
19420 /* Force out return type (in case it wasn't forced out already). */
19421 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19422 DINFO_USAGE_DIR_USE);
19423 gen_subroutine_type_die (type, context_die);
19424 break;
19426 case METHOD_TYPE:
19427 /* Force out return type (in case it wasn't forced out already). */
19428 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19429 DINFO_USAGE_DIR_USE);
19430 gen_subroutine_type_die (type, context_die);
19431 break;
19433 case ARRAY_TYPE:
19434 gen_array_type_die (type, context_die);
19435 break;
19437 case VECTOR_TYPE:
19438 gen_array_type_die (type, context_die);
19439 break;
19441 case ENUMERAL_TYPE:
19442 case RECORD_TYPE:
19443 case UNION_TYPE:
19444 case QUAL_UNION_TYPE:
19445 gen_tagged_type_die (type, context_die, usage);
19446 return;
19448 case VOID_TYPE:
19449 case INTEGER_TYPE:
19450 case REAL_TYPE:
19451 case FIXED_POINT_TYPE:
19452 case COMPLEX_TYPE:
19453 case BOOLEAN_TYPE:
19454 /* No DIEs needed for fundamental types. */
19455 break;
19457 case NULLPTR_TYPE:
19458 case LANG_TYPE:
19459 /* Just use DW_TAG_unspecified_type. */
19461 dw_die_ref type_die = lookup_type_die (type);
19462 if (type_die == NULL)
19464 tree name = TYPE_NAME (type);
19465 if (TREE_CODE (name) == TYPE_DECL)
19466 name = DECL_NAME (name);
19467 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
19468 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
19469 equate_type_number_to_die (type, type_die);
19472 break;
19474 default:
19475 gcc_unreachable ();
19478 TREE_ASM_WRITTEN (type) = 1;
19481 static void
19482 gen_type_die (tree type, dw_die_ref context_die)
19484 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
19487 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
19488 things which are local to the given block. */
19490 static void
19491 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
19493 int must_output_die = 0;
19494 bool inlined_func;
19496 /* Ignore blocks that are NULL. */
19497 if (stmt == NULL_TREE)
19498 return;
19500 inlined_func = inlined_function_outer_scope_p (stmt);
19502 /* If the block is one fragment of a non-contiguous block, do not
19503 process the variables, since they will have been done by the
19504 origin block. Do process subblocks. */
19505 if (BLOCK_FRAGMENT_ORIGIN (stmt))
19507 tree sub;
19509 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
19510 gen_block_die (sub, context_die, depth + 1);
19512 return;
19515 /* Determine if we need to output any Dwarf DIEs at all to represent this
19516 block. */
19517 if (inlined_func)
19518 /* The outer scopes for inlinings *must* always be represented. We
19519 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
19520 must_output_die = 1;
19521 else
19523 /* Determine if this block directly contains any "significant"
19524 local declarations which we will need to output DIEs for. */
19525 if (debug_info_level > DINFO_LEVEL_TERSE)
19526 /* We are not in terse mode so *any* local declaration counts
19527 as being a "significant" one. */
19528 must_output_die = ((BLOCK_VARS (stmt) != NULL
19529 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
19530 && (TREE_USED (stmt)
19531 || TREE_ASM_WRITTEN (stmt)
19532 || BLOCK_ABSTRACT (stmt)));
19533 else if ((TREE_USED (stmt)
19534 || TREE_ASM_WRITTEN (stmt)
19535 || BLOCK_ABSTRACT (stmt))
19536 && !dwarf2out_ignore_block (stmt))
19537 must_output_die = 1;
19540 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
19541 DIE for any block which contains no significant local declarations at
19542 all. Rather, in such cases we just call `decls_for_scope' so that any
19543 needed Dwarf info for any sub-blocks will get properly generated. Note
19544 that in terse mode, our definition of what constitutes a "significant"
19545 local declaration gets restricted to include only inlined function
19546 instances and local (nested) function definitions. */
19547 if (must_output_die)
19549 if (inlined_func)
19551 /* If STMT block is abstract, that means we have been called
19552 indirectly from dwarf2out_abstract_function.
19553 That function rightfully marks the descendent blocks (of
19554 the abstract function it is dealing with) as being abstract,
19555 precisely to prevent us from emitting any
19556 DW_TAG_inlined_subroutine DIE as a descendent
19557 of an abstract function instance. So in that case, we should
19558 not call gen_inlined_subroutine_die.
19560 Later though, when cgraph asks dwarf2out to emit info
19561 for the concrete instance of the function decl into which
19562 the concrete instance of STMT got inlined, the later will lead
19563 to the generation of a DW_TAG_inlined_subroutine DIE. */
19564 if (! BLOCK_ABSTRACT (stmt))
19565 gen_inlined_subroutine_die (stmt, context_die, depth);
19567 else
19568 gen_lexical_block_die (stmt, context_die, depth);
19570 else
19571 decls_for_scope (stmt, context_die, depth);
19574 /* Process variable DECL (or variable with origin ORIGIN) within
19575 block STMT and add it to CONTEXT_DIE. */
19576 static void
19577 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
19579 dw_die_ref die;
19580 tree decl_or_origin = decl ? decl : origin;
19582 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
19583 die = lookup_decl_die (decl_or_origin);
19584 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
19585 && TYPE_DECL_IS_STUB (decl_or_origin))
19586 die = lookup_type_die (TREE_TYPE (decl_or_origin));
19587 else
19588 die = NULL;
19590 if (die != NULL && die->die_parent == NULL)
19591 add_child_die (context_die, die);
19592 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
19593 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
19594 stmt, context_die);
19595 else
19596 gen_decl_die (decl, origin, context_die);
19599 /* Generate all of the decls declared within a given scope and (recursively)
19600 all of its sub-blocks. */
19602 static void
19603 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
19605 tree decl;
19606 unsigned int i;
19607 tree subblocks;
19609 /* Ignore NULL blocks. */
19610 if (stmt == NULL_TREE)
19611 return;
19613 /* Output the DIEs to represent all of the data objects and typedefs
19614 declared directly within this block but not within any nested
19615 sub-blocks. Also, nested function and tag DIEs have been
19616 generated with a parent of NULL; fix that up now. */
19617 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
19618 process_scope_var (stmt, decl, NULL_TREE, context_die);
19619 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
19620 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
19621 context_die);
19623 /* If we're at -g1, we're not interested in subblocks. */
19624 if (debug_info_level <= DINFO_LEVEL_TERSE)
19625 return;
19627 /* Output the DIEs to represent all sub-blocks (and the items declared
19628 therein) of this block. */
19629 for (subblocks = BLOCK_SUBBLOCKS (stmt);
19630 subblocks != NULL;
19631 subblocks = BLOCK_CHAIN (subblocks))
19632 gen_block_die (subblocks, context_die, depth + 1);
19635 /* Is this a typedef we can avoid emitting? */
19637 static inline int
19638 is_redundant_typedef (const_tree decl)
19640 if (TYPE_DECL_IS_STUB (decl))
19641 return 1;
19643 if (DECL_ARTIFICIAL (decl)
19644 && DECL_CONTEXT (decl)
19645 && is_tagged_type (DECL_CONTEXT (decl))
19646 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
19647 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
19648 /* Also ignore the artificial member typedef for the class name. */
19649 return 1;
19651 return 0;
19654 /* Return TRUE if TYPE is a typedef that names a type for linkage
19655 purposes. This kind of typedefs is produced by the C++ FE for
19656 constructs like:
19658 typedef struct {...} foo;
19660 In that case, there is no typedef variant type produced for foo.
19661 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
19662 struct type. */
19664 static bool
19665 is_naming_typedef_decl (const_tree decl)
19667 if (decl == NULL_TREE
19668 || TREE_CODE (decl) != TYPE_DECL
19669 || !is_tagged_type (TREE_TYPE (decl))
19670 || DECL_IS_BUILTIN (decl)
19671 || is_redundant_typedef (decl)
19672 /* It looks like Ada produces TYPE_DECLs that are very similar
19673 to C++ naming typedefs but that have different
19674 semantics. Let's be specific to c++ for now. */
19675 || !is_cxx ())
19676 return FALSE;
19678 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
19679 && TYPE_NAME (TREE_TYPE (decl)) == decl
19680 && (TYPE_STUB_DECL (TREE_TYPE (decl))
19681 != TYPE_NAME (TREE_TYPE (decl))));
19684 /* Returns the DIE for a context. */
19686 static inline dw_die_ref
19687 get_context_die (tree context)
19689 if (context)
19691 /* Find die that represents this context. */
19692 if (TYPE_P (context))
19694 context = TYPE_MAIN_VARIANT (context);
19695 return strip_naming_typedef (context, force_type_die (context));
19697 else
19698 return force_decl_die (context);
19700 return comp_unit_die ();
19703 /* Returns the DIE for decl. A DIE will always be returned. */
19705 static dw_die_ref
19706 force_decl_die (tree decl)
19708 dw_die_ref decl_die;
19709 unsigned saved_external_flag;
19710 tree save_fn = NULL_TREE;
19711 decl_die = lookup_decl_die (decl);
19712 if (!decl_die)
19714 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
19716 decl_die = lookup_decl_die (decl);
19717 if (decl_die)
19718 return decl_die;
19720 switch (TREE_CODE (decl))
19722 case FUNCTION_DECL:
19723 /* Clear current_function_decl, so that gen_subprogram_die thinks
19724 that this is a declaration. At this point, we just want to force
19725 declaration die. */
19726 save_fn = current_function_decl;
19727 current_function_decl = NULL_TREE;
19728 gen_subprogram_die (decl, context_die);
19729 current_function_decl = save_fn;
19730 break;
19732 case VAR_DECL:
19733 /* Set external flag to force declaration die. Restore it after
19734 gen_decl_die() call. */
19735 saved_external_flag = DECL_EXTERNAL (decl);
19736 DECL_EXTERNAL (decl) = 1;
19737 gen_decl_die (decl, NULL, context_die);
19738 DECL_EXTERNAL (decl) = saved_external_flag;
19739 break;
19741 case NAMESPACE_DECL:
19742 if (dwarf_version >= 3 || !dwarf_strict)
19743 dwarf2out_decl (decl);
19744 else
19745 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
19746 decl_die = comp_unit_die ();
19747 break;
19749 case TRANSLATION_UNIT_DECL:
19750 decl_die = comp_unit_die ();
19751 break;
19753 default:
19754 gcc_unreachable ();
19757 /* We should be able to find the DIE now. */
19758 if (!decl_die)
19759 decl_die = lookup_decl_die (decl);
19760 gcc_assert (decl_die);
19763 return decl_die;
19766 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
19767 always returned. */
19769 static dw_die_ref
19770 force_type_die (tree type)
19772 dw_die_ref type_die;
19774 type_die = lookup_type_die (type);
19775 if (!type_die)
19777 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
19779 type_die = modified_type_die (type, TYPE_READONLY (type),
19780 TYPE_VOLATILE (type), context_die);
19781 gcc_assert (type_die);
19783 return type_die;
19786 /* Force out any required namespaces to be able to output DECL,
19787 and return the new context_die for it, if it's changed. */
19789 static dw_die_ref
19790 setup_namespace_context (tree thing, dw_die_ref context_die)
19792 tree context = (DECL_P (thing)
19793 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
19794 if (context && TREE_CODE (context) == NAMESPACE_DECL)
19795 /* Force out the namespace. */
19796 context_die = force_decl_die (context);
19798 return context_die;
19801 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
19802 type) within its namespace, if appropriate.
19804 For compatibility with older debuggers, namespace DIEs only contain
19805 declarations; all definitions are emitted at CU scope. */
19807 static dw_die_ref
19808 declare_in_namespace (tree thing, dw_die_ref context_die)
19810 dw_die_ref ns_context;
19812 if (debug_info_level <= DINFO_LEVEL_TERSE)
19813 return context_die;
19815 /* If this decl is from an inlined function, then don't try to emit it in its
19816 namespace, as we will get confused. It would have already been emitted
19817 when the abstract instance of the inline function was emitted anyways. */
19818 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
19819 return context_die;
19821 ns_context = setup_namespace_context (thing, context_die);
19823 if (ns_context != context_die)
19825 if (is_fortran ())
19826 return ns_context;
19827 if (DECL_P (thing))
19828 gen_decl_die (thing, NULL, ns_context);
19829 else
19830 gen_type_die (thing, ns_context);
19832 return context_die;
19835 /* Generate a DIE for a namespace or namespace alias. */
19837 static void
19838 gen_namespace_die (tree decl, dw_die_ref context_die)
19840 dw_die_ref namespace_die;
19842 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
19843 they are an alias of. */
19844 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
19846 /* Output a real namespace or module. */
19847 context_die = setup_namespace_context (decl, comp_unit_die ());
19848 namespace_die = new_die (is_fortran ()
19849 ? DW_TAG_module : DW_TAG_namespace,
19850 context_die, decl);
19851 /* For Fortran modules defined in different CU don't add src coords. */
19852 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
19854 const char *name = dwarf2_name (decl, 0);
19855 if (name)
19856 add_name_attribute (namespace_die, name);
19858 else
19859 add_name_and_src_coords_attributes (namespace_die, decl);
19860 if (DECL_EXTERNAL (decl))
19861 add_AT_flag (namespace_die, DW_AT_declaration, 1);
19862 equate_decl_number_to_die (decl, namespace_die);
19864 else
19866 /* Output a namespace alias. */
19868 /* Force out the namespace we are an alias of, if necessary. */
19869 dw_die_ref origin_die
19870 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
19872 if (DECL_FILE_SCOPE_P (decl)
19873 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
19874 context_die = setup_namespace_context (decl, comp_unit_die ());
19875 /* Now create the namespace alias DIE. */
19876 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
19877 add_name_and_src_coords_attributes (namespace_die, decl);
19878 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
19879 equate_decl_number_to_die (decl, namespace_die);
19881 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
19882 if (want_pubnames ())
19883 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
19886 /* Generate Dwarf debug information for a decl described by DECL.
19887 The return value is currently only meaningful for PARM_DECLs,
19888 for all other decls it returns NULL. */
19890 static dw_die_ref
19891 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
19893 tree decl_or_origin = decl ? decl : origin;
19894 tree class_origin = NULL, ultimate_origin;
19896 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
19897 return NULL;
19899 switch (TREE_CODE (decl_or_origin))
19901 case ERROR_MARK:
19902 break;
19904 case CONST_DECL:
19905 if (!is_fortran () && !is_ada ())
19907 /* The individual enumerators of an enum type get output when we output
19908 the Dwarf representation of the relevant enum type itself. */
19909 break;
19912 /* Emit its type. */
19913 gen_type_die (TREE_TYPE (decl), context_die);
19915 /* And its containing namespace. */
19916 context_die = declare_in_namespace (decl, context_die);
19918 gen_const_die (decl, context_die);
19919 break;
19921 case FUNCTION_DECL:
19922 /* Don't output any DIEs to represent mere function declarations,
19923 unless they are class members or explicit block externs. */
19924 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
19925 && DECL_FILE_SCOPE_P (decl_or_origin)
19926 && (current_function_decl == NULL_TREE
19927 || DECL_ARTIFICIAL (decl_or_origin)))
19928 break;
19930 #if 0
19931 /* FIXME */
19932 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
19933 on local redeclarations of global functions. That seems broken. */
19934 if (current_function_decl != decl)
19935 /* This is only a declaration. */;
19936 #endif
19938 /* If we're emitting a clone, emit info for the abstract instance. */
19939 if (origin || DECL_ORIGIN (decl) != decl)
19940 dwarf2out_abstract_function (origin
19941 ? DECL_ORIGIN (origin)
19942 : DECL_ABSTRACT_ORIGIN (decl));
19944 /* If we're emitting an out-of-line copy of an inline function,
19945 emit info for the abstract instance and set up to refer to it. */
19946 else if (cgraph_function_possibly_inlined_p (decl)
19947 && ! DECL_ABSTRACT (decl)
19948 && ! class_or_namespace_scope_p (context_die)
19949 /* dwarf2out_abstract_function won't emit a die if this is just
19950 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
19951 that case, because that works only if we have a die. */
19952 && DECL_INITIAL (decl) != NULL_TREE)
19954 dwarf2out_abstract_function (decl);
19955 set_decl_origin_self (decl);
19958 /* Otherwise we're emitting the primary DIE for this decl. */
19959 else if (debug_info_level > DINFO_LEVEL_TERSE)
19961 /* Before we describe the FUNCTION_DECL itself, make sure that we
19962 have its containing type. */
19963 if (!origin)
19964 origin = decl_class_context (decl);
19965 if (origin != NULL_TREE)
19966 gen_type_die (origin, context_die);
19968 /* And its return type. */
19969 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
19971 /* And its virtual context. */
19972 if (DECL_VINDEX (decl) != NULL_TREE)
19973 gen_type_die (DECL_CONTEXT (decl), context_die);
19975 /* Make sure we have a member DIE for decl. */
19976 if (origin != NULL_TREE)
19977 gen_type_die_for_member (origin, decl, context_die);
19979 /* And its containing namespace. */
19980 context_die = declare_in_namespace (decl, context_die);
19983 /* Now output a DIE to represent the function itself. */
19984 if (decl)
19985 gen_subprogram_die (decl, context_die);
19986 break;
19988 case TYPE_DECL:
19989 /* If we are in terse mode, don't generate any DIEs to represent any
19990 actual typedefs. */
19991 if (debug_info_level <= DINFO_LEVEL_TERSE)
19992 break;
19994 /* In the special case of a TYPE_DECL node representing the declaration
19995 of some type tag, if the given TYPE_DECL is marked as having been
19996 instantiated from some other (original) TYPE_DECL node (e.g. one which
19997 was generated within the original definition of an inline function) we
19998 used to generate a special (abbreviated) DW_TAG_structure_type,
19999 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
20000 should be actually referencing those DIEs, as variable DIEs with that
20001 type would be emitted already in the abstract origin, so it was always
20002 removed during unused type prunning. Don't add anything in this
20003 case. */
20004 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20005 break;
20007 if (is_redundant_typedef (decl))
20008 gen_type_die (TREE_TYPE (decl), context_die);
20009 else
20010 /* Output a DIE to represent the typedef itself. */
20011 gen_typedef_die (decl, context_die);
20012 break;
20014 case LABEL_DECL:
20015 if (debug_info_level >= DINFO_LEVEL_NORMAL)
20016 gen_label_die (decl, context_die);
20017 break;
20019 case VAR_DECL:
20020 case RESULT_DECL:
20021 /* If we are in terse mode, don't generate any DIEs to represent any
20022 variable declarations or definitions. */
20023 if (debug_info_level <= DINFO_LEVEL_TERSE)
20024 break;
20026 /* Output any DIEs that are needed to specify the type of this data
20027 object. */
20028 if (decl_by_reference_p (decl_or_origin))
20029 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20030 else
20031 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20033 /* And its containing type. */
20034 class_origin = decl_class_context (decl_or_origin);
20035 if (class_origin != NULL_TREE)
20036 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
20038 /* And its containing namespace. */
20039 context_die = declare_in_namespace (decl_or_origin, context_die);
20041 /* Now output the DIE to represent the data object itself. This gets
20042 complicated because of the possibility that the VAR_DECL really
20043 represents an inlined instance of a formal parameter for an inline
20044 function. */
20045 ultimate_origin = decl_ultimate_origin (decl_or_origin);
20046 if (ultimate_origin != NULL_TREE
20047 && TREE_CODE (ultimate_origin) == PARM_DECL)
20048 gen_formal_parameter_die (decl, origin,
20049 true /* Emit name attribute. */,
20050 context_die);
20051 else
20052 gen_variable_die (decl, origin, context_die);
20053 break;
20055 case FIELD_DECL:
20056 /* Ignore the nameless fields that are used to skip bits but handle C++
20057 anonymous unions and structs. */
20058 if (DECL_NAME (decl) != NULL_TREE
20059 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
20060 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
20062 gen_type_die (member_declared_type (decl), context_die);
20063 gen_field_die (decl, context_die);
20065 break;
20067 case PARM_DECL:
20068 if (DECL_BY_REFERENCE (decl_or_origin))
20069 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20070 else
20071 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20072 return gen_formal_parameter_die (decl, origin,
20073 true /* Emit name attribute. */,
20074 context_die);
20076 case NAMESPACE_DECL:
20077 case IMPORTED_DECL:
20078 if (dwarf_version >= 3 || !dwarf_strict)
20079 gen_namespace_die (decl, context_die);
20080 break;
20082 default:
20083 /* Probably some frontend-internal decl. Assume we don't care. */
20084 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
20085 break;
20088 return NULL;
20091 /* Output debug information for global decl DECL. Called from toplev.c after
20092 compilation proper has finished. */
20094 static void
20095 dwarf2out_global_decl (tree decl)
20097 /* Output DWARF2 information for file-scope tentative data object
20098 declarations, file-scope (extern) function declarations (which
20099 had no corresponding body) and file-scope tagged type declarations
20100 and definitions which have not yet been forced out. */
20101 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
20102 dwarf2out_decl (decl);
20105 /* Output debug information for type decl DECL. Called from toplev.c
20106 and from language front ends (to record built-in types). */
20107 static void
20108 dwarf2out_type_decl (tree decl, int local)
20110 if (!local)
20111 dwarf2out_decl (decl);
20114 /* Output debug information for imported module or decl DECL.
20115 NAME is non-NULL name in the lexical block if the decl has been renamed.
20116 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
20117 that DECL belongs to.
20118 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
20119 static void
20120 dwarf2out_imported_module_or_decl_1 (tree decl,
20121 tree name,
20122 tree lexical_block,
20123 dw_die_ref lexical_block_die)
20125 expanded_location xloc;
20126 dw_die_ref imported_die = NULL;
20127 dw_die_ref at_import_die;
20129 if (TREE_CODE (decl) == IMPORTED_DECL)
20131 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
20132 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
20133 gcc_assert (decl);
20135 else
20136 xloc = expand_location (input_location);
20138 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
20140 at_import_die = force_type_die (TREE_TYPE (decl));
20141 /* For namespace N { typedef void T; } using N::T; base_type_die
20142 returns NULL, but DW_TAG_imported_declaration requires
20143 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
20144 if (!at_import_die)
20146 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
20147 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
20148 at_import_die = lookup_type_die (TREE_TYPE (decl));
20149 gcc_assert (at_import_die);
20152 else
20154 at_import_die = lookup_decl_die (decl);
20155 if (!at_import_die)
20157 /* If we're trying to avoid duplicate debug info, we may not have
20158 emitted the member decl for this field. Emit it now. */
20159 if (TREE_CODE (decl) == FIELD_DECL)
20161 tree type = DECL_CONTEXT (decl);
20163 if (TYPE_CONTEXT (type)
20164 && TYPE_P (TYPE_CONTEXT (type))
20165 && !should_emit_struct_debug (TYPE_CONTEXT (type),
20166 DINFO_USAGE_DIR_USE))
20167 return;
20168 gen_type_die_for_member (type, decl,
20169 get_context_die (TYPE_CONTEXT (type)));
20171 at_import_die = force_decl_die (decl);
20175 if (TREE_CODE (decl) == NAMESPACE_DECL)
20177 if (dwarf_version >= 3 || !dwarf_strict)
20178 imported_die = new_die (DW_TAG_imported_module,
20179 lexical_block_die,
20180 lexical_block);
20181 else
20182 return;
20184 else
20185 imported_die = new_die (DW_TAG_imported_declaration,
20186 lexical_block_die,
20187 lexical_block);
20189 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
20190 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
20191 if (name)
20192 add_AT_string (imported_die, DW_AT_name,
20193 IDENTIFIER_POINTER (name));
20194 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
20197 /* Output debug information for imported module or decl DECL.
20198 NAME is non-NULL name in context if the decl has been renamed.
20199 CHILD is true if decl is one of the renamed decls as part of
20200 importing whole module. */
20202 static void
20203 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
20204 bool child)
20206 /* dw_die_ref at_import_die; */
20207 dw_die_ref scope_die;
20209 if (debug_info_level <= DINFO_LEVEL_TERSE)
20210 return;
20212 gcc_assert (decl);
20214 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
20215 We need decl DIE for reference and scope die. First, get DIE for the decl
20216 itself. */
20218 /* Get the scope die for decl context. Use comp_unit_die for global module
20219 or decl. If die is not found for non globals, force new die. */
20220 if (context
20221 && TYPE_P (context)
20222 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
20223 return;
20225 if (!(dwarf_version >= 3 || !dwarf_strict))
20226 return;
20228 scope_die = get_context_die (context);
20230 if (child)
20232 gcc_assert (scope_die->die_child);
20233 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
20234 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
20235 scope_die = scope_die->die_child;
20238 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
20239 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
20243 /* Write the debugging output for DECL. */
20245 void
20246 dwarf2out_decl (tree decl)
20248 dw_die_ref context_die = comp_unit_die ();
20250 switch (TREE_CODE (decl))
20252 case ERROR_MARK:
20253 return;
20255 case FUNCTION_DECL:
20256 /* What we would really like to do here is to filter out all mere
20257 file-scope declarations of file-scope functions which are never
20258 referenced later within this translation unit (and keep all of ones
20259 that *are* referenced later on) but we aren't clairvoyant, so we have
20260 no idea which functions will be referenced in the future (i.e. later
20261 on within the current translation unit). So here we just ignore all
20262 file-scope function declarations which are not also definitions. If
20263 and when the debugger needs to know something about these functions,
20264 it will have to hunt around and find the DWARF information associated
20265 with the definition of the function.
20267 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
20268 nodes represent definitions and which ones represent mere
20269 declarations. We have to check DECL_INITIAL instead. That's because
20270 the C front-end supports some weird semantics for "extern inline"
20271 function definitions. These can get inlined within the current
20272 translation unit (and thus, we need to generate Dwarf info for their
20273 abstract instances so that the Dwarf info for the concrete inlined
20274 instances can have something to refer to) but the compiler never
20275 generates any out-of-lines instances of such things (despite the fact
20276 that they *are* definitions).
20278 The important point is that the C front-end marks these "extern
20279 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
20280 them anyway. Note that the C++ front-end also plays some similar games
20281 for inline function definitions appearing within include files which
20282 also contain `#pragma interface' pragmas.
20284 If we are called from dwarf2out_abstract_function output a DIE
20285 anyway. We can end up here this way with early inlining and LTO
20286 where the inlined function is output in a different LTRANS unit
20287 or not at all. */
20288 if (DECL_INITIAL (decl) == NULL_TREE
20289 && ! DECL_ABSTRACT (decl))
20290 return;
20292 /* If we're a nested function, initially use a parent of NULL; if we're
20293 a plain function, this will be fixed up in decls_for_scope. If
20294 we're a method, it will be ignored, since we already have a DIE. */
20295 if (decl_function_context (decl)
20296 /* But if we're in terse mode, we don't care about scope. */
20297 && debug_info_level > DINFO_LEVEL_TERSE)
20298 context_die = NULL;
20299 break;
20301 case VAR_DECL:
20302 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
20303 declaration and if the declaration was never even referenced from
20304 within this entire compilation unit. We suppress these DIEs in
20305 order to save space in the .debug section (by eliminating entries
20306 which are probably useless). Note that we must not suppress
20307 block-local extern declarations (whether used or not) because that
20308 would screw-up the debugger's name lookup mechanism and cause it to
20309 miss things which really ought to be in scope at a given point. */
20310 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
20311 return;
20313 /* For local statics lookup proper context die. */
20314 if (TREE_STATIC (decl)
20315 && DECL_CONTEXT (decl)
20316 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
20317 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20319 /* If we are in terse mode, don't generate any DIEs to represent any
20320 variable declarations or definitions. */
20321 if (debug_info_level <= DINFO_LEVEL_TERSE)
20322 return;
20323 break;
20325 case CONST_DECL:
20326 if (debug_info_level <= DINFO_LEVEL_TERSE)
20327 return;
20328 if (!is_fortran () && !is_ada ())
20329 return;
20330 if (TREE_STATIC (decl) && decl_function_context (decl))
20331 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20332 break;
20334 case NAMESPACE_DECL:
20335 case IMPORTED_DECL:
20336 if (debug_info_level <= DINFO_LEVEL_TERSE)
20337 return;
20338 if (lookup_decl_die (decl) != NULL)
20339 return;
20340 break;
20342 case TYPE_DECL:
20343 /* Don't emit stubs for types unless they are needed by other DIEs. */
20344 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
20345 return;
20347 /* Don't bother trying to generate any DIEs to represent any of the
20348 normal built-in types for the language we are compiling. */
20349 if (DECL_IS_BUILTIN (decl))
20350 return;
20352 /* If we are in terse mode, don't generate any DIEs for types. */
20353 if (debug_info_level <= DINFO_LEVEL_TERSE)
20354 return;
20356 /* If we're a function-scope tag, initially use a parent of NULL;
20357 this will be fixed up in decls_for_scope. */
20358 if (decl_function_context (decl))
20359 context_die = NULL;
20361 break;
20363 default:
20364 return;
20367 gen_decl_die (decl, NULL, context_die);
20370 /* Write the debugging output for DECL. */
20372 static void
20373 dwarf2out_function_decl (tree decl)
20375 dwarf2out_decl (decl);
20376 call_arg_locations = NULL;
20377 call_arg_loc_last = NULL;
20378 call_site_count = -1;
20379 tail_call_site_count = -1;
20380 block_map.release ();
20381 htab_empty (decl_loc_table);
20382 htab_empty (cached_dw_loc_list_table);
20385 /* Output a marker (i.e. a label) for the beginning of the generated code for
20386 a lexical block. */
20388 static void
20389 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
20390 unsigned int blocknum)
20392 switch_to_section (current_function_section ());
20393 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
20396 /* Output a marker (i.e. a label) for the end of the generated code for a
20397 lexical block. */
20399 static void
20400 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
20402 switch_to_section (current_function_section ());
20403 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
20406 /* Returns nonzero if it is appropriate not to emit any debugging
20407 information for BLOCK, because it doesn't contain any instructions.
20409 Don't allow this for blocks with nested functions or local classes
20410 as we would end up with orphans, and in the presence of scheduling
20411 we may end up calling them anyway. */
20413 static bool
20414 dwarf2out_ignore_block (const_tree block)
20416 tree decl;
20417 unsigned int i;
20419 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
20420 if (TREE_CODE (decl) == FUNCTION_DECL
20421 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20422 return 0;
20423 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
20425 decl = BLOCK_NONLOCALIZED_VAR (block, i);
20426 if (TREE_CODE (decl) == FUNCTION_DECL
20427 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20428 return 0;
20431 return 1;
20434 /* Hash table routines for file_hash. */
20436 static int
20437 file_table_eq (const void *p1_p, const void *p2_p)
20439 const struct dwarf_file_data *const p1 =
20440 (const struct dwarf_file_data *) p1_p;
20441 const char *const p2 = (const char *) p2_p;
20442 return filename_cmp (p1->filename, p2) == 0;
20445 static hashval_t
20446 file_table_hash (const void *p_p)
20448 const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
20449 return htab_hash_string (p->filename);
20452 /* Lookup FILE_NAME (in the list of filenames that we know about here in
20453 dwarf2out.c) and return its "index". The index of each (known) filename is
20454 just a unique number which is associated with only that one filename. We
20455 need such numbers for the sake of generating labels (in the .debug_sfnames
20456 section) and references to those files numbers (in the .debug_srcinfo
20457 and.debug_macinfo sections). If the filename given as an argument is not
20458 found in our current list, add it to the list and assign it the next
20459 available unique index number. In order to speed up searches, we remember
20460 the index of the filename was looked up last. This handles the majority of
20461 all searches. */
20463 static struct dwarf_file_data *
20464 lookup_filename (const char *file_name)
20466 void ** slot;
20467 struct dwarf_file_data * created;
20469 /* Check to see if the file name that was searched on the previous
20470 call matches this file name. If so, return the index. */
20471 if (file_table_last_lookup
20472 && (file_name == file_table_last_lookup->filename
20473 || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
20474 return file_table_last_lookup;
20476 /* Didn't match the previous lookup, search the table. */
20477 slot = htab_find_slot_with_hash (file_table, file_name,
20478 htab_hash_string (file_name), INSERT);
20479 if (*slot)
20480 return (struct dwarf_file_data *) *slot;
20482 created = ggc_alloc_dwarf_file_data ();
20483 created->filename = file_name;
20484 created->emitted_number = 0;
20485 *slot = created;
20486 return created;
20489 /* If the assembler will construct the file table, then translate the compiler
20490 internal file table number into the assembler file table number, and emit
20491 a .file directive if we haven't already emitted one yet. The file table
20492 numbers are different because we prune debug info for unused variables and
20493 types, which may include filenames. */
20495 static int
20496 maybe_emit_file (struct dwarf_file_data * fd)
20498 if (! fd->emitted_number)
20500 if (last_emitted_file)
20501 fd->emitted_number = last_emitted_file->emitted_number + 1;
20502 else
20503 fd->emitted_number = 1;
20504 last_emitted_file = fd;
20506 if (DWARF2_ASM_LINE_DEBUG_INFO)
20508 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
20509 output_quoted_string (asm_out_file,
20510 remap_debug_filename (fd->filename));
20511 fputc ('\n', asm_out_file);
20515 return fd->emitted_number;
20518 /* Schedule generation of a DW_AT_const_value attribute to DIE.
20519 That generation should happen after function debug info has been
20520 generated. The value of the attribute is the constant value of ARG. */
20522 static void
20523 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
20525 die_arg_entry entry;
20527 if (!die || !arg)
20528 return;
20530 if (!tmpl_value_parm_die_table)
20531 vec_alloc (tmpl_value_parm_die_table, 32);
20533 entry.die = die;
20534 entry.arg = arg;
20535 vec_safe_push (tmpl_value_parm_die_table, entry);
20538 /* Return TRUE if T is an instance of generic type, FALSE
20539 otherwise. */
20541 static bool
20542 generic_type_p (tree t)
20544 if (t == NULL_TREE || !TYPE_P (t))
20545 return false;
20546 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
20549 /* Schedule the generation of the generic parameter dies for the
20550 instance of generic type T. The proper generation itself is later
20551 done by gen_scheduled_generic_parms_dies. */
20553 static void
20554 schedule_generic_params_dies_gen (tree t)
20556 if (!generic_type_p (t))
20557 return;
20559 if (!generic_type_instances)
20560 vec_alloc (generic_type_instances, 256);
20562 vec_safe_push (generic_type_instances, t);
20565 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
20566 by append_entry_to_tmpl_value_parm_die_table. This function must
20567 be called after function DIEs have been generated. */
20569 static void
20570 gen_remaining_tmpl_value_param_die_attribute (void)
20572 if (tmpl_value_parm_die_table)
20574 unsigned i;
20575 die_arg_entry *e;
20577 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
20578 tree_add_const_value_attribute (e->die, e->arg);
20582 /* Generate generic parameters DIEs for instances of generic types
20583 that have been previously scheduled by
20584 schedule_generic_params_dies_gen. This function must be called
20585 after all the types of the CU have been laid out. */
20587 static void
20588 gen_scheduled_generic_parms_dies (void)
20590 unsigned i;
20591 tree t;
20593 if (!generic_type_instances)
20594 return;
20596 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
20597 gen_generic_params_dies (t);
20601 /* Replace DW_AT_name for the decl with name. */
20603 static void
20604 dwarf2out_set_name (tree decl, tree name)
20606 dw_die_ref die;
20607 dw_attr_ref attr;
20608 const char *dname;
20610 die = TYPE_SYMTAB_DIE (decl);
20611 if (!die)
20612 return;
20614 dname = dwarf2_name (name, 0);
20615 if (!dname)
20616 return;
20618 attr = get_AT (die, DW_AT_name);
20619 if (attr)
20621 struct indirect_string_node *node;
20623 node = find_AT_string (dname);
20624 /* replace the string. */
20625 attr->dw_attr_val.v.val_str = node;
20628 else
20629 add_name_attribute (die, dname);
20632 /* Called by the final INSN scan whenever we see a var location. We
20633 use it to drop labels in the right places, and throw the location in
20634 our lookup table. */
20636 static void
20637 dwarf2out_var_location (rtx loc_note)
20639 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
20640 struct var_loc_node *newloc;
20641 rtx next_real, next_note;
20642 static const char *last_label;
20643 static const char *last_postcall_label;
20644 static bool last_in_cold_section_p;
20645 static rtx expected_next_loc_note;
20646 tree decl;
20647 bool var_loc_p;
20649 if (!NOTE_P (loc_note))
20651 if (CALL_P (loc_note))
20653 call_site_count++;
20654 if (SIBLING_CALL_P (loc_note))
20655 tail_call_site_count++;
20657 return;
20660 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
20661 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
20662 return;
20664 /* Optimize processing a large consecutive sequence of location
20665 notes so we don't spend too much time in next_real_insn. If the
20666 next insn is another location note, remember the next_real_insn
20667 calculation for next time. */
20668 next_real = cached_next_real_insn;
20669 if (next_real)
20671 if (expected_next_loc_note != loc_note)
20672 next_real = NULL_RTX;
20675 next_note = NEXT_INSN (loc_note);
20676 if (! next_note
20677 || INSN_DELETED_P (next_note)
20678 || GET_CODE (next_note) != NOTE
20679 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
20680 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
20681 next_note = NULL_RTX;
20683 if (! next_real)
20684 next_real = next_real_insn (loc_note);
20686 if (next_note)
20688 expected_next_loc_note = next_note;
20689 cached_next_real_insn = next_real;
20691 else
20692 cached_next_real_insn = NULL_RTX;
20694 /* If there are no instructions which would be affected by this note,
20695 don't do anything. */
20696 if (var_loc_p
20697 && next_real == NULL_RTX
20698 && !NOTE_DURING_CALL_P (loc_note))
20699 return;
20701 if (next_real == NULL_RTX)
20702 next_real = get_last_insn ();
20704 /* If there were any real insns between note we processed last time
20705 and this note (or if it is the first note), clear
20706 last_{,postcall_}label so that they are not reused this time. */
20707 if (last_var_location_insn == NULL_RTX
20708 || last_var_location_insn != next_real
20709 || last_in_cold_section_p != in_cold_section_p)
20711 last_label = NULL;
20712 last_postcall_label = NULL;
20715 if (var_loc_p)
20717 decl = NOTE_VAR_LOCATION_DECL (loc_note);
20718 newloc = add_var_loc_to_decl (decl, loc_note,
20719 NOTE_DURING_CALL_P (loc_note)
20720 ? last_postcall_label : last_label);
20721 if (newloc == NULL)
20722 return;
20724 else
20726 decl = NULL_TREE;
20727 newloc = NULL;
20730 /* If there were no real insns between note we processed last time
20731 and this note, use the label we emitted last time. Otherwise
20732 create a new label and emit it. */
20733 if (last_label == NULL)
20735 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
20736 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
20737 loclabel_num++;
20738 last_label = ggc_strdup (loclabel);
20741 if (!var_loc_p)
20743 struct call_arg_loc_node *ca_loc
20744 = ggc_alloc_cleared_call_arg_loc_node ();
20745 rtx prev = prev_real_insn (loc_note), x;
20746 ca_loc->call_arg_loc_note = loc_note;
20747 ca_loc->next = NULL;
20748 ca_loc->label = last_label;
20749 gcc_assert (prev
20750 && (CALL_P (prev)
20751 || (NONJUMP_INSN_P (prev)
20752 && GET_CODE (PATTERN (prev)) == SEQUENCE
20753 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
20754 if (!CALL_P (prev))
20755 prev = XVECEXP (PATTERN (prev), 0, 0);
20756 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
20757 x = get_call_rtx_from (PATTERN (prev));
20758 if (x)
20760 x = XEXP (XEXP (x, 0), 0);
20761 if (GET_CODE (x) == SYMBOL_REF
20762 && SYMBOL_REF_DECL (x)
20763 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
20764 ca_loc->symbol_ref = x;
20766 ca_loc->block = insn_scope (prev);
20767 if (call_arg_locations)
20768 call_arg_loc_last->next = ca_loc;
20769 else
20770 call_arg_locations = ca_loc;
20771 call_arg_loc_last = ca_loc;
20773 else if (!NOTE_DURING_CALL_P (loc_note))
20774 newloc->label = last_label;
20775 else
20777 if (!last_postcall_label)
20779 sprintf (loclabel, "%s-1", last_label);
20780 last_postcall_label = ggc_strdup (loclabel);
20782 newloc->label = last_postcall_label;
20785 last_var_location_insn = next_real;
20786 last_in_cold_section_p = in_cold_section_p;
20789 /* Note in one location list that text section has changed. */
20791 static int
20792 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
20794 var_loc_list *list = (var_loc_list *) *slot;
20795 if (list->first)
20796 list->last_before_switch
20797 = list->last->next ? list->last->next : list->last;
20798 return 1;
20801 /* Note in all location lists that text section has changed. */
20803 static void
20804 var_location_switch_text_section (void)
20806 if (decl_loc_table == NULL)
20807 return;
20809 htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
20812 /* Create a new line number table. */
20814 static dw_line_info_table *
20815 new_line_info_table (void)
20817 dw_line_info_table *table;
20819 table = ggc_alloc_cleared_dw_line_info_table_struct ();
20820 table->file_num = 1;
20821 table->line_num = 1;
20822 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
20824 return table;
20827 /* Lookup the "current" table into which we emit line info, so
20828 that we don't have to do it for every source line. */
20830 static void
20831 set_cur_line_info_table (section *sec)
20833 dw_line_info_table *table;
20835 if (sec == text_section)
20836 table = text_section_line_info;
20837 else if (sec == cold_text_section)
20839 table = cold_text_section_line_info;
20840 if (!table)
20842 cold_text_section_line_info = table = new_line_info_table ();
20843 table->end_label = cold_end_label;
20846 else
20848 const char *end_label;
20850 if (flag_reorder_blocks_and_partition)
20852 if (in_cold_section_p)
20853 end_label = crtl->subsections.cold_section_end_label;
20854 else
20855 end_label = crtl->subsections.hot_section_end_label;
20857 else
20859 char label[MAX_ARTIFICIAL_LABEL_BYTES];
20860 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
20861 current_function_funcdef_no);
20862 end_label = ggc_strdup (label);
20865 table = new_line_info_table ();
20866 table->end_label = end_label;
20868 vec_safe_push (separate_line_info, table);
20871 if (DWARF2_ASM_LINE_DEBUG_INFO)
20872 table->is_stmt = (cur_line_info_table
20873 ? cur_line_info_table->is_stmt
20874 : DWARF_LINE_DEFAULT_IS_STMT_START);
20875 cur_line_info_table = table;
20879 /* We need to reset the locations at the beginning of each
20880 function. We can't do this in the end_function hook, because the
20881 declarations that use the locations won't have been output when
20882 that hook is called. Also compute have_multiple_function_sections here. */
20884 static void
20885 dwarf2out_begin_function (tree fun)
20887 section *sec = function_section (fun);
20889 if (sec != text_section)
20890 have_multiple_function_sections = true;
20892 if (flag_reorder_blocks_and_partition && !cold_text_section)
20894 gcc_assert (current_function_decl == fun);
20895 cold_text_section = unlikely_text_section ();
20896 switch_to_section (cold_text_section);
20897 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
20898 switch_to_section (sec);
20901 dwarf2out_note_section_used ();
20902 call_site_count = 0;
20903 tail_call_site_count = 0;
20905 set_cur_line_info_table (sec);
20908 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
20910 static void
20911 push_dw_line_info_entry (dw_line_info_table *table,
20912 enum dw_line_info_opcode opcode, unsigned int val)
20914 dw_line_info_entry e;
20915 e.opcode = opcode;
20916 e.val = val;
20917 vec_safe_push (table->entries, e);
20920 /* Output a label to mark the beginning of a source code line entry
20921 and record information relating to this source line, in
20922 'line_info_table' for later output of the .debug_line section. */
20923 /* ??? The discriminator parameter ought to be unsigned. */
20925 static void
20926 dwarf2out_source_line (unsigned int line, const char *filename,
20927 int discriminator, bool is_stmt)
20929 unsigned int file_num;
20930 dw_line_info_table *table;
20932 if (debug_info_level < DINFO_LEVEL_NORMAL || line == 0)
20933 return;
20935 /* The discriminator column was added in dwarf4. Simplify the below
20936 by simply removing it if we're not supposed to output it. */
20937 if (dwarf_version < 4 && dwarf_strict)
20938 discriminator = 0;
20940 table = cur_line_info_table;
20941 file_num = maybe_emit_file (lookup_filename (filename));
20943 /* ??? TODO: Elide duplicate line number entries. Traditionally,
20944 the debugger has used the second (possibly duplicate) line number
20945 at the beginning of the function to mark the end of the prologue.
20946 We could eliminate any other duplicates within the function. For
20947 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
20948 that second line number entry. */
20949 /* Recall that this end-of-prologue indication is *not* the same thing
20950 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
20951 to which the hook corresponds, follows the last insn that was
20952 emitted by gen_prologue. What we need is to precede the first insn
20953 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
20954 insn that corresponds to something the user wrote. These may be
20955 very different locations once scheduling is enabled. */
20957 if (0 && file_num == table->file_num
20958 && line == table->line_num
20959 && discriminator == table->discrim_num
20960 && is_stmt == table->is_stmt)
20961 return;
20963 switch_to_section (current_function_section ());
20965 /* If requested, emit something human-readable. */
20966 if (flag_debug_asm)
20967 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
20969 if (DWARF2_ASM_LINE_DEBUG_INFO)
20971 /* Emit the .loc directive understood by GNU as. */
20972 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
20973 file_num, line, is_stmt, discriminator */
20974 fputs ("\t.loc ", asm_out_file);
20975 fprint_ul (asm_out_file, file_num);
20976 putc (' ', asm_out_file);
20977 fprint_ul (asm_out_file, line);
20978 putc (' ', asm_out_file);
20979 putc ('0', asm_out_file);
20981 if (is_stmt != table->is_stmt)
20983 fputs (" is_stmt ", asm_out_file);
20984 putc (is_stmt ? '1' : '0', asm_out_file);
20986 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
20988 gcc_assert (discriminator > 0);
20989 fputs (" discriminator ", asm_out_file);
20990 fprint_ul (asm_out_file, (unsigned long) discriminator);
20992 putc ('\n', asm_out_file);
20994 else
20996 unsigned int label_num = ++line_info_label_num;
20998 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
21000 push_dw_line_info_entry (table, LI_set_address, label_num);
21001 if (file_num != table->file_num)
21002 push_dw_line_info_entry (table, LI_set_file, file_num);
21003 if (discriminator != table->discrim_num)
21004 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
21005 if (is_stmt != table->is_stmt)
21006 push_dw_line_info_entry (table, LI_negate_stmt, 0);
21007 push_dw_line_info_entry (table, LI_set_line, line);
21010 table->file_num = file_num;
21011 table->line_num = line;
21012 table->discrim_num = discriminator;
21013 table->is_stmt = is_stmt;
21014 table->in_use = true;
21017 /* Record the beginning of a new source file. */
21019 static void
21020 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
21022 if (flag_eliminate_dwarf2_dups)
21024 /* Record the beginning of the file for break_out_includes. */
21025 dw_die_ref bincl_die;
21027 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
21028 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
21031 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21033 macinfo_entry e;
21034 e.code = DW_MACINFO_start_file;
21035 e.lineno = lineno;
21036 e.info = ggc_strdup (filename);
21037 vec_safe_push (macinfo_table, e);
21041 /* Record the end of a source file. */
21043 static void
21044 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
21046 if (flag_eliminate_dwarf2_dups)
21047 /* Record the end of the file for break_out_includes. */
21048 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
21050 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21052 macinfo_entry e;
21053 e.code = DW_MACINFO_end_file;
21054 e.lineno = lineno;
21055 e.info = NULL;
21056 vec_safe_push (macinfo_table, e);
21060 /* Called from debug_define in toplev.c. The `buffer' parameter contains
21061 the tail part of the directive line, i.e. the part which is past the
21062 initial whitespace, #, whitespace, directive-name, whitespace part. */
21064 static void
21065 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
21066 const char *buffer ATTRIBUTE_UNUSED)
21068 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21070 macinfo_entry e;
21071 /* Insert a dummy first entry to be able to optimize the whole
21072 predefined macro block using DW_MACRO_GNU_transparent_include. */
21073 if (macinfo_table->is_empty () && lineno <= 1)
21075 e.code = 0;
21076 e.lineno = 0;
21077 e.info = NULL;
21078 vec_safe_push (macinfo_table, e);
21080 e.code = DW_MACINFO_define;
21081 e.lineno = lineno;
21082 e.info = ggc_strdup (buffer);
21083 vec_safe_push (macinfo_table, e);
21087 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
21088 the tail part of the directive line, i.e. the part which is past the
21089 initial whitespace, #, whitespace, directive-name, whitespace part. */
21091 static void
21092 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
21093 const char *buffer ATTRIBUTE_UNUSED)
21095 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21097 macinfo_entry e;
21098 /* Insert a dummy first entry to be able to optimize the whole
21099 predefined macro block using DW_MACRO_GNU_transparent_include. */
21100 if (macinfo_table->is_empty () && lineno <= 1)
21102 e.code = 0;
21103 e.lineno = 0;
21104 e.info = NULL;
21105 vec_safe_push (macinfo_table, e);
21107 e.code = DW_MACINFO_undef;
21108 e.lineno = lineno;
21109 e.info = ggc_strdup (buffer);
21110 vec_safe_push (macinfo_table, e);
21114 /* Routines to manipulate hash table of CUs. */
21116 static hashval_t
21117 htab_macinfo_hash (const void *of)
21119 const macinfo_entry *const entry =
21120 (const macinfo_entry *) of;
21122 return htab_hash_string (entry->info);
21125 static int
21126 htab_macinfo_eq (const void *of1, const void *of2)
21128 const macinfo_entry *const entry1 = (const macinfo_entry *) of1;
21129 const macinfo_entry *const entry2 = (const macinfo_entry *) of2;
21131 return !strcmp (entry1->info, entry2->info);
21134 /* Output a single .debug_macinfo entry. */
21136 static void
21137 output_macinfo_op (macinfo_entry *ref)
21139 int file_num;
21140 size_t len;
21141 struct indirect_string_node *node;
21142 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21143 struct dwarf_file_data *fd;
21145 switch (ref->code)
21147 case DW_MACINFO_start_file:
21148 fd = lookup_filename (ref->info);
21149 file_num = maybe_emit_file (fd);
21150 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
21151 dw2_asm_output_data_uleb128 (ref->lineno,
21152 "Included from line number %lu",
21153 (unsigned long) ref->lineno);
21154 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
21155 break;
21156 case DW_MACINFO_end_file:
21157 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
21158 break;
21159 case DW_MACINFO_define:
21160 case DW_MACINFO_undef:
21161 len = strlen (ref->info) + 1;
21162 if (!dwarf_strict
21163 && len > DWARF_OFFSET_SIZE
21164 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21165 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21167 ref->code = ref->code == DW_MACINFO_define
21168 ? DW_MACRO_GNU_define_indirect
21169 : DW_MACRO_GNU_undef_indirect;
21170 output_macinfo_op (ref);
21171 return;
21173 dw2_asm_output_data (1, ref->code,
21174 ref->code == DW_MACINFO_define
21175 ? "Define macro" : "Undefine macro");
21176 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21177 (unsigned long) ref->lineno);
21178 dw2_asm_output_nstring (ref->info, -1, "The macro");
21179 break;
21180 case DW_MACRO_GNU_define_indirect:
21181 case DW_MACRO_GNU_undef_indirect:
21182 node = find_AT_string (ref->info);
21183 gcc_assert (node
21184 && ((node->form == DW_FORM_strp)
21185 || (node->form == DW_FORM_GNU_str_index)));
21186 dw2_asm_output_data (1, ref->code,
21187 ref->code == DW_MACRO_GNU_define_indirect
21188 ? "Define macro indirect"
21189 : "Undefine macro indirect");
21190 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21191 (unsigned long) ref->lineno);
21192 if (node->form == DW_FORM_strp)
21193 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
21194 debug_str_section, "The macro: \"%s\"",
21195 ref->info);
21196 else
21197 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
21198 ref->info);
21199 break;
21200 case DW_MACRO_GNU_transparent_include:
21201 dw2_asm_output_data (1, ref->code, "Transparent include");
21202 ASM_GENERATE_INTERNAL_LABEL (label,
21203 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
21204 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
21205 break;
21206 default:
21207 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
21208 ASM_COMMENT_START, (unsigned long) ref->code);
21209 break;
21213 /* Attempt to make a sequence of define/undef macinfo ops shareable with
21214 other compilation unit .debug_macinfo sections. IDX is the first
21215 index of a define/undef, return the number of ops that should be
21216 emitted in a comdat .debug_macinfo section and emit
21217 a DW_MACRO_GNU_transparent_include entry referencing it.
21218 If the define/undef entry should be emitted normally, return 0. */
21220 static unsigned
21221 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
21222 htab_t *macinfo_htab)
21224 macinfo_entry *first, *second, *cur, *inc;
21225 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
21226 unsigned char checksum[16];
21227 struct md5_ctx ctx;
21228 char *grp_name, *tail;
21229 const char *base;
21230 unsigned int i, count, encoded_filename_len, linebuf_len;
21231 void **slot;
21233 first = &(*macinfo_table)[idx];
21234 second = &(*macinfo_table)[idx + 1];
21236 /* Optimize only if there are at least two consecutive define/undef ops,
21237 and either all of them are before first DW_MACINFO_start_file
21238 with lineno {0,1} (i.e. predefined macro block), or all of them are
21239 in some included header file. */
21240 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
21241 return 0;
21242 if (vec_safe_is_empty (files))
21244 if (first->lineno > 1 || second->lineno > 1)
21245 return 0;
21247 else if (first->lineno == 0)
21248 return 0;
21250 /* Find the last define/undef entry that can be grouped together
21251 with first and at the same time compute md5 checksum of their
21252 codes, linenumbers and strings. */
21253 md5_init_ctx (&ctx);
21254 for (i = idx; macinfo_table->iterate (i, &cur); i++)
21255 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
21256 break;
21257 else if (vec_safe_is_empty (files) && cur->lineno > 1)
21258 break;
21259 else
21261 unsigned char code = cur->code;
21262 md5_process_bytes (&code, 1, &ctx);
21263 checksum_uleb128 (cur->lineno, &ctx);
21264 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
21266 md5_finish_ctx (&ctx, checksum);
21267 count = i - idx;
21269 /* From the containing include filename (if any) pick up just
21270 usable characters from its basename. */
21271 if (vec_safe_is_empty (files))
21272 base = "";
21273 else
21274 base = lbasename (files->last ().info);
21275 for (encoded_filename_len = 0, i = 0; base[i]; i++)
21276 if (ISIDNUM (base[i]) || base[i] == '.')
21277 encoded_filename_len++;
21278 /* Count . at the end. */
21279 if (encoded_filename_len)
21280 encoded_filename_len++;
21282 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
21283 linebuf_len = strlen (linebuf);
21285 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
21286 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
21287 + 16 * 2 + 1);
21288 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
21289 tail = grp_name + 4;
21290 if (encoded_filename_len)
21292 for (i = 0; base[i]; i++)
21293 if (ISIDNUM (base[i]) || base[i] == '.')
21294 *tail++ = base[i];
21295 *tail++ = '.';
21297 memcpy (tail, linebuf, linebuf_len);
21298 tail += linebuf_len;
21299 *tail++ = '.';
21300 for (i = 0; i < 16; i++)
21301 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
21303 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
21304 in the empty vector entry before the first define/undef. */
21305 inc = &(*macinfo_table)[idx - 1];
21306 inc->code = DW_MACRO_GNU_transparent_include;
21307 inc->lineno = 0;
21308 inc->info = ggc_strdup (grp_name);
21309 if (*macinfo_htab == NULL)
21310 *macinfo_htab = htab_create (10, htab_macinfo_hash, htab_macinfo_eq, NULL);
21311 /* Avoid emitting duplicates. */
21312 slot = htab_find_slot (*macinfo_htab, inc, INSERT);
21313 if (*slot != NULL)
21315 inc->code = 0;
21316 inc->info = NULL;
21317 /* If such an entry has been used before, just emit
21318 a DW_MACRO_GNU_transparent_include op. */
21319 inc = (macinfo_entry *) *slot;
21320 output_macinfo_op (inc);
21321 /* And clear all macinfo_entry in the range to avoid emitting them
21322 in the second pass. */
21323 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
21325 cur->code = 0;
21326 cur->info = NULL;
21329 else
21331 *slot = inc;
21332 inc->lineno = htab_elements (*macinfo_htab);
21333 output_macinfo_op (inc);
21335 return count;
21338 /* Save any strings needed by the macinfo table in the debug str
21339 table. All strings must be collected into the table by the time
21340 index_string is called. */
21342 static void
21343 save_macinfo_strings (void)
21345 unsigned len;
21346 unsigned i;
21347 macinfo_entry *ref;
21349 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
21351 switch (ref->code)
21353 /* Match the logic in output_macinfo_op to decide on
21354 indirect strings. */
21355 case DW_MACINFO_define:
21356 case DW_MACINFO_undef:
21357 len = strlen (ref->info) + 1;
21358 if (!dwarf_strict
21359 && len > DWARF_OFFSET_SIZE
21360 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21361 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21362 set_indirect_string (find_AT_string (ref->info));
21363 break;
21364 case DW_MACRO_GNU_define_indirect:
21365 case DW_MACRO_GNU_undef_indirect:
21366 set_indirect_string (find_AT_string (ref->info));
21367 break;
21368 default:
21369 break;
21374 /* Output macinfo section(s). */
21376 static void
21377 output_macinfo (void)
21379 unsigned i;
21380 unsigned long length = vec_safe_length (macinfo_table);
21381 macinfo_entry *ref;
21382 vec<macinfo_entry, va_gc> *files = NULL;
21383 htab_t macinfo_htab = NULL;
21385 if (! length)
21386 return;
21388 /* output_macinfo* uses these interchangeably. */
21389 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
21390 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
21391 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
21392 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
21394 /* For .debug_macro emit the section header. */
21395 if (!dwarf_strict)
21397 dw2_asm_output_data (2, 4, "DWARF macro version number");
21398 if (DWARF_OFFSET_SIZE == 8)
21399 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
21400 else
21401 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
21402 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
21403 (!dwarf_split_debug_info ? debug_line_section_label
21404 : debug_skeleton_line_section_label),
21405 debug_line_section, NULL);
21408 /* In the first loop, it emits the primary .debug_macinfo section
21409 and after each emitted op the macinfo_entry is cleared.
21410 If a longer range of define/undef ops can be optimized using
21411 DW_MACRO_GNU_transparent_include, the
21412 DW_MACRO_GNU_transparent_include op is emitted and kept in
21413 the vector before the first define/undef in the range and the
21414 whole range of define/undef ops is not emitted and kept. */
21415 for (i = 0; macinfo_table->iterate (i, &ref); i++)
21417 switch (ref->code)
21419 case DW_MACINFO_start_file:
21420 vec_safe_push (files, *ref);
21421 break;
21422 case DW_MACINFO_end_file:
21423 if (!vec_safe_is_empty (files))
21424 files->pop ();
21425 break;
21426 case DW_MACINFO_define:
21427 case DW_MACINFO_undef:
21428 if (!dwarf_strict
21429 && HAVE_COMDAT_GROUP
21430 && vec_safe_length (files) != 1
21431 && i > 0
21432 && i + 1 < length
21433 && (*macinfo_table)[i - 1].code == 0)
21435 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
21436 if (count)
21438 i += count - 1;
21439 continue;
21442 break;
21443 case 0:
21444 /* A dummy entry may be inserted at the beginning to be able
21445 to optimize the whole block of predefined macros. */
21446 if (i == 0)
21447 continue;
21448 default:
21449 break;
21451 output_macinfo_op (ref);
21452 ref->info = NULL;
21453 ref->code = 0;
21456 if (macinfo_htab == NULL)
21457 return;
21459 htab_delete (macinfo_htab);
21461 /* If any DW_MACRO_GNU_transparent_include were used, on those
21462 DW_MACRO_GNU_transparent_include entries terminate the
21463 current chain and switch to a new comdat .debug_macinfo
21464 section and emit the define/undef entries within it. */
21465 for (i = 0; macinfo_table->iterate (i, &ref); i++)
21466 switch (ref->code)
21468 case 0:
21469 continue;
21470 case DW_MACRO_GNU_transparent_include:
21472 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21473 tree comdat_key = get_identifier (ref->info);
21474 /* Terminate the previous .debug_macinfo section. */
21475 dw2_asm_output_data (1, 0, "End compilation unit");
21476 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
21477 SECTION_DEBUG
21478 | SECTION_LINKONCE,
21479 comdat_key);
21480 ASM_GENERATE_INTERNAL_LABEL (label,
21481 DEBUG_MACRO_SECTION_LABEL,
21482 ref->lineno);
21483 ASM_OUTPUT_LABEL (asm_out_file, label);
21484 ref->code = 0;
21485 ref->info = NULL;
21486 dw2_asm_output_data (2, 4, "DWARF macro version number");
21487 if (DWARF_OFFSET_SIZE == 8)
21488 dw2_asm_output_data (1, 1, "Flags: 64-bit");
21489 else
21490 dw2_asm_output_data (1, 0, "Flags: 32-bit");
21492 break;
21493 case DW_MACINFO_define:
21494 case DW_MACINFO_undef:
21495 output_macinfo_op (ref);
21496 ref->code = 0;
21497 ref->info = NULL;
21498 break;
21499 default:
21500 gcc_unreachable ();
21504 /* Set up for Dwarf output at the start of compilation. */
21506 static void
21507 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
21509 /* Allocate the file_table. */
21510 file_table = htab_create_ggc (50, file_table_hash,
21511 file_table_eq, NULL);
21513 /* Allocate the decl_die_table. */
21514 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
21515 decl_die_table_eq, NULL);
21517 /* Allocate the decl_loc_table. */
21518 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
21519 decl_loc_table_eq, NULL);
21521 /* Allocate the cached_dw_loc_list_table. */
21522 cached_dw_loc_list_table
21523 = htab_create_ggc (10, cached_dw_loc_list_table_hash,
21524 cached_dw_loc_list_table_eq, NULL);
21526 /* Allocate the initial hunk of the decl_scope_table. */
21527 vec_alloc (decl_scope_table, 256);
21529 /* Allocate the initial hunk of the abbrev_die_table. */
21530 abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
21531 (ABBREV_DIE_TABLE_INCREMENT);
21532 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
21533 /* Zero-th entry is allocated, but unused. */
21534 abbrev_die_table_in_use = 1;
21536 /* Allocate the pubtypes and pubnames vectors. */
21537 vec_alloc (pubname_table, 32);
21538 vec_alloc (pubtype_table, 32);
21540 vec_alloc (incomplete_types, 64);
21542 vec_alloc (used_rtx_array, 32);
21544 if (!dwarf_split_debug_info)
21546 debug_info_section = get_section (DEBUG_INFO_SECTION,
21547 SECTION_DEBUG, NULL);
21548 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
21549 SECTION_DEBUG, NULL);
21550 debug_loc_section = get_section (DEBUG_LOC_SECTION,
21551 SECTION_DEBUG, NULL);
21553 else
21555 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
21556 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
21557 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
21558 SECTION_DEBUG | SECTION_EXCLUDE,
21559 NULL);
21560 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
21561 SECTION_DEBUG, NULL);
21562 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
21563 SECTION_DEBUG, NULL);
21564 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
21565 SECTION_DEBUG, NULL);
21566 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
21567 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
21569 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
21570 the main .o, but the skeleton_line goes into the split off dwo. */
21571 debug_skeleton_line_section
21572 = get_section (DEBUG_DWO_LINE_SECTION,
21573 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
21574 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
21575 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
21576 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
21577 SECTION_DEBUG | SECTION_EXCLUDE,
21578 NULL);
21579 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
21580 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
21581 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
21582 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
21584 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
21585 SECTION_DEBUG, NULL);
21586 debug_macinfo_section = get_section (dwarf_strict
21587 ? DEBUG_MACINFO_SECTION
21588 : DEBUG_MACRO_SECTION,
21589 DEBUG_MACRO_SECTION_FLAGS, NULL);
21590 debug_line_section = get_section (DEBUG_LINE_SECTION,
21591 SECTION_DEBUG, NULL);
21592 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
21593 SECTION_DEBUG, NULL);
21594 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
21595 SECTION_DEBUG, NULL);
21596 debug_str_section = get_section (DEBUG_STR_SECTION,
21597 DEBUG_STR_SECTION_FLAGS, NULL);
21598 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
21599 SECTION_DEBUG, NULL);
21600 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
21601 SECTION_DEBUG, NULL);
21603 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
21604 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
21605 DEBUG_ABBREV_SECTION_LABEL, 0);
21606 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
21607 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
21608 COLD_TEXT_SECTION_LABEL, 0);
21609 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
21611 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
21612 DEBUG_INFO_SECTION_LABEL, 0);
21613 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
21614 DEBUG_LINE_SECTION_LABEL, 0);
21615 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
21616 DEBUG_RANGES_SECTION_LABEL, 0);
21617 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
21618 DEBUG_ADDR_SECTION_LABEL, 0);
21619 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
21620 dwarf_strict
21621 ? DEBUG_MACINFO_SECTION_LABEL
21622 : DEBUG_MACRO_SECTION_LABEL, 0);
21623 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
21625 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21626 vec_alloc (macinfo_table, 64);
21628 switch_to_section (text_section);
21629 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
21631 /* Make sure the line number table for .text always exists. */
21632 text_section_line_info = new_line_info_table ();
21633 text_section_line_info->end_label = text_end_label;
21636 /* Called before compile () starts outputtting functions, variables
21637 and toplevel asms into assembly. */
21639 static void
21640 dwarf2out_assembly_start (void)
21642 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
21643 && dwarf2out_do_cfi_asm ()
21644 && (!(flag_unwind_tables || flag_exceptions)
21645 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
21646 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
21649 /* A helper function for dwarf2out_finish called through
21650 htab_traverse. Assign a string its index. All strings must be
21651 collected into the table by the time index_string is called,
21652 because the indexing code relies on htab_traverse to traverse nodes
21653 in the same order for each run. */
21655 static int
21656 index_string (void **h, void *v)
21658 struct indirect_string_node *node = (struct indirect_string_node *) *h;
21659 unsigned int *index = (unsigned int *) v;
21661 find_string_form (node);
21662 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
21664 gcc_assert(node->index == NO_INDEX_ASSIGNED);
21665 node->index = *index;
21666 *index += 1;
21668 return 1;
21671 /* A helper function for output_indirect_strings called through
21672 htab_traverse. Output the offset to a string and update the
21673 current offset. */
21675 static int
21676 output_index_string_offset (void **h, void *v)
21678 struct indirect_string_node *node = (struct indirect_string_node *) *h;
21679 unsigned int *offset = (unsigned int *) v;
21681 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
21683 /* Assert that this node has been assigned an index. */
21684 gcc_assert (node->index != NO_INDEX_ASSIGNED
21685 && node->index != NOT_INDEXED);
21686 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
21687 "indexed string 0x%x: %s", node->index, node->str);
21688 *offset += strlen (node->str) + 1;
21690 return 1;
21693 /* A helper function for dwarf2out_finish called through
21694 htab_traverse. Output the indexed string. */
21696 static int
21697 output_index_string (void **h, void *v)
21699 struct indirect_string_node *node = (struct indirect_string_node *) *h;
21700 unsigned int *cur_idx = (unsigned int *) v;
21702 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
21704 /* Assert that the strings are output in the same order as their
21705 indexes were assigned. */
21706 gcc_assert (*cur_idx == node->index);
21707 ASM_OUTPUT_LABEL (asm_out_file, node->label);
21708 assemble_string (node->str, strlen (node->str) + 1);
21709 *cur_idx += 1;
21711 return 1;
21714 /* A helper function for dwarf2out_finish called through
21715 htab_traverse. Emit one queued .debug_str string. */
21717 static int
21718 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
21720 struct indirect_string_node *node = (struct indirect_string_node *) *h;
21722 if (node->form == DW_FORM_strp && node->refcount > 0)
21724 ASM_OUTPUT_LABEL (asm_out_file, node->label);
21725 assemble_string (node->str, strlen (node->str) + 1);
21728 return 1;
21731 /* Output the indexed string table. */
21733 static void
21734 output_indirect_strings (void)
21736 if (!dwarf_split_debug_info)
21738 switch_to_section (debug_str_section);
21739 htab_traverse (debug_str_hash, output_indirect_string, NULL);
21741 else
21743 unsigned int offset = 0;
21744 unsigned int cur_idx = 0;
21746 switch_to_section (debug_str_offsets_section);
21747 htab_traverse_noresize (debug_str_hash,
21748 output_index_string_offset,
21749 &offset);
21750 switch_to_section (debug_str_section);
21751 htab_traverse_noresize (debug_str_hash,
21752 output_index_string,
21753 &cur_idx);
21757 /* Callback for htab_traverse to assign an index to an entry in the
21758 table, and to write that entry to the .debug_addr section. */
21760 static int
21761 output_addr_table_entry (void **slot, void *data)
21763 addr_table_entry *entry = (addr_table_entry *) *slot;
21764 unsigned int *cur_index = (unsigned int *)data;
21766 if (entry->refcount == 0)
21768 gcc_assert (entry->index == NO_INDEX_ASSIGNED
21769 || entry->index == NOT_INDEXED);
21770 return 1;
21773 gcc_assert (entry->index == *cur_index);
21774 (*cur_index)++;
21776 switch (entry->kind)
21778 case ate_kind_rtx:
21779 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
21780 "0x%x", entry->index);
21781 break;
21782 case ate_kind_rtx_dtprel:
21783 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
21784 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
21785 DWARF2_ADDR_SIZE,
21786 entry->addr.rtl);
21787 fputc ('\n', asm_out_file);
21788 break;
21789 case ate_kind_label:
21790 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
21791 "0x%x", entry->index);
21792 break;
21793 default:
21794 gcc_unreachable ();
21796 return 1;
21799 /* Produce the .debug_addr section. */
21801 static void
21802 output_addr_table (void)
21804 unsigned int index = 0;
21805 if (addr_index_table == NULL || htab_size (addr_index_table) == 0)
21806 return;
21808 switch_to_section (debug_addr_section);
21809 htab_traverse_noresize (addr_index_table, output_addr_table_entry, &index);
21812 #if ENABLE_ASSERT_CHECKING
21813 /* Verify that all marks are clear. */
21815 static void
21816 verify_marks_clear (dw_die_ref die)
21818 dw_die_ref c;
21820 gcc_assert (! die->die_mark);
21821 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
21823 #endif /* ENABLE_ASSERT_CHECKING */
21825 /* Clear the marks for a die and its children.
21826 Be cool if the mark isn't set. */
21828 static void
21829 prune_unmark_dies (dw_die_ref die)
21831 dw_die_ref c;
21833 if (die->die_mark)
21834 die->die_mark = 0;
21835 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
21838 /* Given DIE that we're marking as used, find any other dies
21839 it references as attributes and mark them as used. */
21841 static void
21842 prune_unused_types_walk_attribs (dw_die_ref die)
21844 dw_attr_ref a;
21845 unsigned ix;
21847 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
21849 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
21851 /* A reference to another DIE.
21852 Make sure that it will get emitted.
21853 If it was broken out into a comdat group, don't follow it. */
21854 if (! AT_ref (a)->comdat_type_p
21855 || a->dw_attr == DW_AT_specification)
21856 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
21858 /* Set the string's refcount to 0 so that prune_unused_types_mark
21859 accounts properly for it. */
21860 if (AT_class (a) == dw_val_class_str)
21861 a->dw_attr_val.v.val_str->refcount = 0;
21865 /* Mark the generic parameters and arguments children DIEs of DIE. */
21867 static void
21868 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
21870 dw_die_ref c;
21872 if (die == NULL || die->die_child == NULL)
21873 return;
21874 c = die->die_child;
21877 switch (c->die_tag)
21879 case DW_TAG_template_type_param:
21880 case DW_TAG_template_value_param:
21881 case DW_TAG_GNU_template_template_param:
21882 case DW_TAG_GNU_template_parameter_pack:
21883 prune_unused_types_mark (c, 1);
21884 break;
21885 default:
21886 break;
21888 c = c->die_sib;
21889 } while (c && c != die->die_child);
21892 /* Mark DIE as being used. If DOKIDS is true, then walk down
21893 to DIE's children. */
21895 static void
21896 prune_unused_types_mark (dw_die_ref die, int dokids)
21898 dw_die_ref c;
21900 if (die->die_mark == 0)
21902 /* We haven't done this node yet. Mark it as used. */
21903 die->die_mark = 1;
21904 /* If this is the DIE of a generic type instantiation,
21905 mark the children DIEs that describe its generic parms and
21906 args. */
21907 prune_unused_types_mark_generic_parms_dies (die);
21909 /* We also have to mark its parents as used.
21910 (But we don't want to mark our parents' kids due to this.) */
21911 if (die->die_parent)
21912 prune_unused_types_mark (die->die_parent, 0);
21914 /* Mark any referenced nodes. */
21915 prune_unused_types_walk_attribs (die);
21917 /* If this node is a specification,
21918 also mark the definition, if it exists. */
21919 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
21920 prune_unused_types_mark (die->die_definition, 1);
21923 if (dokids && die->die_mark != 2)
21925 /* We need to walk the children, but haven't done so yet.
21926 Remember that we've walked the kids. */
21927 die->die_mark = 2;
21929 /* If this is an array type, we need to make sure our
21930 kids get marked, even if they're types. If we're
21931 breaking out types into comdat sections, do this
21932 for all type definitions. */
21933 if (die->die_tag == DW_TAG_array_type
21934 || (use_debug_types
21935 && is_type_die (die) && ! is_declaration_die (die)))
21936 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
21937 else
21938 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
21942 /* For local classes, look if any static member functions were emitted
21943 and if so, mark them. */
21945 static void
21946 prune_unused_types_walk_local_classes (dw_die_ref die)
21948 dw_die_ref c;
21950 if (die->die_mark == 2)
21951 return;
21953 switch (die->die_tag)
21955 case DW_TAG_structure_type:
21956 case DW_TAG_union_type:
21957 case DW_TAG_class_type:
21958 break;
21960 case DW_TAG_subprogram:
21961 if (!get_AT_flag (die, DW_AT_declaration)
21962 || die->die_definition != NULL)
21963 prune_unused_types_mark (die, 1);
21964 return;
21966 default:
21967 return;
21970 /* Mark children. */
21971 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
21974 /* Walk the tree DIE and mark types that we actually use. */
21976 static void
21977 prune_unused_types_walk (dw_die_ref die)
21979 dw_die_ref c;
21981 /* Don't do anything if this node is already marked and
21982 children have been marked as well. */
21983 if (die->die_mark == 2)
21984 return;
21986 switch (die->die_tag)
21988 case DW_TAG_structure_type:
21989 case DW_TAG_union_type:
21990 case DW_TAG_class_type:
21991 if (die->die_perennial_p)
21992 break;
21994 for (c = die->die_parent; c; c = c->die_parent)
21995 if (c->die_tag == DW_TAG_subprogram)
21996 break;
21998 /* Finding used static member functions inside of classes
21999 is needed just for local classes, because for other classes
22000 static member function DIEs with DW_AT_specification
22001 are emitted outside of the DW_TAG_*_type. If we ever change
22002 it, we'd need to call this even for non-local classes. */
22003 if (c)
22004 prune_unused_types_walk_local_classes (die);
22006 /* It's a type node --- don't mark it. */
22007 return;
22009 case DW_TAG_const_type:
22010 case DW_TAG_packed_type:
22011 case DW_TAG_pointer_type:
22012 case DW_TAG_reference_type:
22013 case DW_TAG_rvalue_reference_type:
22014 case DW_TAG_volatile_type:
22015 case DW_TAG_typedef:
22016 case DW_TAG_array_type:
22017 case DW_TAG_interface_type:
22018 case DW_TAG_friend:
22019 case DW_TAG_variant_part:
22020 case DW_TAG_enumeration_type:
22021 case DW_TAG_subroutine_type:
22022 case DW_TAG_string_type:
22023 case DW_TAG_set_type:
22024 case DW_TAG_subrange_type:
22025 case DW_TAG_ptr_to_member_type:
22026 case DW_TAG_file_type:
22027 if (die->die_perennial_p)
22028 break;
22030 /* It's a type node --- don't mark it. */
22031 return;
22033 default:
22034 /* Mark everything else. */
22035 break;
22038 if (die->die_mark == 0)
22040 die->die_mark = 1;
22042 /* Now, mark any dies referenced from here. */
22043 prune_unused_types_walk_attribs (die);
22046 die->die_mark = 2;
22048 /* Mark children. */
22049 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22052 /* Increment the string counts on strings referred to from DIE's
22053 attributes. */
22055 static void
22056 prune_unused_types_update_strings (dw_die_ref die)
22058 dw_attr_ref a;
22059 unsigned ix;
22061 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22062 if (AT_class (a) == dw_val_class_str)
22064 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22065 s->refcount++;
22066 /* Avoid unnecessarily putting strings that are used less than
22067 twice in the hash table. */
22068 if (s->refcount
22069 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22071 void ** slot;
22072 slot = htab_find_slot_with_hash (debug_str_hash, s->str,
22073 htab_hash_string (s->str),
22074 INSERT);
22075 gcc_assert (*slot == NULL);
22076 *slot = s;
22081 /* Remove from the tree DIE any dies that aren't marked. */
22083 static void
22084 prune_unused_types_prune (dw_die_ref die)
22086 dw_die_ref c;
22087 int pruned = 0;
22089 gcc_assert (die->die_mark);
22090 prune_unused_types_update_strings (die);
22092 if (! die->die_child)
22093 return;
22095 c = die->die_child;
22096 do {
22097 dw_die_ref prev = c;
22098 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
22099 if (c == die->die_child)
22101 /* No marked children between 'prev' and the end of the list. */
22102 if (prev == c)
22103 /* No marked children at all. */
22104 die->die_child = NULL;
22105 else
22107 prev->die_sib = c->die_sib;
22108 die->die_child = prev;
22110 pruned = 1;
22111 goto finished;
22114 if (c != prev->die_sib)
22116 prev->die_sib = c;
22117 pruned = 1;
22119 prune_unused_types_prune (c);
22120 } while (c != die->die_child);
22122 finished:
22123 /* If we pruned children, and this is a class, mark it as a
22124 declaration to inform debuggers that this is not a complete
22125 class definition. */
22126 if (pruned && die->die_mark == 1 && class_scope_p (die)
22127 && ! is_declaration_die (die))
22128 add_AT_flag (die, DW_AT_declaration, 1);
22131 /* Remove dies representing declarations that we never use. */
22133 static void
22134 prune_unused_types (void)
22136 unsigned int i;
22137 limbo_die_node *node;
22138 comdat_type_node *ctnode;
22139 pubname_ref pub;
22140 dw_die_ref base_type;
22142 #if ENABLE_ASSERT_CHECKING
22143 /* All the marks should already be clear. */
22144 verify_marks_clear (comp_unit_die ());
22145 for (node = limbo_die_list; node; node = node->next)
22146 verify_marks_clear (node->die);
22147 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22148 verify_marks_clear (ctnode->root_die);
22149 #endif /* ENABLE_ASSERT_CHECKING */
22151 /* Mark types that are used in global variables. */
22152 premark_types_used_by_global_vars ();
22154 /* Set the mark on nodes that are actually used. */
22155 prune_unused_types_walk (comp_unit_die ());
22156 for (node = limbo_die_list; node; node = node->next)
22157 prune_unused_types_walk (node->die);
22158 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22160 prune_unused_types_walk (ctnode->root_die);
22161 prune_unused_types_mark (ctnode->type_die, 1);
22164 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
22165 are unusual in that they are pubnames that are the children of pubtypes.
22166 They should only be marked via their parent DW_TAG_enumeration_type die,
22167 not as roots in themselves. */
22168 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
22169 if (pub->die->die_tag != DW_TAG_enumerator)
22170 prune_unused_types_mark (pub->die, 1);
22171 for (i = 0; base_types.iterate (i, &base_type); i++)
22172 prune_unused_types_mark (base_type, 1);
22174 if (debug_str_hash)
22175 htab_empty (debug_str_hash);
22176 prune_unused_types_prune (comp_unit_die ());
22177 for (node = limbo_die_list; node; node = node->next)
22178 prune_unused_types_prune (node->die);
22179 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22180 prune_unused_types_prune (ctnode->root_die);
22182 /* Leave the marks clear. */
22183 prune_unmark_dies (comp_unit_die ());
22184 for (node = limbo_die_list; node; node = node->next)
22185 prune_unmark_dies (node->die);
22186 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22187 prune_unmark_dies (ctnode->root_die);
22190 /* Set the parameter to true if there are any relative pathnames in
22191 the file table. */
22192 static int
22193 file_table_relative_p (void ** slot, void *param)
22195 bool *p = (bool *) param;
22196 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
22197 if (!IS_ABSOLUTE_PATH (d->filename))
22199 *p = true;
22200 return 0;
22202 return 1;
22205 /* Routines to manipulate hash table of comdat type units. */
22207 static hashval_t
22208 htab_ct_hash (const void *of)
22210 hashval_t h;
22211 const comdat_type_node *const type_node = (const comdat_type_node *) of;
22213 memcpy (&h, type_node->signature, sizeof (h));
22214 return h;
22217 static int
22218 htab_ct_eq (const void *of1, const void *of2)
22220 const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
22221 const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
22223 return (! memcmp (type_node_1->signature, type_node_2->signature,
22224 DWARF_TYPE_SIGNATURE_SIZE));
22227 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
22228 to the location it would have been added, should we know its
22229 DECL_ASSEMBLER_NAME when we added other attributes. This will
22230 probably improve compactness of debug info, removing equivalent
22231 abbrevs, and hide any differences caused by deferring the
22232 computation of the assembler name, triggered by e.g. PCH. */
22234 static inline void
22235 move_linkage_attr (dw_die_ref die)
22237 unsigned ix = vec_safe_length (die->die_attr);
22238 dw_attr_node linkage = (*die->die_attr)[ix - 1];
22240 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
22241 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
22243 while (--ix > 0)
22245 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
22247 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
22248 break;
22251 if (ix != vec_safe_length (die->die_attr) - 1)
22253 die->die_attr->pop ();
22254 die->die_attr->quick_insert (ix, linkage);
22258 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
22259 referenced from typed stack ops and count how often they are used. */
22261 static void
22262 mark_base_types (dw_loc_descr_ref loc)
22264 dw_die_ref base_type = NULL;
22266 for (; loc; loc = loc->dw_loc_next)
22268 switch (loc->dw_loc_opc)
22270 case DW_OP_GNU_regval_type:
22271 case DW_OP_GNU_deref_type:
22272 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
22273 break;
22274 case DW_OP_GNU_convert:
22275 case DW_OP_GNU_reinterpret:
22276 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
22277 continue;
22278 /* FALLTHRU */
22279 case DW_OP_GNU_const_type:
22280 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
22281 break;
22282 case DW_OP_GNU_entry_value:
22283 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
22284 continue;
22285 default:
22286 continue;
22288 gcc_assert (base_type->die_parent == comp_unit_die ());
22289 if (base_type->die_mark)
22290 base_type->die_mark++;
22291 else
22293 base_types.safe_push (base_type);
22294 base_type->die_mark = 1;
22299 /* Comparison function for sorting marked base types. */
22301 static int
22302 base_type_cmp (const void *x, const void *y)
22304 dw_die_ref dx = *(const dw_die_ref *) x;
22305 dw_die_ref dy = *(const dw_die_ref *) y;
22306 unsigned int byte_size1, byte_size2;
22307 unsigned int encoding1, encoding2;
22308 if (dx->die_mark > dy->die_mark)
22309 return -1;
22310 if (dx->die_mark < dy->die_mark)
22311 return 1;
22312 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
22313 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
22314 if (byte_size1 < byte_size2)
22315 return 1;
22316 if (byte_size1 > byte_size2)
22317 return -1;
22318 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
22319 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
22320 if (encoding1 < encoding2)
22321 return 1;
22322 if (encoding1 > encoding2)
22323 return -1;
22324 return 0;
22327 /* Move base types marked by mark_base_types as early as possible
22328 in the CU, sorted by decreasing usage count both to make the
22329 uleb128 references as small as possible and to make sure they
22330 will have die_offset already computed by calc_die_sizes when
22331 sizes of typed stack loc ops is computed. */
22333 static void
22334 move_marked_base_types (void)
22336 unsigned int i;
22337 dw_die_ref base_type, die, c;
22339 if (base_types.is_empty ())
22340 return;
22342 /* Sort by decreasing usage count, they will be added again in that
22343 order later on. */
22344 base_types.qsort (base_type_cmp);
22345 die = comp_unit_die ();
22346 c = die->die_child;
22349 dw_die_ref prev = c;
22350 c = c->die_sib;
22351 while (c->die_mark)
22353 remove_child_with_prev (c, prev);
22354 /* As base types got marked, there must be at least
22355 one node other than DW_TAG_base_type. */
22356 gcc_assert (c != c->die_sib);
22357 c = c->die_sib;
22360 while (c != die->die_child);
22361 gcc_assert (die->die_child);
22362 c = die->die_child;
22363 for (i = 0; base_types.iterate (i, &base_type); i++)
22365 base_type->die_mark = 0;
22366 base_type->die_sib = c->die_sib;
22367 c->die_sib = base_type;
22368 c = base_type;
22372 /* Helper function for resolve_addr, attempt to resolve
22373 one CONST_STRING, return non-zero if not successful. Similarly verify that
22374 SYMBOL_REFs refer to variables emitted in the current CU. */
22376 static int
22377 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
22379 rtx rtl = *addr;
22381 if (GET_CODE (rtl) == CONST_STRING)
22383 size_t len = strlen (XSTR (rtl, 0)) + 1;
22384 tree t = build_string (len, XSTR (rtl, 0));
22385 tree tlen = size_int (len - 1);
22386 TREE_TYPE (t)
22387 = build_array_type (char_type_node, build_index_type (tlen));
22388 rtl = lookup_constant_def (t);
22389 if (!rtl || !MEM_P (rtl))
22390 return 1;
22391 rtl = XEXP (rtl, 0);
22392 vec_safe_push (used_rtx_array, rtl);
22393 *addr = rtl;
22394 return 0;
22397 if (GET_CODE (rtl) == SYMBOL_REF
22398 && SYMBOL_REF_DECL (rtl))
22400 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
22402 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
22403 return 1;
22405 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22406 return 1;
22409 if (GET_CODE (rtl) == CONST
22410 && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
22411 return 1;
22413 return 0;
22416 /* Helper function for resolve_addr, handle one location
22417 expression, return false if at least one CONST_STRING or SYMBOL_REF in
22418 the location list couldn't be resolved. */
22420 static bool
22421 resolve_addr_in_expr (dw_loc_descr_ref loc)
22423 dw_loc_descr_ref keep = NULL;
22424 for (; loc; loc = loc->dw_loc_next)
22425 switch (loc->dw_loc_opc)
22427 case DW_OP_addr:
22428 if (resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
22429 return false;
22430 break;
22431 case DW_OP_GNU_addr_index:
22432 case DW_OP_GNU_const_index:
22434 if ((loc->dw_loc_opc == DW_OP_GNU_addr_index
22435 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
22436 && resolve_one_addr (&loc->dw_loc_oprnd1.val_entry->addr.rtl,
22437 NULL))
22438 return false;
22440 break;
22441 case DW_OP_const4u:
22442 case DW_OP_const8u:
22443 if (loc->dtprel
22444 && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
22445 return false;
22446 break;
22447 case DW_OP_plus_uconst:
22448 if (size_of_loc_descr (loc)
22449 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
22451 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
22453 dw_loc_descr_ref repl
22454 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
22455 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
22456 add_loc_descr (&repl, loc->dw_loc_next);
22457 *loc = *repl;
22459 break;
22460 case DW_OP_implicit_value:
22461 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
22462 && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL))
22463 return false;
22464 break;
22465 case DW_OP_GNU_implicit_pointer:
22466 case DW_OP_GNU_parameter_ref:
22467 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
22469 dw_die_ref ref
22470 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
22471 if (ref == NULL)
22472 return false;
22473 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
22474 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
22475 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
22477 break;
22478 case DW_OP_GNU_const_type:
22479 case DW_OP_GNU_regval_type:
22480 case DW_OP_GNU_deref_type:
22481 case DW_OP_GNU_convert:
22482 case DW_OP_GNU_reinterpret:
22483 while (loc->dw_loc_next
22484 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
22486 dw_die_ref base1, base2;
22487 unsigned enc1, enc2, size1, size2;
22488 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
22489 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
22490 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
22491 else if (loc->dw_loc_oprnd1.val_class
22492 == dw_val_class_unsigned_const)
22493 break;
22494 else
22495 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
22496 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
22497 == dw_val_class_unsigned_const)
22498 break;
22499 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
22500 gcc_assert (base1->die_tag == DW_TAG_base_type
22501 && base2->die_tag == DW_TAG_base_type);
22502 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
22503 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
22504 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
22505 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
22506 if (size1 == size2
22507 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
22508 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
22509 && loc != keep)
22510 || enc1 == enc2))
22512 /* Optimize away next DW_OP_GNU_convert after
22513 adjusting LOC's base type die reference. */
22514 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
22515 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
22516 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
22517 else
22518 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
22519 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
22520 continue;
22522 /* Don't change integer DW_OP_GNU_convert after e.g. floating
22523 point typed stack entry. */
22524 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
22525 keep = loc->dw_loc_next;
22526 break;
22528 break;
22529 default:
22530 break;
22532 return true;
22535 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
22536 an address in .rodata section if the string literal is emitted there,
22537 or remove the containing location list or replace DW_AT_const_value
22538 with DW_AT_location and empty location expression, if it isn't found
22539 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
22540 to something that has been emitted in the current CU. */
22542 static void
22543 resolve_addr (dw_die_ref die)
22545 dw_die_ref c;
22546 dw_attr_ref a;
22547 dw_loc_list_ref *curr, *start, loc;
22548 unsigned ix;
22550 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22551 switch (AT_class (a))
22553 case dw_val_class_loc_list:
22554 start = curr = AT_loc_list_ptr (a);
22555 loc = *curr;
22556 gcc_assert (loc);
22557 /* The same list can be referenced more than once. See if we have
22558 already recorded the result from a previous pass. */
22559 if (loc->replaced)
22560 *curr = loc->dw_loc_next;
22561 else if (!loc->resolved_addr)
22563 /* As things stand, we do not expect or allow one die to
22564 reference a suffix of another die's location list chain.
22565 References must be identical or completely separate.
22566 There is therefore no need to cache the result of this
22567 pass on any list other than the first; doing so
22568 would lead to unnecessary writes. */
22569 while (*curr)
22571 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
22572 if (!resolve_addr_in_expr ((*curr)->expr))
22574 dw_loc_list_ref next = (*curr)->dw_loc_next;
22575 dw_loc_descr_ref l = (*curr)->expr;
22577 if (next && (*curr)->ll_symbol)
22579 gcc_assert (!next->ll_symbol);
22580 next->ll_symbol = (*curr)->ll_symbol;
22582 if (dwarf_split_debug_info)
22583 remove_loc_list_addr_table_entries (l);
22584 *curr = next;
22586 else
22588 mark_base_types ((*curr)->expr);
22589 curr = &(*curr)->dw_loc_next;
22592 if (loc == *start)
22593 loc->resolved_addr = 1;
22594 else
22596 loc->replaced = 1;
22597 if (dwarf_split_debug_info)
22598 remove_loc_list_addr_table_entries (loc->expr);
22599 loc->dw_loc_next = *start;
22602 if (!*start)
22604 remove_AT (die, a->dw_attr);
22605 ix--;
22607 break;
22608 case dw_val_class_loc:
22610 dw_loc_descr_ref l = AT_loc (a);
22611 /* For -gdwarf-2 don't attempt to optimize
22612 DW_AT_data_member_location containing
22613 DW_OP_plus_uconst - older consumers might
22614 rely on it being that op instead of a more complex,
22615 but shorter, location description. */
22616 if ((dwarf_version > 2
22617 || a->dw_attr != DW_AT_data_member_location
22618 || l == NULL
22619 || l->dw_loc_opc != DW_OP_plus_uconst
22620 || l->dw_loc_next != NULL)
22621 && !resolve_addr_in_expr (l))
22623 if (dwarf_split_debug_info)
22624 remove_loc_list_addr_table_entries (l);
22625 remove_AT (die, a->dw_attr);
22626 ix--;
22628 else
22629 mark_base_types (l);
22631 break;
22632 case dw_val_class_addr:
22633 if (a->dw_attr == DW_AT_const_value
22634 && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
22636 if (AT_index (a) != NOT_INDEXED)
22637 remove_addr_table_entry (a->dw_attr_val.val_entry);
22638 remove_AT (die, a->dw_attr);
22639 ix--;
22641 if (die->die_tag == DW_TAG_GNU_call_site
22642 && a->dw_attr == DW_AT_abstract_origin)
22644 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
22645 dw_die_ref tdie = lookup_decl_die (tdecl);
22646 if (tdie == NULL
22647 && DECL_EXTERNAL (tdecl)
22648 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
22650 force_decl_die (tdecl);
22651 tdie = lookup_decl_die (tdecl);
22653 if (tdie)
22655 a->dw_attr_val.val_class = dw_val_class_die_ref;
22656 a->dw_attr_val.v.val_die_ref.die = tdie;
22657 a->dw_attr_val.v.val_die_ref.external = 0;
22659 else
22661 if (AT_index (a) != NOT_INDEXED)
22662 remove_addr_table_entry (a->dw_attr_val.val_entry);
22663 remove_AT (die, a->dw_attr);
22664 ix--;
22667 break;
22668 default:
22669 break;
22672 FOR_EACH_CHILD (die, c, resolve_addr (c));
22675 /* Helper routines for optimize_location_lists.
22676 This pass tries to share identical local lists in .debug_loc
22677 section. */
22679 /* Iteratively hash operands of LOC opcode. */
22681 static inline hashval_t
22682 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
22684 dw_val_ref val1 = &loc->dw_loc_oprnd1;
22685 dw_val_ref val2 = &loc->dw_loc_oprnd2;
22687 switch (loc->dw_loc_opc)
22689 case DW_OP_const4u:
22690 case DW_OP_const8u:
22691 if (loc->dtprel)
22692 goto hash_addr;
22693 /* FALLTHRU */
22694 case DW_OP_const1u:
22695 case DW_OP_const1s:
22696 case DW_OP_const2u:
22697 case DW_OP_const2s:
22698 case DW_OP_const4s:
22699 case DW_OP_const8s:
22700 case DW_OP_constu:
22701 case DW_OP_consts:
22702 case DW_OP_pick:
22703 case DW_OP_plus_uconst:
22704 case DW_OP_breg0:
22705 case DW_OP_breg1:
22706 case DW_OP_breg2:
22707 case DW_OP_breg3:
22708 case DW_OP_breg4:
22709 case DW_OP_breg5:
22710 case DW_OP_breg6:
22711 case DW_OP_breg7:
22712 case DW_OP_breg8:
22713 case DW_OP_breg9:
22714 case DW_OP_breg10:
22715 case DW_OP_breg11:
22716 case DW_OP_breg12:
22717 case DW_OP_breg13:
22718 case DW_OP_breg14:
22719 case DW_OP_breg15:
22720 case DW_OP_breg16:
22721 case DW_OP_breg17:
22722 case DW_OP_breg18:
22723 case DW_OP_breg19:
22724 case DW_OP_breg20:
22725 case DW_OP_breg21:
22726 case DW_OP_breg22:
22727 case DW_OP_breg23:
22728 case DW_OP_breg24:
22729 case DW_OP_breg25:
22730 case DW_OP_breg26:
22731 case DW_OP_breg27:
22732 case DW_OP_breg28:
22733 case DW_OP_breg29:
22734 case DW_OP_breg30:
22735 case DW_OP_breg31:
22736 case DW_OP_regx:
22737 case DW_OP_fbreg:
22738 case DW_OP_piece:
22739 case DW_OP_deref_size:
22740 case DW_OP_xderef_size:
22741 hash = iterative_hash_object (val1->v.val_int, hash);
22742 break;
22743 case DW_OP_skip:
22744 case DW_OP_bra:
22746 int offset;
22748 gcc_assert (val1->val_class == dw_val_class_loc);
22749 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
22750 hash = iterative_hash_object (offset, hash);
22752 break;
22753 case DW_OP_implicit_value:
22754 hash = iterative_hash_object (val1->v.val_unsigned, hash);
22755 switch (val2->val_class)
22757 case dw_val_class_const:
22758 hash = iterative_hash_object (val2->v.val_int, hash);
22759 break;
22760 case dw_val_class_vec:
22762 unsigned int elt_size = val2->v.val_vec.elt_size;
22763 unsigned int len = val2->v.val_vec.length;
22765 hash = iterative_hash_object (elt_size, hash);
22766 hash = iterative_hash_object (len, hash);
22767 hash = iterative_hash (val2->v.val_vec.array,
22768 len * elt_size, hash);
22770 break;
22771 case dw_val_class_const_double:
22772 hash = iterative_hash_object (val2->v.val_double.low, hash);
22773 hash = iterative_hash_object (val2->v.val_double.high, hash);
22774 break;
22775 case dw_val_class_addr:
22776 hash = iterative_hash_rtx (val2->v.val_addr, hash);
22777 break;
22778 default:
22779 gcc_unreachable ();
22781 break;
22782 case DW_OP_bregx:
22783 case DW_OP_bit_piece:
22784 hash = iterative_hash_object (val1->v.val_int, hash);
22785 hash = iterative_hash_object (val2->v.val_int, hash);
22786 break;
22787 case DW_OP_addr:
22788 hash_addr:
22789 if (loc->dtprel)
22791 unsigned char dtprel = 0xd1;
22792 hash = iterative_hash_object (dtprel, hash);
22794 hash = iterative_hash_rtx (val1->v.val_addr, hash);
22795 break;
22796 case DW_OP_GNU_addr_index:
22797 case DW_OP_GNU_const_index:
22799 if (loc->dtprel)
22801 unsigned char dtprel = 0xd1;
22802 hash = iterative_hash_object (dtprel, hash);
22804 hash = iterative_hash_rtx (val1->val_entry->addr.rtl, hash);
22806 break;
22807 case DW_OP_GNU_implicit_pointer:
22808 hash = iterative_hash_object (val2->v.val_int, hash);
22809 break;
22810 case DW_OP_GNU_entry_value:
22811 hash = hash_loc_operands (val1->v.val_loc, hash);
22812 break;
22813 case DW_OP_GNU_regval_type:
22814 case DW_OP_GNU_deref_type:
22816 unsigned int byte_size
22817 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
22818 unsigned int encoding
22819 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
22820 hash = iterative_hash_object (val1->v.val_int, hash);
22821 hash = iterative_hash_object (byte_size, hash);
22822 hash = iterative_hash_object (encoding, hash);
22824 break;
22825 case DW_OP_GNU_convert:
22826 case DW_OP_GNU_reinterpret:
22827 if (val1->val_class == dw_val_class_unsigned_const)
22829 hash = iterative_hash_object (val1->v.val_unsigned, hash);
22830 break;
22832 /* FALLTHRU */
22833 case DW_OP_GNU_const_type:
22835 unsigned int byte_size
22836 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
22837 unsigned int encoding
22838 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
22839 hash = iterative_hash_object (byte_size, hash);
22840 hash = iterative_hash_object (encoding, hash);
22841 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
22842 break;
22843 hash = iterative_hash_object (val2->val_class, hash);
22844 switch (val2->val_class)
22846 case dw_val_class_const:
22847 hash = iterative_hash_object (val2->v.val_int, hash);
22848 break;
22849 case dw_val_class_vec:
22851 unsigned int elt_size = val2->v.val_vec.elt_size;
22852 unsigned int len = val2->v.val_vec.length;
22854 hash = iterative_hash_object (elt_size, hash);
22855 hash = iterative_hash_object (len, hash);
22856 hash = iterative_hash (val2->v.val_vec.array,
22857 len * elt_size, hash);
22859 break;
22860 case dw_val_class_const_double:
22861 hash = iterative_hash_object (val2->v.val_double.low, hash);
22862 hash = iterative_hash_object (val2->v.val_double.high, hash);
22863 break;
22864 default:
22865 gcc_unreachable ();
22868 break;
22870 default:
22871 /* Other codes have no operands. */
22872 break;
22874 return hash;
22877 /* Iteratively hash the whole DWARF location expression LOC. */
22879 static inline hashval_t
22880 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
22882 dw_loc_descr_ref l;
22883 bool sizes_computed = false;
22884 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
22885 size_of_locs (loc);
22887 for (l = loc; l != NULL; l = l->dw_loc_next)
22889 enum dwarf_location_atom opc = l->dw_loc_opc;
22890 hash = iterative_hash_object (opc, hash);
22891 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
22893 size_of_locs (loc);
22894 sizes_computed = true;
22896 hash = hash_loc_operands (l, hash);
22898 return hash;
22901 /* Compute hash of the whole location list LIST_HEAD. */
22903 static inline void
22904 hash_loc_list (dw_loc_list_ref list_head)
22906 dw_loc_list_ref curr = list_head;
22907 hashval_t hash = 0;
22909 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
22911 hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
22912 hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
22913 if (curr->section)
22914 hash = iterative_hash (curr->section, strlen (curr->section) + 1,
22915 hash);
22916 hash = hash_locs (curr->expr, hash);
22918 list_head->hash = hash;
22921 /* Return true if X and Y opcodes have the same operands. */
22923 static inline bool
22924 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
22926 dw_val_ref valx1 = &x->dw_loc_oprnd1;
22927 dw_val_ref valx2 = &x->dw_loc_oprnd2;
22928 dw_val_ref valy1 = &y->dw_loc_oprnd1;
22929 dw_val_ref valy2 = &y->dw_loc_oprnd2;
22931 switch (x->dw_loc_opc)
22933 case DW_OP_const4u:
22934 case DW_OP_const8u:
22935 if (x->dtprel)
22936 goto hash_addr;
22937 /* FALLTHRU */
22938 case DW_OP_const1u:
22939 case DW_OP_const1s:
22940 case DW_OP_const2u:
22941 case DW_OP_const2s:
22942 case DW_OP_const4s:
22943 case DW_OP_const8s:
22944 case DW_OP_constu:
22945 case DW_OP_consts:
22946 case DW_OP_pick:
22947 case DW_OP_plus_uconst:
22948 case DW_OP_breg0:
22949 case DW_OP_breg1:
22950 case DW_OP_breg2:
22951 case DW_OP_breg3:
22952 case DW_OP_breg4:
22953 case DW_OP_breg5:
22954 case DW_OP_breg6:
22955 case DW_OP_breg7:
22956 case DW_OP_breg8:
22957 case DW_OP_breg9:
22958 case DW_OP_breg10:
22959 case DW_OP_breg11:
22960 case DW_OP_breg12:
22961 case DW_OP_breg13:
22962 case DW_OP_breg14:
22963 case DW_OP_breg15:
22964 case DW_OP_breg16:
22965 case DW_OP_breg17:
22966 case DW_OP_breg18:
22967 case DW_OP_breg19:
22968 case DW_OP_breg20:
22969 case DW_OP_breg21:
22970 case DW_OP_breg22:
22971 case DW_OP_breg23:
22972 case DW_OP_breg24:
22973 case DW_OP_breg25:
22974 case DW_OP_breg26:
22975 case DW_OP_breg27:
22976 case DW_OP_breg28:
22977 case DW_OP_breg29:
22978 case DW_OP_breg30:
22979 case DW_OP_breg31:
22980 case DW_OP_regx:
22981 case DW_OP_fbreg:
22982 case DW_OP_piece:
22983 case DW_OP_deref_size:
22984 case DW_OP_xderef_size:
22985 return valx1->v.val_int == valy1->v.val_int;
22986 case DW_OP_skip:
22987 case DW_OP_bra:
22988 /* If splitting debug info, the use of DW_OP_GNU_addr_index
22989 can cause irrelevant differences in dw_loc_addr. */
22990 gcc_assert (valx1->val_class == dw_val_class_loc
22991 && valy1->val_class == dw_val_class_loc
22992 && (dwarf_split_debug_info
22993 || x->dw_loc_addr == y->dw_loc_addr));
22994 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
22995 case DW_OP_implicit_value:
22996 if (valx1->v.val_unsigned != valy1->v.val_unsigned
22997 || valx2->val_class != valy2->val_class)
22998 return false;
22999 switch (valx2->val_class)
23001 case dw_val_class_const:
23002 return valx2->v.val_int == valy2->v.val_int;
23003 case dw_val_class_vec:
23004 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23005 && valx2->v.val_vec.length == valy2->v.val_vec.length
23006 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23007 valx2->v.val_vec.elt_size
23008 * valx2->v.val_vec.length) == 0;
23009 case dw_val_class_const_double:
23010 return valx2->v.val_double.low == valy2->v.val_double.low
23011 && valx2->v.val_double.high == valy2->v.val_double.high;
23012 case dw_val_class_addr:
23013 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
23014 default:
23015 gcc_unreachable ();
23017 case DW_OP_bregx:
23018 case DW_OP_bit_piece:
23019 return valx1->v.val_int == valy1->v.val_int
23020 && valx2->v.val_int == valy2->v.val_int;
23021 case DW_OP_addr:
23022 hash_addr:
23023 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
23024 case DW_OP_GNU_addr_index:
23025 case DW_OP_GNU_const_index:
23027 rtx ax1 = valx1->val_entry->addr.rtl;
23028 rtx ay1 = valy1->val_entry->addr.rtl;
23029 return rtx_equal_p (ax1, ay1);
23031 case DW_OP_GNU_implicit_pointer:
23032 return valx1->val_class == dw_val_class_die_ref
23033 && valx1->val_class == valy1->val_class
23034 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
23035 && valx2->v.val_int == valy2->v.val_int;
23036 case DW_OP_GNU_entry_value:
23037 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
23038 case DW_OP_GNU_const_type:
23039 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
23040 || valx2->val_class != valy2->val_class)
23041 return false;
23042 switch (valx2->val_class)
23044 case dw_val_class_const:
23045 return valx2->v.val_int == valy2->v.val_int;
23046 case dw_val_class_vec:
23047 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23048 && valx2->v.val_vec.length == valy2->v.val_vec.length
23049 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23050 valx2->v.val_vec.elt_size
23051 * valx2->v.val_vec.length) == 0;
23052 case dw_val_class_const_double:
23053 return valx2->v.val_double.low == valy2->v.val_double.low
23054 && valx2->v.val_double.high == valy2->v.val_double.high;
23055 default:
23056 gcc_unreachable ();
23058 case DW_OP_GNU_regval_type:
23059 case DW_OP_GNU_deref_type:
23060 return valx1->v.val_int == valy1->v.val_int
23061 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
23062 case DW_OP_GNU_convert:
23063 case DW_OP_GNU_reinterpret:
23064 if (valx1->val_class != valy1->val_class)
23065 return false;
23066 if (valx1->val_class == dw_val_class_unsigned_const)
23067 return valx1->v.val_unsigned == valy1->v.val_unsigned;
23068 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
23069 case DW_OP_GNU_parameter_ref:
23070 return valx1->val_class == dw_val_class_die_ref
23071 && valx1->val_class == valy1->val_class
23072 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
23073 default:
23074 /* Other codes have no operands. */
23075 return true;
23079 /* Return true if DWARF location expressions X and Y are the same. */
23081 static inline bool
23082 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
23084 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
23085 if (x->dw_loc_opc != y->dw_loc_opc
23086 || x->dtprel != y->dtprel
23087 || !compare_loc_operands (x, y))
23088 break;
23089 return x == NULL && y == NULL;
23092 /* Return precomputed hash of location list X. */
23094 static hashval_t
23095 loc_list_hash (const void *x)
23097 return ((const struct dw_loc_list_struct *) x)->hash;
23100 /* Return 1 if location lists X and Y are the same. */
23102 static int
23103 loc_list_eq (const void *x, const void *y)
23105 const struct dw_loc_list_struct *a = (const struct dw_loc_list_struct *) x;
23106 const struct dw_loc_list_struct *b = (const struct dw_loc_list_struct *) y;
23107 if (a == b)
23108 return 1;
23109 if (a->hash != b->hash)
23110 return 0;
23111 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
23112 if (strcmp (a->begin, b->begin) != 0
23113 || strcmp (a->end, b->end) != 0
23114 || (a->section == NULL) != (b->section == NULL)
23115 || (a->section && strcmp (a->section, b->section) != 0)
23116 || !compare_locs (a->expr, b->expr))
23117 break;
23118 return a == NULL && b == NULL;
23121 /* Recursively optimize location lists referenced from DIE
23122 children and share them whenever possible. */
23124 static void
23125 optimize_location_lists_1 (dw_die_ref die, htab_t htab)
23127 dw_die_ref c;
23128 dw_attr_ref a;
23129 unsigned ix;
23130 void **slot;
23132 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23133 if (AT_class (a) == dw_val_class_loc_list)
23135 dw_loc_list_ref list = AT_loc_list (a);
23136 /* TODO: perform some optimizations here, before hashing
23137 it and storing into the hash table. */
23138 hash_loc_list (list);
23139 slot = htab_find_slot_with_hash (htab, list, list->hash,
23140 INSERT);
23141 if (*slot == NULL)
23142 *slot = (void *) list;
23143 else
23144 a->dw_attr_val.v.val_loc_list = (dw_loc_list_ref) *slot;
23147 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
23151 /* Recursively assign each location list a unique index into the debug_addr
23152 section. */
23154 static void
23155 index_location_lists (dw_die_ref die)
23157 dw_die_ref c;
23158 dw_attr_ref a;
23159 unsigned ix;
23161 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23162 if (AT_class (a) == dw_val_class_loc_list)
23164 dw_loc_list_ref list = AT_loc_list (a);
23165 dw_loc_list_ref curr;
23166 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
23168 /* Don't index an entry that has already been indexed
23169 or won't be output. */
23170 if (curr->begin_entry != NULL
23171 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
23172 continue;
23174 curr->begin_entry
23175 = add_addr_table_entry (xstrdup (curr->begin),
23176 ate_kind_label);
23180 FOR_EACH_CHILD (die, c, index_location_lists (c));
23183 /* Optimize location lists referenced from DIE
23184 children and share them whenever possible. */
23186 static void
23187 optimize_location_lists (dw_die_ref die)
23189 htab_t htab = htab_create (500, loc_list_hash, loc_list_eq, NULL);
23190 optimize_location_lists_1 (die, htab);
23191 htab_delete (htab);
23194 /* Output stuff that dwarf requires at the end of every file,
23195 and generate the DWARF-2 debugging info. */
23197 static void
23198 dwarf2out_finish (const char *filename)
23200 limbo_die_node *node, *next_node;
23201 comdat_type_node *ctnode;
23202 htab_t comdat_type_table;
23203 unsigned int i;
23204 dw_die_ref main_comp_unit_die;
23206 /* PCH might result in DW_AT_producer string being restored from the
23207 header compilation, so always fill it with empty string initially
23208 and overwrite only here. */
23209 dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
23210 producer_string = gen_producer_string ();
23211 producer->dw_attr_val.v.val_str->refcount--;
23212 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
23214 gen_scheduled_generic_parms_dies ();
23215 gen_remaining_tmpl_value_param_die_attribute ();
23217 /* Add the name for the main input file now. We delayed this from
23218 dwarf2out_init to avoid complications with PCH. */
23219 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
23220 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
23221 add_comp_dir_attribute (comp_unit_die ());
23222 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
23224 bool p = false;
23225 htab_traverse (file_table, file_table_relative_p, &p);
23226 if (p)
23227 add_comp_dir_attribute (comp_unit_die ());
23230 if (deferred_locations_list)
23231 for (i = 0; i < deferred_locations_list->length (); i++)
23233 add_location_or_const_value_attribute (
23234 (*deferred_locations_list)[i].die,
23235 (*deferred_locations_list)[i].variable,
23236 false,
23237 DW_AT_location);
23240 /* Traverse the limbo die list, and add parent/child links. The only
23241 dies without parents that should be here are concrete instances of
23242 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
23243 For concrete instances, we can get the parent die from the abstract
23244 instance. */
23245 for (node = limbo_die_list; node; node = next_node)
23247 dw_die_ref die = node->die;
23248 next_node = node->next;
23250 if (die->die_parent == NULL)
23252 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
23254 if (origin && origin->die_parent)
23255 add_child_die (origin->die_parent, die);
23256 else if (is_cu_die (die))
23258 else if (seen_error ())
23259 /* It's OK to be confused by errors in the input. */
23260 add_child_die (comp_unit_die (), die);
23261 else
23263 /* In certain situations, the lexical block containing a
23264 nested function can be optimized away, which results
23265 in the nested function die being orphaned. Likewise
23266 with the return type of that nested function. Force
23267 this to be a child of the containing function.
23269 It may happen that even the containing function got fully
23270 inlined and optimized out. In that case we are lost and
23271 assign the empty child. This should not be big issue as
23272 the function is likely unreachable too. */
23273 gcc_assert (node->created_for);
23275 if (DECL_P (node->created_for))
23276 origin = get_context_die (DECL_CONTEXT (node->created_for));
23277 else if (TYPE_P (node->created_for))
23278 origin = scope_die_for (node->created_for, comp_unit_die ());
23279 else
23280 origin = comp_unit_die ();
23282 add_child_die (origin, die);
23287 limbo_die_list = NULL;
23289 #if ENABLE_ASSERT_CHECKING
23291 dw_die_ref die = comp_unit_die (), c;
23292 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
23294 #endif
23295 resolve_addr (comp_unit_die ());
23296 move_marked_base_types ();
23298 for (node = deferred_asm_name; node; node = node->next)
23300 tree decl = node->created_for;
23301 /* When generating LTO bytecode we can not generate new assembler
23302 names at this point and all important decls got theirs via
23303 free-lang-data. */
23304 if ((!flag_generate_lto || DECL_ASSEMBLER_NAME_SET_P (decl))
23305 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
23307 add_linkage_attr (node->die, decl);
23308 move_linkage_attr (node->die);
23312 deferred_asm_name = NULL;
23314 /* Walk through the list of incomplete types again, trying once more to
23315 emit full debugging info for them. */
23316 retry_incomplete_types ();
23318 if (flag_eliminate_unused_debug_types)
23319 prune_unused_types ();
23321 /* Generate separate COMDAT sections for type DIEs. */
23322 if (use_debug_types)
23324 break_out_comdat_types (comp_unit_die ());
23326 /* Each new type_unit DIE was added to the limbo die list when created.
23327 Since these have all been added to comdat_type_list, clear the
23328 limbo die list. */
23329 limbo_die_list = NULL;
23331 /* For each new comdat type unit, copy declarations for incomplete
23332 types to make the new unit self-contained (i.e., no direct
23333 references to the main compile unit). */
23334 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23335 copy_decls_for_unworthy_types (ctnode->root_die);
23336 copy_decls_for_unworthy_types (comp_unit_die ());
23338 /* In the process of copying declarations from one unit to another,
23339 we may have left some declarations behind that are no longer
23340 referenced. Prune them. */
23341 prune_unused_types ();
23344 /* Generate separate CUs for each of the include files we've seen.
23345 They will go into limbo_die_list. */
23346 if (flag_eliminate_dwarf2_dups)
23347 break_out_includes (comp_unit_die ());
23349 /* Traverse the DIE's and add add sibling attributes to those DIE's
23350 that have children. */
23351 add_sibling_attributes (comp_unit_die ());
23352 for (node = limbo_die_list; node; node = node->next)
23353 add_sibling_attributes (node->die);
23354 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23355 add_sibling_attributes (ctnode->root_die);
23357 /* When splitting DWARF info, we put some attributes in the
23358 skeleton compile_unit DIE that remains in the .o, while
23359 most attributes go in the DWO compile_unit_die. */
23360 if (dwarf_split_debug_info)
23361 main_comp_unit_die = gen_compile_unit_die (NULL);
23362 else
23363 main_comp_unit_die = comp_unit_die ();
23365 /* Output a terminator label for the .text section. */
23366 switch_to_section (text_section);
23367 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
23368 if (cold_text_section)
23370 switch_to_section (cold_text_section);
23371 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
23374 /* We can only use the low/high_pc attributes if all of the code was
23375 in .text. */
23376 if (!have_multiple_function_sections
23377 || (dwarf_version < 3 && dwarf_strict))
23379 /* Don't add if the CU has no associated code. */
23380 if (text_section_used)
23381 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
23382 text_end_label, true);
23384 else
23386 unsigned fde_idx;
23387 dw_fde_ref fde;
23388 bool range_list_added = false;
23390 if (text_section_used)
23391 add_ranges_by_labels (main_comp_unit_die, text_section_label,
23392 text_end_label, &range_list_added, true);
23393 if (cold_text_section_used)
23394 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
23395 cold_end_label, &range_list_added, true);
23397 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
23399 if (DECL_IGNORED_P (fde->decl))
23400 continue;
23401 if (!fde->in_std_section)
23402 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
23403 fde->dw_fde_end, &range_list_added,
23404 true);
23405 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
23406 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
23407 fde->dw_fde_second_end, &range_list_added,
23408 true);
23411 if (range_list_added)
23413 /* We need to give .debug_loc and .debug_ranges an appropriate
23414 "base address". Use zero so that these addresses become
23415 absolute. Historically, we've emitted the unexpected
23416 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
23417 Emit both to give time for other tools to adapt. */
23418 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
23419 if (! dwarf_strict && dwarf_version < 4)
23420 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
23422 add_ranges (NULL);
23426 if (debug_info_level >= DINFO_LEVEL_NORMAL)
23427 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
23428 debug_line_section_label);
23430 if (have_macinfo)
23431 add_AT_macptr (comp_unit_die (),
23432 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
23433 macinfo_section_label);
23435 if (dwarf_split_debug_info && addr_index_table != NULL)
23437 /* optimize_location_lists calculates the size of the lists,
23438 so index them first, and assign indices to the entries.
23439 Although optimize_location_lists will remove entries from
23440 the table, it only does so for duplicates, and therefore
23441 only reduces ref_counts to 1. */
23442 unsigned int index = 0;
23443 index_location_lists (comp_unit_die ());
23444 htab_traverse_noresize (addr_index_table,
23445 index_addr_table_entry, &index);
23447 if (have_location_lists)
23448 optimize_location_lists (comp_unit_die ());
23450 save_macinfo_strings ();
23451 if (dwarf_split_debug_info)
23453 unsigned int index = 0;
23454 htab_traverse_noresize (debug_str_hash, index_string, &index);
23457 /* Output all of the compilation units. We put the main one last so that
23458 the offsets are available to output_pubnames. */
23459 for (node = limbo_die_list; node; node = node->next)
23460 output_comp_unit (node->die, 0);
23462 comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
23463 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23465 void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
23467 /* Don't output duplicate types. */
23468 if (*slot != HTAB_EMPTY_ENTRY)
23469 continue;
23471 /* Add a pointer to the line table for the main compilation unit
23472 so that the debugger can make sense of DW_AT_decl_file
23473 attributes. */
23474 if (debug_info_level >= DINFO_LEVEL_NORMAL)
23475 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
23476 (!dwarf_split_debug_info
23477 ? debug_line_section_label
23478 : debug_skeleton_line_section_label));
23480 output_comdat_type_unit (ctnode);
23481 *slot = ctnode;
23483 htab_delete (comdat_type_table);
23485 /* The AT_pubnames attribute needs to go in all skeleton dies, including
23486 both the main_cu and all skeleton TUs. Making this call unconditional
23487 would end up either adding a second copy of the AT_pubnames attribute, or
23488 requiring a special case in add_top_level_skeleton_die_attrs. */
23489 if (!dwarf_split_debug_info)
23490 add_AT_pubnames (comp_unit_die ());
23492 if (dwarf_split_debug_info)
23494 int mark;
23495 unsigned char checksum[16];
23496 struct md5_ctx ctx;
23498 /* Compute a checksum of the comp_unit to use as the dwo_id. */
23499 md5_init_ctx (&ctx);
23500 mark = 0;
23501 die_checksum (comp_unit_die (), &ctx, &mark);
23502 unmark_all_dies (comp_unit_die ());
23503 md5_finish_ctx (&ctx, checksum);
23505 /* Use the first 8 bytes of the checksum as the dwo_id,
23506 and add it to both comp-unit DIEs. */
23507 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
23508 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
23510 /* Add the base offset of the ranges table to the skeleton
23511 comp-unit DIE. */
23512 if (ranges_table_in_use)
23513 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
23514 ranges_section_label);
23516 switch_to_section (debug_addr_section);
23517 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
23518 output_addr_table ();
23521 /* Output the main compilation unit if non-empty or if .debug_macinfo
23522 or .debug_macro will be emitted. */
23523 output_comp_unit (comp_unit_die (), have_macinfo);
23525 if (dwarf_split_debug_info && info_section_emitted)
23526 output_skeleton_debug_sections (main_comp_unit_die);
23528 /* Output the abbreviation table. */
23529 if (abbrev_die_table_in_use != 1)
23531 switch_to_section (debug_abbrev_section);
23532 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
23533 output_abbrev_section ();
23536 /* Output location list section if necessary. */
23537 if (have_location_lists)
23539 /* Output the location lists info. */
23540 switch_to_section (debug_loc_section);
23541 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
23542 output_location_lists (comp_unit_die ());
23545 /* Output public names and types tables if necessary. */
23546 output_pubnames (pubname_table);
23547 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
23548 It shouldn't hurt to emit it always, since pure DWARF2 consumers
23549 simply won't look for the section. */
23550 output_pubnames (pubtype_table);
23552 /* Output the address range information if a CU (.debug_info section)
23553 was emitted. We output an empty table even if we had no functions
23554 to put in it. This because the consumer has no way to tell the
23555 difference between an empty table that we omitted and failure to
23556 generate a table that would have contained data. */
23557 if (info_section_emitted)
23559 unsigned long aranges_length = size_of_aranges ();
23561 switch_to_section (debug_aranges_section);
23562 output_aranges (aranges_length);
23565 /* Output ranges section if necessary. */
23566 if (ranges_table_in_use)
23568 switch_to_section (debug_ranges_section);
23569 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
23570 output_ranges ();
23573 /* Have to end the macro section. */
23574 if (have_macinfo)
23576 switch_to_section (debug_macinfo_section);
23577 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
23578 output_macinfo ();
23579 dw2_asm_output_data (1, 0, "End compilation unit");
23582 /* Output the source line correspondence table. We must do this
23583 even if there is no line information. Otherwise, on an empty
23584 translation unit, we will generate a present, but empty,
23585 .debug_info section. IRIX 6.5 `nm' will then complain when
23586 examining the file. This is done late so that any filenames
23587 used by the debug_info section are marked as 'used'. */
23588 switch_to_section (debug_line_section);
23589 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
23590 if (! DWARF2_ASM_LINE_DEBUG_INFO)
23591 output_line_info (false);
23593 if (dwarf_split_debug_info && info_section_emitted)
23595 switch_to_section (debug_skeleton_line_section);
23596 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
23597 output_line_info (true);
23600 /* If we emitted any indirect strings, output the string table too. */
23601 if (debug_str_hash)
23602 output_indirect_strings ();
23605 #include "gt-dwarf2out.h"