* decl.c (cp_finish_decl): Check for invalid multiple initializers
[official-gcc.git] / gcc / dwarf2out.c
bloba87eaf2bd7dd4f59ea937846476075d2d480ce64
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 uhwi_to_double_int (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 uhwi_to_double_int (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 = shwi_to_double_int (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 (double_int_to_shwi (val), 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 = uhwi_to_double_int (align);
13650 t = double_int_add (t, alignd);
13651 t = double_int_add (t, double_int_minus_one);
13652 t = double_int_div (t, alignd, true, TRUNC_DIV_EXPR);
13653 t = double_int_mul (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 = double_int_add (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
13766 = double_int_sub (deepest_bitpos, type_size_in_bits);
13768 /* Round up to type_align by default. This works best for
13769 bitfields. */
13770 object_offset_in_bits
13771 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
13773 if (double_int_ucmp (object_offset_in_bits, bitpos_int) > 0)
13775 object_offset_in_bits
13776 = double_int_sub (deepest_bitpos, type_size_in_bits);
13778 /* Round up to decl_align instead. */
13779 object_offset_in_bits
13780 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
13783 else
13784 #endif /* PCC_BITFIELD_TYPE_MATTERS */
13785 object_offset_in_bits = bitpos_int;
13787 object_offset_in_bytes
13788 = double_int_div (object_offset_in_bits,
13789 uhwi_to_double_int (BITS_PER_UNIT), true,
13790 TRUNC_DIV_EXPR);
13791 return double_int_to_shwi (object_offset_in_bytes);
13794 /* The following routines define various Dwarf attributes and any data
13795 associated with them. */
13797 /* Add a location description attribute value to a DIE.
13799 This emits location attributes suitable for whole variables and
13800 whole parameters. Note that the location attributes for struct fields are
13801 generated by the routine `data_member_location_attribute' below. */
13803 static inline void
13804 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
13805 dw_loc_list_ref descr)
13807 if (descr == 0)
13808 return;
13809 if (single_element_loc_list_p (descr))
13810 add_AT_loc (die, attr_kind, descr->expr);
13811 else
13812 add_AT_loc_list (die, attr_kind, descr);
13815 /* Add DW_AT_accessibility attribute to DIE if needed. */
13817 static void
13818 add_accessibility_attribute (dw_die_ref die, tree decl)
13820 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
13821 children, otherwise the default is DW_ACCESS_public. In DWARF2
13822 the default has always been DW_ACCESS_public. */
13823 if (TREE_PROTECTED (decl))
13824 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
13825 else if (TREE_PRIVATE (decl))
13827 if (dwarf_version == 2
13828 || die->die_parent == NULL
13829 || die->die_parent->die_tag != DW_TAG_class_type)
13830 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
13832 else if (dwarf_version > 2
13833 && die->die_parent
13834 && die->die_parent->die_tag == DW_TAG_class_type)
13835 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
13838 /* Attach the specialized form of location attribute used for data members of
13839 struct and union types. In the special case of a FIELD_DECL node which
13840 represents a bit-field, the "offset" part of this special location
13841 descriptor must indicate the distance in bytes from the lowest-addressed
13842 byte of the containing struct or union type to the lowest-addressed byte of
13843 the "containing object" for the bit-field. (See the `field_byte_offset'
13844 function above).
13846 For any given bit-field, the "containing object" is a hypothetical object
13847 (of some integral or enum type) within which the given bit-field lives. The
13848 type of this hypothetical "containing object" is always the same as the
13849 declared type of the individual bit-field itself (for GCC anyway... the
13850 DWARF spec doesn't actually mandate this). Note that it is the size (in
13851 bytes) of the hypothetical "containing object" which will be given in the
13852 DW_AT_byte_size attribute for this bit-field. (See the
13853 `byte_size_attribute' function below.) It is also used when calculating the
13854 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
13855 function below.) */
13857 static void
13858 add_data_member_location_attribute (dw_die_ref die, tree decl)
13860 HOST_WIDE_INT offset;
13861 dw_loc_descr_ref loc_descr = 0;
13863 if (TREE_CODE (decl) == TREE_BINFO)
13865 /* We're working on the TAG_inheritance for a base class. */
13866 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
13868 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
13869 aren't at a fixed offset from all (sub)objects of the same
13870 type. We need to extract the appropriate offset from our
13871 vtable. The following dwarf expression means
13873 BaseAddr = ObAddr + *((*ObAddr) - Offset)
13875 This is specific to the V3 ABI, of course. */
13877 dw_loc_descr_ref tmp;
13879 /* Make a copy of the object address. */
13880 tmp = new_loc_descr (DW_OP_dup, 0, 0);
13881 add_loc_descr (&loc_descr, tmp);
13883 /* Extract the vtable address. */
13884 tmp = new_loc_descr (DW_OP_deref, 0, 0);
13885 add_loc_descr (&loc_descr, tmp);
13887 /* Calculate the address of the offset. */
13888 offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
13889 gcc_assert (offset < 0);
13891 tmp = int_loc_descriptor (-offset);
13892 add_loc_descr (&loc_descr, tmp);
13893 tmp = new_loc_descr (DW_OP_minus, 0, 0);
13894 add_loc_descr (&loc_descr, tmp);
13896 /* Extract the offset. */
13897 tmp = new_loc_descr (DW_OP_deref, 0, 0);
13898 add_loc_descr (&loc_descr, tmp);
13900 /* Add it to the object address. */
13901 tmp = new_loc_descr (DW_OP_plus, 0, 0);
13902 add_loc_descr (&loc_descr, tmp);
13904 else
13905 offset = tree_low_cst (BINFO_OFFSET (decl), 0);
13907 else
13908 offset = field_byte_offset (decl);
13910 if (! loc_descr)
13912 if (dwarf_version > 2)
13914 /* Don't need to output a location expression, just the constant. */
13915 if (offset < 0)
13916 add_AT_int (die, DW_AT_data_member_location, offset);
13917 else
13918 add_AT_unsigned (die, DW_AT_data_member_location, offset);
13919 return;
13921 else
13923 enum dwarf_location_atom op;
13925 /* The DWARF2 standard says that we should assume that the structure
13926 address is already on the stack, so we can specify a structure
13927 field address by using DW_OP_plus_uconst. */
13928 op = DW_OP_plus_uconst;
13929 loc_descr = new_loc_descr (op, offset, 0);
13933 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
13936 /* Writes integer values to dw_vec_const array. */
13938 static void
13939 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
13941 while (size != 0)
13943 *dest++ = val & 0xff;
13944 val >>= 8;
13945 --size;
13949 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
13951 static HOST_WIDE_INT
13952 extract_int (const unsigned char *src, unsigned int size)
13954 HOST_WIDE_INT val = 0;
13956 src += size;
13957 while (size != 0)
13959 val <<= 8;
13960 val |= *--src & 0xff;
13961 --size;
13963 return val;
13966 /* Writes double_int values to dw_vec_const array. */
13968 static void
13969 insert_double (double_int val, unsigned char *dest)
13971 unsigned char *p0 = dest;
13972 unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
13974 if (WORDS_BIG_ENDIAN)
13976 p0 = p1;
13977 p1 = dest;
13980 insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
13981 insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
13984 /* Writes floating point values to dw_vec_const array. */
13986 static void
13987 insert_float (const_rtx rtl, unsigned char *array)
13989 REAL_VALUE_TYPE rv;
13990 long val[4];
13991 int i;
13993 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
13994 real_to_target (val, &rv, GET_MODE (rtl));
13996 /* real_to_target puts 32-bit pieces in each long. Pack them. */
13997 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
13999 insert_int (val[i], 4, array);
14000 array += 4;
14004 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
14005 does not have a "location" either in memory or in a register. These
14006 things can arise in GNU C when a constant is passed as an actual parameter
14007 to an inlined function. They can also arise in C++ where declared
14008 constants do not necessarily get memory "homes". */
14010 static bool
14011 add_const_value_attribute (dw_die_ref die, rtx rtl)
14013 switch (GET_CODE (rtl))
14015 case CONST_INT:
14017 HOST_WIDE_INT val = INTVAL (rtl);
14019 if (val < 0)
14020 add_AT_int (die, DW_AT_const_value, val);
14021 else
14022 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
14024 return true;
14026 case CONST_DOUBLE:
14027 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
14028 floating-point constant. A CONST_DOUBLE is used whenever the
14029 constant requires more than one word in order to be adequately
14030 represented. */
14032 enum machine_mode mode = GET_MODE (rtl);
14034 if (SCALAR_FLOAT_MODE_P (mode))
14036 unsigned int length = GET_MODE_SIZE (mode);
14037 unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
14039 insert_float (rtl, array);
14040 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
14042 else
14043 add_AT_double (die, DW_AT_const_value,
14044 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
14046 return true;
14048 case CONST_VECTOR:
14050 enum machine_mode mode = GET_MODE (rtl);
14051 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
14052 unsigned int length = CONST_VECTOR_NUNITS (rtl);
14053 unsigned char *array = (unsigned char *) ggc_alloc_atomic
14054 (length * elt_size);
14055 unsigned int i;
14056 unsigned char *p;
14058 switch (GET_MODE_CLASS (mode))
14060 case MODE_VECTOR_INT:
14061 for (i = 0, p = array; i < length; i++, p += elt_size)
14063 rtx elt = CONST_VECTOR_ELT (rtl, i);
14064 double_int val = rtx_to_double_int (elt);
14066 if (elt_size <= sizeof (HOST_WIDE_INT))
14067 insert_int (double_int_to_shwi (val), elt_size, p);
14068 else
14070 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
14071 insert_double (val, p);
14074 break;
14076 case MODE_VECTOR_FLOAT:
14077 for (i = 0, p = array; i < length; i++, p += elt_size)
14079 rtx elt = CONST_VECTOR_ELT (rtl, i);
14080 insert_float (elt, p);
14082 break;
14084 default:
14085 gcc_unreachable ();
14088 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
14090 return true;
14092 case CONST_STRING:
14093 if (dwarf_version >= 4 || !dwarf_strict)
14095 dw_loc_descr_ref loc_result;
14096 resolve_one_addr (&rtl, NULL);
14097 rtl_addr:
14098 loc_result = new_loc_descr (DW_OP_addr, 0, 0);
14099 loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
14100 loc_result->dw_loc_oprnd1.v.val_addr = rtl;
14101 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14102 add_AT_loc (die, DW_AT_location, loc_result);
14103 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
14104 return true;
14106 return false;
14108 case CONST:
14109 if (CONSTANT_P (XEXP (rtl, 0)))
14110 return add_const_value_attribute (die, XEXP (rtl, 0));
14111 /* FALLTHROUGH */
14112 case SYMBOL_REF:
14113 if (!const_ok_for_output (rtl))
14114 return false;
14115 case LABEL_REF:
14116 if (dwarf_version >= 4 || !dwarf_strict)
14117 goto rtl_addr;
14118 return false;
14120 case PLUS:
14121 /* In cases where an inlined instance of an inline function is passed
14122 the address of an `auto' variable (which is local to the caller) we
14123 can get a situation where the DECL_RTL of the artificial local
14124 variable (for the inlining) which acts as a stand-in for the
14125 corresponding formal parameter (of the inline function) will look
14126 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
14127 exactly a compile-time constant expression, but it isn't the address
14128 of the (artificial) local variable either. Rather, it represents the
14129 *value* which the artificial local variable always has during its
14130 lifetime. We currently have no way to represent such quasi-constant
14131 values in Dwarf, so for now we just punt and generate nothing. */
14132 return false;
14134 case HIGH:
14135 case CONST_FIXED:
14136 return false;
14138 case MEM:
14139 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
14140 && MEM_READONLY_P (rtl)
14141 && GET_MODE (rtl) == BLKmode)
14143 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
14144 return true;
14146 return false;
14148 default:
14149 /* No other kinds of rtx should be possible here. */
14150 gcc_unreachable ();
14152 return false;
14155 /* Determine whether the evaluation of EXPR references any variables
14156 or functions which aren't otherwise used (and therefore may not be
14157 output). */
14158 static tree
14159 reference_to_unused (tree * tp, int * walk_subtrees,
14160 void * data ATTRIBUTE_UNUSED)
14162 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
14163 *walk_subtrees = 0;
14165 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
14166 && ! TREE_ASM_WRITTEN (*tp))
14167 return *tp;
14168 /* ??? The C++ FE emits debug information for using decls, so
14169 putting gcc_unreachable here falls over. See PR31899. For now
14170 be conservative. */
14171 else if (!cgraph_global_info_ready
14172 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
14173 return *tp;
14174 else if (TREE_CODE (*tp) == VAR_DECL)
14176 struct varpool_node *node = varpool_get_node (*tp);
14177 if (!node || !node->analyzed)
14178 return *tp;
14180 else if (TREE_CODE (*tp) == FUNCTION_DECL
14181 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
14183 /* The call graph machinery must have finished analyzing,
14184 optimizing and gimplifying the CU by now.
14185 So if *TP has no call graph node associated
14186 to it, it means *TP will not be emitted. */
14187 if (!cgraph_get_node (*tp))
14188 return *tp;
14190 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
14191 return *tp;
14193 return NULL_TREE;
14196 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
14197 for use in a later add_const_value_attribute call. */
14199 static rtx
14200 rtl_for_decl_init (tree init, tree type)
14202 rtx rtl = NULL_RTX;
14204 STRIP_NOPS (init);
14206 /* If a variable is initialized with a string constant without embedded
14207 zeros, build CONST_STRING. */
14208 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
14210 tree enttype = TREE_TYPE (type);
14211 tree domain = TYPE_DOMAIN (type);
14212 enum machine_mode mode = TYPE_MODE (enttype);
14214 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
14215 && domain
14216 && integer_zerop (TYPE_MIN_VALUE (domain))
14217 && compare_tree_int (TYPE_MAX_VALUE (domain),
14218 TREE_STRING_LENGTH (init) - 1) == 0
14219 && ((size_t) TREE_STRING_LENGTH (init)
14220 == strlen (TREE_STRING_POINTER (init)) + 1))
14222 rtl = gen_rtx_CONST_STRING (VOIDmode,
14223 ggc_strdup (TREE_STRING_POINTER (init)));
14224 rtl = gen_rtx_MEM (BLKmode, rtl);
14225 MEM_READONLY_P (rtl) = 1;
14228 /* Other aggregates, and complex values, could be represented using
14229 CONCAT: FIXME! */
14230 else if (AGGREGATE_TYPE_P (type)
14231 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
14232 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
14233 || TREE_CODE (type) == COMPLEX_TYPE)
14235 /* Vectors only work if their mode is supported by the target.
14236 FIXME: generic vectors ought to work too. */
14237 else if (TREE_CODE (type) == VECTOR_TYPE
14238 && !VECTOR_MODE_P (TYPE_MODE (type)))
14240 /* If the initializer is something that we know will expand into an
14241 immediate RTL constant, expand it now. We must be careful not to
14242 reference variables which won't be output. */
14243 else if (initializer_constant_valid_p (init, type)
14244 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
14246 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
14247 possible. */
14248 if (TREE_CODE (type) == VECTOR_TYPE)
14249 switch (TREE_CODE (init))
14251 case VECTOR_CST:
14252 break;
14253 case CONSTRUCTOR:
14254 if (TREE_CONSTANT (init))
14256 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
14257 bool constant_p = true;
14258 tree value;
14259 unsigned HOST_WIDE_INT ix;
14261 /* Even when ctor is constant, it might contain non-*_CST
14262 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
14263 belong into VECTOR_CST nodes. */
14264 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
14265 if (!CONSTANT_CLASS_P (value))
14267 constant_p = false;
14268 break;
14271 if (constant_p)
14273 init = build_vector_from_ctor (type, elts);
14274 break;
14277 /* FALLTHRU */
14279 default:
14280 return NULL;
14283 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
14285 /* If expand_expr returns a MEM, it wasn't immediate. */
14286 gcc_assert (!rtl || !MEM_P (rtl));
14289 return rtl;
14292 /* Generate RTL for the variable DECL to represent its location. */
14294 static rtx
14295 rtl_for_decl_location (tree decl)
14297 rtx rtl;
14299 /* Here we have to decide where we are going to say the parameter "lives"
14300 (as far as the debugger is concerned). We only have a couple of
14301 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
14303 DECL_RTL normally indicates where the parameter lives during most of the
14304 activation of the function. If optimization is enabled however, this
14305 could be either NULL or else a pseudo-reg. Both of those cases indicate
14306 that the parameter doesn't really live anywhere (as far as the code
14307 generation parts of GCC are concerned) during most of the function's
14308 activation. That will happen (for example) if the parameter is never
14309 referenced within the function.
14311 We could just generate a location descriptor here for all non-NULL
14312 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
14313 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
14314 where DECL_RTL is NULL or is a pseudo-reg.
14316 Note however that we can only get away with using DECL_INCOMING_RTL as
14317 a backup substitute for DECL_RTL in certain limited cases. In cases
14318 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
14319 we can be sure that the parameter was passed using the same type as it is
14320 declared to have within the function, and that its DECL_INCOMING_RTL
14321 points us to a place where a value of that type is passed.
14323 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
14324 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
14325 because in these cases DECL_INCOMING_RTL points us to a value of some
14326 type which is *different* from the type of the parameter itself. Thus,
14327 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
14328 such cases, the debugger would end up (for example) trying to fetch a
14329 `float' from a place which actually contains the first part of a
14330 `double'. That would lead to really incorrect and confusing
14331 output at debug-time.
14333 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
14334 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
14335 are a couple of exceptions however. On little-endian machines we can
14336 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
14337 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
14338 an integral type that is smaller than TREE_TYPE (decl). These cases arise
14339 when (on a little-endian machine) a non-prototyped function has a
14340 parameter declared to be of type `short' or `char'. In such cases,
14341 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
14342 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
14343 passed `int' value. If the debugger then uses that address to fetch
14344 a `short' or a `char' (on a little-endian machine) the result will be
14345 the correct data, so we allow for such exceptional cases below.
14347 Note that our goal here is to describe the place where the given formal
14348 parameter lives during most of the function's activation (i.e. between the
14349 end of the prologue and the start of the epilogue). We'll do that as best
14350 as we can. Note however that if the given formal parameter is modified
14351 sometime during the execution of the function, then a stack backtrace (at
14352 debug-time) will show the function as having been called with the *new*
14353 value rather than the value which was originally passed in. This happens
14354 rarely enough that it is not a major problem, but it *is* a problem, and
14355 I'd like to fix it.
14357 A future version of dwarf2out.c may generate two additional attributes for
14358 any given DW_TAG_formal_parameter DIE which will describe the "passed
14359 type" and the "passed location" for the given formal parameter in addition
14360 to the attributes we now generate to indicate the "declared type" and the
14361 "active location" for each parameter. This additional set of attributes
14362 could be used by debuggers for stack backtraces. Separately, note that
14363 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
14364 This happens (for example) for inlined-instances of inline function formal
14365 parameters which are never referenced. This really shouldn't be
14366 happening. All PARM_DECL nodes should get valid non-NULL
14367 DECL_INCOMING_RTL values. FIXME. */
14369 /* Use DECL_RTL as the "location" unless we find something better. */
14370 rtl = DECL_RTL_IF_SET (decl);
14372 /* When generating abstract instances, ignore everything except
14373 constants, symbols living in memory, and symbols living in
14374 fixed registers. */
14375 if (! reload_completed)
14377 if (rtl
14378 && (CONSTANT_P (rtl)
14379 || (MEM_P (rtl)
14380 && CONSTANT_P (XEXP (rtl, 0)))
14381 || (REG_P (rtl)
14382 && TREE_CODE (decl) == VAR_DECL
14383 && TREE_STATIC (decl))))
14385 rtl = targetm.delegitimize_address (rtl);
14386 return rtl;
14388 rtl = NULL_RTX;
14390 else if (TREE_CODE (decl) == PARM_DECL)
14392 if (rtl == NULL_RTX
14393 || is_pseudo_reg (rtl)
14394 || (MEM_P (rtl)
14395 && is_pseudo_reg (XEXP (rtl, 0))
14396 && DECL_INCOMING_RTL (decl)
14397 && MEM_P (DECL_INCOMING_RTL (decl))
14398 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
14400 tree declared_type = TREE_TYPE (decl);
14401 tree passed_type = DECL_ARG_TYPE (decl);
14402 enum machine_mode dmode = TYPE_MODE (declared_type);
14403 enum machine_mode pmode = TYPE_MODE (passed_type);
14405 /* This decl represents a formal parameter which was optimized out.
14406 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
14407 all cases where (rtl == NULL_RTX) just below. */
14408 if (dmode == pmode)
14409 rtl = DECL_INCOMING_RTL (decl);
14410 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
14411 && SCALAR_INT_MODE_P (dmode)
14412 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
14413 && DECL_INCOMING_RTL (decl))
14415 rtx inc = DECL_INCOMING_RTL (decl);
14416 if (REG_P (inc))
14417 rtl = inc;
14418 else if (MEM_P (inc))
14420 if (BYTES_BIG_ENDIAN)
14421 rtl = adjust_address_nv (inc, dmode,
14422 GET_MODE_SIZE (pmode)
14423 - GET_MODE_SIZE (dmode));
14424 else
14425 rtl = inc;
14430 /* If the parm was passed in registers, but lives on the stack, then
14431 make a big endian correction if the mode of the type of the
14432 parameter is not the same as the mode of the rtl. */
14433 /* ??? This is the same series of checks that are made in dbxout.c before
14434 we reach the big endian correction code there. It isn't clear if all
14435 of these checks are necessary here, but keeping them all is the safe
14436 thing to do. */
14437 else if (MEM_P (rtl)
14438 && XEXP (rtl, 0) != const0_rtx
14439 && ! CONSTANT_P (XEXP (rtl, 0))
14440 /* Not passed in memory. */
14441 && !MEM_P (DECL_INCOMING_RTL (decl))
14442 /* Not passed by invisible reference. */
14443 && (!REG_P (XEXP (rtl, 0))
14444 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
14445 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
14446 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
14447 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
14448 #endif
14450 /* Big endian correction check. */
14451 && BYTES_BIG_ENDIAN
14452 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
14453 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
14454 < UNITS_PER_WORD))
14456 enum machine_mode addr_mode = get_address_mode (rtl);
14457 int offset = (UNITS_PER_WORD
14458 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
14460 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
14461 plus_constant (addr_mode, XEXP (rtl, 0), offset));
14464 else if (TREE_CODE (decl) == VAR_DECL
14465 && rtl
14466 && MEM_P (rtl)
14467 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
14468 && BYTES_BIG_ENDIAN)
14470 enum machine_mode addr_mode = get_address_mode (rtl);
14471 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
14472 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
14474 /* If a variable is declared "register" yet is smaller than
14475 a register, then if we store the variable to memory, it
14476 looks like we're storing a register-sized value, when in
14477 fact we are not. We need to adjust the offset of the
14478 storage location to reflect the actual value's bytes,
14479 else gdb will not be able to display it. */
14480 if (rsize > dsize)
14481 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
14482 plus_constant (addr_mode, XEXP (rtl, 0),
14483 rsize - dsize));
14486 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
14487 and will have been substituted directly into all expressions that use it.
14488 C does not have such a concept, but C++ and other languages do. */
14489 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
14490 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
14492 if (rtl)
14493 rtl = targetm.delegitimize_address (rtl);
14495 /* If we don't look past the constant pool, we risk emitting a
14496 reference to a constant pool entry that isn't referenced from
14497 code, and thus is not emitted. */
14498 if (rtl)
14499 rtl = avoid_constant_pool_reference (rtl);
14501 /* Try harder to get a rtl. If this symbol ends up not being emitted
14502 in the current CU, resolve_addr will remove the expression referencing
14503 it. */
14504 if (rtl == NULL_RTX
14505 && TREE_CODE (decl) == VAR_DECL
14506 && !DECL_EXTERNAL (decl)
14507 && TREE_STATIC (decl)
14508 && DECL_NAME (decl)
14509 && !DECL_HARD_REGISTER (decl)
14510 && DECL_MODE (decl) != VOIDmode)
14512 rtl = make_decl_rtl_for_debug (decl);
14513 if (!MEM_P (rtl)
14514 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
14515 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
14516 rtl = NULL_RTX;
14519 return rtl;
14522 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
14523 returned. If so, the decl for the COMMON block is returned, and the
14524 value is the offset into the common block for the symbol. */
14526 static tree
14527 fortran_common (tree decl, HOST_WIDE_INT *value)
14529 tree val_expr, cvar;
14530 enum machine_mode mode;
14531 HOST_WIDE_INT bitsize, bitpos;
14532 tree offset;
14533 int unsignedp, volatilep = 0;
14535 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
14536 it does not have a value (the offset into the common area), or if it
14537 is thread local (as opposed to global) then it isn't common, and shouldn't
14538 be handled as such. */
14539 if (TREE_CODE (decl) != VAR_DECL
14540 || !TREE_STATIC (decl)
14541 || !DECL_HAS_VALUE_EXPR_P (decl)
14542 || !is_fortran ())
14543 return NULL_TREE;
14545 val_expr = DECL_VALUE_EXPR (decl);
14546 if (TREE_CODE (val_expr) != COMPONENT_REF)
14547 return NULL_TREE;
14549 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
14550 &mode, &unsignedp, &volatilep, true);
14552 if (cvar == NULL_TREE
14553 || TREE_CODE (cvar) != VAR_DECL
14554 || DECL_ARTIFICIAL (cvar)
14555 || !TREE_PUBLIC (cvar))
14556 return NULL_TREE;
14558 *value = 0;
14559 if (offset != NULL)
14561 if (!host_integerp (offset, 0))
14562 return NULL_TREE;
14563 *value = tree_low_cst (offset, 0);
14565 if (bitpos != 0)
14566 *value += bitpos / BITS_PER_UNIT;
14568 return cvar;
14571 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
14572 data attribute for a variable or a parameter. We generate the
14573 DW_AT_const_value attribute only in those cases where the given variable
14574 or parameter does not have a true "location" either in memory or in a
14575 register. This can happen (for example) when a constant is passed as an
14576 actual argument in a call to an inline function. (It's possible that
14577 these things can crop up in other ways also.) Note that one type of
14578 constant value which can be passed into an inlined function is a constant
14579 pointer. This can happen for example if an actual argument in an inlined
14580 function call evaluates to a compile-time constant address.
14582 CACHE_P is true if it is worth caching the location list for DECL,
14583 so that future calls can reuse it rather than regenerate it from scratch.
14584 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
14585 since we will need to refer to them each time the function is inlined. */
14587 static bool
14588 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
14589 enum dwarf_attribute attr)
14591 rtx rtl;
14592 dw_loc_list_ref list;
14593 var_loc_list *loc_list;
14594 cached_dw_loc_list *cache;
14595 void **slot;
14597 if (TREE_CODE (decl) == ERROR_MARK)
14598 return false;
14600 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
14601 || TREE_CODE (decl) == RESULT_DECL);
14603 /* Try to get some constant RTL for this decl, and use that as the value of
14604 the location. */
14606 rtl = rtl_for_decl_location (decl);
14607 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
14608 && add_const_value_attribute (die, rtl))
14609 return true;
14611 /* See if we have single element location list that is equivalent to
14612 a constant value. That way we are better to use add_const_value_attribute
14613 rather than expanding constant value equivalent. */
14614 loc_list = lookup_decl_loc (decl);
14615 if (loc_list
14616 && loc_list->first
14617 && loc_list->first->next == NULL
14618 && NOTE_P (loc_list->first->loc)
14619 && NOTE_VAR_LOCATION (loc_list->first->loc)
14620 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
14622 struct var_loc_node *node;
14624 node = loc_list->first;
14625 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
14626 if (GET_CODE (rtl) == EXPR_LIST)
14627 rtl = XEXP (rtl, 0);
14628 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
14629 && add_const_value_attribute (die, rtl))
14630 return true;
14632 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
14633 list several times. See if we've already cached the contents. */
14634 list = NULL;
14635 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
14636 cache_p = false;
14637 if (cache_p)
14639 cache = (cached_dw_loc_list *)
14640 htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
14641 if (cache)
14642 list = cache->loc_list;
14644 if (list == NULL)
14646 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
14647 /* It is usually worth caching this result if the decl is from
14648 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
14649 if (cache_p && list && list->dw_loc_next)
14651 slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
14652 DECL_UID (decl), INSERT);
14653 cache = ggc_alloc_cleared_cached_dw_loc_list ();
14654 cache->decl_id = DECL_UID (decl);
14655 cache->loc_list = list;
14656 *slot = cache;
14659 if (list)
14661 add_AT_location_description (die, attr, list);
14662 return true;
14664 /* None of that worked, so it must not really have a location;
14665 try adding a constant value attribute from the DECL_INITIAL. */
14666 return tree_add_const_value_attribute_for_decl (die, decl);
14669 /* Add VARIABLE and DIE into deferred locations list. */
14671 static void
14672 defer_location (tree variable, dw_die_ref die)
14674 deferred_locations entry;
14675 entry.variable = variable;
14676 entry.die = die;
14677 VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
14680 /* Helper function for tree_add_const_value_attribute. Natively encode
14681 initializer INIT into an array. Return true if successful. */
14683 static bool
14684 native_encode_initializer (tree init, unsigned char *array, int size)
14686 tree type;
14688 if (init == NULL_TREE)
14689 return false;
14691 STRIP_NOPS (init);
14692 switch (TREE_CODE (init))
14694 case STRING_CST:
14695 type = TREE_TYPE (init);
14696 if (TREE_CODE (type) == ARRAY_TYPE)
14698 tree enttype = TREE_TYPE (type);
14699 enum machine_mode mode = TYPE_MODE (enttype);
14701 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
14702 return false;
14703 if (int_size_in_bytes (type) != size)
14704 return false;
14705 if (size > TREE_STRING_LENGTH (init))
14707 memcpy (array, TREE_STRING_POINTER (init),
14708 TREE_STRING_LENGTH (init));
14709 memset (array + TREE_STRING_LENGTH (init),
14710 '\0', size - TREE_STRING_LENGTH (init));
14712 else
14713 memcpy (array, TREE_STRING_POINTER (init), size);
14714 return true;
14716 return false;
14717 case CONSTRUCTOR:
14718 type = TREE_TYPE (init);
14719 if (int_size_in_bytes (type) != size)
14720 return false;
14721 if (TREE_CODE (type) == ARRAY_TYPE)
14723 HOST_WIDE_INT min_index;
14724 unsigned HOST_WIDE_INT cnt;
14725 int curpos = 0, fieldsize;
14726 constructor_elt *ce;
14728 if (TYPE_DOMAIN (type) == NULL_TREE
14729 || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
14730 return false;
14732 fieldsize = int_size_in_bytes (TREE_TYPE (type));
14733 if (fieldsize <= 0)
14734 return false;
14736 min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
14737 memset (array, '\0', size);
14738 FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
14740 tree val = ce->value;
14741 tree index = ce->index;
14742 int pos = curpos;
14743 if (index && TREE_CODE (index) == RANGE_EXPR)
14744 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
14745 * fieldsize;
14746 else if (index)
14747 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
14749 if (val)
14751 STRIP_NOPS (val);
14752 if (!native_encode_initializer (val, array + pos, fieldsize))
14753 return false;
14755 curpos = pos + fieldsize;
14756 if (index && TREE_CODE (index) == RANGE_EXPR)
14758 int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
14759 - tree_low_cst (TREE_OPERAND (index, 0), 0);
14760 while (count-- > 0)
14762 if (val)
14763 memcpy (array + curpos, array + pos, fieldsize);
14764 curpos += fieldsize;
14767 gcc_assert (curpos <= size);
14769 return true;
14771 else if (TREE_CODE (type) == RECORD_TYPE
14772 || TREE_CODE (type) == UNION_TYPE)
14774 tree field = NULL_TREE;
14775 unsigned HOST_WIDE_INT cnt;
14776 constructor_elt *ce;
14778 if (int_size_in_bytes (type) != size)
14779 return false;
14781 if (TREE_CODE (type) == RECORD_TYPE)
14782 field = TYPE_FIELDS (type);
14784 FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
14786 tree val = ce->value;
14787 int pos, fieldsize;
14789 if (ce->index != 0)
14790 field = ce->index;
14792 if (val)
14793 STRIP_NOPS (val);
14795 if (field == NULL_TREE || DECL_BIT_FIELD (field))
14796 return false;
14798 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
14799 && TYPE_DOMAIN (TREE_TYPE (field))
14800 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
14801 return false;
14802 else if (DECL_SIZE_UNIT (field) == NULL_TREE
14803 || !host_integerp (DECL_SIZE_UNIT (field), 0))
14804 return false;
14805 fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
14806 pos = int_byte_position (field);
14807 gcc_assert (pos + fieldsize <= size);
14808 if (val
14809 && !native_encode_initializer (val, array + pos, fieldsize))
14810 return false;
14812 return true;
14814 return false;
14815 case VIEW_CONVERT_EXPR:
14816 case NON_LVALUE_EXPR:
14817 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
14818 default:
14819 return native_encode_expr (init, array, size) == size;
14823 /* Attach a DW_AT_const_value attribute to DIE. The value of the
14824 attribute is the const value T. */
14826 static bool
14827 tree_add_const_value_attribute (dw_die_ref die, tree t)
14829 tree init;
14830 tree type = TREE_TYPE (t);
14831 rtx rtl;
14833 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
14834 return false;
14836 init = t;
14837 gcc_assert (!DECL_P (init));
14839 rtl = rtl_for_decl_init (init, type);
14840 if (rtl)
14841 return add_const_value_attribute (die, rtl);
14842 /* If the host and target are sane, try harder. */
14843 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
14844 && initializer_constant_valid_p (init, type))
14846 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
14847 if (size > 0 && (int) size == size)
14849 unsigned char *array = (unsigned char *)
14850 ggc_alloc_cleared_atomic (size);
14852 if (native_encode_initializer (init, array, size))
14854 add_AT_vec (die, DW_AT_const_value, size, 1, array);
14855 return true;
14859 return false;
14862 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
14863 attribute is the const value of T, where T is an integral constant
14864 variable with static storage duration
14865 (so it can't be a PARM_DECL or a RESULT_DECL). */
14867 static bool
14868 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
14871 if (!decl
14872 || (TREE_CODE (decl) != VAR_DECL
14873 && TREE_CODE (decl) != CONST_DECL)
14874 || (TREE_CODE (decl) == VAR_DECL
14875 && !TREE_STATIC (decl)))
14876 return false;
14878 if (TREE_READONLY (decl)
14879 && ! TREE_THIS_VOLATILE (decl)
14880 && DECL_INITIAL (decl))
14881 /* OK */;
14882 else
14883 return false;
14885 /* Don't add DW_AT_const_value if abstract origin already has one. */
14886 if (get_AT (var_die, DW_AT_const_value))
14887 return false;
14889 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
14892 /* Convert the CFI instructions for the current function into a
14893 location list. This is used for DW_AT_frame_base when we targeting
14894 a dwarf2 consumer that does not support the dwarf3
14895 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
14896 expressions. */
14898 static dw_loc_list_ref
14899 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
14901 int ix;
14902 dw_fde_ref fde;
14903 dw_loc_list_ref list, *list_tail;
14904 dw_cfi_ref cfi;
14905 dw_cfa_location last_cfa, next_cfa;
14906 const char *start_label, *last_label, *section;
14907 dw_cfa_location remember;
14909 fde = cfun->fde;
14910 gcc_assert (fde != NULL);
14912 section = secname_for_decl (current_function_decl);
14913 list_tail = &list;
14914 list = NULL;
14916 memset (&next_cfa, 0, sizeof (next_cfa));
14917 next_cfa.reg = INVALID_REGNUM;
14918 remember = next_cfa;
14920 start_label = fde->dw_fde_begin;
14922 /* ??? Bald assumption that the CIE opcode list does not contain
14923 advance opcodes. */
14924 FOR_EACH_VEC_ELT (dw_cfi_ref, cie_cfi_vec, ix, cfi)
14925 lookup_cfa_1 (cfi, &next_cfa, &remember);
14927 last_cfa = next_cfa;
14928 last_label = start_label;
14930 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
14932 /* If the first partition contained no CFI adjustments, the
14933 CIE opcodes apply to the whole first partition. */
14934 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
14935 fde->dw_fde_begin, fde->dw_fde_end, section);
14936 list_tail =&(*list_tail)->dw_loc_next;
14937 start_label = last_label = fde->dw_fde_second_begin;
14940 FOR_EACH_VEC_ELT (dw_cfi_ref, fde->dw_fde_cfi, ix, cfi)
14942 switch (cfi->dw_cfi_opc)
14944 case DW_CFA_set_loc:
14945 case DW_CFA_advance_loc1:
14946 case DW_CFA_advance_loc2:
14947 case DW_CFA_advance_loc4:
14948 if (!cfa_equal_p (&last_cfa, &next_cfa))
14950 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
14951 start_label, last_label, section);
14953 list_tail = &(*list_tail)->dw_loc_next;
14954 last_cfa = next_cfa;
14955 start_label = last_label;
14957 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
14958 break;
14960 case DW_CFA_advance_loc:
14961 /* The encoding is complex enough that we should never emit this. */
14962 gcc_unreachable ();
14964 default:
14965 lookup_cfa_1 (cfi, &next_cfa, &remember);
14966 break;
14968 if (ix + 1 == fde->dw_fde_switch_cfi_index)
14970 if (!cfa_equal_p (&last_cfa, &next_cfa))
14972 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
14973 start_label, last_label, section);
14975 list_tail = &(*list_tail)->dw_loc_next;
14976 last_cfa = next_cfa;
14977 start_label = last_label;
14979 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
14980 start_label, fde->dw_fde_end, section);
14981 list_tail = &(*list_tail)->dw_loc_next;
14982 start_label = last_label = fde->dw_fde_second_begin;
14986 if (!cfa_equal_p (&last_cfa, &next_cfa))
14988 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
14989 start_label, last_label, section);
14990 list_tail = &(*list_tail)->dw_loc_next;
14991 start_label = last_label;
14994 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
14995 start_label,
14996 fde->dw_fde_second_begin
14997 ? fde->dw_fde_second_end : fde->dw_fde_end,
14998 section);
15000 if (list && list->dw_loc_next)
15001 gen_llsym (list);
15003 return list;
15006 /* Compute a displacement from the "steady-state frame pointer" to the
15007 frame base (often the same as the CFA), and store it in
15008 frame_pointer_fb_offset. OFFSET is added to the displacement
15009 before the latter is negated. */
15011 static void
15012 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
15014 rtx reg, elim;
15016 #ifdef FRAME_POINTER_CFA_OFFSET
15017 reg = frame_pointer_rtx;
15018 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
15019 #else
15020 reg = arg_pointer_rtx;
15021 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
15022 #endif
15024 elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
15025 if (GET_CODE (elim) == PLUS)
15027 offset += INTVAL (XEXP (elim, 1));
15028 elim = XEXP (elim, 0);
15031 frame_pointer_fb_offset = -offset;
15033 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
15034 in which to eliminate. This is because it's stack pointer isn't
15035 directly accessible as a register within the ISA. To work around
15036 this, assume that while we cannot provide a proper value for
15037 frame_pointer_fb_offset, we won't need one either. */
15038 frame_pointer_fb_offset_valid
15039 = ((SUPPORTS_STACK_ALIGNMENT
15040 && (elim == hard_frame_pointer_rtx
15041 || elim == stack_pointer_rtx))
15042 || elim == (frame_pointer_needed
15043 ? hard_frame_pointer_rtx
15044 : stack_pointer_rtx));
15047 /* Generate a DW_AT_name attribute given some string value to be included as
15048 the value of the attribute. */
15050 static void
15051 add_name_attribute (dw_die_ref die, const char *name_string)
15053 if (name_string != NULL && *name_string != 0)
15055 if (demangle_name_func)
15056 name_string = (*demangle_name_func) (name_string);
15058 add_AT_string (die, DW_AT_name, name_string);
15062 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
15063 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
15064 of TYPE accordingly.
15066 ??? This is a temporary measure until after we're able to generate
15067 regular DWARF for the complex Ada type system. */
15069 static void
15070 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
15071 dw_die_ref context_die)
15073 tree dtype;
15074 dw_die_ref dtype_die;
15076 if (!lang_hooks.types.descriptive_type)
15077 return;
15079 dtype = lang_hooks.types.descriptive_type (type);
15080 if (!dtype)
15081 return;
15083 dtype_die = lookup_type_die (dtype);
15084 if (!dtype_die)
15086 gen_type_die (dtype, context_die);
15087 dtype_die = lookup_type_die (dtype);
15088 gcc_assert (dtype_die);
15091 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
15094 /* Generate a DW_AT_comp_dir attribute for DIE. */
15096 static void
15097 add_comp_dir_attribute (dw_die_ref die)
15099 const char *wd = get_src_pwd ();
15100 char *wd1;
15102 if (wd == NULL)
15103 return;
15105 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
15107 int wdlen;
15109 wdlen = strlen (wd);
15110 wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
15111 strcpy (wd1, wd);
15112 wd1 [wdlen] = DIR_SEPARATOR;
15113 wd1 [wdlen + 1] = 0;
15114 wd = wd1;
15117 add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
15120 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
15121 default. */
15123 static int
15124 lower_bound_default (void)
15126 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
15128 case DW_LANG_C:
15129 case DW_LANG_C89:
15130 case DW_LANG_C99:
15131 case DW_LANG_C_plus_plus:
15132 case DW_LANG_ObjC:
15133 case DW_LANG_ObjC_plus_plus:
15134 case DW_LANG_Java:
15135 return 0;
15136 case DW_LANG_Fortran77:
15137 case DW_LANG_Fortran90:
15138 case DW_LANG_Fortran95:
15139 return 1;
15140 case DW_LANG_UPC:
15141 case DW_LANG_D:
15142 case DW_LANG_Python:
15143 return dwarf_version >= 4 ? 0 : -1;
15144 case DW_LANG_Ada95:
15145 case DW_LANG_Ada83:
15146 case DW_LANG_Cobol74:
15147 case DW_LANG_Cobol85:
15148 case DW_LANG_Pascal83:
15149 case DW_LANG_Modula2:
15150 case DW_LANG_PLI:
15151 return dwarf_version >= 4 ? 1 : -1;
15152 default:
15153 return -1;
15157 /* Given a tree node describing an array bound (either lower or upper) output
15158 a representation for that bound. */
15160 static void
15161 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
15163 switch (TREE_CODE (bound))
15165 case ERROR_MARK:
15166 return;
15168 /* All fixed-bounds are represented by INTEGER_CST nodes. */
15169 case INTEGER_CST:
15171 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
15172 int dflt;
15174 /* Use the default if possible. */
15175 if (bound_attr == DW_AT_lower_bound
15176 && host_integerp (bound, 0)
15177 && (dflt = lower_bound_default ()) != -1
15178 && tree_low_cst (bound, 0) == dflt)
15181 /* Otherwise represent the bound as an unsigned value with the
15182 precision of its type. The precision and signedness of the
15183 type will be necessary to re-interpret it unambiguously. */
15184 else if (prec < HOST_BITS_PER_WIDE_INT)
15186 unsigned HOST_WIDE_INT mask
15187 = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
15188 add_AT_unsigned (subrange_die, bound_attr,
15189 TREE_INT_CST_LOW (bound) & mask);
15191 else if (prec == HOST_BITS_PER_WIDE_INT
15192 || TREE_INT_CST_HIGH (bound) == 0)
15193 add_AT_unsigned (subrange_die, bound_attr,
15194 TREE_INT_CST_LOW (bound));
15195 else
15196 add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
15197 TREE_INT_CST_LOW (bound));
15199 break;
15201 CASE_CONVERT:
15202 case VIEW_CONVERT_EXPR:
15203 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
15204 break;
15206 case SAVE_EXPR:
15207 break;
15209 case VAR_DECL:
15210 case PARM_DECL:
15211 case RESULT_DECL:
15213 dw_die_ref decl_die = lookup_decl_die (bound);
15215 /* ??? Can this happen, or should the variable have been bound
15216 first? Probably it can, since I imagine that we try to create
15217 the types of parameters in the order in which they exist in
15218 the list, and won't have created a forward reference to a
15219 later parameter. */
15220 if (decl_die != NULL)
15222 add_AT_die_ref (subrange_die, bound_attr, decl_die);
15223 break;
15226 /* FALLTHRU */
15228 default:
15230 /* Otherwise try to create a stack operation procedure to
15231 evaluate the value of the array bound. */
15233 dw_die_ref ctx, decl_die;
15234 dw_loc_list_ref list;
15236 list = loc_list_from_tree (bound, 2);
15237 if (list == NULL || single_element_loc_list_p (list))
15239 /* If DW_AT_*bound is not a reference nor constant, it is
15240 a DWARF expression rather than location description.
15241 For that loc_list_from_tree (bound, 0) is needed.
15242 If that fails to give a single element list,
15243 fall back to outputting this as a reference anyway. */
15244 dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
15245 if (list2 && single_element_loc_list_p (list2))
15247 add_AT_loc (subrange_die, bound_attr, list2->expr);
15248 break;
15251 if (list == NULL)
15252 break;
15254 if (current_function_decl == 0)
15255 ctx = comp_unit_die ();
15256 else
15257 ctx = lookup_decl_die (current_function_decl);
15259 decl_die = new_die (DW_TAG_variable, ctx, bound);
15260 add_AT_flag (decl_die, DW_AT_artificial, 1);
15261 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
15262 add_AT_location_description (decl_die, DW_AT_location, list);
15263 add_AT_die_ref (subrange_die, bound_attr, decl_die);
15264 break;
15269 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
15270 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
15271 Note that the block of subscript information for an array type also
15272 includes information about the element type of the given array type. */
15274 static void
15275 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
15277 unsigned dimension_number;
15278 tree lower, upper;
15279 dw_die_ref subrange_die;
15281 for (dimension_number = 0;
15282 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
15283 type = TREE_TYPE (type), dimension_number++)
15285 tree domain = TYPE_DOMAIN (type);
15287 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
15288 break;
15290 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
15291 and (in GNU C only) variable bounds. Handle all three forms
15292 here. */
15293 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
15294 if (domain)
15296 /* We have an array type with specified bounds. */
15297 lower = TYPE_MIN_VALUE (domain);
15298 upper = TYPE_MAX_VALUE (domain);
15300 /* Define the index type. */
15301 if (TREE_TYPE (domain))
15303 /* ??? This is probably an Ada unnamed subrange type. Ignore the
15304 TREE_TYPE field. We can't emit debug info for this
15305 because it is an unnamed integral type. */
15306 if (TREE_CODE (domain) == INTEGER_TYPE
15307 && TYPE_NAME (domain) == NULL_TREE
15308 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
15309 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
15311 else
15312 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
15313 type_die);
15316 /* ??? If upper is NULL, the array has unspecified length,
15317 but it does have a lower bound. This happens with Fortran
15318 dimension arr(N:*)
15319 Since the debugger is definitely going to need to know N
15320 to produce useful results, go ahead and output the lower
15321 bound solo, and hope the debugger can cope. */
15323 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
15324 if (upper)
15325 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
15328 /* Otherwise we have an array type with an unspecified length. The
15329 DWARF-2 spec does not say how to handle this; let's just leave out the
15330 bounds. */
15334 static void
15335 add_byte_size_attribute (dw_die_ref die, tree tree_node)
15337 dw_die_ref decl_die;
15338 unsigned size;
15340 switch (TREE_CODE (tree_node))
15342 case ERROR_MARK:
15343 size = 0;
15344 break;
15345 case ENUMERAL_TYPE:
15346 case RECORD_TYPE:
15347 case UNION_TYPE:
15348 case QUAL_UNION_TYPE:
15349 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
15350 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
15352 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
15353 return;
15355 size = int_size_in_bytes (tree_node);
15356 break;
15357 case FIELD_DECL:
15358 /* For a data member of a struct or union, the DW_AT_byte_size is
15359 generally given as the number of bytes normally allocated for an
15360 object of the *declared* type of the member itself. This is true
15361 even for bit-fields. */
15362 size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
15363 break;
15364 default:
15365 gcc_unreachable ();
15368 /* Note that `size' might be -1 when we get to this point. If it is, that
15369 indicates that the byte size of the entity in question is variable. We
15370 have no good way of expressing this fact in Dwarf at the present time,
15371 so just let the -1 pass on through. */
15372 add_AT_unsigned (die, DW_AT_byte_size, size);
15375 /* For a FIELD_DECL node which represents a bit-field, output an attribute
15376 which specifies the distance in bits from the highest order bit of the
15377 "containing object" for the bit-field to the highest order bit of the
15378 bit-field itself.
15380 For any given bit-field, the "containing object" is a hypothetical object
15381 (of some integral or enum type) within which the given bit-field lives. The
15382 type of this hypothetical "containing object" is always the same as the
15383 declared type of the individual bit-field itself. The determination of the
15384 exact location of the "containing object" for a bit-field is rather
15385 complicated. It's handled by the `field_byte_offset' function (above).
15387 Note that it is the size (in bytes) of the hypothetical "containing object"
15388 which will be given in the DW_AT_byte_size attribute for this bit-field.
15389 (See `byte_size_attribute' above). */
15391 static inline void
15392 add_bit_offset_attribute (dw_die_ref die, tree decl)
15394 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
15395 tree type = DECL_BIT_FIELD_TYPE (decl);
15396 HOST_WIDE_INT bitpos_int;
15397 HOST_WIDE_INT highest_order_object_bit_offset;
15398 HOST_WIDE_INT highest_order_field_bit_offset;
15399 HOST_WIDE_INT bit_offset;
15401 /* Must be a field and a bit field. */
15402 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
15404 /* We can't yet handle bit-fields whose offsets are variable, so if we
15405 encounter such things, just return without generating any attribute
15406 whatsoever. Likewise for variable or too large size. */
15407 if (! host_integerp (bit_position (decl), 0)
15408 || ! host_integerp (DECL_SIZE (decl), 1))
15409 return;
15411 bitpos_int = int_bit_position (decl);
15413 /* Note that the bit offset is always the distance (in bits) from the
15414 highest-order bit of the "containing object" to the highest-order bit of
15415 the bit-field itself. Since the "high-order end" of any object or field
15416 is different on big-endian and little-endian machines, the computation
15417 below must take account of these differences. */
15418 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
15419 highest_order_field_bit_offset = bitpos_int;
15421 if (! BYTES_BIG_ENDIAN)
15423 highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
15424 highest_order_object_bit_offset += simple_type_size_in_bits (type);
15427 bit_offset
15428 = (! BYTES_BIG_ENDIAN
15429 ? highest_order_object_bit_offset - highest_order_field_bit_offset
15430 : highest_order_field_bit_offset - highest_order_object_bit_offset);
15432 if (bit_offset < 0)
15433 add_AT_int (die, DW_AT_bit_offset, bit_offset);
15434 else
15435 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
15438 /* For a FIELD_DECL node which represents a bit field, output an attribute
15439 which specifies the length in bits of the given field. */
15441 static inline void
15442 add_bit_size_attribute (dw_die_ref die, tree decl)
15444 /* Must be a field and a bit field. */
15445 gcc_assert (TREE_CODE (decl) == FIELD_DECL
15446 && DECL_BIT_FIELD_TYPE (decl));
15448 if (host_integerp (DECL_SIZE (decl), 1))
15449 add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
15452 /* If the compiled language is ANSI C, then add a 'prototyped'
15453 attribute, if arg types are given for the parameters of a function. */
15455 static inline void
15456 add_prototyped_attribute (dw_die_ref die, tree func_type)
15458 if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
15459 && prototype_p (func_type))
15460 add_AT_flag (die, DW_AT_prototyped, 1);
15463 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
15464 by looking in either the type declaration or object declaration
15465 equate table. */
15467 static inline dw_die_ref
15468 add_abstract_origin_attribute (dw_die_ref die, tree origin)
15470 dw_die_ref origin_die = NULL;
15472 if (TREE_CODE (origin) != FUNCTION_DECL)
15474 /* We may have gotten separated from the block for the inlined
15475 function, if we're in an exception handler or some such; make
15476 sure that the abstract function has been written out.
15478 Doing this for nested functions is wrong, however; functions are
15479 distinct units, and our context might not even be inline. */
15480 tree fn = origin;
15482 if (TYPE_P (fn))
15483 fn = TYPE_STUB_DECL (fn);
15485 fn = decl_function_context (fn);
15486 if (fn)
15487 dwarf2out_abstract_function (fn);
15490 if (DECL_P (origin))
15491 origin_die = lookup_decl_die (origin);
15492 else if (TYPE_P (origin))
15493 origin_die = lookup_type_die (origin);
15495 /* XXX: Functions that are never lowered don't always have correct block
15496 trees (in the case of java, they simply have no block tree, in some other
15497 languages). For these functions, there is nothing we can really do to
15498 output correct debug info for inlined functions in all cases. Rather
15499 than die, we'll just produce deficient debug info now, in that we will
15500 have variables without a proper abstract origin. In the future, when all
15501 functions are lowered, we should re-add a gcc_assert (origin_die)
15502 here. */
15504 if (origin_die)
15505 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
15506 return origin_die;
15509 /* We do not currently support the pure_virtual attribute. */
15511 static inline void
15512 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
15514 if (DECL_VINDEX (func_decl))
15516 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
15518 if (host_integerp (DECL_VINDEX (func_decl), 0))
15519 add_AT_loc (die, DW_AT_vtable_elem_location,
15520 new_loc_descr (DW_OP_constu,
15521 tree_low_cst (DECL_VINDEX (func_decl), 0),
15522 0));
15524 /* GNU extension: Record what type this method came from originally. */
15525 if (debug_info_level > DINFO_LEVEL_TERSE
15526 && DECL_CONTEXT (func_decl))
15527 add_AT_die_ref (die, DW_AT_containing_type,
15528 lookup_type_die (DECL_CONTEXT (func_decl)));
15532 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
15533 given decl. This used to be a vendor extension until after DWARF 4
15534 standardized it. */
15536 static void
15537 add_linkage_attr (dw_die_ref die, tree decl)
15539 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
15541 /* Mimic what assemble_name_raw does with a leading '*'. */
15542 if (name[0] == '*')
15543 name = &name[1];
15545 if (dwarf_version >= 4)
15546 add_AT_string (die, DW_AT_linkage_name, name);
15547 else
15548 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
15551 /* Add source coordinate attributes for the given decl. */
15553 static void
15554 add_src_coords_attributes (dw_die_ref die, tree decl)
15556 expanded_location s;
15558 if (DECL_SOURCE_LOCATION (decl) == UNKNOWN_LOCATION)
15559 return;
15560 s = expand_location (DECL_SOURCE_LOCATION (decl));
15561 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
15562 add_AT_unsigned (die, DW_AT_decl_line, s.line);
15565 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
15567 static void
15568 add_linkage_name (dw_die_ref die, tree decl)
15570 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
15571 && TREE_PUBLIC (decl)
15572 && !DECL_ABSTRACT (decl)
15573 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
15574 && die->die_tag != DW_TAG_member)
15576 /* Defer until we have an assembler name set. */
15577 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
15579 limbo_die_node *asm_name;
15581 asm_name = ggc_alloc_cleared_limbo_die_node ();
15582 asm_name->die = die;
15583 asm_name->created_for = decl;
15584 asm_name->next = deferred_asm_name;
15585 deferred_asm_name = asm_name;
15587 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
15588 add_linkage_attr (die, decl);
15592 /* Add a DW_AT_name attribute and source coordinate attribute for the
15593 given decl, but only if it actually has a name. */
15595 static void
15596 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
15598 tree decl_name;
15600 decl_name = DECL_NAME (decl);
15601 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
15603 const char *name = dwarf2_name (decl, 0);
15604 if (name)
15605 add_name_attribute (die, name);
15606 if (! DECL_ARTIFICIAL (decl))
15607 add_src_coords_attributes (die, decl);
15609 add_linkage_name (die, decl);
15612 #ifdef VMS_DEBUGGING_INFO
15613 /* Get the function's name, as described by its RTL. This may be different
15614 from the DECL_NAME name used in the source file. */
15615 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
15617 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
15618 XEXP (DECL_RTL (decl), 0));
15619 VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
15621 #endif /* VMS_DEBUGGING_INFO */
15624 #ifdef VMS_DEBUGGING_INFO
15625 /* Output the debug main pointer die for VMS */
15627 void
15628 dwarf2out_vms_debug_main_pointer (void)
15630 char label[MAX_ARTIFICIAL_LABEL_BYTES];
15631 dw_die_ref die;
15633 /* Allocate the VMS debug main subprogram die. */
15634 die = ggc_alloc_cleared_die_node ();
15635 die->die_tag = DW_TAG_subprogram;
15636 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
15637 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
15638 current_function_funcdef_no);
15639 add_AT_lbl_id (die, DW_AT_entry_pc, label);
15641 /* Make it the first child of comp_unit_die (). */
15642 die->die_parent = comp_unit_die ();
15643 if (comp_unit_die ()->die_child)
15645 die->die_sib = comp_unit_die ()->die_child->die_sib;
15646 comp_unit_die ()->die_child->die_sib = die;
15648 else
15650 die->die_sib = die;
15651 comp_unit_die ()->die_child = die;
15654 #endif /* VMS_DEBUGGING_INFO */
15656 /* Push a new declaration scope. */
15658 static void
15659 push_decl_scope (tree scope)
15661 VEC_safe_push (tree, gc, decl_scope_table, scope);
15664 /* Pop a declaration scope. */
15666 static inline void
15667 pop_decl_scope (void)
15669 VEC_pop (tree, decl_scope_table);
15672 /* walk_tree helper function for uses_local_type, below. */
15674 static tree
15675 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
15677 if (!TYPE_P (*tp))
15678 *walk_subtrees = 0;
15679 else
15681 tree name = TYPE_NAME (*tp);
15682 if (name && DECL_P (name) && decl_function_context (name))
15683 return *tp;
15685 return NULL_TREE;
15688 /* If TYPE involves a function-local type (including a local typedef to a
15689 non-local type), returns that type; otherwise returns NULL_TREE. */
15691 static tree
15692 uses_local_type (tree type)
15694 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
15695 return used;
15698 /* Return the DIE for the scope that immediately contains this type.
15699 Non-named types that do not involve a function-local type get global
15700 scope. Named types nested in namespaces or other types get their
15701 containing scope. All other types (i.e. function-local named types) get
15702 the current active scope. */
15704 static dw_die_ref
15705 scope_die_for (tree t, dw_die_ref context_die)
15707 dw_die_ref scope_die = NULL;
15708 tree containing_scope;
15710 /* Non-types always go in the current scope. */
15711 gcc_assert (TYPE_P (t));
15713 /* Use the scope of the typedef, rather than the scope of the type
15714 it refers to. */
15715 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
15716 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
15717 else
15718 containing_scope = TYPE_CONTEXT (t);
15720 /* Use the containing namespace if there is one. */
15721 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
15723 if (context_die == lookup_decl_die (containing_scope))
15724 /* OK */;
15725 else if (debug_info_level > DINFO_LEVEL_TERSE)
15726 context_die = get_context_die (containing_scope);
15727 else
15728 containing_scope = NULL_TREE;
15731 /* Ignore function type "scopes" from the C frontend. They mean that
15732 a tagged type is local to a parmlist of a function declarator, but
15733 that isn't useful to DWARF. */
15734 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
15735 containing_scope = NULL_TREE;
15737 if (SCOPE_FILE_SCOPE_P (containing_scope))
15739 /* If T uses a local type keep it local as well, to avoid references
15740 to function-local DIEs from outside the function. */
15741 if (current_function_decl && uses_local_type (t))
15742 scope_die = context_die;
15743 else
15744 scope_die = comp_unit_die ();
15746 else if (TYPE_P (containing_scope))
15748 /* For types, we can just look up the appropriate DIE. */
15749 if (debug_info_level > DINFO_LEVEL_TERSE)
15750 scope_die = get_context_die (containing_scope);
15751 else
15753 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
15754 if (scope_die == NULL)
15755 scope_die = comp_unit_die ();
15758 else
15759 scope_die = context_die;
15761 return scope_die;
15764 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
15766 static inline int
15767 local_scope_p (dw_die_ref context_die)
15769 for (; context_die; context_die = context_die->die_parent)
15770 if (context_die->die_tag == DW_TAG_inlined_subroutine
15771 || context_die->die_tag == DW_TAG_subprogram)
15772 return 1;
15774 return 0;
15777 /* Returns nonzero if CONTEXT_DIE is a class. */
15779 static inline int
15780 class_scope_p (dw_die_ref context_die)
15782 return (context_die
15783 && (context_die->die_tag == DW_TAG_structure_type
15784 || context_die->die_tag == DW_TAG_class_type
15785 || context_die->die_tag == DW_TAG_interface_type
15786 || context_die->die_tag == DW_TAG_union_type));
15789 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
15790 whether or not to treat a DIE in this context as a declaration. */
15792 static inline int
15793 class_or_namespace_scope_p (dw_die_ref context_die)
15795 return (class_scope_p (context_die)
15796 || (context_die && context_die->die_tag == DW_TAG_namespace));
15799 /* Many forms of DIEs require a "type description" attribute. This
15800 routine locates the proper "type descriptor" die for the type given
15801 by 'type', and adds a DW_AT_type attribute below the given die. */
15803 static void
15804 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
15805 int decl_volatile, dw_die_ref context_die)
15807 enum tree_code code = TREE_CODE (type);
15808 dw_die_ref type_die = NULL;
15810 /* ??? If this type is an unnamed subrange type of an integral, floating-point
15811 or fixed-point type, use the inner type. This is because we have no
15812 support for unnamed types in base_type_die. This can happen if this is
15813 an Ada subrange type. Correct solution is emit a subrange type die. */
15814 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
15815 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
15816 type = TREE_TYPE (type), code = TREE_CODE (type);
15818 if (code == ERROR_MARK
15819 /* Handle a special case. For functions whose return type is void, we
15820 generate *no* type attribute. (Note that no object may have type
15821 `void', so this only applies to function return types). */
15822 || code == VOID_TYPE)
15823 return;
15825 type_die = modified_type_die (type,
15826 decl_const || TYPE_READONLY (type),
15827 decl_volatile || TYPE_VOLATILE (type),
15828 context_die);
15830 if (type_die != NULL)
15831 add_AT_die_ref (object_die, DW_AT_type, type_die);
15834 /* Given an object die, add the calling convention attribute for the
15835 function call type. */
15836 static void
15837 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
15839 enum dwarf_calling_convention value = DW_CC_normal;
15841 value = ((enum dwarf_calling_convention)
15842 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
15844 if (is_fortran ()
15845 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
15847 /* DWARF 2 doesn't provide a way to identify a program's source-level
15848 entry point. DW_AT_calling_convention attributes are only meant
15849 to describe functions' calling conventions. However, lacking a
15850 better way to signal the Fortran main program, we used this for
15851 a long time, following existing custom. Now, DWARF 4 has
15852 DW_AT_main_subprogram, which we add below, but some tools still
15853 rely on the old way, which we thus keep. */
15854 value = DW_CC_program;
15856 if (dwarf_version >= 4 || !dwarf_strict)
15857 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
15860 /* Only add the attribute if the backend requests it, and
15861 is not DW_CC_normal. */
15862 if (value && (value != DW_CC_normal))
15863 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
15866 /* Given a tree pointer to a struct, class, union, or enum type node, return
15867 a pointer to the (string) tag name for the given type, or zero if the type
15868 was declared without a tag. */
15870 static const char *
15871 type_tag (const_tree type)
15873 const char *name = 0;
15875 if (TYPE_NAME (type) != 0)
15877 tree t = 0;
15879 /* Find the IDENTIFIER_NODE for the type name. */
15880 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
15881 && !TYPE_NAMELESS (type))
15882 t = TYPE_NAME (type);
15884 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
15885 a TYPE_DECL node, regardless of whether or not a `typedef' was
15886 involved. */
15887 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
15888 && ! DECL_IGNORED_P (TYPE_NAME (type)))
15890 /* We want to be extra verbose. Don't call dwarf_name if
15891 DECL_NAME isn't set. The default hook for decl_printable_name
15892 doesn't like that, and in this context it's correct to return
15893 0, instead of "<anonymous>" or the like. */
15894 if (DECL_NAME (TYPE_NAME (type))
15895 && !DECL_NAMELESS (TYPE_NAME (type)))
15896 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
15899 /* Now get the name as a string, or invent one. */
15900 if (!name && t != 0)
15901 name = IDENTIFIER_POINTER (t);
15904 return (name == 0 || *name == '\0') ? 0 : name;
15907 /* Return the type associated with a data member, make a special check
15908 for bit field types. */
15910 static inline tree
15911 member_declared_type (const_tree member)
15913 return (DECL_BIT_FIELD_TYPE (member)
15914 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
15917 /* Get the decl's label, as described by its RTL. This may be different
15918 from the DECL_NAME name used in the source file. */
15920 #if 0
15921 static const char *
15922 decl_start_label (tree decl)
15924 rtx x;
15925 const char *fnname;
15927 x = DECL_RTL (decl);
15928 gcc_assert (MEM_P (x));
15930 x = XEXP (x, 0);
15931 gcc_assert (GET_CODE (x) == SYMBOL_REF);
15933 fnname = XSTR (x, 0);
15934 return fnname;
15936 #endif
15938 /* These routines generate the internal representation of the DIE's for
15939 the compilation unit. Debugging information is collected by walking
15940 the declaration trees passed in from dwarf2out_decl(). */
15942 static void
15943 gen_array_type_die (tree type, dw_die_ref context_die)
15945 dw_die_ref scope_die = scope_die_for (type, context_die);
15946 dw_die_ref array_die;
15948 /* GNU compilers represent multidimensional array types as sequences of one
15949 dimensional array types whose element types are themselves array types.
15950 We sometimes squish that down to a single array_type DIE with multiple
15951 subscripts in the Dwarf debugging info. The draft Dwarf specification
15952 say that we are allowed to do this kind of compression in C, because
15953 there is no difference between an array of arrays and a multidimensional
15954 array. We don't do this for Ada to remain as close as possible to the
15955 actual representation, which is especially important against the language
15956 flexibilty wrt arrays of variable size. */
15958 bool collapse_nested_arrays = !is_ada ();
15959 tree element_type;
15961 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
15962 DW_TAG_string_type doesn't have DW_AT_type attribute). */
15963 if (TYPE_STRING_FLAG (type)
15964 && TREE_CODE (type) == ARRAY_TYPE
15965 && is_fortran ()
15966 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
15968 HOST_WIDE_INT size;
15970 array_die = new_die (DW_TAG_string_type, scope_die, type);
15971 add_name_attribute (array_die, type_tag (type));
15972 equate_type_number_to_die (type, array_die);
15973 size = int_size_in_bytes (type);
15974 if (size >= 0)
15975 add_AT_unsigned (array_die, DW_AT_byte_size, size);
15976 else if (TYPE_DOMAIN (type) != NULL_TREE
15977 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
15978 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
15980 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
15981 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
15983 size = int_size_in_bytes (TREE_TYPE (szdecl));
15984 if (loc && size > 0)
15986 add_AT_location_description (array_die, DW_AT_string_length, loc);
15987 if (size != DWARF2_ADDR_SIZE)
15988 add_AT_unsigned (array_die, DW_AT_byte_size, size);
15991 return;
15994 array_die = new_die (DW_TAG_array_type, scope_die, type);
15995 add_name_attribute (array_die, type_tag (type));
15996 equate_type_number_to_die (type, array_die);
15998 if (TREE_CODE (type) == VECTOR_TYPE)
15999 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
16001 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
16002 if (is_fortran ()
16003 && TREE_CODE (type) == ARRAY_TYPE
16004 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
16005 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
16006 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16008 #if 0
16009 /* We default the array ordering. SDB will probably do
16010 the right things even if DW_AT_ordering is not present. It's not even
16011 an issue until we start to get into multidimensional arrays anyway. If
16012 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
16013 then we'll have to put the DW_AT_ordering attribute back in. (But if
16014 and when we find out that we need to put these in, we will only do so
16015 for multidimensional arrays. */
16016 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
16017 #endif
16019 if (TREE_CODE (type) == VECTOR_TYPE)
16021 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
16022 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
16023 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
16024 add_bound_info (subrange_die, DW_AT_upper_bound,
16025 size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
16027 else
16028 add_subscript_info (array_die, type, collapse_nested_arrays);
16030 /* Add representation of the type of the elements of this array type and
16031 emit the corresponding DIE if we haven't done it already. */
16032 element_type = TREE_TYPE (type);
16033 if (collapse_nested_arrays)
16034 while (TREE_CODE (element_type) == ARRAY_TYPE)
16036 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
16037 break;
16038 element_type = TREE_TYPE (element_type);
16041 add_type_attribute (array_die, element_type, 0, 0, context_die);
16043 add_gnat_descriptive_type_attribute (array_die, type, context_die);
16044 if (TYPE_ARTIFICIAL (type))
16045 add_AT_flag (array_die, DW_AT_artificial, 1);
16047 if (get_AT (array_die, DW_AT_name))
16048 add_pubtype (type, array_die);
16051 static dw_loc_descr_ref
16052 descr_info_loc (tree val, tree base_decl)
16054 HOST_WIDE_INT size;
16055 dw_loc_descr_ref loc, loc2;
16056 enum dwarf_location_atom op;
16058 if (val == base_decl)
16059 return new_loc_descr (DW_OP_push_object_address, 0, 0);
16061 switch (TREE_CODE (val))
16063 CASE_CONVERT:
16064 return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16065 case VAR_DECL:
16066 return loc_descriptor_from_tree (val, 0);
16067 case INTEGER_CST:
16068 if (host_integerp (val, 0))
16069 return int_loc_descriptor (tree_low_cst (val, 0));
16070 break;
16071 case INDIRECT_REF:
16072 size = int_size_in_bytes (TREE_TYPE (val));
16073 if (size < 0)
16074 break;
16075 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16076 if (!loc)
16077 break;
16078 if (size == DWARF2_ADDR_SIZE)
16079 add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
16080 else
16081 add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
16082 return loc;
16083 case POINTER_PLUS_EXPR:
16084 case PLUS_EXPR:
16085 if (host_integerp (TREE_OPERAND (val, 1), 1)
16086 && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
16087 < 16384)
16089 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16090 if (!loc)
16091 break;
16092 loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
16094 else
16096 op = DW_OP_plus;
16097 do_binop:
16098 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16099 if (!loc)
16100 break;
16101 loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
16102 if (!loc2)
16103 break;
16104 add_loc_descr (&loc, loc2);
16105 add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
16107 return loc;
16108 case MINUS_EXPR:
16109 op = DW_OP_minus;
16110 goto do_binop;
16111 case MULT_EXPR:
16112 op = DW_OP_mul;
16113 goto do_binop;
16114 case EQ_EXPR:
16115 op = DW_OP_eq;
16116 goto do_binop;
16117 case NE_EXPR:
16118 op = DW_OP_ne;
16119 goto do_binop;
16120 default:
16121 break;
16123 return NULL;
16126 static void
16127 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
16128 tree val, tree base_decl)
16130 dw_loc_descr_ref loc;
16132 if (host_integerp (val, 0))
16134 add_AT_unsigned (die, attr, tree_low_cst (val, 0));
16135 return;
16138 loc = descr_info_loc (val, base_decl);
16139 if (!loc)
16140 return;
16142 add_AT_loc (die, attr, loc);
16145 /* This routine generates DIE for array with hidden descriptor, details
16146 are filled into *info by a langhook. */
16148 static void
16149 gen_descr_array_type_die (tree type, struct array_descr_info *info,
16150 dw_die_ref context_die)
16152 dw_die_ref scope_die = scope_die_for (type, context_die);
16153 dw_die_ref array_die;
16154 int dim;
16156 array_die = new_die (DW_TAG_array_type, scope_die, type);
16157 add_name_attribute (array_die, type_tag (type));
16158 equate_type_number_to_die (type, array_die);
16160 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
16161 if (is_fortran ()
16162 && info->ndimensions >= 2)
16163 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16165 if (info->data_location)
16166 add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
16167 info->base_decl);
16168 if (info->associated)
16169 add_descr_info_field (array_die, DW_AT_associated, info->associated,
16170 info->base_decl);
16171 if (info->allocated)
16172 add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
16173 info->base_decl);
16175 for (dim = 0; dim < info->ndimensions; dim++)
16177 dw_die_ref subrange_die
16178 = new_die (DW_TAG_subrange_type, array_die, NULL);
16180 if (info->dimen[dim].lower_bound)
16182 /* If it is the default value, omit it. */
16183 int dflt;
16185 if (host_integerp (info->dimen[dim].lower_bound, 0)
16186 && (dflt = lower_bound_default ()) != -1
16187 && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
16189 else
16190 add_descr_info_field (subrange_die, DW_AT_lower_bound,
16191 info->dimen[dim].lower_bound,
16192 info->base_decl);
16194 if (info->dimen[dim].upper_bound)
16195 add_descr_info_field (subrange_die, DW_AT_upper_bound,
16196 info->dimen[dim].upper_bound,
16197 info->base_decl);
16198 if (info->dimen[dim].stride)
16199 add_descr_info_field (subrange_die, DW_AT_byte_stride,
16200 info->dimen[dim].stride,
16201 info->base_decl);
16204 gen_type_die (info->element_type, context_die);
16205 add_type_attribute (array_die, info->element_type, 0, 0, context_die);
16207 if (get_AT (array_die, DW_AT_name))
16208 add_pubtype (type, array_die);
16211 #if 0
16212 static void
16213 gen_entry_point_die (tree decl, dw_die_ref context_die)
16215 tree origin = decl_ultimate_origin (decl);
16216 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
16218 if (origin != NULL)
16219 add_abstract_origin_attribute (decl_die, origin);
16220 else
16222 add_name_and_src_coords_attributes (decl_die, decl);
16223 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
16224 0, 0, context_die);
16227 if (DECL_ABSTRACT (decl))
16228 equate_decl_number_to_die (decl, decl_die);
16229 else
16230 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
16232 #endif
16234 /* Walk through the list of incomplete types again, trying once more to
16235 emit full debugging info for them. */
16237 static void
16238 retry_incomplete_types (void)
16240 int i;
16242 for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
16243 if (should_emit_struct_debug (VEC_index (tree, incomplete_types, i),
16244 DINFO_USAGE_DIR_USE))
16245 gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die ());
16248 /* Determine what tag to use for a record type. */
16250 static enum dwarf_tag
16251 record_type_tag (tree type)
16253 if (! lang_hooks.types.classify_record)
16254 return DW_TAG_structure_type;
16256 switch (lang_hooks.types.classify_record (type))
16258 case RECORD_IS_STRUCT:
16259 return DW_TAG_structure_type;
16261 case RECORD_IS_CLASS:
16262 return DW_TAG_class_type;
16264 case RECORD_IS_INTERFACE:
16265 if (dwarf_version >= 3 || !dwarf_strict)
16266 return DW_TAG_interface_type;
16267 return DW_TAG_structure_type;
16269 default:
16270 gcc_unreachable ();
16274 /* Generate a DIE to represent an enumeration type. Note that these DIEs
16275 include all of the information about the enumeration values also. Each
16276 enumerated type name/value is listed as a child of the enumerated type
16277 DIE. */
16279 static dw_die_ref
16280 gen_enumeration_type_die (tree type, dw_die_ref context_die)
16282 dw_die_ref type_die = lookup_type_die (type);
16284 if (type_die == NULL)
16286 type_die = new_die (DW_TAG_enumeration_type,
16287 scope_die_for (type, context_die), type);
16288 equate_type_number_to_die (type, type_die);
16289 add_name_attribute (type_die, type_tag (type));
16290 if (dwarf_version >= 4 || !dwarf_strict)
16292 if (ENUM_IS_SCOPED (type))
16293 add_AT_flag (type_die, DW_AT_enum_class, 1);
16294 if (ENUM_IS_OPAQUE (type))
16295 add_AT_flag (type_die, DW_AT_declaration, 1);
16298 else if (! TYPE_SIZE (type))
16299 return type_die;
16300 else
16301 remove_AT (type_die, DW_AT_declaration);
16303 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
16304 given enum type is incomplete, do not generate the DW_AT_byte_size
16305 attribute or the DW_AT_element_list attribute. */
16306 if (TYPE_SIZE (type))
16308 tree link;
16310 TREE_ASM_WRITTEN (type) = 1;
16311 add_byte_size_attribute (type_die, type);
16312 if (TYPE_STUB_DECL (type) != NULL_TREE)
16314 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
16315 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
16318 /* If the first reference to this type was as the return type of an
16319 inline function, then it may not have a parent. Fix this now. */
16320 if (type_die->die_parent == NULL)
16321 add_child_die (scope_die_for (type, context_die), type_die);
16323 for (link = TYPE_VALUES (type);
16324 link != NULL; link = TREE_CHAIN (link))
16326 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
16327 tree value = TREE_VALUE (link);
16329 add_name_attribute (enum_die,
16330 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
16332 if (TREE_CODE (value) == CONST_DECL)
16333 value = DECL_INITIAL (value);
16335 if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
16336 /* DWARF2 does not provide a way of indicating whether or
16337 not enumeration constants are signed or unsigned. GDB
16338 always assumes the values are signed, so we output all
16339 values as if they were signed. That means that
16340 enumeration constants with very large unsigned values
16341 will appear to have negative values in the debugger. */
16342 add_AT_int (enum_die, DW_AT_const_value,
16343 tree_low_cst (value, tree_int_cst_sgn (value) > 0));
16346 add_gnat_descriptive_type_attribute (type_die, type, context_die);
16347 if (TYPE_ARTIFICIAL (type))
16348 add_AT_flag (type_die, DW_AT_artificial, 1);
16350 else
16351 add_AT_flag (type_die, DW_AT_declaration, 1);
16353 add_pubtype (type, type_die);
16355 return type_die;
16358 /* Generate a DIE to represent either a real live formal parameter decl or to
16359 represent just the type of some formal parameter position in some function
16360 type.
16362 Note that this routine is a bit unusual because its argument may be a
16363 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
16364 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
16365 node. If it's the former then this function is being called to output a
16366 DIE to represent a formal parameter object (or some inlining thereof). If
16367 it's the latter, then this function is only being called to output a
16368 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
16369 argument type of some subprogram type.
16370 If EMIT_NAME_P is true, name and source coordinate attributes
16371 are emitted. */
16373 static dw_die_ref
16374 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
16375 dw_die_ref context_die)
16377 tree node_or_origin = node ? node : origin;
16378 tree ultimate_origin;
16379 dw_die_ref parm_die
16380 = new_die (DW_TAG_formal_parameter, context_die, node);
16382 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
16384 case tcc_declaration:
16385 ultimate_origin = decl_ultimate_origin (node_or_origin);
16386 if (node || ultimate_origin)
16387 origin = ultimate_origin;
16388 if (origin != NULL)
16389 add_abstract_origin_attribute (parm_die, origin);
16390 else if (emit_name_p)
16391 add_name_and_src_coords_attributes (parm_die, node);
16392 if (origin == NULL
16393 || (! DECL_ABSTRACT (node_or_origin)
16394 && variably_modified_type_p (TREE_TYPE (node_or_origin),
16395 decl_function_context
16396 (node_or_origin))))
16398 tree type = TREE_TYPE (node_or_origin);
16399 if (decl_by_reference_p (node_or_origin))
16400 add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
16401 context_die);
16402 else
16403 add_type_attribute (parm_die, type,
16404 TREE_READONLY (node_or_origin),
16405 TREE_THIS_VOLATILE (node_or_origin),
16406 context_die);
16408 if (origin == NULL && DECL_ARTIFICIAL (node))
16409 add_AT_flag (parm_die, DW_AT_artificial, 1);
16411 if (node && node != origin)
16412 equate_decl_number_to_die (node, parm_die);
16413 if (! DECL_ABSTRACT (node_or_origin))
16414 add_location_or_const_value_attribute (parm_die, node_or_origin,
16415 node == NULL, DW_AT_location);
16417 break;
16419 case tcc_type:
16420 /* We were called with some kind of a ..._TYPE node. */
16421 add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
16422 break;
16424 default:
16425 gcc_unreachable ();
16428 return parm_die;
16431 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
16432 children DW_TAG_formal_parameter DIEs representing the arguments of the
16433 parameter pack.
16435 PARM_PACK must be a function parameter pack.
16436 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
16437 must point to the subsequent arguments of the function PACK_ARG belongs to.
16438 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
16439 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
16440 following the last one for which a DIE was generated. */
16442 static dw_die_ref
16443 gen_formal_parameter_pack_die (tree parm_pack,
16444 tree pack_arg,
16445 dw_die_ref subr_die,
16446 tree *next_arg)
16448 tree arg;
16449 dw_die_ref parm_pack_die;
16451 gcc_assert (parm_pack
16452 && lang_hooks.function_parameter_pack_p (parm_pack)
16453 && subr_die);
16455 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
16456 add_src_coords_attributes (parm_pack_die, parm_pack);
16458 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
16460 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
16461 parm_pack))
16462 break;
16463 gen_formal_parameter_die (arg, NULL,
16464 false /* Don't emit name attribute. */,
16465 parm_pack_die);
16467 if (next_arg)
16468 *next_arg = arg;
16469 return parm_pack_die;
16472 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
16473 at the end of an (ANSI prototyped) formal parameters list. */
16475 static void
16476 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
16478 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
16481 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
16482 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
16483 parameters as specified in some function type specification (except for
16484 those which appear as part of a function *definition*). */
16486 static void
16487 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
16489 tree link;
16490 tree formal_type = NULL;
16491 tree first_parm_type;
16492 tree arg;
16494 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
16496 arg = DECL_ARGUMENTS (function_or_method_type);
16497 function_or_method_type = TREE_TYPE (function_or_method_type);
16499 else
16500 arg = NULL_TREE;
16502 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
16504 /* Make our first pass over the list of formal parameter types and output a
16505 DW_TAG_formal_parameter DIE for each one. */
16506 for (link = first_parm_type; link; )
16508 dw_die_ref parm_die;
16510 formal_type = TREE_VALUE (link);
16511 if (formal_type == void_type_node)
16512 break;
16514 /* Output a (nameless) DIE to represent the formal parameter itself. */
16515 parm_die = gen_formal_parameter_die (formal_type, NULL,
16516 true /* Emit name attribute. */,
16517 context_die);
16518 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
16519 && link == first_parm_type)
16521 add_AT_flag (parm_die, DW_AT_artificial, 1);
16522 if (dwarf_version >= 3 || !dwarf_strict)
16523 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
16525 else if (arg && DECL_ARTIFICIAL (arg))
16526 add_AT_flag (parm_die, DW_AT_artificial, 1);
16528 link = TREE_CHAIN (link);
16529 if (arg)
16530 arg = DECL_CHAIN (arg);
16533 /* If this function type has an ellipsis, add a
16534 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
16535 if (formal_type != void_type_node)
16536 gen_unspecified_parameters_die (function_or_method_type, context_die);
16538 /* Make our second (and final) pass over the list of formal parameter types
16539 and output DIEs to represent those types (as necessary). */
16540 for (link = TYPE_ARG_TYPES (function_or_method_type);
16541 link && TREE_VALUE (link);
16542 link = TREE_CHAIN (link))
16543 gen_type_die (TREE_VALUE (link), context_die);
16546 /* We want to generate the DIE for TYPE so that we can generate the
16547 die for MEMBER, which has been defined; we will need to refer back
16548 to the member declaration nested within TYPE. If we're trying to
16549 generate minimal debug info for TYPE, processing TYPE won't do the
16550 trick; we need to attach the member declaration by hand. */
16552 static void
16553 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
16555 gen_type_die (type, context_die);
16557 /* If we're trying to avoid duplicate debug info, we may not have
16558 emitted the member decl for this function. Emit it now. */
16559 if (TYPE_STUB_DECL (type)
16560 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
16561 && ! lookup_decl_die (member))
16563 dw_die_ref type_die;
16564 gcc_assert (!decl_ultimate_origin (member));
16566 push_decl_scope (type);
16567 type_die = lookup_type_die_strip_naming_typedef (type);
16568 if (TREE_CODE (member) == FUNCTION_DECL)
16569 gen_subprogram_die (member, type_die);
16570 else if (TREE_CODE (member) == FIELD_DECL)
16572 /* Ignore the nameless fields that are used to skip bits but handle
16573 C++ anonymous unions and structs. */
16574 if (DECL_NAME (member) != NULL_TREE
16575 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
16576 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
16578 gen_type_die (member_declared_type (member), type_die);
16579 gen_field_die (member, type_die);
16582 else
16583 gen_variable_die (member, NULL_TREE, type_die);
16585 pop_decl_scope ();
16589 /* Forward declare these functions, because they are mutually recursive
16590 with their set_block_* pairing functions. */
16591 static void set_decl_origin_self (tree);
16592 static void set_decl_abstract_flags (tree, int);
16594 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
16595 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
16596 that it points to the node itself, thus indicating that the node is its
16597 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
16598 the given node is NULL, recursively descend the decl/block tree which
16599 it is the root of, and for each other ..._DECL or BLOCK node contained
16600 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
16601 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
16602 values to point to themselves. */
16604 static void
16605 set_block_origin_self (tree stmt)
16607 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
16609 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
16612 tree local_decl;
16614 for (local_decl = BLOCK_VARS (stmt);
16615 local_decl != NULL_TREE;
16616 local_decl = DECL_CHAIN (local_decl))
16617 if (! DECL_EXTERNAL (local_decl))
16618 set_decl_origin_self (local_decl); /* Potential recursion. */
16622 tree subblock;
16624 for (subblock = BLOCK_SUBBLOCKS (stmt);
16625 subblock != NULL_TREE;
16626 subblock = BLOCK_CHAIN (subblock))
16627 set_block_origin_self (subblock); /* Recurse. */
16632 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
16633 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
16634 node to so that it points to the node itself, thus indicating that the
16635 node represents its own (abstract) origin. Additionally, if the
16636 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
16637 the decl/block tree of which the given node is the root of, and for
16638 each other ..._DECL or BLOCK node contained therein whose
16639 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
16640 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
16641 point to themselves. */
16643 static void
16644 set_decl_origin_self (tree decl)
16646 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
16648 DECL_ABSTRACT_ORIGIN (decl) = decl;
16649 if (TREE_CODE (decl) == FUNCTION_DECL)
16651 tree arg;
16653 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
16654 DECL_ABSTRACT_ORIGIN (arg) = arg;
16655 if (DECL_INITIAL (decl) != NULL_TREE
16656 && DECL_INITIAL (decl) != error_mark_node)
16657 set_block_origin_self (DECL_INITIAL (decl));
16662 /* Given a pointer to some BLOCK node, and a boolean value to set the
16663 "abstract" flags to, set that value into the BLOCK_ABSTRACT flag for
16664 the given block, and for all local decls and all local sub-blocks
16665 (recursively) which are contained therein. */
16667 static void
16668 set_block_abstract_flags (tree stmt, int setting)
16670 tree local_decl;
16671 tree subblock;
16672 unsigned int i;
16674 BLOCK_ABSTRACT (stmt) = setting;
16676 for (local_decl = BLOCK_VARS (stmt);
16677 local_decl != NULL_TREE;
16678 local_decl = DECL_CHAIN (local_decl))
16679 if (! DECL_EXTERNAL (local_decl))
16680 set_decl_abstract_flags (local_decl, setting);
16682 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
16684 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
16685 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
16686 || TREE_CODE (local_decl) == PARM_DECL)
16687 set_decl_abstract_flags (local_decl, setting);
16690 for (subblock = BLOCK_SUBBLOCKS (stmt);
16691 subblock != NULL_TREE;
16692 subblock = BLOCK_CHAIN (subblock))
16693 set_block_abstract_flags (subblock, setting);
16696 /* Given a pointer to some ..._DECL node, and a boolean value to set the
16697 "abstract" flags to, set that value into the DECL_ABSTRACT flag for the
16698 given decl, and (in the case where the decl is a FUNCTION_DECL) also
16699 set the abstract flags for all of the parameters, local vars, local
16700 blocks and sub-blocks (recursively) to the same setting. */
16702 static void
16703 set_decl_abstract_flags (tree decl, int setting)
16705 DECL_ABSTRACT (decl) = setting;
16706 if (TREE_CODE (decl) == FUNCTION_DECL)
16708 tree arg;
16710 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
16711 DECL_ABSTRACT (arg) = setting;
16712 if (DECL_INITIAL (decl) != NULL_TREE
16713 && DECL_INITIAL (decl) != error_mark_node)
16714 set_block_abstract_flags (DECL_INITIAL (decl), setting);
16718 /* Generate the DWARF2 info for the "abstract" instance of a function which we
16719 may later generate inlined and/or out-of-line instances of. */
16721 static void
16722 dwarf2out_abstract_function (tree decl)
16724 dw_die_ref old_die;
16725 tree save_fn;
16726 tree context;
16727 int was_abstract;
16728 htab_t old_decl_loc_table;
16729 htab_t old_cached_dw_loc_list_table;
16730 int old_call_site_count, old_tail_call_site_count;
16731 struct call_arg_loc_node *old_call_arg_locations;
16733 /* Make sure we have the actual abstract inline, not a clone. */
16734 decl = DECL_ORIGIN (decl);
16736 old_die = lookup_decl_die (decl);
16737 if (old_die && get_AT (old_die, DW_AT_inline))
16738 /* We've already generated the abstract instance. */
16739 return;
16741 /* We can be called while recursively when seeing block defining inlined subroutine
16742 DIE. Be sure to not clobber the outer location table nor use it or we would
16743 get locations in abstract instantces. */
16744 old_decl_loc_table = decl_loc_table;
16745 decl_loc_table = NULL;
16746 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
16747 cached_dw_loc_list_table = NULL;
16748 old_call_arg_locations = call_arg_locations;
16749 call_arg_locations = NULL;
16750 old_call_site_count = call_site_count;
16751 call_site_count = -1;
16752 old_tail_call_site_count = tail_call_site_count;
16753 tail_call_site_count = -1;
16755 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
16756 we don't get confused by DECL_ABSTRACT. */
16757 if (debug_info_level > DINFO_LEVEL_TERSE)
16759 context = decl_class_context (decl);
16760 if (context)
16761 gen_type_die_for_member
16762 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
16765 /* Pretend we've just finished compiling this function. */
16766 save_fn = current_function_decl;
16767 current_function_decl = decl;
16768 push_cfun (DECL_STRUCT_FUNCTION (decl));
16770 was_abstract = DECL_ABSTRACT (decl);
16771 set_decl_abstract_flags (decl, 1);
16772 dwarf2out_decl (decl);
16773 if (! was_abstract)
16774 set_decl_abstract_flags (decl, 0);
16776 current_function_decl = save_fn;
16777 decl_loc_table = old_decl_loc_table;
16778 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
16779 call_arg_locations = old_call_arg_locations;
16780 call_site_count = old_call_site_count;
16781 tail_call_site_count = old_tail_call_site_count;
16782 pop_cfun ();
16785 /* Helper function of premark_used_types() which gets called through
16786 htab_traverse.
16788 Marks the DIE of a given type in *SLOT as perennial, so it never gets
16789 marked as unused by prune_unused_types. */
16791 static int
16792 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
16794 tree type;
16795 dw_die_ref die;
16797 type = (tree) *slot;
16798 die = lookup_type_die (type);
16799 if (die != NULL)
16800 die->die_perennial_p = 1;
16801 return 1;
16804 /* Helper function of premark_types_used_by_global_vars which gets called
16805 through htab_traverse.
16807 Marks the DIE of a given type in *SLOT as perennial, so it never gets
16808 marked as unused by prune_unused_types. The DIE of the type is marked
16809 only if the global variable using the type will actually be emitted. */
16811 static int
16812 premark_types_used_by_global_vars_helper (void **slot,
16813 void *data ATTRIBUTE_UNUSED)
16815 struct types_used_by_vars_entry *entry;
16816 dw_die_ref die;
16818 entry = (struct types_used_by_vars_entry *) *slot;
16819 gcc_assert (entry->type != NULL
16820 && entry->var_decl != NULL);
16821 die = lookup_type_die (entry->type);
16822 if (die)
16824 /* Ask cgraph if the global variable really is to be emitted.
16825 If yes, then we'll keep the DIE of ENTRY->TYPE. */
16826 struct varpool_node *node = varpool_get_node (entry->var_decl);
16827 if (node && node->analyzed)
16829 die->die_perennial_p = 1;
16830 /* Keep the parent DIEs as well. */
16831 while ((die = die->die_parent) && die->die_perennial_p == 0)
16832 die->die_perennial_p = 1;
16835 return 1;
16838 /* Mark all members of used_types_hash as perennial. */
16840 static void
16841 premark_used_types (void)
16843 if (cfun && cfun->used_types_hash)
16844 htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
16847 /* Mark all members of types_used_by_vars_entry as perennial. */
16849 static void
16850 premark_types_used_by_global_vars (void)
16852 if (types_used_by_vars_hash)
16853 htab_traverse (types_used_by_vars_hash,
16854 premark_types_used_by_global_vars_helper, NULL);
16857 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
16858 for CA_LOC call arg loc node. */
16860 static dw_die_ref
16861 gen_call_site_die (tree decl, dw_die_ref subr_die,
16862 struct call_arg_loc_node *ca_loc)
16864 dw_die_ref stmt_die = NULL, die;
16865 tree block = ca_loc->block;
16867 while (block
16868 && block != DECL_INITIAL (decl)
16869 && TREE_CODE (block) == BLOCK)
16871 if (VEC_length (dw_die_ref, block_map) > BLOCK_NUMBER (block))
16872 stmt_die = VEC_index (dw_die_ref, block_map, BLOCK_NUMBER (block));
16873 if (stmt_die)
16874 break;
16875 block = BLOCK_SUPERCONTEXT (block);
16877 if (stmt_die == NULL)
16878 stmt_die = subr_die;
16879 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
16880 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
16881 if (ca_loc->tail_call_p)
16882 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
16883 if (ca_loc->symbol_ref)
16885 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
16886 if (tdie)
16887 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
16888 else
16889 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref);
16891 return die;
16894 /* Generate a DIE to represent a declared function (either file-scope or
16895 block-local). */
16897 static void
16898 gen_subprogram_die (tree decl, dw_die_ref context_die)
16900 tree origin = decl_ultimate_origin (decl);
16901 dw_die_ref subr_die;
16902 tree outer_scope;
16903 dw_die_ref old_die = lookup_decl_die (decl);
16904 int declaration = (current_function_decl != decl
16905 || class_or_namespace_scope_p (context_die));
16907 premark_used_types ();
16909 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
16910 started to generate the abstract instance of an inline, decided to output
16911 its containing class, and proceeded to emit the declaration of the inline
16912 from the member list for the class. If so, DECLARATION takes priority;
16913 we'll get back to the abstract instance when done with the class. */
16915 /* The class-scope declaration DIE must be the primary DIE. */
16916 if (origin && declaration && class_or_namespace_scope_p (context_die))
16918 origin = NULL;
16919 gcc_assert (!old_die);
16922 /* Now that the C++ front end lazily declares artificial member fns, we
16923 might need to retrofit the declaration into its class. */
16924 if (!declaration && !origin && !old_die
16925 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
16926 && !class_or_namespace_scope_p (context_die)
16927 && debug_info_level > DINFO_LEVEL_TERSE)
16928 old_die = force_decl_die (decl);
16930 if (origin != NULL)
16932 gcc_assert (!declaration || local_scope_p (context_die));
16934 /* Fixup die_parent for the abstract instance of a nested
16935 inline function. */
16936 if (old_die && old_die->die_parent == NULL)
16937 add_child_die (context_die, old_die);
16939 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
16940 add_abstract_origin_attribute (subr_die, origin);
16941 /* This is where the actual code for a cloned function is.
16942 Let's emit linkage name attribute for it. This helps
16943 debuggers to e.g, set breakpoints into
16944 constructors/destructors when the user asks "break
16945 K::K". */
16946 add_linkage_name (subr_die, decl);
16948 else if (old_die)
16950 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
16951 struct dwarf_file_data * file_index = lookup_filename (s.file);
16953 if (!get_AT_flag (old_die, DW_AT_declaration)
16954 /* We can have a normal definition following an inline one in the
16955 case of redefinition of GNU C extern inlines.
16956 It seems reasonable to use AT_specification in this case. */
16957 && !get_AT (old_die, DW_AT_inline))
16959 /* Detect and ignore this case, where we are trying to output
16960 something we have already output. */
16961 return;
16964 /* If the definition comes from the same place as the declaration,
16965 maybe use the old DIE. We always want the DIE for this function
16966 that has the *_pc attributes to be under comp_unit_die so the
16967 debugger can find it. We also need to do this for abstract
16968 instances of inlines, since the spec requires the out-of-line copy
16969 to have the same parent. For local class methods, this doesn't
16970 apply; we just use the old DIE. */
16971 if ((is_cu_die (old_die->die_parent) || context_die == NULL)
16972 && (DECL_ARTIFICIAL (decl)
16973 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
16974 && (get_AT_unsigned (old_die, DW_AT_decl_line)
16975 == (unsigned) s.line))))
16977 subr_die = old_die;
16979 /* Clear out the declaration attribute and the formal parameters.
16980 Do not remove all children, because it is possible that this
16981 declaration die was forced using force_decl_die(). In such
16982 cases die that forced declaration die (e.g. TAG_imported_module)
16983 is one of the children that we do not want to remove. */
16984 remove_AT (subr_die, DW_AT_declaration);
16985 remove_AT (subr_die, DW_AT_object_pointer);
16986 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
16988 else
16990 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
16991 add_AT_specification (subr_die, old_die);
16992 add_pubname (decl, subr_die);
16993 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
16994 add_AT_file (subr_die, DW_AT_decl_file, file_index);
16995 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
16996 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
16999 else
17001 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17003 if (TREE_PUBLIC (decl))
17004 add_AT_flag (subr_die, DW_AT_external, 1);
17006 add_name_and_src_coords_attributes (subr_die, decl);
17007 add_pubname (decl, subr_die);
17008 if (debug_info_level > DINFO_LEVEL_TERSE)
17010 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
17011 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
17012 0, 0, context_die);
17015 add_pure_or_virtual_attribute (subr_die, decl);
17016 if (DECL_ARTIFICIAL (decl))
17017 add_AT_flag (subr_die, DW_AT_artificial, 1);
17019 add_accessibility_attribute (subr_die, decl);
17022 if (declaration)
17024 if (!old_die || !get_AT (old_die, DW_AT_inline))
17026 add_AT_flag (subr_die, DW_AT_declaration, 1);
17028 /* If this is an explicit function declaration then generate
17029 a DW_AT_explicit attribute. */
17030 if (lang_hooks.decls.function_decl_explicit_p (decl)
17031 && (dwarf_version >= 3 || !dwarf_strict))
17032 add_AT_flag (subr_die, DW_AT_explicit, 1);
17034 /* The first time we see a member function, it is in the context of
17035 the class to which it belongs. We make sure of this by emitting
17036 the class first. The next time is the definition, which is
17037 handled above. The two may come from the same source text.
17039 Note that force_decl_die() forces function declaration die. It is
17040 later reused to represent definition. */
17041 equate_decl_number_to_die (decl, subr_die);
17044 else if (DECL_ABSTRACT (decl))
17046 if (DECL_DECLARED_INLINE_P (decl))
17048 if (cgraph_function_possibly_inlined_p (decl))
17049 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
17050 else
17051 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
17053 else
17055 if (cgraph_function_possibly_inlined_p (decl))
17056 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
17057 else
17058 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
17061 if (DECL_DECLARED_INLINE_P (decl)
17062 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
17063 add_AT_flag (subr_die, DW_AT_artificial, 1);
17065 equate_decl_number_to_die (decl, subr_die);
17067 else if (!DECL_EXTERNAL (decl))
17069 HOST_WIDE_INT cfa_fb_offset;
17071 if (!old_die || !get_AT (old_die, DW_AT_inline))
17072 equate_decl_number_to_die (decl, subr_die);
17074 if (!flag_reorder_blocks_and_partition)
17076 dw_fde_ref fde = cfun->fde;
17077 if (fde->dw_fde_begin)
17079 /* We have already generated the labels. */
17080 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end);
17082 else
17084 /* Create start/end labels and add the range. */
17085 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
17086 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
17087 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
17088 current_function_funcdef_no);
17089 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
17090 current_function_funcdef_no);
17091 add_AT_low_high_pc (subr_die, label_id_low, label_id_high);
17094 #if VMS_DEBUGGING_INFO
17095 /* HP OpenVMS Industry Standard 64: DWARF Extensions
17096 Section 2.3 Prologue and Epilogue Attributes:
17097 When a breakpoint is set on entry to a function, it is generally
17098 desirable for execution to be suspended, not on the very first
17099 instruction of the function, but rather at a point after the
17100 function's frame has been set up, after any language defined local
17101 declaration processing has been completed, and before execution of
17102 the first statement of the function begins. Debuggers generally
17103 cannot properly determine where this point is. Similarly for a
17104 breakpoint set on exit from a function. The prologue and epilogue
17105 attributes allow a compiler to communicate the location(s) to use. */
17108 if (fde->dw_fde_vms_end_prologue)
17109 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
17110 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
17112 if (fde->dw_fde_vms_begin_epilogue)
17113 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
17114 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
17116 #endif
17119 else
17121 /* Generate pubnames entries for the split function code ranges. */
17122 dw_fde_ref fde = cfun->fde;
17124 if (fde->dw_fde_second_begin)
17126 if (dwarf_version >= 3 || !dwarf_strict)
17128 /* We should use ranges for non-contiguous code section
17129 addresses. Use the actual code range for the initial
17130 section, since the HOT/COLD labels might precede an
17131 alignment offset. */
17132 bool range_list_added = false;
17133 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
17134 fde->dw_fde_end, &range_list_added);
17135 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
17136 fde->dw_fde_second_end,
17137 &range_list_added);
17138 if (range_list_added)
17139 add_ranges (NULL);
17141 else
17143 /* There is no real support in DW2 for this .. so we make
17144 a work-around. First, emit the pub name for the segment
17145 containing the function label. Then make and emit a
17146 simplified subprogram DIE for the second segment with the
17147 name pre-fixed by __hot/cold_sect_of_. We use the same
17148 linkage name for the second die so that gdb will find both
17149 sections when given "b foo". */
17150 const char *name = NULL;
17151 tree decl_name = DECL_NAME (decl);
17152 dw_die_ref seg_die;
17154 /* Do the 'primary' section. */
17155 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
17156 fde->dw_fde_end);
17158 /* Build a minimal DIE for the secondary section. */
17159 seg_die = new_die (DW_TAG_subprogram,
17160 subr_die->die_parent, decl);
17162 if (TREE_PUBLIC (decl))
17163 add_AT_flag (seg_die, DW_AT_external, 1);
17165 if (decl_name != NULL
17166 && IDENTIFIER_POINTER (decl_name) != NULL)
17168 name = dwarf2_name (decl, 1);
17169 if (! DECL_ARTIFICIAL (decl))
17170 add_src_coords_attributes (seg_die, decl);
17172 add_linkage_name (seg_die, decl);
17174 gcc_assert (name != NULL);
17175 add_pure_or_virtual_attribute (seg_die, decl);
17176 if (DECL_ARTIFICIAL (decl))
17177 add_AT_flag (seg_die, DW_AT_artificial, 1);
17179 name = concat ("__second_sect_of_", name, NULL);
17180 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
17181 fde->dw_fde_second_end);
17182 add_name_attribute (seg_die, name);
17183 if (want_pubnames ())
17184 add_pubname_string (name, seg_die);
17187 else
17188 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end);
17191 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
17193 /* We define the "frame base" as the function's CFA. This is more
17194 convenient for several reasons: (1) It's stable across the prologue
17195 and epilogue, which makes it better than just a frame pointer,
17196 (2) With dwarf3, there exists a one-byte encoding that allows us
17197 to reference the .debug_frame data by proxy, but failing that,
17198 (3) We can at least reuse the code inspection and interpretation
17199 code that determines the CFA position at various points in the
17200 function. */
17201 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
17203 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
17204 add_AT_loc (subr_die, DW_AT_frame_base, op);
17206 else
17208 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
17209 if (list->dw_loc_next)
17210 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
17211 else
17212 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
17215 /* Compute a displacement from the "steady-state frame pointer" to
17216 the CFA. The former is what all stack slots and argument slots
17217 will reference in the rtl; the later is what we've told the
17218 debugger about. We'll need to adjust all frame_base references
17219 by this displacement. */
17220 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
17222 if (cfun->static_chain_decl)
17223 add_AT_location_description (subr_die, DW_AT_static_link,
17224 loc_list_from_tree (cfun->static_chain_decl, 2));
17227 /* Generate child dies for template paramaters. */
17228 if (debug_info_level > DINFO_LEVEL_TERSE)
17229 gen_generic_params_dies (decl);
17231 /* Now output descriptions of the arguments for this function. This gets
17232 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
17233 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
17234 `...' at the end of the formal parameter list. In order to find out if
17235 there was a trailing ellipsis or not, we must instead look at the type
17236 associated with the FUNCTION_DECL. This will be a node of type
17237 FUNCTION_TYPE. If the chain of type nodes hanging off of this
17238 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
17239 an ellipsis at the end. */
17241 /* In the case where we are describing a mere function declaration, all we
17242 need to do here (and all we *can* do here) is to describe the *types* of
17243 its formal parameters. */
17244 if (debug_info_level <= DINFO_LEVEL_TERSE)
17246 else if (declaration)
17247 gen_formal_types_die (decl, subr_die);
17248 else
17250 /* Generate DIEs to represent all known formal parameters. */
17251 tree parm = DECL_ARGUMENTS (decl);
17252 tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
17253 tree generic_decl_parm = generic_decl
17254 ? DECL_ARGUMENTS (generic_decl)
17255 : NULL;
17257 /* Now we want to walk the list of parameters of the function and
17258 emit their relevant DIEs.
17260 We consider the case of DECL being an instance of a generic function
17261 as well as it being a normal function.
17263 If DECL is an instance of a generic function we walk the
17264 parameters of the generic function declaration _and_ the parameters of
17265 DECL itself. This is useful because we want to emit specific DIEs for
17266 function parameter packs and those are declared as part of the
17267 generic function declaration. In that particular case,
17268 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
17269 That DIE has children DIEs representing the set of arguments
17270 of the pack. Note that the set of pack arguments can be empty.
17271 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
17272 children DIE.
17274 Otherwise, we just consider the parameters of DECL. */
17275 while (generic_decl_parm || parm)
17277 if (generic_decl_parm
17278 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
17279 gen_formal_parameter_pack_die (generic_decl_parm,
17280 parm, subr_die,
17281 &parm);
17282 else if (parm)
17284 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
17286 if (parm == DECL_ARGUMENTS (decl)
17287 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
17288 && parm_die
17289 && (dwarf_version >= 3 || !dwarf_strict))
17290 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
17292 parm = DECL_CHAIN (parm);
17295 if (generic_decl_parm)
17296 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
17299 /* Decide whether we need an unspecified_parameters DIE at the end.
17300 There are 2 more cases to do this for: 1) the ansi ... declaration -
17301 this is detectable when the end of the arg list is not a
17302 void_type_node 2) an unprototyped function declaration (not a
17303 definition). This just means that we have no info about the
17304 parameters at all. */
17305 if (prototype_p (TREE_TYPE (decl)))
17307 /* This is the prototyped case, check for.... */
17308 if (stdarg_p (TREE_TYPE (decl)))
17309 gen_unspecified_parameters_die (decl, subr_die);
17311 else if (DECL_INITIAL (decl) == NULL_TREE)
17312 gen_unspecified_parameters_die (decl, subr_die);
17315 /* Output Dwarf info for all of the stuff within the body of the function
17316 (if it has one - it may be just a declaration). */
17317 outer_scope = DECL_INITIAL (decl);
17319 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
17320 a function. This BLOCK actually represents the outermost binding contour
17321 for the function, i.e. the contour in which the function's formal
17322 parameters and labels get declared. Curiously, it appears that the front
17323 end doesn't actually put the PARM_DECL nodes for the current function onto
17324 the BLOCK_VARS list for this outer scope, but are strung off of the
17325 DECL_ARGUMENTS list for the function instead.
17327 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
17328 the LABEL_DECL nodes for the function however, and we output DWARF info
17329 for those in decls_for_scope. Just within the `outer_scope' there will be
17330 a BLOCK node representing the function's outermost pair of curly braces,
17331 and any blocks used for the base and member initializers of a C++
17332 constructor function. */
17333 if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
17335 int call_site_note_count = 0;
17336 int tail_call_site_note_count = 0;
17338 /* Emit a DW_TAG_variable DIE for a named return value. */
17339 if (DECL_NAME (DECL_RESULT (decl)))
17340 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
17342 current_function_has_inlines = 0;
17343 decls_for_scope (outer_scope, subr_die, 0);
17345 if (call_arg_locations && !dwarf_strict)
17347 struct call_arg_loc_node *ca_loc;
17348 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
17350 dw_die_ref die = NULL;
17351 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
17352 rtx arg, next_arg;
17354 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
17355 arg; arg = next_arg)
17357 dw_loc_descr_ref reg, val;
17358 enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
17359 dw_die_ref cdie, tdie = NULL;
17361 next_arg = XEXP (arg, 1);
17362 if (REG_P (XEXP (XEXP (arg, 0), 0))
17363 && next_arg
17364 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
17365 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
17366 && REGNO (XEXP (XEXP (arg, 0), 0))
17367 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
17368 next_arg = XEXP (next_arg, 1);
17369 if (mode == VOIDmode)
17371 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
17372 if (mode == VOIDmode)
17373 mode = GET_MODE (XEXP (arg, 0));
17375 if (mode == VOIDmode || mode == BLKmode)
17376 continue;
17377 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
17379 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
17380 tloc = XEXP (XEXP (arg, 0), 1);
17381 continue;
17383 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
17384 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
17386 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
17387 tlocc = XEXP (XEXP (arg, 0), 1);
17388 continue;
17390 reg = NULL;
17391 if (REG_P (XEXP (XEXP (arg, 0), 0)))
17392 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
17393 VAR_INIT_STATUS_INITIALIZED);
17394 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
17396 rtx mem = XEXP (XEXP (arg, 0), 0);
17397 reg = mem_loc_descriptor (XEXP (mem, 0),
17398 get_address_mode (mem),
17399 GET_MODE (mem),
17400 VAR_INIT_STATUS_INITIALIZED);
17402 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
17403 == DEBUG_PARAMETER_REF)
17405 tree tdecl
17406 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
17407 tdie = lookup_decl_die (tdecl);
17408 if (tdie == NULL)
17409 continue;
17411 else
17412 continue;
17413 if (reg == NULL
17414 && GET_CODE (XEXP (XEXP (arg, 0), 0))
17415 != DEBUG_PARAMETER_REF)
17416 continue;
17417 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
17418 VOIDmode,
17419 VAR_INIT_STATUS_INITIALIZED);
17420 if (val == NULL)
17421 continue;
17422 if (die == NULL)
17423 die = gen_call_site_die (decl, subr_die, ca_loc);
17424 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
17425 NULL_TREE);
17426 if (reg != NULL)
17427 add_AT_loc (cdie, DW_AT_location, reg);
17428 else if (tdie != NULL)
17429 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
17430 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
17431 if (next_arg != XEXP (arg, 1))
17433 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
17434 if (mode == VOIDmode)
17435 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
17436 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
17437 0), 1),
17438 mode, VOIDmode,
17439 VAR_INIT_STATUS_INITIALIZED);
17440 if (val != NULL)
17441 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
17444 if (die == NULL
17445 && (ca_loc->symbol_ref || tloc))
17446 die = gen_call_site_die (decl, subr_die, ca_loc);
17447 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
17449 dw_loc_descr_ref tval = NULL;
17451 if (tloc != NULL_RTX)
17452 tval = mem_loc_descriptor (tloc,
17453 GET_MODE (tloc) == VOIDmode
17454 ? Pmode : GET_MODE (tloc),
17455 VOIDmode,
17456 VAR_INIT_STATUS_INITIALIZED);
17457 if (tval)
17458 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
17459 else if (tlocc != NULL_RTX)
17461 tval = mem_loc_descriptor (tlocc,
17462 GET_MODE (tlocc) == VOIDmode
17463 ? Pmode : GET_MODE (tlocc),
17464 VOIDmode,
17465 VAR_INIT_STATUS_INITIALIZED);
17466 if (tval)
17467 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
17468 tval);
17471 if (die != NULL)
17473 call_site_note_count++;
17474 if (ca_loc->tail_call_p)
17475 tail_call_site_note_count++;
17479 call_arg_locations = NULL;
17480 call_arg_loc_last = NULL;
17481 if (tail_call_site_count >= 0
17482 && tail_call_site_count == tail_call_site_note_count
17483 && !dwarf_strict)
17485 if (call_site_count >= 0
17486 && call_site_count == call_site_note_count)
17487 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
17488 else
17489 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
17491 call_site_count = -1;
17492 tail_call_site_count = -1;
17494 /* Add the calling convention attribute if requested. */
17495 add_calling_convention_attribute (subr_die, decl);
17499 /* Returns a hash value for X (which really is a die_struct). */
17501 static hashval_t
17502 common_block_die_table_hash (const void *x)
17504 const_dw_die_ref d = (const_dw_die_ref) x;
17505 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
17508 /* Return nonzero if decl_id and die_parent of die_struct X is the same
17509 as decl_id and die_parent of die_struct Y. */
17511 static int
17512 common_block_die_table_eq (const void *x, const void *y)
17514 const_dw_die_ref d = (const_dw_die_ref) x;
17515 const_dw_die_ref e = (const_dw_die_ref) y;
17516 return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
17519 /* Generate a DIE to represent a declared data object.
17520 Either DECL or ORIGIN must be non-null. */
17522 static void
17523 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
17525 HOST_WIDE_INT off = 0;
17526 tree com_decl;
17527 tree decl_or_origin = decl ? decl : origin;
17528 tree ultimate_origin;
17529 dw_die_ref var_die;
17530 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
17531 dw_die_ref origin_die;
17532 bool declaration = (DECL_EXTERNAL (decl_or_origin)
17533 || class_or_namespace_scope_p (context_die));
17534 bool specialization_p = false;
17536 ultimate_origin = decl_ultimate_origin (decl_or_origin);
17537 if (decl || ultimate_origin)
17538 origin = ultimate_origin;
17539 com_decl = fortran_common (decl_or_origin, &off);
17541 /* Symbol in common gets emitted as a child of the common block, in the form
17542 of a data member. */
17543 if (com_decl)
17545 dw_die_ref com_die;
17546 dw_loc_list_ref loc;
17547 die_node com_die_arg;
17549 var_die = lookup_decl_die (decl_or_origin);
17550 if (var_die)
17552 if (get_AT (var_die, DW_AT_location) == NULL)
17554 loc = loc_list_from_tree (com_decl, off ? 1 : 2);
17555 if (loc)
17557 if (off)
17559 /* Optimize the common case. */
17560 if (single_element_loc_list_p (loc)
17561 && loc->expr->dw_loc_opc == DW_OP_addr
17562 && loc->expr->dw_loc_next == NULL
17563 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
17564 == SYMBOL_REF)
17566 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
17567 loc->expr->dw_loc_oprnd1.v.val_addr
17568 = plus_constant (GET_MODE (x), x , off);
17570 else
17571 loc_list_plus_const (loc, off);
17573 add_AT_location_description (var_die, DW_AT_location, loc);
17574 remove_AT (var_die, DW_AT_declaration);
17577 return;
17580 if (common_block_die_table == NULL)
17581 common_block_die_table
17582 = htab_create_ggc (10, common_block_die_table_hash,
17583 common_block_die_table_eq, NULL);
17585 com_die_arg.decl_id = DECL_UID (com_decl);
17586 com_die_arg.die_parent = context_die;
17587 com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
17588 loc = loc_list_from_tree (com_decl, 2);
17589 if (com_die == NULL)
17591 const char *cnam
17592 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
17593 void **slot;
17595 com_die = new_die (DW_TAG_common_block, context_die, decl);
17596 add_name_and_src_coords_attributes (com_die, com_decl);
17597 if (loc)
17599 add_AT_location_description (com_die, DW_AT_location, loc);
17600 /* Avoid sharing the same loc descriptor between
17601 DW_TAG_common_block and DW_TAG_variable. */
17602 loc = loc_list_from_tree (com_decl, 2);
17604 else if (DECL_EXTERNAL (decl))
17605 add_AT_flag (com_die, DW_AT_declaration, 1);
17606 if (want_pubnames ())
17607 add_pubname_string (cnam, com_die); /* ??? needed? */
17608 com_die->decl_id = DECL_UID (com_decl);
17609 slot = htab_find_slot (common_block_die_table, com_die, INSERT);
17610 *slot = (void *) com_die;
17612 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
17614 add_AT_location_description (com_die, DW_AT_location, loc);
17615 loc = loc_list_from_tree (com_decl, 2);
17616 remove_AT (com_die, DW_AT_declaration);
17618 var_die = new_die (DW_TAG_variable, com_die, decl);
17619 add_name_and_src_coords_attributes (var_die, decl);
17620 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
17621 TREE_THIS_VOLATILE (decl), context_die);
17622 add_AT_flag (var_die, DW_AT_external, 1);
17623 if (loc)
17625 if (off)
17627 /* Optimize the common case. */
17628 if (single_element_loc_list_p (loc)
17629 && loc->expr->dw_loc_opc == DW_OP_addr
17630 && loc->expr->dw_loc_next == NULL
17631 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
17633 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
17634 loc->expr->dw_loc_oprnd1.v.val_addr
17635 = plus_constant (GET_MODE (x), x, off);
17637 else
17638 loc_list_plus_const (loc, off);
17640 add_AT_location_description (var_die, DW_AT_location, loc);
17642 else if (DECL_EXTERNAL (decl))
17643 add_AT_flag (var_die, DW_AT_declaration, 1);
17644 equate_decl_number_to_die (decl, var_die);
17645 return;
17648 /* If the compiler emitted a definition for the DECL declaration
17649 and if we already emitted a DIE for it, don't emit a second
17650 DIE for it again. Allow re-declarations of DECLs that are
17651 inside functions, though. */
17652 if (old_die && declaration && !local_scope_p (context_die))
17653 return;
17655 /* For static data members, the declaration in the class is supposed
17656 to have DW_TAG_member tag; the specification should still be
17657 DW_TAG_variable referencing the DW_TAG_member DIE. */
17658 if (declaration && class_scope_p (context_die))
17659 var_die = new_die (DW_TAG_member, context_die, decl);
17660 else
17661 var_die = new_die (DW_TAG_variable, context_die, decl);
17663 origin_die = NULL;
17664 if (origin != NULL)
17665 origin_die = add_abstract_origin_attribute (var_die, origin);
17667 /* Loop unrolling can create multiple blocks that refer to the same
17668 static variable, so we must test for the DW_AT_declaration flag.
17670 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
17671 copy decls and set the DECL_ABSTRACT flag on them instead of
17672 sharing them.
17674 ??? Duplicated blocks have been rewritten to use .debug_ranges.
17676 ??? The declare_in_namespace support causes us to get two DIEs for one
17677 variable, both of which are declarations. We want to avoid considering
17678 one to be a specification, so we must test that this DIE is not a
17679 declaration. */
17680 else if (old_die && TREE_STATIC (decl) && ! declaration
17681 && get_AT_flag (old_die, DW_AT_declaration) == 1)
17683 /* This is a definition of a C++ class level static. */
17684 add_AT_specification (var_die, old_die);
17685 specialization_p = true;
17686 if (DECL_NAME (decl))
17688 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17689 struct dwarf_file_data * file_index = lookup_filename (s.file);
17691 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
17692 add_AT_file (var_die, DW_AT_decl_file, file_index);
17694 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
17695 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
17697 if (old_die->die_tag == DW_TAG_member)
17698 add_linkage_name (var_die, decl);
17701 else
17702 add_name_and_src_coords_attributes (var_die, decl);
17704 if ((origin == NULL && !specialization_p)
17705 || (origin != NULL
17706 && !DECL_ABSTRACT (decl_or_origin)
17707 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
17708 decl_function_context
17709 (decl_or_origin))))
17711 tree type = TREE_TYPE (decl_or_origin);
17713 if (decl_by_reference_p (decl_or_origin))
17714 add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
17715 else
17716 add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
17717 TREE_THIS_VOLATILE (decl_or_origin), context_die);
17720 if (origin == NULL && !specialization_p)
17722 if (TREE_PUBLIC (decl))
17723 add_AT_flag (var_die, DW_AT_external, 1);
17725 if (DECL_ARTIFICIAL (decl))
17726 add_AT_flag (var_die, DW_AT_artificial, 1);
17728 add_accessibility_attribute (var_die, decl);
17731 if (declaration)
17732 add_AT_flag (var_die, DW_AT_declaration, 1);
17734 if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
17735 equate_decl_number_to_die (decl, var_die);
17737 if (! declaration
17738 && (! DECL_ABSTRACT (decl_or_origin)
17739 /* Local static vars are shared between all clones/inlines,
17740 so emit DW_AT_location on the abstract DIE if DECL_RTL is
17741 already set. */
17742 || (TREE_CODE (decl_or_origin) == VAR_DECL
17743 && TREE_STATIC (decl_or_origin)
17744 && DECL_RTL_SET_P (decl_or_origin)))
17745 /* When abstract origin already has DW_AT_location attribute, no need
17746 to add it again. */
17747 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
17749 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
17750 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
17751 defer_location (decl_or_origin, var_die);
17752 else
17753 add_location_or_const_value_attribute (var_die, decl_or_origin,
17754 decl == NULL, DW_AT_location);
17755 add_pubname (decl_or_origin, var_die);
17757 else
17758 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
17761 /* Generate a DIE to represent a named constant. */
17763 static void
17764 gen_const_die (tree decl, dw_die_ref context_die)
17766 dw_die_ref const_die;
17767 tree type = TREE_TYPE (decl);
17769 const_die = new_die (DW_TAG_constant, context_die, decl);
17770 add_name_and_src_coords_attributes (const_die, decl);
17771 add_type_attribute (const_die, type, 1, 0, context_die);
17772 if (TREE_PUBLIC (decl))
17773 add_AT_flag (const_die, DW_AT_external, 1);
17774 if (DECL_ARTIFICIAL (decl))
17775 add_AT_flag (const_die, DW_AT_artificial, 1);
17776 tree_add_const_value_attribute_for_decl (const_die, decl);
17779 /* Generate a DIE to represent a label identifier. */
17781 static void
17782 gen_label_die (tree decl, dw_die_ref context_die)
17784 tree origin = decl_ultimate_origin (decl);
17785 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
17786 rtx insn;
17787 char label[MAX_ARTIFICIAL_LABEL_BYTES];
17789 if (origin != NULL)
17790 add_abstract_origin_attribute (lbl_die, origin);
17791 else
17792 add_name_and_src_coords_attributes (lbl_die, decl);
17794 if (DECL_ABSTRACT (decl))
17795 equate_decl_number_to_die (decl, lbl_die);
17796 else
17798 insn = DECL_RTL_IF_SET (decl);
17800 /* Deleted labels are programmer specified labels which have been
17801 eliminated because of various optimizations. We still emit them
17802 here so that it is possible to put breakpoints on them. */
17803 if (insn
17804 && (LABEL_P (insn)
17805 || ((NOTE_P (insn)
17806 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
17808 /* When optimization is enabled (via -O) some parts of the compiler
17809 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
17810 represent source-level labels which were explicitly declared by
17811 the user. This really shouldn't be happening though, so catch
17812 it if it ever does happen. */
17813 gcc_assert (!INSN_DELETED_P (insn));
17815 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
17816 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
17818 else if (insn
17819 && NOTE_P (insn)
17820 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
17821 && CODE_LABEL_NUMBER (insn) != -1)
17823 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
17824 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
17829 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
17830 attributes to the DIE for a block STMT, to describe where the inlined
17831 function was called from. This is similar to add_src_coords_attributes. */
17833 static inline void
17834 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
17836 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
17838 if (dwarf_version >= 3 || !dwarf_strict)
17840 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
17841 add_AT_unsigned (die, DW_AT_call_line, s.line);
17846 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
17847 Add low_pc and high_pc attributes to the DIE for a block STMT. */
17849 static inline void
17850 add_high_low_attributes (tree stmt, dw_die_ref die)
17852 char label[MAX_ARTIFICIAL_LABEL_BYTES];
17854 if (BLOCK_FRAGMENT_CHAIN (stmt)
17855 && (dwarf_version >= 3 || !dwarf_strict))
17857 tree chain, superblock = NULL_TREE;
17858 dw_die_ref pdie;
17859 dw_attr_ref attr = NULL;
17861 if (inlined_function_outer_scope_p (stmt))
17863 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
17864 BLOCK_NUMBER (stmt));
17865 add_AT_lbl_id (die, DW_AT_entry_pc, label);
17868 /* Optimize duplicate .debug_ranges lists or even tails of
17869 lists. If this BLOCK has same ranges as its supercontext,
17870 lookup DW_AT_ranges attribute in the supercontext (and
17871 recursively so), verify that the ranges_table contains the
17872 right values and use it instead of adding a new .debug_range. */
17873 for (chain = stmt, pdie = die;
17874 BLOCK_SAME_RANGE (chain);
17875 chain = BLOCK_SUPERCONTEXT (chain))
17877 dw_attr_ref new_attr;
17879 pdie = pdie->die_parent;
17880 if (pdie == NULL)
17881 break;
17882 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
17883 break;
17884 new_attr = get_AT (pdie, DW_AT_ranges);
17885 if (new_attr == NULL
17886 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
17887 break;
17888 attr = new_attr;
17889 superblock = BLOCK_SUPERCONTEXT (chain);
17891 if (attr != NULL
17892 && (ranges_table[attr->dw_attr_val.v.val_offset
17893 / 2 / DWARF2_ADDR_SIZE].num
17894 == BLOCK_NUMBER (superblock))
17895 && BLOCK_FRAGMENT_CHAIN (superblock))
17897 unsigned long off = attr->dw_attr_val.v.val_offset
17898 / 2 / DWARF2_ADDR_SIZE;
17899 unsigned long supercnt = 0, thiscnt = 0;
17900 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
17901 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
17903 ++supercnt;
17904 gcc_checking_assert (ranges_table[off + supercnt].num
17905 == BLOCK_NUMBER (chain));
17907 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
17908 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
17909 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
17910 ++thiscnt;
17911 gcc_assert (supercnt >= thiscnt);
17912 add_AT_range_list (die, DW_AT_ranges,
17913 (off + supercnt - thiscnt)
17914 * 2 * DWARF2_ADDR_SIZE);
17915 return;
17918 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
17920 chain = BLOCK_FRAGMENT_CHAIN (stmt);
17923 add_ranges (chain);
17924 chain = BLOCK_FRAGMENT_CHAIN (chain);
17926 while (chain);
17927 add_ranges (NULL);
17929 else
17931 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
17932 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
17933 BLOCK_NUMBER (stmt));
17934 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
17935 BLOCK_NUMBER (stmt));
17936 add_AT_low_high_pc (die, label, label_high);
17940 /* Generate a DIE for a lexical block. */
17942 static void
17943 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
17945 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
17947 if (call_arg_locations)
17949 if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
17950 VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
17951 BLOCK_NUMBER (stmt) + 1);
17952 VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), stmt_die);
17955 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
17956 add_high_low_attributes (stmt, stmt_die);
17958 decls_for_scope (stmt, stmt_die, depth);
17961 /* Generate a DIE for an inlined subprogram. */
17963 static void
17964 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
17966 tree decl;
17968 /* The instance of function that is effectively being inlined shall not
17969 be abstract. */
17970 gcc_assert (! BLOCK_ABSTRACT (stmt));
17972 decl = block_ultimate_origin (stmt);
17974 /* Emit info for the abstract instance first, if we haven't yet. We
17975 must emit this even if the block is abstract, otherwise when we
17976 emit the block below (or elsewhere), we may end up trying to emit
17977 a die whose origin die hasn't been emitted, and crashing. */
17978 dwarf2out_abstract_function (decl);
17980 if (! BLOCK_ABSTRACT (stmt))
17982 dw_die_ref subr_die
17983 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
17985 if (call_arg_locations)
17987 if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
17988 VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
17989 BLOCK_NUMBER (stmt) + 1);
17990 VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), subr_die);
17992 add_abstract_origin_attribute (subr_die, decl);
17993 if (TREE_ASM_WRITTEN (stmt))
17994 add_high_low_attributes (stmt, subr_die);
17995 add_call_src_coords_attributes (stmt, subr_die);
17997 decls_for_scope (stmt, subr_die, depth);
17998 current_function_has_inlines = 1;
18002 /* Generate a DIE for a field in a record, or structure. */
18004 static void
18005 gen_field_die (tree decl, dw_die_ref context_die)
18007 dw_die_ref decl_die;
18009 if (TREE_TYPE (decl) == error_mark_node)
18010 return;
18012 decl_die = new_die (DW_TAG_member, context_die, decl);
18013 add_name_and_src_coords_attributes (decl_die, decl);
18014 add_type_attribute (decl_die, member_declared_type (decl),
18015 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
18016 context_die);
18018 if (DECL_BIT_FIELD_TYPE (decl))
18020 add_byte_size_attribute (decl_die, decl);
18021 add_bit_size_attribute (decl_die, decl);
18022 add_bit_offset_attribute (decl_die, decl);
18025 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
18026 add_data_member_location_attribute (decl_die, decl);
18028 if (DECL_ARTIFICIAL (decl))
18029 add_AT_flag (decl_die, DW_AT_artificial, 1);
18031 add_accessibility_attribute (decl_die, decl);
18033 /* Equate decl number to die, so that we can look up this decl later on. */
18034 equate_decl_number_to_die (decl, decl_die);
18037 #if 0
18038 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18039 Use modified_type_die instead.
18040 We keep this code here just in case these types of DIEs may be needed to
18041 represent certain things in other languages (e.g. Pascal) someday. */
18043 static void
18044 gen_pointer_type_die (tree type, dw_die_ref context_die)
18046 dw_die_ref ptr_die
18047 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
18049 equate_type_number_to_die (type, ptr_die);
18050 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18051 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18054 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18055 Use modified_type_die instead.
18056 We keep this code here just in case these types of DIEs may be needed to
18057 represent certain things in other languages (e.g. Pascal) someday. */
18059 static void
18060 gen_reference_type_die (tree type, dw_die_ref context_die)
18062 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
18064 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
18065 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
18066 else
18067 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
18069 equate_type_number_to_die (type, ref_die);
18070 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
18071 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18073 #endif
18075 /* Generate a DIE for a pointer to a member type. */
18077 static void
18078 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
18080 dw_die_ref ptr_die
18081 = new_die (DW_TAG_ptr_to_member_type,
18082 scope_die_for (type, context_die), type);
18084 equate_type_number_to_die (type, ptr_die);
18085 add_AT_die_ref (ptr_die, DW_AT_containing_type,
18086 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
18087 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18090 typedef const char *dchar_p; /* For DEF_VEC_P. */
18091 DEF_VEC_P(dchar_p);
18092 DEF_VEC_ALLOC_P(dchar_p,heap);
18094 static char *producer_string;
18096 /* Return a heap allocated producer string including command line options
18097 if -grecord-gcc-switches. */
18099 static char *
18100 gen_producer_string (void)
18102 size_t j;
18103 VEC(dchar_p, heap) *switches = NULL;
18104 const char *language_string = lang_hooks.name;
18105 char *producer, *tail;
18106 const char *p;
18107 size_t len = dwarf_record_gcc_switches ? 0 : 3;
18108 size_t plen = strlen (language_string) + 1 + strlen (version_string);
18110 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
18111 switch (save_decoded_options[j].opt_index)
18113 case OPT_o:
18114 case OPT_d:
18115 case OPT_dumpbase:
18116 case OPT_dumpdir:
18117 case OPT_auxbase:
18118 case OPT_auxbase_strip:
18119 case OPT_quiet:
18120 case OPT_version:
18121 case OPT_v:
18122 case OPT_w:
18123 case OPT_L:
18124 case OPT_D:
18125 case OPT_I:
18126 case OPT_U:
18127 case OPT_SPECIAL_unknown:
18128 case OPT_SPECIAL_ignore:
18129 case OPT_SPECIAL_program_name:
18130 case OPT_SPECIAL_input_file:
18131 case OPT_grecord_gcc_switches:
18132 case OPT_gno_record_gcc_switches:
18133 case OPT__output_pch_:
18134 case OPT_fdiagnostics_show_location_:
18135 case OPT_fdiagnostics_show_option:
18136 case OPT_fdiagnostics_show_caret:
18137 case OPT_fverbose_asm:
18138 case OPT____:
18139 case OPT__sysroot_:
18140 case OPT_nostdinc:
18141 case OPT_nostdinc__:
18142 /* Ignore these. */
18143 continue;
18144 default:
18145 if (cl_options[save_decoded_options[j].opt_index].flags
18146 & CL_NO_DWARF_RECORD)
18147 continue;
18148 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
18149 == '-');
18150 switch (save_decoded_options[j].canonical_option[0][1])
18152 case 'M':
18153 case 'i':
18154 case 'W':
18155 continue;
18156 case 'f':
18157 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
18158 "dump", 4) == 0)
18159 continue;
18160 break;
18161 default:
18162 break;
18164 VEC_safe_push (dchar_p, heap, switches,
18165 save_decoded_options[j].orig_option_with_args_text);
18166 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
18167 break;
18170 producer = XNEWVEC (char, plen + 1 + len + 1);
18171 tail = producer;
18172 sprintf (tail, "%s %s", language_string, version_string);
18173 tail += plen;
18175 FOR_EACH_VEC_ELT (dchar_p, switches, j, p)
18177 len = strlen (p);
18178 *tail = ' ';
18179 memcpy (tail + 1, p, len);
18180 tail += len + 1;
18183 *tail = '\0';
18184 VEC_free (dchar_p, heap, switches);
18185 return producer;
18188 /* Generate the DIE for the compilation unit. */
18190 static dw_die_ref
18191 gen_compile_unit_die (const char *filename)
18193 dw_die_ref die;
18194 const char *language_string = lang_hooks.name;
18195 int language;
18197 die = new_die (DW_TAG_compile_unit, NULL, NULL);
18199 if (filename)
18201 add_name_attribute (die, filename);
18202 /* Don't add cwd for <built-in>. */
18203 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
18204 add_comp_dir_attribute (die);
18207 if (producer_string == NULL)
18208 producer_string = gen_producer_string ();
18209 add_AT_string (die, DW_AT_producer, producer_string);
18211 /* If our producer is LTO try to figure out a common language to use
18212 from the global list of translation units. */
18213 if (strcmp (language_string, "GNU GIMPLE") == 0)
18215 unsigned i;
18216 tree t;
18217 const char *common_lang = NULL;
18219 FOR_EACH_VEC_ELT (tree, all_translation_units, i, t)
18221 if (!TRANSLATION_UNIT_LANGUAGE (t))
18222 continue;
18223 if (!common_lang)
18224 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
18225 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
18227 else if (strncmp (common_lang, "GNU C", 5) == 0
18228 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
18229 /* Mixing C and C++ is ok, use C++ in that case. */
18230 common_lang = "GNU C++";
18231 else
18233 /* Fall back to C. */
18234 common_lang = NULL;
18235 break;
18239 if (common_lang)
18240 language_string = common_lang;
18243 language = DW_LANG_C89;
18244 if (strcmp (language_string, "GNU C++") == 0)
18245 language = DW_LANG_C_plus_plus;
18246 else if (strcmp (language_string, "GNU F77") == 0)
18247 language = DW_LANG_Fortran77;
18248 else if (strcmp (language_string, "GNU Pascal") == 0)
18249 language = DW_LANG_Pascal83;
18250 else if (dwarf_version >= 3 || !dwarf_strict)
18252 if (strcmp (language_string, "GNU Ada") == 0)
18253 language = DW_LANG_Ada95;
18254 else if (strcmp (language_string, "GNU Fortran") == 0)
18255 language = DW_LANG_Fortran95;
18256 else if (strcmp (language_string, "GNU Java") == 0)
18257 language = DW_LANG_Java;
18258 else if (strcmp (language_string, "GNU Objective-C") == 0)
18259 language = DW_LANG_ObjC;
18260 else if (strcmp (language_string, "GNU Objective-C++") == 0)
18261 language = DW_LANG_ObjC_plus_plus;
18262 else if (dwarf_version >= 5 || !dwarf_strict)
18264 if (strcmp (language_string, "GNU Go") == 0)
18265 language = DW_LANG_Go;
18268 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
18269 else if (strcmp (language_string, "GNU Fortran") == 0)
18270 language = DW_LANG_Fortran90;
18272 add_AT_unsigned (die, DW_AT_language, language);
18274 switch (language)
18276 case DW_LANG_Fortran77:
18277 case DW_LANG_Fortran90:
18278 case DW_LANG_Fortran95:
18279 /* Fortran has case insensitive identifiers and the front-end
18280 lowercases everything. */
18281 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
18282 break;
18283 default:
18284 /* The default DW_ID_case_sensitive doesn't need to be specified. */
18285 break;
18287 return die;
18290 /* Generate the DIE for a base class. */
18292 static void
18293 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
18295 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
18297 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
18298 add_data_member_location_attribute (die, binfo);
18300 if (BINFO_VIRTUAL_P (binfo))
18301 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
18303 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
18304 children, otherwise the default is DW_ACCESS_public. In DWARF2
18305 the default has always been DW_ACCESS_private. */
18306 if (access == access_public_node)
18308 if (dwarf_version == 2
18309 || context_die->die_tag == DW_TAG_class_type)
18310 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
18312 else if (access == access_protected_node)
18313 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
18314 else if (dwarf_version > 2
18315 && context_die->die_tag != DW_TAG_class_type)
18316 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
18319 /* Generate a DIE for a class member. */
18321 static void
18322 gen_member_die (tree type, dw_die_ref context_die)
18324 tree member;
18325 tree binfo = TYPE_BINFO (type);
18326 dw_die_ref child;
18328 /* If this is not an incomplete type, output descriptions of each of its
18329 members. Note that as we output the DIEs necessary to represent the
18330 members of this record or union type, we will also be trying to output
18331 DIEs to represent the *types* of those members. However the `type'
18332 function (above) will specifically avoid generating type DIEs for member
18333 types *within* the list of member DIEs for this (containing) type except
18334 for those types (of members) which are explicitly marked as also being
18335 members of this (containing) type themselves. The g++ front- end can
18336 force any given type to be treated as a member of some other (containing)
18337 type by setting the TYPE_CONTEXT of the given (member) type to point to
18338 the TREE node representing the appropriate (containing) type. */
18340 /* First output info about the base classes. */
18341 if (binfo)
18343 VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
18344 int i;
18345 tree base;
18347 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
18348 gen_inheritance_die (base,
18349 (accesses ? VEC_index (tree, accesses, i)
18350 : access_public_node), context_die);
18353 /* Now output info about the data members and type members. */
18354 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
18356 /* If we thought we were generating minimal debug info for TYPE
18357 and then changed our minds, some of the member declarations
18358 may have already been defined. Don't define them again, but
18359 do put them in the right order. */
18361 child = lookup_decl_die (member);
18362 if (child)
18363 splice_child_die (context_die, child);
18364 else
18365 gen_decl_die (member, NULL, context_die);
18368 /* Now output info about the function members (if any). */
18369 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
18371 /* Don't include clones in the member list. */
18372 if (DECL_ABSTRACT_ORIGIN (member))
18373 continue;
18375 child = lookup_decl_die (member);
18376 if (child)
18377 splice_child_die (context_die, child);
18378 else
18379 gen_decl_die (member, NULL, context_die);
18383 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
18384 is set, we pretend that the type was never defined, so we only get the
18385 member DIEs needed by later specification DIEs. */
18387 static void
18388 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
18389 enum debug_info_usage usage)
18391 dw_die_ref type_die = lookup_type_die (type);
18392 dw_die_ref scope_die = 0;
18393 int nested = 0;
18394 int complete = (TYPE_SIZE (type)
18395 && (! TYPE_STUB_DECL (type)
18396 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
18397 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
18398 complete = complete && should_emit_struct_debug (type, usage);
18400 if (type_die && ! complete)
18401 return;
18403 if (TYPE_CONTEXT (type) != NULL_TREE
18404 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
18405 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
18406 nested = 1;
18408 scope_die = scope_die_for (type, context_die);
18410 if (! type_die || (nested && is_cu_die (scope_die)))
18411 /* First occurrence of type or toplevel definition of nested class. */
18413 dw_die_ref old_die = type_die;
18415 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
18416 ? record_type_tag (type) : DW_TAG_union_type,
18417 scope_die, type);
18418 equate_type_number_to_die (type, type_die);
18419 if (old_die)
18420 add_AT_specification (type_die, old_die);
18421 else
18422 add_name_attribute (type_die, type_tag (type));
18424 else
18425 remove_AT (type_die, DW_AT_declaration);
18427 /* Generate child dies for template paramaters. */
18428 if (debug_info_level > DINFO_LEVEL_TERSE
18429 && COMPLETE_TYPE_P (type))
18430 schedule_generic_params_dies_gen (type);
18432 /* If this type has been completed, then give it a byte_size attribute and
18433 then give a list of members. */
18434 if (complete && !ns_decl)
18436 /* Prevent infinite recursion in cases where the type of some member of
18437 this type is expressed in terms of this type itself. */
18438 TREE_ASM_WRITTEN (type) = 1;
18439 add_byte_size_attribute (type_die, type);
18440 if (TYPE_STUB_DECL (type) != NULL_TREE)
18442 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
18443 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
18446 /* If the first reference to this type was as the return type of an
18447 inline function, then it may not have a parent. Fix this now. */
18448 if (type_die->die_parent == NULL)
18449 add_child_die (scope_die, type_die);
18451 push_decl_scope (type);
18452 gen_member_die (type, type_die);
18453 pop_decl_scope ();
18455 add_gnat_descriptive_type_attribute (type_die, type, context_die);
18456 if (TYPE_ARTIFICIAL (type))
18457 add_AT_flag (type_die, DW_AT_artificial, 1);
18459 /* GNU extension: Record what type our vtable lives in. */
18460 if (TYPE_VFIELD (type))
18462 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
18464 gen_type_die (vtype, context_die);
18465 add_AT_die_ref (type_die, DW_AT_containing_type,
18466 lookup_type_die (vtype));
18469 else
18471 add_AT_flag (type_die, DW_AT_declaration, 1);
18473 /* We don't need to do this for function-local types. */
18474 if (TYPE_STUB_DECL (type)
18475 && ! decl_function_context (TYPE_STUB_DECL (type)))
18476 VEC_safe_push (tree, gc, incomplete_types, type);
18479 if (get_AT (type_die, DW_AT_name))
18480 add_pubtype (type, type_die);
18483 /* Generate a DIE for a subroutine _type_. */
18485 static void
18486 gen_subroutine_type_die (tree type, dw_die_ref context_die)
18488 tree return_type = TREE_TYPE (type);
18489 dw_die_ref subr_die
18490 = new_die (DW_TAG_subroutine_type,
18491 scope_die_for (type, context_die), type);
18493 equate_type_number_to_die (type, subr_die);
18494 add_prototyped_attribute (subr_die, type);
18495 add_type_attribute (subr_die, return_type, 0, 0, context_die);
18496 gen_formal_types_die (type, subr_die);
18498 if (get_AT (subr_die, DW_AT_name))
18499 add_pubtype (type, subr_die);
18502 /* Generate a DIE for a type definition. */
18504 static void
18505 gen_typedef_die (tree decl, dw_die_ref context_die)
18507 dw_die_ref type_die;
18508 tree origin;
18510 if (TREE_ASM_WRITTEN (decl))
18511 return;
18513 TREE_ASM_WRITTEN (decl) = 1;
18514 type_die = new_die (DW_TAG_typedef, context_die, decl);
18515 origin = decl_ultimate_origin (decl);
18516 if (origin != NULL)
18517 add_abstract_origin_attribute (type_die, origin);
18518 else
18520 tree type;
18522 add_name_and_src_coords_attributes (type_die, decl);
18523 if (DECL_ORIGINAL_TYPE (decl))
18525 type = DECL_ORIGINAL_TYPE (decl);
18527 gcc_assert (type != TREE_TYPE (decl));
18528 equate_type_number_to_die (TREE_TYPE (decl), type_die);
18530 else
18532 type = TREE_TYPE (decl);
18534 if (is_naming_typedef_decl (TYPE_NAME (type)))
18536 /* Here, we are in the case of decl being a typedef naming
18537 an anonymous type, e.g:
18538 typedef struct {...} foo;
18539 In that case TREE_TYPE (decl) is not a typedef variant
18540 type and TYPE_NAME of the anonymous type is set to the
18541 TYPE_DECL of the typedef. This construct is emitted by
18542 the C++ FE.
18544 TYPE is the anonymous struct named by the typedef
18545 DECL. As we need the DW_AT_type attribute of the
18546 DW_TAG_typedef to point to the DIE of TYPE, let's
18547 generate that DIE right away. add_type_attribute
18548 called below will then pick (via lookup_type_die) that
18549 anonymous struct DIE. */
18550 if (!TREE_ASM_WRITTEN (type))
18551 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
18553 /* This is a GNU Extension. We are adding a
18554 DW_AT_linkage_name attribute to the DIE of the
18555 anonymous struct TYPE. The value of that attribute
18556 is the name of the typedef decl naming the anonymous
18557 struct. This greatly eases the work of consumers of
18558 this debug info. */
18559 add_linkage_attr (lookup_type_die (type), decl);
18563 add_type_attribute (type_die, type, TREE_READONLY (decl),
18564 TREE_THIS_VOLATILE (decl), context_die);
18566 if (is_naming_typedef_decl (decl))
18567 /* We want that all subsequent calls to lookup_type_die with
18568 TYPE in argument yield the DW_TAG_typedef we have just
18569 created. */
18570 equate_type_number_to_die (type, type_die);
18572 add_accessibility_attribute (type_die, decl);
18575 if (DECL_ABSTRACT (decl))
18576 equate_decl_number_to_die (decl, type_die);
18578 if (get_AT (type_die, DW_AT_name))
18579 add_pubtype (decl, type_die);
18582 /* Generate a DIE for a struct, class, enum or union type. */
18584 static void
18585 gen_tagged_type_die (tree type,
18586 dw_die_ref context_die,
18587 enum debug_info_usage usage)
18589 int need_pop;
18591 if (type == NULL_TREE
18592 || !is_tagged_type (type))
18593 return;
18595 /* If this is a nested type whose containing class hasn't been written
18596 out yet, writing it out will cover this one, too. This does not apply
18597 to instantiations of member class templates; they need to be added to
18598 the containing class as they are generated. FIXME: This hurts the
18599 idea of combining type decls from multiple TUs, since we can't predict
18600 what set of template instantiations we'll get. */
18601 if (TYPE_CONTEXT (type)
18602 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
18603 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
18605 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
18607 if (TREE_ASM_WRITTEN (type))
18608 return;
18610 /* If that failed, attach ourselves to the stub. */
18611 push_decl_scope (TYPE_CONTEXT (type));
18612 context_die = lookup_type_die (TYPE_CONTEXT (type));
18613 need_pop = 1;
18615 else if (TYPE_CONTEXT (type) != NULL_TREE
18616 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
18618 /* If this type is local to a function that hasn't been written
18619 out yet, use a NULL context for now; it will be fixed up in
18620 decls_for_scope. */
18621 context_die = lookup_decl_die (TYPE_CONTEXT (type));
18622 /* A declaration DIE doesn't count; nested types need to go in the
18623 specification. */
18624 if (context_die && is_declaration_die (context_die))
18625 context_die = NULL;
18626 need_pop = 0;
18628 else
18630 context_die = declare_in_namespace (type, context_die);
18631 need_pop = 0;
18634 if (TREE_CODE (type) == ENUMERAL_TYPE)
18636 /* This might have been written out by the call to
18637 declare_in_namespace. */
18638 if (!TREE_ASM_WRITTEN (type))
18639 gen_enumeration_type_die (type, context_die);
18641 else
18642 gen_struct_or_union_type_die (type, context_die, usage);
18644 if (need_pop)
18645 pop_decl_scope ();
18647 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
18648 it up if it is ever completed. gen_*_type_die will set it for us
18649 when appropriate. */
18652 /* Generate a type description DIE. */
18654 static void
18655 gen_type_die_with_usage (tree type, dw_die_ref context_die,
18656 enum debug_info_usage usage)
18658 struct array_descr_info info;
18660 if (type == NULL_TREE || type == error_mark_node)
18661 return;
18663 if (TYPE_NAME (type) != NULL_TREE
18664 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
18665 && is_redundant_typedef (TYPE_NAME (type))
18666 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
18667 /* The DECL of this type is a typedef we don't want to emit debug
18668 info for but we want debug info for its underlying typedef.
18669 This can happen for e.g, the injected-class-name of a C++
18670 type. */
18671 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
18673 /* If TYPE is a typedef type variant, let's generate debug info
18674 for the parent typedef which TYPE is a type of. */
18675 if (typedef_variant_p (type))
18677 if (TREE_ASM_WRITTEN (type))
18678 return;
18680 /* Prevent broken recursion; we can't hand off to the same type. */
18681 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
18683 /* Give typedefs the right scope. */
18684 context_die = scope_die_for (type, context_die);
18686 TREE_ASM_WRITTEN (type) = 1;
18688 gen_decl_die (TYPE_NAME (type), NULL, context_die);
18689 return;
18692 /* If type is an anonymous tagged type named by a typedef, let's
18693 generate debug info for the typedef. */
18694 if (is_naming_typedef_decl (TYPE_NAME (type)))
18696 /* Use the DIE of the containing namespace as the parent DIE of
18697 the type description DIE we want to generate. */
18698 if (DECL_CONTEXT (TYPE_NAME (type))
18699 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
18700 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
18702 gen_decl_die (TYPE_NAME (type), NULL, context_die);
18703 return;
18706 /* If this is an array type with hidden descriptor, handle it first. */
18707 if (!TREE_ASM_WRITTEN (type)
18708 && lang_hooks.types.get_array_descr_info
18709 && lang_hooks.types.get_array_descr_info (type, &info)
18710 && (dwarf_version >= 3 || !dwarf_strict))
18712 gen_descr_array_type_die (type, &info, context_die);
18713 TREE_ASM_WRITTEN (type) = 1;
18714 return;
18717 /* We are going to output a DIE to represent the unqualified version
18718 of this type (i.e. without any const or volatile qualifiers) so
18719 get the main variant (i.e. the unqualified version) of this type
18720 now. (Vectors are special because the debugging info is in the
18721 cloned type itself). */
18722 if (TREE_CODE (type) != VECTOR_TYPE)
18723 type = type_main_variant (type);
18725 if (TREE_ASM_WRITTEN (type))
18726 return;
18728 switch (TREE_CODE (type))
18730 case ERROR_MARK:
18731 break;
18733 case POINTER_TYPE:
18734 case REFERENCE_TYPE:
18735 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
18736 ensures that the gen_type_die recursion will terminate even if the
18737 type is recursive. Recursive types are possible in Ada. */
18738 /* ??? We could perhaps do this for all types before the switch
18739 statement. */
18740 TREE_ASM_WRITTEN (type) = 1;
18742 /* For these types, all that is required is that we output a DIE (or a
18743 set of DIEs) to represent the "basis" type. */
18744 gen_type_die_with_usage (TREE_TYPE (type), context_die,
18745 DINFO_USAGE_IND_USE);
18746 break;
18748 case OFFSET_TYPE:
18749 /* This code is used for C++ pointer-to-data-member types.
18750 Output a description of the relevant class type. */
18751 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
18752 DINFO_USAGE_IND_USE);
18754 /* Output a description of the type of the object pointed to. */
18755 gen_type_die_with_usage (TREE_TYPE (type), context_die,
18756 DINFO_USAGE_IND_USE);
18758 /* Now output a DIE to represent this pointer-to-data-member type
18759 itself. */
18760 gen_ptr_to_mbr_type_die (type, context_die);
18761 break;
18763 case FUNCTION_TYPE:
18764 /* Force out return type (in case it wasn't forced out already). */
18765 gen_type_die_with_usage (TREE_TYPE (type), context_die,
18766 DINFO_USAGE_DIR_USE);
18767 gen_subroutine_type_die (type, context_die);
18768 break;
18770 case METHOD_TYPE:
18771 /* Force out return type (in case it wasn't forced out already). */
18772 gen_type_die_with_usage (TREE_TYPE (type), context_die,
18773 DINFO_USAGE_DIR_USE);
18774 gen_subroutine_type_die (type, context_die);
18775 break;
18777 case ARRAY_TYPE:
18778 gen_array_type_die (type, context_die);
18779 break;
18781 case VECTOR_TYPE:
18782 gen_array_type_die (type, context_die);
18783 break;
18785 case ENUMERAL_TYPE:
18786 case RECORD_TYPE:
18787 case UNION_TYPE:
18788 case QUAL_UNION_TYPE:
18789 gen_tagged_type_die (type, context_die, usage);
18790 return;
18792 case VOID_TYPE:
18793 case INTEGER_TYPE:
18794 case REAL_TYPE:
18795 case FIXED_POINT_TYPE:
18796 case COMPLEX_TYPE:
18797 case BOOLEAN_TYPE:
18798 /* No DIEs needed for fundamental types. */
18799 break;
18801 case NULLPTR_TYPE:
18802 case LANG_TYPE:
18803 /* Just use DW_TAG_unspecified_type. */
18805 dw_die_ref type_die = lookup_type_die (type);
18806 if (type_die == NULL)
18808 tree name = TYPE_NAME (type);
18809 if (TREE_CODE (name) == TYPE_DECL)
18810 name = DECL_NAME (name);
18811 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
18812 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
18813 equate_type_number_to_die (type, type_die);
18816 break;
18818 default:
18819 gcc_unreachable ();
18822 TREE_ASM_WRITTEN (type) = 1;
18825 static void
18826 gen_type_die (tree type, dw_die_ref context_die)
18828 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
18831 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
18832 things which are local to the given block. */
18834 static void
18835 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
18837 int must_output_die = 0;
18838 bool inlined_func;
18840 /* Ignore blocks that are NULL. */
18841 if (stmt == NULL_TREE)
18842 return;
18844 inlined_func = inlined_function_outer_scope_p (stmt);
18846 /* If the block is one fragment of a non-contiguous block, do not
18847 process the variables, since they will have been done by the
18848 origin block. Do process subblocks. */
18849 if (BLOCK_FRAGMENT_ORIGIN (stmt))
18851 tree sub;
18853 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
18854 gen_block_die (sub, context_die, depth + 1);
18856 return;
18859 /* Determine if we need to output any Dwarf DIEs at all to represent this
18860 block. */
18861 if (inlined_func)
18862 /* The outer scopes for inlinings *must* always be represented. We
18863 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
18864 must_output_die = 1;
18865 else
18867 /* Determine if this block directly contains any "significant"
18868 local declarations which we will need to output DIEs for. */
18869 if (debug_info_level > DINFO_LEVEL_TERSE)
18870 /* We are not in terse mode so *any* local declaration counts
18871 as being a "significant" one. */
18872 must_output_die = ((BLOCK_VARS (stmt) != NULL
18873 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
18874 && (TREE_USED (stmt)
18875 || TREE_ASM_WRITTEN (stmt)
18876 || BLOCK_ABSTRACT (stmt)));
18877 else if ((TREE_USED (stmt)
18878 || TREE_ASM_WRITTEN (stmt)
18879 || BLOCK_ABSTRACT (stmt))
18880 && !dwarf2out_ignore_block (stmt))
18881 must_output_die = 1;
18884 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
18885 DIE for any block which contains no significant local declarations at
18886 all. Rather, in such cases we just call `decls_for_scope' so that any
18887 needed Dwarf info for any sub-blocks will get properly generated. Note
18888 that in terse mode, our definition of what constitutes a "significant"
18889 local declaration gets restricted to include only inlined function
18890 instances and local (nested) function definitions. */
18891 if (must_output_die)
18893 if (inlined_func)
18895 /* If STMT block is abstract, that means we have been called
18896 indirectly from dwarf2out_abstract_function.
18897 That function rightfully marks the descendent blocks (of
18898 the abstract function it is dealing with) as being abstract,
18899 precisely to prevent us from emitting any
18900 DW_TAG_inlined_subroutine DIE as a descendent
18901 of an abstract function instance. So in that case, we should
18902 not call gen_inlined_subroutine_die.
18904 Later though, when cgraph asks dwarf2out to emit info
18905 for the concrete instance of the function decl into which
18906 the concrete instance of STMT got inlined, the later will lead
18907 to the generation of a DW_TAG_inlined_subroutine DIE. */
18908 if (! BLOCK_ABSTRACT (stmt))
18909 gen_inlined_subroutine_die (stmt, context_die, depth);
18911 else
18912 gen_lexical_block_die (stmt, context_die, depth);
18914 else
18915 decls_for_scope (stmt, context_die, depth);
18918 /* Process variable DECL (or variable with origin ORIGIN) within
18919 block STMT and add it to CONTEXT_DIE. */
18920 static void
18921 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
18923 dw_die_ref die;
18924 tree decl_or_origin = decl ? decl : origin;
18926 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
18927 die = lookup_decl_die (decl_or_origin);
18928 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
18929 && TYPE_DECL_IS_STUB (decl_or_origin))
18930 die = lookup_type_die (TREE_TYPE (decl_or_origin));
18931 else
18932 die = NULL;
18934 if (die != NULL && die->die_parent == NULL)
18935 add_child_die (context_die, die);
18936 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
18937 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
18938 stmt, context_die);
18939 else
18940 gen_decl_die (decl, origin, context_die);
18943 /* Generate all of the decls declared within a given scope and (recursively)
18944 all of its sub-blocks. */
18946 static void
18947 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
18949 tree decl;
18950 unsigned int i;
18951 tree subblocks;
18953 /* Ignore NULL blocks. */
18954 if (stmt == NULL_TREE)
18955 return;
18957 /* Output the DIEs to represent all of the data objects and typedefs
18958 declared directly within this block but not within any nested
18959 sub-blocks. Also, nested function and tag DIEs have been
18960 generated with a parent of NULL; fix that up now. */
18961 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
18962 process_scope_var (stmt, decl, NULL_TREE, context_die);
18963 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
18964 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
18965 context_die);
18967 /* If we're at -g1, we're not interested in subblocks. */
18968 if (debug_info_level <= DINFO_LEVEL_TERSE)
18969 return;
18971 /* Output the DIEs to represent all sub-blocks (and the items declared
18972 therein) of this block. */
18973 for (subblocks = BLOCK_SUBBLOCKS (stmt);
18974 subblocks != NULL;
18975 subblocks = BLOCK_CHAIN (subblocks))
18976 gen_block_die (subblocks, context_die, depth + 1);
18979 /* Is this a typedef we can avoid emitting? */
18981 static inline int
18982 is_redundant_typedef (const_tree decl)
18984 if (TYPE_DECL_IS_STUB (decl))
18985 return 1;
18987 if (DECL_ARTIFICIAL (decl)
18988 && DECL_CONTEXT (decl)
18989 && is_tagged_type (DECL_CONTEXT (decl))
18990 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
18991 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
18992 /* Also ignore the artificial member typedef for the class name. */
18993 return 1;
18995 return 0;
18998 /* Return TRUE if TYPE is a typedef that names a type for linkage
18999 purposes. This kind of typedefs is produced by the C++ FE for
19000 constructs like:
19002 typedef struct {...} foo;
19004 In that case, there is no typedef variant type produced for foo.
19005 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
19006 struct type. */
19008 static bool
19009 is_naming_typedef_decl (const_tree decl)
19011 if (decl == NULL_TREE
19012 || TREE_CODE (decl) != TYPE_DECL
19013 || !is_tagged_type (TREE_TYPE (decl))
19014 || DECL_IS_BUILTIN (decl)
19015 || is_redundant_typedef (decl)
19016 /* It looks like Ada produces TYPE_DECLs that are very similar
19017 to C++ naming typedefs but that have different
19018 semantics. Let's be specific to c++ for now. */
19019 || !is_cxx ())
19020 return FALSE;
19022 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
19023 && TYPE_NAME (TREE_TYPE (decl)) == decl
19024 && (TYPE_STUB_DECL (TREE_TYPE (decl))
19025 != TYPE_NAME (TREE_TYPE (decl))));
19028 /* Returns the DIE for a context. */
19030 static inline dw_die_ref
19031 get_context_die (tree context)
19033 if (context)
19035 /* Find die that represents this context. */
19036 if (TYPE_P (context))
19038 context = TYPE_MAIN_VARIANT (context);
19039 return strip_naming_typedef (context, force_type_die (context));
19041 else
19042 return force_decl_die (context);
19044 return comp_unit_die ();
19047 /* Returns the DIE for decl. A DIE will always be returned. */
19049 static dw_die_ref
19050 force_decl_die (tree decl)
19052 dw_die_ref decl_die;
19053 unsigned saved_external_flag;
19054 tree save_fn = NULL_TREE;
19055 decl_die = lookup_decl_die (decl);
19056 if (!decl_die)
19058 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
19060 decl_die = lookup_decl_die (decl);
19061 if (decl_die)
19062 return decl_die;
19064 switch (TREE_CODE (decl))
19066 case FUNCTION_DECL:
19067 /* Clear current_function_decl, so that gen_subprogram_die thinks
19068 that this is a declaration. At this point, we just want to force
19069 declaration die. */
19070 save_fn = current_function_decl;
19071 current_function_decl = NULL_TREE;
19072 gen_subprogram_die (decl, context_die);
19073 current_function_decl = save_fn;
19074 break;
19076 case VAR_DECL:
19077 /* Set external flag to force declaration die. Restore it after
19078 gen_decl_die() call. */
19079 saved_external_flag = DECL_EXTERNAL (decl);
19080 DECL_EXTERNAL (decl) = 1;
19081 gen_decl_die (decl, NULL, context_die);
19082 DECL_EXTERNAL (decl) = saved_external_flag;
19083 break;
19085 case NAMESPACE_DECL:
19086 if (dwarf_version >= 3 || !dwarf_strict)
19087 dwarf2out_decl (decl);
19088 else
19089 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
19090 decl_die = comp_unit_die ();
19091 break;
19093 case TRANSLATION_UNIT_DECL:
19094 decl_die = comp_unit_die ();
19095 break;
19097 default:
19098 gcc_unreachable ();
19101 /* We should be able to find the DIE now. */
19102 if (!decl_die)
19103 decl_die = lookup_decl_die (decl);
19104 gcc_assert (decl_die);
19107 return decl_die;
19110 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
19111 always returned. */
19113 static dw_die_ref
19114 force_type_die (tree type)
19116 dw_die_ref type_die;
19118 type_die = lookup_type_die (type);
19119 if (!type_die)
19121 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
19123 type_die = modified_type_die (type, TYPE_READONLY (type),
19124 TYPE_VOLATILE (type), context_die);
19125 gcc_assert (type_die);
19127 return type_die;
19130 /* Force out any required namespaces to be able to output DECL,
19131 and return the new context_die for it, if it's changed. */
19133 static dw_die_ref
19134 setup_namespace_context (tree thing, dw_die_ref context_die)
19136 tree context = (DECL_P (thing)
19137 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
19138 if (context && TREE_CODE (context) == NAMESPACE_DECL)
19139 /* Force out the namespace. */
19140 context_die = force_decl_die (context);
19142 return context_die;
19145 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
19146 type) within its namespace, if appropriate.
19148 For compatibility with older debuggers, namespace DIEs only contain
19149 declarations; all definitions are emitted at CU scope. */
19151 static dw_die_ref
19152 declare_in_namespace (tree thing, dw_die_ref context_die)
19154 dw_die_ref ns_context;
19156 if (debug_info_level <= DINFO_LEVEL_TERSE)
19157 return context_die;
19159 /* If this decl is from an inlined function, then don't try to emit it in its
19160 namespace, as we will get confused. It would have already been emitted
19161 when the abstract instance of the inline function was emitted anyways. */
19162 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
19163 return context_die;
19165 ns_context = setup_namespace_context (thing, context_die);
19167 if (ns_context != context_die)
19169 if (is_fortran ())
19170 return ns_context;
19171 if (DECL_P (thing))
19172 gen_decl_die (thing, NULL, ns_context);
19173 else
19174 gen_type_die (thing, ns_context);
19176 return context_die;
19179 /* Generate a DIE for a namespace or namespace alias. */
19181 static void
19182 gen_namespace_die (tree decl, dw_die_ref context_die)
19184 dw_die_ref namespace_die;
19186 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
19187 they are an alias of. */
19188 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
19190 /* Output a real namespace or module. */
19191 context_die = setup_namespace_context (decl, comp_unit_die ());
19192 namespace_die = new_die (is_fortran ()
19193 ? DW_TAG_module : DW_TAG_namespace,
19194 context_die, decl);
19195 /* For Fortran modules defined in different CU don't add src coords. */
19196 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
19198 const char *name = dwarf2_name (decl, 0);
19199 if (name)
19200 add_name_attribute (namespace_die, name);
19202 else
19203 add_name_and_src_coords_attributes (namespace_die, decl);
19204 if (DECL_EXTERNAL (decl))
19205 add_AT_flag (namespace_die, DW_AT_declaration, 1);
19206 equate_decl_number_to_die (decl, namespace_die);
19208 else
19210 /* Output a namespace alias. */
19212 /* Force out the namespace we are an alias of, if necessary. */
19213 dw_die_ref origin_die
19214 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
19216 if (DECL_FILE_SCOPE_P (decl)
19217 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
19218 context_die = setup_namespace_context (decl, comp_unit_die ());
19219 /* Now create the namespace alias DIE. */
19220 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
19221 add_name_and_src_coords_attributes (namespace_die, decl);
19222 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
19223 equate_decl_number_to_die (decl, namespace_die);
19225 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
19226 if (want_pubnames ())
19227 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
19230 /* Generate Dwarf debug information for a decl described by DECL.
19231 The return value is currently only meaningful for PARM_DECLs,
19232 for all other decls it returns NULL. */
19234 static dw_die_ref
19235 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
19237 tree decl_or_origin = decl ? decl : origin;
19238 tree class_origin = NULL, ultimate_origin;
19240 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
19241 return NULL;
19243 switch (TREE_CODE (decl_or_origin))
19245 case ERROR_MARK:
19246 break;
19248 case CONST_DECL:
19249 if (!is_fortran () && !is_ada ())
19251 /* The individual enumerators of an enum type get output when we output
19252 the Dwarf representation of the relevant enum type itself. */
19253 break;
19256 /* Emit its type. */
19257 gen_type_die (TREE_TYPE (decl), context_die);
19259 /* And its containing namespace. */
19260 context_die = declare_in_namespace (decl, context_die);
19262 gen_const_die (decl, context_die);
19263 break;
19265 case FUNCTION_DECL:
19266 /* Don't output any DIEs to represent mere function declarations,
19267 unless they are class members or explicit block externs. */
19268 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
19269 && DECL_FILE_SCOPE_P (decl_or_origin)
19270 && (current_function_decl == NULL_TREE
19271 || DECL_ARTIFICIAL (decl_or_origin)))
19272 break;
19274 #if 0
19275 /* FIXME */
19276 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
19277 on local redeclarations of global functions. That seems broken. */
19278 if (current_function_decl != decl)
19279 /* This is only a declaration. */;
19280 #endif
19282 /* If we're emitting a clone, emit info for the abstract instance. */
19283 if (origin || DECL_ORIGIN (decl) != decl)
19284 dwarf2out_abstract_function (origin
19285 ? DECL_ORIGIN (origin)
19286 : DECL_ABSTRACT_ORIGIN (decl));
19288 /* If we're emitting an out-of-line copy of an inline function,
19289 emit info for the abstract instance and set up to refer to it. */
19290 else if (cgraph_function_possibly_inlined_p (decl)
19291 && ! DECL_ABSTRACT (decl)
19292 && ! class_or_namespace_scope_p (context_die)
19293 /* dwarf2out_abstract_function won't emit a die if this is just
19294 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
19295 that case, because that works only if we have a die. */
19296 && DECL_INITIAL (decl) != NULL_TREE)
19298 dwarf2out_abstract_function (decl);
19299 set_decl_origin_self (decl);
19302 /* Otherwise we're emitting the primary DIE for this decl. */
19303 else if (debug_info_level > DINFO_LEVEL_TERSE)
19305 /* Before we describe the FUNCTION_DECL itself, make sure that we
19306 have its containing type. */
19307 if (!origin)
19308 origin = decl_class_context (decl);
19309 if (origin != NULL_TREE)
19310 gen_type_die (origin, context_die);
19312 /* And its return type. */
19313 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
19315 /* And its virtual context. */
19316 if (DECL_VINDEX (decl) != NULL_TREE)
19317 gen_type_die (DECL_CONTEXT (decl), context_die);
19319 /* Make sure we have a member DIE for decl. */
19320 if (origin != NULL_TREE)
19321 gen_type_die_for_member (origin, decl, context_die);
19323 /* And its containing namespace. */
19324 context_die = declare_in_namespace (decl, context_die);
19327 /* Now output a DIE to represent the function itself. */
19328 if (decl)
19329 gen_subprogram_die (decl, context_die);
19330 break;
19332 case TYPE_DECL:
19333 /* If we are in terse mode, don't generate any DIEs to represent any
19334 actual typedefs. */
19335 if (debug_info_level <= DINFO_LEVEL_TERSE)
19336 break;
19338 /* In the special case of a TYPE_DECL node representing the declaration
19339 of some type tag, if the given TYPE_DECL is marked as having been
19340 instantiated from some other (original) TYPE_DECL node (e.g. one which
19341 was generated within the original definition of an inline function) we
19342 used to generate a special (abbreviated) DW_TAG_structure_type,
19343 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
19344 should be actually referencing those DIEs, as variable DIEs with that
19345 type would be emitted already in the abstract origin, so it was always
19346 removed during unused type prunning. Don't add anything in this
19347 case. */
19348 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
19349 break;
19351 if (is_redundant_typedef (decl))
19352 gen_type_die (TREE_TYPE (decl), context_die);
19353 else
19354 /* Output a DIE to represent the typedef itself. */
19355 gen_typedef_die (decl, context_die);
19356 break;
19358 case LABEL_DECL:
19359 if (debug_info_level >= DINFO_LEVEL_NORMAL)
19360 gen_label_die (decl, context_die);
19361 break;
19363 case VAR_DECL:
19364 case RESULT_DECL:
19365 /* If we are in terse mode, don't generate any DIEs to represent any
19366 variable declarations or definitions. */
19367 if (debug_info_level <= DINFO_LEVEL_TERSE)
19368 break;
19370 /* Output any DIEs that are needed to specify the type of this data
19371 object. */
19372 if (decl_by_reference_p (decl_or_origin))
19373 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
19374 else
19375 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
19377 /* And its containing type. */
19378 class_origin = decl_class_context (decl_or_origin);
19379 if (class_origin != NULL_TREE)
19380 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
19382 /* And its containing namespace. */
19383 context_die = declare_in_namespace (decl_or_origin, context_die);
19385 /* Now output the DIE to represent the data object itself. This gets
19386 complicated because of the possibility that the VAR_DECL really
19387 represents an inlined instance of a formal parameter for an inline
19388 function. */
19389 ultimate_origin = decl_ultimate_origin (decl_or_origin);
19390 if (ultimate_origin != NULL_TREE
19391 && TREE_CODE (ultimate_origin) == PARM_DECL)
19392 gen_formal_parameter_die (decl, origin,
19393 true /* Emit name attribute. */,
19394 context_die);
19395 else
19396 gen_variable_die (decl, origin, context_die);
19397 break;
19399 case FIELD_DECL:
19400 /* Ignore the nameless fields that are used to skip bits but handle C++
19401 anonymous unions and structs. */
19402 if (DECL_NAME (decl) != NULL_TREE
19403 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
19404 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
19406 gen_type_die (member_declared_type (decl), context_die);
19407 gen_field_die (decl, context_die);
19409 break;
19411 case PARM_DECL:
19412 if (DECL_BY_REFERENCE (decl_or_origin))
19413 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
19414 else
19415 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
19416 return gen_formal_parameter_die (decl, origin,
19417 true /* Emit name attribute. */,
19418 context_die);
19420 case NAMESPACE_DECL:
19421 case IMPORTED_DECL:
19422 if (dwarf_version >= 3 || !dwarf_strict)
19423 gen_namespace_die (decl, context_die);
19424 break;
19426 default:
19427 /* Probably some frontend-internal decl. Assume we don't care. */
19428 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
19429 break;
19432 return NULL;
19435 /* Output debug information for global decl DECL. Called from toplev.c after
19436 compilation proper has finished. */
19438 static void
19439 dwarf2out_global_decl (tree decl)
19441 /* Output DWARF2 information for file-scope tentative data object
19442 declarations, file-scope (extern) function declarations (which
19443 had no corresponding body) and file-scope tagged type declarations
19444 and definitions which have not yet been forced out. */
19445 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
19446 dwarf2out_decl (decl);
19449 /* Output debug information for type decl DECL. Called from toplev.c
19450 and from language front ends (to record built-in types). */
19451 static void
19452 dwarf2out_type_decl (tree decl, int local)
19454 if (!local)
19455 dwarf2out_decl (decl);
19458 /* Output debug information for imported module or decl DECL.
19459 NAME is non-NULL name in the lexical block if the decl has been renamed.
19460 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
19461 that DECL belongs to.
19462 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
19463 static void
19464 dwarf2out_imported_module_or_decl_1 (tree decl,
19465 tree name,
19466 tree lexical_block,
19467 dw_die_ref lexical_block_die)
19469 expanded_location xloc;
19470 dw_die_ref imported_die = NULL;
19471 dw_die_ref at_import_die;
19473 if (TREE_CODE (decl) == IMPORTED_DECL)
19475 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
19476 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
19477 gcc_assert (decl);
19479 else
19480 xloc = expand_location (input_location);
19482 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
19484 at_import_die = force_type_die (TREE_TYPE (decl));
19485 /* For namespace N { typedef void T; } using N::T; base_type_die
19486 returns NULL, but DW_TAG_imported_declaration requires
19487 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
19488 if (!at_import_die)
19490 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
19491 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
19492 at_import_die = lookup_type_die (TREE_TYPE (decl));
19493 gcc_assert (at_import_die);
19496 else
19498 at_import_die = lookup_decl_die (decl);
19499 if (!at_import_die)
19501 /* If we're trying to avoid duplicate debug info, we may not have
19502 emitted the member decl for this field. Emit it now. */
19503 if (TREE_CODE (decl) == FIELD_DECL)
19505 tree type = DECL_CONTEXT (decl);
19507 if (TYPE_CONTEXT (type)
19508 && TYPE_P (TYPE_CONTEXT (type))
19509 && !should_emit_struct_debug (TYPE_CONTEXT (type),
19510 DINFO_USAGE_DIR_USE))
19511 return;
19512 gen_type_die_for_member (type, decl,
19513 get_context_die (TYPE_CONTEXT (type)));
19515 at_import_die = force_decl_die (decl);
19519 if (TREE_CODE (decl) == NAMESPACE_DECL)
19521 if (dwarf_version >= 3 || !dwarf_strict)
19522 imported_die = new_die (DW_TAG_imported_module,
19523 lexical_block_die,
19524 lexical_block);
19525 else
19526 return;
19528 else
19529 imported_die = new_die (DW_TAG_imported_declaration,
19530 lexical_block_die,
19531 lexical_block);
19533 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
19534 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
19535 if (name)
19536 add_AT_string (imported_die, DW_AT_name,
19537 IDENTIFIER_POINTER (name));
19538 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
19541 /* Output debug information for imported module or decl DECL.
19542 NAME is non-NULL name in context if the decl has been renamed.
19543 CHILD is true if decl is one of the renamed decls as part of
19544 importing whole module. */
19546 static void
19547 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
19548 bool child)
19550 /* dw_die_ref at_import_die; */
19551 dw_die_ref scope_die;
19553 if (debug_info_level <= DINFO_LEVEL_TERSE)
19554 return;
19556 gcc_assert (decl);
19558 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
19559 We need decl DIE for reference and scope die. First, get DIE for the decl
19560 itself. */
19562 /* Get the scope die for decl context. Use comp_unit_die for global module
19563 or decl. If die is not found for non globals, force new die. */
19564 if (context
19565 && TYPE_P (context)
19566 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
19567 return;
19569 if (!(dwarf_version >= 3 || !dwarf_strict))
19570 return;
19572 scope_die = get_context_die (context);
19574 if (child)
19576 gcc_assert (scope_die->die_child);
19577 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
19578 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
19579 scope_die = scope_die->die_child;
19582 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
19583 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
19587 /* Write the debugging output for DECL. */
19589 void
19590 dwarf2out_decl (tree decl)
19592 dw_die_ref context_die = comp_unit_die ();
19594 switch (TREE_CODE (decl))
19596 case ERROR_MARK:
19597 return;
19599 case FUNCTION_DECL:
19600 /* What we would really like to do here is to filter out all mere
19601 file-scope declarations of file-scope functions which are never
19602 referenced later within this translation unit (and keep all of ones
19603 that *are* referenced later on) but we aren't clairvoyant, so we have
19604 no idea which functions will be referenced in the future (i.e. later
19605 on within the current translation unit). So here we just ignore all
19606 file-scope function declarations which are not also definitions. If
19607 and when the debugger needs to know something about these functions,
19608 it will have to hunt around and find the DWARF information associated
19609 with the definition of the function.
19611 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
19612 nodes represent definitions and which ones represent mere
19613 declarations. We have to check DECL_INITIAL instead. That's because
19614 the C front-end supports some weird semantics for "extern inline"
19615 function definitions. These can get inlined within the current
19616 translation unit (and thus, we need to generate Dwarf info for their
19617 abstract instances so that the Dwarf info for the concrete inlined
19618 instances can have something to refer to) but the compiler never
19619 generates any out-of-lines instances of such things (despite the fact
19620 that they *are* definitions).
19622 The important point is that the C front-end marks these "extern
19623 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
19624 them anyway. Note that the C++ front-end also plays some similar games
19625 for inline function definitions appearing within include files which
19626 also contain `#pragma interface' pragmas. */
19627 if (DECL_INITIAL (decl) == NULL_TREE)
19628 return;
19630 /* If we're a nested function, initially use a parent of NULL; if we're
19631 a plain function, this will be fixed up in decls_for_scope. If
19632 we're a method, it will be ignored, since we already have a DIE. */
19633 if (decl_function_context (decl)
19634 /* But if we're in terse mode, we don't care about scope. */
19635 && debug_info_level > DINFO_LEVEL_TERSE)
19636 context_die = NULL;
19637 break;
19639 case VAR_DECL:
19640 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
19641 declaration and if the declaration was never even referenced from
19642 within this entire compilation unit. We suppress these DIEs in
19643 order to save space in the .debug section (by eliminating entries
19644 which are probably useless). Note that we must not suppress
19645 block-local extern declarations (whether used or not) because that
19646 would screw-up the debugger's name lookup mechanism and cause it to
19647 miss things which really ought to be in scope at a given point. */
19648 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
19649 return;
19651 /* For local statics lookup proper context die. */
19652 if (TREE_STATIC (decl)
19653 && DECL_CONTEXT (decl)
19654 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
19655 context_die = lookup_decl_die (DECL_CONTEXT (decl));
19657 /* If we are in terse mode, don't generate any DIEs to represent any
19658 variable declarations or definitions. */
19659 if (debug_info_level <= DINFO_LEVEL_TERSE)
19660 return;
19661 break;
19663 case CONST_DECL:
19664 if (debug_info_level <= DINFO_LEVEL_TERSE)
19665 return;
19666 if (!is_fortran () && !is_ada ())
19667 return;
19668 if (TREE_STATIC (decl) && decl_function_context (decl))
19669 context_die = lookup_decl_die (DECL_CONTEXT (decl));
19670 break;
19672 case NAMESPACE_DECL:
19673 case IMPORTED_DECL:
19674 if (debug_info_level <= DINFO_LEVEL_TERSE)
19675 return;
19676 if (lookup_decl_die (decl) != NULL)
19677 return;
19678 break;
19680 case TYPE_DECL:
19681 /* Don't emit stubs for types unless they are needed by other DIEs. */
19682 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
19683 return;
19685 /* Don't bother trying to generate any DIEs to represent any of the
19686 normal built-in types for the language we are compiling. */
19687 if (DECL_IS_BUILTIN (decl))
19688 return;
19690 /* If we are in terse mode, don't generate any DIEs for types. */
19691 if (debug_info_level <= DINFO_LEVEL_TERSE)
19692 return;
19694 /* If we're a function-scope tag, initially use a parent of NULL;
19695 this will be fixed up in decls_for_scope. */
19696 if (decl_function_context (decl))
19697 context_die = NULL;
19699 break;
19701 default:
19702 return;
19705 gen_decl_die (decl, NULL, context_die);
19708 /* Write the debugging output for DECL. */
19710 static void
19711 dwarf2out_function_decl (tree decl)
19713 dwarf2out_decl (decl);
19714 call_arg_locations = NULL;
19715 call_arg_loc_last = NULL;
19716 call_site_count = -1;
19717 tail_call_site_count = -1;
19718 VEC_free (dw_die_ref, heap, block_map);
19719 htab_empty (decl_loc_table);
19720 htab_empty (cached_dw_loc_list_table);
19723 /* Output a marker (i.e. a label) for the beginning of the generated code for
19724 a lexical block. */
19726 static void
19727 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
19728 unsigned int blocknum)
19730 switch_to_section (current_function_section ());
19731 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
19734 /* Output a marker (i.e. a label) for the end of the generated code for a
19735 lexical block. */
19737 static void
19738 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
19740 switch_to_section (current_function_section ());
19741 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
19744 /* Returns nonzero if it is appropriate not to emit any debugging
19745 information for BLOCK, because it doesn't contain any instructions.
19747 Don't allow this for blocks with nested functions or local classes
19748 as we would end up with orphans, and in the presence of scheduling
19749 we may end up calling them anyway. */
19751 static bool
19752 dwarf2out_ignore_block (const_tree block)
19754 tree decl;
19755 unsigned int i;
19757 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
19758 if (TREE_CODE (decl) == FUNCTION_DECL
19759 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
19760 return 0;
19761 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
19763 decl = BLOCK_NONLOCALIZED_VAR (block, i);
19764 if (TREE_CODE (decl) == FUNCTION_DECL
19765 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
19766 return 0;
19769 return 1;
19772 /* Hash table routines for file_hash. */
19774 static int
19775 file_table_eq (const void *p1_p, const void *p2_p)
19777 const struct dwarf_file_data *const p1 =
19778 (const struct dwarf_file_data *) p1_p;
19779 const char *const p2 = (const char *) p2_p;
19780 return filename_cmp (p1->filename, p2) == 0;
19783 static hashval_t
19784 file_table_hash (const void *p_p)
19786 const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
19787 return htab_hash_string (p->filename);
19790 /* Lookup FILE_NAME (in the list of filenames that we know about here in
19791 dwarf2out.c) and return its "index". The index of each (known) filename is
19792 just a unique number which is associated with only that one filename. We
19793 need such numbers for the sake of generating labels (in the .debug_sfnames
19794 section) and references to those files numbers (in the .debug_srcinfo
19795 and.debug_macinfo sections). If the filename given as an argument is not
19796 found in our current list, add it to the list and assign it the next
19797 available unique index number. In order to speed up searches, we remember
19798 the index of the filename was looked up last. This handles the majority of
19799 all searches. */
19801 static struct dwarf_file_data *
19802 lookup_filename (const char *file_name)
19804 void ** slot;
19805 struct dwarf_file_data * created;
19807 /* Check to see if the file name that was searched on the previous
19808 call matches this file name. If so, return the index. */
19809 if (file_table_last_lookup
19810 && (file_name == file_table_last_lookup->filename
19811 || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
19812 return file_table_last_lookup;
19814 /* Didn't match the previous lookup, search the table. */
19815 slot = htab_find_slot_with_hash (file_table, file_name,
19816 htab_hash_string (file_name), INSERT);
19817 if (*slot)
19818 return (struct dwarf_file_data *) *slot;
19820 created = ggc_alloc_dwarf_file_data ();
19821 created->filename = file_name;
19822 created->emitted_number = 0;
19823 *slot = created;
19824 return created;
19827 /* If the assembler will construct the file table, then translate the compiler
19828 internal file table number into the assembler file table number, and emit
19829 a .file directive if we haven't already emitted one yet. The file table
19830 numbers are different because we prune debug info for unused variables and
19831 types, which may include filenames. */
19833 static int
19834 maybe_emit_file (struct dwarf_file_data * fd)
19836 if (! fd->emitted_number)
19838 if (last_emitted_file)
19839 fd->emitted_number = last_emitted_file->emitted_number + 1;
19840 else
19841 fd->emitted_number = 1;
19842 last_emitted_file = fd;
19844 if (DWARF2_ASM_LINE_DEBUG_INFO)
19846 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
19847 output_quoted_string (asm_out_file,
19848 remap_debug_filename (fd->filename));
19849 fputc ('\n', asm_out_file);
19853 return fd->emitted_number;
19856 /* Schedule generation of a DW_AT_const_value attribute to DIE.
19857 That generation should happen after function debug info has been
19858 generated. The value of the attribute is the constant value of ARG. */
19860 static void
19861 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
19863 die_arg_entry entry;
19865 if (!die || !arg)
19866 return;
19868 if (!tmpl_value_parm_die_table)
19869 tmpl_value_parm_die_table
19870 = VEC_alloc (die_arg_entry, gc, 32);
19872 entry.die = die;
19873 entry.arg = arg;
19874 VEC_safe_push (die_arg_entry, gc,
19875 tmpl_value_parm_die_table,
19876 &entry);
19879 /* Return TRUE if T is an instance of generic type, FALSE
19880 otherwise. */
19882 static bool
19883 generic_type_p (tree t)
19885 if (t == NULL_TREE || !TYPE_P (t))
19886 return false;
19887 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
19890 /* Schedule the generation of the generic parameter dies for the
19891 instance of generic type T. The proper generation itself is later
19892 done by gen_scheduled_generic_parms_dies. */
19894 static void
19895 schedule_generic_params_dies_gen (tree t)
19897 if (!generic_type_p (t))
19898 return;
19900 if (generic_type_instances == NULL)
19901 generic_type_instances = VEC_alloc (tree, gc, 256);
19903 VEC_safe_push (tree, gc, generic_type_instances, t);
19906 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
19907 by append_entry_to_tmpl_value_parm_die_table. This function must
19908 be called after function DIEs have been generated. */
19910 static void
19911 gen_remaining_tmpl_value_param_die_attribute (void)
19913 if (tmpl_value_parm_die_table)
19915 unsigned i;
19916 die_arg_entry *e;
19918 FOR_EACH_VEC_ELT (die_arg_entry, tmpl_value_parm_die_table, i, e)
19919 tree_add_const_value_attribute (e->die, e->arg);
19923 /* Generate generic parameters DIEs for instances of generic types
19924 that have been previously scheduled by
19925 schedule_generic_params_dies_gen. This function must be called
19926 after all the types of the CU have been laid out. */
19928 static void
19929 gen_scheduled_generic_parms_dies (void)
19931 unsigned i;
19932 tree t;
19934 if (generic_type_instances == NULL)
19935 return;
19937 FOR_EACH_VEC_ELT (tree, generic_type_instances, i, t)
19938 gen_generic_params_dies (t);
19942 /* Replace DW_AT_name for the decl with name. */
19944 static void
19945 dwarf2out_set_name (tree decl, tree name)
19947 dw_die_ref die;
19948 dw_attr_ref attr;
19949 const char *dname;
19951 die = TYPE_SYMTAB_DIE (decl);
19952 if (!die)
19953 return;
19955 dname = dwarf2_name (name, 0);
19956 if (!dname)
19957 return;
19959 attr = get_AT (die, DW_AT_name);
19960 if (attr)
19962 struct indirect_string_node *node;
19964 node = find_AT_string (dname);
19965 /* replace the string. */
19966 attr->dw_attr_val.v.val_str = node;
19969 else
19970 add_name_attribute (die, dname);
19973 /* Called by the final INSN scan whenever we see a var location. We
19974 use it to drop labels in the right places, and throw the location in
19975 our lookup table. */
19977 static void
19978 dwarf2out_var_location (rtx loc_note)
19980 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
19981 struct var_loc_node *newloc;
19982 rtx next_real, next_note;
19983 static const char *last_label;
19984 static const char *last_postcall_label;
19985 static bool last_in_cold_section_p;
19986 static rtx expected_next_loc_note;
19987 tree decl;
19988 bool var_loc_p;
19990 if (!NOTE_P (loc_note))
19992 if (CALL_P (loc_note))
19994 call_site_count++;
19995 if (SIBLING_CALL_P (loc_note))
19996 tail_call_site_count++;
19998 return;
20001 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
20002 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
20003 return;
20005 /* Optimize processing a large consecutive sequence of location
20006 notes so we don't spend too much time in next_real_insn. If the
20007 next insn is another location note, remember the next_real_insn
20008 calculation for next time. */
20009 next_real = cached_next_real_insn;
20010 if (next_real)
20012 if (expected_next_loc_note != loc_note)
20013 next_real = NULL_RTX;
20016 next_note = NEXT_INSN (loc_note);
20017 if (! next_note
20018 || INSN_DELETED_P (next_note)
20019 || GET_CODE (next_note) != NOTE
20020 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
20021 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
20022 next_note = NULL_RTX;
20024 if (! next_real)
20025 next_real = next_real_insn (loc_note);
20027 if (next_note)
20029 expected_next_loc_note = next_note;
20030 cached_next_real_insn = next_real;
20032 else
20033 cached_next_real_insn = NULL_RTX;
20035 /* If there are no instructions which would be affected by this note,
20036 don't do anything. */
20037 if (var_loc_p
20038 && next_real == NULL_RTX
20039 && !NOTE_DURING_CALL_P (loc_note))
20040 return;
20042 if (next_real == NULL_RTX)
20043 next_real = get_last_insn ();
20045 /* If there were any real insns between note we processed last time
20046 and this note (or if it is the first note), clear
20047 last_{,postcall_}label so that they are not reused this time. */
20048 if (last_var_location_insn == NULL_RTX
20049 || last_var_location_insn != next_real
20050 || last_in_cold_section_p != in_cold_section_p)
20052 last_label = NULL;
20053 last_postcall_label = NULL;
20056 if (var_loc_p)
20058 decl = NOTE_VAR_LOCATION_DECL (loc_note);
20059 newloc = add_var_loc_to_decl (decl, loc_note,
20060 NOTE_DURING_CALL_P (loc_note)
20061 ? last_postcall_label : last_label);
20062 if (newloc == NULL)
20063 return;
20065 else
20067 decl = NULL_TREE;
20068 newloc = NULL;
20071 /* If there were no real insns between note we processed last time
20072 and this note, use the label we emitted last time. Otherwise
20073 create a new label and emit it. */
20074 if (last_label == NULL)
20076 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
20077 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
20078 loclabel_num++;
20079 last_label = ggc_strdup (loclabel);
20082 if (!var_loc_p)
20084 struct call_arg_loc_node *ca_loc
20085 = ggc_alloc_cleared_call_arg_loc_node ();
20086 rtx prev = prev_real_insn (loc_note), x;
20087 ca_loc->call_arg_loc_note = loc_note;
20088 ca_loc->next = NULL;
20089 ca_loc->label = last_label;
20090 gcc_assert (prev
20091 && (CALL_P (prev)
20092 || (NONJUMP_INSN_P (prev)
20093 && GET_CODE (PATTERN (prev)) == SEQUENCE
20094 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
20095 if (!CALL_P (prev))
20096 prev = XVECEXP (PATTERN (prev), 0, 0);
20097 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
20098 x = PATTERN (prev);
20099 if (GET_CODE (x) == PARALLEL)
20100 x = XVECEXP (x, 0, 0);
20101 if (GET_CODE (x) == SET)
20102 x = SET_SRC (x);
20103 if (GET_CODE (x) == CALL && MEM_P (XEXP (x, 0)))
20105 x = XEXP (XEXP (x, 0), 0);
20106 if (GET_CODE (x) == SYMBOL_REF
20107 && SYMBOL_REF_DECL (x)
20108 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
20109 ca_loc->symbol_ref = x;
20111 ca_loc->block = insn_scope (prev);
20112 if (call_arg_locations)
20113 call_arg_loc_last->next = ca_loc;
20114 else
20115 call_arg_locations = ca_loc;
20116 call_arg_loc_last = ca_loc;
20118 else if (!NOTE_DURING_CALL_P (loc_note))
20119 newloc->label = last_label;
20120 else
20122 if (!last_postcall_label)
20124 sprintf (loclabel, "%s-1", last_label);
20125 last_postcall_label = ggc_strdup (loclabel);
20127 newloc->label = last_postcall_label;
20130 last_var_location_insn = next_real;
20131 last_in_cold_section_p = in_cold_section_p;
20134 /* Note in one location list that text section has changed. */
20136 static int
20137 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
20139 var_loc_list *list = (var_loc_list *) *slot;
20140 if (list->first)
20141 list->last_before_switch
20142 = list->last->next ? list->last->next : list->last;
20143 return 1;
20146 /* Note in all location lists that text section has changed. */
20148 static void
20149 var_location_switch_text_section (void)
20151 if (decl_loc_table == NULL)
20152 return;
20154 htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
20157 /* Create a new line number table. */
20159 static dw_line_info_table *
20160 new_line_info_table (void)
20162 dw_line_info_table *table;
20164 table = ggc_alloc_cleared_dw_line_info_table_struct ();
20165 table->file_num = 1;
20166 table->line_num = 1;
20167 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
20169 return table;
20172 /* Lookup the "current" table into which we emit line info, so
20173 that we don't have to do it for every source line. */
20175 static void
20176 set_cur_line_info_table (section *sec)
20178 dw_line_info_table *table;
20180 if (sec == text_section)
20181 table = text_section_line_info;
20182 else if (sec == cold_text_section)
20184 table = cold_text_section_line_info;
20185 if (!table)
20187 cold_text_section_line_info = table = new_line_info_table ();
20188 table->end_label = cold_end_label;
20191 else
20193 const char *end_label;
20195 if (flag_reorder_blocks_and_partition)
20197 if (in_cold_section_p)
20198 end_label = crtl->subsections.cold_section_end_label;
20199 else
20200 end_label = crtl->subsections.hot_section_end_label;
20202 else
20204 char label[MAX_ARTIFICIAL_LABEL_BYTES];
20205 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
20206 current_function_funcdef_no);
20207 end_label = ggc_strdup (label);
20210 table = new_line_info_table ();
20211 table->end_label = end_label;
20213 VEC_safe_push (dw_line_info_table_p, gc, separate_line_info, table);
20216 if (DWARF2_ASM_LINE_DEBUG_INFO)
20217 table->is_stmt = (cur_line_info_table
20218 ? cur_line_info_table->is_stmt
20219 : DWARF_LINE_DEFAULT_IS_STMT_START);
20220 cur_line_info_table = table;
20224 /* We need to reset the locations at the beginning of each
20225 function. We can't do this in the end_function hook, because the
20226 declarations that use the locations won't have been output when
20227 that hook is called. Also compute have_multiple_function_sections here. */
20229 static void
20230 dwarf2out_begin_function (tree fun)
20232 section *sec = function_section (fun);
20234 if (sec != text_section)
20235 have_multiple_function_sections = true;
20237 if (flag_reorder_blocks_and_partition && !cold_text_section)
20239 gcc_assert (current_function_decl == fun);
20240 cold_text_section = unlikely_text_section ();
20241 switch_to_section (cold_text_section);
20242 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
20243 switch_to_section (sec);
20246 dwarf2out_note_section_used ();
20247 call_site_count = 0;
20248 tail_call_site_count = 0;
20250 set_cur_line_info_table (sec);
20253 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
20255 static void
20256 push_dw_line_info_entry (dw_line_info_table *table,
20257 enum dw_line_info_opcode opcode, unsigned int val)
20259 dw_line_info_entry e;
20260 e.opcode = opcode;
20261 e.val = val;
20262 VEC_safe_push (dw_line_info_entry, gc, table->entries, &e);
20265 /* Output a label to mark the beginning of a source code line entry
20266 and record information relating to this source line, in
20267 'line_info_table' for later output of the .debug_line section. */
20268 /* ??? The discriminator parameter ought to be unsigned. */
20270 static void
20271 dwarf2out_source_line (unsigned int line, const char *filename,
20272 int discriminator, bool is_stmt)
20274 unsigned int file_num;
20275 dw_line_info_table *table;
20277 if (debug_info_level < DINFO_LEVEL_NORMAL || line == 0)
20278 return;
20280 /* The discriminator column was added in dwarf4. Simplify the below
20281 by simply removing it if we're not supposed to output it. */
20282 if (dwarf_version < 4 && dwarf_strict)
20283 discriminator = 0;
20285 table = cur_line_info_table;
20286 file_num = maybe_emit_file (lookup_filename (filename));
20288 /* ??? TODO: Elide duplicate line number entries. Traditionally,
20289 the debugger has used the second (possibly duplicate) line number
20290 at the beginning of the function to mark the end of the prologue.
20291 We could eliminate any other duplicates within the function. For
20292 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
20293 that second line number entry. */
20294 /* Recall that this end-of-prologue indication is *not* the same thing
20295 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
20296 to which the hook corresponds, follows the last insn that was
20297 emitted by gen_prologue. What we need is to precede the first insn
20298 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
20299 insn that corresponds to something the user wrote. These may be
20300 very different locations once scheduling is enabled. */
20302 if (0 && file_num == table->file_num
20303 && line == table->line_num
20304 && discriminator == table->discrim_num
20305 && is_stmt == table->is_stmt)
20306 return;
20308 switch_to_section (current_function_section ());
20310 /* If requested, emit something human-readable. */
20311 if (flag_debug_asm)
20312 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
20314 if (DWARF2_ASM_LINE_DEBUG_INFO)
20316 /* Emit the .loc directive understood by GNU as. */
20317 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
20318 file_num, line, is_stmt, discriminator */
20319 fputs ("\t.loc ", asm_out_file);
20320 fprint_ul (asm_out_file, file_num);
20321 putc (' ', asm_out_file);
20322 fprint_ul (asm_out_file, line);
20323 putc (' ', asm_out_file);
20324 putc ('0', asm_out_file);
20326 if (is_stmt != table->is_stmt)
20328 fputs (" is_stmt ", asm_out_file);
20329 putc (is_stmt ? '1' : '0', asm_out_file);
20331 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
20333 gcc_assert (discriminator > 0);
20334 fputs (" discriminator ", asm_out_file);
20335 fprint_ul (asm_out_file, (unsigned long) discriminator);
20337 putc ('\n', asm_out_file);
20339 else
20341 unsigned int label_num = ++line_info_label_num;
20343 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
20345 push_dw_line_info_entry (table, LI_set_address, label_num);
20346 if (file_num != table->file_num)
20347 push_dw_line_info_entry (table, LI_set_file, file_num);
20348 if (discriminator != table->discrim_num)
20349 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
20350 if (is_stmt != table->is_stmt)
20351 push_dw_line_info_entry (table, LI_negate_stmt, 0);
20352 push_dw_line_info_entry (table, LI_set_line, line);
20355 table->file_num = file_num;
20356 table->line_num = line;
20357 table->discrim_num = discriminator;
20358 table->is_stmt = is_stmt;
20359 table->in_use = true;
20362 /* Record the beginning of a new source file. */
20364 static void
20365 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
20367 if (flag_eliminate_dwarf2_dups)
20369 /* Record the beginning of the file for break_out_includes. */
20370 dw_die_ref bincl_die;
20372 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
20373 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
20376 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20378 macinfo_entry e;
20379 e.code = DW_MACINFO_start_file;
20380 e.lineno = lineno;
20381 e.info = ggc_strdup (filename);
20382 VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20386 /* Record the end of a source file. */
20388 static void
20389 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
20391 if (flag_eliminate_dwarf2_dups)
20392 /* Record the end of the file for break_out_includes. */
20393 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
20395 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20397 macinfo_entry e;
20398 e.code = DW_MACINFO_end_file;
20399 e.lineno = lineno;
20400 e.info = NULL;
20401 VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20405 /* Called from debug_define in toplev.c. The `buffer' parameter contains
20406 the tail part of the directive line, i.e. the part which is past the
20407 initial whitespace, #, whitespace, directive-name, whitespace part. */
20409 static void
20410 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
20411 const char *buffer ATTRIBUTE_UNUSED)
20413 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20415 macinfo_entry e;
20416 /* Insert a dummy first entry to be able to optimize the whole
20417 predefined macro block using DW_MACRO_GNU_transparent_include. */
20418 if (VEC_empty (macinfo_entry, macinfo_table) && lineno <= 1)
20420 e.code = 0;
20421 e.lineno = 0;
20422 e.info = NULL;
20423 VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20425 e.code = DW_MACINFO_define;
20426 e.lineno = lineno;
20427 e.info = ggc_strdup (buffer);
20428 VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20432 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
20433 the tail part of the directive line, i.e. the part which is past the
20434 initial whitespace, #, whitespace, directive-name, whitespace part. */
20436 static void
20437 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
20438 const char *buffer ATTRIBUTE_UNUSED)
20440 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20442 macinfo_entry e;
20443 /* Insert a dummy first entry to be able to optimize the whole
20444 predefined macro block using DW_MACRO_GNU_transparent_include. */
20445 if (VEC_empty (macinfo_entry, macinfo_table) && lineno <= 1)
20447 e.code = 0;
20448 e.lineno = 0;
20449 e.info = NULL;
20450 VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20452 e.code = DW_MACINFO_undef;
20453 e.lineno = lineno;
20454 e.info = ggc_strdup (buffer);
20455 VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20459 /* Routines to manipulate hash table of CUs. */
20461 static hashval_t
20462 htab_macinfo_hash (const void *of)
20464 const macinfo_entry *const entry =
20465 (const macinfo_entry *) of;
20467 return htab_hash_string (entry->info);
20470 static int
20471 htab_macinfo_eq (const void *of1, const void *of2)
20473 const macinfo_entry *const entry1 = (const macinfo_entry *) of1;
20474 const macinfo_entry *const entry2 = (const macinfo_entry *) of2;
20476 return !strcmp (entry1->info, entry2->info);
20479 /* Output a single .debug_macinfo entry. */
20481 static void
20482 output_macinfo_op (macinfo_entry *ref)
20484 int file_num;
20485 size_t len;
20486 struct indirect_string_node *node;
20487 char label[MAX_ARTIFICIAL_LABEL_BYTES];
20488 struct dwarf_file_data *fd;
20490 switch (ref->code)
20492 case DW_MACINFO_start_file:
20493 fd = lookup_filename (ref->info);
20494 file_num = maybe_emit_file (fd);
20495 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
20496 dw2_asm_output_data_uleb128 (ref->lineno,
20497 "Included from line number %lu",
20498 (unsigned long) ref->lineno);
20499 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
20500 break;
20501 case DW_MACINFO_end_file:
20502 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
20503 break;
20504 case DW_MACINFO_define:
20505 case DW_MACINFO_undef:
20506 len = strlen (ref->info) + 1;
20507 if (!dwarf_strict
20508 && len > DWARF_OFFSET_SIZE
20509 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
20510 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
20512 ref->code = ref->code == DW_MACINFO_define
20513 ? DW_MACRO_GNU_define_indirect
20514 : DW_MACRO_GNU_undef_indirect;
20515 output_macinfo_op (ref);
20516 return;
20518 dw2_asm_output_data (1, ref->code,
20519 ref->code == DW_MACINFO_define
20520 ? "Define macro" : "Undefine macro");
20521 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
20522 (unsigned long) ref->lineno);
20523 dw2_asm_output_nstring (ref->info, -1, "The macro");
20524 break;
20525 case DW_MACRO_GNU_define_indirect:
20526 case DW_MACRO_GNU_undef_indirect:
20527 node = find_AT_string (ref->info);
20528 if (node->form != DW_FORM_strp)
20530 char label[32];
20531 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
20532 ++dw2_string_counter;
20533 node->label = xstrdup (label);
20534 node->form = DW_FORM_strp;
20536 dw2_asm_output_data (1, ref->code,
20537 ref->code == DW_MACRO_GNU_define_indirect
20538 ? "Define macro indirect"
20539 : "Undefine macro indirect");
20540 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
20541 (unsigned long) ref->lineno);
20542 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
20543 debug_str_section, "The macro: \"%s\"",
20544 ref->info);
20545 break;
20546 case DW_MACRO_GNU_transparent_include:
20547 dw2_asm_output_data (1, ref->code, "Transparent include");
20548 ASM_GENERATE_INTERNAL_LABEL (label,
20549 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
20550 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
20551 break;
20552 default:
20553 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
20554 ASM_COMMENT_START, (unsigned long) ref->code);
20555 break;
20559 /* Attempt to make a sequence of define/undef macinfo ops shareable with
20560 other compilation unit .debug_macinfo sections. IDX is the first
20561 index of a define/undef, return the number of ops that should be
20562 emitted in a comdat .debug_macinfo section and emit
20563 a DW_MACRO_GNU_transparent_include entry referencing it.
20564 If the define/undef entry should be emitted normally, return 0. */
20566 static unsigned
20567 optimize_macinfo_range (unsigned int idx, VEC (macinfo_entry, gc) *files,
20568 htab_t *macinfo_htab)
20570 macinfo_entry *first, *second, *cur, *inc;
20571 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
20572 unsigned char checksum[16];
20573 struct md5_ctx ctx;
20574 char *grp_name, *tail;
20575 const char *base;
20576 unsigned int i, count, encoded_filename_len, linebuf_len;
20577 void **slot;
20579 first = &VEC_index (macinfo_entry, macinfo_table, idx);
20580 second = &VEC_index (macinfo_entry, macinfo_table, idx + 1);
20582 /* Optimize only if there are at least two consecutive define/undef ops,
20583 and either all of them are before first DW_MACINFO_start_file
20584 with lineno {0,1} (i.e. predefined macro block), or all of them are
20585 in some included header file. */
20586 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
20587 return 0;
20588 if (VEC_empty (macinfo_entry, files))
20590 if (first->lineno > 1 || second->lineno > 1)
20591 return 0;
20593 else if (first->lineno == 0)
20594 return 0;
20596 /* Find the last define/undef entry that can be grouped together
20597 with first and at the same time compute md5 checksum of their
20598 codes, linenumbers and strings. */
20599 md5_init_ctx (&ctx);
20600 for (i = idx; VEC_iterate (macinfo_entry, macinfo_table, i, cur); i++)
20601 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
20602 break;
20603 else if (VEC_empty (macinfo_entry, files) && cur->lineno > 1)
20604 break;
20605 else
20607 unsigned char code = cur->code;
20608 md5_process_bytes (&code, 1, &ctx);
20609 checksum_uleb128 (cur->lineno, &ctx);
20610 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
20612 md5_finish_ctx (&ctx, checksum);
20613 count = i - idx;
20615 /* From the containing include filename (if any) pick up just
20616 usable characters from its basename. */
20617 if (VEC_empty (macinfo_entry, files))
20618 base = "";
20619 else
20620 base = lbasename (VEC_last (macinfo_entry, files).info);
20621 for (encoded_filename_len = 0, i = 0; base[i]; i++)
20622 if (ISIDNUM (base[i]) || base[i] == '.')
20623 encoded_filename_len++;
20624 /* Count . at the end. */
20625 if (encoded_filename_len)
20626 encoded_filename_len++;
20628 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
20629 linebuf_len = strlen (linebuf);
20631 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
20632 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
20633 + 16 * 2 + 1);
20634 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
20635 tail = grp_name + 4;
20636 if (encoded_filename_len)
20638 for (i = 0; base[i]; i++)
20639 if (ISIDNUM (base[i]) || base[i] == '.')
20640 *tail++ = base[i];
20641 *tail++ = '.';
20643 memcpy (tail, linebuf, linebuf_len);
20644 tail += linebuf_len;
20645 *tail++ = '.';
20646 for (i = 0; i < 16; i++)
20647 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
20649 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
20650 in the empty vector entry before the first define/undef. */
20651 inc = &VEC_index (macinfo_entry, macinfo_table, idx - 1);
20652 inc->code = DW_MACRO_GNU_transparent_include;
20653 inc->lineno = 0;
20654 inc->info = ggc_strdup (grp_name);
20655 if (*macinfo_htab == NULL)
20656 *macinfo_htab = htab_create (10, htab_macinfo_hash, htab_macinfo_eq, NULL);
20657 /* Avoid emitting duplicates. */
20658 slot = htab_find_slot (*macinfo_htab, inc, INSERT);
20659 if (*slot != NULL)
20661 inc->code = 0;
20662 inc->info = NULL;
20663 /* If such an entry has been used before, just emit
20664 a DW_MACRO_GNU_transparent_include op. */
20665 inc = (macinfo_entry *) *slot;
20666 output_macinfo_op (inc);
20667 /* And clear all macinfo_entry in the range to avoid emitting them
20668 in the second pass. */
20669 for (i = idx;
20670 VEC_iterate (macinfo_entry, macinfo_table, i, cur)
20671 && i < idx + count;
20672 i++)
20674 cur->code = 0;
20675 cur->info = NULL;
20678 else
20680 *slot = inc;
20681 inc->lineno = htab_elements (*macinfo_htab);
20682 output_macinfo_op (inc);
20684 return count;
20687 /* Output macinfo section(s). */
20689 static void
20690 output_macinfo (void)
20692 unsigned i;
20693 unsigned long length = VEC_length (macinfo_entry, macinfo_table);
20694 macinfo_entry *ref;
20695 VEC (macinfo_entry, gc) *files = NULL;
20696 htab_t macinfo_htab = NULL;
20698 if (! length)
20699 return;
20701 /* output_macinfo* uses these interchangeably. */
20702 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
20703 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
20704 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
20705 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
20707 /* For .debug_macro emit the section header. */
20708 if (!dwarf_strict)
20710 dw2_asm_output_data (2, 4, "DWARF macro version number");
20711 if (DWARF_OFFSET_SIZE == 8)
20712 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
20713 else
20714 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
20715 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_line_section_label,
20716 debug_line_section, NULL);
20719 /* In the first loop, it emits the primary .debug_macinfo section
20720 and after each emitted op the macinfo_entry is cleared.
20721 If a longer range of define/undef ops can be optimized using
20722 DW_MACRO_GNU_transparent_include, the
20723 DW_MACRO_GNU_transparent_include op is emitted and kept in
20724 the vector before the first define/undef in the range and the
20725 whole range of define/undef ops is not emitted and kept. */
20726 for (i = 0; VEC_iterate (macinfo_entry, macinfo_table, i, ref); i++)
20728 switch (ref->code)
20730 case DW_MACINFO_start_file:
20731 VEC_safe_push (macinfo_entry, gc, files, ref);
20732 break;
20733 case DW_MACINFO_end_file:
20734 if (!VEC_empty (macinfo_entry, files))
20735 VEC_pop (macinfo_entry, files);
20736 break;
20737 case DW_MACINFO_define:
20738 case DW_MACINFO_undef:
20739 if (!dwarf_strict
20740 && HAVE_COMDAT_GROUP
20741 && VEC_length (macinfo_entry, files) != 1
20742 && i > 0
20743 && i + 1 < length
20744 && VEC_index (macinfo_entry, macinfo_table, i - 1).code == 0)
20746 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
20747 if (count)
20749 i += count - 1;
20750 continue;
20753 break;
20754 case 0:
20755 /* A dummy entry may be inserted at the beginning to be able
20756 to optimize the whole block of predefined macros. */
20757 if (i == 0)
20758 continue;
20759 default:
20760 break;
20762 output_macinfo_op (ref);
20763 ref->info = NULL;
20764 ref->code = 0;
20767 if (macinfo_htab == NULL)
20768 return;
20770 htab_delete (macinfo_htab);
20772 /* If any DW_MACRO_GNU_transparent_include were used, on those
20773 DW_MACRO_GNU_transparent_include entries terminate the
20774 current chain and switch to a new comdat .debug_macinfo
20775 section and emit the define/undef entries within it. */
20776 for (i = 0; VEC_iterate (macinfo_entry, macinfo_table, i, ref); i++)
20777 switch (ref->code)
20779 case 0:
20780 continue;
20781 case DW_MACRO_GNU_transparent_include:
20783 char label[MAX_ARTIFICIAL_LABEL_BYTES];
20784 tree comdat_key = get_identifier (ref->info);
20785 /* Terminate the previous .debug_macinfo section. */
20786 dw2_asm_output_data (1, 0, "End compilation unit");
20787 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
20788 SECTION_DEBUG
20789 | SECTION_LINKONCE,
20790 comdat_key);
20791 ASM_GENERATE_INTERNAL_LABEL (label,
20792 DEBUG_MACRO_SECTION_LABEL,
20793 ref->lineno);
20794 ASM_OUTPUT_LABEL (asm_out_file, label);
20795 ref->code = 0;
20796 ref->info = NULL;
20797 dw2_asm_output_data (2, 4, "DWARF macro version number");
20798 if (DWARF_OFFSET_SIZE == 8)
20799 dw2_asm_output_data (1, 1, "Flags: 64-bit");
20800 else
20801 dw2_asm_output_data (1, 0, "Flags: 32-bit");
20803 break;
20804 case DW_MACINFO_define:
20805 case DW_MACINFO_undef:
20806 output_macinfo_op (ref);
20807 ref->code = 0;
20808 ref->info = NULL;
20809 break;
20810 default:
20811 gcc_unreachable ();
20815 /* Set up for Dwarf output at the start of compilation. */
20817 static void
20818 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
20820 /* Allocate the file_table. */
20821 file_table = htab_create_ggc (50, file_table_hash,
20822 file_table_eq, NULL);
20824 /* Allocate the decl_die_table. */
20825 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
20826 decl_die_table_eq, NULL);
20828 /* Allocate the decl_loc_table. */
20829 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
20830 decl_loc_table_eq, NULL);
20832 /* Allocate the cached_dw_loc_list_table. */
20833 cached_dw_loc_list_table
20834 = htab_create_ggc (10, cached_dw_loc_list_table_hash,
20835 cached_dw_loc_list_table_eq, NULL);
20837 /* Allocate the initial hunk of the decl_scope_table. */
20838 decl_scope_table = VEC_alloc (tree, gc, 256);
20840 /* Allocate the initial hunk of the abbrev_die_table. */
20841 abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
20842 (ABBREV_DIE_TABLE_INCREMENT);
20843 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
20844 /* Zero-th entry is allocated, but unused. */
20845 abbrev_die_table_in_use = 1;
20847 /* Allocate the pubtypes and pubnames vectors. */
20848 pubname_table = VEC_alloc (pubname_entry, gc, 32);
20849 pubtype_table = VEC_alloc (pubname_entry, gc, 32);
20851 incomplete_types = VEC_alloc (tree, gc, 64);
20853 used_rtx_array = VEC_alloc (rtx, gc, 32);
20855 debug_info_section = get_section (DEBUG_INFO_SECTION,
20856 SECTION_DEBUG, NULL);
20857 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
20858 SECTION_DEBUG, NULL);
20859 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
20860 SECTION_DEBUG, NULL);
20861 debug_macinfo_section = get_section (dwarf_strict
20862 ? DEBUG_MACINFO_SECTION
20863 : DEBUG_MACRO_SECTION,
20864 SECTION_DEBUG, NULL);
20865 debug_line_section = get_section (DEBUG_LINE_SECTION,
20866 SECTION_DEBUG, NULL);
20867 debug_loc_section = get_section (DEBUG_LOC_SECTION,
20868 SECTION_DEBUG, NULL);
20869 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
20870 SECTION_DEBUG, NULL);
20871 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
20872 SECTION_DEBUG, NULL);
20873 debug_str_section = get_section (DEBUG_STR_SECTION,
20874 DEBUG_STR_SECTION_FLAGS, NULL);
20875 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
20876 SECTION_DEBUG, NULL);
20877 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
20878 SECTION_DEBUG, NULL);
20880 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
20881 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
20882 DEBUG_ABBREV_SECTION_LABEL, 0);
20883 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
20884 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
20885 COLD_TEXT_SECTION_LABEL, 0);
20886 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
20888 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
20889 DEBUG_INFO_SECTION_LABEL, 0);
20890 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
20891 DEBUG_LINE_SECTION_LABEL, 0);
20892 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
20893 DEBUG_RANGES_SECTION_LABEL, 0);
20894 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
20895 dwarf_strict
20896 ? DEBUG_MACINFO_SECTION_LABEL
20897 : DEBUG_MACRO_SECTION_LABEL, 0);
20899 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20900 macinfo_table = VEC_alloc (macinfo_entry, gc, 64);
20902 switch_to_section (text_section);
20903 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
20905 /* Make sure the line number table for .text always exists. */
20906 text_section_line_info = new_line_info_table ();
20907 text_section_line_info->end_label = text_end_label;
20910 /* Called before compile () starts outputtting functions, variables
20911 and toplevel asms into assembly. */
20913 static void
20914 dwarf2out_assembly_start (void)
20916 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
20917 && dwarf2out_do_cfi_asm ()
20918 && (!(flag_unwind_tables || flag_exceptions)
20919 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
20920 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
20923 /* A helper function for dwarf2out_finish called through
20924 htab_traverse. Emit one queued .debug_str string. */
20926 static int
20927 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
20929 struct indirect_string_node *node = (struct indirect_string_node *) *h;
20931 if (node->form == DW_FORM_strp)
20933 switch_to_section (debug_str_section);
20934 ASM_OUTPUT_LABEL (asm_out_file, node->label);
20935 assemble_string (node->str, strlen (node->str) + 1);
20938 return 1;
20941 #if ENABLE_ASSERT_CHECKING
20942 /* Verify that all marks are clear. */
20944 static void
20945 verify_marks_clear (dw_die_ref die)
20947 dw_die_ref c;
20949 gcc_assert (! die->die_mark);
20950 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
20952 #endif /* ENABLE_ASSERT_CHECKING */
20954 /* Clear the marks for a die and its children.
20955 Be cool if the mark isn't set. */
20957 static void
20958 prune_unmark_dies (dw_die_ref die)
20960 dw_die_ref c;
20962 if (die->die_mark)
20963 die->die_mark = 0;
20964 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
20967 /* Given DIE that we're marking as used, find any other dies
20968 it references as attributes and mark them as used. */
20970 static void
20971 prune_unused_types_walk_attribs (dw_die_ref die)
20973 dw_attr_ref a;
20974 unsigned ix;
20976 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
20978 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
20980 /* A reference to another DIE.
20981 Make sure that it will get emitted.
20982 If it was broken out into a comdat group, don't follow it. */
20983 if (! AT_ref (a)->comdat_type_p
20984 || a->dw_attr == DW_AT_specification)
20985 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
20987 /* Set the string's refcount to 0 so that prune_unused_types_mark
20988 accounts properly for it. */
20989 if (AT_class (a) == dw_val_class_str)
20990 a->dw_attr_val.v.val_str->refcount = 0;
20994 /* Mark the generic parameters and arguments children DIEs of DIE. */
20996 static void
20997 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
20999 dw_die_ref c;
21001 if (die == NULL || die->die_child == NULL)
21002 return;
21003 c = die->die_child;
21006 switch (c->die_tag)
21008 case DW_TAG_template_type_param:
21009 case DW_TAG_template_value_param:
21010 case DW_TAG_GNU_template_template_param:
21011 case DW_TAG_GNU_template_parameter_pack:
21012 prune_unused_types_mark (c, 1);
21013 break;
21014 default:
21015 break;
21017 c = c->die_sib;
21018 } while (c && c != die->die_child);
21021 /* Mark DIE as being used. If DOKIDS is true, then walk down
21022 to DIE's children. */
21024 static void
21025 prune_unused_types_mark (dw_die_ref die, int dokids)
21027 dw_die_ref c;
21029 if (die->die_mark == 0)
21031 /* We haven't done this node yet. Mark it as used. */
21032 die->die_mark = 1;
21033 /* If this is the DIE of a generic type instantiation,
21034 mark the children DIEs that describe its generic parms and
21035 args. */
21036 prune_unused_types_mark_generic_parms_dies (die);
21038 /* We also have to mark its parents as used.
21039 (But we don't want to mark our parents' kids due to this.) */
21040 if (die->die_parent)
21041 prune_unused_types_mark (die->die_parent, 0);
21043 /* Mark any referenced nodes. */
21044 prune_unused_types_walk_attribs (die);
21046 /* If this node is a specification,
21047 also mark the definition, if it exists. */
21048 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
21049 prune_unused_types_mark (die->die_definition, 1);
21052 if (dokids && die->die_mark != 2)
21054 /* We need to walk the children, but haven't done so yet.
21055 Remember that we've walked the kids. */
21056 die->die_mark = 2;
21058 /* If this is an array type, we need to make sure our
21059 kids get marked, even if they're types. If we're
21060 breaking out types into comdat sections, do this
21061 for all type definitions. */
21062 if (die->die_tag == DW_TAG_array_type
21063 || (use_debug_types
21064 && is_type_die (die) && ! is_declaration_die (die)))
21065 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
21066 else
21067 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
21071 /* For local classes, look if any static member functions were emitted
21072 and if so, mark them. */
21074 static void
21075 prune_unused_types_walk_local_classes (dw_die_ref die)
21077 dw_die_ref c;
21079 if (die->die_mark == 2)
21080 return;
21082 switch (die->die_tag)
21084 case DW_TAG_structure_type:
21085 case DW_TAG_union_type:
21086 case DW_TAG_class_type:
21087 break;
21089 case DW_TAG_subprogram:
21090 if (!get_AT_flag (die, DW_AT_declaration)
21091 || die->die_definition != NULL)
21092 prune_unused_types_mark (die, 1);
21093 return;
21095 default:
21096 return;
21099 /* Mark children. */
21100 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
21103 /* Walk the tree DIE and mark types that we actually use. */
21105 static void
21106 prune_unused_types_walk (dw_die_ref die)
21108 dw_die_ref c;
21110 /* Don't do anything if this node is already marked and
21111 children have been marked as well. */
21112 if (die->die_mark == 2)
21113 return;
21115 switch (die->die_tag)
21117 case DW_TAG_structure_type:
21118 case DW_TAG_union_type:
21119 case DW_TAG_class_type:
21120 if (die->die_perennial_p)
21121 break;
21123 for (c = die->die_parent; c; c = c->die_parent)
21124 if (c->die_tag == DW_TAG_subprogram)
21125 break;
21127 /* Finding used static member functions inside of classes
21128 is needed just for local classes, because for other classes
21129 static member function DIEs with DW_AT_specification
21130 are emitted outside of the DW_TAG_*_type. If we ever change
21131 it, we'd need to call this even for non-local classes. */
21132 if (c)
21133 prune_unused_types_walk_local_classes (die);
21135 /* It's a type node --- don't mark it. */
21136 return;
21138 case DW_TAG_const_type:
21139 case DW_TAG_packed_type:
21140 case DW_TAG_pointer_type:
21141 case DW_TAG_reference_type:
21142 case DW_TAG_rvalue_reference_type:
21143 case DW_TAG_volatile_type:
21144 case DW_TAG_typedef:
21145 case DW_TAG_array_type:
21146 case DW_TAG_interface_type:
21147 case DW_TAG_friend:
21148 case DW_TAG_variant_part:
21149 case DW_TAG_enumeration_type:
21150 case DW_TAG_subroutine_type:
21151 case DW_TAG_string_type:
21152 case DW_TAG_set_type:
21153 case DW_TAG_subrange_type:
21154 case DW_TAG_ptr_to_member_type:
21155 case DW_TAG_file_type:
21156 if (die->die_perennial_p)
21157 break;
21159 /* It's a type node --- don't mark it. */
21160 return;
21162 default:
21163 /* Mark everything else. */
21164 break;
21167 if (die->die_mark == 0)
21169 die->die_mark = 1;
21171 /* Now, mark any dies referenced from here. */
21172 prune_unused_types_walk_attribs (die);
21175 die->die_mark = 2;
21177 /* Mark children. */
21178 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
21181 /* Increment the string counts on strings referred to from DIE's
21182 attributes. */
21184 static void
21185 prune_unused_types_update_strings (dw_die_ref die)
21187 dw_attr_ref a;
21188 unsigned ix;
21190 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
21191 if (AT_class (a) == dw_val_class_str)
21193 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
21194 s->refcount++;
21195 /* Avoid unnecessarily putting strings that are used less than
21196 twice in the hash table. */
21197 if (s->refcount
21198 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
21200 void ** slot;
21201 slot = htab_find_slot_with_hash (debug_str_hash, s->str,
21202 htab_hash_string (s->str),
21203 INSERT);
21204 gcc_assert (*slot == NULL);
21205 *slot = s;
21210 /* Remove from the tree DIE any dies that aren't marked. */
21212 static void
21213 prune_unused_types_prune (dw_die_ref die)
21215 dw_die_ref c;
21217 gcc_assert (die->die_mark);
21218 prune_unused_types_update_strings (die);
21220 if (! die->die_child)
21221 return;
21223 c = die->die_child;
21224 do {
21225 dw_die_ref prev = c;
21226 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
21227 if (c == die->die_child)
21229 /* No marked children between 'prev' and the end of the list. */
21230 if (prev == c)
21231 /* No marked children at all. */
21232 die->die_child = NULL;
21233 else
21235 prev->die_sib = c->die_sib;
21236 die->die_child = prev;
21238 return;
21241 if (c != prev->die_sib)
21242 prev->die_sib = c;
21243 prune_unused_types_prune (c);
21244 } while (c != die->die_child);
21247 /* Remove dies representing declarations that we never use. */
21249 static void
21250 prune_unused_types (void)
21252 unsigned int i;
21253 limbo_die_node *node;
21254 comdat_type_node *ctnode;
21255 pubname_ref pub;
21256 dw_die_ref base_type;
21258 #if ENABLE_ASSERT_CHECKING
21259 /* All the marks should already be clear. */
21260 verify_marks_clear (comp_unit_die ());
21261 for (node = limbo_die_list; node; node = node->next)
21262 verify_marks_clear (node->die);
21263 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21264 verify_marks_clear (ctnode->root_die);
21265 #endif /* ENABLE_ASSERT_CHECKING */
21267 /* Mark types that are used in global variables. */
21268 premark_types_used_by_global_vars ();
21270 /* Set the mark on nodes that are actually used. */
21271 prune_unused_types_walk (comp_unit_die ());
21272 for (node = limbo_die_list; node; node = node->next)
21273 prune_unused_types_walk (node->die);
21274 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21276 prune_unused_types_walk (ctnode->root_die);
21277 prune_unused_types_mark (ctnode->type_die, 1);
21280 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
21281 are unusual in that they are pubnames that are the children of pubtypes.
21282 They should only be marked via their parent DW_TAG_enumeration_type die,
21283 not as roots in themselves. */
21284 FOR_EACH_VEC_ELT (pubname_entry, pubname_table, i, pub)
21285 if (pub->die->die_tag != DW_TAG_enumerator)
21286 prune_unused_types_mark (pub->die, 1);
21287 for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
21288 prune_unused_types_mark (base_type, 1);
21290 if (debug_str_hash)
21291 htab_empty (debug_str_hash);
21292 prune_unused_types_prune (comp_unit_die ());
21293 for (node = limbo_die_list; node; node = node->next)
21294 prune_unused_types_prune (node->die);
21295 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21296 prune_unused_types_prune (ctnode->root_die);
21298 /* Leave the marks clear. */
21299 prune_unmark_dies (comp_unit_die ());
21300 for (node = limbo_die_list; node; node = node->next)
21301 prune_unmark_dies (node->die);
21302 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21303 prune_unmark_dies (ctnode->root_die);
21306 /* Set the parameter to true if there are any relative pathnames in
21307 the file table. */
21308 static int
21309 file_table_relative_p (void ** slot, void *param)
21311 bool *p = (bool *) param;
21312 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
21313 if (!IS_ABSOLUTE_PATH (d->filename))
21315 *p = true;
21316 return 0;
21318 return 1;
21321 /* Routines to manipulate hash table of comdat type units. */
21323 static hashval_t
21324 htab_ct_hash (const void *of)
21326 hashval_t h;
21327 const comdat_type_node *const type_node = (const comdat_type_node *) of;
21329 memcpy (&h, type_node->signature, sizeof (h));
21330 return h;
21333 static int
21334 htab_ct_eq (const void *of1, const void *of2)
21336 const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
21337 const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
21339 return (! memcmp (type_node_1->signature, type_node_2->signature,
21340 DWARF_TYPE_SIGNATURE_SIZE));
21343 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
21344 to the location it would have been added, should we know its
21345 DECL_ASSEMBLER_NAME when we added other attributes. This will
21346 probably improve compactness of debug info, removing equivalent
21347 abbrevs, and hide any differences caused by deferring the
21348 computation of the assembler name, triggered by e.g. PCH. */
21350 static inline void
21351 move_linkage_attr (dw_die_ref die)
21353 unsigned ix = VEC_length (dw_attr_node, die->die_attr);
21354 dw_attr_node linkage = VEC_index (dw_attr_node, die->die_attr, ix - 1);
21356 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
21357 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
21359 while (--ix > 0)
21361 dw_attr_node *prev = &VEC_index (dw_attr_node, die->die_attr, ix - 1);
21363 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
21364 break;
21367 if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
21369 VEC_pop (dw_attr_node, die->die_attr);
21370 VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
21374 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
21375 referenced from typed stack ops and count how often they are used. */
21377 static void
21378 mark_base_types (dw_loc_descr_ref loc)
21380 dw_die_ref base_type = NULL;
21382 for (; loc; loc = loc->dw_loc_next)
21384 switch (loc->dw_loc_opc)
21386 case DW_OP_GNU_regval_type:
21387 case DW_OP_GNU_deref_type:
21388 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
21389 break;
21390 case DW_OP_GNU_convert:
21391 case DW_OP_GNU_reinterpret:
21392 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
21393 continue;
21394 /* FALLTHRU */
21395 case DW_OP_GNU_const_type:
21396 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
21397 break;
21398 case DW_OP_GNU_entry_value:
21399 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
21400 continue;
21401 default:
21402 continue;
21404 gcc_assert (base_type->die_parent == comp_unit_die ());
21405 if (base_type->die_mark)
21406 base_type->die_mark++;
21407 else
21409 VEC_safe_push (dw_die_ref, heap, base_types, base_type);
21410 base_type->die_mark = 1;
21415 /* Comparison function for sorting marked base types. */
21417 static int
21418 base_type_cmp (const void *x, const void *y)
21420 dw_die_ref dx = *(const dw_die_ref *) x;
21421 dw_die_ref dy = *(const dw_die_ref *) y;
21422 unsigned int byte_size1, byte_size2;
21423 unsigned int encoding1, encoding2;
21424 if (dx->die_mark > dy->die_mark)
21425 return -1;
21426 if (dx->die_mark < dy->die_mark)
21427 return 1;
21428 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
21429 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
21430 if (byte_size1 < byte_size2)
21431 return 1;
21432 if (byte_size1 > byte_size2)
21433 return -1;
21434 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
21435 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
21436 if (encoding1 < encoding2)
21437 return 1;
21438 if (encoding1 > encoding2)
21439 return -1;
21440 return 0;
21443 /* Move base types marked by mark_base_types as early as possible
21444 in the CU, sorted by decreasing usage count both to make the
21445 uleb128 references as small as possible and to make sure they
21446 will have die_offset already computed by calc_die_sizes when
21447 sizes of typed stack loc ops is computed. */
21449 static void
21450 move_marked_base_types (void)
21452 unsigned int i;
21453 dw_die_ref base_type, die, c;
21455 if (VEC_empty (dw_die_ref, base_types))
21456 return;
21458 /* Sort by decreasing usage count, they will be added again in that
21459 order later on. */
21460 VEC_qsort (dw_die_ref, base_types, base_type_cmp);
21461 die = comp_unit_die ();
21462 c = die->die_child;
21465 dw_die_ref prev = c;
21466 c = c->die_sib;
21467 while (c->die_mark)
21469 remove_child_with_prev (c, prev);
21470 /* As base types got marked, there must be at least
21471 one node other than DW_TAG_base_type. */
21472 gcc_assert (c != c->die_sib);
21473 c = c->die_sib;
21476 while (c != die->die_child);
21477 gcc_assert (die->die_child);
21478 c = die->die_child;
21479 for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
21481 base_type->die_mark = 0;
21482 base_type->die_sib = c->die_sib;
21483 c->die_sib = base_type;
21484 c = base_type;
21488 /* Helper function for resolve_addr, attempt to resolve
21489 one CONST_STRING, return non-zero if not successful. Similarly verify that
21490 SYMBOL_REFs refer to variables emitted in the current CU. */
21492 static int
21493 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
21495 rtx rtl = *addr;
21497 if (GET_CODE (rtl) == CONST_STRING)
21499 size_t len = strlen (XSTR (rtl, 0)) + 1;
21500 tree t = build_string (len, XSTR (rtl, 0));
21501 tree tlen = size_int (len - 1);
21502 TREE_TYPE (t)
21503 = build_array_type (char_type_node, build_index_type (tlen));
21504 rtl = lookup_constant_def (t);
21505 if (!rtl || !MEM_P (rtl))
21506 return 1;
21507 rtl = XEXP (rtl, 0);
21508 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
21509 *addr = rtl;
21510 return 0;
21513 if (GET_CODE (rtl) == SYMBOL_REF
21514 && SYMBOL_REF_DECL (rtl))
21516 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
21518 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
21519 return 1;
21521 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
21522 return 1;
21525 if (GET_CODE (rtl) == CONST
21526 && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
21527 return 1;
21529 return 0;
21532 /* Helper function for resolve_addr, handle one location
21533 expression, return false if at least one CONST_STRING or SYMBOL_REF in
21534 the location list couldn't be resolved. */
21536 static bool
21537 resolve_addr_in_expr (dw_loc_descr_ref loc)
21539 dw_loc_descr_ref keep = NULL;
21540 for (; loc; loc = loc->dw_loc_next)
21541 switch (loc->dw_loc_opc)
21543 case DW_OP_addr:
21544 if (resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
21545 return false;
21546 break;
21547 case DW_OP_const4u:
21548 case DW_OP_const8u:
21549 if (loc->dtprel
21550 && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
21551 return false;
21552 break;
21553 case DW_OP_plus_uconst:
21554 if (size_of_loc_descr (loc)
21555 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
21557 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
21559 dw_loc_descr_ref repl
21560 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
21561 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
21562 add_loc_descr (&repl, loc->dw_loc_next);
21563 *loc = *repl;
21565 break;
21566 case DW_OP_implicit_value:
21567 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
21568 && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL))
21569 return false;
21570 break;
21571 case DW_OP_GNU_implicit_pointer:
21572 case DW_OP_GNU_parameter_ref:
21573 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
21575 dw_die_ref ref
21576 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
21577 if (ref == NULL)
21578 return false;
21579 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
21580 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
21581 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
21583 break;
21584 case DW_OP_GNU_const_type:
21585 case DW_OP_GNU_regval_type:
21586 case DW_OP_GNU_deref_type:
21587 case DW_OP_GNU_convert:
21588 case DW_OP_GNU_reinterpret:
21589 while (loc->dw_loc_next
21590 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
21592 dw_die_ref base1, base2;
21593 unsigned enc1, enc2, size1, size2;
21594 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
21595 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
21596 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
21597 else if (loc->dw_loc_oprnd1.val_class
21598 == dw_val_class_unsigned_const)
21599 break;
21600 else
21601 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
21602 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
21603 == dw_val_class_unsigned_const)
21604 break;
21605 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
21606 gcc_assert (base1->die_tag == DW_TAG_base_type
21607 && base2->die_tag == DW_TAG_base_type);
21608 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
21609 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
21610 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
21611 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
21612 if (size1 == size2
21613 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
21614 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
21615 && loc != keep)
21616 || enc1 == enc2))
21618 /* Optimize away next DW_OP_GNU_convert after
21619 adjusting LOC's base type die reference. */
21620 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
21621 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
21622 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
21623 else
21624 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
21625 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
21626 continue;
21628 /* Don't change integer DW_OP_GNU_convert after e.g. floating
21629 point typed stack entry. */
21630 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
21631 keep = loc->dw_loc_next;
21632 break;
21634 break;
21635 default:
21636 break;
21638 return true;
21641 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
21642 an address in .rodata section if the string literal is emitted there,
21643 or remove the containing location list or replace DW_AT_const_value
21644 with DW_AT_location and empty location expression, if it isn't found
21645 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
21646 to something that has been emitted in the current CU. */
21648 static void
21649 resolve_addr (dw_die_ref die)
21651 dw_die_ref c;
21652 dw_attr_ref a;
21653 dw_loc_list_ref *curr, *start, loc;
21654 unsigned ix;
21656 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
21657 switch (AT_class (a))
21659 case dw_val_class_loc_list:
21660 start = curr = AT_loc_list_ptr (a);
21661 loc = *curr;
21662 gcc_assert (loc);
21663 /* The same list can be referenced more than once. See if we have
21664 already recorded the result from a previous pass. */
21665 if (loc->replaced)
21666 *curr = loc->dw_loc_next;
21667 else if (!loc->resolved_addr)
21669 /* As things stand, we do not expect or allow one die to
21670 reference a suffix of another die's location list chain.
21671 References must be identical or completely separate.
21672 There is therefore no need to cache the result of this
21673 pass on any list other than the first; doing so
21674 would lead to unnecessary writes. */
21675 while (*curr)
21677 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
21678 if (!resolve_addr_in_expr ((*curr)->expr))
21680 dw_loc_list_ref next = (*curr)->dw_loc_next;
21681 if (next && (*curr)->ll_symbol)
21683 gcc_assert (!next->ll_symbol);
21684 next->ll_symbol = (*curr)->ll_symbol;
21686 *curr = next;
21688 else
21690 mark_base_types ((*curr)->expr);
21691 curr = &(*curr)->dw_loc_next;
21694 if (loc == *start)
21695 loc->resolved_addr = 1;
21696 else
21698 loc->replaced = 1;
21699 loc->dw_loc_next = *start;
21702 if (!*start)
21704 remove_AT (die, a->dw_attr);
21705 ix--;
21707 break;
21708 case dw_val_class_loc:
21710 dw_loc_descr_ref l = AT_loc (a);
21711 /* For -gdwarf-2 don't attempt to optimize
21712 DW_AT_data_member_location containing
21713 DW_OP_plus_uconst - older consumers might
21714 rely on it being that op instead of a more complex,
21715 but shorter, location description. */
21716 if ((dwarf_version > 2
21717 || a->dw_attr != DW_AT_data_member_location
21718 || l == NULL
21719 || l->dw_loc_opc != DW_OP_plus_uconst
21720 || l->dw_loc_next != NULL)
21721 && !resolve_addr_in_expr (l))
21723 remove_AT (die, a->dw_attr);
21724 ix--;
21726 else
21727 mark_base_types (l);
21729 break;
21730 case dw_val_class_addr:
21731 if (a->dw_attr == DW_AT_const_value
21732 && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
21734 remove_AT (die, a->dw_attr);
21735 ix--;
21737 if (die->die_tag == DW_TAG_GNU_call_site
21738 && a->dw_attr == DW_AT_abstract_origin)
21740 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
21741 dw_die_ref tdie = lookup_decl_die (tdecl);
21742 if (tdie == NULL
21743 && DECL_EXTERNAL (tdecl)
21744 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
21746 force_decl_die (tdecl);
21747 tdie = lookup_decl_die (tdecl);
21749 if (tdie)
21751 a->dw_attr_val.val_class = dw_val_class_die_ref;
21752 a->dw_attr_val.v.val_die_ref.die = tdie;
21753 a->dw_attr_val.v.val_die_ref.external = 0;
21755 else
21757 remove_AT (die, a->dw_attr);
21758 ix--;
21761 break;
21762 default:
21763 break;
21766 FOR_EACH_CHILD (die, c, resolve_addr (c));
21769 /* Helper routines for optimize_location_lists.
21770 This pass tries to share identical local lists in .debug_loc
21771 section. */
21773 /* Iteratively hash operands of LOC opcode. */
21775 static inline hashval_t
21776 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
21778 dw_val_ref val1 = &loc->dw_loc_oprnd1;
21779 dw_val_ref val2 = &loc->dw_loc_oprnd2;
21781 switch (loc->dw_loc_opc)
21783 case DW_OP_const4u:
21784 case DW_OP_const8u:
21785 if (loc->dtprel)
21786 goto hash_addr;
21787 /* FALLTHRU */
21788 case DW_OP_const1u:
21789 case DW_OP_const1s:
21790 case DW_OP_const2u:
21791 case DW_OP_const2s:
21792 case DW_OP_const4s:
21793 case DW_OP_const8s:
21794 case DW_OP_constu:
21795 case DW_OP_consts:
21796 case DW_OP_pick:
21797 case DW_OP_plus_uconst:
21798 case DW_OP_breg0:
21799 case DW_OP_breg1:
21800 case DW_OP_breg2:
21801 case DW_OP_breg3:
21802 case DW_OP_breg4:
21803 case DW_OP_breg5:
21804 case DW_OP_breg6:
21805 case DW_OP_breg7:
21806 case DW_OP_breg8:
21807 case DW_OP_breg9:
21808 case DW_OP_breg10:
21809 case DW_OP_breg11:
21810 case DW_OP_breg12:
21811 case DW_OP_breg13:
21812 case DW_OP_breg14:
21813 case DW_OP_breg15:
21814 case DW_OP_breg16:
21815 case DW_OP_breg17:
21816 case DW_OP_breg18:
21817 case DW_OP_breg19:
21818 case DW_OP_breg20:
21819 case DW_OP_breg21:
21820 case DW_OP_breg22:
21821 case DW_OP_breg23:
21822 case DW_OP_breg24:
21823 case DW_OP_breg25:
21824 case DW_OP_breg26:
21825 case DW_OP_breg27:
21826 case DW_OP_breg28:
21827 case DW_OP_breg29:
21828 case DW_OP_breg30:
21829 case DW_OP_breg31:
21830 case DW_OP_regx:
21831 case DW_OP_fbreg:
21832 case DW_OP_piece:
21833 case DW_OP_deref_size:
21834 case DW_OP_xderef_size:
21835 hash = iterative_hash_object (val1->v.val_int, hash);
21836 break;
21837 case DW_OP_skip:
21838 case DW_OP_bra:
21840 int offset;
21842 gcc_assert (val1->val_class == dw_val_class_loc);
21843 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
21844 hash = iterative_hash_object (offset, hash);
21846 break;
21847 case DW_OP_implicit_value:
21848 hash = iterative_hash_object (val1->v.val_unsigned, hash);
21849 switch (val2->val_class)
21851 case dw_val_class_const:
21852 hash = iterative_hash_object (val2->v.val_int, hash);
21853 break;
21854 case dw_val_class_vec:
21856 unsigned int elt_size = val2->v.val_vec.elt_size;
21857 unsigned int len = val2->v.val_vec.length;
21859 hash = iterative_hash_object (elt_size, hash);
21860 hash = iterative_hash_object (len, hash);
21861 hash = iterative_hash (val2->v.val_vec.array,
21862 len * elt_size, hash);
21864 break;
21865 case dw_val_class_const_double:
21866 hash = iterative_hash_object (val2->v.val_double.low, hash);
21867 hash = iterative_hash_object (val2->v.val_double.high, hash);
21868 break;
21869 case dw_val_class_addr:
21870 hash = iterative_hash_rtx (val2->v.val_addr, hash);
21871 break;
21872 default:
21873 gcc_unreachable ();
21875 break;
21876 case DW_OP_bregx:
21877 case DW_OP_bit_piece:
21878 hash = iterative_hash_object (val1->v.val_int, hash);
21879 hash = iterative_hash_object (val2->v.val_int, hash);
21880 break;
21881 case DW_OP_addr:
21882 hash_addr:
21883 if (loc->dtprel)
21885 unsigned char dtprel = 0xd1;
21886 hash = iterative_hash_object (dtprel, hash);
21888 hash = iterative_hash_rtx (val1->v.val_addr, hash);
21889 break;
21890 case DW_OP_GNU_implicit_pointer:
21891 hash = iterative_hash_object (val2->v.val_int, hash);
21892 break;
21893 case DW_OP_GNU_entry_value:
21894 hash = hash_loc_operands (val1->v.val_loc, hash);
21895 break;
21896 case DW_OP_GNU_regval_type:
21897 case DW_OP_GNU_deref_type:
21899 unsigned int byte_size
21900 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
21901 unsigned int encoding
21902 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
21903 hash = iterative_hash_object (val1->v.val_int, hash);
21904 hash = iterative_hash_object (byte_size, hash);
21905 hash = iterative_hash_object (encoding, hash);
21907 break;
21908 case DW_OP_GNU_convert:
21909 case DW_OP_GNU_reinterpret:
21910 if (val1->val_class == dw_val_class_unsigned_const)
21912 hash = iterative_hash_object (val1->v.val_unsigned, hash);
21913 break;
21915 /* FALLTHRU */
21916 case DW_OP_GNU_const_type:
21918 unsigned int byte_size
21919 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
21920 unsigned int encoding
21921 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
21922 hash = iterative_hash_object (byte_size, hash);
21923 hash = iterative_hash_object (encoding, hash);
21924 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
21925 break;
21926 hash = iterative_hash_object (val2->val_class, hash);
21927 switch (val2->val_class)
21929 case dw_val_class_const:
21930 hash = iterative_hash_object (val2->v.val_int, hash);
21931 break;
21932 case dw_val_class_vec:
21934 unsigned int elt_size = val2->v.val_vec.elt_size;
21935 unsigned int len = val2->v.val_vec.length;
21937 hash = iterative_hash_object (elt_size, hash);
21938 hash = iterative_hash_object (len, hash);
21939 hash = iterative_hash (val2->v.val_vec.array,
21940 len * elt_size, hash);
21942 break;
21943 case dw_val_class_const_double:
21944 hash = iterative_hash_object (val2->v.val_double.low, hash);
21945 hash = iterative_hash_object (val2->v.val_double.high, hash);
21946 break;
21947 default:
21948 gcc_unreachable ();
21951 break;
21953 default:
21954 /* Other codes have no operands. */
21955 break;
21957 return hash;
21960 /* Iteratively hash the whole DWARF location expression LOC. */
21962 static inline hashval_t
21963 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
21965 dw_loc_descr_ref l;
21966 bool sizes_computed = false;
21967 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
21968 size_of_locs (loc);
21970 for (l = loc; l != NULL; l = l->dw_loc_next)
21972 enum dwarf_location_atom opc = l->dw_loc_opc;
21973 hash = iterative_hash_object (opc, hash);
21974 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
21976 size_of_locs (loc);
21977 sizes_computed = true;
21979 hash = hash_loc_operands (l, hash);
21981 return hash;
21984 /* Compute hash of the whole location list LIST_HEAD. */
21986 static inline void
21987 hash_loc_list (dw_loc_list_ref list_head)
21989 dw_loc_list_ref curr = list_head;
21990 hashval_t hash = 0;
21992 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
21994 hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
21995 hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
21996 if (curr->section)
21997 hash = iterative_hash (curr->section, strlen (curr->section) + 1,
21998 hash);
21999 hash = hash_locs (curr->expr, hash);
22001 list_head->hash = hash;
22004 /* Return true if X and Y opcodes have the same operands. */
22006 static inline bool
22007 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
22009 dw_val_ref valx1 = &x->dw_loc_oprnd1;
22010 dw_val_ref valx2 = &x->dw_loc_oprnd2;
22011 dw_val_ref valy1 = &y->dw_loc_oprnd1;
22012 dw_val_ref valy2 = &y->dw_loc_oprnd2;
22014 switch (x->dw_loc_opc)
22016 case DW_OP_const4u:
22017 case DW_OP_const8u:
22018 if (x->dtprel)
22019 goto hash_addr;
22020 /* FALLTHRU */
22021 case DW_OP_const1u:
22022 case DW_OP_const1s:
22023 case DW_OP_const2u:
22024 case DW_OP_const2s:
22025 case DW_OP_const4s:
22026 case DW_OP_const8s:
22027 case DW_OP_constu:
22028 case DW_OP_consts:
22029 case DW_OP_pick:
22030 case DW_OP_plus_uconst:
22031 case DW_OP_breg0:
22032 case DW_OP_breg1:
22033 case DW_OP_breg2:
22034 case DW_OP_breg3:
22035 case DW_OP_breg4:
22036 case DW_OP_breg5:
22037 case DW_OP_breg6:
22038 case DW_OP_breg7:
22039 case DW_OP_breg8:
22040 case DW_OP_breg9:
22041 case DW_OP_breg10:
22042 case DW_OP_breg11:
22043 case DW_OP_breg12:
22044 case DW_OP_breg13:
22045 case DW_OP_breg14:
22046 case DW_OP_breg15:
22047 case DW_OP_breg16:
22048 case DW_OP_breg17:
22049 case DW_OP_breg18:
22050 case DW_OP_breg19:
22051 case DW_OP_breg20:
22052 case DW_OP_breg21:
22053 case DW_OP_breg22:
22054 case DW_OP_breg23:
22055 case DW_OP_breg24:
22056 case DW_OP_breg25:
22057 case DW_OP_breg26:
22058 case DW_OP_breg27:
22059 case DW_OP_breg28:
22060 case DW_OP_breg29:
22061 case DW_OP_breg30:
22062 case DW_OP_breg31:
22063 case DW_OP_regx:
22064 case DW_OP_fbreg:
22065 case DW_OP_piece:
22066 case DW_OP_deref_size:
22067 case DW_OP_xderef_size:
22068 return valx1->v.val_int == valy1->v.val_int;
22069 case DW_OP_skip:
22070 case DW_OP_bra:
22071 gcc_assert (valx1->val_class == dw_val_class_loc
22072 && valy1->val_class == dw_val_class_loc
22073 && x->dw_loc_addr == y->dw_loc_addr);
22074 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
22075 case DW_OP_implicit_value:
22076 if (valx1->v.val_unsigned != valy1->v.val_unsigned
22077 || valx2->val_class != valy2->val_class)
22078 return false;
22079 switch (valx2->val_class)
22081 case dw_val_class_const:
22082 return valx2->v.val_int == valy2->v.val_int;
22083 case dw_val_class_vec:
22084 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
22085 && valx2->v.val_vec.length == valy2->v.val_vec.length
22086 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
22087 valx2->v.val_vec.elt_size
22088 * valx2->v.val_vec.length) == 0;
22089 case dw_val_class_const_double:
22090 return valx2->v.val_double.low == valy2->v.val_double.low
22091 && valx2->v.val_double.high == valy2->v.val_double.high;
22092 case dw_val_class_addr:
22093 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
22094 default:
22095 gcc_unreachable ();
22097 case DW_OP_bregx:
22098 case DW_OP_bit_piece:
22099 return valx1->v.val_int == valy1->v.val_int
22100 && valx2->v.val_int == valy2->v.val_int;
22101 case DW_OP_addr:
22102 hash_addr:
22103 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
22104 case DW_OP_GNU_implicit_pointer:
22105 return valx1->val_class == dw_val_class_die_ref
22106 && valx1->val_class == valy1->val_class
22107 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
22108 && valx2->v.val_int == valy2->v.val_int;
22109 case DW_OP_GNU_entry_value:
22110 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
22111 case DW_OP_GNU_const_type:
22112 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
22113 || valx2->val_class != valy2->val_class)
22114 return false;
22115 switch (valx2->val_class)
22117 case dw_val_class_const:
22118 return valx2->v.val_int == valy2->v.val_int;
22119 case dw_val_class_vec:
22120 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
22121 && valx2->v.val_vec.length == valy2->v.val_vec.length
22122 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
22123 valx2->v.val_vec.elt_size
22124 * valx2->v.val_vec.length) == 0;
22125 case dw_val_class_const_double:
22126 return valx2->v.val_double.low == valy2->v.val_double.low
22127 && valx2->v.val_double.high == valy2->v.val_double.high;
22128 default:
22129 gcc_unreachable ();
22131 case DW_OP_GNU_regval_type:
22132 case DW_OP_GNU_deref_type:
22133 return valx1->v.val_int == valy1->v.val_int
22134 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
22135 case DW_OP_GNU_convert:
22136 case DW_OP_GNU_reinterpret:
22137 if (valx1->val_class != valy1->val_class)
22138 return false;
22139 if (valx1->val_class == dw_val_class_unsigned_const)
22140 return valx1->v.val_unsigned == valy1->v.val_unsigned;
22141 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
22142 case DW_OP_GNU_parameter_ref:
22143 return valx1->val_class == dw_val_class_die_ref
22144 && valx1->val_class == valy1->val_class
22145 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
22146 default:
22147 /* Other codes have no operands. */
22148 return true;
22152 /* Return true if DWARF location expressions X and Y are the same. */
22154 static inline bool
22155 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
22157 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
22158 if (x->dw_loc_opc != y->dw_loc_opc
22159 || x->dtprel != y->dtprel
22160 || !compare_loc_operands (x, y))
22161 break;
22162 return x == NULL && y == NULL;
22165 /* Return precomputed hash of location list X. */
22167 static hashval_t
22168 loc_list_hash (const void *x)
22170 return ((const struct dw_loc_list_struct *) x)->hash;
22173 /* Return 1 if location lists X and Y are the same. */
22175 static int
22176 loc_list_eq (const void *x, const void *y)
22178 const struct dw_loc_list_struct *a = (const struct dw_loc_list_struct *) x;
22179 const struct dw_loc_list_struct *b = (const struct dw_loc_list_struct *) y;
22180 if (a == b)
22181 return 1;
22182 if (a->hash != b->hash)
22183 return 0;
22184 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
22185 if (strcmp (a->begin, b->begin) != 0
22186 || strcmp (a->end, b->end) != 0
22187 || (a->section == NULL) != (b->section == NULL)
22188 || (a->section && strcmp (a->section, b->section) != 0)
22189 || !compare_locs (a->expr, b->expr))
22190 break;
22191 return a == NULL && b == NULL;
22194 /* Recursively optimize location lists referenced from DIE
22195 children and share them whenever possible. */
22197 static void
22198 optimize_location_lists_1 (dw_die_ref die, htab_t htab)
22200 dw_die_ref c;
22201 dw_attr_ref a;
22202 unsigned ix;
22203 void **slot;
22205 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22206 if (AT_class (a) == dw_val_class_loc_list)
22208 dw_loc_list_ref list = AT_loc_list (a);
22209 /* TODO: perform some optimizations here, before hashing
22210 it and storing into the hash table. */
22211 hash_loc_list (list);
22212 slot = htab_find_slot_with_hash (htab, list, list->hash,
22213 INSERT);
22214 if (*slot == NULL)
22215 *slot = (void *) list;
22216 else
22217 a->dw_attr_val.v.val_loc_list = (dw_loc_list_ref) *slot;
22220 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
22223 /* Optimize location lists referenced from DIE
22224 children and share them whenever possible. */
22226 static void
22227 optimize_location_lists (dw_die_ref die)
22229 htab_t htab = htab_create (500, loc_list_hash, loc_list_eq, NULL);
22230 optimize_location_lists_1 (die, htab);
22231 htab_delete (htab);
22234 /* Output stuff that dwarf requires at the end of every file,
22235 and generate the DWARF-2 debugging info. */
22237 static void
22238 dwarf2out_finish (const char *filename)
22240 limbo_die_node *node, *next_node;
22241 comdat_type_node *ctnode;
22242 htab_t comdat_type_table;
22243 unsigned int i;
22245 /* PCH might result in DW_AT_producer string being restored from the
22246 header compilation, fix it up if needed. */
22247 dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
22248 if (strcmp (AT_string (producer), producer_string) != 0)
22250 struct indirect_string_node *node = find_AT_string (producer_string);
22251 producer->dw_attr_val.v.val_str = node;
22254 gen_scheduled_generic_parms_dies ();
22255 gen_remaining_tmpl_value_param_die_attribute ();
22257 /* Add the name for the main input file now. We delayed this from
22258 dwarf2out_init to avoid complications with PCH. */
22259 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
22260 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
22261 add_comp_dir_attribute (comp_unit_die ());
22262 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
22264 bool p = false;
22265 htab_traverse (file_table, file_table_relative_p, &p);
22266 if (p)
22267 add_comp_dir_attribute (comp_unit_die ());
22270 for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
22272 add_location_or_const_value_attribute (
22273 VEC_index (deferred_locations, deferred_locations_list, i).die,
22274 VEC_index (deferred_locations, deferred_locations_list, i).variable,
22275 false,
22276 DW_AT_location);
22279 /* Traverse the limbo die list, and add parent/child links. The only
22280 dies without parents that should be here are concrete instances of
22281 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
22282 For concrete instances, we can get the parent die from the abstract
22283 instance. */
22284 for (node = limbo_die_list; node; node = next_node)
22286 dw_die_ref die = node->die;
22287 next_node = node->next;
22289 if (die->die_parent == NULL)
22291 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
22293 if (origin && origin->die_parent)
22294 add_child_die (origin->die_parent, die);
22295 else if (is_cu_die (die))
22297 else if (seen_error ())
22298 /* It's OK to be confused by errors in the input. */
22299 add_child_die (comp_unit_die (), die);
22300 else
22302 /* In certain situations, the lexical block containing a
22303 nested function can be optimized away, which results
22304 in the nested function die being orphaned. Likewise
22305 with the return type of that nested function. Force
22306 this to be a child of the containing function.
22308 It may happen that even the containing function got fully
22309 inlined and optimized out. In that case we are lost and
22310 assign the empty child. This should not be big issue as
22311 the function is likely unreachable too. */
22312 gcc_assert (node->created_for);
22314 if (DECL_P (node->created_for))
22315 origin = get_context_die (DECL_CONTEXT (node->created_for));
22316 else if (TYPE_P (node->created_for))
22317 origin = scope_die_for (node->created_for, comp_unit_die ());
22318 else
22319 origin = comp_unit_die ();
22321 add_child_die (origin, die);
22326 limbo_die_list = NULL;
22328 #if ENABLE_ASSERT_CHECKING
22330 dw_die_ref die = comp_unit_die (), c;
22331 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
22333 #endif
22334 resolve_addr (comp_unit_die ());
22335 move_marked_base_types ();
22337 for (node = deferred_asm_name; node; node = node->next)
22339 tree decl = node->created_for;
22340 /* When generating LTO bytecode we can not generate new assembler
22341 names at this point and all important decls got theirs via
22342 free-lang-data. */
22343 if ((!flag_generate_lto || DECL_ASSEMBLER_NAME_SET_P (decl))
22344 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
22346 add_linkage_attr (node->die, decl);
22347 move_linkage_attr (node->die);
22351 deferred_asm_name = NULL;
22353 /* Walk through the list of incomplete types again, trying once more to
22354 emit full debugging info for them. */
22355 retry_incomplete_types ();
22357 if (flag_eliminate_unused_debug_types)
22358 prune_unused_types ();
22360 /* Generate separate COMDAT sections for type DIEs. */
22361 if (use_debug_types)
22363 break_out_comdat_types (comp_unit_die ());
22365 /* Each new type_unit DIE was added to the limbo die list when created.
22366 Since these have all been added to comdat_type_list, clear the
22367 limbo die list. */
22368 limbo_die_list = NULL;
22370 /* For each new comdat type unit, copy declarations for incomplete
22371 types to make the new unit self-contained (i.e., no direct
22372 references to the main compile unit). */
22373 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22374 copy_decls_for_unworthy_types (ctnode->root_die);
22375 copy_decls_for_unworthy_types (comp_unit_die ());
22377 /* In the process of copying declarations from one unit to another,
22378 we may have left some declarations behind that are no longer
22379 referenced. Prune them. */
22380 prune_unused_types ();
22383 /* Generate separate CUs for each of the include files we've seen.
22384 They will go into limbo_die_list. */
22385 if (flag_eliminate_dwarf2_dups)
22386 break_out_includes (comp_unit_die ());
22388 /* Traverse the DIE's and add add sibling attributes to those DIE's
22389 that have children. */
22390 add_sibling_attributes (comp_unit_die ());
22391 for (node = limbo_die_list; node; node = node->next)
22392 add_sibling_attributes (node->die);
22393 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22394 add_sibling_attributes (ctnode->root_die);
22396 /* Output a terminator label for the .text section. */
22397 switch_to_section (text_section);
22398 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
22399 if (cold_text_section)
22401 switch_to_section (cold_text_section);
22402 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
22405 /* We can only use the low/high_pc attributes if all of the code was
22406 in .text. */
22407 if (!have_multiple_function_sections
22408 || (dwarf_version < 3 && dwarf_strict))
22410 /* Don't add if the CU has no associated code. */
22411 if (text_section_used)
22412 add_AT_low_high_pc (comp_unit_die (), text_section_label,
22413 text_end_label);
22415 else
22417 unsigned fde_idx;
22418 dw_fde_ref fde;
22419 bool range_list_added = false;
22421 if (text_section_used)
22422 add_ranges_by_labels (comp_unit_die (), text_section_label,
22423 text_end_label, &range_list_added);
22424 if (cold_text_section_used)
22425 add_ranges_by_labels (comp_unit_die (), cold_text_section_label,
22426 cold_end_label, &range_list_added);
22428 FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, fde_idx, fde)
22430 if (DECL_IGNORED_P (fde->decl))
22431 continue;
22432 if (!fde->in_std_section)
22433 add_ranges_by_labels (comp_unit_die (), fde->dw_fde_begin,
22434 fde->dw_fde_end, &range_list_added);
22435 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
22436 add_ranges_by_labels (comp_unit_die (), fde->dw_fde_second_begin,
22437 fde->dw_fde_second_end, &range_list_added);
22440 if (range_list_added)
22442 /* We need to give .debug_loc and .debug_ranges an appropriate
22443 "base address". Use zero so that these addresses become
22444 absolute. Historically, we've emitted the unexpected
22445 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
22446 Emit both to give time for other tools to adapt. */
22447 add_AT_addr (comp_unit_die (), DW_AT_low_pc, const0_rtx);
22448 if (! dwarf_strict && dwarf_version < 4)
22449 add_AT_addr (comp_unit_die (), DW_AT_entry_pc, const0_rtx);
22451 add_ranges (NULL);
22455 if (debug_info_level >= DINFO_LEVEL_NORMAL)
22456 add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list,
22457 debug_line_section_label);
22459 if (have_macinfo)
22460 add_AT_macptr (comp_unit_die (),
22461 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
22462 macinfo_section_label);
22464 if (have_location_lists)
22465 optimize_location_lists (comp_unit_die ());
22467 /* Output all of the compilation units. We put the main one last so that
22468 the offsets are available to output_pubnames. */
22469 for (node = limbo_die_list; node; node = node->next)
22470 output_comp_unit (node->die, 0);
22472 comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
22473 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22475 void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
22477 /* Don't output duplicate types. */
22478 if (*slot != HTAB_EMPTY_ENTRY)
22479 continue;
22481 /* Add a pointer to the line table for the main compilation unit
22482 so that the debugger can make sense of DW_AT_decl_file
22483 attributes. */
22484 if (debug_info_level >= DINFO_LEVEL_NORMAL)
22485 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
22486 debug_line_section_label);
22488 output_comdat_type_unit (ctnode);
22489 *slot = ctnode;
22491 htab_delete (comdat_type_table);
22493 add_AT_pubnames (comp_unit_die ());
22495 /* Output the main compilation unit if non-empty or if .debug_macinfo
22496 or .debug_macro will be emitted. */
22497 output_comp_unit (comp_unit_die (), have_macinfo);
22499 /* Output the abbreviation table. */
22500 if (abbrev_die_table_in_use != 1)
22502 switch_to_section (debug_abbrev_section);
22503 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
22504 output_abbrev_section ();
22507 /* Output location list section if necessary. */
22508 if (have_location_lists)
22510 /* Output the location lists info. */
22511 switch_to_section (debug_loc_section);
22512 ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
22513 DEBUG_LOC_SECTION_LABEL, 0);
22514 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
22515 output_location_lists (comp_unit_die ());
22518 /* Output public names and types tables if necessary. */
22519 output_pubnames (pubname_table);
22520 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
22521 It shouldn't hurt to emit it always, since pure DWARF2 consumers
22522 simply won't look for the section. */
22523 output_pubnames (pubtype_table);
22525 /* Output the address range information if a CU (.debug_info section)
22526 was emitted. We output an empty table even if we had no functions
22527 to put in it. This because the consumer has no way to tell the
22528 difference between an empty table that we omitted and failure to
22529 generate a table that would have contained data. */
22530 if (info_section_emitted)
22532 unsigned long aranges_length = size_of_aranges ();
22534 switch_to_section (debug_aranges_section);
22535 output_aranges (aranges_length);
22538 /* Output ranges section if necessary. */
22539 if (ranges_table_in_use)
22541 switch_to_section (debug_ranges_section);
22542 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
22543 output_ranges ();
22546 /* Have to end the macro section. */
22547 if (have_macinfo)
22549 switch_to_section (debug_macinfo_section);
22550 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
22551 output_macinfo ();
22552 dw2_asm_output_data (1, 0, "End compilation unit");
22555 /* Output the source line correspondence table. We must do this
22556 even if there is no line information. Otherwise, on an empty
22557 translation unit, we will generate a present, but empty,
22558 .debug_info section. IRIX 6.5 `nm' will then complain when
22559 examining the file. This is done late so that any filenames
22560 used by the debug_info section are marked as 'used'. */
22561 switch_to_section (debug_line_section);
22562 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
22563 if (! DWARF2_ASM_LINE_DEBUG_INFO)
22564 output_line_info ();
22566 /* If we emitted any DW_FORM_strp form attribute, output the string
22567 table too. */
22568 if (debug_str_hash)
22569 htab_traverse (debug_str_hash, output_indirect_string, NULL);
22572 #include "gt-dwarf2out.h"