* vi.po: Update.
[official-gcc.git] / gcc / dwarf2out.c
blob06920a9d1fb3d6ce2e8069c685dfbacc98073d01
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 "dumpfile.h"
94 #include "opts.h"
96 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
97 static rtx last_var_location_insn;
98 static rtx cached_next_real_insn;
100 #ifdef VMS_DEBUGGING_INFO
101 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
103 /* Define this macro to be a nonzero value if the directory specifications
104 which are output in the debug info should end with a separator. */
105 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
106 /* Define this macro to evaluate to a nonzero value if GCC should refrain
107 from generating indirect strings in DWARF2 debug information, for instance
108 if your target is stuck with an old version of GDB that is unable to
109 process them properly or uses VMS Debug. */
110 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
111 #else
112 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
113 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
114 #endif
116 /* ??? Poison these here until it can be done generically. They've been
117 totally replaced in this file; make sure it stays that way. */
118 #undef DWARF2_UNWIND_INFO
119 #undef DWARF2_FRAME_INFO
120 #if (GCC_VERSION >= 3000)
121 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
122 #endif
124 /* The size of the target's pointer type. */
125 #ifndef PTR_SIZE
126 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
127 #endif
129 /* Array of RTXes referenced by the debugging information, which therefore
130 must be kept around forever. */
131 static GTY(()) VEC(rtx,gc) *used_rtx_array;
133 /* A pointer to the base of a list of incomplete types which might be
134 completed at some later time. incomplete_types_list needs to be a
135 VEC(tree,gc) because we want to tell the garbage collector about
136 it. */
137 static GTY(()) VEC(tree,gc) *incomplete_types;
139 /* A pointer to the base of a table of references to declaration
140 scopes. This table is a display which tracks the nesting
141 of declaration scopes at the current scope and containing
142 scopes. This table is used to find the proper place to
143 define type declaration DIE's. */
144 static GTY(()) VEC(tree,gc) *decl_scope_table;
146 /* Pointers to various DWARF2 sections. */
147 static GTY(()) section *debug_info_section;
148 static GTY(()) section *debug_abbrev_section;
149 static GTY(()) section *debug_aranges_section;
150 static GTY(()) section *debug_macinfo_section;
151 static GTY(()) section *debug_line_section;
152 static GTY(()) section *debug_loc_section;
153 static GTY(()) section *debug_pubnames_section;
154 static GTY(()) section *debug_pubtypes_section;
155 static GTY(()) section *debug_str_section;
156 static GTY(()) section *debug_ranges_section;
157 static GTY(()) section *debug_frame_section;
159 /* Maximum size (in bytes) of an artificially generated label. */
160 #define MAX_ARTIFICIAL_LABEL_BYTES 30
162 /* According to the (draft) DWARF 3 specification, the initial length
163 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
164 bytes are 0xffffffff, followed by the length stored in the next 8
165 bytes.
167 However, the SGI/MIPS ABI uses an initial length which is equal to
168 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
170 #ifndef DWARF_INITIAL_LENGTH_SIZE
171 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
172 #endif
174 /* Round SIZE up to the nearest BOUNDARY. */
175 #define DWARF_ROUND(SIZE,BOUNDARY) \
176 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
178 /* CIE identifier. */
179 #if HOST_BITS_PER_WIDE_INT >= 64
180 #define DWARF_CIE_ID \
181 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
182 #else
183 #define DWARF_CIE_ID DW_CIE_ID
184 #endif
186 DEF_VEC_P (dw_fde_ref);
187 DEF_VEC_ALLOC_P (dw_fde_ref, gc);
189 /* A vector for a table that contains frame description
190 information for each routine. */
191 static GTY(()) VEC(dw_fde_ref, gc) *fde_vec;
193 struct GTY(()) indirect_string_node {
194 const char *str;
195 unsigned int refcount;
196 enum dwarf_form form;
197 char *label;
200 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
202 static GTY(()) int dw2_string_counter;
204 /* True if the compilation unit places functions in more than one section. */
205 static GTY(()) bool have_multiple_function_sections = false;
207 /* Whether the default text and cold text sections have been used at all. */
209 static GTY(()) bool text_section_used = false;
210 static GTY(()) bool cold_text_section_used = false;
212 /* The default cold text section. */
213 static GTY(()) section *cold_text_section;
215 /* Forward declarations for functions defined in this file. */
217 static char *stripattributes (const char *);
218 static void output_call_frame_info (int);
219 static void dwarf2out_note_section_used (void);
221 /* Personality decl of current unit. Used only when assembler does not support
222 personality CFI. */
223 static GTY(()) rtx current_unit_personality;
225 /* Data and reference forms for relocatable data. */
226 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
227 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
229 #ifndef DEBUG_FRAME_SECTION
230 #define DEBUG_FRAME_SECTION ".debug_frame"
231 #endif
233 #ifndef FUNC_BEGIN_LABEL
234 #define FUNC_BEGIN_LABEL "LFB"
235 #endif
237 #ifndef FUNC_END_LABEL
238 #define FUNC_END_LABEL "LFE"
239 #endif
241 #ifndef PROLOGUE_END_LABEL
242 #define PROLOGUE_END_LABEL "LPE"
243 #endif
245 #ifndef EPILOGUE_BEGIN_LABEL
246 #define EPILOGUE_BEGIN_LABEL "LEB"
247 #endif
249 #ifndef FRAME_BEGIN_LABEL
250 #define FRAME_BEGIN_LABEL "Lframe"
251 #endif
252 #define CIE_AFTER_SIZE_LABEL "LSCIE"
253 #define CIE_END_LABEL "LECIE"
254 #define FDE_LABEL "LSFDE"
255 #define FDE_AFTER_SIZE_LABEL "LASFDE"
256 #define FDE_END_LABEL "LEFDE"
257 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
258 #define LINE_NUMBER_END_LABEL "LELT"
259 #define LN_PROLOG_AS_LABEL "LASLTP"
260 #define LN_PROLOG_END_LABEL "LELTP"
261 #define DIE_LABEL_PREFIX "DW"
263 /* Match the base name of a file to the base name of a compilation unit. */
265 static int
266 matches_main_base (const char *path)
268 /* Cache the last query. */
269 static const char *last_path = NULL;
270 static int last_match = 0;
271 if (path != last_path)
273 const char *base;
274 int length = base_of_path (path, &base);
275 last_path = path;
276 last_match = (length == main_input_baselength
277 && memcmp (base, main_input_basename, length) == 0);
279 return last_match;
282 #ifdef DEBUG_DEBUG_STRUCT
284 static int
285 dump_struct_debug (tree type, enum debug_info_usage usage,
286 enum debug_struct_file criterion, int generic,
287 int matches, int result)
289 /* Find the type name. */
290 tree type_decl = TYPE_STUB_DECL (type);
291 tree t = type_decl;
292 const char *name = 0;
293 if (TREE_CODE (t) == TYPE_DECL)
294 t = DECL_NAME (t);
295 if (t)
296 name = IDENTIFIER_POINTER (t);
298 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
299 criterion,
300 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
301 matches ? "bas" : "hdr",
302 generic ? "gen" : "ord",
303 usage == DINFO_USAGE_DFN ? ";" :
304 usage == DINFO_USAGE_DIR_USE ? "." : "*",
305 result,
306 (void*) type_decl, name);
307 return result;
309 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
310 dump_struct_debug (type, usage, criterion, generic, matches, result)
312 #else
314 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
315 (result)
317 #endif
319 static bool
320 should_emit_struct_debug (tree type, enum debug_info_usage usage)
322 enum debug_struct_file criterion;
323 tree type_decl;
324 bool generic = lang_hooks.types.generic_p (type);
326 if (generic)
327 criterion = debug_struct_generic[usage];
328 else
329 criterion = debug_struct_ordinary[usage];
331 if (criterion == DINFO_STRUCT_FILE_NONE)
332 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
333 if (criterion == DINFO_STRUCT_FILE_ANY)
334 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
336 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
338 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
339 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
341 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
342 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
343 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
346 /* Return a pointer to a copy of the section string name S with all
347 attributes stripped off, and an asterisk prepended (for assemble_name). */
349 static inline char *
350 stripattributes (const char *s)
352 char *stripped = XNEWVEC (char, strlen (s) + 2);
353 char *p = stripped;
355 *p++ = '*';
357 while (*s && *s != ',')
358 *p++ = *s++;
360 *p = '\0';
361 return stripped;
364 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
365 switch to the data section instead, and write out a synthetic start label
366 for collect2 the first time around. */
368 static void
369 switch_to_eh_frame_section (bool back)
371 tree label;
373 #ifdef EH_FRAME_SECTION_NAME
374 if (eh_frame_section == 0)
376 int flags;
378 if (EH_TABLES_CAN_BE_READ_ONLY)
380 int fde_encoding;
381 int per_encoding;
382 int lsda_encoding;
384 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
385 /*global=*/0);
386 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
387 /*global=*/1);
388 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
389 /*global=*/0);
390 flags = ((! flag_pic
391 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
392 && (fde_encoding & 0x70) != DW_EH_PE_aligned
393 && (per_encoding & 0x70) != DW_EH_PE_absptr
394 && (per_encoding & 0x70) != DW_EH_PE_aligned
395 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
396 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
397 ? 0 : SECTION_WRITE);
399 else
400 flags = SECTION_WRITE;
401 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
403 #endif /* EH_FRAME_SECTION_NAME */
405 if (eh_frame_section)
406 switch_to_section (eh_frame_section);
407 else
409 /* We have no special eh_frame section. Put the information in
410 the data section and emit special labels to guide collect2. */
411 switch_to_section (data_section);
413 if (!back)
415 label = get_file_function_name ("F");
416 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
417 targetm.asm_out.globalize_label (asm_out_file,
418 IDENTIFIER_POINTER (label));
419 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
424 /* Switch [BACK] to the eh or debug frame table section, depending on
425 FOR_EH. */
427 static void
428 switch_to_frame_table_section (int for_eh, bool back)
430 if (for_eh)
431 switch_to_eh_frame_section (back);
432 else
434 if (!debug_frame_section)
435 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
436 SECTION_DEBUG, NULL);
437 switch_to_section (debug_frame_section);
441 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
443 enum dw_cfi_oprnd_type
444 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
446 switch (cfi)
448 case DW_CFA_nop:
449 case DW_CFA_GNU_window_save:
450 case DW_CFA_remember_state:
451 case DW_CFA_restore_state:
452 return dw_cfi_oprnd_unused;
454 case DW_CFA_set_loc:
455 case DW_CFA_advance_loc1:
456 case DW_CFA_advance_loc2:
457 case DW_CFA_advance_loc4:
458 case DW_CFA_MIPS_advance_loc8:
459 return dw_cfi_oprnd_addr;
461 case DW_CFA_offset:
462 case DW_CFA_offset_extended:
463 case DW_CFA_def_cfa:
464 case DW_CFA_offset_extended_sf:
465 case DW_CFA_def_cfa_sf:
466 case DW_CFA_restore:
467 case DW_CFA_restore_extended:
468 case DW_CFA_undefined:
469 case DW_CFA_same_value:
470 case DW_CFA_def_cfa_register:
471 case DW_CFA_register:
472 case DW_CFA_expression:
473 return dw_cfi_oprnd_reg_num;
475 case DW_CFA_def_cfa_offset:
476 case DW_CFA_GNU_args_size:
477 case DW_CFA_def_cfa_offset_sf:
478 return dw_cfi_oprnd_offset;
480 case DW_CFA_def_cfa_expression:
481 return dw_cfi_oprnd_loc;
483 default:
484 gcc_unreachable ();
488 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
490 enum dw_cfi_oprnd_type
491 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
493 switch (cfi)
495 case DW_CFA_def_cfa:
496 case DW_CFA_def_cfa_sf:
497 case DW_CFA_offset:
498 case DW_CFA_offset_extended_sf:
499 case DW_CFA_offset_extended:
500 return dw_cfi_oprnd_offset;
502 case DW_CFA_register:
503 return dw_cfi_oprnd_reg_num;
505 case DW_CFA_expression:
506 return dw_cfi_oprnd_loc;
508 default:
509 return dw_cfi_oprnd_unused;
513 /* Output one FDE. */
515 static void
516 output_fde (dw_fde_ref fde, bool for_eh, bool second,
517 char *section_start_label, int fde_encoding, char *augmentation,
518 bool any_lsda_needed, int lsda_encoding)
520 const char *begin, *end;
521 static unsigned int j;
522 char l1[20], l2[20];
524 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
525 /* empty */ 0);
526 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
527 for_eh + j);
528 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
529 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
530 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
531 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
532 " indicating 64-bit DWARF extension");
533 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
534 "FDE Length");
535 ASM_OUTPUT_LABEL (asm_out_file, l1);
537 if (for_eh)
538 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
539 else
540 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
541 debug_frame_section, "FDE CIE offset");
543 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
544 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
546 if (for_eh)
548 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
549 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
550 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
551 "FDE initial location");
552 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
553 end, begin, "FDE address range");
555 else
557 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
558 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
561 if (augmentation[0])
563 if (any_lsda_needed)
565 int size = size_of_encoded_value (lsda_encoding);
567 if (lsda_encoding == DW_EH_PE_aligned)
569 int offset = ( 4 /* Length */
570 + 4 /* CIE offset */
571 + 2 * size_of_encoded_value (fde_encoding)
572 + 1 /* Augmentation size */ );
573 int pad = -offset & (PTR_SIZE - 1);
575 size += pad;
576 gcc_assert (size_of_uleb128 (size) == 1);
579 dw2_asm_output_data_uleb128 (size, "Augmentation size");
581 if (fde->uses_eh_lsda)
583 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
584 fde->funcdef_number);
585 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
586 gen_rtx_SYMBOL_REF (Pmode, l1),
587 false,
588 "Language Specific Data Area");
590 else
592 if (lsda_encoding == DW_EH_PE_aligned)
593 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
594 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
595 "Language Specific Data Area (none)");
598 else
599 dw2_asm_output_data_uleb128 (0, "Augmentation size");
602 /* Loop through the Call Frame Instructions associated with this FDE. */
603 fde->dw_fde_current_label = begin;
605 size_t from, until, i;
607 from = 0;
608 until = VEC_length (dw_cfi_ref, fde->dw_fde_cfi);
610 if (fde->dw_fde_second_begin == NULL)
612 else if (!second)
613 until = fde->dw_fde_switch_cfi_index;
614 else
615 from = fde->dw_fde_switch_cfi_index;
617 for (i = from; i < until; i++)
618 output_cfi (VEC_index (dw_cfi_ref, fde->dw_fde_cfi, i), fde, for_eh);
621 /* If we are to emit a ref/link from function bodies to their frame tables,
622 do it now. This is typically performed to make sure that tables
623 associated with functions are dragged with them and not discarded in
624 garbage collecting links. We need to do this on a per function basis to
625 cope with -ffunction-sections. */
627 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
628 /* Switch to the function section, emit the ref to the tables, and
629 switch *back* into the table section. */
630 switch_to_section (function_section (fde->decl));
631 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
632 switch_to_frame_table_section (for_eh, true);
633 #endif
635 /* Pad the FDE out to an address sized boundary. */
636 ASM_OUTPUT_ALIGN (asm_out_file,
637 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
638 ASM_OUTPUT_LABEL (asm_out_file, l2);
640 j += 2;
643 /* Return true if frame description entry FDE is needed for EH. */
645 static bool
646 fde_needed_for_eh_p (dw_fde_ref fde)
648 if (flag_asynchronous_unwind_tables)
649 return true;
651 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
652 return true;
654 if (fde->uses_eh_lsda)
655 return true;
657 /* If exceptions are enabled, we have collected nothrow info. */
658 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
659 return false;
661 return true;
664 /* Output the call frame information used to record information
665 that relates to calculating the frame pointer, and records the
666 location of saved registers. */
668 static void
669 output_call_frame_info (int for_eh)
671 unsigned int i;
672 dw_fde_ref fde;
673 dw_cfi_ref cfi;
674 char l1[20], l2[20], section_start_label[20];
675 bool any_lsda_needed = false;
676 char augmentation[6];
677 int augmentation_size;
678 int fde_encoding = DW_EH_PE_absptr;
679 int per_encoding = DW_EH_PE_absptr;
680 int lsda_encoding = DW_EH_PE_absptr;
681 int return_reg;
682 rtx personality = NULL;
683 int dw_cie_version;
685 /* Don't emit a CIE if there won't be any FDEs. */
686 if (fde_vec == NULL)
687 return;
689 /* Nothing to do if the assembler's doing it all. */
690 if (dwarf2out_do_cfi_asm ())
691 return;
693 /* If we don't have any functions we'll want to unwind out of, don't emit
694 any EH unwind information. If we make FDEs linkonce, we may have to
695 emit an empty label for an FDE that wouldn't otherwise be emitted. We
696 want to avoid having an FDE kept around when the function it refers to
697 is discarded. Example where this matters: a primary function template
698 in C++ requires EH information, an explicit specialization doesn't. */
699 if (for_eh)
701 bool any_eh_needed = false;
703 FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, i, fde)
705 if (fde->uses_eh_lsda)
706 any_eh_needed = any_lsda_needed = true;
707 else if (fde_needed_for_eh_p (fde))
708 any_eh_needed = true;
709 else if (TARGET_USES_WEAK_UNWIND_INFO)
710 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
713 if (!any_eh_needed)
714 return;
717 /* We're going to be generating comments, so turn on app. */
718 if (flag_debug_asm)
719 app_enable ();
721 /* Switch to the proper frame section, first time. */
722 switch_to_frame_table_section (for_eh, false);
724 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
725 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
727 /* Output the CIE. */
728 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
729 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
730 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
731 dw2_asm_output_data (4, 0xffffffff,
732 "Initial length escape value indicating 64-bit DWARF extension");
733 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
734 "Length of Common Information Entry");
735 ASM_OUTPUT_LABEL (asm_out_file, l1);
737 /* Now that the CIE pointer is PC-relative for EH,
738 use 0 to identify the CIE. */
739 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
740 (for_eh ? 0 : DWARF_CIE_ID),
741 "CIE Identifier Tag");
743 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
744 use CIE version 1, unless that would produce incorrect results
745 due to overflowing the return register column. */
746 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
747 dw_cie_version = 1;
748 if (return_reg >= 256 || dwarf_version > 2)
749 dw_cie_version = 3;
750 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
752 augmentation[0] = 0;
753 augmentation_size = 0;
755 personality = current_unit_personality;
756 if (for_eh)
758 char *p;
760 /* Augmentation:
761 z Indicates that a uleb128 is present to size the
762 augmentation section.
763 L Indicates the encoding (and thus presence) of
764 an LSDA pointer in the FDE augmentation.
765 R Indicates a non-default pointer encoding for
766 FDE code pointers.
767 P Indicates the presence of an encoding + language
768 personality routine in the CIE augmentation. */
770 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
771 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
772 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
774 p = augmentation + 1;
775 if (personality)
777 *p++ = 'P';
778 augmentation_size += 1 + size_of_encoded_value (per_encoding);
779 assemble_external_libcall (personality);
781 if (any_lsda_needed)
783 *p++ = 'L';
784 augmentation_size += 1;
786 if (fde_encoding != DW_EH_PE_absptr)
788 *p++ = 'R';
789 augmentation_size += 1;
791 if (p > augmentation + 1)
793 augmentation[0] = 'z';
794 *p = '\0';
797 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
798 if (personality && per_encoding == DW_EH_PE_aligned)
800 int offset = ( 4 /* Length */
801 + 4 /* CIE Id */
802 + 1 /* CIE version */
803 + strlen (augmentation) + 1 /* Augmentation */
804 + size_of_uleb128 (1) /* Code alignment */
805 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
806 + 1 /* RA column */
807 + 1 /* Augmentation size */
808 + 1 /* Personality encoding */ );
809 int pad = -offset & (PTR_SIZE - 1);
811 augmentation_size += pad;
813 /* Augmentations should be small, so there's scarce need to
814 iterate for a solution. Die if we exceed one uleb128 byte. */
815 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
819 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
820 if (dw_cie_version >= 4)
822 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
823 dw2_asm_output_data (1, 0, "CIE Segment Size");
825 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
826 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
827 "CIE Data Alignment Factor");
829 if (dw_cie_version == 1)
830 dw2_asm_output_data (1, return_reg, "CIE RA Column");
831 else
832 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
834 if (augmentation[0])
836 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
837 if (personality)
839 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
840 eh_data_format_name (per_encoding));
841 dw2_asm_output_encoded_addr_rtx (per_encoding,
842 personality,
843 true, NULL);
846 if (any_lsda_needed)
847 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
848 eh_data_format_name (lsda_encoding));
850 if (fde_encoding != DW_EH_PE_absptr)
851 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
852 eh_data_format_name (fde_encoding));
855 FOR_EACH_VEC_ELT (dw_cfi_ref, cie_cfi_vec, i, cfi)
856 output_cfi (cfi, NULL, for_eh);
858 /* Pad the CIE out to an address sized boundary. */
859 ASM_OUTPUT_ALIGN (asm_out_file,
860 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
861 ASM_OUTPUT_LABEL (asm_out_file, l2);
863 /* Loop through all of the FDE's. */
864 FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, i, fde)
866 unsigned int k;
868 /* Don't emit EH unwind info for leaf functions that don't need it. */
869 if (for_eh && !fde_needed_for_eh_p (fde))
870 continue;
872 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
873 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
874 augmentation, any_lsda_needed, lsda_encoding);
877 if (for_eh && targetm.terminate_dw2_eh_frame_info)
878 dw2_asm_output_data (4, 0, "End of Table");
880 /* Turn off app to make assembly quicker. */
881 if (flag_debug_asm)
882 app_disable ();
885 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
887 static void
888 dwarf2out_do_cfi_startproc (bool second)
890 int enc;
891 rtx ref;
892 rtx personality = get_personality_function (current_function_decl);
894 fprintf (asm_out_file, "\t.cfi_startproc\n");
896 if (personality)
898 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
899 ref = personality;
901 /* ??? The GAS support isn't entirely consistent. We have to
902 handle indirect support ourselves, but PC-relative is done
903 in the assembler. Further, the assembler can't handle any
904 of the weirder relocation types. */
905 if (enc & DW_EH_PE_indirect)
906 ref = dw2_force_const_mem (ref, true);
908 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
909 output_addr_const (asm_out_file, ref);
910 fputc ('\n', asm_out_file);
913 if (crtl->uses_eh_lsda)
915 char lab[20];
917 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
918 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
919 current_function_funcdef_no);
920 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
921 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
923 if (enc & DW_EH_PE_indirect)
924 ref = dw2_force_const_mem (ref, true);
926 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
927 output_addr_const (asm_out_file, ref);
928 fputc ('\n', asm_out_file);
932 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
933 this allocation may be done before pass_final. */
935 dw_fde_ref
936 dwarf2out_alloc_current_fde (void)
938 dw_fde_ref fde;
940 fde = ggc_alloc_cleared_dw_fde_node ();
941 fde->decl = current_function_decl;
942 fde->funcdef_number = current_function_funcdef_no;
943 fde->fde_index = VEC_length (dw_fde_ref, fde_vec);
944 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
945 fde->uses_eh_lsda = crtl->uses_eh_lsda;
946 fde->nothrow = crtl->nothrow;
947 fde->drap_reg = INVALID_REGNUM;
948 fde->vdrap_reg = INVALID_REGNUM;
950 /* Record the FDE associated with this function. */
951 cfun->fde = fde;
952 VEC_safe_push (dw_fde_ref, gc, fde_vec, fde);
954 return fde;
957 /* Output a marker (i.e. a label) for the beginning of a function, before
958 the prologue. */
960 void
961 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
962 const char *file ATTRIBUTE_UNUSED)
964 char label[MAX_ARTIFICIAL_LABEL_BYTES];
965 char * dup_label;
966 dw_fde_ref fde;
967 section *fnsec;
968 bool do_frame;
970 current_function_func_begin_label = NULL;
972 do_frame = dwarf2out_do_frame ();
974 /* ??? current_function_func_begin_label is also used by except.c for
975 call-site information. We must emit this label if it might be used. */
976 if (!do_frame
977 && (!flag_exceptions
978 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
979 return;
981 fnsec = function_section (current_function_decl);
982 switch_to_section (fnsec);
983 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
984 current_function_funcdef_no);
985 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
986 current_function_funcdef_no);
987 dup_label = xstrdup (label);
988 current_function_func_begin_label = dup_label;
990 /* We can elide the fde allocation if we're not emitting debug info. */
991 if (!do_frame)
992 return;
994 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
995 emit insns as rtx but bypass the bulk of rest_of_compilation, which
996 would include pass_dwarf2_frame. If we've not created the FDE yet,
997 do so now. */
998 fde = cfun->fde;
999 if (fde == NULL)
1000 fde = dwarf2out_alloc_current_fde ();
1002 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1003 fde->dw_fde_begin = dup_label;
1004 fde->dw_fde_current_label = dup_label;
1005 fde->in_std_section = (fnsec == text_section
1006 || (cold_text_section && fnsec == cold_text_section));
1008 /* We only want to output line number information for the genuine dwarf2
1009 prologue case, not the eh frame case. */
1010 #ifdef DWARF2_DEBUGGING_INFO
1011 if (file)
1012 dwarf2out_source_line (line, file, 0, true);
1013 #endif
1015 if (dwarf2out_do_cfi_asm ())
1016 dwarf2out_do_cfi_startproc (false);
1017 else
1019 rtx personality = get_personality_function (current_function_decl);
1020 if (!current_unit_personality)
1021 current_unit_personality = personality;
1023 /* We cannot keep a current personality per function as without CFI
1024 asm, at the point where we emit the CFI data, there is no current
1025 function anymore. */
1026 if (personality && current_unit_personality != personality)
1027 sorry ("multiple EH personalities are supported only with assemblers "
1028 "supporting .cfi_personality directive");
1032 /* Output a marker (i.e. a label) for the end of the generated code
1033 for a function prologue. This gets called *after* the prologue code has
1034 been generated. */
1036 void
1037 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1038 const char *file ATTRIBUTE_UNUSED)
1040 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1042 /* Output a label to mark the endpoint of the code generated for this
1043 function. */
1044 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1045 current_function_funcdef_no);
1046 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1047 current_function_funcdef_no);
1048 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1051 /* Output a marker (i.e. a label) for the beginning of the generated code
1052 for a function epilogue. This gets called *before* the prologue code has
1053 been generated. */
1055 void
1056 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1057 const char *file ATTRIBUTE_UNUSED)
1059 dw_fde_ref fde = cfun->fde;
1060 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1062 if (fde->dw_fde_vms_begin_epilogue)
1063 return;
1065 /* Output a label to mark the endpoint of the code generated for this
1066 function. */
1067 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1068 current_function_funcdef_no);
1069 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1070 current_function_funcdef_no);
1071 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1074 /* Output a marker (i.e. a label) for the absolute end of the generated code
1075 for a function definition. This gets called *after* the epilogue code has
1076 been generated. */
1078 void
1079 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1080 const char *file ATTRIBUTE_UNUSED)
1082 dw_fde_ref fde;
1083 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1085 last_var_location_insn = NULL_RTX;
1086 cached_next_real_insn = NULL_RTX;
1088 if (dwarf2out_do_cfi_asm ())
1089 fprintf (asm_out_file, "\t.cfi_endproc\n");
1091 /* Output a label to mark the endpoint of the code generated for this
1092 function. */
1093 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1094 current_function_funcdef_no);
1095 ASM_OUTPUT_LABEL (asm_out_file, label);
1096 fde = cfun->fde;
1097 gcc_assert (fde != NULL);
1098 if (fde->dw_fde_second_begin == NULL)
1099 fde->dw_fde_end = xstrdup (label);
1102 void
1103 dwarf2out_frame_finish (void)
1105 /* Output call frame information. */
1106 if (targetm.debug_unwind_info () == UI_DWARF2)
1107 output_call_frame_info (0);
1109 /* Output another copy for the unwinder. */
1110 if ((flag_unwind_tables || flag_exceptions)
1111 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1112 output_call_frame_info (1);
1115 /* Note that the current function section is being used for code. */
1117 static void
1118 dwarf2out_note_section_used (void)
1120 section *sec = current_function_section ();
1121 if (sec == text_section)
1122 text_section_used = true;
1123 else if (sec == cold_text_section)
1124 cold_text_section_used = true;
1127 static void var_location_switch_text_section (void);
1128 static void set_cur_line_info_table (section *);
1130 void
1131 dwarf2out_switch_text_section (void)
1133 section *sect;
1134 dw_fde_ref fde = cfun->fde;
1136 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1138 if (!in_cold_section_p)
1140 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1141 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1142 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1144 else
1146 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1147 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1148 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1150 have_multiple_function_sections = true;
1152 /* There is no need to mark used sections when not debugging. */
1153 if (cold_text_section != NULL)
1154 dwarf2out_note_section_used ();
1156 if (dwarf2out_do_cfi_asm ())
1157 fprintf (asm_out_file, "\t.cfi_endproc\n");
1159 /* Now do the real section switch. */
1160 sect = current_function_section ();
1161 switch_to_section (sect);
1163 fde->second_in_std_section
1164 = (sect == text_section
1165 || (cold_text_section && sect == cold_text_section));
1167 if (dwarf2out_do_cfi_asm ())
1168 dwarf2out_do_cfi_startproc (true);
1170 var_location_switch_text_section ();
1172 if (cold_text_section != NULL)
1173 set_cur_line_info_table (sect);
1176 /* And now, the subset of the debugging information support code necessary
1177 for emitting location expressions. */
1179 /* Data about a single source file. */
1180 struct GTY(()) dwarf_file_data {
1181 const char * filename;
1182 int emitted_number;
1185 typedef struct GTY(()) deferred_locations_struct
1187 tree variable;
1188 dw_die_ref die;
1189 } deferred_locations;
1191 DEF_VEC_O(deferred_locations);
1192 DEF_VEC_ALLOC_O(deferred_locations,gc);
1194 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
1196 DEF_VEC_P(dw_die_ref);
1197 DEF_VEC_ALLOC_P(dw_die_ref,heap);
1199 /* Location lists are ranges + location descriptions for that range,
1200 so you can track variables that are in different places over
1201 their entire life. */
1202 typedef struct GTY(()) dw_loc_list_struct {
1203 dw_loc_list_ref dw_loc_next;
1204 const char *begin; /* Label for begin address of range */
1205 const char *end; /* Label for end address of range */
1206 char *ll_symbol; /* Label for beginning of location list.
1207 Only on head of list */
1208 const char *section; /* Section this loclist is relative to */
1209 dw_loc_descr_ref expr;
1210 hashval_t hash;
1211 /* True if all addresses in this and subsequent lists are known to be
1212 resolved. */
1213 bool resolved_addr;
1214 /* True if this list has been replaced by dw_loc_next. */
1215 bool replaced;
1216 bool emitted;
1217 /* True if the range should be emitted even if begin and end
1218 are the same. */
1219 bool force;
1220 } dw_loc_list_node;
1222 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1224 /* Convert a DWARF stack opcode into its string name. */
1226 static const char *
1227 dwarf_stack_op_name (unsigned int op)
1229 const char *name = get_DW_OP_name (op);
1231 if (name != NULL)
1232 return name;
1234 return "OP_<unknown>";
1237 /* Return a pointer to a newly allocated location description. Location
1238 descriptions are simple expression terms that can be strung
1239 together to form more complicated location (address) descriptions. */
1241 static inline dw_loc_descr_ref
1242 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1243 unsigned HOST_WIDE_INT oprnd2)
1245 dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
1247 descr->dw_loc_opc = op;
1248 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1249 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1250 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1251 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1253 return descr;
1256 /* Return a pointer to a newly allocated location description for
1257 REG and OFFSET. */
1259 static inline dw_loc_descr_ref
1260 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1262 if (reg <= 31)
1263 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1264 offset, 0);
1265 else
1266 return new_loc_descr (DW_OP_bregx, reg, offset);
1269 /* Add a location description term to a location description expression. */
1271 static inline void
1272 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1274 dw_loc_descr_ref *d;
1276 /* Find the end of the chain. */
1277 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1280 *d = descr;
1283 /* Compare two location operands for exact equality. */
1285 static bool
1286 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1288 if (a->val_class != b->val_class)
1289 return false;
1290 switch (a->val_class)
1292 case dw_val_class_none:
1293 return true;
1294 case dw_val_class_addr:
1295 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1297 case dw_val_class_offset:
1298 case dw_val_class_unsigned_const:
1299 case dw_val_class_const:
1300 case dw_val_class_range_list:
1301 case dw_val_class_lineptr:
1302 case dw_val_class_macptr:
1303 /* These are all HOST_WIDE_INT, signed or unsigned. */
1304 return a->v.val_unsigned == b->v.val_unsigned;
1306 case dw_val_class_loc:
1307 return a->v.val_loc == b->v.val_loc;
1308 case dw_val_class_loc_list:
1309 return a->v.val_loc_list == b->v.val_loc_list;
1310 case dw_val_class_die_ref:
1311 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1312 case dw_val_class_fde_ref:
1313 return a->v.val_fde_index == b->v.val_fde_index;
1314 case dw_val_class_lbl_id:
1315 case dw_val_class_high_pc:
1316 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1317 case dw_val_class_str:
1318 return a->v.val_str == b->v.val_str;
1319 case dw_val_class_flag:
1320 return a->v.val_flag == b->v.val_flag;
1321 case dw_val_class_file:
1322 return a->v.val_file == b->v.val_file;
1323 case dw_val_class_decl_ref:
1324 return a->v.val_decl_ref == b->v.val_decl_ref;
1326 case dw_val_class_const_double:
1327 return (a->v.val_double.high == b->v.val_double.high
1328 && a->v.val_double.low == b->v.val_double.low);
1330 case dw_val_class_vec:
1332 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1333 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1335 return (a_len == b_len
1336 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1339 case dw_val_class_data8:
1340 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1342 case dw_val_class_vms_delta:
1343 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1344 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1346 gcc_unreachable ();
1349 /* Compare two location atoms for exact equality. */
1351 static bool
1352 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1354 if (a->dw_loc_opc != b->dw_loc_opc)
1355 return false;
1357 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1358 address size, but since we always allocate cleared storage it
1359 should be zero for other types of locations. */
1360 if (a->dtprel != b->dtprel)
1361 return false;
1363 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1364 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1367 /* Compare two complete location expressions for exact equality. */
1369 bool
1370 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1372 while (1)
1374 if (a == b)
1375 return true;
1376 if (a == NULL || b == NULL)
1377 return false;
1378 if (!loc_descr_equal_p_1 (a, b))
1379 return false;
1381 a = a->dw_loc_next;
1382 b = b->dw_loc_next;
1387 /* Add a constant OFFSET to a location expression. */
1389 static void
1390 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1392 dw_loc_descr_ref loc;
1393 HOST_WIDE_INT *p;
1395 gcc_assert (*list_head != NULL);
1397 if (!offset)
1398 return;
1400 /* Find the end of the chain. */
1401 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1404 p = NULL;
1405 if (loc->dw_loc_opc == DW_OP_fbreg
1406 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1407 p = &loc->dw_loc_oprnd1.v.val_int;
1408 else if (loc->dw_loc_opc == DW_OP_bregx)
1409 p = &loc->dw_loc_oprnd2.v.val_int;
1411 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1412 offset. Don't optimize if an signed integer overflow would happen. */
1413 if (p != NULL
1414 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1415 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1416 *p += offset;
1418 else if (offset > 0)
1419 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1421 else
1423 loc->dw_loc_next = int_loc_descriptor (-offset);
1424 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1428 /* Add a constant OFFSET to a location list. */
1430 static void
1431 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1433 dw_loc_list_ref d;
1434 for (d = list_head; d != NULL; d = d->dw_loc_next)
1435 loc_descr_plus_const (&d->expr, offset);
1438 #define DWARF_REF_SIZE \
1439 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1441 static unsigned long int get_base_type_offset (dw_die_ref);
1443 /* Return the size of a location descriptor. */
1445 static unsigned long
1446 size_of_loc_descr (dw_loc_descr_ref loc)
1448 unsigned long size = 1;
1450 switch (loc->dw_loc_opc)
1452 case DW_OP_addr:
1453 size += DWARF2_ADDR_SIZE;
1454 break;
1455 case DW_OP_const1u:
1456 case DW_OP_const1s:
1457 size += 1;
1458 break;
1459 case DW_OP_const2u:
1460 case DW_OP_const2s:
1461 size += 2;
1462 break;
1463 case DW_OP_const4u:
1464 case DW_OP_const4s:
1465 size += 4;
1466 break;
1467 case DW_OP_const8u:
1468 case DW_OP_const8s:
1469 size += 8;
1470 break;
1471 case DW_OP_constu:
1472 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1473 break;
1474 case DW_OP_consts:
1475 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1476 break;
1477 case DW_OP_pick:
1478 size += 1;
1479 break;
1480 case DW_OP_plus_uconst:
1481 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1482 break;
1483 case DW_OP_skip:
1484 case DW_OP_bra:
1485 size += 2;
1486 break;
1487 case DW_OP_breg0:
1488 case DW_OP_breg1:
1489 case DW_OP_breg2:
1490 case DW_OP_breg3:
1491 case DW_OP_breg4:
1492 case DW_OP_breg5:
1493 case DW_OP_breg6:
1494 case DW_OP_breg7:
1495 case DW_OP_breg8:
1496 case DW_OP_breg9:
1497 case DW_OP_breg10:
1498 case DW_OP_breg11:
1499 case DW_OP_breg12:
1500 case DW_OP_breg13:
1501 case DW_OP_breg14:
1502 case DW_OP_breg15:
1503 case DW_OP_breg16:
1504 case DW_OP_breg17:
1505 case DW_OP_breg18:
1506 case DW_OP_breg19:
1507 case DW_OP_breg20:
1508 case DW_OP_breg21:
1509 case DW_OP_breg22:
1510 case DW_OP_breg23:
1511 case DW_OP_breg24:
1512 case DW_OP_breg25:
1513 case DW_OP_breg26:
1514 case DW_OP_breg27:
1515 case DW_OP_breg28:
1516 case DW_OP_breg29:
1517 case DW_OP_breg30:
1518 case DW_OP_breg31:
1519 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1520 break;
1521 case DW_OP_regx:
1522 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1523 break;
1524 case DW_OP_fbreg:
1525 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1526 break;
1527 case DW_OP_bregx:
1528 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1529 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1530 break;
1531 case DW_OP_piece:
1532 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1533 break;
1534 case DW_OP_bit_piece:
1535 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1536 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1537 break;
1538 case DW_OP_deref_size:
1539 case DW_OP_xderef_size:
1540 size += 1;
1541 break;
1542 case DW_OP_call2:
1543 size += 2;
1544 break;
1545 case DW_OP_call4:
1546 size += 4;
1547 break;
1548 case DW_OP_call_ref:
1549 size += DWARF_REF_SIZE;
1550 break;
1551 case DW_OP_implicit_value:
1552 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1553 + loc->dw_loc_oprnd1.v.val_unsigned;
1554 break;
1555 case DW_OP_GNU_implicit_pointer:
1556 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1557 break;
1558 case DW_OP_GNU_entry_value:
1560 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1561 size += size_of_uleb128 (op_size) + op_size;
1562 break;
1564 case DW_OP_GNU_const_type:
1566 unsigned long o
1567 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1568 size += size_of_uleb128 (o) + 1;
1569 switch (loc->dw_loc_oprnd2.val_class)
1571 case dw_val_class_vec:
1572 size += loc->dw_loc_oprnd2.v.val_vec.length
1573 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1574 break;
1575 case dw_val_class_const:
1576 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1577 break;
1578 case dw_val_class_const_double:
1579 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1580 break;
1581 default:
1582 gcc_unreachable ();
1584 break;
1586 case DW_OP_GNU_regval_type:
1588 unsigned long o
1589 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1590 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1591 + size_of_uleb128 (o);
1593 break;
1594 case DW_OP_GNU_deref_type:
1596 unsigned long o
1597 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1598 size += 1 + size_of_uleb128 (o);
1600 break;
1601 case DW_OP_GNU_convert:
1602 case DW_OP_GNU_reinterpret:
1603 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1604 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1605 else
1607 unsigned long o
1608 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1609 size += size_of_uleb128 (o);
1611 break;
1612 case DW_OP_GNU_parameter_ref:
1613 size += 4;
1614 break;
1615 default:
1616 break;
1619 return size;
1622 /* Return the size of a series of location descriptors. */
1624 unsigned long
1625 size_of_locs (dw_loc_descr_ref loc)
1627 dw_loc_descr_ref l;
1628 unsigned long size;
1630 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1631 field, to avoid writing to a PCH file. */
1632 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1634 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1635 break;
1636 size += size_of_loc_descr (l);
1638 if (! l)
1639 return size;
1641 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1643 l->dw_loc_addr = size;
1644 size += size_of_loc_descr (l);
1647 return size;
1650 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1651 static void get_ref_die_offset_label (char *, dw_die_ref);
1652 static unsigned long int get_ref_die_offset (dw_die_ref);
1654 /* Output location description stack opcode's operands (if any).
1655 The for_eh_or_skip parameter controls whether register numbers are
1656 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1657 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1658 info). This should be suppressed for the cases that have not been converted
1659 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1661 static void
1662 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1664 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1665 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1667 switch (loc->dw_loc_opc)
1669 #ifdef DWARF2_DEBUGGING_INFO
1670 case DW_OP_const2u:
1671 case DW_OP_const2s:
1672 dw2_asm_output_data (2, val1->v.val_int, NULL);
1673 break;
1674 case DW_OP_const4u:
1675 if (loc->dtprel)
1677 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1678 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1679 val1->v.val_addr);
1680 fputc ('\n', asm_out_file);
1681 break;
1683 /* FALLTHRU */
1684 case DW_OP_const4s:
1685 dw2_asm_output_data (4, val1->v.val_int, NULL);
1686 break;
1687 case DW_OP_const8u:
1688 if (loc->dtprel)
1690 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1691 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1692 val1->v.val_addr);
1693 fputc ('\n', asm_out_file);
1694 break;
1696 /* FALLTHRU */
1697 case DW_OP_const8s:
1698 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1699 dw2_asm_output_data (8, val1->v.val_int, NULL);
1700 break;
1701 case DW_OP_skip:
1702 case DW_OP_bra:
1704 int offset;
1706 gcc_assert (val1->val_class == dw_val_class_loc);
1707 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1709 dw2_asm_output_data (2, offset, NULL);
1711 break;
1712 case DW_OP_implicit_value:
1713 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1714 switch (val2->val_class)
1716 case dw_val_class_const:
1717 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1718 break;
1719 case dw_val_class_vec:
1721 unsigned int elt_size = val2->v.val_vec.elt_size;
1722 unsigned int len = val2->v.val_vec.length;
1723 unsigned int i;
1724 unsigned char *p;
1726 if (elt_size > sizeof (HOST_WIDE_INT))
1728 elt_size /= 2;
1729 len *= 2;
1731 for (i = 0, p = val2->v.val_vec.array;
1732 i < len;
1733 i++, p += elt_size)
1734 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1735 "fp or vector constant word %u", i);
1737 break;
1738 case dw_val_class_const_double:
1740 unsigned HOST_WIDE_INT first, second;
1742 if (WORDS_BIG_ENDIAN)
1744 first = val2->v.val_double.high;
1745 second = val2->v.val_double.low;
1747 else
1749 first = val2->v.val_double.low;
1750 second = val2->v.val_double.high;
1752 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1753 first, NULL);
1754 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1755 second, NULL);
1757 break;
1758 case dw_val_class_addr:
1759 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1760 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1761 break;
1762 default:
1763 gcc_unreachable ();
1765 break;
1766 #else
1767 case DW_OP_const2u:
1768 case DW_OP_const2s:
1769 case DW_OP_const4u:
1770 case DW_OP_const4s:
1771 case DW_OP_const8u:
1772 case DW_OP_const8s:
1773 case DW_OP_skip:
1774 case DW_OP_bra:
1775 case DW_OP_implicit_value:
1776 /* We currently don't make any attempt to make sure these are
1777 aligned properly like we do for the main unwind info, so
1778 don't support emitting things larger than a byte if we're
1779 only doing unwinding. */
1780 gcc_unreachable ();
1781 #endif
1782 case DW_OP_const1u:
1783 case DW_OP_const1s:
1784 dw2_asm_output_data (1, val1->v.val_int, NULL);
1785 break;
1786 case DW_OP_constu:
1787 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1788 break;
1789 case DW_OP_consts:
1790 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1791 break;
1792 case DW_OP_pick:
1793 dw2_asm_output_data (1, val1->v.val_int, NULL);
1794 break;
1795 case DW_OP_plus_uconst:
1796 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1797 break;
1798 case DW_OP_breg0:
1799 case DW_OP_breg1:
1800 case DW_OP_breg2:
1801 case DW_OP_breg3:
1802 case DW_OP_breg4:
1803 case DW_OP_breg5:
1804 case DW_OP_breg6:
1805 case DW_OP_breg7:
1806 case DW_OP_breg8:
1807 case DW_OP_breg9:
1808 case DW_OP_breg10:
1809 case DW_OP_breg11:
1810 case DW_OP_breg12:
1811 case DW_OP_breg13:
1812 case DW_OP_breg14:
1813 case DW_OP_breg15:
1814 case DW_OP_breg16:
1815 case DW_OP_breg17:
1816 case DW_OP_breg18:
1817 case DW_OP_breg19:
1818 case DW_OP_breg20:
1819 case DW_OP_breg21:
1820 case DW_OP_breg22:
1821 case DW_OP_breg23:
1822 case DW_OP_breg24:
1823 case DW_OP_breg25:
1824 case DW_OP_breg26:
1825 case DW_OP_breg27:
1826 case DW_OP_breg28:
1827 case DW_OP_breg29:
1828 case DW_OP_breg30:
1829 case DW_OP_breg31:
1830 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1831 break;
1832 case DW_OP_regx:
1834 unsigned r = val1->v.val_unsigned;
1835 if (for_eh_or_skip >= 0)
1836 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1837 gcc_assert (size_of_uleb128 (r)
1838 == size_of_uleb128 (val1->v.val_unsigned));
1839 dw2_asm_output_data_uleb128 (r, NULL);
1841 break;
1842 case DW_OP_fbreg:
1843 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1844 break;
1845 case DW_OP_bregx:
1847 unsigned r = val1->v.val_unsigned;
1848 if (for_eh_or_skip >= 0)
1849 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1850 gcc_assert (size_of_uleb128 (r)
1851 == size_of_uleb128 (val1->v.val_unsigned));
1852 dw2_asm_output_data_uleb128 (r, NULL);
1853 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1855 break;
1856 case DW_OP_piece:
1857 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1858 break;
1859 case DW_OP_bit_piece:
1860 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1861 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1862 break;
1863 case DW_OP_deref_size:
1864 case DW_OP_xderef_size:
1865 dw2_asm_output_data (1, val1->v.val_int, NULL);
1866 break;
1868 case DW_OP_addr:
1869 if (loc->dtprel)
1871 if (targetm.asm_out.output_dwarf_dtprel)
1873 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
1874 DWARF2_ADDR_SIZE,
1875 val1->v.val_addr);
1876 fputc ('\n', asm_out_file);
1878 else
1879 gcc_unreachable ();
1881 else
1883 #ifdef DWARF2_DEBUGGING_INFO
1884 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
1885 #else
1886 gcc_unreachable ();
1887 #endif
1889 break;
1891 case DW_OP_GNU_implicit_pointer:
1893 char label[MAX_ARTIFICIAL_LABEL_BYTES
1894 + HOST_BITS_PER_WIDE_INT / 2 + 2];
1895 gcc_assert (val1->val_class == dw_val_class_die_ref);
1896 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
1897 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
1898 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1900 break;
1902 case DW_OP_GNU_entry_value:
1903 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
1904 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
1905 break;
1907 case DW_OP_GNU_const_type:
1909 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
1910 gcc_assert (o);
1911 dw2_asm_output_data_uleb128 (o, NULL);
1912 switch (val2->val_class)
1914 case dw_val_class_const:
1915 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
1916 dw2_asm_output_data (1, l, NULL);
1917 dw2_asm_output_data (l, val2->v.val_int, NULL);
1918 break;
1919 case dw_val_class_vec:
1921 unsigned int elt_size = val2->v.val_vec.elt_size;
1922 unsigned int len = val2->v.val_vec.length;
1923 unsigned int i;
1924 unsigned char *p;
1926 l = len * elt_size;
1927 dw2_asm_output_data (1, l, NULL);
1928 if (elt_size > sizeof (HOST_WIDE_INT))
1930 elt_size /= 2;
1931 len *= 2;
1933 for (i = 0, p = val2->v.val_vec.array;
1934 i < len;
1935 i++, p += elt_size)
1936 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1937 "fp or vector constant word %u", i);
1939 break;
1940 case dw_val_class_const_double:
1942 unsigned HOST_WIDE_INT first, second;
1943 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
1945 dw2_asm_output_data (1, 2 * l, NULL);
1946 if (WORDS_BIG_ENDIAN)
1948 first = val2->v.val_double.high;
1949 second = val2->v.val_double.low;
1951 else
1953 first = val2->v.val_double.low;
1954 second = val2->v.val_double.high;
1956 dw2_asm_output_data (l, first, NULL);
1957 dw2_asm_output_data (l, second, NULL);
1959 break;
1960 default:
1961 gcc_unreachable ();
1964 break;
1965 case DW_OP_GNU_regval_type:
1967 unsigned r = val1->v.val_unsigned;
1968 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
1969 gcc_assert (o);
1970 if (for_eh_or_skip >= 0)
1972 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1973 gcc_assert (size_of_uleb128 (r)
1974 == size_of_uleb128 (val1->v.val_unsigned));
1976 dw2_asm_output_data_uleb128 (r, NULL);
1977 dw2_asm_output_data_uleb128 (o, NULL);
1979 break;
1980 case DW_OP_GNU_deref_type:
1982 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
1983 gcc_assert (o);
1984 dw2_asm_output_data (1, val1->v.val_int, NULL);
1985 dw2_asm_output_data_uleb128 (o, NULL);
1987 break;
1988 case DW_OP_GNU_convert:
1989 case DW_OP_GNU_reinterpret:
1990 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1991 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1992 else
1994 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
1995 gcc_assert (o);
1996 dw2_asm_output_data_uleb128 (o, NULL);
1998 break;
2000 case DW_OP_GNU_parameter_ref:
2002 unsigned long o;
2003 gcc_assert (val1->val_class == dw_val_class_die_ref);
2004 o = get_ref_die_offset (val1->v.val_die_ref.die);
2005 dw2_asm_output_data (4, o, NULL);
2007 break;
2009 default:
2010 /* Other codes have no operands. */
2011 break;
2015 /* Output a sequence of location operations.
2016 The for_eh_or_skip parameter controls whether register numbers are
2017 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2018 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2019 info). This should be suppressed for the cases that have not been converted
2020 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2022 void
2023 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2025 for (; loc != NULL; loc = loc->dw_loc_next)
2027 enum dwarf_location_atom opc = loc->dw_loc_opc;
2028 /* Output the opcode. */
2029 if (for_eh_or_skip >= 0
2030 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2032 unsigned r = (opc - DW_OP_breg0);
2033 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2034 gcc_assert (r <= 31);
2035 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2037 else if (for_eh_or_skip >= 0
2038 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2040 unsigned r = (opc - DW_OP_reg0);
2041 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2042 gcc_assert (r <= 31);
2043 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2046 dw2_asm_output_data (1, opc,
2047 "%s", dwarf_stack_op_name (opc));
2049 /* Output the operand(s) (if any). */
2050 output_loc_operands (loc, for_eh_or_skip);
2054 /* Output location description stack opcode's operands (if any).
2055 The output is single bytes on a line, suitable for .cfi_escape. */
2057 static void
2058 output_loc_operands_raw (dw_loc_descr_ref loc)
2060 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2061 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2063 switch (loc->dw_loc_opc)
2065 case DW_OP_addr:
2066 case DW_OP_implicit_value:
2067 /* We cannot output addresses in .cfi_escape, only bytes. */
2068 gcc_unreachable ();
2070 case DW_OP_const1u:
2071 case DW_OP_const1s:
2072 case DW_OP_pick:
2073 case DW_OP_deref_size:
2074 case DW_OP_xderef_size:
2075 fputc (',', asm_out_file);
2076 dw2_asm_output_data_raw (1, val1->v.val_int);
2077 break;
2079 case DW_OP_const2u:
2080 case DW_OP_const2s:
2081 fputc (',', asm_out_file);
2082 dw2_asm_output_data_raw (2, val1->v.val_int);
2083 break;
2085 case DW_OP_const4u:
2086 case DW_OP_const4s:
2087 fputc (',', asm_out_file);
2088 dw2_asm_output_data_raw (4, val1->v.val_int);
2089 break;
2091 case DW_OP_const8u:
2092 case DW_OP_const8s:
2093 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2094 fputc (',', asm_out_file);
2095 dw2_asm_output_data_raw (8, val1->v.val_int);
2096 break;
2098 case DW_OP_skip:
2099 case DW_OP_bra:
2101 int offset;
2103 gcc_assert (val1->val_class == dw_val_class_loc);
2104 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2106 fputc (',', asm_out_file);
2107 dw2_asm_output_data_raw (2, offset);
2109 break;
2111 case DW_OP_regx:
2113 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2114 gcc_assert (size_of_uleb128 (r)
2115 == size_of_uleb128 (val1->v.val_unsigned));
2116 fputc (',', asm_out_file);
2117 dw2_asm_output_data_uleb128_raw (r);
2119 break;
2121 case DW_OP_constu:
2122 case DW_OP_plus_uconst:
2123 case DW_OP_piece:
2124 fputc (',', asm_out_file);
2125 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2126 break;
2128 case DW_OP_bit_piece:
2129 fputc (',', asm_out_file);
2130 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2131 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2132 break;
2134 case DW_OP_consts:
2135 case DW_OP_breg0:
2136 case DW_OP_breg1:
2137 case DW_OP_breg2:
2138 case DW_OP_breg3:
2139 case DW_OP_breg4:
2140 case DW_OP_breg5:
2141 case DW_OP_breg6:
2142 case DW_OP_breg7:
2143 case DW_OP_breg8:
2144 case DW_OP_breg9:
2145 case DW_OP_breg10:
2146 case DW_OP_breg11:
2147 case DW_OP_breg12:
2148 case DW_OP_breg13:
2149 case DW_OP_breg14:
2150 case DW_OP_breg15:
2151 case DW_OP_breg16:
2152 case DW_OP_breg17:
2153 case DW_OP_breg18:
2154 case DW_OP_breg19:
2155 case DW_OP_breg20:
2156 case DW_OP_breg21:
2157 case DW_OP_breg22:
2158 case DW_OP_breg23:
2159 case DW_OP_breg24:
2160 case DW_OP_breg25:
2161 case DW_OP_breg26:
2162 case DW_OP_breg27:
2163 case DW_OP_breg28:
2164 case DW_OP_breg29:
2165 case DW_OP_breg30:
2166 case DW_OP_breg31:
2167 case DW_OP_fbreg:
2168 fputc (',', asm_out_file);
2169 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2170 break;
2172 case DW_OP_bregx:
2174 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2175 gcc_assert (size_of_uleb128 (r)
2176 == size_of_uleb128 (val1->v.val_unsigned));
2177 fputc (',', asm_out_file);
2178 dw2_asm_output_data_uleb128_raw (r);
2179 fputc (',', asm_out_file);
2180 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2182 break;
2184 case DW_OP_GNU_implicit_pointer:
2185 case DW_OP_GNU_entry_value:
2186 case DW_OP_GNU_const_type:
2187 case DW_OP_GNU_regval_type:
2188 case DW_OP_GNU_deref_type:
2189 case DW_OP_GNU_convert:
2190 case DW_OP_GNU_reinterpret:
2191 case DW_OP_GNU_parameter_ref:
2192 gcc_unreachable ();
2193 break;
2195 default:
2196 /* Other codes have no operands. */
2197 break;
2201 void
2202 output_loc_sequence_raw (dw_loc_descr_ref loc)
2204 while (1)
2206 enum dwarf_location_atom opc = loc->dw_loc_opc;
2207 /* Output the opcode. */
2208 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2210 unsigned r = (opc - DW_OP_breg0);
2211 r = DWARF2_FRAME_REG_OUT (r, 1);
2212 gcc_assert (r <= 31);
2213 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2215 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2217 unsigned r = (opc - DW_OP_reg0);
2218 r = DWARF2_FRAME_REG_OUT (r, 1);
2219 gcc_assert (r <= 31);
2220 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2222 /* Output the opcode. */
2223 fprintf (asm_out_file, "%#x", opc);
2224 output_loc_operands_raw (loc);
2226 if (!loc->dw_loc_next)
2227 break;
2228 loc = loc->dw_loc_next;
2230 fputc (',', asm_out_file);
2234 /* This function builds a dwarf location descriptor sequence from a
2235 dw_cfa_location, adding the given OFFSET to the result of the
2236 expression. */
2238 struct dw_loc_descr_struct *
2239 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2241 struct dw_loc_descr_struct *head, *tmp;
2243 offset += cfa->offset;
2245 if (cfa->indirect)
2247 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2248 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2249 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2250 add_loc_descr (&head, tmp);
2251 if (offset != 0)
2253 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2254 add_loc_descr (&head, tmp);
2257 else
2258 head = new_reg_loc_descr (cfa->reg, offset);
2260 return head;
2263 /* This function builds a dwarf location descriptor sequence for
2264 the address at OFFSET from the CFA when stack is aligned to
2265 ALIGNMENT byte. */
2267 struct dw_loc_descr_struct *
2268 build_cfa_aligned_loc (dw_cfa_location *cfa,
2269 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2271 struct dw_loc_descr_struct *head;
2272 unsigned int dwarf_fp
2273 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2275 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2276 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2278 head = new_reg_loc_descr (dwarf_fp, 0);
2279 add_loc_descr (&head, int_loc_descriptor (alignment));
2280 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2281 loc_descr_plus_const (&head, offset);
2283 else
2284 head = new_reg_loc_descr (dwarf_fp, offset);
2285 return head;
2288 /* And now, the support for symbolic debugging information. */
2290 /* .debug_str support. */
2291 static int output_indirect_string (void **, void *);
2293 static void dwarf2out_init (const char *);
2294 static void dwarf2out_finish (const char *);
2295 static void dwarf2out_assembly_start (void);
2296 static void dwarf2out_define (unsigned int, const char *);
2297 static void dwarf2out_undef (unsigned int, const char *);
2298 static void dwarf2out_start_source_file (unsigned, const char *);
2299 static void dwarf2out_end_source_file (unsigned);
2300 static void dwarf2out_function_decl (tree);
2301 static void dwarf2out_begin_block (unsigned, unsigned);
2302 static void dwarf2out_end_block (unsigned, unsigned);
2303 static bool dwarf2out_ignore_block (const_tree);
2304 static void dwarf2out_global_decl (tree);
2305 static void dwarf2out_type_decl (tree, int);
2306 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2307 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2308 dw_die_ref);
2309 static void dwarf2out_abstract_function (tree);
2310 static void dwarf2out_var_location (rtx);
2311 static void dwarf2out_begin_function (tree);
2312 static void dwarf2out_set_name (tree, tree);
2314 /* The debug hooks structure. */
2316 const struct gcc_debug_hooks dwarf2_debug_hooks =
2318 dwarf2out_init,
2319 dwarf2out_finish,
2320 dwarf2out_assembly_start,
2321 dwarf2out_define,
2322 dwarf2out_undef,
2323 dwarf2out_start_source_file,
2324 dwarf2out_end_source_file,
2325 dwarf2out_begin_block,
2326 dwarf2out_end_block,
2327 dwarf2out_ignore_block,
2328 dwarf2out_source_line,
2329 dwarf2out_begin_prologue,
2330 #if VMS_DEBUGGING_INFO
2331 dwarf2out_vms_end_prologue,
2332 dwarf2out_vms_begin_epilogue,
2333 #else
2334 debug_nothing_int_charstar,
2335 debug_nothing_int_charstar,
2336 #endif
2337 dwarf2out_end_epilogue,
2338 dwarf2out_begin_function,
2339 debug_nothing_int, /* end_function */
2340 dwarf2out_function_decl, /* function_decl */
2341 dwarf2out_global_decl,
2342 dwarf2out_type_decl, /* type_decl */
2343 dwarf2out_imported_module_or_decl,
2344 debug_nothing_tree, /* deferred_inline_function */
2345 /* The DWARF 2 backend tries to reduce debugging bloat by not
2346 emitting the abstract description of inline functions until
2347 something tries to reference them. */
2348 dwarf2out_abstract_function, /* outlining_inline_function */
2349 debug_nothing_rtx, /* label */
2350 debug_nothing_int, /* handle_pch */
2351 dwarf2out_var_location,
2352 dwarf2out_switch_text_section,
2353 dwarf2out_set_name,
2354 1, /* start_end_main_source_file */
2355 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2358 /* NOTE: In the comments in this file, many references are made to
2359 "Debugging Information Entries". This term is abbreviated as `DIE'
2360 throughout the remainder of this file. */
2362 /* An internal representation of the DWARF output is built, and then
2363 walked to generate the DWARF debugging info. The walk of the internal
2364 representation is done after the entire program has been compiled.
2365 The types below are used to describe the internal representation. */
2367 /* Whether to put type DIEs into their own section .debug_types instead
2368 of making them part of the .debug_info section. Only supported for
2369 Dwarf V4 or higher and the user didn't disable them through
2370 -fno-debug-types-section. It is more efficient to put them in a
2371 separate comdat sections since the linker will then be able to
2372 remove duplicates. But not all tools support .debug_types sections
2373 yet. */
2375 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2377 /* Various DIE's use offsets relative to the beginning of the
2378 .debug_info section to refer to each other. */
2380 typedef long int dw_offset;
2382 /* Define typedefs here to avoid circular dependencies. */
2384 typedef struct dw_attr_struct *dw_attr_ref;
2385 typedef struct dw_line_info_struct *dw_line_info_ref;
2386 typedef struct pubname_struct *pubname_ref;
2387 typedef struct dw_ranges_struct *dw_ranges_ref;
2388 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
2389 typedef struct comdat_type_struct *comdat_type_node_ref;
2391 /* The entries in the line_info table more-or-less mirror the opcodes
2392 that are used in the real dwarf line table. Arrays of these entries
2393 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2394 supported. */
2396 enum dw_line_info_opcode {
2397 /* Emit DW_LNE_set_address; the operand is the label index. */
2398 LI_set_address,
2400 /* Emit a row to the matrix with the given line. This may be done
2401 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2402 special opcodes. */
2403 LI_set_line,
2405 /* Emit a DW_LNS_set_file. */
2406 LI_set_file,
2408 /* Emit a DW_LNS_set_column. */
2409 LI_set_column,
2411 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2412 LI_negate_stmt,
2414 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2415 LI_set_prologue_end,
2416 LI_set_epilogue_begin,
2418 /* Emit a DW_LNE_set_discriminator. */
2419 LI_set_discriminator
2422 typedef struct GTY(()) dw_line_info_struct {
2423 enum dw_line_info_opcode opcode;
2424 unsigned int val;
2425 } dw_line_info_entry;
2427 DEF_VEC_O(dw_line_info_entry);
2428 DEF_VEC_ALLOC_O(dw_line_info_entry, gc);
2430 typedef struct GTY(()) dw_line_info_table_struct {
2431 /* The label that marks the end of this section. */
2432 const char *end_label;
2434 /* The values for the last row of the matrix, as collected in the table.
2435 These are used to minimize the changes to the next row. */
2436 unsigned int file_num;
2437 unsigned int line_num;
2438 unsigned int column_num;
2439 int discrim_num;
2440 bool is_stmt;
2441 bool in_use;
2443 VEC(dw_line_info_entry, gc) *entries;
2444 } dw_line_info_table;
2446 typedef dw_line_info_table *dw_line_info_table_p;
2448 DEF_VEC_P(dw_line_info_table_p);
2449 DEF_VEC_ALLOC_P(dw_line_info_table_p, gc);
2451 /* Each DIE attribute has a field specifying the attribute kind,
2452 a link to the next attribute in the chain, and an attribute value.
2453 Attributes are typically linked below the DIE they modify. */
2455 typedef struct GTY(()) dw_attr_struct {
2456 enum dwarf_attribute dw_attr;
2457 dw_val_node dw_attr_val;
2459 dw_attr_node;
2461 DEF_VEC_O(dw_attr_node);
2462 DEF_VEC_ALLOC_O(dw_attr_node,gc);
2464 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2465 The children of each node form a circular list linked by
2466 die_sib. die_child points to the node *before* the "first" child node. */
2468 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
2469 union die_symbol_or_type_node
2471 const char * GTY ((tag ("0"))) die_symbol;
2472 comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
2474 GTY ((desc ("%0.comdat_type_p"))) die_id;
2475 VEC(dw_attr_node,gc) * die_attr;
2476 dw_die_ref die_parent;
2477 dw_die_ref die_child;
2478 dw_die_ref die_sib;
2479 dw_die_ref die_definition; /* ref from a specification to its definition */
2480 dw_offset die_offset;
2481 unsigned long die_abbrev;
2482 int die_mark;
2483 unsigned int decl_id;
2484 enum dwarf_tag die_tag;
2485 /* Die is used and must not be pruned as unused. */
2486 BOOL_BITFIELD die_perennial_p : 1;
2487 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2488 /* Lots of spare bits. */
2490 die_node;
2492 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2493 #define FOR_EACH_CHILD(die, c, expr) do { \
2494 c = die->die_child; \
2495 if (c) do { \
2496 c = c->die_sib; \
2497 expr; \
2498 } while (c != die->die_child); \
2499 } while (0)
2501 /* The pubname structure */
2503 typedef struct GTY(()) pubname_struct {
2504 dw_die_ref die;
2505 const char *name;
2507 pubname_entry;
2509 DEF_VEC_O(pubname_entry);
2510 DEF_VEC_ALLOC_O(pubname_entry, gc);
2512 struct GTY(()) dw_ranges_struct {
2513 /* If this is positive, it's a block number, otherwise it's a
2514 bitwise-negated index into dw_ranges_by_label. */
2515 int num;
2518 /* A structure to hold a macinfo entry. */
2520 typedef struct GTY(()) macinfo_struct {
2521 unsigned char code;
2522 unsigned HOST_WIDE_INT lineno;
2523 const char *info;
2525 macinfo_entry;
2527 DEF_VEC_O(macinfo_entry);
2528 DEF_VEC_ALLOC_O(macinfo_entry, gc);
2530 struct GTY(()) dw_ranges_by_label_struct {
2531 const char *begin;
2532 const char *end;
2535 /* The comdat type node structure. */
2536 typedef struct GTY(()) comdat_type_struct
2538 dw_die_ref root_die;
2539 dw_die_ref type_die;
2540 dw_die_ref skeleton_die;
2541 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2542 struct comdat_type_struct *next;
2544 comdat_type_node;
2546 /* The limbo die list structure. */
2547 typedef struct GTY(()) limbo_die_struct {
2548 dw_die_ref die;
2549 tree created_for;
2550 struct limbo_die_struct *next;
2552 limbo_die_node;
2554 typedef struct skeleton_chain_struct
2556 dw_die_ref old_die;
2557 dw_die_ref new_die;
2558 struct skeleton_chain_struct *parent;
2560 skeleton_chain_node;
2562 /* Define a macro which returns nonzero for a TYPE_DECL which was
2563 implicitly generated for a type.
2565 Note that, unlike the C front-end (which generates a NULL named
2566 TYPE_DECL node for each complete tagged type, each array type,
2567 and each function type node created) the C++ front-end generates
2568 a _named_ TYPE_DECL node for each tagged type node created.
2569 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2570 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2571 front-end, but for each type, tagged or not. */
2573 #define TYPE_DECL_IS_STUB(decl) \
2574 (DECL_NAME (decl) == NULL_TREE \
2575 || (DECL_ARTIFICIAL (decl) \
2576 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
2577 /* This is necessary for stub decls that \
2578 appear in nested inline functions. */ \
2579 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2580 && (decl_ultimate_origin (decl) \
2581 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2583 /* Information concerning the compilation unit's programming
2584 language, and compiler version. */
2586 /* Fixed size portion of the DWARF compilation unit header. */
2587 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2588 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2590 /* Fixed size portion of the DWARF comdat type unit header. */
2591 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2592 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2593 + DWARF_OFFSET_SIZE)
2595 /* Fixed size portion of public names info. */
2596 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2598 /* Fixed size portion of the address range info. */
2599 #define DWARF_ARANGES_HEADER_SIZE \
2600 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2601 DWARF2_ADDR_SIZE * 2) \
2602 - DWARF_INITIAL_LENGTH_SIZE)
2604 /* Size of padding portion in the address range info. It must be
2605 aligned to twice the pointer size. */
2606 #define DWARF_ARANGES_PAD_SIZE \
2607 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2608 DWARF2_ADDR_SIZE * 2) \
2609 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2611 /* Use assembler line directives if available. */
2612 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2613 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2614 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2615 #else
2616 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2617 #endif
2618 #endif
2620 /* Minimum line offset in a special line info. opcode.
2621 This value was chosen to give a reasonable range of values. */
2622 #define DWARF_LINE_BASE -10
2624 /* First special line opcode - leave room for the standard opcodes. */
2625 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
2627 /* Range of line offsets in a special line info. opcode. */
2628 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
2630 /* Flag that indicates the initial value of the is_stmt_start flag.
2631 In the present implementation, we do not mark any lines as
2632 the beginning of a source statement, because that information
2633 is not made available by the GCC front-end. */
2634 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2636 /* Maximum number of operations per instruction bundle. */
2637 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2638 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2639 #endif
2641 /* This location is used by calc_die_sizes() to keep track
2642 the offset of each DIE within the .debug_info section. */
2643 static unsigned long next_die_offset;
2645 /* Record the root of the DIE's built for the current compilation unit. */
2646 static GTY(()) dw_die_ref single_comp_unit_die;
2648 /* A list of type DIEs that have been separated into comdat sections. */
2649 static GTY(()) comdat_type_node *comdat_type_list;
2651 /* A list of DIEs with a NULL parent waiting to be relocated. */
2652 static GTY(()) limbo_die_node *limbo_die_list;
2654 /* A list of DIEs for which we may have to generate
2655 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
2656 static GTY(()) limbo_die_node *deferred_asm_name;
2658 /* Filenames referenced by this compilation unit. */
2659 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
2661 /* A hash table of references to DIE's that describe declarations.
2662 The key is a DECL_UID() which is a unique number identifying each decl. */
2663 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
2665 /* A hash table of references to DIE's that describe COMMON blocks.
2666 The key is DECL_UID() ^ die_parent. */
2667 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
2669 typedef struct GTY(()) die_arg_entry_struct {
2670 dw_die_ref die;
2671 tree arg;
2672 } die_arg_entry;
2674 DEF_VEC_O(die_arg_entry);
2675 DEF_VEC_ALLOC_O(die_arg_entry,gc);
2677 /* Node of the variable location list. */
2678 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2679 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2680 EXPR_LIST chain. For small bitsizes, bitsize is encoded
2681 in mode of the EXPR_LIST node and first EXPR_LIST operand
2682 is either NOTE_INSN_VAR_LOCATION for a piece with a known
2683 location or NULL for padding. For larger bitsizes,
2684 mode is 0 and first operand is a CONCAT with bitsize
2685 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2686 NULL as second operand. */
2687 rtx GTY (()) loc;
2688 const char * GTY (()) label;
2689 struct var_loc_node * GTY (()) next;
2692 /* Variable location list. */
2693 struct GTY (()) var_loc_list_def {
2694 struct var_loc_node * GTY (()) first;
2696 /* Pointer to the last but one or last element of the
2697 chained list. If the list is empty, both first and
2698 last are NULL, if the list contains just one node
2699 or the last node certainly is not redundant, it points
2700 to the last node, otherwise points to the last but one.
2701 Do not mark it for GC because it is marked through the chain. */
2702 struct var_loc_node * GTY ((skip ("%h"))) last;
2704 /* Pointer to the last element before section switch,
2705 if NULL, either sections weren't switched or first
2706 is after section switch. */
2707 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2709 /* DECL_UID of the variable decl. */
2710 unsigned int decl_id;
2712 typedef struct var_loc_list_def var_loc_list;
2714 /* Call argument location list. */
2715 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2716 rtx GTY (()) call_arg_loc_note;
2717 const char * GTY (()) label;
2718 tree GTY (()) block;
2719 bool tail_call_p;
2720 rtx GTY (()) symbol_ref;
2721 struct call_arg_loc_node * GTY (()) next;
2725 /* Table of decl location linked lists. */
2726 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
2728 /* Head and tail of call_arg_loc chain. */
2729 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2730 static struct call_arg_loc_node *call_arg_loc_last;
2732 /* Number of call sites in the current function. */
2733 static int call_site_count = -1;
2734 /* Number of tail call sites in the current function. */
2735 static int tail_call_site_count = -1;
2737 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
2738 DIEs. */
2739 static VEC (dw_die_ref, heap) *block_map;
2741 /* A cached location list. */
2742 struct GTY (()) cached_dw_loc_list_def {
2743 /* The DECL_UID of the decl that this entry describes. */
2744 unsigned int decl_id;
2746 /* The cached location list. */
2747 dw_loc_list_ref loc_list;
2749 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2751 /* Table of cached location lists. */
2752 static GTY ((param_is (cached_dw_loc_list))) htab_t cached_dw_loc_list_table;
2754 /* A pointer to the base of a list of references to DIE's that
2755 are uniquely identified by their tag, presence/absence of
2756 children DIE's, and list of attribute/value pairs. */
2757 static GTY((length ("abbrev_die_table_allocated")))
2758 dw_die_ref *abbrev_die_table;
2760 /* Number of elements currently allocated for abbrev_die_table. */
2761 static GTY(()) unsigned abbrev_die_table_allocated;
2763 /* Number of elements in type_die_table currently in use. */
2764 static GTY(()) unsigned abbrev_die_table_in_use;
2766 /* Size (in elements) of increments by which we may expand the
2767 abbrev_die_table. */
2768 #define ABBREV_DIE_TABLE_INCREMENT 256
2770 /* A global counter for generating labels for line number data. */
2771 static unsigned int line_info_label_num;
2773 /* The current table to which we should emit line number information
2774 for the current function. This will be set up at the beginning of
2775 assembly for the function. */
2776 static dw_line_info_table *cur_line_info_table;
2778 /* The two default tables of line number info. */
2779 static GTY(()) dw_line_info_table *text_section_line_info;
2780 static GTY(()) dw_line_info_table *cold_text_section_line_info;
2782 /* The set of all non-default tables of line number info. */
2783 static GTY(()) VEC (dw_line_info_table_p, gc) *separate_line_info;
2785 /* A flag to tell pubnames/types export if there is an info section to
2786 refer to. */
2787 static bool info_section_emitted;
2789 /* A pointer to the base of a table that contains a list of publicly
2790 accessible names. */
2791 static GTY (()) VEC (pubname_entry, gc) * pubname_table;
2793 /* A pointer to the base of a table that contains a list of publicly
2794 accessible types. */
2795 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
2797 /* A pointer to the base of a table that contains a list of macro
2798 defines/undefines (and file start/end markers). */
2799 static GTY (()) VEC (macinfo_entry, gc) * macinfo_table;
2801 /* True if .debug_macinfo or .debug_macros section is going to be
2802 emitted. */
2803 #define have_macinfo \
2804 (debug_info_level >= DINFO_LEVEL_VERBOSE \
2805 && !VEC_empty (macinfo_entry, macinfo_table))
2807 /* Array of dies for which we should generate .debug_ranges info. */
2808 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
2810 /* Number of elements currently allocated for ranges_table. */
2811 static GTY(()) unsigned ranges_table_allocated;
2813 /* Number of elements in ranges_table currently in use. */
2814 static GTY(()) unsigned ranges_table_in_use;
2816 /* Array of pairs of labels referenced in ranges_table. */
2817 static GTY ((length ("ranges_by_label_allocated")))
2818 dw_ranges_by_label_ref ranges_by_label;
2820 /* Number of elements currently allocated for ranges_by_label. */
2821 static GTY(()) unsigned ranges_by_label_allocated;
2823 /* Number of elements in ranges_by_label currently in use. */
2824 static GTY(()) unsigned ranges_by_label_in_use;
2826 /* Size (in elements) of increments by which we may expand the
2827 ranges_table. */
2828 #define RANGES_TABLE_INCREMENT 64
2830 /* Whether we have location lists that need outputting */
2831 static GTY(()) bool have_location_lists;
2833 /* Unique label counter. */
2834 static GTY(()) unsigned int loclabel_num;
2836 /* Unique label counter for point-of-call tables. */
2837 static GTY(()) unsigned int poc_label_num;
2839 /* Record whether the function being analyzed contains inlined functions. */
2840 static int current_function_has_inlines;
2842 /* The last file entry emitted by maybe_emit_file(). */
2843 static GTY(()) struct dwarf_file_data * last_emitted_file;
2845 /* Number of internal labels generated by gen_internal_sym(). */
2846 static GTY(()) int label_num;
2848 /* Cached result of previous call to lookup_filename. */
2849 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
2851 static GTY(()) VEC(die_arg_entry,gc) *tmpl_value_parm_die_table;
2853 /* Instances of generic types for which we need to generate debug
2854 info that describe their generic parameters and arguments. That
2855 generation needs to happen once all types are properly laid out so
2856 we do it at the end of compilation. */
2857 static GTY(()) VEC(tree,gc) *generic_type_instances;
2859 /* Offset from the "steady-state frame pointer" to the frame base,
2860 within the current function. */
2861 static HOST_WIDE_INT frame_pointer_fb_offset;
2862 static bool frame_pointer_fb_offset_valid;
2864 static VEC (dw_die_ref, heap) *base_types;
2866 /* Forward declarations for functions defined in this file. */
2868 static int is_pseudo_reg (const_rtx);
2869 static tree type_main_variant (tree);
2870 static int is_tagged_type (const_tree);
2871 static const char *dwarf_tag_name (unsigned);
2872 static const char *dwarf_attr_name (unsigned);
2873 static const char *dwarf_form_name (unsigned);
2874 static tree decl_ultimate_origin (const_tree);
2875 static tree decl_class_context (tree);
2876 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
2877 static inline enum dw_val_class AT_class (dw_attr_ref);
2878 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
2879 static inline unsigned AT_flag (dw_attr_ref);
2880 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
2881 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
2882 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
2883 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
2884 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
2885 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2886 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
2887 unsigned int, unsigned char *);
2888 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
2889 static hashval_t debug_str_do_hash (const void *);
2890 static int debug_str_eq (const void *, const void *);
2891 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
2892 static inline const char *AT_string (dw_attr_ref);
2893 static enum dwarf_form AT_string_form (dw_attr_ref);
2894 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
2895 static void add_AT_specification (dw_die_ref, dw_die_ref);
2896 static inline dw_die_ref AT_ref (dw_attr_ref);
2897 static inline int AT_ref_external (dw_attr_ref);
2898 static inline void set_AT_ref_external (dw_attr_ref, int);
2899 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
2900 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
2901 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
2902 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
2903 dw_loc_list_ref);
2904 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
2905 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
2906 static inline rtx AT_addr (dw_attr_ref);
2907 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
2908 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
2909 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
2910 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
2911 unsigned HOST_WIDE_INT);
2912 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
2913 unsigned long);
2914 static inline const char *AT_lbl (dw_attr_ref);
2915 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
2916 static const char *get_AT_low_pc (dw_die_ref);
2917 static const char *get_AT_hi_pc (dw_die_ref);
2918 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
2919 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
2920 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
2921 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
2922 static bool is_cxx (void);
2923 static bool is_fortran (void);
2924 static bool is_ada (void);
2925 static void remove_AT (dw_die_ref, enum dwarf_attribute);
2926 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
2927 static void add_child_die (dw_die_ref, dw_die_ref);
2928 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
2929 static dw_die_ref lookup_type_die (tree);
2930 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
2931 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
2932 static void equate_type_number_to_die (tree, dw_die_ref);
2933 static hashval_t decl_die_table_hash (const void *);
2934 static int decl_die_table_eq (const void *, const void *);
2935 static dw_die_ref lookup_decl_die (tree);
2936 static hashval_t common_block_die_table_hash (const void *);
2937 static int common_block_die_table_eq (const void *, const void *);
2938 static hashval_t decl_loc_table_hash (const void *);
2939 static int decl_loc_table_eq (const void *, const void *);
2940 static var_loc_list *lookup_decl_loc (const_tree);
2941 static void equate_decl_number_to_die (tree, dw_die_ref);
2942 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
2943 static void print_spaces (FILE *);
2944 static void print_die (dw_die_ref, FILE *);
2945 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
2946 static dw_die_ref pop_compile_unit (dw_die_ref);
2947 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
2948 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
2949 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
2950 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
2951 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
2952 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
2953 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
2954 struct md5_ctx *, int *);
2955 struct checksum_attributes;
2956 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
2957 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
2958 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
2959 static void generate_type_signature (dw_die_ref, comdat_type_node *);
2960 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
2961 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
2962 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
2963 static int same_die_p (dw_die_ref, dw_die_ref, int *);
2964 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
2965 static void compute_section_prefix (dw_die_ref);
2966 static int is_type_die (dw_die_ref);
2967 static int is_comdat_die (dw_die_ref);
2968 static int is_symbol_die (dw_die_ref);
2969 static void assign_symbol_names (dw_die_ref);
2970 static void break_out_includes (dw_die_ref);
2971 static int is_declaration_die (dw_die_ref);
2972 static int should_move_die_to_comdat (dw_die_ref);
2973 static dw_die_ref clone_as_declaration (dw_die_ref);
2974 static dw_die_ref clone_die (dw_die_ref);
2975 static dw_die_ref clone_tree (dw_die_ref);
2976 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
2977 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
2978 static void generate_skeleton_bottom_up (skeleton_chain_node *);
2979 static dw_die_ref generate_skeleton (dw_die_ref);
2980 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
2981 dw_die_ref,
2982 dw_die_ref);
2983 static void break_out_comdat_types (dw_die_ref);
2984 static dw_die_ref copy_ancestor_tree (dw_die_ref, dw_die_ref, htab_t);
2985 static void copy_decls_walk (dw_die_ref, dw_die_ref, htab_t);
2986 static void copy_decls_for_unworthy_types (dw_die_ref);
2988 static hashval_t htab_cu_hash (const void *);
2989 static int htab_cu_eq (const void *, const void *);
2990 static void htab_cu_del (void *);
2991 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
2992 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
2993 static void add_sibling_attributes (dw_die_ref);
2994 static void build_abbrev_table (dw_die_ref, htab_t);
2995 static void output_location_lists (dw_die_ref);
2996 static int constant_size (unsigned HOST_WIDE_INT);
2997 static unsigned long size_of_die (dw_die_ref);
2998 static void calc_die_sizes (dw_die_ref);
2999 static void calc_base_type_die_sizes (void);
3000 static void mark_dies (dw_die_ref);
3001 static void unmark_dies (dw_die_ref);
3002 static void unmark_all_dies (dw_die_ref);
3003 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
3004 static unsigned long size_of_aranges (void);
3005 static enum dwarf_form value_format (dw_attr_ref);
3006 static void output_value_format (dw_attr_ref);
3007 static void output_abbrev_section (void);
3008 static void output_die_symbol (dw_die_ref);
3009 static void output_die (dw_die_ref);
3010 static void output_compilation_unit_header (void);
3011 static void output_comp_unit (dw_die_ref, int);
3012 static void output_comdat_type_unit (comdat_type_node *);
3013 static const char *dwarf2_name (tree, int);
3014 static void add_pubname (tree, dw_die_ref);
3015 static void add_enumerator_pubname (const char *, dw_die_ref);
3016 static void add_pubname_string (const char *, dw_die_ref);
3017 static void add_pubtype (tree, dw_die_ref);
3018 static void output_pubnames (VEC (pubname_entry,gc) *);
3019 static void output_aranges (unsigned long);
3020 static unsigned int add_ranges_num (int);
3021 static unsigned int add_ranges (const_tree);
3022 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3023 bool *);
3024 static void output_ranges (void);
3025 static dw_line_info_table *new_line_info_table (void);
3026 static void output_line_info (void);
3027 static void output_file_names (void);
3028 static dw_die_ref base_type_die (tree);
3029 static int is_base_type (tree);
3030 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3031 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
3032 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3033 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3034 static int type_is_enum (const_tree);
3035 static unsigned int dbx_reg_number (const_rtx);
3036 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3037 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3038 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3039 enum var_init_status);
3040 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3041 enum var_init_status);
3042 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3043 enum var_init_status);
3044 static int is_based_loc (const_rtx);
3045 static int resolve_one_addr (rtx *, void *);
3046 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3047 enum var_init_status);
3048 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
3049 enum var_init_status);
3050 static dw_loc_list_ref loc_list_from_tree (tree, int);
3051 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
3052 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3053 static tree field_type (const_tree);
3054 static unsigned int simple_type_align_in_bits (const_tree);
3055 static unsigned int simple_decl_align_in_bits (const_tree);
3056 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3057 static HOST_WIDE_INT field_byte_offset (const_tree);
3058 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3059 dw_loc_list_ref);
3060 static void add_data_member_location_attribute (dw_die_ref, tree);
3061 static bool add_const_value_attribute (dw_die_ref, rtx);
3062 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3063 static void insert_double (double_int, unsigned char *);
3064 static void insert_float (const_rtx, unsigned char *);
3065 static rtx rtl_for_decl_location (tree);
3066 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3067 enum dwarf_attribute);
3068 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3069 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3070 static void add_name_attribute (dw_die_ref, const char *);
3071 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3072 static void add_comp_dir_attribute (dw_die_ref);
3073 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
3074 static void add_subscript_info (dw_die_ref, tree, bool);
3075 static void add_byte_size_attribute (dw_die_ref, tree);
3076 static void add_bit_offset_attribute (dw_die_ref, tree);
3077 static void add_bit_size_attribute (dw_die_ref, tree);
3078 static void add_prototyped_attribute (dw_die_ref, tree);
3079 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3080 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3081 static void add_src_coords_attributes (dw_die_ref, tree);
3082 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3083 static void push_decl_scope (tree);
3084 static void pop_decl_scope (void);
3085 static dw_die_ref scope_die_for (tree, dw_die_ref);
3086 static inline int local_scope_p (dw_die_ref);
3087 static inline int class_scope_p (dw_die_ref);
3088 static inline int class_or_namespace_scope_p (dw_die_ref);
3089 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
3090 static void add_calling_convention_attribute (dw_die_ref, tree);
3091 static const char *type_tag (const_tree);
3092 static tree member_declared_type (const_tree);
3093 #if 0
3094 static const char *decl_start_label (tree);
3095 #endif
3096 static void gen_array_type_die (tree, dw_die_ref);
3097 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3098 #if 0
3099 static void gen_entry_point_die (tree, dw_die_ref);
3100 #endif
3101 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3102 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3103 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3104 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3105 static void gen_formal_types_die (tree, dw_die_ref);
3106 static void gen_subprogram_die (tree, dw_die_ref);
3107 static void gen_variable_die (tree, tree, dw_die_ref);
3108 static void gen_const_die (tree, dw_die_ref);
3109 static void gen_label_die (tree, dw_die_ref);
3110 static void gen_lexical_block_die (tree, dw_die_ref, int);
3111 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
3112 static void gen_field_die (tree, dw_die_ref);
3113 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3114 static dw_die_ref gen_compile_unit_die (const char *);
3115 static void gen_inheritance_die (tree, tree, dw_die_ref);
3116 static void gen_member_die (tree, dw_die_ref);
3117 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3118 enum debug_info_usage);
3119 static void gen_subroutine_type_die (tree, dw_die_ref);
3120 static void gen_typedef_die (tree, dw_die_ref);
3121 static void gen_type_die (tree, dw_die_ref);
3122 static void gen_block_die (tree, dw_die_ref, int);
3123 static void decls_for_scope (tree, dw_die_ref, int);
3124 static inline int is_redundant_typedef (const_tree);
3125 static bool is_naming_typedef_decl (const_tree);
3126 static inline dw_die_ref get_context_die (tree);
3127 static void gen_namespace_die (tree, dw_die_ref);
3128 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3129 static dw_die_ref force_decl_die (tree);
3130 static dw_die_ref force_type_die (tree);
3131 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3132 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3133 static struct dwarf_file_data * lookup_filename (const char *);
3134 static void retry_incomplete_types (void);
3135 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3136 static void gen_generic_params_dies (tree);
3137 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3138 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3139 static void splice_child_die (dw_die_ref, dw_die_ref);
3140 static int file_info_cmp (const void *, const void *);
3141 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3142 const char *, const char *);
3143 static void output_loc_list (dw_loc_list_ref);
3144 static char *gen_internal_sym (const char *);
3145 static bool want_pubnames (void);
3147 static void prune_unmark_dies (dw_die_ref);
3148 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3149 static void prune_unused_types_mark (dw_die_ref, int);
3150 static void prune_unused_types_walk (dw_die_ref);
3151 static void prune_unused_types_walk_attribs (dw_die_ref);
3152 static void prune_unused_types_prune (dw_die_ref);
3153 static void prune_unused_types (void);
3154 static int maybe_emit_file (struct dwarf_file_data *fd);
3155 static inline const char *AT_vms_delta1 (dw_attr_ref);
3156 static inline const char *AT_vms_delta2 (dw_attr_ref);
3157 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3158 const char *, const char *);
3159 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3160 static void gen_remaining_tmpl_value_param_die_attribute (void);
3161 static bool generic_type_p (tree);
3162 static void schedule_generic_params_dies_gen (tree t);
3163 static void gen_scheduled_generic_parms_dies (void);
3165 /* Section names used to hold DWARF debugging information. */
3166 #ifndef DEBUG_INFO_SECTION
3167 #define DEBUG_INFO_SECTION ".debug_info"
3168 #endif
3169 #ifndef DEBUG_ABBREV_SECTION
3170 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3171 #endif
3172 #ifndef DEBUG_ARANGES_SECTION
3173 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3174 #endif
3175 #ifndef DEBUG_MACINFO_SECTION
3176 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
3177 #endif
3178 #ifndef DEBUG_MACRO_SECTION
3179 #define DEBUG_MACRO_SECTION ".debug_macro"
3180 #endif
3181 #ifndef DEBUG_LINE_SECTION
3182 #define DEBUG_LINE_SECTION ".debug_line"
3183 #endif
3184 #ifndef DEBUG_LOC_SECTION
3185 #define DEBUG_LOC_SECTION ".debug_loc"
3186 #endif
3187 #ifndef DEBUG_PUBNAMES_SECTION
3188 #define DEBUG_PUBNAMES_SECTION ".debug_pubnames"
3189 #endif
3190 #ifndef DEBUG_PUBTYPES_SECTION
3191 #define DEBUG_PUBTYPES_SECTION ".debug_pubtypes"
3192 #endif
3193 #ifndef DEBUG_STR_SECTION
3194 #define DEBUG_STR_SECTION ".debug_str"
3195 #endif
3196 #ifndef DEBUG_RANGES_SECTION
3197 #define DEBUG_RANGES_SECTION ".debug_ranges"
3198 #endif
3200 /* Standard ELF section names for compiled code and data. */
3201 #ifndef TEXT_SECTION_NAME
3202 #define TEXT_SECTION_NAME ".text"
3203 #endif
3205 /* Section flags for .debug_str section. */
3206 #define DEBUG_STR_SECTION_FLAGS \
3207 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3208 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3209 : SECTION_DEBUG)
3211 /* Labels we insert at beginning sections we can reference instead of
3212 the section names themselves. */
3214 #ifndef TEXT_SECTION_LABEL
3215 #define TEXT_SECTION_LABEL "Ltext"
3216 #endif
3217 #ifndef COLD_TEXT_SECTION_LABEL
3218 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3219 #endif
3220 #ifndef DEBUG_LINE_SECTION_LABEL
3221 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3222 #endif
3223 #ifndef DEBUG_INFO_SECTION_LABEL
3224 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3225 #endif
3226 #ifndef DEBUG_ABBREV_SECTION_LABEL
3227 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3228 #endif
3229 #ifndef DEBUG_LOC_SECTION_LABEL
3230 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3231 #endif
3232 #ifndef DEBUG_RANGES_SECTION_LABEL
3233 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3234 #endif
3235 #ifndef DEBUG_MACINFO_SECTION_LABEL
3236 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3237 #endif
3238 #ifndef DEBUG_MACRO_SECTION_LABEL
3239 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3240 #endif
3243 /* Definitions of defaults for formats and names of various special
3244 (artificial) labels which may be generated within this file (when the -g
3245 options is used and DWARF2_DEBUGGING_INFO is in effect.
3246 If necessary, these may be overridden from within the tm.h file, but
3247 typically, overriding these defaults is unnecessary. */
3249 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3250 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3251 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3252 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3253 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3254 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3255 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3256 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3257 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3258 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3260 #ifndef TEXT_END_LABEL
3261 #define TEXT_END_LABEL "Letext"
3262 #endif
3263 #ifndef COLD_END_LABEL
3264 #define COLD_END_LABEL "Letext_cold"
3265 #endif
3266 #ifndef BLOCK_BEGIN_LABEL
3267 #define BLOCK_BEGIN_LABEL "LBB"
3268 #endif
3269 #ifndef BLOCK_END_LABEL
3270 #define BLOCK_END_LABEL "LBE"
3271 #endif
3272 #ifndef LINE_CODE_LABEL
3273 #define LINE_CODE_LABEL "LM"
3274 #endif
3277 /* Return the root of the DIE's built for the current compilation unit. */
3278 static dw_die_ref
3279 comp_unit_die (void)
3281 if (!single_comp_unit_die)
3282 single_comp_unit_die = gen_compile_unit_die (NULL);
3283 return single_comp_unit_die;
3286 /* We allow a language front-end to designate a function that is to be
3287 called to "demangle" any name before it is put into a DIE. */
3289 static const char *(*demangle_name_func) (const char *);
3291 void
3292 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3294 demangle_name_func = func;
3297 /* Test if rtl node points to a pseudo register. */
3299 static inline int
3300 is_pseudo_reg (const_rtx rtl)
3302 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3303 || (GET_CODE (rtl) == SUBREG
3304 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3307 /* Return a reference to a type, with its const and volatile qualifiers
3308 removed. */
3310 static inline tree
3311 type_main_variant (tree type)
3313 type = TYPE_MAIN_VARIANT (type);
3315 /* ??? There really should be only one main variant among any group of
3316 variants of a given type (and all of the MAIN_VARIANT values for all
3317 members of the group should point to that one type) but sometimes the C
3318 front-end messes this up for array types, so we work around that bug
3319 here. */
3320 if (TREE_CODE (type) == ARRAY_TYPE)
3321 while (type != TYPE_MAIN_VARIANT (type))
3322 type = TYPE_MAIN_VARIANT (type);
3324 return type;
3327 /* Return nonzero if the given type node represents a tagged type. */
3329 static inline int
3330 is_tagged_type (const_tree type)
3332 enum tree_code code = TREE_CODE (type);
3334 return (code == RECORD_TYPE || code == UNION_TYPE
3335 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3338 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3340 static void
3341 get_ref_die_offset_label (char *label, dw_die_ref ref)
3343 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3346 /* Return die_offset of a DIE reference to a base type. */
3348 static unsigned long int
3349 get_base_type_offset (dw_die_ref ref)
3351 if (ref->die_offset)
3352 return ref->die_offset;
3353 if (comp_unit_die ()->die_abbrev)
3355 calc_base_type_die_sizes ();
3356 gcc_assert (ref->die_offset);
3358 return ref->die_offset;
3361 /* Return die_offset of a DIE reference other than base type. */
3363 static unsigned long int
3364 get_ref_die_offset (dw_die_ref ref)
3366 gcc_assert (ref->die_offset);
3367 return ref->die_offset;
3370 /* Convert a DIE tag into its string name. */
3372 static const char *
3373 dwarf_tag_name (unsigned int tag)
3375 const char *name = get_DW_TAG_name (tag);
3377 if (name != NULL)
3378 return name;
3380 return "DW_TAG_<unknown>";
3383 /* Convert a DWARF attribute code into its string name. */
3385 static const char *
3386 dwarf_attr_name (unsigned int attr)
3388 const char *name;
3390 switch (attr)
3392 #if VMS_DEBUGGING_INFO
3393 case DW_AT_HP_prologue:
3394 return "DW_AT_HP_prologue";
3395 #else
3396 case DW_AT_MIPS_loop_unroll_factor:
3397 return "DW_AT_MIPS_loop_unroll_factor";
3398 #endif
3400 #if VMS_DEBUGGING_INFO
3401 case DW_AT_HP_epilogue:
3402 return "DW_AT_HP_epilogue";
3403 #else
3404 case DW_AT_MIPS_stride:
3405 return "DW_AT_MIPS_stride";
3406 #endif
3409 name = get_DW_AT_name (attr);
3411 if (name != NULL)
3412 return name;
3414 return "DW_AT_<unknown>";
3417 /* Convert a DWARF value form code into its string name. */
3419 static const char *
3420 dwarf_form_name (unsigned int form)
3422 const char *name = get_DW_FORM_name (form);
3424 if (name != NULL)
3425 return name;
3427 return "DW_FORM_<unknown>";
3430 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3431 instance of an inlined instance of a decl which is local to an inline
3432 function, so we have to trace all of the way back through the origin chain
3433 to find out what sort of node actually served as the original seed for the
3434 given block. */
3436 static tree
3437 decl_ultimate_origin (const_tree decl)
3439 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3440 return NULL_TREE;
3442 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
3443 nodes in the function to point to themselves; ignore that if
3444 we're trying to output the abstract instance of this function. */
3445 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3446 return NULL_TREE;
3448 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3449 most distant ancestor, this should never happen. */
3450 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3452 return DECL_ABSTRACT_ORIGIN (decl);
3455 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
3456 of a virtual function may refer to a base class, so we check the 'this'
3457 parameter. */
3459 static tree
3460 decl_class_context (tree decl)
3462 tree context = NULL_TREE;
3464 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3465 context = DECL_CONTEXT (decl);
3466 else
3467 context = TYPE_MAIN_VARIANT
3468 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3470 if (context && !TYPE_P (context))
3471 context = NULL_TREE;
3473 return context;
3476 /* Add an attribute/value pair to a DIE. */
3478 static inline void
3479 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
3481 /* Maybe this should be an assert? */
3482 if (die == NULL)
3483 return;
3485 if (die->die_attr == NULL)
3486 die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
3487 VEC_safe_push (dw_attr_node, gc, die->die_attr, *attr);
3490 static inline enum dw_val_class
3491 AT_class (dw_attr_ref a)
3493 return a->dw_attr_val.val_class;
3496 /* Add a flag value attribute to a DIE. */
3498 static inline void
3499 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3501 dw_attr_node attr;
3503 attr.dw_attr = attr_kind;
3504 attr.dw_attr_val.val_class = dw_val_class_flag;
3505 attr.dw_attr_val.v.val_flag = flag;
3506 add_dwarf_attr (die, &attr);
3509 static inline unsigned
3510 AT_flag (dw_attr_ref a)
3512 gcc_assert (a && AT_class (a) == dw_val_class_flag);
3513 return a->dw_attr_val.v.val_flag;
3516 /* Add a signed integer attribute value to a DIE. */
3518 static inline void
3519 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3521 dw_attr_node attr;
3523 attr.dw_attr = attr_kind;
3524 attr.dw_attr_val.val_class = dw_val_class_const;
3525 attr.dw_attr_val.v.val_int = int_val;
3526 add_dwarf_attr (die, &attr);
3529 static inline HOST_WIDE_INT
3530 AT_int (dw_attr_ref a)
3532 gcc_assert (a && AT_class (a) == dw_val_class_const);
3533 return a->dw_attr_val.v.val_int;
3536 /* Add an unsigned integer attribute value to a DIE. */
3538 static inline void
3539 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3540 unsigned HOST_WIDE_INT unsigned_val)
3542 dw_attr_node attr;
3544 attr.dw_attr = attr_kind;
3545 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3546 attr.dw_attr_val.v.val_unsigned = unsigned_val;
3547 add_dwarf_attr (die, &attr);
3550 static inline unsigned HOST_WIDE_INT
3551 AT_unsigned (dw_attr_ref a)
3553 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3554 return a->dw_attr_val.v.val_unsigned;
3557 /* Add an unsigned double integer attribute value to a DIE. */
3559 static inline void
3560 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3561 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3563 dw_attr_node attr;
3565 attr.dw_attr = attr_kind;
3566 attr.dw_attr_val.val_class = dw_val_class_const_double;
3567 attr.dw_attr_val.v.val_double.high = high;
3568 attr.dw_attr_val.v.val_double.low = low;
3569 add_dwarf_attr (die, &attr);
3572 /* Add a floating point attribute value to a DIE and return it. */
3574 static inline void
3575 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3576 unsigned int length, unsigned int elt_size, unsigned char *array)
3578 dw_attr_node attr;
3580 attr.dw_attr = attr_kind;
3581 attr.dw_attr_val.val_class = dw_val_class_vec;
3582 attr.dw_attr_val.v.val_vec.length = length;
3583 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3584 attr.dw_attr_val.v.val_vec.array = array;
3585 add_dwarf_attr (die, &attr);
3588 /* Add an 8-byte data attribute value to a DIE. */
3590 static inline void
3591 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3592 unsigned char data8[8])
3594 dw_attr_node attr;
3596 attr.dw_attr = attr_kind;
3597 attr.dw_attr_val.val_class = dw_val_class_data8;
3598 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3599 add_dwarf_attr (die, &attr);
3602 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. */
3603 static inline void
3604 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high)
3606 dw_attr_node attr;
3608 attr.dw_attr = DW_AT_low_pc;
3609 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3610 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_low);
3611 add_dwarf_attr (die, &attr);
3613 attr.dw_attr = DW_AT_high_pc;
3614 if (dwarf_version < 4)
3615 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3616 else
3617 attr.dw_attr_val.val_class = dw_val_class_high_pc;
3618 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_high);
3619 add_dwarf_attr (die, &attr);
3622 /* Hash and equality functions for debug_str_hash. */
3624 static hashval_t
3625 debug_str_do_hash (const void *x)
3627 return htab_hash_string (((const struct indirect_string_node *)x)->str);
3630 static int
3631 debug_str_eq (const void *x1, const void *x2)
3633 return strcmp ((((const struct indirect_string_node *)x1)->str),
3634 (const char *)x2) == 0;
3637 /* Add STR to the indirect string hash table. */
3639 static struct indirect_string_node *
3640 find_AT_string (const char *str)
3642 struct indirect_string_node *node;
3643 void **slot;
3645 if (! debug_str_hash)
3646 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
3647 debug_str_eq, NULL);
3649 slot = htab_find_slot_with_hash (debug_str_hash, str,
3650 htab_hash_string (str), INSERT);
3651 if (*slot == NULL)
3653 node = ggc_alloc_cleared_indirect_string_node ();
3654 node->str = ggc_strdup (str);
3655 *slot = node;
3657 else
3658 node = (struct indirect_string_node *) *slot;
3660 node->refcount++;
3661 return node;
3664 /* Add a string attribute value to a DIE. */
3666 static inline void
3667 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
3669 dw_attr_node attr;
3670 struct indirect_string_node *node;
3672 node = find_AT_string (str);
3674 attr.dw_attr = attr_kind;
3675 attr.dw_attr_val.val_class = dw_val_class_str;
3676 attr.dw_attr_val.v.val_str = node;
3677 add_dwarf_attr (die, &attr);
3680 static inline const char *
3681 AT_string (dw_attr_ref a)
3683 gcc_assert (a && AT_class (a) == dw_val_class_str);
3684 return a->dw_attr_val.v.val_str->str;
3687 /* Find out whether a string should be output inline in DIE
3688 or out-of-line in .debug_str section. */
3690 static enum dwarf_form
3691 AT_string_form (dw_attr_ref a)
3693 struct indirect_string_node *node;
3694 unsigned int len;
3695 char label[32];
3697 gcc_assert (a && AT_class (a) == dw_val_class_str);
3699 node = a->dw_attr_val.v.val_str;
3700 if (node->form)
3701 return node->form;
3703 len = strlen (node->str) + 1;
3705 /* If the string is shorter or equal to the size of the reference, it is
3706 always better to put it inline. */
3707 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
3708 return node->form = DW_FORM_string;
3710 /* If we cannot expect the linker to merge strings in .debug_str
3711 section, only put it into .debug_str if it is worth even in this
3712 single module. */
3713 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
3714 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
3715 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
3716 return node->form = DW_FORM_string;
3718 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
3719 ++dw2_string_counter;
3720 node->label = xstrdup (label);
3722 return node->form = DW_FORM_strp;
3725 /* Add a DIE reference attribute value to a DIE. */
3727 static inline void
3728 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
3730 dw_attr_node attr;
3732 #ifdef ENABLE_CHECKING
3733 gcc_assert (targ_die != NULL);
3734 #else
3735 /* With LTO we can end up trying to reference something we didn't create
3736 a DIE for. Avoid crashing later on a NULL referenced DIE. */
3737 if (targ_die == NULL)
3738 return;
3739 #endif
3741 attr.dw_attr = attr_kind;
3742 attr.dw_attr_val.val_class = dw_val_class_die_ref;
3743 attr.dw_attr_val.v.val_die_ref.die = targ_die;
3744 attr.dw_attr_val.v.val_die_ref.external = 0;
3745 add_dwarf_attr (die, &attr);
3748 /* Change DIE reference REF to point to NEW_DIE instead. */
3750 static inline void
3751 change_AT_die_ref (dw_attr_ref ref, dw_die_ref new_die)
3753 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
3754 ref->dw_attr_val.v.val_die_ref.die = new_die;
3755 ref->dw_attr_val.v.val_die_ref.external = 0;
3758 /* Add an AT_specification attribute to a DIE, and also make the back
3759 pointer from the specification to the definition. */
3761 static inline void
3762 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
3764 add_AT_die_ref (die, DW_AT_specification, targ_die);
3765 gcc_assert (!targ_die->die_definition);
3766 targ_die->die_definition = die;
3769 static inline dw_die_ref
3770 AT_ref (dw_attr_ref a)
3772 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
3773 return a->dw_attr_val.v.val_die_ref.die;
3776 static inline int
3777 AT_ref_external (dw_attr_ref a)
3779 if (a && AT_class (a) == dw_val_class_die_ref)
3780 return a->dw_attr_val.v.val_die_ref.external;
3782 return 0;
3785 static inline void
3786 set_AT_ref_external (dw_attr_ref a, int i)
3788 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
3789 a->dw_attr_val.v.val_die_ref.external = i;
3792 /* Add an FDE reference attribute value to a DIE. */
3794 static inline void
3795 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
3797 dw_attr_node attr;
3799 attr.dw_attr = attr_kind;
3800 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
3801 attr.dw_attr_val.v.val_fde_index = targ_fde;
3802 add_dwarf_attr (die, &attr);
3805 /* Add a location description attribute value to a DIE. */
3807 static inline void
3808 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
3810 dw_attr_node attr;
3812 attr.dw_attr = attr_kind;
3813 attr.dw_attr_val.val_class = dw_val_class_loc;
3814 attr.dw_attr_val.v.val_loc = loc;
3815 add_dwarf_attr (die, &attr);
3818 static inline dw_loc_descr_ref
3819 AT_loc (dw_attr_ref a)
3821 gcc_assert (a && AT_class (a) == dw_val_class_loc);
3822 return a->dw_attr_val.v.val_loc;
3825 static inline void
3826 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
3828 dw_attr_node attr;
3830 attr.dw_attr = attr_kind;
3831 attr.dw_attr_val.val_class = dw_val_class_loc_list;
3832 attr.dw_attr_val.v.val_loc_list = loc_list;
3833 add_dwarf_attr (die, &attr);
3834 have_location_lists = true;
3837 static inline dw_loc_list_ref
3838 AT_loc_list (dw_attr_ref a)
3840 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
3841 return a->dw_attr_val.v.val_loc_list;
3844 static inline dw_loc_list_ref *
3845 AT_loc_list_ptr (dw_attr_ref a)
3847 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
3848 return &a->dw_attr_val.v.val_loc_list;
3851 /* Add an address constant attribute value to a DIE. */
3853 static inline void
3854 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
3856 dw_attr_node attr;
3858 attr.dw_attr = attr_kind;
3859 attr.dw_attr_val.val_class = dw_val_class_addr;
3860 attr.dw_attr_val.v.val_addr = addr;
3861 add_dwarf_attr (die, &attr);
3864 /* Get the RTX from to an address DIE attribute. */
3866 static inline rtx
3867 AT_addr (dw_attr_ref a)
3869 gcc_assert (a && AT_class (a) == dw_val_class_addr);
3870 return a->dw_attr_val.v.val_addr;
3873 /* Add a file attribute value to a DIE. */
3875 static inline void
3876 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
3877 struct dwarf_file_data *fd)
3879 dw_attr_node attr;
3881 attr.dw_attr = attr_kind;
3882 attr.dw_attr_val.val_class = dw_val_class_file;
3883 attr.dw_attr_val.v.val_file = fd;
3884 add_dwarf_attr (die, &attr);
3887 /* Get the dwarf_file_data from a file DIE attribute. */
3889 static inline struct dwarf_file_data *
3890 AT_file (dw_attr_ref a)
3892 gcc_assert (a && AT_class (a) == dw_val_class_file);
3893 return a->dw_attr_val.v.val_file;
3896 /* Add a vms delta attribute value to a DIE. */
3898 static inline void
3899 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
3900 const char *lbl1, const char *lbl2)
3902 dw_attr_node attr;
3904 attr.dw_attr = attr_kind;
3905 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
3906 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
3907 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
3908 add_dwarf_attr (die, &attr);
3911 /* Add a label identifier attribute value to a DIE. */
3913 static inline void
3914 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
3916 dw_attr_node attr;
3918 attr.dw_attr = attr_kind;
3919 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3920 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
3921 add_dwarf_attr (die, &attr);
3924 /* Add a section offset attribute value to a DIE, an offset into the
3925 debug_line section. */
3927 static inline void
3928 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
3929 const char *label)
3931 dw_attr_node attr;
3933 attr.dw_attr = attr_kind;
3934 attr.dw_attr_val.val_class = dw_val_class_lineptr;
3935 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
3936 add_dwarf_attr (die, &attr);
3939 /* Add a section offset attribute value to a DIE, an offset into the
3940 debug_macinfo section. */
3942 static inline void
3943 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
3944 const char *label)
3946 dw_attr_node attr;
3948 attr.dw_attr = attr_kind;
3949 attr.dw_attr_val.val_class = dw_val_class_macptr;
3950 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
3951 add_dwarf_attr (die, &attr);
3954 /* Add an offset attribute value to a DIE. */
3956 static inline void
3957 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
3958 unsigned HOST_WIDE_INT offset)
3960 dw_attr_node attr;
3962 attr.dw_attr = attr_kind;
3963 attr.dw_attr_val.val_class = dw_val_class_offset;
3964 attr.dw_attr_val.v.val_offset = offset;
3965 add_dwarf_attr (die, &attr);
3968 /* Add an range_list attribute value to a DIE. */
3970 static void
3971 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
3972 long unsigned int offset)
3974 dw_attr_node attr;
3976 attr.dw_attr = attr_kind;
3977 attr.dw_attr_val.val_class = dw_val_class_range_list;
3978 attr.dw_attr_val.v.val_offset = offset;
3979 add_dwarf_attr (die, &attr);
3982 /* Return the start label of a delta attribute. */
3984 static inline const char *
3985 AT_vms_delta1 (dw_attr_ref a)
3987 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
3988 return a->dw_attr_val.v.val_vms_delta.lbl1;
3991 /* Return the end label of a delta attribute. */
3993 static inline const char *
3994 AT_vms_delta2 (dw_attr_ref a)
3996 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
3997 return a->dw_attr_val.v.val_vms_delta.lbl2;
4000 static inline const char *
4001 AT_lbl (dw_attr_ref a)
4003 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4004 || AT_class (a) == dw_val_class_lineptr
4005 || AT_class (a) == dw_val_class_macptr
4006 || AT_class (a) == dw_val_class_high_pc));
4007 return a->dw_attr_val.v.val_lbl_id;
4010 /* Get the attribute of type attr_kind. */
4012 static dw_attr_ref
4013 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4015 dw_attr_ref a;
4016 unsigned ix;
4017 dw_die_ref spec = NULL;
4019 if (! die)
4020 return NULL;
4022 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
4023 if (a->dw_attr == attr_kind)
4024 return a;
4025 else if (a->dw_attr == DW_AT_specification
4026 || a->dw_attr == DW_AT_abstract_origin)
4027 spec = AT_ref (a);
4029 if (spec)
4030 return get_AT (spec, attr_kind);
4032 return NULL;
4035 /* Returns the parent of the declaration of DIE. */
4037 static dw_die_ref
4038 get_die_parent (dw_die_ref die)
4040 dw_die_ref t;
4042 if (!die)
4043 return NULL;
4045 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4046 || (t = get_AT_ref (die, DW_AT_specification)))
4047 die = t;
4049 return die->die_parent;
4052 /* Return the "low pc" attribute value, typically associated with a subprogram
4053 DIE. Return null if the "low pc" attribute is either not present, or if it
4054 cannot be represented as an assembler label identifier. */
4056 static inline const char *
4057 get_AT_low_pc (dw_die_ref die)
4059 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4061 return a ? AT_lbl (a) : NULL;
4064 /* Return the "high pc" attribute value, typically associated with a subprogram
4065 DIE. Return null if the "high pc" attribute is either not present, or if it
4066 cannot be represented as an assembler label identifier. */
4068 static inline const char *
4069 get_AT_hi_pc (dw_die_ref die)
4071 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4073 return a ? AT_lbl (a) : NULL;
4076 /* Return the value of the string attribute designated by ATTR_KIND, or
4077 NULL if it is not present. */
4079 static inline const char *
4080 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4082 dw_attr_ref a = get_AT (die, attr_kind);
4084 return a ? AT_string (a) : NULL;
4087 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4088 if it is not present. */
4090 static inline int
4091 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4093 dw_attr_ref a = get_AT (die, attr_kind);
4095 return a ? AT_flag (a) : 0;
4098 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4099 if it is not present. */
4101 static inline unsigned
4102 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4104 dw_attr_ref a = get_AT (die, attr_kind);
4106 return a ? AT_unsigned (a) : 0;
4109 static inline dw_die_ref
4110 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4112 dw_attr_ref a = get_AT (die, attr_kind);
4114 return a ? AT_ref (a) : NULL;
4117 static inline struct dwarf_file_data *
4118 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4120 dw_attr_ref a = get_AT (die, attr_kind);
4122 return a ? AT_file (a) : NULL;
4125 /* Return TRUE if the language is C++. */
4127 static inline bool
4128 is_cxx (void)
4130 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4132 return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
4135 /* Return TRUE if the language is Fortran. */
4137 static inline bool
4138 is_fortran (void)
4140 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4142 return (lang == DW_LANG_Fortran77
4143 || lang == DW_LANG_Fortran90
4144 || lang == DW_LANG_Fortran95);
4147 /* Return TRUE if the language is Ada. */
4149 static inline bool
4150 is_ada (void)
4152 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4154 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4157 /* Remove the specified attribute if present. */
4159 static void
4160 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4162 dw_attr_ref a;
4163 unsigned ix;
4165 if (! die)
4166 return;
4168 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
4169 if (a->dw_attr == attr_kind)
4171 if (AT_class (a) == dw_val_class_str)
4172 if (a->dw_attr_val.v.val_str->refcount)
4173 a->dw_attr_val.v.val_str->refcount--;
4175 /* VEC_ordered_remove should help reduce the number of abbrevs
4176 that are needed. */
4177 VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
4178 return;
4182 /* Remove CHILD from its parent. PREV must have the property that
4183 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4185 static void
4186 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4188 gcc_assert (child->die_parent == prev->die_parent);
4189 gcc_assert (prev->die_sib == child);
4190 if (prev == child)
4192 gcc_assert (child->die_parent->die_child == child);
4193 prev = NULL;
4195 else
4196 prev->die_sib = child->die_sib;
4197 if (child->die_parent->die_child == child)
4198 child->die_parent->die_child = prev;
4201 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4202 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4204 static void
4205 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4207 dw_die_ref parent = old_child->die_parent;
4209 gcc_assert (parent == prev->die_parent);
4210 gcc_assert (prev->die_sib == old_child);
4212 new_child->die_parent = parent;
4213 if (prev == old_child)
4215 gcc_assert (parent->die_child == old_child);
4216 new_child->die_sib = new_child;
4218 else
4220 prev->die_sib = new_child;
4221 new_child->die_sib = old_child->die_sib;
4223 if (old_child->die_parent->die_child == old_child)
4224 old_child->die_parent->die_child = new_child;
4227 /* Move all children from OLD_PARENT to NEW_PARENT. */
4229 static void
4230 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4232 dw_die_ref c;
4233 new_parent->die_child = old_parent->die_child;
4234 old_parent->die_child = NULL;
4235 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4238 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4239 matches TAG. */
4241 static void
4242 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4244 dw_die_ref c;
4246 c = die->die_child;
4247 if (c) do {
4248 dw_die_ref prev = c;
4249 c = c->die_sib;
4250 while (c->die_tag == tag)
4252 remove_child_with_prev (c, prev);
4253 /* Might have removed every child. */
4254 if (c == c->die_sib)
4255 return;
4256 c = c->die_sib;
4258 } while (c != die->die_child);
4261 /* Add a CHILD_DIE as the last child of DIE. */
4263 static void
4264 add_child_die (dw_die_ref die, dw_die_ref child_die)
4266 /* FIXME this should probably be an assert. */
4267 if (! die || ! child_die)
4268 return;
4269 gcc_assert (die != child_die);
4271 child_die->die_parent = die;
4272 if (die->die_child)
4274 child_die->die_sib = die->die_child->die_sib;
4275 die->die_child->die_sib = child_die;
4277 else
4278 child_die->die_sib = child_die;
4279 die->die_child = child_die;
4282 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4283 is the specification, to the end of PARENT's list of children.
4284 This is done by removing and re-adding it. */
4286 static void
4287 splice_child_die (dw_die_ref parent, dw_die_ref child)
4289 dw_die_ref p;
4291 /* We want the declaration DIE from inside the class, not the
4292 specification DIE at toplevel. */
4293 if (child->die_parent != parent)
4295 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4297 if (tmp)
4298 child = tmp;
4301 gcc_assert (child->die_parent == parent
4302 || (child->die_parent
4303 == get_AT_ref (parent, DW_AT_specification)));
4305 for (p = child->die_parent->die_child; ; p = p->die_sib)
4306 if (p->die_sib == child)
4308 remove_child_with_prev (child, p);
4309 break;
4312 add_child_die (parent, child);
4315 /* Return a pointer to a newly created DIE node. */
4317 static inline dw_die_ref
4318 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4320 dw_die_ref die = ggc_alloc_cleared_die_node ();
4322 die->die_tag = tag_value;
4324 if (parent_die != NULL)
4325 add_child_die (parent_die, die);
4326 else
4328 limbo_die_node *limbo_node;
4330 limbo_node = ggc_alloc_cleared_limbo_die_node ();
4331 limbo_node->die = die;
4332 limbo_node->created_for = t;
4333 limbo_node->next = limbo_die_list;
4334 limbo_die_list = limbo_node;
4337 return die;
4340 /* Return the DIE associated with the given type specifier. */
4342 static inline dw_die_ref
4343 lookup_type_die (tree type)
4345 return TYPE_SYMTAB_DIE (type);
4348 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4349 anonymous type named by the typedef TYPE_DIE, return the DIE of the
4350 anonymous type instead the one of the naming typedef. */
4352 static inline dw_die_ref
4353 strip_naming_typedef (tree type, dw_die_ref type_die)
4355 if (type
4356 && TREE_CODE (type) == RECORD_TYPE
4357 && type_die
4358 && type_die->die_tag == DW_TAG_typedef
4359 && is_naming_typedef_decl (TYPE_NAME (type)))
4360 type_die = get_AT_ref (type_die, DW_AT_type);
4361 return type_die;
4364 /* Like lookup_type_die, but if type is an anonymous type named by a
4365 typedef[1], return the DIE of the anonymous type instead the one of
4366 the naming typedef. This is because in gen_typedef_die, we did
4367 equate the anonymous struct named by the typedef with the DIE of
4368 the naming typedef. So by default, lookup_type_die on an anonymous
4369 struct yields the DIE of the naming typedef.
4371 [1]: Read the comment of is_naming_typedef_decl to learn about what
4372 a naming typedef is. */
4374 static inline dw_die_ref
4375 lookup_type_die_strip_naming_typedef (tree type)
4377 dw_die_ref die = lookup_type_die (type);
4378 return strip_naming_typedef (type, die);
4381 /* Equate a DIE to a given type specifier. */
4383 static inline void
4384 equate_type_number_to_die (tree type, dw_die_ref type_die)
4386 TYPE_SYMTAB_DIE (type) = type_die;
4389 /* Returns a hash value for X (which really is a die_struct). */
4391 static hashval_t
4392 decl_die_table_hash (const void *x)
4394 return (hashval_t) ((const_dw_die_ref) x)->decl_id;
4397 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
4399 static int
4400 decl_die_table_eq (const void *x, const void *y)
4402 return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
4405 /* Return the DIE associated with a given declaration. */
4407 static inline dw_die_ref
4408 lookup_decl_die (tree decl)
4410 return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
4413 /* Returns a hash value for X (which really is a var_loc_list). */
4415 static hashval_t
4416 decl_loc_table_hash (const void *x)
4418 return (hashval_t) ((const var_loc_list *) x)->decl_id;
4421 /* Return nonzero if decl_id of var_loc_list X is the same as
4422 UID of decl *Y. */
4424 static int
4425 decl_loc_table_eq (const void *x, const void *y)
4427 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
4430 /* Return the var_loc list associated with a given declaration. */
4432 static inline var_loc_list *
4433 lookup_decl_loc (const_tree decl)
4435 if (!decl_loc_table)
4436 return NULL;
4437 return (var_loc_list *)
4438 htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
4441 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
4443 static hashval_t
4444 cached_dw_loc_list_table_hash (const void *x)
4446 return (hashval_t) ((const cached_dw_loc_list *) x)->decl_id;
4449 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
4450 UID of decl *Y. */
4452 static int
4453 cached_dw_loc_list_table_eq (const void *x, const void *y)
4455 return (((const cached_dw_loc_list *) x)->decl_id
4456 == DECL_UID ((const_tree) y));
4459 /* Equate a DIE to a particular declaration. */
4461 static void
4462 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
4464 unsigned int decl_id = DECL_UID (decl);
4465 void **slot;
4467 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
4468 *slot = decl_die;
4469 decl_die->decl_id = decl_id;
4472 /* Return how many bits covers PIECE EXPR_LIST. */
4474 static int
4475 decl_piece_bitsize (rtx piece)
4477 int ret = (int) GET_MODE (piece);
4478 if (ret)
4479 return ret;
4480 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
4481 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
4482 return INTVAL (XEXP (XEXP (piece, 0), 0));
4485 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
4487 static rtx *
4488 decl_piece_varloc_ptr (rtx piece)
4490 if ((int) GET_MODE (piece))
4491 return &XEXP (piece, 0);
4492 else
4493 return &XEXP (XEXP (piece, 0), 1);
4496 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
4497 Next is the chain of following piece nodes. */
4499 static rtx
4500 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
4502 if (bitsize <= (int) MAX_MACHINE_MODE)
4503 return alloc_EXPR_LIST (bitsize, loc_note, next);
4504 else
4505 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
4506 GEN_INT (bitsize),
4507 loc_note), next);
4510 /* Return rtx that should be stored into loc field for
4511 LOC_NOTE and BITPOS/BITSIZE. */
4513 static rtx
4514 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
4515 HOST_WIDE_INT bitsize)
4517 if (bitsize != -1)
4519 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
4520 if (bitpos != 0)
4521 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
4523 return loc_note;
4526 /* This function either modifies location piece list *DEST in
4527 place (if SRC and INNER is NULL), or copies location piece list
4528 *SRC to *DEST while modifying it. Location BITPOS is modified
4529 to contain LOC_NOTE, any pieces overlapping it are removed resp.
4530 not copied and if needed some padding around it is added.
4531 When modifying in place, DEST should point to EXPR_LIST where
4532 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
4533 to the start of the whole list and INNER points to the EXPR_LIST
4534 where earlier pieces cover PIECE_BITPOS bits. */
4536 static void
4537 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
4538 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
4539 HOST_WIDE_INT bitsize, rtx loc_note)
4541 int diff;
4542 bool copy = inner != NULL;
4544 if (copy)
4546 /* First copy all nodes preceding the current bitpos. */
4547 while (src != inner)
4549 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
4550 decl_piece_bitsize (*src), NULL_RTX);
4551 dest = &XEXP (*dest, 1);
4552 src = &XEXP (*src, 1);
4555 /* Add padding if needed. */
4556 if (bitpos != piece_bitpos)
4558 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
4559 copy ? NULL_RTX : *dest);
4560 dest = &XEXP (*dest, 1);
4562 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
4564 gcc_assert (!copy);
4565 /* A piece with correct bitpos and bitsize already exist,
4566 just update the location for it and return. */
4567 *decl_piece_varloc_ptr (*dest) = loc_note;
4568 return;
4570 /* Add the piece that changed. */
4571 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
4572 dest = &XEXP (*dest, 1);
4573 /* Skip over pieces that overlap it. */
4574 diff = bitpos - piece_bitpos + bitsize;
4575 if (!copy)
4576 src = dest;
4577 while (diff > 0 && *src)
4579 rtx piece = *src;
4580 diff -= decl_piece_bitsize (piece);
4581 if (copy)
4582 src = &XEXP (piece, 1);
4583 else
4585 *src = XEXP (piece, 1);
4586 free_EXPR_LIST_node (piece);
4589 /* Add padding if needed. */
4590 if (diff < 0 && *src)
4592 if (!copy)
4593 dest = src;
4594 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
4595 dest = &XEXP (*dest, 1);
4597 if (!copy)
4598 return;
4599 /* Finally copy all nodes following it. */
4600 while (*src)
4602 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
4603 decl_piece_bitsize (*src), NULL_RTX);
4604 dest = &XEXP (*dest, 1);
4605 src = &XEXP (*src, 1);
4609 /* Add a variable location node to the linked list for DECL. */
4611 static struct var_loc_node *
4612 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
4614 unsigned int decl_id;
4615 var_loc_list *temp;
4616 void **slot;
4617 struct var_loc_node *loc = NULL;
4618 HOST_WIDE_INT bitsize = -1, bitpos = -1;
4620 if (DECL_DEBUG_EXPR_IS_FROM (decl))
4622 tree realdecl = DECL_DEBUG_EXPR (decl);
4623 if (realdecl && handled_component_p (realdecl))
4625 HOST_WIDE_INT maxsize;
4626 tree innerdecl;
4627 innerdecl
4628 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
4629 if (!DECL_P (innerdecl)
4630 || DECL_IGNORED_P (innerdecl)
4631 || TREE_STATIC (innerdecl)
4632 || bitsize <= 0
4633 || bitpos + bitsize > 256
4634 || bitsize != maxsize)
4635 return NULL;
4636 decl = innerdecl;
4640 decl_id = DECL_UID (decl);
4641 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
4642 if (*slot == NULL)
4644 temp = ggc_alloc_cleared_var_loc_list ();
4645 temp->decl_id = decl_id;
4646 *slot = temp;
4648 else
4649 temp = (var_loc_list *) *slot;
4651 /* For PARM_DECLs try to keep around the original incoming value,
4652 even if that means we'll emit a zero-range .debug_loc entry. */
4653 if (temp->last
4654 && temp->first == temp->last
4655 && TREE_CODE (decl) == PARM_DECL
4656 && GET_CODE (temp->first->loc) == NOTE
4657 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
4658 && DECL_INCOMING_RTL (decl)
4659 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
4660 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
4661 == GET_CODE (DECL_INCOMING_RTL (decl))
4662 && prev_real_insn (temp->first->loc) == NULL_RTX
4663 && (bitsize != -1
4664 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
4665 NOTE_VAR_LOCATION_LOC (loc_note))
4666 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
4667 != NOTE_VAR_LOCATION_STATUS (loc_note))))
4669 loc = ggc_alloc_cleared_var_loc_node ();
4670 temp->first->next = loc;
4671 temp->last = loc;
4672 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
4674 else if (temp->last)
4676 struct var_loc_node *last = temp->last, *unused = NULL;
4677 rtx *piece_loc = NULL, last_loc_note;
4678 int piece_bitpos = 0;
4679 if (last->next)
4681 last = last->next;
4682 gcc_assert (last->next == NULL);
4684 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
4686 piece_loc = &last->loc;
4689 int cur_bitsize = decl_piece_bitsize (*piece_loc);
4690 if (piece_bitpos + cur_bitsize > bitpos)
4691 break;
4692 piece_bitpos += cur_bitsize;
4693 piece_loc = &XEXP (*piece_loc, 1);
4695 while (*piece_loc);
4697 /* TEMP->LAST here is either pointer to the last but one or
4698 last element in the chained list, LAST is pointer to the
4699 last element. */
4700 if (label && strcmp (last->label, label) == 0)
4702 /* For SRA optimized variables if there weren't any real
4703 insns since last note, just modify the last node. */
4704 if (piece_loc != NULL)
4706 adjust_piece_list (piece_loc, NULL, NULL,
4707 bitpos, piece_bitpos, bitsize, loc_note);
4708 return NULL;
4710 /* If the last note doesn't cover any instructions, remove it. */
4711 if (temp->last != last)
4713 temp->last->next = NULL;
4714 unused = last;
4715 last = temp->last;
4716 gcc_assert (strcmp (last->label, label) != 0);
4718 else
4720 gcc_assert (temp->first == temp->last
4721 || (temp->first->next == temp->last
4722 && TREE_CODE (decl) == PARM_DECL));
4723 memset (temp->last, '\0', sizeof (*temp->last));
4724 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
4725 return temp->last;
4728 if (bitsize == -1 && NOTE_P (last->loc))
4729 last_loc_note = last->loc;
4730 else if (piece_loc != NULL
4731 && *piece_loc != NULL_RTX
4732 && piece_bitpos == bitpos
4733 && decl_piece_bitsize (*piece_loc) == bitsize)
4734 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
4735 else
4736 last_loc_note = NULL_RTX;
4737 /* If the current location is the same as the end of the list,
4738 and either both or neither of the locations is uninitialized,
4739 we have nothing to do. */
4740 if (last_loc_note == NULL_RTX
4741 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
4742 NOTE_VAR_LOCATION_LOC (loc_note)))
4743 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
4744 != NOTE_VAR_LOCATION_STATUS (loc_note))
4745 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
4746 == VAR_INIT_STATUS_UNINITIALIZED)
4747 || (NOTE_VAR_LOCATION_STATUS (loc_note)
4748 == VAR_INIT_STATUS_UNINITIALIZED))))
4750 /* Add LOC to the end of list and update LAST. If the last
4751 element of the list has been removed above, reuse its
4752 memory for the new node, otherwise allocate a new one. */
4753 if (unused)
4755 loc = unused;
4756 memset (loc, '\0', sizeof (*loc));
4758 else
4759 loc = ggc_alloc_cleared_var_loc_node ();
4760 if (bitsize == -1 || piece_loc == NULL)
4761 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
4762 else
4763 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
4764 bitpos, piece_bitpos, bitsize, loc_note);
4765 last->next = loc;
4766 /* Ensure TEMP->LAST will point either to the new last but one
4767 element of the chain, or to the last element in it. */
4768 if (last != temp->last)
4769 temp->last = last;
4771 else if (unused)
4772 ggc_free (unused);
4774 else
4776 loc = ggc_alloc_cleared_var_loc_node ();
4777 temp->first = loc;
4778 temp->last = loc;
4779 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
4781 return loc;
4784 /* Keep track of the number of spaces used to indent the
4785 output of the debugging routines that print the structure of
4786 the DIE internal representation. */
4787 static int print_indent;
4789 /* Indent the line the number of spaces given by print_indent. */
4791 static inline void
4792 print_spaces (FILE *outfile)
4794 fprintf (outfile, "%*s", print_indent, "");
4797 /* Print a type signature in hex. */
4799 static inline void
4800 print_signature (FILE *outfile, char *sig)
4802 int i;
4804 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
4805 fprintf (outfile, "%02x", sig[i] & 0xff);
4808 /* Print the information associated with a given DIE, and its children.
4809 This routine is a debugging aid only. */
4811 static void
4812 print_die (dw_die_ref die, FILE *outfile)
4814 dw_attr_ref a;
4815 dw_die_ref c;
4816 unsigned ix;
4818 print_spaces (outfile);
4819 fprintf (outfile, "DIE %4ld: %s (%p)\n",
4820 die->die_offset, dwarf_tag_name (die->die_tag),
4821 (void*) die);
4822 print_spaces (outfile);
4823 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
4824 fprintf (outfile, " offset: %ld", die->die_offset);
4825 fprintf (outfile, " mark: %d\n", die->die_mark);
4827 if (die->comdat_type_p)
4829 print_spaces (outfile);
4830 fprintf (outfile, " signature: ");
4831 print_signature (outfile, die->die_id.die_type_node->signature);
4832 fprintf (outfile, "\n");
4835 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
4837 print_spaces (outfile);
4838 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
4840 switch (AT_class (a))
4842 case dw_val_class_addr:
4843 fprintf (outfile, "address");
4844 break;
4845 case dw_val_class_offset:
4846 fprintf (outfile, "offset");
4847 break;
4848 case dw_val_class_loc:
4849 fprintf (outfile, "location descriptor");
4850 break;
4851 case dw_val_class_loc_list:
4852 fprintf (outfile, "location list -> label:%s",
4853 AT_loc_list (a)->ll_symbol);
4854 break;
4855 case dw_val_class_range_list:
4856 fprintf (outfile, "range list");
4857 break;
4858 case dw_val_class_const:
4859 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
4860 break;
4861 case dw_val_class_unsigned_const:
4862 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
4863 break;
4864 case dw_val_class_const_double:
4865 fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
4866 HOST_WIDE_INT_PRINT_UNSIGNED")",
4867 a->dw_attr_val.v.val_double.high,
4868 a->dw_attr_val.v.val_double.low);
4869 break;
4870 case dw_val_class_vec:
4871 fprintf (outfile, "floating-point or vector constant");
4872 break;
4873 case dw_val_class_flag:
4874 fprintf (outfile, "%u", AT_flag (a));
4875 break;
4876 case dw_val_class_die_ref:
4877 if (AT_ref (a) != NULL)
4879 if (AT_ref (a)->comdat_type_p)
4881 fprintf (outfile, "die -> signature: ");
4882 print_signature (outfile,
4883 AT_ref (a)->die_id.die_type_node->signature);
4885 else if (AT_ref (a)->die_id.die_symbol)
4886 fprintf (outfile, "die -> label: %s",
4887 AT_ref (a)->die_id.die_symbol);
4888 else
4889 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
4890 fprintf (outfile, " (%p)", (void *) AT_ref (a));
4892 else
4893 fprintf (outfile, "die -> <null>");
4894 break;
4895 case dw_val_class_vms_delta:
4896 fprintf (outfile, "delta: @slotcount(%s-%s)",
4897 AT_vms_delta2 (a), AT_vms_delta1 (a));
4898 break;
4899 case dw_val_class_lbl_id:
4900 case dw_val_class_lineptr:
4901 case dw_val_class_macptr:
4902 case dw_val_class_high_pc:
4903 fprintf (outfile, "label: %s", AT_lbl (a));
4904 break;
4905 case dw_val_class_str:
4906 if (AT_string (a) != NULL)
4907 fprintf (outfile, "\"%s\"", AT_string (a));
4908 else
4909 fprintf (outfile, "<null>");
4910 break;
4911 case dw_val_class_file:
4912 fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
4913 AT_file (a)->emitted_number);
4914 break;
4915 case dw_val_class_data8:
4917 int i;
4919 for (i = 0; i < 8; i++)
4920 fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
4921 break;
4923 default:
4924 break;
4927 fprintf (outfile, "\n");
4930 if (die->die_child != NULL)
4932 print_indent += 4;
4933 FOR_EACH_CHILD (die, c, print_die (c, outfile));
4934 print_indent -= 4;
4936 if (print_indent == 0)
4937 fprintf (outfile, "\n");
4940 /* Print the information collected for a given DIE. */
4942 DEBUG_FUNCTION void
4943 debug_dwarf_die (dw_die_ref die)
4945 print_die (die, stderr);
4948 /* Print all DWARF information collected for the compilation unit.
4949 This routine is a debugging aid only. */
4951 DEBUG_FUNCTION void
4952 debug_dwarf (void)
4954 print_indent = 0;
4955 print_die (comp_unit_die (), stderr);
4958 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
4959 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
4960 DIE that marks the start of the DIEs for this include file. */
4962 static dw_die_ref
4963 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
4965 const char *filename = get_AT_string (bincl_die, DW_AT_name);
4966 dw_die_ref new_unit = gen_compile_unit_die (filename);
4968 new_unit->die_sib = old_unit;
4969 return new_unit;
4972 /* Close an include-file CU and reopen the enclosing one. */
4974 static dw_die_ref
4975 pop_compile_unit (dw_die_ref old_unit)
4977 dw_die_ref new_unit = old_unit->die_sib;
4979 old_unit->die_sib = NULL;
4980 return new_unit;
4983 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
4984 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
4986 /* Calculate the checksum of a location expression. */
4988 static inline void
4989 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
4991 int tem;
4993 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
4994 CHECKSUM (tem);
4995 CHECKSUM (loc->dw_loc_oprnd1);
4996 CHECKSUM (loc->dw_loc_oprnd2);
4999 /* Calculate the checksum of an attribute. */
5001 static void
5002 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5004 dw_loc_descr_ref loc;
5005 rtx r;
5007 CHECKSUM (at->dw_attr);
5009 /* We don't care that this was compiled with a different compiler
5010 snapshot; if the output is the same, that's what matters. */
5011 if (at->dw_attr == DW_AT_producer)
5012 return;
5014 switch (AT_class (at))
5016 case dw_val_class_const:
5017 CHECKSUM (at->dw_attr_val.v.val_int);
5018 break;
5019 case dw_val_class_unsigned_const:
5020 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5021 break;
5022 case dw_val_class_const_double:
5023 CHECKSUM (at->dw_attr_val.v.val_double);
5024 break;
5025 case dw_val_class_vec:
5026 CHECKSUM (at->dw_attr_val.v.val_vec);
5027 break;
5028 case dw_val_class_flag:
5029 CHECKSUM (at->dw_attr_val.v.val_flag);
5030 break;
5031 case dw_val_class_str:
5032 CHECKSUM_STRING (AT_string (at));
5033 break;
5035 case dw_val_class_addr:
5036 r = AT_addr (at);
5037 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5038 CHECKSUM_STRING (XSTR (r, 0));
5039 break;
5041 case dw_val_class_offset:
5042 CHECKSUM (at->dw_attr_val.v.val_offset);
5043 break;
5045 case dw_val_class_loc:
5046 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5047 loc_checksum (loc, ctx);
5048 break;
5050 case dw_val_class_die_ref:
5051 die_checksum (AT_ref (at), ctx, mark);
5052 break;
5054 case dw_val_class_fde_ref:
5055 case dw_val_class_vms_delta:
5056 case dw_val_class_lbl_id:
5057 case dw_val_class_lineptr:
5058 case dw_val_class_macptr:
5059 case dw_val_class_high_pc:
5060 break;
5062 case dw_val_class_file:
5063 CHECKSUM_STRING (AT_file (at)->filename);
5064 break;
5066 case dw_val_class_data8:
5067 CHECKSUM (at->dw_attr_val.v.val_data8);
5068 break;
5070 default:
5071 break;
5075 /* Calculate the checksum of a DIE. */
5077 static void
5078 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5080 dw_die_ref c;
5081 dw_attr_ref a;
5082 unsigned ix;
5084 /* To avoid infinite recursion. */
5085 if (die->die_mark)
5087 CHECKSUM (die->die_mark);
5088 return;
5090 die->die_mark = ++(*mark);
5092 CHECKSUM (die->die_tag);
5094 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
5095 attr_checksum (a, ctx, mark);
5097 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5100 #undef CHECKSUM
5101 #undef CHECKSUM_STRING
5103 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
5104 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5105 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5106 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5107 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5108 #define CHECKSUM_ATTR(FOO) \
5109 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5111 /* Calculate the checksum of a number in signed LEB128 format. */
5113 static void
5114 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5116 unsigned char byte;
5117 bool more;
5119 while (1)
5121 byte = (value & 0x7f);
5122 value >>= 7;
5123 more = !((value == 0 && (byte & 0x40) == 0)
5124 || (value == -1 && (byte & 0x40) != 0));
5125 if (more)
5126 byte |= 0x80;
5127 CHECKSUM (byte);
5128 if (!more)
5129 break;
5133 /* Calculate the checksum of a number in unsigned LEB128 format. */
5135 static void
5136 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5138 while (1)
5140 unsigned char byte = (value & 0x7f);
5141 value >>= 7;
5142 if (value != 0)
5143 /* More bytes to follow. */
5144 byte |= 0x80;
5145 CHECKSUM (byte);
5146 if (value == 0)
5147 break;
5151 /* Checksum the context of the DIE. This adds the names of any
5152 surrounding namespaces or structures to the checksum. */
5154 static void
5155 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5157 const char *name;
5158 dw_die_ref spec;
5159 int tag = die->die_tag;
5161 if (tag != DW_TAG_namespace
5162 && tag != DW_TAG_structure_type
5163 && tag != DW_TAG_class_type)
5164 return;
5166 name = get_AT_string (die, DW_AT_name);
5168 spec = get_AT_ref (die, DW_AT_specification);
5169 if (spec != NULL)
5170 die = spec;
5172 if (die->die_parent != NULL)
5173 checksum_die_context (die->die_parent, ctx);
5175 CHECKSUM_ULEB128 ('C');
5176 CHECKSUM_ULEB128 (tag);
5177 if (name != NULL)
5178 CHECKSUM_STRING (name);
5181 /* Calculate the checksum of a location expression. */
5183 static inline void
5184 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5186 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5187 were emitted as a DW_FORM_sdata instead of a location expression. */
5188 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5190 CHECKSUM_ULEB128 (DW_FORM_sdata);
5191 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5192 return;
5195 /* Otherwise, just checksum the raw location expression. */
5196 while (loc != NULL)
5198 CHECKSUM_ULEB128 (loc->dw_loc_opc);
5199 CHECKSUM (loc->dw_loc_oprnd1);
5200 CHECKSUM (loc->dw_loc_oprnd2);
5201 loc = loc->dw_loc_next;
5205 /* Calculate the checksum of an attribute. */
5207 static void
5208 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5209 struct md5_ctx *ctx, int *mark)
5211 dw_loc_descr_ref loc;
5212 rtx r;
5214 if (AT_class (at) == dw_val_class_die_ref)
5216 dw_die_ref target_die = AT_ref (at);
5218 /* For pointer and reference types, we checksum only the (qualified)
5219 name of the target type (if there is a name). For friend entries,
5220 we checksum only the (qualified) name of the target type or function.
5221 This allows the checksum to remain the same whether the target type
5222 is complete or not. */
5223 if ((at->dw_attr == DW_AT_type
5224 && (tag == DW_TAG_pointer_type
5225 || tag == DW_TAG_reference_type
5226 || tag == DW_TAG_rvalue_reference_type
5227 || tag == DW_TAG_ptr_to_member_type))
5228 || (at->dw_attr == DW_AT_friend
5229 && tag == DW_TAG_friend))
5231 dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5233 if (name_attr != NULL)
5235 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5237 if (decl == NULL)
5238 decl = target_die;
5239 CHECKSUM_ULEB128 ('N');
5240 CHECKSUM_ULEB128 (at->dw_attr);
5241 if (decl->die_parent != NULL)
5242 checksum_die_context (decl->die_parent, ctx);
5243 CHECKSUM_ULEB128 ('E');
5244 CHECKSUM_STRING (AT_string (name_attr));
5245 return;
5249 /* For all other references to another DIE, we check to see if the
5250 target DIE has already been visited. If it has, we emit a
5251 backward reference; if not, we descend recursively. */
5252 if (target_die->die_mark > 0)
5254 CHECKSUM_ULEB128 ('R');
5255 CHECKSUM_ULEB128 (at->dw_attr);
5256 CHECKSUM_ULEB128 (target_die->die_mark);
5258 else
5260 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5262 if (decl == NULL)
5263 decl = target_die;
5264 target_die->die_mark = ++(*mark);
5265 CHECKSUM_ULEB128 ('T');
5266 CHECKSUM_ULEB128 (at->dw_attr);
5267 if (decl->die_parent != NULL)
5268 checksum_die_context (decl->die_parent, ctx);
5269 die_checksum_ordered (target_die, ctx, mark);
5271 return;
5274 CHECKSUM_ULEB128 ('A');
5275 CHECKSUM_ULEB128 (at->dw_attr);
5277 switch (AT_class (at))
5279 case dw_val_class_const:
5280 CHECKSUM_ULEB128 (DW_FORM_sdata);
5281 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
5282 break;
5284 case dw_val_class_unsigned_const:
5285 CHECKSUM_ULEB128 (DW_FORM_sdata);
5286 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
5287 break;
5289 case dw_val_class_const_double:
5290 CHECKSUM_ULEB128 (DW_FORM_block);
5291 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
5292 CHECKSUM (at->dw_attr_val.v.val_double);
5293 break;
5295 case dw_val_class_vec:
5296 CHECKSUM_ULEB128 (DW_FORM_block);
5297 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
5298 CHECKSUM (at->dw_attr_val.v.val_vec);
5299 break;
5301 case dw_val_class_flag:
5302 CHECKSUM_ULEB128 (DW_FORM_flag);
5303 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
5304 break;
5306 case dw_val_class_str:
5307 CHECKSUM_ULEB128 (DW_FORM_string);
5308 CHECKSUM_STRING (AT_string (at));
5309 break;
5311 case dw_val_class_addr:
5312 r = AT_addr (at);
5313 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5314 CHECKSUM_ULEB128 (DW_FORM_string);
5315 CHECKSUM_STRING (XSTR (r, 0));
5316 break;
5318 case dw_val_class_offset:
5319 CHECKSUM_ULEB128 (DW_FORM_sdata);
5320 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
5321 break;
5323 case dw_val_class_loc:
5324 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5325 loc_checksum_ordered (loc, ctx);
5326 break;
5328 case dw_val_class_fde_ref:
5329 case dw_val_class_lbl_id:
5330 case dw_val_class_lineptr:
5331 case dw_val_class_macptr:
5332 case dw_val_class_high_pc:
5333 break;
5335 case dw_val_class_file:
5336 CHECKSUM_ULEB128 (DW_FORM_string);
5337 CHECKSUM_STRING (AT_file (at)->filename);
5338 break;
5340 case dw_val_class_data8:
5341 CHECKSUM (at->dw_attr_val.v.val_data8);
5342 break;
5344 default:
5345 break;
5349 struct checksum_attributes
5351 dw_attr_ref at_name;
5352 dw_attr_ref at_type;
5353 dw_attr_ref at_friend;
5354 dw_attr_ref at_accessibility;
5355 dw_attr_ref at_address_class;
5356 dw_attr_ref at_allocated;
5357 dw_attr_ref at_artificial;
5358 dw_attr_ref at_associated;
5359 dw_attr_ref at_binary_scale;
5360 dw_attr_ref at_bit_offset;
5361 dw_attr_ref at_bit_size;
5362 dw_attr_ref at_bit_stride;
5363 dw_attr_ref at_byte_size;
5364 dw_attr_ref at_byte_stride;
5365 dw_attr_ref at_const_value;
5366 dw_attr_ref at_containing_type;
5367 dw_attr_ref at_count;
5368 dw_attr_ref at_data_location;
5369 dw_attr_ref at_data_member_location;
5370 dw_attr_ref at_decimal_scale;
5371 dw_attr_ref at_decimal_sign;
5372 dw_attr_ref at_default_value;
5373 dw_attr_ref at_digit_count;
5374 dw_attr_ref at_discr;
5375 dw_attr_ref at_discr_list;
5376 dw_attr_ref at_discr_value;
5377 dw_attr_ref at_encoding;
5378 dw_attr_ref at_endianity;
5379 dw_attr_ref at_explicit;
5380 dw_attr_ref at_is_optional;
5381 dw_attr_ref at_location;
5382 dw_attr_ref at_lower_bound;
5383 dw_attr_ref at_mutable;
5384 dw_attr_ref at_ordering;
5385 dw_attr_ref at_picture_string;
5386 dw_attr_ref at_prototyped;
5387 dw_attr_ref at_small;
5388 dw_attr_ref at_segment;
5389 dw_attr_ref at_string_length;
5390 dw_attr_ref at_threads_scaled;
5391 dw_attr_ref at_upper_bound;
5392 dw_attr_ref at_use_location;
5393 dw_attr_ref at_use_UTF8;
5394 dw_attr_ref at_variable_parameter;
5395 dw_attr_ref at_virtuality;
5396 dw_attr_ref at_visibility;
5397 dw_attr_ref at_vtable_elem_location;
5400 /* Collect the attributes that we will want to use for the checksum. */
5402 static void
5403 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
5405 dw_attr_ref a;
5406 unsigned ix;
5408 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
5410 switch (a->dw_attr)
5412 case DW_AT_name:
5413 attrs->at_name = a;
5414 break;
5415 case DW_AT_type:
5416 attrs->at_type = a;
5417 break;
5418 case DW_AT_friend:
5419 attrs->at_friend = a;
5420 break;
5421 case DW_AT_accessibility:
5422 attrs->at_accessibility = a;
5423 break;
5424 case DW_AT_address_class:
5425 attrs->at_address_class = a;
5426 break;
5427 case DW_AT_allocated:
5428 attrs->at_allocated = a;
5429 break;
5430 case DW_AT_artificial:
5431 attrs->at_artificial = a;
5432 break;
5433 case DW_AT_associated:
5434 attrs->at_associated = a;
5435 break;
5436 case DW_AT_binary_scale:
5437 attrs->at_binary_scale = a;
5438 break;
5439 case DW_AT_bit_offset:
5440 attrs->at_bit_offset = a;
5441 break;
5442 case DW_AT_bit_size:
5443 attrs->at_bit_size = a;
5444 break;
5445 case DW_AT_bit_stride:
5446 attrs->at_bit_stride = a;
5447 break;
5448 case DW_AT_byte_size:
5449 attrs->at_byte_size = a;
5450 break;
5451 case DW_AT_byte_stride:
5452 attrs->at_byte_stride = a;
5453 break;
5454 case DW_AT_const_value:
5455 attrs->at_const_value = a;
5456 break;
5457 case DW_AT_containing_type:
5458 attrs->at_containing_type = a;
5459 break;
5460 case DW_AT_count:
5461 attrs->at_count = a;
5462 break;
5463 case DW_AT_data_location:
5464 attrs->at_data_location = a;
5465 break;
5466 case DW_AT_data_member_location:
5467 attrs->at_data_member_location = a;
5468 break;
5469 case DW_AT_decimal_scale:
5470 attrs->at_decimal_scale = a;
5471 break;
5472 case DW_AT_decimal_sign:
5473 attrs->at_decimal_sign = a;
5474 break;
5475 case DW_AT_default_value:
5476 attrs->at_default_value = a;
5477 break;
5478 case DW_AT_digit_count:
5479 attrs->at_digit_count = a;
5480 break;
5481 case DW_AT_discr:
5482 attrs->at_discr = a;
5483 break;
5484 case DW_AT_discr_list:
5485 attrs->at_discr_list = a;
5486 break;
5487 case DW_AT_discr_value:
5488 attrs->at_discr_value = a;
5489 break;
5490 case DW_AT_encoding:
5491 attrs->at_encoding = a;
5492 break;
5493 case DW_AT_endianity:
5494 attrs->at_endianity = a;
5495 break;
5496 case DW_AT_explicit:
5497 attrs->at_explicit = a;
5498 break;
5499 case DW_AT_is_optional:
5500 attrs->at_is_optional = a;
5501 break;
5502 case DW_AT_location:
5503 attrs->at_location = a;
5504 break;
5505 case DW_AT_lower_bound:
5506 attrs->at_lower_bound = a;
5507 break;
5508 case DW_AT_mutable:
5509 attrs->at_mutable = a;
5510 break;
5511 case DW_AT_ordering:
5512 attrs->at_ordering = a;
5513 break;
5514 case DW_AT_picture_string:
5515 attrs->at_picture_string = a;
5516 break;
5517 case DW_AT_prototyped:
5518 attrs->at_prototyped = a;
5519 break;
5520 case DW_AT_small:
5521 attrs->at_small = a;
5522 break;
5523 case DW_AT_segment:
5524 attrs->at_segment = a;
5525 break;
5526 case DW_AT_string_length:
5527 attrs->at_string_length = a;
5528 break;
5529 case DW_AT_threads_scaled:
5530 attrs->at_threads_scaled = a;
5531 break;
5532 case DW_AT_upper_bound:
5533 attrs->at_upper_bound = a;
5534 break;
5535 case DW_AT_use_location:
5536 attrs->at_use_location = a;
5537 break;
5538 case DW_AT_use_UTF8:
5539 attrs->at_use_UTF8 = a;
5540 break;
5541 case DW_AT_variable_parameter:
5542 attrs->at_variable_parameter = a;
5543 break;
5544 case DW_AT_virtuality:
5545 attrs->at_virtuality = a;
5546 break;
5547 case DW_AT_visibility:
5548 attrs->at_visibility = a;
5549 break;
5550 case DW_AT_vtable_elem_location:
5551 attrs->at_vtable_elem_location = a;
5552 break;
5553 default:
5554 break;
5559 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
5561 static void
5562 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5564 dw_die_ref c;
5565 dw_die_ref decl;
5566 struct checksum_attributes attrs;
5568 CHECKSUM_ULEB128 ('D');
5569 CHECKSUM_ULEB128 (die->die_tag);
5571 memset (&attrs, 0, sizeof (attrs));
5573 decl = get_AT_ref (die, DW_AT_specification);
5574 if (decl != NULL)
5575 collect_checksum_attributes (&attrs, decl);
5576 collect_checksum_attributes (&attrs, die);
5578 CHECKSUM_ATTR (attrs.at_name);
5579 CHECKSUM_ATTR (attrs.at_accessibility);
5580 CHECKSUM_ATTR (attrs.at_address_class);
5581 CHECKSUM_ATTR (attrs.at_allocated);
5582 CHECKSUM_ATTR (attrs.at_artificial);
5583 CHECKSUM_ATTR (attrs.at_associated);
5584 CHECKSUM_ATTR (attrs.at_binary_scale);
5585 CHECKSUM_ATTR (attrs.at_bit_offset);
5586 CHECKSUM_ATTR (attrs.at_bit_size);
5587 CHECKSUM_ATTR (attrs.at_bit_stride);
5588 CHECKSUM_ATTR (attrs.at_byte_size);
5589 CHECKSUM_ATTR (attrs.at_byte_stride);
5590 CHECKSUM_ATTR (attrs.at_const_value);
5591 CHECKSUM_ATTR (attrs.at_containing_type);
5592 CHECKSUM_ATTR (attrs.at_count);
5593 CHECKSUM_ATTR (attrs.at_data_location);
5594 CHECKSUM_ATTR (attrs.at_data_member_location);
5595 CHECKSUM_ATTR (attrs.at_decimal_scale);
5596 CHECKSUM_ATTR (attrs.at_decimal_sign);
5597 CHECKSUM_ATTR (attrs.at_default_value);
5598 CHECKSUM_ATTR (attrs.at_digit_count);
5599 CHECKSUM_ATTR (attrs.at_discr);
5600 CHECKSUM_ATTR (attrs.at_discr_list);
5601 CHECKSUM_ATTR (attrs.at_discr_value);
5602 CHECKSUM_ATTR (attrs.at_encoding);
5603 CHECKSUM_ATTR (attrs.at_endianity);
5604 CHECKSUM_ATTR (attrs.at_explicit);
5605 CHECKSUM_ATTR (attrs.at_is_optional);
5606 CHECKSUM_ATTR (attrs.at_location);
5607 CHECKSUM_ATTR (attrs.at_lower_bound);
5608 CHECKSUM_ATTR (attrs.at_mutable);
5609 CHECKSUM_ATTR (attrs.at_ordering);
5610 CHECKSUM_ATTR (attrs.at_picture_string);
5611 CHECKSUM_ATTR (attrs.at_prototyped);
5612 CHECKSUM_ATTR (attrs.at_small);
5613 CHECKSUM_ATTR (attrs.at_segment);
5614 CHECKSUM_ATTR (attrs.at_string_length);
5615 CHECKSUM_ATTR (attrs.at_threads_scaled);
5616 CHECKSUM_ATTR (attrs.at_upper_bound);
5617 CHECKSUM_ATTR (attrs.at_use_location);
5618 CHECKSUM_ATTR (attrs.at_use_UTF8);
5619 CHECKSUM_ATTR (attrs.at_variable_parameter);
5620 CHECKSUM_ATTR (attrs.at_virtuality);
5621 CHECKSUM_ATTR (attrs.at_visibility);
5622 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
5623 CHECKSUM_ATTR (attrs.at_type);
5624 CHECKSUM_ATTR (attrs.at_friend);
5626 /* Checksum the child DIEs, except for nested types and member functions. */
5627 c = die->die_child;
5628 if (c) do {
5629 dw_attr_ref name_attr;
5631 c = c->die_sib;
5632 name_attr = get_AT (c, DW_AT_name);
5633 if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
5634 && name_attr != NULL)
5636 CHECKSUM_ULEB128 ('S');
5637 CHECKSUM_ULEB128 (c->die_tag);
5638 CHECKSUM_STRING (AT_string (name_attr));
5640 else
5642 /* Mark this DIE so it gets processed when unmarking. */
5643 if (c->die_mark == 0)
5644 c->die_mark = -1;
5645 die_checksum_ordered (c, ctx, mark);
5647 } while (c != die->die_child);
5649 CHECKSUM_ULEB128 (0);
5652 #undef CHECKSUM
5653 #undef CHECKSUM_STRING
5654 #undef CHECKSUM_ATTR
5655 #undef CHECKSUM_LEB128
5656 #undef CHECKSUM_ULEB128
5658 /* Generate the type signature for DIE. This is computed by generating an
5659 MD5 checksum over the DIE's tag, its relevant attributes, and its
5660 children. Attributes that are references to other DIEs are processed
5661 by recursion, using the MARK field to prevent infinite recursion.
5662 If the DIE is nested inside a namespace or another type, we also
5663 need to include that context in the signature. The lower 64 bits
5664 of the resulting MD5 checksum comprise the signature. */
5666 static void
5667 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
5669 int mark;
5670 const char *name;
5671 unsigned char checksum[16];
5672 struct md5_ctx ctx;
5673 dw_die_ref decl;
5674 dw_die_ref parent;
5676 name = get_AT_string (die, DW_AT_name);
5677 decl = get_AT_ref (die, DW_AT_specification);
5678 parent = get_die_parent (die);
5680 /* First, compute a signature for just the type name (and its surrounding
5681 context, if any. This is stored in the type unit DIE for link-time
5682 ODR (one-definition rule) checking. */
5684 if (is_cxx() && name != NULL)
5686 md5_init_ctx (&ctx);
5688 /* Checksum the names of surrounding namespaces and structures. */
5689 if (parent != NULL)
5690 checksum_die_context (parent, &ctx);
5692 md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
5693 md5_process_bytes (name, strlen (name) + 1, &ctx);
5694 md5_finish_ctx (&ctx, checksum);
5696 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
5699 /* Next, compute the complete type signature. */
5701 md5_init_ctx (&ctx);
5702 mark = 1;
5703 die->die_mark = mark;
5705 /* Checksum the names of surrounding namespaces and structures. */
5706 if (parent != NULL)
5707 checksum_die_context (parent, &ctx);
5709 /* Checksum the DIE and its children. */
5710 die_checksum_ordered (die, &ctx, &mark);
5711 unmark_all_dies (die);
5712 md5_finish_ctx (&ctx, checksum);
5714 /* Store the signature in the type node and link the type DIE and the
5715 type node together. */
5716 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
5717 DWARF_TYPE_SIGNATURE_SIZE);
5718 die->comdat_type_p = true;
5719 die->die_id.die_type_node = type_node;
5720 type_node->type_die = die;
5722 /* If the DIE is a specification, link its declaration to the type node
5723 as well. */
5724 if (decl != NULL)
5726 decl->comdat_type_p = true;
5727 decl->die_id.die_type_node = type_node;
5731 /* Do the location expressions look same? */
5732 static inline int
5733 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
5735 return loc1->dw_loc_opc == loc2->dw_loc_opc
5736 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
5737 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
5740 /* Do the values look the same? */
5741 static int
5742 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
5744 dw_loc_descr_ref loc1, loc2;
5745 rtx r1, r2;
5747 if (v1->val_class != v2->val_class)
5748 return 0;
5750 switch (v1->val_class)
5752 case dw_val_class_const:
5753 return v1->v.val_int == v2->v.val_int;
5754 case dw_val_class_unsigned_const:
5755 return v1->v.val_unsigned == v2->v.val_unsigned;
5756 case dw_val_class_const_double:
5757 return v1->v.val_double.high == v2->v.val_double.high
5758 && v1->v.val_double.low == v2->v.val_double.low;
5759 case dw_val_class_vec:
5760 if (v1->v.val_vec.length != v2->v.val_vec.length
5761 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
5762 return 0;
5763 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
5764 v1->v.val_vec.length * v1->v.val_vec.elt_size))
5765 return 0;
5766 return 1;
5767 case dw_val_class_flag:
5768 return v1->v.val_flag == v2->v.val_flag;
5769 case dw_val_class_str:
5770 return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
5772 case dw_val_class_addr:
5773 r1 = v1->v.val_addr;
5774 r2 = v2->v.val_addr;
5775 if (GET_CODE (r1) != GET_CODE (r2))
5776 return 0;
5777 return !rtx_equal_p (r1, r2);
5779 case dw_val_class_offset:
5780 return v1->v.val_offset == v2->v.val_offset;
5782 case dw_val_class_loc:
5783 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
5784 loc1 && loc2;
5785 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
5786 if (!same_loc_p (loc1, loc2, mark))
5787 return 0;
5788 return !loc1 && !loc2;
5790 case dw_val_class_die_ref:
5791 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
5793 case dw_val_class_fde_ref:
5794 case dw_val_class_vms_delta:
5795 case dw_val_class_lbl_id:
5796 case dw_val_class_lineptr:
5797 case dw_val_class_macptr:
5798 case dw_val_class_high_pc:
5799 return 1;
5801 case dw_val_class_file:
5802 return v1->v.val_file == v2->v.val_file;
5804 case dw_val_class_data8:
5805 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
5807 default:
5808 return 1;
5812 /* Do the attributes look the same? */
5814 static int
5815 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
5817 if (at1->dw_attr != at2->dw_attr)
5818 return 0;
5820 /* We don't care that this was compiled with a different compiler
5821 snapshot; if the output is the same, that's what matters. */
5822 if (at1->dw_attr == DW_AT_producer)
5823 return 1;
5825 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
5828 /* Do the dies look the same? */
5830 static int
5831 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
5833 dw_die_ref c1, c2;
5834 dw_attr_ref a1;
5835 unsigned ix;
5837 /* To avoid infinite recursion. */
5838 if (die1->die_mark)
5839 return die1->die_mark == die2->die_mark;
5840 die1->die_mark = die2->die_mark = ++(*mark);
5842 if (die1->die_tag != die2->die_tag)
5843 return 0;
5845 if (VEC_length (dw_attr_node, die1->die_attr)
5846 != VEC_length (dw_attr_node, die2->die_attr))
5847 return 0;
5849 FOR_EACH_VEC_ELT (dw_attr_node, die1->die_attr, ix, a1)
5850 if (!same_attr_p (a1, &VEC_index (dw_attr_node, die2->die_attr, ix), mark))
5851 return 0;
5853 c1 = die1->die_child;
5854 c2 = die2->die_child;
5855 if (! c1)
5857 if (c2)
5858 return 0;
5860 else
5861 for (;;)
5863 if (!same_die_p (c1, c2, mark))
5864 return 0;
5865 c1 = c1->die_sib;
5866 c2 = c2->die_sib;
5867 if (c1 == die1->die_child)
5869 if (c2 == die2->die_child)
5870 break;
5871 else
5872 return 0;
5876 return 1;
5879 /* Do the dies look the same? Wrapper around same_die_p. */
5881 static int
5882 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
5884 int mark = 0;
5885 int ret = same_die_p (die1, die2, &mark);
5887 unmark_all_dies (die1);
5888 unmark_all_dies (die2);
5890 return ret;
5893 /* The prefix to attach to symbols on DIEs in the current comdat debug
5894 info section. */
5895 static const char *comdat_symbol_id;
5897 /* The index of the current symbol within the current comdat CU. */
5898 static unsigned int comdat_symbol_number;
5900 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
5901 children, and set comdat_symbol_id accordingly. */
5903 static void
5904 compute_section_prefix (dw_die_ref unit_die)
5906 const char *die_name = get_AT_string (unit_die, DW_AT_name);
5907 const char *base = die_name ? lbasename (die_name) : "anonymous";
5908 char *name = XALLOCAVEC (char, strlen (base) + 64);
5909 char *p;
5910 int i, mark;
5911 unsigned char checksum[16];
5912 struct md5_ctx ctx;
5914 /* Compute the checksum of the DIE, then append part of it as hex digits to
5915 the name filename of the unit. */
5917 md5_init_ctx (&ctx);
5918 mark = 0;
5919 die_checksum (unit_die, &ctx, &mark);
5920 unmark_all_dies (unit_die);
5921 md5_finish_ctx (&ctx, checksum);
5923 sprintf (name, "%s.", base);
5924 clean_symbol_name (name);
5926 p = name + strlen (name);
5927 for (i = 0; i < 4; i++)
5929 sprintf (p, "%.2x", checksum[i]);
5930 p += 2;
5933 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
5934 comdat_symbol_number = 0;
5937 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
5939 static int
5940 is_type_die (dw_die_ref die)
5942 switch (die->die_tag)
5944 case DW_TAG_array_type:
5945 case DW_TAG_class_type:
5946 case DW_TAG_interface_type:
5947 case DW_TAG_enumeration_type:
5948 case DW_TAG_pointer_type:
5949 case DW_TAG_reference_type:
5950 case DW_TAG_rvalue_reference_type:
5951 case DW_TAG_string_type:
5952 case DW_TAG_structure_type:
5953 case DW_TAG_subroutine_type:
5954 case DW_TAG_union_type:
5955 case DW_TAG_ptr_to_member_type:
5956 case DW_TAG_set_type:
5957 case DW_TAG_subrange_type:
5958 case DW_TAG_base_type:
5959 case DW_TAG_const_type:
5960 case DW_TAG_file_type:
5961 case DW_TAG_packed_type:
5962 case DW_TAG_volatile_type:
5963 case DW_TAG_typedef:
5964 return 1;
5965 default:
5966 return 0;
5970 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
5971 Basically, we want to choose the bits that are likely to be shared between
5972 compilations (types) and leave out the bits that are specific to individual
5973 compilations (functions). */
5975 static int
5976 is_comdat_die (dw_die_ref c)
5978 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
5979 we do for stabs. The advantage is a greater likelihood of sharing between
5980 objects that don't include headers in the same order (and therefore would
5981 put the base types in a different comdat). jason 8/28/00 */
5983 if (c->die_tag == DW_TAG_base_type)
5984 return 0;
5986 if (c->die_tag == DW_TAG_pointer_type
5987 || c->die_tag == DW_TAG_reference_type
5988 || c->die_tag == DW_TAG_rvalue_reference_type
5989 || c->die_tag == DW_TAG_const_type
5990 || c->die_tag == DW_TAG_volatile_type)
5992 dw_die_ref t = get_AT_ref (c, DW_AT_type);
5994 return t ? is_comdat_die (t) : 0;
5997 return is_type_die (c);
6000 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6001 compilation unit. */
6003 static int
6004 is_symbol_die (dw_die_ref c)
6006 return (is_type_die (c)
6007 || is_declaration_die (c)
6008 || c->die_tag == DW_TAG_namespace
6009 || c->die_tag == DW_TAG_module);
6012 /* Returns true iff C is a compile-unit DIE. */
6014 static inline bool
6015 is_cu_die (dw_die_ref c)
6017 return c && c->die_tag == DW_TAG_compile_unit;
6020 /* Returns true iff C is a unit DIE of some sort. */
6022 static inline bool
6023 is_unit_die (dw_die_ref c)
6025 return c && (c->die_tag == DW_TAG_compile_unit
6026 || c->die_tag == DW_TAG_partial_unit
6027 || c->die_tag == DW_TAG_type_unit);
6030 /* Returns true iff C is a namespace DIE. */
6032 static inline bool
6033 is_namespace_die (dw_die_ref c)
6035 return c && c->die_tag == DW_TAG_namespace;
6038 /* Returns true iff C is a class or structure DIE. */
6040 static inline bool
6041 is_class_die (dw_die_ref c)
6043 return c && (c->die_tag == DW_TAG_class_type
6044 || c->die_tag == DW_TAG_structure_type);
6047 static char *
6048 gen_internal_sym (const char *prefix)
6050 char buf[256];
6052 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6053 return xstrdup (buf);
6056 /* Assign symbols to all worthy DIEs under DIE. */
6058 static void
6059 assign_symbol_names (dw_die_ref die)
6061 dw_die_ref c;
6063 if (is_symbol_die (die) && !die->comdat_type_p)
6065 if (comdat_symbol_id)
6067 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6069 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6070 comdat_symbol_id, comdat_symbol_number++);
6071 die->die_id.die_symbol = xstrdup (p);
6073 else
6074 die->die_id.die_symbol = gen_internal_sym ("LDIE");
6077 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6080 struct cu_hash_table_entry
6082 dw_die_ref cu;
6083 unsigned min_comdat_num, max_comdat_num;
6084 struct cu_hash_table_entry *next;
6087 /* Routines to manipulate hash table of CUs. */
6088 static hashval_t
6089 htab_cu_hash (const void *of)
6091 const struct cu_hash_table_entry *const entry =
6092 (const struct cu_hash_table_entry *) of;
6094 return htab_hash_string (entry->cu->die_id.die_symbol);
6097 static int
6098 htab_cu_eq (const void *of1, const void *of2)
6100 const struct cu_hash_table_entry *const entry1 =
6101 (const struct cu_hash_table_entry *) of1;
6102 const struct die_struct *const entry2 = (const struct die_struct *) of2;
6104 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6107 static void
6108 htab_cu_del (void *what)
6110 struct cu_hash_table_entry *next,
6111 *entry = (struct cu_hash_table_entry *) what;
6113 while (entry)
6115 next = entry->next;
6116 free (entry);
6117 entry = next;
6121 /* Check whether we have already seen this CU and set up SYM_NUM
6122 accordingly. */
6123 static int
6124 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
6126 struct cu_hash_table_entry dummy;
6127 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6129 dummy.max_comdat_num = 0;
6131 slot = (struct cu_hash_table_entry **)
6132 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
6133 INSERT);
6134 entry = *slot;
6136 for (; entry; last = entry, entry = entry->next)
6138 if (same_die_p_wrap (cu, entry->cu))
6139 break;
6142 if (entry)
6144 *sym_num = entry->min_comdat_num;
6145 return 1;
6148 entry = XCNEW (struct cu_hash_table_entry);
6149 entry->cu = cu;
6150 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6151 entry->next = *slot;
6152 *slot = entry;
6154 return 0;
6157 /* Record SYM_NUM to record of CU in HTABLE. */
6158 static void
6159 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
6161 struct cu_hash_table_entry **slot, *entry;
6163 slot = (struct cu_hash_table_entry **)
6164 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
6165 NO_INSERT);
6166 entry = *slot;
6168 entry->max_comdat_num = sym_num;
6171 /* Traverse the DIE (which is always comp_unit_die), and set up
6172 additional compilation units for each of the include files we see
6173 bracketed by BINCL/EINCL. */
6175 static void
6176 break_out_includes (dw_die_ref die)
6178 dw_die_ref c;
6179 dw_die_ref unit = NULL;
6180 limbo_die_node *node, **pnode;
6181 htab_t cu_hash_table;
6183 c = die->die_child;
6184 if (c) do {
6185 dw_die_ref prev = c;
6186 c = c->die_sib;
6187 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6188 || (unit && is_comdat_die (c)))
6190 dw_die_ref next = c->die_sib;
6192 /* This DIE is for a secondary CU; remove it from the main one. */
6193 remove_child_with_prev (c, prev);
6195 if (c->die_tag == DW_TAG_GNU_BINCL)
6196 unit = push_new_compile_unit (unit, c);
6197 else if (c->die_tag == DW_TAG_GNU_EINCL)
6198 unit = pop_compile_unit (unit);
6199 else
6200 add_child_die (unit, c);
6201 c = next;
6202 if (c == die->die_child)
6203 break;
6205 } while (c != die->die_child);
6207 #if 0
6208 /* We can only use this in debugging, since the frontend doesn't check
6209 to make sure that we leave every include file we enter. */
6210 gcc_assert (!unit);
6211 #endif
6213 assign_symbol_names (die);
6214 cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6215 for (node = limbo_die_list, pnode = &limbo_die_list;
6216 node;
6217 node = node->next)
6219 int is_dupl;
6221 compute_section_prefix (node->die);
6222 is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6223 &comdat_symbol_number);
6224 assign_symbol_names (node->die);
6225 if (is_dupl)
6226 *pnode = node->next;
6227 else
6229 pnode = &node->next;
6230 record_comdat_symbol_number (node->die, cu_hash_table,
6231 comdat_symbol_number);
6234 htab_delete (cu_hash_table);
6237 /* Return non-zero if this DIE is a declaration. */
6239 static int
6240 is_declaration_die (dw_die_ref die)
6242 dw_attr_ref a;
6243 unsigned ix;
6245 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
6246 if (a->dw_attr == DW_AT_declaration)
6247 return 1;
6249 return 0;
6252 /* Return non-zero if this DIE is nested inside a subprogram. */
6254 static int
6255 is_nested_in_subprogram (dw_die_ref die)
6257 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
6259 if (decl == NULL)
6260 decl = die;
6261 return local_scope_p (decl);
6264 /* Return non-zero if this DIE contains a defining declaration of a
6265 subprogram. */
6267 static int
6268 contains_subprogram_definition (dw_die_ref die)
6270 dw_die_ref c;
6272 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
6273 return 1;
6274 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition(c)) return 1);
6275 return 0;
6278 /* Return non-zero if this is a type DIE that should be moved to a
6279 COMDAT .debug_types section. */
6281 static int
6282 should_move_die_to_comdat (dw_die_ref die)
6284 switch (die->die_tag)
6286 case DW_TAG_class_type:
6287 case DW_TAG_structure_type:
6288 case DW_TAG_enumeration_type:
6289 case DW_TAG_union_type:
6290 /* Don't move declarations, inlined instances, or types nested in a
6291 subprogram. */
6292 if (is_declaration_die (die)
6293 || get_AT (die, DW_AT_abstract_origin)
6294 || is_nested_in_subprogram (die))
6295 return 0;
6296 /* A type definition should never contain a subprogram definition. */
6297 gcc_assert (!contains_subprogram_definition (die));
6298 return 1;
6299 case DW_TAG_array_type:
6300 case DW_TAG_interface_type:
6301 case DW_TAG_pointer_type:
6302 case DW_TAG_reference_type:
6303 case DW_TAG_rvalue_reference_type:
6304 case DW_TAG_string_type:
6305 case DW_TAG_subroutine_type:
6306 case DW_TAG_ptr_to_member_type:
6307 case DW_TAG_set_type:
6308 case DW_TAG_subrange_type:
6309 case DW_TAG_base_type:
6310 case DW_TAG_const_type:
6311 case DW_TAG_file_type:
6312 case DW_TAG_packed_type:
6313 case DW_TAG_volatile_type:
6314 case DW_TAG_typedef:
6315 default:
6316 return 0;
6320 /* Make a clone of DIE. */
6322 static dw_die_ref
6323 clone_die (dw_die_ref die)
6325 dw_die_ref clone;
6326 dw_attr_ref a;
6327 unsigned ix;
6329 clone = ggc_alloc_cleared_die_node ();
6330 clone->die_tag = die->die_tag;
6332 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
6333 add_dwarf_attr (clone, a);
6335 return clone;
6338 /* Make a clone of the tree rooted at DIE. */
6340 static dw_die_ref
6341 clone_tree (dw_die_ref die)
6343 dw_die_ref c;
6344 dw_die_ref clone = clone_die (die);
6346 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
6348 return clone;
6351 /* Make a clone of DIE as a declaration. */
6353 static dw_die_ref
6354 clone_as_declaration (dw_die_ref die)
6356 dw_die_ref clone;
6357 dw_die_ref decl;
6358 dw_attr_ref a;
6359 unsigned ix;
6361 /* If the DIE is already a declaration, just clone it. */
6362 if (is_declaration_die (die))
6363 return clone_die (die);
6365 /* If the DIE is a specification, just clone its declaration DIE. */
6366 decl = get_AT_ref (die, DW_AT_specification);
6367 if (decl != NULL)
6369 clone = clone_die (decl);
6370 if (die->comdat_type_p)
6371 add_AT_die_ref (clone, DW_AT_signature, die);
6372 return clone;
6375 clone = ggc_alloc_cleared_die_node ();
6376 clone->die_tag = die->die_tag;
6378 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
6380 /* We don't want to copy over all attributes.
6381 For example we don't want DW_AT_byte_size because otherwise we will no
6382 longer have a declaration and GDB will treat it as a definition. */
6384 switch (a->dw_attr)
6386 case DW_AT_artificial:
6387 case DW_AT_containing_type:
6388 case DW_AT_external:
6389 case DW_AT_name:
6390 case DW_AT_type:
6391 case DW_AT_virtuality:
6392 case DW_AT_linkage_name:
6393 case DW_AT_MIPS_linkage_name:
6394 add_dwarf_attr (clone, a);
6395 break;
6396 case DW_AT_byte_size:
6397 default:
6398 break;
6402 if (die->comdat_type_p)
6403 add_AT_die_ref (clone, DW_AT_signature, die);
6405 add_AT_flag (clone, DW_AT_declaration, 1);
6406 return clone;
6409 /* Copy the declaration context to the new type unit DIE. This includes
6410 any surrounding namespace or type declarations. If the DIE has an
6411 AT_specification attribute, it also includes attributes and children
6412 attached to the specification, and returns a pointer to the original
6413 parent of the declaration DIE. Returns NULL otherwise. */
6415 static dw_die_ref
6416 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
6418 dw_die_ref decl;
6419 dw_die_ref new_decl;
6420 dw_die_ref orig_parent = NULL;
6422 decl = get_AT_ref (die, DW_AT_specification);
6423 if (decl == NULL)
6424 decl = die;
6425 else
6427 unsigned ix;
6428 dw_die_ref c;
6429 dw_attr_ref a;
6431 /* The original DIE will be changed to a declaration, and must
6432 be moved to be a child of the original declaration DIE. */
6433 orig_parent = decl->die_parent;
6435 /* Copy the type node pointer from the new DIE to the original
6436 declaration DIE so we can forward references later. */
6437 decl->comdat_type_p = true;
6438 decl->die_id.die_type_node = die->die_id.die_type_node;
6440 remove_AT (die, DW_AT_specification);
6442 FOR_EACH_VEC_ELT (dw_attr_node, decl->die_attr, ix, a)
6444 if (a->dw_attr != DW_AT_name
6445 && a->dw_attr != DW_AT_declaration
6446 && a->dw_attr != DW_AT_external)
6447 add_dwarf_attr (die, a);
6450 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
6453 if (decl->die_parent != NULL
6454 && !is_unit_die (decl->die_parent))
6456 new_decl = copy_ancestor_tree (unit, decl, NULL);
6457 if (new_decl != NULL)
6459 remove_AT (new_decl, DW_AT_signature);
6460 add_AT_specification (die, new_decl);
6464 return orig_parent;
6467 /* Generate the skeleton ancestor tree for the given NODE, then clone
6468 the DIE and add the clone into the tree. */
6470 static void
6471 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
6473 if (node->new_die != NULL)
6474 return;
6476 node->new_die = clone_as_declaration (node->old_die);
6478 if (node->parent != NULL)
6480 generate_skeleton_ancestor_tree (node->parent);
6481 add_child_die (node->parent->new_die, node->new_die);
6485 /* Generate a skeleton tree of DIEs containing any declarations that are
6486 found in the original tree. We traverse the tree looking for declaration
6487 DIEs, and construct the skeleton from the bottom up whenever we find one. */
6489 static void
6490 generate_skeleton_bottom_up (skeleton_chain_node *parent)
6492 skeleton_chain_node node;
6493 dw_die_ref c;
6494 dw_die_ref first;
6495 dw_die_ref prev = NULL;
6496 dw_die_ref next = NULL;
6498 node.parent = parent;
6500 first = c = parent->old_die->die_child;
6501 if (c)
6502 next = c->die_sib;
6503 if (c) do {
6504 if (prev == NULL || prev->die_sib == c)
6505 prev = c;
6506 c = next;
6507 next = (c == first ? NULL : c->die_sib);
6508 node.old_die = c;
6509 node.new_die = NULL;
6510 if (is_declaration_die (c))
6512 /* Clone the existing DIE, move the original to the skeleton
6513 tree (which is in the main CU), and put the clone, with
6514 all the original's children, where the original came from. */
6515 dw_die_ref clone = clone_die (c);
6516 move_all_children (c, clone);
6518 replace_child (c, clone, prev);
6519 generate_skeleton_ancestor_tree (parent);
6520 add_child_die (parent->new_die, c);
6521 node.new_die = c;
6522 c = clone;
6524 generate_skeleton_bottom_up (&node);
6525 } while (next != NULL);
6528 /* Wrapper function for generate_skeleton_bottom_up. */
6530 static dw_die_ref
6531 generate_skeleton (dw_die_ref die)
6533 skeleton_chain_node node;
6535 node.old_die = die;
6536 node.new_die = NULL;
6537 node.parent = NULL;
6539 /* If this type definition is nested inside another type,
6540 always leave at least a declaration in its place. */
6541 if (die->die_parent != NULL && is_type_die (die->die_parent))
6542 node.new_die = clone_as_declaration (die);
6544 generate_skeleton_bottom_up (&node);
6545 return node.new_die;
6548 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
6549 declaration. The original DIE is moved to a new compile unit so that
6550 existing references to it follow it to the new location. If any of the
6551 original DIE's descendants is a declaration, we need to replace the
6552 original DIE with a skeleton tree and move the declarations back into the
6553 skeleton tree. */
6555 static dw_die_ref
6556 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
6557 dw_die_ref prev)
6559 dw_die_ref skeleton, orig_parent;
6561 /* Copy the declaration context to the type unit DIE. If the returned
6562 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
6563 that DIE. */
6564 orig_parent = copy_declaration_context (unit, child);
6566 skeleton = generate_skeleton (child);
6567 if (skeleton == NULL)
6568 remove_child_with_prev (child, prev);
6569 else
6571 skeleton->comdat_type_p = true;
6572 skeleton->die_id.die_type_node = child->die_id.die_type_node;
6574 /* If the original DIE was a specification, we need to put
6575 the skeleton under the parent DIE of the declaration.
6576 This leaves the original declaration in the tree, but
6577 it will be pruned later since there are no longer any
6578 references to it. */
6579 if (orig_parent != NULL)
6581 remove_child_with_prev (child, prev);
6582 add_child_die (orig_parent, skeleton);
6584 else
6585 replace_child (child, skeleton, prev);
6588 return skeleton;
6591 /* Traverse the DIE and set up additional .debug_types sections for each
6592 type worthy of being placed in a COMDAT section. */
6594 static void
6595 break_out_comdat_types (dw_die_ref die)
6597 dw_die_ref c;
6598 dw_die_ref first;
6599 dw_die_ref prev = NULL;
6600 dw_die_ref next = NULL;
6601 dw_die_ref unit = NULL;
6603 first = c = die->die_child;
6604 if (c)
6605 next = c->die_sib;
6606 if (c) do {
6607 if (prev == NULL || prev->die_sib == c)
6608 prev = c;
6609 c = next;
6610 next = (c == first ? NULL : c->die_sib);
6611 if (should_move_die_to_comdat (c))
6613 dw_die_ref replacement;
6614 comdat_type_node_ref type_node;
6616 /* Create a new type unit DIE as the root for the new tree, and
6617 add it to the list of comdat types. */
6618 unit = new_die (DW_TAG_type_unit, NULL, NULL);
6619 add_AT_unsigned (unit, DW_AT_language,
6620 get_AT_unsigned (comp_unit_die (), DW_AT_language));
6621 type_node = ggc_alloc_cleared_comdat_type_node ();
6622 type_node->root_die = unit;
6623 type_node->next = comdat_type_list;
6624 comdat_type_list = type_node;
6626 /* Generate the type signature. */
6627 generate_type_signature (c, type_node);
6629 /* Copy the declaration context, attributes, and children of the
6630 declaration into the new type unit DIE, then remove this DIE
6631 from the main CU (or replace it with a skeleton if necessary). */
6632 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
6633 type_node->skeleton_die = replacement;
6635 /* Break out nested types into their own type units. */
6636 break_out_comdat_types (c);
6638 /* Add the DIE to the new compunit. */
6639 add_child_die (unit, c);
6641 if (replacement != NULL)
6642 c = replacement;
6644 else if (c->die_tag == DW_TAG_namespace
6645 || c->die_tag == DW_TAG_class_type
6646 || c->die_tag == DW_TAG_structure_type
6647 || c->die_tag == DW_TAG_union_type)
6649 /* Look for nested types that can be broken out. */
6650 break_out_comdat_types (c);
6652 } while (next != NULL);
6655 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
6657 struct decl_table_entry
6659 dw_die_ref orig;
6660 dw_die_ref copy;
6663 /* Routines to manipulate hash table of copied declarations. */
6665 static hashval_t
6666 htab_decl_hash (const void *of)
6668 const struct decl_table_entry *const entry =
6669 (const struct decl_table_entry *) of;
6671 return htab_hash_pointer (entry->orig);
6674 static int
6675 htab_decl_eq (const void *of1, const void *of2)
6677 const struct decl_table_entry *const entry1 =
6678 (const struct decl_table_entry *) of1;
6679 const struct die_struct *const entry2 = (const struct die_struct *) of2;
6681 return entry1->orig == entry2;
6684 static void
6685 htab_decl_del (void *what)
6687 struct decl_table_entry *entry = (struct decl_table_entry *) what;
6689 free (entry);
6692 /* Copy DIE and its ancestors, up to, but not including, the compile unit
6693 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
6694 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
6695 to check if the ancestor has already been copied into UNIT. */
6697 static dw_die_ref
6698 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
6700 dw_die_ref parent = die->die_parent;
6701 dw_die_ref new_parent = unit;
6702 dw_die_ref copy;
6703 void **slot = NULL;
6704 struct decl_table_entry *entry = NULL;
6706 if (decl_table)
6708 /* Check if the entry has already been copied to UNIT. */
6709 slot = htab_find_slot_with_hash (decl_table, die,
6710 htab_hash_pointer (die), INSERT);
6711 if (*slot != HTAB_EMPTY_ENTRY)
6713 entry = (struct decl_table_entry *) *slot;
6714 return entry->copy;
6717 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
6718 entry = XCNEW (struct decl_table_entry);
6719 entry->orig = die;
6720 entry->copy = NULL;
6721 *slot = entry;
6724 if (parent != NULL)
6726 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
6727 if (spec != NULL)
6728 parent = spec;
6729 if (!is_unit_die (parent))
6730 new_parent = copy_ancestor_tree (unit, parent, decl_table);
6733 copy = clone_as_declaration (die);
6734 add_child_die (new_parent, copy);
6736 if (decl_table != NULL)
6738 /* Record the pointer to the copy. */
6739 entry->copy = copy;
6742 return copy;
6745 /* Like clone_tree, but additionally enter all the children into
6746 the hash table decl_table. */
6748 static dw_die_ref
6749 clone_tree_hash (dw_die_ref die, htab_t decl_table)
6751 dw_die_ref c;
6752 dw_die_ref clone = clone_die (die);
6753 struct decl_table_entry *entry;
6754 void **slot = htab_find_slot_with_hash (decl_table, die,
6755 htab_hash_pointer (die), INSERT);
6756 /* Assert that DIE isn't in the hash table yet. If it would be there
6757 before, the ancestors would be necessarily there as well, therefore
6758 clone_tree_hash wouldn't be called. */
6759 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
6760 entry = XCNEW (struct decl_table_entry);
6761 entry->orig = die;
6762 entry->copy = clone;
6763 *slot = entry;
6765 FOR_EACH_CHILD (die, c,
6766 add_child_die (clone, clone_tree_hash (c, decl_table)));
6768 return clone;
6771 /* Walk the DIE and its children, looking for references to incomplete
6772 or trivial types that are unmarked (i.e., that are not in the current
6773 type_unit). */
6775 static void
6776 copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
6778 dw_die_ref c;
6779 dw_attr_ref a;
6780 unsigned ix;
6782 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
6784 if (AT_class (a) == dw_val_class_die_ref)
6786 dw_die_ref targ = AT_ref (a);
6787 void **slot;
6788 struct decl_table_entry *entry;
6790 if (targ->die_mark != 0 || targ->comdat_type_p)
6791 continue;
6793 slot = htab_find_slot_with_hash (decl_table, targ,
6794 htab_hash_pointer (targ), INSERT);
6796 if (*slot != HTAB_EMPTY_ENTRY)
6798 /* TARG has already been copied, so we just need to
6799 modify the reference to point to the copy. */
6800 entry = (struct decl_table_entry *) *slot;
6801 a->dw_attr_val.v.val_die_ref.die = entry->copy;
6803 else
6805 dw_die_ref parent = unit;
6806 dw_die_ref copy = clone_die (targ);
6808 /* Record in DECL_TABLE that TARG has been copied.
6809 Need to do this now, before the recursive call,
6810 because DECL_TABLE may be expanded and SLOT
6811 would no longer be a valid pointer. */
6812 entry = XCNEW (struct decl_table_entry);
6813 entry->orig = targ;
6814 entry->copy = copy;
6815 *slot = entry;
6817 FOR_EACH_CHILD (targ, c,
6818 add_child_die (copy,
6819 clone_tree_hash (c, decl_table)));
6821 /* Make sure the cloned tree is marked as part of the
6822 type unit. */
6823 mark_dies (copy);
6825 /* If TARG has surrounding context, copy its ancestor tree
6826 into the new type unit. */
6827 if (targ->die_parent != NULL
6828 && !is_unit_die (targ->die_parent))
6829 parent = copy_ancestor_tree (unit, targ->die_parent,
6830 decl_table);
6832 add_child_die (parent, copy);
6833 a->dw_attr_val.v.val_die_ref.die = copy;
6835 /* Make sure the newly-copied DIE is walked. If it was
6836 installed in a previously-added context, it won't
6837 get visited otherwise. */
6838 if (parent != unit)
6840 /* Find the highest point of the newly-added tree,
6841 mark each node along the way, and walk from there. */
6842 parent->die_mark = 1;
6843 while (parent->die_parent
6844 && parent->die_parent->die_mark == 0)
6846 parent = parent->die_parent;
6847 parent->die_mark = 1;
6849 copy_decls_walk (unit, parent, decl_table);
6855 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
6858 /* Copy declarations for "unworthy" types into the new comdat section.
6859 Incomplete types, modified types, and certain other types aren't broken
6860 out into comdat sections of their own, so they don't have a signature,
6861 and we need to copy the declaration into the same section so that we
6862 don't have an external reference. */
6864 static void
6865 copy_decls_for_unworthy_types (dw_die_ref unit)
6867 htab_t decl_table;
6869 mark_dies (unit);
6870 decl_table = htab_create (10, htab_decl_hash, htab_decl_eq, htab_decl_del);
6871 copy_decls_walk (unit, unit, decl_table);
6872 htab_delete (decl_table);
6873 unmark_dies (unit);
6876 /* Traverse the DIE and add a sibling attribute if it may have the
6877 effect of speeding up access to siblings. To save some space,
6878 avoid generating sibling attributes for DIE's without children. */
6880 static void
6881 add_sibling_attributes (dw_die_ref die)
6883 dw_die_ref c;
6885 if (! die->die_child)
6886 return;
6888 if (die->die_parent && die != die->die_parent->die_child)
6889 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
6891 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
6894 /* Output all location lists for the DIE and its children. */
6896 static void
6897 output_location_lists (dw_die_ref die)
6899 dw_die_ref c;
6900 dw_attr_ref a;
6901 unsigned ix;
6903 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
6904 if (AT_class (a) == dw_val_class_loc_list)
6905 output_loc_list (AT_loc_list (a));
6907 FOR_EACH_CHILD (die, c, output_location_lists (c));
6910 /* We want to limit the number of external references, because they are
6911 larger than local references: a relocation takes multiple words, and
6912 even a sig8 reference is always eight bytes, whereas a local reference
6913 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
6914 So if we encounter multiple external references to the same type DIE, we
6915 make a local typedef stub for it and redirect all references there.
6917 This is the element of the hash table for keeping track of these
6918 references. */
6920 struct external_ref
6922 dw_die_ref type;
6923 dw_die_ref stub;
6924 unsigned n_refs;
6927 /* Hash an external_ref. */
6929 static hashval_t
6930 hash_external_ref (const void *p)
6932 const struct external_ref *r = (const struct external_ref *)p;
6933 return htab_hash_pointer (r->type);
6936 /* Compare external_refs. */
6938 static int
6939 external_ref_eq (const void *p1, const void *p2)
6941 const struct external_ref *r1 = (const struct external_ref *)p1;
6942 const struct external_ref *r2 = (const struct external_ref *)p2;
6943 return r1->type == r2->type;
6946 /* Return a pointer to the external_ref for references to DIE. */
6948 static struct external_ref *
6949 lookup_external_ref (htab_t map, dw_die_ref die)
6951 struct external_ref ref, *ref_p;
6952 void ** slot;
6954 ref.type = die;
6955 slot = htab_find_slot (map, &ref, INSERT);
6956 if (*slot != HTAB_EMPTY_ENTRY)
6957 return (struct external_ref *) *slot;
6959 ref_p = XCNEW (struct external_ref);
6960 ref_p->type = die;
6961 *slot = ref_p;
6962 return ref_p;
6965 /* Subroutine of optimize_external_refs, below.
6967 If we see a type skeleton, record it as our stub. If we see external
6968 references, remember how many we've seen. */
6970 static void
6971 optimize_external_refs_1 (dw_die_ref die, htab_t map)
6973 dw_die_ref c;
6974 dw_attr_ref a;
6975 unsigned ix;
6976 struct external_ref *ref_p;
6978 if (is_type_die (die)
6979 && (c = get_AT_ref (die, DW_AT_signature)))
6981 /* This is a local skeleton; use it for local references. */
6982 ref_p = lookup_external_ref (map, c);
6983 ref_p->stub = die;
6986 /* Scan the DIE references, and remember any that refer to DIEs from
6987 other CUs (i.e. those which are not marked). */
6988 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
6989 if (AT_class (a) == dw_val_class_die_ref
6990 && (c = AT_ref (a))->die_mark == 0
6991 && is_type_die (c))
6993 ref_p = lookup_external_ref (map, c);
6994 ref_p->n_refs++;
6997 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7000 /* htab_traverse callback function for optimize_external_refs, below. SLOT
7001 points to an external_ref, DATA is the CU we're processing. If we don't
7002 already have a local stub, and we have multiple refs, build a stub. */
7004 static int
7005 build_local_stub (void **slot, void *data)
7007 struct external_ref *ref_p = (struct external_ref *)*slot;
7009 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7011 /* We have multiple references to this type, so build a small stub.
7012 Both of these forms are a bit dodgy from the perspective of the
7013 DWARF standard, since technically they should have names. */
7014 dw_die_ref cu = (dw_die_ref) data;
7015 dw_die_ref type = ref_p->type;
7016 dw_die_ref stub = NULL;
7018 if (type->comdat_type_p)
7020 /* If we refer to this type via sig8, use AT_signature. */
7021 stub = new_die (type->die_tag, cu, NULL_TREE);
7022 add_AT_die_ref (stub, DW_AT_signature, type);
7024 else
7026 /* Otherwise, use a typedef with no name. */
7027 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7028 add_AT_die_ref (stub, DW_AT_type, type);
7031 stub->die_mark++;
7032 ref_p->stub = stub;
7034 return 1;
7037 /* DIE is a unit; look through all the DIE references to see if there are
7038 any external references to types, and if so, create local stubs for
7039 them which will be applied in build_abbrev_table. This is useful because
7040 references to local DIEs are smaller. */
7042 static htab_t
7043 optimize_external_refs (dw_die_ref die)
7045 htab_t map = htab_create (10, hash_external_ref, external_ref_eq, free);
7046 optimize_external_refs_1 (die, map);
7047 htab_traverse (map, build_local_stub, die);
7048 return map;
7051 /* The format of each DIE (and its attribute value pairs) is encoded in an
7052 abbreviation table. This routine builds the abbreviation table and assigns
7053 a unique abbreviation id for each abbreviation entry. The children of each
7054 die are visited recursively. */
7056 static void
7057 build_abbrev_table (dw_die_ref die, htab_t extern_map)
7059 unsigned long abbrev_id;
7060 unsigned int n_alloc;
7061 dw_die_ref c;
7062 dw_attr_ref a;
7063 unsigned ix;
7065 /* Scan the DIE references, and replace any that refer to
7066 DIEs from other CUs (i.e. those which are not marked) with
7067 the local stubs we built in optimize_external_refs. */
7068 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7069 if (AT_class (a) == dw_val_class_die_ref
7070 && (c = AT_ref (a))->die_mark == 0)
7072 struct external_ref *ref_p;
7073 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
7075 ref_p = lookup_external_ref (extern_map, c);
7076 if (ref_p->stub && ref_p->stub != die)
7077 change_AT_die_ref (a, ref_p->stub);
7078 else
7079 /* We aren't changing this reference, so mark it external. */
7080 set_AT_ref_external (a, 1);
7083 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7085 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7086 dw_attr_ref die_a, abbrev_a;
7087 unsigned ix;
7088 bool ok = true;
7090 if (abbrev->die_tag != die->die_tag)
7091 continue;
7092 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7093 continue;
7095 if (VEC_length (dw_attr_node, abbrev->die_attr)
7096 != VEC_length (dw_attr_node, die->die_attr))
7097 continue;
7099 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, die_a)
7101 abbrev_a = &VEC_index (dw_attr_node, abbrev->die_attr, ix);
7102 if ((abbrev_a->dw_attr != die_a->dw_attr)
7103 || (value_format (abbrev_a) != value_format (die_a)))
7105 ok = false;
7106 break;
7109 if (ok)
7110 break;
7113 if (abbrev_id >= abbrev_die_table_in_use)
7115 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7117 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7118 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7119 n_alloc);
7121 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7122 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7123 abbrev_die_table_allocated = n_alloc;
7126 ++abbrev_die_table_in_use;
7127 abbrev_die_table[abbrev_id] = die;
7130 die->die_abbrev = abbrev_id;
7131 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
7134 /* Return the power-of-two number of bytes necessary to represent VALUE. */
7136 static int
7137 constant_size (unsigned HOST_WIDE_INT value)
7139 int log;
7141 if (value == 0)
7142 log = 0;
7143 else
7144 log = floor_log2 (value);
7146 log = log / 8;
7147 log = 1 << (floor_log2 (log) + 1);
7149 return log;
7152 /* Return the size of a DIE as it is represented in the
7153 .debug_info section. */
7155 static unsigned long
7156 size_of_die (dw_die_ref die)
7158 unsigned long size = 0;
7159 dw_attr_ref a;
7160 unsigned ix;
7162 size += size_of_uleb128 (die->die_abbrev);
7163 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7165 switch (AT_class (a))
7167 case dw_val_class_addr:
7168 size += DWARF2_ADDR_SIZE;
7169 break;
7170 case dw_val_class_offset:
7171 size += DWARF_OFFSET_SIZE;
7172 break;
7173 case dw_val_class_loc:
7175 unsigned long lsize = size_of_locs (AT_loc (a));
7177 /* Block length. */
7178 if (dwarf_version >= 4)
7179 size += size_of_uleb128 (lsize);
7180 else
7181 size += constant_size (lsize);
7182 size += lsize;
7184 break;
7185 case dw_val_class_loc_list:
7186 size += DWARF_OFFSET_SIZE;
7187 break;
7188 case dw_val_class_range_list:
7189 size += DWARF_OFFSET_SIZE;
7190 break;
7191 case dw_val_class_const:
7192 size += size_of_sleb128 (AT_int (a));
7193 break;
7194 case dw_val_class_unsigned_const:
7196 int csize = constant_size (AT_unsigned (a));
7197 if (dwarf_version == 3
7198 && a->dw_attr == DW_AT_data_member_location
7199 && csize >= 4)
7200 size += size_of_uleb128 (AT_unsigned (a));
7201 else
7202 size += csize;
7204 break;
7205 case dw_val_class_const_double:
7206 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
7207 if (HOST_BITS_PER_WIDE_INT >= 64)
7208 size++; /* block */
7209 break;
7210 case dw_val_class_vec:
7211 size += constant_size (a->dw_attr_val.v.val_vec.length
7212 * a->dw_attr_val.v.val_vec.elt_size)
7213 + a->dw_attr_val.v.val_vec.length
7214 * a->dw_attr_val.v.val_vec.elt_size; /* block */
7215 break;
7216 case dw_val_class_flag:
7217 if (dwarf_version >= 4)
7218 /* Currently all add_AT_flag calls pass in 1 as last argument,
7219 so DW_FORM_flag_present can be used. If that ever changes,
7220 we'll need to use DW_FORM_flag and have some optimization
7221 in build_abbrev_table that will change those to
7222 DW_FORM_flag_present if it is set to 1 in all DIEs using
7223 the same abbrev entry. */
7224 gcc_assert (a->dw_attr_val.v.val_flag == 1);
7225 else
7226 size += 1;
7227 break;
7228 case dw_val_class_die_ref:
7229 if (AT_ref_external (a))
7231 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
7232 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
7233 is sized by target address length, whereas in DWARF3
7234 it's always sized as an offset. */
7235 if (use_debug_types)
7236 size += DWARF_TYPE_SIGNATURE_SIZE;
7237 else if (dwarf_version == 2)
7238 size += DWARF2_ADDR_SIZE;
7239 else
7240 size += DWARF_OFFSET_SIZE;
7242 else
7243 size += DWARF_OFFSET_SIZE;
7244 break;
7245 case dw_val_class_fde_ref:
7246 size += DWARF_OFFSET_SIZE;
7247 break;
7248 case dw_val_class_lbl_id:
7249 size += DWARF2_ADDR_SIZE;
7250 break;
7251 case dw_val_class_lineptr:
7252 case dw_val_class_macptr:
7253 size += DWARF_OFFSET_SIZE;
7254 break;
7255 case dw_val_class_str:
7256 if (AT_string_form (a) == DW_FORM_strp)
7257 size += DWARF_OFFSET_SIZE;
7258 else
7259 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7260 break;
7261 case dw_val_class_file:
7262 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7263 break;
7264 case dw_val_class_data8:
7265 size += 8;
7266 break;
7267 case dw_val_class_vms_delta:
7268 size += DWARF_OFFSET_SIZE;
7269 break;
7270 case dw_val_class_high_pc:
7271 size += DWARF2_ADDR_SIZE;
7272 break;
7273 default:
7274 gcc_unreachable ();
7278 return size;
7281 /* Size the debugging information associated with a given DIE. Visits the
7282 DIE's children recursively. Updates the global variable next_die_offset, on
7283 each time through. Uses the current value of next_die_offset to update the
7284 die_offset field in each DIE. */
7286 static void
7287 calc_die_sizes (dw_die_ref die)
7289 dw_die_ref c;
7291 gcc_assert (die->die_offset == 0
7292 || (unsigned long int) die->die_offset == next_die_offset);
7293 die->die_offset = next_die_offset;
7294 next_die_offset += size_of_die (die);
7296 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
7298 if (die->die_child != NULL)
7299 /* Count the null byte used to terminate sibling lists. */
7300 next_die_offset += 1;
7303 /* Size just the base type children at the start of the CU.
7304 This is needed because build_abbrev needs to size locs
7305 and sizing of type based stack ops needs to know die_offset
7306 values for the base types. */
7308 static void
7309 calc_base_type_die_sizes (void)
7311 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7312 unsigned int i;
7313 dw_die_ref base_type;
7314 #if ENABLE_ASSERT_CHECKING
7315 dw_die_ref prev = comp_unit_die ()->die_child;
7316 #endif
7318 die_offset += size_of_die (comp_unit_die ());
7319 for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
7321 #if ENABLE_ASSERT_CHECKING
7322 gcc_assert (base_type->die_offset == 0
7323 && prev->die_sib == base_type
7324 && base_type->die_child == NULL
7325 && base_type->die_abbrev);
7326 prev = base_type;
7327 #endif
7328 base_type->die_offset = die_offset;
7329 die_offset += size_of_die (base_type);
7333 /* Set the marks for a die and its children. We do this so
7334 that we know whether or not a reference needs to use FORM_ref_addr; only
7335 DIEs in the same CU will be marked. We used to clear out the offset
7336 and use that as the flag, but ran into ordering problems. */
7338 static void
7339 mark_dies (dw_die_ref die)
7341 dw_die_ref c;
7343 gcc_assert (!die->die_mark);
7345 die->die_mark = 1;
7346 FOR_EACH_CHILD (die, c, mark_dies (c));
7349 /* Clear the marks for a die and its children. */
7351 static void
7352 unmark_dies (dw_die_ref die)
7354 dw_die_ref c;
7356 if (! use_debug_types)
7357 gcc_assert (die->die_mark);
7359 die->die_mark = 0;
7360 FOR_EACH_CHILD (die, c, unmark_dies (c));
7363 /* Clear the marks for a die, its children and referred dies. */
7365 static void
7366 unmark_all_dies (dw_die_ref die)
7368 dw_die_ref c;
7369 dw_attr_ref a;
7370 unsigned ix;
7372 if (!die->die_mark)
7373 return;
7374 die->die_mark = 0;
7376 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
7378 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7379 if (AT_class (a) == dw_val_class_die_ref)
7380 unmark_all_dies (AT_ref (a));
7383 /* Return the size of the .debug_pubnames or .debug_pubtypes table
7384 generated for the compilation unit. */
7386 static unsigned long
7387 size_of_pubnames (VEC (pubname_entry, gc) * names)
7389 unsigned long size;
7390 unsigned i;
7391 pubname_ref p;
7393 size = DWARF_PUBNAMES_HEADER_SIZE;
7394 FOR_EACH_VEC_ELT (pubname_entry, names, i, p)
7395 if (names != pubtype_table
7396 || p->die->die_offset != 0
7397 || !flag_eliminate_unused_debug_types)
7398 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
7400 size += DWARF_OFFSET_SIZE;
7401 return size;
7404 /* Return the size of the information in the .debug_aranges section. */
7406 static unsigned long
7407 size_of_aranges (void)
7409 unsigned long size;
7411 size = DWARF_ARANGES_HEADER_SIZE;
7413 /* Count the address/length pair for this compilation unit. */
7414 if (text_section_used)
7415 size += 2 * DWARF2_ADDR_SIZE;
7416 if (cold_text_section_used)
7417 size += 2 * DWARF2_ADDR_SIZE;
7418 if (have_multiple_function_sections)
7420 unsigned fde_idx;
7421 dw_fde_ref fde;
7423 FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, fde_idx, fde)
7425 if (!fde->in_std_section)
7426 size += 2 * DWARF2_ADDR_SIZE;
7427 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
7428 size += 2 * DWARF2_ADDR_SIZE;
7432 /* Count the two zero words used to terminated the address range table. */
7433 size += 2 * DWARF2_ADDR_SIZE;
7434 return size;
7437 /* Select the encoding of an attribute value. */
7439 static enum dwarf_form
7440 value_format (dw_attr_ref a)
7442 switch (a->dw_attr_val.val_class)
7444 case dw_val_class_addr:
7445 /* Only very few attributes allow DW_FORM_addr. */
7446 switch (a->dw_attr)
7448 case DW_AT_low_pc:
7449 case DW_AT_high_pc:
7450 case DW_AT_entry_pc:
7451 case DW_AT_trampoline:
7452 return DW_FORM_addr;
7453 default:
7454 break;
7456 switch (DWARF2_ADDR_SIZE)
7458 case 1:
7459 return DW_FORM_data1;
7460 case 2:
7461 return DW_FORM_data2;
7462 case 4:
7463 return DW_FORM_data4;
7464 case 8:
7465 return DW_FORM_data8;
7466 default:
7467 gcc_unreachable ();
7469 case dw_val_class_range_list:
7470 case dw_val_class_loc_list:
7471 if (dwarf_version >= 4)
7472 return DW_FORM_sec_offset;
7473 /* FALLTHRU */
7474 case dw_val_class_vms_delta:
7475 case dw_val_class_offset:
7476 switch (DWARF_OFFSET_SIZE)
7478 case 4:
7479 return DW_FORM_data4;
7480 case 8:
7481 return DW_FORM_data8;
7482 default:
7483 gcc_unreachable ();
7485 case dw_val_class_loc:
7486 if (dwarf_version >= 4)
7487 return DW_FORM_exprloc;
7488 switch (constant_size (size_of_locs (AT_loc (a))))
7490 case 1:
7491 return DW_FORM_block1;
7492 case 2:
7493 return DW_FORM_block2;
7494 default:
7495 gcc_unreachable ();
7497 case dw_val_class_const:
7498 return DW_FORM_sdata;
7499 case dw_val_class_unsigned_const:
7500 switch (constant_size (AT_unsigned (a)))
7502 case 1:
7503 return DW_FORM_data1;
7504 case 2:
7505 return DW_FORM_data2;
7506 case 4:
7507 /* In DWARF3 DW_AT_data_member_location with
7508 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
7509 constant, so we need to use DW_FORM_udata if we need
7510 a large constant. */
7511 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
7512 return DW_FORM_udata;
7513 return DW_FORM_data4;
7514 case 8:
7515 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
7516 return DW_FORM_udata;
7517 return DW_FORM_data8;
7518 default:
7519 gcc_unreachable ();
7521 case dw_val_class_const_double:
7522 switch (HOST_BITS_PER_WIDE_INT)
7524 case 8:
7525 return DW_FORM_data2;
7526 case 16:
7527 return DW_FORM_data4;
7528 case 32:
7529 return DW_FORM_data8;
7530 case 64:
7531 default:
7532 return DW_FORM_block1;
7534 case dw_val_class_vec:
7535 switch (constant_size (a->dw_attr_val.v.val_vec.length
7536 * a->dw_attr_val.v.val_vec.elt_size))
7538 case 1:
7539 return DW_FORM_block1;
7540 case 2:
7541 return DW_FORM_block2;
7542 case 4:
7543 return DW_FORM_block4;
7544 default:
7545 gcc_unreachable ();
7547 case dw_val_class_flag:
7548 if (dwarf_version >= 4)
7550 /* Currently all add_AT_flag calls pass in 1 as last argument,
7551 so DW_FORM_flag_present can be used. If that ever changes,
7552 we'll need to use DW_FORM_flag and have some optimization
7553 in build_abbrev_table that will change those to
7554 DW_FORM_flag_present if it is set to 1 in all DIEs using
7555 the same abbrev entry. */
7556 gcc_assert (a->dw_attr_val.v.val_flag == 1);
7557 return DW_FORM_flag_present;
7559 return DW_FORM_flag;
7560 case dw_val_class_die_ref:
7561 if (AT_ref_external (a))
7562 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
7563 else
7564 return DW_FORM_ref;
7565 case dw_val_class_fde_ref:
7566 return DW_FORM_data;
7567 case dw_val_class_lbl_id:
7568 return DW_FORM_addr;
7569 case dw_val_class_lineptr:
7570 case dw_val_class_macptr:
7571 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
7572 case dw_val_class_str:
7573 return AT_string_form (a);
7574 case dw_val_class_file:
7575 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
7577 case 1:
7578 return DW_FORM_data1;
7579 case 2:
7580 return DW_FORM_data2;
7581 case 4:
7582 return DW_FORM_data4;
7583 default:
7584 gcc_unreachable ();
7587 case dw_val_class_data8:
7588 return DW_FORM_data8;
7590 case dw_val_class_high_pc:
7591 switch (DWARF2_ADDR_SIZE)
7593 case 4:
7594 return DW_FORM_data4;
7595 case 8:
7596 return DW_FORM_data8;
7597 default:
7598 gcc_unreachable ();
7601 default:
7602 gcc_unreachable ();
7606 /* Output the encoding of an attribute value. */
7608 static void
7609 output_value_format (dw_attr_ref a)
7611 enum dwarf_form form = value_format (a);
7613 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
7616 /* Output the .debug_abbrev section which defines the DIE abbreviation
7617 table. */
7619 static void
7620 output_abbrev_section (void)
7622 unsigned long abbrev_id;
7624 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7626 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7627 unsigned ix;
7628 dw_attr_ref a_attr;
7630 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
7631 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
7632 dwarf_tag_name (abbrev->die_tag));
7634 if (abbrev->die_child != NULL)
7635 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
7636 else
7637 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
7639 for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
7640 ix++)
7642 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
7643 dwarf_attr_name (a_attr->dw_attr));
7644 output_value_format (a_attr);
7647 dw2_asm_output_data (1, 0, NULL);
7648 dw2_asm_output_data (1, 0, NULL);
7651 /* Terminate the table. */
7652 dw2_asm_output_data (1, 0, NULL);
7655 /* Output a symbol we can use to refer to this DIE from another CU. */
7657 static inline void
7658 output_die_symbol (dw_die_ref die)
7660 const char *sym = die->die_id.die_symbol;
7662 gcc_assert (!die->comdat_type_p);
7664 if (sym == 0)
7665 return;
7667 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
7668 /* We make these global, not weak; if the target doesn't support
7669 .linkonce, it doesn't support combining the sections, so debugging
7670 will break. */
7671 targetm.asm_out.globalize_label (asm_out_file, sym);
7673 ASM_OUTPUT_LABEL (asm_out_file, sym);
7676 /* Return a new location list, given the begin and end range, and the
7677 expression. */
7679 static inline dw_loc_list_ref
7680 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
7681 const char *section)
7683 dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
7685 retlist->begin = begin;
7686 retlist->end = end;
7687 retlist->expr = expr;
7688 retlist->section = section;
7690 return retlist;
7693 /* Generate a new internal symbol for this location list node, if it
7694 hasn't got one yet. */
7696 static inline void
7697 gen_llsym (dw_loc_list_ref list)
7699 gcc_assert (!list->ll_symbol);
7700 list->ll_symbol = gen_internal_sym ("LLST");
7703 /* Output the location list given to us. */
7705 static void
7706 output_loc_list (dw_loc_list_ref list_head)
7708 dw_loc_list_ref curr = list_head;
7710 if (list_head->emitted)
7711 return;
7712 list_head->emitted = true;
7714 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
7716 /* Walk the location list, and output each range + expression. */
7717 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
7719 unsigned long size;
7720 /* Don't output an entry that starts and ends at the same address. */
7721 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
7722 continue;
7723 size = size_of_locs (curr->expr);
7724 /* If the expression is too large, drop it on the floor. We could
7725 perhaps put it into DW_TAG_dwarf_procedure and refer to that
7726 in the expression, but >= 64KB expressions for a single value
7727 in a single range are unlikely very useful. */
7728 if (size > 0xffff)
7729 continue;
7730 if (!have_multiple_function_sections)
7732 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
7733 "Location list begin address (%s)",
7734 list_head->ll_symbol);
7735 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
7736 "Location list end address (%s)",
7737 list_head->ll_symbol);
7739 else
7741 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
7742 "Location list begin address (%s)",
7743 list_head->ll_symbol);
7744 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
7745 "Location list end address (%s)",
7746 list_head->ll_symbol);
7749 /* Output the block length for this list of location operations. */
7750 gcc_assert (size <= 0xffff);
7751 dw2_asm_output_data (2, size, "%s", "Location expression size");
7753 output_loc_sequence (curr->expr, -1);
7756 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7757 "Location list terminator begin (%s)",
7758 list_head->ll_symbol);
7759 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7760 "Location list terminator end (%s)",
7761 list_head->ll_symbol);
7764 /* Output a type signature. */
7766 static inline void
7767 output_signature (const char *sig, const char *name)
7769 int i;
7771 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
7772 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
7775 /* Output the DIE and its attributes. Called recursively to generate
7776 the definitions of each child DIE. */
7778 static void
7779 output_die (dw_die_ref die)
7781 dw_attr_ref a;
7782 dw_die_ref c;
7783 unsigned long size;
7784 unsigned ix;
7786 /* If someone in another CU might refer to us, set up a symbol for
7787 them to point to. */
7788 if (! die->comdat_type_p && die->die_id.die_symbol)
7789 output_die_symbol (die);
7791 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
7792 (unsigned long)die->die_offset,
7793 dwarf_tag_name (die->die_tag));
7795 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7797 const char *name = dwarf_attr_name (a->dw_attr);
7799 switch (AT_class (a))
7801 case dw_val_class_addr:
7802 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
7803 break;
7805 case dw_val_class_offset:
7806 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
7807 "%s", name);
7808 break;
7810 case dw_val_class_range_list:
7812 char *p = strchr (ranges_section_label, '\0');
7814 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
7815 a->dw_attr_val.v.val_offset);
7816 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
7817 debug_ranges_section, "%s", name);
7818 *p = '\0';
7820 break;
7822 case dw_val_class_loc:
7823 size = size_of_locs (AT_loc (a));
7825 /* Output the block length for this list of location operations. */
7826 if (dwarf_version >= 4)
7827 dw2_asm_output_data_uleb128 (size, "%s", name);
7828 else
7829 dw2_asm_output_data (constant_size (size), size, "%s", name);
7831 output_loc_sequence (AT_loc (a), -1);
7832 break;
7834 case dw_val_class_const:
7835 /* ??? It would be slightly more efficient to use a scheme like is
7836 used for unsigned constants below, but gdb 4.x does not sign
7837 extend. Gdb 5.x does sign extend. */
7838 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
7839 break;
7841 case dw_val_class_unsigned_const:
7843 int csize = constant_size (AT_unsigned (a));
7844 if (dwarf_version == 3
7845 && a->dw_attr == DW_AT_data_member_location
7846 && csize >= 4)
7847 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
7848 else
7849 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
7851 break;
7853 case dw_val_class_const_double:
7855 unsigned HOST_WIDE_INT first, second;
7857 if (HOST_BITS_PER_WIDE_INT >= 64)
7858 dw2_asm_output_data (1,
7859 HOST_BITS_PER_DOUBLE_INT
7860 / HOST_BITS_PER_CHAR,
7861 NULL);
7863 if (WORDS_BIG_ENDIAN)
7865 first = a->dw_attr_val.v.val_double.high;
7866 second = a->dw_attr_val.v.val_double.low;
7868 else
7870 first = a->dw_attr_val.v.val_double.low;
7871 second = a->dw_attr_val.v.val_double.high;
7874 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
7875 first, name);
7876 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
7877 second, NULL);
7879 break;
7881 case dw_val_class_vec:
7883 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
7884 unsigned int len = a->dw_attr_val.v.val_vec.length;
7885 unsigned int i;
7886 unsigned char *p;
7888 dw2_asm_output_data (constant_size (len * elt_size),
7889 len * elt_size, "%s", name);
7890 if (elt_size > sizeof (HOST_WIDE_INT))
7892 elt_size /= 2;
7893 len *= 2;
7895 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
7896 i < len;
7897 i++, p += elt_size)
7898 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
7899 "fp or vector constant word %u", i);
7900 break;
7903 case dw_val_class_flag:
7904 if (dwarf_version >= 4)
7906 /* Currently all add_AT_flag calls pass in 1 as last argument,
7907 so DW_FORM_flag_present can be used. If that ever changes,
7908 we'll need to use DW_FORM_flag and have some optimization
7909 in build_abbrev_table that will change those to
7910 DW_FORM_flag_present if it is set to 1 in all DIEs using
7911 the same abbrev entry. */
7912 gcc_assert (AT_flag (a) == 1);
7913 if (flag_debug_asm)
7914 fprintf (asm_out_file, "\t\t\t%s %s\n",
7915 ASM_COMMENT_START, name);
7916 break;
7918 dw2_asm_output_data (1, AT_flag (a), "%s", name);
7919 break;
7921 case dw_val_class_loc_list:
7923 char *sym = AT_loc_list (a)->ll_symbol;
7925 gcc_assert (sym);
7926 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
7927 "%s", name);
7929 break;
7931 case dw_val_class_die_ref:
7932 if (AT_ref_external (a))
7934 if (AT_ref (a)->comdat_type_p)
7936 comdat_type_node_ref type_node =
7937 AT_ref (a)->die_id.die_type_node;
7939 gcc_assert (type_node);
7940 output_signature (type_node->signature, name);
7942 else
7944 const char *sym = AT_ref (a)->die_id.die_symbol;
7945 int size;
7947 gcc_assert (sym);
7948 /* In DWARF2, DW_FORM_ref_addr is sized by target address
7949 length, whereas in DWARF3 it's always sized as an
7950 offset. */
7951 if (dwarf_version == 2)
7952 size = DWARF2_ADDR_SIZE;
7953 else
7954 size = DWARF_OFFSET_SIZE;
7955 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
7956 name);
7959 else
7961 gcc_assert (AT_ref (a)->die_offset);
7962 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
7963 "%s", name);
7965 break;
7967 case dw_val_class_fde_ref:
7969 char l1[20];
7971 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
7972 a->dw_attr_val.v.val_fde_index * 2);
7973 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
7974 "%s", name);
7976 break;
7978 case dw_val_class_vms_delta:
7979 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
7980 AT_vms_delta2 (a), AT_vms_delta1 (a),
7981 "%s", name);
7982 break;
7984 case dw_val_class_lbl_id:
7985 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
7986 break;
7988 case dw_val_class_lineptr:
7989 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7990 debug_line_section, "%s", name);
7991 break;
7993 case dw_val_class_macptr:
7994 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7995 debug_macinfo_section, "%s", name);
7996 break;
7998 case dw_val_class_str:
7999 if (AT_string_form (a) == DW_FORM_strp)
8000 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8001 a->dw_attr_val.v.val_str->label,
8002 debug_str_section,
8003 "%s: \"%s\"", name, AT_string (a));
8004 else
8005 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8006 break;
8008 case dw_val_class_file:
8010 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8012 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8013 a->dw_attr_val.v.val_file->filename);
8014 break;
8017 case dw_val_class_data8:
8019 int i;
8021 for (i = 0; i < 8; i++)
8022 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
8023 i == 0 ? "%s" : NULL, name);
8024 break;
8027 case dw_val_class_high_pc:
8028 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
8029 get_AT_low_pc (die), "DW_AT_high_pc");
8030 break;
8032 default:
8033 gcc_unreachable ();
8037 FOR_EACH_CHILD (die, c, output_die (c));
8039 /* Add null byte to terminate sibling list. */
8040 if (die->die_child != NULL)
8041 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
8042 (unsigned long) die->die_offset);
8045 /* Output the compilation unit that appears at the beginning of the
8046 .debug_info section, and precedes the DIE descriptions. */
8048 static void
8049 output_compilation_unit_header (void)
8051 int ver = dwarf_version;
8053 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8054 dw2_asm_output_data (4, 0xffffffff,
8055 "Initial length escape value indicating 64-bit DWARF extension");
8056 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8057 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8058 "Length of Compilation Unit Info");
8059 dw2_asm_output_data (2, ver, "DWARF version number");
8060 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8061 debug_abbrev_section,
8062 "Offset Into Abbrev. Section");
8063 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8066 /* Output the compilation unit DIE and its children. */
8068 static void
8069 output_comp_unit (dw_die_ref die, int output_if_empty)
8071 const char *secname, *oldsym;
8072 char *tmp;
8073 htab_t extern_map;
8075 /* Unless we are outputting main CU, we may throw away empty ones. */
8076 if (!output_if_empty && die->die_child == NULL)
8077 return;
8079 /* Even if there are no children of this DIE, we must output the information
8080 about the compilation unit. Otherwise, on an empty translation unit, we
8081 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
8082 will then complain when examining the file. First mark all the DIEs in
8083 this CU so we know which get local refs. */
8084 mark_dies (die);
8086 extern_map = optimize_external_refs (die);
8088 build_abbrev_table (die, extern_map);
8090 htab_delete (extern_map);
8092 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8093 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8094 calc_die_sizes (die);
8096 oldsym = die->die_id.die_symbol;
8097 if (oldsym)
8099 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8101 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8102 secname = tmp;
8103 die->die_id.die_symbol = NULL;
8104 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8106 else
8108 switch_to_section (debug_info_section);
8109 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
8110 info_section_emitted = true;
8113 /* Output debugging information. */
8114 output_compilation_unit_header ();
8115 output_die (die);
8117 /* Leave the marks on the main CU, so we can check them in
8118 output_pubnames. */
8119 if (oldsym)
8121 unmark_dies (die);
8122 die->die_id.die_symbol = oldsym;
8126 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
8127 and .debug_pubtypes. This is configured per-target, but can be
8128 overridden by the -gpubnames or -gno-pubnames options. */
8130 static inline bool
8131 want_pubnames (void)
8133 return (debug_generate_pub_sections != -1
8134 ? debug_generate_pub_sections
8135 : targetm.want_debug_pub_sections);
8138 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
8140 static void
8141 add_AT_pubnames (dw_die_ref die)
8143 if (want_pubnames ())
8144 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
8147 /* Output a comdat type unit DIE and its children. */
8149 static void
8150 output_comdat_type_unit (comdat_type_node *node)
8152 const char *secname;
8153 char *tmp;
8154 int i;
8155 #if defined (OBJECT_FORMAT_ELF)
8156 tree comdat_key;
8157 #endif
8158 htab_t extern_map;
8160 /* First mark all the DIEs in this CU so we know which get local refs. */
8161 mark_dies (node->root_die);
8163 extern_map = optimize_external_refs (node->root_die);
8165 build_abbrev_table (node->root_die, extern_map);
8167 htab_delete (extern_map);
8169 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8170 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
8171 calc_die_sizes (node->root_die);
8173 #if defined (OBJECT_FORMAT_ELF)
8174 secname = ".debug_types";
8175 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
8176 sprintf (tmp, "wt.");
8177 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8178 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
8179 comdat_key = get_identifier (tmp);
8180 targetm.asm_out.named_section (secname,
8181 SECTION_DEBUG | SECTION_LINKONCE,
8182 comdat_key);
8183 #else
8184 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
8185 sprintf (tmp, ".gnu.linkonce.wt.");
8186 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8187 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
8188 secname = tmp;
8189 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8190 #endif
8192 /* Output debugging information. */
8193 output_compilation_unit_header ();
8194 output_signature (node->signature, "Type Signature");
8195 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
8196 "Offset to Type DIE");
8197 output_die (node->root_die);
8199 unmark_dies (node->root_die);
8202 /* Return the DWARF2/3 pubname associated with a decl. */
8204 static const char *
8205 dwarf2_name (tree decl, int scope)
8207 if (DECL_NAMELESS (decl))
8208 return NULL;
8209 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
8212 /* Add a new entry to .debug_pubnames if appropriate. */
8214 static void
8215 add_pubname_string (const char *str, dw_die_ref die)
8217 pubname_entry e;
8219 e.die = die;
8220 e.name = xstrdup (str);
8221 VEC_safe_push (pubname_entry, gc, pubname_table, e);
8224 static void
8225 add_pubname (tree decl, dw_die_ref die)
8227 if (!want_pubnames ())
8228 return;
8230 /* Don't add items to the table when we expect that the consumer will have
8231 just read the enclosing die. For example, if the consumer is looking at a
8232 class_member, it will either be inside the class already, or will have just
8233 looked up the class to find the member. Either way, searching the class is
8234 faster than searching the index. */
8235 if ((TREE_PUBLIC (decl) && !is_class_die (die->die_parent))
8236 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
8238 const char *name = dwarf2_name (decl, 1);
8240 if (name)
8241 add_pubname_string (name, die);
8245 /* Add an enumerator to the pubnames section. */
8247 static void
8248 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
8250 pubname_entry e;
8252 gcc_assert (scope_name);
8253 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
8254 e.die = die;
8255 VEC_safe_push (pubname_entry, gc, pubname_table, e);
8258 /* Add a new entry to .debug_pubtypes if appropriate. */
8260 static void
8261 add_pubtype (tree decl, dw_die_ref die)
8263 pubname_entry e;
8265 if (!want_pubnames ())
8266 return;
8268 if ((TREE_PUBLIC (decl)
8269 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
8270 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
8272 tree scope = NULL;
8273 const char *scope_name = "";
8274 const char *sep = is_cxx () ? "::" : ".";
8275 const char *name;
8277 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
8278 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
8280 scope_name = lang_hooks.dwarf_name (scope, 1);
8281 if (scope_name != NULL && scope_name[0] != '\0')
8282 scope_name = concat (scope_name, sep, NULL);
8283 else
8284 scope_name = "";
8287 if (TYPE_P (decl))
8288 name = type_tag (decl);
8289 else
8290 name = lang_hooks.dwarf_name (decl, 1);
8292 /* If we don't have a name for the type, there's no point in adding
8293 it to the table. */
8294 if (name != NULL && name[0] != '\0')
8296 e.die = die;
8297 e.name = concat (scope_name, name, NULL);
8298 VEC_safe_push (pubname_entry, gc, pubtype_table, e);
8301 /* Although it might be more consistent to add the pubinfo for the
8302 enumerators as their dies are created, they should only be added if the
8303 enum type meets the criteria above. So rather than re-check the parent
8304 enum type whenever an enumerator die is created, just output them all
8305 here. This isn't protected by the name conditional because anonymous
8306 enums don't have names. */
8307 if (die->die_tag == DW_TAG_enumeration_type)
8309 dw_die_ref c;
8311 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
8316 /* Output the public names table used to speed up access to externally
8317 visible names; or the public types table used to find type definitions. */
8319 static void
8320 output_pubnames (VEC (pubname_entry, gc) * names)
8322 unsigned i;
8323 unsigned long pubnames_length = size_of_pubnames (names);
8324 pubname_ref pub;
8326 if (!want_pubnames () || !info_section_emitted)
8327 return;
8328 if (names == pubname_table)
8329 switch_to_section (debug_pubnames_section);
8330 else
8331 switch_to_section (debug_pubtypes_section);
8332 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8333 dw2_asm_output_data (4, 0xffffffff,
8334 "Initial length escape value indicating 64-bit DWARF extension");
8335 if (names == pubname_table)
8336 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8337 "Length of Public Names Info");
8338 else
8339 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8340 "Length of Public Type Names Info");
8341 /* Version number for pubnames/pubtypes is still 2, even in DWARF3. */
8342 dw2_asm_output_data (2, 2, "DWARF Version");
8343 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8344 debug_info_section,
8345 "Offset of Compilation Unit Info");
8346 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
8347 "Compilation Unit Length");
8349 FOR_EACH_VEC_ELT (pubname_entry, names, i, pub)
8351 /* Enumerator names are part of the pubname table, but the parent
8352 DW_TAG_enumeration_type die may have been pruned. Don't output
8353 them if that is the case. */
8354 if (pub->die->die_tag == DW_TAG_enumerator && !pub->die->die_mark)
8355 continue;
8357 /* We shouldn't see pubnames for DIEs outside of the main CU. */
8358 if (names == pubname_table)
8359 gcc_assert (pub->die->die_mark);
8361 if (names != pubtype_table
8362 || pub->die->die_offset != 0
8363 || !flag_eliminate_unused_debug_types)
8365 dw_offset die_offset = pub->die->die_offset;
8367 /* If we're putting types in their own .debug_types sections,
8368 the .debug_pubtypes table will still point to the compile
8369 unit (not the type unit), so we want to use the offset of
8370 the skeleton DIE (if there is one). */
8371 if (pub->die->comdat_type_p && names == pubtype_table)
8373 comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
8375 if (type_node != NULL)
8376 die_offset = (type_node->skeleton_die != NULL
8377 ? type_node->skeleton_die->die_offset
8378 : 0);
8381 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
8383 dw2_asm_output_nstring (pub->name, -1, "external name");
8387 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
8390 /* Output the information that goes into the .debug_aranges table.
8391 Namely, define the beginning and ending address range of the
8392 text section generated for this compilation unit. */
8394 static void
8395 output_aranges (unsigned long aranges_length)
8397 unsigned i;
8399 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8400 dw2_asm_output_data (4, 0xffffffff,
8401 "Initial length escape value indicating 64-bit DWARF extension");
8402 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
8403 "Length of Address Ranges Info");
8404 /* Version number for aranges is still 2, even in DWARF3. */
8405 dw2_asm_output_data (2, 2, "DWARF Version");
8406 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8407 debug_info_section,
8408 "Offset of Compilation Unit Info");
8409 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
8410 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
8412 /* We need to align to twice the pointer size here. */
8413 if (DWARF_ARANGES_PAD_SIZE)
8415 /* Pad using a 2 byte words so that padding is correct for any
8416 pointer size. */
8417 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
8418 2 * DWARF2_ADDR_SIZE);
8419 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
8420 dw2_asm_output_data (2, 0, NULL);
8423 /* It is necessary not to output these entries if the sections were
8424 not used; if the sections were not used, the length will be 0 and
8425 the address may end up as 0 if the section is discarded by ld
8426 --gc-sections, leaving an invalid (0, 0) entry that can be
8427 confused with the terminator. */
8428 if (text_section_used)
8430 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
8431 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
8432 text_section_label, "Length");
8434 if (cold_text_section_used)
8436 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
8437 "Address");
8438 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
8439 cold_text_section_label, "Length");
8442 if (have_multiple_function_sections)
8444 unsigned fde_idx;
8445 dw_fde_ref fde;
8447 FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, fde_idx, fde)
8449 if (DECL_IGNORED_P (fde->decl))
8450 continue;
8451 if (!fde->in_std_section)
8453 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
8454 "Address");
8455 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
8456 fde->dw_fde_begin, "Length");
8458 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8460 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
8461 "Address");
8462 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
8463 fde->dw_fde_second_begin, "Length");
8468 /* Output the terminator words. */
8469 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8470 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8473 /* Add a new entry to .debug_ranges. Return the offset at which it
8474 was placed. */
8476 static unsigned int
8477 add_ranges_num (int num)
8479 unsigned int in_use = ranges_table_in_use;
8481 if (in_use == ranges_table_allocated)
8483 ranges_table_allocated += RANGES_TABLE_INCREMENT;
8484 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
8485 ranges_table_allocated);
8486 memset (ranges_table + ranges_table_in_use, 0,
8487 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
8490 ranges_table[in_use].num = num;
8491 ranges_table_in_use = in_use + 1;
8493 return in_use * 2 * DWARF2_ADDR_SIZE;
8496 /* Add a new entry to .debug_ranges corresponding to a block, or a
8497 range terminator if BLOCK is NULL. */
8499 static unsigned int
8500 add_ranges (const_tree block)
8502 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
8505 /* Add a new entry to .debug_ranges corresponding to a pair of
8506 labels. */
8508 static void
8509 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
8510 bool *added)
8512 unsigned int in_use = ranges_by_label_in_use;
8513 unsigned int offset;
8515 if (in_use == ranges_by_label_allocated)
8517 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
8518 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
8519 ranges_by_label,
8520 ranges_by_label_allocated);
8521 memset (ranges_by_label + ranges_by_label_in_use, 0,
8522 RANGES_TABLE_INCREMENT
8523 * sizeof (struct dw_ranges_by_label_struct));
8526 ranges_by_label[in_use].begin = begin;
8527 ranges_by_label[in_use].end = end;
8528 ranges_by_label_in_use = in_use + 1;
8530 offset = add_ranges_num (-(int)in_use - 1);
8531 if (!*added)
8533 add_AT_range_list (die, DW_AT_ranges, offset);
8534 *added = true;
8538 static void
8539 output_ranges (void)
8541 unsigned i;
8542 static const char *const start_fmt = "Offset %#x";
8543 const char *fmt = start_fmt;
8545 for (i = 0; i < ranges_table_in_use; i++)
8547 int block_num = ranges_table[i].num;
8549 if (block_num > 0)
8551 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
8552 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
8554 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
8555 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
8557 /* If all code is in the text section, then the compilation
8558 unit base address defaults to DW_AT_low_pc, which is the
8559 base of the text section. */
8560 if (!have_multiple_function_sections)
8562 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
8563 text_section_label,
8564 fmt, i * 2 * DWARF2_ADDR_SIZE);
8565 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
8566 text_section_label, NULL);
8569 /* Otherwise, the compilation unit base address is zero,
8570 which allows us to use absolute addresses, and not worry
8571 about whether the target supports cross-section
8572 arithmetic. */
8573 else
8575 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
8576 fmt, i * 2 * DWARF2_ADDR_SIZE);
8577 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
8580 fmt = NULL;
8583 /* Negative block_num stands for an index into ranges_by_label. */
8584 else if (block_num < 0)
8586 int lab_idx = - block_num - 1;
8588 if (!have_multiple_function_sections)
8590 gcc_unreachable ();
8591 #if 0
8592 /* If we ever use add_ranges_by_labels () for a single
8593 function section, all we have to do is to take out
8594 the #if 0 above. */
8595 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8596 ranges_by_label[lab_idx].begin,
8597 text_section_label,
8598 fmt, i * 2 * DWARF2_ADDR_SIZE);
8599 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8600 ranges_by_label[lab_idx].end,
8601 text_section_label, NULL);
8602 #endif
8604 else
8606 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
8607 ranges_by_label[lab_idx].begin,
8608 fmt, i * 2 * DWARF2_ADDR_SIZE);
8609 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
8610 ranges_by_label[lab_idx].end,
8611 NULL);
8614 else
8616 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8617 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8618 fmt = start_fmt;
8623 /* Data structure containing information about input files. */
8624 struct file_info
8626 const char *path; /* Complete file name. */
8627 const char *fname; /* File name part. */
8628 int length; /* Length of entire string. */
8629 struct dwarf_file_data * file_idx; /* Index in input file table. */
8630 int dir_idx; /* Index in directory table. */
8633 /* Data structure containing information about directories with source
8634 files. */
8635 struct dir_info
8637 const char *path; /* Path including directory name. */
8638 int length; /* Path length. */
8639 int prefix; /* Index of directory entry which is a prefix. */
8640 int count; /* Number of files in this directory. */
8641 int dir_idx; /* Index of directory used as base. */
8644 /* Callback function for file_info comparison. We sort by looking at
8645 the directories in the path. */
8647 static int
8648 file_info_cmp (const void *p1, const void *p2)
8650 const struct file_info *const s1 = (const struct file_info *) p1;
8651 const struct file_info *const s2 = (const struct file_info *) p2;
8652 const unsigned char *cp1;
8653 const unsigned char *cp2;
8655 /* Take care of file names without directories. We need to make sure that
8656 we return consistent values to qsort since some will get confused if
8657 we return the same value when identical operands are passed in opposite
8658 orders. So if neither has a directory, return 0 and otherwise return
8659 1 or -1 depending on which one has the directory. */
8660 if ((s1->path == s1->fname || s2->path == s2->fname))
8661 return (s2->path == s2->fname) - (s1->path == s1->fname);
8663 cp1 = (const unsigned char *) s1->path;
8664 cp2 = (const unsigned char *) s2->path;
8666 while (1)
8668 ++cp1;
8669 ++cp2;
8670 /* Reached the end of the first path? If so, handle like above. */
8671 if ((cp1 == (const unsigned char *) s1->fname)
8672 || (cp2 == (const unsigned char *) s2->fname))
8673 return ((cp2 == (const unsigned char *) s2->fname)
8674 - (cp1 == (const unsigned char *) s1->fname));
8676 /* Character of current path component the same? */
8677 else if (*cp1 != *cp2)
8678 return *cp1 - *cp2;
8682 struct file_name_acquire_data
8684 struct file_info *files;
8685 int used_files;
8686 int max_files;
8689 /* Traversal function for the hash table. */
8691 static int
8692 file_name_acquire (void ** slot, void *data)
8694 struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
8695 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
8696 struct file_info *fi;
8697 const char *f;
8699 gcc_assert (fnad->max_files >= d->emitted_number);
8701 if (! d->emitted_number)
8702 return 1;
8704 gcc_assert (fnad->max_files != fnad->used_files);
8706 fi = fnad->files + fnad->used_files++;
8708 /* Skip all leading "./". */
8709 f = d->filename;
8710 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
8711 f += 2;
8713 /* Create a new array entry. */
8714 fi->path = f;
8715 fi->length = strlen (f);
8716 fi->file_idx = d;
8718 /* Search for the file name part. */
8719 f = strrchr (f, DIR_SEPARATOR);
8720 #if defined (DIR_SEPARATOR_2)
8722 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
8724 if (g != NULL)
8726 if (f == NULL || f < g)
8727 f = g;
8730 #endif
8732 fi->fname = f == NULL ? fi->path : f + 1;
8733 return 1;
8736 /* Output the directory table and the file name table. We try to minimize
8737 the total amount of memory needed. A heuristic is used to avoid large
8738 slowdowns with many input files. */
8740 static void
8741 output_file_names (void)
8743 struct file_name_acquire_data fnad;
8744 int numfiles;
8745 struct file_info *files;
8746 struct dir_info *dirs;
8747 int *saved;
8748 int *savehere;
8749 int *backmap;
8750 int ndirs;
8751 int idx_offset;
8752 int i;
8754 if (!last_emitted_file)
8756 dw2_asm_output_data (1, 0, "End directory table");
8757 dw2_asm_output_data (1, 0, "End file name table");
8758 return;
8761 numfiles = last_emitted_file->emitted_number;
8763 /* Allocate the various arrays we need. */
8764 files = XALLOCAVEC (struct file_info, numfiles);
8765 dirs = XALLOCAVEC (struct dir_info, numfiles);
8767 fnad.files = files;
8768 fnad.used_files = 0;
8769 fnad.max_files = numfiles;
8770 htab_traverse (file_table, file_name_acquire, &fnad);
8771 gcc_assert (fnad.used_files == fnad.max_files);
8773 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
8775 /* Find all the different directories used. */
8776 dirs[0].path = files[0].path;
8777 dirs[0].length = files[0].fname - files[0].path;
8778 dirs[0].prefix = -1;
8779 dirs[0].count = 1;
8780 dirs[0].dir_idx = 0;
8781 files[0].dir_idx = 0;
8782 ndirs = 1;
8784 for (i = 1; i < numfiles; i++)
8785 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
8786 && memcmp (dirs[ndirs - 1].path, files[i].path,
8787 dirs[ndirs - 1].length) == 0)
8789 /* Same directory as last entry. */
8790 files[i].dir_idx = ndirs - 1;
8791 ++dirs[ndirs - 1].count;
8793 else
8795 int j;
8797 /* This is a new directory. */
8798 dirs[ndirs].path = files[i].path;
8799 dirs[ndirs].length = files[i].fname - files[i].path;
8800 dirs[ndirs].count = 1;
8801 dirs[ndirs].dir_idx = ndirs;
8802 files[i].dir_idx = ndirs;
8804 /* Search for a prefix. */
8805 dirs[ndirs].prefix = -1;
8806 for (j = 0; j < ndirs; j++)
8807 if (dirs[j].length < dirs[ndirs].length
8808 && dirs[j].length > 1
8809 && (dirs[ndirs].prefix == -1
8810 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
8811 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
8812 dirs[ndirs].prefix = j;
8814 ++ndirs;
8817 /* Now to the actual work. We have to find a subset of the directories which
8818 allow expressing the file name using references to the directory table
8819 with the least amount of characters. We do not do an exhaustive search
8820 where we would have to check out every combination of every single
8821 possible prefix. Instead we use a heuristic which provides nearly optimal
8822 results in most cases and never is much off. */
8823 saved = XALLOCAVEC (int, ndirs);
8824 savehere = XALLOCAVEC (int, ndirs);
8826 memset (saved, '\0', ndirs * sizeof (saved[0]));
8827 for (i = 0; i < ndirs; i++)
8829 int j;
8830 int total;
8832 /* We can always save some space for the current directory. But this
8833 does not mean it will be enough to justify adding the directory. */
8834 savehere[i] = dirs[i].length;
8835 total = (savehere[i] - saved[i]) * dirs[i].count;
8837 for (j = i + 1; j < ndirs; j++)
8839 savehere[j] = 0;
8840 if (saved[j] < dirs[i].length)
8842 /* Determine whether the dirs[i] path is a prefix of the
8843 dirs[j] path. */
8844 int k;
8846 k = dirs[j].prefix;
8847 while (k != -1 && k != (int) i)
8848 k = dirs[k].prefix;
8850 if (k == (int) i)
8852 /* Yes it is. We can possibly save some memory by
8853 writing the filenames in dirs[j] relative to
8854 dirs[i]. */
8855 savehere[j] = dirs[i].length;
8856 total += (savehere[j] - saved[j]) * dirs[j].count;
8861 /* Check whether we can save enough to justify adding the dirs[i]
8862 directory. */
8863 if (total > dirs[i].length + 1)
8865 /* It's worthwhile adding. */
8866 for (j = i; j < ndirs; j++)
8867 if (savehere[j] > 0)
8869 /* Remember how much we saved for this directory so far. */
8870 saved[j] = savehere[j];
8872 /* Remember the prefix directory. */
8873 dirs[j].dir_idx = i;
8878 /* Emit the directory name table. */
8879 idx_offset = dirs[0].length > 0 ? 1 : 0;
8880 for (i = 1 - idx_offset; i < ndirs; i++)
8881 dw2_asm_output_nstring (dirs[i].path,
8882 dirs[i].length
8883 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
8884 "Directory Entry: %#x", i + idx_offset);
8886 dw2_asm_output_data (1, 0, "End directory table");
8888 /* We have to emit them in the order of emitted_number since that's
8889 used in the debug info generation. To do this efficiently we
8890 generate a back-mapping of the indices first. */
8891 backmap = XALLOCAVEC (int, numfiles);
8892 for (i = 0; i < numfiles; i++)
8893 backmap[files[i].file_idx->emitted_number - 1] = i;
8895 /* Now write all the file names. */
8896 for (i = 0; i < numfiles; i++)
8898 int file_idx = backmap[i];
8899 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
8901 #ifdef VMS_DEBUGGING_INFO
8902 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
8904 /* Setting these fields can lead to debugger miscomparisons,
8905 but VMS Debug requires them to be set correctly. */
8907 int ver;
8908 long long cdt;
8909 long siz;
8910 int maxfilelen = strlen (files[file_idx].path)
8911 + dirs[dir_idx].length
8912 + MAX_VMS_VERSION_LEN + 1;
8913 char *filebuf = XALLOCAVEC (char, maxfilelen);
8915 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
8916 snprintf (filebuf, maxfilelen, "%s;%d",
8917 files[file_idx].path + dirs[dir_idx].length, ver);
8919 dw2_asm_output_nstring
8920 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
8922 /* Include directory index. */
8923 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
8925 /* Modification time. */
8926 dw2_asm_output_data_uleb128
8927 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
8928 ? cdt : 0,
8929 NULL);
8931 /* File length in bytes. */
8932 dw2_asm_output_data_uleb128
8933 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
8934 ? siz : 0,
8935 NULL);
8936 #else
8937 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
8938 "File Entry: %#x", (unsigned) i + 1);
8940 /* Include directory index. */
8941 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
8943 /* Modification time. */
8944 dw2_asm_output_data_uleb128 (0, NULL);
8946 /* File length in bytes. */
8947 dw2_asm_output_data_uleb128 (0, NULL);
8948 #endif /* VMS_DEBUGGING_INFO */
8951 dw2_asm_output_data (1, 0, "End file name table");
8955 /* Output one line number table into the .debug_line section. */
8957 static void
8958 output_one_line_info_table (dw_line_info_table *table)
8960 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
8961 unsigned int current_line = 1;
8962 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
8963 dw_line_info_entry *ent;
8964 size_t i;
8966 FOR_EACH_VEC_ELT (dw_line_info_entry, table->entries, i, ent)
8968 switch (ent->opcode)
8970 case LI_set_address:
8971 /* ??? Unfortunately, we have little choice here currently, and
8972 must always use the most general form. GCC does not know the
8973 address delta itself, so we can't use DW_LNS_advance_pc. Many
8974 ports do have length attributes which will give an upper bound
8975 on the address range. We could perhaps use length attributes
8976 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
8977 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
8979 /* This can handle any delta. This takes
8980 4+DWARF2_ADDR_SIZE bytes. */
8981 dw2_asm_output_data (1, 0, "set address %s", line_label);
8982 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8983 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8984 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8985 break;
8987 case LI_set_line:
8988 if (ent->val == current_line)
8990 /* We still need to start a new row, so output a copy insn. */
8991 dw2_asm_output_data (1, DW_LNS_copy,
8992 "copy line %u", current_line);
8994 else
8996 int line_offset = ent->val - current_line;
8997 int line_delta = line_offset - DWARF_LINE_BASE;
8999 current_line = ent->val;
9000 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9002 /* This can handle deltas from -10 to 234, using the current
9003 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
9004 This takes 1 byte. */
9005 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9006 "line %u", current_line);
9008 else
9010 /* This can handle any delta. This takes at least 4 bytes,
9011 depending on the value being encoded. */
9012 dw2_asm_output_data (1, DW_LNS_advance_line,
9013 "advance to line %u", current_line);
9014 dw2_asm_output_data_sleb128 (line_offset, NULL);
9015 dw2_asm_output_data (1, DW_LNS_copy, NULL);
9018 break;
9020 case LI_set_file:
9021 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
9022 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9023 break;
9025 case LI_set_column:
9026 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
9027 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9028 break;
9030 case LI_negate_stmt:
9031 current_is_stmt = !current_is_stmt;
9032 dw2_asm_output_data (1, DW_LNS_negate_stmt,
9033 "is_stmt %d", current_is_stmt);
9034 break;
9036 case LI_set_prologue_end:
9037 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
9038 "set prologue end");
9039 break;
9041 case LI_set_epilogue_begin:
9042 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
9043 "set epilogue begin");
9044 break;
9046 case LI_set_discriminator:
9047 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
9048 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
9049 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
9050 dw2_asm_output_data_uleb128 (ent->val, NULL);
9051 break;
9055 /* Emit debug info for the address of the end of the table. */
9056 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
9057 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9058 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9059 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
9061 dw2_asm_output_data (1, 0, "end sequence");
9062 dw2_asm_output_data_uleb128 (1, NULL);
9063 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9066 /* Output the source line number correspondence information. This
9067 information goes into the .debug_line section. */
9069 static void
9070 output_line_info (void)
9072 char l1[20], l2[20], p1[20], p2[20];
9073 int ver = dwarf_version;
9074 bool saw_one = false;
9075 int opc;
9077 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
9078 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
9079 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
9080 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
9082 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9083 dw2_asm_output_data (4, 0xffffffff,
9084 "Initial length escape value indicating 64-bit DWARF extension");
9085 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
9086 "Length of Source Line Info");
9087 ASM_OUTPUT_LABEL (asm_out_file, l1);
9089 dw2_asm_output_data (2, ver, "DWARF Version");
9090 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
9091 ASM_OUTPUT_LABEL (asm_out_file, p1);
9093 /* Define the architecture-dependent minimum instruction length (in bytes).
9094 In this implementation of DWARF, this field is used for information
9095 purposes only. Since GCC generates assembly language, we have no
9096 a priori knowledge of how many instruction bytes are generated for each
9097 source line, and therefore can use only the DW_LNE_set_address and
9098 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
9099 this as '1', which is "correct enough" for all architectures,
9100 and don't let the target override. */
9101 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
9103 if (ver >= 4)
9104 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
9105 "Maximum Operations Per Instruction");
9106 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
9107 "Default is_stmt_start flag");
9108 dw2_asm_output_data (1, DWARF_LINE_BASE,
9109 "Line Base Value (Special Opcodes)");
9110 dw2_asm_output_data (1, DWARF_LINE_RANGE,
9111 "Line Range Value (Special Opcodes)");
9112 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
9113 "Special Opcode Base");
9115 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
9117 int n_op_args;
9118 switch (opc)
9120 case DW_LNS_advance_pc:
9121 case DW_LNS_advance_line:
9122 case DW_LNS_set_file:
9123 case DW_LNS_set_column:
9124 case DW_LNS_fixed_advance_pc:
9125 case DW_LNS_set_isa:
9126 n_op_args = 1;
9127 break;
9128 default:
9129 n_op_args = 0;
9130 break;
9133 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
9134 opc, n_op_args);
9137 /* Write out the information about the files we use. */
9138 output_file_names ();
9139 ASM_OUTPUT_LABEL (asm_out_file, p2);
9141 if (separate_line_info)
9143 dw_line_info_table *table;
9144 size_t i;
9146 FOR_EACH_VEC_ELT (dw_line_info_table_p, separate_line_info, i, table)
9147 if (table->in_use)
9149 output_one_line_info_table (table);
9150 saw_one = true;
9153 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
9155 output_one_line_info_table (cold_text_section_line_info);
9156 saw_one = true;
9159 /* ??? Some Darwin linkers crash on a .debug_line section with no
9160 sequences. Further, merely a DW_LNE_end_sequence entry is not
9161 sufficient -- the address column must also be initialized.
9162 Make sure to output at least one set_address/end_sequence pair,
9163 choosing .text since that section is always present. */
9164 if (text_section_line_info->in_use || !saw_one)
9165 output_one_line_info_table (text_section_line_info);
9167 /* Output the marker for the end of the line number info. */
9168 ASM_OUTPUT_LABEL (asm_out_file, l2);
9171 /* Given a pointer to a tree node for some base type, return a pointer to
9172 a DIE that describes the given type.
9174 This routine must only be called for GCC type nodes that correspond to
9175 Dwarf base (fundamental) types. */
9177 static dw_die_ref
9178 base_type_die (tree type)
9180 dw_die_ref base_type_result;
9181 enum dwarf_type encoding;
9183 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
9184 return 0;
9186 /* If this is a subtype that should not be emitted as a subrange type,
9187 use the base type. See subrange_type_for_debug_p. */
9188 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
9189 type = TREE_TYPE (type);
9191 switch (TREE_CODE (type))
9193 case INTEGER_TYPE:
9194 if ((dwarf_version >= 4 || !dwarf_strict)
9195 && TYPE_NAME (type)
9196 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
9197 && DECL_IS_BUILTIN (TYPE_NAME (type))
9198 && DECL_NAME (TYPE_NAME (type)))
9200 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
9201 if (strcmp (name, "char16_t") == 0
9202 || strcmp (name, "char32_t") == 0)
9204 encoding = DW_ATE_UTF;
9205 break;
9208 if (TYPE_STRING_FLAG (type))
9210 if (TYPE_UNSIGNED (type))
9211 encoding = DW_ATE_unsigned_char;
9212 else
9213 encoding = DW_ATE_signed_char;
9215 else if (TYPE_UNSIGNED (type))
9216 encoding = DW_ATE_unsigned;
9217 else
9218 encoding = DW_ATE_signed;
9219 break;
9221 case REAL_TYPE:
9222 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
9224 if (dwarf_version >= 3 || !dwarf_strict)
9225 encoding = DW_ATE_decimal_float;
9226 else
9227 encoding = DW_ATE_lo_user;
9229 else
9230 encoding = DW_ATE_float;
9231 break;
9233 case FIXED_POINT_TYPE:
9234 if (!(dwarf_version >= 3 || !dwarf_strict))
9235 encoding = DW_ATE_lo_user;
9236 else if (TYPE_UNSIGNED (type))
9237 encoding = DW_ATE_unsigned_fixed;
9238 else
9239 encoding = DW_ATE_signed_fixed;
9240 break;
9242 /* Dwarf2 doesn't know anything about complex ints, so use
9243 a user defined type for it. */
9244 case COMPLEX_TYPE:
9245 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
9246 encoding = DW_ATE_complex_float;
9247 else
9248 encoding = DW_ATE_lo_user;
9249 break;
9251 case BOOLEAN_TYPE:
9252 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
9253 encoding = DW_ATE_boolean;
9254 break;
9256 default:
9257 /* No other TREE_CODEs are Dwarf fundamental types. */
9258 gcc_unreachable ();
9261 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
9263 add_AT_unsigned (base_type_result, DW_AT_byte_size,
9264 int_size_in_bytes (type));
9265 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
9266 add_pubtype (type, base_type_result);
9268 return base_type_result;
9271 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
9272 given input type is a Dwarf "fundamental" type. Otherwise return null. */
9274 static inline int
9275 is_base_type (tree type)
9277 switch (TREE_CODE (type))
9279 case ERROR_MARK:
9280 case VOID_TYPE:
9281 case INTEGER_TYPE:
9282 case REAL_TYPE:
9283 case FIXED_POINT_TYPE:
9284 case COMPLEX_TYPE:
9285 case BOOLEAN_TYPE:
9286 return 1;
9288 case ARRAY_TYPE:
9289 case RECORD_TYPE:
9290 case UNION_TYPE:
9291 case QUAL_UNION_TYPE:
9292 case ENUMERAL_TYPE:
9293 case FUNCTION_TYPE:
9294 case METHOD_TYPE:
9295 case POINTER_TYPE:
9296 case REFERENCE_TYPE:
9297 case NULLPTR_TYPE:
9298 case OFFSET_TYPE:
9299 case LANG_TYPE:
9300 case VECTOR_TYPE:
9301 return 0;
9303 default:
9304 gcc_unreachable ();
9307 return 0;
9310 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
9311 node, return the size in bits for the type if it is a constant, or else
9312 return the alignment for the type if the type's size is not constant, or
9313 else return BITS_PER_WORD if the type actually turns out to be an
9314 ERROR_MARK node. */
9316 static inline unsigned HOST_WIDE_INT
9317 simple_type_size_in_bits (const_tree type)
9319 if (TREE_CODE (type) == ERROR_MARK)
9320 return BITS_PER_WORD;
9321 else if (TYPE_SIZE (type) == NULL_TREE)
9322 return 0;
9323 else if (host_integerp (TYPE_SIZE (type), 1))
9324 return tree_low_cst (TYPE_SIZE (type), 1);
9325 else
9326 return TYPE_ALIGN (type);
9329 /* Similarly, but return a double_int instead of UHWI. */
9331 static inline double_int
9332 double_int_type_size_in_bits (const_tree type)
9334 if (TREE_CODE (type) == ERROR_MARK)
9335 return double_int::from_uhwi (BITS_PER_WORD);
9336 else if (TYPE_SIZE (type) == NULL_TREE)
9337 return double_int_zero;
9338 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
9339 return tree_to_double_int (TYPE_SIZE (type));
9340 else
9341 return double_int::from_uhwi (TYPE_ALIGN (type));
9344 /* Given a pointer to a tree node for a subrange type, return a pointer
9345 to a DIE that describes the given type. */
9347 static dw_die_ref
9348 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
9350 dw_die_ref subrange_die;
9351 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
9353 if (context_die == NULL)
9354 context_die = comp_unit_die ();
9356 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
9358 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
9360 /* The size of the subrange type and its base type do not match,
9361 so we need to generate a size attribute for the subrange type. */
9362 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
9365 if (low)
9366 add_bound_info (subrange_die, DW_AT_lower_bound, low);
9367 if (high)
9368 add_bound_info (subrange_die, DW_AT_upper_bound, high);
9370 return subrange_die;
9373 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
9374 entry that chains various modifiers in front of the given type. */
9376 static dw_die_ref
9377 modified_type_die (tree type, int is_const_type, int is_volatile_type,
9378 dw_die_ref context_die)
9380 enum tree_code code = TREE_CODE (type);
9381 dw_die_ref mod_type_die;
9382 dw_die_ref sub_die = NULL;
9383 tree item_type = NULL;
9384 tree qualified_type;
9385 tree name, low, high;
9386 dw_die_ref mod_scope;
9388 if (code == ERROR_MARK)
9389 return NULL;
9391 /* See if we already have the appropriately qualified variant of
9392 this type. */
9393 qualified_type
9394 = get_qualified_type (type,
9395 ((is_const_type ? TYPE_QUAL_CONST : 0)
9396 | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
9398 if (qualified_type == sizetype
9399 && TYPE_NAME (qualified_type)
9400 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
9402 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
9404 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
9405 && TYPE_PRECISION (t)
9406 == TYPE_PRECISION (qualified_type)
9407 && TYPE_UNSIGNED (t)
9408 == TYPE_UNSIGNED (qualified_type));
9409 qualified_type = t;
9412 /* If we do, then we can just use its DIE, if it exists. */
9413 if (qualified_type)
9415 mod_type_die = lookup_type_die (qualified_type);
9416 if (mod_type_die)
9417 return mod_type_die;
9420 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
9422 /* Handle C typedef types. */
9423 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
9424 && !DECL_ARTIFICIAL (name))
9426 tree dtype = TREE_TYPE (name);
9428 if (qualified_type == dtype)
9430 /* For a named type, use the typedef. */
9431 gen_type_die (qualified_type, context_die);
9432 return lookup_type_die (qualified_type);
9434 else if (is_const_type < TYPE_READONLY (dtype)
9435 || is_volatile_type < TYPE_VOLATILE (dtype)
9436 || (is_const_type <= TYPE_READONLY (dtype)
9437 && is_volatile_type <= TYPE_VOLATILE (dtype)
9438 && DECL_ORIGINAL_TYPE (name) != type))
9439 /* cv-unqualified version of named type. Just use the unnamed
9440 type to which it refers. */
9441 return modified_type_die (DECL_ORIGINAL_TYPE (name),
9442 is_const_type, is_volatile_type,
9443 context_die);
9444 /* Else cv-qualified version of named type; fall through. */
9447 mod_scope = scope_die_for (type, context_die);
9449 if (is_const_type
9450 /* If both is_const_type and is_volatile_type, prefer the path
9451 which leads to a qualified type. */
9452 && (!is_volatile_type
9453 || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
9454 || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
9456 mod_type_die = new_die (DW_TAG_const_type, mod_scope, type);
9457 sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
9459 else if (is_volatile_type)
9461 mod_type_die = new_die (DW_TAG_volatile_type, mod_scope, type);
9462 sub_die = modified_type_die (type, is_const_type, 0, context_die);
9464 else if (code == POINTER_TYPE)
9466 mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
9467 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9468 simple_type_size_in_bits (type) / BITS_PER_UNIT);
9469 item_type = TREE_TYPE (type);
9470 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
9471 add_AT_unsigned (mod_type_die, DW_AT_address_class,
9472 TYPE_ADDR_SPACE (item_type));
9474 else if (code == REFERENCE_TYPE)
9476 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
9477 mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
9478 type);
9479 else
9480 mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
9481 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9482 simple_type_size_in_bits (type) / BITS_PER_UNIT);
9483 item_type = TREE_TYPE (type);
9484 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
9485 add_AT_unsigned (mod_type_die, DW_AT_address_class,
9486 TYPE_ADDR_SPACE (item_type));
9488 else if (code == INTEGER_TYPE
9489 && TREE_TYPE (type) != NULL_TREE
9490 && subrange_type_for_debug_p (type, &low, &high))
9492 mod_type_die = subrange_type_die (type, low, high, context_die);
9493 item_type = TREE_TYPE (type);
9495 else if (is_base_type (type))
9496 mod_type_die = base_type_die (type);
9497 else
9499 gen_type_die (type, context_die);
9501 /* We have to get the type_main_variant here (and pass that to the
9502 `lookup_type_die' routine) because the ..._TYPE node we have
9503 might simply be a *copy* of some original type node (where the
9504 copy was created to help us keep track of typedef names) and
9505 that copy might have a different TYPE_UID from the original
9506 ..._TYPE node. */
9507 if (TREE_CODE (type) != VECTOR_TYPE)
9508 return lookup_type_die (type_main_variant (type));
9509 else
9510 /* Vectors have the debugging information in the type,
9511 not the main variant. */
9512 return lookup_type_die (type);
9515 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
9516 don't output a DW_TAG_typedef, since there isn't one in the
9517 user's program; just attach a DW_AT_name to the type.
9518 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
9519 if the base type already has the same name. */
9520 if (name
9521 && ((TREE_CODE (name) != TYPE_DECL
9522 && (qualified_type == TYPE_MAIN_VARIANT (type)
9523 || (!is_const_type && !is_volatile_type)))
9524 || (TREE_CODE (name) == TYPE_DECL
9525 && TREE_TYPE (name) == qualified_type
9526 && DECL_NAME (name))))
9528 if (TREE_CODE (name) == TYPE_DECL)
9529 /* Could just call add_name_and_src_coords_attributes here,
9530 but since this is a builtin type it doesn't have any
9531 useful source coordinates anyway. */
9532 name = DECL_NAME (name);
9533 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
9535 /* This probably indicates a bug. */
9536 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
9538 name = TYPE_NAME (type);
9539 if (name
9540 && TREE_CODE (name) == TYPE_DECL)
9541 name = DECL_NAME (name);
9542 add_name_attribute (mod_type_die,
9543 name ? IDENTIFIER_POINTER (name) : "__unknown__");
9546 if (qualified_type)
9547 equate_type_number_to_die (qualified_type, mod_type_die);
9549 if (item_type)
9550 /* We must do this after the equate_type_number_to_die call, in case
9551 this is a recursive type. This ensures that the modified_type_die
9552 recursion will terminate even if the type is recursive. Recursive
9553 types are possible in Ada. */
9554 sub_die = modified_type_die (item_type,
9555 TYPE_READONLY (item_type),
9556 TYPE_VOLATILE (item_type),
9557 context_die);
9559 if (sub_die != NULL)
9560 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
9562 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
9563 if (TYPE_ARTIFICIAL (type))
9564 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
9566 return mod_type_die;
9569 /* Generate DIEs for the generic parameters of T.
9570 T must be either a generic type or a generic function.
9571 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
9573 static void
9574 gen_generic_params_dies (tree t)
9576 tree parms, args;
9577 int parms_num, i;
9578 dw_die_ref die = NULL;
9580 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
9581 return;
9583 if (TYPE_P (t))
9584 die = lookup_type_die (t);
9585 else if (DECL_P (t))
9586 die = lookup_decl_die (t);
9588 gcc_assert (die);
9590 parms = lang_hooks.get_innermost_generic_parms (t);
9591 if (!parms)
9592 /* T has no generic parameter. It means T is neither a generic type
9593 or function. End of story. */
9594 return;
9596 parms_num = TREE_VEC_LENGTH (parms);
9597 args = lang_hooks.get_innermost_generic_args (t);
9598 for (i = 0; i < parms_num; i++)
9600 tree parm, arg, arg_pack_elems;
9602 parm = TREE_VEC_ELT (parms, i);
9603 arg = TREE_VEC_ELT (args, i);
9604 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
9605 gcc_assert (parm && TREE_VALUE (parm) && arg);
9607 if (parm && TREE_VALUE (parm) && arg)
9609 /* If PARM represents a template parameter pack,
9610 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
9611 by DW_TAG_template_*_parameter DIEs for the argument
9612 pack elements of ARG. Note that ARG would then be
9613 an argument pack. */
9614 if (arg_pack_elems)
9615 template_parameter_pack_die (TREE_VALUE (parm),
9616 arg_pack_elems,
9617 die);
9618 else
9619 generic_parameter_die (TREE_VALUE (parm), arg,
9620 true /* Emit DW_AT_name */, die);
9625 /* Create and return a DIE for PARM which should be
9626 the representation of a generic type parameter.
9627 For instance, in the C++ front end, PARM would be a template parameter.
9628 ARG is the argument to PARM.
9629 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
9630 name of the PARM.
9631 PARENT_DIE is the parent DIE which the new created DIE should be added to,
9632 as a child node. */
9634 static dw_die_ref
9635 generic_parameter_die (tree parm, tree arg,
9636 bool emit_name_p,
9637 dw_die_ref parent_die)
9639 dw_die_ref tmpl_die = NULL;
9640 const char *name = NULL;
9642 if (!parm || !DECL_NAME (parm) || !arg)
9643 return NULL;
9645 /* We support non-type generic parameters and arguments,
9646 type generic parameters and arguments, as well as
9647 generic generic parameters (a.k.a. template template parameters in C++)
9648 and arguments. */
9649 if (TREE_CODE (parm) == PARM_DECL)
9650 /* PARM is a nontype generic parameter */
9651 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
9652 else if (TREE_CODE (parm) == TYPE_DECL)
9653 /* PARM is a type generic parameter. */
9654 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
9655 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
9656 /* PARM is a generic generic parameter.
9657 Its DIE is a GNU extension. It shall have a
9658 DW_AT_name attribute to represent the name of the template template
9659 parameter, and a DW_AT_GNU_template_name attribute to represent the
9660 name of the template template argument. */
9661 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
9662 parent_die, parm);
9663 else
9664 gcc_unreachable ();
9666 if (tmpl_die)
9668 tree tmpl_type;
9670 /* If PARM is a generic parameter pack, it means we are
9671 emitting debug info for a template argument pack element.
9672 In other terms, ARG is a template argument pack element.
9673 In that case, we don't emit any DW_AT_name attribute for
9674 the die. */
9675 if (emit_name_p)
9677 name = IDENTIFIER_POINTER (DECL_NAME (parm));
9678 gcc_assert (name);
9679 add_AT_string (tmpl_die, DW_AT_name, name);
9682 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
9684 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
9685 TMPL_DIE should have a child DW_AT_type attribute that is set
9686 to the type of the argument to PARM, which is ARG.
9687 If PARM is a type generic parameter, TMPL_DIE should have a
9688 child DW_AT_type that is set to ARG. */
9689 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
9690 add_type_attribute (tmpl_die, tmpl_type, 0,
9691 TREE_THIS_VOLATILE (tmpl_type),
9692 parent_die);
9694 else
9696 /* So TMPL_DIE is a DIE representing a
9697 a generic generic template parameter, a.k.a template template
9698 parameter in C++ and arg is a template. */
9700 /* The DW_AT_GNU_template_name attribute of the DIE must be set
9701 to the name of the argument. */
9702 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
9703 if (name)
9704 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
9707 if (TREE_CODE (parm) == PARM_DECL)
9708 /* So PARM is a non-type generic parameter.
9709 DWARF3 5.6.8 says we must set a DW_AT_const_value child
9710 attribute of TMPL_DIE which value represents the value
9711 of ARG.
9712 We must be careful here:
9713 The value of ARG might reference some function decls.
9714 We might currently be emitting debug info for a generic
9715 type and types are emitted before function decls, we don't
9716 know if the function decls referenced by ARG will actually be
9717 emitted after cgraph computations.
9718 So must defer the generation of the DW_AT_const_value to
9719 after cgraph is ready. */
9720 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
9723 return tmpl_die;
9726 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
9727 PARM_PACK must be a template parameter pack. The returned DIE
9728 will be child DIE of PARENT_DIE. */
9730 static dw_die_ref
9731 template_parameter_pack_die (tree parm_pack,
9732 tree parm_pack_args,
9733 dw_die_ref parent_die)
9735 dw_die_ref die;
9736 int j;
9738 gcc_assert (parent_die && parm_pack);
9740 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
9741 add_name_and_src_coords_attributes (die, parm_pack);
9742 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
9743 generic_parameter_die (parm_pack,
9744 TREE_VEC_ELT (parm_pack_args, j),
9745 false /* Don't emit DW_AT_name */,
9746 die);
9747 return die;
9750 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
9751 an enumerated type. */
9753 static inline int
9754 type_is_enum (const_tree type)
9756 return TREE_CODE (type) == ENUMERAL_TYPE;
9759 /* Return the DBX register number described by a given RTL node. */
9761 static unsigned int
9762 dbx_reg_number (const_rtx rtl)
9764 unsigned regno = REGNO (rtl);
9766 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
9768 #ifdef LEAF_REG_REMAP
9769 if (crtl->uses_only_leaf_regs)
9771 int leaf_reg = LEAF_REG_REMAP (regno);
9772 if (leaf_reg != -1)
9773 regno = (unsigned) leaf_reg;
9775 #endif
9777 regno = DBX_REGISTER_NUMBER (regno);
9778 gcc_assert (regno != INVALID_REGNUM);
9779 return regno;
9782 /* Optionally add a DW_OP_piece term to a location description expression.
9783 DW_OP_piece is only added if the location description expression already
9784 doesn't end with DW_OP_piece. */
9786 static void
9787 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
9789 dw_loc_descr_ref loc;
9791 if (*list_head != NULL)
9793 /* Find the end of the chain. */
9794 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
9797 if (loc->dw_loc_opc != DW_OP_piece)
9798 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
9802 /* Return a location descriptor that designates a machine register or
9803 zero if there is none. */
9805 static dw_loc_descr_ref
9806 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
9808 rtx regs;
9810 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
9811 return 0;
9813 /* We only use "frame base" when we're sure we're talking about the
9814 post-prologue local stack frame. We do this by *not* running
9815 register elimination until this point, and recognizing the special
9816 argument pointer and soft frame pointer rtx's.
9817 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
9818 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
9819 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
9821 dw_loc_descr_ref result = NULL;
9823 if (dwarf_version >= 4 || !dwarf_strict)
9825 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
9826 initialized);
9827 if (result)
9828 add_loc_descr (&result,
9829 new_loc_descr (DW_OP_stack_value, 0, 0));
9831 return result;
9834 regs = targetm.dwarf_register_span (rtl);
9836 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
9837 return multiple_reg_loc_descriptor (rtl, regs, initialized);
9838 else
9839 return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
9842 /* Return a location descriptor that designates a machine register for
9843 a given hard register number. */
9845 static dw_loc_descr_ref
9846 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
9848 dw_loc_descr_ref reg_loc_descr;
9850 if (regno <= 31)
9851 reg_loc_descr
9852 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
9853 else
9854 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
9856 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
9857 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9859 return reg_loc_descr;
9862 /* Given an RTL of a register, return a location descriptor that
9863 designates a value that spans more than one register. */
9865 static dw_loc_descr_ref
9866 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
9867 enum var_init_status initialized)
9869 int nregs, size, i;
9870 unsigned reg;
9871 dw_loc_descr_ref loc_result = NULL;
9873 reg = REGNO (rtl);
9874 #ifdef LEAF_REG_REMAP
9875 if (crtl->uses_only_leaf_regs)
9877 int leaf_reg = LEAF_REG_REMAP (reg);
9878 if (leaf_reg != -1)
9879 reg = (unsigned) leaf_reg;
9881 #endif
9882 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
9883 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
9885 /* Simple, contiguous registers. */
9886 if (regs == NULL_RTX)
9888 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
9890 loc_result = NULL;
9891 while (nregs--)
9893 dw_loc_descr_ref t;
9895 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
9896 VAR_INIT_STATUS_INITIALIZED);
9897 add_loc_descr (&loc_result, t);
9898 add_loc_descr_op_piece (&loc_result, size);
9899 ++reg;
9901 return loc_result;
9904 /* Now onto stupid register sets in non contiguous locations. */
9906 gcc_assert (GET_CODE (regs) == PARALLEL);
9908 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
9909 loc_result = NULL;
9911 for (i = 0; i < XVECLEN (regs, 0); ++i)
9913 dw_loc_descr_ref t;
9915 t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
9916 VAR_INIT_STATUS_INITIALIZED);
9917 add_loc_descr (&loc_result, t);
9918 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
9919 add_loc_descr_op_piece (&loc_result, size);
9922 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9923 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9924 return loc_result;
9927 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
9929 /* Return a location descriptor that designates a constant i,
9930 as a compound operation from constant (i >> shift), constant shift
9931 and DW_OP_shl. */
9933 static dw_loc_descr_ref
9934 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
9936 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
9937 add_loc_descr (&ret, int_loc_descriptor (shift));
9938 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
9939 return ret;
9942 /* Return a location descriptor that designates a constant. */
9944 static dw_loc_descr_ref
9945 int_loc_descriptor (HOST_WIDE_INT i)
9947 enum dwarf_location_atom op;
9949 /* Pick the smallest representation of a constant, rather than just
9950 defaulting to the LEB encoding. */
9951 if (i >= 0)
9953 int clz = clz_hwi (i);
9954 int ctz = ctz_hwi (i);
9955 if (i <= 31)
9956 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
9957 else if (i <= 0xff)
9958 op = DW_OP_const1u;
9959 else if (i <= 0xffff)
9960 op = DW_OP_const2u;
9961 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
9962 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
9963 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
9964 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
9965 while DW_OP_const4u is 5 bytes. */
9966 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
9967 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
9968 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
9969 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
9970 while DW_OP_const4u is 5 bytes. */
9971 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
9972 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
9973 op = DW_OP_const4u;
9974 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
9975 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
9976 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
9977 while DW_OP_constu of constant >= 0x100000000 takes at least
9978 6 bytes. */
9979 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
9980 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
9981 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
9982 >= HOST_BITS_PER_WIDE_INT)
9983 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
9984 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
9985 while DW_OP_constu takes in this case at least 6 bytes. */
9986 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
9987 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
9988 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
9989 && size_of_uleb128 (i) > 6)
9990 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
9991 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
9992 else
9993 op = DW_OP_constu;
9995 else
9997 if (i >= -0x80)
9998 op = DW_OP_const1s;
9999 else if (i >= -0x8000)
10000 op = DW_OP_const2s;
10001 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10003 if (size_of_int_loc_descriptor (i) < 5)
10005 dw_loc_descr_ref ret = int_loc_descriptor (-i);
10006 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10007 return ret;
10009 op = DW_OP_const4s;
10011 else
10013 if (size_of_int_loc_descriptor (i)
10014 < (unsigned long) 1 + size_of_sleb128 (i))
10016 dw_loc_descr_ref ret = int_loc_descriptor (-i);
10017 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10018 return ret;
10020 op = DW_OP_consts;
10024 return new_loc_descr (op, i, 0);
10027 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
10028 without actually allocating it. */
10030 static unsigned long
10031 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10033 return size_of_int_loc_descriptor (i >> shift)
10034 + size_of_int_loc_descriptor (shift)
10035 + 1;
10038 /* Return size_of_locs (int_loc_descriptor (i)) without
10039 actually allocating it. */
10041 static unsigned long
10042 size_of_int_loc_descriptor (HOST_WIDE_INT i)
10044 unsigned long s;
10046 if (i >= 0)
10048 int clz, ctz;
10049 if (i <= 31)
10050 return 1;
10051 else if (i <= 0xff)
10052 return 2;
10053 else if (i <= 0xffff)
10054 return 3;
10055 clz = clz_hwi (i);
10056 ctz = ctz_hwi (i);
10057 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10058 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10059 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10060 - clz - 5);
10061 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10062 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10063 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10064 - clz - 8);
10065 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10066 return 5;
10067 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
10068 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10069 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10070 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10071 - clz - 8);
10072 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10073 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
10074 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10075 - clz - 16);
10076 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10077 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10078 && s > 6)
10079 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10080 - clz - 32);
10081 else
10082 return 1 + s;
10084 else
10086 if (i >= -0x80)
10087 return 2;
10088 else if (i >= -0x8000)
10089 return 3;
10090 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10092 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
10094 s = size_of_int_loc_descriptor (-i) + 1;
10095 if (s < 5)
10096 return s;
10098 return 5;
10100 else
10102 unsigned long r = 1 + size_of_sleb128 (i);
10103 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
10105 s = size_of_int_loc_descriptor (-i) + 1;
10106 if (s < r)
10107 return s;
10109 return r;
10114 /* Return loc description representing "address" of integer value.
10115 This can appear only as toplevel expression. */
10117 static dw_loc_descr_ref
10118 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
10120 int litsize;
10121 dw_loc_descr_ref loc_result = NULL;
10123 if (!(dwarf_version >= 4 || !dwarf_strict))
10124 return NULL;
10126 litsize = size_of_int_loc_descriptor (i);
10127 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
10128 is more compact. For DW_OP_stack_value we need:
10129 litsize + 1 (DW_OP_stack_value)
10130 and for DW_OP_implicit_value:
10131 1 (DW_OP_implicit_value) + 1 (length) + size. */
10132 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
10134 loc_result = int_loc_descriptor (i);
10135 add_loc_descr (&loc_result,
10136 new_loc_descr (DW_OP_stack_value, 0, 0));
10137 return loc_result;
10140 loc_result = new_loc_descr (DW_OP_implicit_value,
10141 size, 0);
10142 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
10143 loc_result->dw_loc_oprnd2.v.val_int = i;
10144 return loc_result;
10147 /* Return a location descriptor that designates a base+offset location. */
10149 static dw_loc_descr_ref
10150 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
10151 enum var_init_status initialized)
10153 unsigned int regno;
10154 dw_loc_descr_ref result;
10155 dw_fde_ref fde = cfun->fde;
10157 /* We only use "frame base" when we're sure we're talking about the
10158 post-prologue local stack frame. We do this by *not* running
10159 register elimination until this point, and recognizing the special
10160 argument pointer and soft frame pointer rtx's. */
10161 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
10163 rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
10165 if (elim != reg)
10167 if (GET_CODE (elim) == PLUS)
10169 offset += INTVAL (XEXP (elim, 1));
10170 elim = XEXP (elim, 0);
10172 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
10173 && (elim == hard_frame_pointer_rtx
10174 || elim == stack_pointer_rtx))
10175 || elim == (frame_pointer_needed
10176 ? hard_frame_pointer_rtx
10177 : stack_pointer_rtx));
10179 /* If drap register is used to align stack, use frame
10180 pointer + offset to access stack variables. If stack
10181 is aligned without drap, use stack pointer + offset to
10182 access stack variables. */
10183 if (crtl->stack_realign_tried
10184 && reg == frame_pointer_rtx)
10186 int base_reg
10187 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
10188 ? HARD_FRAME_POINTER_REGNUM
10189 : REGNO (elim));
10190 return new_reg_loc_descr (base_reg, offset);
10193 gcc_assert (frame_pointer_fb_offset_valid);
10194 offset += frame_pointer_fb_offset;
10195 return new_loc_descr (DW_OP_fbreg, offset, 0);
10199 regno = DWARF_FRAME_REGNUM (REGNO (reg));
10201 if (!optimize && fde
10202 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
10204 /* Use cfa+offset to represent the location of arguments passed
10205 on the stack when drap is used to align stack.
10206 Only do this when not optimizing, for optimized code var-tracking
10207 is supposed to track where the arguments live and the register
10208 used as vdrap or drap in some spot might be used for something
10209 else in other part of the routine. */
10210 return new_loc_descr (DW_OP_fbreg, offset, 0);
10213 if (regno <= 31)
10214 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
10215 offset, 0);
10216 else
10217 result = new_loc_descr (DW_OP_bregx, regno, offset);
10219 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10220 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10222 return result;
10225 /* Return true if this RTL expression describes a base+offset calculation. */
10227 static inline int
10228 is_based_loc (const_rtx rtl)
10230 return (GET_CODE (rtl) == PLUS
10231 && ((REG_P (XEXP (rtl, 0))
10232 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
10233 && CONST_INT_P (XEXP (rtl, 1)))));
10236 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
10237 failed. */
10239 static dw_loc_descr_ref
10240 tls_mem_loc_descriptor (rtx mem)
10242 tree base;
10243 dw_loc_descr_ref loc_result;
10245 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
10246 return NULL;
10248 base = get_base_address (MEM_EXPR (mem));
10249 if (base == NULL
10250 || TREE_CODE (base) != VAR_DECL
10251 || !DECL_THREAD_LOCAL_P (base))
10252 return NULL;
10254 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
10255 if (loc_result == NULL)
10256 return NULL;
10258 if (MEM_OFFSET (mem))
10259 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
10261 return loc_result;
10264 /* Output debug info about reason why we failed to expand expression as dwarf
10265 expression. */
10267 static void
10268 expansion_failed (tree expr, rtx rtl, char const *reason)
10270 if (dump_file && (dump_flags & TDF_DETAILS))
10272 fprintf (dump_file, "Failed to expand as dwarf: ");
10273 if (expr)
10274 print_generic_expr (dump_file, expr, dump_flags);
10275 if (rtl)
10277 fprintf (dump_file, "\n");
10278 print_rtl (dump_file, rtl);
10280 fprintf (dump_file, "\nReason: %s\n", reason);
10284 /* Helper function for const_ok_for_output, called either directly
10285 or via for_each_rtx. */
10287 static int
10288 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
10290 rtx rtl = *rtlp;
10292 if (GET_CODE (rtl) == UNSPEC)
10294 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
10295 we can't express it in the debug info. */
10296 #ifdef ENABLE_CHECKING
10297 /* Don't complain about TLS UNSPECs, those are just too hard to
10298 delegitimize. Note this could be a non-decl SYMBOL_REF such as
10299 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
10300 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
10301 if (XVECLEN (rtl, 0) == 0
10302 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
10303 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
10304 inform (current_function_decl
10305 ? DECL_SOURCE_LOCATION (current_function_decl)
10306 : UNKNOWN_LOCATION,
10307 #if NUM_UNSPEC_VALUES > 0
10308 "non-delegitimized UNSPEC %s (%d) found in variable location",
10309 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
10310 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
10311 XINT (rtl, 1));
10312 #else
10313 "non-delegitimized UNSPEC %d found in variable location",
10314 XINT (rtl, 1));
10315 #endif
10316 #endif
10317 expansion_failed (NULL_TREE, rtl,
10318 "UNSPEC hasn't been delegitimized.\n");
10319 return 1;
10322 if (targetm.const_not_ok_for_debug_p (rtl))
10324 expansion_failed (NULL_TREE, rtl,
10325 "Expression rejected for debug by the backend.\n");
10326 return 1;
10329 if (GET_CODE (rtl) != SYMBOL_REF)
10330 return 0;
10332 if (CONSTANT_POOL_ADDRESS_P (rtl))
10334 bool marked;
10335 get_pool_constant_mark (rtl, &marked);
10336 /* If all references to this pool constant were optimized away,
10337 it was not output and thus we can't represent it. */
10338 if (!marked)
10340 expansion_failed (NULL_TREE, rtl,
10341 "Constant was removed from constant pool.\n");
10342 return 1;
10346 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
10347 return 1;
10349 /* Avoid references to external symbols in debug info, on several targets
10350 the linker might even refuse to link when linking a shared library,
10351 and in many other cases the relocations for .debug_info/.debug_loc are
10352 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
10353 to be defined within the same shared library or executable are fine. */
10354 if (SYMBOL_REF_EXTERNAL_P (rtl))
10356 tree decl = SYMBOL_REF_DECL (rtl);
10358 if (decl == NULL || !targetm.binds_local_p (decl))
10360 expansion_failed (NULL_TREE, rtl,
10361 "Symbol not defined in current TU.\n");
10362 return 1;
10366 return 0;
10369 /* Return true if constant RTL can be emitted in DW_OP_addr or
10370 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
10371 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
10373 static bool
10374 const_ok_for_output (rtx rtl)
10376 if (GET_CODE (rtl) == SYMBOL_REF)
10377 return const_ok_for_output_1 (&rtl, NULL) == 0;
10379 if (GET_CODE (rtl) == CONST)
10380 return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
10382 return true;
10385 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
10386 if possible, NULL otherwise. */
10388 static dw_die_ref
10389 base_type_for_mode (enum machine_mode mode, bool unsignedp)
10391 dw_die_ref type_die;
10392 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
10394 if (type == NULL)
10395 return NULL;
10396 switch (TREE_CODE (type))
10398 case INTEGER_TYPE:
10399 case REAL_TYPE:
10400 break;
10401 default:
10402 return NULL;
10404 type_die = lookup_type_die (type);
10405 if (!type_die)
10406 type_die = modified_type_die (type, false, false, comp_unit_die ());
10407 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
10408 return NULL;
10409 return type_die;
10412 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
10413 type matching MODE, or, if MODE is narrower than or as wide as
10414 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
10415 possible. */
10417 static dw_loc_descr_ref
10418 convert_descriptor_to_mode (enum machine_mode mode, dw_loc_descr_ref op)
10420 enum machine_mode outer_mode = mode;
10421 dw_die_ref type_die;
10422 dw_loc_descr_ref cvt;
10424 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
10426 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
10427 return op;
10429 type_die = base_type_for_mode (outer_mode, 1);
10430 if (type_die == NULL)
10431 return NULL;
10432 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10433 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10434 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10435 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10436 add_loc_descr (&op, cvt);
10437 return op;
10440 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
10442 static dw_loc_descr_ref
10443 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
10444 dw_loc_descr_ref op1)
10446 dw_loc_descr_ref ret = op0;
10447 add_loc_descr (&ret, op1);
10448 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
10449 if (STORE_FLAG_VALUE != 1)
10451 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
10452 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
10454 return ret;
10457 /* Return location descriptor for signed comparison OP RTL. */
10459 static dw_loc_descr_ref
10460 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
10461 enum machine_mode mem_mode)
10463 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
10464 dw_loc_descr_ref op0, op1;
10465 int shift;
10467 if (op_mode == VOIDmode)
10468 op_mode = GET_MODE (XEXP (rtl, 1));
10469 if (op_mode == VOIDmode)
10470 return NULL;
10472 if (dwarf_strict
10473 && (GET_MODE_CLASS (op_mode) != MODE_INT
10474 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
10475 return NULL;
10477 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
10478 VAR_INIT_STATUS_INITIALIZED);
10479 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
10480 VAR_INIT_STATUS_INITIALIZED);
10482 if (op0 == NULL || op1 == NULL)
10483 return NULL;
10485 if (GET_MODE_CLASS (op_mode) != MODE_INT
10486 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
10487 return compare_loc_descriptor (op, op0, op1);
10489 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
10491 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
10492 dw_loc_descr_ref cvt;
10494 if (type_die == NULL)
10495 return NULL;
10496 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10497 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10498 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10499 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10500 add_loc_descr (&op0, cvt);
10501 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10502 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10503 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10504 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10505 add_loc_descr (&op1, cvt);
10506 return compare_loc_descriptor (op, op0, op1);
10509 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
10510 /* For eq/ne, if the operands are known to be zero-extended,
10511 there is no need to do the fancy shifting up. */
10512 if (op == DW_OP_eq || op == DW_OP_ne)
10514 dw_loc_descr_ref last0, last1;
10515 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
10517 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
10519 /* deref_size zero extends, and for constants we can check
10520 whether they are zero extended or not. */
10521 if (((last0->dw_loc_opc == DW_OP_deref_size
10522 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
10523 || (CONST_INT_P (XEXP (rtl, 0))
10524 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
10525 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
10526 && ((last1->dw_loc_opc == DW_OP_deref_size
10527 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
10528 || (CONST_INT_P (XEXP (rtl, 1))
10529 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
10530 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
10531 return compare_loc_descriptor (op, op0, op1);
10533 /* EQ/NE comparison against constant in narrower type than
10534 DWARF2_ADDR_SIZE can be performed either as
10535 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
10536 DW_OP_{eq,ne}
10538 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
10539 DW_OP_{eq,ne}. Pick whatever is shorter. */
10540 if (CONST_INT_P (XEXP (rtl, 1))
10541 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
10542 && (size_of_int_loc_descriptor (shift) + 1
10543 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
10544 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
10545 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
10546 & GET_MODE_MASK (op_mode))))
10548 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
10549 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
10550 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
10551 & GET_MODE_MASK (op_mode));
10552 return compare_loc_descriptor (op, op0, op1);
10555 add_loc_descr (&op0, int_loc_descriptor (shift));
10556 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
10557 if (CONST_INT_P (XEXP (rtl, 1)))
10558 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
10559 else
10561 add_loc_descr (&op1, int_loc_descriptor (shift));
10562 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
10564 return compare_loc_descriptor (op, op0, op1);
10567 /* Return location descriptor for unsigned comparison OP RTL. */
10569 static dw_loc_descr_ref
10570 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
10571 enum machine_mode mem_mode)
10573 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
10574 dw_loc_descr_ref op0, op1;
10576 if (op_mode == VOIDmode)
10577 op_mode = GET_MODE (XEXP (rtl, 1));
10578 if (op_mode == VOIDmode)
10579 return NULL;
10580 if (GET_MODE_CLASS (op_mode) != MODE_INT)
10581 return NULL;
10583 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
10584 return NULL;
10586 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
10587 VAR_INIT_STATUS_INITIALIZED);
10588 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
10589 VAR_INIT_STATUS_INITIALIZED);
10591 if (op0 == NULL || op1 == NULL)
10592 return NULL;
10594 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
10596 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
10597 dw_loc_descr_ref last0, last1;
10598 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
10600 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
10602 if (CONST_INT_P (XEXP (rtl, 0)))
10603 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
10604 /* deref_size zero extends, so no need to mask it again. */
10605 else if (last0->dw_loc_opc != DW_OP_deref_size
10606 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
10608 add_loc_descr (&op0, int_loc_descriptor (mask));
10609 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
10611 if (CONST_INT_P (XEXP (rtl, 1)))
10612 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
10613 /* deref_size zero extends, so no need to mask it again. */
10614 else if (last1->dw_loc_opc != DW_OP_deref_size
10615 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
10617 add_loc_descr (&op1, int_loc_descriptor (mask));
10618 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
10621 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
10623 HOST_WIDE_INT bias = 1;
10624 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
10625 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
10626 if (CONST_INT_P (XEXP (rtl, 1)))
10627 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
10628 + INTVAL (XEXP (rtl, 1)));
10629 else
10630 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
10631 bias, 0));
10633 return compare_loc_descriptor (op, op0, op1);
10636 /* Return location descriptor for {U,S}{MIN,MAX}. */
10638 static dw_loc_descr_ref
10639 minmax_loc_descriptor (rtx rtl, enum machine_mode mode,
10640 enum machine_mode mem_mode)
10642 enum dwarf_location_atom op;
10643 dw_loc_descr_ref op0, op1, ret;
10644 dw_loc_descr_ref bra_node, drop_node;
10646 if (dwarf_strict
10647 && (GET_MODE_CLASS (mode) != MODE_INT
10648 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
10649 return NULL;
10651 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
10652 VAR_INIT_STATUS_INITIALIZED);
10653 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
10654 VAR_INIT_STATUS_INITIALIZED);
10656 if (op0 == NULL || op1 == NULL)
10657 return NULL;
10659 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
10660 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
10661 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
10662 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
10664 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
10666 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
10667 add_loc_descr (&op0, int_loc_descriptor (mask));
10668 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
10669 add_loc_descr (&op1, int_loc_descriptor (mask));
10670 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
10672 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
10674 HOST_WIDE_INT bias = 1;
10675 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
10676 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
10677 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
10680 else if (GET_MODE_CLASS (mode) == MODE_INT
10681 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
10683 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
10684 add_loc_descr (&op0, int_loc_descriptor (shift));
10685 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
10686 add_loc_descr (&op1, int_loc_descriptor (shift));
10687 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
10689 else if (GET_MODE_CLASS (mode) == MODE_INT
10690 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
10692 dw_die_ref type_die = base_type_for_mode (mode, 0);
10693 dw_loc_descr_ref cvt;
10694 if (type_die == NULL)
10695 return NULL;
10696 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10697 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10698 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10699 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10700 add_loc_descr (&op0, cvt);
10701 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10702 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10703 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10704 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10705 add_loc_descr (&op1, cvt);
10708 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
10709 op = DW_OP_lt;
10710 else
10711 op = DW_OP_gt;
10712 ret = op0;
10713 add_loc_descr (&ret, op1);
10714 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
10715 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
10716 add_loc_descr (&ret, bra_node);
10717 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
10718 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
10719 add_loc_descr (&ret, drop_node);
10720 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
10721 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
10722 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
10723 && GET_MODE_CLASS (mode) == MODE_INT
10724 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
10725 ret = convert_descriptor_to_mode (mode, ret);
10726 return ret;
10729 /* Helper function for mem_loc_descriptor. Perform OP binary op,
10730 but after converting arguments to type_die, afterwards
10731 convert back to unsigned. */
10733 static dw_loc_descr_ref
10734 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
10735 enum machine_mode mode, enum machine_mode mem_mode)
10737 dw_loc_descr_ref cvt, op0, op1;
10739 if (type_die == NULL)
10740 return NULL;
10741 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
10742 VAR_INIT_STATUS_INITIALIZED);
10743 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
10744 VAR_INIT_STATUS_INITIALIZED);
10745 if (op0 == NULL || op1 == NULL)
10746 return NULL;
10747 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10748 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10749 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10750 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10751 add_loc_descr (&op0, cvt);
10752 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10753 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10754 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10755 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10756 add_loc_descr (&op1, cvt);
10757 add_loc_descr (&op0, op1);
10758 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
10759 return convert_descriptor_to_mode (mode, op0);
10762 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
10763 const0 is DW_OP_lit0 or corresponding typed constant,
10764 const1 is DW_OP_lit1 or corresponding typed constant
10765 and constMSB is constant with just the MSB bit set
10766 for the mode):
10767 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
10768 L1: const0 DW_OP_swap
10769 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
10770 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
10771 L3: DW_OP_drop
10772 L4: DW_OP_nop
10774 CTZ is similar:
10775 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
10776 L1: const0 DW_OP_swap
10777 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
10778 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
10779 L3: DW_OP_drop
10780 L4: DW_OP_nop
10782 FFS is similar:
10783 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
10784 L1: const1 DW_OP_swap
10785 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
10786 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
10787 L3: DW_OP_drop
10788 L4: DW_OP_nop */
10790 static dw_loc_descr_ref
10791 clz_loc_descriptor (rtx rtl, enum machine_mode mode,
10792 enum machine_mode mem_mode)
10794 dw_loc_descr_ref op0, ret, tmp;
10795 HOST_WIDE_INT valv;
10796 dw_loc_descr_ref l1jump, l1label;
10797 dw_loc_descr_ref l2jump, l2label;
10798 dw_loc_descr_ref l3jump, l3label;
10799 dw_loc_descr_ref l4jump, l4label;
10800 rtx msb;
10802 if (GET_MODE_CLASS (mode) != MODE_INT
10803 || GET_MODE (XEXP (rtl, 0)) != mode
10804 || (GET_CODE (rtl) == CLZ
10805 && GET_MODE_BITSIZE (mode) > HOST_BITS_PER_DOUBLE_INT))
10806 return NULL;
10808 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
10809 VAR_INIT_STATUS_INITIALIZED);
10810 if (op0 == NULL)
10811 return NULL;
10812 ret = op0;
10813 if (GET_CODE (rtl) == CLZ)
10815 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
10816 valv = GET_MODE_BITSIZE (mode);
10818 else if (GET_CODE (rtl) == FFS)
10819 valv = 0;
10820 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
10821 valv = GET_MODE_BITSIZE (mode);
10822 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
10823 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
10824 add_loc_descr (&ret, l1jump);
10825 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
10826 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
10827 VAR_INIT_STATUS_INITIALIZED);
10828 if (tmp == NULL)
10829 return NULL;
10830 add_loc_descr (&ret, tmp);
10831 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
10832 add_loc_descr (&ret, l4jump);
10833 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
10834 ? const1_rtx : const0_rtx,
10835 mode, mem_mode,
10836 VAR_INIT_STATUS_INITIALIZED);
10837 if (l1label == NULL)
10838 return NULL;
10839 add_loc_descr (&ret, l1label);
10840 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
10841 l2label = new_loc_descr (DW_OP_dup, 0, 0);
10842 add_loc_descr (&ret, l2label);
10843 if (GET_CODE (rtl) != CLZ)
10844 msb = const1_rtx;
10845 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10846 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
10847 << (GET_MODE_BITSIZE (mode) - 1));
10848 else
10849 msb = immed_double_const (0, (unsigned HOST_WIDE_INT) 1
10850 << (GET_MODE_BITSIZE (mode)
10851 - HOST_BITS_PER_WIDE_INT - 1), mode);
10852 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
10853 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
10854 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
10855 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
10856 else
10857 tmp = mem_loc_descriptor (msb, mode, mem_mode,
10858 VAR_INIT_STATUS_INITIALIZED);
10859 if (tmp == NULL)
10860 return NULL;
10861 add_loc_descr (&ret, tmp);
10862 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
10863 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
10864 add_loc_descr (&ret, l3jump);
10865 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
10866 VAR_INIT_STATUS_INITIALIZED);
10867 if (tmp == NULL)
10868 return NULL;
10869 add_loc_descr (&ret, tmp);
10870 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
10871 ? DW_OP_shl : DW_OP_shr, 0, 0));
10872 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
10873 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
10874 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
10875 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
10876 add_loc_descr (&ret, l2jump);
10877 l3label = new_loc_descr (DW_OP_drop, 0, 0);
10878 add_loc_descr (&ret, l3label);
10879 l4label = new_loc_descr (DW_OP_nop, 0, 0);
10880 add_loc_descr (&ret, l4label);
10881 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
10882 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
10883 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
10884 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
10885 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
10886 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
10887 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
10888 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
10889 return ret;
10892 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
10893 const1 is DW_OP_lit1 or corresponding typed constant):
10894 const0 DW_OP_swap
10895 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
10896 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
10897 L2: DW_OP_drop
10899 PARITY is similar:
10900 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
10901 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
10902 L2: DW_OP_drop */
10904 static dw_loc_descr_ref
10905 popcount_loc_descriptor (rtx rtl, enum machine_mode mode,
10906 enum machine_mode mem_mode)
10908 dw_loc_descr_ref op0, ret, tmp;
10909 dw_loc_descr_ref l1jump, l1label;
10910 dw_loc_descr_ref l2jump, l2label;
10912 if (GET_MODE_CLASS (mode) != MODE_INT
10913 || GET_MODE (XEXP (rtl, 0)) != mode)
10914 return NULL;
10916 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
10917 VAR_INIT_STATUS_INITIALIZED);
10918 if (op0 == NULL)
10919 return NULL;
10920 ret = op0;
10921 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
10922 VAR_INIT_STATUS_INITIALIZED);
10923 if (tmp == NULL)
10924 return NULL;
10925 add_loc_descr (&ret, tmp);
10926 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
10927 l1label = new_loc_descr (DW_OP_dup, 0, 0);
10928 add_loc_descr (&ret, l1label);
10929 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
10930 add_loc_descr (&ret, l2jump);
10931 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
10932 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
10933 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
10934 VAR_INIT_STATUS_INITIALIZED);
10935 if (tmp == NULL)
10936 return NULL;
10937 add_loc_descr (&ret, tmp);
10938 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
10939 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
10940 ? DW_OP_plus : DW_OP_xor, 0, 0));
10941 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
10942 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
10943 VAR_INIT_STATUS_INITIALIZED);
10944 add_loc_descr (&ret, tmp);
10945 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
10946 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
10947 add_loc_descr (&ret, l1jump);
10948 l2label = new_loc_descr (DW_OP_drop, 0, 0);
10949 add_loc_descr (&ret, l2label);
10950 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
10951 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
10952 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
10953 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
10954 return ret;
10957 /* BSWAP (constS is initial shift count, either 56 or 24):
10958 constS const0
10959 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
10960 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
10961 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
10962 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
10963 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
10965 static dw_loc_descr_ref
10966 bswap_loc_descriptor (rtx rtl, enum machine_mode mode,
10967 enum machine_mode mem_mode)
10969 dw_loc_descr_ref op0, ret, tmp;
10970 dw_loc_descr_ref l1jump, l1label;
10971 dw_loc_descr_ref l2jump, l2label;
10973 if (GET_MODE_CLASS (mode) != MODE_INT
10974 || BITS_PER_UNIT != 8
10975 || (GET_MODE_BITSIZE (mode) != 32
10976 && GET_MODE_BITSIZE (mode) != 64))
10977 return NULL;
10979 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
10980 VAR_INIT_STATUS_INITIALIZED);
10981 if (op0 == NULL)
10982 return NULL;
10984 ret = op0;
10985 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
10986 mode, mem_mode,
10987 VAR_INIT_STATUS_INITIALIZED);
10988 if (tmp == NULL)
10989 return NULL;
10990 add_loc_descr (&ret, tmp);
10991 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
10992 VAR_INIT_STATUS_INITIALIZED);
10993 if (tmp == NULL)
10994 return NULL;
10995 add_loc_descr (&ret, tmp);
10996 l1label = new_loc_descr (DW_OP_pick, 2, 0);
10997 add_loc_descr (&ret, l1label);
10998 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
10999 mode, mem_mode,
11000 VAR_INIT_STATUS_INITIALIZED);
11001 add_loc_descr (&ret, tmp);
11002 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
11003 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11004 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11005 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
11006 VAR_INIT_STATUS_INITIALIZED);
11007 if (tmp == NULL)
11008 return NULL;
11009 add_loc_descr (&ret, tmp);
11010 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11011 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
11012 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11013 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11014 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11015 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11016 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11017 VAR_INIT_STATUS_INITIALIZED);
11018 add_loc_descr (&ret, tmp);
11019 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
11020 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11021 add_loc_descr (&ret, l2jump);
11022 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
11023 VAR_INIT_STATUS_INITIALIZED);
11024 add_loc_descr (&ret, tmp);
11025 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11026 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11027 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11028 add_loc_descr (&ret, l1jump);
11029 l2label = new_loc_descr (DW_OP_drop, 0, 0);
11030 add_loc_descr (&ret, l2label);
11031 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11032 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11033 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11034 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11035 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11036 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11037 return ret;
11040 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
11041 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11042 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
11043 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
11045 ROTATERT is similar:
11046 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
11047 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11048 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
11050 static dw_loc_descr_ref
11051 rotate_loc_descriptor (rtx rtl, enum machine_mode mode,
11052 enum machine_mode mem_mode)
11054 rtx rtlop1 = XEXP (rtl, 1);
11055 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
11056 int i;
11058 if (GET_MODE_CLASS (mode) != MODE_INT)
11059 return NULL;
11061 if (GET_MODE (rtlop1) != VOIDmode
11062 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
11063 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
11064 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11065 VAR_INIT_STATUS_INITIALIZED);
11066 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
11067 VAR_INIT_STATUS_INITIALIZED);
11068 if (op0 == NULL || op1 == NULL)
11069 return NULL;
11070 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11071 for (i = 0; i < 2; i++)
11073 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
11074 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
11075 mode, mem_mode,
11076 VAR_INIT_STATUS_INITIALIZED);
11077 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
11078 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11079 ? DW_OP_const4u
11080 : HOST_BITS_PER_WIDE_INT == 64
11081 ? DW_OP_const8u : DW_OP_constu,
11082 GET_MODE_MASK (mode), 0);
11083 else
11084 mask[i] = NULL;
11085 if (mask[i] == NULL)
11086 return NULL;
11087 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
11089 ret = op0;
11090 add_loc_descr (&ret, op1);
11091 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11092 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11093 if (GET_CODE (rtl) == ROTATERT)
11095 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11096 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11097 GET_MODE_BITSIZE (mode), 0));
11099 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11100 if (mask[0] != NULL)
11101 add_loc_descr (&ret, mask[0]);
11102 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11103 if (mask[1] != NULL)
11105 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11106 add_loc_descr (&ret, mask[1]);
11107 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11109 if (GET_CODE (rtl) == ROTATE)
11111 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11112 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11113 GET_MODE_BITSIZE (mode), 0));
11115 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11116 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11117 return ret;
11120 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
11121 for DEBUG_PARAMETER_REF RTL. */
11123 static dw_loc_descr_ref
11124 parameter_ref_descriptor (rtx rtl)
11126 dw_loc_descr_ref ret;
11127 dw_die_ref ref;
11129 if (dwarf_strict)
11130 return NULL;
11131 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
11132 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
11133 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
11134 if (ref)
11136 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11137 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
11138 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
11140 else
11142 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
11143 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
11145 return ret;
11148 /* The following routine converts the RTL for a variable or parameter
11149 (resident in memory) into an equivalent Dwarf representation of a
11150 mechanism for getting the address of that same variable onto the top of a
11151 hypothetical "address evaluation" stack.
11153 When creating memory location descriptors, we are effectively transforming
11154 the RTL for a memory-resident object into its Dwarf postfix expression
11155 equivalent. This routine recursively descends an RTL tree, turning
11156 it into Dwarf postfix code as it goes.
11158 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
11160 MEM_MODE is the mode of the memory reference, needed to handle some
11161 autoincrement addressing modes.
11163 Return 0 if we can't represent the location. */
11165 dw_loc_descr_ref
11166 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
11167 enum machine_mode mem_mode,
11168 enum var_init_status initialized)
11170 dw_loc_descr_ref mem_loc_result = NULL;
11171 enum dwarf_location_atom op;
11172 dw_loc_descr_ref op0, op1;
11174 if (mode == VOIDmode)
11175 mode = GET_MODE (rtl);
11177 /* Note that for a dynamically sized array, the location we will generate a
11178 description of here will be the lowest numbered location which is
11179 actually within the array. That's *not* necessarily the same as the
11180 zeroth element of the array. */
11182 rtl = targetm.delegitimize_address (rtl);
11184 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
11185 return NULL;
11187 switch (GET_CODE (rtl))
11189 case POST_INC:
11190 case POST_DEC:
11191 case POST_MODIFY:
11192 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
11194 case SUBREG:
11195 /* The case of a subreg may arise when we have a local (register)
11196 variable or a formal (register) parameter which doesn't quite fill
11197 up an entire register. For now, just assume that it is
11198 legitimate to make the Dwarf info refer to the whole register which
11199 contains the given subreg. */
11200 if (!subreg_lowpart_p (rtl))
11201 break;
11202 if (GET_MODE_CLASS (mode) == MODE_INT
11203 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (rtl))) == MODE_INT
11204 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11205 #ifdef POINTERS_EXTEND_UNSIGNED
11206 || (mode == Pmode && mem_mode != VOIDmode)
11207 #endif
11209 && GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))) <= DWARF2_ADDR_SIZE)
11211 mem_loc_result = mem_loc_descriptor (SUBREG_REG (rtl),
11212 GET_MODE (SUBREG_REG (rtl)),
11213 mem_mode, initialized);
11214 break;
11216 if (dwarf_strict)
11217 break;
11218 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))))
11219 break;
11220 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl)))
11221 && (GET_MODE_CLASS (mode) != MODE_INT
11222 || GET_MODE_CLASS (GET_MODE (SUBREG_REG (rtl))) != MODE_INT))
11223 break;
11224 else
11226 dw_die_ref type_die;
11227 dw_loc_descr_ref cvt;
11229 mem_loc_result = mem_loc_descriptor (SUBREG_REG (rtl),
11230 GET_MODE (SUBREG_REG (rtl)),
11231 mem_mode, initialized);
11232 if (mem_loc_result == NULL)
11233 break;
11234 type_die = base_type_for_mode (mode,
11235 GET_MODE_CLASS (mode) == MODE_INT);
11236 if (type_die == NULL)
11238 mem_loc_result = NULL;
11239 break;
11241 if (GET_MODE_SIZE (mode)
11242 != GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))))
11243 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11244 else
11245 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
11246 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11247 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11248 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11249 add_loc_descr (&mem_loc_result, cvt);
11251 break;
11253 case REG:
11254 if (GET_MODE_CLASS (mode) != MODE_INT
11255 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
11256 && rtl != arg_pointer_rtx
11257 && rtl != frame_pointer_rtx
11258 #ifdef POINTERS_EXTEND_UNSIGNED
11259 && (mode != Pmode || mem_mode == VOIDmode)
11260 #endif
11263 dw_die_ref type_die;
11265 if (dwarf_strict)
11266 break;
11267 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
11268 break;
11269 type_die = base_type_for_mode (mode,
11270 GET_MODE_CLASS (mode) == MODE_INT);
11271 if (type_die == NULL)
11272 break;
11273 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
11274 dbx_reg_number (rtl), 0);
11275 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
11276 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
11277 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
11278 break;
11280 /* Whenever a register number forms a part of the description of the
11281 method for calculating the (dynamic) address of a memory resident
11282 object, DWARF rules require the register number be referred to as
11283 a "base register". This distinction is not based in any way upon
11284 what category of register the hardware believes the given register
11285 belongs to. This is strictly DWARF terminology we're dealing with
11286 here. Note that in cases where the location of a memory-resident
11287 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
11288 OP_CONST (0)) the actual DWARF location descriptor that we generate
11289 may just be OP_BASEREG (basereg). This may look deceptively like
11290 the object in question was allocated to a register (rather than in
11291 memory) so DWARF consumers need to be aware of the subtle
11292 distinction between OP_REG and OP_BASEREG. */
11293 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
11294 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
11295 else if (stack_realign_drap
11296 && crtl->drap_reg
11297 && crtl->args.internal_arg_pointer == rtl
11298 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
11300 /* If RTL is internal_arg_pointer, which has been optimized
11301 out, use DRAP instead. */
11302 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
11303 VAR_INIT_STATUS_INITIALIZED);
11305 break;
11307 case SIGN_EXTEND:
11308 case ZERO_EXTEND:
11309 if (GET_MODE_CLASS (mode) != MODE_INT)
11310 break;
11311 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
11312 mem_mode, VAR_INIT_STATUS_INITIALIZED);
11313 if (op0 == 0)
11314 break;
11315 else if (GET_CODE (rtl) == ZERO_EXTEND
11316 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11317 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
11318 < HOST_BITS_PER_WIDE_INT
11319 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
11320 to expand zero extend as two shifts instead of
11321 masking. */
11322 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
11324 enum machine_mode imode = GET_MODE (XEXP (rtl, 0));
11325 mem_loc_result = op0;
11326 add_loc_descr (&mem_loc_result,
11327 int_loc_descriptor (GET_MODE_MASK (imode)));
11328 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
11330 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11332 int shift = DWARF2_ADDR_SIZE
11333 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
11334 shift *= BITS_PER_UNIT;
11335 if (GET_CODE (rtl) == SIGN_EXTEND)
11336 op = DW_OP_shra;
11337 else
11338 op = DW_OP_shr;
11339 mem_loc_result = op0;
11340 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
11341 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
11342 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
11343 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11345 else if (!dwarf_strict)
11347 dw_die_ref type_die1, type_die2;
11348 dw_loc_descr_ref cvt;
11350 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
11351 GET_CODE (rtl) == ZERO_EXTEND);
11352 if (type_die1 == NULL)
11353 break;
11354 type_die2 = base_type_for_mode (mode, 1);
11355 if (type_die2 == NULL)
11356 break;
11357 mem_loc_result = op0;
11358 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11359 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11360 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
11361 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11362 add_loc_descr (&mem_loc_result, cvt);
11363 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11364 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11365 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
11366 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11367 add_loc_descr (&mem_loc_result, cvt);
11369 break;
11371 case MEM:
11373 rtx new_rtl = avoid_constant_pool_reference (rtl);
11374 if (new_rtl != rtl)
11376 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
11377 initialized);
11378 if (mem_loc_result != NULL)
11379 return mem_loc_result;
11382 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
11383 get_address_mode (rtl), mode,
11384 VAR_INIT_STATUS_INITIALIZED);
11385 if (mem_loc_result == NULL)
11386 mem_loc_result = tls_mem_loc_descriptor (rtl);
11387 if (mem_loc_result != NULL)
11389 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
11390 || GET_MODE_CLASS (mode) != MODE_INT)
11392 dw_die_ref type_die;
11393 dw_loc_descr_ref deref;
11395 if (dwarf_strict)
11396 return NULL;
11397 type_die
11398 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
11399 if (type_die == NULL)
11400 return NULL;
11401 deref = new_loc_descr (DW_OP_GNU_deref_type,
11402 GET_MODE_SIZE (mode), 0);
11403 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
11404 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
11405 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
11406 add_loc_descr (&mem_loc_result, deref);
11408 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11409 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
11410 else
11411 add_loc_descr (&mem_loc_result,
11412 new_loc_descr (DW_OP_deref_size,
11413 GET_MODE_SIZE (mode), 0));
11415 break;
11417 case LO_SUM:
11418 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
11420 case LABEL_REF:
11421 /* Some ports can transform a symbol ref into a label ref, because
11422 the symbol ref is too far away and has to be dumped into a constant
11423 pool. */
11424 case CONST:
11425 case SYMBOL_REF:
11426 if (GET_MODE_CLASS (mode) != MODE_INT
11427 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
11428 #ifdef POINTERS_EXTEND_UNSIGNED
11429 && (mode != Pmode || mem_mode == VOIDmode)
11430 #endif
11432 break;
11433 if (GET_CODE (rtl) == SYMBOL_REF
11434 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11436 dw_loc_descr_ref temp;
11438 /* If this is not defined, we have no way to emit the data. */
11439 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
11440 break;
11442 /* We used to emit DW_OP_addr here, but that's wrong, since
11443 DW_OP_addr should be relocated by the debug info consumer,
11444 while DW_OP_GNU_push_tls_address operand should not. */
11445 temp = new_loc_descr (DWARF2_ADDR_SIZE == 4
11446 ? DW_OP_const4u : DW_OP_const8u, 0, 0);
11447 temp->dw_loc_oprnd1.val_class = dw_val_class_addr;
11448 temp->dw_loc_oprnd1.v.val_addr = rtl;
11449 temp->dtprel = true;
11451 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
11452 add_loc_descr (&mem_loc_result, temp);
11454 break;
11457 if (!const_ok_for_output (rtl))
11458 break;
11460 symref:
11461 mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
11462 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
11463 mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
11464 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
11465 break;
11467 case CONCAT:
11468 case CONCATN:
11469 case VAR_LOCATION:
11470 case DEBUG_IMPLICIT_PTR:
11471 expansion_failed (NULL_TREE, rtl,
11472 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
11473 return 0;
11475 case ENTRY_VALUE:
11476 if (dwarf_strict)
11477 return NULL;
11478 if (REG_P (ENTRY_VALUE_EXP (rtl)))
11480 if (GET_MODE_CLASS (mode) != MODE_INT
11481 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11482 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
11483 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
11484 else
11486 = one_reg_loc_descriptor (dbx_reg_number (ENTRY_VALUE_EXP (rtl)),
11487 VAR_INIT_STATUS_INITIALIZED);
11489 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
11490 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
11492 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
11493 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
11494 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
11495 return NULL;
11497 else
11498 gcc_unreachable ();
11499 if (op0 == NULL)
11500 return NULL;
11501 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
11502 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
11503 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
11504 break;
11506 case DEBUG_PARAMETER_REF:
11507 mem_loc_result = parameter_ref_descriptor (rtl);
11508 break;
11510 case PRE_MODIFY:
11511 /* Extract the PLUS expression nested inside and fall into
11512 PLUS code below. */
11513 rtl = XEXP (rtl, 1);
11514 goto plus;
11516 case PRE_INC:
11517 case PRE_DEC:
11518 /* Turn these into a PLUS expression and fall into the PLUS code
11519 below. */
11520 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
11521 GEN_INT (GET_CODE (rtl) == PRE_INC
11522 ? GET_MODE_UNIT_SIZE (mem_mode)
11523 : -GET_MODE_UNIT_SIZE (mem_mode)));
11525 /* ... fall through ... */
11527 case PLUS:
11528 plus:
11529 if (is_based_loc (rtl)
11530 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11531 || XEXP (rtl, 0) == arg_pointer_rtx
11532 || XEXP (rtl, 0) == frame_pointer_rtx)
11533 && GET_MODE_CLASS (mode) == MODE_INT)
11534 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
11535 INTVAL (XEXP (rtl, 1)),
11536 VAR_INIT_STATUS_INITIALIZED);
11537 else
11539 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11540 VAR_INIT_STATUS_INITIALIZED);
11541 if (mem_loc_result == 0)
11542 break;
11544 if (CONST_INT_P (XEXP (rtl, 1))
11545 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11546 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
11547 else
11549 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11550 VAR_INIT_STATUS_INITIALIZED);
11551 if (op1 == 0)
11552 break;
11553 add_loc_descr (&mem_loc_result, op1);
11554 add_loc_descr (&mem_loc_result,
11555 new_loc_descr (DW_OP_plus, 0, 0));
11558 break;
11560 /* If a pseudo-reg is optimized away, it is possible for it to
11561 be replaced with a MEM containing a multiply or shift. */
11562 case MINUS:
11563 op = DW_OP_minus;
11564 goto do_binop;
11566 case MULT:
11567 op = DW_OP_mul;
11568 goto do_binop;
11570 case DIV:
11571 if (!dwarf_strict
11572 && GET_MODE_CLASS (mode) == MODE_INT
11573 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11575 mem_loc_result = typed_binop (DW_OP_div, rtl,
11576 base_type_for_mode (mode, 0),
11577 mode, mem_mode);
11578 break;
11580 op = DW_OP_div;
11581 goto do_binop;
11583 case UMOD:
11584 op = DW_OP_mod;
11585 goto do_binop;
11587 case ASHIFT:
11588 op = DW_OP_shl;
11589 goto do_shift;
11591 case ASHIFTRT:
11592 op = DW_OP_shra;
11593 goto do_shift;
11595 case LSHIFTRT:
11596 op = DW_OP_shr;
11597 goto do_shift;
11599 do_shift:
11600 if (GET_MODE_CLASS (mode) != MODE_INT)
11601 break;
11602 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11603 VAR_INIT_STATUS_INITIALIZED);
11605 rtx rtlop1 = XEXP (rtl, 1);
11606 if (GET_MODE (rtlop1) != VOIDmode
11607 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
11608 < GET_MODE_BITSIZE (mode))
11609 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
11610 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
11611 VAR_INIT_STATUS_INITIALIZED);
11614 if (op0 == 0 || op1 == 0)
11615 break;
11617 mem_loc_result = op0;
11618 add_loc_descr (&mem_loc_result, op1);
11619 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11620 break;
11622 case AND:
11623 op = DW_OP_and;
11624 goto do_binop;
11626 case IOR:
11627 op = DW_OP_or;
11628 goto do_binop;
11630 case XOR:
11631 op = DW_OP_xor;
11632 goto do_binop;
11634 do_binop:
11635 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11636 VAR_INIT_STATUS_INITIALIZED);
11637 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11638 VAR_INIT_STATUS_INITIALIZED);
11640 if (op0 == 0 || op1 == 0)
11641 break;
11643 mem_loc_result = op0;
11644 add_loc_descr (&mem_loc_result, op1);
11645 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11646 break;
11648 case MOD:
11649 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
11651 mem_loc_result = typed_binop (DW_OP_mod, rtl,
11652 base_type_for_mode (mode, 0),
11653 mode, mem_mode);
11654 break;
11657 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11658 VAR_INIT_STATUS_INITIALIZED);
11659 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11660 VAR_INIT_STATUS_INITIALIZED);
11662 if (op0 == 0 || op1 == 0)
11663 break;
11665 mem_loc_result = op0;
11666 add_loc_descr (&mem_loc_result, op1);
11667 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
11668 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
11669 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
11670 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
11671 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
11672 break;
11674 case UDIV:
11675 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
11677 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
11679 op = DW_OP_div;
11680 goto do_binop;
11682 mem_loc_result = typed_binop (DW_OP_div, rtl,
11683 base_type_for_mode (mode, 1),
11684 mode, mem_mode);
11686 break;
11688 case NOT:
11689 op = DW_OP_not;
11690 goto do_unop;
11692 case ABS:
11693 op = DW_OP_abs;
11694 goto do_unop;
11696 case NEG:
11697 op = DW_OP_neg;
11698 goto do_unop;
11700 do_unop:
11701 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11702 VAR_INIT_STATUS_INITIALIZED);
11704 if (op0 == 0)
11705 break;
11707 mem_loc_result = op0;
11708 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11709 break;
11711 case CONST_INT:
11712 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11713 #ifdef POINTERS_EXTEND_UNSIGNED
11714 || (mode == Pmode
11715 && mem_mode != VOIDmode
11716 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
11717 #endif
11720 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
11721 break;
11723 if (!dwarf_strict
11724 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
11725 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
11727 dw_die_ref type_die = base_type_for_mode (mode, 1);
11728 enum machine_mode amode;
11729 if (type_die == NULL)
11730 return NULL;
11731 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
11732 MODE_INT, 0);
11733 if (INTVAL (rtl) >= 0
11734 && amode != BLKmode
11735 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
11736 /* const DW_OP_GNU_convert <XXX> vs.
11737 DW_OP_GNU_const_type <XXX, 1, const>. */
11738 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
11739 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
11741 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
11742 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11743 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11744 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11745 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
11746 add_loc_descr (&mem_loc_result, op0);
11747 return mem_loc_result;
11749 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
11750 INTVAL (rtl));
11751 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11752 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11753 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
11754 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
11755 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11756 else
11758 mem_loc_result->dw_loc_oprnd2.val_class
11759 = dw_val_class_const_double;
11760 mem_loc_result->dw_loc_oprnd2.v.val_double
11761 = double_int::from_shwi (INTVAL (rtl));
11764 break;
11766 case CONST_DOUBLE:
11767 if (!dwarf_strict)
11769 dw_die_ref type_die;
11771 /* Note that a CONST_DOUBLE rtx could represent either an integer
11772 or a floating-point constant. A CONST_DOUBLE is used whenever
11773 the constant requires more than one word in order to be
11774 adequately represented. We output CONST_DOUBLEs as blocks. */
11775 if (mode == VOIDmode
11776 || (GET_MODE (rtl) == VOIDmode
11777 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
11778 break;
11779 type_die = base_type_for_mode (mode,
11780 GET_MODE_CLASS (mode) == MODE_INT);
11781 if (type_die == NULL)
11782 return NULL;
11783 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
11784 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11785 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11786 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
11787 if (SCALAR_FLOAT_MODE_P (mode))
11789 unsigned int length = GET_MODE_SIZE (mode);
11790 unsigned char *array
11791 = (unsigned char*) ggc_alloc_atomic (length);
11793 insert_float (rtl, array);
11794 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
11795 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
11796 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
11797 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
11799 else
11801 mem_loc_result->dw_loc_oprnd2.val_class
11802 = dw_val_class_const_double;
11803 mem_loc_result->dw_loc_oprnd2.v.val_double
11804 = rtx_to_double_int (rtl);
11807 break;
11809 case EQ:
11810 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
11811 break;
11813 case GE:
11814 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
11815 break;
11817 case GT:
11818 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
11819 break;
11821 case LE:
11822 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
11823 break;
11825 case LT:
11826 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
11827 break;
11829 case NE:
11830 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
11831 break;
11833 case GEU:
11834 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
11835 break;
11837 case GTU:
11838 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
11839 break;
11841 case LEU:
11842 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
11843 break;
11845 case LTU:
11846 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
11847 break;
11849 case UMIN:
11850 case UMAX:
11851 if (GET_MODE_CLASS (mode) != MODE_INT)
11852 break;
11853 /* FALLTHRU */
11854 case SMIN:
11855 case SMAX:
11856 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
11857 break;
11859 case ZERO_EXTRACT:
11860 case SIGN_EXTRACT:
11861 if (CONST_INT_P (XEXP (rtl, 1))
11862 && CONST_INT_P (XEXP (rtl, 2))
11863 && ((unsigned) INTVAL (XEXP (rtl, 1))
11864 + (unsigned) INTVAL (XEXP (rtl, 2))
11865 <= GET_MODE_BITSIZE (mode))
11866 && GET_MODE_CLASS (mode) == MODE_INT
11867 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11868 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
11870 int shift, size;
11871 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
11872 mem_mode, VAR_INIT_STATUS_INITIALIZED);
11873 if (op0 == 0)
11874 break;
11875 if (GET_CODE (rtl) == SIGN_EXTRACT)
11876 op = DW_OP_shra;
11877 else
11878 op = DW_OP_shr;
11879 mem_loc_result = op0;
11880 size = INTVAL (XEXP (rtl, 1));
11881 shift = INTVAL (XEXP (rtl, 2));
11882 if (BITS_BIG_ENDIAN)
11883 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
11884 - shift - size;
11885 if (shift + size != (int) DWARF2_ADDR_SIZE)
11887 add_loc_descr (&mem_loc_result,
11888 int_loc_descriptor (DWARF2_ADDR_SIZE
11889 - shift - size));
11890 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
11892 if (size != (int) DWARF2_ADDR_SIZE)
11894 add_loc_descr (&mem_loc_result,
11895 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
11896 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11899 break;
11901 case IF_THEN_ELSE:
11903 dw_loc_descr_ref op2, bra_node, drop_node;
11904 op0 = mem_loc_descriptor (XEXP (rtl, 0),
11905 GET_MODE (XEXP (rtl, 0)) == VOIDmode
11906 ? word_mode : GET_MODE (XEXP (rtl, 0)),
11907 mem_mode, VAR_INIT_STATUS_INITIALIZED);
11908 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11909 VAR_INIT_STATUS_INITIALIZED);
11910 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
11911 VAR_INIT_STATUS_INITIALIZED);
11912 if (op0 == NULL || op1 == NULL || op2 == NULL)
11913 break;
11915 mem_loc_result = op1;
11916 add_loc_descr (&mem_loc_result, op2);
11917 add_loc_descr (&mem_loc_result, op0);
11918 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11919 add_loc_descr (&mem_loc_result, bra_node);
11920 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
11921 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11922 add_loc_descr (&mem_loc_result, drop_node);
11923 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11924 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11926 break;
11928 case FLOAT_EXTEND:
11929 case FLOAT_TRUNCATE:
11930 case FLOAT:
11931 case UNSIGNED_FLOAT:
11932 case FIX:
11933 case UNSIGNED_FIX:
11934 if (!dwarf_strict)
11936 dw_die_ref type_die;
11937 dw_loc_descr_ref cvt;
11939 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
11940 mem_mode, VAR_INIT_STATUS_INITIALIZED);
11941 if (op0 == NULL)
11942 break;
11943 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
11944 && (GET_CODE (rtl) == FLOAT
11945 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
11946 <= DWARF2_ADDR_SIZE))
11948 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
11949 GET_CODE (rtl) == UNSIGNED_FLOAT);
11950 if (type_die == NULL)
11951 break;
11952 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11953 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11954 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11955 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11956 add_loc_descr (&op0, cvt);
11958 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
11959 if (type_die == NULL)
11960 break;
11961 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11962 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11963 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11964 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11965 add_loc_descr (&op0, cvt);
11966 if (GET_MODE_CLASS (mode) == MODE_INT
11967 && (GET_CODE (rtl) == FIX
11968 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
11970 op0 = convert_descriptor_to_mode (mode, op0);
11971 if (op0 == NULL)
11972 break;
11974 mem_loc_result = op0;
11976 break;
11978 case CLZ:
11979 case CTZ:
11980 case FFS:
11981 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
11982 break;
11984 case POPCOUNT:
11985 case PARITY:
11986 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
11987 break;
11989 case BSWAP:
11990 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
11991 break;
11993 case ROTATE:
11994 case ROTATERT:
11995 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
11996 break;
11998 case COMPARE:
11999 case TRUNCATE:
12000 /* In theory, we could implement the above. */
12001 /* DWARF cannot represent the unsigned compare operations
12002 natively. */
12003 case SS_MULT:
12004 case US_MULT:
12005 case SS_DIV:
12006 case US_DIV:
12007 case SS_PLUS:
12008 case US_PLUS:
12009 case SS_MINUS:
12010 case US_MINUS:
12011 case SS_NEG:
12012 case US_NEG:
12013 case SS_ABS:
12014 case SS_ASHIFT:
12015 case US_ASHIFT:
12016 case SS_TRUNCATE:
12017 case US_TRUNCATE:
12018 case UNORDERED:
12019 case ORDERED:
12020 case UNEQ:
12021 case UNGE:
12022 case UNGT:
12023 case UNLE:
12024 case UNLT:
12025 case LTGT:
12026 case FRACT_CONVERT:
12027 case UNSIGNED_FRACT_CONVERT:
12028 case SAT_FRACT:
12029 case UNSIGNED_SAT_FRACT:
12030 case SQRT:
12031 case ASM_OPERANDS:
12032 case VEC_MERGE:
12033 case VEC_SELECT:
12034 case VEC_CONCAT:
12035 case VEC_DUPLICATE:
12036 case UNSPEC:
12037 case HIGH:
12038 case FMA:
12039 case STRICT_LOW_PART:
12040 case CONST_VECTOR:
12041 case CONST_FIXED:
12042 case CLRSB:
12043 /* If delegitimize_address couldn't do anything with the UNSPEC, we
12044 can't express it in the debug info. This can happen e.g. with some
12045 TLS UNSPECs. */
12046 break;
12048 case CONST_STRING:
12049 resolve_one_addr (&rtl, NULL);
12050 goto symref;
12052 default:
12053 #ifdef ENABLE_CHECKING
12054 print_rtl (stderr, rtl);
12055 gcc_unreachable ();
12056 #else
12057 break;
12058 #endif
12061 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12062 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12064 return mem_loc_result;
12067 /* Return a descriptor that describes the concatenation of two locations.
12068 This is typically a complex variable. */
12070 static dw_loc_descr_ref
12071 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
12073 dw_loc_descr_ref cc_loc_result = NULL;
12074 dw_loc_descr_ref x0_ref
12075 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12076 dw_loc_descr_ref x1_ref
12077 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12079 if (x0_ref == 0 || x1_ref == 0)
12080 return 0;
12082 cc_loc_result = x0_ref;
12083 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
12085 add_loc_descr (&cc_loc_result, x1_ref);
12086 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
12088 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
12089 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12091 return cc_loc_result;
12094 /* Return a descriptor that describes the concatenation of N
12095 locations. */
12097 static dw_loc_descr_ref
12098 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
12100 unsigned int i;
12101 dw_loc_descr_ref cc_loc_result = NULL;
12102 unsigned int n = XVECLEN (concatn, 0);
12104 for (i = 0; i < n; ++i)
12106 dw_loc_descr_ref ref;
12107 rtx x = XVECEXP (concatn, 0, i);
12109 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12110 if (ref == NULL)
12111 return NULL;
12113 add_loc_descr (&cc_loc_result, ref);
12114 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
12117 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12118 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12120 return cc_loc_result;
12123 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
12124 for DEBUG_IMPLICIT_PTR RTL. */
12126 static dw_loc_descr_ref
12127 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
12129 dw_loc_descr_ref ret;
12130 dw_die_ref ref;
12132 if (dwarf_strict)
12133 return NULL;
12134 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
12135 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
12136 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
12137 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
12138 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
12139 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
12140 if (ref)
12142 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12143 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12144 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12146 else
12148 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12149 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
12151 return ret;
12154 /* Output a proper Dwarf location descriptor for a variable or parameter
12155 which is either allocated in a register or in a memory location. For a
12156 register, we just generate an OP_REG and the register number. For a
12157 memory location we provide a Dwarf postfix expression describing how to
12158 generate the (dynamic) address of the object onto the address stack.
12160 MODE is mode of the decl if this loc_descriptor is going to be used in
12161 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
12162 allowed, VOIDmode otherwise.
12164 If we don't know how to describe it, return 0. */
12166 static dw_loc_descr_ref
12167 loc_descriptor (rtx rtl, enum machine_mode mode,
12168 enum var_init_status initialized)
12170 dw_loc_descr_ref loc_result = NULL;
12172 switch (GET_CODE (rtl))
12174 case SUBREG:
12175 /* The case of a subreg may arise when we have a local (register)
12176 variable or a formal (register) parameter which doesn't quite fill
12177 up an entire register. For now, just assume that it is
12178 legitimate to make the Dwarf info refer to the whole register which
12179 contains the given subreg. */
12180 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
12181 loc_result = loc_descriptor (SUBREG_REG (rtl),
12182 GET_MODE (SUBREG_REG (rtl)), initialized);
12183 else
12184 goto do_default;
12185 break;
12187 case REG:
12188 loc_result = reg_loc_descriptor (rtl, initialized);
12189 break;
12191 case MEM:
12192 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
12193 GET_MODE (rtl), initialized);
12194 if (loc_result == NULL)
12195 loc_result = tls_mem_loc_descriptor (rtl);
12196 if (loc_result == NULL)
12198 rtx new_rtl = avoid_constant_pool_reference (rtl);
12199 if (new_rtl != rtl)
12200 loc_result = loc_descriptor (new_rtl, mode, initialized);
12202 break;
12204 case CONCAT:
12205 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
12206 initialized);
12207 break;
12209 case CONCATN:
12210 loc_result = concatn_loc_descriptor (rtl, initialized);
12211 break;
12213 case VAR_LOCATION:
12214 /* Single part. */
12215 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
12217 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
12218 if (GET_CODE (loc) == EXPR_LIST)
12219 loc = XEXP (loc, 0);
12220 loc_result = loc_descriptor (loc, mode, initialized);
12221 break;
12224 rtl = XEXP (rtl, 1);
12225 /* FALLTHRU */
12227 case PARALLEL:
12229 rtvec par_elems = XVEC (rtl, 0);
12230 int num_elem = GET_NUM_ELEM (par_elems);
12231 enum machine_mode mode;
12232 int i;
12234 /* Create the first one, so we have something to add to. */
12235 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
12236 VOIDmode, initialized);
12237 if (loc_result == NULL)
12238 return NULL;
12239 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
12240 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
12241 for (i = 1; i < num_elem; i++)
12243 dw_loc_descr_ref temp;
12245 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
12246 VOIDmode, initialized);
12247 if (temp == NULL)
12248 return NULL;
12249 add_loc_descr (&loc_result, temp);
12250 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
12251 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
12254 break;
12256 case CONST_INT:
12257 if (mode != VOIDmode && mode != BLKmode)
12258 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
12259 INTVAL (rtl));
12260 break;
12262 case CONST_DOUBLE:
12263 if (mode == VOIDmode)
12264 mode = GET_MODE (rtl);
12266 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
12268 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
12270 /* Note that a CONST_DOUBLE rtx could represent either an integer
12271 or a floating-point constant. A CONST_DOUBLE is used whenever
12272 the constant requires more than one word in order to be
12273 adequately represented. We output CONST_DOUBLEs as blocks. */
12274 loc_result = new_loc_descr (DW_OP_implicit_value,
12275 GET_MODE_SIZE (mode), 0);
12276 if (SCALAR_FLOAT_MODE_P (mode))
12278 unsigned int length = GET_MODE_SIZE (mode);
12279 unsigned char *array
12280 = (unsigned char*) ggc_alloc_atomic (length);
12282 insert_float (rtl, array);
12283 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12284 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12285 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12286 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12288 else
12290 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
12291 loc_result->dw_loc_oprnd2.v.val_double
12292 = rtx_to_double_int (rtl);
12295 break;
12297 case CONST_VECTOR:
12298 if (mode == VOIDmode)
12299 mode = GET_MODE (rtl);
12301 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
12303 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
12304 unsigned int length = CONST_VECTOR_NUNITS (rtl);
12305 unsigned char *array = (unsigned char *)
12306 ggc_alloc_atomic (length * elt_size);
12307 unsigned int i;
12308 unsigned char *p;
12310 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
12311 switch (GET_MODE_CLASS (mode))
12313 case MODE_VECTOR_INT:
12314 for (i = 0, p = array; i < length; i++, p += elt_size)
12316 rtx elt = CONST_VECTOR_ELT (rtl, i);
12317 double_int val = rtx_to_double_int (elt);
12319 if (elt_size <= sizeof (HOST_WIDE_INT))
12320 insert_int (val.to_shwi (), elt_size, p);
12321 else
12323 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
12324 insert_double (val, p);
12327 break;
12329 case MODE_VECTOR_FLOAT:
12330 for (i = 0, p = array; i < length; i++, p += elt_size)
12332 rtx elt = CONST_VECTOR_ELT (rtl, i);
12333 insert_float (elt, p);
12335 break;
12337 default:
12338 gcc_unreachable ();
12341 loc_result = new_loc_descr (DW_OP_implicit_value,
12342 length * elt_size, 0);
12343 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12344 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
12345 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
12346 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12348 break;
12350 case CONST:
12351 if (mode == VOIDmode
12352 || GET_CODE (XEXP (rtl, 0)) == CONST_INT
12353 || GET_CODE (XEXP (rtl, 0)) == CONST_DOUBLE
12354 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
12356 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
12357 break;
12359 /* FALLTHROUGH */
12360 case SYMBOL_REF:
12361 if (!const_ok_for_output (rtl))
12362 break;
12363 case LABEL_REF:
12364 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
12365 && (dwarf_version >= 4 || !dwarf_strict))
12367 loc_result = new_loc_descr (DW_OP_addr, 0, 0);
12368 loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
12369 loc_result->dw_loc_oprnd1.v.val_addr = rtl;
12370 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
12371 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
12373 break;
12375 case DEBUG_IMPLICIT_PTR:
12376 loc_result = implicit_ptr_descriptor (rtl, 0);
12377 break;
12379 case PLUS:
12380 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
12381 && CONST_INT_P (XEXP (rtl, 1)))
12383 loc_result
12384 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
12385 break;
12387 /* FALLTHRU */
12388 do_default:
12389 default:
12390 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
12391 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
12392 && dwarf_version >= 4)
12393 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
12395 /* Value expression. */
12396 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
12397 if (loc_result)
12398 add_loc_descr (&loc_result,
12399 new_loc_descr (DW_OP_stack_value, 0, 0));
12401 break;
12404 return loc_result;
12407 /* We need to figure out what section we should use as the base for the
12408 address ranges where a given location is valid.
12409 1. If this particular DECL has a section associated with it, use that.
12410 2. If this function has a section associated with it, use that.
12411 3. Otherwise, use the text section.
12412 XXX: If you split a variable across multiple sections, we won't notice. */
12414 static const char *
12415 secname_for_decl (const_tree decl)
12417 const char *secname;
12419 if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
12421 tree sectree = DECL_SECTION_NAME (decl);
12422 secname = TREE_STRING_POINTER (sectree);
12424 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
12426 tree sectree = DECL_SECTION_NAME (current_function_decl);
12427 secname = TREE_STRING_POINTER (sectree);
12429 else if (cfun && in_cold_section_p)
12430 secname = crtl->subsections.cold_section_label;
12431 else
12432 secname = text_section_label;
12434 return secname;
12437 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
12439 static bool
12440 decl_by_reference_p (tree decl)
12442 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
12443 || TREE_CODE (decl) == VAR_DECL)
12444 && DECL_BY_REFERENCE (decl));
12447 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
12448 for VARLOC. */
12450 static dw_loc_descr_ref
12451 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
12452 enum var_init_status initialized)
12454 int have_address = 0;
12455 dw_loc_descr_ref descr;
12456 enum machine_mode mode;
12458 if (want_address != 2)
12460 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
12461 /* Single part. */
12462 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
12464 varloc = PAT_VAR_LOCATION_LOC (varloc);
12465 if (GET_CODE (varloc) == EXPR_LIST)
12466 varloc = XEXP (varloc, 0);
12467 mode = GET_MODE (varloc);
12468 if (MEM_P (varloc))
12470 rtx addr = XEXP (varloc, 0);
12471 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
12472 mode, initialized);
12473 if (descr)
12474 have_address = 1;
12475 else
12477 rtx x = avoid_constant_pool_reference (varloc);
12478 if (x != varloc)
12479 descr = mem_loc_descriptor (x, mode, VOIDmode,
12480 initialized);
12483 else
12484 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
12486 else
12487 return 0;
12489 else
12491 if (GET_CODE (varloc) == VAR_LOCATION)
12492 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
12493 else
12494 mode = DECL_MODE (loc);
12495 descr = loc_descriptor (varloc, mode, initialized);
12496 have_address = 1;
12499 if (!descr)
12500 return 0;
12502 if (want_address == 2 && !have_address
12503 && (dwarf_version >= 4 || !dwarf_strict))
12505 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
12507 expansion_failed (loc, NULL_RTX,
12508 "DWARF address size mismatch");
12509 return 0;
12511 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
12512 have_address = 1;
12514 /* Show if we can't fill the request for an address. */
12515 if (want_address && !have_address)
12517 expansion_failed (loc, NULL_RTX,
12518 "Want address and only have value");
12519 return 0;
12522 /* If we've got an address and don't want one, dereference. */
12523 if (!want_address && have_address)
12525 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
12526 enum dwarf_location_atom op;
12528 if (size > DWARF2_ADDR_SIZE || size == -1)
12530 expansion_failed (loc, NULL_RTX,
12531 "DWARF address size mismatch");
12532 return 0;
12534 else if (size == DWARF2_ADDR_SIZE)
12535 op = DW_OP_deref;
12536 else
12537 op = DW_OP_deref_size;
12539 add_loc_descr (&descr, new_loc_descr (op, size, 0));
12542 return descr;
12545 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
12546 if it is not possible. */
12548 static dw_loc_descr_ref
12549 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
12551 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
12552 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
12553 else if (dwarf_version >= 3 || !dwarf_strict)
12554 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
12555 else
12556 return NULL;
12559 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
12560 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
12562 static dw_loc_descr_ref
12563 dw_sra_loc_expr (tree decl, rtx loc)
12565 rtx p;
12566 unsigned int padsize = 0;
12567 dw_loc_descr_ref descr, *descr_tail;
12568 unsigned HOST_WIDE_INT decl_size;
12569 rtx varloc;
12570 enum var_init_status initialized;
12572 if (DECL_SIZE (decl) == NULL
12573 || !host_integerp (DECL_SIZE (decl), 1))
12574 return NULL;
12576 decl_size = tree_low_cst (DECL_SIZE (decl), 1);
12577 descr = NULL;
12578 descr_tail = &descr;
12580 for (p = loc; p; p = XEXP (p, 1))
12582 unsigned int bitsize = decl_piece_bitsize (p);
12583 rtx loc_note = *decl_piece_varloc_ptr (p);
12584 dw_loc_descr_ref cur_descr;
12585 dw_loc_descr_ref *tail, last = NULL;
12586 unsigned int opsize = 0;
12588 if (loc_note == NULL_RTX
12589 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
12591 padsize += bitsize;
12592 continue;
12594 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
12595 varloc = NOTE_VAR_LOCATION (loc_note);
12596 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
12597 if (cur_descr == NULL)
12599 padsize += bitsize;
12600 continue;
12603 /* Check that cur_descr either doesn't use
12604 DW_OP_*piece operations, or their sum is equal
12605 to bitsize. Otherwise we can't embed it. */
12606 for (tail = &cur_descr; *tail != NULL;
12607 tail = &(*tail)->dw_loc_next)
12608 if ((*tail)->dw_loc_opc == DW_OP_piece)
12610 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
12611 * BITS_PER_UNIT;
12612 last = *tail;
12614 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
12616 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
12617 last = *tail;
12620 if (last != NULL && opsize != bitsize)
12622 padsize += bitsize;
12623 continue;
12626 /* If there is a hole, add DW_OP_*piece after empty DWARF
12627 expression, which means that those bits are optimized out. */
12628 if (padsize)
12630 if (padsize > decl_size)
12631 return NULL;
12632 decl_size -= padsize;
12633 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
12634 if (*descr_tail == NULL)
12635 return NULL;
12636 descr_tail = &(*descr_tail)->dw_loc_next;
12637 padsize = 0;
12639 *descr_tail = cur_descr;
12640 descr_tail = tail;
12641 if (bitsize > decl_size)
12642 return NULL;
12643 decl_size -= bitsize;
12644 if (last == NULL)
12646 HOST_WIDE_INT offset = 0;
12647 if (GET_CODE (varloc) == VAR_LOCATION
12648 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
12650 varloc = PAT_VAR_LOCATION_LOC (varloc);
12651 if (GET_CODE (varloc) == EXPR_LIST)
12652 varloc = XEXP (varloc, 0);
12656 if (GET_CODE (varloc) == CONST
12657 || GET_CODE (varloc) == SIGN_EXTEND
12658 || GET_CODE (varloc) == ZERO_EXTEND)
12659 varloc = XEXP (varloc, 0);
12660 else if (GET_CODE (varloc) == SUBREG)
12661 varloc = SUBREG_REG (varloc);
12662 else
12663 break;
12665 while (1);
12666 /* DW_OP_bit_size offset should be zero for register
12667 or implicit location descriptions and empty location
12668 descriptions, but for memory addresses needs big endian
12669 adjustment. */
12670 if (MEM_P (varloc))
12672 unsigned HOST_WIDE_INT memsize
12673 = MEM_SIZE (varloc) * BITS_PER_UNIT;
12674 if (memsize != bitsize)
12676 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
12677 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
12678 return NULL;
12679 if (memsize < bitsize)
12680 return NULL;
12681 if (BITS_BIG_ENDIAN)
12682 offset = memsize - bitsize;
12686 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
12687 if (*descr_tail == NULL)
12688 return NULL;
12689 descr_tail = &(*descr_tail)->dw_loc_next;
12693 /* If there were any non-empty expressions, add padding till the end of
12694 the decl. */
12695 if (descr != NULL && decl_size != 0)
12697 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
12698 if (*descr_tail == NULL)
12699 return NULL;
12701 return descr;
12704 /* Return the dwarf representation of the location list LOC_LIST of
12705 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
12706 function. */
12708 static dw_loc_list_ref
12709 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
12711 const char *endname, *secname;
12712 rtx varloc;
12713 enum var_init_status initialized;
12714 struct var_loc_node *node;
12715 dw_loc_descr_ref descr;
12716 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
12717 dw_loc_list_ref list = NULL;
12718 dw_loc_list_ref *listp = &list;
12720 /* Now that we know what section we are using for a base,
12721 actually construct the list of locations.
12722 The first location information is what is passed to the
12723 function that creates the location list, and the remaining
12724 locations just get added on to that list.
12725 Note that we only know the start address for a location
12726 (IE location changes), so to build the range, we use
12727 the range [current location start, next location start].
12728 This means we have to special case the last node, and generate
12729 a range of [last location start, end of function label]. */
12731 secname = secname_for_decl (decl);
12733 for (node = loc_list->first; node; node = node->next)
12734 if (GET_CODE (node->loc) == EXPR_LIST
12735 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
12737 if (GET_CODE (node->loc) == EXPR_LIST)
12739 /* This requires DW_OP_{,bit_}piece, which is not usable
12740 inside DWARF expressions. */
12741 if (want_address != 2)
12742 continue;
12743 descr = dw_sra_loc_expr (decl, node->loc);
12744 if (descr == NULL)
12745 continue;
12747 else
12749 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
12750 varloc = NOTE_VAR_LOCATION (node->loc);
12751 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
12753 if (descr)
12755 bool range_across_switch = false;
12756 /* If section switch happens in between node->label
12757 and node->next->label (or end of function) and
12758 we can't emit it as a single entry list,
12759 emit two ranges, first one ending at the end
12760 of first partition and second one starting at the
12761 beginning of second partition. */
12762 if (node == loc_list->last_before_switch
12763 && (node != loc_list->first || loc_list->first->next)
12764 && current_function_decl)
12766 endname = cfun->fde->dw_fde_end;
12767 range_across_switch = true;
12769 /* The variable has a location between NODE->LABEL and
12770 NODE->NEXT->LABEL. */
12771 else if (node->next)
12772 endname = node->next->label;
12773 /* If the variable has a location at the last label
12774 it keeps its location until the end of function. */
12775 else if (!current_function_decl)
12776 endname = text_end_label;
12777 else
12779 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
12780 current_function_funcdef_no);
12781 endname = ggc_strdup (label_id);
12784 *listp = new_loc_list (descr, node->label, endname, secname);
12785 if (TREE_CODE (decl) == PARM_DECL
12786 && node == loc_list->first
12787 && GET_CODE (node->loc) == NOTE
12788 && strcmp (node->label, endname) == 0)
12789 (*listp)->force = true;
12790 listp = &(*listp)->dw_loc_next;
12792 if (range_across_switch)
12794 if (GET_CODE (node->loc) == EXPR_LIST)
12795 descr = dw_sra_loc_expr (decl, node->loc);
12796 else
12798 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
12799 varloc = NOTE_VAR_LOCATION (node->loc);
12800 descr = dw_loc_list_1 (decl, varloc, want_address,
12801 initialized);
12803 gcc_assert (descr);
12804 /* The variable has a location between NODE->LABEL and
12805 NODE->NEXT->LABEL. */
12806 if (node->next)
12807 endname = node->next->label;
12808 else
12809 endname = cfun->fde->dw_fde_second_end;
12810 *listp = new_loc_list (descr,
12811 cfun->fde->dw_fde_second_begin,
12812 endname, secname);
12813 listp = &(*listp)->dw_loc_next;
12818 /* Try to avoid the overhead of a location list emitting a location
12819 expression instead, but only if we didn't have more than one
12820 location entry in the first place. If some entries were not
12821 representable, we don't want to pretend a single entry that was
12822 applies to the entire scope in which the variable is
12823 available. */
12824 if (list && loc_list->first->next)
12825 gen_llsym (list);
12827 return list;
12830 /* Return if the loc_list has only single element and thus can be represented
12831 as location description. */
12833 static bool
12834 single_element_loc_list_p (dw_loc_list_ref list)
12836 gcc_assert (!list->dw_loc_next || list->ll_symbol);
12837 return !list->ll_symbol;
12840 /* To each location in list LIST add loc descr REF. */
12842 static void
12843 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
12845 dw_loc_descr_ref copy;
12846 add_loc_descr (&list->expr, ref);
12847 list = list->dw_loc_next;
12848 while (list)
12850 copy = ggc_alloc_dw_loc_descr_node ();
12851 memcpy (copy, ref, sizeof (dw_loc_descr_node));
12852 add_loc_descr (&list->expr, copy);
12853 while (copy->dw_loc_next)
12855 dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
12856 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
12857 copy->dw_loc_next = new_copy;
12858 copy = new_copy;
12860 list = list->dw_loc_next;
12864 /* Given two lists RET and LIST
12865 produce location list that is result of adding expression in LIST
12866 to expression in RET on each position in program.
12867 Might be destructive on both RET and LIST.
12869 TODO: We handle only simple cases of RET or LIST having at most one
12870 element. General case would inolve sorting the lists in program order
12871 and merging them that will need some additional work.
12872 Adding that will improve quality of debug info especially for SRA-ed
12873 structures. */
12875 static void
12876 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
12878 if (!list)
12879 return;
12880 if (!*ret)
12882 *ret = list;
12883 return;
12885 if (!list->dw_loc_next)
12887 add_loc_descr_to_each (*ret, list->expr);
12888 return;
12890 if (!(*ret)->dw_loc_next)
12892 add_loc_descr_to_each (list, (*ret)->expr);
12893 *ret = list;
12894 return;
12896 expansion_failed (NULL_TREE, NULL_RTX,
12897 "Don't know how to merge two non-trivial"
12898 " location lists.\n");
12899 *ret = NULL;
12900 return;
12903 /* LOC is constant expression. Try a luck, look it up in constant
12904 pool and return its loc_descr of its address. */
12906 static dw_loc_descr_ref
12907 cst_pool_loc_descr (tree loc)
12909 /* Get an RTL for this, if something has been emitted. */
12910 rtx rtl = lookup_constant_def (loc);
12912 if (!rtl || !MEM_P (rtl))
12914 gcc_assert (!rtl);
12915 return 0;
12917 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
12919 /* TODO: We might get more coverage if we was actually delaying expansion
12920 of all expressions till end of compilation when constant pools are fully
12921 populated. */
12922 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
12924 expansion_failed (loc, NULL_RTX,
12925 "CST value in contant pool but not marked.");
12926 return 0;
12928 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
12929 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
12932 /* Return dw_loc_list representing address of addr_expr LOC
12933 by looking for inner INDIRECT_REF expression and turning
12934 it into simple arithmetics. */
12936 static dw_loc_list_ref
12937 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
12939 tree obj, offset;
12940 HOST_WIDE_INT bitsize, bitpos, bytepos;
12941 enum machine_mode mode;
12942 int unsignedp, volatilep = 0;
12943 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
12945 obj = get_inner_reference (TREE_OPERAND (loc, 0),
12946 &bitsize, &bitpos, &offset, &mode,
12947 &unsignedp, &volatilep, false);
12948 STRIP_NOPS (obj);
12949 if (bitpos % BITS_PER_UNIT)
12951 expansion_failed (loc, NULL_RTX, "bitfield access");
12952 return 0;
12954 if (!INDIRECT_REF_P (obj))
12956 expansion_failed (obj,
12957 NULL_RTX, "no indirect ref in inner refrence");
12958 return 0;
12960 if (!offset && !bitpos)
12961 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
12962 else if (toplev
12963 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
12964 && (dwarf_version >= 4 || !dwarf_strict))
12966 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
12967 if (!list_ret)
12968 return 0;
12969 if (offset)
12971 /* Variable offset. */
12972 list_ret1 = loc_list_from_tree (offset, 0);
12973 if (list_ret1 == 0)
12974 return 0;
12975 add_loc_list (&list_ret, list_ret1);
12976 if (!list_ret)
12977 return 0;
12978 add_loc_descr_to_each (list_ret,
12979 new_loc_descr (DW_OP_plus, 0, 0));
12981 bytepos = bitpos / BITS_PER_UNIT;
12982 if (bytepos > 0)
12983 add_loc_descr_to_each (list_ret,
12984 new_loc_descr (DW_OP_plus_uconst,
12985 bytepos, 0));
12986 else if (bytepos < 0)
12987 loc_list_plus_const (list_ret, bytepos);
12988 add_loc_descr_to_each (list_ret,
12989 new_loc_descr (DW_OP_stack_value, 0, 0));
12991 return list_ret;
12995 /* Generate Dwarf location list representing LOC.
12996 If WANT_ADDRESS is false, expression computing LOC will be computed
12997 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
12998 if WANT_ADDRESS is 2, expression computing address useable in location
12999 will be returned (i.e. DW_OP_reg can be used
13000 to refer to register values). */
13002 static dw_loc_list_ref
13003 loc_list_from_tree (tree loc, int want_address)
13005 dw_loc_descr_ref ret = NULL, ret1 = NULL;
13006 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13007 int have_address = 0;
13008 enum dwarf_location_atom op;
13010 /* ??? Most of the time we do not take proper care for sign/zero
13011 extending the values properly. Hopefully this won't be a real
13012 problem... */
13014 switch (TREE_CODE (loc))
13016 case ERROR_MARK:
13017 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
13018 return 0;
13020 case PLACEHOLDER_EXPR:
13021 /* This case involves extracting fields from an object to determine the
13022 position of other fields. We don't try to encode this here. The
13023 only user of this is Ada, which encodes the needed information using
13024 the names of types. */
13025 expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
13026 return 0;
13028 case CALL_EXPR:
13029 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
13030 /* There are no opcodes for these operations. */
13031 return 0;
13033 case PREINCREMENT_EXPR:
13034 case PREDECREMENT_EXPR:
13035 case POSTINCREMENT_EXPR:
13036 case POSTDECREMENT_EXPR:
13037 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
13038 /* There are no opcodes for these operations. */
13039 return 0;
13041 case ADDR_EXPR:
13042 /* If we already want an address, see if there is INDIRECT_REF inside
13043 e.g. for &this->field. */
13044 if (want_address)
13046 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
13047 (loc, want_address == 2);
13048 if (list_ret)
13049 have_address = 1;
13050 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
13051 && (ret = cst_pool_loc_descr (loc)))
13052 have_address = 1;
13054 /* Otherwise, process the argument and look for the address. */
13055 if (!list_ret && !ret)
13056 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
13057 else
13059 if (want_address)
13060 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
13061 return NULL;
13063 break;
13065 case VAR_DECL:
13066 if (DECL_THREAD_LOCAL_P (loc))
13068 rtx rtl;
13069 enum dwarf_location_atom first_op;
13070 enum dwarf_location_atom second_op;
13071 bool dtprel = false;
13073 if (targetm.have_tls)
13075 /* If this is not defined, we have no way to emit the
13076 data. */
13077 if (!targetm.asm_out.output_dwarf_dtprel)
13078 return 0;
13080 /* The way DW_OP_GNU_push_tls_address is specified, we
13081 can only look up addresses of objects in the current
13082 module. We used DW_OP_addr as first op, but that's
13083 wrong, because DW_OP_addr is relocated by the debug
13084 info consumer, while DW_OP_GNU_push_tls_address
13085 operand shouldn't be. */
13086 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
13087 return 0;
13088 first_op = DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u;
13089 dtprel = true;
13090 second_op = DW_OP_GNU_push_tls_address;
13092 else
13094 if (!targetm.emutls.debug_form_tls_address
13095 || !(dwarf_version >= 3 || !dwarf_strict))
13096 return 0;
13097 /* We stuffed the control variable into the DECL_VALUE_EXPR
13098 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
13099 no longer appear in gimple code. We used the control
13100 variable in specific so that we could pick it up here. */
13101 loc = DECL_VALUE_EXPR (loc);
13102 first_op = DW_OP_addr;
13103 second_op = DW_OP_form_tls_address;
13106 rtl = rtl_for_decl_location (loc);
13107 if (rtl == NULL_RTX)
13108 return 0;
13110 if (!MEM_P (rtl))
13111 return 0;
13112 rtl = XEXP (rtl, 0);
13113 if (! CONSTANT_P (rtl))
13114 return 0;
13116 ret = new_loc_descr (first_op, 0, 0);
13117 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
13118 ret->dw_loc_oprnd1.v.val_addr = rtl;
13119 ret->dtprel = dtprel;
13121 ret1 = new_loc_descr (second_op, 0, 0);
13122 add_loc_descr (&ret, ret1);
13124 have_address = 1;
13125 break;
13127 /* FALLTHRU */
13129 case PARM_DECL:
13130 case RESULT_DECL:
13131 if (DECL_HAS_VALUE_EXPR_P (loc))
13132 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
13133 want_address);
13134 /* FALLTHRU */
13136 case FUNCTION_DECL:
13138 rtx rtl;
13139 var_loc_list *loc_list = lookup_decl_loc (loc);
13141 if (loc_list && loc_list->first)
13143 list_ret = dw_loc_list (loc_list, loc, want_address);
13144 have_address = want_address != 0;
13145 break;
13147 rtl = rtl_for_decl_location (loc);
13148 if (rtl == NULL_RTX)
13150 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
13151 return 0;
13153 else if (CONST_INT_P (rtl))
13155 HOST_WIDE_INT val = INTVAL (rtl);
13156 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13157 val &= GET_MODE_MASK (DECL_MODE (loc));
13158 ret = int_loc_descriptor (val);
13160 else if (GET_CODE (rtl) == CONST_STRING)
13162 expansion_failed (loc, NULL_RTX, "CONST_STRING");
13163 return 0;
13165 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
13167 ret = new_loc_descr (DW_OP_addr, 0, 0);
13168 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
13169 ret->dw_loc_oprnd1.v.val_addr = rtl;
13171 else
13173 enum machine_mode mode, mem_mode;
13175 /* Certain constructs can only be represented at top-level. */
13176 if (want_address == 2)
13178 ret = loc_descriptor (rtl, VOIDmode,
13179 VAR_INIT_STATUS_INITIALIZED);
13180 have_address = 1;
13182 else
13184 mode = GET_MODE (rtl);
13185 mem_mode = VOIDmode;
13186 if (MEM_P (rtl))
13188 mem_mode = mode;
13189 mode = get_address_mode (rtl);
13190 rtl = XEXP (rtl, 0);
13191 have_address = 1;
13193 ret = mem_loc_descriptor (rtl, mode, mem_mode,
13194 VAR_INIT_STATUS_INITIALIZED);
13196 if (!ret)
13197 expansion_failed (loc, rtl,
13198 "failed to produce loc descriptor for rtl");
13201 break;
13203 case MEM_REF:
13204 /* ??? FIXME. */
13205 if (!integer_zerop (TREE_OPERAND (loc, 1)))
13206 return 0;
13207 /* Fallthru. */
13208 case INDIRECT_REF:
13209 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13210 have_address = 1;
13211 break;
13213 case COMPOUND_EXPR:
13214 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
13216 CASE_CONVERT:
13217 case VIEW_CONVERT_EXPR:
13218 case SAVE_EXPR:
13219 case MODIFY_EXPR:
13220 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
13222 case COMPONENT_REF:
13223 case BIT_FIELD_REF:
13224 case ARRAY_REF:
13225 case ARRAY_RANGE_REF:
13226 case REALPART_EXPR:
13227 case IMAGPART_EXPR:
13229 tree obj, offset;
13230 HOST_WIDE_INT bitsize, bitpos, bytepos;
13231 enum machine_mode mode;
13232 int unsignedp, volatilep = 0;
13234 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
13235 &unsignedp, &volatilep, false);
13237 gcc_assert (obj != loc);
13239 list_ret = loc_list_from_tree (obj,
13240 want_address == 2
13241 && !bitpos && !offset ? 2 : 1);
13242 /* TODO: We can extract value of the small expression via shifting even
13243 for nonzero bitpos. */
13244 if (list_ret == 0)
13245 return 0;
13246 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
13248 expansion_failed (loc, NULL_RTX,
13249 "bitfield access");
13250 return 0;
13253 if (offset != NULL_TREE)
13255 /* Variable offset. */
13256 list_ret1 = loc_list_from_tree (offset, 0);
13257 if (list_ret1 == 0)
13258 return 0;
13259 add_loc_list (&list_ret, list_ret1);
13260 if (!list_ret)
13261 return 0;
13262 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
13265 bytepos = bitpos / BITS_PER_UNIT;
13266 if (bytepos > 0)
13267 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
13268 else if (bytepos < 0)
13269 loc_list_plus_const (list_ret, bytepos);
13271 have_address = 1;
13272 break;
13275 case INTEGER_CST:
13276 if ((want_address || !host_integerp (loc, 0))
13277 && (ret = cst_pool_loc_descr (loc)))
13278 have_address = 1;
13279 else if (want_address == 2
13280 && host_integerp (loc, 0)
13281 && (ret = address_of_int_loc_descriptor
13282 (int_size_in_bytes (TREE_TYPE (loc)),
13283 tree_low_cst (loc, 0))))
13284 have_address = 1;
13285 else if (host_integerp (loc, 0))
13286 ret = int_loc_descriptor (tree_low_cst (loc, 0));
13287 else
13289 expansion_failed (loc, NULL_RTX,
13290 "Integer operand is not host integer");
13291 return 0;
13293 break;
13295 case CONSTRUCTOR:
13296 case REAL_CST:
13297 case STRING_CST:
13298 case COMPLEX_CST:
13299 if ((ret = cst_pool_loc_descr (loc)))
13300 have_address = 1;
13301 else
13302 /* We can construct small constants here using int_loc_descriptor. */
13303 expansion_failed (loc, NULL_RTX,
13304 "constructor or constant not in constant pool");
13305 break;
13307 case TRUTH_AND_EXPR:
13308 case TRUTH_ANDIF_EXPR:
13309 case BIT_AND_EXPR:
13310 op = DW_OP_and;
13311 goto do_binop;
13313 case TRUTH_XOR_EXPR:
13314 case BIT_XOR_EXPR:
13315 op = DW_OP_xor;
13316 goto do_binop;
13318 case TRUTH_OR_EXPR:
13319 case TRUTH_ORIF_EXPR:
13320 case BIT_IOR_EXPR:
13321 op = DW_OP_or;
13322 goto do_binop;
13324 case FLOOR_DIV_EXPR:
13325 case CEIL_DIV_EXPR:
13326 case ROUND_DIV_EXPR:
13327 case TRUNC_DIV_EXPR:
13328 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13329 return 0;
13330 op = DW_OP_div;
13331 goto do_binop;
13333 case MINUS_EXPR:
13334 op = DW_OP_minus;
13335 goto do_binop;
13337 case FLOOR_MOD_EXPR:
13338 case CEIL_MOD_EXPR:
13339 case ROUND_MOD_EXPR:
13340 case TRUNC_MOD_EXPR:
13341 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13343 op = DW_OP_mod;
13344 goto do_binop;
13346 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13347 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
13348 if (list_ret == 0 || list_ret1 == 0)
13349 return 0;
13351 add_loc_list (&list_ret, list_ret1);
13352 if (list_ret == 0)
13353 return 0;
13354 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
13355 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
13356 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
13357 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
13358 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
13359 break;
13361 case MULT_EXPR:
13362 op = DW_OP_mul;
13363 goto do_binop;
13365 case LSHIFT_EXPR:
13366 op = DW_OP_shl;
13367 goto do_binop;
13369 case RSHIFT_EXPR:
13370 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
13371 goto do_binop;
13373 case POINTER_PLUS_EXPR:
13374 case PLUS_EXPR:
13375 if (host_integerp (TREE_OPERAND (loc, 1), 0))
13377 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13378 if (list_ret == 0)
13379 return 0;
13381 loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
13382 break;
13385 op = DW_OP_plus;
13386 goto do_binop;
13388 case LE_EXPR:
13389 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13390 return 0;
13392 op = DW_OP_le;
13393 goto do_binop;
13395 case GE_EXPR:
13396 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13397 return 0;
13399 op = DW_OP_ge;
13400 goto do_binop;
13402 case LT_EXPR:
13403 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13404 return 0;
13406 op = DW_OP_lt;
13407 goto do_binop;
13409 case GT_EXPR:
13410 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13411 return 0;
13413 op = DW_OP_gt;
13414 goto do_binop;
13416 case EQ_EXPR:
13417 op = DW_OP_eq;
13418 goto do_binop;
13420 case NE_EXPR:
13421 op = DW_OP_ne;
13422 goto do_binop;
13424 do_binop:
13425 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13426 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
13427 if (list_ret == 0 || list_ret1 == 0)
13428 return 0;
13430 add_loc_list (&list_ret, list_ret1);
13431 if (list_ret == 0)
13432 return 0;
13433 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
13434 break;
13436 case TRUTH_NOT_EXPR:
13437 case BIT_NOT_EXPR:
13438 op = DW_OP_not;
13439 goto do_unop;
13441 case ABS_EXPR:
13442 op = DW_OP_abs;
13443 goto do_unop;
13445 case NEGATE_EXPR:
13446 op = DW_OP_neg;
13447 goto do_unop;
13449 do_unop:
13450 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13451 if (list_ret == 0)
13452 return 0;
13454 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
13455 break;
13457 case MIN_EXPR:
13458 case MAX_EXPR:
13460 const enum tree_code code =
13461 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
13463 loc = build3 (COND_EXPR, TREE_TYPE (loc),
13464 build2 (code, integer_type_node,
13465 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
13466 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
13469 /* ... fall through ... */
13471 case COND_EXPR:
13473 dw_loc_descr_ref lhs
13474 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
13475 dw_loc_list_ref rhs
13476 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
13477 dw_loc_descr_ref bra_node, jump_node, tmp;
13479 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13480 if (list_ret == 0 || lhs == 0 || rhs == 0)
13481 return 0;
13483 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13484 add_loc_descr_to_each (list_ret, bra_node);
13486 add_loc_list (&list_ret, rhs);
13487 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
13488 add_loc_descr_to_each (list_ret, jump_node);
13490 add_loc_descr_to_each (list_ret, lhs);
13491 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13492 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
13494 /* ??? Need a node to point the skip at. Use a nop. */
13495 tmp = new_loc_descr (DW_OP_nop, 0, 0);
13496 add_loc_descr_to_each (list_ret, tmp);
13497 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13498 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
13500 break;
13502 case FIX_TRUNC_EXPR:
13503 return 0;
13505 default:
13506 /* Leave front-end specific codes as simply unknown. This comes
13507 up, for instance, with the C STMT_EXPR. */
13508 if ((unsigned int) TREE_CODE (loc)
13509 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
13511 expansion_failed (loc, NULL_RTX,
13512 "language specific tree node");
13513 return 0;
13516 #ifdef ENABLE_CHECKING
13517 /* Otherwise this is a generic code; we should just lists all of
13518 these explicitly. We forgot one. */
13519 gcc_unreachable ();
13520 #else
13521 /* In a release build, we want to degrade gracefully: better to
13522 generate incomplete debugging information than to crash. */
13523 return NULL;
13524 #endif
13527 if (!ret && !list_ret)
13528 return 0;
13530 if (want_address == 2 && !have_address
13531 && (dwarf_version >= 4 || !dwarf_strict))
13533 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13535 expansion_failed (loc, NULL_RTX,
13536 "DWARF address size mismatch");
13537 return 0;
13539 if (ret)
13540 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
13541 else
13542 add_loc_descr_to_each (list_ret,
13543 new_loc_descr (DW_OP_stack_value, 0, 0));
13544 have_address = 1;
13546 /* Show if we can't fill the request for an address. */
13547 if (want_address && !have_address)
13549 expansion_failed (loc, NULL_RTX,
13550 "Want address and only have value");
13551 return 0;
13554 gcc_assert (!ret || !list_ret);
13556 /* If we've got an address and don't want one, dereference. */
13557 if (!want_address && have_address)
13559 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13561 if (size > DWARF2_ADDR_SIZE || size == -1)
13563 expansion_failed (loc, NULL_RTX,
13564 "DWARF address size mismatch");
13565 return 0;
13567 else if (size == DWARF2_ADDR_SIZE)
13568 op = DW_OP_deref;
13569 else
13570 op = DW_OP_deref_size;
13572 if (ret)
13573 add_loc_descr (&ret, new_loc_descr (op, size, 0));
13574 else
13575 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
13577 if (ret)
13578 list_ret = new_loc_list (ret, NULL, NULL, NULL);
13580 return list_ret;
13583 /* Same as above but return only single location expression. */
13584 static dw_loc_descr_ref
13585 loc_descriptor_from_tree (tree loc, int want_address)
13587 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
13588 if (!ret)
13589 return NULL;
13590 if (ret->dw_loc_next)
13592 expansion_failed (loc, NULL_RTX,
13593 "Location list where only loc descriptor needed");
13594 return NULL;
13596 return ret->expr;
13599 /* Given a value, round it up to the lowest multiple of `boundary'
13600 which is not less than the value itself. */
13602 static inline HOST_WIDE_INT
13603 ceiling (HOST_WIDE_INT value, unsigned int boundary)
13605 return (((value + boundary - 1) / boundary) * boundary);
13608 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
13609 pointer to the declared type for the relevant field variable, or return
13610 `integer_type_node' if the given node turns out to be an
13611 ERROR_MARK node. */
13613 static inline tree
13614 field_type (const_tree decl)
13616 tree type;
13618 if (TREE_CODE (decl) == ERROR_MARK)
13619 return integer_type_node;
13621 type = DECL_BIT_FIELD_TYPE (decl);
13622 if (type == NULL_TREE)
13623 type = TREE_TYPE (decl);
13625 return type;
13628 /* Given a pointer to a tree node, return the alignment in bits for
13629 it, or else return BITS_PER_WORD if the node actually turns out to
13630 be an ERROR_MARK node. */
13632 static inline unsigned
13633 simple_type_align_in_bits (const_tree type)
13635 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
13638 static inline unsigned
13639 simple_decl_align_in_bits (const_tree decl)
13641 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
13644 /* Return the result of rounding T up to ALIGN. */
13646 static inline double_int
13647 round_up_to_align (double_int t, unsigned int align)
13649 double_int alignd = double_int::from_uhwi (align);
13650 t += alignd;
13651 t += double_int_minus_one;
13652 t = t.div (alignd, true, TRUNC_DIV_EXPR);
13653 t *= alignd;
13654 return t;
13657 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
13658 lowest addressed byte of the "containing object" for the given FIELD_DECL,
13659 or return 0 if we are unable to determine what that offset is, either
13660 because the argument turns out to be a pointer to an ERROR_MARK node, or
13661 because the offset is actually variable. (We can't handle the latter case
13662 just yet). */
13664 static HOST_WIDE_INT
13665 field_byte_offset (const_tree decl)
13667 double_int object_offset_in_bits;
13668 double_int object_offset_in_bytes;
13669 double_int bitpos_int;
13671 if (TREE_CODE (decl) == ERROR_MARK)
13672 return 0;
13674 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
13676 /* We cannot yet cope with fields whose positions are variable, so
13677 for now, when we see such things, we simply return 0. Someday, we may
13678 be able to handle such cases, but it will be damn difficult. */
13679 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
13680 return 0;
13682 bitpos_int = tree_to_double_int (bit_position (decl));
13684 #ifdef PCC_BITFIELD_TYPE_MATTERS
13685 if (PCC_BITFIELD_TYPE_MATTERS)
13687 tree type;
13688 tree field_size_tree;
13689 double_int deepest_bitpos;
13690 double_int field_size_in_bits;
13691 unsigned int type_align_in_bits;
13692 unsigned int decl_align_in_bits;
13693 double_int type_size_in_bits;
13695 type = field_type (decl);
13696 type_size_in_bits = double_int_type_size_in_bits (type);
13697 type_align_in_bits = simple_type_align_in_bits (type);
13699 field_size_tree = DECL_SIZE (decl);
13701 /* The size could be unspecified if there was an error, or for
13702 a flexible array member. */
13703 if (!field_size_tree)
13704 field_size_tree = bitsize_zero_node;
13706 /* If the size of the field is not constant, use the type size. */
13707 if (TREE_CODE (field_size_tree) == INTEGER_CST)
13708 field_size_in_bits = tree_to_double_int (field_size_tree);
13709 else
13710 field_size_in_bits = type_size_in_bits;
13712 decl_align_in_bits = simple_decl_align_in_bits (decl);
13714 /* The GCC front-end doesn't make any attempt to keep track of the
13715 starting bit offset (relative to the start of the containing
13716 structure type) of the hypothetical "containing object" for a
13717 bit-field. Thus, when computing the byte offset value for the
13718 start of the "containing object" of a bit-field, we must deduce
13719 this information on our own. This can be rather tricky to do in
13720 some cases. For example, handling the following structure type
13721 definition when compiling for an i386/i486 target (which only
13722 aligns long long's to 32-bit boundaries) can be very tricky:
13724 struct S { int field1; long long field2:31; };
13726 Fortunately, there is a simple rule-of-thumb which can be used
13727 in such cases. When compiling for an i386/i486, GCC will
13728 allocate 8 bytes for the structure shown above. It decides to
13729 do this based upon one simple rule for bit-field allocation.
13730 GCC allocates each "containing object" for each bit-field at
13731 the first (i.e. lowest addressed) legitimate alignment boundary
13732 (based upon the required minimum alignment for the declared
13733 type of the field) which it can possibly use, subject to the
13734 condition that there is still enough available space remaining
13735 in the containing object (when allocated at the selected point)
13736 to fully accommodate all of the bits of the bit-field itself.
13738 This simple rule makes it obvious why GCC allocates 8 bytes for
13739 each object of the structure type shown above. When looking
13740 for a place to allocate the "containing object" for `field2',
13741 the compiler simply tries to allocate a 64-bit "containing
13742 object" at each successive 32-bit boundary (starting at zero)
13743 until it finds a place to allocate that 64- bit field such that
13744 at least 31 contiguous (and previously unallocated) bits remain
13745 within that selected 64 bit field. (As it turns out, for the
13746 example above, the compiler finds it is OK to allocate the
13747 "containing object" 64-bit field at bit-offset zero within the
13748 structure type.)
13750 Here we attempt to work backwards from the limited set of facts
13751 we're given, and we try to deduce from those facts, where GCC
13752 must have believed that the containing object started (within
13753 the structure type). The value we deduce is then used (by the
13754 callers of this routine) to generate DW_AT_location and
13755 DW_AT_bit_offset attributes for fields (both bit-fields and, in
13756 the case of DW_AT_location, regular fields as well). */
13758 /* Figure out the bit-distance from the start of the structure to
13759 the "deepest" bit of the bit-field. */
13760 deepest_bitpos = bitpos_int + field_size_in_bits;
13762 /* This is the tricky part. Use some fancy footwork to deduce
13763 where the lowest addressed bit of the containing object must
13764 be. */
13765 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
13767 /* Round up to type_align by default. This works best for
13768 bitfields. */
13769 object_offset_in_bits
13770 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
13772 if (object_offset_in_bits.ugt (bitpos_int))
13774 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
13776 /* Round up to decl_align instead. */
13777 object_offset_in_bits
13778 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
13781 else
13782 #endif /* PCC_BITFIELD_TYPE_MATTERS */
13783 object_offset_in_bits = bitpos_int;
13785 object_offset_in_bytes
13786 = object_offset_in_bits.div (double_int::from_uhwi (BITS_PER_UNIT),
13787 true, TRUNC_DIV_EXPR);
13788 return object_offset_in_bytes.to_shwi ();
13791 /* The following routines define various Dwarf attributes and any data
13792 associated with them. */
13794 /* Add a location description attribute value to a DIE.
13796 This emits location attributes suitable for whole variables and
13797 whole parameters. Note that the location attributes for struct fields are
13798 generated by the routine `data_member_location_attribute' below. */
13800 static inline void
13801 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
13802 dw_loc_list_ref descr)
13804 if (descr == 0)
13805 return;
13806 if (single_element_loc_list_p (descr))
13807 add_AT_loc (die, attr_kind, descr->expr);
13808 else
13809 add_AT_loc_list (die, attr_kind, descr);
13812 /* Add DW_AT_accessibility attribute to DIE if needed. */
13814 static void
13815 add_accessibility_attribute (dw_die_ref die, tree decl)
13817 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
13818 children, otherwise the default is DW_ACCESS_public. In DWARF2
13819 the default has always been DW_ACCESS_public. */
13820 if (TREE_PROTECTED (decl))
13821 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
13822 else if (TREE_PRIVATE (decl))
13824 if (dwarf_version == 2
13825 || die->die_parent == NULL
13826 || die->die_parent->die_tag != DW_TAG_class_type)
13827 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
13829 else if (dwarf_version > 2
13830 && die->die_parent
13831 && die->die_parent->die_tag == DW_TAG_class_type)
13832 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
13835 /* Attach the specialized form of location attribute used for data members of
13836 struct and union types. In the special case of a FIELD_DECL node which
13837 represents a bit-field, the "offset" part of this special location
13838 descriptor must indicate the distance in bytes from the lowest-addressed
13839 byte of the containing struct or union type to the lowest-addressed byte of
13840 the "containing object" for the bit-field. (See the `field_byte_offset'
13841 function above).
13843 For any given bit-field, the "containing object" is a hypothetical object
13844 (of some integral or enum type) within which the given bit-field lives. The
13845 type of this hypothetical "containing object" is always the same as the
13846 declared type of the individual bit-field itself (for GCC anyway... the
13847 DWARF spec doesn't actually mandate this). Note that it is the size (in
13848 bytes) of the hypothetical "containing object" which will be given in the
13849 DW_AT_byte_size attribute for this bit-field. (See the
13850 `byte_size_attribute' function below.) It is also used when calculating the
13851 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
13852 function below.) */
13854 static void
13855 add_data_member_location_attribute (dw_die_ref die, tree decl)
13857 HOST_WIDE_INT offset;
13858 dw_loc_descr_ref loc_descr = 0;
13860 if (TREE_CODE (decl) == TREE_BINFO)
13862 /* We're working on the TAG_inheritance for a base class. */
13863 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
13865 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
13866 aren't at a fixed offset from all (sub)objects of the same
13867 type. We need to extract the appropriate offset from our
13868 vtable. The following dwarf expression means
13870 BaseAddr = ObAddr + *((*ObAddr) - Offset)
13872 This is specific to the V3 ABI, of course. */
13874 dw_loc_descr_ref tmp;
13876 /* Make a copy of the object address. */
13877 tmp = new_loc_descr (DW_OP_dup, 0, 0);
13878 add_loc_descr (&loc_descr, tmp);
13880 /* Extract the vtable address. */
13881 tmp = new_loc_descr (DW_OP_deref, 0, 0);
13882 add_loc_descr (&loc_descr, tmp);
13884 /* Calculate the address of the offset. */
13885 offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
13886 gcc_assert (offset < 0);
13888 tmp = int_loc_descriptor (-offset);
13889 add_loc_descr (&loc_descr, tmp);
13890 tmp = new_loc_descr (DW_OP_minus, 0, 0);
13891 add_loc_descr (&loc_descr, tmp);
13893 /* Extract the offset. */
13894 tmp = new_loc_descr (DW_OP_deref, 0, 0);
13895 add_loc_descr (&loc_descr, tmp);
13897 /* Add it to the object address. */
13898 tmp = new_loc_descr (DW_OP_plus, 0, 0);
13899 add_loc_descr (&loc_descr, tmp);
13901 else
13902 offset = tree_low_cst (BINFO_OFFSET (decl), 0);
13904 else
13905 offset = field_byte_offset (decl);
13907 if (! loc_descr)
13909 if (dwarf_version > 2)
13911 /* Don't need to output a location expression, just the constant. */
13912 if (offset < 0)
13913 add_AT_int (die, DW_AT_data_member_location, offset);
13914 else
13915 add_AT_unsigned (die, DW_AT_data_member_location, offset);
13916 return;
13918 else
13920 enum dwarf_location_atom op;
13922 /* The DWARF2 standard says that we should assume that the structure
13923 address is already on the stack, so we can specify a structure
13924 field address by using DW_OP_plus_uconst. */
13925 op = DW_OP_plus_uconst;
13926 loc_descr = new_loc_descr (op, offset, 0);
13930 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
13933 /* Writes integer values to dw_vec_const array. */
13935 static void
13936 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
13938 while (size != 0)
13940 *dest++ = val & 0xff;
13941 val >>= 8;
13942 --size;
13946 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
13948 static HOST_WIDE_INT
13949 extract_int (const unsigned char *src, unsigned int size)
13951 HOST_WIDE_INT val = 0;
13953 src += size;
13954 while (size != 0)
13956 val <<= 8;
13957 val |= *--src & 0xff;
13958 --size;
13960 return val;
13963 /* Writes double_int values to dw_vec_const array. */
13965 static void
13966 insert_double (double_int val, unsigned char *dest)
13968 unsigned char *p0 = dest;
13969 unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
13971 if (WORDS_BIG_ENDIAN)
13973 p0 = p1;
13974 p1 = dest;
13977 insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
13978 insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
13981 /* Writes floating point values to dw_vec_const array. */
13983 static void
13984 insert_float (const_rtx rtl, unsigned char *array)
13986 REAL_VALUE_TYPE rv;
13987 long val[4];
13988 int i;
13990 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
13991 real_to_target (val, &rv, GET_MODE (rtl));
13993 /* real_to_target puts 32-bit pieces in each long. Pack them. */
13994 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
13996 insert_int (val[i], 4, array);
13997 array += 4;
14001 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
14002 does not have a "location" either in memory or in a register. These
14003 things can arise in GNU C when a constant is passed as an actual parameter
14004 to an inlined function. They can also arise in C++ where declared
14005 constants do not necessarily get memory "homes". */
14007 static bool
14008 add_const_value_attribute (dw_die_ref die, rtx rtl)
14010 switch (GET_CODE (rtl))
14012 case CONST_INT:
14014 HOST_WIDE_INT val = INTVAL (rtl);
14016 if (val < 0)
14017 add_AT_int (die, DW_AT_const_value, val);
14018 else
14019 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
14021 return true;
14023 case CONST_DOUBLE:
14024 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
14025 floating-point constant. A CONST_DOUBLE is used whenever the
14026 constant requires more than one word in order to be adequately
14027 represented. */
14029 enum machine_mode mode = GET_MODE (rtl);
14031 if (SCALAR_FLOAT_MODE_P (mode))
14033 unsigned int length = GET_MODE_SIZE (mode);
14034 unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
14036 insert_float (rtl, array);
14037 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
14039 else
14040 add_AT_double (die, DW_AT_const_value,
14041 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
14043 return true;
14045 case CONST_VECTOR:
14047 enum machine_mode mode = GET_MODE (rtl);
14048 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
14049 unsigned int length = CONST_VECTOR_NUNITS (rtl);
14050 unsigned char *array = (unsigned char *) ggc_alloc_atomic
14051 (length * elt_size);
14052 unsigned int i;
14053 unsigned char *p;
14055 switch (GET_MODE_CLASS (mode))
14057 case MODE_VECTOR_INT:
14058 for (i = 0, p = array; i < length; i++, p += elt_size)
14060 rtx elt = CONST_VECTOR_ELT (rtl, i);
14061 double_int val = rtx_to_double_int (elt);
14063 if (elt_size <= sizeof (HOST_WIDE_INT))
14064 insert_int (val.to_shwi (), elt_size, p);
14065 else
14067 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
14068 insert_double (val, p);
14071 break;
14073 case MODE_VECTOR_FLOAT:
14074 for (i = 0, p = array; i < length; i++, p += elt_size)
14076 rtx elt = CONST_VECTOR_ELT (rtl, i);
14077 insert_float (elt, p);
14079 break;
14081 default:
14082 gcc_unreachable ();
14085 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
14087 return true;
14089 case CONST_STRING:
14090 if (dwarf_version >= 4 || !dwarf_strict)
14092 dw_loc_descr_ref loc_result;
14093 resolve_one_addr (&rtl, NULL);
14094 rtl_addr:
14095 loc_result = new_loc_descr (DW_OP_addr, 0, 0);
14096 loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
14097 loc_result->dw_loc_oprnd1.v.val_addr = rtl;
14098 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14099 add_AT_loc (die, DW_AT_location, loc_result);
14100 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
14101 return true;
14103 return false;
14105 case CONST:
14106 if (CONSTANT_P (XEXP (rtl, 0)))
14107 return add_const_value_attribute (die, XEXP (rtl, 0));
14108 /* FALLTHROUGH */
14109 case SYMBOL_REF:
14110 if (!const_ok_for_output (rtl))
14111 return false;
14112 case LABEL_REF:
14113 if (dwarf_version >= 4 || !dwarf_strict)
14114 goto rtl_addr;
14115 return false;
14117 case PLUS:
14118 /* In cases where an inlined instance of an inline function is passed
14119 the address of an `auto' variable (which is local to the caller) we
14120 can get a situation where the DECL_RTL of the artificial local
14121 variable (for the inlining) which acts as a stand-in for the
14122 corresponding formal parameter (of the inline function) will look
14123 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
14124 exactly a compile-time constant expression, but it isn't the address
14125 of the (artificial) local variable either. Rather, it represents the
14126 *value* which the artificial local variable always has during its
14127 lifetime. We currently have no way to represent such quasi-constant
14128 values in Dwarf, so for now we just punt and generate nothing. */
14129 return false;
14131 case HIGH:
14132 case CONST_FIXED:
14133 return false;
14135 case MEM:
14136 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
14137 && MEM_READONLY_P (rtl)
14138 && GET_MODE (rtl) == BLKmode)
14140 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
14141 return true;
14143 return false;
14145 default:
14146 /* No other kinds of rtx should be possible here. */
14147 gcc_unreachable ();
14149 return false;
14152 /* Determine whether the evaluation of EXPR references any variables
14153 or functions which aren't otherwise used (and therefore may not be
14154 output). */
14155 static tree
14156 reference_to_unused (tree * tp, int * walk_subtrees,
14157 void * data ATTRIBUTE_UNUSED)
14159 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
14160 *walk_subtrees = 0;
14162 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
14163 && ! TREE_ASM_WRITTEN (*tp))
14164 return *tp;
14165 /* ??? The C++ FE emits debug information for using decls, so
14166 putting gcc_unreachable here falls over. See PR31899. For now
14167 be conservative. */
14168 else if (!cgraph_global_info_ready
14169 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
14170 return *tp;
14171 else if (TREE_CODE (*tp) == VAR_DECL)
14173 struct varpool_node *node = varpool_get_node (*tp);
14174 if (!node || !node->analyzed)
14175 return *tp;
14177 else if (TREE_CODE (*tp) == FUNCTION_DECL
14178 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
14180 /* The call graph machinery must have finished analyzing,
14181 optimizing and gimplifying the CU by now.
14182 So if *TP has no call graph node associated
14183 to it, it means *TP will not be emitted. */
14184 if (!cgraph_get_node (*tp))
14185 return *tp;
14187 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
14188 return *tp;
14190 return NULL_TREE;
14193 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
14194 for use in a later add_const_value_attribute call. */
14196 static rtx
14197 rtl_for_decl_init (tree init, tree type)
14199 rtx rtl = NULL_RTX;
14201 STRIP_NOPS (init);
14203 /* If a variable is initialized with a string constant without embedded
14204 zeros, build CONST_STRING. */
14205 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
14207 tree enttype = TREE_TYPE (type);
14208 tree domain = TYPE_DOMAIN (type);
14209 enum machine_mode mode = TYPE_MODE (enttype);
14211 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
14212 && domain
14213 && integer_zerop (TYPE_MIN_VALUE (domain))
14214 && compare_tree_int (TYPE_MAX_VALUE (domain),
14215 TREE_STRING_LENGTH (init) - 1) == 0
14216 && ((size_t) TREE_STRING_LENGTH (init)
14217 == strlen (TREE_STRING_POINTER (init)) + 1))
14219 rtl = gen_rtx_CONST_STRING (VOIDmode,
14220 ggc_strdup (TREE_STRING_POINTER (init)));
14221 rtl = gen_rtx_MEM (BLKmode, rtl);
14222 MEM_READONLY_P (rtl) = 1;
14225 /* Other aggregates, and complex values, could be represented using
14226 CONCAT: FIXME! */
14227 else if (AGGREGATE_TYPE_P (type)
14228 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
14229 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
14230 || TREE_CODE (type) == COMPLEX_TYPE)
14232 /* Vectors only work if their mode is supported by the target.
14233 FIXME: generic vectors ought to work too. */
14234 else if (TREE_CODE (type) == VECTOR_TYPE
14235 && !VECTOR_MODE_P (TYPE_MODE (type)))
14237 /* If the initializer is something that we know will expand into an
14238 immediate RTL constant, expand it now. We must be careful not to
14239 reference variables which won't be output. */
14240 else if (initializer_constant_valid_p (init, type)
14241 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
14243 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
14244 possible. */
14245 if (TREE_CODE (type) == VECTOR_TYPE)
14246 switch (TREE_CODE (init))
14248 case VECTOR_CST:
14249 break;
14250 case CONSTRUCTOR:
14251 if (TREE_CONSTANT (init))
14253 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
14254 bool constant_p = true;
14255 tree value;
14256 unsigned HOST_WIDE_INT ix;
14258 /* Even when ctor is constant, it might contain non-*_CST
14259 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
14260 belong into VECTOR_CST nodes. */
14261 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
14262 if (!CONSTANT_CLASS_P (value))
14264 constant_p = false;
14265 break;
14268 if (constant_p)
14270 init = build_vector_from_ctor (type, elts);
14271 break;
14274 /* FALLTHRU */
14276 default:
14277 return NULL;
14280 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
14282 /* If expand_expr returns a MEM, it wasn't immediate. */
14283 gcc_assert (!rtl || !MEM_P (rtl));
14286 return rtl;
14289 /* Generate RTL for the variable DECL to represent its location. */
14291 static rtx
14292 rtl_for_decl_location (tree decl)
14294 rtx rtl;
14296 /* Here we have to decide where we are going to say the parameter "lives"
14297 (as far as the debugger is concerned). We only have a couple of
14298 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
14300 DECL_RTL normally indicates where the parameter lives during most of the
14301 activation of the function. If optimization is enabled however, this
14302 could be either NULL or else a pseudo-reg. Both of those cases indicate
14303 that the parameter doesn't really live anywhere (as far as the code
14304 generation parts of GCC are concerned) during most of the function's
14305 activation. That will happen (for example) if the parameter is never
14306 referenced within the function.
14308 We could just generate a location descriptor here for all non-NULL
14309 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
14310 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
14311 where DECL_RTL is NULL or is a pseudo-reg.
14313 Note however that we can only get away with using DECL_INCOMING_RTL as
14314 a backup substitute for DECL_RTL in certain limited cases. In cases
14315 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
14316 we can be sure that the parameter was passed using the same type as it is
14317 declared to have within the function, and that its DECL_INCOMING_RTL
14318 points us to a place where a value of that type is passed.
14320 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
14321 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
14322 because in these cases DECL_INCOMING_RTL points us to a value of some
14323 type which is *different* from the type of the parameter itself. Thus,
14324 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
14325 such cases, the debugger would end up (for example) trying to fetch a
14326 `float' from a place which actually contains the first part of a
14327 `double'. That would lead to really incorrect and confusing
14328 output at debug-time.
14330 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
14331 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
14332 are a couple of exceptions however. On little-endian machines we can
14333 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
14334 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
14335 an integral type that is smaller than TREE_TYPE (decl). These cases arise
14336 when (on a little-endian machine) a non-prototyped function has a
14337 parameter declared to be of type `short' or `char'. In such cases,
14338 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
14339 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
14340 passed `int' value. If the debugger then uses that address to fetch
14341 a `short' or a `char' (on a little-endian machine) the result will be
14342 the correct data, so we allow for such exceptional cases below.
14344 Note that our goal here is to describe the place where the given formal
14345 parameter lives during most of the function's activation (i.e. between the
14346 end of the prologue and the start of the epilogue). We'll do that as best
14347 as we can. Note however that if the given formal parameter is modified
14348 sometime during the execution of the function, then a stack backtrace (at
14349 debug-time) will show the function as having been called with the *new*
14350 value rather than the value which was originally passed in. This happens
14351 rarely enough that it is not a major problem, but it *is* a problem, and
14352 I'd like to fix it.
14354 A future version of dwarf2out.c may generate two additional attributes for
14355 any given DW_TAG_formal_parameter DIE which will describe the "passed
14356 type" and the "passed location" for the given formal parameter in addition
14357 to the attributes we now generate to indicate the "declared type" and the
14358 "active location" for each parameter. This additional set of attributes
14359 could be used by debuggers for stack backtraces. Separately, note that
14360 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
14361 This happens (for example) for inlined-instances of inline function formal
14362 parameters which are never referenced. This really shouldn't be
14363 happening. All PARM_DECL nodes should get valid non-NULL
14364 DECL_INCOMING_RTL values. FIXME. */
14366 /* Use DECL_RTL as the "location" unless we find something better. */
14367 rtl = DECL_RTL_IF_SET (decl);
14369 /* When generating abstract instances, ignore everything except
14370 constants, symbols living in memory, and symbols living in
14371 fixed registers. */
14372 if (! reload_completed)
14374 if (rtl
14375 && (CONSTANT_P (rtl)
14376 || (MEM_P (rtl)
14377 && CONSTANT_P (XEXP (rtl, 0)))
14378 || (REG_P (rtl)
14379 && TREE_CODE (decl) == VAR_DECL
14380 && TREE_STATIC (decl))))
14382 rtl = targetm.delegitimize_address (rtl);
14383 return rtl;
14385 rtl = NULL_RTX;
14387 else if (TREE_CODE (decl) == PARM_DECL)
14389 if (rtl == NULL_RTX
14390 || is_pseudo_reg (rtl)
14391 || (MEM_P (rtl)
14392 && is_pseudo_reg (XEXP (rtl, 0))
14393 && DECL_INCOMING_RTL (decl)
14394 && MEM_P (DECL_INCOMING_RTL (decl))
14395 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
14397 tree declared_type = TREE_TYPE (decl);
14398 tree passed_type = DECL_ARG_TYPE (decl);
14399 enum machine_mode dmode = TYPE_MODE (declared_type);
14400 enum machine_mode pmode = TYPE_MODE (passed_type);
14402 /* This decl represents a formal parameter which was optimized out.
14403 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
14404 all cases where (rtl == NULL_RTX) just below. */
14405 if (dmode == pmode)
14406 rtl = DECL_INCOMING_RTL (decl);
14407 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
14408 && SCALAR_INT_MODE_P (dmode)
14409 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
14410 && DECL_INCOMING_RTL (decl))
14412 rtx inc = DECL_INCOMING_RTL (decl);
14413 if (REG_P (inc))
14414 rtl = inc;
14415 else if (MEM_P (inc))
14417 if (BYTES_BIG_ENDIAN)
14418 rtl = adjust_address_nv (inc, dmode,
14419 GET_MODE_SIZE (pmode)
14420 - GET_MODE_SIZE (dmode));
14421 else
14422 rtl = inc;
14427 /* If the parm was passed in registers, but lives on the stack, then
14428 make a big endian correction if the mode of the type of the
14429 parameter is not the same as the mode of the rtl. */
14430 /* ??? This is the same series of checks that are made in dbxout.c before
14431 we reach the big endian correction code there. It isn't clear if all
14432 of these checks are necessary here, but keeping them all is the safe
14433 thing to do. */
14434 else if (MEM_P (rtl)
14435 && XEXP (rtl, 0) != const0_rtx
14436 && ! CONSTANT_P (XEXP (rtl, 0))
14437 /* Not passed in memory. */
14438 && !MEM_P (DECL_INCOMING_RTL (decl))
14439 /* Not passed by invisible reference. */
14440 && (!REG_P (XEXP (rtl, 0))
14441 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
14442 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
14443 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
14444 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
14445 #endif
14447 /* Big endian correction check. */
14448 && BYTES_BIG_ENDIAN
14449 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
14450 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
14451 < UNITS_PER_WORD))
14453 enum machine_mode addr_mode = get_address_mode (rtl);
14454 int offset = (UNITS_PER_WORD
14455 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
14457 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
14458 plus_constant (addr_mode, XEXP (rtl, 0), offset));
14461 else if (TREE_CODE (decl) == VAR_DECL
14462 && rtl
14463 && MEM_P (rtl)
14464 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
14465 && BYTES_BIG_ENDIAN)
14467 enum machine_mode addr_mode = get_address_mode (rtl);
14468 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
14469 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
14471 /* If a variable is declared "register" yet is smaller than
14472 a register, then if we store the variable to memory, it
14473 looks like we're storing a register-sized value, when in
14474 fact we are not. We need to adjust the offset of the
14475 storage location to reflect the actual value's bytes,
14476 else gdb will not be able to display it. */
14477 if (rsize > dsize)
14478 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
14479 plus_constant (addr_mode, XEXP (rtl, 0),
14480 rsize - dsize));
14483 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
14484 and will have been substituted directly into all expressions that use it.
14485 C does not have such a concept, but C++ and other languages do. */
14486 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
14487 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
14489 if (rtl)
14490 rtl = targetm.delegitimize_address (rtl);
14492 /* If we don't look past the constant pool, we risk emitting a
14493 reference to a constant pool entry that isn't referenced from
14494 code, and thus is not emitted. */
14495 if (rtl)
14496 rtl = avoid_constant_pool_reference (rtl);
14498 /* Try harder to get a rtl. If this symbol ends up not being emitted
14499 in the current CU, resolve_addr will remove the expression referencing
14500 it. */
14501 if (rtl == NULL_RTX
14502 && TREE_CODE (decl) == VAR_DECL
14503 && !DECL_EXTERNAL (decl)
14504 && TREE_STATIC (decl)
14505 && DECL_NAME (decl)
14506 && !DECL_HARD_REGISTER (decl)
14507 && DECL_MODE (decl) != VOIDmode)
14509 rtl = make_decl_rtl_for_debug (decl);
14510 if (!MEM_P (rtl)
14511 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
14512 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
14513 rtl = NULL_RTX;
14516 return rtl;
14519 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
14520 returned. If so, the decl for the COMMON block is returned, and the
14521 value is the offset into the common block for the symbol. */
14523 static tree
14524 fortran_common (tree decl, HOST_WIDE_INT *value)
14526 tree val_expr, cvar;
14527 enum machine_mode mode;
14528 HOST_WIDE_INT bitsize, bitpos;
14529 tree offset;
14530 int unsignedp, volatilep = 0;
14532 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
14533 it does not have a value (the offset into the common area), or if it
14534 is thread local (as opposed to global) then it isn't common, and shouldn't
14535 be handled as such. */
14536 if (TREE_CODE (decl) != VAR_DECL
14537 || !TREE_STATIC (decl)
14538 || !DECL_HAS_VALUE_EXPR_P (decl)
14539 || !is_fortran ())
14540 return NULL_TREE;
14542 val_expr = DECL_VALUE_EXPR (decl);
14543 if (TREE_CODE (val_expr) != COMPONENT_REF)
14544 return NULL_TREE;
14546 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
14547 &mode, &unsignedp, &volatilep, true);
14549 if (cvar == NULL_TREE
14550 || TREE_CODE (cvar) != VAR_DECL
14551 || DECL_ARTIFICIAL (cvar)
14552 || !TREE_PUBLIC (cvar))
14553 return NULL_TREE;
14555 *value = 0;
14556 if (offset != NULL)
14558 if (!host_integerp (offset, 0))
14559 return NULL_TREE;
14560 *value = tree_low_cst (offset, 0);
14562 if (bitpos != 0)
14563 *value += bitpos / BITS_PER_UNIT;
14565 return cvar;
14568 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
14569 data attribute for a variable or a parameter. We generate the
14570 DW_AT_const_value attribute only in those cases where the given variable
14571 or parameter does not have a true "location" either in memory or in a
14572 register. This can happen (for example) when a constant is passed as an
14573 actual argument in a call to an inline function. (It's possible that
14574 these things can crop up in other ways also.) Note that one type of
14575 constant value which can be passed into an inlined function is a constant
14576 pointer. This can happen for example if an actual argument in an inlined
14577 function call evaluates to a compile-time constant address.
14579 CACHE_P is true if it is worth caching the location list for DECL,
14580 so that future calls can reuse it rather than regenerate it from scratch.
14581 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
14582 since we will need to refer to them each time the function is inlined. */
14584 static bool
14585 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
14586 enum dwarf_attribute attr)
14588 rtx rtl;
14589 dw_loc_list_ref list;
14590 var_loc_list *loc_list;
14591 cached_dw_loc_list *cache;
14592 void **slot;
14594 if (TREE_CODE (decl) == ERROR_MARK)
14595 return false;
14597 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
14598 || TREE_CODE (decl) == RESULT_DECL);
14600 /* Try to get some constant RTL for this decl, and use that as the value of
14601 the location. */
14603 rtl = rtl_for_decl_location (decl);
14604 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
14605 && add_const_value_attribute (die, rtl))
14606 return true;
14608 /* See if we have single element location list that is equivalent to
14609 a constant value. That way we are better to use add_const_value_attribute
14610 rather than expanding constant value equivalent. */
14611 loc_list = lookup_decl_loc (decl);
14612 if (loc_list
14613 && loc_list->first
14614 && loc_list->first->next == NULL
14615 && NOTE_P (loc_list->first->loc)
14616 && NOTE_VAR_LOCATION (loc_list->first->loc)
14617 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
14619 struct var_loc_node *node;
14621 node = loc_list->first;
14622 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
14623 if (GET_CODE (rtl) == EXPR_LIST)
14624 rtl = XEXP (rtl, 0);
14625 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
14626 && add_const_value_attribute (die, rtl))
14627 return true;
14629 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
14630 list several times. See if we've already cached the contents. */
14631 list = NULL;
14632 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
14633 cache_p = false;
14634 if (cache_p)
14636 cache = (cached_dw_loc_list *)
14637 htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
14638 if (cache)
14639 list = cache->loc_list;
14641 if (list == NULL)
14643 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
14644 /* It is usually worth caching this result if the decl is from
14645 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
14646 if (cache_p && list && list->dw_loc_next)
14648 slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
14649 DECL_UID (decl), INSERT);
14650 cache = ggc_alloc_cleared_cached_dw_loc_list ();
14651 cache->decl_id = DECL_UID (decl);
14652 cache->loc_list = list;
14653 *slot = cache;
14656 if (list)
14658 add_AT_location_description (die, attr, list);
14659 return true;
14661 /* None of that worked, so it must not really have a location;
14662 try adding a constant value attribute from the DECL_INITIAL. */
14663 return tree_add_const_value_attribute_for_decl (die, decl);
14666 /* Add VARIABLE and DIE into deferred locations list. */
14668 static void
14669 defer_location (tree variable, dw_die_ref die)
14671 deferred_locations entry;
14672 entry.variable = variable;
14673 entry.die = die;
14674 VEC_safe_push (deferred_locations, gc, deferred_locations_list, entry);
14677 /* Helper function for tree_add_const_value_attribute. Natively encode
14678 initializer INIT into an array. Return true if successful. */
14680 static bool
14681 native_encode_initializer (tree init, unsigned char *array, int size)
14683 tree type;
14685 if (init == NULL_TREE)
14686 return false;
14688 STRIP_NOPS (init);
14689 switch (TREE_CODE (init))
14691 case STRING_CST:
14692 type = TREE_TYPE (init);
14693 if (TREE_CODE (type) == ARRAY_TYPE)
14695 tree enttype = TREE_TYPE (type);
14696 enum machine_mode mode = TYPE_MODE (enttype);
14698 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
14699 return false;
14700 if (int_size_in_bytes (type) != size)
14701 return false;
14702 if (size > TREE_STRING_LENGTH (init))
14704 memcpy (array, TREE_STRING_POINTER (init),
14705 TREE_STRING_LENGTH (init));
14706 memset (array + TREE_STRING_LENGTH (init),
14707 '\0', size - TREE_STRING_LENGTH (init));
14709 else
14710 memcpy (array, TREE_STRING_POINTER (init), size);
14711 return true;
14713 return false;
14714 case CONSTRUCTOR:
14715 type = TREE_TYPE (init);
14716 if (int_size_in_bytes (type) != size)
14717 return false;
14718 if (TREE_CODE (type) == ARRAY_TYPE)
14720 HOST_WIDE_INT min_index;
14721 unsigned HOST_WIDE_INT cnt;
14722 int curpos = 0, fieldsize;
14723 constructor_elt *ce;
14725 if (TYPE_DOMAIN (type) == NULL_TREE
14726 || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
14727 return false;
14729 fieldsize = int_size_in_bytes (TREE_TYPE (type));
14730 if (fieldsize <= 0)
14731 return false;
14733 min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
14734 memset (array, '\0', size);
14735 FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
14737 tree val = ce->value;
14738 tree index = ce->index;
14739 int pos = curpos;
14740 if (index && TREE_CODE (index) == RANGE_EXPR)
14741 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
14742 * fieldsize;
14743 else if (index)
14744 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
14746 if (val)
14748 STRIP_NOPS (val);
14749 if (!native_encode_initializer (val, array + pos, fieldsize))
14750 return false;
14752 curpos = pos + fieldsize;
14753 if (index && TREE_CODE (index) == RANGE_EXPR)
14755 int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
14756 - tree_low_cst (TREE_OPERAND (index, 0), 0);
14757 while (count-- > 0)
14759 if (val)
14760 memcpy (array + curpos, array + pos, fieldsize);
14761 curpos += fieldsize;
14764 gcc_assert (curpos <= size);
14766 return true;
14768 else if (TREE_CODE (type) == RECORD_TYPE
14769 || TREE_CODE (type) == UNION_TYPE)
14771 tree field = NULL_TREE;
14772 unsigned HOST_WIDE_INT cnt;
14773 constructor_elt *ce;
14775 if (int_size_in_bytes (type) != size)
14776 return false;
14778 if (TREE_CODE (type) == RECORD_TYPE)
14779 field = TYPE_FIELDS (type);
14781 FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
14783 tree val = ce->value;
14784 int pos, fieldsize;
14786 if (ce->index != 0)
14787 field = ce->index;
14789 if (val)
14790 STRIP_NOPS (val);
14792 if (field == NULL_TREE || DECL_BIT_FIELD (field))
14793 return false;
14795 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
14796 && TYPE_DOMAIN (TREE_TYPE (field))
14797 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
14798 return false;
14799 else if (DECL_SIZE_UNIT (field) == NULL_TREE
14800 || !host_integerp (DECL_SIZE_UNIT (field), 0))
14801 return false;
14802 fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
14803 pos = int_byte_position (field);
14804 gcc_assert (pos + fieldsize <= size);
14805 if (val
14806 && !native_encode_initializer (val, array + pos, fieldsize))
14807 return false;
14809 return true;
14811 return false;
14812 case VIEW_CONVERT_EXPR:
14813 case NON_LVALUE_EXPR:
14814 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
14815 default:
14816 return native_encode_expr (init, array, size) == size;
14820 /* Attach a DW_AT_const_value attribute to DIE. The value of the
14821 attribute is the const value T. */
14823 static bool
14824 tree_add_const_value_attribute (dw_die_ref die, tree t)
14826 tree init;
14827 tree type = TREE_TYPE (t);
14828 rtx rtl;
14830 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
14831 return false;
14833 init = t;
14834 gcc_assert (!DECL_P (init));
14836 rtl = rtl_for_decl_init (init, type);
14837 if (rtl)
14838 return add_const_value_attribute (die, rtl);
14839 /* If the host and target are sane, try harder. */
14840 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
14841 && initializer_constant_valid_p (init, type))
14843 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
14844 if (size > 0 && (int) size == size)
14846 unsigned char *array = (unsigned char *)
14847 ggc_alloc_cleared_atomic (size);
14849 if (native_encode_initializer (init, array, size))
14851 add_AT_vec (die, DW_AT_const_value, size, 1, array);
14852 return true;
14856 return false;
14859 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
14860 attribute is the const value of T, where T is an integral constant
14861 variable with static storage duration
14862 (so it can't be a PARM_DECL or a RESULT_DECL). */
14864 static bool
14865 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
14868 if (!decl
14869 || (TREE_CODE (decl) != VAR_DECL
14870 && TREE_CODE (decl) != CONST_DECL)
14871 || (TREE_CODE (decl) == VAR_DECL
14872 && !TREE_STATIC (decl)))
14873 return false;
14875 if (TREE_READONLY (decl)
14876 && ! TREE_THIS_VOLATILE (decl)
14877 && DECL_INITIAL (decl))
14878 /* OK */;
14879 else
14880 return false;
14882 /* Don't add DW_AT_const_value if abstract origin already has one. */
14883 if (get_AT (var_die, DW_AT_const_value))
14884 return false;
14886 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
14889 /* Convert the CFI instructions for the current function into a
14890 location list. This is used for DW_AT_frame_base when we targeting
14891 a dwarf2 consumer that does not support the dwarf3
14892 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
14893 expressions. */
14895 static dw_loc_list_ref
14896 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
14898 int ix;
14899 dw_fde_ref fde;
14900 dw_loc_list_ref list, *list_tail;
14901 dw_cfi_ref cfi;
14902 dw_cfa_location last_cfa, next_cfa;
14903 const char *start_label, *last_label, *section;
14904 dw_cfa_location remember;
14906 fde = cfun->fde;
14907 gcc_assert (fde != NULL);
14909 section = secname_for_decl (current_function_decl);
14910 list_tail = &list;
14911 list = NULL;
14913 memset (&next_cfa, 0, sizeof (next_cfa));
14914 next_cfa.reg = INVALID_REGNUM;
14915 remember = next_cfa;
14917 start_label = fde->dw_fde_begin;
14919 /* ??? Bald assumption that the CIE opcode list does not contain
14920 advance opcodes. */
14921 FOR_EACH_VEC_ELT (dw_cfi_ref, cie_cfi_vec, ix, cfi)
14922 lookup_cfa_1 (cfi, &next_cfa, &remember);
14924 last_cfa = next_cfa;
14925 last_label = start_label;
14927 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
14929 /* If the first partition contained no CFI adjustments, the
14930 CIE opcodes apply to the whole first partition. */
14931 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
14932 fde->dw_fde_begin, fde->dw_fde_end, section);
14933 list_tail =&(*list_tail)->dw_loc_next;
14934 start_label = last_label = fde->dw_fde_second_begin;
14937 FOR_EACH_VEC_ELT (dw_cfi_ref, fde->dw_fde_cfi, ix, cfi)
14939 switch (cfi->dw_cfi_opc)
14941 case DW_CFA_set_loc:
14942 case DW_CFA_advance_loc1:
14943 case DW_CFA_advance_loc2:
14944 case DW_CFA_advance_loc4:
14945 if (!cfa_equal_p (&last_cfa, &next_cfa))
14947 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
14948 start_label, last_label, section);
14950 list_tail = &(*list_tail)->dw_loc_next;
14951 last_cfa = next_cfa;
14952 start_label = last_label;
14954 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
14955 break;
14957 case DW_CFA_advance_loc:
14958 /* The encoding is complex enough that we should never emit this. */
14959 gcc_unreachable ();
14961 default:
14962 lookup_cfa_1 (cfi, &next_cfa, &remember);
14963 break;
14965 if (ix + 1 == fde->dw_fde_switch_cfi_index)
14967 if (!cfa_equal_p (&last_cfa, &next_cfa))
14969 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
14970 start_label, last_label, section);
14972 list_tail = &(*list_tail)->dw_loc_next;
14973 last_cfa = next_cfa;
14974 start_label = last_label;
14976 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
14977 start_label, fde->dw_fde_end, section);
14978 list_tail = &(*list_tail)->dw_loc_next;
14979 start_label = last_label = fde->dw_fde_second_begin;
14983 if (!cfa_equal_p (&last_cfa, &next_cfa))
14985 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
14986 start_label, last_label, section);
14987 list_tail = &(*list_tail)->dw_loc_next;
14988 start_label = last_label;
14991 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
14992 start_label,
14993 fde->dw_fde_second_begin
14994 ? fde->dw_fde_second_end : fde->dw_fde_end,
14995 section);
14997 if (list && list->dw_loc_next)
14998 gen_llsym (list);
15000 return list;
15003 /* Compute a displacement from the "steady-state frame pointer" to the
15004 frame base (often the same as the CFA), and store it in
15005 frame_pointer_fb_offset. OFFSET is added to the displacement
15006 before the latter is negated. */
15008 static void
15009 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
15011 rtx reg, elim;
15013 #ifdef FRAME_POINTER_CFA_OFFSET
15014 reg = frame_pointer_rtx;
15015 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
15016 #else
15017 reg = arg_pointer_rtx;
15018 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
15019 #endif
15021 elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
15022 if (GET_CODE (elim) == PLUS)
15024 offset += INTVAL (XEXP (elim, 1));
15025 elim = XEXP (elim, 0);
15028 frame_pointer_fb_offset = -offset;
15030 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
15031 in which to eliminate. This is because it's stack pointer isn't
15032 directly accessible as a register within the ISA. To work around
15033 this, assume that while we cannot provide a proper value for
15034 frame_pointer_fb_offset, we won't need one either. */
15035 frame_pointer_fb_offset_valid
15036 = ((SUPPORTS_STACK_ALIGNMENT
15037 && (elim == hard_frame_pointer_rtx
15038 || elim == stack_pointer_rtx))
15039 || elim == (frame_pointer_needed
15040 ? hard_frame_pointer_rtx
15041 : stack_pointer_rtx));
15044 /* Generate a DW_AT_name attribute given some string value to be included as
15045 the value of the attribute. */
15047 static void
15048 add_name_attribute (dw_die_ref die, const char *name_string)
15050 if (name_string != NULL && *name_string != 0)
15052 if (demangle_name_func)
15053 name_string = (*demangle_name_func) (name_string);
15055 add_AT_string (die, DW_AT_name, name_string);
15059 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
15060 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
15061 of TYPE accordingly.
15063 ??? This is a temporary measure until after we're able to generate
15064 regular DWARF for the complex Ada type system. */
15066 static void
15067 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
15068 dw_die_ref context_die)
15070 tree dtype;
15071 dw_die_ref dtype_die;
15073 if (!lang_hooks.types.descriptive_type)
15074 return;
15076 dtype = lang_hooks.types.descriptive_type (type);
15077 if (!dtype)
15078 return;
15080 dtype_die = lookup_type_die (dtype);
15081 if (!dtype_die)
15083 gen_type_die (dtype, context_die);
15084 dtype_die = lookup_type_die (dtype);
15085 gcc_assert (dtype_die);
15088 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
15091 /* Generate a DW_AT_comp_dir attribute for DIE. */
15093 static void
15094 add_comp_dir_attribute (dw_die_ref die)
15096 const char *wd = get_src_pwd ();
15097 char *wd1;
15099 if (wd == NULL)
15100 return;
15102 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
15104 int wdlen;
15106 wdlen = strlen (wd);
15107 wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
15108 strcpy (wd1, wd);
15109 wd1 [wdlen] = DIR_SEPARATOR;
15110 wd1 [wdlen + 1] = 0;
15111 wd = wd1;
15114 add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
15117 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
15118 default. */
15120 static int
15121 lower_bound_default (void)
15123 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
15125 case DW_LANG_C:
15126 case DW_LANG_C89:
15127 case DW_LANG_C99:
15128 case DW_LANG_C_plus_plus:
15129 case DW_LANG_ObjC:
15130 case DW_LANG_ObjC_plus_plus:
15131 case DW_LANG_Java:
15132 return 0;
15133 case DW_LANG_Fortran77:
15134 case DW_LANG_Fortran90:
15135 case DW_LANG_Fortran95:
15136 return 1;
15137 case DW_LANG_UPC:
15138 case DW_LANG_D:
15139 case DW_LANG_Python:
15140 return dwarf_version >= 4 ? 0 : -1;
15141 case DW_LANG_Ada95:
15142 case DW_LANG_Ada83:
15143 case DW_LANG_Cobol74:
15144 case DW_LANG_Cobol85:
15145 case DW_LANG_Pascal83:
15146 case DW_LANG_Modula2:
15147 case DW_LANG_PLI:
15148 return dwarf_version >= 4 ? 1 : -1;
15149 default:
15150 return -1;
15154 /* Given a tree node describing an array bound (either lower or upper) output
15155 a representation for that bound. */
15157 static void
15158 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
15160 switch (TREE_CODE (bound))
15162 case ERROR_MARK:
15163 return;
15165 /* All fixed-bounds are represented by INTEGER_CST nodes. */
15166 case INTEGER_CST:
15168 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
15169 int dflt;
15171 /* Use the default if possible. */
15172 if (bound_attr == DW_AT_lower_bound
15173 && host_integerp (bound, 0)
15174 && (dflt = lower_bound_default ()) != -1
15175 && tree_low_cst (bound, 0) == dflt)
15178 /* Otherwise represent the bound as an unsigned value with the
15179 precision of its type. The precision and signedness of the
15180 type will be necessary to re-interpret it unambiguously. */
15181 else if (prec < HOST_BITS_PER_WIDE_INT)
15183 unsigned HOST_WIDE_INT mask
15184 = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
15185 add_AT_unsigned (subrange_die, bound_attr,
15186 TREE_INT_CST_LOW (bound) & mask);
15188 else if (prec == HOST_BITS_PER_WIDE_INT
15189 || TREE_INT_CST_HIGH (bound) == 0)
15190 add_AT_unsigned (subrange_die, bound_attr,
15191 TREE_INT_CST_LOW (bound));
15192 else
15193 add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
15194 TREE_INT_CST_LOW (bound));
15196 break;
15198 CASE_CONVERT:
15199 case VIEW_CONVERT_EXPR:
15200 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
15201 break;
15203 case SAVE_EXPR:
15204 break;
15206 case VAR_DECL:
15207 case PARM_DECL:
15208 case RESULT_DECL:
15210 dw_die_ref decl_die = lookup_decl_die (bound);
15212 /* ??? Can this happen, or should the variable have been bound
15213 first? Probably it can, since I imagine that we try to create
15214 the types of parameters in the order in which they exist in
15215 the list, and won't have created a forward reference to a
15216 later parameter. */
15217 if (decl_die != NULL)
15219 add_AT_die_ref (subrange_die, bound_attr, decl_die);
15220 break;
15223 /* FALLTHRU */
15225 default:
15227 /* Otherwise try to create a stack operation procedure to
15228 evaluate the value of the array bound. */
15230 dw_die_ref ctx, decl_die;
15231 dw_loc_list_ref list;
15233 list = loc_list_from_tree (bound, 2);
15234 if (list == NULL || single_element_loc_list_p (list))
15236 /* If DW_AT_*bound is not a reference nor constant, it is
15237 a DWARF expression rather than location description.
15238 For that loc_list_from_tree (bound, 0) is needed.
15239 If that fails to give a single element list,
15240 fall back to outputting this as a reference anyway. */
15241 dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
15242 if (list2 && single_element_loc_list_p (list2))
15244 add_AT_loc (subrange_die, bound_attr, list2->expr);
15245 break;
15248 if (list == NULL)
15249 break;
15251 if (current_function_decl == 0)
15252 ctx = comp_unit_die ();
15253 else
15254 ctx = lookup_decl_die (current_function_decl);
15256 decl_die = new_die (DW_TAG_variable, ctx, bound);
15257 add_AT_flag (decl_die, DW_AT_artificial, 1);
15258 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
15259 add_AT_location_description (decl_die, DW_AT_location, list);
15260 add_AT_die_ref (subrange_die, bound_attr, decl_die);
15261 break;
15266 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
15267 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
15268 Note that the block of subscript information for an array type also
15269 includes information about the element type of the given array type. */
15271 static void
15272 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
15274 unsigned dimension_number;
15275 tree lower, upper;
15276 dw_die_ref subrange_die;
15278 for (dimension_number = 0;
15279 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
15280 type = TREE_TYPE (type), dimension_number++)
15282 tree domain = TYPE_DOMAIN (type);
15284 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
15285 break;
15287 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
15288 and (in GNU C only) variable bounds. Handle all three forms
15289 here. */
15290 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
15291 if (domain)
15293 /* We have an array type with specified bounds. */
15294 lower = TYPE_MIN_VALUE (domain);
15295 upper = TYPE_MAX_VALUE (domain);
15297 /* Define the index type. */
15298 if (TREE_TYPE (domain))
15300 /* ??? This is probably an Ada unnamed subrange type. Ignore the
15301 TREE_TYPE field. We can't emit debug info for this
15302 because it is an unnamed integral type. */
15303 if (TREE_CODE (domain) == INTEGER_TYPE
15304 && TYPE_NAME (domain) == NULL_TREE
15305 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
15306 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
15308 else
15309 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
15310 type_die);
15313 /* ??? If upper is NULL, the array has unspecified length,
15314 but it does have a lower bound. This happens with Fortran
15315 dimension arr(N:*)
15316 Since the debugger is definitely going to need to know N
15317 to produce useful results, go ahead and output the lower
15318 bound solo, and hope the debugger can cope. */
15320 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
15321 if (upper)
15322 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
15325 /* Otherwise we have an array type with an unspecified length. The
15326 DWARF-2 spec does not say how to handle this; let's just leave out the
15327 bounds. */
15331 static void
15332 add_byte_size_attribute (dw_die_ref die, tree tree_node)
15334 dw_die_ref decl_die;
15335 unsigned size;
15337 switch (TREE_CODE (tree_node))
15339 case ERROR_MARK:
15340 size = 0;
15341 break;
15342 case ENUMERAL_TYPE:
15343 case RECORD_TYPE:
15344 case UNION_TYPE:
15345 case QUAL_UNION_TYPE:
15346 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
15347 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
15349 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
15350 return;
15352 size = int_size_in_bytes (tree_node);
15353 break;
15354 case FIELD_DECL:
15355 /* For a data member of a struct or union, the DW_AT_byte_size is
15356 generally given as the number of bytes normally allocated for an
15357 object of the *declared* type of the member itself. This is true
15358 even for bit-fields. */
15359 size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
15360 break;
15361 default:
15362 gcc_unreachable ();
15365 /* Note that `size' might be -1 when we get to this point. If it is, that
15366 indicates that the byte size of the entity in question is variable. We
15367 have no good way of expressing this fact in Dwarf at the present time,
15368 so just let the -1 pass on through. */
15369 add_AT_unsigned (die, DW_AT_byte_size, size);
15372 /* For a FIELD_DECL node which represents a bit-field, output an attribute
15373 which specifies the distance in bits from the highest order bit of the
15374 "containing object" for the bit-field to the highest order bit of the
15375 bit-field itself.
15377 For any given bit-field, the "containing object" is a hypothetical object
15378 (of some integral or enum type) within which the given bit-field lives. The
15379 type of this hypothetical "containing object" is always the same as the
15380 declared type of the individual bit-field itself. The determination of the
15381 exact location of the "containing object" for a bit-field is rather
15382 complicated. It's handled by the `field_byte_offset' function (above).
15384 Note that it is the size (in bytes) of the hypothetical "containing object"
15385 which will be given in the DW_AT_byte_size attribute for this bit-field.
15386 (See `byte_size_attribute' above). */
15388 static inline void
15389 add_bit_offset_attribute (dw_die_ref die, tree decl)
15391 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
15392 tree type = DECL_BIT_FIELD_TYPE (decl);
15393 HOST_WIDE_INT bitpos_int;
15394 HOST_WIDE_INT highest_order_object_bit_offset;
15395 HOST_WIDE_INT highest_order_field_bit_offset;
15396 HOST_WIDE_INT bit_offset;
15398 /* Must be a field and a bit field. */
15399 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
15401 /* We can't yet handle bit-fields whose offsets are variable, so if we
15402 encounter such things, just return without generating any attribute
15403 whatsoever. Likewise for variable or too large size. */
15404 if (! host_integerp (bit_position (decl), 0)
15405 || ! host_integerp (DECL_SIZE (decl), 1))
15406 return;
15408 bitpos_int = int_bit_position (decl);
15410 /* Note that the bit offset is always the distance (in bits) from the
15411 highest-order bit of the "containing object" to the highest-order bit of
15412 the bit-field itself. Since the "high-order end" of any object or field
15413 is different on big-endian and little-endian machines, the computation
15414 below must take account of these differences. */
15415 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
15416 highest_order_field_bit_offset = bitpos_int;
15418 if (! BYTES_BIG_ENDIAN)
15420 highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
15421 highest_order_object_bit_offset += simple_type_size_in_bits (type);
15424 bit_offset
15425 = (! BYTES_BIG_ENDIAN
15426 ? highest_order_object_bit_offset - highest_order_field_bit_offset
15427 : highest_order_field_bit_offset - highest_order_object_bit_offset);
15429 if (bit_offset < 0)
15430 add_AT_int (die, DW_AT_bit_offset, bit_offset);
15431 else
15432 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
15435 /* For a FIELD_DECL node which represents a bit field, output an attribute
15436 which specifies the length in bits of the given field. */
15438 static inline void
15439 add_bit_size_attribute (dw_die_ref die, tree decl)
15441 /* Must be a field and a bit field. */
15442 gcc_assert (TREE_CODE (decl) == FIELD_DECL
15443 && DECL_BIT_FIELD_TYPE (decl));
15445 if (host_integerp (DECL_SIZE (decl), 1))
15446 add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
15449 /* If the compiled language is ANSI C, then add a 'prototyped'
15450 attribute, if arg types are given for the parameters of a function. */
15452 static inline void
15453 add_prototyped_attribute (dw_die_ref die, tree func_type)
15455 if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
15456 && prototype_p (func_type))
15457 add_AT_flag (die, DW_AT_prototyped, 1);
15460 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
15461 by looking in either the type declaration or object declaration
15462 equate table. */
15464 static inline dw_die_ref
15465 add_abstract_origin_attribute (dw_die_ref die, tree origin)
15467 dw_die_ref origin_die = NULL;
15469 if (TREE_CODE (origin) != FUNCTION_DECL)
15471 /* We may have gotten separated from the block for the inlined
15472 function, if we're in an exception handler or some such; make
15473 sure that the abstract function has been written out.
15475 Doing this for nested functions is wrong, however; functions are
15476 distinct units, and our context might not even be inline. */
15477 tree fn = origin;
15479 if (TYPE_P (fn))
15480 fn = TYPE_STUB_DECL (fn);
15482 fn = decl_function_context (fn);
15483 if (fn)
15484 dwarf2out_abstract_function (fn);
15487 if (DECL_P (origin))
15488 origin_die = lookup_decl_die (origin);
15489 else if (TYPE_P (origin))
15490 origin_die = lookup_type_die (origin);
15492 /* XXX: Functions that are never lowered don't always have correct block
15493 trees (in the case of java, they simply have no block tree, in some other
15494 languages). For these functions, there is nothing we can really do to
15495 output correct debug info for inlined functions in all cases. Rather
15496 than die, we'll just produce deficient debug info now, in that we will
15497 have variables without a proper abstract origin. In the future, when all
15498 functions are lowered, we should re-add a gcc_assert (origin_die)
15499 here. */
15501 if (origin_die)
15502 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
15503 return origin_die;
15506 /* We do not currently support the pure_virtual attribute. */
15508 static inline void
15509 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
15511 if (DECL_VINDEX (func_decl))
15513 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
15515 if (host_integerp (DECL_VINDEX (func_decl), 0))
15516 add_AT_loc (die, DW_AT_vtable_elem_location,
15517 new_loc_descr (DW_OP_constu,
15518 tree_low_cst (DECL_VINDEX (func_decl), 0),
15519 0));
15521 /* GNU extension: Record what type this method came from originally. */
15522 if (debug_info_level > DINFO_LEVEL_TERSE
15523 && DECL_CONTEXT (func_decl))
15524 add_AT_die_ref (die, DW_AT_containing_type,
15525 lookup_type_die (DECL_CONTEXT (func_decl)));
15529 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
15530 given decl. This used to be a vendor extension until after DWARF 4
15531 standardized it. */
15533 static void
15534 add_linkage_attr (dw_die_ref die, tree decl)
15536 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
15538 /* Mimic what assemble_name_raw does with a leading '*'. */
15539 if (name[0] == '*')
15540 name = &name[1];
15542 if (dwarf_version >= 4)
15543 add_AT_string (die, DW_AT_linkage_name, name);
15544 else
15545 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
15548 /* Add source coordinate attributes for the given decl. */
15550 static void
15551 add_src_coords_attributes (dw_die_ref die, tree decl)
15553 expanded_location s;
15555 if (DECL_SOURCE_LOCATION (decl) == UNKNOWN_LOCATION)
15556 return;
15557 s = expand_location (DECL_SOURCE_LOCATION (decl));
15558 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
15559 add_AT_unsigned (die, DW_AT_decl_line, s.line);
15562 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
15564 static void
15565 add_linkage_name (dw_die_ref die, tree decl)
15567 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
15568 && TREE_PUBLIC (decl)
15569 && !DECL_ABSTRACT (decl)
15570 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
15571 && die->die_tag != DW_TAG_member)
15573 /* Defer until we have an assembler name set. */
15574 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
15576 limbo_die_node *asm_name;
15578 asm_name = ggc_alloc_cleared_limbo_die_node ();
15579 asm_name->die = die;
15580 asm_name->created_for = decl;
15581 asm_name->next = deferred_asm_name;
15582 deferred_asm_name = asm_name;
15584 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
15585 add_linkage_attr (die, decl);
15589 /* Add a DW_AT_name attribute and source coordinate attribute for the
15590 given decl, but only if it actually has a name. */
15592 static void
15593 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
15595 tree decl_name;
15597 decl_name = DECL_NAME (decl);
15598 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
15600 const char *name = dwarf2_name (decl, 0);
15601 if (name)
15602 add_name_attribute (die, name);
15603 if (! DECL_ARTIFICIAL (decl))
15604 add_src_coords_attributes (die, decl);
15606 add_linkage_name (die, decl);
15609 #ifdef VMS_DEBUGGING_INFO
15610 /* Get the function's name, as described by its RTL. This may be different
15611 from the DECL_NAME name used in the source file. */
15612 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
15614 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
15615 XEXP (DECL_RTL (decl), 0));
15616 VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
15618 #endif /* VMS_DEBUGGING_INFO */
15621 #ifdef VMS_DEBUGGING_INFO
15622 /* Output the debug main pointer die for VMS */
15624 void
15625 dwarf2out_vms_debug_main_pointer (void)
15627 char label[MAX_ARTIFICIAL_LABEL_BYTES];
15628 dw_die_ref die;
15630 /* Allocate the VMS debug main subprogram die. */
15631 die = ggc_alloc_cleared_die_node ();
15632 die->die_tag = DW_TAG_subprogram;
15633 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
15634 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
15635 current_function_funcdef_no);
15636 add_AT_lbl_id (die, DW_AT_entry_pc, label);
15638 /* Make it the first child of comp_unit_die (). */
15639 die->die_parent = comp_unit_die ();
15640 if (comp_unit_die ()->die_child)
15642 die->die_sib = comp_unit_die ()->die_child->die_sib;
15643 comp_unit_die ()->die_child->die_sib = die;
15645 else
15647 die->die_sib = die;
15648 comp_unit_die ()->die_child = die;
15651 #endif /* VMS_DEBUGGING_INFO */
15653 /* Push a new declaration scope. */
15655 static void
15656 push_decl_scope (tree scope)
15658 VEC_safe_push (tree, gc, decl_scope_table, scope);
15661 /* Pop a declaration scope. */
15663 static inline void
15664 pop_decl_scope (void)
15666 VEC_pop (tree, decl_scope_table);
15669 /* walk_tree helper function for uses_local_type, below. */
15671 static tree
15672 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
15674 if (!TYPE_P (*tp))
15675 *walk_subtrees = 0;
15676 else
15678 tree name = TYPE_NAME (*tp);
15679 if (name && DECL_P (name) && decl_function_context (name))
15680 return *tp;
15682 return NULL_TREE;
15685 /* If TYPE involves a function-local type (including a local typedef to a
15686 non-local type), returns that type; otherwise returns NULL_TREE. */
15688 static tree
15689 uses_local_type (tree type)
15691 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
15692 return used;
15695 /* Return the DIE for the scope that immediately contains this type.
15696 Non-named types that do not involve a function-local type get global
15697 scope. Named types nested in namespaces or other types get their
15698 containing scope. All other types (i.e. function-local named types) get
15699 the current active scope. */
15701 static dw_die_ref
15702 scope_die_for (tree t, dw_die_ref context_die)
15704 dw_die_ref scope_die = NULL;
15705 tree containing_scope;
15707 /* Non-types always go in the current scope. */
15708 gcc_assert (TYPE_P (t));
15710 /* Use the scope of the typedef, rather than the scope of the type
15711 it refers to. */
15712 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
15713 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
15714 else
15715 containing_scope = TYPE_CONTEXT (t);
15717 /* Use the containing namespace if there is one. */
15718 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
15720 if (context_die == lookup_decl_die (containing_scope))
15721 /* OK */;
15722 else if (debug_info_level > DINFO_LEVEL_TERSE)
15723 context_die = get_context_die (containing_scope);
15724 else
15725 containing_scope = NULL_TREE;
15728 /* Ignore function type "scopes" from the C frontend. They mean that
15729 a tagged type is local to a parmlist of a function declarator, but
15730 that isn't useful to DWARF. */
15731 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
15732 containing_scope = NULL_TREE;
15734 if (SCOPE_FILE_SCOPE_P (containing_scope))
15736 /* If T uses a local type keep it local as well, to avoid references
15737 to function-local DIEs from outside the function. */
15738 if (current_function_decl && uses_local_type (t))
15739 scope_die = context_die;
15740 else
15741 scope_die = comp_unit_die ();
15743 else if (TYPE_P (containing_scope))
15745 /* For types, we can just look up the appropriate DIE. */
15746 if (debug_info_level > DINFO_LEVEL_TERSE)
15747 scope_die = get_context_die (containing_scope);
15748 else
15750 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
15751 if (scope_die == NULL)
15752 scope_die = comp_unit_die ();
15755 else
15756 scope_die = context_die;
15758 return scope_die;
15761 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
15763 static inline int
15764 local_scope_p (dw_die_ref context_die)
15766 for (; context_die; context_die = context_die->die_parent)
15767 if (context_die->die_tag == DW_TAG_inlined_subroutine
15768 || context_die->die_tag == DW_TAG_subprogram)
15769 return 1;
15771 return 0;
15774 /* Returns nonzero if CONTEXT_DIE is a class. */
15776 static inline int
15777 class_scope_p (dw_die_ref context_die)
15779 return (context_die
15780 && (context_die->die_tag == DW_TAG_structure_type
15781 || context_die->die_tag == DW_TAG_class_type
15782 || context_die->die_tag == DW_TAG_interface_type
15783 || context_die->die_tag == DW_TAG_union_type));
15786 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
15787 whether or not to treat a DIE in this context as a declaration. */
15789 static inline int
15790 class_or_namespace_scope_p (dw_die_ref context_die)
15792 return (class_scope_p (context_die)
15793 || (context_die && context_die->die_tag == DW_TAG_namespace));
15796 /* Many forms of DIEs require a "type description" attribute. This
15797 routine locates the proper "type descriptor" die for the type given
15798 by 'type', and adds a DW_AT_type attribute below the given die. */
15800 static void
15801 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
15802 int decl_volatile, dw_die_ref context_die)
15804 enum tree_code code = TREE_CODE (type);
15805 dw_die_ref type_die = NULL;
15807 /* ??? If this type is an unnamed subrange type of an integral, floating-point
15808 or fixed-point type, use the inner type. This is because we have no
15809 support for unnamed types in base_type_die. This can happen if this is
15810 an Ada subrange type. Correct solution is emit a subrange type die. */
15811 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
15812 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
15813 type = TREE_TYPE (type), code = TREE_CODE (type);
15815 if (code == ERROR_MARK
15816 /* Handle a special case. For functions whose return type is void, we
15817 generate *no* type attribute. (Note that no object may have type
15818 `void', so this only applies to function return types). */
15819 || code == VOID_TYPE)
15820 return;
15822 type_die = modified_type_die (type,
15823 decl_const || TYPE_READONLY (type),
15824 decl_volatile || TYPE_VOLATILE (type),
15825 context_die);
15827 if (type_die != NULL)
15828 add_AT_die_ref (object_die, DW_AT_type, type_die);
15831 /* Given an object die, add the calling convention attribute for the
15832 function call type. */
15833 static void
15834 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
15836 enum dwarf_calling_convention value = DW_CC_normal;
15838 value = ((enum dwarf_calling_convention)
15839 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
15841 if (is_fortran ()
15842 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
15844 /* DWARF 2 doesn't provide a way to identify a program's source-level
15845 entry point. DW_AT_calling_convention attributes are only meant
15846 to describe functions' calling conventions. However, lacking a
15847 better way to signal the Fortran main program, we used this for
15848 a long time, following existing custom. Now, DWARF 4 has
15849 DW_AT_main_subprogram, which we add below, but some tools still
15850 rely on the old way, which we thus keep. */
15851 value = DW_CC_program;
15853 if (dwarf_version >= 4 || !dwarf_strict)
15854 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
15857 /* Only add the attribute if the backend requests it, and
15858 is not DW_CC_normal. */
15859 if (value && (value != DW_CC_normal))
15860 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
15863 /* Given a tree pointer to a struct, class, union, or enum type node, return
15864 a pointer to the (string) tag name for the given type, or zero if the type
15865 was declared without a tag. */
15867 static const char *
15868 type_tag (const_tree type)
15870 const char *name = 0;
15872 if (TYPE_NAME (type) != 0)
15874 tree t = 0;
15876 /* Find the IDENTIFIER_NODE for the type name. */
15877 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
15878 && !TYPE_NAMELESS (type))
15879 t = TYPE_NAME (type);
15881 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
15882 a TYPE_DECL node, regardless of whether or not a `typedef' was
15883 involved. */
15884 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
15885 && ! DECL_IGNORED_P (TYPE_NAME (type)))
15887 /* We want to be extra verbose. Don't call dwarf_name if
15888 DECL_NAME isn't set. The default hook for decl_printable_name
15889 doesn't like that, and in this context it's correct to return
15890 0, instead of "<anonymous>" or the like. */
15891 if (DECL_NAME (TYPE_NAME (type))
15892 && !DECL_NAMELESS (TYPE_NAME (type)))
15893 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
15896 /* Now get the name as a string, or invent one. */
15897 if (!name && t != 0)
15898 name = IDENTIFIER_POINTER (t);
15901 return (name == 0 || *name == '\0') ? 0 : name;
15904 /* Return the type associated with a data member, make a special check
15905 for bit field types. */
15907 static inline tree
15908 member_declared_type (const_tree member)
15910 return (DECL_BIT_FIELD_TYPE (member)
15911 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
15914 /* Get the decl's label, as described by its RTL. This may be different
15915 from the DECL_NAME name used in the source file. */
15917 #if 0
15918 static const char *
15919 decl_start_label (tree decl)
15921 rtx x;
15922 const char *fnname;
15924 x = DECL_RTL (decl);
15925 gcc_assert (MEM_P (x));
15927 x = XEXP (x, 0);
15928 gcc_assert (GET_CODE (x) == SYMBOL_REF);
15930 fnname = XSTR (x, 0);
15931 return fnname;
15933 #endif
15935 /* These routines generate the internal representation of the DIE's for
15936 the compilation unit. Debugging information is collected by walking
15937 the declaration trees passed in from dwarf2out_decl(). */
15939 static void
15940 gen_array_type_die (tree type, dw_die_ref context_die)
15942 dw_die_ref scope_die = scope_die_for (type, context_die);
15943 dw_die_ref array_die;
15945 /* GNU compilers represent multidimensional array types as sequences of one
15946 dimensional array types whose element types are themselves array types.
15947 We sometimes squish that down to a single array_type DIE with multiple
15948 subscripts in the Dwarf debugging info. The draft Dwarf specification
15949 say that we are allowed to do this kind of compression in C, because
15950 there is no difference between an array of arrays and a multidimensional
15951 array. We don't do this for Ada to remain as close as possible to the
15952 actual representation, which is especially important against the language
15953 flexibilty wrt arrays of variable size. */
15955 bool collapse_nested_arrays = !is_ada ();
15956 tree element_type;
15958 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
15959 DW_TAG_string_type doesn't have DW_AT_type attribute). */
15960 if (TYPE_STRING_FLAG (type)
15961 && TREE_CODE (type) == ARRAY_TYPE
15962 && is_fortran ()
15963 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
15965 HOST_WIDE_INT size;
15967 array_die = new_die (DW_TAG_string_type, scope_die, type);
15968 add_name_attribute (array_die, type_tag (type));
15969 equate_type_number_to_die (type, array_die);
15970 size = int_size_in_bytes (type);
15971 if (size >= 0)
15972 add_AT_unsigned (array_die, DW_AT_byte_size, size);
15973 else if (TYPE_DOMAIN (type) != NULL_TREE
15974 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
15975 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
15977 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
15978 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
15980 size = int_size_in_bytes (TREE_TYPE (szdecl));
15981 if (loc && size > 0)
15983 add_AT_location_description (array_die, DW_AT_string_length, loc);
15984 if (size != DWARF2_ADDR_SIZE)
15985 add_AT_unsigned (array_die, DW_AT_byte_size, size);
15988 return;
15991 array_die = new_die (DW_TAG_array_type, scope_die, type);
15992 add_name_attribute (array_die, type_tag (type));
15993 equate_type_number_to_die (type, array_die);
15995 if (TREE_CODE (type) == VECTOR_TYPE)
15996 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
15998 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
15999 if (is_fortran ()
16000 && TREE_CODE (type) == ARRAY_TYPE
16001 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
16002 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
16003 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16005 #if 0
16006 /* We default the array ordering. SDB will probably do
16007 the right things even if DW_AT_ordering is not present. It's not even
16008 an issue until we start to get into multidimensional arrays anyway. If
16009 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
16010 then we'll have to put the DW_AT_ordering attribute back in. (But if
16011 and when we find out that we need to put these in, we will only do so
16012 for multidimensional arrays. */
16013 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
16014 #endif
16016 if (TREE_CODE (type) == VECTOR_TYPE)
16018 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
16019 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
16020 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
16021 add_bound_info (subrange_die, DW_AT_upper_bound,
16022 size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
16024 else
16025 add_subscript_info (array_die, type, collapse_nested_arrays);
16027 /* Add representation of the type of the elements of this array type and
16028 emit the corresponding DIE if we haven't done it already. */
16029 element_type = TREE_TYPE (type);
16030 if (collapse_nested_arrays)
16031 while (TREE_CODE (element_type) == ARRAY_TYPE)
16033 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
16034 break;
16035 element_type = TREE_TYPE (element_type);
16038 add_type_attribute (array_die, element_type, 0, 0, context_die);
16040 add_gnat_descriptive_type_attribute (array_die, type, context_die);
16041 if (TYPE_ARTIFICIAL (type))
16042 add_AT_flag (array_die, DW_AT_artificial, 1);
16044 if (get_AT (array_die, DW_AT_name))
16045 add_pubtype (type, array_die);
16048 static dw_loc_descr_ref
16049 descr_info_loc (tree val, tree base_decl)
16051 HOST_WIDE_INT size;
16052 dw_loc_descr_ref loc, loc2;
16053 enum dwarf_location_atom op;
16055 if (val == base_decl)
16056 return new_loc_descr (DW_OP_push_object_address, 0, 0);
16058 switch (TREE_CODE (val))
16060 CASE_CONVERT:
16061 return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16062 case VAR_DECL:
16063 return loc_descriptor_from_tree (val, 0);
16064 case INTEGER_CST:
16065 if (host_integerp (val, 0))
16066 return int_loc_descriptor (tree_low_cst (val, 0));
16067 break;
16068 case INDIRECT_REF:
16069 size = int_size_in_bytes (TREE_TYPE (val));
16070 if (size < 0)
16071 break;
16072 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16073 if (!loc)
16074 break;
16075 if (size == DWARF2_ADDR_SIZE)
16076 add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
16077 else
16078 add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
16079 return loc;
16080 case POINTER_PLUS_EXPR:
16081 case PLUS_EXPR:
16082 if (host_integerp (TREE_OPERAND (val, 1), 1)
16083 && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
16084 < 16384)
16086 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16087 if (!loc)
16088 break;
16089 loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
16091 else
16093 op = DW_OP_plus;
16094 do_binop:
16095 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16096 if (!loc)
16097 break;
16098 loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
16099 if (!loc2)
16100 break;
16101 add_loc_descr (&loc, loc2);
16102 add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
16104 return loc;
16105 case MINUS_EXPR:
16106 op = DW_OP_minus;
16107 goto do_binop;
16108 case MULT_EXPR:
16109 op = DW_OP_mul;
16110 goto do_binop;
16111 case EQ_EXPR:
16112 op = DW_OP_eq;
16113 goto do_binop;
16114 case NE_EXPR:
16115 op = DW_OP_ne;
16116 goto do_binop;
16117 default:
16118 break;
16120 return NULL;
16123 static void
16124 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
16125 tree val, tree base_decl)
16127 dw_loc_descr_ref loc;
16129 if (host_integerp (val, 0))
16131 add_AT_unsigned (die, attr, tree_low_cst (val, 0));
16132 return;
16135 loc = descr_info_loc (val, base_decl);
16136 if (!loc)
16137 return;
16139 add_AT_loc (die, attr, loc);
16142 /* This routine generates DIE for array with hidden descriptor, details
16143 are filled into *info by a langhook. */
16145 static void
16146 gen_descr_array_type_die (tree type, struct array_descr_info *info,
16147 dw_die_ref context_die)
16149 dw_die_ref scope_die = scope_die_for (type, context_die);
16150 dw_die_ref array_die;
16151 int dim;
16153 array_die = new_die (DW_TAG_array_type, scope_die, type);
16154 add_name_attribute (array_die, type_tag (type));
16155 equate_type_number_to_die (type, array_die);
16157 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
16158 if (is_fortran ()
16159 && info->ndimensions >= 2)
16160 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16162 if (info->data_location)
16163 add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
16164 info->base_decl);
16165 if (info->associated)
16166 add_descr_info_field (array_die, DW_AT_associated, info->associated,
16167 info->base_decl);
16168 if (info->allocated)
16169 add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
16170 info->base_decl);
16172 for (dim = 0; dim < info->ndimensions; dim++)
16174 dw_die_ref subrange_die
16175 = new_die (DW_TAG_subrange_type, array_die, NULL);
16177 if (info->dimen[dim].lower_bound)
16179 /* If it is the default value, omit it. */
16180 int dflt;
16182 if (host_integerp (info->dimen[dim].lower_bound, 0)
16183 && (dflt = lower_bound_default ()) != -1
16184 && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
16186 else
16187 add_descr_info_field (subrange_die, DW_AT_lower_bound,
16188 info->dimen[dim].lower_bound,
16189 info->base_decl);
16191 if (info->dimen[dim].upper_bound)
16192 add_descr_info_field (subrange_die, DW_AT_upper_bound,
16193 info->dimen[dim].upper_bound,
16194 info->base_decl);
16195 if (info->dimen[dim].stride)
16196 add_descr_info_field (subrange_die, DW_AT_byte_stride,
16197 info->dimen[dim].stride,
16198 info->base_decl);
16201 gen_type_die (info->element_type, context_die);
16202 add_type_attribute (array_die, info->element_type, 0, 0, context_die);
16204 if (get_AT (array_die, DW_AT_name))
16205 add_pubtype (type, array_die);
16208 #if 0
16209 static void
16210 gen_entry_point_die (tree decl, dw_die_ref context_die)
16212 tree origin = decl_ultimate_origin (decl);
16213 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
16215 if (origin != NULL)
16216 add_abstract_origin_attribute (decl_die, origin);
16217 else
16219 add_name_and_src_coords_attributes (decl_die, decl);
16220 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
16221 0, 0, context_die);
16224 if (DECL_ABSTRACT (decl))
16225 equate_decl_number_to_die (decl, decl_die);
16226 else
16227 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
16229 #endif
16231 /* Walk through the list of incomplete types again, trying once more to
16232 emit full debugging info for them. */
16234 static void
16235 retry_incomplete_types (void)
16237 int i;
16239 for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
16240 if (should_emit_struct_debug (VEC_index (tree, incomplete_types, i),
16241 DINFO_USAGE_DIR_USE))
16242 gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die ());
16245 /* Determine what tag to use for a record type. */
16247 static enum dwarf_tag
16248 record_type_tag (tree type)
16250 if (! lang_hooks.types.classify_record)
16251 return DW_TAG_structure_type;
16253 switch (lang_hooks.types.classify_record (type))
16255 case RECORD_IS_STRUCT:
16256 return DW_TAG_structure_type;
16258 case RECORD_IS_CLASS:
16259 return DW_TAG_class_type;
16261 case RECORD_IS_INTERFACE:
16262 if (dwarf_version >= 3 || !dwarf_strict)
16263 return DW_TAG_interface_type;
16264 return DW_TAG_structure_type;
16266 default:
16267 gcc_unreachable ();
16271 /* Generate a DIE to represent an enumeration type. Note that these DIEs
16272 include all of the information about the enumeration values also. Each
16273 enumerated type name/value is listed as a child of the enumerated type
16274 DIE. */
16276 static dw_die_ref
16277 gen_enumeration_type_die (tree type, dw_die_ref context_die)
16279 dw_die_ref type_die = lookup_type_die (type);
16281 if (type_die == NULL)
16283 type_die = new_die (DW_TAG_enumeration_type,
16284 scope_die_for (type, context_die), type);
16285 equate_type_number_to_die (type, type_die);
16286 add_name_attribute (type_die, type_tag (type));
16287 if (dwarf_version >= 4 || !dwarf_strict)
16289 if (ENUM_IS_SCOPED (type))
16290 add_AT_flag (type_die, DW_AT_enum_class, 1);
16291 if (ENUM_IS_OPAQUE (type))
16292 add_AT_flag (type_die, DW_AT_declaration, 1);
16295 else if (! TYPE_SIZE (type))
16296 return type_die;
16297 else
16298 remove_AT (type_die, DW_AT_declaration);
16300 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
16301 given enum type is incomplete, do not generate the DW_AT_byte_size
16302 attribute or the DW_AT_element_list attribute. */
16303 if (TYPE_SIZE (type))
16305 tree link;
16307 TREE_ASM_WRITTEN (type) = 1;
16308 add_byte_size_attribute (type_die, type);
16309 if (TYPE_STUB_DECL (type) != NULL_TREE)
16311 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
16312 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
16315 /* If the first reference to this type was as the return type of an
16316 inline function, then it may not have a parent. Fix this now. */
16317 if (type_die->die_parent == NULL)
16318 add_child_die (scope_die_for (type, context_die), type_die);
16320 for (link = TYPE_VALUES (type);
16321 link != NULL; link = TREE_CHAIN (link))
16323 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
16324 tree value = TREE_VALUE (link);
16326 add_name_attribute (enum_die,
16327 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
16329 if (TREE_CODE (value) == CONST_DECL)
16330 value = DECL_INITIAL (value);
16332 if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
16333 /* DWARF2 does not provide a way of indicating whether or
16334 not enumeration constants are signed or unsigned. GDB
16335 always assumes the values are signed, so we output all
16336 values as if they were signed. That means that
16337 enumeration constants with very large unsigned values
16338 will appear to have negative values in the debugger. */
16339 add_AT_int (enum_die, DW_AT_const_value,
16340 tree_low_cst (value, tree_int_cst_sgn (value) > 0));
16343 add_gnat_descriptive_type_attribute (type_die, type, context_die);
16344 if (TYPE_ARTIFICIAL (type))
16345 add_AT_flag (type_die, DW_AT_artificial, 1);
16347 else
16348 add_AT_flag (type_die, DW_AT_declaration, 1);
16350 add_pubtype (type, type_die);
16352 return type_die;
16355 /* Generate a DIE to represent either a real live formal parameter decl or to
16356 represent just the type of some formal parameter position in some function
16357 type.
16359 Note that this routine is a bit unusual because its argument may be a
16360 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
16361 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
16362 node. If it's the former then this function is being called to output a
16363 DIE to represent a formal parameter object (or some inlining thereof). If
16364 it's the latter, then this function is only being called to output a
16365 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
16366 argument type of some subprogram type.
16367 If EMIT_NAME_P is true, name and source coordinate attributes
16368 are emitted. */
16370 static dw_die_ref
16371 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
16372 dw_die_ref context_die)
16374 tree node_or_origin = node ? node : origin;
16375 tree ultimate_origin;
16376 dw_die_ref parm_die
16377 = new_die (DW_TAG_formal_parameter, context_die, node);
16379 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
16381 case tcc_declaration:
16382 ultimate_origin = decl_ultimate_origin (node_or_origin);
16383 if (node || ultimate_origin)
16384 origin = ultimate_origin;
16385 if (origin != NULL)
16386 add_abstract_origin_attribute (parm_die, origin);
16387 else if (emit_name_p)
16388 add_name_and_src_coords_attributes (parm_die, node);
16389 if (origin == NULL
16390 || (! DECL_ABSTRACT (node_or_origin)
16391 && variably_modified_type_p (TREE_TYPE (node_or_origin),
16392 decl_function_context
16393 (node_or_origin))))
16395 tree type = TREE_TYPE (node_or_origin);
16396 if (decl_by_reference_p (node_or_origin))
16397 add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
16398 context_die);
16399 else
16400 add_type_attribute (parm_die, type,
16401 TREE_READONLY (node_or_origin),
16402 TREE_THIS_VOLATILE (node_or_origin),
16403 context_die);
16405 if (origin == NULL && DECL_ARTIFICIAL (node))
16406 add_AT_flag (parm_die, DW_AT_artificial, 1);
16408 if (node && node != origin)
16409 equate_decl_number_to_die (node, parm_die);
16410 if (! DECL_ABSTRACT (node_or_origin))
16411 add_location_or_const_value_attribute (parm_die, node_or_origin,
16412 node == NULL, DW_AT_location);
16414 break;
16416 case tcc_type:
16417 /* We were called with some kind of a ..._TYPE node. */
16418 add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
16419 break;
16421 default:
16422 gcc_unreachable ();
16425 return parm_die;
16428 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
16429 children DW_TAG_formal_parameter DIEs representing the arguments of the
16430 parameter pack.
16432 PARM_PACK must be a function parameter pack.
16433 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
16434 must point to the subsequent arguments of the function PACK_ARG belongs to.
16435 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
16436 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
16437 following the last one for which a DIE was generated. */
16439 static dw_die_ref
16440 gen_formal_parameter_pack_die (tree parm_pack,
16441 tree pack_arg,
16442 dw_die_ref subr_die,
16443 tree *next_arg)
16445 tree arg;
16446 dw_die_ref parm_pack_die;
16448 gcc_assert (parm_pack
16449 && lang_hooks.function_parameter_pack_p (parm_pack)
16450 && subr_die);
16452 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
16453 add_src_coords_attributes (parm_pack_die, parm_pack);
16455 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
16457 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
16458 parm_pack))
16459 break;
16460 gen_formal_parameter_die (arg, NULL,
16461 false /* Don't emit name attribute. */,
16462 parm_pack_die);
16464 if (next_arg)
16465 *next_arg = arg;
16466 return parm_pack_die;
16469 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
16470 at the end of an (ANSI prototyped) formal parameters list. */
16472 static void
16473 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
16475 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
16478 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
16479 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
16480 parameters as specified in some function type specification (except for
16481 those which appear as part of a function *definition*). */
16483 static void
16484 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
16486 tree link;
16487 tree formal_type = NULL;
16488 tree first_parm_type;
16489 tree arg;
16491 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
16493 arg = DECL_ARGUMENTS (function_or_method_type);
16494 function_or_method_type = TREE_TYPE (function_or_method_type);
16496 else
16497 arg = NULL_TREE;
16499 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
16501 /* Make our first pass over the list of formal parameter types and output a
16502 DW_TAG_formal_parameter DIE for each one. */
16503 for (link = first_parm_type; link; )
16505 dw_die_ref parm_die;
16507 formal_type = TREE_VALUE (link);
16508 if (formal_type == void_type_node)
16509 break;
16511 /* Output a (nameless) DIE to represent the formal parameter itself. */
16512 parm_die = gen_formal_parameter_die (formal_type, NULL,
16513 true /* Emit name attribute. */,
16514 context_die);
16515 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
16516 && link == first_parm_type)
16518 add_AT_flag (parm_die, DW_AT_artificial, 1);
16519 if (dwarf_version >= 3 || !dwarf_strict)
16520 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
16522 else if (arg && DECL_ARTIFICIAL (arg))
16523 add_AT_flag (parm_die, DW_AT_artificial, 1);
16525 link = TREE_CHAIN (link);
16526 if (arg)
16527 arg = DECL_CHAIN (arg);
16530 /* If this function type has an ellipsis, add a
16531 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
16532 if (formal_type != void_type_node)
16533 gen_unspecified_parameters_die (function_or_method_type, context_die);
16535 /* Make our second (and final) pass over the list of formal parameter types
16536 and output DIEs to represent those types (as necessary). */
16537 for (link = TYPE_ARG_TYPES (function_or_method_type);
16538 link && TREE_VALUE (link);
16539 link = TREE_CHAIN (link))
16540 gen_type_die (TREE_VALUE (link), context_die);
16543 /* We want to generate the DIE for TYPE so that we can generate the
16544 die for MEMBER, which has been defined; we will need to refer back
16545 to the member declaration nested within TYPE. If we're trying to
16546 generate minimal debug info for TYPE, processing TYPE won't do the
16547 trick; we need to attach the member declaration by hand. */
16549 static void
16550 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
16552 gen_type_die (type, context_die);
16554 /* If we're trying to avoid duplicate debug info, we may not have
16555 emitted the member decl for this function. Emit it now. */
16556 if (TYPE_STUB_DECL (type)
16557 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
16558 && ! lookup_decl_die (member))
16560 dw_die_ref type_die;
16561 gcc_assert (!decl_ultimate_origin (member));
16563 push_decl_scope (type);
16564 type_die = lookup_type_die_strip_naming_typedef (type);
16565 if (TREE_CODE (member) == FUNCTION_DECL)
16566 gen_subprogram_die (member, type_die);
16567 else if (TREE_CODE (member) == FIELD_DECL)
16569 /* Ignore the nameless fields that are used to skip bits but handle
16570 C++ anonymous unions and structs. */
16571 if (DECL_NAME (member) != NULL_TREE
16572 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
16573 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
16575 gen_type_die (member_declared_type (member), type_die);
16576 gen_field_die (member, type_die);
16579 else
16580 gen_variable_die (member, NULL_TREE, type_die);
16582 pop_decl_scope ();
16586 /* Forward declare these functions, because they are mutually recursive
16587 with their set_block_* pairing functions. */
16588 static void set_decl_origin_self (tree);
16589 static void set_decl_abstract_flags (tree, int);
16591 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
16592 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
16593 that it points to the node itself, thus indicating that the node is its
16594 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
16595 the given node is NULL, recursively descend the decl/block tree which
16596 it is the root of, and for each other ..._DECL or BLOCK node contained
16597 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
16598 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
16599 values to point to themselves. */
16601 static void
16602 set_block_origin_self (tree stmt)
16604 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
16606 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
16609 tree local_decl;
16611 for (local_decl = BLOCK_VARS (stmt);
16612 local_decl != NULL_TREE;
16613 local_decl = DECL_CHAIN (local_decl))
16614 if (! DECL_EXTERNAL (local_decl))
16615 set_decl_origin_self (local_decl); /* Potential recursion. */
16619 tree subblock;
16621 for (subblock = BLOCK_SUBBLOCKS (stmt);
16622 subblock != NULL_TREE;
16623 subblock = BLOCK_CHAIN (subblock))
16624 set_block_origin_self (subblock); /* Recurse. */
16629 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
16630 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
16631 node to so that it points to the node itself, thus indicating that the
16632 node represents its own (abstract) origin. Additionally, if the
16633 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
16634 the decl/block tree of which the given node is the root of, and for
16635 each other ..._DECL or BLOCK node contained therein whose
16636 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
16637 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
16638 point to themselves. */
16640 static void
16641 set_decl_origin_self (tree decl)
16643 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
16645 DECL_ABSTRACT_ORIGIN (decl) = decl;
16646 if (TREE_CODE (decl) == FUNCTION_DECL)
16648 tree arg;
16650 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
16651 DECL_ABSTRACT_ORIGIN (arg) = arg;
16652 if (DECL_INITIAL (decl) != NULL_TREE
16653 && DECL_INITIAL (decl) != error_mark_node)
16654 set_block_origin_self (DECL_INITIAL (decl));
16659 /* Given a pointer to some BLOCK node, and a boolean value to set the
16660 "abstract" flags to, set that value into the BLOCK_ABSTRACT flag for
16661 the given block, and for all local decls and all local sub-blocks
16662 (recursively) which are contained therein. */
16664 static void
16665 set_block_abstract_flags (tree stmt, int setting)
16667 tree local_decl;
16668 tree subblock;
16669 unsigned int i;
16671 BLOCK_ABSTRACT (stmt) = setting;
16673 for (local_decl = BLOCK_VARS (stmt);
16674 local_decl != NULL_TREE;
16675 local_decl = DECL_CHAIN (local_decl))
16676 if (! DECL_EXTERNAL (local_decl))
16677 set_decl_abstract_flags (local_decl, setting);
16679 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
16681 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
16682 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
16683 || TREE_CODE (local_decl) == PARM_DECL)
16684 set_decl_abstract_flags (local_decl, setting);
16687 for (subblock = BLOCK_SUBBLOCKS (stmt);
16688 subblock != NULL_TREE;
16689 subblock = BLOCK_CHAIN (subblock))
16690 set_block_abstract_flags (subblock, setting);
16693 /* Given a pointer to some ..._DECL node, and a boolean value to set the
16694 "abstract" flags to, set that value into the DECL_ABSTRACT flag for the
16695 given decl, and (in the case where the decl is a FUNCTION_DECL) also
16696 set the abstract flags for all of the parameters, local vars, local
16697 blocks and sub-blocks (recursively) to the same setting. */
16699 static void
16700 set_decl_abstract_flags (tree decl, int setting)
16702 DECL_ABSTRACT (decl) = setting;
16703 if (TREE_CODE (decl) == FUNCTION_DECL)
16705 tree arg;
16707 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
16708 DECL_ABSTRACT (arg) = setting;
16709 if (DECL_INITIAL (decl) != NULL_TREE
16710 && DECL_INITIAL (decl) != error_mark_node)
16711 set_block_abstract_flags (DECL_INITIAL (decl), setting);
16715 /* Generate the DWARF2 info for the "abstract" instance of a function which we
16716 may later generate inlined and/or out-of-line instances of. */
16718 static void
16719 dwarf2out_abstract_function (tree decl)
16721 dw_die_ref old_die;
16722 tree save_fn;
16723 tree context;
16724 int was_abstract;
16725 htab_t old_decl_loc_table;
16726 htab_t old_cached_dw_loc_list_table;
16727 int old_call_site_count, old_tail_call_site_count;
16728 struct call_arg_loc_node *old_call_arg_locations;
16730 /* Make sure we have the actual abstract inline, not a clone. */
16731 decl = DECL_ORIGIN (decl);
16733 old_die = lookup_decl_die (decl);
16734 if (old_die && get_AT (old_die, DW_AT_inline))
16735 /* We've already generated the abstract instance. */
16736 return;
16738 /* We can be called while recursively when seeing block defining inlined subroutine
16739 DIE. Be sure to not clobber the outer location table nor use it or we would
16740 get locations in abstract instantces. */
16741 old_decl_loc_table = decl_loc_table;
16742 decl_loc_table = NULL;
16743 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
16744 cached_dw_loc_list_table = NULL;
16745 old_call_arg_locations = call_arg_locations;
16746 call_arg_locations = NULL;
16747 old_call_site_count = call_site_count;
16748 call_site_count = -1;
16749 old_tail_call_site_count = tail_call_site_count;
16750 tail_call_site_count = -1;
16752 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
16753 we don't get confused by DECL_ABSTRACT. */
16754 if (debug_info_level > DINFO_LEVEL_TERSE)
16756 context = decl_class_context (decl);
16757 if (context)
16758 gen_type_die_for_member
16759 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
16762 /* Pretend we've just finished compiling this function. */
16763 save_fn = current_function_decl;
16764 current_function_decl = decl;
16766 was_abstract = DECL_ABSTRACT (decl);
16767 set_decl_abstract_flags (decl, 1);
16768 dwarf2out_decl (decl);
16769 if (! was_abstract)
16770 set_decl_abstract_flags (decl, 0);
16772 current_function_decl = save_fn;
16773 decl_loc_table = old_decl_loc_table;
16774 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
16775 call_arg_locations = old_call_arg_locations;
16776 call_site_count = old_call_site_count;
16777 tail_call_site_count = old_tail_call_site_count;
16780 /* Helper function of premark_used_types() which gets called through
16781 htab_traverse.
16783 Marks the DIE of a given type in *SLOT as perennial, so it never gets
16784 marked as unused by prune_unused_types. */
16786 static int
16787 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
16789 tree type;
16790 dw_die_ref die;
16792 type = (tree) *slot;
16793 die = lookup_type_die (type);
16794 if (die != NULL)
16795 die->die_perennial_p = 1;
16796 return 1;
16799 /* Helper function of premark_types_used_by_global_vars which gets called
16800 through htab_traverse.
16802 Marks the DIE of a given type in *SLOT as perennial, so it never gets
16803 marked as unused by prune_unused_types. The DIE of the type is marked
16804 only if the global variable using the type will actually be emitted. */
16806 static int
16807 premark_types_used_by_global_vars_helper (void **slot,
16808 void *data ATTRIBUTE_UNUSED)
16810 struct types_used_by_vars_entry *entry;
16811 dw_die_ref die;
16813 entry = (struct types_used_by_vars_entry *) *slot;
16814 gcc_assert (entry->type != NULL
16815 && entry->var_decl != NULL);
16816 die = lookup_type_die (entry->type);
16817 if (die)
16819 /* Ask cgraph if the global variable really is to be emitted.
16820 If yes, then we'll keep the DIE of ENTRY->TYPE. */
16821 struct varpool_node *node = varpool_get_node (entry->var_decl);
16822 if (node && node->analyzed)
16824 die->die_perennial_p = 1;
16825 /* Keep the parent DIEs as well. */
16826 while ((die = die->die_parent) && die->die_perennial_p == 0)
16827 die->die_perennial_p = 1;
16830 return 1;
16833 /* Mark all members of used_types_hash as perennial. */
16835 static void
16836 premark_used_types (struct function *fun)
16838 if (fun && fun->used_types_hash)
16839 htab_traverse (fun->used_types_hash, premark_used_types_helper, NULL);
16842 /* Mark all members of types_used_by_vars_entry as perennial. */
16844 static void
16845 premark_types_used_by_global_vars (void)
16847 if (types_used_by_vars_hash)
16848 htab_traverse (types_used_by_vars_hash,
16849 premark_types_used_by_global_vars_helper, NULL);
16852 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
16853 for CA_LOC call arg loc node. */
16855 static dw_die_ref
16856 gen_call_site_die (tree decl, dw_die_ref subr_die,
16857 struct call_arg_loc_node *ca_loc)
16859 dw_die_ref stmt_die = NULL, die;
16860 tree block = ca_loc->block;
16862 while (block
16863 && block != DECL_INITIAL (decl)
16864 && TREE_CODE (block) == BLOCK)
16866 if (VEC_length (dw_die_ref, block_map) > BLOCK_NUMBER (block))
16867 stmt_die = VEC_index (dw_die_ref, block_map, BLOCK_NUMBER (block));
16868 if (stmt_die)
16869 break;
16870 block = BLOCK_SUPERCONTEXT (block);
16872 if (stmt_die == NULL)
16873 stmt_die = subr_die;
16874 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
16875 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
16876 if (ca_loc->tail_call_p)
16877 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
16878 if (ca_loc->symbol_ref)
16880 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
16881 if (tdie)
16882 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
16883 else
16884 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref);
16886 return die;
16889 /* Generate a DIE to represent a declared function (either file-scope or
16890 block-local). */
16892 static void
16893 gen_subprogram_die (tree decl, dw_die_ref context_die)
16895 tree origin = decl_ultimate_origin (decl);
16896 dw_die_ref subr_die;
16897 tree outer_scope;
16898 dw_die_ref old_die = lookup_decl_die (decl);
16899 int declaration = (current_function_decl != decl
16900 || class_or_namespace_scope_p (context_die));
16902 premark_used_types (DECL_STRUCT_FUNCTION (decl));
16904 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
16905 started to generate the abstract instance of an inline, decided to output
16906 its containing class, and proceeded to emit the declaration of the inline
16907 from the member list for the class. If so, DECLARATION takes priority;
16908 we'll get back to the abstract instance when done with the class. */
16910 /* The class-scope declaration DIE must be the primary DIE. */
16911 if (origin && declaration && class_or_namespace_scope_p (context_die))
16913 origin = NULL;
16914 gcc_assert (!old_die);
16917 /* Now that the C++ front end lazily declares artificial member fns, we
16918 might need to retrofit the declaration into its class. */
16919 if (!declaration && !origin && !old_die
16920 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
16921 && !class_or_namespace_scope_p (context_die)
16922 && debug_info_level > DINFO_LEVEL_TERSE)
16923 old_die = force_decl_die (decl);
16925 if (origin != NULL)
16927 gcc_assert (!declaration || local_scope_p (context_die));
16929 /* Fixup die_parent for the abstract instance of a nested
16930 inline function. */
16931 if (old_die && old_die->die_parent == NULL)
16932 add_child_die (context_die, old_die);
16934 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
16935 add_abstract_origin_attribute (subr_die, origin);
16936 /* This is where the actual code for a cloned function is.
16937 Let's emit linkage name attribute for it. This helps
16938 debuggers to e.g, set breakpoints into
16939 constructors/destructors when the user asks "break
16940 K::K". */
16941 add_linkage_name (subr_die, decl);
16943 else if (old_die)
16945 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
16946 struct dwarf_file_data * file_index = lookup_filename (s.file);
16948 if (!get_AT_flag (old_die, DW_AT_declaration)
16949 /* We can have a normal definition following an inline one in the
16950 case of redefinition of GNU C extern inlines.
16951 It seems reasonable to use AT_specification in this case. */
16952 && !get_AT (old_die, DW_AT_inline))
16954 /* Detect and ignore this case, where we are trying to output
16955 something we have already output. */
16956 return;
16959 /* If the definition comes from the same place as the declaration,
16960 maybe use the old DIE. We always want the DIE for this function
16961 that has the *_pc attributes to be under comp_unit_die so the
16962 debugger can find it. We also need to do this for abstract
16963 instances of inlines, since the spec requires the out-of-line copy
16964 to have the same parent. For local class methods, this doesn't
16965 apply; we just use the old DIE. */
16966 if ((is_cu_die (old_die->die_parent) || context_die == NULL)
16967 && (DECL_ARTIFICIAL (decl)
16968 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
16969 && (get_AT_unsigned (old_die, DW_AT_decl_line)
16970 == (unsigned) s.line))))
16972 subr_die = old_die;
16974 /* Clear out the declaration attribute and the formal parameters.
16975 Do not remove all children, because it is possible that this
16976 declaration die was forced using force_decl_die(). In such
16977 cases die that forced declaration die (e.g. TAG_imported_module)
16978 is one of the children that we do not want to remove. */
16979 remove_AT (subr_die, DW_AT_declaration);
16980 remove_AT (subr_die, DW_AT_object_pointer);
16981 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
16983 else
16985 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
16986 add_AT_specification (subr_die, old_die);
16987 add_pubname (decl, subr_die);
16988 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
16989 add_AT_file (subr_die, DW_AT_decl_file, file_index);
16990 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
16991 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
16994 else
16996 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
16998 if (TREE_PUBLIC (decl))
16999 add_AT_flag (subr_die, DW_AT_external, 1);
17001 add_name_and_src_coords_attributes (subr_die, decl);
17002 add_pubname (decl, subr_die);
17003 if (debug_info_level > DINFO_LEVEL_TERSE)
17005 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
17006 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
17007 0, 0, context_die);
17010 add_pure_or_virtual_attribute (subr_die, decl);
17011 if (DECL_ARTIFICIAL (decl))
17012 add_AT_flag (subr_die, DW_AT_artificial, 1);
17014 add_accessibility_attribute (subr_die, decl);
17017 if (declaration)
17019 if (!old_die || !get_AT (old_die, DW_AT_inline))
17021 add_AT_flag (subr_die, DW_AT_declaration, 1);
17023 /* If this is an explicit function declaration then generate
17024 a DW_AT_explicit attribute. */
17025 if (lang_hooks.decls.function_decl_explicit_p (decl)
17026 && (dwarf_version >= 3 || !dwarf_strict))
17027 add_AT_flag (subr_die, DW_AT_explicit, 1);
17029 /* The first time we see a member function, it is in the context of
17030 the class to which it belongs. We make sure of this by emitting
17031 the class first. The next time is the definition, which is
17032 handled above. The two may come from the same source text.
17034 Note that force_decl_die() forces function declaration die. It is
17035 later reused to represent definition. */
17036 equate_decl_number_to_die (decl, subr_die);
17039 else if (DECL_ABSTRACT (decl))
17041 if (DECL_DECLARED_INLINE_P (decl))
17043 if (cgraph_function_possibly_inlined_p (decl))
17044 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
17045 else
17046 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
17048 else
17050 if (cgraph_function_possibly_inlined_p (decl))
17051 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
17052 else
17053 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
17056 if (DECL_DECLARED_INLINE_P (decl)
17057 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
17058 add_AT_flag (subr_die, DW_AT_artificial, 1);
17060 equate_decl_number_to_die (decl, subr_die);
17062 else if (!DECL_EXTERNAL (decl))
17064 HOST_WIDE_INT cfa_fb_offset;
17065 struct function *fun = DECL_STRUCT_FUNCTION (decl);
17067 if (!old_die || !get_AT (old_die, DW_AT_inline))
17068 equate_decl_number_to_die (decl, subr_die);
17070 gcc_checking_assert (fun);
17071 if (!flag_reorder_blocks_and_partition)
17073 dw_fde_ref fde = fun->fde;
17074 if (fde->dw_fde_begin)
17076 /* We have already generated the labels. */
17077 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end);
17079 else
17081 /* Create start/end labels and add the range. */
17082 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
17083 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
17084 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
17085 current_function_funcdef_no);
17086 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
17087 current_function_funcdef_no);
17088 add_AT_low_high_pc (subr_die, label_id_low, label_id_high);
17091 #if VMS_DEBUGGING_INFO
17092 /* HP OpenVMS Industry Standard 64: DWARF Extensions
17093 Section 2.3 Prologue and Epilogue Attributes:
17094 When a breakpoint is set on entry to a function, it is generally
17095 desirable for execution to be suspended, not on the very first
17096 instruction of the function, but rather at a point after the
17097 function's frame has been set up, after any language defined local
17098 declaration processing has been completed, and before execution of
17099 the first statement of the function begins. Debuggers generally
17100 cannot properly determine where this point is. Similarly for a
17101 breakpoint set on exit from a function. The prologue and epilogue
17102 attributes allow a compiler to communicate the location(s) to use. */
17105 if (fde->dw_fde_vms_end_prologue)
17106 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
17107 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
17109 if (fde->dw_fde_vms_begin_epilogue)
17110 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
17111 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
17113 #endif
17116 else
17118 /* Generate pubnames entries for the split function code ranges. */
17119 dw_fde_ref fde = fun->fde;
17121 if (fde->dw_fde_second_begin)
17123 if (dwarf_version >= 3 || !dwarf_strict)
17125 /* We should use ranges for non-contiguous code section
17126 addresses. Use the actual code range for the initial
17127 section, since the HOT/COLD labels might precede an
17128 alignment offset. */
17129 bool range_list_added = false;
17130 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
17131 fde->dw_fde_end, &range_list_added);
17132 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
17133 fde->dw_fde_second_end,
17134 &range_list_added);
17135 if (range_list_added)
17136 add_ranges (NULL);
17138 else
17140 /* There is no real support in DW2 for this .. so we make
17141 a work-around. First, emit the pub name for the segment
17142 containing the function label. Then make and emit a
17143 simplified subprogram DIE for the second segment with the
17144 name pre-fixed by __hot/cold_sect_of_. We use the same
17145 linkage name for the second die so that gdb will find both
17146 sections when given "b foo". */
17147 const char *name = NULL;
17148 tree decl_name = DECL_NAME (decl);
17149 dw_die_ref seg_die;
17151 /* Do the 'primary' section. */
17152 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
17153 fde->dw_fde_end);
17155 /* Build a minimal DIE for the secondary section. */
17156 seg_die = new_die (DW_TAG_subprogram,
17157 subr_die->die_parent, decl);
17159 if (TREE_PUBLIC (decl))
17160 add_AT_flag (seg_die, DW_AT_external, 1);
17162 if (decl_name != NULL
17163 && IDENTIFIER_POINTER (decl_name) != NULL)
17165 name = dwarf2_name (decl, 1);
17166 if (! DECL_ARTIFICIAL (decl))
17167 add_src_coords_attributes (seg_die, decl);
17169 add_linkage_name (seg_die, decl);
17171 gcc_assert (name != NULL);
17172 add_pure_or_virtual_attribute (seg_die, decl);
17173 if (DECL_ARTIFICIAL (decl))
17174 add_AT_flag (seg_die, DW_AT_artificial, 1);
17176 name = concat ("__second_sect_of_", name, NULL);
17177 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
17178 fde->dw_fde_second_end);
17179 add_name_attribute (seg_die, name);
17180 if (want_pubnames ())
17181 add_pubname_string (name, seg_die);
17184 else
17185 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end);
17188 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
17190 /* We define the "frame base" as the function's CFA. This is more
17191 convenient for several reasons: (1) It's stable across the prologue
17192 and epilogue, which makes it better than just a frame pointer,
17193 (2) With dwarf3, there exists a one-byte encoding that allows us
17194 to reference the .debug_frame data by proxy, but failing that,
17195 (3) We can at least reuse the code inspection and interpretation
17196 code that determines the CFA position at various points in the
17197 function. */
17198 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
17200 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
17201 add_AT_loc (subr_die, DW_AT_frame_base, op);
17203 else
17205 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
17206 if (list->dw_loc_next)
17207 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
17208 else
17209 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
17212 /* Compute a displacement from the "steady-state frame pointer" to
17213 the CFA. The former is what all stack slots and argument slots
17214 will reference in the rtl; the later is what we've told the
17215 debugger about. We'll need to adjust all frame_base references
17216 by this displacement. */
17217 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
17219 if (fun->static_chain_decl)
17220 add_AT_location_description (subr_die, DW_AT_static_link,
17221 loc_list_from_tree (fun->static_chain_decl, 2));
17224 /* Generate child dies for template paramaters. */
17225 if (debug_info_level > DINFO_LEVEL_TERSE)
17226 gen_generic_params_dies (decl);
17228 /* Now output descriptions of the arguments for this function. This gets
17229 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
17230 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
17231 `...' at the end of the formal parameter list. In order to find out if
17232 there was a trailing ellipsis or not, we must instead look at the type
17233 associated with the FUNCTION_DECL. This will be a node of type
17234 FUNCTION_TYPE. If the chain of type nodes hanging off of this
17235 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
17236 an ellipsis at the end. */
17238 /* In the case where we are describing a mere function declaration, all we
17239 need to do here (and all we *can* do here) is to describe the *types* of
17240 its formal parameters. */
17241 if (debug_info_level <= DINFO_LEVEL_TERSE)
17243 else if (declaration)
17244 gen_formal_types_die (decl, subr_die);
17245 else
17247 /* Generate DIEs to represent all known formal parameters. */
17248 tree parm = DECL_ARGUMENTS (decl);
17249 tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
17250 tree generic_decl_parm = generic_decl
17251 ? DECL_ARGUMENTS (generic_decl)
17252 : NULL;
17254 /* Now we want to walk the list of parameters of the function and
17255 emit their relevant DIEs.
17257 We consider the case of DECL being an instance of a generic function
17258 as well as it being a normal function.
17260 If DECL is an instance of a generic function we walk the
17261 parameters of the generic function declaration _and_ the parameters of
17262 DECL itself. This is useful because we want to emit specific DIEs for
17263 function parameter packs and those are declared as part of the
17264 generic function declaration. In that particular case,
17265 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
17266 That DIE has children DIEs representing the set of arguments
17267 of the pack. Note that the set of pack arguments can be empty.
17268 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
17269 children DIE.
17271 Otherwise, we just consider the parameters of DECL. */
17272 while (generic_decl_parm || parm)
17274 if (generic_decl_parm
17275 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
17276 gen_formal_parameter_pack_die (generic_decl_parm,
17277 parm, subr_die,
17278 &parm);
17279 else if (parm)
17281 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
17283 if (parm == DECL_ARGUMENTS (decl)
17284 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
17285 && parm_die
17286 && (dwarf_version >= 3 || !dwarf_strict))
17287 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
17289 parm = DECL_CHAIN (parm);
17292 if (generic_decl_parm)
17293 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
17296 /* Decide whether we need an unspecified_parameters DIE at the end.
17297 There are 2 more cases to do this for: 1) the ansi ... declaration -
17298 this is detectable when the end of the arg list is not a
17299 void_type_node 2) an unprototyped function declaration (not a
17300 definition). This just means that we have no info about the
17301 parameters at all. */
17302 if (prototype_p (TREE_TYPE (decl)))
17304 /* This is the prototyped case, check for.... */
17305 if (stdarg_p (TREE_TYPE (decl)))
17306 gen_unspecified_parameters_die (decl, subr_die);
17308 else if (DECL_INITIAL (decl) == NULL_TREE)
17309 gen_unspecified_parameters_die (decl, subr_die);
17312 /* Output Dwarf info for all of the stuff within the body of the function
17313 (if it has one - it may be just a declaration). */
17314 outer_scope = DECL_INITIAL (decl);
17316 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
17317 a function. This BLOCK actually represents the outermost binding contour
17318 for the function, i.e. the contour in which the function's formal
17319 parameters and labels get declared. Curiously, it appears that the front
17320 end doesn't actually put the PARM_DECL nodes for the current function onto
17321 the BLOCK_VARS list for this outer scope, but are strung off of the
17322 DECL_ARGUMENTS list for the function instead.
17324 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
17325 the LABEL_DECL nodes for the function however, and we output DWARF info
17326 for those in decls_for_scope. Just within the `outer_scope' there will be
17327 a BLOCK node representing the function's outermost pair of curly braces,
17328 and any blocks used for the base and member initializers of a C++
17329 constructor function. */
17330 if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
17332 int call_site_note_count = 0;
17333 int tail_call_site_note_count = 0;
17335 /* Emit a DW_TAG_variable DIE for a named return value. */
17336 if (DECL_NAME (DECL_RESULT (decl)))
17337 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
17339 current_function_has_inlines = 0;
17340 decls_for_scope (outer_scope, subr_die, 0);
17342 if (call_arg_locations && !dwarf_strict)
17344 struct call_arg_loc_node *ca_loc;
17345 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
17347 dw_die_ref die = NULL;
17348 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
17349 rtx arg, next_arg;
17351 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
17352 arg; arg = next_arg)
17354 dw_loc_descr_ref reg, val;
17355 enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
17356 dw_die_ref cdie, tdie = NULL;
17358 next_arg = XEXP (arg, 1);
17359 if (REG_P (XEXP (XEXP (arg, 0), 0))
17360 && next_arg
17361 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
17362 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
17363 && REGNO (XEXP (XEXP (arg, 0), 0))
17364 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
17365 next_arg = XEXP (next_arg, 1);
17366 if (mode == VOIDmode)
17368 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
17369 if (mode == VOIDmode)
17370 mode = GET_MODE (XEXP (arg, 0));
17372 if (mode == VOIDmode || mode == BLKmode)
17373 continue;
17374 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
17376 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
17377 tloc = XEXP (XEXP (arg, 0), 1);
17378 continue;
17380 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
17381 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
17383 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
17384 tlocc = XEXP (XEXP (arg, 0), 1);
17385 continue;
17387 reg = NULL;
17388 if (REG_P (XEXP (XEXP (arg, 0), 0)))
17389 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
17390 VAR_INIT_STATUS_INITIALIZED);
17391 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
17393 rtx mem = XEXP (XEXP (arg, 0), 0);
17394 reg = mem_loc_descriptor (XEXP (mem, 0),
17395 get_address_mode (mem),
17396 GET_MODE (mem),
17397 VAR_INIT_STATUS_INITIALIZED);
17399 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
17400 == DEBUG_PARAMETER_REF)
17402 tree tdecl
17403 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
17404 tdie = lookup_decl_die (tdecl);
17405 if (tdie == NULL)
17406 continue;
17408 else
17409 continue;
17410 if (reg == NULL
17411 && GET_CODE (XEXP (XEXP (arg, 0), 0))
17412 != DEBUG_PARAMETER_REF)
17413 continue;
17414 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
17415 VOIDmode,
17416 VAR_INIT_STATUS_INITIALIZED);
17417 if (val == NULL)
17418 continue;
17419 if (die == NULL)
17420 die = gen_call_site_die (decl, subr_die, ca_loc);
17421 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
17422 NULL_TREE);
17423 if (reg != NULL)
17424 add_AT_loc (cdie, DW_AT_location, reg);
17425 else if (tdie != NULL)
17426 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
17427 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
17428 if (next_arg != XEXP (arg, 1))
17430 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
17431 if (mode == VOIDmode)
17432 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
17433 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
17434 0), 1),
17435 mode, VOIDmode,
17436 VAR_INIT_STATUS_INITIALIZED);
17437 if (val != NULL)
17438 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
17441 if (die == NULL
17442 && (ca_loc->symbol_ref || tloc))
17443 die = gen_call_site_die (decl, subr_die, ca_loc);
17444 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
17446 dw_loc_descr_ref tval = NULL;
17448 if (tloc != NULL_RTX)
17449 tval = mem_loc_descriptor (tloc,
17450 GET_MODE (tloc) == VOIDmode
17451 ? Pmode : GET_MODE (tloc),
17452 VOIDmode,
17453 VAR_INIT_STATUS_INITIALIZED);
17454 if (tval)
17455 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
17456 else if (tlocc != NULL_RTX)
17458 tval = mem_loc_descriptor (tlocc,
17459 GET_MODE (tlocc) == VOIDmode
17460 ? Pmode : GET_MODE (tlocc),
17461 VOIDmode,
17462 VAR_INIT_STATUS_INITIALIZED);
17463 if (tval)
17464 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
17465 tval);
17468 if (die != NULL)
17470 call_site_note_count++;
17471 if (ca_loc->tail_call_p)
17472 tail_call_site_note_count++;
17476 call_arg_locations = NULL;
17477 call_arg_loc_last = NULL;
17478 if (tail_call_site_count >= 0
17479 && tail_call_site_count == tail_call_site_note_count
17480 && !dwarf_strict)
17482 if (call_site_count >= 0
17483 && call_site_count == call_site_note_count)
17484 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
17485 else
17486 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
17488 call_site_count = -1;
17489 tail_call_site_count = -1;
17491 /* Add the calling convention attribute if requested. */
17492 add_calling_convention_attribute (subr_die, decl);
17496 /* Returns a hash value for X (which really is a die_struct). */
17498 static hashval_t
17499 common_block_die_table_hash (const void *x)
17501 const_dw_die_ref d = (const_dw_die_ref) x;
17502 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
17505 /* Return nonzero if decl_id and die_parent of die_struct X is the same
17506 as decl_id and die_parent of die_struct Y. */
17508 static int
17509 common_block_die_table_eq (const void *x, const void *y)
17511 const_dw_die_ref d = (const_dw_die_ref) x;
17512 const_dw_die_ref e = (const_dw_die_ref) y;
17513 return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
17516 /* Generate a DIE to represent a declared data object.
17517 Either DECL or ORIGIN must be non-null. */
17519 static void
17520 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
17522 HOST_WIDE_INT off = 0;
17523 tree com_decl;
17524 tree decl_or_origin = decl ? decl : origin;
17525 tree ultimate_origin;
17526 dw_die_ref var_die;
17527 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
17528 dw_die_ref origin_die;
17529 bool declaration = (DECL_EXTERNAL (decl_or_origin)
17530 || class_or_namespace_scope_p (context_die));
17531 bool specialization_p = false;
17533 ultimate_origin = decl_ultimate_origin (decl_or_origin);
17534 if (decl || ultimate_origin)
17535 origin = ultimate_origin;
17536 com_decl = fortran_common (decl_or_origin, &off);
17538 /* Symbol in common gets emitted as a child of the common block, in the form
17539 of a data member. */
17540 if (com_decl)
17542 dw_die_ref com_die;
17543 dw_loc_list_ref loc;
17544 die_node com_die_arg;
17546 var_die = lookup_decl_die (decl_or_origin);
17547 if (var_die)
17549 if (get_AT (var_die, DW_AT_location) == NULL)
17551 loc = loc_list_from_tree (com_decl, off ? 1 : 2);
17552 if (loc)
17554 if (off)
17556 /* Optimize the common case. */
17557 if (single_element_loc_list_p (loc)
17558 && loc->expr->dw_loc_opc == DW_OP_addr
17559 && loc->expr->dw_loc_next == NULL
17560 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
17561 == SYMBOL_REF)
17563 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
17564 loc->expr->dw_loc_oprnd1.v.val_addr
17565 = plus_constant (GET_MODE (x), x , off);
17567 else
17568 loc_list_plus_const (loc, off);
17570 add_AT_location_description (var_die, DW_AT_location, loc);
17571 remove_AT (var_die, DW_AT_declaration);
17574 return;
17577 if (common_block_die_table == NULL)
17578 common_block_die_table
17579 = htab_create_ggc (10, common_block_die_table_hash,
17580 common_block_die_table_eq, NULL);
17582 com_die_arg.decl_id = DECL_UID (com_decl);
17583 com_die_arg.die_parent = context_die;
17584 com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
17585 loc = loc_list_from_tree (com_decl, 2);
17586 if (com_die == NULL)
17588 const char *cnam
17589 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
17590 void **slot;
17592 com_die = new_die (DW_TAG_common_block, context_die, decl);
17593 add_name_and_src_coords_attributes (com_die, com_decl);
17594 if (loc)
17596 add_AT_location_description (com_die, DW_AT_location, loc);
17597 /* Avoid sharing the same loc descriptor between
17598 DW_TAG_common_block and DW_TAG_variable. */
17599 loc = loc_list_from_tree (com_decl, 2);
17601 else if (DECL_EXTERNAL (decl))
17602 add_AT_flag (com_die, DW_AT_declaration, 1);
17603 if (want_pubnames ())
17604 add_pubname_string (cnam, com_die); /* ??? needed? */
17605 com_die->decl_id = DECL_UID (com_decl);
17606 slot = htab_find_slot (common_block_die_table, com_die, INSERT);
17607 *slot = (void *) com_die;
17609 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
17611 add_AT_location_description (com_die, DW_AT_location, loc);
17612 loc = loc_list_from_tree (com_decl, 2);
17613 remove_AT (com_die, DW_AT_declaration);
17615 var_die = new_die (DW_TAG_variable, com_die, decl);
17616 add_name_and_src_coords_attributes (var_die, decl);
17617 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
17618 TREE_THIS_VOLATILE (decl), context_die);
17619 add_AT_flag (var_die, DW_AT_external, 1);
17620 if (loc)
17622 if (off)
17624 /* Optimize the common case. */
17625 if (single_element_loc_list_p (loc)
17626 && loc->expr->dw_loc_opc == DW_OP_addr
17627 && loc->expr->dw_loc_next == NULL
17628 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
17630 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
17631 loc->expr->dw_loc_oprnd1.v.val_addr
17632 = plus_constant (GET_MODE (x), x, off);
17634 else
17635 loc_list_plus_const (loc, off);
17637 add_AT_location_description (var_die, DW_AT_location, loc);
17639 else if (DECL_EXTERNAL (decl))
17640 add_AT_flag (var_die, DW_AT_declaration, 1);
17641 equate_decl_number_to_die (decl, var_die);
17642 return;
17645 /* If the compiler emitted a definition for the DECL declaration
17646 and if we already emitted a DIE for it, don't emit a second
17647 DIE for it again. Allow re-declarations of DECLs that are
17648 inside functions, though. */
17649 if (old_die && declaration && !local_scope_p (context_die))
17650 return;
17652 /* For static data members, the declaration in the class is supposed
17653 to have DW_TAG_member tag; the specification should still be
17654 DW_TAG_variable referencing the DW_TAG_member DIE. */
17655 if (declaration && class_scope_p (context_die))
17656 var_die = new_die (DW_TAG_member, context_die, decl);
17657 else
17658 var_die = new_die (DW_TAG_variable, context_die, decl);
17660 origin_die = NULL;
17661 if (origin != NULL)
17662 origin_die = add_abstract_origin_attribute (var_die, origin);
17664 /* Loop unrolling can create multiple blocks that refer to the same
17665 static variable, so we must test for the DW_AT_declaration flag.
17667 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
17668 copy decls and set the DECL_ABSTRACT flag on them instead of
17669 sharing them.
17671 ??? Duplicated blocks have been rewritten to use .debug_ranges.
17673 ??? The declare_in_namespace support causes us to get two DIEs for one
17674 variable, both of which are declarations. We want to avoid considering
17675 one to be a specification, so we must test that this DIE is not a
17676 declaration. */
17677 else if (old_die && TREE_STATIC (decl) && ! declaration
17678 && get_AT_flag (old_die, DW_AT_declaration) == 1)
17680 /* This is a definition of a C++ class level static. */
17681 add_AT_specification (var_die, old_die);
17682 specialization_p = true;
17683 if (DECL_NAME (decl))
17685 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17686 struct dwarf_file_data * file_index = lookup_filename (s.file);
17688 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
17689 add_AT_file (var_die, DW_AT_decl_file, file_index);
17691 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
17692 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
17694 if (old_die->die_tag == DW_TAG_member)
17695 add_linkage_name (var_die, decl);
17698 else
17699 add_name_and_src_coords_attributes (var_die, decl);
17701 if ((origin == NULL && !specialization_p)
17702 || (origin != NULL
17703 && !DECL_ABSTRACT (decl_or_origin)
17704 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
17705 decl_function_context
17706 (decl_or_origin))))
17708 tree type = TREE_TYPE (decl_or_origin);
17710 if (decl_by_reference_p (decl_or_origin))
17711 add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
17712 else
17713 add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
17714 TREE_THIS_VOLATILE (decl_or_origin), context_die);
17717 if (origin == NULL && !specialization_p)
17719 if (TREE_PUBLIC (decl))
17720 add_AT_flag (var_die, DW_AT_external, 1);
17722 if (DECL_ARTIFICIAL (decl))
17723 add_AT_flag (var_die, DW_AT_artificial, 1);
17725 add_accessibility_attribute (var_die, decl);
17728 if (declaration)
17729 add_AT_flag (var_die, DW_AT_declaration, 1);
17731 if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
17732 equate_decl_number_to_die (decl, var_die);
17734 if (! declaration
17735 && (! DECL_ABSTRACT (decl_or_origin)
17736 /* Local static vars are shared between all clones/inlines,
17737 so emit DW_AT_location on the abstract DIE if DECL_RTL is
17738 already set. */
17739 || (TREE_CODE (decl_or_origin) == VAR_DECL
17740 && TREE_STATIC (decl_or_origin)
17741 && DECL_RTL_SET_P (decl_or_origin)))
17742 /* When abstract origin already has DW_AT_location attribute, no need
17743 to add it again. */
17744 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
17746 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
17747 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
17748 defer_location (decl_or_origin, var_die);
17749 else
17750 add_location_or_const_value_attribute (var_die, decl_or_origin,
17751 decl == NULL, DW_AT_location);
17752 add_pubname (decl_or_origin, var_die);
17754 else
17755 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
17758 /* Generate a DIE to represent a named constant. */
17760 static void
17761 gen_const_die (tree decl, dw_die_ref context_die)
17763 dw_die_ref const_die;
17764 tree type = TREE_TYPE (decl);
17766 const_die = new_die (DW_TAG_constant, context_die, decl);
17767 add_name_and_src_coords_attributes (const_die, decl);
17768 add_type_attribute (const_die, type, 1, 0, context_die);
17769 if (TREE_PUBLIC (decl))
17770 add_AT_flag (const_die, DW_AT_external, 1);
17771 if (DECL_ARTIFICIAL (decl))
17772 add_AT_flag (const_die, DW_AT_artificial, 1);
17773 tree_add_const_value_attribute_for_decl (const_die, decl);
17776 /* Generate a DIE to represent a label identifier. */
17778 static void
17779 gen_label_die (tree decl, dw_die_ref context_die)
17781 tree origin = decl_ultimate_origin (decl);
17782 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
17783 rtx insn;
17784 char label[MAX_ARTIFICIAL_LABEL_BYTES];
17786 if (origin != NULL)
17787 add_abstract_origin_attribute (lbl_die, origin);
17788 else
17789 add_name_and_src_coords_attributes (lbl_die, decl);
17791 if (DECL_ABSTRACT (decl))
17792 equate_decl_number_to_die (decl, lbl_die);
17793 else
17795 insn = DECL_RTL_IF_SET (decl);
17797 /* Deleted labels are programmer specified labels which have been
17798 eliminated because of various optimizations. We still emit them
17799 here so that it is possible to put breakpoints on them. */
17800 if (insn
17801 && (LABEL_P (insn)
17802 || ((NOTE_P (insn)
17803 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
17805 /* When optimization is enabled (via -O) some parts of the compiler
17806 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
17807 represent source-level labels which were explicitly declared by
17808 the user. This really shouldn't be happening though, so catch
17809 it if it ever does happen. */
17810 gcc_assert (!INSN_DELETED_P (insn));
17812 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
17813 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
17815 else if (insn
17816 && NOTE_P (insn)
17817 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
17818 && CODE_LABEL_NUMBER (insn) != -1)
17820 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
17821 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
17826 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
17827 attributes to the DIE for a block STMT, to describe where the inlined
17828 function was called from. This is similar to add_src_coords_attributes. */
17830 static inline void
17831 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
17833 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
17835 if (dwarf_version >= 3 || !dwarf_strict)
17837 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
17838 add_AT_unsigned (die, DW_AT_call_line, s.line);
17843 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
17844 Add low_pc and high_pc attributes to the DIE for a block STMT. */
17846 static inline void
17847 add_high_low_attributes (tree stmt, dw_die_ref die)
17849 char label[MAX_ARTIFICIAL_LABEL_BYTES];
17851 if (BLOCK_FRAGMENT_CHAIN (stmt)
17852 && (dwarf_version >= 3 || !dwarf_strict))
17854 tree chain, superblock = NULL_TREE;
17855 dw_die_ref pdie;
17856 dw_attr_ref attr = NULL;
17858 if (inlined_function_outer_scope_p (stmt))
17860 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
17861 BLOCK_NUMBER (stmt));
17862 add_AT_lbl_id (die, DW_AT_entry_pc, label);
17865 /* Optimize duplicate .debug_ranges lists or even tails of
17866 lists. If this BLOCK has same ranges as its supercontext,
17867 lookup DW_AT_ranges attribute in the supercontext (and
17868 recursively so), verify that the ranges_table contains the
17869 right values and use it instead of adding a new .debug_range. */
17870 for (chain = stmt, pdie = die;
17871 BLOCK_SAME_RANGE (chain);
17872 chain = BLOCK_SUPERCONTEXT (chain))
17874 dw_attr_ref new_attr;
17876 pdie = pdie->die_parent;
17877 if (pdie == NULL)
17878 break;
17879 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
17880 break;
17881 new_attr = get_AT (pdie, DW_AT_ranges);
17882 if (new_attr == NULL
17883 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
17884 break;
17885 attr = new_attr;
17886 superblock = BLOCK_SUPERCONTEXT (chain);
17888 if (attr != NULL
17889 && (ranges_table[attr->dw_attr_val.v.val_offset
17890 / 2 / DWARF2_ADDR_SIZE].num
17891 == BLOCK_NUMBER (superblock))
17892 && BLOCK_FRAGMENT_CHAIN (superblock))
17894 unsigned long off = attr->dw_attr_val.v.val_offset
17895 / 2 / DWARF2_ADDR_SIZE;
17896 unsigned long supercnt = 0, thiscnt = 0;
17897 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
17898 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
17900 ++supercnt;
17901 gcc_checking_assert (ranges_table[off + supercnt].num
17902 == BLOCK_NUMBER (chain));
17904 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
17905 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
17906 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
17907 ++thiscnt;
17908 gcc_assert (supercnt >= thiscnt);
17909 add_AT_range_list (die, DW_AT_ranges,
17910 (off + supercnt - thiscnt)
17911 * 2 * DWARF2_ADDR_SIZE);
17912 return;
17915 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
17917 chain = BLOCK_FRAGMENT_CHAIN (stmt);
17920 add_ranges (chain);
17921 chain = BLOCK_FRAGMENT_CHAIN (chain);
17923 while (chain);
17924 add_ranges (NULL);
17926 else
17928 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
17929 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
17930 BLOCK_NUMBER (stmt));
17931 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
17932 BLOCK_NUMBER (stmt));
17933 add_AT_low_high_pc (die, label, label_high);
17937 /* Generate a DIE for a lexical block. */
17939 static void
17940 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
17942 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
17944 if (call_arg_locations)
17946 if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
17947 VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
17948 BLOCK_NUMBER (stmt) + 1);
17949 VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), stmt_die);
17952 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
17953 add_high_low_attributes (stmt, stmt_die);
17955 decls_for_scope (stmt, stmt_die, depth);
17958 /* Generate a DIE for an inlined subprogram. */
17960 static void
17961 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
17963 tree decl;
17965 /* The instance of function that is effectively being inlined shall not
17966 be abstract. */
17967 gcc_assert (! BLOCK_ABSTRACT (stmt));
17969 decl = block_ultimate_origin (stmt);
17971 /* Emit info for the abstract instance first, if we haven't yet. We
17972 must emit this even if the block is abstract, otherwise when we
17973 emit the block below (or elsewhere), we may end up trying to emit
17974 a die whose origin die hasn't been emitted, and crashing. */
17975 dwarf2out_abstract_function (decl);
17977 if (! BLOCK_ABSTRACT (stmt))
17979 dw_die_ref subr_die
17980 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
17982 if (call_arg_locations)
17984 if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
17985 VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
17986 BLOCK_NUMBER (stmt) + 1);
17987 VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), subr_die);
17989 add_abstract_origin_attribute (subr_die, decl);
17990 if (TREE_ASM_WRITTEN (stmt))
17991 add_high_low_attributes (stmt, subr_die);
17992 add_call_src_coords_attributes (stmt, subr_die);
17994 decls_for_scope (stmt, subr_die, depth);
17995 current_function_has_inlines = 1;
17999 /* Generate a DIE for a field in a record, or structure. */
18001 static void
18002 gen_field_die (tree decl, dw_die_ref context_die)
18004 dw_die_ref decl_die;
18006 if (TREE_TYPE (decl) == error_mark_node)
18007 return;
18009 decl_die = new_die (DW_TAG_member, context_die, decl);
18010 add_name_and_src_coords_attributes (decl_die, decl);
18011 add_type_attribute (decl_die, member_declared_type (decl),
18012 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
18013 context_die);
18015 if (DECL_BIT_FIELD_TYPE (decl))
18017 add_byte_size_attribute (decl_die, decl);
18018 add_bit_size_attribute (decl_die, decl);
18019 add_bit_offset_attribute (decl_die, decl);
18022 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
18023 add_data_member_location_attribute (decl_die, decl);
18025 if (DECL_ARTIFICIAL (decl))
18026 add_AT_flag (decl_die, DW_AT_artificial, 1);
18028 add_accessibility_attribute (decl_die, decl);
18030 /* Equate decl number to die, so that we can look up this decl later on. */
18031 equate_decl_number_to_die (decl, decl_die);
18034 #if 0
18035 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18036 Use modified_type_die instead.
18037 We keep this code here just in case these types of DIEs may be needed to
18038 represent certain things in other languages (e.g. Pascal) someday. */
18040 static void
18041 gen_pointer_type_die (tree type, dw_die_ref context_die)
18043 dw_die_ref ptr_die
18044 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
18046 equate_type_number_to_die (type, ptr_die);
18047 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18048 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18051 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18052 Use modified_type_die instead.
18053 We keep this code here just in case these types of DIEs may be needed to
18054 represent certain things in other languages (e.g. Pascal) someday. */
18056 static void
18057 gen_reference_type_die (tree type, dw_die_ref context_die)
18059 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
18061 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
18062 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
18063 else
18064 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
18066 equate_type_number_to_die (type, ref_die);
18067 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
18068 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18070 #endif
18072 /* Generate a DIE for a pointer to a member type. */
18074 static void
18075 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
18077 dw_die_ref ptr_die
18078 = new_die (DW_TAG_ptr_to_member_type,
18079 scope_die_for (type, context_die), type);
18081 equate_type_number_to_die (type, ptr_die);
18082 add_AT_die_ref (ptr_die, DW_AT_containing_type,
18083 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
18084 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18087 typedef const char *dchar_p; /* For DEF_VEC_P. */
18088 DEF_VEC_P(dchar_p);
18089 DEF_VEC_ALLOC_P(dchar_p,heap);
18091 static char *producer_string;
18093 /* Return a heap allocated producer string including command line options
18094 if -grecord-gcc-switches. */
18096 static char *
18097 gen_producer_string (void)
18099 size_t j;
18100 VEC(dchar_p, heap) *switches = NULL;
18101 const char *language_string = lang_hooks.name;
18102 char *producer, *tail;
18103 const char *p;
18104 size_t len = dwarf_record_gcc_switches ? 0 : 3;
18105 size_t plen = strlen (language_string) + 1 + strlen (version_string);
18107 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
18108 switch (save_decoded_options[j].opt_index)
18110 case OPT_o:
18111 case OPT_d:
18112 case OPT_dumpbase:
18113 case OPT_dumpdir:
18114 case OPT_auxbase:
18115 case OPT_auxbase_strip:
18116 case OPT_quiet:
18117 case OPT_version:
18118 case OPT_v:
18119 case OPT_w:
18120 case OPT_L:
18121 case OPT_D:
18122 case OPT_I:
18123 case OPT_U:
18124 case OPT_SPECIAL_unknown:
18125 case OPT_SPECIAL_ignore:
18126 case OPT_SPECIAL_program_name:
18127 case OPT_SPECIAL_input_file:
18128 case OPT_grecord_gcc_switches:
18129 case OPT_gno_record_gcc_switches:
18130 case OPT__output_pch_:
18131 case OPT_fdiagnostics_show_location_:
18132 case OPT_fdiagnostics_show_option:
18133 case OPT_fdiagnostics_show_caret:
18134 case OPT_fverbose_asm:
18135 case OPT____:
18136 case OPT__sysroot_:
18137 case OPT_nostdinc:
18138 case OPT_nostdinc__:
18139 /* Ignore these. */
18140 continue;
18141 default:
18142 if (cl_options[save_decoded_options[j].opt_index].flags
18143 & CL_NO_DWARF_RECORD)
18144 continue;
18145 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
18146 == '-');
18147 switch (save_decoded_options[j].canonical_option[0][1])
18149 case 'M':
18150 case 'i':
18151 case 'W':
18152 continue;
18153 case 'f':
18154 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
18155 "dump", 4) == 0)
18156 continue;
18157 break;
18158 default:
18159 break;
18161 VEC_safe_push (dchar_p, heap, switches,
18162 save_decoded_options[j].orig_option_with_args_text);
18163 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
18164 break;
18167 producer = XNEWVEC (char, plen + 1 + len + 1);
18168 tail = producer;
18169 sprintf (tail, "%s %s", language_string, version_string);
18170 tail += plen;
18172 FOR_EACH_VEC_ELT (dchar_p, switches, j, p)
18174 len = strlen (p);
18175 *tail = ' ';
18176 memcpy (tail + 1, p, len);
18177 tail += len + 1;
18180 *tail = '\0';
18181 VEC_free (dchar_p, heap, switches);
18182 return producer;
18185 /* Generate the DIE for the compilation unit. */
18187 static dw_die_ref
18188 gen_compile_unit_die (const char *filename)
18190 dw_die_ref die;
18191 const char *language_string = lang_hooks.name;
18192 int language;
18194 die = new_die (DW_TAG_compile_unit, NULL, NULL);
18196 if (filename)
18198 add_name_attribute (die, filename);
18199 /* Don't add cwd for <built-in>. */
18200 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
18201 add_comp_dir_attribute (die);
18204 if (producer_string == NULL)
18205 producer_string = gen_producer_string ();
18206 add_AT_string (die, DW_AT_producer, producer_string);
18208 /* If our producer is LTO try to figure out a common language to use
18209 from the global list of translation units. */
18210 if (strcmp (language_string, "GNU GIMPLE") == 0)
18212 unsigned i;
18213 tree t;
18214 const char *common_lang = NULL;
18216 FOR_EACH_VEC_ELT (tree, all_translation_units, i, t)
18218 if (!TRANSLATION_UNIT_LANGUAGE (t))
18219 continue;
18220 if (!common_lang)
18221 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
18222 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
18224 else if (strncmp (common_lang, "GNU C", 5) == 0
18225 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
18226 /* Mixing C and C++ is ok, use C++ in that case. */
18227 common_lang = "GNU C++";
18228 else
18230 /* Fall back to C. */
18231 common_lang = NULL;
18232 break;
18236 if (common_lang)
18237 language_string = common_lang;
18240 language = DW_LANG_C89;
18241 if (strcmp (language_string, "GNU C++") == 0)
18242 language = DW_LANG_C_plus_plus;
18243 else if (strcmp (language_string, "GNU F77") == 0)
18244 language = DW_LANG_Fortran77;
18245 else if (strcmp (language_string, "GNU Pascal") == 0)
18246 language = DW_LANG_Pascal83;
18247 else if (dwarf_version >= 3 || !dwarf_strict)
18249 if (strcmp (language_string, "GNU Ada") == 0)
18250 language = DW_LANG_Ada95;
18251 else if (strcmp (language_string, "GNU Fortran") == 0)
18252 language = DW_LANG_Fortran95;
18253 else if (strcmp (language_string, "GNU Java") == 0)
18254 language = DW_LANG_Java;
18255 else if (strcmp (language_string, "GNU Objective-C") == 0)
18256 language = DW_LANG_ObjC;
18257 else if (strcmp (language_string, "GNU Objective-C++") == 0)
18258 language = DW_LANG_ObjC_plus_plus;
18259 else if (dwarf_version >= 5 || !dwarf_strict)
18261 if (strcmp (language_string, "GNU Go") == 0)
18262 language = DW_LANG_Go;
18265 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
18266 else if (strcmp (language_string, "GNU Fortran") == 0)
18267 language = DW_LANG_Fortran90;
18269 add_AT_unsigned (die, DW_AT_language, language);
18271 switch (language)
18273 case DW_LANG_Fortran77:
18274 case DW_LANG_Fortran90:
18275 case DW_LANG_Fortran95:
18276 /* Fortran has case insensitive identifiers and the front-end
18277 lowercases everything. */
18278 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
18279 break;
18280 default:
18281 /* The default DW_ID_case_sensitive doesn't need to be specified. */
18282 break;
18284 return die;
18287 /* Generate the DIE for a base class. */
18289 static void
18290 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
18292 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
18294 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
18295 add_data_member_location_attribute (die, binfo);
18297 if (BINFO_VIRTUAL_P (binfo))
18298 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
18300 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
18301 children, otherwise the default is DW_ACCESS_public. In DWARF2
18302 the default has always been DW_ACCESS_private. */
18303 if (access == access_public_node)
18305 if (dwarf_version == 2
18306 || context_die->die_tag == DW_TAG_class_type)
18307 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
18309 else if (access == access_protected_node)
18310 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
18311 else if (dwarf_version > 2
18312 && context_die->die_tag != DW_TAG_class_type)
18313 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
18316 /* Generate a DIE for a class member. */
18318 static void
18319 gen_member_die (tree type, dw_die_ref context_die)
18321 tree member;
18322 tree binfo = TYPE_BINFO (type);
18323 dw_die_ref child;
18325 /* If this is not an incomplete type, output descriptions of each of its
18326 members. Note that as we output the DIEs necessary to represent the
18327 members of this record or union type, we will also be trying to output
18328 DIEs to represent the *types* of those members. However the `type'
18329 function (above) will specifically avoid generating type DIEs for member
18330 types *within* the list of member DIEs for this (containing) type except
18331 for those types (of members) which are explicitly marked as also being
18332 members of this (containing) type themselves. The g++ front- end can
18333 force any given type to be treated as a member of some other (containing)
18334 type by setting the TYPE_CONTEXT of the given (member) type to point to
18335 the TREE node representing the appropriate (containing) type. */
18337 /* First output info about the base classes. */
18338 if (binfo)
18340 VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
18341 int i;
18342 tree base;
18344 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
18345 gen_inheritance_die (base,
18346 (accesses ? VEC_index (tree, accesses, i)
18347 : access_public_node), context_die);
18350 /* Now output info about the data members and type members. */
18351 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
18353 /* If we thought we were generating minimal debug info for TYPE
18354 and then changed our minds, some of the member declarations
18355 may have already been defined. Don't define them again, but
18356 do put them in the right order. */
18358 child = lookup_decl_die (member);
18359 if (child)
18360 splice_child_die (context_die, child);
18361 else
18362 gen_decl_die (member, NULL, context_die);
18365 /* Now output info about the function members (if any). */
18366 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
18368 /* Don't include clones in the member list. */
18369 if (DECL_ABSTRACT_ORIGIN (member))
18370 continue;
18372 child = lookup_decl_die (member);
18373 if (child)
18374 splice_child_die (context_die, child);
18375 else
18376 gen_decl_die (member, NULL, context_die);
18380 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
18381 is set, we pretend that the type was never defined, so we only get the
18382 member DIEs needed by later specification DIEs. */
18384 static void
18385 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
18386 enum debug_info_usage usage)
18388 dw_die_ref type_die = lookup_type_die (type);
18389 dw_die_ref scope_die = 0;
18390 int nested = 0;
18391 int complete = (TYPE_SIZE (type)
18392 && (! TYPE_STUB_DECL (type)
18393 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
18394 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
18395 complete = complete && should_emit_struct_debug (type, usage);
18397 if (type_die && ! complete)
18398 return;
18400 if (TYPE_CONTEXT (type) != NULL_TREE
18401 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
18402 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
18403 nested = 1;
18405 scope_die = scope_die_for (type, context_die);
18407 if (! type_die || (nested && is_cu_die (scope_die)))
18408 /* First occurrence of type or toplevel definition of nested class. */
18410 dw_die_ref old_die = type_die;
18412 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
18413 ? record_type_tag (type) : DW_TAG_union_type,
18414 scope_die, type);
18415 equate_type_number_to_die (type, type_die);
18416 if (old_die)
18417 add_AT_specification (type_die, old_die);
18418 else
18419 add_name_attribute (type_die, type_tag (type));
18421 else
18422 remove_AT (type_die, DW_AT_declaration);
18424 /* Generate child dies for template paramaters. */
18425 if (debug_info_level > DINFO_LEVEL_TERSE
18426 && COMPLETE_TYPE_P (type))
18427 schedule_generic_params_dies_gen (type);
18429 /* If this type has been completed, then give it a byte_size attribute and
18430 then give a list of members. */
18431 if (complete && !ns_decl)
18433 /* Prevent infinite recursion in cases where the type of some member of
18434 this type is expressed in terms of this type itself. */
18435 TREE_ASM_WRITTEN (type) = 1;
18436 add_byte_size_attribute (type_die, type);
18437 if (TYPE_STUB_DECL (type) != NULL_TREE)
18439 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
18440 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
18443 /* If the first reference to this type was as the return type of an
18444 inline function, then it may not have a parent. Fix this now. */
18445 if (type_die->die_parent == NULL)
18446 add_child_die (scope_die, type_die);
18448 push_decl_scope (type);
18449 gen_member_die (type, type_die);
18450 pop_decl_scope ();
18452 add_gnat_descriptive_type_attribute (type_die, type, context_die);
18453 if (TYPE_ARTIFICIAL (type))
18454 add_AT_flag (type_die, DW_AT_artificial, 1);
18456 /* GNU extension: Record what type our vtable lives in. */
18457 if (TYPE_VFIELD (type))
18459 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
18461 gen_type_die (vtype, context_die);
18462 add_AT_die_ref (type_die, DW_AT_containing_type,
18463 lookup_type_die (vtype));
18466 else
18468 add_AT_flag (type_die, DW_AT_declaration, 1);
18470 /* We don't need to do this for function-local types. */
18471 if (TYPE_STUB_DECL (type)
18472 && ! decl_function_context (TYPE_STUB_DECL (type)))
18473 VEC_safe_push (tree, gc, incomplete_types, type);
18476 if (get_AT (type_die, DW_AT_name))
18477 add_pubtype (type, type_die);
18480 /* Generate a DIE for a subroutine _type_. */
18482 static void
18483 gen_subroutine_type_die (tree type, dw_die_ref context_die)
18485 tree return_type = TREE_TYPE (type);
18486 dw_die_ref subr_die
18487 = new_die (DW_TAG_subroutine_type,
18488 scope_die_for (type, context_die), type);
18490 equate_type_number_to_die (type, subr_die);
18491 add_prototyped_attribute (subr_die, type);
18492 add_type_attribute (subr_die, return_type, 0, 0, context_die);
18493 gen_formal_types_die (type, subr_die);
18495 if (get_AT (subr_die, DW_AT_name))
18496 add_pubtype (type, subr_die);
18499 /* Generate a DIE for a type definition. */
18501 static void
18502 gen_typedef_die (tree decl, dw_die_ref context_die)
18504 dw_die_ref type_die;
18505 tree origin;
18507 if (TREE_ASM_WRITTEN (decl))
18508 return;
18510 TREE_ASM_WRITTEN (decl) = 1;
18511 type_die = new_die (DW_TAG_typedef, context_die, decl);
18512 origin = decl_ultimate_origin (decl);
18513 if (origin != NULL)
18514 add_abstract_origin_attribute (type_die, origin);
18515 else
18517 tree type;
18519 add_name_and_src_coords_attributes (type_die, decl);
18520 if (DECL_ORIGINAL_TYPE (decl))
18522 type = DECL_ORIGINAL_TYPE (decl);
18524 gcc_assert (type != TREE_TYPE (decl));
18525 equate_type_number_to_die (TREE_TYPE (decl), type_die);
18527 else
18529 type = TREE_TYPE (decl);
18531 if (is_naming_typedef_decl (TYPE_NAME (type)))
18533 /* Here, we are in the case of decl being a typedef naming
18534 an anonymous type, e.g:
18535 typedef struct {...} foo;
18536 In that case TREE_TYPE (decl) is not a typedef variant
18537 type and TYPE_NAME of the anonymous type is set to the
18538 TYPE_DECL of the typedef. This construct is emitted by
18539 the C++ FE.
18541 TYPE is the anonymous struct named by the typedef
18542 DECL. As we need the DW_AT_type attribute of the
18543 DW_TAG_typedef to point to the DIE of TYPE, let's
18544 generate that DIE right away. add_type_attribute
18545 called below will then pick (via lookup_type_die) that
18546 anonymous struct DIE. */
18547 if (!TREE_ASM_WRITTEN (type))
18548 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
18550 /* This is a GNU Extension. We are adding a
18551 DW_AT_linkage_name attribute to the DIE of the
18552 anonymous struct TYPE. The value of that attribute
18553 is the name of the typedef decl naming the anonymous
18554 struct. This greatly eases the work of consumers of
18555 this debug info. */
18556 add_linkage_attr (lookup_type_die (type), decl);
18560 add_type_attribute (type_die, type, TREE_READONLY (decl),
18561 TREE_THIS_VOLATILE (decl), context_die);
18563 if (is_naming_typedef_decl (decl))
18564 /* We want that all subsequent calls to lookup_type_die with
18565 TYPE in argument yield the DW_TAG_typedef we have just
18566 created. */
18567 equate_type_number_to_die (type, type_die);
18569 add_accessibility_attribute (type_die, decl);
18572 if (DECL_ABSTRACT (decl))
18573 equate_decl_number_to_die (decl, type_die);
18575 if (get_AT (type_die, DW_AT_name))
18576 add_pubtype (decl, type_die);
18579 /* Generate a DIE for a struct, class, enum or union type. */
18581 static void
18582 gen_tagged_type_die (tree type,
18583 dw_die_ref context_die,
18584 enum debug_info_usage usage)
18586 int need_pop;
18588 if (type == NULL_TREE
18589 || !is_tagged_type (type))
18590 return;
18592 /* If this is a nested type whose containing class hasn't been written
18593 out yet, writing it out will cover this one, too. This does not apply
18594 to instantiations of member class templates; they need to be added to
18595 the containing class as they are generated. FIXME: This hurts the
18596 idea of combining type decls from multiple TUs, since we can't predict
18597 what set of template instantiations we'll get. */
18598 if (TYPE_CONTEXT (type)
18599 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
18600 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
18602 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
18604 if (TREE_ASM_WRITTEN (type))
18605 return;
18607 /* If that failed, attach ourselves to the stub. */
18608 push_decl_scope (TYPE_CONTEXT (type));
18609 context_die = lookup_type_die (TYPE_CONTEXT (type));
18610 need_pop = 1;
18612 else if (TYPE_CONTEXT (type) != NULL_TREE
18613 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
18615 /* If this type is local to a function that hasn't been written
18616 out yet, use a NULL context for now; it will be fixed up in
18617 decls_for_scope. */
18618 context_die = lookup_decl_die (TYPE_CONTEXT (type));
18619 /* A declaration DIE doesn't count; nested types need to go in the
18620 specification. */
18621 if (context_die && is_declaration_die (context_die))
18622 context_die = NULL;
18623 need_pop = 0;
18625 else
18627 context_die = declare_in_namespace (type, context_die);
18628 need_pop = 0;
18631 if (TREE_CODE (type) == ENUMERAL_TYPE)
18633 /* This might have been written out by the call to
18634 declare_in_namespace. */
18635 if (!TREE_ASM_WRITTEN (type))
18636 gen_enumeration_type_die (type, context_die);
18638 else
18639 gen_struct_or_union_type_die (type, context_die, usage);
18641 if (need_pop)
18642 pop_decl_scope ();
18644 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
18645 it up if it is ever completed. gen_*_type_die will set it for us
18646 when appropriate. */
18649 /* Generate a type description DIE. */
18651 static void
18652 gen_type_die_with_usage (tree type, dw_die_ref context_die,
18653 enum debug_info_usage usage)
18655 struct array_descr_info info;
18657 if (type == NULL_TREE || type == error_mark_node)
18658 return;
18660 if (TYPE_NAME (type) != NULL_TREE
18661 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
18662 && is_redundant_typedef (TYPE_NAME (type))
18663 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
18664 /* The DECL of this type is a typedef we don't want to emit debug
18665 info for but we want debug info for its underlying typedef.
18666 This can happen for e.g, the injected-class-name of a C++
18667 type. */
18668 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
18670 /* If TYPE is a typedef type variant, let's generate debug info
18671 for the parent typedef which TYPE is a type of. */
18672 if (typedef_variant_p (type))
18674 if (TREE_ASM_WRITTEN (type))
18675 return;
18677 /* Prevent broken recursion; we can't hand off to the same type. */
18678 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
18680 /* Give typedefs the right scope. */
18681 context_die = scope_die_for (type, context_die);
18683 TREE_ASM_WRITTEN (type) = 1;
18685 gen_decl_die (TYPE_NAME (type), NULL, context_die);
18686 return;
18689 /* If type is an anonymous tagged type named by a typedef, let's
18690 generate debug info for the typedef. */
18691 if (is_naming_typedef_decl (TYPE_NAME (type)))
18693 /* Use the DIE of the containing namespace as the parent DIE of
18694 the type description DIE we want to generate. */
18695 if (DECL_CONTEXT (TYPE_NAME (type))
18696 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
18697 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
18699 gen_decl_die (TYPE_NAME (type), NULL, context_die);
18700 return;
18703 /* If this is an array type with hidden descriptor, handle it first. */
18704 if (!TREE_ASM_WRITTEN (type)
18705 && lang_hooks.types.get_array_descr_info
18706 && lang_hooks.types.get_array_descr_info (type, &info)
18707 && (dwarf_version >= 3 || !dwarf_strict))
18709 gen_descr_array_type_die (type, &info, context_die);
18710 TREE_ASM_WRITTEN (type) = 1;
18711 return;
18714 /* We are going to output a DIE to represent the unqualified version
18715 of this type (i.e. without any const or volatile qualifiers) so
18716 get the main variant (i.e. the unqualified version) of this type
18717 now. (Vectors are special because the debugging info is in the
18718 cloned type itself). */
18719 if (TREE_CODE (type) != VECTOR_TYPE)
18720 type = type_main_variant (type);
18722 if (TREE_ASM_WRITTEN (type))
18723 return;
18725 switch (TREE_CODE (type))
18727 case ERROR_MARK:
18728 break;
18730 case POINTER_TYPE:
18731 case REFERENCE_TYPE:
18732 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
18733 ensures that the gen_type_die recursion will terminate even if the
18734 type is recursive. Recursive types are possible in Ada. */
18735 /* ??? We could perhaps do this for all types before the switch
18736 statement. */
18737 TREE_ASM_WRITTEN (type) = 1;
18739 /* For these types, all that is required is that we output a DIE (or a
18740 set of DIEs) to represent the "basis" type. */
18741 gen_type_die_with_usage (TREE_TYPE (type), context_die,
18742 DINFO_USAGE_IND_USE);
18743 break;
18745 case OFFSET_TYPE:
18746 /* This code is used for C++ pointer-to-data-member types.
18747 Output a description of the relevant class type. */
18748 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
18749 DINFO_USAGE_IND_USE);
18751 /* Output a description of the type of the object pointed to. */
18752 gen_type_die_with_usage (TREE_TYPE (type), context_die,
18753 DINFO_USAGE_IND_USE);
18755 /* Now output a DIE to represent this pointer-to-data-member type
18756 itself. */
18757 gen_ptr_to_mbr_type_die (type, context_die);
18758 break;
18760 case FUNCTION_TYPE:
18761 /* Force out return type (in case it wasn't forced out already). */
18762 gen_type_die_with_usage (TREE_TYPE (type), context_die,
18763 DINFO_USAGE_DIR_USE);
18764 gen_subroutine_type_die (type, context_die);
18765 break;
18767 case METHOD_TYPE:
18768 /* Force out return type (in case it wasn't forced out already). */
18769 gen_type_die_with_usage (TREE_TYPE (type), context_die,
18770 DINFO_USAGE_DIR_USE);
18771 gen_subroutine_type_die (type, context_die);
18772 break;
18774 case ARRAY_TYPE:
18775 gen_array_type_die (type, context_die);
18776 break;
18778 case VECTOR_TYPE:
18779 gen_array_type_die (type, context_die);
18780 break;
18782 case ENUMERAL_TYPE:
18783 case RECORD_TYPE:
18784 case UNION_TYPE:
18785 case QUAL_UNION_TYPE:
18786 gen_tagged_type_die (type, context_die, usage);
18787 return;
18789 case VOID_TYPE:
18790 case INTEGER_TYPE:
18791 case REAL_TYPE:
18792 case FIXED_POINT_TYPE:
18793 case COMPLEX_TYPE:
18794 case BOOLEAN_TYPE:
18795 /* No DIEs needed for fundamental types. */
18796 break;
18798 case NULLPTR_TYPE:
18799 case LANG_TYPE:
18800 /* Just use DW_TAG_unspecified_type. */
18802 dw_die_ref type_die = lookup_type_die (type);
18803 if (type_die == NULL)
18805 tree name = TYPE_NAME (type);
18806 if (TREE_CODE (name) == TYPE_DECL)
18807 name = DECL_NAME (name);
18808 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
18809 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
18810 equate_type_number_to_die (type, type_die);
18813 break;
18815 default:
18816 gcc_unreachable ();
18819 TREE_ASM_WRITTEN (type) = 1;
18822 static void
18823 gen_type_die (tree type, dw_die_ref context_die)
18825 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
18828 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
18829 things which are local to the given block. */
18831 static void
18832 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
18834 int must_output_die = 0;
18835 bool inlined_func;
18837 /* Ignore blocks that are NULL. */
18838 if (stmt == NULL_TREE)
18839 return;
18841 inlined_func = inlined_function_outer_scope_p (stmt);
18843 /* If the block is one fragment of a non-contiguous block, do not
18844 process the variables, since they will have been done by the
18845 origin block. Do process subblocks. */
18846 if (BLOCK_FRAGMENT_ORIGIN (stmt))
18848 tree sub;
18850 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
18851 gen_block_die (sub, context_die, depth + 1);
18853 return;
18856 /* Determine if we need to output any Dwarf DIEs at all to represent this
18857 block. */
18858 if (inlined_func)
18859 /* The outer scopes for inlinings *must* always be represented. We
18860 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
18861 must_output_die = 1;
18862 else
18864 /* Determine if this block directly contains any "significant"
18865 local declarations which we will need to output DIEs for. */
18866 if (debug_info_level > DINFO_LEVEL_TERSE)
18867 /* We are not in terse mode so *any* local declaration counts
18868 as being a "significant" one. */
18869 must_output_die = ((BLOCK_VARS (stmt) != NULL
18870 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
18871 && (TREE_USED (stmt)
18872 || TREE_ASM_WRITTEN (stmt)
18873 || BLOCK_ABSTRACT (stmt)));
18874 else if ((TREE_USED (stmt)
18875 || TREE_ASM_WRITTEN (stmt)
18876 || BLOCK_ABSTRACT (stmt))
18877 && !dwarf2out_ignore_block (stmt))
18878 must_output_die = 1;
18881 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
18882 DIE for any block which contains no significant local declarations at
18883 all. Rather, in such cases we just call `decls_for_scope' so that any
18884 needed Dwarf info for any sub-blocks will get properly generated. Note
18885 that in terse mode, our definition of what constitutes a "significant"
18886 local declaration gets restricted to include only inlined function
18887 instances and local (nested) function definitions. */
18888 if (must_output_die)
18890 if (inlined_func)
18892 /* If STMT block is abstract, that means we have been called
18893 indirectly from dwarf2out_abstract_function.
18894 That function rightfully marks the descendent blocks (of
18895 the abstract function it is dealing with) as being abstract,
18896 precisely to prevent us from emitting any
18897 DW_TAG_inlined_subroutine DIE as a descendent
18898 of an abstract function instance. So in that case, we should
18899 not call gen_inlined_subroutine_die.
18901 Later though, when cgraph asks dwarf2out to emit info
18902 for the concrete instance of the function decl into which
18903 the concrete instance of STMT got inlined, the later will lead
18904 to the generation of a DW_TAG_inlined_subroutine DIE. */
18905 if (! BLOCK_ABSTRACT (stmt))
18906 gen_inlined_subroutine_die (stmt, context_die, depth);
18908 else
18909 gen_lexical_block_die (stmt, context_die, depth);
18911 else
18912 decls_for_scope (stmt, context_die, depth);
18915 /* Process variable DECL (or variable with origin ORIGIN) within
18916 block STMT and add it to CONTEXT_DIE. */
18917 static void
18918 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
18920 dw_die_ref die;
18921 tree decl_or_origin = decl ? decl : origin;
18923 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
18924 die = lookup_decl_die (decl_or_origin);
18925 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
18926 && TYPE_DECL_IS_STUB (decl_or_origin))
18927 die = lookup_type_die (TREE_TYPE (decl_or_origin));
18928 else
18929 die = NULL;
18931 if (die != NULL && die->die_parent == NULL)
18932 add_child_die (context_die, die);
18933 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
18934 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
18935 stmt, context_die);
18936 else
18937 gen_decl_die (decl, origin, context_die);
18940 /* Generate all of the decls declared within a given scope and (recursively)
18941 all of its sub-blocks. */
18943 static void
18944 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
18946 tree decl;
18947 unsigned int i;
18948 tree subblocks;
18950 /* Ignore NULL blocks. */
18951 if (stmt == NULL_TREE)
18952 return;
18954 /* Output the DIEs to represent all of the data objects and typedefs
18955 declared directly within this block but not within any nested
18956 sub-blocks. Also, nested function and tag DIEs have been
18957 generated with a parent of NULL; fix that up now. */
18958 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
18959 process_scope_var (stmt, decl, NULL_TREE, context_die);
18960 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
18961 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
18962 context_die);
18964 /* If we're at -g1, we're not interested in subblocks. */
18965 if (debug_info_level <= DINFO_LEVEL_TERSE)
18966 return;
18968 /* Output the DIEs to represent all sub-blocks (and the items declared
18969 therein) of this block. */
18970 for (subblocks = BLOCK_SUBBLOCKS (stmt);
18971 subblocks != NULL;
18972 subblocks = BLOCK_CHAIN (subblocks))
18973 gen_block_die (subblocks, context_die, depth + 1);
18976 /* Is this a typedef we can avoid emitting? */
18978 static inline int
18979 is_redundant_typedef (const_tree decl)
18981 if (TYPE_DECL_IS_STUB (decl))
18982 return 1;
18984 if (DECL_ARTIFICIAL (decl)
18985 && DECL_CONTEXT (decl)
18986 && is_tagged_type (DECL_CONTEXT (decl))
18987 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
18988 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
18989 /* Also ignore the artificial member typedef for the class name. */
18990 return 1;
18992 return 0;
18995 /* Return TRUE if TYPE is a typedef that names a type for linkage
18996 purposes. This kind of typedefs is produced by the C++ FE for
18997 constructs like:
18999 typedef struct {...} foo;
19001 In that case, there is no typedef variant type produced for foo.
19002 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
19003 struct type. */
19005 static bool
19006 is_naming_typedef_decl (const_tree decl)
19008 if (decl == NULL_TREE
19009 || TREE_CODE (decl) != TYPE_DECL
19010 || !is_tagged_type (TREE_TYPE (decl))
19011 || DECL_IS_BUILTIN (decl)
19012 || is_redundant_typedef (decl)
19013 /* It looks like Ada produces TYPE_DECLs that are very similar
19014 to C++ naming typedefs but that have different
19015 semantics. Let's be specific to c++ for now. */
19016 || !is_cxx ())
19017 return FALSE;
19019 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
19020 && TYPE_NAME (TREE_TYPE (decl)) == decl
19021 && (TYPE_STUB_DECL (TREE_TYPE (decl))
19022 != TYPE_NAME (TREE_TYPE (decl))));
19025 /* Returns the DIE for a context. */
19027 static inline dw_die_ref
19028 get_context_die (tree context)
19030 if (context)
19032 /* Find die that represents this context. */
19033 if (TYPE_P (context))
19035 context = TYPE_MAIN_VARIANT (context);
19036 return strip_naming_typedef (context, force_type_die (context));
19038 else
19039 return force_decl_die (context);
19041 return comp_unit_die ();
19044 /* Returns the DIE for decl. A DIE will always be returned. */
19046 static dw_die_ref
19047 force_decl_die (tree decl)
19049 dw_die_ref decl_die;
19050 unsigned saved_external_flag;
19051 tree save_fn = NULL_TREE;
19052 decl_die = lookup_decl_die (decl);
19053 if (!decl_die)
19055 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
19057 decl_die = lookup_decl_die (decl);
19058 if (decl_die)
19059 return decl_die;
19061 switch (TREE_CODE (decl))
19063 case FUNCTION_DECL:
19064 /* Clear current_function_decl, so that gen_subprogram_die thinks
19065 that this is a declaration. At this point, we just want to force
19066 declaration die. */
19067 save_fn = current_function_decl;
19068 current_function_decl = NULL_TREE;
19069 gen_subprogram_die (decl, context_die);
19070 current_function_decl = save_fn;
19071 break;
19073 case VAR_DECL:
19074 /* Set external flag to force declaration die. Restore it after
19075 gen_decl_die() call. */
19076 saved_external_flag = DECL_EXTERNAL (decl);
19077 DECL_EXTERNAL (decl) = 1;
19078 gen_decl_die (decl, NULL, context_die);
19079 DECL_EXTERNAL (decl) = saved_external_flag;
19080 break;
19082 case NAMESPACE_DECL:
19083 if (dwarf_version >= 3 || !dwarf_strict)
19084 dwarf2out_decl (decl);
19085 else
19086 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
19087 decl_die = comp_unit_die ();
19088 break;
19090 case TRANSLATION_UNIT_DECL:
19091 decl_die = comp_unit_die ();
19092 break;
19094 default:
19095 gcc_unreachable ();
19098 /* We should be able to find the DIE now. */
19099 if (!decl_die)
19100 decl_die = lookup_decl_die (decl);
19101 gcc_assert (decl_die);
19104 return decl_die;
19107 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
19108 always returned. */
19110 static dw_die_ref
19111 force_type_die (tree type)
19113 dw_die_ref type_die;
19115 type_die = lookup_type_die (type);
19116 if (!type_die)
19118 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
19120 type_die = modified_type_die (type, TYPE_READONLY (type),
19121 TYPE_VOLATILE (type), context_die);
19122 gcc_assert (type_die);
19124 return type_die;
19127 /* Force out any required namespaces to be able to output DECL,
19128 and return the new context_die for it, if it's changed. */
19130 static dw_die_ref
19131 setup_namespace_context (tree thing, dw_die_ref context_die)
19133 tree context = (DECL_P (thing)
19134 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
19135 if (context && TREE_CODE (context) == NAMESPACE_DECL)
19136 /* Force out the namespace. */
19137 context_die = force_decl_die (context);
19139 return context_die;
19142 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
19143 type) within its namespace, if appropriate.
19145 For compatibility with older debuggers, namespace DIEs only contain
19146 declarations; all definitions are emitted at CU scope. */
19148 static dw_die_ref
19149 declare_in_namespace (tree thing, dw_die_ref context_die)
19151 dw_die_ref ns_context;
19153 if (debug_info_level <= DINFO_LEVEL_TERSE)
19154 return context_die;
19156 /* If this decl is from an inlined function, then don't try to emit it in its
19157 namespace, as we will get confused. It would have already been emitted
19158 when the abstract instance of the inline function was emitted anyways. */
19159 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
19160 return context_die;
19162 ns_context = setup_namespace_context (thing, context_die);
19164 if (ns_context != context_die)
19166 if (is_fortran ())
19167 return ns_context;
19168 if (DECL_P (thing))
19169 gen_decl_die (thing, NULL, ns_context);
19170 else
19171 gen_type_die (thing, ns_context);
19173 return context_die;
19176 /* Generate a DIE for a namespace or namespace alias. */
19178 static void
19179 gen_namespace_die (tree decl, dw_die_ref context_die)
19181 dw_die_ref namespace_die;
19183 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
19184 they are an alias of. */
19185 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
19187 /* Output a real namespace or module. */
19188 context_die = setup_namespace_context (decl, comp_unit_die ());
19189 namespace_die = new_die (is_fortran ()
19190 ? DW_TAG_module : DW_TAG_namespace,
19191 context_die, decl);
19192 /* For Fortran modules defined in different CU don't add src coords. */
19193 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
19195 const char *name = dwarf2_name (decl, 0);
19196 if (name)
19197 add_name_attribute (namespace_die, name);
19199 else
19200 add_name_and_src_coords_attributes (namespace_die, decl);
19201 if (DECL_EXTERNAL (decl))
19202 add_AT_flag (namespace_die, DW_AT_declaration, 1);
19203 equate_decl_number_to_die (decl, namespace_die);
19205 else
19207 /* Output a namespace alias. */
19209 /* Force out the namespace we are an alias of, if necessary. */
19210 dw_die_ref origin_die
19211 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
19213 if (DECL_FILE_SCOPE_P (decl)
19214 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
19215 context_die = setup_namespace_context (decl, comp_unit_die ());
19216 /* Now create the namespace alias DIE. */
19217 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
19218 add_name_and_src_coords_attributes (namespace_die, decl);
19219 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
19220 equate_decl_number_to_die (decl, namespace_die);
19222 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
19223 if (want_pubnames ())
19224 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
19227 /* Generate Dwarf debug information for a decl described by DECL.
19228 The return value is currently only meaningful for PARM_DECLs,
19229 for all other decls it returns NULL. */
19231 static dw_die_ref
19232 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
19234 tree decl_or_origin = decl ? decl : origin;
19235 tree class_origin = NULL, ultimate_origin;
19237 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
19238 return NULL;
19240 switch (TREE_CODE (decl_or_origin))
19242 case ERROR_MARK:
19243 break;
19245 case CONST_DECL:
19246 if (!is_fortran () && !is_ada ())
19248 /* The individual enumerators of an enum type get output when we output
19249 the Dwarf representation of the relevant enum type itself. */
19250 break;
19253 /* Emit its type. */
19254 gen_type_die (TREE_TYPE (decl), context_die);
19256 /* And its containing namespace. */
19257 context_die = declare_in_namespace (decl, context_die);
19259 gen_const_die (decl, context_die);
19260 break;
19262 case FUNCTION_DECL:
19263 /* Don't output any DIEs to represent mere function declarations,
19264 unless they are class members or explicit block externs. */
19265 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
19266 && DECL_FILE_SCOPE_P (decl_or_origin)
19267 && (current_function_decl == NULL_TREE
19268 || DECL_ARTIFICIAL (decl_or_origin)))
19269 break;
19271 #if 0
19272 /* FIXME */
19273 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
19274 on local redeclarations of global functions. That seems broken. */
19275 if (current_function_decl != decl)
19276 /* This is only a declaration. */;
19277 #endif
19279 /* If we're emitting a clone, emit info for the abstract instance. */
19280 if (origin || DECL_ORIGIN (decl) != decl)
19281 dwarf2out_abstract_function (origin
19282 ? DECL_ORIGIN (origin)
19283 : DECL_ABSTRACT_ORIGIN (decl));
19285 /* If we're emitting an out-of-line copy of an inline function,
19286 emit info for the abstract instance and set up to refer to it. */
19287 else if (cgraph_function_possibly_inlined_p (decl)
19288 && ! DECL_ABSTRACT (decl)
19289 && ! class_or_namespace_scope_p (context_die)
19290 /* dwarf2out_abstract_function won't emit a die if this is just
19291 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
19292 that case, because that works only if we have a die. */
19293 && DECL_INITIAL (decl) != NULL_TREE)
19295 dwarf2out_abstract_function (decl);
19296 set_decl_origin_self (decl);
19299 /* Otherwise we're emitting the primary DIE for this decl. */
19300 else if (debug_info_level > DINFO_LEVEL_TERSE)
19302 /* Before we describe the FUNCTION_DECL itself, make sure that we
19303 have its containing type. */
19304 if (!origin)
19305 origin = decl_class_context (decl);
19306 if (origin != NULL_TREE)
19307 gen_type_die (origin, context_die);
19309 /* And its return type. */
19310 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
19312 /* And its virtual context. */
19313 if (DECL_VINDEX (decl) != NULL_TREE)
19314 gen_type_die (DECL_CONTEXT (decl), context_die);
19316 /* Make sure we have a member DIE for decl. */
19317 if (origin != NULL_TREE)
19318 gen_type_die_for_member (origin, decl, context_die);
19320 /* And its containing namespace. */
19321 context_die = declare_in_namespace (decl, context_die);
19324 /* Now output a DIE to represent the function itself. */
19325 if (decl)
19326 gen_subprogram_die (decl, context_die);
19327 break;
19329 case TYPE_DECL:
19330 /* If we are in terse mode, don't generate any DIEs to represent any
19331 actual typedefs. */
19332 if (debug_info_level <= DINFO_LEVEL_TERSE)
19333 break;
19335 /* In the special case of a TYPE_DECL node representing the declaration
19336 of some type tag, if the given TYPE_DECL is marked as having been
19337 instantiated from some other (original) TYPE_DECL node (e.g. one which
19338 was generated within the original definition of an inline function) we
19339 used to generate a special (abbreviated) DW_TAG_structure_type,
19340 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
19341 should be actually referencing those DIEs, as variable DIEs with that
19342 type would be emitted already in the abstract origin, so it was always
19343 removed during unused type prunning. Don't add anything in this
19344 case. */
19345 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
19346 break;
19348 if (is_redundant_typedef (decl))
19349 gen_type_die (TREE_TYPE (decl), context_die);
19350 else
19351 /* Output a DIE to represent the typedef itself. */
19352 gen_typedef_die (decl, context_die);
19353 break;
19355 case LABEL_DECL:
19356 if (debug_info_level >= DINFO_LEVEL_NORMAL)
19357 gen_label_die (decl, context_die);
19358 break;
19360 case VAR_DECL:
19361 case RESULT_DECL:
19362 /* If we are in terse mode, don't generate any DIEs to represent any
19363 variable declarations or definitions. */
19364 if (debug_info_level <= DINFO_LEVEL_TERSE)
19365 break;
19367 /* Output any DIEs that are needed to specify the type of this data
19368 object. */
19369 if (decl_by_reference_p (decl_or_origin))
19370 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
19371 else
19372 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
19374 /* And its containing type. */
19375 class_origin = decl_class_context (decl_or_origin);
19376 if (class_origin != NULL_TREE)
19377 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
19379 /* And its containing namespace. */
19380 context_die = declare_in_namespace (decl_or_origin, context_die);
19382 /* Now output the DIE to represent the data object itself. This gets
19383 complicated because of the possibility that the VAR_DECL really
19384 represents an inlined instance of a formal parameter for an inline
19385 function. */
19386 ultimate_origin = decl_ultimate_origin (decl_or_origin);
19387 if (ultimate_origin != NULL_TREE
19388 && TREE_CODE (ultimate_origin) == PARM_DECL)
19389 gen_formal_parameter_die (decl, origin,
19390 true /* Emit name attribute. */,
19391 context_die);
19392 else
19393 gen_variable_die (decl, origin, context_die);
19394 break;
19396 case FIELD_DECL:
19397 /* Ignore the nameless fields that are used to skip bits but handle C++
19398 anonymous unions and structs. */
19399 if (DECL_NAME (decl) != NULL_TREE
19400 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
19401 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
19403 gen_type_die (member_declared_type (decl), context_die);
19404 gen_field_die (decl, context_die);
19406 break;
19408 case PARM_DECL:
19409 if (DECL_BY_REFERENCE (decl_or_origin))
19410 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
19411 else
19412 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
19413 return gen_formal_parameter_die (decl, origin,
19414 true /* Emit name attribute. */,
19415 context_die);
19417 case NAMESPACE_DECL:
19418 case IMPORTED_DECL:
19419 if (dwarf_version >= 3 || !dwarf_strict)
19420 gen_namespace_die (decl, context_die);
19421 break;
19423 default:
19424 /* Probably some frontend-internal decl. Assume we don't care. */
19425 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
19426 break;
19429 return NULL;
19432 /* Output debug information for global decl DECL. Called from toplev.c after
19433 compilation proper has finished. */
19435 static void
19436 dwarf2out_global_decl (tree decl)
19438 /* Output DWARF2 information for file-scope tentative data object
19439 declarations, file-scope (extern) function declarations (which
19440 had no corresponding body) and file-scope tagged type declarations
19441 and definitions which have not yet been forced out. */
19442 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
19443 dwarf2out_decl (decl);
19446 /* Output debug information for type decl DECL. Called from toplev.c
19447 and from language front ends (to record built-in types). */
19448 static void
19449 dwarf2out_type_decl (tree decl, int local)
19451 if (!local)
19452 dwarf2out_decl (decl);
19455 /* Output debug information for imported module or decl DECL.
19456 NAME is non-NULL name in the lexical block if the decl has been renamed.
19457 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
19458 that DECL belongs to.
19459 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
19460 static void
19461 dwarf2out_imported_module_or_decl_1 (tree decl,
19462 tree name,
19463 tree lexical_block,
19464 dw_die_ref lexical_block_die)
19466 expanded_location xloc;
19467 dw_die_ref imported_die = NULL;
19468 dw_die_ref at_import_die;
19470 if (TREE_CODE (decl) == IMPORTED_DECL)
19472 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
19473 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
19474 gcc_assert (decl);
19476 else
19477 xloc = expand_location (input_location);
19479 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
19481 at_import_die = force_type_die (TREE_TYPE (decl));
19482 /* For namespace N { typedef void T; } using N::T; base_type_die
19483 returns NULL, but DW_TAG_imported_declaration requires
19484 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
19485 if (!at_import_die)
19487 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
19488 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
19489 at_import_die = lookup_type_die (TREE_TYPE (decl));
19490 gcc_assert (at_import_die);
19493 else
19495 at_import_die = lookup_decl_die (decl);
19496 if (!at_import_die)
19498 /* If we're trying to avoid duplicate debug info, we may not have
19499 emitted the member decl for this field. Emit it now. */
19500 if (TREE_CODE (decl) == FIELD_DECL)
19502 tree type = DECL_CONTEXT (decl);
19504 if (TYPE_CONTEXT (type)
19505 && TYPE_P (TYPE_CONTEXT (type))
19506 && !should_emit_struct_debug (TYPE_CONTEXT (type),
19507 DINFO_USAGE_DIR_USE))
19508 return;
19509 gen_type_die_for_member (type, decl,
19510 get_context_die (TYPE_CONTEXT (type)));
19512 at_import_die = force_decl_die (decl);
19516 if (TREE_CODE (decl) == NAMESPACE_DECL)
19518 if (dwarf_version >= 3 || !dwarf_strict)
19519 imported_die = new_die (DW_TAG_imported_module,
19520 lexical_block_die,
19521 lexical_block);
19522 else
19523 return;
19525 else
19526 imported_die = new_die (DW_TAG_imported_declaration,
19527 lexical_block_die,
19528 lexical_block);
19530 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
19531 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
19532 if (name)
19533 add_AT_string (imported_die, DW_AT_name,
19534 IDENTIFIER_POINTER (name));
19535 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
19538 /* Output debug information for imported module or decl DECL.
19539 NAME is non-NULL name in context if the decl has been renamed.
19540 CHILD is true if decl is one of the renamed decls as part of
19541 importing whole module. */
19543 static void
19544 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
19545 bool child)
19547 /* dw_die_ref at_import_die; */
19548 dw_die_ref scope_die;
19550 if (debug_info_level <= DINFO_LEVEL_TERSE)
19551 return;
19553 gcc_assert (decl);
19555 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
19556 We need decl DIE for reference and scope die. First, get DIE for the decl
19557 itself. */
19559 /* Get the scope die for decl context. Use comp_unit_die for global module
19560 or decl. If die is not found for non globals, force new die. */
19561 if (context
19562 && TYPE_P (context)
19563 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
19564 return;
19566 if (!(dwarf_version >= 3 || !dwarf_strict))
19567 return;
19569 scope_die = get_context_die (context);
19571 if (child)
19573 gcc_assert (scope_die->die_child);
19574 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
19575 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
19576 scope_die = scope_die->die_child;
19579 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
19580 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
19584 /* Write the debugging output for DECL. */
19586 void
19587 dwarf2out_decl (tree decl)
19589 dw_die_ref context_die = comp_unit_die ();
19591 switch (TREE_CODE (decl))
19593 case ERROR_MARK:
19594 return;
19596 case FUNCTION_DECL:
19597 /* What we would really like to do here is to filter out all mere
19598 file-scope declarations of file-scope functions which are never
19599 referenced later within this translation unit (and keep all of ones
19600 that *are* referenced later on) but we aren't clairvoyant, so we have
19601 no idea which functions will be referenced in the future (i.e. later
19602 on within the current translation unit). So here we just ignore all
19603 file-scope function declarations which are not also definitions. If
19604 and when the debugger needs to know something about these functions,
19605 it will have to hunt around and find the DWARF information associated
19606 with the definition of the function.
19608 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
19609 nodes represent definitions and which ones represent mere
19610 declarations. We have to check DECL_INITIAL instead. That's because
19611 the C front-end supports some weird semantics for "extern inline"
19612 function definitions. These can get inlined within the current
19613 translation unit (and thus, we need to generate Dwarf info for their
19614 abstract instances so that the Dwarf info for the concrete inlined
19615 instances can have something to refer to) but the compiler never
19616 generates any out-of-lines instances of such things (despite the fact
19617 that they *are* definitions).
19619 The important point is that the C front-end marks these "extern
19620 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
19621 them anyway. Note that the C++ front-end also plays some similar games
19622 for inline function definitions appearing within include files which
19623 also contain `#pragma interface' pragmas. */
19624 if (DECL_INITIAL (decl) == NULL_TREE)
19625 return;
19627 /* If we're a nested function, initially use a parent of NULL; if we're
19628 a plain function, this will be fixed up in decls_for_scope. If
19629 we're a method, it will be ignored, since we already have a DIE. */
19630 if (decl_function_context (decl)
19631 /* But if we're in terse mode, we don't care about scope. */
19632 && debug_info_level > DINFO_LEVEL_TERSE)
19633 context_die = NULL;
19634 break;
19636 case VAR_DECL:
19637 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
19638 declaration and if the declaration was never even referenced from
19639 within this entire compilation unit. We suppress these DIEs in
19640 order to save space in the .debug section (by eliminating entries
19641 which are probably useless). Note that we must not suppress
19642 block-local extern declarations (whether used or not) because that
19643 would screw-up the debugger's name lookup mechanism and cause it to
19644 miss things which really ought to be in scope at a given point. */
19645 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
19646 return;
19648 /* For local statics lookup proper context die. */
19649 if (TREE_STATIC (decl)
19650 && DECL_CONTEXT (decl)
19651 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
19652 context_die = lookup_decl_die (DECL_CONTEXT (decl));
19654 /* If we are in terse mode, don't generate any DIEs to represent any
19655 variable declarations or definitions. */
19656 if (debug_info_level <= DINFO_LEVEL_TERSE)
19657 return;
19658 break;
19660 case CONST_DECL:
19661 if (debug_info_level <= DINFO_LEVEL_TERSE)
19662 return;
19663 if (!is_fortran () && !is_ada ())
19664 return;
19665 if (TREE_STATIC (decl) && decl_function_context (decl))
19666 context_die = lookup_decl_die (DECL_CONTEXT (decl));
19667 break;
19669 case NAMESPACE_DECL:
19670 case IMPORTED_DECL:
19671 if (debug_info_level <= DINFO_LEVEL_TERSE)
19672 return;
19673 if (lookup_decl_die (decl) != NULL)
19674 return;
19675 break;
19677 case TYPE_DECL:
19678 /* Don't emit stubs for types unless they are needed by other DIEs. */
19679 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
19680 return;
19682 /* Don't bother trying to generate any DIEs to represent any of the
19683 normal built-in types for the language we are compiling. */
19684 if (DECL_IS_BUILTIN (decl))
19685 return;
19687 /* If we are in terse mode, don't generate any DIEs for types. */
19688 if (debug_info_level <= DINFO_LEVEL_TERSE)
19689 return;
19691 /* If we're a function-scope tag, initially use a parent of NULL;
19692 this will be fixed up in decls_for_scope. */
19693 if (decl_function_context (decl))
19694 context_die = NULL;
19696 break;
19698 default:
19699 return;
19702 gen_decl_die (decl, NULL, context_die);
19705 /* Write the debugging output for DECL. */
19707 static void
19708 dwarf2out_function_decl (tree decl)
19710 dwarf2out_decl (decl);
19711 call_arg_locations = NULL;
19712 call_arg_loc_last = NULL;
19713 call_site_count = -1;
19714 tail_call_site_count = -1;
19715 VEC_free (dw_die_ref, heap, block_map);
19716 htab_empty (decl_loc_table);
19717 htab_empty (cached_dw_loc_list_table);
19720 /* Output a marker (i.e. a label) for the beginning of the generated code for
19721 a lexical block. */
19723 static void
19724 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
19725 unsigned int blocknum)
19727 switch_to_section (current_function_section ());
19728 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
19731 /* Output a marker (i.e. a label) for the end of the generated code for a
19732 lexical block. */
19734 static void
19735 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
19737 switch_to_section (current_function_section ());
19738 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
19741 /* Returns nonzero if it is appropriate not to emit any debugging
19742 information for BLOCK, because it doesn't contain any instructions.
19744 Don't allow this for blocks with nested functions or local classes
19745 as we would end up with orphans, and in the presence of scheduling
19746 we may end up calling them anyway. */
19748 static bool
19749 dwarf2out_ignore_block (const_tree block)
19751 tree decl;
19752 unsigned int i;
19754 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
19755 if (TREE_CODE (decl) == FUNCTION_DECL
19756 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
19757 return 0;
19758 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
19760 decl = BLOCK_NONLOCALIZED_VAR (block, i);
19761 if (TREE_CODE (decl) == FUNCTION_DECL
19762 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
19763 return 0;
19766 return 1;
19769 /* Hash table routines for file_hash. */
19771 static int
19772 file_table_eq (const void *p1_p, const void *p2_p)
19774 const struct dwarf_file_data *const p1 =
19775 (const struct dwarf_file_data *) p1_p;
19776 const char *const p2 = (const char *) p2_p;
19777 return filename_cmp (p1->filename, p2) == 0;
19780 static hashval_t
19781 file_table_hash (const void *p_p)
19783 const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
19784 return htab_hash_string (p->filename);
19787 /* Lookup FILE_NAME (in the list of filenames that we know about here in
19788 dwarf2out.c) and return its "index". The index of each (known) filename is
19789 just a unique number which is associated with only that one filename. We
19790 need such numbers for the sake of generating labels (in the .debug_sfnames
19791 section) and references to those files numbers (in the .debug_srcinfo
19792 and.debug_macinfo sections). If the filename given as an argument is not
19793 found in our current list, add it to the list and assign it the next
19794 available unique index number. In order to speed up searches, we remember
19795 the index of the filename was looked up last. This handles the majority of
19796 all searches. */
19798 static struct dwarf_file_data *
19799 lookup_filename (const char *file_name)
19801 void ** slot;
19802 struct dwarf_file_data * created;
19804 /* Check to see if the file name that was searched on the previous
19805 call matches this file name. If so, return the index. */
19806 if (file_table_last_lookup
19807 && (file_name == file_table_last_lookup->filename
19808 || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
19809 return file_table_last_lookup;
19811 /* Didn't match the previous lookup, search the table. */
19812 slot = htab_find_slot_with_hash (file_table, file_name,
19813 htab_hash_string (file_name), INSERT);
19814 if (*slot)
19815 return (struct dwarf_file_data *) *slot;
19817 created = ggc_alloc_dwarf_file_data ();
19818 created->filename = file_name;
19819 created->emitted_number = 0;
19820 *slot = created;
19821 return created;
19824 /* If the assembler will construct the file table, then translate the compiler
19825 internal file table number into the assembler file table number, and emit
19826 a .file directive if we haven't already emitted one yet. The file table
19827 numbers are different because we prune debug info for unused variables and
19828 types, which may include filenames. */
19830 static int
19831 maybe_emit_file (struct dwarf_file_data * fd)
19833 if (! fd->emitted_number)
19835 if (last_emitted_file)
19836 fd->emitted_number = last_emitted_file->emitted_number + 1;
19837 else
19838 fd->emitted_number = 1;
19839 last_emitted_file = fd;
19841 if (DWARF2_ASM_LINE_DEBUG_INFO)
19843 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
19844 output_quoted_string (asm_out_file,
19845 remap_debug_filename (fd->filename));
19846 fputc ('\n', asm_out_file);
19850 return fd->emitted_number;
19853 /* Schedule generation of a DW_AT_const_value attribute to DIE.
19854 That generation should happen after function debug info has been
19855 generated. The value of the attribute is the constant value of ARG. */
19857 static void
19858 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
19860 die_arg_entry entry;
19862 if (!die || !arg)
19863 return;
19865 if (!tmpl_value_parm_die_table)
19866 tmpl_value_parm_die_table
19867 = VEC_alloc (die_arg_entry, gc, 32);
19869 entry.die = die;
19870 entry.arg = arg;
19871 VEC_safe_push (die_arg_entry, gc,
19872 tmpl_value_parm_die_table,
19873 entry);
19876 /* Return TRUE if T is an instance of generic type, FALSE
19877 otherwise. */
19879 static bool
19880 generic_type_p (tree t)
19882 if (t == NULL_TREE || !TYPE_P (t))
19883 return false;
19884 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
19887 /* Schedule the generation of the generic parameter dies for the
19888 instance of generic type T. The proper generation itself is later
19889 done by gen_scheduled_generic_parms_dies. */
19891 static void
19892 schedule_generic_params_dies_gen (tree t)
19894 if (!generic_type_p (t))
19895 return;
19897 if (generic_type_instances == NULL)
19898 generic_type_instances = VEC_alloc (tree, gc, 256);
19900 VEC_safe_push (tree, gc, generic_type_instances, t);
19903 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
19904 by append_entry_to_tmpl_value_parm_die_table. This function must
19905 be called after function DIEs have been generated. */
19907 static void
19908 gen_remaining_tmpl_value_param_die_attribute (void)
19910 if (tmpl_value_parm_die_table)
19912 unsigned i;
19913 die_arg_entry *e;
19915 FOR_EACH_VEC_ELT (die_arg_entry, tmpl_value_parm_die_table, i, e)
19916 tree_add_const_value_attribute (e->die, e->arg);
19920 /* Generate generic parameters DIEs for instances of generic types
19921 that have been previously scheduled by
19922 schedule_generic_params_dies_gen. This function must be called
19923 after all the types of the CU have been laid out. */
19925 static void
19926 gen_scheduled_generic_parms_dies (void)
19928 unsigned i;
19929 tree t;
19931 if (generic_type_instances == NULL)
19932 return;
19934 FOR_EACH_VEC_ELT (tree, generic_type_instances, i, t)
19935 gen_generic_params_dies (t);
19939 /* Replace DW_AT_name for the decl with name. */
19941 static void
19942 dwarf2out_set_name (tree decl, tree name)
19944 dw_die_ref die;
19945 dw_attr_ref attr;
19946 const char *dname;
19948 die = TYPE_SYMTAB_DIE (decl);
19949 if (!die)
19950 return;
19952 dname = dwarf2_name (name, 0);
19953 if (!dname)
19954 return;
19956 attr = get_AT (die, DW_AT_name);
19957 if (attr)
19959 struct indirect_string_node *node;
19961 node = find_AT_string (dname);
19962 /* replace the string. */
19963 attr->dw_attr_val.v.val_str = node;
19966 else
19967 add_name_attribute (die, dname);
19970 /* Called by the final INSN scan whenever we see a var location. We
19971 use it to drop labels in the right places, and throw the location in
19972 our lookup table. */
19974 static void
19975 dwarf2out_var_location (rtx loc_note)
19977 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
19978 struct var_loc_node *newloc;
19979 rtx next_real, next_note;
19980 static const char *last_label;
19981 static const char *last_postcall_label;
19982 static bool last_in_cold_section_p;
19983 static rtx expected_next_loc_note;
19984 tree decl;
19985 bool var_loc_p;
19987 if (!NOTE_P (loc_note))
19989 if (CALL_P (loc_note))
19991 call_site_count++;
19992 if (SIBLING_CALL_P (loc_note))
19993 tail_call_site_count++;
19995 return;
19998 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
19999 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
20000 return;
20002 /* Optimize processing a large consecutive sequence of location
20003 notes so we don't spend too much time in next_real_insn. If the
20004 next insn is another location note, remember the next_real_insn
20005 calculation for next time. */
20006 next_real = cached_next_real_insn;
20007 if (next_real)
20009 if (expected_next_loc_note != loc_note)
20010 next_real = NULL_RTX;
20013 next_note = NEXT_INSN (loc_note);
20014 if (! next_note
20015 || INSN_DELETED_P (next_note)
20016 || GET_CODE (next_note) != NOTE
20017 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
20018 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
20019 next_note = NULL_RTX;
20021 if (! next_real)
20022 next_real = next_real_insn (loc_note);
20024 if (next_note)
20026 expected_next_loc_note = next_note;
20027 cached_next_real_insn = next_real;
20029 else
20030 cached_next_real_insn = NULL_RTX;
20032 /* If there are no instructions which would be affected by this note,
20033 don't do anything. */
20034 if (var_loc_p
20035 && next_real == NULL_RTX
20036 && !NOTE_DURING_CALL_P (loc_note))
20037 return;
20039 if (next_real == NULL_RTX)
20040 next_real = get_last_insn ();
20042 /* If there were any real insns between note we processed last time
20043 and this note (or if it is the first note), clear
20044 last_{,postcall_}label so that they are not reused this time. */
20045 if (last_var_location_insn == NULL_RTX
20046 || last_var_location_insn != next_real
20047 || last_in_cold_section_p != in_cold_section_p)
20049 last_label = NULL;
20050 last_postcall_label = NULL;
20053 if (var_loc_p)
20055 decl = NOTE_VAR_LOCATION_DECL (loc_note);
20056 newloc = add_var_loc_to_decl (decl, loc_note,
20057 NOTE_DURING_CALL_P (loc_note)
20058 ? last_postcall_label : last_label);
20059 if (newloc == NULL)
20060 return;
20062 else
20064 decl = NULL_TREE;
20065 newloc = NULL;
20068 /* If there were no real insns between note we processed last time
20069 and this note, use the label we emitted last time. Otherwise
20070 create a new label and emit it. */
20071 if (last_label == NULL)
20073 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
20074 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
20075 loclabel_num++;
20076 last_label = ggc_strdup (loclabel);
20079 if (!var_loc_p)
20081 struct call_arg_loc_node *ca_loc
20082 = ggc_alloc_cleared_call_arg_loc_node ();
20083 rtx prev = prev_real_insn (loc_note), x;
20084 ca_loc->call_arg_loc_note = loc_note;
20085 ca_loc->next = NULL;
20086 ca_loc->label = last_label;
20087 gcc_assert (prev
20088 && (CALL_P (prev)
20089 || (NONJUMP_INSN_P (prev)
20090 && GET_CODE (PATTERN (prev)) == SEQUENCE
20091 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
20092 if (!CALL_P (prev))
20093 prev = XVECEXP (PATTERN (prev), 0, 0);
20094 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
20095 x = PATTERN (prev);
20096 if (GET_CODE (x) == PARALLEL)
20097 x = XVECEXP (x, 0, 0);
20098 if (GET_CODE (x) == SET)
20099 x = SET_SRC (x);
20100 if (GET_CODE (x) == CALL && MEM_P (XEXP (x, 0)))
20102 x = XEXP (XEXP (x, 0), 0);
20103 if (GET_CODE (x) == SYMBOL_REF
20104 && SYMBOL_REF_DECL (x)
20105 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
20106 ca_loc->symbol_ref = x;
20108 ca_loc->block = insn_scope (prev);
20109 if (call_arg_locations)
20110 call_arg_loc_last->next = ca_loc;
20111 else
20112 call_arg_locations = ca_loc;
20113 call_arg_loc_last = ca_loc;
20115 else if (!NOTE_DURING_CALL_P (loc_note))
20116 newloc->label = last_label;
20117 else
20119 if (!last_postcall_label)
20121 sprintf (loclabel, "%s-1", last_label);
20122 last_postcall_label = ggc_strdup (loclabel);
20124 newloc->label = last_postcall_label;
20127 last_var_location_insn = next_real;
20128 last_in_cold_section_p = in_cold_section_p;
20131 /* Note in one location list that text section has changed. */
20133 static int
20134 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
20136 var_loc_list *list = (var_loc_list *) *slot;
20137 if (list->first)
20138 list->last_before_switch
20139 = list->last->next ? list->last->next : list->last;
20140 return 1;
20143 /* Note in all location lists that text section has changed. */
20145 static void
20146 var_location_switch_text_section (void)
20148 if (decl_loc_table == NULL)
20149 return;
20151 htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
20154 /* Create a new line number table. */
20156 static dw_line_info_table *
20157 new_line_info_table (void)
20159 dw_line_info_table *table;
20161 table = ggc_alloc_cleared_dw_line_info_table_struct ();
20162 table->file_num = 1;
20163 table->line_num = 1;
20164 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
20166 return table;
20169 /* Lookup the "current" table into which we emit line info, so
20170 that we don't have to do it for every source line. */
20172 static void
20173 set_cur_line_info_table (section *sec)
20175 dw_line_info_table *table;
20177 if (sec == text_section)
20178 table = text_section_line_info;
20179 else if (sec == cold_text_section)
20181 table = cold_text_section_line_info;
20182 if (!table)
20184 cold_text_section_line_info = table = new_line_info_table ();
20185 table->end_label = cold_end_label;
20188 else
20190 const char *end_label;
20192 if (flag_reorder_blocks_and_partition)
20194 if (in_cold_section_p)
20195 end_label = crtl->subsections.cold_section_end_label;
20196 else
20197 end_label = crtl->subsections.hot_section_end_label;
20199 else
20201 char label[MAX_ARTIFICIAL_LABEL_BYTES];
20202 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
20203 current_function_funcdef_no);
20204 end_label = ggc_strdup (label);
20207 table = new_line_info_table ();
20208 table->end_label = end_label;
20210 VEC_safe_push (dw_line_info_table_p, gc, separate_line_info, table);
20213 if (DWARF2_ASM_LINE_DEBUG_INFO)
20214 table->is_stmt = (cur_line_info_table
20215 ? cur_line_info_table->is_stmt
20216 : DWARF_LINE_DEFAULT_IS_STMT_START);
20217 cur_line_info_table = table;
20221 /* We need to reset the locations at the beginning of each
20222 function. We can't do this in the end_function hook, because the
20223 declarations that use the locations won't have been output when
20224 that hook is called. Also compute have_multiple_function_sections here. */
20226 static void
20227 dwarf2out_begin_function (tree fun)
20229 section *sec = function_section (fun);
20231 if (sec != text_section)
20232 have_multiple_function_sections = true;
20234 if (flag_reorder_blocks_and_partition && !cold_text_section)
20236 gcc_assert (current_function_decl == fun);
20237 cold_text_section = unlikely_text_section ();
20238 switch_to_section (cold_text_section);
20239 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
20240 switch_to_section (sec);
20243 dwarf2out_note_section_used ();
20244 call_site_count = 0;
20245 tail_call_site_count = 0;
20247 set_cur_line_info_table (sec);
20250 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
20252 static void
20253 push_dw_line_info_entry (dw_line_info_table *table,
20254 enum dw_line_info_opcode opcode, unsigned int val)
20256 dw_line_info_entry e;
20257 e.opcode = opcode;
20258 e.val = val;
20259 VEC_safe_push (dw_line_info_entry, gc, table->entries, e);
20262 /* Output a label to mark the beginning of a source code line entry
20263 and record information relating to this source line, in
20264 'line_info_table' for later output of the .debug_line section. */
20265 /* ??? The discriminator parameter ought to be unsigned. */
20267 static void
20268 dwarf2out_source_line (unsigned int line, const char *filename,
20269 int discriminator, bool is_stmt)
20271 unsigned int file_num;
20272 dw_line_info_table *table;
20274 if (debug_info_level < DINFO_LEVEL_NORMAL || line == 0)
20275 return;
20277 /* The discriminator column was added in dwarf4. Simplify the below
20278 by simply removing it if we're not supposed to output it. */
20279 if (dwarf_version < 4 && dwarf_strict)
20280 discriminator = 0;
20282 table = cur_line_info_table;
20283 file_num = maybe_emit_file (lookup_filename (filename));
20285 /* ??? TODO: Elide duplicate line number entries. Traditionally,
20286 the debugger has used the second (possibly duplicate) line number
20287 at the beginning of the function to mark the end of the prologue.
20288 We could eliminate any other duplicates within the function. For
20289 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
20290 that second line number entry. */
20291 /* Recall that this end-of-prologue indication is *not* the same thing
20292 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
20293 to which the hook corresponds, follows the last insn that was
20294 emitted by gen_prologue. What we need is to precede the first insn
20295 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
20296 insn that corresponds to something the user wrote. These may be
20297 very different locations once scheduling is enabled. */
20299 if (0 && file_num == table->file_num
20300 && line == table->line_num
20301 && discriminator == table->discrim_num
20302 && is_stmt == table->is_stmt)
20303 return;
20305 switch_to_section (current_function_section ());
20307 /* If requested, emit something human-readable. */
20308 if (flag_debug_asm)
20309 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
20311 if (DWARF2_ASM_LINE_DEBUG_INFO)
20313 /* Emit the .loc directive understood by GNU as. */
20314 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
20315 file_num, line, is_stmt, discriminator */
20316 fputs ("\t.loc ", asm_out_file);
20317 fprint_ul (asm_out_file, file_num);
20318 putc (' ', asm_out_file);
20319 fprint_ul (asm_out_file, line);
20320 putc (' ', asm_out_file);
20321 putc ('0', asm_out_file);
20323 if (is_stmt != table->is_stmt)
20325 fputs (" is_stmt ", asm_out_file);
20326 putc (is_stmt ? '1' : '0', asm_out_file);
20328 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
20330 gcc_assert (discriminator > 0);
20331 fputs (" discriminator ", asm_out_file);
20332 fprint_ul (asm_out_file, (unsigned long) discriminator);
20334 putc ('\n', asm_out_file);
20336 else
20338 unsigned int label_num = ++line_info_label_num;
20340 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
20342 push_dw_line_info_entry (table, LI_set_address, label_num);
20343 if (file_num != table->file_num)
20344 push_dw_line_info_entry (table, LI_set_file, file_num);
20345 if (discriminator != table->discrim_num)
20346 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
20347 if (is_stmt != table->is_stmt)
20348 push_dw_line_info_entry (table, LI_negate_stmt, 0);
20349 push_dw_line_info_entry (table, LI_set_line, line);
20352 table->file_num = file_num;
20353 table->line_num = line;
20354 table->discrim_num = discriminator;
20355 table->is_stmt = is_stmt;
20356 table->in_use = true;
20359 /* Record the beginning of a new source file. */
20361 static void
20362 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
20364 if (flag_eliminate_dwarf2_dups)
20366 /* Record the beginning of the file for break_out_includes. */
20367 dw_die_ref bincl_die;
20369 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
20370 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
20373 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20375 macinfo_entry e;
20376 e.code = DW_MACINFO_start_file;
20377 e.lineno = lineno;
20378 e.info = ggc_strdup (filename);
20379 VEC_safe_push (macinfo_entry, gc, macinfo_table, e);
20383 /* Record the end of a source file. */
20385 static void
20386 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
20388 if (flag_eliminate_dwarf2_dups)
20389 /* Record the end of the file for break_out_includes. */
20390 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
20392 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20394 macinfo_entry e;
20395 e.code = DW_MACINFO_end_file;
20396 e.lineno = lineno;
20397 e.info = NULL;
20398 VEC_safe_push (macinfo_entry, gc, macinfo_table, e);
20402 /* Called from debug_define in toplev.c. The `buffer' parameter contains
20403 the tail part of the directive line, i.e. the part which is past the
20404 initial whitespace, #, whitespace, directive-name, whitespace part. */
20406 static void
20407 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
20408 const char *buffer ATTRIBUTE_UNUSED)
20410 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20412 macinfo_entry e;
20413 /* Insert a dummy first entry to be able to optimize the whole
20414 predefined macro block using DW_MACRO_GNU_transparent_include. */
20415 if (VEC_empty (macinfo_entry, macinfo_table) && lineno <= 1)
20417 e.code = 0;
20418 e.lineno = 0;
20419 e.info = NULL;
20420 VEC_safe_push (macinfo_entry, gc, macinfo_table, e);
20422 e.code = DW_MACINFO_define;
20423 e.lineno = lineno;
20424 e.info = ggc_strdup (buffer);
20425 VEC_safe_push (macinfo_entry, gc, macinfo_table, e);
20429 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
20430 the tail part of the directive line, i.e. the part which is past the
20431 initial whitespace, #, whitespace, directive-name, whitespace part. */
20433 static void
20434 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
20435 const char *buffer ATTRIBUTE_UNUSED)
20437 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20439 macinfo_entry e;
20440 /* Insert a dummy first entry to be able to optimize the whole
20441 predefined macro block using DW_MACRO_GNU_transparent_include. */
20442 if (VEC_empty (macinfo_entry, macinfo_table) && lineno <= 1)
20444 e.code = 0;
20445 e.lineno = 0;
20446 e.info = NULL;
20447 VEC_safe_push (macinfo_entry, gc, macinfo_table, e);
20449 e.code = DW_MACINFO_undef;
20450 e.lineno = lineno;
20451 e.info = ggc_strdup (buffer);
20452 VEC_safe_push (macinfo_entry, gc, macinfo_table, e);
20456 /* Routines to manipulate hash table of CUs. */
20458 static hashval_t
20459 htab_macinfo_hash (const void *of)
20461 const macinfo_entry *const entry =
20462 (const macinfo_entry *) of;
20464 return htab_hash_string (entry->info);
20467 static int
20468 htab_macinfo_eq (const void *of1, const void *of2)
20470 const macinfo_entry *const entry1 = (const macinfo_entry *) of1;
20471 const macinfo_entry *const entry2 = (const macinfo_entry *) of2;
20473 return !strcmp (entry1->info, entry2->info);
20476 /* Output a single .debug_macinfo entry. */
20478 static void
20479 output_macinfo_op (macinfo_entry *ref)
20481 int file_num;
20482 size_t len;
20483 struct indirect_string_node *node;
20484 char label[MAX_ARTIFICIAL_LABEL_BYTES];
20485 struct dwarf_file_data *fd;
20487 switch (ref->code)
20489 case DW_MACINFO_start_file:
20490 fd = lookup_filename (ref->info);
20491 file_num = maybe_emit_file (fd);
20492 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
20493 dw2_asm_output_data_uleb128 (ref->lineno,
20494 "Included from line number %lu",
20495 (unsigned long) ref->lineno);
20496 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
20497 break;
20498 case DW_MACINFO_end_file:
20499 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
20500 break;
20501 case DW_MACINFO_define:
20502 case DW_MACINFO_undef:
20503 len = strlen (ref->info) + 1;
20504 if (!dwarf_strict
20505 && len > DWARF_OFFSET_SIZE
20506 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
20507 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
20509 ref->code = ref->code == DW_MACINFO_define
20510 ? DW_MACRO_GNU_define_indirect
20511 : DW_MACRO_GNU_undef_indirect;
20512 output_macinfo_op (ref);
20513 return;
20515 dw2_asm_output_data (1, ref->code,
20516 ref->code == DW_MACINFO_define
20517 ? "Define macro" : "Undefine macro");
20518 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
20519 (unsigned long) ref->lineno);
20520 dw2_asm_output_nstring (ref->info, -1, "The macro");
20521 break;
20522 case DW_MACRO_GNU_define_indirect:
20523 case DW_MACRO_GNU_undef_indirect:
20524 node = find_AT_string (ref->info);
20525 if (node->form != DW_FORM_strp)
20527 char label[32];
20528 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
20529 ++dw2_string_counter;
20530 node->label = xstrdup (label);
20531 node->form = DW_FORM_strp;
20533 dw2_asm_output_data (1, ref->code,
20534 ref->code == DW_MACRO_GNU_define_indirect
20535 ? "Define macro indirect"
20536 : "Undefine macro indirect");
20537 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
20538 (unsigned long) ref->lineno);
20539 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
20540 debug_str_section, "The macro: \"%s\"",
20541 ref->info);
20542 break;
20543 case DW_MACRO_GNU_transparent_include:
20544 dw2_asm_output_data (1, ref->code, "Transparent include");
20545 ASM_GENERATE_INTERNAL_LABEL (label,
20546 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
20547 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
20548 break;
20549 default:
20550 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
20551 ASM_COMMENT_START, (unsigned long) ref->code);
20552 break;
20556 /* Attempt to make a sequence of define/undef macinfo ops shareable with
20557 other compilation unit .debug_macinfo sections. IDX is the first
20558 index of a define/undef, return the number of ops that should be
20559 emitted in a comdat .debug_macinfo section and emit
20560 a DW_MACRO_GNU_transparent_include entry referencing it.
20561 If the define/undef entry should be emitted normally, return 0. */
20563 static unsigned
20564 optimize_macinfo_range (unsigned int idx, VEC (macinfo_entry, gc) *files,
20565 htab_t *macinfo_htab)
20567 macinfo_entry *first, *second, *cur, *inc;
20568 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
20569 unsigned char checksum[16];
20570 struct md5_ctx ctx;
20571 char *grp_name, *tail;
20572 const char *base;
20573 unsigned int i, count, encoded_filename_len, linebuf_len;
20574 void **slot;
20576 first = &VEC_index (macinfo_entry, macinfo_table, idx);
20577 second = &VEC_index (macinfo_entry, macinfo_table, idx + 1);
20579 /* Optimize only if there are at least two consecutive define/undef ops,
20580 and either all of them are before first DW_MACINFO_start_file
20581 with lineno {0,1} (i.e. predefined macro block), or all of them are
20582 in some included header file. */
20583 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
20584 return 0;
20585 if (VEC_empty (macinfo_entry, files))
20587 if (first->lineno > 1 || second->lineno > 1)
20588 return 0;
20590 else if (first->lineno == 0)
20591 return 0;
20593 /* Find the last define/undef entry that can be grouped together
20594 with first and at the same time compute md5 checksum of their
20595 codes, linenumbers and strings. */
20596 md5_init_ctx (&ctx);
20597 for (i = idx; VEC_iterate (macinfo_entry, macinfo_table, i, cur); i++)
20598 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
20599 break;
20600 else if (VEC_empty (macinfo_entry, files) && cur->lineno > 1)
20601 break;
20602 else
20604 unsigned char code = cur->code;
20605 md5_process_bytes (&code, 1, &ctx);
20606 checksum_uleb128 (cur->lineno, &ctx);
20607 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
20609 md5_finish_ctx (&ctx, checksum);
20610 count = i - idx;
20612 /* From the containing include filename (if any) pick up just
20613 usable characters from its basename. */
20614 if (VEC_empty (macinfo_entry, files))
20615 base = "";
20616 else
20617 base = lbasename (VEC_last (macinfo_entry, files).info);
20618 for (encoded_filename_len = 0, i = 0; base[i]; i++)
20619 if (ISIDNUM (base[i]) || base[i] == '.')
20620 encoded_filename_len++;
20621 /* Count . at the end. */
20622 if (encoded_filename_len)
20623 encoded_filename_len++;
20625 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
20626 linebuf_len = strlen (linebuf);
20628 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
20629 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
20630 + 16 * 2 + 1);
20631 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
20632 tail = grp_name + 4;
20633 if (encoded_filename_len)
20635 for (i = 0; base[i]; i++)
20636 if (ISIDNUM (base[i]) || base[i] == '.')
20637 *tail++ = base[i];
20638 *tail++ = '.';
20640 memcpy (tail, linebuf, linebuf_len);
20641 tail += linebuf_len;
20642 *tail++ = '.';
20643 for (i = 0; i < 16; i++)
20644 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
20646 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
20647 in the empty vector entry before the first define/undef. */
20648 inc = &VEC_index (macinfo_entry, macinfo_table, idx - 1);
20649 inc->code = DW_MACRO_GNU_transparent_include;
20650 inc->lineno = 0;
20651 inc->info = ggc_strdup (grp_name);
20652 if (*macinfo_htab == NULL)
20653 *macinfo_htab = htab_create (10, htab_macinfo_hash, htab_macinfo_eq, NULL);
20654 /* Avoid emitting duplicates. */
20655 slot = htab_find_slot (*macinfo_htab, inc, INSERT);
20656 if (*slot != NULL)
20658 inc->code = 0;
20659 inc->info = NULL;
20660 /* If such an entry has been used before, just emit
20661 a DW_MACRO_GNU_transparent_include op. */
20662 inc = (macinfo_entry *) *slot;
20663 output_macinfo_op (inc);
20664 /* And clear all macinfo_entry in the range to avoid emitting them
20665 in the second pass. */
20666 for (i = idx;
20667 VEC_iterate (macinfo_entry, macinfo_table, i, cur)
20668 && i < idx + count;
20669 i++)
20671 cur->code = 0;
20672 cur->info = NULL;
20675 else
20677 *slot = inc;
20678 inc->lineno = htab_elements (*macinfo_htab);
20679 output_macinfo_op (inc);
20681 return count;
20684 /* Output macinfo section(s). */
20686 static void
20687 output_macinfo (void)
20689 unsigned i;
20690 unsigned long length = VEC_length (macinfo_entry, macinfo_table);
20691 macinfo_entry *ref;
20692 VEC (macinfo_entry, gc) *files = NULL;
20693 htab_t macinfo_htab = NULL;
20695 if (! length)
20696 return;
20698 /* output_macinfo* uses these interchangeably. */
20699 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
20700 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
20701 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
20702 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
20704 /* For .debug_macro emit the section header. */
20705 if (!dwarf_strict)
20707 dw2_asm_output_data (2, 4, "DWARF macro version number");
20708 if (DWARF_OFFSET_SIZE == 8)
20709 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
20710 else
20711 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
20712 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_line_section_label,
20713 debug_line_section, NULL);
20716 /* In the first loop, it emits the primary .debug_macinfo section
20717 and after each emitted op the macinfo_entry is cleared.
20718 If a longer range of define/undef ops can be optimized using
20719 DW_MACRO_GNU_transparent_include, the
20720 DW_MACRO_GNU_transparent_include op is emitted and kept in
20721 the vector before the first define/undef in the range and the
20722 whole range of define/undef ops is not emitted and kept. */
20723 for (i = 0; VEC_iterate (macinfo_entry, macinfo_table, i, ref); i++)
20725 switch (ref->code)
20727 case DW_MACINFO_start_file:
20728 VEC_safe_push (macinfo_entry, gc, files, *ref);
20729 break;
20730 case DW_MACINFO_end_file:
20731 if (!VEC_empty (macinfo_entry, files))
20732 VEC_pop (macinfo_entry, files);
20733 break;
20734 case DW_MACINFO_define:
20735 case DW_MACINFO_undef:
20736 if (!dwarf_strict
20737 && HAVE_COMDAT_GROUP
20738 && VEC_length (macinfo_entry, files) != 1
20739 && i > 0
20740 && i + 1 < length
20741 && VEC_index (macinfo_entry, macinfo_table, i - 1).code == 0)
20743 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
20744 if (count)
20746 i += count - 1;
20747 continue;
20750 break;
20751 case 0:
20752 /* A dummy entry may be inserted at the beginning to be able
20753 to optimize the whole block of predefined macros. */
20754 if (i == 0)
20755 continue;
20756 default:
20757 break;
20759 output_macinfo_op (ref);
20760 ref->info = NULL;
20761 ref->code = 0;
20764 if (macinfo_htab == NULL)
20765 return;
20767 htab_delete (macinfo_htab);
20769 /* If any DW_MACRO_GNU_transparent_include were used, on those
20770 DW_MACRO_GNU_transparent_include entries terminate the
20771 current chain and switch to a new comdat .debug_macinfo
20772 section and emit the define/undef entries within it. */
20773 for (i = 0; VEC_iterate (macinfo_entry, macinfo_table, i, ref); i++)
20774 switch (ref->code)
20776 case 0:
20777 continue;
20778 case DW_MACRO_GNU_transparent_include:
20780 char label[MAX_ARTIFICIAL_LABEL_BYTES];
20781 tree comdat_key = get_identifier (ref->info);
20782 /* Terminate the previous .debug_macinfo section. */
20783 dw2_asm_output_data (1, 0, "End compilation unit");
20784 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
20785 SECTION_DEBUG
20786 | SECTION_LINKONCE,
20787 comdat_key);
20788 ASM_GENERATE_INTERNAL_LABEL (label,
20789 DEBUG_MACRO_SECTION_LABEL,
20790 ref->lineno);
20791 ASM_OUTPUT_LABEL (asm_out_file, label);
20792 ref->code = 0;
20793 ref->info = NULL;
20794 dw2_asm_output_data (2, 4, "DWARF macro version number");
20795 if (DWARF_OFFSET_SIZE == 8)
20796 dw2_asm_output_data (1, 1, "Flags: 64-bit");
20797 else
20798 dw2_asm_output_data (1, 0, "Flags: 32-bit");
20800 break;
20801 case DW_MACINFO_define:
20802 case DW_MACINFO_undef:
20803 output_macinfo_op (ref);
20804 ref->code = 0;
20805 ref->info = NULL;
20806 break;
20807 default:
20808 gcc_unreachable ();
20812 /* Set up for Dwarf output at the start of compilation. */
20814 static void
20815 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
20817 /* Allocate the file_table. */
20818 file_table = htab_create_ggc (50, file_table_hash,
20819 file_table_eq, NULL);
20821 /* Allocate the decl_die_table. */
20822 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
20823 decl_die_table_eq, NULL);
20825 /* Allocate the decl_loc_table. */
20826 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
20827 decl_loc_table_eq, NULL);
20829 /* Allocate the cached_dw_loc_list_table. */
20830 cached_dw_loc_list_table
20831 = htab_create_ggc (10, cached_dw_loc_list_table_hash,
20832 cached_dw_loc_list_table_eq, NULL);
20834 /* Allocate the initial hunk of the decl_scope_table. */
20835 decl_scope_table = VEC_alloc (tree, gc, 256);
20837 /* Allocate the initial hunk of the abbrev_die_table. */
20838 abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
20839 (ABBREV_DIE_TABLE_INCREMENT);
20840 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
20841 /* Zero-th entry is allocated, but unused. */
20842 abbrev_die_table_in_use = 1;
20844 /* Allocate the pubtypes and pubnames vectors. */
20845 pubname_table = VEC_alloc (pubname_entry, gc, 32);
20846 pubtype_table = VEC_alloc (pubname_entry, gc, 32);
20848 incomplete_types = VEC_alloc (tree, gc, 64);
20850 used_rtx_array = VEC_alloc (rtx, gc, 32);
20852 debug_info_section = get_section (DEBUG_INFO_SECTION,
20853 SECTION_DEBUG, NULL);
20854 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
20855 SECTION_DEBUG, NULL);
20856 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
20857 SECTION_DEBUG, NULL);
20858 debug_macinfo_section = get_section (dwarf_strict
20859 ? DEBUG_MACINFO_SECTION
20860 : DEBUG_MACRO_SECTION,
20861 SECTION_DEBUG, NULL);
20862 debug_line_section = get_section (DEBUG_LINE_SECTION,
20863 SECTION_DEBUG, NULL);
20864 debug_loc_section = get_section (DEBUG_LOC_SECTION,
20865 SECTION_DEBUG, NULL);
20866 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
20867 SECTION_DEBUG, NULL);
20868 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
20869 SECTION_DEBUG, NULL);
20870 debug_str_section = get_section (DEBUG_STR_SECTION,
20871 DEBUG_STR_SECTION_FLAGS, NULL);
20872 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
20873 SECTION_DEBUG, NULL);
20874 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
20875 SECTION_DEBUG, NULL);
20877 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
20878 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
20879 DEBUG_ABBREV_SECTION_LABEL, 0);
20880 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
20881 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
20882 COLD_TEXT_SECTION_LABEL, 0);
20883 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
20885 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
20886 DEBUG_INFO_SECTION_LABEL, 0);
20887 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
20888 DEBUG_LINE_SECTION_LABEL, 0);
20889 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
20890 DEBUG_RANGES_SECTION_LABEL, 0);
20891 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
20892 dwarf_strict
20893 ? DEBUG_MACINFO_SECTION_LABEL
20894 : DEBUG_MACRO_SECTION_LABEL, 0);
20896 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20897 macinfo_table = VEC_alloc (macinfo_entry, gc, 64);
20899 switch_to_section (text_section);
20900 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
20902 /* Make sure the line number table for .text always exists. */
20903 text_section_line_info = new_line_info_table ();
20904 text_section_line_info->end_label = text_end_label;
20907 /* Called before compile () starts outputtting functions, variables
20908 and toplevel asms into assembly. */
20910 static void
20911 dwarf2out_assembly_start (void)
20913 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
20914 && dwarf2out_do_cfi_asm ()
20915 && (!(flag_unwind_tables || flag_exceptions)
20916 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
20917 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
20920 /* A helper function for dwarf2out_finish called through
20921 htab_traverse. Emit one queued .debug_str string. */
20923 static int
20924 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
20926 struct indirect_string_node *node = (struct indirect_string_node *) *h;
20928 if (node->form == DW_FORM_strp)
20930 switch_to_section (debug_str_section);
20931 ASM_OUTPUT_LABEL (asm_out_file, node->label);
20932 assemble_string (node->str, strlen (node->str) + 1);
20935 return 1;
20938 #if ENABLE_ASSERT_CHECKING
20939 /* Verify that all marks are clear. */
20941 static void
20942 verify_marks_clear (dw_die_ref die)
20944 dw_die_ref c;
20946 gcc_assert (! die->die_mark);
20947 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
20949 #endif /* ENABLE_ASSERT_CHECKING */
20951 /* Clear the marks for a die and its children.
20952 Be cool if the mark isn't set. */
20954 static void
20955 prune_unmark_dies (dw_die_ref die)
20957 dw_die_ref c;
20959 if (die->die_mark)
20960 die->die_mark = 0;
20961 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
20964 /* Given DIE that we're marking as used, find any other dies
20965 it references as attributes and mark them as used. */
20967 static void
20968 prune_unused_types_walk_attribs (dw_die_ref die)
20970 dw_attr_ref a;
20971 unsigned ix;
20973 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
20975 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
20977 /* A reference to another DIE.
20978 Make sure that it will get emitted.
20979 If it was broken out into a comdat group, don't follow it. */
20980 if (! AT_ref (a)->comdat_type_p
20981 || a->dw_attr == DW_AT_specification)
20982 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
20984 /* Set the string's refcount to 0 so that prune_unused_types_mark
20985 accounts properly for it. */
20986 if (AT_class (a) == dw_val_class_str)
20987 a->dw_attr_val.v.val_str->refcount = 0;
20991 /* Mark the generic parameters and arguments children DIEs of DIE. */
20993 static void
20994 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
20996 dw_die_ref c;
20998 if (die == NULL || die->die_child == NULL)
20999 return;
21000 c = die->die_child;
21003 switch (c->die_tag)
21005 case DW_TAG_template_type_param:
21006 case DW_TAG_template_value_param:
21007 case DW_TAG_GNU_template_template_param:
21008 case DW_TAG_GNU_template_parameter_pack:
21009 prune_unused_types_mark (c, 1);
21010 break;
21011 default:
21012 break;
21014 c = c->die_sib;
21015 } while (c && c != die->die_child);
21018 /* Mark DIE as being used. If DOKIDS is true, then walk down
21019 to DIE's children. */
21021 static void
21022 prune_unused_types_mark (dw_die_ref die, int dokids)
21024 dw_die_ref c;
21026 if (die->die_mark == 0)
21028 /* We haven't done this node yet. Mark it as used. */
21029 die->die_mark = 1;
21030 /* If this is the DIE of a generic type instantiation,
21031 mark the children DIEs that describe its generic parms and
21032 args. */
21033 prune_unused_types_mark_generic_parms_dies (die);
21035 /* We also have to mark its parents as used.
21036 (But we don't want to mark our parents' kids due to this.) */
21037 if (die->die_parent)
21038 prune_unused_types_mark (die->die_parent, 0);
21040 /* Mark any referenced nodes. */
21041 prune_unused_types_walk_attribs (die);
21043 /* If this node is a specification,
21044 also mark the definition, if it exists. */
21045 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
21046 prune_unused_types_mark (die->die_definition, 1);
21049 if (dokids && die->die_mark != 2)
21051 /* We need to walk the children, but haven't done so yet.
21052 Remember that we've walked the kids. */
21053 die->die_mark = 2;
21055 /* If this is an array type, we need to make sure our
21056 kids get marked, even if they're types. If we're
21057 breaking out types into comdat sections, do this
21058 for all type definitions. */
21059 if (die->die_tag == DW_TAG_array_type
21060 || (use_debug_types
21061 && is_type_die (die) && ! is_declaration_die (die)))
21062 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
21063 else
21064 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
21068 /* For local classes, look if any static member functions were emitted
21069 and if so, mark them. */
21071 static void
21072 prune_unused_types_walk_local_classes (dw_die_ref die)
21074 dw_die_ref c;
21076 if (die->die_mark == 2)
21077 return;
21079 switch (die->die_tag)
21081 case DW_TAG_structure_type:
21082 case DW_TAG_union_type:
21083 case DW_TAG_class_type:
21084 break;
21086 case DW_TAG_subprogram:
21087 if (!get_AT_flag (die, DW_AT_declaration)
21088 || die->die_definition != NULL)
21089 prune_unused_types_mark (die, 1);
21090 return;
21092 default:
21093 return;
21096 /* Mark children. */
21097 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
21100 /* Walk the tree DIE and mark types that we actually use. */
21102 static void
21103 prune_unused_types_walk (dw_die_ref die)
21105 dw_die_ref c;
21107 /* Don't do anything if this node is already marked and
21108 children have been marked as well. */
21109 if (die->die_mark == 2)
21110 return;
21112 switch (die->die_tag)
21114 case DW_TAG_structure_type:
21115 case DW_TAG_union_type:
21116 case DW_TAG_class_type:
21117 if (die->die_perennial_p)
21118 break;
21120 for (c = die->die_parent; c; c = c->die_parent)
21121 if (c->die_tag == DW_TAG_subprogram)
21122 break;
21124 /* Finding used static member functions inside of classes
21125 is needed just for local classes, because for other classes
21126 static member function DIEs with DW_AT_specification
21127 are emitted outside of the DW_TAG_*_type. If we ever change
21128 it, we'd need to call this even for non-local classes. */
21129 if (c)
21130 prune_unused_types_walk_local_classes (die);
21132 /* It's a type node --- don't mark it. */
21133 return;
21135 case DW_TAG_const_type:
21136 case DW_TAG_packed_type:
21137 case DW_TAG_pointer_type:
21138 case DW_TAG_reference_type:
21139 case DW_TAG_rvalue_reference_type:
21140 case DW_TAG_volatile_type:
21141 case DW_TAG_typedef:
21142 case DW_TAG_array_type:
21143 case DW_TAG_interface_type:
21144 case DW_TAG_friend:
21145 case DW_TAG_variant_part:
21146 case DW_TAG_enumeration_type:
21147 case DW_TAG_subroutine_type:
21148 case DW_TAG_string_type:
21149 case DW_TAG_set_type:
21150 case DW_TAG_subrange_type:
21151 case DW_TAG_ptr_to_member_type:
21152 case DW_TAG_file_type:
21153 if (die->die_perennial_p)
21154 break;
21156 /* It's a type node --- don't mark it. */
21157 return;
21159 default:
21160 /* Mark everything else. */
21161 break;
21164 if (die->die_mark == 0)
21166 die->die_mark = 1;
21168 /* Now, mark any dies referenced from here. */
21169 prune_unused_types_walk_attribs (die);
21172 die->die_mark = 2;
21174 /* Mark children. */
21175 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
21178 /* Increment the string counts on strings referred to from DIE's
21179 attributes. */
21181 static void
21182 prune_unused_types_update_strings (dw_die_ref die)
21184 dw_attr_ref a;
21185 unsigned ix;
21187 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
21188 if (AT_class (a) == dw_val_class_str)
21190 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
21191 s->refcount++;
21192 /* Avoid unnecessarily putting strings that are used less than
21193 twice in the hash table. */
21194 if (s->refcount
21195 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
21197 void ** slot;
21198 slot = htab_find_slot_with_hash (debug_str_hash, s->str,
21199 htab_hash_string (s->str),
21200 INSERT);
21201 gcc_assert (*slot == NULL);
21202 *slot = s;
21207 /* Remove from the tree DIE any dies that aren't marked. */
21209 static void
21210 prune_unused_types_prune (dw_die_ref die)
21212 dw_die_ref c;
21214 gcc_assert (die->die_mark);
21215 prune_unused_types_update_strings (die);
21217 if (! die->die_child)
21218 return;
21220 c = die->die_child;
21221 do {
21222 dw_die_ref prev = c;
21223 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
21224 if (c == die->die_child)
21226 /* No marked children between 'prev' and the end of the list. */
21227 if (prev == c)
21228 /* No marked children at all. */
21229 die->die_child = NULL;
21230 else
21232 prev->die_sib = c->die_sib;
21233 die->die_child = prev;
21235 return;
21238 if (c != prev->die_sib)
21239 prev->die_sib = c;
21240 prune_unused_types_prune (c);
21241 } while (c != die->die_child);
21244 /* Remove dies representing declarations that we never use. */
21246 static void
21247 prune_unused_types (void)
21249 unsigned int i;
21250 limbo_die_node *node;
21251 comdat_type_node *ctnode;
21252 pubname_ref pub;
21253 dw_die_ref base_type;
21255 #if ENABLE_ASSERT_CHECKING
21256 /* All the marks should already be clear. */
21257 verify_marks_clear (comp_unit_die ());
21258 for (node = limbo_die_list; node; node = node->next)
21259 verify_marks_clear (node->die);
21260 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21261 verify_marks_clear (ctnode->root_die);
21262 #endif /* ENABLE_ASSERT_CHECKING */
21264 /* Mark types that are used in global variables. */
21265 premark_types_used_by_global_vars ();
21267 /* Set the mark on nodes that are actually used. */
21268 prune_unused_types_walk (comp_unit_die ());
21269 for (node = limbo_die_list; node; node = node->next)
21270 prune_unused_types_walk (node->die);
21271 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21273 prune_unused_types_walk (ctnode->root_die);
21274 prune_unused_types_mark (ctnode->type_die, 1);
21277 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
21278 are unusual in that they are pubnames that are the children of pubtypes.
21279 They should only be marked via their parent DW_TAG_enumeration_type die,
21280 not as roots in themselves. */
21281 FOR_EACH_VEC_ELT (pubname_entry, pubname_table, i, pub)
21282 if (pub->die->die_tag != DW_TAG_enumerator)
21283 prune_unused_types_mark (pub->die, 1);
21284 for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
21285 prune_unused_types_mark (base_type, 1);
21287 if (debug_str_hash)
21288 htab_empty (debug_str_hash);
21289 prune_unused_types_prune (comp_unit_die ());
21290 for (node = limbo_die_list; node; node = node->next)
21291 prune_unused_types_prune (node->die);
21292 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21293 prune_unused_types_prune (ctnode->root_die);
21295 /* Leave the marks clear. */
21296 prune_unmark_dies (comp_unit_die ());
21297 for (node = limbo_die_list; node; node = node->next)
21298 prune_unmark_dies (node->die);
21299 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21300 prune_unmark_dies (ctnode->root_die);
21303 /* Set the parameter to true if there are any relative pathnames in
21304 the file table. */
21305 static int
21306 file_table_relative_p (void ** slot, void *param)
21308 bool *p = (bool *) param;
21309 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
21310 if (!IS_ABSOLUTE_PATH (d->filename))
21312 *p = true;
21313 return 0;
21315 return 1;
21318 /* Routines to manipulate hash table of comdat type units. */
21320 static hashval_t
21321 htab_ct_hash (const void *of)
21323 hashval_t h;
21324 const comdat_type_node *const type_node = (const comdat_type_node *) of;
21326 memcpy (&h, type_node->signature, sizeof (h));
21327 return h;
21330 static int
21331 htab_ct_eq (const void *of1, const void *of2)
21333 const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
21334 const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
21336 return (! memcmp (type_node_1->signature, type_node_2->signature,
21337 DWARF_TYPE_SIGNATURE_SIZE));
21340 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
21341 to the location it would have been added, should we know its
21342 DECL_ASSEMBLER_NAME when we added other attributes. This will
21343 probably improve compactness of debug info, removing equivalent
21344 abbrevs, and hide any differences caused by deferring the
21345 computation of the assembler name, triggered by e.g. PCH. */
21347 static inline void
21348 move_linkage_attr (dw_die_ref die)
21350 unsigned ix = VEC_length (dw_attr_node, die->die_attr);
21351 dw_attr_node linkage = VEC_index (dw_attr_node, die->die_attr, ix - 1);
21353 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
21354 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
21356 while (--ix > 0)
21358 dw_attr_node *prev = &VEC_index (dw_attr_node, die->die_attr, ix - 1);
21360 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
21361 break;
21364 if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
21366 VEC_pop (dw_attr_node, die->die_attr);
21367 VEC_quick_insert (dw_attr_node, die->die_attr, ix, linkage);
21371 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
21372 referenced from typed stack ops and count how often they are used. */
21374 static void
21375 mark_base_types (dw_loc_descr_ref loc)
21377 dw_die_ref base_type = NULL;
21379 for (; loc; loc = loc->dw_loc_next)
21381 switch (loc->dw_loc_opc)
21383 case DW_OP_GNU_regval_type:
21384 case DW_OP_GNU_deref_type:
21385 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
21386 break;
21387 case DW_OP_GNU_convert:
21388 case DW_OP_GNU_reinterpret:
21389 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
21390 continue;
21391 /* FALLTHRU */
21392 case DW_OP_GNU_const_type:
21393 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
21394 break;
21395 case DW_OP_GNU_entry_value:
21396 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
21397 continue;
21398 default:
21399 continue;
21401 gcc_assert (base_type->die_parent == comp_unit_die ());
21402 if (base_type->die_mark)
21403 base_type->die_mark++;
21404 else
21406 VEC_safe_push (dw_die_ref, heap, base_types, base_type);
21407 base_type->die_mark = 1;
21412 /* Comparison function for sorting marked base types. */
21414 static int
21415 base_type_cmp (const void *x, const void *y)
21417 dw_die_ref dx = *(const dw_die_ref *) x;
21418 dw_die_ref dy = *(const dw_die_ref *) y;
21419 unsigned int byte_size1, byte_size2;
21420 unsigned int encoding1, encoding2;
21421 if (dx->die_mark > dy->die_mark)
21422 return -1;
21423 if (dx->die_mark < dy->die_mark)
21424 return 1;
21425 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
21426 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
21427 if (byte_size1 < byte_size2)
21428 return 1;
21429 if (byte_size1 > byte_size2)
21430 return -1;
21431 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
21432 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
21433 if (encoding1 < encoding2)
21434 return 1;
21435 if (encoding1 > encoding2)
21436 return -1;
21437 return 0;
21440 /* Move base types marked by mark_base_types as early as possible
21441 in the CU, sorted by decreasing usage count both to make the
21442 uleb128 references as small as possible and to make sure they
21443 will have die_offset already computed by calc_die_sizes when
21444 sizes of typed stack loc ops is computed. */
21446 static void
21447 move_marked_base_types (void)
21449 unsigned int i;
21450 dw_die_ref base_type, die, c;
21452 if (VEC_empty (dw_die_ref, base_types))
21453 return;
21455 /* Sort by decreasing usage count, they will be added again in that
21456 order later on. */
21457 VEC_qsort (dw_die_ref, base_types, base_type_cmp);
21458 die = comp_unit_die ();
21459 c = die->die_child;
21462 dw_die_ref prev = c;
21463 c = c->die_sib;
21464 while (c->die_mark)
21466 remove_child_with_prev (c, prev);
21467 /* As base types got marked, there must be at least
21468 one node other than DW_TAG_base_type. */
21469 gcc_assert (c != c->die_sib);
21470 c = c->die_sib;
21473 while (c != die->die_child);
21474 gcc_assert (die->die_child);
21475 c = die->die_child;
21476 for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
21478 base_type->die_mark = 0;
21479 base_type->die_sib = c->die_sib;
21480 c->die_sib = base_type;
21481 c = base_type;
21485 /* Helper function for resolve_addr, attempt to resolve
21486 one CONST_STRING, return non-zero if not successful. Similarly verify that
21487 SYMBOL_REFs refer to variables emitted in the current CU. */
21489 static int
21490 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
21492 rtx rtl = *addr;
21494 if (GET_CODE (rtl) == CONST_STRING)
21496 size_t len = strlen (XSTR (rtl, 0)) + 1;
21497 tree t = build_string (len, XSTR (rtl, 0));
21498 tree tlen = size_int (len - 1);
21499 TREE_TYPE (t)
21500 = build_array_type (char_type_node, build_index_type (tlen));
21501 rtl = lookup_constant_def (t);
21502 if (!rtl || !MEM_P (rtl))
21503 return 1;
21504 rtl = XEXP (rtl, 0);
21505 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
21506 *addr = rtl;
21507 return 0;
21510 if (GET_CODE (rtl) == SYMBOL_REF
21511 && SYMBOL_REF_DECL (rtl))
21513 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
21515 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
21516 return 1;
21518 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
21519 return 1;
21522 if (GET_CODE (rtl) == CONST
21523 && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
21524 return 1;
21526 return 0;
21529 /* Helper function for resolve_addr, handle one location
21530 expression, return false if at least one CONST_STRING or SYMBOL_REF in
21531 the location list couldn't be resolved. */
21533 static bool
21534 resolve_addr_in_expr (dw_loc_descr_ref loc)
21536 dw_loc_descr_ref keep = NULL;
21537 for (; loc; loc = loc->dw_loc_next)
21538 switch (loc->dw_loc_opc)
21540 case DW_OP_addr:
21541 if (resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
21542 return false;
21543 break;
21544 case DW_OP_const4u:
21545 case DW_OP_const8u:
21546 if (loc->dtprel
21547 && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
21548 return false;
21549 break;
21550 case DW_OP_plus_uconst:
21551 if (size_of_loc_descr (loc)
21552 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
21554 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
21556 dw_loc_descr_ref repl
21557 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
21558 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
21559 add_loc_descr (&repl, loc->dw_loc_next);
21560 *loc = *repl;
21562 break;
21563 case DW_OP_implicit_value:
21564 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
21565 && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL))
21566 return false;
21567 break;
21568 case DW_OP_GNU_implicit_pointer:
21569 case DW_OP_GNU_parameter_ref:
21570 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
21572 dw_die_ref ref
21573 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
21574 if (ref == NULL)
21575 return false;
21576 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
21577 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
21578 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
21580 break;
21581 case DW_OP_GNU_const_type:
21582 case DW_OP_GNU_regval_type:
21583 case DW_OP_GNU_deref_type:
21584 case DW_OP_GNU_convert:
21585 case DW_OP_GNU_reinterpret:
21586 while (loc->dw_loc_next
21587 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
21589 dw_die_ref base1, base2;
21590 unsigned enc1, enc2, size1, size2;
21591 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
21592 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
21593 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
21594 else if (loc->dw_loc_oprnd1.val_class
21595 == dw_val_class_unsigned_const)
21596 break;
21597 else
21598 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
21599 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
21600 == dw_val_class_unsigned_const)
21601 break;
21602 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
21603 gcc_assert (base1->die_tag == DW_TAG_base_type
21604 && base2->die_tag == DW_TAG_base_type);
21605 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
21606 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
21607 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
21608 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
21609 if (size1 == size2
21610 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
21611 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
21612 && loc != keep)
21613 || enc1 == enc2))
21615 /* Optimize away next DW_OP_GNU_convert after
21616 adjusting LOC's base type die reference. */
21617 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
21618 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
21619 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
21620 else
21621 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
21622 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
21623 continue;
21625 /* Don't change integer DW_OP_GNU_convert after e.g. floating
21626 point typed stack entry. */
21627 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
21628 keep = loc->dw_loc_next;
21629 break;
21631 break;
21632 default:
21633 break;
21635 return true;
21638 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
21639 an address in .rodata section if the string literal is emitted there,
21640 or remove the containing location list or replace DW_AT_const_value
21641 with DW_AT_location and empty location expression, if it isn't found
21642 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
21643 to something that has been emitted in the current CU. */
21645 static void
21646 resolve_addr (dw_die_ref die)
21648 dw_die_ref c;
21649 dw_attr_ref a;
21650 dw_loc_list_ref *curr, *start, loc;
21651 unsigned ix;
21653 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
21654 switch (AT_class (a))
21656 case dw_val_class_loc_list:
21657 start = curr = AT_loc_list_ptr (a);
21658 loc = *curr;
21659 gcc_assert (loc);
21660 /* The same list can be referenced more than once. See if we have
21661 already recorded the result from a previous pass. */
21662 if (loc->replaced)
21663 *curr = loc->dw_loc_next;
21664 else if (!loc->resolved_addr)
21666 /* As things stand, we do not expect or allow one die to
21667 reference a suffix of another die's location list chain.
21668 References must be identical or completely separate.
21669 There is therefore no need to cache the result of this
21670 pass on any list other than the first; doing so
21671 would lead to unnecessary writes. */
21672 while (*curr)
21674 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
21675 if (!resolve_addr_in_expr ((*curr)->expr))
21677 dw_loc_list_ref next = (*curr)->dw_loc_next;
21678 if (next && (*curr)->ll_symbol)
21680 gcc_assert (!next->ll_symbol);
21681 next->ll_symbol = (*curr)->ll_symbol;
21683 *curr = next;
21685 else
21687 mark_base_types ((*curr)->expr);
21688 curr = &(*curr)->dw_loc_next;
21691 if (loc == *start)
21692 loc->resolved_addr = 1;
21693 else
21695 loc->replaced = 1;
21696 loc->dw_loc_next = *start;
21699 if (!*start)
21701 remove_AT (die, a->dw_attr);
21702 ix--;
21704 break;
21705 case dw_val_class_loc:
21707 dw_loc_descr_ref l = AT_loc (a);
21708 /* For -gdwarf-2 don't attempt to optimize
21709 DW_AT_data_member_location containing
21710 DW_OP_plus_uconst - older consumers might
21711 rely on it being that op instead of a more complex,
21712 but shorter, location description. */
21713 if ((dwarf_version > 2
21714 || a->dw_attr != DW_AT_data_member_location
21715 || l == NULL
21716 || l->dw_loc_opc != DW_OP_plus_uconst
21717 || l->dw_loc_next != NULL)
21718 && !resolve_addr_in_expr (l))
21720 remove_AT (die, a->dw_attr);
21721 ix--;
21723 else
21724 mark_base_types (l);
21726 break;
21727 case dw_val_class_addr:
21728 if (a->dw_attr == DW_AT_const_value
21729 && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
21731 remove_AT (die, a->dw_attr);
21732 ix--;
21734 if (die->die_tag == DW_TAG_GNU_call_site
21735 && a->dw_attr == DW_AT_abstract_origin)
21737 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
21738 dw_die_ref tdie = lookup_decl_die (tdecl);
21739 if (tdie == NULL
21740 && DECL_EXTERNAL (tdecl)
21741 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
21743 force_decl_die (tdecl);
21744 tdie = lookup_decl_die (tdecl);
21746 if (tdie)
21748 a->dw_attr_val.val_class = dw_val_class_die_ref;
21749 a->dw_attr_val.v.val_die_ref.die = tdie;
21750 a->dw_attr_val.v.val_die_ref.external = 0;
21752 else
21754 remove_AT (die, a->dw_attr);
21755 ix--;
21758 break;
21759 default:
21760 break;
21763 FOR_EACH_CHILD (die, c, resolve_addr (c));
21766 /* Helper routines for optimize_location_lists.
21767 This pass tries to share identical local lists in .debug_loc
21768 section. */
21770 /* Iteratively hash operands of LOC opcode. */
21772 static inline hashval_t
21773 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
21775 dw_val_ref val1 = &loc->dw_loc_oprnd1;
21776 dw_val_ref val2 = &loc->dw_loc_oprnd2;
21778 switch (loc->dw_loc_opc)
21780 case DW_OP_const4u:
21781 case DW_OP_const8u:
21782 if (loc->dtprel)
21783 goto hash_addr;
21784 /* FALLTHRU */
21785 case DW_OP_const1u:
21786 case DW_OP_const1s:
21787 case DW_OP_const2u:
21788 case DW_OP_const2s:
21789 case DW_OP_const4s:
21790 case DW_OP_const8s:
21791 case DW_OP_constu:
21792 case DW_OP_consts:
21793 case DW_OP_pick:
21794 case DW_OP_plus_uconst:
21795 case DW_OP_breg0:
21796 case DW_OP_breg1:
21797 case DW_OP_breg2:
21798 case DW_OP_breg3:
21799 case DW_OP_breg4:
21800 case DW_OP_breg5:
21801 case DW_OP_breg6:
21802 case DW_OP_breg7:
21803 case DW_OP_breg8:
21804 case DW_OP_breg9:
21805 case DW_OP_breg10:
21806 case DW_OP_breg11:
21807 case DW_OP_breg12:
21808 case DW_OP_breg13:
21809 case DW_OP_breg14:
21810 case DW_OP_breg15:
21811 case DW_OP_breg16:
21812 case DW_OP_breg17:
21813 case DW_OP_breg18:
21814 case DW_OP_breg19:
21815 case DW_OP_breg20:
21816 case DW_OP_breg21:
21817 case DW_OP_breg22:
21818 case DW_OP_breg23:
21819 case DW_OP_breg24:
21820 case DW_OP_breg25:
21821 case DW_OP_breg26:
21822 case DW_OP_breg27:
21823 case DW_OP_breg28:
21824 case DW_OP_breg29:
21825 case DW_OP_breg30:
21826 case DW_OP_breg31:
21827 case DW_OP_regx:
21828 case DW_OP_fbreg:
21829 case DW_OP_piece:
21830 case DW_OP_deref_size:
21831 case DW_OP_xderef_size:
21832 hash = iterative_hash_object (val1->v.val_int, hash);
21833 break;
21834 case DW_OP_skip:
21835 case DW_OP_bra:
21837 int offset;
21839 gcc_assert (val1->val_class == dw_val_class_loc);
21840 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
21841 hash = iterative_hash_object (offset, hash);
21843 break;
21844 case DW_OP_implicit_value:
21845 hash = iterative_hash_object (val1->v.val_unsigned, hash);
21846 switch (val2->val_class)
21848 case dw_val_class_const:
21849 hash = iterative_hash_object (val2->v.val_int, hash);
21850 break;
21851 case dw_val_class_vec:
21853 unsigned int elt_size = val2->v.val_vec.elt_size;
21854 unsigned int len = val2->v.val_vec.length;
21856 hash = iterative_hash_object (elt_size, hash);
21857 hash = iterative_hash_object (len, hash);
21858 hash = iterative_hash (val2->v.val_vec.array,
21859 len * elt_size, hash);
21861 break;
21862 case dw_val_class_const_double:
21863 hash = iterative_hash_object (val2->v.val_double.low, hash);
21864 hash = iterative_hash_object (val2->v.val_double.high, hash);
21865 break;
21866 case dw_val_class_addr:
21867 hash = iterative_hash_rtx (val2->v.val_addr, hash);
21868 break;
21869 default:
21870 gcc_unreachable ();
21872 break;
21873 case DW_OP_bregx:
21874 case DW_OP_bit_piece:
21875 hash = iterative_hash_object (val1->v.val_int, hash);
21876 hash = iterative_hash_object (val2->v.val_int, hash);
21877 break;
21878 case DW_OP_addr:
21879 hash_addr:
21880 if (loc->dtprel)
21882 unsigned char dtprel = 0xd1;
21883 hash = iterative_hash_object (dtprel, hash);
21885 hash = iterative_hash_rtx (val1->v.val_addr, hash);
21886 break;
21887 case DW_OP_GNU_implicit_pointer:
21888 hash = iterative_hash_object (val2->v.val_int, hash);
21889 break;
21890 case DW_OP_GNU_entry_value:
21891 hash = hash_loc_operands (val1->v.val_loc, hash);
21892 break;
21893 case DW_OP_GNU_regval_type:
21894 case DW_OP_GNU_deref_type:
21896 unsigned int byte_size
21897 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
21898 unsigned int encoding
21899 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
21900 hash = iterative_hash_object (val1->v.val_int, hash);
21901 hash = iterative_hash_object (byte_size, hash);
21902 hash = iterative_hash_object (encoding, hash);
21904 break;
21905 case DW_OP_GNU_convert:
21906 case DW_OP_GNU_reinterpret:
21907 if (val1->val_class == dw_val_class_unsigned_const)
21909 hash = iterative_hash_object (val1->v.val_unsigned, hash);
21910 break;
21912 /* FALLTHRU */
21913 case DW_OP_GNU_const_type:
21915 unsigned int byte_size
21916 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
21917 unsigned int encoding
21918 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
21919 hash = iterative_hash_object (byte_size, hash);
21920 hash = iterative_hash_object (encoding, hash);
21921 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
21922 break;
21923 hash = iterative_hash_object (val2->val_class, hash);
21924 switch (val2->val_class)
21926 case dw_val_class_const:
21927 hash = iterative_hash_object (val2->v.val_int, hash);
21928 break;
21929 case dw_val_class_vec:
21931 unsigned int elt_size = val2->v.val_vec.elt_size;
21932 unsigned int len = val2->v.val_vec.length;
21934 hash = iterative_hash_object (elt_size, hash);
21935 hash = iterative_hash_object (len, hash);
21936 hash = iterative_hash (val2->v.val_vec.array,
21937 len * elt_size, hash);
21939 break;
21940 case dw_val_class_const_double:
21941 hash = iterative_hash_object (val2->v.val_double.low, hash);
21942 hash = iterative_hash_object (val2->v.val_double.high, hash);
21943 break;
21944 default:
21945 gcc_unreachable ();
21948 break;
21950 default:
21951 /* Other codes have no operands. */
21952 break;
21954 return hash;
21957 /* Iteratively hash the whole DWARF location expression LOC. */
21959 static inline hashval_t
21960 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
21962 dw_loc_descr_ref l;
21963 bool sizes_computed = false;
21964 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
21965 size_of_locs (loc);
21967 for (l = loc; l != NULL; l = l->dw_loc_next)
21969 enum dwarf_location_atom opc = l->dw_loc_opc;
21970 hash = iterative_hash_object (opc, hash);
21971 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
21973 size_of_locs (loc);
21974 sizes_computed = true;
21976 hash = hash_loc_operands (l, hash);
21978 return hash;
21981 /* Compute hash of the whole location list LIST_HEAD. */
21983 static inline void
21984 hash_loc_list (dw_loc_list_ref list_head)
21986 dw_loc_list_ref curr = list_head;
21987 hashval_t hash = 0;
21989 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
21991 hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
21992 hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
21993 if (curr->section)
21994 hash = iterative_hash (curr->section, strlen (curr->section) + 1,
21995 hash);
21996 hash = hash_locs (curr->expr, hash);
21998 list_head->hash = hash;
22001 /* Return true if X and Y opcodes have the same operands. */
22003 static inline bool
22004 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
22006 dw_val_ref valx1 = &x->dw_loc_oprnd1;
22007 dw_val_ref valx2 = &x->dw_loc_oprnd2;
22008 dw_val_ref valy1 = &y->dw_loc_oprnd1;
22009 dw_val_ref valy2 = &y->dw_loc_oprnd2;
22011 switch (x->dw_loc_opc)
22013 case DW_OP_const4u:
22014 case DW_OP_const8u:
22015 if (x->dtprel)
22016 goto hash_addr;
22017 /* FALLTHRU */
22018 case DW_OP_const1u:
22019 case DW_OP_const1s:
22020 case DW_OP_const2u:
22021 case DW_OP_const2s:
22022 case DW_OP_const4s:
22023 case DW_OP_const8s:
22024 case DW_OP_constu:
22025 case DW_OP_consts:
22026 case DW_OP_pick:
22027 case DW_OP_plus_uconst:
22028 case DW_OP_breg0:
22029 case DW_OP_breg1:
22030 case DW_OP_breg2:
22031 case DW_OP_breg3:
22032 case DW_OP_breg4:
22033 case DW_OP_breg5:
22034 case DW_OP_breg6:
22035 case DW_OP_breg7:
22036 case DW_OP_breg8:
22037 case DW_OP_breg9:
22038 case DW_OP_breg10:
22039 case DW_OP_breg11:
22040 case DW_OP_breg12:
22041 case DW_OP_breg13:
22042 case DW_OP_breg14:
22043 case DW_OP_breg15:
22044 case DW_OP_breg16:
22045 case DW_OP_breg17:
22046 case DW_OP_breg18:
22047 case DW_OP_breg19:
22048 case DW_OP_breg20:
22049 case DW_OP_breg21:
22050 case DW_OP_breg22:
22051 case DW_OP_breg23:
22052 case DW_OP_breg24:
22053 case DW_OP_breg25:
22054 case DW_OP_breg26:
22055 case DW_OP_breg27:
22056 case DW_OP_breg28:
22057 case DW_OP_breg29:
22058 case DW_OP_breg30:
22059 case DW_OP_breg31:
22060 case DW_OP_regx:
22061 case DW_OP_fbreg:
22062 case DW_OP_piece:
22063 case DW_OP_deref_size:
22064 case DW_OP_xderef_size:
22065 return valx1->v.val_int == valy1->v.val_int;
22066 case DW_OP_skip:
22067 case DW_OP_bra:
22068 gcc_assert (valx1->val_class == dw_val_class_loc
22069 && valy1->val_class == dw_val_class_loc
22070 && x->dw_loc_addr == y->dw_loc_addr);
22071 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
22072 case DW_OP_implicit_value:
22073 if (valx1->v.val_unsigned != valy1->v.val_unsigned
22074 || valx2->val_class != valy2->val_class)
22075 return false;
22076 switch (valx2->val_class)
22078 case dw_val_class_const:
22079 return valx2->v.val_int == valy2->v.val_int;
22080 case dw_val_class_vec:
22081 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
22082 && valx2->v.val_vec.length == valy2->v.val_vec.length
22083 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
22084 valx2->v.val_vec.elt_size
22085 * valx2->v.val_vec.length) == 0;
22086 case dw_val_class_const_double:
22087 return valx2->v.val_double.low == valy2->v.val_double.low
22088 && valx2->v.val_double.high == valy2->v.val_double.high;
22089 case dw_val_class_addr:
22090 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
22091 default:
22092 gcc_unreachable ();
22094 case DW_OP_bregx:
22095 case DW_OP_bit_piece:
22096 return valx1->v.val_int == valy1->v.val_int
22097 && valx2->v.val_int == valy2->v.val_int;
22098 case DW_OP_addr:
22099 hash_addr:
22100 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
22101 case DW_OP_GNU_implicit_pointer:
22102 return valx1->val_class == dw_val_class_die_ref
22103 && valx1->val_class == valy1->val_class
22104 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
22105 && valx2->v.val_int == valy2->v.val_int;
22106 case DW_OP_GNU_entry_value:
22107 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
22108 case DW_OP_GNU_const_type:
22109 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
22110 || valx2->val_class != valy2->val_class)
22111 return false;
22112 switch (valx2->val_class)
22114 case dw_val_class_const:
22115 return valx2->v.val_int == valy2->v.val_int;
22116 case dw_val_class_vec:
22117 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
22118 && valx2->v.val_vec.length == valy2->v.val_vec.length
22119 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
22120 valx2->v.val_vec.elt_size
22121 * valx2->v.val_vec.length) == 0;
22122 case dw_val_class_const_double:
22123 return valx2->v.val_double.low == valy2->v.val_double.low
22124 && valx2->v.val_double.high == valy2->v.val_double.high;
22125 default:
22126 gcc_unreachable ();
22128 case DW_OP_GNU_regval_type:
22129 case DW_OP_GNU_deref_type:
22130 return valx1->v.val_int == valy1->v.val_int
22131 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
22132 case DW_OP_GNU_convert:
22133 case DW_OP_GNU_reinterpret:
22134 if (valx1->val_class != valy1->val_class)
22135 return false;
22136 if (valx1->val_class == dw_val_class_unsigned_const)
22137 return valx1->v.val_unsigned == valy1->v.val_unsigned;
22138 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
22139 case DW_OP_GNU_parameter_ref:
22140 return valx1->val_class == dw_val_class_die_ref
22141 && valx1->val_class == valy1->val_class
22142 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
22143 default:
22144 /* Other codes have no operands. */
22145 return true;
22149 /* Return true if DWARF location expressions X and Y are the same. */
22151 static inline bool
22152 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
22154 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
22155 if (x->dw_loc_opc != y->dw_loc_opc
22156 || x->dtprel != y->dtprel
22157 || !compare_loc_operands (x, y))
22158 break;
22159 return x == NULL && y == NULL;
22162 /* Return precomputed hash of location list X. */
22164 static hashval_t
22165 loc_list_hash (const void *x)
22167 return ((const struct dw_loc_list_struct *) x)->hash;
22170 /* Return 1 if location lists X and Y are the same. */
22172 static int
22173 loc_list_eq (const void *x, const void *y)
22175 const struct dw_loc_list_struct *a = (const struct dw_loc_list_struct *) x;
22176 const struct dw_loc_list_struct *b = (const struct dw_loc_list_struct *) y;
22177 if (a == b)
22178 return 1;
22179 if (a->hash != b->hash)
22180 return 0;
22181 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
22182 if (strcmp (a->begin, b->begin) != 0
22183 || strcmp (a->end, b->end) != 0
22184 || (a->section == NULL) != (b->section == NULL)
22185 || (a->section && strcmp (a->section, b->section) != 0)
22186 || !compare_locs (a->expr, b->expr))
22187 break;
22188 return a == NULL && b == NULL;
22191 /* Recursively optimize location lists referenced from DIE
22192 children and share them whenever possible. */
22194 static void
22195 optimize_location_lists_1 (dw_die_ref die, htab_t htab)
22197 dw_die_ref c;
22198 dw_attr_ref a;
22199 unsigned ix;
22200 void **slot;
22202 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22203 if (AT_class (a) == dw_val_class_loc_list)
22205 dw_loc_list_ref list = AT_loc_list (a);
22206 /* TODO: perform some optimizations here, before hashing
22207 it and storing into the hash table. */
22208 hash_loc_list (list);
22209 slot = htab_find_slot_with_hash (htab, list, list->hash,
22210 INSERT);
22211 if (*slot == NULL)
22212 *slot = (void *) list;
22213 else
22214 a->dw_attr_val.v.val_loc_list = (dw_loc_list_ref) *slot;
22217 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
22220 /* Optimize location lists referenced from DIE
22221 children and share them whenever possible. */
22223 static void
22224 optimize_location_lists (dw_die_ref die)
22226 htab_t htab = htab_create (500, loc_list_hash, loc_list_eq, NULL);
22227 optimize_location_lists_1 (die, htab);
22228 htab_delete (htab);
22231 /* Output stuff that dwarf requires at the end of every file,
22232 and generate the DWARF-2 debugging info. */
22234 static void
22235 dwarf2out_finish (const char *filename)
22237 limbo_die_node *node, *next_node;
22238 comdat_type_node *ctnode;
22239 htab_t comdat_type_table;
22240 unsigned int i;
22242 /* PCH might result in DW_AT_producer string being restored from the
22243 header compilation, fix it up if needed. */
22244 dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
22245 if (strcmp (AT_string (producer), producer_string) != 0)
22247 struct indirect_string_node *node = find_AT_string (producer_string);
22248 producer->dw_attr_val.v.val_str = node;
22251 gen_scheduled_generic_parms_dies ();
22252 gen_remaining_tmpl_value_param_die_attribute ();
22254 /* Add the name for the main input file now. We delayed this from
22255 dwarf2out_init to avoid complications with PCH. */
22256 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
22257 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
22258 add_comp_dir_attribute (comp_unit_die ());
22259 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
22261 bool p = false;
22262 htab_traverse (file_table, file_table_relative_p, &p);
22263 if (p)
22264 add_comp_dir_attribute (comp_unit_die ());
22267 for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
22269 add_location_or_const_value_attribute (
22270 VEC_index (deferred_locations, deferred_locations_list, i).die,
22271 VEC_index (deferred_locations, deferred_locations_list, i).variable,
22272 false,
22273 DW_AT_location);
22276 /* Traverse the limbo die list, and add parent/child links. The only
22277 dies without parents that should be here are concrete instances of
22278 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
22279 For concrete instances, we can get the parent die from the abstract
22280 instance. */
22281 for (node = limbo_die_list; node; node = next_node)
22283 dw_die_ref die = node->die;
22284 next_node = node->next;
22286 if (die->die_parent == NULL)
22288 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
22290 if (origin && origin->die_parent)
22291 add_child_die (origin->die_parent, die);
22292 else if (is_cu_die (die))
22294 else if (seen_error ())
22295 /* It's OK to be confused by errors in the input. */
22296 add_child_die (comp_unit_die (), die);
22297 else
22299 /* In certain situations, the lexical block containing a
22300 nested function can be optimized away, which results
22301 in the nested function die being orphaned. Likewise
22302 with the return type of that nested function. Force
22303 this to be a child of the containing function.
22305 It may happen that even the containing function got fully
22306 inlined and optimized out. In that case we are lost and
22307 assign the empty child. This should not be big issue as
22308 the function is likely unreachable too. */
22309 gcc_assert (node->created_for);
22311 if (DECL_P (node->created_for))
22312 origin = get_context_die (DECL_CONTEXT (node->created_for));
22313 else if (TYPE_P (node->created_for))
22314 origin = scope_die_for (node->created_for, comp_unit_die ());
22315 else
22316 origin = comp_unit_die ();
22318 add_child_die (origin, die);
22323 limbo_die_list = NULL;
22325 #if ENABLE_ASSERT_CHECKING
22327 dw_die_ref die = comp_unit_die (), c;
22328 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
22330 #endif
22331 resolve_addr (comp_unit_die ());
22332 move_marked_base_types ();
22334 for (node = deferred_asm_name; node; node = node->next)
22336 tree decl = node->created_for;
22337 /* When generating LTO bytecode we can not generate new assembler
22338 names at this point and all important decls got theirs via
22339 free-lang-data. */
22340 if ((!flag_generate_lto || DECL_ASSEMBLER_NAME_SET_P (decl))
22341 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
22343 add_linkage_attr (node->die, decl);
22344 move_linkage_attr (node->die);
22348 deferred_asm_name = NULL;
22350 /* Walk through the list of incomplete types again, trying once more to
22351 emit full debugging info for them. */
22352 retry_incomplete_types ();
22354 if (flag_eliminate_unused_debug_types)
22355 prune_unused_types ();
22357 /* Generate separate COMDAT sections for type DIEs. */
22358 if (use_debug_types)
22360 break_out_comdat_types (comp_unit_die ());
22362 /* Each new type_unit DIE was added to the limbo die list when created.
22363 Since these have all been added to comdat_type_list, clear the
22364 limbo die list. */
22365 limbo_die_list = NULL;
22367 /* For each new comdat type unit, copy declarations for incomplete
22368 types to make the new unit self-contained (i.e., no direct
22369 references to the main compile unit). */
22370 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22371 copy_decls_for_unworthy_types (ctnode->root_die);
22372 copy_decls_for_unworthy_types (comp_unit_die ());
22374 /* In the process of copying declarations from one unit to another,
22375 we may have left some declarations behind that are no longer
22376 referenced. Prune them. */
22377 prune_unused_types ();
22380 /* Generate separate CUs for each of the include files we've seen.
22381 They will go into limbo_die_list. */
22382 if (flag_eliminate_dwarf2_dups)
22383 break_out_includes (comp_unit_die ());
22385 /* Traverse the DIE's and add add sibling attributes to those DIE's
22386 that have children. */
22387 add_sibling_attributes (comp_unit_die ());
22388 for (node = limbo_die_list; node; node = node->next)
22389 add_sibling_attributes (node->die);
22390 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22391 add_sibling_attributes (ctnode->root_die);
22393 /* Output a terminator label for the .text section. */
22394 switch_to_section (text_section);
22395 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
22396 if (cold_text_section)
22398 switch_to_section (cold_text_section);
22399 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
22402 /* We can only use the low/high_pc attributes if all of the code was
22403 in .text. */
22404 if (!have_multiple_function_sections
22405 || (dwarf_version < 3 && dwarf_strict))
22407 /* Don't add if the CU has no associated code. */
22408 if (text_section_used)
22409 add_AT_low_high_pc (comp_unit_die (), text_section_label,
22410 text_end_label);
22412 else
22414 unsigned fde_idx;
22415 dw_fde_ref fde;
22416 bool range_list_added = false;
22418 if (text_section_used)
22419 add_ranges_by_labels (comp_unit_die (), text_section_label,
22420 text_end_label, &range_list_added);
22421 if (cold_text_section_used)
22422 add_ranges_by_labels (comp_unit_die (), cold_text_section_label,
22423 cold_end_label, &range_list_added);
22425 FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, fde_idx, fde)
22427 if (DECL_IGNORED_P (fde->decl))
22428 continue;
22429 if (!fde->in_std_section)
22430 add_ranges_by_labels (comp_unit_die (), fde->dw_fde_begin,
22431 fde->dw_fde_end, &range_list_added);
22432 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
22433 add_ranges_by_labels (comp_unit_die (), fde->dw_fde_second_begin,
22434 fde->dw_fde_second_end, &range_list_added);
22437 if (range_list_added)
22439 /* We need to give .debug_loc and .debug_ranges an appropriate
22440 "base address". Use zero so that these addresses become
22441 absolute. Historically, we've emitted the unexpected
22442 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
22443 Emit both to give time for other tools to adapt. */
22444 add_AT_addr (comp_unit_die (), DW_AT_low_pc, const0_rtx);
22445 if (! dwarf_strict && dwarf_version < 4)
22446 add_AT_addr (comp_unit_die (), DW_AT_entry_pc, const0_rtx);
22448 add_ranges (NULL);
22452 if (debug_info_level >= DINFO_LEVEL_NORMAL)
22453 add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list,
22454 debug_line_section_label);
22456 if (have_macinfo)
22457 add_AT_macptr (comp_unit_die (),
22458 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
22459 macinfo_section_label);
22461 if (have_location_lists)
22462 optimize_location_lists (comp_unit_die ());
22464 /* Output all of the compilation units. We put the main one last so that
22465 the offsets are available to output_pubnames. */
22466 for (node = limbo_die_list; node; node = node->next)
22467 output_comp_unit (node->die, 0);
22469 comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
22470 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22472 void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
22474 /* Don't output duplicate types. */
22475 if (*slot != HTAB_EMPTY_ENTRY)
22476 continue;
22478 /* Add a pointer to the line table for the main compilation unit
22479 so that the debugger can make sense of DW_AT_decl_file
22480 attributes. */
22481 if (debug_info_level >= DINFO_LEVEL_NORMAL)
22482 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
22483 debug_line_section_label);
22485 output_comdat_type_unit (ctnode);
22486 *slot = ctnode;
22488 htab_delete (comdat_type_table);
22490 add_AT_pubnames (comp_unit_die ());
22492 /* Output the main compilation unit if non-empty or if .debug_macinfo
22493 or .debug_macro will be emitted. */
22494 output_comp_unit (comp_unit_die (), have_macinfo);
22496 /* Output the abbreviation table. */
22497 if (abbrev_die_table_in_use != 1)
22499 switch_to_section (debug_abbrev_section);
22500 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
22501 output_abbrev_section ();
22504 /* Output location list section if necessary. */
22505 if (have_location_lists)
22507 /* Output the location lists info. */
22508 switch_to_section (debug_loc_section);
22509 ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
22510 DEBUG_LOC_SECTION_LABEL, 0);
22511 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
22512 output_location_lists (comp_unit_die ());
22515 /* Output public names and types tables if necessary. */
22516 output_pubnames (pubname_table);
22517 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
22518 It shouldn't hurt to emit it always, since pure DWARF2 consumers
22519 simply won't look for the section. */
22520 output_pubnames (pubtype_table);
22522 /* Output the address range information if a CU (.debug_info section)
22523 was emitted. We output an empty table even if we had no functions
22524 to put in it. This because the consumer has no way to tell the
22525 difference between an empty table that we omitted and failure to
22526 generate a table that would have contained data. */
22527 if (info_section_emitted)
22529 unsigned long aranges_length = size_of_aranges ();
22531 switch_to_section (debug_aranges_section);
22532 output_aranges (aranges_length);
22535 /* Output ranges section if necessary. */
22536 if (ranges_table_in_use)
22538 switch_to_section (debug_ranges_section);
22539 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
22540 output_ranges ();
22543 /* Have to end the macro section. */
22544 if (have_macinfo)
22546 switch_to_section (debug_macinfo_section);
22547 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
22548 output_macinfo ();
22549 dw2_asm_output_data (1, 0, "End compilation unit");
22552 /* Output the source line correspondence table. We must do this
22553 even if there is no line information. Otherwise, on an empty
22554 translation unit, we will generate a present, but empty,
22555 .debug_info section. IRIX 6.5 `nm' will then complain when
22556 examining the file. This is done late so that any filenames
22557 used by the debug_info section are marked as 'used'. */
22558 switch_to_section (debug_line_section);
22559 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
22560 if (! DWARF2_ASM_LINE_DEBUG_INFO)
22561 output_line_info ();
22563 /* If we emitted any DW_FORM_strp form attribute, output the string
22564 table too. */
22565 if (debug_str_hash)
22566 htab_traverse (debug_str_hash, output_indirect_string, NULL);
22569 #include "gt-dwarf2out.h"