2012-10-23 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / dwarf2out.c
blobbc5868b6a252c19105585d6860be3e033789e73b
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992, 1993, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
4 Free Software Foundation, Inc.
5 Contributed by Gary Funck (gary@intrepid.com).
6 Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
7 Extensively modified by Jason Merrill (jason@cygnus.com).
9 This file is part of GCC.
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
14 version.
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 for more details.
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3. If not see
23 <http://www.gnu.org/licenses/>. */
25 /* TODO: Emit .debug_line header even when there are no functions, since
26 the file numbers are used by .debug_info. Alternately, leave
27 out locations for types and decls.
28 Avoid talking about ctors and op= for PODs.
29 Factor out common prologue sequences into multiple CIEs. */
31 /* The first part of this file deals with the DWARF 2 frame unwind
32 information, which is also used by the GCC efficient exception handling
33 mechanism. The second part, controlled only by an #ifdef
34 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
35 information. */
37 /* DWARF2 Abbreviation Glossary:
39 CFA = Canonical Frame Address
40 a fixed address on the stack which identifies a call frame.
41 We define it to be the value of SP just before the call insn.
42 The CFA register and offset, which may change during the course
43 of the function, are used to calculate its value at runtime.
45 CFI = Call Frame Instruction
46 an instruction for the DWARF2 abstract machine
48 CIE = Common Information Entry
49 information describing information common to one or more FDEs
51 DIE = Debugging Information Entry
53 FDE = Frame Description Entry
54 information describing the stack call frame, in particular,
55 how to restore registers
57 DW_CFA_... = DWARF2 CFA call frame instruction
58 DW_TAG_... = DWARF2 DIE tag */
60 #include "config.h"
61 #include "system.h"
62 #include "coretypes.h"
63 #include "tm.h"
64 #include "tree.h"
65 #include "version.h"
66 #include "flags.h"
67 #include "rtl.h"
68 #include "hard-reg-set.h"
69 #include "regs.h"
70 #include "insn-config.h"
71 #include "reload.h"
72 #include "function.h"
73 #include "output.h"
74 #include "expr.h"
75 #include "except.h"
76 #include "dwarf2.h"
77 #include "dwarf2out.h"
78 #include "dwarf2asm.h"
79 #include "toplev.h"
80 #include "ggc.h"
81 #include "md5.h"
82 #include "tm_p.h"
83 #include "diagnostic.h"
84 #include "tree-pretty-print.h"
85 #include "debug.h"
86 #include "target.h"
87 #include "common/common-target.h"
88 #include "langhooks.h"
89 #include "hashtab.h"
90 #include "cgraph.h"
91 #include "input.h"
92 #include "gimple.h"
93 #include "ira.h"
94 #include "lra.h"
95 #include "dumpfile.h"
96 #include "opts.h"
98 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
99 static rtx last_var_location_insn;
100 static rtx cached_next_real_insn;
102 #ifdef VMS_DEBUGGING_INFO
103 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
105 /* Define this macro to be a nonzero value if the directory specifications
106 which are output in the debug info should end with a separator. */
107 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
108 /* Define this macro to evaluate to a nonzero value if GCC should refrain
109 from generating indirect strings in DWARF2 debug information, for instance
110 if your target is stuck with an old version of GDB that is unable to
111 process them properly or uses VMS Debug. */
112 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
113 #else
114 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
115 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
116 #endif
118 /* ??? Poison these here until it can be done generically. They've been
119 totally replaced in this file; make sure it stays that way. */
120 #undef DWARF2_UNWIND_INFO
121 #undef DWARF2_FRAME_INFO
122 #if (GCC_VERSION >= 3000)
123 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
124 #endif
126 /* The size of the target's pointer type. */
127 #ifndef PTR_SIZE
128 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
129 #endif
131 /* Array of RTXes referenced by the debugging information, which therefore
132 must be kept around forever. */
133 static GTY(()) VEC(rtx,gc) *used_rtx_array;
135 /* A pointer to the base of a list of incomplete types which might be
136 completed at some later time. incomplete_types_list needs to be a
137 VEC(tree,gc) because we want to tell the garbage collector about
138 it. */
139 static GTY(()) VEC(tree,gc) *incomplete_types;
141 /* A pointer to the base of a table of references to declaration
142 scopes. This table is a display which tracks the nesting
143 of declaration scopes at the current scope and containing
144 scopes. This table is used to find the proper place to
145 define type declaration DIE's. */
146 static GTY(()) VEC(tree,gc) *decl_scope_table;
148 /* Pointers to various DWARF2 sections. */
149 static GTY(()) section *debug_info_section;
150 static GTY(()) section *debug_abbrev_section;
151 static GTY(()) section *debug_aranges_section;
152 static GTY(()) section *debug_macinfo_section;
153 static GTY(()) section *debug_line_section;
154 static GTY(()) section *debug_loc_section;
155 static GTY(()) section *debug_pubnames_section;
156 static GTY(()) section *debug_pubtypes_section;
157 static GTY(()) section *debug_str_section;
158 static GTY(()) section *debug_ranges_section;
159 static GTY(()) section *debug_frame_section;
161 /* Maximum size (in bytes) of an artificially generated label. */
162 #define MAX_ARTIFICIAL_LABEL_BYTES 30
164 /* According to the (draft) DWARF 3 specification, the initial length
165 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
166 bytes are 0xffffffff, followed by the length stored in the next 8
167 bytes.
169 However, the SGI/MIPS ABI uses an initial length which is equal to
170 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
172 #ifndef DWARF_INITIAL_LENGTH_SIZE
173 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
174 #endif
176 /* Round SIZE up to the nearest BOUNDARY. */
177 #define DWARF_ROUND(SIZE,BOUNDARY) \
178 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
180 /* CIE identifier. */
181 #if HOST_BITS_PER_WIDE_INT >= 64
182 #define DWARF_CIE_ID \
183 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
184 #else
185 #define DWARF_CIE_ID DW_CIE_ID
186 #endif
188 DEF_VEC_P (dw_fde_ref);
189 DEF_VEC_ALLOC_P (dw_fde_ref, gc);
191 /* A vector for a table that contains frame description
192 information for each routine. */
193 static GTY(()) VEC(dw_fde_ref, gc) *fde_vec;
195 struct GTY(()) indirect_string_node {
196 const char *str;
197 unsigned int refcount;
198 enum dwarf_form form;
199 char *label;
202 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
204 static GTY(()) int dw2_string_counter;
206 /* True if the compilation unit places functions in more than one section. */
207 static GTY(()) bool have_multiple_function_sections = false;
209 /* Whether the default text and cold text sections have been used at all. */
211 static GTY(()) bool text_section_used = false;
212 static GTY(()) bool cold_text_section_used = false;
214 /* The default cold text section. */
215 static GTY(()) section *cold_text_section;
217 /* Forward declarations for functions defined in this file. */
219 static char *stripattributes (const char *);
220 static void output_call_frame_info (int);
221 static void dwarf2out_note_section_used (void);
223 /* Personality decl of current unit. Used only when assembler does not support
224 personality CFI. */
225 static GTY(()) rtx current_unit_personality;
227 /* Data and reference forms for relocatable data. */
228 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
229 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
231 #ifndef DEBUG_FRAME_SECTION
232 #define DEBUG_FRAME_SECTION ".debug_frame"
233 #endif
235 #ifndef FUNC_BEGIN_LABEL
236 #define FUNC_BEGIN_LABEL "LFB"
237 #endif
239 #ifndef FUNC_END_LABEL
240 #define FUNC_END_LABEL "LFE"
241 #endif
243 #ifndef PROLOGUE_END_LABEL
244 #define PROLOGUE_END_LABEL "LPE"
245 #endif
247 #ifndef EPILOGUE_BEGIN_LABEL
248 #define EPILOGUE_BEGIN_LABEL "LEB"
249 #endif
251 #ifndef FRAME_BEGIN_LABEL
252 #define FRAME_BEGIN_LABEL "Lframe"
253 #endif
254 #define CIE_AFTER_SIZE_LABEL "LSCIE"
255 #define CIE_END_LABEL "LECIE"
256 #define FDE_LABEL "LSFDE"
257 #define FDE_AFTER_SIZE_LABEL "LASFDE"
258 #define FDE_END_LABEL "LEFDE"
259 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
260 #define LINE_NUMBER_END_LABEL "LELT"
261 #define LN_PROLOG_AS_LABEL "LASLTP"
262 #define LN_PROLOG_END_LABEL "LELTP"
263 #define DIE_LABEL_PREFIX "DW"
265 /* Match the base name of a file to the base name of a compilation unit. */
267 static int
268 matches_main_base (const char *path)
270 /* Cache the last query. */
271 static const char *last_path = NULL;
272 static int last_match = 0;
273 if (path != last_path)
275 const char *base;
276 int length = base_of_path (path, &base);
277 last_path = path;
278 last_match = (length == main_input_baselength
279 && memcmp (base, main_input_basename, length) == 0);
281 return last_match;
284 #ifdef DEBUG_DEBUG_STRUCT
286 static int
287 dump_struct_debug (tree type, enum debug_info_usage usage,
288 enum debug_struct_file criterion, int generic,
289 int matches, int result)
291 /* Find the type name. */
292 tree type_decl = TYPE_STUB_DECL (type);
293 tree t = type_decl;
294 const char *name = 0;
295 if (TREE_CODE (t) == TYPE_DECL)
296 t = DECL_NAME (t);
297 if (t)
298 name = IDENTIFIER_POINTER (t);
300 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
301 criterion,
302 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
303 matches ? "bas" : "hdr",
304 generic ? "gen" : "ord",
305 usage == DINFO_USAGE_DFN ? ";" :
306 usage == DINFO_USAGE_DIR_USE ? "." : "*",
307 result,
308 (void*) type_decl, name);
309 return result;
311 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
312 dump_struct_debug (type, usage, criterion, generic, matches, result)
314 #else
316 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
317 (result)
319 #endif
321 static bool
322 should_emit_struct_debug (tree type, enum debug_info_usage usage)
324 enum debug_struct_file criterion;
325 tree type_decl;
326 bool generic = lang_hooks.types.generic_p (type);
328 if (generic)
329 criterion = debug_struct_generic[usage];
330 else
331 criterion = debug_struct_ordinary[usage];
333 if (criterion == DINFO_STRUCT_FILE_NONE)
334 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
335 if (criterion == DINFO_STRUCT_FILE_ANY)
336 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
338 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
340 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
341 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
343 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
344 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
345 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
348 /* Return a pointer to a copy of the section string name S with all
349 attributes stripped off, and an asterisk prepended (for assemble_name). */
351 static inline char *
352 stripattributes (const char *s)
354 char *stripped = XNEWVEC (char, strlen (s) + 2);
355 char *p = stripped;
357 *p++ = '*';
359 while (*s && *s != ',')
360 *p++ = *s++;
362 *p = '\0';
363 return stripped;
366 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
367 switch to the data section instead, and write out a synthetic start label
368 for collect2 the first time around. */
370 static void
371 switch_to_eh_frame_section (bool back)
373 tree label;
375 #ifdef EH_FRAME_SECTION_NAME
376 if (eh_frame_section == 0)
378 int flags;
380 if (EH_TABLES_CAN_BE_READ_ONLY)
382 int fde_encoding;
383 int per_encoding;
384 int lsda_encoding;
386 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
387 /*global=*/0);
388 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
389 /*global=*/1);
390 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
391 /*global=*/0);
392 flags = ((! flag_pic
393 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
394 && (fde_encoding & 0x70) != DW_EH_PE_aligned
395 && (per_encoding & 0x70) != DW_EH_PE_absptr
396 && (per_encoding & 0x70) != DW_EH_PE_aligned
397 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
398 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
399 ? 0 : SECTION_WRITE);
401 else
402 flags = SECTION_WRITE;
403 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
405 #endif /* EH_FRAME_SECTION_NAME */
407 if (eh_frame_section)
408 switch_to_section (eh_frame_section);
409 else
411 /* We have no special eh_frame section. Put the information in
412 the data section and emit special labels to guide collect2. */
413 switch_to_section (data_section);
415 if (!back)
417 label = get_file_function_name ("F");
418 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
419 targetm.asm_out.globalize_label (asm_out_file,
420 IDENTIFIER_POINTER (label));
421 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
426 /* Switch [BACK] to the eh or debug frame table section, depending on
427 FOR_EH. */
429 static void
430 switch_to_frame_table_section (int for_eh, bool back)
432 if (for_eh)
433 switch_to_eh_frame_section (back);
434 else
436 if (!debug_frame_section)
437 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
438 SECTION_DEBUG, NULL);
439 switch_to_section (debug_frame_section);
443 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
445 enum dw_cfi_oprnd_type
446 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
448 switch (cfi)
450 case DW_CFA_nop:
451 case DW_CFA_GNU_window_save:
452 case DW_CFA_remember_state:
453 case DW_CFA_restore_state:
454 return dw_cfi_oprnd_unused;
456 case DW_CFA_set_loc:
457 case DW_CFA_advance_loc1:
458 case DW_CFA_advance_loc2:
459 case DW_CFA_advance_loc4:
460 case DW_CFA_MIPS_advance_loc8:
461 return dw_cfi_oprnd_addr;
463 case DW_CFA_offset:
464 case DW_CFA_offset_extended:
465 case DW_CFA_def_cfa:
466 case DW_CFA_offset_extended_sf:
467 case DW_CFA_def_cfa_sf:
468 case DW_CFA_restore:
469 case DW_CFA_restore_extended:
470 case DW_CFA_undefined:
471 case DW_CFA_same_value:
472 case DW_CFA_def_cfa_register:
473 case DW_CFA_register:
474 case DW_CFA_expression:
475 return dw_cfi_oprnd_reg_num;
477 case DW_CFA_def_cfa_offset:
478 case DW_CFA_GNU_args_size:
479 case DW_CFA_def_cfa_offset_sf:
480 return dw_cfi_oprnd_offset;
482 case DW_CFA_def_cfa_expression:
483 return dw_cfi_oprnd_loc;
485 default:
486 gcc_unreachable ();
490 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
492 enum dw_cfi_oprnd_type
493 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
495 switch (cfi)
497 case DW_CFA_def_cfa:
498 case DW_CFA_def_cfa_sf:
499 case DW_CFA_offset:
500 case DW_CFA_offset_extended_sf:
501 case DW_CFA_offset_extended:
502 return dw_cfi_oprnd_offset;
504 case DW_CFA_register:
505 return dw_cfi_oprnd_reg_num;
507 case DW_CFA_expression:
508 return dw_cfi_oprnd_loc;
510 default:
511 return dw_cfi_oprnd_unused;
515 /* Output one FDE. */
517 static void
518 output_fde (dw_fde_ref fde, bool for_eh, bool second,
519 char *section_start_label, int fde_encoding, char *augmentation,
520 bool any_lsda_needed, int lsda_encoding)
522 const char *begin, *end;
523 static unsigned int j;
524 char l1[20], l2[20];
526 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
527 /* empty */ 0);
528 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
529 for_eh + j);
530 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
531 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
532 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
533 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
534 " indicating 64-bit DWARF extension");
535 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
536 "FDE Length");
537 ASM_OUTPUT_LABEL (asm_out_file, l1);
539 if (for_eh)
540 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
541 else
542 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
543 debug_frame_section, "FDE CIE offset");
545 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
546 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
548 if (for_eh)
550 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
551 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
552 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
553 "FDE initial location");
554 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
555 end, begin, "FDE address range");
557 else
559 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
560 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
563 if (augmentation[0])
565 if (any_lsda_needed)
567 int size = size_of_encoded_value (lsda_encoding);
569 if (lsda_encoding == DW_EH_PE_aligned)
571 int offset = ( 4 /* Length */
572 + 4 /* CIE offset */
573 + 2 * size_of_encoded_value (fde_encoding)
574 + 1 /* Augmentation size */ );
575 int pad = -offset & (PTR_SIZE - 1);
577 size += pad;
578 gcc_assert (size_of_uleb128 (size) == 1);
581 dw2_asm_output_data_uleb128 (size, "Augmentation size");
583 if (fde->uses_eh_lsda)
585 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
586 fde->funcdef_number);
587 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
588 gen_rtx_SYMBOL_REF (Pmode, l1),
589 false,
590 "Language Specific Data Area");
592 else
594 if (lsda_encoding == DW_EH_PE_aligned)
595 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
596 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
597 "Language Specific Data Area (none)");
600 else
601 dw2_asm_output_data_uleb128 (0, "Augmentation size");
604 /* Loop through the Call Frame Instructions associated with this FDE. */
605 fde->dw_fde_current_label = begin;
607 size_t from, until, i;
609 from = 0;
610 until = VEC_length (dw_cfi_ref, fde->dw_fde_cfi);
612 if (fde->dw_fde_second_begin == NULL)
614 else if (!second)
615 until = fde->dw_fde_switch_cfi_index;
616 else
617 from = fde->dw_fde_switch_cfi_index;
619 for (i = from; i < until; i++)
620 output_cfi (VEC_index (dw_cfi_ref, fde->dw_fde_cfi, i), fde, for_eh);
623 /* If we are to emit a ref/link from function bodies to their frame tables,
624 do it now. This is typically performed to make sure that tables
625 associated with functions are dragged with them and not discarded in
626 garbage collecting links. We need to do this on a per function basis to
627 cope with -ffunction-sections. */
629 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
630 /* Switch to the function section, emit the ref to the tables, and
631 switch *back* into the table section. */
632 switch_to_section (function_section (fde->decl));
633 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
634 switch_to_frame_table_section (for_eh, true);
635 #endif
637 /* Pad the FDE out to an address sized boundary. */
638 ASM_OUTPUT_ALIGN (asm_out_file,
639 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
640 ASM_OUTPUT_LABEL (asm_out_file, l2);
642 j += 2;
645 /* Return true if frame description entry FDE is needed for EH. */
647 static bool
648 fde_needed_for_eh_p (dw_fde_ref fde)
650 if (flag_asynchronous_unwind_tables)
651 return true;
653 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
654 return true;
656 if (fde->uses_eh_lsda)
657 return true;
659 /* If exceptions are enabled, we have collected nothrow info. */
660 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
661 return false;
663 return true;
666 /* Output the call frame information used to record information
667 that relates to calculating the frame pointer, and records the
668 location of saved registers. */
670 static void
671 output_call_frame_info (int for_eh)
673 unsigned int i;
674 dw_fde_ref fde;
675 dw_cfi_ref cfi;
676 char l1[20], l2[20], section_start_label[20];
677 bool any_lsda_needed = false;
678 char augmentation[6];
679 int augmentation_size;
680 int fde_encoding = DW_EH_PE_absptr;
681 int per_encoding = DW_EH_PE_absptr;
682 int lsda_encoding = DW_EH_PE_absptr;
683 int return_reg;
684 rtx personality = NULL;
685 int dw_cie_version;
687 /* Don't emit a CIE if there won't be any FDEs. */
688 if (fde_vec == NULL)
689 return;
691 /* Nothing to do if the assembler's doing it all. */
692 if (dwarf2out_do_cfi_asm ())
693 return;
695 /* If we don't have any functions we'll want to unwind out of, don't emit
696 any EH unwind information. If we make FDEs linkonce, we may have to
697 emit an empty label for an FDE that wouldn't otherwise be emitted. We
698 want to avoid having an FDE kept around when the function it refers to
699 is discarded. Example where this matters: a primary function template
700 in C++ requires EH information, an explicit specialization doesn't. */
701 if (for_eh)
703 bool any_eh_needed = false;
705 FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, i, fde)
707 if (fde->uses_eh_lsda)
708 any_eh_needed = any_lsda_needed = true;
709 else if (fde_needed_for_eh_p (fde))
710 any_eh_needed = true;
711 else if (TARGET_USES_WEAK_UNWIND_INFO)
712 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
715 if (!any_eh_needed)
716 return;
719 /* We're going to be generating comments, so turn on app. */
720 if (flag_debug_asm)
721 app_enable ();
723 /* Switch to the proper frame section, first time. */
724 switch_to_frame_table_section (for_eh, false);
726 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
727 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
729 /* Output the CIE. */
730 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
731 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
732 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
733 dw2_asm_output_data (4, 0xffffffff,
734 "Initial length escape value indicating 64-bit DWARF extension");
735 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
736 "Length of Common Information Entry");
737 ASM_OUTPUT_LABEL (asm_out_file, l1);
739 /* Now that the CIE pointer is PC-relative for EH,
740 use 0 to identify the CIE. */
741 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
742 (for_eh ? 0 : DWARF_CIE_ID),
743 "CIE Identifier Tag");
745 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
746 use CIE version 1, unless that would produce incorrect results
747 due to overflowing the return register column. */
748 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
749 dw_cie_version = 1;
750 if (return_reg >= 256 || dwarf_version > 2)
751 dw_cie_version = 3;
752 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
754 augmentation[0] = 0;
755 augmentation_size = 0;
757 personality = current_unit_personality;
758 if (for_eh)
760 char *p;
762 /* Augmentation:
763 z Indicates that a uleb128 is present to size the
764 augmentation section.
765 L Indicates the encoding (and thus presence) of
766 an LSDA pointer in the FDE augmentation.
767 R Indicates a non-default pointer encoding for
768 FDE code pointers.
769 P Indicates the presence of an encoding + language
770 personality routine in the CIE augmentation. */
772 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
773 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
774 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
776 p = augmentation + 1;
777 if (personality)
779 *p++ = 'P';
780 augmentation_size += 1 + size_of_encoded_value (per_encoding);
781 assemble_external_libcall (personality);
783 if (any_lsda_needed)
785 *p++ = 'L';
786 augmentation_size += 1;
788 if (fde_encoding != DW_EH_PE_absptr)
790 *p++ = 'R';
791 augmentation_size += 1;
793 if (p > augmentation + 1)
795 augmentation[0] = 'z';
796 *p = '\0';
799 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
800 if (personality && per_encoding == DW_EH_PE_aligned)
802 int offset = ( 4 /* Length */
803 + 4 /* CIE Id */
804 + 1 /* CIE version */
805 + strlen (augmentation) + 1 /* Augmentation */
806 + size_of_uleb128 (1) /* Code alignment */
807 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
808 + 1 /* RA column */
809 + 1 /* Augmentation size */
810 + 1 /* Personality encoding */ );
811 int pad = -offset & (PTR_SIZE - 1);
813 augmentation_size += pad;
815 /* Augmentations should be small, so there's scarce need to
816 iterate for a solution. Die if we exceed one uleb128 byte. */
817 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
821 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
822 if (dw_cie_version >= 4)
824 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
825 dw2_asm_output_data (1, 0, "CIE Segment Size");
827 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
828 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
829 "CIE Data Alignment Factor");
831 if (dw_cie_version == 1)
832 dw2_asm_output_data (1, return_reg, "CIE RA Column");
833 else
834 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
836 if (augmentation[0])
838 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
839 if (personality)
841 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
842 eh_data_format_name (per_encoding));
843 dw2_asm_output_encoded_addr_rtx (per_encoding,
844 personality,
845 true, NULL);
848 if (any_lsda_needed)
849 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
850 eh_data_format_name (lsda_encoding));
852 if (fde_encoding != DW_EH_PE_absptr)
853 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
854 eh_data_format_name (fde_encoding));
857 FOR_EACH_VEC_ELT (dw_cfi_ref, cie_cfi_vec, i, cfi)
858 output_cfi (cfi, NULL, for_eh);
860 /* Pad the CIE out to an address sized boundary. */
861 ASM_OUTPUT_ALIGN (asm_out_file,
862 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
863 ASM_OUTPUT_LABEL (asm_out_file, l2);
865 /* Loop through all of the FDE's. */
866 FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, i, fde)
868 unsigned int k;
870 /* Don't emit EH unwind info for leaf functions that don't need it. */
871 if (for_eh && !fde_needed_for_eh_p (fde))
872 continue;
874 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
875 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
876 augmentation, any_lsda_needed, lsda_encoding);
879 if (for_eh && targetm.terminate_dw2_eh_frame_info)
880 dw2_asm_output_data (4, 0, "End of Table");
882 /* Turn off app to make assembly quicker. */
883 if (flag_debug_asm)
884 app_disable ();
887 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
889 static void
890 dwarf2out_do_cfi_startproc (bool second)
892 int enc;
893 rtx ref;
894 rtx personality = get_personality_function (current_function_decl);
896 fprintf (asm_out_file, "\t.cfi_startproc\n");
898 if (personality)
900 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
901 ref = personality;
903 /* ??? The GAS support isn't entirely consistent. We have to
904 handle indirect support ourselves, but PC-relative is done
905 in the assembler. Further, the assembler can't handle any
906 of the weirder relocation types. */
907 if (enc & DW_EH_PE_indirect)
908 ref = dw2_force_const_mem (ref, true);
910 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
911 output_addr_const (asm_out_file, ref);
912 fputc ('\n', asm_out_file);
915 if (crtl->uses_eh_lsda)
917 char lab[20];
919 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
920 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
921 current_function_funcdef_no);
922 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
923 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
925 if (enc & DW_EH_PE_indirect)
926 ref = dw2_force_const_mem (ref, true);
928 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
929 output_addr_const (asm_out_file, ref);
930 fputc ('\n', asm_out_file);
934 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
935 this allocation may be done before pass_final. */
937 dw_fde_ref
938 dwarf2out_alloc_current_fde (void)
940 dw_fde_ref fde;
942 fde = ggc_alloc_cleared_dw_fde_node ();
943 fde->decl = current_function_decl;
944 fde->funcdef_number = current_function_funcdef_no;
945 fde->fde_index = VEC_length (dw_fde_ref, fde_vec);
946 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
947 fde->uses_eh_lsda = crtl->uses_eh_lsda;
948 fde->nothrow = crtl->nothrow;
949 fde->drap_reg = INVALID_REGNUM;
950 fde->vdrap_reg = INVALID_REGNUM;
952 /* Record the FDE associated with this function. */
953 cfun->fde = fde;
954 VEC_safe_push (dw_fde_ref, gc, fde_vec, fde);
956 return fde;
959 /* Output a marker (i.e. a label) for the beginning of a function, before
960 the prologue. */
962 void
963 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
964 const char *file ATTRIBUTE_UNUSED)
966 char label[MAX_ARTIFICIAL_LABEL_BYTES];
967 char * dup_label;
968 dw_fde_ref fde;
969 section *fnsec;
970 bool do_frame;
972 current_function_func_begin_label = NULL;
974 do_frame = dwarf2out_do_frame ();
976 /* ??? current_function_func_begin_label is also used by except.c for
977 call-site information. We must emit this label if it might be used. */
978 if (!do_frame
979 && (!flag_exceptions
980 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
981 return;
983 fnsec = function_section (current_function_decl);
984 switch_to_section (fnsec);
985 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
986 current_function_funcdef_no);
987 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
988 current_function_funcdef_no);
989 dup_label = xstrdup (label);
990 current_function_func_begin_label = dup_label;
992 /* We can elide the fde allocation if we're not emitting debug info. */
993 if (!do_frame)
994 return;
996 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
997 emit insns as rtx but bypass the bulk of rest_of_compilation, which
998 would include pass_dwarf2_frame. If we've not created the FDE yet,
999 do so now. */
1000 fde = cfun->fde;
1001 if (fde == NULL)
1002 fde = dwarf2out_alloc_current_fde ();
1004 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1005 fde->dw_fde_begin = dup_label;
1006 fde->dw_fde_current_label = dup_label;
1007 fde->in_std_section = (fnsec == text_section
1008 || (cold_text_section && fnsec == cold_text_section));
1010 /* We only want to output line number information for the genuine dwarf2
1011 prologue case, not the eh frame case. */
1012 #ifdef DWARF2_DEBUGGING_INFO
1013 if (file)
1014 dwarf2out_source_line (line, file, 0, true);
1015 #endif
1017 if (dwarf2out_do_cfi_asm ())
1018 dwarf2out_do_cfi_startproc (false);
1019 else
1021 rtx personality = get_personality_function (current_function_decl);
1022 if (!current_unit_personality)
1023 current_unit_personality = personality;
1025 /* We cannot keep a current personality per function as without CFI
1026 asm, at the point where we emit the CFI data, there is no current
1027 function anymore. */
1028 if (personality && current_unit_personality != personality)
1029 sorry ("multiple EH personalities are supported only with assemblers "
1030 "supporting .cfi_personality directive");
1034 /* Output a marker (i.e. a label) for the end of the generated code
1035 for a function prologue. This gets called *after* the prologue code has
1036 been generated. */
1038 void
1039 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1040 const char *file ATTRIBUTE_UNUSED)
1042 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1044 /* Output a label to mark the endpoint of the code generated for this
1045 function. */
1046 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1047 current_function_funcdef_no);
1048 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1049 current_function_funcdef_no);
1050 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1053 /* Output a marker (i.e. a label) for the beginning of the generated code
1054 for a function epilogue. This gets called *before* the prologue code has
1055 been generated. */
1057 void
1058 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1059 const char *file ATTRIBUTE_UNUSED)
1061 dw_fde_ref fde = cfun->fde;
1062 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1064 if (fde->dw_fde_vms_begin_epilogue)
1065 return;
1067 /* Output a label to mark the endpoint of the code generated for this
1068 function. */
1069 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1070 current_function_funcdef_no);
1071 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1072 current_function_funcdef_no);
1073 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1076 /* Output a marker (i.e. a label) for the absolute end of the generated code
1077 for a function definition. This gets called *after* the epilogue code has
1078 been generated. */
1080 void
1081 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1082 const char *file ATTRIBUTE_UNUSED)
1084 dw_fde_ref fde;
1085 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1087 last_var_location_insn = NULL_RTX;
1088 cached_next_real_insn = NULL_RTX;
1090 if (dwarf2out_do_cfi_asm ())
1091 fprintf (asm_out_file, "\t.cfi_endproc\n");
1093 /* Output a label to mark the endpoint of the code generated for this
1094 function. */
1095 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1096 current_function_funcdef_no);
1097 ASM_OUTPUT_LABEL (asm_out_file, label);
1098 fde = cfun->fde;
1099 gcc_assert (fde != NULL);
1100 if (fde->dw_fde_second_begin == NULL)
1101 fde->dw_fde_end = xstrdup (label);
1104 void
1105 dwarf2out_frame_finish (void)
1107 /* Output call frame information. */
1108 if (targetm.debug_unwind_info () == UI_DWARF2)
1109 output_call_frame_info (0);
1111 /* Output another copy for the unwinder. */
1112 if ((flag_unwind_tables || flag_exceptions)
1113 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1114 output_call_frame_info (1);
1117 /* Note that the current function section is being used for code. */
1119 static void
1120 dwarf2out_note_section_used (void)
1122 section *sec = current_function_section ();
1123 if (sec == text_section)
1124 text_section_used = true;
1125 else if (sec == cold_text_section)
1126 cold_text_section_used = true;
1129 static void var_location_switch_text_section (void);
1130 static void set_cur_line_info_table (section *);
1132 void
1133 dwarf2out_switch_text_section (void)
1135 section *sect;
1136 dw_fde_ref fde = cfun->fde;
1138 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1140 if (!in_cold_section_p)
1142 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1143 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1144 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1146 else
1148 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1149 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1150 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1152 have_multiple_function_sections = true;
1154 /* There is no need to mark used sections when not debugging. */
1155 if (cold_text_section != NULL)
1156 dwarf2out_note_section_used ();
1158 if (dwarf2out_do_cfi_asm ())
1159 fprintf (asm_out_file, "\t.cfi_endproc\n");
1161 /* Now do the real section switch. */
1162 sect = current_function_section ();
1163 switch_to_section (sect);
1165 fde->second_in_std_section
1166 = (sect == text_section
1167 || (cold_text_section && sect == cold_text_section));
1169 if (dwarf2out_do_cfi_asm ())
1170 dwarf2out_do_cfi_startproc (true);
1172 var_location_switch_text_section ();
1174 if (cold_text_section != NULL)
1175 set_cur_line_info_table (sect);
1178 /* And now, the subset of the debugging information support code necessary
1179 for emitting location expressions. */
1181 /* Data about a single source file. */
1182 struct GTY(()) dwarf_file_data {
1183 const char * filename;
1184 int emitted_number;
1187 typedef struct GTY(()) deferred_locations_struct
1189 tree variable;
1190 dw_die_ref die;
1191 } deferred_locations;
1193 DEF_VEC_O(deferred_locations);
1194 DEF_VEC_ALLOC_O(deferred_locations,gc);
1196 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
1198 DEF_VEC_P(dw_die_ref);
1199 DEF_VEC_ALLOC_P(dw_die_ref,heap);
1201 /* Location lists are ranges + location descriptions for that range,
1202 so you can track variables that are in different places over
1203 their entire life. */
1204 typedef struct GTY(()) dw_loc_list_struct {
1205 dw_loc_list_ref dw_loc_next;
1206 const char *begin; /* Label for begin address of range */
1207 const char *end; /* Label for end address of range */
1208 char *ll_symbol; /* Label for beginning of location list.
1209 Only on head of list */
1210 const char *section; /* Section this loclist is relative to */
1211 dw_loc_descr_ref expr;
1212 hashval_t hash;
1213 /* True if all addresses in this and subsequent lists are known to be
1214 resolved. */
1215 bool resolved_addr;
1216 /* True if this list has been replaced by dw_loc_next. */
1217 bool replaced;
1218 bool emitted;
1219 /* True if the range should be emitted even if begin and end
1220 are the same. */
1221 bool force;
1222 } dw_loc_list_node;
1224 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1226 /* Convert a DWARF stack opcode into its string name. */
1228 static const char *
1229 dwarf_stack_op_name (unsigned int op)
1231 const char *name = get_DW_OP_name (op);
1233 if (name != NULL)
1234 return name;
1236 return "OP_<unknown>";
1239 /* Return a pointer to a newly allocated location description. Location
1240 descriptions are simple expression terms that can be strung
1241 together to form more complicated location (address) descriptions. */
1243 static inline dw_loc_descr_ref
1244 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1245 unsigned HOST_WIDE_INT oprnd2)
1247 dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
1249 descr->dw_loc_opc = op;
1250 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1251 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1252 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1253 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1255 return descr;
1258 /* Return a pointer to a newly allocated location description for
1259 REG and OFFSET. */
1261 static inline dw_loc_descr_ref
1262 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1264 if (reg <= 31)
1265 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1266 offset, 0);
1267 else
1268 return new_loc_descr (DW_OP_bregx, reg, offset);
1271 /* Add a location description term to a location description expression. */
1273 static inline void
1274 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1276 dw_loc_descr_ref *d;
1278 /* Find the end of the chain. */
1279 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1282 *d = descr;
1285 /* Compare two location operands for exact equality. */
1287 static bool
1288 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1290 if (a->val_class != b->val_class)
1291 return false;
1292 switch (a->val_class)
1294 case dw_val_class_none:
1295 return true;
1296 case dw_val_class_addr:
1297 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1299 case dw_val_class_offset:
1300 case dw_val_class_unsigned_const:
1301 case dw_val_class_const:
1302 case dw_val_class_range_list:
1303 case dw_val_class_lineptr:
1304 case dw_val_class_macptr:
1305 /* These are all HOST_WIDE_INT, signed or unsigned. */
1306 return a->v.val_unsigned == b->v.val_unsigned;
1308 case dw_val_class_loc:
1309 return a->v.val_loc == b->v.val_loc;
1310 case dw_val_class_loc_list:
1311 return a->v.val_loc_list == b->v.val_loc_list;
1312 case dw_val_class_die_ref:
1313 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1314 case dw_val_class_fde_ref:
1315 return a->v.val_fde_index == b->v.val_fde_index;
1316 case dw_val_class_lbl_id:
1317 case dw_val_class_high_pc:
1318 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1319 case dw_val_class_str:
1320 return a->v.val_str == b->v.val_str;
1321 case dw_val_class_flag:
1322 return a->v.val_flag == b->v.val_flag;
1323 case dw_val_class_file:
1324 return a->v.val_file == b->v.val_file;
1325 case dw_val_class_decl_ref:
1326 return a->v.val_decl_ref == b->v.val_decl_ref;
1328 case dw_val_class_const_double:
1329 return (a->v.val_double.high == b->v.val_double.high
1330 && a->v.val_double.low == b->v.val_double.low);
1332 case dw_val_class_vec:
1334 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1335 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1337 return (a_len == b_len
1338 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1341 case dw_val_class_data8:
1342 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1344 case dw_val_class_vms_delta:
1345 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1346 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1348 gcc_unreachable ();
1351 /* Compare two location atoms for exact equality. */
1353 static bool
1354 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1356 if (a->dw_loc_opc != b->dw_loc_opc)
1357 return false;
1359 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1360 address size, but since we always allocate cleared storage it
1361 should be zero for other types of locations. */
1362 if (a->dtprel != b->dtprel)
1363 return false;
1365 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1366 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1369 /* Compare two complete location expressions for exact equality. */
1371 bool
1372 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1374 while (1)
1376 if (a == b)
1377 return true;
1378 if (a == NULL || b == NULL)
1379 return false;
1380 if (!loc_descr_equal_p_1 (a, b))
1381 return false;
1383 a = a->dw_loc_next;
1384 b = b->dw_loc_next;
1389 /* Add a constant OFFSET to a location expression. */
1391 static void
1392 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1394 dw_loc_descr_ref loc;
1395 HOST_WIDE_INT *p;
1397 gcc_assert (*list_head != NULL);
1399 if (!offset)
1400 return;
1402 /* Find the end of the chain. */
1403 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1406 p = NULL;
1407 if (loc->dw_loc_opc == DW_OP_fbreg
1408 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1409 p = &loc->dw_loc_oprnd1.v.val_int;
1410 else if (loc->dw_loc_opc == DW_OP_bregx)
1411 p = &loc->dw_loc_oprnd2.v.val_int;
1413 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1414 offset. Don't optimize if an signed integer overflow would happen. */
1415 if (p != NULL
1416 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1417 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1418 *p += offset;
1420 else if (offset > 0)
1421 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1423 else
1425 loc->dw_loc_next = int_loc_descriptor (-offset);
1426 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1430 /* Add a constant OFFSET to a location list. */
1432 static void
1433 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1435 dw_loc_list_ref d;
1436 for (d = list_head; d != NULL; d = d->dw_loc_next)
1437 loc_descr_plus_const (&d->expr, offset);
1440 #define DWARF_REF_SIZE \
1441 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1443 static unsigned long int get_base_type_offset (dw_die_ref);
1445 /* Return the size of a location descriptor. */
1447 static unsigned long
1448 size_of_loc_descr (dw_loc_descr_ref loc)
1450 unsigned long size = 1;
1452 switch (loc->dw_loc_opc)
1454 case DW_OP_addr:
1455 size += DWARF2_ADDR_SIZE;
1456 break;
1457 case DW_OP_const1u:
1458 case DW_OP_const1s:
1459 size += 1;
1460 break;
1461 case DW_OP_const2u:
1462 case DW_OP_const2s:
1463 size += 2;
1464 break;
1465 case DW_OP_const4u:
1466 case DW_OP_const4s:
1467 size += 4;
1468 break;
1469 case DW_OP_const8u:
1470 case DW_OP_const8s:
1471 size += 8;
1472 break;
1473 case DW_OP_constu:
1474 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1475 break;
1476 case DW_OP_consts:
1477 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1478 break;
1479 case DW_OP_pick:
1480 size += 1;
1481 break;
1482 case DW_OP_plus_uconst:
1483 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1484 break;
1485 case DW_OP_skip:
1486 case DW_OP_bra:
1487 size += 2;
1488 break;
1489 case DW_OP_breg0:
1490 case DW_OP_breg1:
1491 case DW_OP_breg2:
1492 case DW_OP_breg3:
1493 case DW_OP_breg4:
1494 case DW_OP_breg5:
1495 case DW_OP_breg6:
1496 case DW_OP_breg7:
1497 case DW_OP_breg8:
1498 case DW_OP_breg9:
1499 case DW_OP_breg10:
1500 case DW_OP_breg11:
1501 case DW_OP_breg12:
1502 case DW_OP_breg13:
1503 case DW_OP_breg14:
1504 case DW_OP_breg15:
1505 case DW_OP_breg16:
1506 case DW_OP_breg17:
1507 case DW_OP_breg18:
1508 case DW_OP_breg19:
1509 case DW_OP_breg20:
1510 case DW_OP_breg21:
1511 case DW_OP_breg22:
1512 case DW_OP_breg23:
1513 case DW_OP_breg24:
1514 case DW_OP_breg25:
1515 case DW_OP_breg26:
1516 case DW_OP_breg27:
1517 case DW_OP_breg28:
1518 case DW_OP_breg29:
1519 case DW_OP_breg30:
1520 case DW_OP_breg31:
1521 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1522 break;
1523 case DW_OP_regx:
1524 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1525 break;
1526 case DW_OP_fbreg:
1527 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1528 break;
1529 case DW_OP_bregx:
1530 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1531 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1532 break;
1533 case DW_OP_piece:
1534 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1535 break;
1536 case DW_OP_bit_piece:
1537 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1538 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1539 break;
1540 case DW_OP_deref_size:
1541 case DW_OP_xderef_size:
1542 size += 1;
1543 break;
1544 case DW_OP_call2:
1545 size += 2;
1546 break;
1547 case DW_OP_call4:
1548 size += 4;
1549 break;
1550 case DW_OP_call_ref:
1551 size += DWARF_REF_SIZE;
1552 break;
1553 case DW_OP_implicit_value:
1554 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1555 + loc->dw_loc_oprnd1.v.val_unsigned;
1556 break;
1557 case DW_OP_GNU_implicit_pointer:
1558 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1559 break;
1560 case DW_OP_GNU_entry_value:
1562 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1563 size += size_of_uleb128 (op_size) + op_size;
1564 break;
1566 case DW_OP_GNU_const_type:
1568 unsigned long o
1569 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1570 size += size_of_uleb128 (o) + 1;
1571 switch (loc->dw_loc_oprnd2.val_class)
1573 case dw_val_class_vec:
1574 size += loc->dw_loc_oprnd2.v.val_vec.length
1575 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1576 break;
1577 case dw_val_class_const:
1578 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1579 break;
1580 case dw_val_class_const_double:
1581 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1582 break;
1583 default:
1584 gcc_unreachable ();
1586 break;
1588 case DW_OP_GNU_regval_type:
1590 unsigned long o
1591 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1592 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1593 + size_of_uleb128 (o);
1595 break;
1596 case DW_OP_GNU_deref_type:
1598 unsigned long o
1599 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1600 size += 1 + size_of_uleb128 (o);
1602 break;
1603 case DW_OP_GNU_convert:
1604 case DW_OP_GNU_reinterpret:
1605 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1606 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1607 else
1609 unsigned long o
1610 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1611 size += size_of_uleb128 (o);
1613 break;
1614 case DW_OP_GNU_parameter_ref:
1615 size += 4;
1616 break;
1617 default:
1618 break;
1621 return size;
1624 /* Return the size of a series of location descriptors. */
1626 unsigned long
1627 size_of_locs (dw_loc_descr_ref loc)
1629 dw_loc_descr_ref l;
1630 unsigned long size;
1632 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1633 field, to avoid writing to a PCH file. */
1634 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1636 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1637 break;
1638 size += size_of_loc_descr (l);
1640 if (! l)
1641 return size;
1643 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1645 l->dw_loc_addr = size;
1646 size += size_of_loc_descr (l);
1649 return size;
1652 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1653 static void get_ref_die_offset_label (char *, dw_die_ref);
1654 static unsigned long int get_ref_die_offset (dw_die_ref);
1656 /* Output location description stack opcode's operands (if any).
1657 The for_eh_or_skip parameter controls whether register numbers are
1658 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1659 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1660 info). This should be suppressed for the cases that have not been converted
1661 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1663 static void
1664 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1666 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1667 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1669 switch (loc->dw_loc_opc)
1671 #ifdef DWARF2_DEBUGGING_INFO
1672 case DW_OP_const2u:
1673 case DW_OP_const2s:
1674 dw2_asm_output_data (2, val1->v.val_int, NULL);
1675 break;
1676 case DW_OP_const4u:
1677 if (loc->dtprel)
1679 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1680 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1681 val1->v.val_addr);
1682 fputc ('\n', asm_out_file);
1683 break;
1685 /* FALLTHRU */
1686 case DW_OP_const4s:
1687 dw2_asm_output_data (4, val1->v.val_int, NULL);
1688 break;
1689 case DW_OP_const8u:
1690 if (loc->dtprel)
1692 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1693 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1694 val1->v.val_addr);
1695 fputc ('\n', asm_out_file);
1696 break;
1698 /* FALLTHRU */
1699 case DW_OP_const8s:
1700 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1701 dw2_asm_output_data (8, val1->v.val_int, NULL);
1702 break;
1703 case DW_OP_skip:
1704 case DW_OP_bra:
1706 int offset;
1708 gcc_assert (val1->val_class == dw_val_class_loc);
1709 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1711 dw2_asm_output_data (2, offset, NULL);
1713 break;
1714 case DW_OP_implicit_value:
1715 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1716 switch (val2->val_class)
1718 case dw_val_class_const:
1719 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1720 break;
1721 case dw_val_class_vec:
1723 unsigned int elt_size = val2->v.val_vec.elt_size;
1724 unsigned int len = val2->v.val_vec.length;
1725 unsigned int i;
1726 unsigned char *p;
1728 if (elt_size > sizeof (HOST_WIDE_INT))
1730 elt_size /= 2;
1731 len *= 2;
1733 for (i = 0, p = val2->v.val_vec.array;
1734 i < len;
1735 i++, p += elt_size)
1736 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1737 "fp or vector constant word %u", i);
1739 break;
1740 case dw_val_class_const_double:
1742 unsigned HOST_WIDE_INT first, second;
1744 if (WORDS_BIG_ENDIAN)
1746 first = val2->v.val_double.high;
1747 second = val2->v.val_double.low;
1749 else
1751 first = val2->v.val_double.low;
1752 second = val2->v.val_double.high;
1754 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1755 first, NULL);
1756 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1757 second, NULL);
1759 break;
1760 case dw_val_class_addr:
1761 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1762 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1763 break;
1764 default:
1765 gcc_unreachable ();
1767 break;
1768 #else
1769 case DW_OP_const2u:
1770 case DW_OP_const2s:
1771 case DW_OP_const4u:
1772 case DW_OP_const4s:
1773 case DW_OP_const8u:
1774 case DW_OP_const8s:
1775 case DW_OP_skip:
1776 case DW_OP_bra:
1777 case DW_OP_implicit_value:
1778 /* We currently don't make any attempt to make sure these are
1779 aligned properly like we do for the main unwind info, so
1780 don't support emitting things larger than a byte if we're
1781 only doing unwinding. */
1782 gcc_unreachable ();
1783 #endif
1784 case DW_OP_const1u:
1785 case DW_OP_const1s:
1786 dw2_asm_output_data (1, val1->v.val_int, NULL);
1787 break;
1788 case DW_OP_constu:
1789 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1790 break;
1791 case DW_OP_consts:
1792 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1793 break;
1794 case DW_OP_pick:
1795 dw2_asm_output_data (1, val1->v.val_int, NULL);
1796 break;
1797 case DW_OP_plus_uconst:
1798 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1799 break;
1800 case DW_OP_breg0:
1801 case DW_OP_breg1:
1802 case DW_OP_breg2:
1803 case DW_OP_breg3:
1804 case DW_OP_breg4:
1805 case DW_OP_breg5:
1806 case DW_OP_breg6:
1807 case DW_OP_breg7:
1808 case DW_OP_breg8:
1809 case DW_OP_breg9:
1810 case DW_OP_breg10:
1811 case DW_OP_breg11:
1812 case DW_OP_breg12:
1813 case DW_OP_breg13:
1814 case DW_OP_breg14:
1815 case DW_OP_breg15:
1816 case DW_OP_breg16:
1817 case DW_OP_breg17:
1818 case DW_OP_breg18:
1819 case DW_OP_breg19:
1820 case DW_OP_breg20:
1821 case DW_OP_breg21:
1822 case DW_OP_breg22:
1823 case DW_OP_breg23:
1824 case DW_OP_breg24:
1825 case DW_OP_breg25:
1826 case DW_OP_breg26:
1827 case DW_OP_breg27:
1828 case DW_OP_breg28:
1829 case DW_OP_breg29:
1830 case DW_OP_breg30:
1831 case DW_OP_breg31:
1832 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1833 break;
1834 case DW_OP_regx:
1836 unsigned r = val1->v.val_unsigned;
1837 if (for_eh_or_skip >= 0)
1838 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1839 gcc_assert (size_of_uleb128 (r)
1840 == size_of_uleb128 (val1->v.val_unsigned));
1841 dw2_asm_output_data_uleb128 (r, NULL);
1843 break;
1844 case DW_OP_fbreg:
1845 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1846 break;
1847 case DW_OP_bregx:
1849 unsigned r = val1->v.val_unsigned;
1850 if (for_eh_or_skip >= 0)
1851 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1852 gcc_assert (size_of_uleb128 (r)
1853 == size_of_uleb128 (val1->v.val_unsigned));
1854 dw2_asm_output_data_uleb128 (r, NULL);
1855 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1857 break;
1858 case DW_OP_piece:
1859 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1860 break;
1861 case DW_OP_bit_piece:
1862 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1863 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1864 break;
1865 case DW_OP_deref_size:
1866 case DW_OP_xderef_size:
1867 dw2_asm_output_data (1, val1->v.val_int, NULL);
1868 break;
1870 case DW_OP_addr:
1871 if (loc->dtprel)
1873 if (targetm.asm_out.output_dwarf_dtprel)
1875 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
1876 DWARF2_ADDR_SIZE,
1877 val1->v.val_addr);
1878 fputc ('\n', asm_out_file);
1880 else
1881 gcc_unreachable ();
1883 else
1885 #ifdef DWARF2_DEBUGGING_INFO
1886 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
1887 #else
1888 gcc_unreachable ();
1889 #endif
1891 break;
1893 case DW_OP_GNU_implicit_pointer:
1895 char label[MAX_ARTIFICIAL_LABEL_BYTES
1896 + HOST_BITS_PER_WIDE_INT / 2 + 2];
1897 gcc_assert (val1->val_class == dw_val_class_die_ref);
1898 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
1899 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
1900 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1902 break;
1904 case DW_OP_GNU_entry_value:
1905 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
1906 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
1907 break;
1909 case DW_OP_GNU_const_type:
1911 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
1912 gcc_assert (o);
1913 dw2_asm_output_data_uleb128 (o, NULL);
1914 switch (val2->val_class)
1916 case dw_val_class_const:
1917 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
1918 dw2_asm_output_data (1, l, NULL);
1919 dw2_asm_output_data (l, val2->v.val_int, NULL);
1920 break;
1921 case dw_val_class_vec:
1923 unsigned int elt_size = val2->v.val_vec.elt_size;
1924 unsigned int len = val2->v.val_vec.length;
1925 unsigned int i;
1926 unsigned char *p;
1928 l = len * elt_size;
1929 dw2_asm_output_data (1, l, NULL);
1930 if (elt_size > sizeof (HOST_WIDE_INT))
1932 elt_size /= 2;
1933 len *= 2;
1935 for (i = 0, p = val2->v.val_vec.array;
1936 i < len;
1937 i++, p += elt_size)
1938 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1939 "fp or vector constant word %u", i);
1941 break;
1942 case dw_val_class_const_double:
1944 unsigned HOST_WIDE_INT first, second;
1945 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
1947 dw2_asm_output_data (1, 2 * l, NULL);
1948 if (WORDS_BIG_ENDIAN)
1950 first = val2->v.val_double.high;
1951 second = val2->v.val_double.low;
1953 else
1955 first = val2->v.val_double.low;
1956 second = val2->v.val_double.high;
1958 dw2_asm_output_data (l, first, NULL);
1959 dw2_asm_output_data (l, second, NULL);
1961 break;
1962 default:
1963 gcc_unreachable ();
1966 break;
1967 case DW_OP_GNU_regval_type:
1969 unsigned r = val1->v.val_unsigned;
1970 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
1971 gcc_assert (o);
1972 if (for_eh_or_skip >= 0)
1974 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1975 gcc_assert (size_of_uleb128 (r)
1976 == size_of_uleb128 (val1->v.val_unsigned));
1978 dw2_asm_output_data_uleb128 (r, NULL);
1979 dw2_asm_output_data_uleb128 (o, NULL);
1981 break;
1982 case DW_OP_GNU_deref_type:
1984 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
1985 gcc_assert (o);
1986 dw2_asm_output_data (1, val1->v.val_int, NULL);
1987 dw2_asm_output_data_uleb128 (o, NULL);
1989 break;
1990 case DW_OP_GNU_convert:
1991 case DW_OP_GNU_reinterpret:
1992 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1993 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1994 else
1996 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
1997 gcc_assert (o);
1998 dw2_asm_output_data_uleb128 (o, NULL);
2000 break;
2002 case DW_OP_GNU_parameter_ref:
2004 unsigned long o;
2005 gcc_assert (val1->val_class == dw_val_class_die_ref);
2006 o = get_ref_die_offset (val1->v.val_die_ref.die);
2007 dw2_asm_output_data (4, o, NULL);
2009 break;
2011 default:
2012 /* Other codes have no operands. */
2013 break;
2017 /* Output a sequence of location operations.
2018 The for_eh_or_skip parameter controls whether register numbers are
2019 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2020 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2021 info). This should be suppressed for the cases that have not been converted
2022 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2024 void
2025 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2027 for (; loc != NULL; loc = loc->dw_loc_next)
2029 enum dwarf_location_atom opc = loc->dw_loc_opc;
2030 /* Output the opcode. */
2031 if (for_eh_or_skip >= 0
2032 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2034 unsigned r = (opc - DW_OP_breg0);
2035 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2036 gcc_assert (r <= 31);
2037 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2039 else if (for_eh_or_skip >= 0
2040 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2042 unsigned r = (opc - DW_OP_reg0);
2043 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2044 gcc_assert (r <= 31);
2045 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2048 dw2_asm_output_data (1, opc,
2049 "%s", dwarf_stack_op_name (opc));
2051 /* Output the operand(s) (if any). */
2052 output_loc_operands (loc, for_eh_or_skip);
2056 /* Output location description stack opcode's operands (if any).
2057 The output is single bytes on a line, suitable for .cfi_escape. */
2059 static void
2060 output_loc_operands_raw (dw_loc_descr_ref loc)
2062 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2063 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2065 switch (loc->dw_loc_opc)
2067 case DW_OP_addr:
2068 case DW_OP_implicit_value:
2069 /* We cannot output addresses in .cfi_escape, only bytes. */
2070 gcc_unreachable ();
2072 case DW_OP_const1u:
2073 case DW_OP_const1s:
2074 case DW_OP_pick:
2075 case DW_OP_deref_size:
2076 case DW_OP_xderef_size:
2077 fputc (',', asm_out_file);
2078 dw2_asm_output_data_raw (1, val1->v.val_int);
2079 break;
2081 case DW_OP_const2u:
2082 case DW_OP_const2s:
2083 fputc (',', asm_out_file);
2084 dw2_asm_output_data_raw (2, val1->v.val_int);
2085 break;
2087 case DW_OP_const4u:
2088 case DW_OP_const4s:
2089 fputc (',', asm_out_file);
2090 dw2_asm_output_data_raw (4, val1->v.val_int);
2091 break;
2093 case DW_OP_const8u:
2094 case DW_OP_const8s:
2095 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2096 fputc (',', asm_out_file);
2097 dw2_asm_output_data_raw (8, val1->v.val_int);
2098 break;
2100 case DW_OP_skip:
2101 case DW_OP_bra:
2103 int offset;
2105 gcc_assert (val1->val_class == dw_val_class_loc);
2106 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2108 fputc (',', asm_out_file);
2109 dw2_asm_output_data_raw (2, offset);
2111 break;
2113 case DW_OP_regx:
2115 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2116 gcc_assert (size_of_uleb128 (r)
2117 == size_of_uleb128 (val1->v.val_unsigned));
2118 fputc (',', asm_out_file);
2119 dw2_asm_output_data_uleb128_raw (r);
2121 break;
2123 case DW_OP_constu:
2124 case DW_OP_plus_uconst:
2125 case DW_OP_piece:
2126 fputc (',', asm_out_file);
2127 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2128 break;
2130 case DW_OP_bit_piece:
2131 fputc (',', asm_out_file);
2132 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2133 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2134 break;
2136 case DW_OP_consts:
2137 case DW_OP_breg0:
2138 case DW_OP_breg1:
2139 case DW_OP_breg2:
2140 case DW_OP_breg3:
2141 case DW_OP_breg4:
2142 case DW_OP_breg5:
2143 case DW_OP_breg6:
2144 case DW_OP_breg7:
2145 case DW_OP_breg8:
2146 case DW_OP_breg9:
2147 case DW_OP_breg10:
2148 case DW_OP_breg11:
2149 case DW_OP_breg12:
2150 case DW_OP_breg13:
2151 case DW_OP_breg14:
2152 case DW_OP_breg15:
2153 case DW_OP_breg16:
2154 case DW_OP_breg17:
2155 case DW_OP_breg18:
2156 case DW_OP_breg19:
2157 case DW_OP_breg20:
2158 case DW_OP_breg21:
2159 case DW_OP_breg22:
2160 case DW_OP_breg23:
2161 case DW_OP_breg24:
2162 case DW_OP_breg25:
2163 case DW_OP_breg26:
2164 case DW_OP_breg27:
2165 case DW_OP_breg28:
2166 case DW_OP_breg29:
2167 case DW_OP_breg30:
2168 case DW_OP_breg31:
2169 case DW_OP_fbreg:
2170 fputc (',', asm_out_file);
2171 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2172 break;
2174 case DW_OP_bregx:
2176 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2177 gcc_assert (size_of_uleb128 (r)
2178 == size_of_uleb128 (val1->v.val_unsigned));
2179 fputc (',', asm_out_file);
2180 dw2_asm_output_data_uleb128_raw (r);
2181 fputc (',', asm_out_file);
2182 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2184 break;
2186 case DW_OP_GNU_implicit_pointer:
2187 case DW_OP_GNU_entry_value:
2188 case DW_OP_GNU_const_type:
2189 case DW_OP_GNU_regval_type:
2190 case DW_OP_GNU_deref_type:
2191 case DW_OP_GNU_convert:
2192 case DW_OP_GNU_reinterpret:
2193 case DW_OP_GNU_parameter_ref:
2194 gcc_unreachable ();
2195 break;
2197 default:
2198 /* Other codes have no operands. */
2199 break;
2203 void
2204 output_loc_sequence_raw (dw_loc_descr_ref loc)
2206 while (1)
2208 enum dwarf_location_atom opc = loc->dw_loc_opc;
2209 /* Output the opcode. */
2210 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2212 unsigned r = (opc - DW_OP_breg0);
2213 r = DWARF2_FRAME_REG_OUT (r, 1);
2214 gcc_assert (r <= 31);
2215 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2217 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2219 unsigned r = (opc - DW_OP_reg0);
2220 r = DWARF2_FRAME_REG_OUT (r, 1);
2221 gcc_assert (r <= 31);
2222 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2224 /* Output the opcode. */
2225 fprintf (asm_out_file, "%#x", opc);
2226 output_loc_operands_raw (loc);
2228 if (!loc->dw_loc_next)
2229 break;
2230 loc = loc->dw_loc_next;
2232 fputc (',', asm_out_file);
2236 /* This function builds a dwarf location descriptor sequence from a
2237 dw_cfa_location, adding the given OFFSET to the result of the
2238 expression. */
2240 struct dw_loc_descr_struct *
2241 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2243 struct dw_loc_descr_struct *head, *tmp;
2245 offset += cfa->offset;
2247 if (cfa->indirect)
2249 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2250 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2251 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2252 add_loc_descr (&head, tmp);
2253 if (offset != 0)
2255 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2256 add_loc_descr (&head, tmp);
2259 else
2260 head = new_reg_loc_descr (cfa->reg, offset);
2262 return head;
2265 /* This function builds a dwarf location descriptor sequence for
2266 the address at OFFSET from the CFA when stack is aligned to
2267 ALIGNMENT byte. */
2269 struct dw_loc_descr_struct *
2270 build_cfa_aligned_loc (dw_cfa_location *cfa,
2271 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2273 struct dw_loc_descr_struct *head;
2274 unsigned int dwarf_fp
2275 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2277 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2278 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2280 head = new_reg_loc_descr (dwarf_fp, 0);
2281 add_loc_descr (&head, int_loc_descriptor (alignment));
2282 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2283 loc_descr_plus_const (&head, offset);
2285 else
2286 head = new_reg_loc_descr (dwarf_fp, offset);
2287 return head;
2290 /* And now, the support for symbolic debugging information. */
2292 /* .debug_str support. */
2293 static int output_indirect_string (void **, void *);
2295 static void dwarf2out_init (const char *);
2296 static void dwarf2out_finish (const char *);
2297 static void dwarf2out_assembly_start (void);
2298 static void dwarf2out_define (unsigned int, const char *);
2299 static void dwarf2out_undef (unsigned int, const char *);
2300 static void dwarf2out_start_source_file (unsigned, const char *);
2301 static void dwarf2out_end_source_file (unsigned);
2302 static void dwarf2out_function_decl (tree);
2303 static void dwarf2out_begin_block (unsigned, unsigned);
2304 static void dwarf2out_end_block (unsigned, unsigned);
2305 static bool dwarf2out_ignore_block (const_tree);
2306 static void dwarf2out_global_decl (tree);
2307 static void dwarf2out_type_decl (tree, int);
2308 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2309 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2310 dw_die_ref);
2311 static void dwarf2out_abstract_function (tree);
2312 static void dwarf2out_var_location (rtx);
2313 static void dwarf2out_begin_function (tree);
2314 static void dwarf2out_set_name (tree, tree);
2316 /* The debug hooks structure. */
2318 const struct gcc_debug_hooks dwarf2_debug_hooks =
2320 dwarf2out_init,
2321 dwarf2out_finish,
2322 dwarf2out_assembly_start,
2323 dwarf2out_define,
2324 dwarf2out_undef,
2325 dwarf2out_start_source_file,
2326 dwarf2out_end_source_file,
2327 dwarf2out_begin_block,
2328 dwarf2out_end_block,
2329 dwarf2out_ignore_block,
2330 dwarf2out_source_line,
2331 dwarf2out_begin_prologue,
2332 #if VMS_DEBUGGING_INFO
2333 dwarf2out_vms_end_prologue,
2334 dwarf2out_vms_begin_epilogue,
2335 #else
2336 debug_nothing_int_charstar,
2337 debug_nothing_int_charstar,
2338 #endif
2339 dwarf2out_end_epilogue,
2340 dwarf2out_begin_function,
2341 debug_nothing_int, /* end_function */
2342 dwarf2out_function_decl, /* function_decl */
2343 dwarf2out_global_decl,
2344 dwarf2out_type_decl, /* type_decl */
2345 dwarf2out_imported_module_or_decl,
2346 debug_nothing_tree, /* deferred_inline_function */
2347 /* The DWARF 2 backend tries to reduce debugging bloat by not
2348 emitting the abstract description of inline functions until
2349 something tries to reference them. */
2350 dwarf2out_abstract_function, /* outlining_inline_function */
2351 debug_nothing_rtx, /* label */
2352 debug_nothing_int, /* handle_pch */
2353 dwarf2out_var_location,
2354 dwarf2out_switch_text_section,
2355 dwarf2out_set_name,
2356 1, /* start_end_main_source_file */
2357 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2360 /* NOTE: In the comments in this file, many references are made to
2361 "Debugging Information Entries". This term is abbreviated as `DIE'
2362 throughout the remainder of this file. */
2364 /* An internal representation of the DWARF output is built, and then
2365 walked to generate the DWARF debugging info. The walk of the internal
2366 representation is done after the entire program has been compiled.
2367 The types below are used to describe the internal representation. */
2369 /* Whether to put type DIEs into their own section .debug_types instead
2370 of making them part of the .debug_info section. Only supported for
2371 Dwarf V4 or higher and the user didn't disable them through
2372 -fno-debug-types-section. It is more efficient to put them in a
2373 separate comdat sections since the linker will then be able to
2374 remove duplicates. But not all tools support .debug_types sections
2375 yet. */
2377 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2379 /* Various DIE's use offsets relative to the beginning of the
2380 .debug_info section to refer to each other. */
2382 typedef long int dw_offset;
2384 /* Define typedefs here to avoid circular dependencies. */
2386 typedef struct dw_attr_struct *dw_attr_ref;
2387 typedef struct dw_line_info_struct *dw_line_info_ref;
2388 typedef struct pubname_struct *pubname_ref;
2389 typedef struct dw_ranges_struct *dw_ranges_ref;
2390 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
2391 typedef struct comdat_type_struct *comdat_type_node_ref;
2393 /* The entries in the line_info table more-or-less mirror the opcodes
2394 that are used in the real dwarf line table. Arrays of these entries
2395 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2396 supported. */
2398 enum dw_line_info_opcode {
2399 /* Emit DW_LNE_set_address; the operand is the label index. */
2400 LI_set_address,
2402 /* Emit a row to the matrix with the given line. This may be done
2403 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2404 special opcodes. */
2405 LI_set_line,
2407 /* Emit a DW_LNS_set_file. */
2408 LI_set_file,
2410 /* Emit a DW_LNS_set_column. */
2411 LI_set_column,
2413 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2414 LI_negate_stmt,
2416 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2417 LI_set_prologue_end,
2418 LI_set_epilogue_begin,
2420 /* Emit a DW_LNE_set_discriminator. */
2421 LI_set_discriminator
2424 typedef struct GTY(()) dw_line_info_struct {
2425 enum dw_line_info_opcode opcode;
2426 unsigned int val;
2427 } dw_line_info_entry;
2429 DEF_VEC_O(dw_line_info_entry);
2430 DEF_VEC_ALLOC_O(dw_line_info_entry, gc);
2432 typedef struct GTY(()) dw_line_info_table_struct {
2433 /* The label that marks the end of this section. */
2434 const char *end_label;
2436 /* The values for the last row of the matrix, as collected in the table.
2437 These are used to minimize the changes to the next row. */
2438 unsigned int file_num;
2439 unsigned int line_num;
2440 unsigned int column_num;
2441 int discrim_num;
2442 bool is_stmt;
2443 bool in_use;
2445 VEC(dw_line_info_entry, gc) *entries;
2446 } dw_line_info_table;
2448 typedef dw_line_info_table *dw_line_info_table_p;
2450 DEF_VEC_P(dw_line_info_table_p);
2451 DEF_VEC_ALLOC_P(dw_line_info_table_p, gc);
2453 /* Each DIE attribute has a field specifying the attribute kind,
2454 a link to the next attribute in the chain, and an attribute value.
2455 Attributes are typically linked below the DIE they modify. */
2457 typedef struct GTY(()) dw_attr_struct {
2458 enum dwarf_attribute dw_attr;
2459 dw_val_node dw_attr_val;
2461 dw_attr_node;
2463 DEF_VEC_O(dw_attr_node);
2464 DEF_VEC_ALLOC_O(dw_attr_node,gc);
2466 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2467 The children of each node form a circular list linked by
2468 die_sib. die_child points to the node *before* the "first" child node. */
2470 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
2471 union die_symbol_or_type_node
2473 const char * GTY ((tag ("0"))) die_symbol;
2474 comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
2476 GTY ((desc ("%0.comdat_type_p"))) die_id;
2477 VEC(dw_attr_node,gc) * die_attr;
2478 dw_die_ref die_parent;
2479 dw_die_ref die_child;
2480 dw_die_ref die_sib;
2481 dw_die_ref die_definition; /* ref from a specification to its definition */
2482 dw_offset die_offset;
2483 unsigned long die_abbrev;
2484 int die_mark;
2485 unsigned int decl_id;
2486 enum dwarf_tag die_tag;
2487 /* Die is used and must not be pruned as unused. */
2488 BOOL_BITFIELD die_perennial_p : 1;
2489 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2490 /* Lots of spare bits. */
2492 die_node;
2494 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2495 #define FOR_EACH_CHILD(die, c, expr) do { \
2496 c = die->die_child; \
2497 if (c) do { \
2498 c = c->die_sib; \
2499 expr; \
2500 } while (c != die->die_child); \
2501 } while (0)
2503 /* The pubname structure */
2505 typedef struct GTY(()) pubname_struct {
2506 dw_die_ref die;
2507 const char *name;
2509 pubname_entry;
2511 DEF_VEC_O(pubname_entry);
2512 DEF_VEC_ALLOC_O(pubname_entry, gc);
2514 struct GTY(()) dw_ranges_struct {
2515 /* If this is positive, it's a block number, otherwise it's a
2516 bitwise-negated index into dw_ranges_by_label. */
2517 int num;
2520 /* A structure to hold a macinfo entry. */
2522 typedef struct GTY(()) macinfo_struct {
2523 unsigned char code;
2524 unsigned HOST_WIDE_INT lineno;
2525 const char *info;
2527 macinfo_entry;
2529 DEF_VEC_O(macinfo_entry);
2530 DEF_VEC_ALLOC_O(macinfo_entry, gc);
2532 struct GTY(()) dw_ranges_by_label_struct {
2533 const char *begin;
2534 const char *end;
2537 /* The comdat type node structure. */
2538 typedef struct GTY(()) comdat_type_struct
2540 dw_die_ref root_die;
2541 dw_die_ref type_die;
2542 dw_die_ref skeleton_die;
2543 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2544 struct comdat_type_struct *next;
2546 comdat_type_node;
2548 /* The limbo die list structure. */
2549 typedef struct GTY(()) limbo_die_struct {
2550 dw_die_ref die;
2551 tree created_for;
2552 struct limbo_die_struct *next;
2554 limbo_die_node;
2556 typedef struct skeleton_chain_struct
2558 dw_die_ref old_die;
2559 dw_die_ref new_die;
2560 struct skeleton_chain_struct *parent;
2562 skeleton_chain_node;
2564 /* Define a macro which returns nonzero for a TYPE_DECL which was
2565 implicitly generated for a type.
2567 Note that, unlike the C front-end (which generates a NULL named
2568 TYPE_DECL node for each complete tagged type, each array type,
2569 and each function type node created) the C++ front-end generates
2570 a _named_ TYPE_DECL node for each tagged type node created.
2571 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2572 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2573 front-end, but for each type, tagged or not. */
2575 #define TYPE_DECL_IS_STUB(decl) \
2576 (DECL_NAME (decl) == NULL_TREE \
2577 || (DECL_ARTIFICIAL (decl) \
2578 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
2579 /* This is necessary for stub decls that \
2580 appear in nested inline functions. */ \
2581 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2582 && (decl_ultimate_origin (decl) \
2583 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2585 /* Information concerning the compilation unit's programming
2586 language, and compiler version. */
2588 /* Fixed size portion of the DWARF compilation unit header. */
2589 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2590 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2592 /* Fixed size portion of the DWARF comdat type unit header. */
2593 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2594 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2595 + DWARF_OFFSET_SIZE)
2597 /* Fixed size portion of public names info. */
2598 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2600 /* Fixed size portion of the address range info. */
2601 #define DWARF_ARANGES_HEADER_SIZE \
2602 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2603 DWARF2_ADDR_SIZE * 2) \
2604 - DWARF_INITIAL_LENGTH_SIZE)
2606 /* Size of padding portion in the address range info. It must be
2607 aligned to twice the pointer size. */
2608 #define DWARF_ARANGES_PAD_SIZE \
2609 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2610 DWARF2_ADDR_SIZE * 2) \
2611 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2613 /* Use assembler line directives if available. */
2614 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2615 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2616 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2617 #else
2618 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2619 #endif
2620 #endif
2622 /* Minimum line offset in a special line info. opcode.
2623 This value was chosen to give a reasonable range of values. */
2624 #define DWARF_LINE_BASE -10
2626 /* First special line opcode - leave room for the standard opcodes. */
2627 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
2629 /* Range of line offsets in a special line info. opcode. */
2630 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
2632 /* Flag that indicates the initial value of the is_stmt_start flag.
2633 In the present implementation, we do not mark any lines as
2634 the beginning of a source statement, because that information
2635 is not made available by the GCC front-end. */
2636 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2638 /* Maximum number of operations per instruction bundle. */
2639 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2640 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2641 #endif
2643 /* This location is used by calc_die_sizes() to keep track
2644 the offset of each DIE within the .debug_info section. */
2645 static unsigned long next_die_offset;
2647 /* Record the root of the DIE's built for the current compilation unit. */
2648 static GTY(()) dw_die_ref single_comp_unit_die;
2650 /* A list of type DIEs that have been separated into comdat sections. */
2651 static GTY(()) comdat_type_node *comdat_type_list;
2653 /* A list of DIEs with a NULL parent waiting to be relocated. */
2654 static GTY(()) limbo_die_node *limbo_die_list;
2656 /* A list of DIEs for which we may have to generate
2657 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
2658 static GTY(()) limbo_die_node *deferred_asm_name;
2660 /* Filenames referenced by this compilation unit. */
2661 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
2663 /* A hash table of references to DIE's that describe declarations.
2664 The key is a DECL_UID() which is a unique number identifying each decl. */
2665 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
2667 /* A hash table of references to DIE's that describe COMMON blocks.
2668 The key is DECL_UID() ^ die_parent. */
2669 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
2671 typedef struct GTY(()) die_arg_entry_struct {
2672 dw_die_ref die;
2673 tree arg;
2674 } die_arg_entry;
2676 DEF_VEC_O(die_arg_entry);
2677 DEF_VEC_ALLOC_O(die_arg_entry,gc);
2679 /* Node of the variable location list. */
2680 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2681 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2682 EXPR_LIST chain. For small bitsizes, bitsize is encoded
2683 in mode of the EXPR_LIST node and first EXPR_LIST operand
2684 is either NOTE_INSN_VAR_LOCATION for a piece with a known
2685 location or NULL for padding. For larger bitsizes,
2686 mode is 0 and first operand is a CONCAT with bitsize
2687 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2688 NULL as second operand. */
2689 rtx GTY (()) loc;
2690 const char * GTY (()) label;
2691 struct var_loc_node * GTY (()) next;
2694 /* Variable location list. */
2695 struct GTY (()) var_loc_list_def {
2696 struct var_loc_node * GTY (()) first;
2698 /* Pointer to the last but one or last element of the
2699 chained list. If the list is empty, both first and
2700 last are NULL, if the list contains just one node
2701 or the last node certainly is not redundant, it points
2702 to the last node, otherwise points to the last but one.
2703 Do not mark it for GC because it is marked through the chain. */
2704 struct var_loc_node * GTY ((skip ("%h"))) last;
2706 /* Pointer to the last element before section switch,
2707 if NULL, either sections weren't switched or first
2708 is after section switch. */
2709 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2711 /* DECL_UID of the variable decl. */
2712 unsigned int decl_id;
2714 typedef struct var_loc_list_def var_loc_list;
2716 /* Call argument location list. */
2717 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2718 rtx GTY (()) call_arg_loc_note;
2719 const char * GTY (()) label;
2720 tree GTY (()) block;
2721 bool tail_call_p;
2722 rtx GTY (()) symbol_ref;
2723 struct call_arg_loc_node * GTY (()) next;
2727 /* Table of decl location linked lists. */
2728 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
2730 /* Head and tail of call_arg_loc chain. */
2731 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2732 static struct call_arg_loc_node *call_arg_loc_last;
2734 /* Number of call sites in the current function. */
2735 static int call_site_count = -1;
2736 /* Number of tail call sites in the current function. */
2737 static int tail_call_site_count = -1;
2739 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
2740 DIEs. */
2741 static VEC (dw_die_ref, heap) *block_map;
2743 /* A cached location list. */
2744 struct GTY (()) cached_dw_loc_list_def {
2745 /* The DECL_UID of the decl that this entry describes. */
2746 unsigned int decl_id;
2748 /* The cached location list. */
2749 dw_loc_list_ref loc_list;
2751 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2753 /* Table of cached location lists. */
2754 static GTY ((param_is (cached_dw_loc_list))) htab_t cached_dw_loc_list_table;
2756 /* A pointer to the base of a list of references to DIE's that
2757 are uniquely identified by their tag, presence/absence of
2758 children DIE's, and list of attribute/value pairs. */
2759 static GTY((length ("abbrev_die_table_allocated")))
2760 dw_die_ref *abbrev_die_table;
2762 /* Number of elements currently allocated for abbrev_die_table. */
2763 static GTY(()) unsigned abbrev_die_table_allocated;
2765 /* Number of elements in type_die_table currently in use. */
2766 static GTY(()) unsigned abbrev_die_table_in_use;
2768 /* Size (in elements) of increments by which we may expand the
2769 abbrev_die_table. */
2770 #define ABBREV_DIE_TABLE_INCREMENT 256
2772 /* A global counter for generating labels for line number data. */
2773 static unsigned int line_info_label_num;
2775 /* The current table to which we should emit line number information
2776 for the current function. This will be set up at the beginning of
2777 assembly for the function. */
2778 static dw_line_info_table *cur_line_info_table;
2780 /* The two default tables of line number info. */
2781 static GTY(()) dw_line_info_table *text_section_line_info;
2782 static GTY(()) dw_line_info_table *cold_text_section_line_info;
2784 /* The set of all non-default tables of line number info. */
2785 static GTY(()) VEC (dw_line_info_table_p, gc) *separate_line_info;
2787 /* A flag to tell pubnames/types export if there is an info section to
2788 refer to. */
2789 static bool info_section_emitted;
2791 /* A pointer to the base of a table that contains a list of publicly
2792 accessible names. */
2793 static GTY (()) VEC (pubname_entry, gc) * pubname_table;
2795 /* A pointer to the base of a table that contains a list of publicly
2796 accessible types. */
2797 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
2799 /* A pointer to the base of a table that contains a list of macro
2800 defines/undefines (and file start/end markers). */
2801 static GTY (()) VEC (macinfo_entry, gc) * macinfo_table;
2803 /* True if .debug_macinfo or .debug_macros section is going to be
2804 emitted. */
2805 #define have_macinfo \
2806 (debug_info_level >= DINFO_LEVEL_VERBOSE \
2807 && !VEC_empty (macinfo_entry, macinfo_table))
2809 /* Array of dies for which we should generate .debug_ranges info. */
2810 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
2812 /* Number of elements currently allocated for ranges_table. */
2813 static GTY(()) unsigned ranges_table_allocated;
2815 /* Number of elements in ranges_table currently in use. */
2816 static GTY(()) unsigned ranges_table_in_use;
2818 /* Array of pairs of labels referenced in ranges_table. */
2819 static GTY ((length ("ranges_by_label_allocated")))
2820 dw_ranges_by_label_ref ranges_by_label;
2822 /* Number of elements currently allocated for ranges_by_label. */
2823 static GTY(()) unsigned ranges_by_label_allocated;
2825 /* Number of elements in ranges_by_label currently in use. */
2826 static GTY(()) unsigned ranges_by_label_in_use;
2828 /* Size (in elements) of increments by which we may expand the
2829 ranges_table. */
2830 #define RANGES_TABLE_INCREMENT 64
2832 /* Whether we have location lists that need outputting */
2833 static GTY(()) bool have_location_lists;
2835 /* Unique label counter. */
2836 static GTY(()) unsigned int loclabel_num;
2838 /* Unique label counter for point-of-call tables. */
2839 static GTY(()) unsigned int poc_label_num;
2841 /* Record whether the function being analyzed contains inlined functions. */
2842 static int current_function_has_inlines;
2844 /* The last file entry emitted by maybe_emit_file(). */
2845 static GTY(()) struct dwarf_file_data * last_emitted_file;
2847 /* Number of internal labels generated by gen_internal_sym(). */
2848 static GTY(()) int label_num;
2850 /* Cached result of previous call to lookup_filename. */
2851 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
2853 static GTY(()) VEC(die_arg_entry,gc) *tmpl_value_parm_die_table;
2855 /* Instances of generic types for which we need to generate debug
2856 info that describe their generic parameters and arguments. That
2857 generation needs to happen once all types are properly laid out so
2858 we do it at the end of compilation. */
2859 static GTY(()) VEC(tree,gc) *generic_type_instances;
2861 /* Offset from the "steady-state frame pointer" to the frame base,
2862 within the current function. */
2863 static HOST_WIDE_INT frame_pointer_fb_offset;
2864 static bool frame_pointer_fb_offset_valid;
2866 static VEC (dw_die_ref, heap) *base_types;
2868 /* Forward declarations for functions defined in this file. */
2870 static int is_pseudo_reg (const_rtx);
2871 static tree type_main_variant (tree);
2872 static int is_tagged_type (const_tree);
2873 static const char *dwarf_tag_name (unsigned);
2874 static const char *dwarf_attr_name (unsigned);
2875 static const char *dwarf_form_name (unsigned);
2876 static tree decl_ultimate_origin (const_tree);
2877 static tree decl_class_context (tree);
2878 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
2879 static inline enum dw_val_class AT_class (dw_attr_ref);
2880 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
2881 static inline unsigned AT_flag (dw_attr_ref);
2882 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
2883 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
2884 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
2885 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
2886 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
2887 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2888 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
2889 unsigned int, unsigned char *);
2890 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
2891 static hashval_t debug_str_do_hash (const void *);
2892 static int debug_str_eq (const void *, const void *);
2893 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
2894 static inline const char *AT_string (dw_attr_ref);
2895 static enum dwarf_form AT_string_form (dw_attr_ref);
2896 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
2897 static void add_AT_specification (dw_die_ref, dw_die_ref);
2898 static inline dw_die_ref AT_ref (dw_attr_ref);
2899 static inline int AT_ref_external (dw_attr_ref);
2900 static inline void set_AT_ref_external (dw_attr_ref, int);
2901 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
2902 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
2903 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
2904 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
2905 dw_loc_list_ref);
2906 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
2907 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
2908 static inline rtx AT_addr (dw_attr_ref);
2909 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
2910 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
2911 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
2912 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
2913 unsigned HOST_WIDE_INT);
2914 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
2915 unsigned long);
2916 static inline const char *AT_lbl (dw_attr_ref);
2917 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
2918 static const char *get_AT_low_pc (dw_die_ref);
2919 static const char *get_AT_hi_pc (dw_die_ref);
2920 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
2921 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
2922 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
2923 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
2924 static bool is_cxx (void);
2925 static bool is_fortran (void);
2926 static bool is_ada (void);
2927 static void remove_AT (dw_die_ref, enum dwarf_attribute);
2928 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
2929 static void add_child_die (dw_die_ref, dw_die_ref);
2930 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
2931 static dw_die_ref lookup_type_die (tree);
2932 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
2933 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
2934 static void equate_type_number_to_die (tree, dw_die_ref);
2935 static hashval_t decl_die_table_hash (const void *);
2936 static int decl_die_table_eq (const void *, const void *);
2937 static dw_die_ref lookup_decl_die (tree);
2938 static hashval_t common_block_die_table_hash (const void *);
2939 static int common_block_die_table_eq (const void *, const void *);
2940 static hashval_t decl_loc_table_hash (const void *);
2941 static int decl_loc_table_eq (const void *, const void *);
2942 static var_loc_list *lookup_decl_loc (const_tree);
2943 static void equate_decl_number_to_die (tree, dw_die_ref);
2944 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
2945 static void print_spaces (FILE *);
2946 static void print_die (dw_die_ref, FILE *);
2947 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
2948 static dw_die_ref pop_compile_unit (dw_die_ref);
2949 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
2950 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
2951 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
2952 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
2953 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
2954 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
2955 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
2956 struct md5_ctx *, int *);
2957 struct checksum_attributes;
2958 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
2959 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
2960 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
2961 static void generate_type_signature (dw_die_ref, comdat_type_node *);
2962 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
2963 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
2964 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
2965 static int same_die_p (dw_die_ref, dw_die_ref, int *);
2966 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
2967 static void compute_section_prefix (dw_die_ref);
2968 static int is_type_die (dw_die_ref);
2969 static int is_comdat_die (dw_die_ref);
2970 static int is_symbol_die (dw_die_ref);
2971 static void assign_symbol_names (dw_die_ref);
2972 static void break_out_includes (dw_die_ref);
2973 static int is_declaration_die (dw_die_ref);
2974 static int should_move_die_to_comdat (dw_die_ref);
2975 static dw_die_ref clone_as_declaration (dw_die_ref);
2976 static dw_die_ref clone_die (dw_die_ref);
2977 static dw_die_ref clone_tree (dw_die_ref);
2978 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
2979 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
2980 static void generate_skeleton_bottom_up (skeleton_chain_node *);
2981 static dw_die_ref generate_skeleton (dw_die_ref);
2982 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
2983 dw_die_ref,
2984 dw_die_ref);
2985 static void break_out_comdat_types (dw_die_ref);
2986 static dw_die_ref copy_ancestor_tree (dw_die_ref, dw_die_ref, htab_t);
2987 static void copy_decls_walk (dw_die_ref, dw_die_ref, htab_t);
2988 static void copy_decls_for_unworthy_types (dw_die_ref);
2990 static hashval_t htab_cu_hash (const void *);
2991 static int htab_cu_eq (const void *, const void *);
2992 static void htab_cu_del (void *);
2993 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
2994 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
2995 static void add_sibling_attributes (dw_die_ref);
2996 static void build_abbrev_table (dw_die_ref, htab_t);
2997 static void output_location_lists (dw_die_ref);
2998 static int constant_size (unsigned HOST_WIDE_INT);
2999 static unsigned long size_of_die (dw_die_ref);
3000 static void calc_die_sizes (dw_die_ref);
3001 static void calc_base_type_die_sizes (void);
3002 static void mark_dies (dw_die_ref);
3003 static void unmark_dies (dw_die_ref);
3004 static void unmark_all_dies (dw_die_ref);
3005 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
3006 static unsigned long size_of_aranges (void);
3007 static enum dwarf_form value_format (dw_attr_ref);
3008 static void output_value_format (dw_attr_ref);
3009 static void output_abbrev_section (void);
3010 static void output_die_symbol (dw_die_ref);
3011 static void output_die (dw_die_ref);
3012 static void output_compilation_unit_header (void);
3013 static void output_comp_unit (dw_die_ref, int);
3014 static void output_comdat_type_unit (comdat_type_node *);
3015 static const char *dwarf2_name (tree, int);
3016 static void add_pubname (tree, dw_die_ref);
3017 static void add_enumerator_pubname (const char *, dw_die_ref);
3018 static void add_pubname_string (const char *, dw_die_ref);
3019 static void add_pubtype (tree, dw_die_ref);
3020 static void output_pubnames (VEC (pubname_entry,gc) *);
3021 static void output_aranges (unsigned long);
3022 static unsigned int add_ranges_num (int);
3023 static unsigned int add_ranges (const_tree);
3024 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3025 bool *);
3026 static void output_ranges (void);
3027 static dw_line_info_table *new_line_info_table (void);
3028 static void output_line_info (void);
3029 static void output_file_names (void);
3030 static dw_die_ref base_type_die (tree);
3031 static int is_base_type (tree);
3032 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3033 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
3034 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3035 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3036 static int type_is_enum (const_tree);
3037 static unsigned int dbx_reg_number (const_rtx);
3038 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3039 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3040 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3041 enum var_init_status);
3042 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3043 enum var_init_status);
3044 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3045 enum var_init_status);
3046 static int is_based_loc (const_rtx);
3047 static int resolve_one_addr (rtx *, void *);
3048 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3049 enum var_init_status);
3050 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
3051 enum var_init_status);
3052 static dw_loc_list_ref loc_list_from_tree (tree, int);
3053 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
3054 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3055 static tree field_type (const_tree);
3056 static unsigned int simple_type_align_in_bits (const_tree);
3057 static unsigned int simple_decl_align_in_bits (const_tree);
3058 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3059 static HOST_WIDE_INT field_byte_offset (const_tree);
3060 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3061 dw_loc_list_ref);
3062 static void add_data_member_location_attribute (dw_die_ref, tree);
3063 static bool add_const_value_attribute (dw_die_ref, rtx);
3064 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3065 static void insert_double (double_int, unsigned char *);
3066 static void insert_float (const_rtx, unsigned char *);
3067 static rtx rtl_for_decl_location (tree);
3068 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3069 enum dwarf_attribute);
3070 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3071 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3072 static void add_name_attribute (dw_die_ref, const char *);
3073 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3074 static void add_comp_dir_attribute (dw_die_ref);
3075 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
3076 static void add_subscript_info (dw_die_ref, tree, bool);
3077 static void add_byte_size_attribute (dw_die_ref, tree);
3078 static void add_bit_offset_attribute (dw_die_ref, tree);
3079 static void add_bit_size_attribute (dw_die_ref, tree);
3080 static void add_prototyped_attribute (dw_die_ref, tree);
3081 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3082 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3083 static void add_src_coords_attributes (dw_die_ref, tree);
3084 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3085 static void push_decl_scope (tree);
3086 static void pop_decl_scope (void);
3087 static dw_die_ref scope_die_for (tree, dw_die_ref);
3088 static inline int local_scope_p (dw_die_ref);
3089 static inline int class_scope_p (dw_die_ref);
3090 static inline int class_or_namespace_scope_p (dw_die_ref);
3091 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
3092 static void add_calling_convention_attribute (dw_die_ref, tree);
3093 static const char *type_tag (const_tree);
3094 static tree member_declared_type (const_tree);
3095 #if 0
3096 static const char *decl_start_label (tree);
3097 #endif
3098 static void gen_array_type_die (tree, dw_die_ref);
3099 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3100 #if 0
3101 static void gen_entry_point_die (tree, dw_die_ref);
3102 #endif
3103 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3104 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3105 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3106 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3107 static void gen_formal_types_die (tree, dw_die_ref);
3108 static void gen_subprogram_die (tree, dw_die_ref);
3109 static void gen_variable_die (tree, tree, dw_die_ref);
3110 static void gen_const_die (tree, dw_die_ref);
3111 static void gen_label_die (tree, dw_die_ref);
3112 static void gen_lexical_block_die (tree, dw_die_ref, int);
3113 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
3114 static void gen_field_die (tree, dw_die_ref);
3115 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3116 static dw_die_ref gen_compile_unit_die (const char *);
3117 static void gen_inheritance_die (tree, tree, dw_die_ref);
3118 static void gen_member_die (tree, dw_die_ref);
3119 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3120 enum debug_info_usage);
3121 static void gen_subroutine_type_die (tree, dw_die_ref);
3122 static void gen_typedef_die (tree, dw_die_ref);
3123 static void gen_type_die (tree, dw_die_ref);
3124 static void gen_block_die (tree, dw_die_ref, int);
3125 static void decls_for_scope (tree, dw_die_ref, int);
3126 static inline int is_redundant_typedef (const_tree);
3127 static bool is_naming_typedef_decl (const_tree);
3128 static inline dw_die_ref get_context_die (tree);
3129 static void gen_namespace_die (tree, dw_die_ref);
3130 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3131 static dw_die_ref force_decl_die (tree);
3132 static dw_die_ref force_type_die (tree);
3133 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3134 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3135 static struct dwarf_file_data * lookup_filename (const char *);
3136 static void retry_incomplete_types (void);
3137 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3138 static void gen_generic_params_dies (tree);
3139 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3140 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3141 static void splice_child_die (dw_die_ref, dw_die_ref);
3142 static int file_info_cmp (const void *, const void *);
3143 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3144 const char *, const char *);
3145 static void output_loc_list (dw_loc_list_ref);
3146 static char *gen_internal_sym (const char *);
3147 static bool want_pubnames (void);
3149 static void prune_unmark_dies (dw_die_ref);
3150 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3151 static void prune_unused_types_mark (dw_die_ref, int);
3152 static void prune_unused_types_walk (dw_die_ref);
3153 static void prune_unused_types_walk_attribs (dw_die_ref);
3154 static void prune_unused_types_prune (dw_die_ref);
3155 static void prune_unused_types (void);
3156 static int maybe_emit_file (struct dwarf_file_data *fd);
3157 static inline const char *AT_vms_delta1 (dw_attr_ref);
3158 static inline const char *AT_vms_delta2 (dw_attr_ref);
3159 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3160 const char *, const char *);
3161 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3162 static void gen_remaining_tmpl_value_param_die_attribute (void);
3163 static bool generic_type_p (tree);
3164 static void schedule_generic_params_dies_gen (tree t);
3165 static void gen_scheduled_generic_parms_dies (void);
3167 /* Section names used to hold DWARF debugging information. */
3168 #ifndef DEBUG_INFO_SECTION
3169 #define DEBUG_INFO_SECTION ".debug_info"
3170 #endif
3171 #ifndef DEBUG_ABBREV_SECTION
3172 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3173 #endif
3174 #ifndef DEBUG_ARANGES_SECTION
3175 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3176 #endif
3177 #ifndef DEBUG_MACINFO_SECTION
3178 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
3179 #endif
3180 #ifndef DEBUG_MACRO_SECTION
3181 #define DEBUG_MACRO_SECTION ".debug_macro"
3182 #endif
3183 #ifndef DEBUG_LINE_SECTION
3184 #define DEBUG_LINE_SECTION ".debug_line"
3185 #endif
3186 #ifndef DEBUG_LOC_SECTION
3187 #define DEBUG_LOC_SECTION ".debug_loc"
3188 #endif
3189 #ifndef DEBUG_PUBNAMES_SECTION
3190 #define DEBUG_PUBNAMES_SECTION ".debug_pubnames"
3191 #endif
3192 #ifndef DEBUG_PUBTYPES_SECTION
3193 #define DEBUG_PUBTYPES_SECTION ".debug_pubtypes"
3194 #endif
3195 #ifndef DEBUG_STR_SECTION
3196 #define DEBUG_STR_SECTION ".debug_str"
3197 #endif
3198 #ifndef DEBUG_RANGES_SECTION
3199 #define DEBUG_RANGES_SECTION ".debug_ranges"
3200 #endif
3202 /* Standard ELF section names for compiled code and data. */
3203 #ifndef TEXT_SECTION_NAME
3204 #define TEXT_SECTION_NAME ".text"
3205 #endif
3207 /* Section flags for .debug_str section. */
3208 #define DEBUG_STR_SECTION_FLAGS \
3209 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3210 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3211 : SECTION_DEBUG)
3213 /* Labels we insert at beginning sections we can reference instead of
3214 the section names themselves. */
3216 #ifndef TEXT_SECTION_LABEL
3217 #define TEXT_SECTION_LABEL "Ltext"
3218 #endif
3219 #ifndef COLD_TEXT_SECTION_LABEL
3220 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3221 #endif
3222 #ifndef DEBUG_LINE_SECTION_LABEL
3223 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3224 #endif
3225 #ifndef DEBUG_INFO_SECTION_LABEL
3226 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3227 #endif
3228 #ifndef DEBUG_ABBREV_SECTION_LABEL
3229 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3230 #endif
3231 #ifndef DEBUG_LOC_SECTION_LABEL
3232 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3233 #endif
3234 #ifndef DEBUG_RANGES_SECTION_LABEL
3235 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3236 #endif
3237 #ifndef DEBUG_MACINFO_SECTION_LABEL
3238 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3239 #endif
3240 #ifndef DEBUG_MACRO_SECTION_LABEL
3241 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3242 #endif
3245 /* Definitions of defaults for formats and names of various special
3246 (artificial) labels which may be generated within this file (when the -g
3247 options is used and DWARF2_DEBUGGING_INFO is in effect.
3248 If necessary, these may be overridden from within the tm.h file, but
3249 typically, overriding these defaults is unnecessary. */
3251 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3252 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3253 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3254 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3255 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3256 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3257 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3258 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3259 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3260 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3262 #ifndef TEXT_END_LABEL
3263 #define TEXT_END_LABEL "Letext"
3264 #endif
3265 #ifndef COLD_END_LABEL
3266 #define COLD_END_LABEL "Letext_cold"
3267 #endif
3268 #ifndef BLOCK_BEGIN_LABEL
3269 #define BLOCK_BEGIN_LABEL "LBB"
3270 #endif
3271 #ifndef BLOCK_END_LABEL
3272 #define BLOCK_END_LABEL "LBE"
3273 #endif
3274 #ifndef LINE_CODE_LABEL
3275 #define LINE_CODE_LABEL "LM"
3276 #endif
3279 /* Return the root of the DIE's built for the current compilation unit. */
3280 static dw_die_ref
3281 comp_unit_die (void)
3283 if (!single_comp_unit_die)
3284 single_comp_unit_die = gen_compile_unit_die (NULL);
3285 return single_comp_unit_die;
3288 /* We allow a language front-end to designate a function that is to be
3289 called to "demangle" any name before it is put into a DIE. */
3291 static const char *(*demangle_name_func) (const char *);
3293 void
3294 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3296 demangle_name_func = func;
3299 /* Test if rtl node points to a pseudo register. */
3301 static inline int
3302 is_pseudo_reg (const_rtx rtl)
3304 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3305 || (GET_CODE (rtl) == SUBREG
3306 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3309 /* Return a reference to a type, with its const and volatile qualifiers
3310 removed. */
3312 static inline tree
3313 type_main_variant (tree type)
3315 type = TYPE_MAIN_VARIANT (type);
3317 /* ??? There really should be only one main variant among any group of
3318 variants of a given type (and all of the MAIN_VARIANT values for all
3319 members of the group should point to that one type) but sometimes the C
3320 front-end messes this up for array types, so we work around that bug
3321 here. */
3322 if (TREE_CODE (type) == ARRAY_TYPE)
3323 while (type != TYPE_MAIN_VARIANT (type))
3324 type = TYPE_MAIN_VARIANT (type);
3326 return type;
3329 /* Return nonzero if the given type node represents a tagged type. */
3331 static inline int
3332 is_tagged_type (const_tree type)
3334 enum tree_code code = TREE_CODE (type);
3336 return (code == RECORD_TYPE || code == UNION_TYPE
3337 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3340 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3342 static void
3343 get_ref_die_offset_label (char *label, dw_die_ref ref)
3345 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3348 /* Return die_offset of a DIE reference to a base type. */
3350 static unsigned long int
3351 get_base_type_offset (dw_die_ref ref)
3353 if (ref->die_offset)
3354 return ref->die_offset;
3355 if (comp_unit_die ()->die_abbrev)
3357 calc_base_type_die_sizes ();
3358 gcc_assert (ref->die_offset);
3360 return ref->die_offset;
3363 /* Return die_offset of a DIE reference other than base type. */
3365 static unsigned long int
3366 get_ref_die_offset (dw_die_ref ref)
3368 gcc_assert (ref->die_offset);
3369 return ref->die_offset;
3372 /* Convert a DIE tag into its string name. */
3374 static const char *
3375 dwarf_tag_name (unsigned int tag)
3377 const char *name = get_DW_TAG_name (tag);
3379 if (name != NULL)
3380 return name;
3382 return "DW_TAG_<unknown>";
3385 /* Convert a DWARF attribute code into its string name. */
3387 static const char *
3388 dwarf_attr_name (unsigned int attr)
3390 const char *name;
3392 switch (attr)
3394 #if VMS_DEBUGGING_INFO
3395 case DW_AT_HP_prologue:
3396 return "DW_AT_HP_prologue";
3397 #else
3398 case DW_AT_MIPS_loop_unroll_factor:
3399 return "DW_AT_MIPS_loop_unroll_factor";
3400 #endif
3402 #if VMS_DEBUGGING_INFO
3403 case DW_AT_HP_epilogue:
3404 return "DW_AT_HP_epilogue";
3405 #else
3406 case DW_AT_MIPS_stride:
3407 return "DW_AT_MIPS_stride";
3408 #endif
3411 name = get_DW_AT_name (attr);
3413 if (name != NULL)
3414 return name;
3416 return "DW_AT_<unknown>";
3419 /* Convert a DWARF value form code into its string name. */
3421 static const char *
3422 dwarf_form_name (unsigned int form)
3424 const char *name = get_DW_FORM_name (form);
3426 if (name != NULL)
3427 return name;
3429 return "DW_FORM_<unknown>";
3432 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3433 instance of an inlined instance of a decl which is local to an inline
3434 function, so we have to trace all of the way back through the origin chain
3435 to find out what sort of node actually served as the original seed for the
3436 given block. */
3438 static tree
3439 decl_ultimate_origin (const_tree decl)
3441 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3442 return NULL_TREE;
3444 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
3445 nodes in the function to point to themselves; ignore that if
3446 we're trying to output the abstract instance of this function. */
3447 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3448 return NULL_TREE;
3450 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3451 most distant ancestor, this should never happen. */
3452 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3454 return DECL_ABSTRACT_ORIGIN (decl);
3457 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
3458 of a virtual function may refer to a base class, so we check the 'this'
3459 parameter. */
3461 static tree
3462 decl_class_context (tree decl)
3464 tree context = NULL_TREE;
3466 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3467 context = DECL_CONTEXT (decl);
3468 else
3469 context = TYPE_MAIN_VARIANT
3470 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3472 if (context && !TYPE_P (context))
3473 context = NULL_TREE;
3475 return context;
3478 /* Add an attribute/value pair to a DIE. */
3480 static inline void
3481 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
3483 /* Maybe this should be an assert? */
3484 if (die == NULL)
3485 return;
3487 if (die->die_attr == NULL)
3488 die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
3489 VEC_safe_push (dw_attr_node, gc, die->die_attr, *attr);
3492 static inline enum dw_val_class
3493 AT_class (dw_attr_ref a)
3495 return a->dw_attr_val.val_class;
3498 /* Add a flag value attribute to a DIE. */
3500 static inline void
3501 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3503 dw_attr_node attr;
3505 attr.dw_attr = attr_kind;
3506 attr.dw_attr_val.val_class = dw_val_class_flag;
3507 attr.dw_attr_val.v.val_flag = flag;
3508 add_dwarf_attr (die, &attr);
3511 static inline unsigned
3512 AT_flag (dw_attr_ref a)
3514 gcc_assert (a && AT_class (a) == dw_val_class_flag);
3515 return a->dw_attr_val.v.val_flag;
3518 /* Add a signed integer attribute value to a DIE. */
3520 static inline void
3521 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3523 dw_attr_node attr;
3525 attr.dw_attr = attr_kind;
3526 attr.dw_attr_val.val_class = dw_val_class_const;
3527 attr.dw_attr_val.v.val_int = int_val;
3528 add_dwarf_attr (die, &attr);
3531 static inline HOST_WIDE_INT
3532 AT_int (dw_attr_ref a)
3534 gcc_assert (a && AT_class (a) == dw_val_class_const);
3535 return a->dw_attr_val.v.val_int;
3538 /* Add an unsigned integer attribute value to a DIE. */
3540 static inline void
3541 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3542 unsigned HOST_WIDE_INT unsigned_val)
3544 dw_attr_node attr;
3546 attr.dw_attr = attr_kind;
3547 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3548 attr.dw_attr_val.v.val_unsigned = unsigned_val;
3549 add_dwarf_attr (die, &attr);
3552 static inline unsigned HOST_WIDE_INT
3553 AT_unsigned (dw_attr_ref a)
3555 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3556 return a->dw_attr_val.v.val_unsigned;
3559 /* Add an unsigned double integer attribute value to a DIE. */
3561 static inline void
3562 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3563 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3565 dw_attr_node attr;
3567 attr.dw_attr = attr_kind;
3568 attr.dw_attr_val.val_class = dw_val_class_const_double;
3569 attr.dw_attr_val.v.val_double.high = high;
3570 attr.dw_attr_val.v.val_double.low = low;
3571 add_dwarf_attr (die, &attr);
3574 /* Add a floating point attribute value to a DIE and return it. */
3576 static inline void
3577 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3578 unsigned int length, unsigned int elt_size, unsigned char *array)
3580 dw_attr_node attr;
3582 attr.dw_attr = attr_kind;
3583 attr.dw_attr_val.val_class = dw_val_class_vec;
3584 attr.dw_attr_val.v.val_vec.length = length;
3585 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3586 attr.dw_attr_val.v.val_vec.array = array;
3587 add_dwarf_attr (die, &attr);
3590 /* Add an 8-byte data attribute value to a DIE. */
3592 static inline void
3593 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3594 unsigned char data8[8])
3596 dw_attr_node attr;
3598 attr.dw_attr = attr_kind;
3599 attr.dw_attr_val.val_class = dw_val_class_data8;
3600 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3601 add_dwarf_attr (die, &attr);
3604 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. */
3605 static inline void
3606 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high)
3608 dw_attr_node attr;
3610 attr.dw_attr = DW_AT_low_pc;
3611 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3612 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_low);
3613 add_dwarf_attr (die, &attr);
3615 attr.dw_attr = DW_AT_high_pc;
3616 if (dwarf_version < 4)
3617 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3618 else
3619 attr.dw_attr_val.val_class = dw_val_class_high_pc;
3620 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_high);
3621 add_dwarf_attr (die, &attr);
3624 /* Hash and equality functions for debug_str_hash. */
3626 static hashval_t
3627 debug_str_do_hash (const void *x)
3629 return htab_hash_string (((const struct indirect_string_node *)x)->str);
3632 static int
3633 debug_str_eq (const void *x1, const void *x2)
3635 return strcmp ((((const struct indirect_string_node *)x1)->str),
3636 (const char *)x2) == 0;
3639 /* Add STR to the indirect string hash table. */
3641 static struct indirect_string_node *
3642 find_AT_string (const char *str)
3644 struct indirect_string_node *node;
3645 void **slot;
3647 if (! debug_str_hash)
3648 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
3649 debug_str_eq, NULL);
3651 slot = htab_find_slot_with_hash (debug_str_hash, str,
3652 htab_hash_string (str), INSERT);
3653 if (*slot == NULL)
3655 node = ggc_alloc_cleared_indirect_string_node ();
3656 node->str = ggc_strdup (str);
3657 *slot = node;
3659 else
3660 node = (struct indirect_string_node *) *slot;
3662 node->refcount++;
3663 return node;
3666 /* Add a string attribute value to a DIE. */
3668 static inline void
3669 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
3671 dw_attr_node attr;
3672 struct indirect_string_node *node;
3674 node = find_AT_string (str);
3676 attr.dw_attr = attr_kind;
3677 attr.dw_attr_val.val_class = dw_val_class_str;
3678 attr.dw_attr_val.v.val_str = node;
3679 add_dwarf_attr (die, &attr);
3682 static inline const char *
3683 AT_string (dw_attr_ref a)
3685 gcc_assert (a && AT_class (a) == dw_val_class_str);
3686 return a->dw_attr_val.v.val_str->str;
3689 /* Find out whether a string should be output inline in DIE
3690 or out-of-line in .debug_str section. */
3692 static enum dwarf_form
3693 AT_string_form (dw_attr_ref a)
3695 struct indirect_string_node *node;
3696 unsigned int len;
3697 char label[32];
3699 gcc_assert (a && AT_class (a) == dw_val_class_str);
3701 node = a->dw_attr_val.v.val_str;
3702 if (node->form)
3703 return node->form;
3705 len = strlen (node->str) + 1;
3707 /* If the string is shorter or equal to the size of the reference, it is
3708 always better to put it inline. */
3709 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
3710 return node->form = DW_FORM_string;
3712 /* If we cannot expect the linker to merge strings in .debug_str
3713 section, only put it into .debug_str if it is worth even in this
3714 single module. */
3715 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
3716 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
3717 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
3718 return node->form = DW_FORM_string;
3720 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
3721 ++dw2_string_counter;
3722 node->label = xstrdup (label);
3724 return node->form = DW_FORM_strp;
3727 /* Add a DIE reference attribute value to a DIE. */
3729 static inline void
3730 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
3732 dw_attr_node attr;
3734 #ifdef ENABLE_CHECKING
3735 gcc_assert (targ_die != NULL);
3736 #else
3737 /* With LTO we can end up trying to reference something we didn't create
3738 a DIE for. Avoid crashing later on a NULL referenced DIE. */
3739 if (targ_die == NULL)
3740 return;
3741 #endif
3743 attr.dw_attr = attr_kind;
3744 attr.dw_attr_val.val_class = dw_val_class_die_ref;
3745 attr.dw_attr_val.v.val_die_ref.die = targ_die;
3746 attr.dw_attr_val.v.val_die_ref.external = 0;
3747 add_dwarf_attr (die, &attr);
3750 /* Change DIE reference REF to point to NEW_DIE instead. */
3752 static inline void
3753 change_AT_die_ref (dw_attr_ref ref, dw_die_ref new_die)
3755 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
3756 ref->dw_attr_val.v.val_die_ref.die = new_die;
3757 ref->dw_attr_val.v.val_die_ref.external = 0;
3760 /* Add an AT_specification attribute to a DIE, and also make the back
3761 pointer from the specification to the definition. */
3763 static inline void
3764 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
3766 add_AT_die_ref (die, DW_AT_specification, targ_die);
3767 gcc_assert (!targ_die->die_definition);
3768 targ_die->die_definition = die;
3771 static inline dw_die_ref
3772 AT_ref (dw_attr_ref a)
3774 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
3775 return a->dw_attr_val.v.val_die_ref.die;
3778 static inline int
3779 AT_ref_external (dw_attr_ref a)
3781 if (a && AT_class (a) == dw_val_class_die_ref)
3782 return a->dw_attr_val.v.val_die_ref.external;
3784 return 0;
3787 static inline void
3788 set_AT_ref_external (dw_attr_ref a, int i)
3790 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
3791 a->dw_attr_val.v.val_die_ref.external = i;
3794 /* Add an FDE reference attribute value to a DIE. */
3796 static inline void
3797 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
3799 dw_attr_node attr;
3801 attr.dw_attr = attr_kind;
3802 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
3803 attr.dw_attr_val.v.val_fde_index = targ_fde;
3804 add_dwarf_attr (die, &attr);
3807 /* Add a location description attribute value to a DIE. */
3809 static inline void
3810 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
3812 dw_attr_node attr;
3814 attr.dw_attr = attr_kind;
3815 attr.dw_attr_val.val_class = dw_val_class_loc;
3816 attr.dw_attr_val.v.val_loc = loc;
3817 add_dwarf_attr (die, &attr);
3820 static inline dw_loc_descr_ref
3821 AT_loc (dw_attr_ref a)
3823 gcc_assert (a && AT_class (a) == dw_val_class_loc);
3824 return a->dw_attr_val.v.val_loc;
3827 static inline void
3828 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
3830 dw_attr_node attr;
3832 attr.dw_attr = attr_kind;
3833 attr.dw_attr_val.val_class = dw_val_class_loc_list;
3834 attr.dw_attr_val.v.val_loc_list = loc_list;
3835 add_dwarf_attr (die, &attr);
3836 have_location_lists = true;
3839 static inline dw_loc_list_ref
3840 AT_loc_list (dw_attr_ref a)
3842 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
3843 return a->dw_attr_val.v.val_loc_list;
3846 static inline dw_loc_list_ref *
3847 AT_loc_list_ptr (dw_attr_ref a)
3849 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
3850 return &a->dw_attr_val.v.val_loc_list;
3853 /* Add an address constant attribute value to a DIE. */
3855 static inline void
3856 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
3858 dw_attr_node attr;
3860 attr.dw_attr = attr_kind;
3861 attr.dw_attr_val.val_class = dw_val_class_addr;
3862 attr.dw_attr_val.v.val_addr = addr;
3863 add_dwarf_attr (die, &attr);
3866 /* Get the RTX from to an address DIE attribute. */
3868 static inline rtx
3869 AT_addr (dw_attr_ref a)
3871 gcc_assert (a && AT_class (a) == dw_val_class_addr);
3872 return a->dw_attr_val.v.val_addr;
3875 /* Add a file attribute value to a DIE. */
3877 static inline void
3878 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
3879 struct dwarf_file_data *fd)
3881 dw_attr_node attr;
3883 attr.dw_attr = attr_kind;
3884 attr.dw_attr_val.val_class = dw_val_class_file;
3885 attr.dw_attr_val.v.val_file = fd;
3886 add_dwarf_attr (die, &attr);
3889 /* Get the dwarf_file_data from a file DIE attribute. */
3891 static inline struct dwarf_file_data *
3892 AT_file (dw_attr_ref a)
3894 gcc_assert (a && AT_class (a) == dw_val_class_file);
3895 return a->dw_attr_val.v.val_file;
3898 /* Add a vms delta attribute value to a DIE. */
3900 static inline void
3901 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
3902 const char *lbl1, const char *lbl2)
3904 dw_attr_node attr;
3906 attr.dw_attr = attr_kind;
3907 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
3908 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
3909 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
3910 add_dwarf_attr (die, &attr);
3913 /* Add a label identifier attribute value to a DIE. */
3915 static inline void
3916 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
3918 dw_attr_node attr;
3920 attr.dw_attr = attr_kind;
3921 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3922 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
3923 add_dwarf_attr (die, &attr);
3926 /* Add a section offset attribute value to a DIE, an offset into the
3927 debug_line section. */
3929 static inline void
3930 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
3931 const char *label)
3933 dw_attr_node attr;
3935 attr.dw_attr = attr_kind;
3936 attr.dw_attr_val.val_class = dw_val_class_lineptr;
3937 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
3938 add_dwarf_attr (die, &attr);
3941 /* Add a section offset attribute value to a DIE, an offset into the
3942 debug_macinfo section. */
3944 static inline void
3945 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
3946 const char *label)
3948 dw_attr_node attr;
3950 attr.dw_attr = attr_kind;
3951 attr.dw_attr_val.val_class = dw_val_class_macptr;
3952 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
3953 add_dwarf_attr (die, &attr);
3956 /* Add an offset attribute value to a DIE. */
3958 static inline void
3959 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
3960 unsigned HOST_WIDE_INT offset)
3962 dw_attr_node attr;
3964 attr.dw_attr = attr_kind;
3965 attr.dw_attr_val.val_class = dw_val_class_offset;
3966 attr.dw_attr_val.v.val_offset = offset;
3967 add_dwarf_attr (die, &attr);
3970 /* Add an range_list attribute value to a DIE. */
3972 static void
3973 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
3974 long unsigned int offset)
3976 dw_attr_node attr;
3978 attr.dw_attr = attr_kind;
3979 attr.dw_attr_val.val_class = dw_val_class_range_list;
3980 attr.dw_attr_val.v.val_offset = offset;
3981 add_dwarf_attr (die, &attr);
3984 /* Return the start label of a delta attribute. */
3986 static inline const char *
3987 AT_vms_delta1 (dw_attr_ref a)
3989 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
3990 return a->dw_attr_val.v.val_vms_delta.lbl1;
3993 /* Return the end label of a delta attribute. */
3995 static inline const char *
3996 AT_vms_delta2 (dw_attr_ref a)
3998 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
3999 return a->dw_attr_val.v.val_vms_delta.lbl2;
4002 static inline const char *
4003 AT_lbl (dw_attr_ref a)
4005 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4006 || AT_class (a) == dw_val_class_lineptr
4007 || AT_class (a) == dw_val_class_macptr
4008 || AT_class (a) == dw_val_class_high_pc));
4009 return a->dw_attr_val.v.val_lbl_id;
4012 /* Get the attribute of type attr_kind. */
4014 static dw_attr_ref
4015 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4017 dw_attr_ref a;
4018 unsigned ix;
4019 dw_die_ref spec = NULL;
4021 if (! die)
4022 return NULL;
4024 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
4025 if (a->dw_attr == attr_kind)
4026 return a;
4027 else if (a->dw_attr == DW_AT_specification
4028 || a->dw_attr == DW_AT_abstract_origin)
4029 spec = AT_ref (a);
4031 if (spec)
4032 return get_AT (spec, attr_kind);
4034 return NULL;
4037 /* Returns the parent of the declaration of DIE. */
4039 static dw_die_ref
4040 get_die_parent (dw_die_ref die)
4042 dw_die_ref t;
4044 if (!die)
4045 return NULL;
4047 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4048 || (t = get_AT_ref (die, DW_AT_specification)))
4049 die = t;
4051 return die->die_parent;
4054 /* Return the "low pc" attribute value, typically associated with a subprogram
4055 DIE. Return null if the "low pc" attribute is either not present, or if it
4056 cannot be represented as an assembler label identifier. */
4058 static inline const char *
4059 get_AT_low_pc (dw_die_ref die)
4061 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4063 return a ? AT_lbl (a) : NULL;
4066 /* Return the "high pc" attribute value, typically associated with a subprogram
4067 DIE. Return null if the "high pc" attribute is either not present, or if it
4068 cannot be represented as an assembler label identifier. */
4070 static inline const char *
4071 get_AT_hi_pc (dw_die_ref die)
4073 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4075 return a ? AT_lbl (a) : NULL;
4078 /* Return the value of the string attribute designated by ATTR_KIND, or
4079 NULL if it is not present. */
4081 static inline const char *
4082 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4084 dw_attr_ref a = get_AT (die, attr_kind);
4086 return a ? AT_string (a) : NULL;
4089 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4090 if it is not present. */
4092 static inline int
4093 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4095 dw_attr_ref a = get_AT (die, attr_kind);
4097 return a ? AT_flag (a) : 0;
4100 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4101 if it is not present. */
4103 static inline unsigned
4104 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4106 dw_attr_ref a = get_AT (die, attr_kind);
4108 return a ? AT_unsigned (a) : 0;
4111 static inline dw_die_ref
4112 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4114 dw_attr_ref a = get_AT (die, attr_kind);
4116 return a ? AT_ref (a) : NULL;
4119 static inline struct dwarf_file_data *
4120 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4122 dw_attr_ref a = get_AT (die, attr_kind);
4124 return a ? AT_file (a) : NULL;
4127 /* Return TRUE if the language is C++. */
4129 static inline bool
4130 is_cxx (void)
4132 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4134 return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
4137 /* Return TRUE if the language is Fortran. */
4139 static inline bool
4140 is_fortran (void)
4142 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4144 return (lang == DW_LANG_Fortran77
4145 || lang == DW_LANG_Fortran90
4146 || lang == DW_LANG_Fortran95);
4149 /* Return TRUE if the language is Ada. */
4151 static inline bool
4152 is_ada (void)
4154 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4156 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4159 /* Remove the specified attribute if present. */
4161 static void
4162 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4164 dw_attr_ref a;
4165 unsigned ix;
4167 if (! die)
4168 return;
4170 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
4171 if (a->dw_attr == attr_kind)
4173 if (AT_class (a) == dw_val_class_str)
4174 if (a->dw_attr_val.v.val_str->refcount)
4175 a->dw_attr_val.v.val_str->refcount--;
4177 /* VEC_ordered_remove should help reduce the number of abbrevs
4178 that are needed. */
4179 VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
4180 return;
4184 /* Remove CHILD from its parent. PREV must have the property that
4185 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4187 static void
4188 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4190 gcc_assert (child->die_parent == prev->die_parent);
4191 gcc_assert (prev->die_sib == child);
4192 if (prev == child)
4194 gcc_assert (child->die_parent->die_child == child);
4195 prev = NULL;
4197 else
4198 prev->die_sib = child->die_sib;
4199 if (child->die_parent->die_child == child)
4200 child->die_parent->die_child = prev;
4203 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4204 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4206 static void
4207 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4209 dw_die_ref parent = old_child->die_parent;
4211 gcc_assert (parent == prev->die_parent);
4212 gcc_assert (prev->die_sib == old_child);
4214 new_child->die_parent = parent;
4215 if (prev == old_child)
4217 gcc_assert (parent->die_child == old_child);
4218 new_child->die_sib = new_child;
4220 else
4222 prev->die_sib = new_child;
4223 new_child->die_sib = old_child->die_sib;
4225 if (old_child->die_parent->die_child == old_child)
4226 old_child->die_parent->die_child = new_child;
4229 /* Move all children from OLD_PARENT to NEW_PARENT. */
4231 static void
4232 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4234 dw_die_ref c;
4235 new_parent->die_child = old_parent->die_child;
4236 old_parent->die_child = NULL;
4237 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4240 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4241 matches TAG. */
4243 static void
4244 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4246 dw_die_ref c;
4248 c = die->die_child;
4249 if (c) do {
4250 dw_die_ref prev = c;
4251 c = c->die_sib;
4252 while (c->die_tag == tag)
4254 remove_child_with_prev (c, prev);
4255 /* Might have removed every child. */
4256 if (c == c->die_sib)
4257 return;
4258 c = c->die_sib;
4260 } while (c != die->die_child);
4263 /* Add a CHILD_DIE as the last child of DIE. */
4265 static void
4266 add_child_die (dw_die_ref die, dw_die_ref child_die)
4268 /* FIXME this should probably be an assert. */
4269 if (! die || ! child_die)
4270 return;
4271 gcc_assert (die != child_die);
4273 child_die->die_parent = die;
4274 if (die->die_child)
4276 child_die->die_sib = die->die_child->die_sib;
4277 die->die_child->die_sib = child_die;
4279 else
4280 child_die->die_sib = child_die;
4281 die->die_child = child_die;
4284 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4285 is the specification, to the end of PARENT's list of children.
4286 This is done by removing and re-adding it. */
4288 static void
4289 splice_child_die (dw_die_ref parent, dw_die_ref child)
4291 dw_die_ref p;
4293 /* We want the declaration DIE from inside the class, not the
4294 specification DIE at toplevel. */
4295 if (child->die_parent != parent)
4297 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4299 if (tmp)
4300 child = tmp;
4303 gcc_assert (child->die_parent == parent
4304 || (child->die_parent
4305 == get_AT_ref (parent, DW_AT_specification)));
4307 for (p = child->die_parent->die_child; ; p = p->die_sib)
4308 if (p->die_sib == child)
4310 remove_child_with_prev (child, p);
4311 break;
4314 add_child_die (parent, child);
4317 /* Return a pointer to a newly created DIE node. */
4319 static inline dw_die_ref
4320 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4322 dw_die_ref die = ggc_alloc_cleared_die_node ();
4324 die->die_tag = tag_value;
4326 if (parent_die != NULL)
4327 add_child_die (parent_die, die);
4328 else
4330 limbo_die_node *limbo_node;
4332 limbo_node = ggc_alloc_cleared_limbo_die_node ();
4333 limbo_node->die = die;
4334 limbo_node->created_for = t;
4335 limbo_node->next = limbo_die_list;
4336 limbo_die_list = limbo_node;
4339 return die;
4342 /* Return the DIE associated with the given type specifier. */
4344 static inline dw_die_ref
4345 lookup_type_die (tree type)
4347 return TYPE_SYMTAB_DIE (type);
4350 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4351 anonymous type named by the typedef TYPE_DIE, return the DIE of the
4352 anonymous type instead the one of the naming typedef. */
4354 static inline dw_die_ref
4355 strip_naming_typedef (tree type, dw_die_ref type_die)
4357 if (type
4358 && TREE_CODE (type) == RECORD_TYPE
4359 && type_die
4360 && type_die->die_tag == DW_TAG_typedef
4361 && is_naming_typedef_decl (TYPE_NAME (type)))
4362 type_die = get_AT_ref (type_die, DW_AT_type);
4363 return type_die;
4366 /* Like lookup_type_die, but if type is an anonymous type named by a
4367 typedef[1], return the DIE of the anonymous type instead the one of
4368 the naming typedef. This is because in gen_typedef_die, we did
4369 equate the anonymous struct named by the typedef with the DIE of
4370 the naming typedef. So by default, lookup_type_die on an anonymous
4371 struct yields the DIE of the naming typedef.
4373 [1]: Read the comment of is_naming_typedef_decl to learn about what
4374 a naming typedef is. */
4376 static inline dw_die_ref
4377 lookup_type_die_strip_naming_typedef (tree type)
4379 dw_die_ref die = lookup_type_die (type);
4380 return strip_naming_typedef (type, die);
4383 /* Equate a DIE to a given type specifier. */
4385 static inline void
4386 equate_type_number_to_die (tree type, dw_die_ref type_die)
4388 TYPE_SYMTAB_DIE (type) = type_die;
4391 /* Returns a hash value for X (which really is a die_struct). */
4393 static hashval_t
4394 decl_die_table_hash (const void *x)
4396 return (hashval_t) ((const_dw_die_ref) x)->decl_id;
4399 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
4401 static int
4402 decl_die_table_eq (const void *x, const void *y)
4404 return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
4407 /* Return the DIE associated with a given declaration. */
4409 static inline dw_die_ref
4410 lookup_decl_die (tree decl)
4412 return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
4415 /* Returns a hash value for X (which really is a var_loc_list). */
4417 static hashval_t
4418 decl_loc_table_hash (const void *x)
4420 return (hashval_t) ((const var_loc_list *) x)->decl_id;
4423 /* Return nonzero if decl_id of var_loc_list X is the same as
4424 UID of decl *Y. */
4426 static int
4427 decl_loc_table_eq (const void *x, const void *y)
4429 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
4432 /* Return the var_loc list associated with a given declaration. */
4434 static inline var_loc_list *
4435 lookup_decl_loc (const_tree decl)
4437 if (!decl_loc_table)
4438 return NULL;
4439 return (var_loc_list *)
4440 htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
4443 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
4445 static hashval_t
4446 cached_dw_loc_list_table_hash (const void *x)
4448 return (hashval_t) ((const cached_dw_loc_list *) x)->decl_id;
4451 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
4452 UID of decl *Y. */
4454 static int
4455 cached_dw_loc_list_table_eq (const void *x, const void *y)
4457 return (((const cached_dw_loc_list *) x)->decl_id
4458 == DECL_UID ((const_tree) y));
4461 /* Equate a DIE to a particular declaration. */
4463 static void
4464 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
4466 unsigned int decl_id = DECL_UID (decl);
4467 void **slot;
4469 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
4470 *slot = decl_die;
4471 decl_die->decl_id = decl_id;
4474 /* Return how many bits covers PIECE EXPR_LIST. */
4476 static int
4477 decl_piece_bitsize (rtx piece)
4479 int ret = (int) GET_MODE (piece);
4480 if (ret)
4481 return ret;
4482 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
4483 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
4484 return INTVAL (XEXP (XEXP (piece, 0), 0));
4487 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
4489 static rtx *
4490 decl_piece_varloc_ptr (rtx piece)
4492 if ((int) GET_MODE (piece))
4493 return &XEXP (piece, 0);
4494 else
4495 return &XEXP (XEXP (piece, 0), 1);
4498 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
4499 Next is the chain of following piece nodes. */
4501 static rtx
4502 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
4504 if (bitsize <= (int) MAX_MACHINE_MODE)
4505 return alloc_EXPR_LIST (bitsize, loc_note, next);
4506 else
4507 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
4508 GEN_INT (bitsize),
4509 loc_note), next);
4512 /* Return rtx that should be stored into loc field for
4513 LOC_NOTE and BITPOS/BITSIZE. */
4515 static rtx
4516 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
4517 HOST_WIDE_INT bitsize)
4519 if (bitsize != -1)
4521 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
4522 if (bitpos != 0)
4523 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
4525 return loc_note;
4528 /* This function either modifies location piece list *DEST in
4529 place (if SRC and INNER is NULL), or copies location piece list
4530 *SRC to *DEST while modifying it. Location BITPOS is modified
4531 to contain LOC_NOTE, any pieces overlapping it are removed resp.
4532 not copied and if needed some padding around it is added.
4533 When modifying in place, DEST should point to EXPR_LIST where
4534 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
4535 to the start of the whole list and INNER points to the EXPR_LIST
4536 where earlier pieces cover PIECE_BITPOS bits. */
4538 static void
4539 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
4540 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
4541 HOST_WIDE_INT bitsize, rtx loc_note)
4543 int diff;
4544 bool copy = inner != NULL;
4546 if (copy)
4548 /* First copy all nodes preceding the current bitpos. */
4549 while (src != inner)
4551 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
4552 decl_piece_bitsize (*src), NULL_RTX);
4553 dest = &XEXP (*dest, 1);
4554 src = &XEXP (*src, 1);
4557 /* Add padding if needed. */
4558 if (bitpos != piece_bitpos)
4560 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
4561 copy ? NULL_RTX : *dest);
4562 dest = &XEXP (*dest, 1);
4564 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
4566 gcc_assert (!copy);
4567 /* A piece with correct bitpos and bitsize already exist,
4568 just update the location for it and return. */
4569 *decl_piece_varloc_ptr (*dest) = loc_note;
4570 return;
4572 /* Add the piece that changed. */
4573 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
4574 dest = &XEXP (*dest, 1);
4575 /* Skip over pieces that overlap it. */
4576 diff = bitpos - piece_bitpos + bitsize;
4577 if (!copy)
4578 src = dest;
4579 while (diff > 0 && *src)
4581 rtx piece = *src;
4582 diff -= decl_piece_bitsize (piece);
4583 if (copy)
4584 src = &XEXP (piece, 1);
4585 else
4587 *src = XEXP (piece, 1);
4588 free_EXPR_LIST_node (piece);
4591 /* Add padding if needed. */
4592 if (diff < 0 && *src)
4594 if (!copy)
4595 dest = src;
4596 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
4597 dest = &XEXP (*dest, 1);
4599 if (!copy)
4600 return;
4601 /* Finally copy all nodes following it. */
4602 while (*src)
4604 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
4605 decl_piece_bitsize (*src), NULL_RTX);
4606 dest = &XEXP (*dest, 1);
4607 src = &XEXP (*src, 1);
4611 /* Add a variable location node to the linked list for DECL. */
4613 static struct var_loc_node *
4614 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
4616 unsigned int decl_id;
4617 var_loc_list *temp;
4618 void **slot;
4619 struct var_loc_node *loc = NULL;
4620 HOST_WIDE_INT bitsize = -1, bitpos = -1;
4622 if (DECL_DEBUG_EXPR_IS_FROM (decl))
4624 tree realdecl = DECL_DEBUG_EXPR (decl);
4625 if (realdecl && handled_component_p (realdecl))
4627 HOST_WIDE_INT maxsize;
4628 tree innerdecl;
4629 innerdecl
4630 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
4631 if (!DECL_P (innerdecl)
4632 || DECL_IGNORED_P (innerdecl)
4633 || TREE_STATIC (innerdecl)
4634 || bitsize <= 0
4635 || bitpos + bitsize > 256
4636 || bitsize != maxsize)
4637 return NULL;
4638 decl = innerdecl;
4642 decl_id = DECL_UID (decl);
4643 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
4644 if (*slot == NULL)
4646 temp = ggc_alloc_cleared_var_loc_list ();
4647 temp->decl_id = decl_id;
4648 *slot = temp;
4650 else
4651 temp = (var_loc_list *) *slot;
4653 /* For PARM_DECLs try to keep around the original incoming value,
4654 even if that means we'll emit a zero-range .debug_loc entry. */
4655 if (temp->last
4656 && temp->first == temp->last
4657 && TREE_CODE (decl) == PARM_DECL
4658 && GET_CODE (temp->first->loc) == NOTE
4659 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
4660 && DECL_INCOMING_RTL (decl)
4661 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
4662 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
4663 == GET_CODE (DECL_INCOMING_RTL (decl))
4664 && prev_real_insn (temp->first->loc) == NULL_RTX
4665 && (bitsize != -1
4666 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
4667 NOTE_VAR_LOCATION_LOC (loc_note))
4668 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
4669 != NOTE_VAR_LOCATION_STATUS (loc_note))))
4671 loc = ggc_alloc_cleared_var_loc_node ();
4672 temp->first->next = loc;
4673 temp->last = loc;
4674 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
4676 else if (temp->last)
4678 struct var_loc_node *last = temp->last, *unused = NULL;
4679 rtx *piece_loc = NULL, last_loc_note;
4680 int piece_bitpos = 0;
4681 if (last->next)
4683 last = last->next;
4684 gcc_assert (last->next == NULL);
4686 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
4688 piece_loc = &last->loc;
4691 int cur_bitsize = decl_piece_bitsize (*piece_loc);
4692 if (piece_bitpos + cur_bitsize > bitpos)
4693 break;
4694 piece_bitpos += cur_bitsize;
4695 piece_loc = &XEXP (*piece_loc, 1);
4697 while (*piece_loc);
4699 /* TEMP->LAST here is either pointer to the last but one or
4700 last element in the chained list, LAST is pointer to the
4701 last element. */
4702 if (label && strcmp (last->label, label) == 0)
4704 /* For SRA optimized variables if there weren't any real
4705 insns since last note, just modify the last node. */
4706 if (piece_loc != NULL)
4708 adjust_piece_list (piece_loc, NULL, NULL,
4709 bitpos, piece_bitpos, bitsize, loc_note);
4710 return NULL;
4712 /* If the last note doesn't cover any instructions, remove it. */
4713 if (temp->last != last)
4715 temp->last->next = NULL;
4716 unused = last;
4717 last = temp->last;
4718 gcc_assert (strcmp (last->label, label) != 0);
4720 else
4722 gcc_assert (temp->first == temp->last
4723 || (temp->first->next == temp->last
4724 && TREE_CODE (decl) == PARM_DECL));
4725 memset (temp->last, '\0', sizeof (*temp->last));
4726 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
4727 return temp->last;
4730 if (bitsize == -1 && NOTE_P (last->loc))
4731 last_loc_note = last->loc;
4732 else if (piece_loc != NULL
4733 && *piece_loc != NULL_RTX
4734 && piece_bitpos == bitpos
4735 && decl_piece_bitsize (*piece_loc) == bitsize)
4736 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
4737 else
4738 last_loc_note = NULL_RTX;
4739 /* If the current location is the same as the end of the list,
4740 and either both or neither of the locations is uninitialized,
4741 we have nothing to do. */
4742 if (last_loc_note == NULL_RTX
4743 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
4744 NOTE_VAR_LOCATION_LOC (loc_note)))
4745 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
4746 != NOTE_VAR_LOCATION_STATUS (loc_note))
4747 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
4748 == VAR_INIT_STATUS_UNINITIALIZED)
4749 || (NOTE_VAR_LOCATION_STATUS (loc_note)
4750 == VAR_INIT_STATUS_UNINITIALIZED))))
4752 /* Add LOC to the end of list and update LAST. If the last
4753 element of the list has been removed above, reuse its
4754 memory for the new node, otherwise allocate a new one. */
4755 if (unused)
4757 loc = unused;
4758 memset (loc, '\0', sizeof (*loc));
4760 else
4761 loc = ggc_alloc_cleared_var_loc_node ();
4762 if (bitsize == -1 || piece_loc == NULL)
4763 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
4764 else
4765 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
4766 bitpos, piece_bitpos, bitsize, loc_note);
4767 last->next = loc;
4768 /* Ensure TEMP->LAST will point either to the new last but one
4769 element of the chain, or to the last element in it. */
4770 if (last != temp->last)
4771 temp->last = last;
4773 else if (unused)
4774 ggc_free (unused);
4776 else
4778 loc = ggc_alloc_cleared_var_loc_node ();
4779 temp->first = loc;
4780 temp->last = loc;
4781 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
4783 return loc;
4786 /* Keep track of the number of spaces used to indent the
4787 output of the debugging routines that print the structure of
4788 the DIE internal representation. */
4789 static int print_indent;
4791 /* Indent the line the number of spaces given by print_indent. */
4793 static inline void
4794 print_spaces (FILE *outfile)
4796 fprintf (outfile, "%*s", print_indent, "");
4799 /* Print a type signature in hex. */
4801 static inline void
4802 print_signature (FILE *outfile, char *sig)
4804 int i;
4806 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
4807 fprintf (outfile, "%02x", sig[i] & 0xff);
4810 /* Print the information associated with a given DIE, and its children.
4811 This routine is a debugging aid only. */
4813 static void
4814 print_die (dw_die_ref die, FILE *outfile)
4816 dw_attr_ref a;
4817 dw_die_ref c;
4818 unsigned ix;
4820 print_spaces (outfile);
4821 fprintf (outfile, "DIE %4ld: %s (%p)\n",
4822 die->die_offset, dwarf_tag_name (die->die_tag),
4823 (void*) die);
4824 print_spaces (outfile);
4825 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
4826 fprintf (outfile, " offset: %ld", die->die_offset);
4827 fprintf (outfile, " mark: %d\n", die->die_mark);
4829 if (die->comdat_type_p)
4831 print_spaces (outfile);
4832 fprintf (outfile, " signature: ");
4833 print_signature (outfile, die->die_id.die_type_node->signature);
4834 fprintf (outfile, "\n");
4837 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
4839 print_spaces (outfile);
4840 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
4842 switch (AT_class (a))
4844 case dw_val_class_addr:
4845 fprintf (outfile, "address");
4846 break;
4847 case dw_val_class_offset:
4848 fprintf (outfile, "offset");
4849 break;
4850 case dw_val_class_loc:
4851 fprintf (outfile, "location descriptor");
4852 break;
4853 case dw_val_class_loc_list:
4854 fprintf (outfile, "location list -> label:%s",
4855 AT_loc_list (a)->ll_symbol);
4856 break;
4857 case dw_val_class_range_list:
4858 fprintf (outfile, "range list");
4859 break;
4860 case dw_val_class_const:
4861 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
4862 break;
4863 case dw_val_class_unsigned_const:
4864 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
4865 break;
4866 case dw_val_class_const_double:
4867 fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
4868 HOST_WIDE_INT_PRINT_UNSIGNED")",
4869 a->dw_attr_val.v.val_double.high,
4870 a->dw_attr_val.v.val_double.low);
4871 break;
4872 case dw_val_class_vec:
4873 fprintf (outfile, "floating-point or vector constant");
4874 break;
4875 case dw_val_class_flag:
4876 fprintf (outfile, "%u", AT_flag (a));
4877 break;
4878 case dw_val_class_die_ref:
4879 if (AT_ref (a) != NULL)
4881 if (AT_ref (a)->comdat_type_p)
4883 fprintf (outfile, "die -> signature: ");
4884 print_signature (outfile,
4885 AT_ref (a)->die_id.die_type_node->signature);
4887 else if (AT_ref (a)->die_id.die_symbol)
4888 fprintf (outfile, "die -> label: %s",
4889 AT_ref (a)->die_id.die_symbol);
4890 else
4891 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
4892 fprintf (outfile, " (%p)", (void *) AT_ref (a));
4894 else
4895 fprintf (outfile, "die -> <null>");
4896 break;
4897 case dw_val_class_vms_delta:
4898 fprintf (outfile, "delta: @slotcount(%s-%s)",
4899 AT_vms_delta2 (a), AT_vms_delta1 (a));
4900 break;
4901 case dw_val_class_lbl_id:
4902 case dw_val_class_lineptr:
4903 case dw_val_class_macptr:
4904 case dw_val_class_high_pc:
4905 fprintf (outfile, "label: %s", AT_lbl (a));
4906 break;
4907 case dw_val_class_str:
4908 if (AT_string (a) != NULL)
4909 fprintf (outfile, "\"%s\"", AT_string (a));
4910 else
4911 fprintf (outfile, "<null>");
4912 break;
4913 case dw_val_class_file:
4914 fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
4915 AT_file (a)->emitted_number);
4916 break;
4917 case dw_val_class_data8:
4919 int i;
4921 for (i = 0; i < 8; i++)
4922 fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
4923 break;
4925 default:
4926 break;
4929 fprintf (outfile, "\n");
4932 if (die->die_child != NULL)
4934 print_indent += 4;
4935 FOR_EACH_CHILD (die, c, print_die (c, outfile));
4936 print_indent -= 4;
4938 if (print_indent == 0)
4939 fprintf (outfile, "\n");
4942 /* Print the information collected for a given DIE. */
4944 DEBUG_FUNCTION void
4945 debug_dwarf_die (dw_die_ref die)
4947 print_die (die, stderr);
4950 /* Print all DWARF information collected for the compilation unit.
4951 This routine is a debugging aid only. */
4953 DEBUG_FUNCTION void
4954 debug_dwarf (void)
4956 print_indent = 0;
4957 print_die (comp_unit_die (), stderr);
4960 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
4961 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
4962 DIE that marks the start of the DIEs for this include file. */
4964 static dw_die_ref
4965 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
4967 const char *filename = get_AT_string (bincl_die, DW_AT_name);
4968 dw_die_ref new_unit = gen_compile_unit_die (filename);
4970 new_unit->die_sib = old_unit;
4971 return new_unit;
4974 /* Close an include-file CU and reopen the enclosing one. */
4976 static dw_die_ref
4977 pop_compile_unit (dw_die_ref old_unit)
4979 dw_die_ref new_unit = old_unit->die_sib;
4981 old_unit->die_sib = NULL;
4982 return new_unit;
4985 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
4986 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
4988 /* Calculate the checksum of a location expression. */
4990 static inline void
4991 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
4993 int tem;
4995 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
4996 CHECKSUM (tem);
4997 CHECKSUM (loc->dw_loc_oprnd1);
4998 CHECKSUM (loc->dw_loc_oprnd2);
5001 /* Calculate the checksum of an attribute. */
5003 static void
5004 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5006 dw_loc_descr_ref loc;
5007 rtx r;
5009 CHECKSUM (at->dw_attr);
5011 /* We don't care that this was compiled with a different compiler
5012 snapshot; if the output is the same, that's what matters. */
5013 if (at->dw_attr == DW_AT_producer)
5014 return;
5016 switch (AT_class (at))
5018 case dw_val_class_const:
5019 CHECKSUM (at->dw_attr_val.v.val_int);
5020 break;
5021 case dw_val_class_unsigned_const:
5022 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5023 break;
5024 case dw_val_class_const_double:
5025 CHECKSUM (at->dw_attr_val.v.val_double);
5026 break;
5027 case dw_val_class_vec:
5028 CHECKSUM (at->dw_attr_val.v.val_vec);
5029 break;
5030 case dw_val_class_flag:
5031 CHECKSUM (at->dw_attr_val.v.val_flag);
5032 break;
5033 case dw_val_class_str:
5034 CHECKSUM_STRING (AT_string (at));
5035 break;
5037 case dw_val_class_addr:
5038 r = AT_addr (at);
5039 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5040 CHECKSUM_STRING (XSTR (r, 0));
5041 break;
5043 case dw_val_class_offset:
5044 CHECKSUM (at->dw_attr_val.v.val_offset);
5045 break;
5047 case dw_val_class_loc:
5048 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5049 loc_checksum (loc, ctx);
5050 break;
5052 case dw_val_class_die_ref:
5053 die_checksum (AT_ref (at), ctx, mark);
5054 break;
5056 case dw_val_class_fde_ref:
5057 case dw_val_class_vms_delta:
5058 case dw_val_class_lbl_id:
5059 case dw_val_class_lineptr:
5060 case dw_val_class_macptr:
5061 case dw_val_class_high_pc:
5062 break;
5064 case dw_val_class_file:
5065 CHECKSUM_STRING (AT_file (at)->filename);
5066 break;
5068 case dw_val_class_data8:
5069 CHECKSUM (at->dw_attr_val.v.val_data8);
5070 break;
5072 default:
5073 break;
5077 /* Calculate the checksum of a DIE. */
5079 static void
5080 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5082 dw_die_ref c;
5083 dw_attr_ref a;
5084 unsigned ix;
5086 /* To avoid infinite recursion. */
5087 if (die->die_mark)
5089 CHECKSUM (die->die_mark);
5090 return;
5092 die->die_mark = ++(*mark);
5094 CHECKSUM (die->die_tag);
5096 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
5097 attr_checksum (a, ctx, mark);
5099 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5102 #undef CHECKSUM
5103 #undef CHECKSUM_STRING
5105 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
5106 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5107 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5108 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5109 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5110 #define CHECKSUM_ATTR(FOO) \
5111 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5113 /* Calculate the checksum of a number in signed LEB128 format. */
5115 static void
5116 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5118 unsigned char byte;
5119 bool more;
5121 while (1)
5123 byte = (value & 0x7f);
5124 value >>= 7;
5125 more = !((value == 0 && (byte & 0x40) == 0)
5126 || (value == -1 && (byte & 0x40) != 0));
5127 if (more)
5128 byte |= 0x80;
5129 CHECKSUM (byte);
5130 if (!more)
5131 break;
5135 /* Calculate the checksum of a number in unsigned LEB128 format. */
5137 static void
5138 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5140 while (1)
5142 unsigned char byte = (value & 0x7f);
5143 value >>= 7;
5144 if (value != 0)
5145 /* More bytes to follow. */
5146 byte |= 0x80;
5147 CHECKSUM (byte);
5148 if (value == 0)
5149 break;
5153 /* Checksum the context of the DIE. This adds the names of any
5154 surrounding namespaces or structures to the checksum. */
5156 static void
5157 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5159 const char *name;
5160 dw_die_ref spec;
5161 int tag = die->die_tag;
5163 if (tag != DW_TAG_namespace
5164 && tag != DW_TAG_structure_type
5165 && tag != DW_TAG_class_type)
5166 return;
5168 name = get_AT_string (die, DW_AT_name);
5170 spec = get_AT_ref (die, DW_AT_specification);
5171 if (spec != NULL)
5172 die = spec;
5174 if (die->die_parent != NULL)
5175 checksum_die_context (die->die_parent, ctx);
5177 CHECKSUM_ULEB128 ('C');
5178 CHECKSUM_ULEB128 (tag);
5179 if (name != NULL)
5180 CHECKSUM_STRING (name);
5183 /* Calculate the checksum of a location expression. */
5185 static inline void
5186 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5188 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5189 were emitted as a DW_FORM_sdata instead of a location expression. */
5190 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5192 CHECKSUM_ULEB128 (DW_FORM_sdata);
5193 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5194 return;
5197 /* Otherwise, just checksum the raw location expression. */
5198 while (loc != NULL)
5200 CHECKSUM_ULEB128 (loc->dw_loc_opc);
5201 CHECKSUM (loc->dw_loc_oprnd1);
5202 CHECKSUM (loc->dw_loc_oprnd2);
5203 loc = loc->dw_loc_next;
5207 /* Calculate the checksum of an attribute. */
5209 static void
5210 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5211 struct md5_ctx *ctx, int *mark)
5213 dw_loc_descr_ref loc;
5214 rtx r;
5216 if (AT_class (at) == dw_val_class_die_ref)
5218 dw_die_ref target_die = AT_ref (at);
5220 /* For pointer and reference types, we checksum only the (qualified)
5221 name of the target type (if there is a name). For friend entries,
5222 we checksum only the (qualified) name of the target type or function.
5223 This allows the checksum to remain the same whether the target type
5224 is complete or not. */
5225 if ((at->dw_attr == DW_AT_type
5226 && (tag == DW_TAG_pointer_type
5227 || tag == DW_TAG_reference_type
5228 || tag == DW_TAG_rvalue_reference_type
5229 || tag == DW_TAG_ptr_to_member_type))
5230 || (at->dw_attr == DW_AT_friend
5231 && tag == DW_TAG_friend))
5233 dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5235 if (name_attr != NULL)
5237 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5239 if (decl == NULL)
5240 decl = target_die;
5241 CHECKSUM_ULEB128 ('N');
5242 CHECKSUM_ULEB128 (at->dw_attr);
5243 if (decl->die_parent != NULL)
5244 checksum_die_context (decl->die_parent, ctx);
5245 CHECKSUM_ULEB128 ('E');
5246 CHECKSUM_STRING (AT_string (name_attr));
5247 return;
5251 /* For all other references to another DIE, we check to see if the
5252 target DIE has already been visited. If it has, we emit a
5253 backward reference; if not, we descend recursively. */
5254 if (target_die->die_mark > 0)
5256 CHECKSUM_ULEB128 ('R');
5257 CHECKSUM_ULEB128 (at->dw_attr);
5258 CHECKSUM_ULEB128 (target_die->die_mark);
5260 else
5262 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5264 if (decl == NULL)
5265 decl = target_die;
5266 target_die->die_mark = ++(*mark);
5267 CHECKSUM_ULEB128 ('T');
5268 CHECKSUM_ULEB128 (at->dw_attr);
5269 if (decl->die_parent != NULL)
5270 checksum_die_context (decl->die_parent, ctx);
5271 die_checksum_ordered (target_die, ctx, mark);
5273 return;
5276 CHECKSUM_ULEB128 ('A');
5277 CHECKSUM_ULEB128 (at->dw_attr);
5279 switch (AT_class (at))
5281 case dw_val_class_const:
5282 CHECKSUM_ULEB128 (DW_FORM_sdata);
5283 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
5284 break;
5286 case dw_val_class_unsigned_const:
5287 CHECKSUM_ULEB128 (DW_FORM_sdata);
5288 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
5289 break;
5291 case dw_val_class_const_double:
5292 CHECKSUM_ULEB128 (DW_FORM_block);
5293 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
5294 CHECKSUM (at->dw_attr_val.v.val_double);
5295 break;
5297 case dw_val_class_vec:
5298 CHECKSUM_ULEB128 (DW_FORM_block);
5299 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
5300 CHECKSUM (at->dw_attr_val.v.val_vec);
5301 break;
5303 case dw_val_class_flag:
5304 CHECKSUM_ULEB128 (DW_FORM_flag);
5305 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
5306 break;
5308 case dw_val_class_str:
5309 CHECKSUM_ULEB128 (DW_FORM_string);
5310 CHECKSUM_STRING (AT_string (at));
5311 break;
5313 case dw_val_class_addr:
5314 r = AT_addr (at);
5315 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5316 CHECKSUM_ULEB128 (DW_FORM_string);
5317 CHECKSUM_STRING (XSTR (r, 0));
5318 break;
5320 case dw_val_class_offset:
5321 CHECKSUM_ULEB128 (DW_FORM_sdata);
5322 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
5323 break;
5325 case dw_val_class_loc:
5326 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5327 loc_checksum_ordered (loc, ctx);
5328 break;
5330 case dw_val_class_fde_ref:
5331 case dw_val_class_lbl_id:
5332 case dw_val_class_lineptr:
5333 case dw_val_class_macptr:
5334 case dw_val_class_high_pc:
5335 break;
5337 case dw_val_class_file:
5338 CHECKSUM_ULEB128 (DW_FORM_string);
5339 CHECKSUM_STRING (AT_file (at)->filename);
5340 break;
5342 case dw_val_class_data8:
5343 CHECKSUM (at->dw_attr_val.v.val_data8);
5344 break;
5346 default:
5347 break;
5351 struct checksum_attributes
5353 dw_attr_ref at_name;
5354 dw_attr_ref at_type;
5355 dw_attr_ref at_friend;
5356 dw_attr_ref at_accessibility;
5357 dw_attr_ref at_address_class;
5358 dw_attr_ref at_allocated;
5359 dw_attr_ref at_artificial;
5360 dw_attr_ref at_associated;
5361 dw_attr_ref at_binary_scale;
5362 dw_attr_ref at_bit_offset;
5363 dw_attr_ref at_bit_size;
5364 dw_attr_ref at_bit_stride;
5365 dw_attr_ref at_byte_size;
5366 dw_attr_ref at_byte_stride;
5367 dw_attr_ref at_const_value;
5368 dw_attr_ref at_containing_type;
5369 dw_attr_ref at_count;
5370 dw_attr_ref at_data_location;
5371 dw_attr_ref at_data_member_location;
5372 dw_attr_ref at_decimal_scale;
5373 dw_attr_ref at_decimal_sign;
5374 dw_attr_ref at_default_value;
5375 dw_attr_ref at_digit_count;
5376 dw_attr_ref at_discr;
5377 dw_attr_ref at_discr_list;
5378 dw_attr_ref at_discr_value;
5379 dw_attr_ref at_encoding;
5380 dw_attr_ref at_endianity;
5381 dw_attr_ref at_explicit;
5382 dw_attr_ref at_is_optional;
5383 dw_attr_ref at_location;
5384 dw_attr_ref at_lower_bound;
5385 dw_attr_ref at_mutable;
5386 dw_attr_ref at_ordering;
5387 dw_attr_ref at_picture_string;
5388 dw_attr_ref at_prototyped;
5389 dw_attr_ref at_small;
5390 dw_attr_ref at_segment;
5391 dw_attr_ref at_string_length;
5392 dw_attr_ref at_threads_scaled;
5393 dw_attr_ref at_upper_bound;
5394 dw_attr_ref at_use_location;
5395 dw_attr_ref at_use_UTF8;
5396 dw_attr_ref at_variable_parameter;
5397 dw_attr_ref at_virtuality;
5398 dw_attr_ref at_visibility;
5399 dw_attr_ref at_vtable_elem_location;
5402 /* Collect the attributes that we will want to use for the checksum. */
5404 static void
5405 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
5407 dw_attr_ref a;
5408 unsigned ix;
5410 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
5412 switch (a->dw_attr)
5414 case DW_AT_name:
5415 attrs->at_name = a;
5416 break;
5417 case DW_AT_type:
5418 attrs->at_type = a;
5419 break;
5420 case DW_AT_friend:
5421 attrs->at_friend = a;
5422 break;
5423 case DW_AT_accessibility:
5424 attrs->at_accessibility = a;
5425 break;
5426 case DW_AT_address_class:
5427 attrs->at_address_class = a;
5428 break;
5429 case DW_AT_allocated:
5430 attrs->at_allocated = a;
5431 break;
5432 case DW_AT_artificial:
5433 attrs->at_artificial = a;
5434 break;
5435 case DW_AT_associated:
5436 attrs->at_associated = a;
5437 break;
5438 case DW_AT_binary_scale:
5439 attrs->at_binary_scale = a;
5440 break;
5441 case DW_AT_bit_offset:
5442 attrs->at_bit_offset = a;
5443 break;
5444 case DW_AT_bit_size:
5445 attrs->at_bit_size = a;
5446 break;
5447 case DW_AT_bit_stride:
5448 attrs->at_bit_stride = a;
5449 break;
5450 case DW_AT_byte_size:
5451 attrs->at_byte_size = a;
5452 break;
5453 case DW_AT_byte_stride:
5454 attrs->at_byte_stride = a;
5455 break;
5456 case DW_AT_const_value:
5457 attrs->at_const_value = a;
5458 break;
5459 case DW_AT_containing_type:
5460 attrs->at_containing_type = a;
5461 break;
5462 case DW_AT_count:
5463 attrs->at_count = a;
5464 break;
5465 case DW_AT_data_location:
5466 attrs->at_data_location = a;
5467 break;
5468 case DW_AT_data_member_location:
5469 attrs->at_data_member_location = a;
5470 break;
5471 case DW_AT_decimal_scale:
5472 attrs->at_decimal_scale = a;
5473 break;
5474 case DW_AT_decimal_sign:
5475 attrs->at_decimal_sign = a;
5476 break;
5477 case DW_AT_default_value:
5478 attrs->at_default_value = a;
5479 break;
5480 case DW_AT_digit_count:
5481 attrs->at_digit_count = a;
5482 break;
5483 case DW_AT_discr:
5484 attrs->at_discr = a;
5485 break;
5486 case DW_AT_discr_list:
5487 attrs->at_discr_list = a;
5488 break;
5489 case DW_AT_discr_value:
5490 attrs->at_discr_value = a;
5491 break;
5492 case DW_AT_encoding:
5493 attrs->at_encoding = a;
5494 break;
5495 case DW_AT_endianity:
5496 attrs->at_endianity = a;
5497 break;
5498 case DW_AT_explicit:
5499 attrs->at_explicit = a;
5500 break;
5501 case DW_AT_is_optional:
5502 attrs->at_is_optional = a;
5503 break;
5504 case DW_AT_location:
5505 attrs->at_location = a;
5506 break;
5507 case DW_AT_lower_bound:
5508 attrs->at_lower_bound = a;
5509 break;
5510 case DW_AT_mutable:
5511 attrs->at_mutable = a;
5512 break;
5513 case DW_AT_ordering:
5514 attrs->at_ordering = a;
5515 break;
5516 case DW_AT_picture_string:
5517 attrs->at_picture_string = a;
5518 break;
5519 case DW_AT_prototyped:
5520 attrs->at_prototyped = a;
5521 break;
5522 case DW_AT_small:
5523 attrs->at_small = a;
5524 break;
5525 case DW_AT_segment:
5526 attrs->at_segment = a;
5527 break;
5528 case DW_AT_string_length:
5529 attrs->at_string_length = a;
5530 break;
5531 case DW_AT_threads_scaled:
5532 attrs->at_threads_scaled = a;
5533 break;
5534 case DW_AT_upper_bound:
5535 attrs->at_upper_bound = a;
5536 break;
5537 case DW_AT_use_location:
5538 attrs->at_use_location = a;
5539 break;
5540 case DW_AT_use_UTF8:
5541 attrs->at_use_UTF8 = a;
5542 break;
5543 case DW_AT_variable_parameter:
5544 attrs->at_variable_parameter = a;
5545 break;
5546 case DW_AT_virtuality:
5547 attrs->at_virtuality = a;
5548 break;
5549 case DW_AT_visibility:
5550 attrs->at_visibility = a;
5551 break;
5552 case DW_AT_vtable_elem_location:
5553 attrs->at_vtable_elem_location = a;
5554 break;
5555 default:
5556 break;
5561 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
5563 static void
5564 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5566 dw_die_ref c;
5567 dw_die_ref decl;
5568 struct checksum_attributes attrs;
5570 CHECKSUM_ULEB128 ('D');
5571 CHECKSUM_ULEB128 (die->die_tag);
5573 memset (&attrs, 0, sizeof (attrs));
5575 decl = get_AT_ref (die, DW_AT_specification);
5576 if (decl != NULL)
5577 collect_checksum_attributes (&attrs, decl);
5578 collect_checksum_attributes (&attrs, die);
5580 CHECKSUM_ATTR (attrs.at_name);
5581 CHECKSUM_ATTR (attrs.at_accessibility);
5582 CHECKSUM_ATTR (attrs.at_address_class);
5583 CHECKSUM_ATTR (attrs.at_allocated);
5584 CHECKSUM_ATTR (attrs.at_artificial);
5585 CHECKSUM_ATTR (attrs.at_associated);
5586 CHECKSUM_ATTR (attrs.at_binary_scale);
5587 CHECKSUM_ATTR (attrs.at_bit_offset);
5588 CHECKSUM_ATTR (attrs.at_bit_size);
5589 CHECKSUM_ATTR (attrs.at_bit_stride);
5590 CHECKSUM_ATTR (attrs.at_byte_size);
5591 CHECKSUM_ATTR (attrs.at_byte_stride);
5592 CHECKSUM_ATTR (attrs.at_const_value);
5593 CHECKSUM_ATTR (attrs.at_containing_type);
5594 CHECKSUM_ATTR (attrs.at_count);
5595 CHECKSUM_ATTR (attrs.at_data_location);
5596 CHECKSUM_ATTR (attrs.at_data_member_location);
5597 CHECKSUM_ATTR (attrs.at_decimal_scale);
5598 CHECKSUM_ATTR (attrs.at_decimal_sign);
5599 CHECKSUM_ATTR (attrs.at_default_value);
5600 CHECKSUM_ATTR (attrs.at_digit_count);
5601 CHECKSUM_ATTR (attrs.at_discr);
5602 CHECKSUM_ATTR (attrs.at_discr_list);
5603 CHECKSUM_ATTR (attrs.at_discr_value);
5604 CHECKSUM_ATTR (attrs.at_encoding);
5605 CHECKSUM_ATTR (attrs.at_endianity);
5606 CHECKSUM_ATTR (attrs.at_explicit);
5607 CHECKSUM_ATTR (attrs.at_is_optional);
5608 CHECKSUM_ATTR (attrs.at_location);
5609 CHECKSUM_ATTR (attrs.at_lower_bound);
5610 CHECKSUM_ATTR (attrs.at_mutable);
5611 CHECKSUM_ATTR (attrs.at_ordering);
5612 CHECKSUM_ATTR (attrs.at_picture_string);
5613 CHECKSUM_ATTR (attrs.at_prototyped);
5614 CHECKSUM_ATTR (attrs.at_small);
5615 CHECKSUM_ATTR (attrs.at_segment);
5616 CHECKSUM_ATTR (attrs.at_string_length);
5617 CHECKSUM_ATTR (attrs.at_threads_scaled);
5618 CHECKSUM_ATTR (attrs.at_upper_bound);
5619 CHECKSUM_ATTR (attrs.at_use_location);
5620 CHECKSUM_ATTR (attrs.at_use_UTF8);
5621 CHECKSUM_ATTR (attrs.at_variable_parameter);
5622 CHECKSUM_ATTR (attrs.at_virtuality);
5623 CHECKSUM_ATTR (attrs.at_visibility);
5624 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
5625 CHECKSUM_ATTR (attrs.at_type);
5626 CHECKSUM_ATTR (attrs.at_friend);
5628 /* Checksum the child DIEs, except for nested types and member functions. */
5629 c = die->die_child;
5630 if (c) do {
5631 dw_attr_ref name_attr;
5633 c = c->die_sib;
5634 name_attr = get_AT (c, DW_AT_name);
5635 if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
5636 && name_attr != NULL)
5638 CHECKSUM_ULEB128 ('S');
5639 CHECKSUM_ULEB128 (c->die_tag);
5640 CHECKSUM_STRING (AT_string (name_attr));
5642 else
5644 /* Mark this DIE so it gets processed when unmarking. */
5645 if (c->die_mark == 0)
5646 c->die_mark = -1;
5647 die_checksum_ordered (c, ctx, mark);
5649 } while (c != die->die_child);
5651 CHECKSUM_ULEB128 (0);
5654 #undef CHECKSUM
5655 #undef CHECKSUM_STRING
5656 #undef CHECKSUM_ATTR
5657 #undef CHECKSUM_LEB128
5658 #undef CHECKSUM_ULEB128
5660 /* Generate the type signature for DIE. This is computed by generating an
5661 MD5 checksum over the DIE's tag, its relevant attributes, and its
5662 children. Attributes that are references to other DIEs are processed
5663 by recursion, using the MARK field to prevent infinite recursion.
5664 If the DIE is nested inside a namespace or another type, we also
5665 need to include that context in the signature. The lower 64 bits
5666 of the resulting MD5 checksum comprise the signature. */
5668 static void
5669 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
5671 int mark;
5672 const char *name;
5673 unsigned char checksum[16];
5674 struct md5_ctx ctx;
5675 dw_die_ref decl;
5676 dw_die_ref parent;
5678 name = get_AT_string (die, DW_AT_name);
5679 decl = get_AT_ref (die, DW_AT_specification);
5680 parent = get_die_parent (die);
5682 /* First, compute a signature for just the type name (and its surrounding
5683 context, if any. This is stored in the type unit DIE for link-time
5684 ODR (one-definition rule) checking. */
5686 if (is_cxx() && name != NULL)
5688 md5_init_ctx (&ctx);
5690 /* Checksum the names of surrounding namespaces and structures. */
5691 if (parent != NULL)
5692 checksum_die_context (parent, &ctx);
5694 md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
5695 md5_process_bytes (name, strlen (name) + 1, &ctx);
5696 md5_finish_ctx (&ctx, checksum);
5698 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
5701 /* Next, compute the complete type signature. */
5703 md5_init_ctx (&ctx);
5704 mark = 1;
5705 die->die_mark = mark;
5707 /* Checksum the names of surrounding namespaces and structures. */
5708 if (parent != NULL)
5709 checksum_die_context (parent, &ctx);
5711 /* Checksum the DIE and its children. */
5712 die_checksum_ordered (die, &ctx, &mark);
5713 unmark_all_dies (die);
5714 md5_finish_ctx (&ctx, checksum);
5716 /* Store the signature in the type node and link the type DIE and the
5717 type node together. */
5718 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
5719 DWARF_TYPE_SIGNATURE_SIZE);
5720 die->comdat_type_p = true;
5721 die->die_id.die_type_node = type_node;
5722 type_node->type_die = die;
5724 /* If the DIE is a specification, link its declaration to the type node
5725 as well. */
5726 if (decl != NULL)
5728 decl->comdat_type_p = true;
5729 decl->die_id.die_type_node = type_node;
5733 /* Do the location expressions look same? */
5734 static inline int
5735 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
5737 return loc1->dw_loc_opc == loc2->dw_loc_opc
5738 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
5739 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
5742 /* Do the values look the same? */
5743 static int
5744 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
5746 dw_loc_descr_ref loc1, loc2;
5747 rtx r1, r2;
5749 if (v1->val_class != v2->val_class)
5750 return 0;
5752 switch (v1->val_class)
5754 case dw_val_class_const:
5755 return v1->v.val_int == v2->v.val_int;
5756 case dw_val_class_unsigned_const:
5757 return v1->v.val_unsigned == v2->v.val_unsigned;
5758 case dw_val_class_const_double:
5759 return v1->v.val_double.high == v2->v.val_double.high
5760 && v1->v.val_double.low == v2->v.val_double.low;
5761 case dw_val_class_vec:
5762 if (v1->v.val_vec.length != v2->v.val_vec.length
5763 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
5764 return 0;
5765 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
5766 v1->v.val_vec.length * v1->v.val_vec.elt_size))
5767 return 0;
5768 return 1;
5769 case dw_val_class_flag:
5770 return v1->v.val_flag == v2->v.val_flag;
5771 case dw_val_class_str:
5772 return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
5774 case dw_val_class_addr:
5775 r1 = v1->v.val_addr;
5776 r2 = v2->v.val_addr;
5777 if (GET_CODE (r1) != GET_CODE (r2))
5778 return 0;
5779 return !rtx_equal_p (r1, r2);
5781 case dw_val_class_offset:
5782 return v1->v.val_offset == v2->v.val_offset;
5784 case dw_val_class_loc:
5785 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
5786 loc1 && loc2;
5787 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
5788 if (!same_loc_p (loc1, loc2, mark))
5789 return 0;
5790 return !loc1 && !loc2;
5792 case dw_val_class_die_ref:
5793 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
5795 case dw_val_class_fde_ref:
5796 case dw_val_class_vms_delta:
5797 case dw_val_class_lbl_id:
5798 case dw_val_class_lineptr:
5799 case dw_val_class_macptr:
5800 case dw_val_class_high_pc:
5801 return 1;
5803 case dw_val_class_file:
5804 return v1->v.val_file == v2->v.val_file;
5806 case dw_val_class_data8:
5807 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
5809 default:
5810 return 1;
5814 /* Do the attributes look the same? */
5816 static int
5817 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
5819 if (at1->dw_attr != at2->dw_attr)
5820 return 0;
5822 /* We don't care that this was compiled with a different compiler
5823 snapshot; if the output is the same, that's what matters. */
5824 if (at1->dw_attr == DW_AT_producer)
5825 return 1;
5827 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
5830 /* Do the dies look the same? */
5832 static int
5833 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
5835 dw_die_ref c1, c2;
5836 dw_attr_ref a1;
5837 unsigned ix;
5839 /* To avoid infinite recursion. */
5840 if (die1->die_mark)
5841 return die1->die_mark == die2->die_mark;
5842 die1->die_mark = die2->die_mark = ++(*mark);
5844 if (die1->die_tag != die2->die_tag)
5845 return 0;
5847 if (VEC_length (dw_attr_node, die1->die_attr)
5848 != VEC_length (dw_attr_node, die2->die_attr))
5849 return 0;
5851 FOR_EACH_VEC_ELT (dw_attr_node, die1->die_attr, ix, a1)
5852 if (!same_attr_p (a1, &VEC_index (dw_attr_node, die2->die_attr, ix), mark))
5853 return 0;
5855 c1 = die1->die_child;
5856 c2 = die2->die_child;
5857 if (! c1)
5859 if (c2)
5860 return 0;
5862 else
5863 for (;;)
5865 if (!same_die_p (c1, c2, mark))
5866 return 0;
5867 c1 = c1->die_sib;
5868 c2 = c2->die_sib;
5869 if (c1 == die1->die_child)
5871 if (c2 == die2->die_child)
5872 break;
5873 else
5874 return 0;
5878 return 1;
5881 /* Do the dies look the same? Wrapper around same_die_p. */
5883 static int
5884 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
5886 int mark = 0;
5887 int ret = same_die_p (die1, die2, &mark);
5889 unmark_all_dies (die1);
5890 unmark_all_dies (die2);
5892 return ret;
5895 /* The prefix to attach to symbols on DIEs in the current comdat debug
5896 info section. */
5897 static const char *comdat_symbol_id;
5899 /* The index of the current symbol within the current comdat CU. */
5900 static unsigned int comdat_symbol_number;
5902 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
5903 children, and set comdat_symbol_id accordingly. */
5905 static void
5906 compute_section_prefix (dw_die_ref unit_die)
5908 const char *die_name = get_AT_string (unit_die, DW_AT_name);
5909 const char *base = die_name ? lbasename (die_name) : "anonymous";
5910 char *name = XALLOCAVEC (char, strlen (base) + 64);
5911 char *p;
5912 int i, mark;
5913 unsigned char checksum[16];
5914 struct md5_ctx ctx;
5916 /* Compute the checksum of the DIE, then append part of it as hex digits to
5917 the name filename of the unit. */
5919 md5_init_ctx (&ctx);
5920 mark = 0;
5921 die_checksum (unit_die, &ctx, &mark);
5922 unmark_all_dies (unit_die);
5923 md5_finish_ctx (&ctx, checksum);
5925 sprintf (name, "%s.", base);
5926 clean_symbol_name (name);
5928 p = name + strlen (name);
5929 for (i = 0; i < 4; i++)
5931 sprintf (p, "%.2x", checksum[i]);
5932 p += 2;
5935 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
5936 comdat_symbol_number = 0;
5939 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
5941 static int
5942 is_type_die (dw_die_ref die)
5944 switch (die->die_tag)
5946 case DW_TAG_array_type:
5947 case DW_TAG_class_type:
5948 case DW_TAG_interface_type:
5949 case DW_TAG_enumeration_type:
5950 case DW_TAG_pointer_type:
5951 case DW_TAG_reference_type:
5952 case DW_TAG_rvalue_reference_type:
5953 case DW_TAG_string_type:
5954 case DW_TAG_structure_type:
5955 case DW_TAG_subroutine_type:
5956 case DW_TAG_union_type:
5957 case DW_TAG_ptr_to_member_type:
5958 case DW_TAG_set_type:
5959 case DW_TAG_subrange_type:
5960 case DW_TAG_base_type:
5961 case DW_TAG_const_type:
5962 case DW_TAG_file_type:
5963 case DW_TAG_packed_type:
5964 case DW_TAG_volatile_type:
5965 case DW_TAG_typedef:
5966 return 1;
5967 default:
5968 return 0;
5972 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
5973 Basically, we want to choose the bits that are likely to be shared between
5974 compilations (types) and leave out the bits that are specific to individual
5975 compilations (functions). */
5977 static int
5978 is_comdat_die (dw_die_ref c)
5980 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
5981 we do for stabs. The advantage is a greater likelihood of sharing between
5982 objects that don't include headers in the same order (and therefore would
5983 put the base types in a different comdat). jason 8/28/00 */
5985 if (c->die_tag == DW_TAG_base_type)
5986 return 0;
5988 if (c->die_tag == DW_TAG_pointer_type
5989 || c->die_tag == DW_TAG_reference_type
5990 || c->die_tag == DW_TAG_rvalue_reference_type
5991 || c->die_tag == DW_TAG_const_type
5992 || c->die_tag == DW_TAG_volatile_type)
5994 dw_die_ref t = get_AT_ref (c, DW_AT_type);
5996 return t ? is_comdat_die (t) : 0;
5999 return is_type_die (c);
6002 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6003 compilation unit. */
6005 static int
6006 is_symbol_die (dw_die_ref c)
6008 return (is_type_die (c)
6009 || is_declaration_die (c)
6010 || c->die_tag == DW_TAG_namespace
6011 || c->die_tag == DW_TAG_module);
6014 /* Returns true iff C is a compile-unit DIE. */
6016 static inline bool
6017 is_cu_die (dw_die_ref c)
6019 return c && c->die_tag == DW_TAG_compile_unit;
6022 /* Returns true iff C is a unit DIE of some sort. */
6024 static inline bool
6025 is_unit_die (dw_die_ref c)
6027 return c && (c->die_tag == DW_TAG_compile_unit
6028 || c->die_tag == DW_TAG_partial_unit
6029 || c->die_tag == DW_TAG_type_unit);
6032 /* Returns true iff C is a namespace DIE. */
6034 static inline bool
6035 is_namespace_die (dw_die_ref c)
6037 return c && c->die_tag == DW_TAG_namespace;
6040 /* Returns true iff C is a class or structure DIE. */
6042 static inline bool
6043 is_class_die (dw_die_ref c)
6045 return c && (c->die_tag == DW_TAG_class_type
6046 || c->die_tag == DW_TAG_structure_type);
6049 static char *
6050 gen_internal_sym (const char *prefix)
6052 char buf[256];
6054 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6055 return xstrdup (buf);
6058 /* Assign symbols to all worthy DIEs under DIE. */
6060 static void
6061 assign_symbol_names (dw_die_ref die)
6063 dw_die_ref c;
6065 if (is_symbol_die (die) && !die->comdat_type_p)
6067 if (comdat_symbol_id)
6069 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6071 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6072 comdat_symbol_id, comdat_symbol_number++);
6073 die->die_id.die_symbol = xstrdup (p);
6075 else
6076 die->die_id.die_symbol = gen_internal_sym ("LDIE");
6079 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6082 struct cu_hash_table_entry
6084 dw_die_ref cu;
6085 unsigned min_comdat_num, max_comdat_num;
6086 struct cu_hash_table_entry *next;
6089 /* Routines to manipulate hash table of CUs. */
6090 static hashval_t
6091 htab_cu_hash (const void *of)
6093 const struct cu_hash_table_entry *const entry =
6094 (const struct cu_hash_table_entry *) of;
6096 return htab_hash_string (entry->cu->die_id.die_symbol);
6099 static int
6100 htab_cu_eq (const void *of1, const void *of2)
6102 const struct cu_hash_table_entry *const entry1 =
6103 (const struct cu_hash_table_entry *) of1;
6104 const struct die_struct *const entry2 = (const struct die_struct *) of2;
6106 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6109 static void
6110 htab_cu_del (void *what)
6112 struct cu_hash_table_entry *next,
6113 *entry = (struct cu_hash_table_entry *) what;
6115 while (entry)
6117 next = entry->next;
6118 free (entry);
6119 entry = next;
6123 /* Check whether we have already seen this CU and set up SYM_NUM
6124 accordingly. */
6125 static int
6126 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
6128 struct cu_hash_table_entry dummy;
6129 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6131 dummy.max_comdat_num = 0;
6133 slot = (struct cu_hash_table_entry **)
6134 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
6135 INSERT);
6136 entry = *slot;
6138 for (; entry; last = entry, entry = entry->next)
6140 if (same_die_p_wrap (cu, entry->cu))
6141 break;
6144 if (entry)
6146 *sym_num = entry->min_comdat_num;
6147 return 1;
6150 entry = XCNEW (struct cu_hash_table_entry);
6151 entry->cu = cu;
6152 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6153 entry->next = *slot;
6154 *slot = entry;
6156 return 0;
6159 /* Record SYM_NUM to record of CU in HTABLE. */
6160 static void
6161 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
6163 struct cu_hash_table_entry **slot, *entry;
6165 slot = (struct cu_hash_table_entry **)
6166 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
6167 NO_INSERT);
6168 entry = *slot;
6170 entry->max_comdat_num = sym_num;
6173 /* Traverse the DIE (which is always comp_unit_die), and set up
6174 additional compilation units for each of the include files we see
6175 bracketed by BINCL/EINCL. */
6177 static void
6178 break_out_includes (dw_die_ref die)
6180 dw_die_ref c;
6181 dw_die_ref unit = NULL;
6182 limbo_die_node *node, **pnode;
6183 htab_t cu_hash_table;
6185 c = die->die_child;
6186 if (c) do {
6187 dw_die_ref prev = c;
6188 c = c->die_sib;
6189 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6190 || (unit && is_comdat_die (c)))
6192 dw_die_ref next = c->die_sib;
6194 /* This DIE is for a secondary CU; remove it from the main one. */
6195 remove_child_with_prev (c, prev);
6197 if (c->die_tag == DW_TAG_GNU_BINCL)
6198 unit = push_new_compile_unit (unit, c);
6199 else if (c->die_tag == DW_TAG_GNU_EINCL)
6200 unit = pop_compile_unit (unit);
6201 else
6202 add_child_die (unit, c);
6203 c = next;
6204 if (c == die->die_child)
6205 break;
6207 } while (c != die->die_child);
6209 #if 0
6210 /* We can only use this in debugging, since the frontend doesn't check
6211 to make sure that we leave every include file we enter. */
6212 gcc_assert (!unit);
6213 #endif
6215 assign_symbol_names (die);
6216 cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6217 for (node = limbo_die_list, pnode = &limbo_die_list;
6218 node;
6219 node = node->next)
6221 int is_dupl;
6223 compute_section_prefix (node->die);
6224 is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6225 &comdat_symbol_number);
6226 assign_symbol_names (node->die);
6227 if (is_dupl)
6228 *pnode = node->next;
6229 else
6231 pnode = &node->next;
6232 record_comdat_symbol_number (node->die, cu_hash_table,
6233 comdat_symbol_number);
6236 htab_delete (cu_hash_table);
6239 /* Return non-zero if this DIE is a declaration. */
6241 static int
6242 is_declaration_die (dw_die_ref die)
6244 dw_attr_ref a;
6245 unsigned ix;
6247 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
6248 if (a->dw_attr == DW_AT_declaration)
6249 return 1;
6251 return 0;
6254 /* Return non-zero if this DIE is nested inside a subprogram. */
6256 static int
6257 is_nested_in_subprogram (dw_die_ref die)
6259 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
6261 if (decl == NULL)
6262 decl = die;
6263 return local_scope_p (decl);
6266 /* Return non-zero if this DIE contains a defining declaration of a
6267 subprogram. */
6269 static int
6270 contains_subprogram_definition (dw_die_ref die)
6272 dw_die_ref c;
6274 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
6275 return 1;
6276 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition(c)) return 1);
6277 return 0;
6280 /* Return non-zero if this is a type DIE that should be moved to a
6281 COMDAT .debug_types section. */
6283 static int
6284 should_move_die_to_comdat (dw_die_ref die)
6286 switch (die->die_tag)
6288 case DW_TAG_class_type:
6289 case DW_TAG_structure_type:
6290 case DW_TAG_enumeration_type:
6291 case DW_TAG_union_type:
6292 /* Don't move declarations, inlined instances, or types nested in a
6293 subprogram. */
6294 if (is_declaration_die (die)
6295 || get_AT (die, DW_AT_abstract_origin)
6296 || is_nested_in_subprogram (die))
6297 return 0;
6298 /* A type definition should never contain a subprogram definition. */
6299 gcc_assert (!contains_subprogram_definition (die));
6300 return 1;
6301 case DW_TAG_array_type:
6302 case DW_TAG_interface_type:
6303 case DW_TAG_pointer_type:
6304 case DW_TAG_reference_type:
6305 case DW_TAG_rvalue_reference_type:
6306 case DW_TAG_string_type:
6307 case DW_TAG_subroutine_type:
6308 case DW_TAG_ptr_to_member_type:
6309 case DW_TAG_set_type:
6310 case DW_TAG_subrange_type:
6311 case DW_TAG_base_type:
6312 case DW_TAG_const_type:
6313 case DW_TAG_file_type:
6314 case DW_TAG_packed_type:
6315 case DW_TAG_volatile_type:
6316 case DW_TAG_typedef:
6317 default:
6318 return 0;
6322 /* Make a clone of DIE. */
6324 static dw_die_ref
6325 clone_die (dw_die_ref die)
6327 dw_die_ref clone;
6328 dw_attr_ref a;
6329 unsigned ix;
6331 clone = ggc_alloc_cleared_die_node ();
6332 clone->die_tag = die->die_tag;
6334 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
6335 add_dwarf_attr (clone, a);
6337 return clone;
6340 /* Make a clone of the tree rooted at DIE. */
6342 static dw_die_ref
6343 clone_tree (dw_die_ref die)
6345 dw_die_ref c;
6346 dw_die_ref clone = clone_die (die);
6348 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
6350 return clone;
6353 /* Make a clone of DIE as a declaration. */
6355 static dw_die_ref
6356 clone_as_declaration (dw_die_ref die)
6358 dw_die_ref clone;
6359 dw_die_ref decl;
6360 dw_attr_ref a;
6361 unsigned ix;
6363 /* If the DIE is already a declaration, just clone it. */
6364 if (is_declaration_die (die))
6365 return clone_die (die);
6367 /* If the DIE is a specification, just clone its declaration DIE. */
6368 decl = get_AT_ref (die, DW_AT_specification);
6369 if (decl != NULL)
6371 clone = clone_die (decl);
6372 if (die->comdat_type_p)
6373 add_AT_die_ref (clone, DW_AT_signature, die);
6374 return clone;
6377 clone = ggc_alloc_cleared_die_node ();
6378 clone->die_tag = die->die_tag;
6380 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
6382 /* We don't want to copy over all attributes.
6383 For example we don't want DW_AT_byte_size because otherwise we will no
6384 longer have a declaration and GDB will treat it as a definition. */
6386 switch (a->dw_attr)
6388 case DW_AT_artificial:
6389 case DW_AT_containing_type:
6390 case DW_AT_external:
6391 case DW_AT_name:
6392 case DW_AT_type:
6393 case DW_AT_virtuality:
6394 case DW_AT_linkage_name:
6395 case DW_AT_MIPS_linkage_name:
6396 add_dwarf_attr (clone, a);
6397 break;
6398 case DW_AT_byte_size:
6399 default:
6400 break;
6404 if (die->comdat_type_p)
6405 add_AT_die_ref (clone, DW_AT_signature, die);
6407 add_AT_flag (clone, DW_AT_declaration, 1);
6408 return clone;
6411 /* Copy the declaration context to the new type unit DIE. This includes
6412 any surrounding namespace or type declarations. If the DIE has an
6413 AT_specification attribute, it also includes attributes and children
6414 attached to the specification, and returns a pointer to the original
6415 parent of the declaration DIE. Returns NULL otherwise. */
6417 static dw_die_ref
6418 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
6420 dw_die_ref decl;
6421 dw_die_ref new_decl;
6422 dw_die_ref orig_parent = NULL;
6424 decl = get_AT_ref (die, DW_AT_specification);
6425 if (decl == NULL)
6426 decl = die;
6427 else
6429 unsigned ix;
6430 dw_die_ref c;
6431 dw_attr_ref a;
6433 /* The original DIE will be changed to a declaration, and must
6434 be moved to be a child of the original declaration DIE. */
6435 orig_parent = decl->die_parent;
6437 /* Copy the type node pointer from the new DIE to the original
6438 declaration DIE so we can forward references later. */
6439 decl->comdat_type_p = true;
6440 decl->die_id.die_type_node = die->die_id.die_type_node;
6442 remove_AT (die, DW_AT_specification);
6444 FOR_EACH_VEC_ELT (dw_attr_node, decl->die_attr, ix, a)
6446 if (a->dw_attr != DW_AT_name
6447 && a->dw_attr != DW_AT_declaration
6448 && a->dw_attr != DW_AT_external)
6449 add_dwarf_attr (die, a);
6452 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
6455 if (decl->die_parent != NULL
6456 && !is_unit_die (decl->die_parent))
6458 new_decl = copy_ancestor_tree (unit, decl, NULL);
6459 if (new_decl != NULL)
6461 remove_AT (new_decl, DW_AT_signature);
6462 add_AT_specification (die, new_decl);
6466 return orig_parent;
6469 /* Generate the skeleton ancestor tree for the given NODE, then clone
6470 the DIE and add the clone into the tree. */
6472 static void
6473 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
6475 if (node->new_die != NULL)
6476 return;
6478 node->new_die = clone_as_declaration (node->old_die);
6480 if (node->parent != NULL)
6482 generate_skeleton_ancestor_tree (node->parent);
6483 add_child_die (node->parent->new_die, node->new_die);
6487 /* Generate a skeleton tree of DIEs containing any declarations that are
6488 found in the original tree. We traverse the tree looking for declaration
6489 DIEs, and construct the skeleton from the bottom up whenever we find one. */
6491 static void
6492 generate_skeleton_bottom_up (skeleton_chain_node *parent)
6494 skeleton_chain_node node;
6495 dw_die_ref c;
6496 dw_die_ref first;
6497 dw_die_ref prev = NULL;
6498 dw_die_ref next = NULL;
6500 node.parent = parent;
6502 first = c = parent->old_die->die_child;
6503 if (c)
6504 next = c->die_sib;
6505 if (c) do {
6506 if (prev == NULL || prev->die_sib == c)
6507 prev = c;
6508 c = next;
6509 next = (c == first ? NULL : c->die_sib);
6510 node.old_die = c;
6511 node.new_die = NULL;
6512 if (is_declaration_die (c))
6514 /* Clone the existing DIE, move the original to the skeleton
6515 tree (which is in the main CU), and put the clone, with
6516 all the original's children, where the original came from. */
6517 dw_die_ref clone = clone_die (c);
6518 move_all_children (c, clone);
6520 replace_child (c, clone, prev);
6521 generate_skeleton_ancestor_tree (parent);
6522 add_child_die (parent->new_die, c);
6523 node.new_die = c;
6524 c = clone;
6526 generate_skeleton_bottom_up (&node);
6527 } while (next != NULL);
6530 /* Wrapper function for generate_skeleton_bottom_up. */
6532 static dw_die_ref
6533 generate_skeleton (dw_die_ref die)
6535 skeleton_chain_node node;
6537 node.old_die = die;
6538 node.new_die = NULL;
6539 node.parent = NULL;
6541 /* If this type definition is nested inside another type,
6542 always leave at least a declaration in its place. */
6543 if (die->die_parent != NULL && is_type_die (die->die_parent))
6544 node.new_die = clone_as_declaration (die);
6546 generate_skeleton_bottom_up (&node);
6547 return node.new_die;
6550 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
6551 declaration. The original DIE is moved to a new compile unit so that
6552 existing references to it follow it to the new location. If any of the
6553 original DIE's descendants is a declaration, we need to replace the
6554 original DIE with a skeleton tree and move the declarations back into the
6555 skeleton tree. */
6557 static dw_die_ref
6558 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
6559 dw_die_ref prev)
6561 dw_die_ref skeleton, orig_parent;
6563 /* Copy the declaration context to the type unit DIE. If the returned
6564 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
6565 that DIE. */
6566 orig_parent = copy_declaration_context (unit, child);
6568 skeleton = generate_skeleton (child);
6569 if (skeleton == NULL)
6570 remove_child_with_prev (child, prev);
6571 else
6573 skeleton->comdat_type_p = true;
6574 skeleton->die_id.die_type_node = child->die_id.die_type_node;
6576 /* If the original DIE was a specification, we need to put
6577 the skeleton under the parent DIE of the declaration.
6578 This leaves the original declaration in the tree, but
6579 it will be pruned later since there are no longer any
6580 references to it. */
6581 if (orig_parent != NULL)
6583 remove_child_with_prev (child, prev);
6584 add_child_die (orig_parent, skeleton);
6586 else
6587 replace_child (child, skeleton, prev);
6590 return skeleton;
6593 /* Traverse the DIE and set up additional .debug_types sections for each
6594 type worthy of being placed in a COMDAT section. */
6596 static void
6597 break_out_comdat_types (dw_die_ref die)
6599 dw_die_ref c;
6600 dw_die_ref first;
6601 dw_die_ref prev = NULL;
6602 dw_die_ref next = NULL;
6603 dw_die_ref unit = NULL;
6605 first = c = die->die_child;
6606 if (c)
6607 next = c->die_sib;
6608 if (c) do {
6609 if (prev == NULL || prev->die_sib == c)
6610 prev = c;
6611 c = next;
6612 next = (c == first ? NULL : c->die_sib);
6613 if (should_move_die_to_comdat (c))
6615 dw_die_ref replacement;
6616 comdat_type_node_ref type_node;
6618 /* Create a new type unit DIE as the root for the new tree, and
6619 add it to the list of comdat types. */
6620 unit = new_die (DW_TAG_type_unit, NULL, NULL);
6621 add_AT_unsigned (unit, DW_AT_language,
6622 get_AT_unsigned (comp_unit_die (), DW_AT_language));
6623 type_node = ggc_alloc_cleared_comdat_type_node ();
6624 type_node->root_die = unit;
6625 type_node->next = comdat_type_list;
6626 comdat_type_list = type_node;
6628 /* Generate the type signature. */
6629 generate_type_signature (c, type_node);
6631 /* Copy the declaration context, attributes, and children of the
6632 declaration into the new type unit DIE, then remove this DIE
6633 from the main CU (or replace it with a skeleton if necessary). */
6634 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
6635 type_node->skeleton_die = replacement;
6637 /* Break out nested types into their own type units. */
6638 break_out_comdat_types (c);
6640 /* Add the DIE to the new compunit. */
6641 add_child_die (unit, c);
6643 if (replacement != NULL)
6644 c = replacement;
6646 else if (c->die_tag == DW_TAG_namespace
6647 || c->die_tag == DW_TAG_class_type
6648 || c->die_tag == DW_TAG_structure_type
6649 || c->die_tag == DW_TAG_union_type)
6651 /* Look for nested types that can be broken out. */
6652 break_out_comdat_types (c);
6654 } while (next != NULL);
6657 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
6659 struct decl_table_entry
6661 dw_die_ref orig;
6662 dw_die_ref copy;
6665 /* Routines to manipulate hash table of copied declarations. */
6667 static hashval_t
6668 htab_decl_hash (const void *of)
6670 const struct decl_table_entry *const entry =
6671 (const struct decl_table_entry *) of;
6673 return htab_hash_pointer (entry->orig);
6676 static int
6677 htab_decl_eq (const void *of1, const void *of2)
6679 const struct decl_table_entry *const entry1 =
6680 (const struct decl_table_entry *) of1;
6681 const struct die_struct *const entry2 = (const struct die_struct *) of2;
6683 return entry1->orig == entry2;
6686 static void
6687 htab_decl_del (void *what)
6689 struct decl_table_entry *entry = (struct decl_table_entry *) what;
6691 free (entry);
6694 /* Copy DIE and its ancestors, up to, but not including, the compile unit
6695 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
6696 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
6697 to check if the ancestor has already been copied into UNIT. */
6699 static dw_die_ref
6700 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
6702 dw_die_ref parent = die->die_parent;
6703 dw_die_ref new_parent = unit;
6704 dw_die_ref copy;
6705 void **slot = NULL;
6706 struct decl_table_entry *entry = NULL;
6708 if (decl_table)
6710 /* Check if the entry has already been copied to UNIT. */
6711 slot = htab_find_slot_with_hash (decl_table, die,
6712 htab_hash_pointer (die), INSERT);
6713 if (*slot != HTAB_EMPTY_ENTRY)
6715 entry = (struct decl_table_entry *) *slot;
6716 return entry->copy;
6719 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
6720 entry = XCNEW (struct decl_table_entry);
6721 entry->orig = die;
6722 entry->copy = NULL;
6723 *slot = entry;
6726 if (parent != NULL)
6728 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
6729 if (spec != NULL)
6730 parent = spec;
6731 if (!is_unit_die (parent))
6732 new_parent = copy_ancestor_tree (unit, parent, decl_table);
6735 copy = clone_as_declaration (die);
6736 add_child_die (new_parent, copy);
6738 if (decl_table != NULL)
6740 /* Record the pointer to the copy. */
6741 entry->copy = copy;
6744 return copy;
6747 /* Like clone_tree, but additionally enter all the children into
6748 the hash table decl_table. */
6750 static dw_die_ref
6751 clone_tree_hash (dw_die_ref die, htab_t decl_table)
6753 dw_die_ref c;
6754 dw_die_ref clone = clone_die (die);
6755 struct decl_table_entry *entry;
6756 void **slot = htab_find_slot_with_hash (decl_table, die,
6757 htab_hash_pointer (die), INSERT);
6758 /* Assert that DIE isn't in the hash table yet. If it would be there
6759 before, the ancestors would be necessarily there as well, therefore
6760 clone_tree_hash wouldn't be called. */
6761 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
6762 entry = XCNEW (struct decl_table_entry);
6763 entry->orig = die;
6764 entry->copy = clone;
6765 *slot = entry;
6767 FOR_EACH_CHILD (die, c,
6768 add_child_die (clone, clone_tree_hash (c, decl_table)));
6770 return clone;
6773 /* Walk the DIE and its children, looking for references to incomplete
6774 or trivial types that are unmarked (i.e., that are not in the current
6775 type_unit). */
6777 static void
6778 copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
6780 dw_die_ref c;
6781 dw_attr_ref a;
6782 unsigned ix;
6784 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
6786 if (AT_class (a) == dw_val_class_die_ref)
6788 dw_die_ref targ = AT_ref (a);
6789 void **slot;
6790 struct decl_table_entry *entry;
6792 if (targ->die_mark != 0 || targ->comdat_type_p)
6793 continue;
6795 slot = htab_find_slot_with_hash (decl_table, targ,
6796 htab_hash_pointer (targ), INSERT);
6798 if (*slot != HTAB_EMPTY_ENTRY)
6800 /* TARG has already been copied, so we just need to
6801 modify the reference to point to the copy. */
6802 entry = (struct decl_table_entry *) *slot;
6803 a->dw_attr_val.v.val_die_ref.die = entry->copy;
6805 else
6807 dw_die_ref parent = unit;
6808 dw_die_ref copy = clone_die (targ);
6810 /* Record in DECL_TABLE that TARG has been copied.
6811 Need to do this now, before the recursive call,
6812 because DECL_TABLE may be expanded and SLOT
6813 would no longer be a valid pointer. */
6814 entry = XCNEW (struct decl_table_entry);
6815 entry->orig = targ;
6816 entry->copy = copy;
6817 *slot = entry;
6819 FOR_EACH_CHILD (targ, c,
6820 add_child_die (copy,
6821 clone_tree_hash (c, decl_table)));
6823 /* Make sure the cloned tree is marked as part of the
6824 type unit. */
6825 mark_dies (copy);
6827 /* If TARG has surrounding context, copy its ancestor tree
6828 into the new type unit. */
6829 if (targ->die_parent != NULL
6830 && !is_unit_die (targ->die_parent))
6831 parent = copy_ancestor_tree (unit, targ->die_parent,
6832 decl_table);
6834 add_child_die (parent, copy);
6835 a->dw_attr_val.v.val_die_ref.die = copy;
6837 /* Make sure the newly-copied DIE is walked. If it was
6838 installed in a previously-added context, it won't
6839 get visited otherwise. */
6840 if (parent != unit)
6842 /* Find the highest point of the newly-added tree,
6843 mark each node along the way, and walk from there. */
6844 parent->die_mark = 1;
6845 while (parent->die_parent
6846 && parent->die_parent->die_mark == 0)
6848 parent = parent->die_parent;
6849 parent->die_mark = 1;
6851 copy_decls_walk (unit, parent, decl_table);
6857 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
6860 /* Copy declarations for "unworthy" types into the new comdat section.
6861 Incomplete types, modified types, and certain other types aren't broken
6862 out into comdat sections of their own, so they don't have a signature,
6863 and we need to copy the declaration into the same section so that we
6864 don't have an external reference. */
6866 static void
6867 copy_decls_for_unworthy_types (dw_die_ref unit)
6869 htab_t decl_table;
6871 mark_dies (unit);
6872 decl_table = htab_create (10, htab_decl_hash, htab_decl_eq, htab_decl_del);
6873 copy_decls_walk (unit, unit, decl_table);
6874 htab_delete (decl_table);
6875 unmark_dies (unit);
6878 /* Traverse the DIE and add a sibling attribute if it may have the
6879 effect of speeding up access to siblings. To save some space,
6880 avoid generating sibling attributes for DIE's without children. */
6882 static void
6883 add_sibling_attributes (dw_die_ref die)
6885 dw_die_ref c;
6887 if (! die->die_child)
6888 return;
6890 if (die->die_parent && die != die->die_parent->die_child)
6891 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
6893 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
6896 /* Output all location lists for the DIE and its children. */
6898 static void
6899 output_location_lists (dw_die_ref die)
6901 dw_die_ref c;
6902 dw_attr_ref a;
6903 unsigned ix;
6905 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
6906 if (AT_class (a) == dw_val_class_loc_list)
6907 output_loc_list (AT_loc_list (a));
6909 FOR_EACH_CHILD (die, c, output_location_lists (c));
6912 /* We want to limit the number of external references, because they are
6913 larger than local references: a relocation takes multiple words, and
6914 even a sig8 reference is always eight bytes, whereas a local reference
6915 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
6916 So if we encounter multiple external references to the same type DIE, we
6917 make a local typedef stub for it and redirect all references there.
6919 This is the element of the hash table for keeping track of these
6920 references. */
6922 struct external_ref
6924 dw_die_ref type;
6925 dw_die_ref stub;
6926 unsigned n_refs;
6929 /* Hash an external_ref. */
6931 static hashval_t
6932 hash_external_ref (const void *p)
6934 const struct external_ref *r = (const struct external_ref *)p;
6935 return htab_hash_pointer (r->type);
6938 /* Compare external_refs. */
6940 static int
6941 external_ref_eq (const void *p1, const void *p2)
6943 const struct external_ref *r1 = (const struct external_ref *)p1;
6944 const struct external_ref *r2 = (const struct external_ref *)p2;
6945 return r1->type == r2->type;
6948 /* Return a pointer to the external_ref for references to DIE. */
6950 static struct external_ref *
6951 lookup_external_ref (htab_t map, dw_die_ref die)
6953 struct external_ref ref, *ref_p;
6954 void ** slot;
6956 ref.type = die;
6957 slot = htab_find_slot (map, &ref, INSERT);
6958 if (*slot != HTAB_EMPTY_ENTRY)
6959 return (struct external_ref *) *slot;
6961 ref_p = XCNEW (struct external_ref);
6962 ref_p->type = die;
6963 *slot = ref_p;
6964 return ref_p;
6967 /* Subroutine of optimize_external_refs, below.
6969 If we see a type skeleton, record it as our stub. If we see external
6970 references, remember how many we've seen. */
6972 static void
6973 optimize_external_refs_1 (dw_die_ref die, htab_t map)
6975 dw_die_ref c;
6976 dw_attr_ref a;
6977 unsigned ix;
6978 struct external_ref *ref_p;
6980 if (is_type_die (die)
6981 && (c = get_AT_ref (die, DW_AT_signature)))
6983 /* This is a local skeleton; use it for local references. */
6984 ref_p = lookup_external_ref (map, c);
6985 ref_p->stub = die;
6988 /* Scan the DIE references, and remember any that refer to DIEs from
6989 other CUs (i.e. those which are not marked). */
6990 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
6991 if (AT_class (a) == dw_val_class_die_ref
6992 && (c = AT_ref (a))->die_mark == 0
6993 && is_type_die (c))
6995 ref_p = lookup_external_ref (map, c);
6996 ref_p->n_refs++;
6999 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7002 /* htab_traverse callback function for optimize_external_refs, below. SLOT
7003 points to an external_ref, DATA is the CU we're processing. If we don't
7004 already have a local stub, and we have multiple refs, build a stub. */
7006 static int
7007 build_local_stub (void **slot, void *data)
7009 struct external_ref *ref_p = (struct external_ref *)*slot;
7011 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7013 /* We have multiple references to this type, so build a small stub.
7014 Both of these forms are a bit dodgy from the perspective of the
7015 DWARF standard, since technically they should have names. */
7016 dw_die_ref cu = (dw_die_ref) data;
7017 dw_die_ref type = ref_p->type;
7018 dw_die_ref stub = NULL;
7020 if (type->comdat_type_p)
7022 /* If we refer to this type via sig8, use AT_signature. */
7023 stub = new_die (type->die_tag, cu, NULL_TREE);
7024 add_AT_die_ref (stub, DW_AT_signature, type);
7026 else
7028 /* Otherwise, use a typedef with no name. */
7029 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7030 add_AT_die_ref (stub, DW_AT_type, type);
7033 stub->die_mark++;
7034 ref_p->stub = stub;
7036 return 1;
7039 /* DIE is a unit; look through all the DIE references to see if there are
7040 any external references to types, and if so, create local stubs for
7041 them which will be applied in build_abbrev_table. This is useful because
7042 references to local DIEs are smaller. */
7044 static htab_t
7045 optimize_external_refs (dw_die_ref die)
7047 htab_t map = htab_create (10, hash_external_ref, external_ref_eq, free);
7048 optimize_external_refs_1 (die, map);
7049 htab_traverse (map, build_local_stub, die);
7050 return map;
7053 /* The format of each DIE (and its attribute value pairs) is encoded in an
7054 abbreviation table. This routine builds the abbreviation table and assigns
7055 a unique abbreviation id for each abbreviation entry. The children of each
7056 die are visited recursively. */
7058 static void
7059 build_abbrev_table (dw_die_ref die, htab_t extern_map)
7061 unsigned long abbrev_id;
7062 unsigned int n_alloc;
7063 dw_die_ref c;
7064 dw_attr_ref a;
7065 unsigned ix;
7067 /* Scan the DIE references, and replace any that refer to
7068 DIEs from other CUs (i.e. those which are not marked) with
7069 the local stubs we built in optimize_external_refs. */
7070 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7071 if (AT_class (a) == dw_val_class_die_ref
7072 && (c = AT_ref (a))->die_mark == 0)
7074 struct external_ref *ref_p;
7075 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
7077 ref_p = lookup_external_ref (extern_map, c);
7078 if (ref_p->stub && ref_p->stub != die)
7079 change_AT_die_ref (a, ref_p->stub);
7080 else
7081 /* We aren't changing this reference, so mark it external. */
7082 set_AT_ref_external (a, 1);
7085 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7087 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7088 dw_attr_ref die_a, abbrev_a;
7089 unsigned ix;
7090 bool ok = true;
7092 if (abbrev->die_tag != die->die_tag)
7093 continue;
7094 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7095 continue;
7097 if (VEC_length (dw_attr_node, abbrev->die_attr)
7098 != VEC_length (dw_attr_node, die->die_attr))
7099 continue;
7101 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, die_a)
7103 abbrev_a = &VEC_index (dw_attr_node, abbrev->die_attr, ix);
7104 if ((abbrev_a->dw_attr != die_a->dw_attr)
7105 || (value_format (abbrev_a) != value_format (die_a)))
7107 ok = false;
7108 break;
7111 if (ok)
7112 break;
7115 if (abbrev_id >= abbrev_die_table_in_use)
7117 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7119 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7120 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7121 n_alloc);
7123 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7124 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7125 abbrev_die_table_allocated = n_alloc;
7128 ++abbrev_die_table_in_use;
7129 abbrev_die_table[abbrev_id] = die;
7132 die->die_abbrev = abbrev_id;
7133 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
7136 /* Return the power-of-two number of bytes necessary to represent VALUE. */
7138 static int
7139 constant_size (unsigned HOST_WIDE_INT value)
7141 int log;
7143 if (value == 0)
7144 log = 0;
7145 else
7146 log = floor_log2 (value);
7148 log = log / 8;
7149 log = 1 << (floor_log2 (log) + 1);
7151 return log;
7154 /* Return the size of a DIE as it is represented in the
7155 .debug_info section. */
7157 static unsigned long
7158 size_of_die (dw_die_ref die)
7160 unsigned long size = 0;
7161 dw_attr_ref a;
7162 unsigned ix;
7164 size += size_of_uleb128 (die->die_abbrev);
7165 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7167 switch (AT_class (a))
7169 case dw_val_class_addr:
7170 size += DWARF2_ADDR_SIZE;
7171 break;
7172 case dw_val_class_offset:
7173 size += DWARF_OFFSET_SIZE;
7174 break;
7175 case dw_val_class_loc:
7177 unsigned long lsize = size_of_locs (AT_loc (a));
7179 /* Block length. */
7180 if (dwarf_version >= 4)
7181 size += size_of_uleb128 (lsize);
7182 else
7183 size += constant_size (lsize);
7184 size += lsize;
7186 break;
7187 case dw_val_class_loc_list:
7188 size += DWARF_OFFSET_SIZE;
7189 break;
7190 case dw_val_class_range_list:
7191 size += DWARF_OFFSET_SIZE;
7192 break;
7193 case dw_val_class_const:
7194 size += size_of_sleb128 (AT_int (a));
7195 break;
7196 case dw_val_class_unsigned_const:
7198 int csize = constant_size (AT_unsigned (a));
7199 if (dwarf_version == 3
7200 && a->dw_attr == DW_AT_data_member_location
7201 && csize >= 4)
7202 size += size_of_uleb128 (AT_unsigned (a));
7203 else
7204 size += csize;
7206 break;
7207 case dw_val_class_const_double:
7208 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
7209 if (HOST_BITS_PER_WIDE_INT >= 64)
7210 size++; /* block */
7211 break;
7212 case dw_val_class_vec:
7213 size += constant_size (a->dw_attr_val.v.val_vec.length
7214 * a->dw_attr_val.v.val_vec.elt_size)
7215 + a->dw_attr_val.v.val_vec.length
7216 * a->dw_attr_val.v.val_vec.elt_size; /* block */
7217 break;
7218 case dw_val_class_flag:
7219 if (dwarf_version >= 4)
7220 /* Currently all add_AT_flag calls pass in 1 as last argument,
7221 so DW_FORM_flag_present can be used. If that ever changes,
7222 we'll need to use DW_FORM_flag and have some optimization
7223 in build_abbrev_table that will change those to
7224 DW_FORM_flag_present if it is set to 1 in all DIEs using
7225 the same abbrev entry. */
7226 gcc_assert (a->dw_attr_val.v.val_flag == 1);
7227 else
7228 size += 1;
7229 break;
7230 case dw_val_class_die_ref:
7231 if (AT_ref_external (a))
7233 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
7234 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
7235 is sized by target address length, whereas in DWARF3
7236 it's always sized as an offset. */
7237 if (use_debug_types)
7238 size += DWARF_TYPE_SIGNATURE_SIZE;
7239 else if (dwarf_version == 2)
7240 size += DWARF2_ADDR_SIZE;
7241 else
7242 size += DWARF_OFFSET_SIZE;
7244 else
7245 size += DWARF_OFFSET_SIZE;
7246 break;
7247 case dw_val_class_fde_ref:
7248 size += DWARF_OFFSET_SIZE;
7249 break;
7250 case dw_val_class_lbl_id:
7251 size += DWARF2_ADDR_SIZE;
7252 break;
7253 case dw_val_class_lineptr:
7254 case dw_val_class_macptr:
7255 size += DWARF_OFFSET_SIZE;
7256 break;
7257 case dw_val_class_str:
7258 if (AT_string_form (a) == DW_FORM_strp)
7259 size += DWARF_OFFSET_SIZE;
7260 else
7261 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7262 break;
7263 case dw_val_class_file:
7264 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7265 break;
7266 case dw_val_class_data8:
7267 size += 8;
7268 break;
7269 case dw_val_class_vms_delta:
7270 size += DWARF_OFFSET_SIZE;
7271 break;
7272 case dw_val_class_high_pc:
7273 size += DWARF2_ADDR_SIZE;
7274 break;
7275 default:
7276 gcc_unreachable ();
7280 return size;
7283 /* Size the debugging information associated with a given DIE. Visits the
7284 DIE's children recursively. Updates the global variable next_die_offset, on
7285 each time through. Uses the current value of next_die_offset to update the
7286 die_offset field in each DIE. */
7288 static void
7289 calc_die_sizes (dw_die_ref die)
7291 dw_die_ref c;
7293 gcc_assert (die->die_offset == 0
7294 || (unsigned long int) die->die_offset == next_die_offset);
7295 die->die_offset = next_die_offset;
7296 next_die_offset += size_of_die (die);
7298 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
7300 if (die->die_child != NULL)
7301 /* Count the null byte used to terminate sibling lists. */
7302 next_die_offset += 1;
7305 /* Size just the base type children at the start of the CU.
7306 This is needed because build_abbrev needs to size locs
7307 and sizing of type based stack ops needs to know die_offset
7308 values for the base types. */
7310 static void
7311 calc_base_type_die_sizes (void)
7313 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7314 unsigned int i;
7315 dw_die_ref base_type;
7316 #if ENABLE_ASSERT_CHECKING
7317 dw_die_ref prev = comp_unit_die ()->die_child;
7318 #endif
7320 die_offset += size_of_die (comp_unit_die ());
7321 for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
7323 #if ENABLE_ASSERT_CHECKING
7324 gcc_assert (base_type->die_offset == 0
7325 && prev->die_sib == base_type
7326 && base_type->die_child == NULL
7327 && base_type->die_abbrev);
7328 prev = base_type;
7329 #endif
7330 base_type->die_offset = die_offset;
7331 die_offset += size_of_die (base_type);
7335 /* Set the marks for a die and its children. We do this so
7336 that we know whether or not a reference needs to use FORM_ref_addr; only
7337 DIEs in the same CU will be marked. We used to clear out the offset
7338 and use that as the flag, but ran into ordering problems. */
7340 static void
7341 mark_dies (dw_die_ref die)
7343 dw_die_ref c;
7345 gcc_assert (!die->die_mark);
7347 die->die_mark = 1;
7348 FOR_EACH_CHILD (die, c, mark_dies (c));
7351 /* Clear the marks for a die and its children. */
7353 static void
7354 unmark_dies (dw_die_ref die)
7356 dw_die_ref c;
7358 if (! use_debug_types)
7359 gcc_assert (die->die_mark);
7361 die->die_mark = 0;
7362 FOR_EACH_CHILD (die, c, unmark_dies (c));
7365 /* Clear the marks for a die, its children and referred dies. */
7367 static void
7368 unmark_all_dies (dw_die_ref die)
7370 dw_die_ref c;
7371 dw_attr_ref a;
7372 unsigned ix;
7374 if (!die->die_mark)
7375 return;
7376 die->die_mark = 0;
7378 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
7380 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7381 if (AT_class (a) == dw_val_class_die_ref)
7382 unmark_all_dies (AT_ref (a));
7385 /* Return the size of the .debug_pubnames or .debug_pubtypes table
7386 generated for the compilation unit. */
7388 static unsigned long
7389 size_of_pubnames (VEC (pubname_entry, gc) * names)
7391 unsigned long size;
7392 unsigned i;
7393 pubname_ref p;
7395 size = DWARF_PUBNAMES_HEADER_SIZE;
7396 FOR_EACH_VEC_ELT (pubname_entry, names, i, p)
7397 if (names != pubtype_table
7398 || p->die->die_offset != 0
7399 || !flag_eliminate_unused_debug_types)
7400 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
7402 size += DWARF_OFFSET_SIZE;
7403 return size;
7406 /* Return the size of the information in the .debug_aranges section. */
7408 static unsigned long
7409 size_of_aranges (void)
7411 unsigned long size;
7413 size = DWARF_ARANGES_HEADER_SIZE;
7415 /* Count the address/length pair for this compilation unit. */
7416 if (text_section_used)
7417 size += 2 * DWARF2_ADDR_SIZE;
7418 if (cold_text_section_used)
7419 size += 2 * DWARF2_ADDR_SIZE;
7420 if (have_multiple_function_sections)
7422 unsigned fde_idx;
7423 dw_fde_ref fde;
7425 FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, fde_idx, fde)
7427 if (!fde->in_std_section)
7428 size += 2 * DWARF2_ADDR_SIZE;
7429 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
7430 size += 2 * DWARF2_ADDR_SIZE;
7434 /* Count the two zero words used to terminated the address range table. */
7435 size += 2 * DWARF2_ADDR_SIZE;
7436 return size;
7439 /* Select the encoding of an attribute value. */
7441 static enum dwarf_form
7442 value_format (dw_attr_ref a)
7444 switch (a->dw_attr_val.val_class)
7446 case dw_val_class_addr:
7447 /* Only very few attributes allow DW_FORM_addr. */
7448 switch (a->dw_attr)
7450 case DW_AT_low_pc:
7451 case DW_AT_high_pc:
7452 case DW_AT_entry_pc:
7453 case DW_AT_trampoline:
7454 return DW_FORM_addr;
7455 default:
7456 break;
7458 switch (DWARF2_ADDR_SIZE)
7460 case 1:
7461 return DW_FORM_data1;
7462 case 2:
7463 return DW_FORM_data2;
7464 case 4:
7465 return DW_FORM_data4;
7466 case 8:
7467 return DW_FORM_data8;
7468 default:
7469 gcc_unreachable ();
7471 case dw_val_class_range_list:
7472 case dw_val_class_loc_list:
7473 if (dwarf_version >= 4)
7474 return DW_FORM_sec_offset;
7475 /* FALLTHRU */
7476 case dw_val_class_vms_delta:
7477 case dw_val_class_offset:
7478 switch (DWARF_OFFSET_SIZE)
7480 case 4:
7481 return DW_FORM_data4;
7482 case 8:
7483 return DW_FORM_data8;
7484 default:
7485 gcc_unreachable ();
7487 case dw_val_class_loc:
7488 if (dwarf_version >= 4)
7489 return DW_FORM_exprloc;
7490 switch (constant_size (size_of_locs (AT_loc (a))))
7492 case 1:
7493 return DW_FORM_block1;
7494 case 2:
7495 return DW_FORM_block2;
7496 case 4:
7497 return DW_FORM_block4;
7498 default:
7499 gcc_unreachable ();
7501 case dw_val_class_const:
7502 return DW_FORM_sdata;
7503 case dw_val_class_unsigned_const:
7504 switch (constant_size (AT_unsigned (a)))
7506 case 1:
7507 return DW_FORM_data1;
7508 case 2:
7509 return DW_FORM_data2;
7510 case 4:
7511 /* In DWARF3 DW_AT_data_member_location with
7512 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
7513 constant, so we need to use DW_FORM_udata if we need
7514 a large constant. */
7515 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
7516 return DW_FORM_udata;
7517 return DW_FORM_data4;
7518 case 8:
7519 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
7520 return DW_FORM_udata;
7521 return DW_FORM_data8;
7522 default:
7523 gcc_unreachable ();
7525 case dw_val_class_const_double:
7526 switch (HOST_BITS_PER_WIDE_INT)
7528 case 8:
7529 return DW_FORM_data2;
7530 case 16:
7531 return DW_FORM_data4;
7532 case 32:
7533 return DW_FORM_data8;
7534 case 64:
7535 default:
7536 return DW_FORM_block1;
7538 case dw_val_class_vec:
7539 switch (constant_size (a->dw_attr_val.v.val_vec.length
7540 * a->dw_attr_val.v.val_vec.elt_size))
7542 case 1:
7543 return DW_FORM_block1;
7544 case 2:
7545 return DW_FORM_block2;
7546 case 4:
7547 return DW_FORM_block4;
7548 default:
7549 gcc_unreachable ();
7551 case dw_val_class_flag:
7552 if (dwarf_version >= 4)
7554 /* Currently all add_AT_flag calls pass in 1 as last argument,
7555 so DW_FORM_flag_present can be used. If that ever changes,
7556 we'll need to use DW_FORM_flag and have some optimization
7557 in build_abbrev_table that will change those to
7558 DW_FORM_flag_present if it is set to 1 in all DIEs using
7559 the same abbrev entry. */
7560 gcc_assert (a->dw_attr_val.v.val_flag == 1);
7561 return DW_FORM_flag_present;
7563 return DW_FORM_flag;
7564 case dw_val_class_die_ref:
7565 if (AT_ref_external (a))
7566 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
7567 else
7568 return DW_FORM_ref;
7569 case dw_val_class_fde_ref:
7570 return DW_FORM_data;
7571 case dw_val_class_lbl_id:
7572 return DW_FORM_addr;
7573 case dw_val_class_lineptr:
7574 case dw_val_class_macptr:
7575 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
7576 case dw_val_class_str:
7577 return AT_string_form (a);
7578 case dw_val_class_file:
7579 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
7581 case 1:
7582 return DW_FORM_data1;
7583 case 2:
7584 return DW_FORM_data2;
7585 case 4:
7586 return DW_FORM_data4;
7587 default:
7588 gcc_unreachable ();
7591 case dw_val_class_data8:
7592 return DW_FORM_data8;
7594 case dw_val_class_high_pc:
7595 switch (DWARF2_ADDR_SIZE)
7597 case 4:
7598 return DW_FORM_data4;
7599 case 8:
7600 return DW_FORM_data8;
7601 default:
7602 gcc_unreachable ();
7605 default:
7606 gcc_unreachable ();
7610 /* Output the encoding of an attribute value. */
7612 static void
7613 output_value_format (dw_attr_ref a)
7615 enum dwarf_form form = value_format (a);
7617 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
7620 /* Output the .debug_abbrev section which defines the DIE abbreviation
7621 table. */
7623 static void
7624 output_abbrev_section (void)
7626 unsigned long abbrev_id;
7628 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7630 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7631 unsigned ix;
7632 dw_attr_ref a_attr;
7634 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
7635 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
7636 dwarf_tag_name (abbrev->die_tag));
7638 if (abbrev->die_child != NULL)
7639 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
7640 else
7641 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
7643 for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
7644 ix++)
7646 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
7647 dwarf_attr_name (a_attr->dw_attr));
7648 output_value_format (a_attr);
7651 dw2_asm_output_data (1, 0, NULL);
7652 dw2_asm_output_data (1, 0, NULL);
7655 /* Terminate the table. */
7656 dw2_asm_output_data (1, 0, NULL);
7659 /* Output a symbol we can use to refer to this DIE from another CU. */
7661 static inline void
7662 output_die_symbol (dw_die_ref die)
7664 const char *sym = die->die_id.die_symbol;
7666 gcc_assert (!die->comdat_type_p);
7668 if (sym == 0)
7669 return;
7671 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
7672 /* We make these global, not weak; if the target doesn't support
7673 .linkonce, it doesn't support combining the sections, so debugging
7674 will break. */
7675 targetm.asm_out.globalize_label (asm_out_file, sym);
7677 ASM_OUTPUT_LABEL (asm_out_file, sym);
7680 /* Return a new location list, given the begin and end range, and the
7681 expression. */
7683 static inline dw_loc_list_ref
7684 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
7685 const char *section)
7687 dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
7689 retlist->begin = begin;
7690 retlist->end = end;
7691 retlist->expr = expr;
7692 retlist->section = section;
7694 return retlist;
7697 /* Generate a new internal symbol for this location list node, if it
7698 hasn't got one yet. */
7700 static inline void
7701 gen_llsym (dw_loc_list_ref list)
7703 gcc_assert (!list->ll_symbol);
7704 list->ll_symbol = gen_internal_sym ("LLST");
7707 /* Output the location list given to us. */
7709 static void
7710 output_loc_list (dw_loc_list_ref list_head)
7712 dw_loc_list_ref curr = list_head;
7714 if (list_head->emitted)
7715 return;
7716 list_head->emitted = true;
7718 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
7720 /* Walk the location list, and output each range + expression. */
7721 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
7723 unsigned long size;
7724 /* Don't output an entry that starts and ends at the same address. */
7725 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
7726 continue;
7727 size = size_of_locs (curr->expr);
7728 /* If the expression is too large, drop it on the floor. We could
7729 perhaps put it into DW_TAG_dwarf_procedure and refer to that
7730 in the expression, but >= 64KB expressions for a single value
7731 in a single range are unlikely very useful. */
7732 if (size > 0xffff)
7733 continue;
7734 if (!have_multiple_function_sections)
7736 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
7737 "Location list begin address (%s)",
7738 list_head->ll_symbol);
7739 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
7740 "Location list end address (%s)",
7741 list_head->ll_symbol);
7743 else
7745 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
7746 "Location list begin address (%s)",
7747 list_head->ll_symbol);
7748 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
7749 "Location list end address (%s)",
7750 list_head->ll_symbol);
7753 /* Output the block length for this list of location operations. */
7754 gcc_assert (size <= 0xffff);
7755 dw2_asm_output_data (2, size, "%s", "Location expression size");
7757 output_loc_sequence (curr->expr, -1);
7760 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7761 "Location list terminator begin (%s)",
7762 list_head->ll_symbol);
7763 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7764 "Location list terminator end (%s)",
7765 list_head->ll_symbol);
7768 /* Output a type signature. */
7770 static inline void
7771 output_signature (const char *sig, const char *name)
7773 int i;
7775 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
7776 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
7779 /* Output the DIE and its attributes. Called recursively to generate
7780 the definitions of each child DIE. */
7782 static void
7783 output_die (dw_die_ref die)
7785 dw_attr_ref a;
7786 dw_die_ref c;
7787 unsigned long size;
7788 unsigned ix;
7790 /* If someone in another CU might refer to us, set up a symbol for
7791 them to point to. */
7792 if (! die->comdat_type_p && die->die_id.die_symbol)
7793 output_die_symbol (die);
7795 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
7796 (unsigned long)die->die_offset,
7797 dwarf_tag_name (die->die_tag));
7799 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7801 const char *name = dwarf_attr_name (a->dw_attr);
7803 switch (AT_class (a))
7805 case dw_val_class_addr:
7806 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
7807 break;
7809 case dw_val_class_offset:
7810 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
7811 "%s", name);
7812 break;
7814 case dw_val_class_range_list:
7816 char *p = strchr (ranges_section_label, '\0');
7818 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
7819 a->dw_attr_val.v.val_offset);
7820 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
7821 debug_ranges_section, "%s", name);
7822 *p = '\0';
7824 break;
7826 case dw_val_class_loc:
7827 size = size_of_locs (AT_loc (a));
7829 /* Output the block length for this list of location operations. */
7830 if (dwarf_version >= 4)
7831 dw2_asm_output_data_uleb128 (size, "%s", name);
7832 else
7833 dw2_asm_output_data (constant_size (size), size, "%s", name);
7835 output_loc_sequence (AT_loc (a), -1);
7836 break;
7838 case dw_val_class_const:
7839 /* ??? It would be slightly more efficient to use a scheme like is
7840 used for unsigned constants below, but gdb 4.x does not sign
7841 extend. Gdb 5.x does sign extend. */
7842 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
7843 break;
7845 case dw_val_class_unsigned_const:
7847 int csize = constant_size (AT_unsigned (a));
7848 if (dwarf_version == 3
7849 && a->dw_attr == DW_AT_data_member_location
7850 && csize >= 4)
7851 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
7852 else
7853 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
7855 break;
7857 case dw_val_class_const_double:
7859 unsigned HOST_WIDE_INT first, second;
7861 if (HOST_BITS_PER_WIDE_INT >= 64)
7862 dw2_asm_output_data (1,
7863 HOST_BITS_PER_DOUBLE_INT
7864 / HOST_BITS_PER_CHAR,
7865 NULL);
7867 if (WORDS_BIG_ENDIAN)
7869 first = a->dw_attr_val.v.val_double.high;
7870 second = a->dw_attr_val.v.val_double.low;
7872 else
7874 first = a->dw_attr_val.v.val_double.low;
7875 second = a->dw_attr_val.v.val_double.high;
7878 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
7879 first, name);
7880 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
7881 second, NULL);
7883 break;
7885 case dw_val_class_vec:
7887 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
7888 unsigned int len = a->dw_attr_val.v.val_vec.length;
7889 unsigned int i;
7890 unsigned char *p;
7892 dw2_asm_output_data (constant_size (len * elt_size),
7893 len * elt_size, "%s", name);
7894 if (elt_size > sizeof (HOST_WIDE_INT))
7896 elt_size /= 2;
7897 len *= 2;
7899 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
7900 i < len;
7901 i++, p += elt_size)
7902 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
7903 "fp or vector constant word %u", i);
7904 break;
7907 case dw_val_class_flag:
7908 if (dwarf_version >= 4)
7910 /* Currently all add_AT_flag calls pass in 1 as last argument,
7911 so DW_FORM_flag_present can be used. If that ever changes,
7912 we'll need to use DW_FORM_flag and have some optimization
7913 in build_abbrev_table that will change those to
7914 DW_FORM_flag_present if it is set to 1 in all DIEs using
7915 the same abbrev entry. */
7916 gcc_assert (AT_flag (a) == 1);
7917 if (flag_debug_asm)
7918 fprintf (asm_out_file, "\t\t\t%s %s\n",
7919 ASM_COMMENT_START, name);
7920 break;
7922 dw2_asm_output_data (1, AT_flag (a), "%s", name);
7923 break;
7925 case dw_val_class_loc_list:
7927 char *sym = AT_loc_list (a)->ll_symbol;
7929 gcc_assert (sym);
7930 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
7931 "%s", name);
7933 break;
7935 case dw_val_class_die_ref:
7936 if (AT_ref_external (a))
7938 if (AT_ref (a)->comdat_type_p)
7940 comdat_type_node_ref type_node =
7941 AT_ref (a)->die_id.die_type_node;
7943 gcc_assert (type_node);
7944 output_signature (type_node->signature, name);
7946 else
7948 const char *sym = AT_ref (a)->die_id.die_symbol;
7949 int size;
7951 gcc_assert (sym);
7952 /* In DWARF2, DW_FORM_ref_addr is sized by target address
7953 length, whereas in DWARF3 it's always sized as an
7954 offset. */
7955 if (dwarf_version == 2)
7956 size = DWARF2_ADDR_SIZE;
7957 else
7958 size = DWARF_OFFSET_SIZE;
7959 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
7960 name);
7963 else
7965 gcc_assert (AT_ref (a)->die_offset);
7966 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
7967 "%s", name);
7969 break;
7971 case dw_val_class_fde_ref:
7973 char l1[20];
7975 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
7976 a->dw_attr_val.v.val_fde_index * 2);
7977 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
7978 "%s", name);
7980 break;
7982 case dw_val_class_vms_delta:
7983 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
7984 AT_vms_delta2 (a), AT_vms_delta1 (a),
7985 "%s", name);
7986 break;
7988 case dw_val_class_lbl_id:
7989 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
7990 break;
7992 case dw_val_class_lineptr:
7993 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7994 debug_line_section, "%s", name);
7995 break;
7997 case dw_val_class_macptr:
7998 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7999 debug_macinfo_section, "%s", name);
8000 break;
8002 case dw_val_class_str:
8003 if (AT_string_form (a) == DW_FORM_strp)
8004 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8005 a->dw_attr_val.v.val_str->label,
8006 debug_str_section,
8007 "%s: \"%s\"", name, AT_string (a));
8008 else
8009 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8010 break;
8012 case dw_val_class_file:
8014 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8016 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8017 a->dw_attr_val.v.val_file->filename);
8018 break;
8021 case dw_val_class_data8:
8023 int i;
8025 for (i = 0; i < 8; i++)
8026 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
8027 i == 0 ? "%s" : NULL, name);
8028 break;
8031 case dw_val_class_high_pc:
8032 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
8033 get_AT_low_pc (die), "DW_AT_high_pc");
8034 break;
8036 default:
8037 gcc_unreachable ();
8041 FOR_EACH_CHILD (die, c, output_die (c));
8043 /* Add null byte to terminate sibling list. */
8044 if (die->die_child != NULL)
8045 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
8046 (unsigned long) die->die_offset);
8049 /* Output the compilation unit that appears at the beginning of the
8050 .debug_info section, and precedes the DIE descriptions. */
8052 static void
8053 output_compilation_unit_header (void)
8055 int ver = dwarf_version;
8057 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8058 dw2_asm_output_data (4, 0xffffffff,
8059 "Initial length escape value indicating 64-bit DWARF extension");
8060 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8061 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8062 "Length of Compilation Unit Info");
8063 dw2_asm_output_data (2, ver, "DWARF version number");
8064 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8065 debug_abbrev_section,
8066 "Offset Into Abbrev. Section");
8067 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8070 /* Output the compilation unit DIE and its children. */
8072 static void
8073 output_comp_unit (dw_die_ref die, int output_if_empty)
8075 const char *secname, *oldsym;
8076 char *tmp;
8077 htab_t extern_map;
8079 /* Unless we are outputting main CU, we may throw away empty ones. */
8080 if (!output_if_empty && die->die_child == NULL)
8081 return;
8083 /* Even if there are no children of this DIE, we must output the information
8084 about the compilation unit. Otherwise, on an empty translation unit, we
8085 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
8086 will then complain when examining the file. First mark all the DIEs in
8087 this CU so we know which get local refs. */
8088 mark_dies (die);
8090 extern_map = optimize_external_refs (die);
8092 build_abbrev_table (die, extern_map);
8094 htab_delete (extern_map);
8096 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8097 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8098 calc_die_sizes (die);
8100 oldsym = die->die_id.die_symbol;
8101 if (oldsym)
8103 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8105 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8106 secname = tmp;
8107 die->die_id.die_symbol = NULL;
8108 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8110 else
8112 switch_to_section (debug_info_section);
8113 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
8114 info_section_emitted = true;
8117 /* Output debugging information. */
8118 output_compilation_unit_header ();
8119 output_die (die);
8121 /* Leave the marks on the main CU, so we can check them in
8122 output_pubnames. */
8123 if (oldsym)
8125 unmark_dies (die);
8126 die->die_id.die_symbol = oldsym;
8130 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
8131 and .debug_pubtypes. This is configured per-target, but can be
8132 overridden by the -gpubnames or -gno-pubnames options. */
8134 static inline bool
8135 want_pubnames (void)
8137 return (debug_generate_pub_sections != -1
8138 ? debug_generate_pub_sections
8139 : targetm.want_debug_pub_sections);
8142 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
8144 static void
8145 add_AT_pubnames (dw_die_ref die)
8147 if (want_pubnames ())
8148 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
8151 /* Output a comdat type unit DIE and its children. */
8153 static void
8154 output_comdat_type_unit (comdat_type_node *node)
8156 const char *secname;
8157 char *tmp;
8158 int i;
8159 #if defined (OBJECT_FORMAT_ELF)
8160 tree comdat_key;
8161 #endif
8162 htab_t extern_map;
8164 /* First mark all the DIEs in this CU so we know which get local refs. */
8165 mark_dies (node->root_die);
8167 extern_map = optimize_external_refs (node->root_die);
8169 build_abbrev_table (node->root_die, extern_map);
8171 htab_delete (extern_map);
8173 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8174 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
8175 calc_die_sizes (node->root_die);
8177 #if defined (OBJECT_FORMAT_ELF)
8178 secname = ".debug_types";
8179 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
8180 sprintf (tmp, "wt.");
8181 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8182 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
8183 comdat_key = get_identifier (tmp);
8184 targetm.asm_out.named_section (secname,
8185 SECTION_DEBUG | SECTION_LINKONCE,
8186 comdat_key);
8187 #else
8188 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
8189 sprintf (tmp, ".gnu.linkonce.wt.");
8190 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8191 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
8192 secname = tmp;
8193 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8194 #endif
8196 /* Output debugging information. */
8197 output_compilation_unit_header ();
8198 output_signature (node->signature, "Type Signature");
8199 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
8200 "Offset to Type DIE");
8201 output_die (node->root_die);
8203 unmark_dies (node->root_die);
8206 /* Return the DWARF2/3 pubname associated with a decl. */
8208 static const char *
8209 dwarf2_name (tree decl, int scope)
8211 if (DECL_NAMELESS (decl))
8212 return NULL;
8213 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
8216 /* Add a new entry to .debug_pubnames if appropriate. */
8218 static void
8219 add_pubname_string (const char *str, dw_die_ref die)
8221 pubname_entry e;
8223 e.die = die;
8224 e.name = xstrdup (str);
8225 VEC_safe_push (pubname_entry, gc, pubname_table, e);
8228 static void
8229 add_pubname (tree decl, dw_die_ref die)
8231 if (!want_pubnames ())
8232 return;
8234 /* Don't add items to the table when we expect that the consumer will have
8235 just read the enclosing die. For example, if the consumer is looking at a
8236 class_member, it will either be inside the class already, or will have just
8237 looked up the class to find the member. Either way, searching the class is
8238 faster than searching the index. */
8239 if ((TREE_PUBLIC (decl) && !is_class_die (die->die_parent))
8240 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
8242 const char *name = dwarf2_name (decl, 1);
8244 if (name)
8245 add_pubname_string (name, die);
8249 /* Add an enumerator to the pubnames section. */
8251 static void
8252 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
8254 pubname_entry e;
8256 gcc_assert (scope_name);
8257 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
8258 e.die = die;
8259 VEC_safe_push (pubname_entry, gc, pubname_table, e);
8262 /* Add a new entry to .debug_pubtypes if appropriate. */
8264 static void
8265 add_pubtype (tree decl, dw_die_ref die)
8267 pubname_entry e;
8269 if (!want_pubnames ())
8270 return;
8272 if ((TREE_PUBLIC (decl)
8273 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
8274 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
8276 tree scope = NULL;
8277 const char *scope_name = "";
8278 const char *sep = is_cxx () ? "::" : ".";
8279 const char *name;
8281 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
8282 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
8284 scope_name = lang_hooks.dwarf_name (scope, 1);
8285 if (scope_name != NULL && scope_name[0] != '\0')
8286 scope_name = concat (scope_name, sep, NULL);
8287 else
8288 scope_name = "";
8291 if (TYPE_P (decl))
8292 name = type_tag (decl);
8293 else
8294 name = lang_hooks.dwarf_name (decl, 1);
8296 /* If we don't have a name for the type, there's no point in adding
8297 it to the table. */
8298 if (name != NULL && name[0] != '\0')
8300 e.die = die;
8301 e.name = concat (scope_name, name, NULL);
8302 VEC_safe_push (pubname_entry, gc, pubtype_table, e);
8305 /* Although it might be more consistent to add the pubinfo for the
8306 enumerators as their dies are created, they should only be added if the
8307 enum type meets the criteria above. So rather than re-check the parent
8308 enum type whenever an enumerator die is created, just output them all
8309 here. This isn't protected by the name conditional because anonymous
8310 enums don't have names. */
8311 if (die->die_tag == DW_TAG_enumeration_type)
8313 dw_die_ref c;
8315 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
8320 /* Output the public names table used to speed up access to externally
8321 visible names; or the public types table used to find type definitions. */
8323 static void
8324 output_pubnames (VEC (pubname_entry, gc) * names)
8326 unsigned i;
8327 unsigned long pubnames_length = size_of_pubnames (names);
8328 pubname_ref pub;
8330 if (!want_pubnames () || !info_section_emitted)
8331 return;
8332 if (names == pubname_table)
8333 switch_to_section (debug_pubnames_section);
8334 else
8335 switch_to_section (debug_pubtypes_section);
8336 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8337 dw2_asm_output_data (4, 0xffffffff,
8338 "Initial length escape value indicating 64-bit DWARF extension");
8339 if (names == pubname_table)
8340 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8341 "Length of Public Names Info");
8342 else
8343 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8344 "Length of Public Type Names Info");
8345 /* Version number for pubnames/pubtypes is still 2, even in DWARF3. */
8346 dw2_asm_output_data (2, 2, "DWARF Version");
8347 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8348 debug_info_section,
8349 "Offset of Compilation Unit Info");
8350 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
8351 "Compilation Unit Length");
8353 FOR_EACH_VEC_ELT (pubname_entry, names, i, pub)
8355 /* Enumerator names are part of the pubname table, but the parent
8356 DW_TAG_enumeration_type die may have been pruned. Don't output
8357 them if that is the case. */
8358 if (pub->die->die_tag == DW_TAG_enumerator && !pub->die->die_mark)
8359 continue;
8361 /* We shouldn't see pubnames for DIEs outside of the main CU. */
8362 if (names == pubname_table)
8363 gcc_assert (pub->die->die_mark);
8365 if (names != pubtype_table
8366 || pub->die->die_offset != 0
8367 || !flag_eliminate_unused_debug_types)
8369 dw_offset die_offset = pub->die->die_offset;
8371 /* If we're putting types in their own .debug_types sections,
8372 the .debug_pubtypes table will still point to the compile
8373 unit (not the type unit), so we want to use the offset of
8374 the skeleton DIE (if there is one). */
8375 if (pub->die->comdat_type_p && names == pubtype_table)
8377 comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
8379 if (type_node != NULL)
8380 die_offset = (type_node->skeleton_die != NULL
8381 ? type_node->skeleton_die->die_offset
8382 : 0);
8385 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
8387 dw2_asm_output_nstring (pub->name, -1, "external name");
8391 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
8394 /* Output the information that goes into the .debug_aranges table.
8395 Namely, define the beginning and ending address range of the
8396 text section generated for this compilation unit. */
8398 static void
8399 output_aranges (unsigned long aranges_length)
8401 unsigned i;
8403 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8404 dw2_asm_output_data (4, 0xffffffff,
8405 "Initial length escape value indicating 64-bit DWARF extension");
8406 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
8407 "Length of Address Ranges Info");
8408 /* Version number for aranges is still 2, even in DWARF3. */
8409 dw2_asm_output_data (2, 2, "DWARF Version");
8410 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8411 debug_info_section,
8412 "Offset of Compilation Unit Info");
8413 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
8414 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
8416 /* We need to align to twice the pointer size here. */
8417 if (DWARF_ARANGES_PAD_SIZE)
8419 /* Pad using a 2 byte words so that padding is correct for any
8420 pointer size. */
8421 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
8422 2 * DWARF2_ADDR_SIZE);
8423 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
8424 dw2_asm_output_data (2, 0, NULL);
8427 /* It is necessary not to output these entries if the sections were
8428 not used; if the sections were not used, the length will be 0 and
8429 the address may end up as 0 if the section is discarded by ld
8430 --gc-sections, leaving an invalid (0, 0) entry that can be
8431 confused with the terminator. */
8432 if (text_section_used)
8434 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
8435 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
8436 text_section_label, "Length");
8438 if (cold_text_section_used)
8440 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
8441 "Address");
8442 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
8443 cold_text_section_label, "Length");
8446 if (have_multiple_function_sections)
8448 unsigned fde_idx;
8449 dw_fde_ref fde;
8451 FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, fde_idx, fde)
8453 if (DECL_IGNORED_P (fde->decl))
8454 continue;
8455 if (!fde->in_std_section)
8457 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
8458 "Address");
8459 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
8460 fde->dw_fde_begin, "Length");
8462 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8464 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
8465 "Address");
8466 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
8467 fde->dw_fde_second_begin, "Length");
8472 /* Output the terminator words. */
8473 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8474 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8477 /* Add a new entry to .debug_ranges. Return the offset at which it
8478 was placed. */
8480 static unsigned int
8481 add_ranges_num (int num)
8483 unsigned int in_use = ranges_table_in_use;
8485 if (in_use == ranges_table_allocated)
8487 ranges_table_allocated += RANGES_TABLE_INCREMENT;
8488 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
8489 ranges_table_allocated);
8490 memset (ranges_table + ranges_table_in_use, 0,
8491 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
8494 ranges_table[in_use].num = num;
8495 ranges_table_in_use = in_use + 1;
8497 return in_use * 2 * DWARF2_ADDR_SIZE;
8500 /* Add a new entry to .debug_ranges corresponding to a block, or a
8501 range terminator if BLOCK is NULL. */
8503 static unsigned int
8504 add_ranges (const_tree block)
8506 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
8509 /* Add a new entry to .debug_ranges corresponding to a pair of
8510 labels. */
8512 static void
8513 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
8514 bool *added)
8516 unsigned int in_use = ranges_by_label_in_use;
8517 unsigned int offset;
8519 if (in_use == ranges_by_label_allocated)
8521 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
8522 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
8523 ranges_by_label,
8524 ranges_by_label_allocated);
8525 memset (ranges_by_label + ranges_by_label_in_use, 0,
8526 RANGES_TABLE_INCREMENT
8527 * sizeof (struct dw_ranges_by_label_struct));
8530 ranges_by_label[in_use].begin = begin;
8531 ranges_by_label[in_use].end = end;
8532 ranges_by_label_in_use = in_use + 1;
8534 offset = add_ranges_num (-(int)in_use - 1);
8535 if (!*added)
8537 add_AT_range_list (die, DW_AT_ranges, offset);
8538 *added = true;
8542 static void
8543 output_ranges (void)
8545 unsigned i;
8546 static const char *const start_fmt = "Offset %#x";
8547 const char *fmt = start_fmt;
8549 for (i = 0; i < ranges_table_in_use; i++)
8551 int block_num = ranges_table[i].num;
8553 if (block_num > 0)
8555 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
8556 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
8558 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
8559 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
8561 /* If all code is in the text section, then the compilation
8562 unit base address defaults to DW_AT_low_pc, which is the
8563 base of the text section. */
8564 if (!have_multiple_function_sections)
8566 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
8567 text_section_label,
8568 fmt, i * 2 * DWARF2_ADDR_SIZE);
8569 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
8570 text_section_label, NULL);
8573 /* Otherwise, the compilation unit base address is zero,
8574 which allows us to use absolute addresses, and not worry
8575 about whether the target supports cross-section
8576 arithmetic. */
8577 else
8579 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
8580 fmt, i * 2 * DWARF2_ADDR_SIZE);
8581 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
8584 fmt = NULL;
8587 /* Negative block_num stands for an index into ranges_by_label. */
8588 else if (block_num < 0)
8590 int lab_idx = - block_num - 1;
8592 if (!have_multiple_function_sections)
8594 gcc_unreachable ();
8595 #if 0
8596 /* If we ever use add_ranges_by_labels () for a single
8597 function section, all we have to do is to take out
8598 the #if 0 above. */
8599 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8600 ranges_by_label[lab_idx].begin,
8601 text_section_label,
8602 fmt, i * 2 * DWARF2_ADDR_SIZE);
8603 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8604 ranges_by_label[lab_idx].end,
8605 text_section_label, NULL);
8606 #endif
8608 else
8610 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
8611 ranges_by_label[lab_idx].begin,
8612 fmt, i * 2 * DWARF2_ADDR_SIZE);
8613 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
8614 ranges_by_label[lab_idx].end,
8615 NULL);
8618 else
8620 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8621 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8622 fmt = start_fmt;
8627 /* Data structure containing information about input files. */
8628 struct file_info
8630 const char *path; /* Complete file name. */
8631 const char *fname; /* File name part. */
8632 int length; /* Length of entire string. */
8633 struct dwarf_file_data * file_idx; /* Index in input file table. */
8634 int dir_idx; /* Index in directory table. */
8637 /* Data structure containing information about directories with source
8638 files. */
8639 struct dir_info
8641 const char *path; /* Path including directory name. */
8642 int length; /* Path length. */
8643 int prefix; /* Index of directory entry which is a prefix. */
8644 int count; /* Number of files in this directory. */
8645 int dir_idx; /* Index of directory used as base. */
8648 /* Callback function for file_info comparison. We sort by looking at
8649 the directories in the path. */
8651 static int
8652 file_info_cmp (const void *p1, const void *p2)
8654 const struct file_info *const s1 = (const struct file_info *) p1;
8655 const struct file_info *const s2 = (const struct file_info *) p2;
8656 const unsigned char *cp1;
8657 const unsigned char *cp2;
8659 /* Take care of file names without directories. We need to make sure that
8660 we return consistent values to qsort since some will get confused if
8661 we return the same value when identical operands are passed in opposite
8662 orders. So if neither has a directory, return 0 and otherwise return
8663 1 or -1 depending on which one has the directory. */
8664 if ((s1->path == s1->fname || s2->path == s2->fname))
8665 return (s2->path == s2->fname) - (s1->path == s1->fname);
8667 cp1 = (const unsigned char *) s1->path;
8668 cp2 = (const unsigned char *) s2->path;
8670 while (1)
8672 ++cp1;
8673 ++cp2;
8674 /* Reached the end of the first path? If so, handle like above. */
8675 if ((cp1 == (const unsigned char *) s1->fname)
8676 || (cp2 == (const unsigned char *) s2->fname))
8677 return ((cp2 == (const unsigned char *) s2->fname)
8678 - (cp1 == (const unsigned char *) s1->fname));
8680 /* Character of current path component the same? */
8681 else if (*cp1 != *cp2)
8682 return *cp1 - *cp2;
8686 struct file_name_acquire_data
8688 struct file_info *files;
8689 int used_files;
8690 int max_files;
8693 /* Traversal function for the hash table. */
8695 static int
8696 file_name_acquire (void ** slot, void *data)
8698 struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
8699 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
8700 struct file_info *fi;
8701 const char *f;
8703 gcc_assert (fnad->max_files >= d->emitted_number);
8705 if (! d->emitted_number)
8706 return 1;
8708 gcc_assert (fnad->max_files != fnad->used_files);
8710 fi = fnad->files + fnad->used_files++;
8712 /* Skip all leading "./". */
8713 f = d->filename;
8714 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
8715 f += 2;
8717 /* Create a new array entry. */
8718 fi->path = f;
8719 fi->length = strlen (f);
8720 fi->file_idx = d;
8722 /* Search for the file name part. */
8723 f = strrchr (f, DIR_SEPARATOR);
8724 #if defined (DIR_SEPARATOR_2)
8726 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
8728 if (g != NULL)
8730 if (f == NULL || f < g)
8731 f = g;
8734 #endif
8736 fi->fname = f == NULL ? fi->path : f + 1;
8737 return 1;
8740 /* Output the directory table and the file name table. We try to minimize
8741 the total amount of memory needed. A heuristic is used to avoid large
8742 slowdowns with many input files. */
8744 static void
8745 output_file_names (void)
8747 struct file_name_acquire_data fnad;
8748 int numfiles;
8749 struct file_info *files;
8750 struct dir_info *dirs;
8751 int *saved;
8752 int *savehere;
8753 int *backmap;
8754 int ndirs;
8755 int idx_offset;
8756 int i;
8758 if (!last_emitted_file)
8760 dw2_asm_output_data (1, 0, "End directory table");
8761 dw2_asm_output_data (1, 0, "End file name table");
8762 return;
8765 numfiles = last_emitted_file->emitted_number;
8767 /* Allocate the various arrays we need. */
8768 files = XALLOCAVEC (struct file_info, numfiles);
8769 dirs = XALLOCAVEC (struct dir_info, numfiles);
8771 fnad.files = files;
8772 fnad.used_files = 0;
8773 fnad.max_files = numfiles;
8774 htab_traverse (file_table, file_name_acquire, &fnad);
8775 gcc_assert (fnad.used_files == fnad.max_files);
8777 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
8779 /* Find all the different directories used. */
8780 dirs[0].path = files[0].path;
8781 dirs[0].length = files[0].fname - files[0].path;
8782 dirs[0].prefix = -1;
8783 dirs[0].count = 1;
8784 dirs[0].dir_idx = 0;
8785 files[0].dir_idx = 0;
8786 ndirs = 1;
8788 for (i = 1; i < numfiles; i++)
8789 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
8790 && memcmp (dirs[ndirs - 1].path, files[i].path,
8791 dirs[ndirs - 1].length) == 0)
8793 /* Same directory as last entry. */
8794 files[i].dir_idx = ndirs - 1;
8795 ++dirs[ndirs - 1].count;
8797 else
8799 int j;
8801 /* This is a new directory. */
8802 dirs[ndirs].path = files[i].path;
8803 dirs[ndirs].length = files[i].fname - files[i].path;
8804 dirs[ndirs].count = 1;
8805 dirs[ndirs].dir_idx = ndirs;
8806 files[i].dir_idx = ndirs;
8808 /* Search for a prefix. */
8809 dirs[ndirs].prefix = -1;
8810 for (j = 0; j < ndirs; j++)
8811 if (dirs[j].length < dirs[ndirs].length
8812 && dirs[j].length > 1
8813 && (dirs[ndirs].prefix == -1
8814 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
8815 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
8816 dirs[ndirs].prefix = j;
8818 ++ndirs;
8821 /* Now to the actual work. We have to find a subset of the directories which
8822 allow expressing the file name using references to the directory table
8823 with the least amount of characters. We do not do an exhaustive search
8824 where we would have to check out every combination of every single
8825 possible prefix. Instead we use a heuristic which provides nearly optimal
8826 results in most cases and never is much off. */
8827 saved = XALLOCAVEC (int, ndirs);
8828 savehere = XALLOCAVEC (int, ndirs);
8830 memset (saved, '\0', ndirs * sizeof (saved[0]));
8831 for (i = 0; i < ndirs; i++)
8833 int j;
8834 int total;
8836 /* We can always save some space for the current directory. But this
8837 does not mean it will be enough to justify adding the directory. */
8838 savehere[i] = dirs[i].length;
8839 total = (savehere[i] - saved[i]) * dirs[i].count;
8841 for (j = i + 1; j < ndirs; j++)
8843 savehere[j] = 0;
8844 if (saved[j] < dirs[i].length)
8846 /* Determine whether the dirs[i] path is a prefix of the
8847 dirs[j] path. */
8848 int k;
8850 k = dirs[j].prefix;
8851 while (k != -1 && k != (int) i)
8852 k = dirs[k].prefix;
8854 if (k == (int) i)
8856 /* Yes it is. We can possibly save some memory by
8857 writing the filenames in dirs[j] relative to
8858 dirs[i]. */
8859 savehere[j] = dirs[i].length;
8860 total += (savehere[j] - saved[j]) * dirs[j].count;
8865 /* Check whether we can save enough to justify adding the dirs[i]
8866 directory. */
8867 if (total > dirs[i].length + 1)
8869 /* It's worthwhile adding. */
8870 for (j = i; j < ndirs; j++)
8871 if (savehere[j] > 0)
8873 /* Remember how much we saved for this directory so far. */
8874 saved[j] = savehere[j];
8876 /* Remember the prefix directory. */
8877 dirs[j].dir_idx = i;
8882 /* Emit the directory name table. */
8883 idx_offset = dirs[0].length > 0 ? 1 : 0;
8884 for (i = 1 - idx_offset; i < ndirs; i++)
8885 dw2_asm_output_nstring (dirs[i].path,
8886 dirs[i].length
8887 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
8888 "Directory Entry: %#x", i + idx_offset);
8890 dw2_asm_output_data (1, 0, "End directory table");
8892 /* We have to emit them in the order of emitted_number since that's
8893 used in the debug info generation. To do this efficiently we
8894 generate a back-mapping of the indices first. */
8895 backmap = XALLOCAVEC (int, numfiles);
8896 for (i = 0; i < numfiles; i++)
8897 backmap[files[i].file_idx->emitted_number - 1] = i;
8899 /* Now write all the file names. */
8900 for (i = 0; i < numfiles; i++)
8902 int file_idx = backmap[i];
8903 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
8905 #ifdef VMS_DEBUGGING_INFO
8906 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
8908 /* Setting these fields can lead to debugger miscomparisons,
8909 but VMS Debug requires them to be set correctly. */
8911 int ver;
8912 long long cdt;
8913 long siz;
8914 int maxfilelen = strlen (files[file_idx].path)
8915 + dirs[dir_idx].length
8916 + MAX_VMS_VERSION_LEN + 1;
8917 char *filebuf = XALLOCAVEC (char, maxfilelen);
8919 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
8920 snprintf (filebuf, maxfilelen, "%s;%d",
8921 files[file_idx].path + dirs[dir_idx].length, ver);
8923 dw2_asm_output_nstring
8924 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
8926 /* Include directory index. */
8927 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
8929 /* Modification time. */
8930 dw2_asm_output_data_uleb128
8931 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
8932 ? cdt : 0,
8933 NULL);
8935 /* File length in bytes. */
8936 dw2_asm_output_data_uleb128
8937 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
8938 ? siz : 0,
8939 NULL);
8940 #else
8941 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
8942 "File Entry: %#x", (unsigned) i + 1);
8944 /* Include directory index. */
8945 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
8947 /* Modification time. */
8948 dw2_asm_output_data_uleb128 (0, NULL);
8950 /* File length in bytes. */
8951 dw2_asm_output_data_uleb128 (0, NULL);
8952 #endif /* VMS_DEBUGGING_INFO */
8955 dw2_asm_output_data (1, 0, "End file name table");
8959 /* Output one line number table into the .debug_line section. */
8961 static void
8962 output_one_line_info_table (dw_line_info_table *table)
8964 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
8965 unsigned int current_line = 1;
8966 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
8967 dw_line_info_entry *ent;
8968 size_t i;
8970 FOR_EACH_VEC_ELT (dw_line_info_entry, table->entries, i, ent)
8972 switch (ent->opcode)
8974 case LI_set_address:
8975 /* ??? Unfortunately, we have little choice here currently, and
8976 must always use the most general form. GCC does not know the
8977 address delta itself, so we can't use DW_LNS_advance_pc. Many
8978 ports do have length attributes which will give an upper bound
8979 on the address range. We could perhaps use length attributes
8980 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
8981 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
8983 /* This can handle any delta. This takes
8984 4+DWARF2_ADDR_SIZE bytes. */
8985 dw2_asm_output_data (1, 0, "set address %s", line_label);
8986 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8987 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8988 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8989 break;
8991 case LI_set_line:
8992 if (ent->val == current_line)
8994 /* We still need to start a new row, so output a copy insn. */
8995 dw2_asm_output_data (1, DW_LNS_copy,
8996 "copy line %u", current_line);
8998 else
9000 int line_offset = ent->val - current_line;
9001 int line_delta = line_offset - DWARF_LINE_BASE;
9003 current_line = ent->val;
9004 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9006 /* This can handle deltas from -10 to 234, using the current
9007 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
9008 This takes 1 byte. */
9009 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9010 "line %u", current_line);
9012 else
9014 /* This can handle any delta. This takes at least 4 bytes,
9015 depending on the value being encoded. */
9016 dw2_asm_output_data (1, DW_LNS_advance_line,
9017 "advance to line %u", current_line);
9018 dw2_asm_output_data_sleb128 (line_offset, NULL);
9019 dw2_asm_output_data (1, DW_LNS_copy, NULL);
9022 break;
9024 case LI_set_file:
9025 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
9026 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9027 break;
9029 case LI_set_column:
9030 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
9031 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9032 break;
9034 case LI_negate_stmt:
9035 current_is_stmt = !current_is_stmt;
9036 dw2_asm_output_data (1, DW_LNS_negate_stmt,
9037 "is_stmt %d", current_is_stmt);
9038 break;
9040 case LI_set_prologue_end:
9041 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
9042 "set prologue end");
9043 break;
9045 case LI_set_epilogue_begin:
9046 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
9047 "set epilogue begin");
9048 break;
9050 case LI_set_discriminator:
9051 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
9052 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
9053 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
9054 dw2_asm_output_data_uleb128 (ent->val, NULL);
9055 break;
9059 /* Emit debug info for the address of the end of the table. */
9060 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
9061 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9062 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9063 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
9065 dw2_asm_output_data (1, 0, "end sequence");
9066 dw2_asm_output_data_uleb128 (1, NULL);
9067 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9070 /* Output the source line number correspondence information. This
9071 information goes into the .debug_line section. */
9073 static void
9074 output_line_info (void)
9076 char l1[20], l2[20], p1[20], p2[20];
9077 int ver = dwarf_version;
9078 bool saw_one = false;
9079 int opc;
9081 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
9082 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
9083 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
9084 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
9086 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9087 dw2_asm_output_data (4, 0xffffffff,
9088 "Initial length escape value indicating 64-bit DWARF extension");
9089 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
9090 "Length of Source Line Info");
9091 ASM_OUTPUT_LABEL (asm_out_file, l1);
9093 dw2_asm_output_data (2, ver, "DWARF Version");
9094 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
9095 ASM_OUTPUT_LABEL (asm_out_file, p1);
9097 /* Define the architecture-dependent minimum instruction length (in bytes).
9098 In this implementation of DWARF, this field is used for information
9099 purposes only. Since GCC generates assembly language, we have no
9100 a priori knowledge of how many instruction bytes are generated for each
9101 source line, and therefore can use only the DW_LNE_set_address and
9102 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
9103 this as '1', which is "correct enough" for all architectures,
9104 and don't let the target override. */
9105 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
9107 if (ver >= 4)
9108 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
9109 "Maximum Operations Per Instruction");
9110 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
9111 "Default is_stmt_start flag");
9112 dw2_asm_output_data (1, DWARF_LINE_BASE,
9113 "Line Base Value (Special Opcodes)");
9114 dw2_asm_output_data (1, DWARF_LINE_RANGE,
9115 "Line Range Value (Special Opcodes)");
9116 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
9117 "Special Opcode Base");
9119 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
9121 int n_op_args;
9122 switch (opc)
9124 case DW_LNS_advance_pc:
9125 case DW_LNS_advance_line:
9126 case DW_LNS_set_file:
9127 case DW_LNS_set_column:
9128 case DW_LNS_fixed_advance_pc:
9129 case DW_LNS_set_isa:
9130 n_op_args = 1;
9131 break;
9132 default:
9133 n_op_args = 0;
9134 break;
9137 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
9138 opc, n_op_args);
9141 /* Write out the information about the files we use. */
9142 output_file_names ();
9143 ASM_OUTPUT_LABEL (asm_out_file, p2);
9145 if (separate_line_info)
9147 dw_line_info_table *table;
9148 size_t i;
9150 FOR_EACH_VEC_ELT (dw_line_info_table_p, separate_line_info, i, table)
9151 if (table->in_use)
9153 output_one_line_info_table (table);
9154 saw_one = true;
9157 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
9159 output_one_line_info_table (cold_text_section_line_info);
9160 saw_one = true;
9163 /* ??? Some Darwin linkers crash on a .debug_line section with no
9164 sequences. Further, merely a DW_LNE_end_sequence entry is not
9165 sufficient -- the address column must also be initialized.
9166 Make sure to output at least one set_address/end_sequence pair,
9167 choosing .text since that section is always present. */
9168 if (text_section_line_info->in_use || !saw_one)
9169 output_one_line_info_table (text_section_line_info);
9171 /* Output the marker for the end of the line number info. */
9172 ASM_OUTPUT_LABEL (asm_out_file, l2);
9175 /* Given a pointer to a tree node for some base type, return a pointer to
9176 a DIE that describes the given type.
9178 This routine must only be called for GCC type nodes that correspond to
9179 Dwarf base (fundamental) types. */
9181 static dw_die_ref
9182 base_type_die (tree type)
9184 dw_die_ref base_type_result;
9185 enum dwarf_type encoding;
9187 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
9188 return 0;
9190 /* If this is a subtype that should not be emitted as a subrange type,
9191 use the base type. See subrange_type_for_debug_p. */
9192 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
9193 type = TREE_TYPE (type);
9195 switch (TREE_CODE (type))
9197 case INTEGER_TYPE:
9198 if ((dwarf_version >= 4 || !dwarf_strict)
9199 && TYPE_NAME (type)
9200 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
9201 && DECL_IS_BUILTIN (TYPE_NAME (type))
9202 && DECL_NAME (TYPE_NAME (type)))
9204 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
9205 if (strcmp (name, "char16_t") == 0
9206 || strcmp (name, "char32_t") == 0)
9208 encoding = DW_ATE_UTF;
9209 break;
9212 if (TYPE_STRING_FLAG (type))
9214 if (TYPE_UNSIGNED (type))
9215 encoding = DW_ATE_unsigned_char;
9216 else
9217 encoding = DW_ATE_signed_char;
9219 else if (TYPE_UNSIGNED (type))
9220 encoding = DW_ATE_unsigned;
9221 else
9222 encoding = DW_ATE_signed;
9223 break;
9225 case REAL_TYPE:
9226 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
9228 if (dwarf_version >= 3 || !dwarf_strict)
9229 encoding = DW_ATE_decimal_float;
9230 else
9231 encoding = DW_ATE_lo_user;
9233 else
9234 encoding = DW_ATE_float;
9235 break;
9237 case FIXED_POINT_TYPE:
9238 if (!(dwarf_version >= 3 || !dwarf_strict))
9239 encoding = DW_ATE_lo_user;
9240 else if (TYPE_UNSIGNED (type))
9241 encoding = DW_ATE_unsigned_fixed;
9242 else
9243 encoding = DW_ATE_signed_fixed;
9244 break;
9246 /* Dwarf2 doesn't know anything about complex ints, so use
9247 a user defined type for it. */
9248 case COMPLEX_TYPE:
9249 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
9250 encoding = DW_ATE_complex_float;
9251 else
9252 encoding = DW_ATE_lo_user;
9253 break;
9255 case BOOLEAN_TYPE:
9256 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
9257 encoding = DW_ATE_boolean;
9258 break;
9260 default:
9261 /* No other TREE_CODEs are Dwarf fundamental types. */
9262 gcc_unreachable ();
9265 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
9267 add_AT_unsigned (base_type_result, DW_AT_byte_size,
9268 int_size_in_bytes (type));
9269 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
9270 add_pubtype (type, base_type_result);
9272 return base_type_result;
9275 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
9276 given input type is a Dwarf "fundamental" type. Otherwise return null. */
9278 static inline int
9279 is_base_type (tree type)
9281 switch (TREE_CODE (type))
9283 case ERROR_MARK:
9284 case VOID_TYPE:
9285 case INTEGER_TYPE:
9286 case REAL_TYPE:
9287 case FIXED_POINT_TYPE:
9288 case COMPLEX_TYPE:
9289 case BOOLEAN_TYPE:
9290 return 1;
9292 case ARRAY_TYPE:
9293 case RECORD_TYPE:
9294 case UNION_TYPE:
9295 case QUAL_UNION_TYPE:
9296 case ENUMERAL_TYPE:
9297 case FUNCTION_TYPE:
9298 case METHOD_TYPE:
9299 case POINTER_TYPE:
9300 case REFERENCE_TYPE:
9301 case NULLPTR_TYPE:
9302 case OFFSET_TYPE:
9303 case LANG_TYPE:
9304 case VECTOR_TYPE:
9305 return 0;
9307 default:
9308 gcc_unreachable ();
9311 return 0;
9314 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
9315 node, return the size in bits for the type if it is a constant, or else
9316 return the alignment for the type if the type's size is not constant, or
9317 else return BITS_PER_WORD if the type actually turns out to be an
9318 ERROR_MARK node. */
9320 static inline unsigned HOST_WIDE_INT
9321 simple_type_size_in_bits (const_tree type)
9323 if (TREE_CODE (type) == ERROR_MARK)
9324 return BITS_PER_WORD;
9325 else if (TYPE_SIZE (type) == NULL_TREE)
9326 return 0;
9327 else if (host_integerp (TYPE_SIZE (type), 1))
9328 return tree_low_cst (TYPE_SIZE (type), 1);
9329 else
9330 return TYPE_ALIGN (type);
9333 /* Similarly, but return a double_int instead of UHWI. */
9335 static inline double_int
9336 double_int_type_size_in_bits (const_tree type)
9338 if (TREE_CODE (type) == ERROR_MARK)
9339 return double_int::from_uhwi (BITS_PER_WORD);
9340 else if (TYPE_SIZE (type) == NULL_TREE)
9341 return double_int_zero;
9342 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
9343 return tree_to_double_int (TYPE_SIZE (type));
9344 else
9345 return double_int::from_uhwi (TYPE_ALIGN (type));
9348 /* Given a pointer to a tree node for a subrange type, return a pointer
9349 to a DIE that describes the given type. */
9351 static dw_die_ref
9352 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
9354 dw_die_ref subrange_die;
9355 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
9357 if (context_die == NULL)
9358 context_die = comp_unit_die ();
9360 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
9362 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
9364 /* The size of the subrange type and its base type do not match,
9365 so we need to generate a size attribute for the subrange type. */
9366 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
9369 if (low)
9370 add_bound_info (subrange_die, DW_AT_lower_bound, low);
9371 if (high)
9372 add_bound_info (subrange_die, DW_AT_upper_bound, high);
9374 return subrange_die;
9377 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
9378 entry that chains various modifiers in front of the given type. */
9380 static dw_die_ref
9381 modified_type_die (tree type, int is_const_type, int is_volatile_type,
9382 dw_die_ref context_die)
9384 enum tree_code code = TREE_CODE (type);
9385 dw_die_ref mod_type_die;
9386 dw_die_ref sub_die = NULL;
9387 tree item_type = NULL;
9388 tree qualified_type;
9389 tree name, low, high;
9390 dw_die_ref mod_scope;
9392 if (code == ERROR_MARK)
9393 return NULL;
9395 /* See if we already have the appropriately qualified variant of
9396 this type. */
9397 qualified_type
9398 = get_qualified_type (type,
9399 ((is_const_type ? TYPE_QUAL_CONST : 0)
9400 | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
9402 if (qualified_type == sizetype
9403 && TYPE_NAME (qualified_type)
9404 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
9406 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
9408 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
9409 && TYPE_PRECISION (t)
9410 == TYPE_PRECISION (qualified_type)
9411 && TYPE_UNSIGNED (t)
9412 == TYPE_UNSIGNED (qualified_type));
9413 qualified_type = t;
9416 /* If we do, then we can just use its DIE, if it exists. */
9417 if (qualified_type)
9419 mod_type_die = lookup_type_die (qualified_type);
9420 if (mod_type_die)
9421 return mod_type_die;
9424 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
9426 /* Handle C typedef types. */
9427 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
9428 && !DECL_ARTIFICIAL (name))
9430 tree dtype = TREE_TYPE (name);
9432 if (qualified_type == dtype)
9434 /* For a named type, use the typedef. */
9435 gen_type_die (qualified_type, context_die);
9436 return lookup_type_die (qualified_type);
9438 else if (is_const_type < TYPE_READONLY (dtype)
9439 || is_volatile_type < TYPE_VOLATILE (dtype)
9440 || (is_const_type <= TYPE_READONLY (dtype)
9441 && is_volatile_type <= TYPE_VOLATILE (dtype)
9442 && DECL_ORIGINAL_TYPE (name) != type))
9443 /* cv-unqualified version of named type. Just use the unnamed
9444 type to which it refers. */
9445 return modified_type_die (DECL_ORIGINAL_TYPE (name),
9446 is_const_type, is_volatile_type,
9447 context_die);
9448 /* Else cv-qualified version of named type; fall through. */
9451 mod_scope = scope_die_for (type, context_die);
9453 if (is_const_type
9454 /* If both is_const_type and is_volatile_type, prefer the path
9455 which leads to a qualified type. */
9456 && (!is_volatile_type
9457 || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
9458 || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
9460 mod_type_die = new_die (DW_TAG_const_type, mod_scope, type);
9461 sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
9463 else if (is_volatile_type)
9465 mod_type_die = new_die (DW_TAG_volatile_type, mod_scope, type);
9466 sub_die = modified_type_die (type, is_const_type, 0, context_die);
9468 else if (code == POINTER_TYPE)
9470 mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
9471 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9472 simple_type_size_in_bits (type) / BITS_PER_UNIT);
9473 item_type = TREE_TYPE (type);
9474 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
9475 add_AT_unsigned (mod_type_die, DW_AT_address_class,
9476 TYPE_ADDR_SPACE (item_type));
9478 else if (code == REFERENCE_TYPE)
9480 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
9481 mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
9482 type);
9483 else
9484 mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
9485 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9486 simple_type_size_in_bits (type) / BITS_PER_UNIT);
9487 item_type = TREE_TYPE (type);
9488 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
9489 add_AT_unsigned (mod_type_die, DW_AT_address_class,
9490 TYPE_ADDR_SPACE (item_type));
9492 else if (code == INTEGER_TYPE
9493 && TREE_TYPE (type) != NULL_TREE
9494 && subrange_type_for_debug_p (type, &low, &high))
9496 mod_type_die = subrange_type_die (type, low, high, context_die);
9497 item_type = TREE_TYPE (type);
9499 else if (is_base_type (type))
9500 mod_type_die = base_type_die (type);
9501 else
9503 gen_type_die (type, context_die);
9505 /* We have to get the type_main_variant here (and pass that to the
9506 `lookup_type_die' routine) because the ..._TYPE node we have
9507 might simply be a *copy* of some original type node (where the
9508 copy was created to help us keep track of typedef names) and
9509 that copy might have a different TYPE_UID from the original
9510 ..._TYPE node. */
9511 if (TREE_CODE (type) != VECTOR_TYPE)
9512 return lookup_type_die (type_main_variant (type));
9513 else
9514 /* Vectors have the debugging information in the type,
9515 not the main variant. */
9516 return lookup_type_die (type);
9519 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
9520 don't output a DW_TAG_typedef, since there isn't one in the
9521 user's program; just attach a DW_AT_name to the type.
9522 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
9523 if the base type already has the same name. */
9524 if (name
9525 && ((TREE_CODE (name) != TYPE_DECL
9526 && (qualified_type == TYPE_MAIN_VARIANT (type)
9527 || (!is_const_type && !is_volatile_type)))
9528 || (TREE_CODE (name) == TYPE_DECL
9529 && TREE_TYPE (name) == qualified_type
9530 && DECL_NAME (name))))
9532 if (TREE_CODE (name) == TYPE_DECL)
9533 /* Could just call add_name_and_src_coords_attributes here,
9534 but since this is a builtin type it doesn't have any
9535 useful source coordinates anyway. */
9536 name = DECL_NAME (name);
9537 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
9539 /* This probably indicates a bug. */
9540 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
9542 name = TYPE_NAME (type);
9543 if (name
9544 && TREE_CODE (name) == TYPE_DECL)
9545 name = DECL_NAME (name);
9546 add_name_attribute (mod_type_die,
9547 name ? IDENTIFIER_POINTER (name) : "__unknown__");
9550 if (qualified_type)
9551 equate_type_number_to_die (qualified_type, mod_type_die);
9553 if (item_type)
9554 /* We must do this after the equate_type_number_to_die call, in case
9555 this is a recursive type. This ensures that the modified_type_die
9556 recursion will terminate even if the type is recursive. Recursive
9557 types are possible in Ada. */
9558 sub_die = modified_type_die (item_type,
9559 TYPE_READONLY (item_type),
9560 TYPE_VOLATILE (item_type),
9561 context_die);
9563 if (sub_die != NULL)
9564 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
9566 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
9567 if (TYPE_ARTIFICIAL (type))
9568 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
9570 return mod_type_die;
9573 /* Generate DIEs for the generic parameters of T.
9574 T must be either a generic type or a generic function.
9575 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
9577 static void
9578 gen_generic_params_dies (tree t)
9580 tree parms, args;
9581 int parms_num, i;
9582 dw_die_ref die = NULL;
9584 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
9585 return;
9587 if (TYPE_P (t))
9588 die = lookup_type_die (t);
9589 else if (DECL_P (t))
9590 die = lookup_decl_die (t);
9592 gcc_assert (die);
9594 parms = lang_hooks.get_innermost_generic_parms (t);
9595 if (!parms)
9596 /* T has no generic parameter. It means T is neither a generic type
9597 or function. End of story. */
9598 return;
9600 parms_num = TREE_VEC_LENGTH (parms);
9601 args = lang_hooks.get_innermost_generic_args (t);
9602 for (i = 0; i < parms_num; i++)
9604 tree parm, arg, arg_pack_elems;
9606 parm = TREE_VEC_ELT (parms, i);
9607 arg = TREE_VEC_ELT (args, i);
9608 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
9609 gcc_assert (parm && TREE_VALUE (parm) && arg);
9611 if (parm && TREE_VALUE (parm) && arg)
9613 /* If PARM represents a template parameter pack,
9614 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
9615 by DW_TAG_template_*_parameter DIEs for the argument
9616 pack elements of ARG. Note that ARG would then be
9617 an argument pack. */
9618 if (arg_pack_elems)
9619 template_parameter_pack_die (TREE_VALUE (parm),
9620 arg_pack_elems,
9621 die);
9622 else
9623 generic_parameter_die (TREE_VALUE (parm), arg,
9624 true /* Emit DW_AT_name */, die);
9629 /* Create and return a DIE for PARM which should be
9630 the representation of a generic type parameter.
9631 For instance, in the C++ front end, PARM would be a template parameter.
9632 ARG is the argument to PARM.
9633 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
9634 name of the PARM.
9635 PARENT_DIE is the parent DIE which the new created DIE should be added to,
9636 as a child node. */
9638 static dw_die_ref
9639 generic_parameter_die (tree parm, tree arg,
9640 bool emit_name_p,
9641 dw_die_ref parent_die)
9643 dw_die_ref tmpl_die = NULL;
9644 const char *name = NULL;
9646 if (!parm || !DECL_NAME (parm) || !arg)
9647 return NULL;
9649 /* We support non-type generic parameters and arguments,
9650 type generic parameters and arguments, as well as
9651 generic generic parameters (a.k.a. template template parameters in C++)
9652 and arguments. */
9653 if (TREE_CODE (parm) == PARM_DECL)
9654 /* PARM is a nontype generic parameter */
9655 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
9656 else if (TREE_CODE (parm) == TYPE_DECL)
9657 /* PARM is a type generic parameter. */
9658 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
9659 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
9660 /* PARM is a generic generic parameter.
9661 Its DIE is a GNU extension. It shall have a
9662 DW_AT_name attribute to represent the name of the template template
9663 parameter, and a DW_AT_GNU_template_name attribute to represent the
9664 name of the template template argument. */
9665 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
9666 parent_die, parm);
9667 else
9668 gcc_unreachable ();
9670 if (tmpl_die)
9672 tree tmpl_type;
9674 /* If PARM is a generic parameter pack, it means we are
9675 emitting debug info for a template argument pack element.
9676 In other terms, ARG is a template argument pack element.
9677 In that case, we don't emit any DW_AT_name attribute for
9678 the die. */
9679 if (emit_name_p)
9681 name = IDENTIFIER_POINTER (DECL_NAME (parm));
9682 gcc_assert (name);
9683 add_AT_string (tmpl_die, DW_AT_name, name);
9686 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
9688 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
9689 TMPL_DIE should have a child DW_AT_type attribute that is set
9690 to the type of the argument to PARM, which is ARG.
9691 If PARM is a type generic parameter, TMPL_DIE should have a
9692 child DW_AT_type that is set to ARG. */
9693 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
9694 add_type_attribute (tmpl_die, tmpl_type, 0,
9695 TREE_THIS_VOLATILE (tmpl_type),
9696 parent_die);
9698 else
9700 /* So TMPL_DIE is a DIE representing a
9701 a generic generic template parameter, a.k.a template template
9702 parameter in C++ and arg is a template. */
9704 /* The DW_AT_GNU_template_name attribute of the DIE must be set
9705 to the name of the argument. */
9706 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
9707 if (name)
9708 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
9711 if (TREE_CODE (parm) == PARM_DECL)
9712 /* So PARM is a non-type generic parameter.
9713 DWARF3 5.6.8 says we must set a DW_AT_const_value child
9714 attribute of TMPL_DIE which value represents the value
9715 of ARG.
9716 We must be careful here:
9717 The value of ARG might reference some function decls.
9718 We might currently be emitting debug info for a generic
9719 type and types are emitted before function decls, we don't
9720 know if the function decls referenced by ARG will actually be
9721 emitted after cgraph computations.
9722 So must defer the generation of the DW_AT_const_value to
9723 after cgraph is ready. */
9724 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
9727 return tmpl_die;
9730 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
9731 PARM_PACK must be a template parameter pack. The returned DIE
9732 will be child DIE of PARENT_DIE. */
9734 static dw_die_ref
9735 template_parameter_pack_die (tree parm_pack,
9736 tree parm_pack_args,
9737 dw_die_ref parent_die)
9739 dw_die_ref die;
9740 int j;
9742 gcc_assert (parent_die && parm_pack);
9744 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
9745 add_name_and_src_coords_attributes (die, parm_pack);
9746 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
9747 generic_parameter_die (parm_pack,
9748 TREE_VEC_ELT (parm_pack_args, j),
9749 false /* Don't emit DW_AT_name */,
9750 die);
9751 return die;
9754 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
9755 an enumerated type. */
9757 static inline int
9758 type_is_enum (const_tree type)
9760 return TREE_CODE (type) == ENUMERAL_TYPE;
9763 /* Return the DBX register number described by a given RTL node. */
9765 static unsigned int
9766 dbx_reg_number (const_rtx rtl)
9768 unsigned regno = REGNO (rtl);
9770 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
9772 #ifdef LEAF_REG_REMAP
9773 if (crtl->uses_only_leaf_regs)
9775 int leaf_reg = LEAF_REG_REMAP (regno);
9776 if (leaf_reg != -1)
9777 regno = (unsigned) leaf_reg;
9779 #endif
9781 regno = DBX_REGISTER_NUMBER (regno);
9782 gcc_assert (regno != INVALID_REGNUM);
9783 return regno;
9786 /* Optionally add a DW_OP_piece term to a location description expression.
9787 DW_OP_piece is only added if the location description expression already
9788 doesn't end with DW_OP_piece. */
9790 static void
9791 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
9793 dw_loc_descr_ref loc;
9795 if (*list_head != NULL)
9797 /* Find the end of the chain. */
9798 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
9801 if (loc->dw_loc_opc != DW_OP_piece)
9802 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
9806 /* Return a location descriptor that designates a machine register or
9807 zero if there is none. */
9809 static dw_loc_descr_ref
9810 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
9812 rtx regs;
9814 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
9815 return 0;
9817 /* We only use "frame base" when we're sure we're talking about the
9818 post-prologue local stack frame. We do this by *not* running
9819 register elimination until this point, and recognizing the special
9820 argument pointer and soft frame pointer rtx's.
9821 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
9822 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
9823 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
9825 dw_loc_descr_ref result = NULL;
9827 if (dwarf_version >= 4 || !dwarf_strict)
9829 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
9830 initialized);
9831 if (result)
9832 add_loc_descr (&result,
9833 new_loc_descr (DW_OP_stack_value, 0, 0));
9835 return result;
9838 regs = targetm.dwarf_register_span (rtl);
9840 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
9841 return multiple_reg_loc_descriptor (rtl, regs, initialized);
9842 else
9843 return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
9846 /* Return a location descriptor that designates a machine register for
9847 a given hard register number. */
9849 static dw_loc_descr_ref
9850 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
9852 dw_loc_descr_ref reg_loc_descr;
9854 if (regno <= 31)
9855 reg_loc_descr
9856 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
9857 else
9858 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
9860 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
9861 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9863 return reg_loc_descr;
9866 /* Given an RTL of a register, return a location descriptor that
9867 designates a value that spans more than one register. */
9869 static dw_loc_descr_ref
9870 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
9871 enum var_init_status initialized)
9873 int nregs, size, i;
9874 unsigned reg;
9875 dw_loc_descr_ref loc_result = NULL;
9877 reg = REGNO (rtl);
9878 #ifdef LEAF_REG_REMAP
9879 if (crtl->uses_only_leaf_regs)
9881 int leaf_reg = LEAF_REG_REMAP (reg);
9882 if (leaf_reg != -1)
9883 reg = (unsigned) leaf_reg;
9885 #endif
9886 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
9887 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
9889 /* Simple, contiguous registers. */
9890 if (regs == NULL_RTX)
9892 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
9894 loc_result = NULL;
9895 while (nregs--)
9897 dw_loc_descr_ref t;
9899 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
9900 VAR_INIT_STATUS_INITIALIZED);
9901 add_loc_descr (&loc_result, t);
9902 add_loc_descr_op_piece (&loc_result, size);
9903 ++reg;
9905 return loc_result;
9908 /* Now onto stupid register sets in non contiguous locations. */
9910 gcc_assert (GET_CODE (regs) == PARALLEL);
9912 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
9913 loc_result = NULL;
9915 for (i = 0; i < XVECLEN (regs, 0); ++i)
9917 dw_loc_descr_ref t;
9919 t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
9920 VAR_INIT_STATUS_INITIALIZED);
9921 add_loc_descr (&loc_result, t);
9922 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
9923 add_loc_descr_op_piece (&loc_result, size);
9926 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9927 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9928 return loc_result;
9931 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
9933 /* Return a location descriptor that designates a constant i,
9934 as a compound operation from constant (i >> shift), constant shift
9935 and DW_OP_shl. */
9937 static dw_loc_descr_ref
9938 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
9940 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
9941 add_loc_descr (&ret, int_loc_descriptor (shift));
9942 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
9943 return ret;
9946 /* Return a location descriptor that designates a constant. */
9948 static dw_loc_descr_ref
9949 int_loc_descriptor (HOST_WIDE_INT i)
9951 enum dwarf_location_atom op;
9953 /* Pick the smallest representation of a constant, rather than just
9954 defaulting to the LEB encoding. */
9955 if (i >= 0)
9957 int clz = clz_hwi (i);
9958 int ctz = ctz_hwi (i);
9959 if (i <= 31)
9960 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
9961 else if (i <= 0xff)
9962 op = DW_OP_const1u;
9963 else if (i <= 0xffff)
9964 op = DW_OP_const2u;
9965 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
9966 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
9967 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
9968 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
9969 while DW_OP_const4u is 5 bytes. */
9970 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
9971 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
9972 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
9973 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
9974 while DW_OP_const4u is 5 bytes. */
9975 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
9976 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
9977 op = DW_OP_const4u;
9978 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
9979 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
9980 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
9981 while DW_OP_constu of constant >= 0x100000000 takes at least
9982 6 bytes. */
9983 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
9984 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
9985 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
9986 >= HOST_BITS_PER_WIDE_INT)
9987 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
9988 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
9989 while DW_OP_constu takes in this case at least 6 bytes. */
9990 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
9991 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
9992 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
9993 && size_of_uleb128 (i) > 6)
9994 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
9995 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
9996 else
9997 op = DW_OP_constu;
9999 else
10001 if (i >= -0x80)
10002 op = DW_OP_const1s;
10003 else if (i >= -0x8000)
10004 op = DW_OP_const2s;
10005 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10007 if (size_of_int_loc_descriptor (i) < 5)
10009 dw_loc_descr_ref ret = int_loc_descriptor (-i);
10010 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10011 return ret;
10013 op = DW_OP_const4s;
10015 else
10017 if (size_of_int_loc_descriptor (i)
10018 < (unsigned long) 1 + size_of_sleb128 (i))
10020 dw_loc_descr_ref ret = int_loc_descriptor (-i);
10021 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10022 return ret;
10024 op = DW_OP_consts;
10028 return new_loc_descr (op, i, 0);
10031 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
10032 without actually allocating it. */
10034 static unsigned long
10035 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10037 return size_of_int_loc_descriptor (i >> shift)
10038 + size_of_int_loc_descriptor (shift)
10039 + 1;
10042 /* Return size_of_locs (int_loc_descriptor (i)) without
10043 actually allocating it. */
10045 static unsigned long
10046 size_of_int_loc_descriptor (HOST_WIDE_INT i)
10048 unsigned long s;
10050 if (i >= 0)
10052 int clz, ctz;
10053 if (i <= 31)
10054 return 1;
10055 else if (i <= 0xff)
10056 return 2;
10057 else if (i <= 0xffff)
10058 return 3;
10059 clz = clz_hwi (i);
10060 ctz = ctz_hwi (i);
10061 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10062 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10063 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10064 - clz - 5);
10065 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10066 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10067 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10068 - clz - 8);
10069 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10070 return 5;
10071 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
10072 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10073 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10074 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10075 - clz - 8);
10076 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10077 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
10078 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10079 - clz - 16);
10080 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10081 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10082 && s > 6)
10083 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10084 - clz - 32);
10085 else
10086 return 1 + s;
10088 else
10090 if (i >= -0x80)
10091 return 2;
10092 else if (i >= -0x8000)
10093 return 3;
10094 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10096 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
10098 s = size_of_int_loc_descriptor (-i) + 1;
10099 if (s < 5)
10100 return s;
10102 return 5;
10104 else
10106 unsigned long r = 1 + size_of_sleb128 (i);
10107 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
10109 s = size_of_int_loc_descriptor (-i) + 1;
10110 if (s < r)
10111 return s;
10113 return r;
10118 /* Return loc description representing "address" of integer value.
10119 This can appear only as toplevel expression. */
10121 static dw_loc_descr_ref
10122 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
10124 int litsize;
10125 dw_loc_descr_ref loc_result = NULL;
10127 if (!(dwarf_version >= 4 || !dwarf_strict))
10128 return NULL;
10130 litsize = size_of_int_loc_descriptor (i);
10131 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
10132 is more compact. For DW_OP_stack_value we need:
10133 litsize + 1 (DW_OP_stack_value)
10134 and for DW_OP_implicit_value:
10135 1 (DW_OP_implicit_value) + 1 (length) + size. */
10136 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
10138 loc_result = int_loc_descriptor (i);
10139 add_loc_descr (&loc_result,
10140 new_loc_descr (DW_OP_stack_value, 0, 0));
10141 return loc_result;
10144 loc_result = new_loc_descr (DW_OP_implicit_value,
10145 size, 0);
10146 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
10147 loc_result->dw_loc_oprnd2.v.val_int = i;
10148 return loc_result;
10151 /* Return a location descriptor that designates a base+offset location. */
10153 static dw_loc_descr_ref
10154 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
10155 enum var_init_status initialized)
10157 unsigned int regno;
10158 dw_loc_descr_ref result;
10159 dw_fde_ref fde = cfun->fde;
10161 /* We only use "frame base" when we're sure we're talking about the
10162 post-prologue local stack frame. We do this by *not* running
10163 register elimination until this point, and recognizing the special
10164 argument pointer and soft frame pointer rtx's. */
10165 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
10167 rtx elim = (ira_use_lra_p
10168 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
10169 : eliminate_regs (reg, VOIDmode, NULL_RTX));
10171 if (elim != reg)
10173 if (GET_CODE (elim) == PLUS)
10175 offset += INTVAL (XEXP (elim, 1));
10176 elim = XEXP (elim, 0);
10178 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
10179 && (elim == hard_frame_pointer_rtx
10180 || elim == stack_pointer_rtx))
10181 || elim == (frame_pointer_needed
10182 ? hard_frame_pointer_rtx
10183 : stack_pointer_rtx));
10185 /* If drap register is used to align stack, use frame
10186 pointer + offset to access stack variables. If stack
10187 is aligned without drap, use stack pointer + offset to
10188 access stack variables. */
10189 if (crtl->stack_realign_tried
10190 && reg == frame_pointer_rtx)
10192 int base_reg
10193 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
10194 ? HARD_FRAME_POINTER_REGNUM
10195 : REGNO (elim));
10196 return new_reg_loc_descr (base_reg, offset);
10199 gcc_assert (frame_pointer_fb_offset_valid);
10200 offset += frame_pointer_fb_offset;
10201 return new_loc_descr (DW_OP_fbreg, offset, 0);
10205 regno = DWARF_FRAME_REGNUM (REGNO (reg));
10207 if (!optimize && fde
10208 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
10210 /* Use cfa+offset to represent the location of arguments passed
10211 on the stack when drap is used to align stack.
10212 Only do this when not optimizing, for optimized code var-tracking
10213 is supposed to track where the arguments live and the register
10214 used as vdrap or drap in some spot might be used for something
10215 else in other part of the routine. */
10216 return new_loc_descr (DW_OP_fbreg, offset, 0);
10219 if (regno <= 31)
10220 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
10221 offset, 0);
10222 else
10223 result = new_loc_descr (DW_OP_bregx, regno, offset);
10225 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10226 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10228 return result;
10231 /* Return true if this RTL expression describes a base+offset calculation. */
10233 static inline int
10234 is_based_loc (const_rtx rtl)
10236 return (GET_CODE (rtl) == PLUS
10237 && ((REG_P (XEXP (rtl, 0))
10238 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
10239 && CONST_INT_P (XEXP (rtl, 1)))));
10242 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
10243 failed. */
10245 static dw_loc_descr_ref
10246 tls_mem_loc_descriptor (rtx mem)
10248 tree base;
10249 dw_loc_descr_ref loc_result;
10251 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
10252 return NULL;
10254 base = get_base_address (MEM_EXPR (mem));
10255 if (base == NULL
10256 || TREE_CODE (base) != VAR_DECL
10257 || !DECL_THREAD_LOCAL_P (base))
10258 return NULL;
10260 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
10261 if (loc_result == NULL)
10262 return NULL;
10264 if (MEM_OFFSET (mem))
10265 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
10267 return loc_result;
10270 /* Output debug info about reason why we failed to expand expression as dwarf
10271 expression. */
10273 static void
10274 expansion_failed (tree expr, rtx rtl, char const *reason)
10276 if (dump_file && (dump_flags & TDF_DETAILS))
10278 fprintf (dump_file, "Failed to expand as dwarf: ");
10279 if (expr)
10280 print_generic_expr (dump_file, expr, dump_flags);
10281 if (rtl)
10283 fprintf (dump_file, "\n");
10284 print_rtl (dump_file, rtl);
10286 fprintf (dump_file, "\nReason: %s\n", reason);
10290 /* Helper function for const_ok_for_output, called either directly
10291 or via for_each_rtx. */
10293 static int
10294 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
10296 rtx rtl = *rtlp;
10298 if (GET_CODE (rtl) == UNSPEC)
10300 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
10301 we can't express it in the debug info. */
10302 #ifdef ENABLE_CHECKING
10303 /* Don't complain about TLS UNSPECs, those are just too hard to
10304 delegitimize. Note this could be a non-decl SYMBOL_REF such as
10305 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
10306 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
10307 if (XVECLEN (rtl, 0) == 0
10308 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
10309 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
10310 inform (current_function_decl
10311 ? DECL_SOURCE_LOCATION (current_function_decl)
10312 : UNKNOWN_LOCATION,
10313 #if NUM_UNSPEC_VALUES > 0
10314 "non-delegitimized UNSPEC %s (%d) found in variable location",
10315 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
10316 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
10317 XINT (rtl, 1));
10318 #else
10319 "non-delegitimized UNSPEC %d found in variable location",
10320 XINT (rtl, 1));
10321 #endif
10322 #endif
10323 expansion_failed (NULL_TREE, rtl,
10324 "UNSPEC hasn't been delegitimized.\n");
10325 return 1;
10328 if (targetm.const_not_ok_for_debug_p (rtl))
10330 expansion_failed (NULL_TREE, rtl,
10331 "Expression rejected for debug by the backend.\n");
10332 return 1;
10335 if (GET_CODE (rtl) != SYMBOL_REF)
10336 return 0;
10338 if (CONSTANT_POOL_ADDRESS_P (rtl))
10340 bool marked;
10341 get_pool_constant_mark (rtl, &marked);
10342 /* If all references to this pool constant were optimized away,
10343 it was not output and thus we can't represent it. */
10344 if (!marked)
10346 expansion_failed (NULL_TREE, rtl,
10347 "Constant was removed from constant pool.\n");
10348 return 1;
10352 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
10353 return 1;
10355 /* Avoid references to external symbols in debug info, on several targets
10356 the linker might even refuse to link when linking a shared library,
10357 and in many other cases the relocations for .debug_info/.debug_loc are
10358 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
10359 to be defined within the same shared library or executable are fine. */
10360 if (SYMBOL_REF_EXTERNAL_P (rtl))
10362 tree decl = SYMBOL_REF_DECL (rtl);
10364 if (decl == NULL || !targetm.binds_local_p (decl))
10366 expansion_failed (NULL_TREE, rtl,
10367 "Symbol not defined in current TU.\n");
10368 return 1;
10372 return 0;
10375 /* Return true if constant RTL can be emitted in DW_OP_addr or
10376 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
10377 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
10379 static bool
10380 const_ok_for_output (rtx rtl)
10382 if (GET_CODE (rtl) == SYMBOL_REF)
10383 return const_ok_for_output_1 (&rtl, NULL) == 0;
10385 if (GET_CODE (rtl) == CONST)
10386 return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
10388 return true;
10391 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
10392 if possible, NULL otherwise. */
10394 static dw_die_ref
10395 base_type_for_mode (enum machine_mode mode, bool unsignedp)
10397 dw_die_ref type_die;
10398 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
10400 if (type == NULL)
10401 return NULL;
10402 switch (TREE_CODE (type))
10404 case INTEGER_TYPE:
10405 case REAL_TYPE:
10406 break;
10407 default:
10408 return NULL;
10410 type_die = lookup_type_die (type);
10411 if (!type_die)
10412 type_die = modified_type_die (type, false, false, comp_unit_die ());
10413 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
10414 return NULL;
10415 return type_die;
10418 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
10419 type matching MODE, or, if MODE is narrower than or as wide as
10420 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
10421 possible. */
10423 static dw_loc_descr_ref
10424 convert_descriptor_to_mode (enum machine_mode mode, dw_loc_descr_ref op)
10426 enum machine_mode outer_mode = mode;
10427 dw_die_ref type_die;
10428 dw_loc_descr_ref cvt;
10430 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
10432 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
10433 return op;
10435 type_die = base_type_for_mode (outer_mode, 1);
10436 if (type_die == NULL)
10437 return NULL;
10438 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10439 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10440 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10441 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10442 add_loc_descr (&op, cvt);
10443 return op;
10446 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
10448 static dw_loc_descr_ref
10449 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
10450 dw_loc_descr_ref op1)
10452 dw_loc_descr_ref ret = op0;
10453 add_loc_descr (&ret, op1);
10454 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
10455 if (STORE_FLAG_VALUE != 1)
10457 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
10458 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
10460 return ret;
10463 /* Return location descriptor for signed comparison OP RTL. */
10465 static dw_loc_descr_ref
10466 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
10467 enum machine_mode mem_mode)
10469 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
10470 dw_loc_descr_ref op0, op1;
10471 int shift;
10473 if (op_mode == VOIDmode)
10474 op_mode = GET_MODE (XEXP (rtl, 1));
10475 if (op_mode == VOIDmode)
10476 return NULL;
10478 if (dwarf_strict
10479 && (GET_MODE_CLASS (op_mode) != MODE_INT
10480 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
10481 return NULL;
10483 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
10484 VAR_INIT_STATUS_INITIALIZED);
10485 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
10486 VAR_INIT_STATUS_INITIALIZED);
10488 if (op0 == NULL || op1 == NULL)
10489 return NULL;
10491 if (GET_MODE_CLASS (op_mode) != MODE_INT
10492 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
10493 return compare_loc_descriptor (op, op0, op1);
10495 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
10497 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
10498 dw_loc_descr_ref cvt;
10500 if (type_die == NULL)
10501 return NULL;
10502 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10503 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10504 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10505 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10506 add_loc_descr (&op0, cvt);
10507 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10508 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10509 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10510 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10511 add_loc_descr (&op1, cvt);
10512 return compare_loc_descriptor (op, op0, op1);
10515 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
10516 /* For eq/ne, if the operands are known to be zero-extended,
10517 there is no need to do the fancy shifting up. */
10518 if (op == DW_OP_eq || op == DW_OP_ne)
10520 dw_loc_descr_ref last0, last1;
10521 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
10523 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
10525 /* deref_size zero extends, and for constants we can check
10526 whether they are zero extended or not. */
10527 if (((last0->dw_loc_opc == DW_OP_deref_size
10528 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
10529 || (CONST_INT_P (XEXP (rtl, 0))
10530 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
10531 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
10532 && ((last1->dw_loc_opc == DW_OP_deref_size
10533 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
10534 || (CONST_INT_P (XEXP (rtl, 1))
10535 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
10536 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
10537 return compare_loc_descriptor (op, op0, op1);
10539 /* EQ/NE comparison against constant in narrower type than
10540 DWARF2_ADDR_SIZE can be performed either as
10541 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
10542 DW_OP_{eq,ne}
10544 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
10545 DW_OP_{eq,ne}. Pick whatever is shorter. */
10546 if (CONST_INT_P (XEXP (rtl, 1))
10547 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
10548 && (size_of_int_loc_descriptor (shift) + 1
10549 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
10550 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
10551 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
10552 & GET_MODE_MASK (op_mode))))
10554 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
10555 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
10556 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
10557 & GET_MODE_MASK (op_mode));
10558 return compare_loc_descriptor (op, op0, op1);
10561 add_loc_descr (&op0, int_loc_descriptor (shift));
10562 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
10563 if (CONST_INT_P (XEXP (rtl, 1)))
10564 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
10565 else
10567 add_loc_descr (&op1, int_loc_descriptor (shift));
10568 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
10570 return compare_loc_descriptor (op, op0, op1);
10573 /* Return location descriptor for unsigned comparison OP RTL. */
10575 static dw_loc_descr_ref
10576 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
10577 enum machine_mode mem_mode)
10579 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
10580 dw_loc_descr_ref op0, op1;
10582 if (op_mode == VOIDmode)
10583 op_mode = GET_MODE (XEXP (rtl, 1));
10584 if (op_mode == VOIDmode)
10585 return NULL;
10586 if (GET_MODE_CLASS (op_mode) != MODE_INT)
10587 return NULL;
10589 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
10590 return NULL;
10592 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
10593 VAR_INIT_STATUS_INITIALIZED);
10594 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
10595 VAR_INIT_STATUS_INITIALIZED);
10597 if (op0 == NULL || op1 == NULL)
10598 return NULL;
10600 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
10602 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
10603 dw_loc_descr_ref last0, last1;
10604 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
10606 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
10608 if (CONST_INT_P (XEXP (rtl, 0)))
10609 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
10610 /* deref_size zero extends, so no need to mask it again. */
10611 else if (last0->dw_loc_opc != DW_OP_deref_size
10612 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
10614 add_loc_descr (&op0, int_loc_descriptor (mask));
10615 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
10617 if (CONST_INT_P (XEXP (rtl, 1)))
10618 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
10619 /* deref_size zero extends, so no need to mask it again. */
10620 else if (last1->dw_loc_opc != DW_OP_deref_size
10621 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
10623 add_loc_descr (&op1, int_loc_descriptor (mask));
10624 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
10627 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
10629 HOST_WIDE_INT bias = 1;
10630 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
10631 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
10632 if (CONST_INT_P (XEXP (rtl, 1)))
10633 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
10634 + INTVAL (XEXP (rtl, 1)));
10635 else
10636 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
10637 bias, 0));
10639 return compare_loc_descriptor (op, op0, op1);
10642 /* Return location descriptor for {U,S}{MIN,MAX}. */
10644 static dw_loc_descr_ref
10645 minmax_loc_descriptor (rtx rtl, enum machine_mode mode,
10646 enum machine_mode mem_mode)
10648 enum dwarf_location_atom op;
10649 dw_loc_descr_ref op0, op1, ret;
10650 dw_loc_descr_ref bra_node, drop_node;
10652 if (dwarf_strict
10653 && (GET_MODE_CLASS (mode) != MODE_INT
10654 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
10655 return NULL;
10657 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
10658 VAR_INIT_STATUS_INITIALIZED);
10659 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
10660 VAR_INIT_STATUS_INITIALIZED);
10662 if (op0 == NULL || op1 == NULL)
10663 return NULL;
10665 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
10666 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
10667 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
10668 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
10670 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
10672 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
10673 add_loc_descr (&op0, int_loc_descriptor (mask));
10674 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
10675 add_loc_descr (&op1, int_loc_descriptor (mask));
10676 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
10678 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
10680 HOST_WIDE_INT bias = 1;
10681 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
10682 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
10683 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
10686 else if (GET_MODE_CLASS (mode) == MODE_INT
10687 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
10689 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
10690 add_loc_descr (&op0, int_loc_descriptor (shift));
10691 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
10692 add_loc_descr (&op1, int_loc_descriptor (shift));
10693 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
10695 else if (GET_MODE_CLASS (mode) == MODE_INT
10696 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
10698 dw_die_ref type_die = base_type_for_mode (mode, 0);
10699 dw_loc_descr_ref cvt;
10700 if (type_die == NULL)
10701 return NULL;
10702 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10703 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10704 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10705 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10706 add_loc_descr (&op0, cvt);
10707 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10708 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10709 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10710 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10711 add_loc_descr (&op1, cvt);
10714 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
10715 op = DW_OP_lt;
10716 else
10717 op = DW_OP_gt;
10718 ret = op0;
10719 add_loc_descr (&ret, op1);
10720 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
10721 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
10722 add_loc_descr (&ret, bra_node);
10723 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
10724 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
10725 add_loc_descr (&ret, drop_node);
10726 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
10727 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
10728 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
10729 && GET_MODE_CLASS (mode) == MODE_INT
10730 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
10731 ret = convert_descriptor_to_mode (mode, ret);
10732 return ret;
10735 /* Helper function for mem_loc_descriptor. Perform OP binary op,
10736 but after converting arguments to type_die, afterwards
10737 convert back to unsigned. */
10739 static dw_loc_descr_ref
10740 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
10741 enum machine_mode mode, enum machine_mode mem_mode)
10743 dw_loc_descr_ref cvt, op0, op1;
10745 if (type_die == NULL)
10746 return NULL;
10747 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
10748 VAR_INIT_STATUS_INITIALIZED);
10749 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
10750 VAR_INIT_STATUS_INITIALIZED);
10751 if (op0 == NULL || op1 == NULL)
10752 return NULL;
10753 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10754 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10755 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10756 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10757 add_loc_descr (&op0, cvt);
10758 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10759 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10760 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10761 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10762 add_loc_descr (&op1, cvt);
10763 add_loc_descr (&op0, op1);
10764 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
10765 return convert_descriptor_to_mode (mode, op0);
10768 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
10769 const0 is DW_OP_lit0 or corresponding typed constant,
10770 const1 is DW_OP_lit1 or corresponding typed constant
10771 and constMSB is constant with just the MSB bit set
10772 for the mode):
10773 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
10774 L1: const0 DW_OP_swap
10775 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
10776 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
10777 L3: DW_OP_drop
10778 L4: DW_OP_nop
10780 CTZ is similar:
10781 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
10782 L1: const0 DW_OP_swap
10783 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
10784 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
10785 L3: DW_OP_drop
10786 L4: DW_OP_nop
10788 FFS is similar:
10789 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
10790 L1: const1 DW_OP_swap
10791 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
10792 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
10793 L3: DW_OP_drop
10794 L4: DW_OP_nop */
10796 static dw_loc_descr_ref
10797 clz_loc_descriptor (rtx rtl, enum machine_mode mode,
10798 enum machine_mode mem_mode)
10800 dw_loc_descr_ref op0, ret, tmp;
10801 HOST_WIDE_INT valv;
10802 dw_loc_descr_ref l1jump, l1label;
10803 dw_loc_descr_ref l2jump, l2label;
10804 dw_loc_descr_ref l3jump, l3label;
10805 dw_loc_descr_ref l4jump, l4label;
10806 rtx msb;
10808 if (GET_MODE_CLASS (mode) != MODE_INT
10809 || GET_MODE (XEXP (rtl, 0)) != mode
10810 || (GET_CODE (rtl) == CLZ
10811 && GET_MODE_BITSIZE (mode) > HOST_BITS_PER_DOUBLE_INT))
10812 return NULL;
10814 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
10815 VAR_INIT_STATUS_INITIALIZED);
10816 if (op0 == NULL)
10817 return NULL;
10818 ret = op0;
10819 if (GET_CODE (rtl) == CLZ)
10821 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
10822 valv = GET_MODE_BITSIZE (mode);
10824 else if (GET_CODE (rtl) == FFS)
10825 valv = 0;
10826 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
10827 valv = GET_MODE_BITSIZE (mode);
10828 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
10829 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
10830 add_loc_descr (&ret, l1jump);
10831 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
10832 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
10833 VAR_INIT_STATUS_INITIALIZED);
10834 if (tmp == NULL)
10835 return NULL;
10836 add_loc_descr (&ret, tmp);
10837 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
10838 add_loc_descr (&ret, l4jump);
10839 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
10840 ? const1_rtx : const0_rtx,
10841 mode, mem_mode,
10842 VAR_INIT_STATUS_INITIALIZED);
10843 if (l1label == NULL)
10844 return NULL;
10845 add_loc_descr (&ret, l1label);
10846 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
10847 l2label = new_loc_descr (DW_OP_dup, 0, 0);
10848 add_loc_descr (&ret, l2label);
10849 if (GET_CODE (rtl) != CLZ)
10850 msb = const1_rtx;
10851 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10852 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
10853 << (GET_MODE_BITSIZE (mode) - 1));
10854 else
10855 msb = immed_double_const (0, (unsigned HOST_WIDE_INT) 1
10856 << (GET_MODE_BITSIZE (mode)
10857 - HOST_BITS_PER_WIDE_INT - 1), mode);
10858 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
10859 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
10860 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
10861 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
10862 else
10863 tmp = mem_loc_descriptor (msb, mode, mem_mode,
10864 VAR_INIT_STATUS_INITIALIZED);
10865 if (tmp == NULL)
10866 return NULL;
10867 add_loc_descr (&ret, tmp);
10868 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
10869 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
10870 add_loc_descr (&ret, l3jump);
10871 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
10872 VAR_INIT_STATUS_INITIALIZED);
10873 if (tmp == NULL)
10874 return NULL;
10875 add_loc_descr (&ret, tmp);
10876 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
10877 ? DW_OP_shl : DW_OP_shr, 0, 0));
10878 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
10879 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
10880 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
10881 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
10882 add_loc_descr (&ret, l2jump);
10883 l3label = new_loc_descr (DW_OP_drop, 0, 0);
10884 add_loc_descr (&ret, l3label);
10885 l4label = new_loc_descr (DW_OP_nop, 0, 0);
10886 add_loc_descr (&ret, l4label);
10887 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
10888 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
10889 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
10890 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
10891 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
10892 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
10893 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
10894 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
10895 return ret;
10898 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
10899 const1 is DW_OP_lit1 or corresponding typed constant):
10900 const0 DW_OP_swap
10901 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
10902 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
10903 L2: DW_OP_drop
10905 PARITY is similar:
10906 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
10907 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
10908 L2: DW_OP_drop */
10910 static dw_loc_descr_ref
10911 popcount_loc_descriptor (rtx rtl, enum machine_mode mode,
10912 enum machine_mode mem_mode)
10914 dw_loc_descr_ref op0, ret, tmp;
10915 dw_loc_descr_ref l1jump, l1label;
10916 dw_loc_descr_ref l2jump, l2label;
10918 if (GET_MODE_CLASS (mode) != MODE_INT
10919 || GET_MODE (XEXP (rtl, 0)) != mode)
10920 return NULL;
10922 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
10923 VAR_INIT_STATUS_INITIALIZED);
10924 if (op0 == NULL)
10925 return NULL;
10926 ret = op0;
10927 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
10928 VAR_INIT_STATUS_INITIALIZED);
10929 if (tmp == NULL)
10930 return NULL;
10931 add_loc_descr (&ret, tmp);
10932 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
10933 l1label = new_loc_descr (DW_OP_dup, 0, 0);
10934 add_loc_descr (&ret, l1label);
10935 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
10936 add_loc_descr (&ret, l2jump);
10937 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
10938 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
10939 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
10940 VAR_INIT_STATUS_INITIALIZED);
10941 if (tmp == NULL)
10942 return NULL;
10943 add_loc_descr (&ret, tmp);
10944 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
10945 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
10946 ? DW_OP_plus : DW_OP_xor, 0, 0));
10947 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
10948 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
10949 VAR_INIT_STATUS_INITIALIZED);
10950 add_loc_descr (&ret, tmp);
10951 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
10952 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
10953 add_loc_descr (&ret, l1jump);
10954 l2label = new_loc_descr (DW_OP_drop, 0, 0);
10955 add_loc_descr (&ret, l2label);
10956 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
10957 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
10958 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
10959 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
10960 return ret;
10963 /* BSWAP (constS is initial shift count, either 56 or 24):
10964 constS const0
10965 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
10966 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
10967 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
10968 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
10969 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
10971 static dw_loc_descr_ref
10972 bswap_loc_descriptor (rtx rtl, enum machine_mode mode,
10973 enum machine_mode mem_mode)
10975 dw_loc_descr_ref op0, ret, tmp;
10976 dw_loc_descr_ref l1jump, l1label;
10977 dw_loc_descr_ref l2jump, l2label;
10979 if (GET_MODE_CLASS (mode) != MODE_INT
10980 || BITS_PER_UNIT != 8
10981 || (GET_MODE_BITSIZE (mode) != 32
10982 && GET_MODE_BITSIZE (mode) != 64))
10983 return NULL;
10985 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
10986 VAR_INIT_STATUS_INITIALIZED);
10987 if (op0 == NULL)
10988 return NULL;
10990 ret = op0;
10991 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
10992 mode, mem_mode,
10993 VAR_INIT_STATUS_INITIALIZED);
10994 if (tmp == NULL)
10995 return NULL;
10996 add_loc_descr (&ret, tmp);
10997 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
10998 VAR_INIT_STATUS_INITIALIZED);
10999 if (tmp == NULL)
11000 return NULL;
11001 add_loc_descr (&ret, tmp);
11002 l1label = new_loc_descr (DW_OP_pick, 2, 0);
11003 add_loc_descr (&ret, l1label);
11004 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11005 mode, mem_mode,
11006 VAR_INIT_STATUS_INITIALIZED);
11007 add_loc_descr (&ret, tmp);
11008 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
11009 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11010 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11011 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
11012 VAR_INIT_STATUS_INITIALIZED);
11013 if (tmp == NULL)
11014 return NULL;
11015 add_loc_descr (&ret, tmp);
11016 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11017 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
11018 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11019 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11020 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11021 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11022 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11023 VAR_INIT_STATUS_INITIALIZED);
11024 add_loc_descr (&ret, tmp);
11025 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
11026 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11027 add_loc_descr (&ret, l2jump);
11028 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
11029 VAR_INIT_STATUS_INITIALIZED);
11030 add_loc_descr (&ret, tmp);
11031 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11032 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11033 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11034 add_loc_descr (&ret, l1jump);
11035 l2label = new_loc_descr (DW_OP_drop, 0, 0);
11036 add_loc_descr (&ret, l2label);
11037 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11038 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11039 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11040 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11041 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11042 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11043 return ret;
11046 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
11047 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11048 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
11049 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
11051 ROTATERT is similar:
11052 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
11053 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11054 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
11056 static dw_loc_descr_ref
11057 rotate_loc_descriptor (rtx rtl, enum machine_mode mode,
11058 enum machine_mode mem_mode)
11060 rtx rtlop1 = XEXP (rtl, 1);
11061 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
11062 int i;
11064 if (GET_MODE_CLASS (mode) != MODE_INT)
11065 return NULL;
11067 if (GET_MODE (rtlop1) != VOIDmode
11068 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
11069 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
11070 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11071 VAR_INIT_STATUS_INITIALIZED);
11072 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
11073 VAR_INIT_STATUS_INITIALIZED);
11074 if (op0 == NULL || op1 == NULL)
11075 return NULL;
11076 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11077 for (i = 0; i < 2; i++)
11079 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
11080 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
11081 mode, mem_mode,
11082 VAR_INIT_STATUS_INITIALIZED);
11083 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
11084 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11085 ? DW_OP_const4u
11086 : HOST_BITS_PER_WIDE_INT == 64
11087 ? DW_OP_const8u : DW_OP_constu,
11088 GET_MODE_MASK (mode), 0);
11089 else
11090 mask[i] = NULL;
11091 if (mask[i] == NULL)
11092 return NULL;
11093 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
11095 ret = op0;
11096 add_loc_descr (&ret, op1);
11097 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11098 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11099 if (GET_CODE (rtl) == ROTATERT)
11101 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11102 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11103 GET_MODE_BITSIZE (mode), 0));
11105 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11106 if (mask[0] != NULL)
11107 add_loc_descr (&ret, mask[0]);
11108 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11109 if (mask[1] != NULL)
11111 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11112 add_loc_descr (&ret, mask[1]);
11113 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11115 if (GET_CODE (rtl) == ROTATE)
11117 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11118 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11119 GET_MODE_BITSIZE (mode), 0));
11121 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11122 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11123 return ret;
11126 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
11127 for DEBUG_PARAMETER_REF RTL. */
11129 static dw_loc_descr_ref
11130 parameter_ref_descriptor (rtx rtl)
11132 dw_loc_descr_ref ret;
11133 dw_die_ref ref;
11135 if (dwarf_strict)
11136 return NULL;
11137 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
11138 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
11139 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
11140 if (ref)
11142 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11143 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
11144 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
11146 else
11148 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
11149 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
11151 return ret;
11154 /* The following routine converts the RTL for a variable or parameter
11155 (resident in memory) into an equivalent Dwarf representation of a
11156 mechanism for getting the address of that same variable onto the top of a
11157 hypothetical "address evaluation" stack.
11159 When creating memory location descriptors, we are effectively transforming
11160 the RTL for a memory-resident object into its Dwarf postfix expression
11161 equivalent. This routine recursively descends an RTL tree, turning
11162 it into Dwarf postfix code as it goes.
11164 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
11166 MEM_MODE is the mode of the memory reference, needed to handle some
11167 autoincrement addressing modes.
11169 Return 0 if we can't represent the location. */
11171 dw_loc_descr_ref
11172 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
11173 enum machine_mode mem_mode,
11174 enum var_init_status initialized)
11176 dw_loc_descr_ref mem_loc_result = NULL;
11177 enum dwarf_location_atom op;
11178 dw_loc_descr_ref op0, op1;
11180 if (mode == VOIDmode)
11181 mode = GET_MODE (rtl);
11183 /* Note that for a dynamically sized array, the location we will generate a
11184 description of here will be the lowest numbered location which is
11185 actually within the array. That's *not* necessarily the same as the
11186 zeroth element of the array. */
11188 rtl = targetm.delegitimize_address (rtl);
11190 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
11191 return NULL;
11193 switch (GET_CODE (rtl))
11195 case POST_INC:
11196 case POST_DEC:
11197 case POST_MODIFY:
11198 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
11200 case SUBREG:
11201 /* The case of a subreg may arise when we have a local (register)
11202 variable or a formal (register) parameter which doesn't quite fill
11203 up an entire register. For now, just assume that it is
11204 legitimate to make the Dwarf info refer to the whole register which
11205 contains the given subreg. */
11206 if (!subreg_lowpart_p (rtl))
11207 break;
11208 if (GET_MODE_CLASS (mode) == MODE_INT
11209 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (rtl))) == MODE_INT
11210 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11211 #ifdef POINTERS_EXTEND_UNSIGNED
11212 || (mode == Pmode && mem_mode != VOIDmode)
11213 #endif
11215 && GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))) <= DWARF2_ADDR_SIZE)
11217 mem_loc_result = mem_loc_descriptor (SUBREG_REG (rtl),
11218 GET_MODE (SUBREG_REG (rtl)),
11219 mem_mode, initialized);
11220 break;
11222 if (dwarf_strict)
11223 break;
11224 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))))
11225 break;
11226 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl)))
11227 && (GET_MODE_CLASS (mode) != MODE_INT
11228 || GET_MODE_CLASS (GET_MODE (SUBREG_REG (rtl))) != MODE_INT))
11229 break;
11230 else
11232 dw_die_ref type_die;
11233 dw_loc_descr_ref cvt;
11235 mem_loc_result = mem_loc_descriptor (SUBREG_REG (rtl),
11236 GET_MODE (SUBREG_REG (rtl)),
11237 mem_mode, initialized);
11238 if (mem_loc_result == NULL)
11239 break;
11240 type_die = base_type_for_mode (mode,
11241 GET_MODE_CLASS (mode) == MODE_INT);
11242 if (type_die == NULL)
11244 mem_loc_result = NULL;
11245 break;
11247 if (GET_MODE_SIZE (mode)
11248 != GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))))
11249 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11250 else
11251 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
11252 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11253 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11254 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11255 add_loc_descr (&mem_loc_result, cvt);
11257 break;
11259 case REG:
11260 if (GET_MODE_CLASS (mode) != MODE_INT
11261 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
11262 && rtl != arg_pointer_rtx
11263 && rtl != frame_pointer_rtx
11264 #ifdef POINTERS_EXTEND_UNSIGNED
11265 && (mode != Pmode || mem_mode == VOIDmode)
11266 #endif
11269 dw_die_ref type_die;
11271 if (dwarf_strict)
11272 break;
11273 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
11274 break;
11275 type_die = base_type_for_mode (mode,
11276 GET_MODE_CLASS (mode) == MODE_INT);
11277 if (type_die == NULL)
11278 break;
11279 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
11280 dbx_reg_number (rtl), 0);
11281 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
11282 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
11283 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
11284 break;
11286 /* Whenever a register number forms a part of the description of the
11287 method for calculating the (dynamic) address of a memory resident
11288 object, DWARF rules require the register number be referred to as
11289 a "base register". This distinction is not based in any way upon
11290 what category of register the hardware believes the given register
11291 belongs to. This is strictly DWARF terminology we're dealing with
11292 here. Note that in cases where the location of a memory-resident
11293 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
11294 OP_CONST (0)) the actual DWARF location descriptor that we generate
11295 may just be OP_BASEREG (basereg). This may look deceptively like
11296 the object in question was allocated to a register (rather than in
11297 memory) so DWARF consumers need to be aware of the subtle
11298 distinction between OP_REG and OP_BASEREG. */
11299 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
11300 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
11301 else if (stack_realign_drap
11302 && crtl->drap_reg
11303 && crtl->args.internal_arg_pointer == rtl
11304 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
11306 /* If RTL is internal_arg_pointer, which has been optimized
11307 out, use DRAP instead. */
11308 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
11309 VAR_INIT_STATUS_INITIALIZED);
11311 break;
11313 case SIGN_EXTEND:
11314 case ZERO_EXTEND:
11315 if (GET_MODE_CLASS (mode) != MODE_INT)
11316 break;
11317 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
11318 mem_mode, VAR_INIT_STATUS_INITIALIZED);
11319 if (op0 == 0)
11320 break;
11321 else if (GET_CODE (rtl) == ZERO_EXTEND
11322 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11323 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
11324 < HOST_BITS_PER_WIDE_INT
11325 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
11326 to expand zero extend as two shifts instead of
11327 masking. */
11328 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
11330 enum machine_mode imode = GET_MODE (XEXP (rtl, 0));
11331 mem_loc_result = op0;
11332 add_loc_descr (&mem_loc_result,
11333 int_loc_descriptor (GET_MODE_MASK (imode)));
11334 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
11336 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11338 int shift = DWARF2_ADDR_SIZE
11339 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
11340 shift *= BITS_PER_UNIT;
11341 if (GET_CODE (rtl) == SIGN_EXTEND)
11342 op = DW_OP_shra;
11343 else
11344 op = DW_OP_shr;
11345 mem_loc_result = op0;
11346 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
11347 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
11348 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
11349 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11351 else if (!dwarf_strict)
11353 dw_die_ref type_die1, type_die2;
11354 dw_loc_descr_ref cvt;
11356 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
11357 GET_CODE (rtl) == ZERO_EXTEND);
11358 if (type_die1 == NULL)
11359 break;
11360 type_die2 = base_type_for_mode (mode, 1);
11361 if (type_die2 == NULL)
11362 break;
11363 mem_loc_result = op0;
11364 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11365 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11366 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
11367 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11368 add_loc_descr (&mem_loc_result, cvt);
11369 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11370 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11371 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
11372 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11373 add_loc_descr (&mem_loc_result, cvt);
11375 break;
11377 case MEM:
11379 rtx new_rtl = avoid_constant_pool_reference (rtl);
11380 if (new_rtl != rtl)
11382 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
11383 initialized);
11384 if (mem_loc_result != NULL)
11385 return mem_loc_result;
11388 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
11389 get_address_mode (rtl), mode,
11390 VAR_INIT_STATUS_INITIALIZED);
11391 if (mem_loc_result == NULL)
11392 mem_loc_result = tls_mem_loc_descriptor (rtl);
11393 if (mem_loc_result != NULL)
11395 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
11396 || GET_MODE_CLASS (mode) != MODE_INT)
11398 dw_die_ref type_die;
11399 dw_loc_descr_ref deref;
11401 if (dwarf_strict)
11402 return NULL;
11403 type_die
11404 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
11405 if (type_die == NULL)
11406 return NULL;
11407 deref = new_loc_descr (DW_OP_GNU_deref_type,
11408 GET_MODE_SIZE (mode), 0);
11409 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
11410 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
11411 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
11412 add_loc_descr (&mem_loc_result, deref);
11414 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11415 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
11416 else
11417 add_loc_descr (&mem_loc_result,
11418 new_loc_descr (DW_OP_deref_size,
11419 GET_MODE_SIZE (mode), 0));
11421 break;
11423 case LO_SUM:
11424 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
11426 case LABEL_REF:
11427 /* Some ports can transform a symbol ref into a label ref, because
11428 the symbol ref is too far away and has to be dumped into a constant
11429 pool. */
11430 case CONST:
11431 case SYMBOL_REF:
11432 if (GET_MODE_CLASS (mode) != MODE_INT
11433 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
11434 #ifdef POINTERS_EXTEND_UNSIGNED
11435 && (mode != Pmode || mem_mode == VOIDmode)
11436 #endif
11438 break;
11439 if (GET_CODE (rtl) == SYMBOL_REF
11440 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11442 dw_loc_descr_ref temp;
11444 /* If this is not defined, we have no way to emit the data. */
11445 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
11446 break;
11448 /* We used to emit DW_OP_addr here, but that's wrong, since
11449 DW_OP_addr should be relocated by the debug info consumer,
11450 while DW_OP_GNU_push_tls_address operand should not. */
11451 temp = new_loc_descr (DWARF2_ADDR_SIZE == 4
11452 ? DW_OP_const4u : DW_OP_const8u, 0, 0);
11453 temp->dw_loc_oprnd1.val_class = dw_val_class_addr;
11454 temp->dw_loc_oprnd1.v.val_addr = rtl;
11455 temp->dtprel = true;
11457 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
11458 add_loc_descr (&mem_loc_result, temp);
11460 break;
11463 if (!const_ok_for_output (rtl))
11464 break;
11466 symref:
11467 mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
11468 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
11469 mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
11470 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
11471 break;
11473 case CONCAT:
11474 case CONCATN:
11475 case VAR_LOCATION:
11476 case DEBUG_IMPLICIT_PTR:
11477 expansion_failed (NULL_TREE, rtl,
11478 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
11479 return 0;
11481 case ENTRY_VALUE:
11482 if (dwarf_strict)
11483 return NULL;
11484 if (REG_P (ENTRY_VALUE_EXP (rtl)))
11486 if (GET_MODE_CLASS (mode) != MODE_INT
11487 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11488 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
11489 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
11490 else
11492 = one_reg_loc_descriptor (dbx_reg_number (ENTRY_VALUE_EXP (rtl)),
11493 VAR_INIT_STATUS_INITIALIZED);
11495 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
11496 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
11498 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
11499 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
11500 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
11501 return NULL;
11503 else
11504 gcc_unreachable ();
11505 if (op0 == NULL)
11506 return NULL;
11507 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
11508 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
11509 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
11510 break;
11512 case DEBUG_PARAMETER_REF:
11513 mem_loc_result = parameter_ref_descriptor (rtl);
11514 break;
11516 case PRE_MODIFY:
11517 /* Extract the PLUS expression nested inside and fall into
11518 PLUS code below. */
11519 rtl = XEXP (rtl, 1);
11520 goto plus;
11522 case PRE_INC:
11523 case PRE_DEC:
11524 /* Turn these into a PLUS expression and fall into the PLUS code
11525 below. */
11526 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
11527 GEN_INT (GET_CODE (rtl) == PRE_INC
11528 ? GET_MODE_UNIT_SIZE (mem_mode)
11529 : -GET_MODE_UNIT_SIZE (mem_mode)));
11531 /* ... fall through ... */
11533 case PLUS:
11534 plus:
11535 if (is_based_loc (rtl)
11536 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11537 || XEXP (rtl, 0) == arg_pointer_rtx
11538 || XEXP (rtl, 0) == frame_pointer_rtx)
11539 && GET_MODE_CLASS (mode) == MODE_INT)
11540 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
11541 INTVAL (XEXP (rtl, 1)),
11542 VAR_INIT_STATUS_INITIALIZED);
11543 else
11545 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11546 VAR_INIT_STATUS_INITIALIZED);
11547 if (mem_loc_result == 0)
11548 break;
11550 if (CONST_INT_P (XEXP (rtl, 1))
11551 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11552 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
11553 else
11555 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11556 VAR_INIT_STATUS_INITIALIZED);
11557 if (op1 == 0)
11558 break;
11559 add_loc_descr (&mem_loc_result, op1);
11560 add_loc_descr (&mem_loc_result,
11561 new_loc_descr (DW_OP_plus, 0, 0));
11564 break;
11566 /* If a pseudo-reg is optimized away, it is possible for it to
11567 be replaced with a MEM containing a multiply or shift. */
11568 case MINUS:
11569 op = DW_OP_minus;
11570 goto do_binop;
11572 case MULT:
11573 op = DW_OP_mul;
11574 goto do_binop;
11576 case DIV:
11577 if (!dwarf_strict
11578 && GET_MODE_CLASS (mode) == MODE_INT
11579 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11581 mem_loc_result = typed_binop (DW_OP_div, rtl,
11582 base_type_for_mode (mode, 0),
11583 mode, mem_mode);
11584 break;
11586 op = DW_OP_div;
11587 goto do_binop;
11589 case UMOD:
11590 op = DW_OP_mod;
11591 goto do_binop;
11593 case ASHIFT:
11594 op = DW_OP_shl;
11595 goto do_shift;
11597 case ASHIFTRT:
11598 op = DW_OP_shra;
11599 goto do_shift;
11601 case LSHIFTRT:
11602 op = DW_OP_shr;
11603 goto do_shift;
11605 do_shift:
11606 if (GET_MODE_CLASS (mode) != MODE_INT)
11607 break;
11608 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11609 VAR_INIT_STATUS_INITIALIZED);
11611 rtx rtlop1 = XEXP (rtl, 1);
11612 if (GET_MODE (rtlop1) != VOIDmode
11613 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
11614 < GET_MODE_BITSIZE (mode))
11615 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
11616 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
11617 VAR_INIT_STATUS_INITIALIZED);
11620 if (op0 == 0 || op1 == 0)
11621 break;
11623 mem_loc_result = op0;
11624 add_loc_descr (&mem_loc_result, op1);
11625 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11626 break;
11628 case AND:
11629 op = DW_OP_and;
11630 goto do_binop;
11632 case IOR:
11633 op = DW_OP_or;
11634 goto do_binop;
11636 case XOR:
11637 op = DW_OP_xor;
11638 goto do_binop;
11640 do_binop:
11641 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11642 VAR_INIT_STATUS_INITIALIZED);
11643 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11644 VAR_INIT_STATUS_INITIALIZED);
11646 if (op0 == 0 || op1 == 0)
11647 break;
11649 mem_loc_result = op0;
11650 add_loc_descr (&mem_loc_result, op1);
11651 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11652 break;
11654 case MOD:
11655 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
11657 mem_loc_result = typed_binop (DW_OP_mod, rtl,
11658 base_type_for_mode (mode, 0),
11659 mode, mem_mode);
11660 break;
11663 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11664 VAR_INIT_STATUS_INITIALIZED);
11665 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11666 VAR_INIT_STATUS_INITIALIZED);
11668 if (op0 == 0 || op1 == 0)
11669 break;
11671 mem_loc_result = op0;
11672 add_loc_descr (&mem_loc_result, op1);
11673 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
11674 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
11675 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
11676 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
11677 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
11678 break;
11680 case UDIV:
11681 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
11683 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
11685 op = DW_OP_div;
11686 goto do_binop;
11688 mem_loc_result = typed_binop (DW_OP_div, rtl,
11689 base_type_for_mode (mode, 1),
11690 mode, mem_mode);
11692 break;
11694 case NOT:
11695 op = DW_OP_not;
11696 goto do_unop;
11698 case ABS:
11699 op = DW_OP_abs;
11700 goto do_unop;
11702 case NEG:
11703 op = DW_OP_neg;
11704 goto do_unop;
11706 do_unop:
11707 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11708 VAR_INIT_STATUS_INITIALIZED);
11710 if (op0 == 0)
11711 break;
11713 mem_loc_result = op0;
11714 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11715 break;
11717 case CONST_INT:
11718 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11719 #ifdef POINTERS_EXTEND_UNSIGNED
11720 || (mode == Pmode
11721 && mem_mode != VOIDmode
11722 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
11723 #endif
11726 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
11727 break;
11729 if (!dwarf_strict
11730 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
11731 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
11733 dw_die_ref type_die = base_type_for_mode (mode, 1);
11734 enum machine_mode amode;
11735 if (type_die == NULL)
11736 return NULL;
11737 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
11738 MODE_INT, 0);
11739 if (INTVAL (rtl) >= 0
11740 && amode != BLKmode
11741 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
11742 /* const DW_OP_GNU_convert <XXX> vs.
11743 DW_OP_GNU_const_type <XXX, 1, const>. */
11744 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
11745 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
11747 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
11748 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11749 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11750 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11751 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
11752 add_loc_descr (&mem_loc_result, op0);
11753 return mem_loc_result;
11755 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
11756 INTVAL (rtl));
11757 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11758 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11759 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
11760 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
11761 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11762 else
11764 mem_loc_result->dw_loc_oprnd2.val_class
11765 = dw_val_class_const_double;
11766 mem_loc_result->dw_loc_oprnd2.v.val_double
11767 = double_int::from_shwi (INTVAL (rtl));
11770 break;
11772 case CONST_DOUBLE:
11773 if (!dwarf_strict)
11775 dw_die_ref type_die;
11777 /* Note that a CONST_DOUBLE rtx could represent either an integer
11778 or a floating-point constant. A CONST_DOUBLE is used whenever
11779 the constant requires more than one word in order to be
11780 adequately represented. We output CONST_DOUBLEs as blocks. */
11781 if (mode == VOIDmode
11782 || (GET_MODE (rtl) == VOIDmode
11783 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
11784 break;
11785 type_die = base_type_for_mode (mode,
11786 GET_MODE_CLASS (mode) == MODE_INT);
11787 if (type_die == NULL)
11788 return NULL;
11789 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
11790 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11791 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11792 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
11793 if (SCALAR_FLOAT_MODE_P (mode))
11795 unsigned int length = GET_MODE_SIZE (mode);
11796 unsigned char *array
11797 = (unsigned char*) ggc_alloc_atomic (length);
11799 insert_float (rtl, array);
11800 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
11801 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
11802 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
11803 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
11805 else
11807 mem_loc_result->dw_loc_oprnd2.val_class
11808 = dw_val_class_const_double;
11809 mem_loc_result->dw_loc_oprnd2.v.val_double
11810 = rtx_to_double_int (rtl);
11813 break;
11815 case EQ:
11816 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
11817 break;
11819 case GE:
11820 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
11821 break;
11823 case GT:
11824 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
11825 break;
11827 case LE:
11828 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
11829 break;
11831 case LT:
11832 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
11833 break;
11835 case NE:
11836 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
11837 break;
11839 case GEU:
11840 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
11841 break;
11843 case GTU:
11844 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
11845 break;
11847 case LEU:
11848 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
11849 break;
11851 case LTU:
11852 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
11853 break;
11855 case UMIN:
11856 case UMAX:
11857 if (GET_MODE_CLASS (mode) != MODE_INT)
11858 break;
11859 /* FALLTHRU */
11860 case SMIN:
11861 case SMAX:
11862 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
11863 break;
11865 case ZERO_EXTRACT:
11866 case SIGN_EXTRACT:
11867 if (CONST_INT_P (XEXP (rtl, 1))
11868 && CONST_INT_P (XEXP (rtl, 2))
11869 && ((unsigned) INTVAL (XEXP (rtl, 1))
11870 + (unsigned) INTVAL (XEXP (rtl, 2))
11871 <= GET_MODE_BITSIZE (mode))
11872 && GET_MODE_CLASS (mode) == MODE_INT
11873 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11874 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
11876 int shift, size;
11877 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
11878 mem_mode, VAR_INIT_STATUS_INITIALIZED);
11879 if (op0 == 0)
11880 break;
11881 if (GET_CODE (rtl) == SIGN_EXTRACT)
11882 op = DW_OP_shra;
11883 else
11884 op = DW_OP_shr;
11885 mem_loc_result = op0;
11886 size = INTVAL (XEXP (rtl, 1));
11887 shift = INTVAL (XEXP (rtl, 2));
11888 if (BITS_BIG_ENDIAN)
11889 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
11890 - shift - size;
11891 if (shift + size != (int) DWARF2_ADDR_SIZE)
11893 add_loc_descr (&mem_loc_result,
11894 int_loc_descriptor (DWARF2_ADDR_SIZE
11895 - shift - size));
11896 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
11898 if (size != (int) DWARF2_ADDR_SIZE)
11900 add_loc_descr (&mem_loc_result,
11901 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
11902 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11905 break;
11907 case IF_THEN_ELSE:
11909 dw_loc_descr_ref op2, bra_node, drop_node;
11910 op0 = mem_loc_descriptor (XEXP (rtl, 0),
11911 GET_MODE (XEXP (rtl, 0)) == VOIDmode
11912 ? word_mode : GET_MODE (XEXP (rtl, 0)),
11913 mem_mode, VAR_INIT_STATUS_INITIALIZED);
11914 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11915 VAR_INIT_STATUS_INITIALIZED);
11916 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
11917 VAR_INIT_STATUS_INITIALIZED);
11918 if (op0 == NULL || op1 == NULL || op2 == NULL)
11919 break;
11921 mem_loc_result = op1;
11922 add_loc_descr (&mem_loc_result, op2);
11923 add_loc_descr (&mem_loc_result, op0);
11924 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11925 add_loc_descr (&mem_loc_result, bra_node);
11926 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
11927 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11928 add_loc_descr (&mem_loc_result, drop_node);
11929 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11930 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11932 break;
11934 case FLOAT_EXTEND:
11935 case FLOAT_TRUNCATE:
11936 case FLOAT:
11937 case UNSIGNED_FLOAT:
11938 case FIX:
11939 case UNSIGNED_FIX:
11940 if (!dwarf_strict)
11942 dw_die_ref type_die;
11943 dw_loc_descr_ref cvt;
11945 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
11946 mem_mode, VAR_INIT_STATUS_INITIALIZED);
11947 if (op0 == NULL)
11948 break;
11949 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
11950 && (GET_CODE (rtl) == FLOAT
11951 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
11952 <= DWARF2_ADDR_SIZE))
11954 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
11955 GET_CODE (rtl) == UNSIGNED_FLOAT);
11956 if (type_die == NULL)
11957 break;
11958 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11959 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11960 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11961 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11962 add_loc_descr (&op0, cvt);
11964 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
11965 if (type_die == NULL)
11966 break;
11967 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11968 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11969 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11970 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11971 add_loc_descr (&op0, cvt);
11972 if (GET_MODE_CLASS (mode) == MODE_INT
11973 && (GET_CODE (rtl) == FIX
11974 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
11976 op0 = convert_descriptor_to_mode (mode, op0);
11977 if (op0 == NULL)
11978 break;
11980 mem_loc_result = op0;
11982 break;
11984 case CLZ:
11985 case CTZ:
11986 case FFS:
11987 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
11988 break;
11990 case POPCOUNT:
11991 case PARITY:
11992 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
11993 break;
11995 case BSWAP:
11996 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
11997 break;
11999 case ROTATE:
12000 case ROTATERT:
12001 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
12002 break;
12004 case COMPARE:
12005 case TRUNCATE:
12006 /* In theory, we could implement the above. */
12007 /* DWARF cannot represent the unsigned compare operations
12008 natively. */
12009 case SS_MULT:
12010 case US_MULT:
12011 case SS_DIV:
12012 case US_DIV:
12013 case SS_PLUS:
12014 case US_PLUS:
12015 case SS_MINUS:
12016 case US_MINUS:
12017 case SS_NEG:
12018 case US_NEG:
12019 case SS_ABS:
12020 case SS_ASHIFT:
12021 case US_ASHIFT:
12022 case SS_TRUNCATE:
12023 case US_TRUNCATE:
12024 case UNORDERED:
12025 case ORDERED:
12026 case UNEQ:
12027 case UNGE:
12028 case UNGT:
12029 case UNLE:
12030 case UNLT:
12031 case LTGT:
12032 case FRACT_CONVERT:
12033 case UNSIGNED_FRACT_CONVERT:
12034 case SAT_FRACT:
12035 case UNSIGNED_SAT_FRACT:
12036 case SQRT:
12037 case ASM_OPERANDS:
12038 case VEC_MERGE:
12039 case VEC_SELECT:
12040 case VEC_CONCAT:
12041 case VEC_DUPLICATE:
12042 case UNSPEC:
12043 case HIGH:
12044 case FMA:
12045 case STRICT_LOW_PART:
12046 case CONST_VECTOR:
12047 case CONST_FIXED:
12048 case CLRSB:
12049 /* If delegitimize_address couldn't do anything with the UNSPEC, we
12050 can't express it in the debug info. This can happen e.g. with some
12051 TLS UNSPECs. */
12052 break;
12054 case CONST_STRING:
12055 resolve_one_addr (&rtl, NULL);
12056 goto symref;
12058 default:
12059 #ifdef ENABLE_CHECKING
12060 print_rtl (stderr, rtl);
12061 gcc_unreachable ();
12062 #else
12063 break;
12064 #endif
12067 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12068 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12070 return mem_loc_result;
12073 /* Return a descriptor that describes the concatenation of two locations.
12074 This is typically a complex variable. */
12076 static dw_loc_descr_ref
12077 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
12079 dw_loc_descr_ref cc_loc_result = NULL;
12080 dw_loc_descr_ref x0_ref
12081 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12082 dw_loc_descr_ref x1_ref
12083 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12085 if (x0_ref == 0 || x1_ref == 0)
12086 return 0;
12088 cc_loc_result = x0_ref;
12089 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
12091 add_loc_descr (&cc_loc_result, x1_ref);
12092 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
12094 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
12095 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12097 return cc_loc_result;
12100 /* Return a descriptor that describes the concatenation of N
12101 locations. */
12103 static dw_loc_descr_ref
12104 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
12106 unsigned int i;
12107 dw_loc_descr_ref cc_loc_result = NULL;
12108 unsigned int n = XVECLEN (concatn, 0);
12110 for (i = 0; i < n; ++i)
12112 dw_loc_descr_ref ref;
12113 rtx x = XVECEXP (concatn, 0, i);
12115 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12116 if (ref == NULL)
12117 return NULL;
12119 add_loc_descr (&cc_loc_result, ref);
12120 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
12123 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12124 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12126 return cc_loc_result;
12129 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
12130 for DEBUG_IMPLICIT_PTR RTL. */
12132 static dw_loc_descr_ref
12133 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
12135 dw_loc_descr_ref ret;
12136 dw_die_ref ref;
12138 if (dwarf_strict)
12139 return NULL;
12140 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
12141 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
12142 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
12143 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
12144 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
12145 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
12146 if (ref)
12148 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12149 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12150 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12152 else
12154 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12155 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
12157 return ret;
12160 /* Output a proper Dwarf location descriptor for a variable or parameter
12161 which is either allocated in a register or in a memory location. For a
12162 register, we just generate an OP_REG and the register number. For a
12163 memory location we provide a Dwarf postfix expression describing how to
12164 generate the (dynamic) address of the object onto the address stack.
12166 MODE is mode of the decl if this loc_descriptor is going to be used in
12167 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
12168 allowed, VOIDmode otherwise.
12170 If we don't know how to describe it, return 0. */
12172 static dw_loc_descr_ref
12173 loc_descriptor (rtx rtl, enum machine_mode mode,
12174 enum var_init_status initialized)
12176 dw_loc_descr_ref loc_result = NULL;
12178 switch (GET_CODE (rtl))
12180 case SUBREG:
12181 /* The case of a subreg may arise when we have a local (register)
12182 variable or a formal (register) parameter which doesn't quite fill
12183 up an entire register. For now, just assume that it is
12184 legitimate to make the Dwarf info refer to the whole register which
12185 contains the given subreg. */
12186 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
12187 loc_result = loc_descriptor (SUBREG_REG (rtl),
12188 GET_MODE (SUBREG_REG (rtl)), initialized);
12189 else
12190 goto do_default;
12191 break;
12193 case REG:
12194 loc_result = reg_loc_descriptor (rtl, initialized);
12195 break;
12197 case MEM:
12198 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
12199 GET_MODE (rtl), initialized);
12200 if (loc_result == NULL)
12201 loc_result = tls_mem_loc_descriptor (rtl);
12202 if (loc_result == NULL)
12204 rtx new_rtl = avoid_constant_pool_reference (rtl);
12205 if (new_rtl != rtl)
12206 loc_result = loc_descriptor (new_rtl, mode, initialized);
12208 break;
12210 case CONCAT:
12211 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
12212 initialized);
12213 break;
12215 case CONCATN:
12216 loc_result = concatn_loc_descriptor (rtl, initialized);
12217 break;
12219 case VAR_LOCATION:
12220 /* Single part. */
12221 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
12223 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
12224 if (GET_CODE (loc) == EXPR_LIST)
12225 loc = XEXP (loc, 0);
12226 loc_result = loc_descriptor (loc, mode, initialized);
12227 break;
12230 rtl = XEXP (rtl, 1);
12231 /* FALLTHRU */
12233 case PARALLEL:
12235 rtvec par_elems = XVEC (rtl, 0);
12236 int num_elem = GET_NUM_ELEM (par_elems);
12237 enum machine_mode mode;
12238 int i;
12240 /* Create the first one, so we have something to add to. */
12241 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
12242 VOIDmode, initialized);
12243 if (loc_result == NULL)
12244 return NULL;
12245 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
12246 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
12247 for (i = 1; i < num_elem; i++)
12249 dw_loc_descr_ref temp;
12251 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
12252 VOIDmode, initialized);
12253 if (temp == NULL)
12254 return NULL;
12255 add_loc_descr (&loc_result, temp);
12256 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
12257 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
12260 break;
12262 case CONST_INT:
12263 if (mode != VOIDmode && mode != BLKmode)
12264 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
12265 INTVAL (rtl));
12266 break;
12268 case CONST_DOUBLE:
12269 if (mode == VOIDmode)
12270 mode = GET_MODE (rtl);
12272 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
12274 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
12276 /* Note that a CONST_DOUBLE rtx could represent either an integer
12277 or a floating-point constant. A CONST_DOUBLE is used whenever
12278 the constant requires more than one word in order to be
12279 adequately represented. We output CONST_DOUBLEs as blocks. */
12280 loc_result = new_loc_descr (DW_OP_implicit_value,
12281 GET_MODE_SIZE (mode), 0);
12282 if (SCALAR_FLOAT_MODE_P (mode))
12284 unsigned int length = GET_MODE_SIZE (mode);
12285 unsigned char *array
12286 = (unsigned char*) ggc_alloc_atomic (length);
12288 insert_float (rtl, array);
12289 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12290 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12291 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12292 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12294 else
12296 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
12297 loc_result->dw_loc_oprnd2.v.val_double
12298 = rtx_to_double_int (rtl);
12301 break;
12303 case CONST_VECTOR:
12304 if (mode == VOIDmode)
12305 mode = GET_MODE (rtl);
12307 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
12309 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
12310 unsigned int length = CONST_VECTOR_NUNITS (rtl);
12311 unsigned char *array = (unsigned char *)
12312 ggc_alloc_atomic (length * elt_size);
12313 unsigned int i;
12314 unsigned char *p;
12316 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
12317 switch (GET_MODE_CLASS (mode))
12319 case MODE_VECTOR_INT:
12320 for (i = 0, p = array; i < length; i++, p += elt_size)
12322 rtx elt = CONST_VECTOR_ELT (rtl, i);
12323 double_int val = rtx_to_double_int (elt);
12325 if (elt_size <= sizeof (HOST_WIDE_INT))
12326 insert_int (val.to_shwi (), elt_size, p);
12327 else
12329 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
12330 insert_double (val, p);
12333 break;
12335 case MODE_VECTOR_FLOAT:
12336 for (i = 0, p = array; i < length; i++, p += elt_size)
12338 rtx elt = CONST_VECTOR_ELT (rtl, i);
12339 insert_float (elt, p);
12341 break;
12343 default:
12344 gcc_unreachable ();
12347 loc_result = new_loc_descr (DW_OP_implicit_value,
12348 length * elt_size, 0);
12349 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12350 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
12351 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
12352 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12354 break;
12356 case CONST:
12357 if (mode == VOIDmode
12358 || GET_CODE (XEXP (rtl, 0)) == CONST_INT
12359 || GET_CODE (XEXP (rtl, 0)) == CONST_DOUBLE
12360 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
12362 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
12363 break;
12365 /* FALLTHROUGH */
12366 case SYMBOL_REF:
12367 if (!const_ok_for_output (rtl))
12368 break;
12369 case LABEL_REF:
12370 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
12371 && (dwarf_version >= 4 || !dwarf_strict))
12373 loc_result = new_loc_descr (DW_OP_addr, 0, 0);
12374 loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
12375 loc_result->dw_loc_oprnd1.v.val_addr = rtl;
12376 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
12377 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
12379 break;
12381 case DEBUG_IMPLICIT_PTR:
12382 loc_result = implicit_ptr_descriptor (rtl, 0);
12383 break;
12385 case PLUS:
12386 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
12387 && CONST_INT_P (XEXP (rtl, 1)))
12389 loc_result
12390 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
12391 break;
12393 /* FALLTHRU */
12394 do_default:
12395 default:
12396 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
12397 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
12398 && dwarf_version >= 4)
12399 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
12401 /* Value expression. */
12402 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
12403 if (loc_result)
12404 add_loc_descr (&loc_result,
12405 new_loc_descr (DW_OP_stack_value, 0, 0));
12407 break;
12410 return loc_result;
12413 /* We need to figure out what section we should use as the base for the
12414 address ranges where a given location is valid.
12415 1. If this particular DECL has a section associated with it, use that.
12416 2. If this function has a section associated with it, use that.
12417 3. Otherwise, use the text section.
12418 XXX: If you split a variable across multiple sections, we won't notice. */
12420 static const char *
12421 secname_for_decl (const_tree decl)
12423 const char *secname;
12425 if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
12427 tree sectree = DECL_SECTION_NAME (decl);
12428 secname = TREE_STRING_POINTER (sectree);
12430 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
12432 tree sectree = DECL_SECTION_NAME (current_function_decl);
12433 secname = TREE_STRING_POINTER (sectree);
12435 else if (cfun && in_cold_section_p)
12436 secname = crtl->subsections.cold_section_label;
12437 else
12438 secname = text_section_label;
12440 return secname;
12443 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
12445 static bool
12446 decl_by_reference_p (tree decl)
12448 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
12449 || TREE_CODE (decl) == VAR_DECL)
12450 && DECL_BY_REFERENCE (decl));
12453 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
12454 for VARLOC. */
12456 static dw_loc_descr_ref
12457 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
12458 enum var_init_status initialized)
12460 int have_address = 0;
12461 dw_loc_descr_ref descr;
12462 enum machine_mode mode;
12464 if (want_address != 2)
12466 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
12467 /* Single part. */
12468 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
12470 varloc = PAT_VAR_LOCATION_LOC (varloc);
12471 if (GET_CODE (varloc) == EXPR_LIST)
12472 varloc = XEXP (varloc, 0);
12473 mode = GET_MODE (varloc);
12474 if (MEM_P (varloc))
12476 rtx addr = XEXP (varloc, 0);
12477 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
12478 mode, initialized);
12479 if (descr)
12480 have_address = 1;
12481 else
12483 rtx x = avoid_constant_pool_reference (varloc);
12484 if (x != varloc)
12485 descr = mem_loc_descriptor (x, mode, VOIDmode,
12486 initialized);
12489 else
12490 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
12492 else
12493 return 0;
12495 else
12497 if (GET_CODE (varloc) == VAR_LOCATION)
12498 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
12499 else
12500 mode = DECL_MODE (loc);
12501 descr = loc_descriptor (varloc, mode, initialized);
12502 have_address = 1;
12505 if (!descr)
12506 return 0;
12508 if (want_address == 2 && !have_address
12509 && (dwarf_version >= 4 || !dwarf_strict))
12511 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
12513 expansion_failed (loc, NULL_RTX,
12514 "DWARF address size mismatch");
12515 return 0;
12517 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
12518 have_address = 1;
12520 /* Show if we can't fill the request for an address. */
12521 if (want_address && !have_address)
12523 expansion_failed (loc, NULL_RTX,
12524 "Want address and only have value");
12525 return 0;
12528 /* If we've got an address and don't want one, dereference. */
12529 if (!want_address && have_address)
12531 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
12532 enum dwarf_location_atom op;
12534 if (size > DWARF2_ADDR_SIZE || size == -1)
12536 expansion_failed (loc, NULL_RTX,
12537 "DWARF address size mismatch");
12538 return 0;
12540 else if (size == DWARF2_ADDR_SIZE)
12541 op = DW_OP_deref;
12542 else
12543 op = DW_OP_deref_size;
12545 add_loc_descr (&descr, new_loc_descr (op, size, 0));
12548 return descr;
12551 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
12552 if it is not possible. */
12554 static dw_loc_descr_ref
12555 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
12557 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
12558 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
12559 else if (dwarf_version >= 3 || !dwarf_strict)
12560 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
12561 else
12562 return NULL;
12565 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
12566 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
12568 static dw_loc_descr_ref
12569 dw_sra_loc_expr (tree decl, rtx loc)
12571 rtx p;
12572 unsigned int padsize = 0;
12573 dw_loc_descr_ref descr, *descr_tail;
12574 unsigned HOST_WIDE_INT decl_size;
12575 rtx varloc;
12576 enum var_init_status initialized;
12578 if (DECL_SIZE (decl) == NULL
12579 || !host_integerp (DECL_SIZE (decl), 1))
12580 return NULL;
12582 decl_size = tree_low_cst (DECL_SIZE (decl), 1);
12583 descr = NULL;
12584 descr_tail = &descr;
12586 for (p = loc; p; p = XEXP (p, 1))
12588 unsigned int bitsize = decl_piece_bitsize (p);
12589 rtx loc_note = *decl_piece_varloc_ptr (p);
12590 dw_loc_descr_ref cur_descr;
12591 dw_loc_descr_ref *tail, last = NULL;
12592 unsigned int opsize = 0;
12594 if (loc_note == NULL_RTX
12595 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
12597 padsize += bitsize;
12598 continue;
12600 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
12601 varloc = NOTE_VAR_LOCATION (loc_note);
12602 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
12603 if (cur_descr == NULL)
12605 padsize += bitsize;
12606 continue;
12609 /* Check that cur_descr either doesn't use
12610 DW_OP_*piece operations, or their sum is equal
12611 to bitsize. Otherwise we can't embed it. */
12612 for (tail = &cur_descr; *tail != NULL;
12613 tail = &(*tail)->dw_loc_next)
12614 if ((*tail)->dw_loc_opc == DW_OP_piece)
12616 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
12617 * BITS_PER_UNIT;
12618 last = *tail;
12620 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
12622 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
12623 last = *tail;
12626 if (last != NULL && opsize != bitsize)
12628 padsize += bitsize;
12629 continue;
12632 /* If there is a hole, add DW_OP_*piece after empty DWARF
12633 expression, which means that those bits are optimized out. */
12634 if (padsize)
12636 if (padsize > decl_size)
12637 return NULL;
12638 decl_size -= padsize;
12639 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
12640 if (*descr_tail == NULL)
12641 return NULL;
12642 descr_tail = &(*descr_tail)->dw_loc_next;
12643 padsize = 0;
12645 *descr_tail = cur_descr;
12646 descr_tail = tail;
12647 if (bitsize > decl_size)
12648 return NULL;
12649 decl_size -= bitsize;
12650 if (last == NULL)
12652 HOST_WIDE_INT offset = 0;
12653 if (GET_CODE (varloc) == VAR_LOCATION
12654 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
12656 varloc = PAT_VAR_LOCATION_LOC (varloc);
12657 if (GET_CODE (varloc) == EXPR_LIST)
12658 varloc = XEXP (varloc, 0);
12662 if (GET_CODE (varloc) == CONST
12663 || GET_CODE (varloc) == SIGN_EXTEND
12664 || GET_CODE (varloc) == ZERO_EXTEND)
12665 varloc = XEXP (varloc, 0);
12666 else if (GET_CODE (varloc) == SUBREG)
12667 varloc = SUBREG_REG (varloc);
12668 else
12669 break;
12671 while (1);
12672 /* DW_OP_bit_size offset should be zero for register
12673 or implicit location descriptions and empty location
12674 descriptions, but for memory addresses needs big endian
12675 adjustment. */
12676 if (MEM_P (varloc))
12678 unsigned HOST_WIDE_INT memsize
12679 = MEM_SIZE (varloc) * BITS_PER_UNIT;
12680 if (memsize != bitsize)
12682 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
12683 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
12684 return NULL;
12685 if (memsize < bitsize)
12686 return NULL;
12687 if (BITS_BIG_ENDIAN)
12688 offset = memsize - bitsize;
12692 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
12693 if (*descr_tail == NULL)
12694 return NULL;
12695 descr_tail = &(*descr_tail)->dw_loc_next;
12699 /* If there were any non-empty expressions, add padding till the end of
12700 the decl. */
12701 if (descr != NULL && decl_size != 0)
12703 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
12704 if (*descr_tail == NULL)
12705 return NULL;
12707 return descr;
12710 /* Return the dwarf representation of the location list LOC_LIST of
12711 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
12712 function. */
12714 static dw_loc_list_ref
12715 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
12717 const char *endname, *secname;
12718 rtx varloc;
12719 enum var_init_status initialized;
12720 struct var_loc_node *node;
12721 dw_loc_descr_ref descr;
12722 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
12723 dw_loc_list_ref list = NULL;
12724 dw_loc_list_ref *listp = &list;
12726 /* Now that we know what section we are using for a base,
12727 actually construct the list of locations.
12728 The first location information is what is passed to the
12729 function that creates the location list, and the remaining
12730 locations just get added on to that list.
12731 Note that we only know the start address for a location
12732 (IE location changes), so to build the range, we use
12733 the range [current location start, next location start].
12734 This means we have to special case the last node, and generate
12735 a range of [last location start, end of function label]. */
12737 secname = secname_for_decl (decl);
12739 for (node = loc_list->first; node; node = node->next)
12740 if (GET_CODE (node->loc) == EXPR_LIST
12741 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
12743 if (GET_CODE (node->loc) == EXPR_LIST)
12745 /* This requires DW_OP_{,bit_}piece, which is not usable
12746 inside DWARF expressions. */
12747 if (want_address != 2)
12748 continue;
12749 descr = dw_sra_loc_expr (decl, node->loc);
12750 if (descr == NULL)
12751 continue;
12753 else
12755 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
12756 varloc = NOTE_VAR_LOCATION (node->loc);
12757 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
12759 if (descr)
12761 bool range_across_switch = false;
12762 /* If section switch happens in between node->label
12763 and node->next->label (or end of function) and
12764 we can't emit it as a single entry list,
12765 emit two ranges, first one ending at the end
12766 of first partition and second one starting at the
12767 beginning of second partition. */
12768 if (node == loc_list->last_before_switch
12769 && (node != loc_list->first || loc_list->first->next)
12770 && current_function_decl)
12772 endname = cfun->fde->dw_fde_end;
12773 range_across_switch = true;
12775 /* The variable has a location between NODE->LABEL and
12776 NODE->NEXT->LABEL. */
12777 else if (node->next)
12778 endname = node->next->label;
12779 /* If the variable has a location at the last label
12780 it keeps its location until the end of function. */
12781 else if (!current_function_decl)
12782 endname = text_end_label;
12783 else
12785 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
12786 current_function_funcdef_no);
12787 endname = ggc_strdup (label_id);
12790 *listp = new_loc_list (descr, node->label, endname, secname);
12791 if (TREE_CODE (decl) == PARM_DECL
12792 && node == loc_list->first
12793 && GET_CODE (node->loc) == NOTE
12794 && strcmp (node->label, endname) == 0)
12795 (*listp)->force = true;
12796 listp = &(*listp)->dw_loc_next;
12798 if (range_across_switch)
12800 if (GET_CODE (node->loc) == EXPR_LIST)
12801 descr = dw_sra_loc_expr (decl, node->loc);
12802 else
12804 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
12805 varloc = NOTE_VAR_LOCATION (node->loc);
12806 descr = dw_loc_list_1 (decl, varloc, want_address,
12807 initialized);
12809 gcc_assert (descr);
12810 /* The variable has a location between NODE->LABEL and
12811 NODE->NEXT->LABEL. */
12812 if (node->next)
12813 endname = node->next->label;
12814 else
12815 endname = cfun->fde->dw_fde_second_end;
12816 *listp = new_loc_list (descr,
12817 cfun->fde->dw_fde_second_begin,
12818 endname, secname);
12819 listp = &(*listp)->dw_loc_next;
12824 /* Try to avoid the overhead of a location list emitting a location
12825 expression instead, but only if we didn't have more than one
12826 location entry in the first place. If some entries were not
12827 representable, we don't want to pretend a single entry that was
12828 applies to the entire scope in which the variable is
12829 available. */
12830 if (list && loc_list->first->next)
12831 gen_llsym (list);
12833 return list;
12836 /* Return if the loc_list has only single element and thus can be represented
12837 as location description. */
12839 static bool
12840 single_element_loc_list_p (dw_loc_list_ref list)
12842 gcc_assert (!list->dw_loc_next || list->ll_symbol);
12843 return !list->ll_symbol;
12846 /* To each location in list LIST add loc descr REF. */
12848 static void
12849 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
12851 dw_loc_descr_ref copy;
12852 add_loc_descr (&list->expr, ref);
12853 list = list->dw_loc_next;
12854 while (list)
12856 copy = ggc_alloc_dw_loc_descr_node ();
12857 memcpy (copy, ref, sizeof (dw_loc_descr_node));
12858 add_loc_descr (&list->expr, copy);
12859 while (copy->dw_loc_next)
12861 dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
12862 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
12863 copy->dw_loc_next = new_copy;
12864 copy = new_copy;
12866 list = list->dw_loc_next;
12870 /* Given two lists RET and LIST
12871 produce location list that is result of adding expression in LIST
12872 to expression in RET on each position in program.
12873 Might be destructive on both RET and LIST.
12875 TODO: We handle only simple cases of RET or LIST having at most one
12876 element. General case would inolve sorting the lists in program order
12877 and merging them that will need some additional work.
12878 Adding that will improve quality of debug info especially for SRA-ed
12879 structures. */
12881 static void
12882 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
12884 if (!list)
12885 return;
12886 if (!*ret)
12888 *ret = list;
12889 return;
12891 if (!list->dw_loc_next)
12893 add_loc_descr_to_each (*ret, list->expr);
12894 return;
12896 if (!(*ret)->dw_loc_next)
12898 add_loc_descr_to_each (list, (*ret)->expr);
12899 *ret = list;
12900 return;
12902 expansion_failed (NULL_TREE, NULL_RTX,
12903 "Don't know how to merge two non-trivial"
12904 " location lists.\n");
12905 *ret = NULL;
12906 return;
12909 /* LOC is constant expression. Try a luck, look it up in constant
12910 pool and return its loc_descr of its address. */
12912 static dw_loc_descr_ref
12913 cst_pool_loc_descr (tree loc)
12915 /* Get an RTL for this, if something has been emitted. */
12916 rtx rtl = lookup_constant_def (loc);
12918 if (!rtl || !MEM_P (rtl))
12920 gcc_assert (!rtl);
12921 return 0;
12923 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
12925 /* TODO: We might get more coverage if we was actually delaying expansion
12926 of all expressions till end of compilation when constant pools are fully
12927 populated. */
12928 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
12930 expansion_failed (loc, NULL_RTX,
12931 "CST value in contant pool but not marked.");
12932 return 0;
12934 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
12935 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
12938 /* Return dw_loc_list representing address of addr_expr LOC
12939 by looking for inner INDIRECT_REF expression and turning
12940 it into simple arithmetics. */
12942 static dw_loc_list_ref
12943 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
12945 tree obj, offset;
12946 HOST_WIDE_INT bitsize, bitpos, bytepos;
12947 enum machine_mode mode;
12948 int unsignedp, volatilep = 0;
12949 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
12951 obj = get_inner_reference (TREE_OPERAND (loc, 0),
12952 &bitsize, &bitpos, &offset, &mode,
12953 &unsignedp, &volatilep, false);
12954 STRIP_NOPS (obj);
12955 if (bitpos % BITS_PER_UNIT)
12957 expansion_failed (loc, NULL_RTX, "bitfield access");
12958 return 0;
12960 if (!INDIRECT_REF_P (obj))
12962 expansion_failed (obj,
12963 NULL_RTX, "no indirect ref in inner refrence");
12964 return 0;
12966 if (!offset && !bitpos)
12967 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
12968 else if (toplev
12969 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
12970 && (dwarf_version >= 4 || !dwarf_strict))
12972 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
12973 if (!list_ret)
12974 return 0;
12975 if (offset)
12977 /* Variable offset. */
12978 list_ret1 = loc_list_from_tree (offset, 0);
12979 if (list_ret1 == 0)
12980 return 0;
12981 add_loc_list (&list_ret, list_ret1);
12982 if (!list_ret)
12983 return 0;
12984 add_loc_descr_to_each (list_ret,
12985 new_loc_descr (DW_OP_plus, 0, 0));
12987 bytepos = bitpos / BITS_PER_UNIT;
12988 if (bytepos > 0)
12989 add_loc_descr_to_each (list_ret,
12990 new_loc_descr (DW_OP_plus_uconst,
12991 bytepos, 0));
12992 else if (bytepos < 0)
12993 loc_list_plus_const (list_ret, bytepos);
12994 add_loc_descr_to_each (list_ret,
12995 new_loc_descr (DW_OP_stack_value, 0, 0));
12997 return list_ret;
13001 /* Generate Dwarf location list representing LOC.
13002 If WANT_ADDRESS is false, expression computing LOC will be computed
13003 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
13004 if WANT_ADDRESS is 2, expression computing address useable in location
13005 will be returned (i.e. DW_OP_reg can be used
13006 to refer to register values). */
13008 static dw_loc_list_ref
13009 loc_list_from_tree (tree loc, int want_address)
13011 dw_loc_descr_ref ret = NULL, ret1 = NULL;
13012 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13013 int have_address = 0;
13014 enum dwarf_location_atom op;
13016 /* ??? Most of the time we do not take proper care for sign/zero
13017 extending the values properly. Hopefully this won't be a real
13018 problem... */
13020 switch (TREE_CODE (loc))
13022 case ERROR_MARK:
13023 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
13024 return 0;
13026 case PLACEHOLDER_EXPR:
13027 /* This case involves extracting fields from an object to determine the
13028 position of other fields. We don't try to encode this here. The
13029 only user of this is Ada, which encodes the needed information using
13030 the names of types. */
13031 expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
13032 return 0;
13034 case CALL_EXPR:
13035 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
13036 /* There are no opcodes for these operations. */
13037 return 0;
13039 case PREINCREMENT_EXPR:
13040 case PREDECREMENT_EXPR:
13041 case POSTINCREMENT_EXPR:
13042 case POSTDECREMENT_EXPR:
13043 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
13044 /* There are no opcodes for these operations. */
13045 return 0;
13047 case ADDR_EXPR:
13048 /* If we already want an address, see if there is INDIRECT_REF inside
13049 e.g. for &this->field. */
13050 if (want_address)
13052 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
13053 (loc, want_address == 2);
13054 if (list_ret)
13055 have_address = 1;
13056 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
13057 && (ret = cst_pool_loc_descr (loc)))
13058 have_address = 1;
13060 /* Otherwise, process the argument and look for the address. */
13061 if (!list_ret && !ret)
13062 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
13063 else
13065 if (want_address)
13066 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
13067 return NULL;
13069 break;
13071 case VAR_DECL:
13072 if (DECL_THREAD_LOCAL_P (loc))
13074 rtx rtl;
13075 enum dwarf_location_atom first_op;
13076 enum dwarf_location_atom second_op;
13077 bool dtprel = false;
13079 if (targetm.have_tls)
13081 /* If this is not defined, we have no way to emit the
13082 data. */
13083 if (!targetm.asm_out.output_dwarf_dtprel)
13084 return 0;
13086 /* The way DW_OP_GNU_push_tls_address is specified, we
13087 can only look up addresses of objects in the current
13088 module. We used DW_OP_addr as first op, but that's
13089 wrong, because DW_OP_addr is relocated by the debug
13090 info consumer, while DW_OP_GNU_push_tls_address
13091 operand shouldn't be. */
13092 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
13093 return 0;
13094 first_op = DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u;
13095 dtprel = true;
13096 second_op = DW_OP_GNU_push_tls_address;
13098 else
13100 if (!targetm.emutls.debug_form_tls_address
13101 || !(dwarf_version >= 3 || !dwarf_strict))
13102 return 0;
13103 /* We stuffed the control variable into the DECL_VALUE_EXPR
13104 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
13105 no longer appear in gimple code. We used the control
13106 variable in specific so that we could pick it up here. */
13107 loc = DECL_VALUE_EXPR (loc);
13108 first_op = DW_OP_addr;
13109 second_op = DW_OP_form_tls_address;
13112 rtl = rtl_for_decl_location (loc);
13113 if (rtl == NULL_RTX)
13114 return 0;
13116 if (!MEM_P (rtl))
13117 return 0;
13118 rtl = XEXP (rtl, 0);
13119 if (! CONSTANT_P (rtl))
13120 return 0;
13122 ret = new_loc_descr (first_op, 0, 0);
13123 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
13124 ret->dw_loc_oprnd1.v.val_addr = rtl;
13125 ret->dtprel = dtprel;
13127 ret1 = new_loc_descr (second_op, 0, 0);
13128 add_loc_descr (&ret, ret1);
13130 have_address = 1;
13131 break;
13133 /* FALLTHRU */
13135 case PARM_DECL:
13136 case RESULT_DECL:
13137 if (DECL_HAS_VALUE_EXPR_P (loc))
13138 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
13139 want_address);
13140 /* FALLTHRU */
13142 case FUNCTION_DECL:
13144 rtx rtl;
13145 var_loc_list *loc_list = lookup_decl_loc (loc);
13147 if (loc_list && loc_list->first)
13149 list_ret = dw_loc_list (loc_list, loc, want_address);
13150 have_address = want_address != 0;
13151 break;
13153 rtl = rtl_for_decl_location (loc);
13154 if (rtl == NULL_RTX)
13156 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
13157 return 0;
13159 else if (CONST_INT_P (rtl))
13161 HOST_WIDE_INT val = INTVAL (rtl);
13162 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13163 val &= GET_MODE_MASK (DECL_MODE (loc));
13164 ret = int_loc_descriptor (val);
13166 else if (GET_CODE (rtl) == CONST_STRING)
13168 expansion_failed (loc, NULL_RTX, "CONST_STRING");
13169 return 0;
13171 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
13173 ret = new_loc_descr (DW_OP_addr, 0, 0);
13174 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
13175 ret->dw_loc_oprnd1.v.val_addr = rtl;
13177 else
13179 enum machine_mode mode, mem_mode;
13181 /* Certain constructs can only be represented at top-level. */
13182 if (want_address == 2)
13184 ret = loc_descriptor (rtl, VOIDmode,
13185 VAR_INIT_STATUS_INITIALIZED);
13186 have_address = 1;
13188 else
13190 mode = GET_MODE (rtl);
13191 mem_mode = VOIDmode;
13192 if (MEM_P (rtl))
13194 mem_mode = mode;
13195 mode = get_address_mode (rtl);
13196 rtl = XEXP (rtl, 0);
13197 have_address = 1;
13199 ret = mem_loc_descriptor (rtl, mode, mem_mode,
13200 VAR_INIT_STATUS_INITIALIZED);
13202 if (!ret)
13203 expansion_failed (loc, rtl,
13204 "failed to produce loc descriptor for rtl");
13207 break;
13209 case MEM_REF:
13210 /* ??? FIXME. */
13211 if (!integer_zerop (TREE_OPERAND (loc, 1)))
13212 return 0;
13213 /* Fallthru. */
13214 case INDIRECT_REF:
13215 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13216 have_address = 1;
13217 break;
13219 case COMPOUND_EXPR:
13220 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
13222 CASE_CONVERT:
13223 case VIEW_CONVERT_EXPR:
13224 case SAVE_EXPR:
13225 case MODIFY_EXPR:
13226 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
13228 case COMPONENT_REF:
13229 case BIT_FIELD_REF:
13230 case ARRAY_REF:
13231 case ARRAY_RANGE_REF:
13232 case REALPART_EXPR:
13233 case IMAGPART_EXPR:
13235 tree obj, offset;
13236 HOST_WIDE_INT bitsize, bitpos, bytepos;
13237 enum machine_mode mode;
13238 int unsignedp, volatilep = 0;
13240 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
13241 &unsignedp, &volatilep, false);
13243 gcc_assert (obj != loc);
13245 list_ret = loc_list_from_tree (obj,
13246 want_address == 2
13247 && !bitpos && !offset ? 2 : 1);
13248 /* TODO: We can extract value of the small expression via shifting even
13249 for nonzero bitpos. */
13250 if (list_ret == 0)
13251 return 0;
13252 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
13254 expansion_failed (loc, NULL_RTX,
13255 "bitfield access");
13256 return 0;
13259 if (offset != NULL_TREE)
13261 /* Variable offset. */
13262 list_ret1 = loc_list_from_tree (offset, 0);
13263 if (list_ret1 == 0)
13264 return 0;
13265 add_loc_list (&list_ret, list_ret1);
13266 if (!list_ret)
13267 return 0;
13268 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
13271 bytepos = bitpos / BITS_PER_UNIT;
13272 if (bytepos > 0)
13273 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
13274 else if (bytepos < 0)
13275 loc_list_plus_const (list_ret, bytepos);
13277 have_address = 1;
13278 break;
13281 case INTEGER_CST:
13282 if ((want_address || !host_integerp (loc, 0))
13283 && (ret = cst_pool_loc_descr (loc)))
13284 have_address = 1;
13285 else if (want_address == 2
13286 && host_integerp (loc, 0)
13287 && (ret = address_of_int_loc_descriptor
13288 (int_size_in_bytes (TREE_TYPE (loc)),
13289 tree_low_cst (loc, 0))))
13290 have_address = 1;
13291 else if (host_integerp (loc, 0))
13292 ret = int_loc_descriptor (tree_low_cst (loc, 0));
13293 else
13295 expansion_failed (loc, NULL_RTX,
13296 "Integer operand is not host integer");
13297 return 0;
13299 break;
13301 case CONSTRUCTOR:
13302 case REAL_CST:
13303 case STRING_CST:
13304 case COMPLEX_CST:
13305 if ((ret = cst_pool_loc_descr (loc)))
13306 have_address = 1;
13307 else
13308 /* We can construct small constants here using int_loc_descriptor. */
13309 expansion_failed (loc, NULL_RTX,
13310 "constructor or constant not in constant pool");
13311 break;
13313 case TRUTH_AND_EXPR:
13314 case TRUTH_ANDIF_EXPR:
13315 case BIT_AND_EXPR:
13316 op = DW_OP_and;
13317 goto do_binop;
13319 case TRUTH_XOR_EXPR:
13320 case BIT_XOR_EXPR:
13321 op = DW_OP_xor;
13322 goto do_binop;
13324 case TRUTH_OR_EXPR:
13325 case TRUTH_ORIF_EXPR:
13326 case BIT_IOR_EXPR:
13327 op = DW_OP_or;
13328 goto do_binop;
13330 case FLOOR_DIV_EXPR:
13331 case CEIL_DIV_EXPR:
13332 case ROUND_DIV_EXPR:
13333 case TRUNC_DIV_EXPR:
13334 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13335 return 0;
13336 op = DW_OP_div;
13337 goto do_binop;
13339 case MINUS_EXPR:
13340 op = DW_OP_minus;
13341 goto do_binop;
13343 case FLOOR_MOD_EXPR:
13344 case CEIL_MOD_EXPR:
13345 case ROUND_MOD_EXPR:
13346 case TRUNC_MOD_EXPR:
13347 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13349 op = DW_OP_mod;
13350 goto do_binop;
13352 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13353 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
13354 if (list_ret == 0 || list_ret1 == 0)
13355 return 0;
13357 add_loc_list (&list_ret, list_ret1);
13358 if (list_ret == 0)
13359 return 0;
13360 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
13361 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
13362 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
13363 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
13364 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
13365 break;
13367 case MULT_EXPR:
13368 op = DW_OP_mul;
13369 goto do_binop;
13371 case LSHIFT_EXPR:
13372 op = DW_OP_shl;
13373 goto do_binop;
13375 case RSHIFT_EXPR:
13376 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
13377 goto do_binop;
13379 case POINTER_PLUS_EXPR:
13380 case PLUS_EXPR:
13381 if (host_integerp (TREE_OPERAND (loc, 1), 0))
13383 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13384 if (list_ret == 0)
13385 return 0;
13387 loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
13388 break;
13391 op = DW_OP_plus;
13392 goto do_binop;
13394 case LE_EXPR:
13395 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13396 return 0;
13398 op = DW_OP_le;
13399 goto do_binop;
13401 case GE_EXPR:
13402 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13403 return 0;
13405 op = DW_OP_ge;
13406 goto do_binop;
13408 case LT_EXPR:
13409 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13410 return 0;
13412 op = DW_OP_lt;
13413 goto do_binop;
13415 case GT_EXPR:
13416 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13417 return 0;
13419 op = DW_OP_gt;
13420 goto do_binop;
13422 case EQ_EXPR:
13423 op = DW_OP_eq;
13424 goto do_binop;
13426 case NE_EXPR:
13427 op = DW_OP_ne;
13428 goto do_binop;
13430 do_binop:
13431 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13432 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
13433 if (list_ret == 0 || list_ret1 == 0)
13434 return 0;
13436 add_loc_list (&list_ret, list_ret1);
13437 if (list_ret == 0)
13438 return 0;
13439 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
13440 break;
13442 case TRUTH_NOT_EXPR:
13443 case BIT_NOT_EXPR:
13444 op = DW_OP_not;
13445 goto do_unop;
13447 case ABS_EXPR:
13448 op = DW_OP_abs;
13449 goto do_unop;
13451 case NEGATE_EXPR:
13452 op = DW_OP_neg;
13453 goto do_unop;
13455 do_unop:
13456 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13457 if (list_ret == 0)
13458 return 0;
13460 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
13461 break;
13463 case MIN_EXPR:
13464 case MAX_EXPR:
13466 const enum tree_code code =
13467 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
13469 loc = build3 (COND_EXPR, TREE_TYPE (loc),
13470 build2 (code, integer_type_node,
13471 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
13472 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
13475 /* ... fall through ... */
13477 case COND_EXPR:
13479 dw_loc_descr_ref lhs
13480 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
13481 dw_loc_list_ref rhs
13482 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
13483 dw_loc_descr_ref bra_node, jump_node, tmp;
13485 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13486 if (list_ret == 0 || lhs == 0 || rhs == 0)
13487 return 0;
13489 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13490 add_loc_descr_to_each (list_ret, bra_node);
13492 add_loc_list (&list_ret, rhs);
13493 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
13494 add_loc_descr_to_each (list_ret, jump_node);
13496 add_loc_descr_to_each (list_ret, lhs);
13497 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13498 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
13500 /* ??? Need a node to point the skip at. Use a nop. */
13501 tmp = new_loc_descr (DW_OP_nop, 0, 0);
13502 add_loc_descr_to_each (list_ret, tmp);
13503 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13504 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
13506 break;
13508 case FIX_TRUNC_EXPR:
13509 return 0;
13511 default:
13512 /* Leave front-end specific codes as simply unknown. This comes
13513 up, for instance, with the C STMT_EXPR. */
13514 if ((unsigned int) TREE_CODE (loc)
13515 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
13517 expansion_failed (loc, NULL_RTX,
13518 "language specific tree node");
13519 return 0;
13522 #ifdef ENABLE_CHECKING
13523 /* Otherwise this is a generic code; we should just lists all of
13524 these explicitly. We forgot one. */
13525 gcc_unreachable ();
13526 #else
13527 /* In a release build, we want to degrade gracefully: better to
13528 generate incomplete debugging information than to crash. */
13529 return NULL;
13530 #endif
13533 if (!ret && !list_ret)
13534 return 0;
13536 if (want_address == 2 && !have_address
13537 && (dwarf_version >= 4 || !dwarf_strict))
13539 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13541 expansion_failed (loc, NULL_RTX,
13542 "DWARF address size mismatch");
13543 return 0;
13545 if (ret)
13546 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
13547 else
13548 add_loc_descr_to_each (list_ret,
13549 new_loc_descr (DW_OP_stack_value, 0, 0));
13550 have_address = 1;
13552 /* Show if we can't fill the request for an address. */
13553 if (want_address && !have_address)
13555 expansion_failed (loc, NULL_RTX,
13556 "Want address and only have value");
13557 return 0;
13560 gcc_assert (!ret || !list_ret);
13562 /* If we've got an address and don't want one, dereference. */
13563 if (!want_address && have_address)
13565 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13567 if (size > DWARF2_ADDR_SIZE || size == -1)
13569 expansion_failed (loc, NULL_RTX,
13570 "DWARF address size mismatch");
13571 return 0;
13573 else if (size == DWARF2_ADDR_SIZE)
13574 op = DW_OP_deref;
13575 else
13576 op = DW_OP_deref_size;
13578 if (ret)
13579 add_loc_descr (&ret, new_loc_descr (op, size, 0));
13580 else
13581 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
13583 if (ret)
13584 list_ret = new_loc_list (ret, NULL, NULL, NULL);
13586 return list_ret;
13589 /* Same as above but return only single location expression. */
13590 static dw_loc_descr_ref
13591 loc_descriptor_from_tree (tree loc, int want_address)
13593 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
13594 if (!ret)
13595 return NULL;
13596 if (ret->dw_loc_next)
13598 expansion_failed (loc, NULL_RTX,
13599 "Location list where only loc descriptor needed");
13600 return NULL;
13602 return ret->expr;
13605 /* Given a value, round it up to the lowest multiple of `boundary'
13606 which is not less than the value itself. */
13608 static inline HOST_WIDE_INT
13609 ceiling (HOST_WIDE_INT value, unsigned int boundary)
13611 return (((value + boundary - 1) / boundary) * boundary);
13614 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
13615 pointer to the declared type for the relevant field variable, or return
13616 `integer_type_node' if the given node turns out to be an
13617 ERROR_MARK node. */
13619 static inline tree
13620 field_type (const_tree decl)
13622 tree type;
13624 if (TREE_CODE (decl) == ERROR_MARK)
13625 return integer_type_node;
13627 type = DECL_BIT_FIELD_TYPE (decl);
13628 if (type == NULL_TREE)
13629 type = TREE_TYPE (decl);
13631 return type;
13634 /* Given a pointer to a tree node, return the alignment in bits for
13635 it, or else return BITS_PER_WORD if the node actually turns out to
13636 be an ERROR_MARK node. */
13638 static inline unsigned
13639 simple_type_align_in_bits (const_tree type)
13641 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
13644 static inline unsigned
13645 simple_decl_align_in_bits (const_tree decl)
13647 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
13650 /* Return the result of rounding T up to ALIGN. */
13652 static inline double_int
13653 round_up_to_align (double_int t, unsigned int align)
13655 double_int alignd = double_int::from_uhwi (align);
13656 t += alignd;
13657 t += double_int_minus_one;
13658 t = t.div (alignd, true, TRUNC_DIV_EXPR);
13659 t *= alignd;
13660 return t;
13663 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
13664 lowest addressed byte of the "containing object" for the given FIELD_DECL,
13665 or return 0 if we are unable to determine what that offset is, either
13666 because the argument turns out to be a pointer to an ERROR_MARK node, or
13667 because the offset is actually variable. (We can't handle the latter case
13668 just yet). */
13670 static HOST_WIDE_INT
13671 field_byte_offset (const_tree decl)
13673 double_int object_offset_in_bits;
13674 double_int object_offset_in_bytes;
13675 double_int bitpos_int;
13677 if (TREE_CODE (decl) == ERROR_MARK)
13678 return 0;
13680 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
13682 /* We cannot yet cope with fields whose positions are variable, so
13683 for now, when we see such things, we simply return 0. Someday, we may
13684 be able to handle such cases, but it will be damn difficult. */
13685 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
13686 return 0;
13688 bitpos_int = tree_to_double_int (bit_position (decl));
13690 #ifdef PCC_BITFIELD_TYPE_MATTERS
13691 if (PCC_BITFIELD_TYPE_MATTERS)
13693 tree type;
13694 tree field_size_tree;
13695 double_int deepest_bitpos;
13696 double_int field_size_in_bits;
13697 unsigned int type_align_in_bits;
13698 unsigned int decl_align_in_bits;
13699 double_int type_size_in_bits;
13701 type = field_type (decl);
13702 type_size_in_bits = double_int_type_size_in_bits (type);
13703 type_align_in_bits = simple_type_align_in_bits (type);
13705 field_size_tree = DECL_SIZE (decl);
13707 /* The size could be unspecified if there was an error, or for
13708 a flexible array member. */
13709 if (!field_size_tree)
13710 field_size_tree = bitsize_zero_node;
13712 /* If the size of the field is not constant, use the type size. */
13713 if (TREE_CODE (field_size_tree) == INTEGER_CST)
13714 field_size_in_bits = tree_to_double_int (field_size_tree);
13715 else
13716 field_size_in_bits = type_size_in_bits;
13718 decl_align_in_bits = simple_decl_align_in_bits (decl);
13720 /* The GCC front-end doesn't make any attempt to keep track of the
13721 starting bit offset (relative to the start of the containing
13722 structure type) of the hypothetical "containing object" for a
13723 bit-field. Thus, when computing the byte offset value for the
13724 start of the "containing object" of a bit-field, we must deduce
13725 this information on our own. This can be rather tricky to do in
13726 some cases. For example, handling the following structure type
13727 definition when compiling for an i386/i486 target (which only
13728 aligns long long's to 32-bit boundaries) can be very tricky:
13730 struct S { int field1; long long field2:31; };
13732 Fortunately, there is a simple rule-of-thumb which can be used
13733 in such cases. When compiling for an i386/i486, GCC will
13734 allocate 8 bytes for the structure shown above. It decides to
13735 do this based upon one simple rule for bit-field allocation.
13736 GCC allocates each "containing object" for each bit-field at
13737 the first (i.e. lowest addressed) legitimate alignment boundary
13738 (based upon the required minimum alignment for the declared
13739 type of the field) which it can possibly use, subject to the
13740 condition that there is still enough available space remaining
13741 in the containing object (when allocated at the selected point)
13742 to fully accommodate all of the bits of the bit-field itself.
13744 This simple rule makes it obvious why GCC allocates 8 bytes for
13745 each object of the structure type shown above. When looking
13746 for a place to allocate the "containing object" for `field2',
13747 the compiler simply tries to allocate a 64-bit "containing
13748 object" at each successive 32-bit boundary (starting at zero)
13749 until it finds a place to allocate that 64- bit field such that
13750 at least 31 contiguous (and previously unallocated) bits remain
13751 within that selected 64 bit field. (As it turns out, for the
13752 example above, the compiler finds it is OK to allocate the
13753 "containing object" 64-bit field at bit-offset zero within the
13754 structure type.)
13756 Here we attempt to work backwards from the limited set of facts
13757 we're given, and we try to deduce from those facts, where GCC
13758 must have believed that the containing object started (within
13759 the structure type). The value we deduce is then used (by the
13760 callers of this routine) to generate DW_AT_location and
13761 DW_AT_bit_offset attributes for fields (both bit-fields and, in
13762 the case of DW_AT_location, regular fields as well). */
13764 /* Figure out the bit-distance from the start of the structure to
13765 the "deepest" bit of the bit-field. */
13766 deepest_bitpos = bitpos_int + field_size_in_bits;
13768 /* This is the tricky part. Use some fancy footwork to deduce
13769 where the lowest addressed bit of the containing object must
13770 be. */
13771 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
13773 /* Round up to type_align by default. This works best for
13774 bitfields. */
13775 object_offset_in_bits
13776 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
13778 if (object_offset_in_bits.ugt (bitpos_int))
13780 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
13782 /* Round up to decl_align instead. */
13783 object_offset_in_bits
13784 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
13787 else
13788 #endif /* PCC_BITFIELD_TYPE_MATTERS */
13789 object_offset_in_bits = bitpos_int;
13791 object_offset_in_bytes
13792 = object_offset_in_bits.div (double_int::from_uhwi (BITS_PER_UNIT),
13793 true, TRUNC_DIV_EXPR);
13794 return object_offset_in_bytes.to_shwi ();
13797 /* The following routines define various Dwarf attributes and any data
13798 associated with them. */
13800 /* Add a location description attribute value to a DIE.
13802 This emits location attributes suitable for whole variables and
13803 whole parameters. Note that the location attributes for struct fields are
13804 generated by the routine `data_member_location_attribute' below. */
13806 static inline void
13807 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
13808 dw_loc_list_ref descr)
13810 if (descr == 0)
13811 return;
13812 if (single_element_loc_list_p (descr))
13813 add_AT_loc (die, attr_kind, descr->expr);
13814 else
13815 add_AT_loc_list (die, attr_kind, descr);
13818 /* Add DW_AT_accessibility attribute to DIE if needed. */
13820 static void
13821 add_accessibility_attribute (dw_die_ref die, tree decl)
13823 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
13824 children, otherwise the default is DW_ACCESS_public. In DWARF2
13825 the default has always been DW_ACCESS_public. */
13826 if (TREE_PROTECTED (decl))
13827 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
13828 else if (TREE_PRIVATE (decl))
13830 if (dwarf_version == 2
13831 || die->die_parent == NULL
13832 || die->die_parent->die_tag != DW_TAG_class_type)
13833 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
13835 else if (dwarf_version > 2
13836 && die->die_parent
13837 && die->die_parent->die_tag == DW_TAG_class_type)
13838 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
13841 /* Attach the specialized form of location attribute used for data members of
13842 struct and union types. In the special case of a FIELD_DECL node which
13843 represents a bit-field, the "offset" part of this special location
13844 descriptor must indicate the distance in bytes from the lowest-addressed
13845 byte of the containing struct or union type to the lowest-addressed byte of
13846 the "containing object" for the bit-field. (See the `field_byte_offset'
13847 function above).
13849 For any given bit-field, the "containing object" is a hypothetical object
13850 (of some integral or enum type) within which the given bit-field lives. The
13851 type of this hypothetical "containing object" is always the same as the
13852 declared type of the individual bit-field itself (for GCC anyway... the
13853 DWARF spec doesn't actually mandate this). Note that it is the size (in
13854 bytes) of the hypothetical "containing object" which will be given in the
13855 DW_AT_byte_size attribute for this bit-field. (See the
13856 `byte_size_attribute' function below.) It is also used when calculating the
13857 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
13858 function below.) */
13860 static void
13861 add_data_member_location_attribute (dw_die_ref die, tree decl)
13863 HOST_WIDE_INT offset;
13864 dw_loc_descr_ref loc_descr = 0;
13866 if (TREE_CODE (decl) == TREE_BINFO)
13868 /* We're working on the TAG_inheritance for a base class. */
13869 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
13871 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
13872 aren't at a fixed offset from all (sub)objects of the same
13873 type. We need to extract the appropriate offset from our
13874 vtable. The following dwarf expression means
13876 BaseAddr = ObAddr + *((*ObAddr) - Offset)
13878 This is specific to the V3 ABI, of course. */
13880 dw_loc_descr_ref tmp;
13882 /* Make a copy of the object address. */
13883 tmp = new_loc_descr (DW_OP_dup, 0, 0);
13884 add_loc_descr (&loc_descr, tmp);
13886 /* Extract the vtable address. */
13887 tmp = new_loc_descr (DW_OP_deref, 0, 0);
13888 add_loc_descr (&loc_descr, tmp);
13890 /* Calculate the address of the offset. */
13891 offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
13892 gcc_assert (offset < 0);
13894 tmp = int_loc_descriptor (-offset);
13895 add_loc_descr (&loc_descr, tmp);
13896 tmp = new_loc_descr (DW_OP_minus, 0, 0);
13897 add_loc_descr (&loc_descr, tmp);
13899 /* Extract the offset. */
13900 tmp = new_loc_descr (DW_OP_deref, 0, 0);
13901 add_loc_descr (&loc_descr, tmp);
13903 /* Add it to the object address. */
13904 tmp = new_loc_descr (DW_OP_plus, 0, 0);
13905 add_loc_descr (&loc_descr, tmp);
13907 else
13908 offset = tree_low_cst (BINFO_OFFSET (decl), 0);
13910 else
13911 offset = field_byte_offset (decl);
13913 if (! loc_descr)
13915 if (dwarf_version > 2)
13917 /* Don't need to output a location expression, just the constant. */
13918 if (offset < 0)
13919 add_AT_int (die, DW_AT_data_member_location, offset);
13920 else
13921 add_AT_unsigned (die, DW_AT_data_member_location, offset);
13922 return;
13924 else
13926 enum dwarf_location_atom op;
13928 /* The DWARF2 standard says that we should assume that the structure
13929 address is already on the stack, so we can specify a structure
13930 field address by using DW_OP_plus_uconst. */
13931 op = DW_OP_plus_uconst;
13932 loc_descr = new_loc_descr (op, offset, 0);
13936 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
13939 /* Writes integer values to dw_vec_const array. */
13941 static void
13942 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
13944 while (size != 0)
13946 *dest++ = val & 0xff;
13947 val >>= 8;
13948 --size;
13952 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
13954 static HOST_WIDE_INT
13955 extract_int (const unsigned char *src, unsigned int size)
13957 HOST_WIDE_INT val = 0;
13959 src += size;
13960 while (size != 0)
13962 val <<= 8;
13963 val |= *--src & 0xff;
13964 --size;
13966 return val;
13969 /* Writes double_int values to dw_vec_const array. */
13971 static void
13972 insert_double (double_int val, unsigned char *dest)
13974 unsigned char *p0 = dest;
13975 unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
13977 if (WORDS_BIG_ENDIAN)
13979 p0 = p1;
13980 p1 = dest;
13983 insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
13984 insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
13987 /* Writes floating point values to dw_vec_const array. */
13989 static void
13990 insert_float (const_rtx rtl, unsigned char *array)
13992 REAL_VALUE_TYPE rv;
13993 long val[4];
13994 int i;
13996 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
13997 real_to_target (val, &rv, GET_MODE (rtl));
13999 /* real_to_target puts 32-bit pieces in each long. Pack them. */
14000 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
14002 insert_int (val[i], 4, array);
14003 array += 4;
14007 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
14008 does not have a "location" either in memory or in a register. These
14009 things can arise in GNU C when a constant is passed as an actual parameter
14010 to an inlined function. They can also arise in C++ where declared
14011 constants do not necessarily get memory "homes". */
14013 static bool
14014 add_const_value_attribute (dw_die_ref die, rtx rtl)
14016 switch (GET_CODE (rtl))
14018 case CONST_INT:
14020 HOST_WIDE_INT val = INTVAL (rtl);
14022 if (val < 0)
14023 add_AT_int (die, DW_AT_const_value, val);
14024 else
14025 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
14027 return true;
14029 case CONST_DOUBLE:
14030 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
14031 floating-point constant. A CONST_DOUBLE is used whenever the
14032 constant requires more than one word in order to be adequately
14033 represented. */
14035 enum machine_mode mode = GET_MODE (rtl);
14037 if (SCALAR_FLOAT_MODE_P (mode))
14039 unsigned int length = GET_MODE_SIZE (mode);
14040 unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
14042 insert_float (rtl, array);
14043 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
14045 else
14046 add_AT_double (die, DW_AT_const_value,
14047 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
14049 return true;
14051 case CONST_VECTOR:
14053 enum machine_mode mode = GET_MODE (rtl);
14054 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
14055 unsigned int length = CONST_VECTOR_NUNITS (rtl);
14056 unsigned char *array = (unsigned char *) ggc_alloc_atomic
14057 (length * elt_size);
14058 unsigned int i;
14059 unsigned char *p;
14061 switch (GET_MODE_CLASS (mode))
14063 case MODE_VECTOR_INT:
14064 for (i = 0, p = array; i < length; i++, p += elt_size)
14066 rtx elt = CONST_VECTOR_ELT (rtl, i);
14067 double_int val = rtx_to_double_int (elt);
14069 if (elt_size <= sizeof (HOST_WIDE_INT))
14070 insert_int (val.to_shwi (), elt_size, p);
14071 else
14073 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
14074 insert_double (val, p);
14077 break;
14079 case MODE_VECTOR_FLOAT:
14080 for (i = 0, p = array; i < length; i++, p += elt_size)
14082 rtx elt = CONST_VECTOR_ELT (rtl, i);
14083 insert_float (elt, p);
14085 break;
14087 default:
14088 gcc_unreachable ();
14091 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
14093 return true;
14095 case CONST_STRING:
14096 if (dwarf_version >= 4 || !dwarf_strict)
14098 dw_loc_descr_ref loc_result;
14099 resolve_one_addr (&rtl, NULL);
14100 rtl_addr:
14101 loc_result = new_loc_descr (DW_OP_addr, 0, 0);
14102 loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
14103 loc_result->dw_loc_oprnd1.v.val_addr = rtl;
14104 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14105 add_AT_loc (die, DW_AT_location, loc_result);
14106 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
14107 return true;
14109 return false;
14111 case CONST:
14112 if (CONSTANT_P (XEXP (rtl, 0)))
14113 return add_const_value_attribute (die, XEXP (rtl, 0));
14114 /* FALLTHROUGH */
14115 case SYMBOL_REF:
14116 if (!const_ok_for_output (rtl))
14117 return false;
14118 case LABEL_REF:
14119 if (dwarf_version >= 4 || !dwarf_strict)
14120 goto rtl_addr;
14121 return false;
14123 case PLUS:
14124 /* In cases where an inlined instance of an inline function is passed
14125 the address of an `auto' variable (which is local to the caller) we
14126 can get a situation where the DECL_RTL of the artificial local
14127 variable (for the inlining) which acts as a stand-in for the
14128 corresponding formal parameter (of the inline function) will look
14129 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
14130 exactly a compile-time constant expression, but it isn't the address
14131 of the (artificial) local variable either. Rather, it represents the
14132 *value* which the artificial local variable always has during its
14133 lifetime. We currently have no way to represent such quasi-constant
14134 values in Dwarf, so for now we just punt and generate nothing. */
14135 return false;
14137 case HIGH:
14138 case CONST_FIXED:
14139 return false;
14141 case MEM:
14142 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
14143 && MEM_READONLY_P (rtl)
14144 && GET_MODE (rtl) == BLKmode)
14146 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
14147 return true;
14149 return false;
14151 default:
14152 /* No other kinds of rtx should be possible here. */
14153 gcc_unreachable ();
14155 return false;
14158 /* Determine whether the evaluation of EXPR references any variables
14159 or functions which aren't otherwise used (and therefore may not be
14160 output). */
14161 static tree
14162 reference_to_unused (tree * tp, int * walk_subtrees,
14163 void * data ATTRIBUTE_UNUSED)
14165 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
14166 *walk_subtrees = 0;
14168 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
14169 && ! TREE_ASM_WRITTEN (*tp))
14170 return *tp;
14171 /* ??? The C++ FE emits debug information for using decls, so
14172 putting gcc_unreachable here falls over. See PR31899. For now
14173 be conservative. */
14174 else if (!cgraph_global_info_ready
14175 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
14176 return *tp;
14177 else if (TREE_CODE (*tp) == VAR_DECL)
14179 struct varpool_node *node = varpool_get_node (*tp);
14180 if (!node || !node->analyzed)
14181 return *tp;
14183 else if (TREE_CODE (*tp) == FUNCTION_DECL
14184 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
14186 /* The call graph machinery must have finished analyzing,
14187 optimizing and gimplifying the CU by now.
14188 So if *TP has no call graph node associated
14189 to it, it means *TP will not be emitted. */
14190 if (!cgraph_get_node (*tp))
14191 return *tp;
14193 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
14194 return *tp;
14196 return NULL_TREE;
14199 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
14200 for use in a later add_const_value_attribute call. */
14202 static rtx
14203 rtl_for_decl_init (tree init, tree type)
14205 rtx rtl = NULL_RTX;
14207 STRIP_NOPS (init);
14209 /* If a variable is initialized with a string constant without embedded
14210 zeros, build CONST_STRING. */
14211 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
14213 tree enttype = TREE_TYPE (type);
14214 tree domain = TYPE_DOMAIN (type);
14215 enum machine_mode mode = TYPE_MODE (enttype);
14217 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
14218 && domain
14219 && integer_zerop (TYPE_MIN_VALUE (domain))
14220 && compare_tree_int (TYPE_MAX_VALUE (domain),
14221 TREE_STRING_LENGTH (init) - 1) == 0
14222 && ((size_t) TREE_STRING_LENGTH (init)
14223 == strlen (TREE_STRING_POINTER (init)) + 1))
14225 rtl = gen_rtx_CONST_STRING (VOIDmode,
14226 ggc_strdup (TREE_STRING_POINTER (init)));
14227 rtl = gen_rtx_MEM (BLKmode, rtl);
14228 MEM_READONLY_P (rtl) = 1;
14231 /* Other aggregates, and complex values, could be represented using
14232 CONCAT: FIXME! */
14233 else if (AGGREGATE_TYPE_P (type)
14234 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
14235 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
14236 || TREE_CODE (type) == COMPLEX_TYPE)
14238 /* Vectors only work if their mode is supported by the target.
14239 FIXME: generic vectors ought to work too. */
14240 else if (TREE_CODE (type) == VECTOR_TYPE
14241 && !VECTOR_MODE_P (TYPE_MODE (type)))
14243 /* If the initializer is something that we know will expand into an
14244 immediate RTL constant, expand it now. We must be careful not to
14245 reference variables which won't be output. */
14246 else if (initializer_constant_valid_p (init, type)
14247 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
14249 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
14250 possible. */
14251 if (TREE_CODE (type) == VECTOR_TYPE)
14252 switch (TREE_CODE (init))
14254 case VECTOR_CST:
14255 break;
14256 case CONSTRUCTOR:
14257 if (TREE_CONSTANT (init))
14259 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
14260 bool constant_p = true;
14261 tree value;
14262 unsigned HOST_WIDE_INT ix;
14264 /* Even when ctor is constant, it might contain non-*_CST
14265 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
14266 belong into VECTOR_CST nodes. */
14267 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
14268 if (!CONSTANT_CLASS_P (value))
14270 constant_p = false;
14271 break;
14274 if (constant_p)
14276 init = build_vector_from_ctor (type, elts);
14277 break;
14280 /* FALLTHRU */
14282 default:
14283 return NULL;
14286 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
14288 /* If expand_expr returns a MEM, it wasn't immediate. */
14289 gcc_assert (!rtl || !MEM_P (rtl));
14292 return rtl;
14295 /* Generate RTL for the variable DECL to represent its location. */
14297 static rtx
14298 rtl_for_decl_location (tree decl)
14300 rtx rtl;
14302 /* Here we have to decide where we are going to say the parameter "lives"
14303 (as far as the debugger is concerned). We only have a couple of
14304 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
14306 DECL_RTL normally indicates where the parameter lives during most of the
14307 activation of the function. If optimization is enabled however, this
14308 could be either NULL or else a pseudo-reg. Both of those cases indicate
14309 that the parameter doesn't really live anywhere (as far as the code
14310 generation parts of GCC are concerned) during most of the function's
14311 activation. That will happen (for example) if the parameter is never
14312 referenced within the function.
14314 We could just generate a location descriptor here for all non-NULL
14315 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
14316 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
14317 where DECL_RTL is NULL or is a pseudo-reg.
14319 Note however that we can only get away with using DECL_INCOMING_RTL as
14320 a backup substitute for DECL_RTL in certain limited cases. In cases
14321 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
14322 we can be sure that the parameter was passed using the same type as it is
14323 declared to have within the function, and that its DECL_INCOMING_RTL
14324 points us to a place where a value of that type is passed.
14326 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
14327 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
14328 because in these cases DECL_INCOMING_RTL points us to a value of some
14329 type which is *different* from the type of the parameter itself. Thus,
14330 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
14331 such cases, the debugger would end up (for example) trying to fetch a
14332 `float' from a place which actually contains the first part of a
14333 `double'. That would lead to really incorrect and confusing
14334 output at debug-time.
14336 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
14337 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
14338 are a couple of exceptions however. On little-endian machines we can
14339 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
14340 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
14341 an integral type that is smaller than TREE_TYPE (decl). These cases arise
14342 when (on a little-endian machine) a non-prototyped function has a
14343 parameter declared to be of type `short' or `char'. In such cases,
14344 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
14345 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
14346 passed `int' value. If the debugger then uses that address to fetch
14347 a `short' or a `char' (on a little-endian machine) the result will be
14348 the correct data, so we allow for such exceptional cases below.
14350 Note that our goal here is to describe the place where the given formal
14351 parameter lives during most of the function's activation (i.e. between the
14352 end of the prologue and the start of the epilogue). We'll do that as best
14353 as we can. Note however that if the given formal parameter is modified
14354 sometime during the execution of the function, then a stack backtrace (at
14355 debug-time) will show the function as having been called with the *new*
14356 value rather than the value which was originally passed in. This happens
14357 rarely enough that it is not a major problem, but it *is* a problem, and
14358 I'd like to fix it.
14360 A future version of dwarf2out.c may generate two additional attributes for
14361 any given DW_TAG_formal_parameter DIE which will describe the "passed
14362 type" and the "passed location" for the given formal parameter in addition
14363 to the attributes we now generate to indicate the "declared type" and the
14364 "active location" for each parameter. This additional set of attributes
14365 could be used by debuggers for stack backtraces. Separately, note that
14366 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
14367 This happens (for example) for inlined-instances of inline function formal
14368 parameters which are never referenced. This really shouldn't be
14369 happening. All PARM_DECL nodes should get valid non-NULL
14370 DECL_INCOMING_RTL values. FIXME. */
14372 /* Use DECL_RTL as the "location" unless we find something better. */
14373 rtl = DECL_RTL_IF_SET (decl);
14375 /* When generating abstract instances, ignore everything except
14376 constants, symbols living in memory, and symbols living in
14377 fixed registers. */
14378 if (! reload_completed)
14380 if (rtl
14381 && (CONSTANT_P (rtl)
14382 || (MEM_P (rtl)
14383 && CONSTANT_P (XEXP (rtl, 0)))
14384 || (REG_P (rtl)
14385 && TREE_CODE (decl) == VAR_DECL
14386 && TREE_STATIC (decl))))
14388 rtl = targetm.delegitimize_address (rtl);
14389 return rtl;
14391 rtl = NULL_RTX;
14393 else if (TREE_CODE (decl) == PARM_DECL)
14395 if (rtl == NULL_RTX
14396 || is_pseudo_reg (rtl)
14397 || (MEM_P (rtl)
14398 && is_pseudo_reg (XEXP (rtl, 0))
14399 && DECL_INCOMING_RTL (decl)
14400 && MEM_P (DECL_INCOMING_RTL (decl))
14401 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
14403 tree declared_type = TREE_TYPE (decl);
14404 tree passed_type = DECL_ARG_TYPE (decl);
14405 enum machine_mode dmode = TYPE_MODE (declared_type);
14406 enum machine_mode pmode = TYPE_MODE (passed_type);
14408 /* This decl represents a formal parameter which was optimized out.
14409 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
14410 all cases where (rtl == NULL_RTX) just below. */
14411 if (dmode == pmode)
14412 rtl = DECL_INCOMING_RTL (decl);
14413 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
14414 && SCALAR_INT_MODE_P (dmode)
14415 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
14416 && DECL_INCOMING_RTL (decl))
14418 rtx inc = DECL_INCOMING_RTL (decl);
14419 if (REG_P (inc))
14420 rtl = inc;
14421 else if (MEM_P (inc))
14423 if (BYTES_BIG_ENDIAN)
14424 rtl = adjust_address_nv (inc, dmode,
14425 GET_MODE_SIZE (pmode)
14426 - GET_MODE_SIZE (dmode));
14427 else
14428 rtl = inc;
14433 /* If the parm was passed in registers, but lives on the stack, then
14434 make a big endian correction if the mode of the type of the
14435 parameter is not the same as the mode of the rtl. */
14436 /* ??? This is the same series of checks that are made in dbxout.c before
14437 we reach the big endian correction code there. It isn't clear if all
14438 of these checks are necessary here, but keeping them all is the safe
14439 thing to do. */
14440 else if (MEM_P (rtl)
14441 && XEXP (rtl, 0) != const0_rtx
14442 && ! CONSTANT_P (XEXP (rtl, 0))
14443 /* Not passed in memory. */
14444 && !MEM_P (DECL_INCOMING_RTL (decl))
14445 /* Not passed by invisible reference. */
14446 && (!REG_P (XEXP (rtl, 0))
14447 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
14448 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
14449 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
14450 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
14451 #endif
14453 /* Big endian correction check. */
14454 && BYTES_BIG_ENDIAN
14455 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
14456 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
14457 < UNITS_PER_WORD))
14459 enum machine_mode addr_mode = get_address_mode (rtl);
14460 int offset = (UNITS_PER_WORD
14461 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
14463 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
14464 plus_constant (addr_mode, XEXP (rtl, 0), offset));
14467 else if (TREE_CODE (decl) == VAR_DECL
14468 && rtl
14469 && MEM_P (rtl)
14470 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
14471 && BYTES_BIG_ENDIAN)
14473 enum machine_mode addr_mode = get_address_mode (rtl);
14474 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
14475 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
14477 /* If a variable is declared "register" yet is smaller than
14478 a register, then if we store the variable to memory, it
14479 looks like we're storing a register-sized value, when in
14480 fact we are not. We need to adjust the offset of the
14481 storage location to reflect the actual value's bytes,
14482 else gdb will not be able to display it. */
14483 if (rsize > dsize)
14484 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
14485 plus_constant (addr_mode, XEXP (rtl, 0),
14486 rsize - dsize));
14489 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
14490 and will have been substituted directly into all expressions that use it.
14491 C does not have such a concept, but C++ and other languages do. */
14492 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
14493 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
14495 if (rtl)
14496 rtl = targetm.delegitimize_address (rtl);
14498 /* If we don't look past the constant pool, we risk emitting a
14499 reference to a constant pool entry that isn't referenced from
14500 code, and thus is not emitted. */
14501 if (rtl)
14502 rtl = avoid_constant_pool_reference (rtl);
14504 /* Try harder to get a rtl. If this symbol ends up not being emitted
14505 in the current CU, resolve_addr will remove the expression referencing
14506 it. */
14507 if (rtl == NULL_RTX
14508 && TREE_CODE (decl) == VAR_DECL
14509 && !DECL_EXTERNAL (decl)
14510 && TREE_STATIC (decl)
14511 && DECL_NAME (decl)
14512 && !DECL_HARD_REGISTER (decl)
14513 && DECL_MODE (decl) != VOIDmode)
14515 rtl = make_decl_rtl_for_debug (decl);
14516 if (!MEM_P (rtl)
14517 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
14518 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
14519 rtl = NULL_RTX;
14522 return rtl;
14525 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
14526 returned. If so, the decl for the COMMON block is returned, and the
14527 value is the offset into the common block for the symbol. */
14529 static tree
14530 fortran_common (tree decl, HOST_WIDE_INT *value)
14532 tree val_expr, cvar;
14533 enum machine_mode mode;
14534 HOST_WIDE_INT bitsize, bitpos;
14535 tree offset;
14536 int unsignedp, volatilep = 0;
14538 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
14539 it does not have a value (the offset into the common area), or if it
14540 is thread local (as opposed to global) then it isn't common, and shouldn't
14541 be handled as such. */
14542 if (TREE_CODE (decl) != VAR_DECL
14543 || !TREE_STATIC (decl)
14544 || !DECL_HAS_VALUE_EXPR_P (decl)
14545 || !is_fortran ())
14546 return NULL_TREE;
14548 val_expr = DECL_VALUE_EXPR (decl);
14549 if (TREE_CODE (val_expr) != COMPONENT_REF)
14550 return NULL_TREE;
14552 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
14553 &mode, &unsignedp, &volatilep, true);
14555 if (cvar == NULL_TREE
14556 || TREE_CODE (cvar) != VAR_DECL
14557 || DECL_ARTIFICIAL (cvar)
14558 || !TREE_PUBLIC (cvar))
14559 return NULL_TREE;
14561 *value = 0;
14562 if (offset != NULL)
14564 if (!host_integerp (offset, 0))
14565 return NULL_TREE;
14566 *value = tree_low_cst (offset, 0);
14568 if (bitpos != 0)
14569 *value += bitpos / BITS_PER_UNIT;
14571 return cvar;
14574 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
14575 data attribute for a variable or a parameter. We generate the
14576 DW_AT_const_value attribute only in those cases where the given variable
14577 or parameter does not have a true "location" either in memory or in a
14578 register. This can happen (for example) when a constant is passed as an
14579 actual argument in a call to an inline function. (It's possible that
14580 these things can crop up in other ways also.) Note that one type of
14581 constant value which can be passed into an inlined function is a constant
14582 pointer. This can happen for example if an actual argument in an inlined
14583 function call evaluates to a compile-time constant address.
14585 CACHE_P is true if it is worth caching the location list for DECL,
14586 so that future calls can reuse it rather than regenerate it from scratch.
14587 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
14588 since we will need to refer to them each time the function is inlined. */
14590 static bool
14591 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
14592 enum dwarf_attribute attr)
14594 rtx rtl;
14595 dw_loc_list_ref list;
14596 var_loc_list *loc_list;
14597 cached_dw_loc_list *cache;
14598 void **slot;
14600 if (TREE_CODE (decl) == ERROR_MARK)
14601 return false;
14603 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
14604 || TREE_CODE (decl) == RESULT_DECL);
14606 /* Try to get some constant RTL for this decl, and use that as the value of
14607 the location. */
14609 rtl = rtl_for_decl_location (decl);
14610 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
14611 && add_const_value_attribute (die, rtl))
14612 return true;
14614 /* See if we have single element location list that is equivalent to
14615 a constant value. That way we are better to use add_const_value_attribute
14616 rather than expanding constant value equivalent. */
14617 loc_list = lookup_decl_loc (decl);
14618 if (loc_list
14619 && loc_list->first
14620 && loc_list->first->next == NULL
14621 && NOTE_P (loc_list->first->loc)
14622 && NOTE_VAR_LOCATION (loc_list->first->loc)
14623 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
14625 struct var_loc_node *node;
14627 node = loc_list->first;
14628 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
14629 if (GET_CODE (rtl) == EXPR_LIST)
14630 rtl = XEXP (rtl, 0);
14631 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
14632 && add_const_value_attribute (die, rtl))
14633 return true;
14635 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
14636 list several times. See if we've already cached the contents. */
14637 list = NULL;
14638 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
14639 cache_p = false;
14640 if (cache_p)
14642 cache = (cached_dw_loc_list *)
14643 htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
14644 if (cache)
14645 list = cache->loc_list;
14647 if (list == NULL)
14649 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
14650 /* It is usually worth caching this result if the decl is from
14651 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
14652 if (cache_p && list && list->dw_loc_next)
14654 slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
14655 DECL_UID (decl), INSERT);
14656 cache = ggc_alloc_cleared_cached_dw_loc_list ();
14657 cache->decl_id = DECL_UID (decl);
14658 cache->loc_list = list;
14659 *slot = cache;
14662 if (list)
14664 add_AT_location_description (die, attr, list);
14665 return true;
14667 /* None of that worked, so it must not really have a location;
14668 try adding a constant value attribute from the DECL_INITIAL. */
14669 return tree_add_const_value_attribute_for_decl (die, decl);
14672 /* Add VARIABLE and DIE into deferred locations list. */
14674 static void
14675 defer_location (tree variable, dw_die_ref die)
14677 deferred_locations entry;
14678 entry.variable = variable;
14679 entry.die = die;
14680 VEC_safe_push (deferred_locations, gc, deferred_locations_list, entry);
14683 /* Helper function for tree_add_const_value_attribute. Natively encode
14684 initializer INIT into an array. Return true if successful. */
14686 static bool
14687 native_encode_initializer (tree init, unsigned char *array, int size)
14689 tree type;
14691 if (init == NULL_TREE)
14692 return false;
14694 STRIP_NOPS (init);
14695 switch (TREE_CODE (init))
14697 case STRING_CST:
14698 type = TREE_TYPE (init);
14699 if (TREE_CODE (type) == ARRAY_TYPE)
14701 tree enttype = TREE_TYPE (type);
14702 enum machine_mode mode = TYPE_MODE (enttype);
14704 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
14705 return false;
14706 if (int_size_in_bytes (type) != size)
14707 return false;
14708 if (size > TREE_STRING_LENGTH (init))
14710 memcpy (array, TREE_STRING_POINTER (init),
14711 TREE_STRING_LENGTH (init));
14712 memset (array + TREE_STRING_LENGTH (init),
14713 '\0', size - TREE_STRING_LENGTH (init));
14715 else
14716 memcpy (array, TREE_STRING_POINTER (init), size);
14717 return true;
14719 return false;
14720 case CONSTRUCTOR:
14721 type = TREE_TYPE (init);
14722 if (int_size_in_bytes (type) != size)
14723 return false;
14724 if (TREE_CODE (type) == ARRAY_TYPE)
14726 HOST_WIDE_INT min_index;
14727 unsigned HOST_WIDE_INT cnt;
14728 int curpos = 0, fieldsize;
14729 constructor_elt *ce;
14731 if (TYPE_DOMAIN (type) == NULL_TREE
14732 || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
14733 return false;
14735 fieldsize = int_size_in_bytes (TREE_TYPE (type));
14736 if (fieldsize <= 0)
14737 return false;
14739 min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
14740 memset (array, '\0', size);
14741 FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
14743 tree val = ce->value;
14744 tree index = ce->index;
14745 int pos = curpos;
14746 if (index && TREE_CODE (index) == RANGE_EXPR)
14747 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
14748 * fieldsize;
14749 else if (index)
14750 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
14752 if (val)
14754 STRIP_NOPS (val);
14755 if (!native_encode_initializer (val, array + pos, fieldsize))
14756 return false;
14758 curpos = pos + fieldsize;
14759 if (index && TREE_CODE (index) == RANGE_EXPR)
14761 int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
14762 - tree_low_cst (TREE_OPERAND (index, 0), 0);
14763 while (count-- > 0)
14765 if (val)
14766 memcpy (array + curpos, array + pos, fieldsize);
14767 curpos += fieldsize;
14770 gcc_assert (curpos <= size);
14772 return true;
14774 else if (TREE_CODE (type) == RECORD_TYPE
14775 || TREE_CODE (type) == UNION_TYPE)
14777 tree field = NULL_TREE;
14778 unsigned HOST_WIDE_INT cnt;
14779 constructor_elt *ce;
14781 if (int_size_in_bytes (type) != size)
14782 return false;
14784 if (TREE_CODE (type) == RECORD_TYPE)
14785 field = TYPE_FIELDS (type);
14787 FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
14789 tree val = ce->value;
14790 int pos, fieldsize;
14792 if (ce->index != 0)
14793 field = ce->index;
14795 if (val)
14796 STRIP_NOPS (val);
14798 if (field == NULL_TREE || DECL_BIT_FIELD (field))
14799 return false;
14801 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
14802 && TYPE_DOMAIN (TREE_TYPE (field))
14803 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
14804 return false;
14805 else if (DECL_SIZE_UNIT (field) == NULL_TREE
14806 || !host_integerp (DECL_SIZE_UNIT (field), 0))
14807 return false;
14808 fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
14809 pos = int_byte_position (field);
14810 gcc_assert (pos + fieldsize <= size);
14811 if (val
14812 && !native_encode_initializer (val, array + pos, fieldsize))
14813 return false;
14815 return true;
14817 return false;
14818 case VIEW_CONVERT_EXPR:
14819 case NON_LVALUE_EXPR:
14820 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
14821 default:
14822 return native_encode_expr (init, array, size) == size;
14826 /* Attach a DW_AT_const_value attribute to DIE. The value of the
14827 attribute is the const value T. */
14829 static bool
14830 tree_add_const_value_attribute (dw_die_ref die, tree t)
14832 tree init;
14833 tree type = TREE_TYPE (t);
14834 rtx rtl;
14836 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
14837 return false;
14839 init = t;
14840 gcc_assert (!DECL_P (init));
14842 rtl = rtl_for_decl_init (init, type);
14843 if (rtl)
14844 return add_const_value_attribute (die, rtl);
14845 /* If the host and target are sane, try harder. */
14846 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
14847 && initializer_constant_valid_p (init, type))
14849 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
14850 if (size > 0 && (int) size == size)
14852 unsigned char *array = (unsigned char *)
14853 ggc_alloc_cleared_atomic (size);
14855 if (native_encode_initializer (init, array, size))
14857 add_AT_vec (die, DW_AT_const_value, size, 1, array);
14858 return true;
14862 return false;
14865 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
14866 attribute is the const value of T, where T is an integral constant
14867 variable with static storage duration
14868 (so it can't be a PARM_DECL or a RESULT_DECL). */
14870 static bool
14871 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
14874 if (!decl
14875 || (TREE_CODE (decl) != VAR_DECL
14876 && TREE_CODE (decl) != CONST_DECL)
14877 || (TREE_CODE (decl) == VAR_DECL
14878 && !TREE_STATIC (decl)))
14879 return false;
14881 if (TREE_READONLY (decl)
14882 && ! TREE_THIS_VOLATILE (decl)
14883 && DECL_INITIAL (decl))
14884 /* OK */;
14885 else
14886 return false;
14888 /* Don't add DW_AT_const_value if abstract origin already has one. */
14889 if (get_AT (var_die, DW_AT_const_value))
14890 return false;
14892 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
14895 /* Convert the CFI instructions for the current function into a
14896 location list. This is used for DW_AT_frame_base when we targeting
14897 a dwarf2 consumer that does not support the dwarf3
14898 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
14899 expressions. */
14901 static dw_loc_list_ref
14902 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
14904 int ix;
14905 dw_fde_ref fde;
14906 dw_loc_list_ref list, *list_tail;
14907 dw_cfi_ref cfi;
14908 dw_cfa_location last_cfa, next_cfa;
14909 const char *start_label, *last_label, *section;
14910 dw_cfa_location remember;
14912 fde = cfun->fde;
14913 gcc_assert (fde != NULL);
14915 section = secname_for_decl (current_function_decl);
14916 list_tail = &list;
14917 list = NULL;
14919 memset (&next_cfa, 0, sizeof (next_cfa));
14920 next_cfa.reg = INVALID_REGNUM;
14921 remember = next_cfa;
14923 start_label = fde->dw_fde_begin;
14925 /* ??? Bald assumption that the CIE opcode list does not contain
14926 advance opcodes. */
14927 FOR_EACH_VEC_ELT (dw_cfi_ref, cie_cfi_vec, ix, cfi)
14928 lookup_cfa_1 (cfi, &next_cfa, &remember);
14930 last_cfa = next_cfa;
14931 last_label = start_label;
14933 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
14935 /* If the first partition contained no CFI adjustments, the
14936 CIE opcodes apply to the whole first partition. */
14937 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
14938 fde->dw_fde_begin, fde->dw_fde_end, section);
14939 list_tail =&(*list_tail)->dw_loc_next;
14940 start_label = last_label = fde->dw_fde_second_begin;
14943 FOR_EACH_VEC_ELT (dw_cfi_ref, fde->dw_fde_cfi, ix, cfi)
14945 switch (cfi->dw_cfi_opc)
14947 case DW_CFA_set_loc:
14948 case DW_CFA_advance_loc1:
14949 case DW_CFA_advance_loc2:
14950 case DW_CFA_advance_loc4:
14951 if (!cfa_equal_p (&last_cfa, &next_cfa))
14953 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
14954 start_label, last_label, section);
14956 list_tail = &(*list_tail)->dw_loc_next;
14957 last_cfa = next_cfa;
14958 start_label = last_label;
14960 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
14961 break;
14963 case DW_CFA_advance_loc:
14964 /* The encoding is complex enough that we should never emit this. */
14965 gcc_unreachable ();
14967 default:
14968 lookup_cfa_1 (cfi, &next_cfa, &remember);
14969 break;
14971 if (ix + 1 == fde->dw_fde_switch_cfi_index)
14973 if (!cfa_equal_p (&last_cfa, &next_cfa))
14975 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
14976 start_label, last_label, section);
14978 list_tail = &(*list_tail)->dw_loc_next;
14979 last_cfa = next_cfa;
14980 start_label = last_label;
14982 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
14983 start_label, fde->dw_fde_end, section);
14984 list_tail = &(*list_tail)->dw_loc_next;
14985 start_label = last_label = fde->dw_fde_second_begin;
14989 if (!cfa_equal_p (&last_cfa, &next_cfa))
14991 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
14992 start_label, last_label, section);
14993 list_tail = &(*list_tail)->dw_loc_next;
14994 start_label = last_label;
14997 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
14998 start_label,
14999 fde->dw_fde_second_begin
15000 ? fde->dw_fde_second_end : fde->dw_fde_end,
15001 section);
15003 if (list && list->dw_loc_next)
15004 gen_llsym (list);
15006 return list;
15009 /* Compute a displacement from the "steady-state frame pointer" to the
15010 frame base (often the same as the CFA), and store it in
15011 frame_pointer_fb_offset. OFFSET is added to the displacement
15012 before the latter is negated. */
15014 static void
15015 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
15017 rtx reg, elim;
15019 #ifdef FRAME_POINTER_CFA_OFFSET
15020 reg = frame_pointer_rtx;
15021 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
15022 #else
15023 reg = arg_pointer_rtx;
15024 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
15025 #endif
15027 elim = (ira_use_lra_p
15028 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
15029 : eliminate_regs (reg, VOIDmode, NULL_RTX));
15030 if (GET_CODE (elim) == PLUS)
15032 offset += INTVAL (XEXP (elim, 1));
15033 elim = XEXP (elim, 0);
15036 frame_pointer_fb_offset = -offset;
15038 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
15039 in which to eliminate. This is because it's stack pointer isn't
15040 directly accessible as a register within the ISA. To work around
15041 this, assume that while we cannot provide a proper value for
15042 frame_pointer_fb_offset, we won't need one either. */
15043 frame_pointer_fb_offset_valid
15044 = ((SUPPORTS_STACK_ALIGNMENT
15045 && (elim == hard_frame_pointer_rtx
15046 || elim == stack_pointer_rtx))
15047 || elim == (frame_pointer_needed
15048 ? hard_frame_pointer_rtx
15049 : stack_pointer_rtx));
15052 /* Generate a DW_AT_name attribute given some string value to be included as
15053 the value of the attribute. */
15055 static void
15056 add_name_attribute (dw_die_ref die, const char *name_string)
15058 if (name_string != NULL && *name_string != 0)
15060 if (demangle_name_func)
15061 name_string = (*demangle_name_func) (name_string);
15063 add_AT_string (die, DW_AT_name, name_string);
15067 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
15068 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
15069 of TYPE accordingly.
15071 ??? This is a temporary measure until after we're able to generate
15072 regular DWARF for the complex Ada type system. */
15074 static void
15075 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
15076 dw_die_ref context_die)
15078 tree dtype;
15079 dw_die_ref dtype_die;
15081 if (!lang_hooks.types.descriptive_type)
15082 return;
15084 dtype = lang_hooks.types.descriptive_type (type);
15085 if (!dtype)
15086 return;
15088 dtype_die = lookup_type_die (dtype);
15089 if (!dtype_die)
15091 gen_type_die (dtype, context_die);
15092 dtype_die = lookup_type_die (dtype);
15093 gcc_assert (dtype_die);
15096 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
15099 /* Generate a DW_AT_comp_dir attribute for DIE. */
15101 static void
15102 add_comp_dir_attribute (dw_die_ref die)
15104 const char *wd = get_src_pwd ();
15105 char *wd1;
15107 if (wd == NULL)
15108 return;
15110 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
15112 int wdlen;
15114 wdlen = strlen (wd);
15115 wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
15116 strcpy (wd1, wd);
15117 wd1 [wdlen] = DIR_SEPARATOR;
15118 wd1 [wdlen + 1] = 0;
15119 wd = wd1;
15122 add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
15125 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
15126 default. */
15128 static int
15129 lower_bound_default (void)
15131 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
15133 case DW_LANG_C:
15134 case DW_LANG_C89:
15135 case DW_LANG_C99:
15136 case DW_LANG_C_plus_plus:
15137 case DW_LANG_ObjC:
15138 case DW_LANG_ObjC_plus_plus:
15139 case DW_LANG_Java:
15140 return 0;
15141 case DW_LANG_Fortran77:
15142 case DW_LANG_Fortran90:
15143 case DW_LANG_Fortran95:
15144 return 1;
15145 case DW_LANG_UPC:
15146 case DW_LANG_D:
15147 case DW_LANG_Python:
15148 return dwarf_version >= 4 ? 0 : -1;
15149 case DW_LANG_Ada95:
15150 case DW_LANG_Ada83:
15151 case DW_LANG_Cobol74:
15152 case DW_LANG_Cobol85:
15153 case DW_LANG_Pascal83:
15154 case DW_LANG_Modula2:
15155 case DW_LANG_PLI:
15156 return dwarf_version >= 4 ? 1 : -1;
15157 default:
15158 return -1;
15162 /* Given a tree node describing an array bound (either lower or upper) output
15163 a representation for that bound. */
15165 static void
15166 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
15168 switch (TREE_CODE (bound))
15170 case ERROR_MARK:
15171 return;
15173 /* All fixed-bounds are represented by INTEGER_CST nodes. */
15174 case INTEGER_CST:
15176 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
15177 int dflt;
15179 /* Use the default if possible. */
15180 if (bound_attr == DW_AT_lower_bound
15181 && host_integerp (bound, 0)
15182 && (dflt = lower_bound_default ()) != -1
15183 && tree_low_cst (bound, 0) == dflt)
15186 /* Otherwise represent the bound as an unsigned value with the
15187 precision of its type. The precision and signedness of the
15188 type will be necessary to re-interpret it unambiguously. */
15189 else if (prec < HOST_BITS_PER_WIDE_INT)
15191 unsigned HOST_WIDE_INT mask
15192 = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
15193 add_AT_unsigned (subrange_die, bound_attr,
15194 TREE_INT_CST_LOW (bound) & mask);
15196 else if (prec == HOST_BITS_PER_WIDE_INT
15197 || TREE_INT_CST_HIGH (bound) == 0)
15198 add_AT_unsigned (subrange_die, bound_attr,
15199 TREE_INT_CST_LOW (bound));
15200 else
15201 add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
15202 TREE_INT_CST_LOW (bound));
15204 break;
15206 CASE_CONVERT:
15207 case VIEW_CONVERT_EXPR:
15208 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
15209 break;
15211 case SAVE_EXPR:
15212 break;
15214 case VAR_DECL:
15215 case PARM_DECL:
15216 case RESULT_DECL:
15218 dw_die_ref decl_die = lookup_decl_die (bound);
15220 /* ??? Can this happen, or should the variable have been bound
15221 first? Probably it can, since I imagine that we try to create
15222 the types of parameters in the order in which they exist in
15223 the list, and won't have created a forward reference to a
15224 later parameter. */
15225 if (decl_die != NULL)
15227 add_AT_die_ref (subrange_die, bound_attr, decl_die);
15228 break;
15231 /* FALLTHRU */
15233 default:
15235 /* Otherwise try to create a stack operation procedure to
15236 evaluate the value of the array bound. */
15238 dw_die_ref ctx, decl_die;
15239 dw_loc_list_ref list;
15241 list = loc_list_from_tree (bound, 2);
15242 if (list == NULL || single_element_loc_list_p (list))
15244 /* If DW_AT_*bound is not a reference nor constant, it is
15245 a DWARF expression rather than location description.
15246 For that loc_list_from_tree (bound, 0) is needed.
15247 If that fails to give a single element list,
15248 fall back to outputting this as a reference anyway. */
15249 dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
15250 if (list2 && single_element_loc_list_p (list2))
15252 add_AT_loc (subrange_die, bound_attr, list2->expr);
15253 break;
15256 if (list == NULL)
15257 break;
15259 if (current_function_decl == 0)
15260 ctx = comp_unit_die ();
15261 else
15262 ctx = lookup_decl_die (current_function_decl);
15264 decl_die = new_die (DW_TAG_variable, ctx, bound);
15265 add_AT_flag (decl_die, DW_AT_artificial, 1);
15266 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
15267 add_AT_location_description (decl_die, DW_AT_location, list);
15268 add_AT_die_ref (subrange_die, bound_attr, decl_die);
15269 break;
15274 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
15275 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
15276 Note that the block of subscript information for an array type also
15277 includes information about the element type of the given array type. */
15279 static void
15280 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
15282 unsigned dimension_number;
15283 tree lower, upper;
15284 dw_die_ref subrange_die;
15286 for (dimension_number = 0;
15287 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
15288 type = TREE_TYPE (type), dimension_number++)
15290 tree domain = TYPE_DOMAIN (type);
15292 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
15293 break;
15295 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
15296 and (in GNU C only) variable bounds. Handle all three forms
15297 here. */
15298 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
15299 if (domain)
15301 /* We have an array type with specified bounds. */
15302 lower = TYPE_MIN_VALUE (domain);
15303 upper = TYPE_MAX_VALUE (domain);
15305 /* Define the index type. */
15306 if (TREE_TYPE (domain))
15308 /* ??? This is probably an Ada unnamed subrange type. Ignore the
15309 TREE_TYPE field. We can't emit debug info for this
15310 because it is an unnamed integral type. */
15311 if (TREE_CODE (domain) == INTEGER_TYPE
15312 && TYPE_NAME (domain) == NULL_TREE
15313 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
15314 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
15316 else
15317 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
15318 type_die);
15321 /* ??? If upper is NULL, the array has unspecified length,
15322 but it does have a lower bound. This happens with Fortran
15323 dimension arr(N:*)
15324 Since the debugger is definitely going to need to know N
15325 to produce useful results, go ahead and output the lower
15326 bound solo, and hope the debugger can cope. */
15328 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
15329 if (upper)
15330 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
15333 /* Otherwise we have an array type with an unspecified length. The
15334 DWARF-2 spec does not say how to handle this; let's just leave out the
15335 bounds. */
15339 static void
15340 add_byte_size_attribute (dw_die_ref die, tree tree_node)
15342 dw_die_ref decl_die;
15343 unsigned size;
15345 switch (TREE_CODE (tree_node))
15347 case ERROR_MARK:
15348 size = 0;
15349 break;
15350 case ENUMERAL_TYPE:
15351 case RECORD_TYPE:
15352 case UNION_TYPE:
15353 case QUAL_UNION_TYPE:
15354 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
15355 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
15357 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
15358 return;
15360 size = int_size_in_bytes (tree_node);
15361 break;
15362 case FIELD_DECL:
15363 /* For a data member of a struct or union, the DW_AT_byte_size is
15364 generally given as the number of bytes normally allocated for an
15365 object of the *declared* type of the member itself. This is true
15366 even for bit-fields. */
15367 size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
15368 break;
15369 default:
15370 gcc_unreachable ();
15373 /* Note that `size' might be -1 when we get to this point. If it is, that
15374 indicates that the byte size of the entity in question is variable. We
15375 have no good way of expressing this fact in Dwarf at the present time,
15376 so just let the -1 pass on through. */
15377 add_AT_unsigned (die, DW_AT_byte_size, size);
15380 /* For a FIELD_DECL node which represents a bit-field, output an attribute
15381 which specifies the distance in bits from the highest order bit of the
15382 "containing object" for the bit-field to the highest order bit of the
15383 bit-field itself.
15385 For any given bit-field, the "containing object" is a hypothetical object
15386 (of some integral or enum type) within which the given bit-field lives. The
15387 type of this hypothetical "containing object" is always the same as the
15388 declared type of the individual bit-field itself. The determination of the
15389 exact location of the "containing object" for a bit-field is rather
15390 complicated. It's handled by the `field_byte_offset' function (above).
15392 Note that it is the size (in bytes) of the hypothetical "containing object"
15393 which will be given in the DW_AT_byte_size attribute for this bit-field.
15394 (See `byte_size_attribute' above). */
15396 static inline void
15397 add_bit_offset_attribute (dw_die_ref die, tree decl)
15399 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
15400 tree type = DECL_BIT_FIELD_TYPE (decl);
15401 HOST_WIDE_INT bitpos_int;
15402 HOST_WIDE_INT highest_order_object_bit_offset;
15403 HOST_WIDE_INT highest_order_field_bit_offset;
15404 HOST_WIDE_INT bit_offset;
15406 /* Must be a field and a bit field. */
15407 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
15409 /* We can't yet handle bit-fields whose offsets are variable, so if we
15410 encounter such things, just return without generating any attribute
15411 whatsoever. Likewise for variable or too large size. */
15412 if (! host_integerp (bit_position (decl), 0)
15413 || ! host_integerp (DECL_SIZE (decl), 1))
15414 return;
15416 bitpos_int = int_bit_position (decl);
15418 /* Note that the bit offset is always the distance (in bits) from the
15419 highest-order bit of the "containing object" to the highest-order bit of
15420 the bit-field itself. Since the "high-order end" of any object or field
15421 is different on big-endian and little-endian machines, the computation
15422 below must take account of these differences. */
15423 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
15424 highest_order_field_bit_offset = bitpos_int;
15426 if (! BYTES_BIG_ENDIAN)
15428 highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
15429 highest_order_object_bit_offset += simple_type_size_in_bits (type);
15432 bit_offset
15433 = (! BYTES_BIG_ENDIAN
15434 ? highest_order_object_bit_offset - highest_order_field_bit_offset
15435 : highest_order_field_bit_offset - highest_order_object_bit_offset);
15437 if (bit_offset < 0)
15438 add_AT_int (die, DW_AT_bit_offset, bit_offset);
15439 else
15440 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
15443 /* For a FIELD_DECL node which represents a bit field, output an attribute
15444 which specifies the length in bits of the given field. */
15446 static inline void
15447 add_bit_size_attribute (dw_die_ref die, tree decl)
15449 /* Must be a field and a bit field. */
15450 gcc_assert (TREE_CODE (decl) == FIELD_DECL
15451 && DECL_BIT_FIELD_TYPE (decl));
15453 if (host_integerp (DECL_SIZE (decl), 1))
15454 add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
15457 /* If the compiled language is ANSI C, then add a 'prototyped'
15458 attribute, if arg types are given for the parameters of a function. */
15460 static inline void
15461 add_prototyped_attribute (dw_die_ref die, tree func_type)
15463 if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
15464 && prototype_p (func_type))
15465 add_AT_flag (die, DW_AT_prototyped, 1);
15468 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
15469 by looking in either the type declaration or object declaration
15470 equate table. */
15472 static inline dw_die_ref
15473 add_abstract_origin_attribute (dw_die_ref die, tree origin)
15475 dw_die_ref origin_die = NULL;
15477 if (TREE_CODE (origin) != FUNCTION_DECL)
15479 /* We may have gotten separated from the block for the inlined
15480 function, if we're in an exception handler or some such; make
15481 sure that the abstract function has been written out.
15483 Doing this for nested functions is wrong, however; functions are
15484 distinct units, and our context might not even be inline. */
15485 tree fn = origin;
15487 if (TYPE_P (fn))
15488 fn = TYPE_STUB_DECL (fn);
15490 fn = decl_function_context (fn);
15491 if (fn)
15492 dwarf2out_abstract_function (fn);
15495 if (DECL_P (origin))
15496 origin_die = lookup_decl_die (origin);
15497 else if (TYPE_P (origin))
15498 origin_die = lookup_type_die (origin);
15500 /* XXX: Functions that are never lowered don't always have correct block
15501 trees (in the case of java, they simply have no block tree, in some other
15502 languages). For these functions, there is nothing we can really do to
15503 output correct debug info for inlined functions in all cases. Rather
15504 than die, we'll just produce deficient debug info now, in that we will
15505 have variables without a proper abstract origin. In the future, when all
15506 functions are lowered, we should re-add a gcc_assert (origin_die)
15507 here. */
15509 if (origin_die)
15510 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
15511 return origin_die;
15514 /* We do not currently support the pure_virtual attribute. */
15516 static inline void
15517 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
15519 if (DECL_VINDEX (func_decl))
15521 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
15523 if (host_integerp (DECL_VINDEX (func_decl), 0))
15524 add_AT_loc (die, DW_AT_vtable_elem_location,
15525 new_loc_descr (DW_OP_constu,
15526 tree_low_cst (DECL_VINDEX (func_decl), 0),
15527 0));
15529 /* GNU extension: Record what type this method came from originally. */
15530 if (debug_info_level > DINFO_LEVEL_TERSE
15531 && DECL_CONTEXT (func_decl))
15532 add_AT_die_ref (die, DW_AT_containing_type,
15533 lookup_type_die (DECL_CONTEXT (func_decl)));
15537 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
15538 given decl. This used to be a vendor extension until after DWARF 4
15539 standardized it. */
15541 static void
15542 add_linkage_attr (dw_die_ref die, tree decl)
15544 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
15546 /* Mimic what assemble_name_raw does with a leading '*'. */
15547 if (name[0] == '*')
15548 name = &name[1];
15550 if (dwarf_version >= 4)
15551 add_AT_string (die, DW_AT_linkage_name, name);
15552 else
15553 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
15556 /* Add source coordinate attributes for the given decl. */
15558 static void
15559 add_src_coords_attributes (dw_die_ref die, tree decl)
15561 expanded_location s;
15563 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
15564 return;
15565 s = expand_location (DECL_SOURCE_LOCATION (decl));
15566 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
15567 add_AT_unsigned (die, DW_AT_decl_line, s.line);
15570 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
15572 static void
15573 add_linkage_name (dw_die_ref die, tree decl)
15575 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
15576 && TREE_PUBLIC (decl)
15577 && !DECL_ABSTRACT (decl)
15578 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
15579 && die->die_tag != DW_TAG_member)
15581 /* Defer until we have an assembler name set. */
15582 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
15584 limbo_die_node *asm_name;
15586 asm_name = ggc_alloc_cleared_limbo_die_node ();
15587 asm_name->die = die;
15588 asm_name->created_for = decl;
15589 asm_name->next = deferred_asm_name;
15590 deferred_asm_name = asm_name;
15592 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
15593 add_linkage_attr (die, decl);
15597 /* Add a DW_AT_name attribute and source coordinate attribute for the
15598 given decl, but only if it actually has a name. */
15600 static void
15601 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
15603 tree decl_name;
15605 decl_name = DECL_NAME (decl);
15606 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
15608 const char *name = dwarf2_name (decl, 0);
15609 if (name)
15610 add_name_attribute (die, name);
15611 if (! DECL_ARTIFICIAL (decl))
15612 add_src_coords_attributes (die, decl);
15614 add_linkage_name (die, decl);
15617 #ifdef VMS_DEBUGGING_INFO
15618 /* Get the function's name, as described by its RTL. This may be different
15619 from the DECL_NAME name used in the source file. */
15620 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
15622 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
15623 XEXP (DECL_RTL (decl), 0));
15624 VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
15626 #endif /* VMS_DEBUGGING_INFO */
15629 #ifdef VMS_DEBUGGING_INFO
15630 /* Output the debug main pointer die for VMS */
15632 void
15633 dwarf2out_vms_debug_main_pointer (void)
15635 char label[MAX_ARTIFICIAL_LABEL_BYTES];
15636 dw_die_ref die;
15638 /* Allocate the VMS debug main subprogram die. */
15639 die = ggc_alloc_cleared_die_node ();
15640 die->die_tag = DW_TAG_subprogram;
15641 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
15642 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
15643 current_function_funcdef_no);
15644 add_AT_lbl_id (die, DW_AT_entry_pc, label);
15646 /* Make it the first child of comp_unit_die (). */
15647 die->die_parent = comp_unit_die ();
15648 if (comp_unit_die ()->die_child)
15650 die->die_sib = comp_unit_die ()->die_child->die_sib;
15651 comp_unit_die ()->die_child->die_sib = die;
15653 else
15655 die->die_sib = die;
15656 comp_unit_die ()->die_child = die;
15659 #endif /* VMS_DEBUGGING_INFO */
15661 /* Push a new declaration scope. */
15663 static void
15664 push_decl_scope (tree scope)
15666 VEC_safe_push (tree, gc, decl_scope_table, scope);
15669 /* Pop a declaration scope. */
15671 static inline void
15672 pop_decl_scope (void)
15674 VEC_pop (tree, decl_scope_table);
15677 /* walk_tree helper function for uses_local_type, below. */
15679 static tree
15680 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
15682 if (!TYPE_P (*tp))
15683 *walk_subtrees = 0;
15684 else
15686 tree name = TYPE_NAME (*tp);
15687 if (name && DECL_P (name) && decl_function_context (name))
15688 return *tp;
15690 return NULL_TREE;
15693 /* If TYPE involves a function-local type (including a local typedef to a
15694 non-local type), returns that type; otherwise returns NULL_TREE. */
15696 static tree
15697 uses_local_type (tree type)
15699 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
15700 return used;
15703 /* Return the DIE for the scope that immediately contains this type.
15704 Non-named types that do not involve a function-local type get global
15705 scope. Named types nested in namespaces or other types get their
15706 containing scope. All other types (i.e. function-local named types) get
15707 the current active scope. */
15709 static dw_die_ref
15710 scope_die_for (tree t, dw_die_ref context_die)
15712 dw_die_ref scope_die = NULL;
15713 tree containing_scope;
15715 /* Non-types always go in the current scope. */
15716 gcc_assert (TYPE_P (t));
15718 /* Use the scope of the typedef, rather than the scope of the type
15719 it refers to. */
15720 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
15721 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
15722 else
15723 containing_scope = TYPE_CONTEXT (t);
15725 /* Use the containing namespace if there is one. */
15726 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
15728 if (context_die == lookup_decl_die (containing_scope))
15729 /* OK */;
15730 else if (debug_info_level > DINFO_LEVEL_TERSE)
15731 context_die = get_context_die (containing_scope);
15732 else
15733 containing_scope = NULL_TREE;
15736 /* Ignore function type "scopes" from the C frontend. They mean that
15737 a tagged type is local to a parmlist of a function declarator, but
15738 that isn't useful to DWARF. */
15739 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
15740 containing_scope = NULL_TREE;
15742 if (SCOPE_FILE_SCOPE_P (containing_scope))
15744 /* If T uses a local type keep it local as well, to avoid references
15745 to function-local DIEs from outside the function. */
15746 if (current_function_decl && uses_local_type (t))
15747 scope_die = context_die;
15748 else
15749 scope_die = comp_unit_die ();
15751 else if (TYPE_P (containing_scope))
15753 /* For types, we can just look up the appropriate DIE. */
15754 if (debug_info_level > DINFO_LEVEL_TERSE)
15755 scope_die = get_context_die (containing_scope);
15756 else
15758 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
15759 if (scope_die == NULL)
15760 scope_die = comp_unit_die ();
15763 else
15764 scope_die = context_die;
15766 return scope_die;
15769 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
15771 static inline int
15772 local_scope_p (dw_die_ref context_die)
15774 for (; context_die; context_die = context_die->die_parent)
15775 if (context_die->die_tag == DW_TAG_inlined_subroutine
15776 || context_die->die_tag == DW_TAG_subprogram)
15777 return 1;
15779 return 0;
15782 /* Returns nonzero if CONTEXT_DIE is a class. */
15784 static inline int
15785 class_scope_p (dw_die_ref context_die)
15787 return (context_die
15788 && (context_die->die_tag == DW_TAG_structure_type
15789 || context_die->die_tag == DW_TAG_class_type
15790 || context_die->die_tag == DW_TAG_interface_type
15791 || context_die->die_tag == DW_TAG_union_type));
15794 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
15795 whether or not to treat a DIE in this context as a declaration. */
15797 static inline int
15798 class_or_namespace_scope_p (dw_die_ref context_die)
15800 return (class_scope_p (context_die)
15801 || (context_die && context_die->die_tag == DW_TAG_namespace));
15804 /* Many forms of DIEs require a "type description" attribute. This
15805 routine locates the proper "type descriptor" die for the type given
15806 by 'type', and adds a DW_AT_type attribute below the given die. */
15808 static void
15809 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
15810 int decl_volatile, dw_die_ref context_die)
15812 enum tree_code code = TREE_CODE (type);
15813 dw_die_ref type_die = NULL;
15815 /* ??? If this type is an unnamed subrange type of an integral, floating-point
15816 or fixed-point type, use the inner type. This is because we have no
15817 support for unnamed types in base_type_die. This can happen if this is
15818 an Ada subrange type. Correct solution is emit a subrange type die. */
15819 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
15820 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
15821 type = TREE_TYPE (type), code = TREE_CODE (type);
15823 if (code == ERROR_MARK
15824 /* Handle a special case. For functions whose return type is void, we
15825 generate *no* type attribute. (Note that no object may have type
15826 `void', so this only applies to function return types). */
15827 || code == VOID_TYPE)
15828 return;
15830 type_die = modified_type_die (type,
15831 decl_const || TYPE_READONLY (type),
15832 decl_volatile || TYPE_VOLATILE (type),
15833 context_die);
15835 if (type_die != NULL)
15836 add_AT_die_ref (object_die, DW_AT_type, type_die);
15839 /* Given an object die, add the calling convention attribute for the
15840 function call type. */
15841 static void
15842 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
15844 enum dwarf_calling_convention value = DW_CC_normal;
15846 value = ((enum dwarf_calling_convention)
15847 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
15849 if (is_fortran ()
15850 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
15852 /* DWARF 2 doesn't provide a way to identify a program's source-level
15853 entry point. DW_AT_calling_convention attributes are only meant
15854 to describe functions' calling conventions. However, lacking a
15855 better way to signal the Fortran main program, we used this for
15856 a long time, following existing custom. Now, DWARF 4 has
15857 DW_AT_main_subprogram, which we add below, but some tools still
15858 rely on the old way, which we thus keep. */
15859 value = DW_CC_program;
15861 if (dwarf_version >= 4 || !dwarf_strict)
15862 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
15865 /* Only add the attribute if the backend requests it, and
15866 is not DW_CC_normal. */
15867 if (value && (value != DW_CC_normal))
15868 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
15871 /* Given a tree pointer to a struct, class, union, or enum type node, return
15872 a pointer to the (string) tag name for the given type, or zero if the type
15873 was declared without a tag. */
15875 static const char *
15876 type_tag (const_tree type)
15878 const char *name = 0;
15880 if (TYPE_NAME (type) != 0)
15882 tree t = 0;
15884 /* Find the IDENTIFIER_NODE for the type name. */
15885 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
15886 && !TYPE_NAMELESS (type))
15887 t = TYPE_NAME (type);
15889 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
15890 a TYPE_DECL node, regardless of whether or not a `typedef' was
15891 involved. */
15892 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
15893 && ! DECL_IGNORED_P (TYPE_NAME (type)))
15895 /* We want to be extra verbose. Don't call dwarf_name if
15896 DECL_NAME isn't set. The default hook for decl_printable_name
15897 doesn't like that, and in this context it's correct to return
15898 0, instead of "<anonymous>" or the like. */
15899 if (DECL_NAME (TYPE_NAME (type))
15900 && !DECL_NAMELESS (TYPE_NAME (type)))
15901 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
15904 /* Now get the name as a string, or invent one. */
15905 if (!name && t != 0)
15906 name = IDENTIFIER_POINTER (t);
15909 return (name == 0 || *name == '\0') ? 0 : name;
15912 /* Return the type associated with a data member, make a special check
15913 for bit field types. */
15915 static inline tree
15916 member_declared_type (const_tree member)
15918 return (DECL_BIT_FIELD_TYPE (member)
15919 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
15922 /* Get the decl's label, as described by its RTL. This may be different
15923 from the DECL_NAME name used in the source file. */
15925 #if 0
15926 static const char *
15927 decl_start_label (tree decl)
15929 rtx x;
15930 const char *fnname;
15932 x = DECL_RTL (decl);
15933 gcc_assert (MEM_P (x));
15935 x = XEXP (x, 0);
15936 gcc_assert (GET_CODE (x) == SYMBOL_REF);
15938 fnname = XSTR (x, 0);
15939 return fnname;
15941 #endif
15943 /* These routines generate the internal representation of the DIE's for
15944 the compilation unit. Debugging information is collected by walking
15945 the declaration trees passed in from dwarf2out_decl(). */
15947 static void
15948 gen_array_type_die (tree type, dw_die_ref context_die)
15950 dw_die_ref scope_die = scope_die_for (type, context_die);
15951 dw_die_ref array_die;
15953 /* GNU compilers represent multidimensional array types as sequences of one
15954 dimensional array types whose element types are themselves array types.
15955 We sometimes squish that down to a single array_type DIE with multiple
15956 subscripts in the Dwarf debugging info. The draft Dwarf specification
15957 say that we are allowed to do this kind of compression in C, because
15958 there is no difference between an array of arrays and a multidimensional
15959 array. We don't do this for Ada to remain as close as possible to the
15960 actual representation, which is especially important against the language
15961 flexibilty wrt arrays of variable size. */
15963 bool collapse_nested_arrays = !is_ada ();
15964 tree element_type;
15966 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
15967 DW_TAG_string_type doesn't have DW_AT_type attribute). */
15968 if (TYPE_STRING_FLAG (type)
15969 && TREE_CODE (type) == ARRAY_TYPE
15970 && is_fortran ()
15971 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
15973 HOST_WIDE_INT size;
15975 array_die = new_die (DW_TAG_string_type, scope_die, type);
15976 add_name_attribute (array_die, type_tag (type));
15977 equate_type_number_to_die (type, array_die);
15978 size = int_size_in_bytes (type);
15979 if (size >= 0)
15980 add_AT_unsigned (array_die, DW_AT_byte_size, size);
15981 else if (TYPE_DOMAIN (type) != NULL_TREE
15982 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
15983 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
15985 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
15986 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
15988 size = int_size_in_bytes (TREE_TYPE (szdecl));
15989 if (loc && size > 0)
15991 add_AT_location_description (array_die, DW_AT_string_length, loc);
15992 if (size != DWARF2_ADDR_SIZE)
15993 add_AT_unsigned (array_die, DW_AT_byte_size, size);
15996 return;
15999 array_die = new_die (DW_TAG_array_type, scope_die, type);
16000 add_name_attribute (array_die, type_tag (type));
16001 equate_type_number_to_die (type, array_die);
16003 if (TREE_CODE (type) == VECTOR_TYPE)
16004 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
16006 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
16007 if (is_fortran ()
16008 && TREE_CODE (type) == ARRAY_TYPE
16009 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
16010 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
16011 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16013 #if 0
16014 /* We default the array ordering. SDB will probably do
16015 the right things even if DW_AT_ordering is not present. It's not even
16016 an issue until we start to get into multidimensional arrays anyway. If
16017 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
16018 then we'll have to put the DW_AT_ordering attribute back in. (But if
16019 and when we find out that we need to put these in, we will only do so
16020 for multidimensional arrays. */
16021 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
16022 #endif
16024 if (TREE_CODE (type) == VECTOR_TYPE)
16026 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
16027 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
16028 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
16029 add_bound_info (subrange_die, DW_AT_upper_bound,
16030 size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
16032 else
16033 add_subscript_info (array_die, type, collapse_nested_arrays);
16035 /* Add representation of the type of the elements of this array type and
16036 emit the corresponding DIE if we haven't done it already. */
16037 element_type = TREE_TYPE (type);
16038 if (collapse_nested_arrays)
16039 while (TREE_CODE (element_type) == ARRAY_TYPE)
16041 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
16042 break;
16043 element_type = TREE_TYPE (element_type);
16046 add_type_attribute (array_die, element_type, 0, 0, context_die);
16048 add_gnat_descriptive_type_attribute (array_die, type, context_die);
16049 if (TYPE_ARTIFICIAL (type))
16050 add_AT_flag (array_die, DW_AT_artificial, 1);
16052 if (get_AT (array_die, DW_AT_name))
16053 add_pubtype (type, array_die);
16056 static dw_loc_descr_ref
16057 descr_info_loc (tree val, tree base_decl)
16059 HOST_WIDE_INT size;
16060 dw_loc_descr_ref loc, loc2;
16061 enum dwarf_location_atom op;
16063 if (val == base_decl)
16064 return new_loc_descr (DW_OP_push_object_address, 0, 0);
16066 switch (TREE_CODE (val))
16068 CASE_CONVERT:
16069 return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16070 case VAR_DECL:
16071 return loc_descriptor_from_tree (val, 0);
16072 case INTEGER_CST:
16073 if (host_integerp (val, 0))
16074 return int_loc_descriptor (tree_low_cst (val, 0));
16075 break;
16076 case INDIRECT_REF:
16077 size = int_size_in_bytes (TREE_TYPE (val));
16078 if (size < 0)
16079 break;
16080 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16081 if (!loc)
16082 break;
16083 if (size == DWARF2_ADDR_SIZE)
16084 add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
16085 else
16086 add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
16087 return loc;
16088 case POINTER_PLUS_EXPR:
16089 case PLUS_EXPR:
16090 if (host_integerp (TREE_OPERAND (val, 1), 1)
16091 && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
16092 < 16384)
16094 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16095 if (!loc)
16096 break;
16097 loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
16099 else
16101 op = DW_OP_plus;
16102 do_binop:
16103 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16104 if (!loc)
16105 break;
16106 loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
16107 if (!loc2)
16108 break;
16109 add_loc_descr (&loc, loc2);
16110 add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
16112 return loc;
16113 case MINUS_EXPR:
16114 op = DW_OP_minus;
16115 goto do_binop;
16116 case MULT_EXPR:
16117 op = DW_OP_mul;
16118 goto do_binop;
16119 case EQ_EXPR:
16120 op = DW_OP_eq;
16121 goto do_binop;
16122 case NE_EXPR:
16123 op = DW_OP_ne;
16124 goto do_binop;
16125 default:
16126 break;
16128 return NULL;
16131 static void
16132 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
16133 tree val, tree base_decl)
16135 dw_loc_descr_ref loc;
16137 if (host_integerp (val, 0))
16139 add_AT_unsigned (die, attr, tree_low_cst (val, 0));
16140 return;
16143 loc = descr_info_loc (val, base_decl);
16144 if (!loc)
16145 return;
16147 add_AT_loc (die, attr, loc);
16150 /* This routine generates DIE for array with hidden descriptor, details
16151 are filled into *info by a langhook. */
16153 static void
16154 gen_descr_array_type_die (tree type, struct array_descr_info *info,
16155 dw_die_ref context_die)
16157 dw_die_ref scope_die = scope_die_for (type, context_die);
16158 dw_die_ref array_die;
16159 int dim;
16161 array_die = new_die (DW_TAG_array_type, scope_die, type);
16162 add_name_attribute (array_die, type_tag (type));
16163 equate_type_number_to_die (type, array_die);
16165 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
16166 if (is_fortran ()
16167 && info->ndimensions >= 2)
16168 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16170 if (info->data_location)
16171 add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
16172 info->base_decl);
16173 if (info->associated)
16174 add_descr_info_field (array_die, DW_AT_associated, info->associated,
16175 info->base_decl);
16176 if (info->allocated)
16177 add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
16178 info->base_decl);
16180 for (dim = 0; dim < info->ndimensions; dim++)
16182 dw_die_ref subrange_die
16183 = new_die (DW_TAG_subrange_type, array_die, NULL);
16185 if (info->dimen[dim].lower_bound)
16187 /* If it is the default value, omit it. */
16188 int dflt;
16190 if (host_integerp (info->dimen[dim].lower_bound, 0)
16191 && (dflt = lower_bound_default ()) != -1
16192 && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
16194 else
16195 add_descr_info_field (subrange_die, DW_AT_lower_bound,
16196 info->dimen[dim].lower_bound,
16197 info->base_decl);
16199 if (info->dimen[dim].upper_bound)
16200 add_descr_info_field (subrange_die, DW_AT_upper_bound,
16201 info->dimen[dim].upper_bound,
16202 info->base_decl);
16203 if (info->dimen[dim].stride)
16204 add_descr_info_field (subrange_die, DW_AT_byte_stride,
16205 info->dimen[dim].stride,
16206 info->base_decl);
16209 gen_type_die (info->element_type, context_die);
16210 add_type_attribute (array_die, info->element_type, 0, 0, context_die);
16212 if (get_AT (array_die, DW_AT_name))
16213 add_pubtype (type, array_die);
16216 #if 0
16217 static void
16218 gen_entry_point_die (tree decl, dw_die_ref context_die)
16220 tree origin = decl_ultimate_origin (decl);
16221 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
16223 if (origin != NULL)
16224 add_abstract_origin_attribute (decl_die, origin);
16225 else
16227 add_name_and_src_coords_attributes (decl_die, decl);
16228 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
16229 0, 0, context_die);
16232 if (DECL_ABSTRACT (decl))
16233 equate_decl_number_to_die (decl, decl_die);
16234 else
16235 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
16237 #endif
16239 /* Walk through the list of incomplete types again, trying once more to
16240 emit full debugging info for them. */
16242 static void
16243 retry_incomplete_types (void)
16245 int i;
16247 for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
16248 if (should_emit_struct_debug (VEC_index (tree, incomplete_types, i),
16249 DINFO_USAGE_DIR_USE))
16250 gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die ());
16253 /* Determine what tag to use for a record type. */
16255 static enum dwarf_tag
16256 record_type_tag (tree type)
16258 if (! lang_hooks.types.classify_record)
16259 return DW_TAG_structure_type;
16261 switch (lang_hooks.types.classify_record (type))
16263 case RECORD_IS_STRUCT:
16264 return DW_TAG_structure_type;
16266 case RECORD_IS_CLASS:
16267 return DW_TAG_class_type;
16269 case RECORD_IS_INTERFACE:
16270 if (dwarf_version >= 3 || !dwarf_strict)
16271 return DW_TAG_interface_type;
16272 return DW_TAG_structure_type;
16274 default:
16275 gcc_unreachable ();
16279 /* Generate a DIE to represent an enumeration type. Note that these DIEs
16280 include all of the information about the enumeration values also. Each
16281 enumerated type name/value is listed as a child of the enumerated type
16282 DIE. */
16284 static dw_die_ref
16285 gen_enumeration_type_die (tree type, dw_die_ref context_die)
16287 dw_die_ref type_die = lookup_type_die (type);
16289 if (type_die == NULL)
16291 type_die = new_die (DW_TAG_enumeration_type,
16292 scope_die_for (type, context_die), type);
16293 equate_type_number_to_die (type, type_die);
16294 add_name_attribute (type_die, type_tag (type));
16295 if (dwarf_version >= 4 || !dwarf_strict)
16297 if (ENUM_IS_SCOPED (type))
16298 add_AT_flag (type_die, DW_AT_enum_class, 1);
16299 if (ENUM_IS_OPAQUE (type))
16300 add_AT_flag (type_die, DW_AT_declaration, 1);
16303 else if (! TYPE_SIZE (type))
16304 return type_die;
16305 else
16306 remove_AT (type_die, DW_AT_declaration);
16308 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
16309 given enum type is incomplete, do not generate the DW_AT_byte_size
16310 attribute or the DW_AT_element_list attribute. */
16311 if (TYPE_SIZE (type))
16313 tree link;
16315 TREE_ASM_WRITTEN (type) = 1;
16316 add_byte_size_attribute (type_die, type);
16317 if (TYPE_STUB_DECL (type) != NULL_TREE)
16319 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
16320 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
16323 /* If the first reference to this type was as the return type of an
16324 inline function, then it may not have a parent. Fix this now. */
16325 if (type_die->die_parent == NULL)
16326 add_child_die (scope_die_for (type, context_die), type_die);
16328 for (link = TYPE_VALUES (type);
16329 link != NULL; link = TREE_CHAIN (link))
16331 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
16332 tree value = TREE_VALUE (link);
16334 add_name_attribute (enum_die,
16335 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
16337 if (TREE_CODE (value) == CONST_DECL)
16338 value = DECL_INITIAL (value);
16340 if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
16341 /* DWARF2 does not provide a way of indicating whether or
16342 not enumeration constants are signed or unsigned. GDB
16343 always assumes the values are signed, so we output all
16344 values as if they were signed. That means that
16345 enumeration constants with very large unsigned values
16346 will appear to have negative values in the debugger. */
16347 add_AT_int (enum_die, DW_AT_const_value,
16348 tree_low_cst (value, tree_int_cst_sgn (value) > 0));
16351 add_gnat_descriptive_type_attribute (type_die, type, context_die);
16352 if (TYPE_ARTIFICIAL (type))
16353 add_AT_flag (type_die, DW_AT_artificial, 1);
16355 else
16356 add_AT_flag (type_die, DW_AT_declaration, 1);
16358 add_pubtype (type, type_die);
16360 return type_die;
16363 /* Generate a DIE to represent either a real live formal parameter decl or to
16364 represent just the type of some formal parameter position in some function
16365 type.
16367 Note that this routine is a bit unusual because its argument may be a
16368 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
16369 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
16370 node. If it's the former then this function is being called to output a
16371 DIE to represent a formal parameter object (or some inlining thereof). If
16372 it's the latter, then this function is only being called to output a
16373 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
16374 argument type of some subprogram type.
16375 If EMIT_NAME_P is true, name and source coordinate attributes
16376 are emitted. */
16378 static dw_die_ref
16379 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
16380 dw_die_ref context_die)
16382 tree node_or_origin = node ? node : origin;
16383 tree ultimate_origin;
16384 dw_die_ref parm_die
16385 = new_die (DW_TAG_formal_parameter, context_die, node);
16387 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
16389 case tcc_declaration:
16390 ultimate_origin = decl_ultimate_origin (node_or_origin);
16391 if (node || ultimate_origin)
16392 origin = ultimate_origin;
16393 if (origin != NULL)
16394 add_abstract_origin_attribute (parm_die, origin);
16395 else if (emit_name_p)
16396 add_name_and_src_coords_attributes (parm_die, node);
16397 if (origin == NULL
16398 || (! DECL_ABSTRACT (node_or_origin)
16399 && variably_modified_type_p (TREE_TYPE (node_or_origin),
16400 decl_function_context
16401 (node_or_origin))))
16403 tree type = TREE_TYPE (node_or_origin);
16404 if (decl_by_reference_p (node_or_origin))
16405 add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
16406 context_die);
16407 else
16408 add_type_attribute (parm_die, type,
16409 TREE_READONLY (node_or_origin),
16410 TREE_THIS_VOLATILE (node_or_origin),
16411 context_die);
16413 if (origin == NULL && DECL_ARTIFICIAL (node))
16414 add_AT_flag (parm_die, DW_AT_artificial, 1);
16416 if (node && node != origin)
16417 equate_decl_number_to_die (node, parm_die);
16418 if (! DECL_ABSTRACT (node_or_origin))
16419 add_location_or_const_value_attribute (parm_die, node_or_origin,
16420 node == NULL, DW_AT_location);
16422 break;
16424 case tcc_type:
16425 /* We were called with some kind of a ..._TYPE node. */
16426 add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
16427 break;
16429 default:
16430 gcc_unreachable ();
16433 return parm_die;
16436 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
16437 children DW_TAG_formal_parameter DIEs representing the arguments of the
16438 parameter pack.
16440 PARM_PACK must be a function parameter pack.
16441 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
16442 must point to the subsequent arguments of the function PACK_ARG belongs to.
16443 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
16444 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
16445 following the last one for which a DIE was generated. */
16447 static dw_die_ref
16448 gen_formal_parameter_pack_die (tree parm_pack,
16449 tree pack_arg,
16450 dw_die_ref subr_die,
16451 tree *next_arg)
16453 tree arg;
16454 dw_die_ref parm_pack_die;
16456 gcc_assert (parm_pack
16457 && lang_hooks.function_parameter_pack_p (parm_pack)
16458 && subr_die);
16460 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
16461 add_src_coords_attributes (parm_pack_die, parm_pack);
16463 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
16465 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
16466 parm_pack))
16467 break;
16468 gen_formal_parameter_die (arg, NULL,
16469 false /* Don't emit name attribute. */,
16470 parm_pack_die);
16472 if (next_arg)
16473 *next_arg = arg;
16474 return parm_pack_die;
16477 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
16478 at the end of an (ANSI prototyped) formal parameters list. */
16480 static void
16481 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
16483 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
16486 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
16487 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
16488 parameters as specified in some function type specification (except for
16489 those which appear as part of a function *definition*). */
16491 static void
16492 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
16494 tree link;
16495 tree formal_type = NULL;
16496 tree first_parm_type;
16497 tree arg;
16499 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
16501 arg = DECL_ARGUMENTS (function_or_method_type);
16502 function_or_method_type = TREE_TYPE (function_or_method_type);
16504 else
16505 arg = NULL_TREE;
16507 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
16509 /* Make our first pass over the list of formal parameter types and output a
16510 DW_TAG_formal_parameter DIE for each one. */
16511 for (link = first_parm_type; link; )
16513 dw_die_ref parm_die;
16515 formal_type = TREE_VALUE (link);
16516 if (formal_type == void_type_node)
16517 break;
16519 /* Output a (nameless) DIE to represent the formal parameter itself. */
16520 parm_die = gen_formal_parameter_die (formal_type, NULL,
16521 true /* Emit name attribute. */,
16522 context_die);
16523 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
16524 && link == first_parm_type)
16526 add_AT_flag (parm_die, DW_AT_artificial, 1);
16527 if (dwarf_version >= 3 || !dwarf_strict)
16528 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
16530 else if (arg && DECL_ARTIFICIAL (arg))
16531 add_AT_flag (parm_die, DW_AT_artificial, 1);
16533 link = TREE_CHAIN (link);
16534 if (arg)
16535 arg = DECL_CHAIN (arg);
16538 /* If this function type has an ellipsis, add a
16539 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
16540 if (formal_type != void_type_node)
16541 gen_unspecified_parameters_die (function_or_method_type, context_die);
16543 /* Make our second (and final) pass over the list of formal parameter types
16544 and output DIEs to represent those types (as necessary). */
16545 for (link = TYPE_ARG_TYPES (function_or_method_type);
16546 link && TREE_VALUE (link);
16547 link = TREE_CHAIN (link))
16548 gen_type_die (TREE_VALUE (link), context_die);
16551 /* We want to generate the DIE for TYPE so that we can generate the
16552 die for MEMBER, which has been defined; we will need to refer back
16553 to the member declaration nested within TYPE. If we're trying to
16554 generate minimal debug info for TYPE, processing TYPE won't do the
16555 trick; we need to attach the member declaration by hand. */
16557 static void
16558 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
16560 gen_type_die (type, context_die);
16562 /* If we're trying to avoid duplicate debug info, we may not have
16563 emitted the member decl for this function. Emit it now. */
16564 if (TYPE_STUB_DECL (type)
16565 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
16566 && ! lookup_decl_die (member))
16568 dw_die_ref type_die;
16569 gcc_assert (!decl_ultimate_origin (member));
16571 push_decl_scope (type);
16572 type_die = lookup_type_die_strip_naming_typedef (type);
16573 if (TREE_CODE (member) == FUNCTION_DECL)
16574 gen_subprogram_die (member, type_die);
16575 else if (TREE_CODE (member) == FIELD_DECL)
16577 /* Ignore the nameless fields that are used to skip bits but handle
16578 C++ anonymous unions and structs. */
16579 if (DECL_NAME (member) != NULL_TREE
16580 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
16581 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
16583 gen_type_die (member_declared_type (member), type_die);
16584 gen_field_die (member, type_die);
16587 else
16588 gen_variable_die (member, NULL_TREE, type_die);
16590 pop_decl_scope ();
16594 /* Forward declare these functions, because they are mutually recursive
16595 with their set_block_* pairing functions. */
16596 static void set_decl_origin_self (tree);
16597 static void set_decl_abstract_flags (tree, int);
16599 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
16600 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
16601 that it points to the node itself, thus indicating that the node is its
16602 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
16603 the given node is NULL, recursively descend the decl/block tree which
16604 it is the root of, and for each other ..._DECL or BLOCK node contained
16605 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
16606 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
16607 values to point to themselves. */
16609 static void
16610 set_block_origin_self (tree stmt)
16612 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
16614 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
16617 tree local_decl;
16619 for (local_decl = BLOCK_VARS (stmt);
16620 local_decl != NULL_TREE;
16621 local_decl = DECL_CHAIN (local_decl))
16622 if (! DECL_EXTERNAL (local_decl))
16623 set_decl_origin_self (local_decl); /* Potential recursion. */
16627 tree subblock;
16629 for (subblock = BLOCK_SUBBLOCKS (stmt);
16630 subblock != NULL_TREE;
16631 subblock = BLOCK_CHAIN (subblock))
16632 set_block_origin_self (subblock); /* Recurse. */
16637 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
16638 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
16639 node to so that it points to the node itself, thus indicating that the
16640 node represents its own (abstract) origin. Additionally, if the
16641 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
16642 the decl/block tree of which the given node is the root of, and for
16643 each other ..._DECL or BLOCK node contained therein whose
16644 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
16645 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
16646 point to themselves. */
16648 static void
16649 set_decl_origin_self (tree decl)
16651 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
16653 DECL_ABSTRACT_ORIGIN (decl) = decl;
16654 if (TREE_CODE (decl) == FUNCTION_DECL)
16656 tree arg;
16658 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
16659 DECL_ABSTRACT_ORIGIN (arg) = arg;
16660 if (DECL_INITIAL (decl) != NULL_TREE
16661 && DECL_INITIAL (decl) != error_mark_node)
16662 set_block_origin_self (DECL_INITIAL (decl));
16667 /* Given a pointer to some BLOCK node, and a boolean value to set the
16668 "abstract" flags to, set that value into the BLOCK_ABSTRACT flag for
16669 the given block, and for all local decls and all local sub-blocks
16670 (recursively) which are contained therein. */
16672 static void
16673 set_block_abstract_flags (tree stmt, int setting)
16675 tree local_decl;
16676 tree subblock;
16677 unsigned int i;
16679 BLOCK_ABSTRACT (stmt) = setting;
16681 for (local_decl = BLOCK_VARS (stmt);
16682 local_decl != NULL_TREE;
16683 local_decl = DECL_CHAIN (local_decl))
16684 if (! DECL_EXTERNAL (local_decl))
16685 set_decl_abstract_flags (local_decl, setting);
16687 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
16689 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
16690 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
16691 || TREE_CODE (local_decl) == PARM_DECL)
16692 set_decl_abstract_flags (local_decl, setting);
16695 for (subblock = BLOCK_SUBBLOCKS (stmt);
16696 subblock != NULL_TREE;
16697 subblock = BLOCK_CHAIN (subblock))
16698 set_block_abstract_flags (subblock, setting);
16701 /* Given a pointer to some ..._DECL node, and a boolean value to set the
16702 "abstract" flags to, set that value into the DECL_ABSTRACT flag for the
16703 given decl, and (in the case where the decl is a FUNCTION_DECL) also
16704 set the abstract flags for all of the parameters, local vars, local
16705 blocks and sub-blocks (recursively) to the same setting. */
16707 static void
16708 set_decl_abstract_flags (tree decl, int setting)
16710 DECL_ABSTRACT (decl) = setting;
16711 if (TREE_CODE (decl) == FUNCTION_DECL)
16713 tree arg;
16715 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
16716 DECL_ABSTRACT (arg) = setting;
16717 if (DECL_INITIAL (decl) != NULL_TREE
16718 && DECL_INITIAL (decl) != error_mark_node)
16719 set_block_abstract_flags (DECL_INITIAL (decl), setting);
16723 /* Generate the DWARF2 info for the "abstract" instance of a function which we
16724 may later generate inlined and/or out-of-line instances of. */
16726 static void
16727 dwarf2out_abstract_function (tree decl)
16729 dw_die_ref old_die;
16730 tree save_fn;
16731 tree context;
16732 int was_abstract;
16733 htab_t old_decl_loc_table;
16734 htab_t old_cached_dw_loc_list_table;
16735 int old_call_site_count, old_tail_call_site_count;
16736 struct call_arg_loc_node *old_call_arg_locations;
16738 /* Make sure we have the actual abstract inline, not a clone. */
16739 decl = DECL_ORIGIN (decl);
16741 old_die = lookup_decl_die (decl);
16742 if (old_die && get_AT (old_die, DW_AT_inline))
16743 /* We've already generated the abstract instance. */
16744 return;
16746 /* We can be called while recursively when seeing block defining inlined subroutine
16747 DIE. Be sure to not clobber the outer location table nor use it or we would
16748 get locations in abstract instantces. */
16749 old_decl_loc_table = decl_loc_table;
16750 decl_loc_table = NULL;
16751 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
16752 cached_dw_loc_list_table = NULL;
16753 old_call_arg_locations = call_arg_locations;
16754 call_arg_locations = NULL;
16755 old_call_site_count = call_site_count;
16756 call_site_count = -1;
16757 old_tail_call_site_count = tail_call_site_count;
16758 tail_call_site_count = -1;
16760 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
16761 we don't get confused by DECL_ABSTRACT. */
16762 if (debug_info_level > DINFO_LEVEL_TERSE)
16764 context = decl_class_context (decl);
16765 if (context)
16766 gen_type_die_for_member
16767 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
16770 /* Pretend we've just finished compiling this function. */
16771 save_fn = current_function_decl;
16772 current_function_decl = decl;
16774 was_abstract = DECL_ABSTRACT (decl);
16775 set_decl_abstract_flags (decl, 1);
16776 dwarf2out_decl (decl);
16777 if (! was_abstract)
16778 set_decl_abstract_flags (decl, 0);
16780 current_function_decl = save_fn;
16781 decl_loc_table = old_decl_loc_table;
16782 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
16783 call_arg_locations = old_call_arg_locations;
16784 call_site_count = old_call_site_count;
16785 tail_call_site_count = old_tail_call_site_count;
16788 /* Helper function of premark_used_types() which gets called through
16789 htab_traverse.
16791 Marks the DIE of a given type in *SLOT as perennial, so it never gets
16792 marked as unused by prune_unused_types. */
16794 static int
16795 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
16797 tree type;
16798 dw_die_ref die;
16800 type = (tree) *slot;
16801 die = lookup_type_die (type);
16802 if (die != NULL)
16803 die->die_perennial_p = 1;
16804 return 1;
16807 /* Helper function of premark_types_used_by_global_vars which gets called
16808 through htab_traverse.
16810 Marks the DIE of a given type in *SLOT as perennial, so it never gets
16811 marked as unused by prune_unused_types. The DIE of the type is marked
16812 only if the global variable using the type will actually be emitted. */
16814 static int
16815 premark_types_used_by_global_vars_helper (void **slot,
16816 void *data ATTRIBUTE_UNUSED)
16818 struct types_used_by_vars_entry *entry;
16819 dw_die_ref die;
16821 entry = (struct types_used_by_vars_entry *) *slot;
16822 gcc_assert (entry->type != NULL
16823 && entry->var_decl != NULL);
16824 die = lookup_type_die (entry->type);
16825 if (die)
16827 /* Ask cgraph if the global variable really is to be emitted.
16828 If yes, then we'll keep the DIE of ENTRY->TYPE. */
16829 struct varpool_node *node = varpool_get_node (entry->var_decl);
16830 if (node && node->analyzed)
16832 die->die_perennial_p = 1;
16833 /* Keep the parent DIEs as well. */
16834 while ((die = die->die_parent) && die->die_perennial_p == 0)
16835 die->die_perennial_p = 1;
16838 return 1;
16841 /* Mark all members of used_types_hash as perennial. */
16843 static void
16844 premark_used_types (struct function *fun)
16846 if (fun && fun->used_types_hash)
16847 htab_traverse (fun->used_types_hash, premark_used_types_helper, NULL);
16850 /* Mark all members of types_used_by_vars_entry as perennial. */
16852 static void
16853 premark_types_used_by_global_vars (void)
16855 if (types_used_by_vars_hash)
16856 htab_traverse (types_used_by_vars_hash,
16857 premark_types_used_by_global_vars_helper, NULL);
16860 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
16861 for CA_LOC call arg loc node. */
16863 static dw_die_ref
16864 gen_call_site_die (tree decl, dw_die_ref subr_die,
16865 struct call_arg_loc_node *ca_loc)
16867 dw_die_ref stmt_die = NULL, die;
16868 tree block = ca_loc->block;
16870 while (block
16871 && block != DECL_INITIAL (decl)
16872 && TREE_CODE (block) == BLOCK)
16874 if (VEC_length (dw_die_ref, block_map) > BLOCK_NUMBER (block))
16875 stmt_die = VEC_index (dw_die_ref, block_map, BLOCK_NUMBER (block));
16876 if (stmt_die)
16877 break;
16878 block = BLOCK_SUPERCONTEXT (block);
16880 if (stmt_die == NULL)
16881 stmt_die = subr_die;
16882 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
16883 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
16884 if (ca_loc->tail_call_p)
16885 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
16886 if (ca_loc->symbol_ref)
16888 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
16889 if (tdie)
16890 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
16891 else
16892 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref);
16894 return die;
16897 /* Generate a DIE to represent a declared function (either file-scope or
16898 block-local). */
16900 static void
16901 gen_subprogram_die (tree decl, dw_die_ref context_die)
16903 tree origin = decl_ultimate_origin (decl);
16904 dw_die_ref subr_die;
16905 tree outer_scope;
16906 dw_die_ref old_die = lookup_decl_die (decl);
16907 int declaration = (current_function_decl != decl
16908 || class_or_namespace_scope_p (context_die));
16910 premark_used_types (DECL_STRUCT_FUNCTION (decl));
16912 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
16913 started to generate the abstract instance of an inline, decided to output
16914 its containing class, and proceeded to emit the declaration of the inline
16915 from the member list for the class. If so, DECLARATION takes priority;
16916 we'll get back to the abstract instance when done with the class. */
16918 /* The class-scope declaration DIE must be the primary DIE. */
16919 if (origin && declaration && class_or_namespace_scope_p (context_die))
16921 origin = NULL;
16922 gcc_assert (!old_die);
16925 /* Now that the C++ front end lazily declares artificial member fns, we
16926 might need to retrofit the declaration into its class. */
16927 if (!declaration && !origin && !old_die
16928 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
16929 && !class_or_namespace_scope_p (context_die)
16930 && debug_info_level > DINFO_LEVEL_TERSE)
16931 old_die = force_decl_die (decl);
16933 if (origin != NULL)
16935 gcc_assert (!declaration || local_scope_p (context_die));
16937 /* Fixup die_parent for the abstract instance of a nested
16938 inline function. */
16939 if (old_die && old_die->die_parent == NULL)
16940 add_child_die (context_die, old_die);
16942 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
16943 add_abstract_origin_attribute (subr_die, origin);
16944 /* This is where the actual code for a cloned function is.
16945 Let's emit linkage name attribute for it. This helps
16946 debuggers to e.g, set breakpoints into
16947 constructors/destructors when the user asks "break
16948 K::K". */
16949 add_linkage_name (subr_die, decl);
16951 else if (old_die)
16953 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
16954 struct dwarf_file_data * file_index = lookup_filename (s.file);
16956 if (!get_AT_flag (old_die, DW_AT_declaration)
16957 /* We can have a normal definition following an inline one in the
16958 case of redefinition of GNU C extern inlines.
16959 It seems reasonable to use AT_specification in this case. */
16960 && !get_AT (old_die, DW_AT_inline))
16962 /* Detect and ignore this case, where we are trying to output
16963 something we have already output. */
16964 return;
16967 /* If the definition comes from the same place as the declaration,
16968 maybe use the old DIE. We always want the DIE for this function
16969 that has the *_pc attributes to be under comp_unit_die so the
16970 debugger can find it. We also need to do this for abstract
16971 instances of inlines, since the spec requires the out-of-line copy
16972 to have the same parent. For local class methods, this doesn't
16973 apply; we just use the old DIE. */
16974 if ((is_cu_die (old_die->die_parent) || context_die == NULL)
16975 && (DECL_ARTIFICIAL (decl)
16976 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
16977 && (get_AT_unsigned (old_die, DW_AT_decl_line)
16978 == (unsigned) s.line))))
16980 subr_die = old_die;
16982 /* Clear out the declaration attribute and the formal parameters.
16983 Do not remove all children, because it is possible that this
16984 declaration die was forced using force_decl_die(). In such
16985 cases die that forced declaration die (e.g. TAG_imported_module)
16986 is one of the children that we do not want to remove. */
16987 remove_AT (subr_die, DW_AT_declaration);
16988 remove_AT (subr_die, DW_AT_object_pointer);
16989 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
16991 else
16993 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
16994 add_AT_specification (subr_die, old_die);
16995 add_pubname (decl, subr_die);
16996 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
16997 add_AT_file (subr_die, DW_AT_decl_file, file_index);
16998 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
16999 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
17002 else
17004 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17006 if (TREE_PUBLIC (decl))
17007 add_AT_flag (subr_die, DW_AT_external, 1);
17009 add_name_and_src_coords_attributes (subr_die, decl);
17010 add_pubname (decl, subr_die);
17011 if (debug_info_level > DINFO_LEVEL_TERSE)
17013 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
17014 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
17015 0, 0, context_die);
17018 add_pure_or_virtual_attribute (subr_die, decl);
17019 if (DECL_ARTIFICIAL (decl))
17020 add_AT_flag (subr_die, DW_AT_artificial, 1);
17022 add_accessibility_attribute (subr_die, decl);
17025 if (declaration)
17027 if (!old_die || !get_AT (old_die, DW_AT_inline))
17029 add_AT_flag (subr_die, DW_AT_declaration, 1);
17031 /* If this is an explicit function declaration then generate
17032 a DW_AT_explicit attribute. */
17033 if (lang_hooks.decls.function_decl_explicit_p (decl)
17034 && (dwarf_version >= 3 || !dwarf_strict))
17035 add_AT_flag (subr_die, DW_AT_explicit, 1);
17037 /* The first time we see a member function, it is in the context of
17038 the class to which it belongs. We make sure of this by emitting
17039 the class first. The next time is the definition, which is
17040 handled above. The two may come from the same source text.
17042 Note that force_decl_die() forces function declaration die. It is
17043 later reused to represent definition. */
17044 equate_decl_number_to_die (decl, subr_die);
17047 else if (DECL_ABSTRACT (decl))
17049 if (DECL_DECLARED_INLINE_P (decl))
17051 if (cgraph_function_possibly_inlined_p (decl))
17052 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
17053 else
17054 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
17056 else
17058 if (cgraph_function_possibly_inlined_p (decl))
17059 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
17060 else
17061 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
17064 if (DECL_DECLARED_INLINE_P (decl)
17065 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
17066 add_AT_flag (subr_die, DW_AT_artificial, 1);
17068 equate_decl_number_to_die (decl, subr_die);
17070 else if (!DECL_EXTERNAL (decl))
17072 HOST_WIDE_INT cfa_fb_offset;
17073 struct function *fun = DECL_STRUCT_FUNCTION (decl);
17075 if (!old_die || !get_AT (old_die, DW_AT_inline))
17076 equate_decl_number_to_die (decl, subr_die);
17078 gcc_checking_assert (fun);
17079 if (!flag_reorder_blocks_and_partition)
17081 dw_fde_ref fde = fun->fde;
17082 if (fde->dw_fde_begin)
17084 /* We have already generated the labels. */
17085 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end);
17087 else
17089 /* Create start/end labels and add the range. */
17090 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
17091 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
17092 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
17093 current_function_funcdef_no);
17094 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
17095 current_function_funcdef_no);
17096 add_AT_low_high_pc (subr_die, label_id_low, label_id_high);
17099 #if VMS_DEBUGGING_INFO
17100 /* HP OpenVMS Industry Standard 64: DWARF Extensions
17101 Section 2.3 Prologue and Epilogue Attributes:
17102 When a breakpoint is set on entry to a function, it is generally
17103 desirable for execution to be suspended, not on the very first
17104 instruction of the function, but rather at a point after the
17105 function's frame has been set up, after any language defined local
17106 declaration processing has been completed, and before execution of
17107 the first statement of the function begins. Debuggers generally
17108 cannot properly determine where this point is. Similarly for a
17109 breakpoint set on exit from a function. The prologue and epilogue
17110 attributes allow a compiler to communicate the location(s) to use. */
17113 if (fde->dw_fde_vms_end_prologue)
17114 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
17115 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
17117 if (fde->dw_fde_vms_begin_epilogue)
17118 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
17119 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
17121 #endif
17124 else
17126 /* Generate pubnames entries for the split function code ranges. */
17127 dw_fde_ref fde = fun->fde;
17129 if (fde->dw_fde_second_begin)
17131 if (dwarf_version >= 3 || !dwarf_strict)
17133 /* We should use ranges for non-contiguous code section
17134 addresses. Use the actual code range for the initial
17135 section, since the HOT/COLD labels might precede an
17136 alignment offset. */
17137 bool range_list_added = false;
17138 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
17139 fde->dw_fde_end, &range_list_added);
17140 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
17141 fde->dw_fde_second_end,
17142 &range_list_added);
17143 if (range_list_added)
17144 add_ranges (NULL);
17146 else
17148 /* There is no real support in DW2 for this .. so we make
17149 a work-around. First, emit the pub name for the segment
17150 containing the function label. Then make and emit a
17151 simplified subprogram DIE for the second segment with the
17152 name pre-fixed by __hot/cold_sect_of_. We use the same
17153 linkage name for the second die so that gdb will find both
17154 sections when given "b foo". */
17155 const char *name = NULL;
17156 tree decl_name = DECL_NAME (decl);
17157 dw_die_ref seg_die;
17159 /* Do the 'primary' section. */
17160 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
17161 fde->dw_fde_end);
17163 /* Build a minimal DIE for the secondary section. */
17164 seg_die = new_die (DW_TAG_subprogram,
17165 subr_die->die_parent, decl);
17167 if (TREE_PUBLIC (decl))
17168 add_AT_flag (seg_die, DW_AT_external, 1);
17170 if (decl_name != NULL
17171 && IDENTIFIER_POINTER (decl_name) != NULL)
17173 name = dwarf2_name (decl, 1);
17174 if (! DECL_ARTIFICIAL (decl))
17175 add_src_coords_attributes (seg_die, decl);
17177 add_linkage_name (seg_die, decl);
17179 gcc_assert (name != NULL);
17180 add_pure_or_virtual_attribute (seg_die, decl);
17181 if (DECL_ARTIFICIAL (decl))
17182 add_AT_flag (seg_die, DW_AT_artificial, 1);
17184 name = concat ("__second_sect_of_", name, NULL);
17185 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
17186 fde->dw_fde_second_end);
17187 add_name_attribute (seg_die, name);
17188 if (want_pubnames ())
17189 add_pubname_string (name, seg_die);
17192 else
17193 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end);
17196 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
17198 /* We define the "frame base" as the function's CFA. This is more
17199 convenient for several reasons: (1) It's stable across the prologue
17200 and epilogue, which makes it better than just a frame pointer,
17201 (2) With dwarf3, there exists a one-byte encoding that allows us
17202 to reference the .debug_frame data by proxy, but failing that,
17203 (3) We can at least reuse the code inspection and interpretation
17204 code that determines the CFA position at various points in the
17205 function. */
17206 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
17208 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
17209 add_AT_loc (subr_die, DW_AT_frame_base, op);
17211 else
17213 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
17214 if (list->dw_loc_next)
17215 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
17216 else
17217 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
17220 /* Compute a displacement from the "steady-state frame pointer" to
17221 the CFA. The former is what all stack slots and argument slots
17222 will reference in the rtl; the later is what we've told the
17223 debugger about. We'll need to adjust all frame_base references
17224 by this displacement. */
17225 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
17227 if (fun->static_chain_decl)
17228 add_AT_location_description (subr_die, DW_AT_static_link,
17229 loc_list_from_tree (fun->static_chain_decl, 2));
17232 /* Generate child dies for template paramaters. */
17233 if (debug_info_level > DINFO_LEVEL_TERSE)
17234 gen_generic_params_dies (decl);
17236 /* Now output descriptions of the arguments for this function. This gets
17237 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
17238 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
17239 `...' at the end of the formal parameter list. In order to find out if
17240 there was a trailing ellipsis or not, we must instead look at the type
17241 associated with the FUNCTION_DECL. This will be a node of type
17242 FUNCTION_TYPE. If the chain of type nodes hanging off of this
17243 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
17244 an ellipsis at the end. */
17246 /* In the case where we are describing a mere function declaration, all we
17247 need to do here (and all we *can* do here) is to describe the *types* of
17248 its formal parameters. */
17249 if (debug_info_level <= DINFO_LEVEL_TERSE)
17251 else if (declaration)
17252 gen_formal_types_die (decl, subr_die);
17253 else
17255 /* Generate DIEs to represent all known formal parameters. */
17256 tree parm = DECL_ARGUMENTS (decl);
17257 tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
17258 tree generic_decl_parm = generic_decl
17259 ? DECL_ARGUMENTS (generic_decl)
17260 : NULL;
17262 /* Now we want to walk the list of parameters of the function and
17263 emit their relevant DIEs.
17265 We consider the case of DECL being an instance of a generic function
17266 as well as it being a normal function.
17268 If DECL is an instance of a generic function we walk the
17269 parameters of the generic function declaration _and_ the parameters of
17270 DECL itself. This is useful because we want to emit specific DIEs for
17271 function parameter packs and those are declared as part of the
17272 generic function declaration. In that particular case,
17273 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
17274 That DIE has children DIEs representing the set of arguments
17275 of the pack. Note that the set of pack arguments can be empty.
17276 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
17277 children DIE.
17279 Otherwise, we just consider the parameters of DECL. */
17280 while (generic_decl_parm || parm)
17282 if (generic_decl_parm
17283 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
17284 gen_formal_parameter_pack_die (generic_decl_parm,
17285 parm, subr_die,
17286 &parm);
17287 else if (parm)
17289 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
17291 if (parm == DECL_ARGUMENTS (decl)
17292 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
17293 && parm_die
17294 && (dwarf_version >= 3 || !dwarf_strict))
17295 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
17297 parm = DECL_CHAIN (parm);
17300 if (generic_decl_parm)
17301 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
17304 /* Decide whether we need an unspecified_parameters DIE at the end.
17305 There are 2 more cases to do this for: 1) the ansi ... declaration -
17306 this is detectable when the end of the arg list is not a
17307 void_type_node 2) an unprototyped function declaration (not a
17308 definition). This just means that we have no info about the
17309 parameters at all. */
17310 if (prototype_p (TREE_TYPE (decl)))
17312 /* This is the prototyped case, check for.... */
17313 if (stdarg_p (TREE_TYPE (decl)))
17314 gen_unspecified_parameters_die (decl, subr_die);
17316 else if (DECL_INITIAL (decl) == NULL_TREE)
17317 gen_unspecified_parameters_die (decl, subr_die);
17320 /* Output Dwarf info for all of the stuff within the body of the function
17321 (if it has one - it may be just a declaration). */
17322 outer_scope = DECL_INITIAL (decl);
17324 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
17325 a function. This BLOCK actually represents the outermost binding contour
17326 for the function, i.e. the contour in which the function's formal
17327 parameters and labels get declared. Curiously, it appears that the front
17328 end doesn't actually put the PARM_DECL nodes for the current function onto
17329 the BLOCK_VARS list for this outer scope, but are strung off of the
17330 DECL_ARGUMENTS list for the function instead.
17332 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
17333 the LABEL_DECL nodes for the function however, and we output DWARF info
17334 for those in decls_for_scope. Just within the `outer_scope' there will be
17335 a BLOCK node representing the function's outermost pair of curly braces,
17336 and any blocks used for the base and member initializers of a C++
17337 constructor function. */
17338 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
17340 int call_site_note_count = 0;
17341 int tail_call_site_note_count = 0;
17343 /* Emit a DW_TAG_variable DIE for a named return value. */
17344 if (DECL_NAME (DECL_RESULT (decl)))
17345 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
17347 current_function_has_inlines = 0;
17348 decls_for_scope (outer_scope, subr_die, 0);
17350 if (call_arg_locations && !dwarf_strict)
17352 struct call_arg_loc_node *ca_loc;
17353 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
17355 dw_die_ref die = NULL;
17356 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
17357 rtx arg, next_arg;
17359 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
17360 arg; arg = next_arg)
17362 dw_loc_descr_ref reg, val;
17363 enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
17364 dw_die_ref cdie, tdie = NULL;
17366 next_arg = XEXP (arg, 1);
17367 if (REG_P (XEXP (XEXP (arg, 0), 0))
17368 && next_arg
17369 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
17370 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
17371 && REGNO (XEXP (XEXP (arg, 0), 0))
17372 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
17373 next_arg = XEXP (next_arg, 1);
17374 if (mode == VOIDmode)
17376 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
17377 if (mode == VOIDmode)
17378 mode = GET_MODE (XEXP (arg, 0));
17380 if (mode == VOIDmode || mode == BLKmode)
17381 continue;
17382 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
17384 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
17385 tloc = XEXP (XEXP (arg, 0), 1);
17386 continue;
17388 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
17389 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
17391 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
17392 tlocc = XEXP (XEXP (arg, 0), 1);
17393 continue;
17395 reg = NULL;
17396 if (REG_P (XEXP (XEXP (arg, 0), 0)))
17397 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
17398 VAR_INIT_STATUS_INITIALIZED);
17399 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
17401 rtx mem = XEXP (XEXP (arg, 0), 0);
17402 reg = mem_loc_descriptor (XEXP (mem, 0),
17403 get_address_mode (mem),
17404 GET_MODE (mem),
17405 VAR_INIT_STATUS_INITIALIZED);
17407 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
17408 == DEBUG_PARAMETER_REF)
17410 tree tdecl
17411 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
17412 tdie = lookup_decl_die (tdecl);
17413 if (tdie == NULL)
17414 continue;
17416 else
17417 continue;
17418 if (reg == NULL
17419 && GET_CODE (XEXP (XEXP (arg, 0), 0))
17420 != DEBUG_PARAMETER_REF)
17421 continue;
17422 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
17423 VOIDmode,
17424 VAR_INIT_STATUS_INITIALIZED);
17425 if (val == NULL)
17426 continue;
17427 if (die == NULL)
17428 die = gen_call_site_die (decl, subr_die, ca_loc);
17429 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
17430 NULL_TREE);
17431 if (reg != NULL)
17432 add_AT_loc (cdie, DW_AT_location, reg);
17433 else if (tdie != NULL)
17434 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
17435 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
17436 if (next_arg != XEXP (arg, 1))
17438 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
17439 if (mode == VOIDmode)
17440 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
17441 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
17442 0), 1),
17443 mode, VOIDmode,
17444 VAR_INIT_STATUS_INITIALIZED);
17445 if (val != NULL)
17446 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
17449 if (die == NULL
17450 && (ca_loc->symbol_ref || tloc))
17451 die = gen_call_site_die (decl, subr_die, ca_loc);
17452 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
17454 dw_loc_descr_ref tval = NULL;
17456 if (tloc != NULL_RTX)
17457 tval = mem_loc_descriptor (tloc,
17458 GET_MODE (tloc) == VOIDmode
17459 ? Pmode : GET_MODE (tloc),
17460 VOIDmode,
17461 VAR_INIT_STATUS_INITIALIZED);
17462 if (tval)
17463 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
17464 else if (tlocc != NULL_RTX)
17466 tval = mem_loc_descriptor (tlocc,
17467 GET_MODE (tlocc) == VOIDmode
17468 ? Pmode : GET_MODE (tlocc),
17469 VOIDmode,
17470 VAR_INIT_STATUS_INITIALIZED);
17471 if (tval)
17472 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
17473 tval);
17476 if (die != NULL)
17478 call_site_note_count++;
17479 if (ca_loc->tail_call_p)
17480 tail_call_site_note_count++;
17484 call_arg_locations = NULL;
17485 call_arg_loc_last = NULL;
17486 if (tail_call_site_count >= 0
17487 && tail_call_site_count == tail_call_site_note_count
17488 && !dwarf_strict)
17490 if (call_site_count >= 0
17491 && call_site_count == call_site_note_count)
17492 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
17493 else
17494 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
17496 call_site_count = -1;
17497 tail_call_site_count = -1;
17499 /* Add the calling convention attribute if requested. */
17500 add_calling_convention_attribute (subr_die, decl);
17504 /* Returns a hash value for X (which really is a die_struct). */
17506 static hashval_t
17507 common_block_die_table_hash (const void *x)
17509 const_dw_die_ref d = (const_dw_die_ref) x;
17510 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
17513 /* Return nonzero if decl_id and die_parent of die_struct X is the same
17514 as decl_id and die_parent of die_struct Y. */
17516 static int
17517 common_block_die_table_eq (const void *x, const void *y)
17519 const_dw_die_ref d = (const_dw_die_ref) x;
17520 const_dw_die_ref e = (const_dw_die_ref) y;
17521 return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
17524 /* Generate a DIE to represent a declared data object.
17525 Either DECL or ORIGIN must be non-null. */
17527 static void
17528 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
17530 HOST_WIDE_INT off = 0;
17531 tree com_decl;
17532 tree decl_or_origin = decl ? decl : origin;
17533 tree ultimate_origin;
17534 dw_die_ref var_die;
17535 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
17536 dw_die_ref origin_die;
17537 bool declaration = (DECL_EXTERNAL (decl_or_origin)
17538 || class_or_namespace_scope_p (context_die));
17539 bool specialization_p = false;
17541 ultimate_origin = decl_ultimate_origin (decl_or_origin);
17542 if (decl || ultimate_origin)
17543 origin = ultimate_origin;
17544 com_decl = fortran_common (decl_or_origin, &off);
17546 /* Symbol in common gets emitted as a child of the common block, in the form
17547 of a data member. */
17548 if (com_decl)
17550 dw_die_ref com_die;
17551 dw_loc_list_ref loc;
17552 die_node com_die_arg;
17554 var_die = lookup_decl_die (decl_or_origin);
17555 if (var_die)
17557 if (get_AT (var_die, DW_AT_location) == NULL)
17559 loc = loc_list_from_tree (com_decl, off ? 1 : 2);
17560 if (loc)
17562 if (off)
17564 /* Optimize the common case. */
17565 if (single_element_loc_list_p (loc)
17566 && loc->expr->dw_loc_opc == DW_OP_addr
17567 && loc->expr->dw_loc_next == NULL
17568 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
17569 == SYMBOL_REF)
17571 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
17572 loc->expr->dw_loc_oprnd1.v.val_addr
17573 = plus_constant (GET_MODE (x), x , off);
17575 else
17576 loc_list_plus_const (loc, off);
17578 add_AT_location_description (var_die, DW_AT_location, loc);
17579 remove_AT (var_die, DW_AT_declaration);
17582 return;
17585 if (common_block_die_table == NULL)
17586 common_block_die_table
17587 = htab_create_ggc (10, common_block_die_table_hash,
17588 common_block_die_table_eq, NULL);
17590 com_die_arg.decl_id = DECL_UID (com_decl);
17591 com_die_arg.die_parent = context_die;
17592 com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
17593 loc = loc_list_from_tree (com_decl, 2);
17594 if (com_die == NULL)
17596 const char *cnam
17597 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
17598 void **slot;
17600 com_die = new_die (DW_TAG_common_block, context_die, decl);
17601 add_name_and_src_coords_attributes (com_die, com_decl);
17602 if (loc)
17604 add_AT_location_description (com_die, DW_AT_location, loc);
17605 /* Avoid sharing the same loc descriptor between
17606 DW_TAG_common_block and DW_TAG_variable. */
17607 loc = loc_list_from_tree (com_decl, 2);
17609 else if (DECL_EXTERNAL (decl))
17610 add_AT_flag (com_die, DW_AT_declaration, 1);
17611 if (want_pubnames ())
17612 add_pubname_string (cnam, com_die); /* ??? needed? */
17613 com_die->decl_id = DECL_UID (com_decl);
17614 slot = htab_find_slot (common_block_die_table, com_die, INSERT);
17615 *slot = (void *) com_die;
17617 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
17619 add_AT_location_description (com_die, DW_AT_location, loc);
17620 loc = loc_list_from_tree (com_decl, 2);
17621 remove_AT (com_die, DW_AT_declaration);
17623 var_die = new_die (DW_TAG_variable, com_die, decl);
17624 add_name_and_src_coords_attributes (var_die, decl);
17625 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
17626 TREE_THIS_VOLATILE (decl), context_die);
17627 add_AT_flag (var_die, DW_AT_external, 1);
17628 if (loc)
17630 if (off)
17632 /* Optimize the common case. */
17633 if (single_element_loc_list_p (loc)
17634 && loc->expr->dw_loc_opc == DW_OP_addr
17635 && loc->expr->dw_loc_next == NULL
17636 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
17638 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
17639 loc->expr->dw_loc_oprnd1.v.val_addr
17640 = plus_constant (GET_MODE (x), x, off);
17642 else
17643 loc_list_plus_const (loc, off);
17645 add_AT_location_description (var_die, DW_AT_location, loc);
17647 else if (DECL_EXTERNAL (decl))
17648 add_AT_flag (var_die, DW_AT_declaration, 1);
17649 equate_decl_number_to_die (decl, var_die);
17650 return;
17653 /* If the compiler emitted a definition for the DECL declaration
17654 and if we already emitted a DIE for it, don't emit a second
17655 DIE for it again. Allow re-declarations of DECLs that are
17656 inside functions, though. */
17657 if (old_die && declaration && !local_scope_p (context_die))
17658 return;
17660 /* For static data members, the declaration in the class is supposed
17661 to have DW_TAG_member tag; the specification should still be
17662 DW_TAG_variable referencing the DW_TAG_member DIE. */
17663 if (declaration && class_scope_p (context_die))
17664 var_die = new_die (DW_TAG_member, context_die, decl);
17665 else
17666 var_die = new_die (DW_TAG_variable, context_die, decl);
17668 origin_die = NULL;
17669 if (origin != NULL)
17670 origin_die = add_abstract_origin_attribute (var_die, origin);
17672 /* Loop unrolling can create multiple blocks that refer to the same
17673 static variable, so we must test for the DW_AT_declaration flag.
17675 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
17676 copy decls and set the DECL_ABSTRACT flag on them instead of
17677 sharing them.
17679 ??? Duplicated blocks have been rewritten to use .debug_ranges.
17681 ??? The declare_in_namespace support causes us to get two DIEs for one
17682 variable, both of which are declarations. We want to avoid considering
17683 one to be a specification, so we must test that this DIE is not a
17684 declaration. */
17685 else if (old_die && TREE_STATIC (decl) && ! declaration
17686 && get_AT_flag (old_die, DW_AT_declaration) == 1)
17688 /* This is a definition of a C++ class level static. */
17689 add_AT_specification (var_die, old_die);
17690 specialization_p = true;
17691 if (DECL_NAME (decl))
17693 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17694 struct dwarf_file_data * file_index = lookup_filename (s.file);
17696 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
17697 add_AT_file (var_die, DW_AT_decl_file, file_index);
17699 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
17700 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
17702 if (old_die->die_tag == DW_TAG_member)
17703 add_linkage_name (var_die, decl);
17706 else
17707 add_name_and_src_coords_attributes (var_die, decl);
17709 if ((origin == NULL && !specialization_p)
17710 || (origin != NULL
17711 && !DECL_ABSTRACT (decl_or_origin)
17712 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
17713 decl_function_context
17714 (decl_or_origin))))
17716 tree type = TREE_TYPE (decl_or_origin);
17718 if (decl_by_reference_p (decl_or_origin))
17719 add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
17720 else
17721 add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
17722 TREE_THIS_VOLATILE (decl_or_origin), context_die);
17725 if (origin == NULL && !specialization_p)
17727 if (TREE_PUBLIC (decl))
17728 add_AT_flag (var_die, DW_AT_external, 1);
17730 if (DECL_ARTIFICIAL (decl))
17731 add_AT_flag (var_die, DW_AT_artificial, 1);
17733 add_accessibility_attribute (var_die, decl);
17736 if (declaration)
17737 add_AT_flag (var_die, DW_AT_declaration, 1);
17739 if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
17740 equate_decl_number_to_die (decl, var_die);
17742 if (! declaration
17743 && (! DECL_ABSTRACT (decl_or_origin)
17744 /* Local static vars are shared between all clones/inlines,
17745 so emit DW_AT_location on the abstract DIE if DECL_RTL is
17746 already set. */
17747 || (TREE_CODE (decl_or_origin) == VAR_DECL
17748 && TREE_STATIC (decl_or_origin)
17749 && DECL_RTL_SET_P (decl_or_origin)))
17750 /* When abstract origin already has DW_AT_location attribute, no need
17751 to add it again. */
17752 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
17754 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
17755 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
17756 defer_location (decl_or_origin, var_die);
17757 else
17758 add_location_or_const_value_attribute (var_die, decl_or_origin,
17759 decl == NULL, DW_AT_location);
17760 add_pubname (decl_or_origin, var_die);
17762 else
17763 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
17766 /* Generate a DIE to represent a named constant. */
17768 static void
17769 gen_const_die (tree decl, dw_die_ref context_die)
17771 dw_die_ref const_die;
17772 tree type = TREE_TYPE (decl);
17774 const_die = new_die (DW_TAG_constant, context_die, decl);
17775 add_name_and_src_coords_attributes (const_die, decl);
17776 add_type_attribute (const_die, type, 1, 0, context_die);
17777 if (TREE_PUBLIC (decl))
17778 add_AT_flag (const_die, DW_AT_external, 1);
17779 if (DECL_ARTIFICIAL (decl))
17780 add_AT_flag (const_die, DW_AT_artificial, 1);
17781 tree_add_const_value_attribute_for_decl (const_die, decl);
17784 /* Generate a DIE to represent a label identifier. */
17786 static void
17787 gen_label_die (tree decl, dw_die_ref context_die)
17789 tree origin = decl_ultimate_origin (decl);
17790 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
17791 rtx insn;
17792 char label[MAX_ARTIFICIAL_LABEL_BYTES];
17794 if (origin != NULL)
17795 add_abstract_origin_attribute (lbl_die, origin);
17796 else
17797 add_name_and_src_coords_attributes (lbl_die, decl);
17799 if (DECL_ABSTRACT (decl))
17800 equate_decl_number_to_die (decl, lbl_die);
17801 else
17803 insn = DECL_RTL_IF_SET (decl);
17805 /* Deleted labels are programmer specified labels which have been
17806 eliminated because of various optimizations. We still emit them
17807 here so that it is possible to put breakpoints on them. */
17808 if (insn
17809 && (LABEL_P (insn)
17810 || ((NOTE_P (insn)
17811 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
17813 /* When optimization is enabled (via -O) some parts of the compiler
17814 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
17815 represent source-level labels which were explicitly declared by
17816 the user. This really shouldn't be happening though, so catch
17817 it if it ever does happen. */
17818 gcc_assert (!INSN_DELETED_P (insn));
17820 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
17821 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
17823 else if (insn
17824 && NOTE_P (insn)
17825 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
17826 && CODE_LABEL_NUMBER (insn) != -1)
17828 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
17829 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
17834 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
17835 attributes to the DIE for a block STMT, to describe where the inlined
17836 function was called from. This is similar to add_src_coords_attributes. */
17838 static inline void
17839 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
17841 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
17843 if (dwarf_version >= 3 || !dwarf_strict)
17845 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
17846 add_AT_unsigned (die, DW_AT_call_line, s.line);
17851 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
17852 Add low_pc and high_pc attributes to the DIE for a block STMT. */
17854 static inline void
17855 add_high_low_attributes (tree stmt, dw_die_ref die)
17857 char label[MAX_ARTIFICIAL_LABEL_BYTES];
17859 if (BLOCK_FRAGMENT_CHAIN (stmt)
17860 && (dwarf_version >= 3 || !dwarf_strict))
17862 tree chain, superblock = NULL_TREE;
17863 dw_die_ref pdie;
17864 dw_attr_ref attr = NULL;
17866 if (inlined_function_outer_scope_p (stmt))
17868 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
17869 BLOCK_NUMBER (stmt));
17870 add_AT_lbl_id (die, DW_AT_entry_pc, label);
17873 /* Optimize duplicate .debug_ranges lists or even tails of
17874 lists. If this BLOCK has same ranges as its supercontext,
17875 lookup DW_AT_ranges attribute in the supercontext (and
17876 recursively so), verify that the ranges_table contains the
17877 right values and use it instead of adding a new .debug_range. */
17878 for (chain = stmt, pdie = die;
17879 BLOCK_SAME_RANGE (chain);
17880 chain = BLOCK_SUPERCONTEXT (chain))
17882 dw_attr_ref new_attr;
17884 pdie = pdie->die_parent;
17885 if (pdie == NULL)
17886 break;
17887 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
17888 break;
17889 new_attr = get_AT (pdie, DW_AT_ranges);
17890 if (new_attr == NULL
17891 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
17892 break;
17893 attr = new_attr;
17894 superblock = BLOCK_SUPERCONTEXT (chain);
17896 if (attr != NULL
17897 && (ranges_table[attr->dw_attr_val.v.val_offset
17898 / 2 / DWARF2_ADDR_SIZE].num
17899 == BLOCK_NUMBER (superblock))
17900 && BLOCK_FRAGMENT_CHAIN (superblock))
17902 unsigned long off = attr->dw_attr_val.v.val_offset
17903 / 2 / DWARF2_ADDR_SIZE;
17904 unsigned long supercnt = 0, thiscnt = 0;
17905 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
17906 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
17908 ++supercnt;
17909 gcc_checking_assert (ranges_table[off + supercnt].num
17910 == BLOCK_NUMBER (chain));
17912 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
17913 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
17914 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
17915 ++thiscnt;
17916 gcc_assert (supercnt >= thiscnt);
17917 add_AT_range_list (die, DW_AT_ranges,
17918 (off + supercnt - thiscnt)
17919 * 2 * DWARF2_ADDR_SIZE);
17920 return;
17923 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
17925 chain = BLOCK_FRAGMENT_CHAIN (stmt);
17928 add_ranges (chain);
17929 chain = BLOCK_FRAGMENT_CHAIN (chain);
17931 while (chain);
17932 add_ranges (NULL);
17934 else
17936 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
17937 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
17938 BLOCK_NUMBER (stmt));
17939 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
17940 BLOCK_NUMBER (stmt));
17941 add_AT_low_high_pc (die, label, label_high);
17945 /* Generate a DIE for a lexical block. */
17947 static void
17948 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
17950 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
17952 if (call_arg_locations)
17954 if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
17955 VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
17956 BLOCK_NUMBER (stmt) + 1);
17957 VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), stmt_die);
17960 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
17961 add_high_low_attributes (stmt, stmt_die);
17963 decls_for_scope (stmt, stmt_die, depth);
17966 /* Generate a DIE for an inlined subprogram. */
17968 static void
17969 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
17971 tree decl;
17973 /* The instance of function that is effectively being inlined shall not
17974 be abstract. */
17975 gcc_assert (! BLOCK_ABSTRACT (stmt));
17977 decl = block_ultimate_origin (stmt);
17979 /* Emit info for the abstract instance first, if we haven't yet. We
17980 must emit this even if the block is abstract, otherwise when we
17981 emit the block below (or elsewhere), we may end up trying to emit
17982 a die whose origin die hasn't been emitted, and crashing. */
17983 dwarf2out_abstract_function (decl);
17985 if (! BLOCK_ABSTRACT (stmt))
17987 dw_die_ref subr_die
17988 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
17990 if (call_arg_locations)
17992 if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
17993 VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
17994 BLOCK_NUMBER (stmt) + 1);
17995 VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), subr_die);
17997 add_abstract_origin_attribute (subr_die, decl);
17998 if (TREE_ASM_WRITTEN (stmt))
17999 add_high_low_attributes (stmt, subr_die);
18000 add_call_src_coords_attributes (stmt, subr_die);
18002 decls_for_scope (stmt, subr_die, depth);
18003 current_function_has_inlines = 1;
18007 /* Generate a DIE for a field in a record, or structure. */
18009 static void
18010 gen_field_die (tree decl, dw_die_ref context_die)
18012 dw_die_ref decl_die;
18014 if (TREE_TYPE (decl) == error_mark_node)
18015 return;
18017 decl_die = new_die (DW_TAG_member, context_die, decl);
18018 add_name_and_src_coords_attributes (decl_die, decl);
18019 add_type_attribute (decl_die, member_declared_type (decl),
18020 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
18021 context_die);
18023 if (DECL_BIT_FIELD_TYPE (decl))
18025 add_byte_size_attribute (decl_die, decl);
18026 add_bit_size_attribute (decl_die, decl);
18027 add_bit_offset_attribute (decl_die, decl);
18030 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
18031 add_data_member_location_attribute (decl_die, decl);
18033 if (DECL_ARTIFICIAL (decl))
18034 add_AT_flag (decl_die, DW_AT_artificial, 1);
18036 add_accessibility_attribute (decl_die, decl);
18038 /* Equate decl number to die, so that we can look up this decl later on. */
18039 equate_decl_number_to_die (decl, decl_die);
18042 #if 0
18043 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18044 Use modified_type_die instead.
18045 We keep this code here just in case these types of DIEs may be needed to
18046 represent certain things in other languages (e.g. Pascal) someday. */
18048 static void
18049 gen_pointer_type_die (tree type, dw_die_ref context_die)
18051 dw_die_ref ptr_die
18052 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
18054 equate_type_number_to_die (type, ptr_die);
18055 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18056 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18059 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18060 Use modified_type_die instead.
18061 We keep this code here just in case these types of DIEs may be needed to
18062 represent certain things in other languages (e.g. Pascal) someday. */
18064 static void
18065 gen_reference_type_die (tree type, dw_die_ref context_die)
18067 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
18069 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
18070 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
18071 else
18072 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
18074 equate_type_number_to_die (type, ref_die);
18075 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
18076 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18078 #endif
18080 /* Generate a DIE for a pointer to a member type. */
18082 static void
18083 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
18085 dw_die_ref ptr_die
18086 = new_die (DW_TAG_ptr_to_member_type,
18087 scope_die_for (type, context_die), type);
18089 equate_type_number_to_die (type, ptr_die);
18090 add_AT_die_ref (ptr_die, DW_AT_containing_type,
18091 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
18092 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18095 typedef const char *dchar_p; /* For DEF_VEC_P. */
18096 DEF_VEC_P(dchar_p);
18097 DEF_VEC_ALLOC_P(dchar_p,heap);
18099 static char *producer_string;
18101 /* Return a heap allocated producer string including command line options
18102 if -grecord-gcc-switches. */
18104 static char *
18105 gen_producer_string (void)
18107 size_t j;
18108 VEC(dchar_p, heap) *switches = NULL;
18109 const char *language_string = lang_hooks.name;
18110 char *producer, *tail;
18111 const char *p;
18112 size_t len = dwarf_record_gcc_switches ? 0 : 3;
18113 size_t plen = strlen (language_string) + 1 + strlen (version_string);
18115 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
18116 switch (save_decoded_options[j].opt_index)
18118 case OPT_o:
18119 case OPT_d:
18120 case OPT_dumpbase:
18121 case OPT_dumpdir:
18122 case OPT_auxbase:
18123 case OPT_auxbase_strip:
18124 case OPT_quiet:
18125 case OPT_version:
18126 case OPT_v:
18127 case OPT_w:
18128 case OPT_L:
18129 case OPT_D:
18130 case OPT_I:
18131 case OPT_U:
18132 case OPT_SPECIAL_unknown:
18133 case OPT_SPECIAL_ignore:
18134 case OPT_SPECIAL_program_name:
18135 case OPT_SPECIAL_input_file:
18136 case OPT_grecord_gcc_switches:
18137 case OPT_gno_record_gcc_switches:
18138 case OPT__output_pch_:
18139 case OPT_fdiagnostics_show_location_:
18140 case OPT_fdiagnostics_show_option:
18141 case OPT_fdiagnostics_show_caret:
18142 case OPT_fverbose_asm:
18143 case OPT____:
18144 case OPT__sysroot_:
18145 case OPT_nostdinc:
18146 case OPT_nostdinc__:
18147 /* Ignore these. */
18148 continue;
18149 default:
18150 if (cl_options[save_decoded_options[j].opt_index].flags
18151 & CL_NO_DWARF_RECORD)
18152 continue;
18153 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
18154 == '-');
18155 switch (save_decoded_options[j].canonical_option[0][1])
18157 case 'M':
18158 case 'i':
18159 case 'W':
18160 continue;
18161 case 'f':
18162 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
18163 "dump", 4) == 0)
18164 continue;
18165 break;
18166 default:
18167 break;
18169 VEC_safe_push (dchar_p, heap, switches,
18170 save_decoded_options[j].orig_option_with_args_text);
18171 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
18172 break;
18175 producer = XNEWVEC (char, plen + 1 + len + 1);
18176 tail = producer;
18177 sprintf (tail, "%s %s", language_string, version_string);
18178 tail += plen;
18180 FOR_EACH_VEC_ELT (dchar_p, switches, j, p)
18182 len = strlen (p);
18183 *tail = ' ';
18184 memcpy (tail + 1, p, len);
18185 tail += len + 1;
18188 *tail = '\0';
18189 VEC_free (dchar_p, heap, switches);
18190 return producer;
18193 /* Generate the DIE for the compilation unit. */
18195 static dw_die_ref
18196 gen_compile_unit_die (const char *filename)
18198 dw_die_ref die;
18199 const char *language_string = lang_hooks.name;
18200 int language;
18202 die = new_die (DW_TAG_compile_unit, NULL, NULL);
18204 if (filename)
18206 add_name_attribute (die, filename);
18207 /* Don't add cwd for <built-in>. */
18208 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
18209 add_comp_dir_attribute (die);
18212 if (producer_string == NULL)
18213 producer_string = gen_producer_string ();
18214 add_AT_string (die, DW_AT_producer, producer_string);
18216 /* If our producer is LTO try to figure out a common language to use
18217 from the global list of translation units. */
18218 if (strcmp (language_string, "GNU GIMPLE") == 0)
18220 unsigned i;
18221 tree t;
18222 const char *common_lang = NULL;
18224 FOR_EACH_VEC_ELT (tree, all_translation_units, i, t)
18226 if (!TRANSLATION_UNIT_LANGUAGE (t))
18227 continue;
18228 if (!common_lang)
18229 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
18230 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
18232 else if (strncmp (common_lang, "GNU C", 5) == 0
18233 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
18234 /* Mixing C and C++ is ok, use C++ in that case. */
18235 common_lang = "GNU C++";
18236 else
18238 /* Fall back to C. */
18239 common_lang = NULL;
18240 break;
18244 if (common_lang)
18245 language_string = common_lang;
18248 language = DW_LANG_C89;
18249 if (strcmp (language_string, "GNU C++") == 0)
18250 language = DW_LANG_C_plus_plus;
18251 else if (strcmp (language_string, "GNU F77") == 0)
18252 language = DW_LANG_Fortran77;
18253 else if (strcmp (language_string, "GNU Pascal") == 0)
18254 language = DW_LANG_Pascal83;
18255 else if (dwarf_version >= 3 || !dwarf_strict)
18257 if (strcmp (language_string, "GNU Ada") == 0)
18258 language = DW_LANG_Ada95;
18259 else if (strcmp (language_string, "GNU Fortran") == 0)
18260 language = DW_LANG_Fortran95;
18261 else if (strcmp (language_string, "GNU Java") == 0)
18262 language = DW_LANG_Java;
18263 else if (strcmp (language_string, "GNU Objective-C") == 0)
18264 language = DW_LANG_ObjC;
18265 else if (strcmp (language_string, "GNU Objective-C++") == 0)
18266 language = DW_LANG_ObjC_plus_plus;
18267 else if (dwarf_version >= 5 || !dwarf_strict)
18269 if (strcmp (language_string, "GNU Go") == 0)
18270 language = DW_LANG_Go;
18273 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
18274 else if (strcmp (language_string, "GNU Fortran") == 0)
18275 language = DW_LANG_Fortran90;
18277 add_AT_unsigned (die, DW_AT_language, language);
18279 switch (language)
18281 case DW_LANG_Fortran77:
18282 case DW_LANG_Fortran90:
18283 case DW_LANG_Fortran95:
18284 /* Fortran has case insensitive identifiers and the front-end
18285 lowercases everything. */
18286 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
18287 break;
18288 default:
18289 /* The default DW_ID_case_sensitive doesn't need to be specified. */
18290 break;
18292 return die;
18295 /* Generate the DIE for a base class. */
18297 static void
18298 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
18300 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
18302 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
18303 add_data_member_location_attribute (die, binfo);
18305 if (BINFO_VIRTUAL_P (binfo))
18306 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
18308 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
18309 children, otherwise the default is DW_ACCESS_public. In DWARF2
18310 the default has always been DW_ACCESS_private. */
18311 if (access == access_public_node)
18313 if (dwarf_version == 2
18314 || context_die->die_tag == DW_TAG_class_type)
18315 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
18317 else if (access == access_protected_node)
18318 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
18319 else if (dwarf_version > 2
18320 && context_die->die_tag != DW_TAG_class_type)
18321 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
18324 /* Generate a DIE for a class member. */
18326 static void
18327 gen_member_die (tree type, dw_die_ref context_die)
18329 tree member;
18330 tree binfo = TYPE_BINFO (type);
18331 dw_die_ref child;
18333 /* If this is not an incomplete type, output descriptions of each of its
18334 members. Note that as we output the DIEs necessary to represent the
18335 members of this record or union type, we will also be trying to output
18336 DIEs to represent the *types* of those members. However the `type'
18337 function (above) will specifically avoid generating type DIEs for member
18338 types *within* the list of member DIEs for this (containing) type except
18339 for those types (of members) which are explicitly marked as also being
18340 members of this (containing) type themselves. The g++ front- end can
18341 force any given type to be treated as a member of some other (containing)
18342 type by setting the TYPE_CONTEXT of the given (member) type to point to
18343 the TREE node representing the appropriate (containing) type. */
18345 /* First output info about the base classes. */
18346 if (binfo)
18348 VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
18349 int i;
18350 tree base;
18352 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
18353 gen_inheritance_die (base,
18354 (accesses ? VEC_index (tree, accesses, i)
18355 : access_public_node), context_die);
18358 /* Now output info about the data members and type members. */
18359 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
18361 /* If we thought we were generating minimal debug info for TYPE
18362 and then changed our minds, some of the member declarations
18363 may have already been defined. Don't define them again, but
18364 do put them in the right order. */
18366 child = lookup_decl_die (member);
18367 if (child)
18368 splice_child_die (context_die, child);
18369 else
18370 gen_decl_die (member, NULL, context_die);
18373 /* Now output info about the function members (if any). */
18374 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
18376 /* Don't include clones in the member list. */
18377 if (DECL_ABSTRACT_ORIGIN (member))
18378 continue;
18380 child = lookup_decl_die (member);
18381 if (child)
18382 splice_child_die (context_die, child);
18383 else
18384 gen_decl_die (member, NULL, context_die);
18388 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
18389 is set, we pretend that the type was never defined, so we only get the
18390 member DIEs needed by later specification DIEs. */
18392 static void
18393 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
18394 enum debug_info_usage usage)
18396 dw_die_ref type_die = lookup_type_die (type);
18397 dw_die_ref scope_die = 0;
18398 int nested = 0;
18399 int complete = (TYPE_SIZE (type)
18400 && (! TYPE_STUB_DECL (type)
18401 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
18402 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
18403 complete = complete && should_emit_struct_debug (type, usage);
18405 if (type_die && ! complete)
18406 return;
18408 if (TYPE_CONTEXT (type) != NULL_TREE
18409 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
18410 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
18411 nested = 1;
18413 scope_die = scope_die_for (type, context_die);
18415 if (! type_die || (nested && is_cu_die (scope_die)))
18416 /* First occurrence of type or toplevel definition of nested class. */
18418 dw_die_ref old_die = type_die;
18420 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
18421 ? record_type_tag (type) : DW_TAG_union_type,
18422 scope_die, type);
18423 equate_type_number_to_die (type, type_die);
18424 if (old_die)
18425 add_AT_specification (type_die, old_die);
18426 else
18427 add_name_attribute (type_die, type_tag (type));
18429 else
18430 remove_AT (type_die, DW_AT_declaration);
18432 /* Generate child dies for template paramaters. */
18433 if (debug_info_level > DINFO_LEVEL_TERSE
18434 && COMPLETE_TYPE_P (type))
18435 schedule_generic_params_dies_gen (type);
18437 /* If this type has been completed, then give it a byte_size attribute and
18438 then give a list of members. */
18439 if (complete && !ns_decl)
18441 /* Prevent infinite recursion in cases where the type of some member of
18442 this type is expressed in terms of this type itself. */
18443 TREE_ASM_WRITTEN (type) = 1;
18444 add_byte_size_attribute (type_die, type);
18445 if (TYPE_STUB_DECL (type) != NULL_TREE)
18447 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
18448 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
18451 /* If the first reference to this type was as the return type of an
18452 inline function, then it may not have a parent. Fix this now. */
18453 if (type_die->die_parent == NULL)
18454 add_child_die (scope_die, type_die);
18456 push_decl_scope (type);
18457 gen_member_die (type, type_die);
18458 pop_decl_scope ();
18460 add_gnat_descriptive_type_attribute (type_die, type, context_die);
18461 if (TYPE_ARTIFICIAL (type))
18462 add_AT_flag (type_die, DW_AT_artificial, 1);
18464 /* GNU extension: Record what type our vtable lives in. */
18465 if (TYPE_VFIELD (type))
18467 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
18469 gen_type_die (vtype, context_die);
18470 add_AT_die_ref (type_die, DW_AT_containing_type,
18471 lookup_type_die (vtype));
18474 else
18476 add_AT_flag (type_die, DW_AT_declaration, 1);
18478 /* We don't need to do this for function-local types. */
18479 if (TYPE_STUB_DECL (type)
18480 && ! decl_function_context (TYPE_STUB_DECL (type)))
18481 VEC_safe_push (tree, gc, incomplete_types, type);
18484 if (get_AT (type_die, DW_AT_name))
18485 add_pubtype (type, type_die);
18488 /* Generate a DIE for a subroutine _type_. */
18490 static void
18491 gen_subroutine_type_die (tree type, dw_die_ref context_die)
18493 tree return_type = TREE_TYPE (type);
18494 dw_die_ref subr_die
18495 = new_die (DW_TAG_subroutine_type,
18496 scope_die_for (type, context_die), type);
18498 equate_type_number_to_die (type, subr_die);
18499 add_prototyped_attribute (subr_die, type);
18500 add_type_attribute (subr_die, return_type, 0, 0, context_die);
18501 gen_formal_types_die (type, subr_die);
18503 if (get_AT (subr_die, DW_AT_name))
18504 add_pubtype (type, subr_die);
18507 /* Generate a DIE for a type definition. */
18509 static void
18510 gen_typedef_die (tree decl, dw_die_ref context_die)
18512 dw_die_ref type_die;
18513 tree origin;
18515 if (TREE_ASM_WRITTEN (decl))
18516 return;
18518 TREE_ASM_WRITTEN (decl) = 1;
18519 type_die = new_die (DW_TAG_typedef, context_die, decl);
18520 origin = decl_ultimate_origin (decl);
18521 if (origin != NULL)
18522 add_abstract_origin_attribute (type_die, origin);
18523 else
18525 tree type;
18527 add_name_and_src_coords_attributes (type_die, decl);
18528 if (DECL_ORIGINAL_TYPE (decl))
18530 type = DECL_ORIGINAL_TYPE (decl);
18532 gcc_assert (type != TREE_TYPE (decl));
18533 equate_type_number_to_die (TREE_TYPE (decl), type_die);
18535 else
18537 type = TREE_TYPE (decl);
18539 if (is_naming_typedef_decl (TYPE_NAME (type)))
18541 /* Here, we are in the case of decl being a typedef naming
18542 an anonymous type, e.g:
18543 typedef struct {...} foo;
18544 In that case TREE_TYPE (decl) is not a typedef variant
18545 type and TYPE_NAME of the anonymous type is set to the
18546 TYPE_DECL of the typedef. This construct is emitted by
18547 the C++ FE.
18549 TYPE is the anonymous struct named by the typedef
18550 DECL. As we need the DW_AT_type attribute of the
18551 DW_TAG_typedef to point to the DIE of TYPE, let's
18552 generate that DIE right away. add_type_attribute
18553 called below will then pick (via lookup_type_die) that
18554 anonymous struct DIE. */
18555 if (!TREE_ASM_WRITTEN (type))
18556 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
18558 /* This is a GNU Extension. We are adding a
18559 DW_AT_linkage_name attribute to the DIE of the
18560 anonymous struct TYPE. The value of that attribute
18561 is the name of the typedef decl naming the anonymous
18562 struct. This greatly eases the work of consumers of
18563 this debug info. */
18564 add_linkage_attr (lookup_type_die (type), decl);
18568 add_type_attribute (type_die, type, TREE_READONLY (decl),
18569 TREE_THIS_VOLATILE (decl), context_die);
18571 if (is_naming_typedef_decl (decl))
18572 /* We want that all subsequent calls to lookup_type_die with
18573 TYPE in argument yield the DW_TAG_typedef we have just
18574 created. */
18575 equate_type_number_to_die (type, type_die);
18577 add_accessibility_attribute (type_die, decl);
18580 if (DECL_ABSTRACT (decl))
18581 equate_decl_number_to_die (decl, type_die);
18583 if (get_AT (type_die, DW_AT_name))
18584 add_pubtype (decl, type_die);
18587 /* Generate a DIE for a struct, class, enum or union type. */
18589 static void
18590 gen_tagged_type_die (tree type,
18591 dw_die_ref context_die,
18592 enum debug_info_usage usage)
18594 int need_pop;
18596 if (type == NULL_TREE
18597 || !is_tagged_type (type))
18598 return;
18600 /* If this is a nested type whose containing class hasn't been written
18601 out yet, writing it out will cover this one, too. This does not apply
18602 to instantiations of member class templates; they need to be added to
18603 the containing class as they are generated. FIXME: This hurts the
18604 idea of combining type decls from multiple TUs, since we can't predict
18605 what set of template instantiations we'll get. */
18606 if (TYPE_CONTEXT (type)
18607 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
18608 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
18610 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
18612 if (TREE_ASM_WRITTEN (type))
18613 return;
18615 /* If that failed, attach ourselves to the stub. */
18616 push_decl_scope (TYPE_CONTEXT (type));
18617 context_die = lookup_type_die (TYPE_CONTEXT (type));
18618 need_pop = 1;
18620 else if (TYPE_CONTEXT (type) != NULL_TREE
18621 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
18623 /* If this type is local to a function that hasn't been written
18624 out yet, use a NULL context for now; it will be fixed up in
18625 decls_for_scope. */
18626 context_die = lookup_decl_die (TYPE_CONTEXT (type));
18627 /* A declaration DIE doesn't count; nested types need to go in the
18628 specification. */
18629 if (context_die && is_declaration_die (context_die))
18630 context_die = NULL;
18631 need_pop = 0;
18633 else
18635 context_die = declare_in_namespace (type, context_die);
18636 need_pop = 0;
18639 if (TREE_CODE (type) == ENUMERAL_TYPE)
18641 /* This might have been written out by the call to
18642 declare_in_namespace. */
18643 if (!TREE_ASM_WRITTEN (type))
18644 gen_enumeration_type_die (type, context_die);
18646 else
18647 gen_struct_or_union_type_die (type, context_die, usage);
18649 if (need_pop)
18650 pop_decl_scope ();
18652 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
18653 it up if it is ever completed. gen_*_type_die will set it for us
18654 when appropriate. */
18657 /* Generate a type description DIE. */
18659 static void
18660 gen_type_die_with_usage (tree type, dw_die_ref context_die,
18661 enum debug_info_usage usage)
18663 struct array_descr_info info;
18665 if (type == NULL_TREE || type == error_mark_node)
18666 return;
18668 if (TYPE_NAME (type) != NULL_TREE
18669 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
18670 && is_redundant_typedef (TYPE_NAME (type))
18671 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
18672 /* The DECL of this type is a typedef we don't want to emit debug
18673 info for but we want debug info for its underlying typedef.
18674 This can happen for e.g, the injected-class-name of a C++
18675 type. */
18676 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
18678 /* If TYPE is a typedef type variant, let's generate debug info
18679 for the parent typedef which TYPE is a type of. */
18680 if (typedef_variant_p (type))
18682 if (TREE_ASM_WRITTEN (type))
18683 return;
18685 /* Prevent broken recursion; we can't hand off to the same type. */
18686 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
18688 /* Give typedefs the right scope. */
18689 context_die = scope_die_for (type, context_die);
18691 TREE_ASM_WRITTEN (type) = 1;
18693 gen_decl_die (TYPE_NAME (type), NULL, context_die);
18694 return;
18697 /* If type is an anonymous tagged type named by a typedef, let's
18698 generate debug info for the typedef. */
18699 if (is_naming_typedef_decl (TYPE_NAME (type)))
18701 /* Use the DIE of the containing namespace as the parent DIE of
18702 the type description DIE we want to generate. */
18703 if (DECL_CONTEXT (TYPE_NAME (type))
18704 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
18705 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
18707 gen_decl_die (TYPE_NAME (type), NULL, context_die);
18708 return;
18711 /* If this is an array type with hidden descriptor, handle it first. */
18712 if (!TREE_ASM_WRITTEN (type)
18713 && lang_hooks.types.get_array_descr_info
18714 && lang_hooks.types.get_array_descr_info (type, &info)
18715 && (dwarf_version >= 3 || !dwarf_strict))
18717 gen_descr_array_type_die (type, &info, context_die);
18718 TREE_ASM_WRITTEN (type) = 1;
18719 return;
18722 /* We are going to output a DIE to represent the unqualified version
18723 of this type (i.e. without any const or volatile qualifiers) so
18724 get the main variant (i.e. the unqualified version) of this type
18725 now. (Vectors are special because the debugging info is in the
18726 cloned type itself). */
18727 if (TREE_CODE (type) != VECTOR_TYPE)
18728 type = type_main_variant (type);
18730 if (TREE_ASM_WRITTEN (type))
18731 return;
18733 switch (TREE_CODE (type))
18735 case ERROR_MARK:
18736 break;
18738 case POINTER_TYPE:
18739 case REFERENCE_TYPE:
18740 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
18741 ensures that the gen_type_die recursion will terminate even if the
18742 type is recursive. Recursive types are possible in Ada. */
18743 /* ??? We could perhaps do this for all types before the switch
18744 statement. */
18745 TREE_ASM_WRITTEN (type) = 1;
18747 /* For these types, all that is required is that we output a DIE (or a
18748 set of DIEs) to represent the "basis" type. */
18749 gen_type_die_with_usage (TREE_TYPE (type), context_die,
18750 DINFO_USAGE_IND_USE);
18751 break;
18753 case OFFSET_TYPE:
18754 /* This code is used for C++ pointer-to-data-member types.
18755 Output a description of the relevant class type. */
18756 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
18757 DINFO_USAGE_IND_USE);
18759 /* Output a description of the type of the object pointed to. */
18760 gen_type_die_with_usage (TREE_TYPE (type), context_die,
18761 DINFO_USAGE_IND_USE);
18763 /* Now output a DIE to represent this pointer-to-data-member type
18764 itself. */
18765 gen_ptr_to_mbr_type_die (type, context_die);
18766 break;
18768 case FUNCTION_TYPE:
18769 /* Force out return type (in case it wasn't forced out already). */
18770 gen_type_die_with_usage (TREE_TYPE (type), context_die,
18771 DINFO_USAGE_DIR_USE);
18772 gen_subroutine_type_die (type, context_die);
18773 break;
18775 case METHOD_TYPE:
18776 /* Force out return type (in case it wasn't forced out already). */
18777 gen_type_die_with_usage (TREE_TYPE (type), context_die,
18778 DINFO_USAGE_DIR_USE);
18779 gen_subroutine_type_die (type, context_die);
18780 break;
18782 case ARRAY_TYPE:
18783 gen_array_type_die (type, context_die);
18784 break;
18786 case VECTOR_TYPE:
18787 gen_array_type_die (type, context_die);
18788 break;
18790 case ENUMERAL_TYPE:
18791 case RECORD_TYPE:
18792 case UNION_TYPE:
18793 case QUAL_UNION_TYPE:
18794 gen_tagged_type_die (type, context_die, usage);
18795 return;
18797 case VOID_TYPE:
18798 case INTEGER_TYPE:
18799 case REAL_TYPE:
18800 case FIXED_POINT_TYPE:
18801 case COMPLEX_TYPE:
18802 case BOOLEAN_TYPE:
18803 /* No DIEs needed for fundamental types. */
18804 break;
18806 case NULLPTR_TYPE:
18807 case LANG_TYPE:
18808 /* Just use DW_TAG_unspecified_type. */
18810 dw_die_ref type_die = lookup_type_die (type);
18811 if (type_die == NULL)
18813 tree name = TYPE_NAME (type);
18814 if (TREE_CODE (name) == TYPE_DECL)
18815 name = DECL_NAME (name);
18816 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
18817 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
18818 equate_type_number_to_die (type, type_die);
18821 break;
18823 default:
18824 gcc_unreachable ();
18827 TREE_ASM_WRITTEN (type) = 1;
18830 static void
18831 gen_type_die (tree type, dw_die_ref context_die)
18833 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
18836 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
18837 things which are local to the given block. */
18839 static void
18840 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
18842 int must_output_die = 0;
18843 bool inlined_func;
18845 /* Ignore blocks that are NULL. */
18846 if (stmt == NULL_TREE)
18847 return;
18849 inlined_func = inlined_function_outer_scope_p (stmt);
18851 /* If the block is one fragment of a non-contiguous block, do not
18852 process the variables, since they will have been done by the
18853 origin block. Do process subblocks. */
18854 if (BLOCK_FRAGMENT_ORIGIN (stmt))
18856 tree sub;
18858 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
18859 gen_block_die (sub, context_die, depth + 1);
18861 return;
18864 /* Determine if we need to output any Dwarf DIEs at all to represent this
18865 block. */
18866 if (inlined_func)
18867 /* The outer scopes for inlinings *must* always be represented. We
18868 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
18869 must_output_die = 1;
18870 else
18872 /* Determine if this block directly contains any "significant"
18873 local declarations which we will need to output DIEs for. */
18874 if (debug_info_level > DINFO_LEVEL_TERSE)
18875 /* We are not in terse mode so *any* local declaration counts
18876 as being a "significant" one. */
18877 must_output_die = ((BLOCK_VARS (stmt) != NULL
18878 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
18879 && (TREE_USED (stmt)
18880 || TREE_ASM_WRITTEN (stmt)
18881 || BLOCK_ABSTRACT (stmt)));
18882 else if ((TREE_USED (stmt)
18883 || TREE_ASM_WRITTEN (stmt)
18884 || BLOCK_ABSTRACT (stmt))
18885 && !dwarf2out_ignore_block (stmt))
18886 must_output_die = 1;
18889 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
18890 DIE for any block which contains no significant local declarations at
18891 all. Rather, in such cases we just call `decls_for_scope' so that any
18892 needed Dwarf info for any sub-blocks will get properly generated. Note
18893 that in terse mode, our definition of what constitutes a "significant"
18894 local declaration gets restricted to include only inlined function
18895 instances and local (nested) function definitions. */
18896 if (must_output_die)
18898 if (inlined_func)
18900 /* If STMT block is abstract, that means we have been called
18901 indirectly from dwarf2out_abstract_function.
18902 That function rightfully marks the descendent blocks (of
18903 the abstract function it is dealing with) as being abstract,
18904 precisely to prevent us from emitting any
18905 DW_TAG_inlined_subroutine DIE as a descendent
18906 of an abstract function instance. So in that case, we should
18907 not call gen_inlined_subroutine_die.
18909 Later though, when cgraph asks dwarf2out to emit info
18910 for the concrete instance of the function decl into which
18911 the concrete instance of STMT got inlined, the later will lead
18912 to the generation of a DW_TAG_inlined_subroutine DIE. */
18913 if (! BLOCK_ABSTRACT (stmt))
18914 gen_inlined_subroutine_die (stmt, context_die, depth);
18916 else
18917 gen_lexical_block_die (stmt, context_die, depth);
18919 else
18920 decls_for_scope (stmt, context_die, depth);
18923 /* Process variable DECL (or variable with origin ORIGIN) within
18924 block STMT and add it to CONTEXT_DIE. */
18925 static void
18926 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
18928 dw_die_ref die;
18929 tree decl_or_origin = decl ? decl : origin;
18931 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
18932 die = lookup_decl_die (decl_or_origin);
18933 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
18934 && TYPE_DECL_IS_STUB (decl_or_origin))
18935 die = lookup_type_die (TREE_TYPE (decl_or_origin));
18936 else
18937 die = NULL;
18939 if (die != NULL && die->die_parent == NULL)
18940 add_child_die (context_die, die);
18941 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
18942 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
18943 stmt, context_die);
18944 else
18945 gen_decl_die (decl, origin, context_die);
18948 /* Generate all of the decls declared within a given scope and (recursively)
18949 all of its sub-blocks. */
18951 static void
18952 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
18954 tree decl;
18955 unsigned int i;
18956 tree subblocks;
18958 /* Ignore NULL blocks. */
18959 if (stmt == NULL_TREE)
18960 return;
18962 /* Output the DIEs to represent all of the data objects and typedefs
18963 declared directly within this block but not within any nested
18964 sub-blocks. Also, nested function and tag DIEs have been
18965 generated with a parent of NULL; fix that up now. */
18966 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
18967 process_scope_var (stmt, decl, NULL_TREE, context_die);
18968 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
18969 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
18970 context_die);
18972 /* If we're at -g1, we're not interested in subblocks. */
18973 if (debug_info_level <= DINFO_LEVEL_TERSE)
18974 return;
18976 /* Output the DIEs to represent all sub-blocks (and the items declared
18977 therein) of this block. */
18978 for (subblocks = BLOCK_SUBBLOCKS (stmt);
18979 subblocks != NULL;
18980 subblocks = BLOCK_CHAIN (subblocks))
18981 gen_block_die (subblocks, context_die, depth + 1);
18984 /* Is this a typedef we can avoid emitting? */
18986 static inline int
18987 is_redundant_typedef (const_tree decl)
18989 if (TYPE_DECL_IS_STUB (decl))
18990 return 1;
18992 if (DECL_ARTIFICIAL (decl)
18993 && DECL_CONTEXT (decl)
18994 && is_tagged_type (DECL_CONTEXT (decl))
18995 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
18996 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
18997 /* Also ignore the artificial member typedef for the class name. */
18998 return 1;
19000 return 0;
19003 /* Return TRUE if TYPE is a typedef that names a type for linkage
19004 purposes. This kind of typedefs is produced by the C++ FE for
19005 constructs like:
19007 typedef struct {...} foo;
19009 In that case, there is no typedef variant type produced for foo.
19010 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
19011 struct type. */
19013 static bool
19014 is_naming_typedef_decl (const_tree decl)
19016 if (decl == NULL_TREE
19017 || TREE_CODE (decl) != TYPE_DECL
19018 || !is_tagged_type (TREE_TYPE (decl))
19019 || DECL_IS_BUILTIN (decl)
19020 || is_redundant_typedef (decl)
19021 /* It looks like Ada produces TYPE_DECLs that are very similar
19022 to C++ naming typedefs but that have different
19023 semantics. Let's be specific to c++ for now. */
19024 || !is_cxx ())
19025 return FALSE;
19027 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
19028 && TYPE_NAME (TREE_TYPE (decl)) == decl
19029 && (TYPE_STUB_DECL (TREE_TYPE (decl))
19030 != TYPE_NAME (TREE_TYPE (decl))));
19033 /* Returns the DIE for a context. */
19035 static inline dw_die_ref
19036 get_context_die (tree context)
19038 if (context)
19040 /* Find die that represents this context. */
19041 if (TYPE_P (context))
19043 context = TYPE_MAIN_VARIANT (context);
19044 return strip_naming_typedef (context, force_type_die (context));
19046 else
19047 return force_decl_die (context);
19049 return comp_unit_die ();
19052 /* Returns the DIE for decl. A DIE will always be returned. */
19054 static dw_die_ref
19055 force_decl_die (tree decl)
19057 dw_die_ref decl_die;
19058 unsigned saved_external_flag;
19059 tree save_fn = NULL_TREE;
19060 decl_die = lookup_decl_die (decl);
19061 if (!decl_die)
19063 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
19065 decl_die = lookup_decl_die (decl);
19066 if (decl_die)
19067 return decl_die;
19069 switch (TREE_CODE (decl))
19071 case FUNCTION_DECL:
19072 /* Clear current_function_decl, so that gen_subprogram_die thinks
19073 that this is a declaration. At this point, we just want to force
19074 declaration die. */
19075 save_fn = current_function_decl;
19076 current_function_decl = NULL_TREE;
19077 gen_subprogram_die (decl, context_die);
19078 current_function_decl = save_fn;
19079 break;
19081 case VAR_DECL:
19082 /* Set external flag to force declaration die. Restore it after
19083 gen_decl_die() call. */
19084 saved_external_flag = DECL_EXTERNAL (decl);
19085 DECL_EXTERNAL (decl) = 1;
19086 gen_decl_die (decl, NULL, context_die);
19087 DECL_EXTERNAL (decl) = saved_external_flag;
19088 break;
19090 case NAMESPACE_DECL:
19091 if (dwarf_version >= 3 || !dwarf_strict)
19092 dwarf2out_decl (decl);
19093 else
19094 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
19095 decl_die = comp_unit_die ();
19096 break;
19098 case TRANSLATION_UNIT_DECL:
19099 decl_die = comp_unit_die ();
19100 break;
19102 default:
19103 gcc_unreachable ();
19106 /* We should be able to find the DIE now. */
19107 if (!decl_die)
19108 decl_die = lookup_decl_die (decl);
19109 gcc_assert (decl_die);
19112 return decl_die;
19115 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
19116 always returned. */
19118 static dw_die_ref
19119 force_type_die (tree type)
19121 dw_die_ref type_die;
19123 type_die = lookup_type_die (type);
19124 if (!type_die)
19126 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
19128 type_die = modified_type_die (type, TYPE_READONLY (type),
19129 TYPE_VOLATILE (type), context_die);
19130 gcc_assert (type_die);
19132 return type_die;
19135 /* Force out any required namespaces to be able to output DECL,
19136 and return the new context_die for it, if it's changed. */
19138 static dw_die_ref
19139 setup_namespace_context (tree thing, dw_die_ref context_die)
19141 tree context = (DECL_P (thing)
19142 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
19143 if (context && TREE_CODE (context) == NAMESPACE_DECL)
19144 /* Force out the namespace. */
19145 context_die = force_decl_die (context);
19147 return context_die;
19150 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
19151 type) within its namespace, if appropriate.
19153 For compatibility with older debuggers, namespace DIEs only contain
19154 declarations; all definitions are emitted at CU scope. */
19156 static dw_die_ref
19157 declare_in_namespace (tree thing, dw_die_ref context_die)
19159 dw_die_ref ns_context;
19161 if (debug_info_level <= DINFO_LEVEL_TERSE)
19162 return context_die;
19164 /* If this decl is from an inlined function, then don't try to emit it in its
19165 namespace, as we will get confused. It would have already been emitted
19166 when the abstract instance of the inline function was emitted anyways. */
19167 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
19168 return context_die;
19170 ns_context = setup_namespace_context (thing, context_die);
19172 if (ns_context != context_die)
19174 if (is_fortran ())
19175 return ns_context;
19176 if (DECL_P (thing))
19177 gen_decl_die (thing, NULL, ns_context);
19178 else
19179 gen_type_die (thing, ns_context);
19181 return context_die;
19184 /* Generate a DIE for a namespace or namespace alias. */
19186 static void
19187 gen_namespace_die (tree decl, dw_die_ref context_die)
19189 dw_die_ref namespace_die;
19191 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
19192 they are an alias of. */
19193 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
19195 /* Output a real namespace or module. */
19196 context_die = setup_namespace_context (decl, comp_unit_die ());
19197 namespace_die = new_die (is_fortran ()
19198 ? DW_TAG_module : DW_TAG_namespace,
19199 context_die, decl);
19200 /* For Fortran modules defined in different CU don't add src coords. */
19201 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
19203 const char *name = dwarf2_name (decl, 0);
19204 if (name)
19205 add_name_attribute (namespace_die, name);
19207 else
19208 add_name_and_src_coords_attributes (namespace_die, decl);
19209 if (DECL_EXTERNAL (decl))
19210 add_AT_flag (namespace_die, DW_AT_declaration, 1);
19211 equate_decl_number_to_die (decl, namespace_die);
19213 else
19215 /* Output a namespace alias. */
19217 /* Force out the namespace we are an alias of, if necessary. */
19218 dw_die_ref origin_die
19219 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
19221 if (DECL_FILE_SCOPE_P (decl)
19222 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
19223 context_die = setup_namespace_context (decl, comp_unit_die ());
19224 /* Now create the namespace alias DIE. */
19225 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
19226 add_name_and_src_coords_attributes (namespace_die, decl);
19227 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
19228 equate_decl_number_to_die (decl, namespace_die);
19230 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
19231 if (want_pubnames ())
19232 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
19235 /* Generate Dwarf debug information for a decl described by DECL.
19236 The return value is currently only meaningful for PARM_DECLs,
19237 for all other decls it returns NULL. */
19239 static dw_die_ref
19240 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
19242 tree decl_or_origin = decl ? decl : origin;
19243 tree class_origin = NULL, ultimate_origin;
19245 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
19246 return NULL;
19248 switch (TREE_CODE (decl_or_origin))
19250 case ERROR_MARK:
19251 break;
19253 case CONST_DECL:
19254 if (!is_fortran () && !is_ada ())
19256 /* The individual enumerators of an enum type get output when we output
19257 the Dwarf representation of the relevant enum type itself. */
19258 break;
19261 /* Emit its type. */
19262 gen_type_die (TREE_TYPE (decl), context_die);
19264 /* And its containing namespace. */
19265 context_die = declare_in_namespace (decl, context_die);
19267 gen_const_die (decl, context_die);
19268 break;
19270 case FUNCTION_DECL:
19271 /* Don't output any DIEs to represent mere function declarations,
19272 unless they are class members or explicit block externs. */
19273 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
19274 && DECL_FILE_SCOPE_P (decl_or_origin)
19275 && (current_function_decl == NULL_TREE
19276 || DECL_ARTIFICIAL (decl_or_origin)))
19277 break;
19279 #if 0
19280 /* FIXME */
19281 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
19282 on local redeclarations of global functions. That seems broken. */
19283 if (current_function_decl != decl)
19284 /* This is only a declaration. */;
19285 #endif
19287 /* If we're emitting a clone, emit info for the abstract instance. */
19288 if (origin || DECL_ORIGIN (decl) != decl)
19289 dwarf2out_abstract_function (origin
19290 ? DECL_ORIGIN (origin)
19291 : DECL_ABSTRACT_ORIGIN (decl));
19293 /* If we're emitting an out-of-line copy of an inline function,
19294 emit info for the abstract instance and set up to refer to it. */
19295 else if (cgraph_function_possibly_inlined_p (decl)
19296 && ! DECL_ABSTRACT (decl)
19297 && ! class_or_namespace_scope_p (context_die)
19298 /* dwarf2out_abstract_function won't emit a die if this is just
19299 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
19300 that case, because that works only if we have a die. */
19301 && DECL_INITIAL (decl) != NULL_TREE)
19303 dwarf2out_abstract_function (decl);
19304 set_decl_origin_self (decl);
19307 /* Otherwise we're emitting the primary DIE for this decl. */
19308 else if (debug_info_level > DINFO_LEVEL_TERSE)
19310 /* Before we describe the FUNCTION_DECL itself, make sure that we
19311 have its containing type. */
19312 if (!origin)
19313 origin = decl_class_context (decl);
19314 if (origin != NULL_TREE)
19315 gen_type_die (origin, context_die);
19317 /* And its return type. */
19318 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
19320 /* And its virtual context. */
19321 if (DECL_VINDEX (decl) != NULL_TREE)
19322 gen_type_die (DECL_CONTEXT (decl), context_die);
19324 /* Make sure we have a member DIE for decl. */
19325 if (origin != NULL_TREE)
19326 gen_type_die_for_member (origin, decl, context_die);
19328 /* And its containing namespace. */
19329 context_die = declare_in_namespace (decl, context_die);
19332 /* Now output a DIE to represent the function itself. */
19333 if (decl)
19334 gen_subprogram_die (decl, context_die);
19335 break;
19337 case TYPE_DECL:
19338 /* If we are in terse mode, don't generate any DIEs to represent any
19339 actual typedefs. */
19340 if (debug_info_level <= DINFO_LEVEL_TERSE)
19341 break;
19343 /* In the special case of a TYPE_DECL node representing the declaration
19344 of some type tag, if the given TYPE_DECL is marked as having been
19345 instantiated from some other (original) TYPE_DECL node (e.g. one which
19346 was generated within the original definition of an inline function) we
19347 used to generate a special (abbreviated) DW_TAG_structure_type,
19348 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
19349 should be actually referencing those DIEs, as variable DIEs with that
19350 type would be emitted already in the abstract origin, so it was always
19351 removed during unused type prunning. Don't add anything in this
19352 case. */
19353 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
19354 break;
19356 if (is_redundant_typedef (decl))
19357 gen_type_die (TREE_TYPE (decl), context_die);
19358 else
19359 /* Output a DIE to represent the typedef itself. */
19360 gen_typedef_die (decl, context_die);
19361 break;
19363 case LABEL_DECL:
19364 if (debug_info_level >= DINFO_LEVEL_NORMAL)
19365 gen_label_die (decl, context_die);
19366 break;
19368 case VAR_DECL:
19369 case RESULT_DECL:
19370 /* If we are in terse mode, don't generate any DIEs to represent any
19371 variable declarations or definitions. */
19372 if (debug_info_level <= DINFO_LEVEL_TERSE)
19373 break;
19375 /* Output any DIEs that are needed to specify the type of this data
19376 object. */
19377 if (decl_by_reference_p (decl_or_origin))
19378 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
19379 else
19380 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
19382 /* And its containing type. */
19383 class_origin = decl_class_context (decl_or_origin);
19384 if (class_origin != NULL_TREE)
19385 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
19387 /* And its containing namespace. */
19388 context_die = declare_in_namespace (decl_or_origin, context_die);
19390 /* Now output the DIE to represent the data object itself. This gets
19391 complicated because of the possibility that the VAR_DECL really
19392 represents an inlined instance of a formal parameter for an inline
19393 function. */
19394 ultimate_origin = decl_ultimate_origin (decl_or_origin);
19395 if (ultimate_origin != NULL_TREE
19396 && TREE_CODE (ultimate_origin) == PARM_DECL)
19397 gen_formal_parameter_die (decl, origin,
19398 true /* Emit name attribute. */,
19399 context_die);
19400 else
19401 gen_variable_die (decl, origin, context_die);
19402 break;
19404 case FIELD_DECL:
19405 /* Ignore the nameless fields that are used to skip bits but handle C++
19406 anonymous unions and structs. */
19407 if (DECL_NAME (decl) != NULL_TREE
19408 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
19409 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
19411 gen_type_die (member_declared_type (decl), context_die);
19412 gen_field_die (decl, context_die);
19414 break;
19416 case PARM_DECL:
19417 if (DECL_BY_REFERENCE (decl_or_origin))
19418 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
19419 else
19420 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
19421 return gen_formal_parameter_die (decl, origin,
19422 true /* Emit name attribute. */,
19423 context_die);
19425 case NAMESPACE_DECL:
19426 case IMPORTED_DECL:
19427 if (dwarf_version >= 3 || !dwarf_strict)
19428 gen_namespace_die (decl, context_die);
19429 break;
19431 default:
19432 /* Probably some frontend-internal decl. Assume we don't care. */
19433 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
19434 break;
19437 return NULL;
19440 /* Output debug information for global decl DECL. Called from toplev.c after
19441 compilation proper has finished. */
19443 static void
19444 dwarf2out_global_decl (tree decl)
19446 /* Output DWARF2 information for file-scope tentative data object
19447 declarations, file-scope (extern) function declarations (which
19448 had no corresponding body) and file-scope tagged type declarations
19449 and definitions which have not yet been forced out. */
19450 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
19451 dwarf2out_decl (decl);
19454 /* Output debug information for type decl DECL. Called from toplev.c
19455 and from language front ends (to record built-in types). */
19456 static void
19457 dwarf2out_type_decl (tree decl, int local)
19459 if (!local)
19460 dwarf2out_decl (decl);
19463 /* Output debug information for imported module or decl DECL.
19464 NAME is non-NULL name in the lexical block if the decl has been renamed.
19465 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
19466 that DECL belongs to.
19467 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
19468 static void
19469 dwarf2out_imported_module_or_decl_1 (tree decl,
19470 tree name,
19471 tree lexical_block,
19472 dw_die_ref lexical_block_die)
19474 expanded_location xloc;
19475 dw_die_ref imported_die = NULL;
19476 dw_die_ref at_import_die;
19478 if (TREE_CODE (decl) == IMPORTED_DECL)
19480 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
19481 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
19482 gcc_assert (decl);
19484 else
19485 xloc = expand_location (input_location);
19487 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
19489 at_import_die = force_type_die (TREE_TYPE (decl));
19490 /* For namespace N { typedef void T; } using N::T; base_type_die
19491 returns NULL, but DW_TAG_imported_declaration requires
19492 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
19493 if (!at_import_die)
19495 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
19496 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
19497 at_import_die = lookup_type_die (TREE_TYPE (decl));
19498 gcc_assert (at_import_die);
19501 else
19503 at_import_die = lookup_decl_die (decl);
19504 if (!at_import_die)
19506 /* If we're trying to avoid duplicate debug info, we may not have
19507 emitted the member decl for this field. Emit it now. */
19508 if (TREE_CODE (decl) == FIELD_DECL)
19510 tree type = DECL_CONTEXT (decl);
19512 if (TYPE_CONTEXT (type)
19513 && TYPE_P (TYPE_CONTEXT (type))
19514 && !should_emit_struct_debug (TYPE_CONTEXT (type),
19515 DINFO_USAGE_DIR_USE))
19516 return;
19517 gen_type_die_for_member (type, decl,
19518 get_context_die (TYPE_CONTEXT (type)));
19520 at_import_die = force_decl_die (decl);
19524 if (TREE_CODE (decl) == NAMESPACE_DECL)
19526 if (dwarf_version >= 3 || !dwarf_strict)
19527 imported_die = new_die (DW_TAG_imported_module,
19528 lexical_block_die,
19529 lexical_block);
19530 else
19531 return;
19533 else
19534 imported_die = new_die (DW_TAG_imported_declaration,
19535 lexical_block_die,
19536 lexical_block);
19538 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
19539 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
19540 if (name)
19541 add_AT_string (imported_die, DW_AT_name,
19542 IDENTIFIER_POINTER (name));
19543 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
19546 /* Output debug information for imported module or decl DECL.
19547 NAME is non-NULL name in context if the decl has been renamed.
19548 CHILD is true if decl is one of the renamed decls as part of
19549 importing whole module. */
19551 static void
19552 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
19553 bool child)
19555 /* dw_die_ref at_import_die; */
19556 dw_die_ref scope_die;
19558 if (debug_info_level <= DINFO_LEVEL_TERSE)
19559 return;
19561 gcc_assert (decl);
19563 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
19564 We need decl DIE for reference and scope die. First, get DIE for the decl
19565 itself. */
19567 /* Get the scope die for decl context. Use comp_unit_die for global module
19568 or decl. If die is not found for non globals, force new die. */
19569 if (context
19570 && TYPE_P (context)
19571 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
19572 return;
19574 if (!(dwarf_version >= 3 || !dwarf_strict))
19575 return;
19577 scope_die = get_context_die (context);
19579 if (child)
19581 gcc_assert (scope_die->die_child);
19582 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
19583 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
19584 scope_die = scope_die->die_child;
19587 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
19588 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
19592 /* Write the debugging output for DECL. */
19594 void
19595 dwarf2out_decl (tree decl)
19597 dw_die_ref context_die = comp_unit_die ();
19599 switch (TREE_CODE (decl))
19601 case ERROR_MARK:
19602 return;
19604 case FUNCTION_DECL:
19605 /* What we would really like to do here is to filter out all mere
19606 file-scope declarations of file-scope functions which are never
19607 referenced later within this translation unit (and keep all of ones
19608 that *are* referenced later on) but we aren't clairvoyant, so we have
19609 no idea which functions will be referenced in the future (i.e. later
19610 on within the current translation unit). So here we just ignore all
19611 file-scope function declarations which are not also definitions. If
19612 and when the debugger needs to know something about these functions,
19613 it will have to hunt around and find the DWARF information associated
19614 with the definition of the function.
19616 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
19617 nodes represent definitions and which ones represent mere
19618 declarations. We have to check DECL_INITIAL instead. That's because
19619 the C front-end supports some weird semantics for "extern inline"
19620 function definitions. These can get inlined within the current
19621 translation unit (and thus, we need to generate Dwarf info for their
19622 abstract instances so that the Dwarf info for the concrete inlined
19623 instances can have something to refer to) but the compiler never
19624 generates any out-of-lines instances of such things (despite the fact
19625 that they *are* definitions).
19627 The important point is that the C front-end marks these "extern
19628 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
19629 them anyway. Note that the C++ front-end also plays some similar games
19630 for inline function definitions appearing within include files which
19631 also contain `#pragma interface' pragmas.
19633 If we are called from dwarf2out_abstract_function output a DIE
19634 anyway. We can end up here this way with early inlining and LTO
19635 where the inlined function is output in a different LTRANS unit
19636 or not at all. */
19637 if (DECL_INITIAL (decl) == NULL_TREE
19638 && ! DECL_ABSTRACT (decl))
19639 return;
19641 /* If we're a nested function, initially use a parent of NULL; if we're
19642 a plain function, this will be fixed up in decls_for_scope. If
19643 we're a method, it will be ignored, since we already have a DIE. */
19644 if (decl_function_context (decl)
19645 /* But if we're in terse mode, we don't care about scope. */
19646 && debug_info_level > DINFO_LEVEL_TERSE)
19647 context_die = NULL;
19648 break;
19650 case VAR_DECL:
19651 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
19652 declaration and if the declaration was never even referenced from
19653 within this entire compilation unit. We suppress these DIEs in
19654 order to save space in the .debug section (by eliminating entries
19655 which are probably useless). Note that we must not suppress
19656 block-local extern declarations (whether used or not) because that
19657 would screw-up the debugger's name lookup mechanism and cause it to
19658 miss things which really ought to be in scope at a given point. */
19659 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
19660 return;
19662 /* For local statics lookup proper context die. */
19663 if (TREE_STATIC (decl)
19664 && DECL_CONTEXT (decl)
19665 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
19666 context_die = lookup_decl_die (DECL_CONTEXT (decl));
19668 /* If we are in terse mode, don't generate any DIEs to represent any
19669 variable declarations or definitions. */
19670 if (debug_info_level <= DINFO_LEVEL_TERSE)
19671 return;
19672 break;
19674 case CONST_DECL:
19675 if (debug_info_level <= DINFO_LEVEL_TERSE)
19676 return;
19677 if (!is_fortran () && !is_ada ())
19678 return;
19679 if (TREE_STATIC (decl) && decl_function_context (decl))
19680 context_die = lookup_decl_die (DECL_CONTEXT (decl));
19681 break;
19683 case NAMESPACE_DECL:
19684 case IMPORTED_DECL:
19685 if (debug_info_level <= DINFO_LEVEL_TERSE)
19686 return;
19687 if (lookup_decl_die (decl) != NULL)
19688 return;
19689 break;
19691 case TYPE_DECL:
19692 /* Don't emit stubs for types unless they are needed by other DIEs. */
19693 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
19694 return;
19696 /* Don't bother trying to generate any DIEs to represent any of the
19697 normal built-in types for the language we are compiling. */
19698 if (DECL_IS_BUILTIN (decl))
19699 return;
19701 /* If we are in terse mode, don't generate any DIEs for types. */
19702 if (debug_info_level <= DINFO_LEVEL_TERSE)
19703 return;
19705 /* If we're a function-scope tag, initially use a parent of NULL;
19706 this will be fixed up in decls_for_scope. */
19707 if (decl_function_context (decl))
19708 context_die = NULL;
19710 break;
19712 default:
19713 return;
19716 gen_decl_die (decl, NULL, context_die);
19719 /* Write the debugging output for DECL. */
19721 static void
19722 dwarf2out_function_decl (tree decl)
19724 dwarf2out_decl (decl);
19725 call_arg_locations = NULL;
19726 call_arg_loc_last = NULL;
19727 call_site_count = -1;
19728 tail_call_site_count = -1;
19729 VEC_free (dw_die_ref, heap, block_map);
19730 htab_empty (decl_loc_table);
19731 htab_empty (cached_dw_loc_list_table);
19734 /* Output a marker (i.e. a label) for the beginning of the generated code for
19735 a lexical block. */
19737 static void
19738 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
19739 unsigned int blocknum)
19741 switch_to_section (current_function_section ());
19742 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
19745 /* Output a marker (i.e. a label) for the end of the generated code for a
19746 lexical block. */
19748 static void
19749 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
19751 switch_to_section (current_function_section ());
19752 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
19755 /* Returns nonzero if it is appropriate not to emit any debugging
19756 information for BLOCK, because it doesn't contain any instructions.
19758 Don't allow this for blocks with nested functions or local classes
19759 as we would end up with orphans, and in the presence of scheduling
19760 we may end up calling them anyway. */
19762 static bool
19763 dwarf2out_ignore_block (const_tree block)
19765 tree decl;
19766 unsigned int i;
19768 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
19769 if (TREE_CODE (decl) == FUNCTION_DECL
19770 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
19771 return 0;
19772 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
19774 decl = BLOCK_NONLOCALIZED_VAR (block, i);
19775 if (TREE_CODE (decl) == FUNCTION_DECL
19776 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
19777 return 0;
19780 return 1;
19783 /* Hash table routines for file_hash. */
19785 static int
19786 file_table_eq (const void *p1_p, const void *p2_p)
19788 const struct dwarf_file_data *const p1 =
19789 (const struct dwarf_file_data *) p1_p;
19790 const char *const p2 = (const char *) p2_p;
19791 return filename_cmp (p1->filename, p2) == 0;
19794 static hashval_t
19795 file_table_hash (const void *p_p)
19797 const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
19798 return htab_hash_string (p->filename);
19801 /* Lookup FILE_NAME (in the list of filenames that we know about here in
19802 dwarf2out.c) and return its "index". The index of each (known) filename is
19803 just a unique number which is associated with only that one filename. We
19804 need such numbers for the sake of generating labels (in the .debug_sfnames
19805 section) and references to those files numbers (in the .debug_srcinfo
19806 and.debug_macinfo sections). If the filename given as an argument is not
19807 found in our current list, add it to the list and assign it the next
19808 available unique index number. In order to speed up searches, we remember
19809 the index of the filename was looked up last. This handles the majority of
19810 all searches. */
19812 static struct dwarf_file_data *
19813 lookup_filename (const char *file_name)
19815 void ** slot;
19816 struct dwarf_file_data * created;
19818 /* Check to see if the file name that was searched on the previous
19819 call matches this file name. If so, return the index. */
19820 if (file_table_last_lookup
19821 && (file_name == file_table_last_lookup->filename
19822 || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
19823 return file_table_last_lookup;
19825 /* Didn't match the previous lookup, search the table. */
19826 slot = htab_find_slot_with_hash (file_table, file_name,
19827 htab_hash_string (file_name), INSERT);
19828 if (*slot)
19829 return (struct dwarf_file_data *) *slot;
19831 created = ggc_alloc_dwarf_file_data ();
19832 created->filename = file_name;
19833 created->emitted_number = 0;
19834 *slot = created;
19835 return created;
19838 /* If the assembler will construct the file table, then translate the compiler
19839 internal file table number into the assembler file table number, and emit
19840 a .file directive if we haven't already emitted one yet. The file table
19841 numbers are different because we prune debug info for unused variables and
19842 types, which may include filenames. */
19844 static int
19845 maybe_emit_file (struct dwarf_file_data * fd)
19847 if (! fd->emitted_number)
19849 if (last_emitted_file)
19850 fd->emitted_number = last_emitted_file->emitted_number + 1;
19851 else
19852 fd->emitted_number = 1;
19853 last_emitted_file = fd;
19855 if (DWARF2_ASM_LINE_DEBUG_INFO)
19857 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
19858 output_quoted_string (asm_out_file,
19859 remap_debug_filename (fd->filename));
19860 fputc ('\n', asm_out_file);
19864 return fd->emitted_number;
19867 /* Schedule generation of a DW_AT_const_value attribute to DIE.
19868 That generation should happen after function debug info has been
19869 generated. The value of the attribute is the constant value of ARG. */
19871 static void
19872 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
19874 die_arg_entry entry;
19876 if (!die || !arg)
19877 return;
19879 if (!tmpl_value_parm_die_table)
19880 tmpl_value_parm_die_table
19881 = VEC_alloc (die_arg_entry, gc, 32);
19883 entry.die = die;
19884 entry.arg = arg;
19885 VEC_safe_push (die_arg_entry, gc,
19886 tmpl_value_parm_die_table,
19887 entry);
19890 /* Return TRUE if T is an instance of generic type, FALSE
19891 otherwise. */
19893 static bool
19894 generic_type_p (tree t)
19896 if (t == NULL_TREE || !TYPE_P (t))
19897 return false;
19898 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
19901 /* Schedule the generation of the generic parameter dies for the
19902 instance of generic type T. The proper generation itself is later
19903 done by gen_scheduled_generic_parms_dies. */
19905 static void
19906 schedule_generic_params_dies_gen (tree t)
19908 if (!generic_type_p (t))
19909 return;
19911 if (generic_type_instances == NULL)
19912 generic_type_instances = VEC_alloc (tree, gc, 256);
19914 VEC_safe_push (tree, gc, generic_type_instances, t);
19917 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
19918 by append_entry_to_tmpl_value_parm_die_table. This function must
19919 be called after function DIEs have been generated. */
19921 static void
19922 gen_remaining_tmpl_value_param_die_attribute (void)
19924 if (tmpl_value_parm_die_table)
19926 unsigned i;
19927 die_arg_entry *e;
19929 FOR_EACH_VEC_ELT (die_arg_entry, tmpl_value_parm_die_table, i, e)
19930 tree_add_const_value_attribute (e->die, e->arg);
19934 /* Generate generic parameters DIEs for instances of generic types
19935 that have been previously scheduled by
19936 schedule_generic_params_dies_gen. This function must be called
19937 after all the types of the CU have been laid out. */
19939 static void
19940 gen_scheduled_generic_parms_dies (void)
19942 unsigned i;
19943 tree t;
19945 if (generic_type_instances == NULL)
19946 return;
19948 FOR_EACH_VEC_ELT (tree, generic_type_instances, i, t)
19949 gen_generic_params_dies (t);
19953 /* Replace DW_AT_name for the decl with name. */
19955 static void
19956 dwarf2out_set_name (tree decl, tree name)
19958 dw_die_ref die;
19959 dw_attr_ref attr;
19960 const char *dname;
19962 die = TYPE_SYMTAB_DIE (decl);
19963 if (!die)
19964 return;
19966 dname = dwarf2_name (name, 0);
19967 if (!dname)
19968 return;
19970 attr = get_AT (die, DW_AT_name);
19971 if (attr)
19973 struct indirect_string_node *node;
19975 node = find_AT_string (dname);
19976 /* replace the string. */
19977 attr->dw_attr_val.v.val_str = node;
19980 else
19981 add_name_attribute (die, dname);
19984 /* Called by the final INSN scan whenever we see a var location. We
19985 use it to drop labels in the right places, and throw the location in
19986 our lookup table. */
19988 static void
19989 dwarf2out_var_location (rtx loc_note)
19991 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
19992 struct var_loc_node *newloc;
19993 rtx next_real, next_note;
19994 static const char *last_label;
19995 static const char *last_postcall_label;
19996 static bool last_in_cold_section_p;
19997 static rtx expected_next_loc_note;
19998 tree decl;
19999 bool var_loc_p;
20001 if (!NOTE_P (loc_note))
20003 if (CALL_P (loc_note))
20005 call_site_count++;
20006 if (SIBLING_CALL_P (loc_note))
20007 tail_call_site_count++;
20009 return;
20012 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
20013 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
20014 return;
20016 /* Optimize processing a large consecutive sequence of location
20017 notes so we don't spend too much time in next_real_insn. If the
20018 next insn is another location note, remember the next_real_insn
20019 calculation for next time. */
20020 next_real = cached_next_real_insn;
20021 if (next_real)
20023 if (expected_next_loc_note != loc_note)
20024 next_real = NULL_RTX;
20027 next_note = NEXT_INSN (loc_note);
20028 if (! next_note
20029 || INSN_DELETED_P (next_note)
20030 || GET_CODE (next_note) != NOTE
20031 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
20032 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
20033 next_note = NULL_RTX;
20035 if (! next_real)
20036 next_real = next_real_insn (loc_note);
20038 if (next_note)
20040 expected_next_loc_note = next_note;
20041 cached_next_real_insn = next_real;
20043 else
20044 cached_next_real_insn = NULL_RTX;
20046 /* If there are no instructions which would be affected by this note,
20047 don't do anything. */
20048 if (var_loc_p
20049 && next_real == NULL_RTX
20050 && !NOTE_DURING_CALL_P (loc_note))
20051 return;
20053 if (next_real == NULL_RTX)
20054 next_real = get_last_insn ();
20056 /* If there were any real insns between note we processed last time
20057 and this note (or if it is the first note), clear
20058 last_{,postcall_}label so that they are not reused this time. */
20059 if (last_var_location_insn == NULL_RTX
20060 || last_var_location_insn != next_real
20061 || last_in_cold_section_p != in_cold_section_p)
20063 last_label = NULL;
20064 last_postcall_label = NULL;
20067 if (var_loc_p)
20069 decl = NOTE_VAR_LOCATION_DECL (loc_note);
20070 newloc = add_var_loc_to_decl (decl, loc_note,
20071 NOTE_DURING_CALL_P (loc_note)
20072 ? last_postcall_label : last_label);
20073 if (newloc == NULL)
20074 return;
20076 else
20078 decl = NULL_TREE;
20079 newloc = NULL;
20082 /* If there were no real insns between note we processed last time
20083 and this note, use the label we emitted last time. Otherwise
20084 create a new label and emit it. */
20085 if (last_label == NULL)
20087 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
20088 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
20089 loclabel_num++;
20090 last_label = ggc_strdup (loclabel);
20093 if (!var_loc_p)
20095 struct call_arg_loc_node *ca_loc
20096 = ggc_alloc_cleared_call_arg_loc_node ();
20097 rtx prev = prev_real_insn (loc_note), x;
20098 ca_loc->call_arg_loc_note = loc_note;
20099 ca_loc->next = NULL;
20100 ca_loc->label = last_label;
20101 gcc_assert (prev
20102 && (CALL_P (prev)
20103 || (NONJUMP_INSN_P (prev)
20104 && GET_CODE (PATTERN (prev)) == SEQUENCE
20105 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
20106 if (!CALL_P (prev))
20107 prev = XVECEXP (PATTERN (prev), 0, 0);
20108 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
20109 x = get_call_rtx_from (PATTERN (prev));
20110 if (x)
20112 x = XEXP (XEXP (x, 0), 0);
20113 if (GET_CODE (x) == SYMBOL_REF
20114 && SYMBOL_REF_DECL (x)
20115 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
20116 ca_loc->symbol_ref = x;
20118 ca_loc->block = insn_scope (prev);
20119 if (call_arg_locations)
20120 call_arg_loc_last->next = ca_loc;
20121 else
20122 call_arg_locations = ca_loc;
20123 call_arg_loc_last = ca_loc;
20125 else if (!NOTE_DURING_CALL_P (loc_note))
20126 newloc->label = last_label;
20127 else
20129 if (!last_postcall_label)
20131 sprintf (loclabel, "%s-1", last_label);
20132 last_postcall_label = ggc_strdup (loclabel);
20134 newloc->label = last_postcall_label;
20137 last_var_location_insn = next_real;
20138 last_in_cold_section_p = in_cold_section_p;
20141 /* Note in one location list that text section has changed. */
20143 static int
20144 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
20146 var_loc_list *list = (var_loc_list *) *slot;
20147 if (list->first)
20148 list->last_before_switch
20149 = list->last->next ? list->last->next : list->last;
20150 return 1;
20153 /* Note in all location lists that text section has changed. */
20155 static void
20156 var_location_switch_text_section (void)
20158 if (decl_loc_table == NULL)
20159 return;
20161 htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
20164 /* Create a new line number table. */
20166 static dw_line_info_table *
20167 new_line_info_table (void)
20169 dw_line_info_table *table;
20171 table = ggc_alloc_cleared_dw_line_info_table_struct ();
20172 table->file_num = 1;
20173 table->line_num = 1;
20174 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
20176 return table;
20179 /* Lookup the "current" table into which we emit line info, so
20180 that we don't have to do it for every source line. */
20182 static void
20183 set_cur_line_info_table (section *sec)
20185 dw_line_info_table *table;
20187 if (sec == text_section)
20188 table = text_section_line_info;
20189 else if (sec == cold_text_section)
20191 table = cold_text_section_line_info;
20192 if (!table)
20194 cold_text_section_line_info = table = new_line_info_table ();
20195 table->end_label = cold_end_label;
20198 else
20200 const char *end_label;
20202 if (flag_reorder_blocks_and_partition)
20204 if (in_cold_section_p)
20205 end_label = crtl->subsections.cold_section_end_label;
20206 else
20207 end_label = crtl->subsections.hot_section_end_label;
20209 else
20211 char label[MAX_ARTIFICIAL_LABEL_BYTES];
20212 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
20213 current_function_funcdef_no);
20214 end_label = ggc_strdup (label);
20217 table = new_line_info_table ();
20218 table->end_label = end_label;
20220 VEC_safe_push (dw_line_info_table_p, gc, separate_line_info, table);
20223 if (DWARF2_ASM_LINE_DEBUG_INFO)
20224 table->is_stmt = (cur_line_info_table
20225 ? cur_line_info_table->is_stmt
20226 : DWARF_LINE_DEFAULT_IS_STMT_START);
20227 cur_line_info_table = table;
20231 /* We need to reset the locations at the beginning of each
20232 function. We can't do this in the end_function hook, because the
20233 declarations that use the locations won't have been output when
20234 that hook is called. Also compute have_multiple_function_sections here. */
20236 static void
20237 dwarf2out_begin_function (tree fun)
20239 section *sec = function_section (fun);
20241 if (sec != text_section)
20242 have_multiple_function_sections = true;
20244 if (flag_reorder_blocks_and_partition && !cold_text_section)
20246 gcc_assert (current_function_decl == fun);
20247 cold_text_section = unlikely_text_section ();
20248 switch_to_section (cold_text_section);
20249 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
20250 switch_to_section (sec);
20253 dwarf2out_note_section_used ();
20254 call_site_count = 0;
20255 tail_call_site_count = 0;
20257 set_cur_line_info_table (sec);
20260 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
20262 static void
20263 push_dw_line_info_entry (dw_line_info_table *table,
20264 enum dw_line_info_opcode opcode, unsigned int val)
20266 dw_line_info_entry e;
20267 e.opcode = opcode;
20268 e.val = val;
20269 VEC_safe_push (dw_line_info_entry, gc, table->entries, e);
20272 /* Output a label to mark the beginning of a source code line entry
20273 and record information relating to this source line, in
20274 'line_info_table' for later output of the .debug_line section. */
20275 /* ??? The discriminator parameter ought to be unsigned. */
20277 static void
20278 dwarf2out_source_line (unsigned int line, const char *filename,
20279 int discriminator, bool is_stmt)
20281 unsigned int file_num;
20282 dw_line_info_table *table;
20284 if (debug_info_level < DINFO_LEVEL_NORMAL || line == 0)
20285 return;
20287 /* The discriminator column was added in dwarf4. Simplify the below
20288 by simply removing it if we're not supposed to output it. */
20289 if (dwarf_version < 4 && dwarf_strict)
20290 discriminator = 0;
20292 table = cur_line_info_table;
20293 file_num = maybe_emit_file (lookup_filename (filename));
20295 /* ??? TODO: Elide duplicate line number entries. Traditionally,
20296 the debugger has used the second (possibly duplicate) line number
20297 at the beginning of the function to mark the end of the prologue.
20298 We could eliminate any other duplicates within the function. For
20299 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
20300 that second line number entry. */
20301 /* Recall that this end-of-prologue indication is *not* the same thing
20302 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
20303 to which the hook corresponds, follows the last insn that was
20304 emitted by gen_prologue. What we need is to precede the first insn
20305 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
20306 insn that corresponds to something the user wrote. These may be
20307 very different locations once scheduling is enabled. */
20309 if (0 && file_num == table->file_num
20310 && line == table->line_num
20311 && discriminator == table->discrim_num
20312 && is_stmt == table->is_stmt)
20313 return;
20315 switch_to_section (current_function_section ());
20317 /* If requested, emit something human-readable. */
20318 if (flag_debug_asm)
20319 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
20321 if (DWARF2_ASM_LINE_DEBUG_INFO)
20323 /* Emit the .loc directive understood by GNU as. */
20324 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
20325 file_num, line, is_stmt, discriminator */
20326 fputs ("\t.loc ", asm_out_file);
20327 fprint_ul (asm_out_file, file_num);
20328 putc (' ', asm_out_file);
20329 fprint_ul (asm_out_file, line);
20330 putc (' ', asm_out_file);
20331 putc ('0', asm_out_file);
20333 if (is_stmt != table->is_stmt)
20335 fputs (" is_stmt ", asm_out_file);
20336 putc (is_stmt ? '1' : '0', asm_out_file);
20338 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
20340 gcc_assert (discriminator > 0);
20341 fputs (" discriminator ", asm_out_file);
20342 fprint_ul (asm_out_file, (unsigned long) discriminator);
20344 putc ('\n', asm_out_file);
20346 else
20348 unsigned int label_num = ++line_info_label_num;
20350 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
20352 push_dw_line_info_entry (table, LI_set_address, label_num);
20353 if (file_num != table->file_num)
20354 push_dw_line_info_entry (table, LI_set_file, file_num);
20355 if (discriminator != table->discrim_num)
20356 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
20357 if (is_stmt != table->is_stmt)
20358 push_dw_line_info_entry (table, LI_negate_stmt, 0);
20359 push_dw_line_info_entry (table, LI_set_line, line);
20362 table->file_num = file_num;
20363 table->line_num = line;
20364 table->discrim_num = discriminator;
20365 table->is_stmt = is_stmt;
20366 table->in_use = true;
20369 /* Record the beginning of a new source file. */
20371 static void
20372 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
20374 if (flag_eliminate_dwarf2_dups)
20376 /* Record the beginning of the file for break_out_includes. */
20377 dw_die_ref bincl_die;
20379 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
20380 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
20383 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20385 macinfo_entry e;
20386 e.code = DW_MACINFO_start_file;
20387 e.lineno = lineno;
20388 e.info = ggc_strdup (filename);
20389 VEC_safe_push (macinfo_entry, gc, macinfo_table, e);
20393 /* Record the end of a source file. */
20395 static void
20396 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
20398 if (flag_eliminate_dwarf2_dups)
20399 /* Record the end of the file for break_out_includes. */
20400 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
20402 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20404 macinfo_entry e;
20405 e.code = DW_MACINFO_end_file;
20406 e.lineno = lineno;
20407 e.info = NULL;
20408 VEC_safe_push (macinfo_entry, gc, macinfo_table, e);
20412 /* Called from debug_define in toplev.c. The `buffer' parameter contains
20413 the tail part of the directive line, i.e. the part which is past the
20414 initial whitespace, #, whitespace, directive-name, whitespace part. */
20416 static void
20417 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
20418 const char *buffer ATTRIBUTE_UNUSED)
20420 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20422 macinfo_entry e;
20423 /* Insert a dummy first entry to be able to optimize the whole
20424 predefined macro block using DW_MACRO_GNU_transparent_include. */
20425 if (VEC_empty (macinfo_entry, macinfo_table) && lineno <= 1)
20427 e.code = 0;
20428 e.lineno = 0;
20429 e.info = NULL;
20430 VEC_safe_push (macinfo_entry, gc, macinfo_table, e);
20432 e.code = DW_MACINFO_define;
20433 e.lineno = lineno;
20434 e.info = ggc_strdup (buffer);
20435 VEC_safe_push (macinfo_entry, gc, macinfo_table, e);
20439 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
20440 the tail part of the directive line, i.e. the part which is past the
20441 initial whitespace, #, whitespace, directive-name, whitespace part. */
20443 static void
20444 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
20445 const char *buffer ATTRIBUTE_UNUSED)
20447 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20449 macinfo_entry e;
20450 /* Insert a dummy first entry to be able to optimize the whole
20451 predefined macro block using DW_MACRO_GNU_transparent_include. */
20452 if (VEC_empty (macinfo_entry, macinfo_table) && lineno <= 1)
20454 e.code = 0;
20455 e.lineno = 0;
20456 e.info = NULL;
20457 VEC_safe_push (macinfo_entry, gc, macinfo_table, e);
20459 e.code = DW_MACINFO_undef;
20460 e.lineno = lineno;
20461 e.info = ggc_strdup (buffer);
20462 VEC_safe_push (macinfo_entry, gc, macinfo_table, e);
20466 /* Routines to manipulate hash table of CUs. */
20468 static hashval_t
20469 htab_macinfo_hash (const void *of)
20471 const macinfo_entry *const entry =
20472 (const macinfo_entry *) of;
20474 return htab_hash_string (entry->info);
20477 static int
20478 htab_macinfo_eq (const void *of1, const void *of2)
20480 const macinfo_entry *const entry1 = (const macinfo_entry *) of1;
20481 const macinfo_entry *const entry2 = (const macinfo_entry *) of2;
20483 return !strcmp (entry1->info, entry2->info);
20486 /* Output a single .debug_macinfo entry. */
20488 static void
20489 output_macinfo_op (macinfo_entry *ref)
20491 int file_num;
20492 size_t len;
20493 struct indirect_string_node *node;
20494 char label[MAX_ARTIFICIAL_LABEL_BYTES];
20495 struct dwarf_file_data *fd;
20497 switch (ref->code)
20499 case DW_MACINFO_start_file:
20500 fd = lookup_filename (ref->info);
20501 file_num = maybe_emit_file (fd);
20502 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
20503 dw2_asm_output_data_uleb128 (ref->lineno,
20504 "Included from line number %lu",
20505 (unsigned long) ref->lineno);
20506 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
20507 break;
20508 case DW_MACINFO_end_file:
20509 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
20510 break;
20511 case DW_MACINFO_define:
20512 case DW_MACINFO_undef:
20513 len = strlen (ref->info) + 1;
20514 if (!dwarf_strict
20515 && len > DWARF_OFFSET_SIZE
20516 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
20517 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
20519 ref->code = ref->code == DW_MACINFO_define
20520 ? DW_MACRO_GNU_define_indirect
20521 : DW_MACRO_GNU_undef_indirect;
20522 output_macinfo_op (ref);
20523 return;
20525 dw2_asm_output_data (1, ref->code,
20526 ref->code == DW_MACINFO_define
20527 ? "Define macro" : "Undefine macro");
20528 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
20529 (unsigned long) ref->lineno);
20530 dw2_asm_output_nstring (ref->info, -1, "The macro");
20531 break;
20532 case DW_MACRO_GNU_define_indirect:
20533 case DW_MACRO_GNU_undef_indirect:
20534 node = find_AT_string (ref->info);
20535 if (node->form != DW_FORM_strp)
20537 char label[32];
20538 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
20539 ++dw2_string_counter;
20540 node->label = xstrdup (label);
20541 node->form = DW_FORM_strp;
20543 dw2_asm_output_data (1, ref->code,
20544 ref->code == DW_MACRO_GNU_define_indirect
20545 ? "Define macro indirect"
20546 : "Undefine macro indirect");
20547 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
20548 (unsigned long) ref->lineno);
20549 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
20550 debug_str_section, "The macro: \"%s\"",
20551 ref->info);
20552 break;
20553 case DW_MACRO_GNU_transparent_include:
20554 dw2_asm_output_data (1, ref->code, "Transparent include");
20555 ASM_GENERATE_INTERNAL_LABEL (label,
20556 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
20557 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
20558 break;
20559 default:
20560 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
20561 ASM_COMMENT_START, (unsigned long) ref->code);
20562 break;
20566 /* Attempt to make a sequence of define/undef macinfo ops shareable with
20567 other compilation unit .debug_macinfo sections. IDX is the first
20568 index of a define/undef, return the number of ops that should be
20569 emitted in a comdat .debug_macinfo section and emit
20570 a DW_MACRO_GNU_transparent_include entry referencing it.
20571 If the define/undef entry should be emitted normally, return 0. */
20573 static unsigned
20574 optimize_macinfo_range (unsigned int idx, VEC (macinfo_entry, gc) *files,
20575 htab_t *macinfo_htab)
20577 macinfo_entry *first, *second, *cur, *inc;
20578 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
20579 unsigned char checksum[16];
20580 struct md5_ctx ctx;
20581 char *grp_name, *tail;
20582 const char *base;
20583 unsigned int i, count, encoded_filename_len, linebuf_len;
20584 void **slot;
20586 first = &VEC_index (macinfo_entry, macinfo_table, idx);
20587 second = &VEC_index (macinfo_entry, macinfo_table, idx + 1);
20589 /* Optimize only if there are at least two consecutive define/undef ops,
20590 and either all of them are before first DW_MACINFO_start_file
20591 with lineno {0,1} (i.e. predefined macro block), or all of them are
20592 in some included header file. */
20593 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
20594 return 0;
20595 if (VEC_empty (macinfo_entry, files))
20597 if (first->lineno > 1 || second->lineno > 1)
20598 return 0;
20600 else if (first->lineno == 0)
20601 return 0;
20603 /* Find the last define/undef entry that can be grouped together
20604 with first and at the same time compute md5 checksum of their
20605 codes, linenumbers and strings. */
20606 md5_init_ctx (&ctx);
20607 for (i = idx; VEC_iterate (macinfo_entry, macinfo_table, i, cur); i++)
20608 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
20609 break;
20610 else if (VEC_empty (macinfo_entry, files) && cur->lineno > 1)
20611 break;
20612 else
20614 unsigned char code = cur->code;
20615 md5_process_bytes (&code, 1, &ctx);
20616 checksum_uleb128 (cur->lineno, &ctx);
20617 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
20619 md5_finish_ctx (&ctx, checksum);
20620 count = i - idx;
20622 /* From the containing include filename (if any) pick up just
20623 usable characters from its basename. */
20624 if (VEC_empty (macinfo_entry, files))
20625 base = "";
20626 else
20627 base = lbasename (VEC_last (macinfo_entry, files).info);
20628 for (encoded_filename_len = 0, i = 0; base[i]; i++)
20629 if (ISIDNUM (base[i]) || base[i] == '.')
20630 encoded_filename_len++;
20631 /* Count . at the end. */
20632 if (encoded_filename_len)
20633 encoded_filename_len++;
20635 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
20636 linebuf_len = strlen (linebuf);
20638 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
20639 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
20640 + 16 * 2 + 1);
20641 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
20642 tail = grp_name + 4;
20643 if (encoded_filename_len)
20645 for (i = 0; base[i]; i++)
20646 if (ISIDNUM (base[i]) || base[i] == '.')
20647 *tail++ = base[i];
20648 *tail++ = '.';
20650 memcpy (tail, linebuf, linebuf_len);
20651 tail += linebuf_len;
20652 *tail++ = '.';
20653 for (i = 0; i < 16; i++)
20654 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
20656 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
20657 in the empty vector entry before the first define/undef. */
20658 inc = &VEC_index (macinfo_entry, macinfo_table, idx - 1);
20659 inc->code = DW_MACRO_GNU_transparent_include;
20660 inc->lineno = 0;
20661 inc->info = ggc_strdup (grp_name);
20662 if (*macinfo_htab == NULL)
20663 *macinfo_htab = htab_create (10, htab_macinfo_hash, htab_macinfo_eq, NULL);
20664 /* Avoid emitting duplicates. */
20665 slot = htab_find_slot (*macinfo_htab, inc, INSERT);
20666 if (*slot != NULL)
20668 inc->code = 0;
20669 inc->info = NULL;
20670 /* If such an entry has been used before, just emit
20671 a DW_MACRO_GNU_transparent_include op. */
20672 inc = (macinfo_entry *) *slot;
20673 output_macinfo_op (inc);
20674 /* And clear all macinfo_entry in the range to avoid emitting them
20675 in the second pass. */
20676 for (i = idx;
20677 VEC_iterate (macinfo_entry, macinfo_table, i, cur)
20678 && i < idx + count;
20679 i++)
20681 cur->code = 0;
20682 cur->info = NULL;
20685 else
20687 *slot = inc;
20688 inc->lineno = htab_elements (*macinfo_htab);
20689 output_macinfo_op (inc);
20691 return count;
20694 /* Output macinfo section(s). */
20696 static void
20697 output_macinfo (void)
20699 unsigned i;
20700 unsigned long length = VEC_length (macinfo_entry, macinfo_table);
20701 macinfo_entry *ref;
20702 VEC (macinfo_entry, gc) *files = NULL;
20703 htab_t macinfo_htab = NULL;
20705 if (! length)
20706 return;
20708 /* output_macinfo* uses these interchangeably. */
20709 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
20710 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
20711 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
20712 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
20714 /* For .debug_macro emit the section header. */
20715 if (!dwarf_strict)
20717 dw2_asm_output_data (2, 4, "DWARF macro version number");
20718 if (DWARF_OFFSET_SIZE == 8)
20719 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
20720 else
20721 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
20722 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_line_section_label,
20723 debug_line_section, NULL);
20726 /* In the first loop, it emits the primary .debug_macinfo section
20727 and after each emitted op the macinfo_entry is cleared.
20728 If a longer range of define/undef ops can be optimized using
20729 DW_MACRO_GNU_transparent_include, the
20730 DW_MACRO_GNU_transparent_include op is emitted and kept in
20731 the vector before the first define/undef in the range and the
20732 whole range of define/undef ops is not emitted and kept. */
20733 for (i = 0; VEC_iterate (macinfo_entry, macinfo_table, i, ref); i++)
20735 switch (ref->code)
20737 case DW_MACINFO_start_file:
20738 VEC_safe_push (macinfo_entry, gc, files, *ref);
20739 break;
20740 case DW_MACINFO_end_file:
20741 if (!VEC_empty (macinfo_entry, files))
20742 VEC_pop (macinfo_entry, files);
20743 break;
20744 case DW_MACINFO_define:
20745 case DW_MACINFO_undef:
20746 if (!dwarf_strict
20747 && HAVE_COMDAT_GROUP
20748 && VEC_length (macinfo_entry, files) != 1
20749 && i > 0
20750 && i + 1 < length
20751 && VEC_index (macinfo_entry, macinfo_table, i - 1).code == 0)
20753 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
20754 if (count)
20756 i += count - 1;
20757 continue;
20760 break;
20761 case 0:
20762 /* A dummy entry may be inserted at the beginning to be able
20763 to optimize the whole block of predefined macros. */
20764 if (i == 0)
20765 continue;
20766 default:
20767 break;
20769 output_macinfo_op (ref);
20770 ref->info = NULL;
20771 ref->code = 0;
20774 if (macinfo_htab == NULL)
20775 return;
20777 htab_delete (macinfo_htab);
20779 /* If any DW_MACRO_GNU_transparent_include were used, on those
20780 DW_MACRO_GNU_transparent_include entries terminate the
20781 current chain and switch to a new comdat .debug_macinfo
20782 section and emit the define/undef entries within it. */
20783 for (i = 0; VEC_iterate (macinfo_entry, macinfo_table, i, ref); i++)
20784 switch (ref->code)
20786 case 0:
20787 continue;
20788 case DW_MACRO_GNU_transparent_include:
20790 char label[MAX_ARTIFICIAL_LABEL_BYTES];
20791 tree comdat_key = get_identifier (ref->info);
20792 /* Terminate the previous .debug_macinfo section. */
20793 dw2_asm_output_data (1, 0, "End compilation unit");
20794 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
20795 SECTION_DEBUG
20796 | SECTION_LINKONCE,
20797 comdat_key);
20798 ASM_GENERATE_INTERNAL_LABEL (label,
20799 DEBUG_MACRO_SECTION_LABEL,
20800 ref->lineno);
20801 ASM_OUTPUT_LABEL (asm_out_file, label);
20802 ref->code = 0;
20803 ref->info = NULL;
20804 dw2_asm_output_data (2, 4, "DWARF macro version number");
20805 if (DWARF_OFFSET_SIZE == 8)
20806 dw2_asm_output_data (1, 1, "Flags: 64-bit");
20807 else
20808 dw2_asm_output_data (1, 0, "Flags: 32-bit");
20810 break;
20811 case DW_MACINFO_define:
20812 case DW_MACINFO_undef:
20813 output_macinfo_op (ref);
20814 ref->code = 0;
20815 ref->info = NULL;
20816 break;
20817 default:
20818 gcc_unreachable ();
20822 /* Set up for Dwarf output at the start of compilation. */
20824 static void
20825 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
20827 /* Allocate the file_table. */
20828 file_table = htab_create_ggc (50, file_table_hash,
20829 file_table_eq, NULL);
20831 /* Allocate the decl_die_table. */
20832 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
20833 decl_die_table_eq, NULL);
20835 /* Allocate the decl_loc_table. */
20836 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
20837 decl_loc_table_eq, NULL);
20839 /* Allocate the cached_dw_loc_list_table. */
20840 cached_dw_loc_list_table
20841 = htab_create_ggc (10, cached_dw_loc_list_table_hash,
20842 cached_dw_loc_list_table_eq, NULL);
20844 /* Allocate the initial hunk of the decl_scope_table. */
20845 decl_scope_table = VEC_alloc (tree, gc, 256);
20847 /* Allocate the initial hunk of the abbrev_die_table. */
20848 abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
20849 (ABBREV_DIE_TABLE_INCREMENT);
20850 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
20851 /* Zero-th entry is allocated, but unused. */
20852 abbrev_die_table_in_use = 1;
20854 /* Allocate the pubtypes and pubnames vectors. */
20855 pubname_table = VEC_alloc (pubname_entry, gc, 32);
20856 pubtype_table = VEC_alloc (pubname_entry, gc, 32);
20858 incomplete_types = VEC_alloc (tree, gc, 64);
20860 used_rtx_array = VEC_alloc (rtx, gc, 32);
20862 debug_info_section = get_section (DEBUG_INFO_SECTION,
20863 SECTION_DEBUG, NULL);
20864 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
20865 SECTION_DEBUG, NULL);
20866 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
20867 SECTION_DEBUG, NULL);
20868 debug_macinfo_section = get_section (dwarf_strict
20869 ? DEBUG_MACINFO_SECTION
20870 : DEBUG_MACRO_SECTION,
20871 SECTION_DEBUG, NULL);
20872 debug_line_section = get_section (DEBUG_LINE_SECTION,
20873 SECTION_DEBUG, NULL);
20874 debug_loc_section = get_section (DEBUG_LOC_SECTION,
20875 SECTION_DEBUG, NULL);
20876 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
20877 SECTION_DEBUG, NULL);
20878 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
20879 SECTION_DEBUG, NULL);
20880 debug_str_section = get_section (DEBUG_STR_SECTION,
20881 DEBUG_STR_SECTION_FLAGS, NULL);
20882 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
20883 SECTION_DEBUG, NULL);
20884 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
20885 SECTION_DEBUG, NULL);
20887 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
20888 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
20889 DEBUG_ABBREV_SECTION_LABEL, 0);
20890 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
20891 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
20892 COLD_TEXT_SECTION_LABEL, 0);
20893 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
20895 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
20896 DEBUG_INFO_SECTION_LABEL, 0);
20897 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
20898 DEBUG_LINE_SECTION_LABEL, 0);
20899 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
20900 DEBUG_RANGES_SECTION_LABEL, 0);
20901 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
20902 dwarf_strict
20903 ? DEBUG_MACINFO_SECTION_LABEL
20904 : DEBUG_MACRO_SECTION_LABEL, 0);
20906 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20907 macinfo_table = VEC_alloc (macinfo_entry, gc, 64);
20909 switch_to_section (text_section);
20910 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
20912 /* Make sure the line number table for .text always exists. */
20913 text_section_line_info = new_line_info_table ();
20914 text_section_line_info->end_label = text_end_label;
20917 /* Called before compile () starts outputtting functions, variables
20918 and toplevel asms into assembly. */
20920 static void
20921 dwarf2out_assembly_start (void)
20923 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
20924 && dwarf2out_do_cfi_asm ()
20925 && (!(flag_unwind_tables || flag_exceptions)
20926 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
20927 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
20930 /* A helper function for dwarf2out_finish called through
20931 htab_traverse. Emit one queued .debug_str string. */
20933 static int
20934 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
20936 struct indirect_string_node *node = (struct indirect_string_node *) *h;
20938 if (node->form == DW_FORM_strp)
20940 switch_to_section (debug_str_section);
20941 ASM_OUTPUT_LABEL (asm_out_file, node->label);
20942 assemble_string (node->str, strlen (node->str) + 1);
20945 return 1;
20948 #if ENABLE_ASSERT_CHECKING
20949 /* Verify that all marks are clear. */
20951 static void
20952 verify_marks_clear (dw_die_ref die)
20954 dw_die_ref c;
20956 gcc_assert (! die->die_mark);
20957 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
20959 #endif /* ENABLE_ASSERT_CHECKING */
20961 /* Clear the marks for a die and its children.
20962 Be cool if the mark isn't set. */
20964 static void
20965 prune_unmark_dies (dw_die_ref die)
20967 dw_die_ref c;
20969 if (die->die_mark)
20970 die->die_mark = 0;
20971 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
20974 /* Given DIE that we're marking as used, find any other dies
20975 it references as attributes and mark them as used. */
20977 static void
20978 prune_unused_types_walk_attribs (dw_die_ref die)
20980 dw_attr_ref a;
20981 unsigned ix;
20983 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
20985 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
20987 /* A reference to another DIE.
20988 Make sure that it will get emitted.
20989 If it was broken out into a comdat group, don't follow it. */
20990 if (! AT_ref (a)->comdat_type_p
20991 || a->dw_attr == DW_AT_specification)
20992 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
20994 /* Set the string's refcount to 0 so that prune_unused_types_mark
20995 accounts properly for it. */
20996 if (AT_class (a) == dw_val_class_str)
20997 a->dw_attr_val.v.val_str->refcount = 0;
21001 /* Mark the generic parameters and arguments children DIEs of DIE. */
21003 static void
21004 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
21006 dw_die_ref c;
21008 if (die == NULL || die->die_child == NULL)
21009 return;
21010 c = die->die_child;
21013 switch (c->die_tag)
21015 case DW_TAG_template_type_param:
21016 case DW_TAG_template_value_param:
21017 case DW_TAG_GNU_template_template_param:
21018 case DW_TAG_GNU_template_parameter_pack:
21019 prune_unused_types_mark (c, 1);
21020 break;
21021 default:
21022 break;
21024 c = c->die_sib;
21025 } while (c && c != die->die_child);
21028 /* Mark DIE as being used. If DOKIDS is true, then walk down
21029 to DIE's children. */
21031 static void
21032 prune_unused_types_mark (dw_die_ref die, int dokids)
21034 dw_die_ref c;
21036 if (die->die_mark == 0)
21038 /* We haven't done this node yet. Mark it as used. */
21039 die->die_mark = 1;
21040 /* If this is the DIE of a generic type instantiation,
21041 mark the children DIEs that describe its generic parms and
21042 args. */
21043 prune_unused_types_mark_generic_parms_dies (die);
21045 /* We also have to mark its parents as used.
21046 (But we don't want to mark our parents' kids due to this.) */
21047 if (die->die_parent)
21048 prune_unused_types_mark (die->die_parent, 0);
21050 /* Mark any referenced nodes. */
21051 prune_unused_types_walk_attribs (die);
21053 /* If this node is a specification,
21054 also mark the definition, if it exists. */
21055 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
21056 prune_unused_types_mark (die->die_definition, 1);
21059 if (dokids && die->die_mark != 2)
21061 /* We need to walk the children, but haven't done so yet.
21062 Remember that we've walked the kids. */
21063 die->die_mark = 2;
21065 /* If this is an array type, we need to make sure our
21066 kids get marked, even if they're types. If we're
21067 breaking out types into comdat sections, do this
21068 for all type definitions. */
21069 if (die->die_tag == DW_TAG_array_type
21070 || (use_debug_types
21071 && is_type_die (die) && ! is_declaration_die (die)))
21072 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
21073 else
21074 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
21078 /* For local classes, look if any static member functions were emitted
21079 and if so, mark them. */
21081 static void
21082 prune_unused_types_walk_local_classes (dw_die_ref die)
21084 dw_die_ref c;
21086 if (die->die_mark == 2)
21087 return;
21089 switch (die->die_tag)
21091 case DW_TAG_structure_type:
21092 case DW_TAG_union_type:
21093 case DW_TAG_class_type:
21094 break;
21096 case DW_TAG_subprogram:
21097 if (!get_AT_flag (die, DW_AT_declaration)
21098 || die->die_definition != NULL)
21099 prune_unused_types_mark (die, 1);
21100 return;
21102 default:
21103 return;
21106 /* Mark children. */
21107 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
21110 /* Walk the tree DIE and mark types that we actually use. */
21112 static void
21113 prune_unused_types_walk (dw_die_ref die)
21115 dw_die_ref c;
21117 /* Don't do anything if this node is already marked and
21118 children have been marked as well. */
21119 if (die->die_mark == 2)
21120 return;
21122 switch (die->die_tag)
21124 case DW_TAG_structure_type:
21125 case DW_TAG_union_type:
21126 case DW_TAG_class_type:
21127 if (die->die_perennial_p)
21128 break;
21130 for (c = die->die_parent; c; c = c->die_parent)
21131 if (c->die_tag == DW_TAG_subprogram)
21132 break;
21134 /* Finding used static member functions inside of classes
21135 is needed just for local classes, because for other classes
21136 static member function DIEs with DW_AT_specification
21137 are emitted outside of the DW_TAG_*_type. If we ever change
21138 it, we'd need to call this even for non-local classes. */
21139 if (c)
21140 prune_unused_types_walk_local_classes (die);
21142 /* It's a type node --- don't mark it. */
21143 return;
21145 case DW_TAG_const_type:
21146 case DW_TAG_packed_type:
21147 case DW_TAG_pointer_type:
21148 case DW_TAG_reference_type:
21149 case DW_TAG_rvalue_reference_type:
21150 case DW_TAG_volatile_type:
21151 case DW_TAG_typedef:
21152 case DW_TAG_array_type:
21153 case DW_TAG_interface_type:
21154 case DW_TAG_friend:
21155 case DW_TAG_variant_part:
21156 case DW_TAG_enumeration_type:
21157 case DW_TAG_subroutine_type:
21158 case DW_TAG_string_type:
21159 case DW_TAG_set_type:
21160 case DW_TAG_subrange_type:
21161 case DW_TAG_ptr_to_member_type:
21162 case DW_TAG_file_type:
21163 if (die->die_perennial_p)
21164 break;
21166 /* It's a type node --- don't mark it. */
21167 return;
21169 default:
21170 /* Mark everything else. */
21171 break;
21174 if (die->die_mark == 0)
21176 die->die_mark = 1;
21178 /* Now, mark any dies referenced from here. */
21179 prune_unused_types_walk_attribs (die);
21182 die->die_mark = 2;
21184 /* Mark children. */
21185 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
21188 /* Increment the string counts on strings referred to from DIE's
21189 attributes. */
21191 static void
21192 prune_unused_types_update_strings (dw_die_ref die)
21194 dw_attr_ref a;
21195 unsigned ix;
21197 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
21198 if (AT_class (a) == dw_val_class_str)
21200 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
21201 s->refcount++;
21202 /* Avoid unnecessarily putting strings that are used less than
21203 twice in the hash table. */
21204 if (s->refcount
21205 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
21207 void ** slot;
21208 slot = htab_find_slot_with_hash (debug_str_hash, s->str,
21209 htab_hash_string (s->str),
21210 INSERT);
21211 gcc_assert (*slot == NULL);
21212 *slot = s;
21217 /* Remove from the tree DIE any dies that aren't marked. */
21219 static void
21220 prune_unused_types_prune (dw_die_ref die)
21222 dw_die_ref c;
21224 gcc_assert (die->die_mark);
21225 prune_unused_types_update_strings (die);
21227 if (! die->die_child)
21228 return;
21230 c = die->die_child;
21231 do {
21232 dw_die_ref prev = c;
21233 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
21234 if (c == die->die_child)
21236 /* No marked children between 'prev' and the end of the list. */
21237 if (prev == c)
21238 /* No marked children at all. */
21239 die->die_child = NULL;
21240 else
21242 prev->die_sib = c->die_sib;
21243 die->die_child = prev;
21245 return;
21248 if (c != prev->die_sib)
21249 prev->die_sib = c;
21250 prune_unused_types_prune (c);
21251 } while (c != die->die_child);
21254 /* Remove dies representing declarations that we never use. */
21256 static void
21257 prune_unused_types (void)
21259 unsigned int i;
21260 limbo_die_node *node;
21261 comdat_type_node *ctnode;
21262 pubname_ref pub;
21263 dw_die_ref base_type;
21265 #if ENABLE_ASSERT_CHECKING
21266 /* All the marks should already be clear. */
21267 verify_marks_clear (comp_unit_die ());
21268 for (node = limbo_die_list; node; node = node->next)
21269 verify_marks_clear (node->die);
21270 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21271 verify_marks_clear (ctnode->root_die);
21272 #endif /* ENABLE_ASSERT_CHECKING */
21274 /* Mark types that are used in global variables. */
21275 premark_types_used_by_global_vars ();
21277 /* Set the mark on nodes that are actually used. */
21278 prune_unused_types_walk (comp_unit_die ());
21279 for (node = limbo_die_list; node; node = node->next)
21280 prune_unused_types_walk (node->die);
21281 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21283 prune_unused_types_walk (ctnode->root_die);
21284 prune_unused_types_mark (ctnode->type_die, 1);
21287 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
21288 are unusual in that they are pubnames that are the children of pubtypes.
21289 They should only be marked via their parent DW_TAG_enumeration_type die,
21290 not as roots in themselves. */
21291 FOR_EACH_VEC_ELT (pubname_entry, pubname_table, i, pub)
21292 if (pub->die->die_tag != DW_TAG_enumerator)
21293 prune_unused_types_mark (pub->die, 1);
21294 for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
21295 prune_unused_types_mark (base_type, 1);
21297 if (debug_str_hash)
21298 htab_empty (debug_str_hash);
21299 prune_unused_types_prune (comp_unit_die ());
21300 for (node = limbo_die_list; node; node = node->next)
21301 prune_unused_types_prune (node->die);
21302 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21303 prune_unused_types_prune (ctnode->root_die);
21305 /* Leave the marks clear. */
21306 prune_unmark_dies (comp_unit_die ());
21307 for (node = limbo_die_list; node; node = node->next)
21308 prune_unmark_dies (node->die);
21309 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21310 prune_unmark_dies (ctnode->root_die);
21313 /* Set the parameter to true if there are any relative pathnames in
21314 the file table. */
21315 static int
21316 file_table_relative_p (void ** slot, void *param)
21318 bool *p = (bool *) param;
21319 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
21320 if (!IS_ABSOLUTE_PATH (d->filename))
21322 *p = true;
21323 return 0;
21325 return 1;
21328 /* Routines to manipulate hash table of comdat type units. */
21330 static hashval_t
21331 htab_ct_hash (const void *of)
21333 hashval_t h;
21334 const comdat_type_node *const type_node = (const comdat_type_node *) of;
21336 memcpy (&h, type_node->signature, sizeof (h));
21337 return h;
21340 static int
21341 htab_ct_eq (const void *of1, const void *of2)
21343 const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
21344 const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
21346 return (! memcmp (type_node_1->signature, type_node_2->signature,
21347 DWARF_TYPE_SIGNATURE_SIZE));
21350 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
21351 to the location it would have been added, should we know its
21352 DECL_ASSEMBLER_NAME when we added other attributes. This will
21353 probably improve compactness of debug info, removing equivalent
21354 abbrevs, and hide any differences caused by deferring the
21355 computation of the assembler name, triggered by e.g. PCH. */
21357 static inline void
21358 move_linkage_attr (dw_die_ref die)
21360 unsigned ix = VEC_length (dw_attr_node, die->die_attr);
21361 dw_attr_node linkage = VEC_index (dw_attr_node, die->die_attr, ix - 1);
21363 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
21364 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
21366 while (--ix > 0)
21368 dw_attr_node *prev = &VEC_index (dw_attr_node, die->die_attr, ix - 1);
21370 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
21371 break;
21374 if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
21376 VEC_pop (dw_attr_node, die->die_attr);
21377 VEC_quick_insert (dw_attr_node, die->die_attr, ix, linkage);
21381 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
21382 referenced from typed stack ops and count how often they are used. */
21384 static void
21385 mark_base_types (dw_loc_descr_ref loc)
21387 dw_die_ref base_type = NULL;
21389 for (; loc; loc = loc->dw_loc_next)
21391 switch (loc->dw_loc_opc)
21393 case DW_OP_GNU_regval_type:
21394 case DW_OP_GNU_deref_type:
21395 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
21396 break;
21397 case DW_OP_GNU_convert:
21398 case DW_OP_GNU_reinterpret:
21399 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
21400 continue;
21401 /* FALLTHRU */
21402 case DW_OP_GNU_const_type:
21403 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
21404 break;
21405 case DW_OP_GNU_entry_value:
21406 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
21407 continue;
21408 default:
21409 continue;
21411 gcc_assert (base_type->die_parent == comp_unit_die ());
21412 if (base_type->die_mark)
21413 base_type->die_mark++;
21414 else
21416 VEC_safe_push (dw_die_ref, heap, base_types, base_type);
21417 base_type->die_mark = 1;
21422 /* Comparison function for sorting marked base types. */
21424 static int
21425 base_type_cmp (const void *x, const void *y)
21427 dw_die_ref dx = *(const dw_die_ref *) x;
21428 dw_die_ref dy = *(const dw_die_ref *) y;
21429 unsigned int byte_size1, byte_size2;
21430 unsigned int encoding1, encoding2;
21431 if (dx->die_mark > dy->die_mark)
21432 return -1;
21433 if (dx->die_mark < dy->die_mark)
21434 return 1;
21435 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
21436 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
21437 if (byte_size1 < byte_size2)
21438 return 1;
21439 if (byte_size1 > byte_size2)
21440 return -1;
21441 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
21442 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
21443 if (encoding1 < encoding2)
21444 return 1;
21445 if (encoding1 > encoding2)
21446 return -1;
21447 return 0;
21450 /* Move base types marked by mark_base_types as early as possible
21451 in the CU, sorted by decreasing usage count both to make the
21452 uleb128 references as small as possible and to make sure they
21453 will have die_offset already computed by calc_die_sizes when
21454 sizes of typed stack loc ops is computed. */
21456 static void
21457 move_marked_base_types (void)
21459 unsigned int i;
21460 dw_die_ref base_type, die, c;
21462 if (VEC_empty (dw_die_ref, base_types))
21463 return;
21465 /* Sort by decreasing usage count, they will be added again in that
21466 order later on. */
21467 VEC_qsort (dw_die_ref, base_types, base_type_cmp);
21468 die = comp_unit_die ();
21469 c = die->die_child;
21472 dw_die_ref prev = c;
21473 c = c->die_sib;
21474 while (c->die_mark)
21476 remove_child_with_prev (c, prev);
21477 /* As base types got marked, there must be at least
21478 one node other than DW_TAG_base_type. */
21479 gcc_assert (c != c->die_sib);
21480 c = c->die_sib;
21483 while (c != die->die_child);
21484 gcc_assert (die->die_child);
21485 c = die->die_child;
21486 for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
21488 base_type->die_mark = 0;
21489 base_type->die_sib = c->die_sib;
21490 c->die_sib = base_type;
21491 c = base_type;
21495 /* Helper function for resolve_addr, attempt to resolve
21496 one CONST_STRING, return non-zero if not successful. Similarly verify that
21497 SYMBOL_REFs refer to variables emitted in the current CU. */
21499 static int
21500 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
21502 rtx rtl = *addr;
21504 if (GET_CODE (rtl) == CONST_STRING)
21506 size_t len = strlen (XSTR (rtl, 0)) + 1;
21507 tree t = build_string (len, XSTR (rtl, 0));
21508 tree tlen = size_int (len - 1);
21509 TREE_TYPE (t)
21510 = build_array_type (char_type_node, build_index_type (tlen));
21511 rtl = lookup_constant_def (t);
21512 if (!rtl || !MEM_P (rtl))
21513 return 1;
21514 rtl = XEXP (rtl, 0);
21515 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
21516 *addr = rtl;
21517 return 0;
21520 if (GET_CODE (rtl) == SYMBOL_REF
21521 && SYMBOL_REF_DECL (rtl))
21523 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
21525 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
21526 return 1;
21528 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
21529 return 1;
21532 if (GET_CODE (rtl) == CONST
21533 && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
21534 return 1;
21536 return 0;
21539 /* Helper function for resolve_addr, handle one location
21540 expression, return false if at least one CONST_STRING or SYMBOL_REF in
21541 the location list couldn't be resolved. */
21543 static bool
21544 resolve_addr_in_expr (dw_loc_descr_ref loc)
21546 dw_loc_descr_ref keep = NULL;
21547 for (; loc; loc = loc->dw_loc_next)
21548 switch (loc->dw_loc_opc)
21550 case DW_OP_addr:
21551 if (resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
21552 return false;
21553 break;
21554 case DW_OP_const4u:
21555 case DW_OP_const8u:
21556 if (loc->dtprel
21557 && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
21558 return false;
21559 break;
21560 case DW_OP_plus_uconst:
21561 if (size_of_loc_descr (loc)
21562 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
21564 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
21566 dw_loc_descr_ref repl
21567 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
21568 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
21569 add_loc_descr (&repl, loc->dw_loc_next);
21570 *loc = *repl;
21572 break;
21573 case DW_OP_implicit_value:
21574 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
21575 && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL))
21576 return false;
21577 break;
21578 case DW_OP_GNU_implicit_pointer:
21579 case DW_OP_GNU_parameter_ref:
21580 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
21582 dw_die_ref ref
21583 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
21584 if (ref == NULL)
21585 return false;
21586 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
21587 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
21588 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
21590 break;
21591 case DW_OP_GNU_const_type:
21592 case DW_OP_GNU_regval_type:
21593 case DW_OP_GNU_deref_type:
21594 case DW_OP_GNU_convert:
21595 case DW_OP_GNU_reinterpret:
21596 while (loc->dw_loc_next
21597 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
21599 dw_die_ref base1, base2;
21600 unsigned enc1, enc2, size1, size2;
21601 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
21602 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
21603 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
21604 else if (loc->dw_loc_oprnd1.val_class
21605 == dw_val_class_unsigned_const)
21606 break;
21607 else
21608 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
21609 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
21610 == dw_val_class_unsigned_const)
21611 break;
21612 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
21613 gcc_assert (base1->die_tag == DW_TAG_base_type
21614 && base2->die_tag == DW_TAG_base_type);
21615 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
21616 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
21617 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
21618 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
21619 if (size1 == size2
21620 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
21621 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
21622 && loc != keep)
21623 || enc1 == enc2))
21625 /* Optimize away next DW_OP_GNU_convert after
21626 adjusting LOC's base type die reference. */
21627 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
21628 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
21629 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
21630 else
21631 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
21632 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
21633 continue;
21635 /* Don't change integer DW_OP_GNU_convert after e.g. floating
21636 point typed stack entry. */
21637 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
21638 keep = loc->dw_loc_next;
21639 break;
21641 break;
21642 default:
21643 break;
21645 return true;
21648 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
21649 an address in .rodata section if the string literal is emitted there,
21650 or remove the containing location list or replace DW_AT_const_value
21651 with DW_AT_location and empty location expression, if it isn't found
21652 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
21653 to something that has been emitted in the current CU. */
21655 static void
21656 resolve_addr (dw_die_ref die)
21658 dw_die_ref c;
21659 dw_attr_ref a;
21660 dw_loc_list_ref *curr, *start, loc;
21661 unsigned ix;
21663 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
21664 switch (AT_class (a))
21666 case dw_val_class_loc_list:
21667 start = curr = AT_loc_list_ptr (a);
21668 loc = *curr;
21669 gcc_assert (loc);
21670 /* The same list can be referenced more than once. See if we have
21671 already recorded the result from a previous pass. */
21672 if (loc->replaced)
21673 *curr = loc->dw_loc_next;
21674 else if (!loc->resolved_addr)
21676 /* As things stand, we do not expect or allow one die to
21677 reference a suffix of another die's location list chain.
21678 References must be identical or completely separate.
21679 There is therefore no need to cache the result of this
21680 pass on any list other than the first; doing so
21681 would lead to unnecessary writes. */
21682 while (*curr)
21684 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
21685 if (!resolve_addr_in_expr ((*curr)->expr))
21687 dw_loc_list_ref next = (*curr)->dw_loc_next;
21688 if (next && (*curr)->ll_symbol)
21690 gcc_assert (!next->ll_symbol);
21691 next->ll_symbol = (*curr)->ll_symbol;
21693 *curr = next;
21695 else
21697 mark_base_types ((*curr)->expr);
21698 curr = &(*curr)->dw_loc_next;
21701 if (loc == *start)
21702 loc->resolved_addr = 1;
21703 else
21705 loc->replaced = 1;
21706 loc->dw_loc_next = *start;
21709 if (!*start)
21711 remove_AT (die, a->dw_attr);
21712 ix--;
21714 break;
21715 case dw_val_class_loc:
21717 dw_loc_descr_ref l = AT_loc (a);
21718 /* For -gdwarf-2 don't attempt to optimize
21719 DW_AT_data_member_location containing
21720 DW_OP_plus_uconst - older consumers might
21721 rely on it being that op instead of a more complex,
21722 but shorter, location description. */
21723 if ((dwarf_version > 2
21724 || a->dw_attr != DW_AT_data_member_location
21725 || l == NULL
21726 || l->dw_loc_opc != DW_OP_plus_uconst
21727 || l->dw_loc_next != NULL)
21728 && !resolve_addr_in_expr (l))
21730 remove_AT (die, a->dw_attr);
21731 ix--;
21733 else
21734 mark_base_types (l);
21736 break;
21737 case dw_val_class_addr:
21738 if (a->dw_attr == DW_AT_const_value
21739 && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
21741 remove_AT (die, a->dw_attr);
21742 ix--;
21744 if (die->die_tag == DW_TAG_GNU_call_site
21745 && a->dw_attr == DW_AT_abstract_origin)
21747 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
21748 dw_die_ref tdie = lookup_decl_die (tdecl);
21749 if (tdie == NULL
21750 && DECL_EXTERNAL (tdecl)
21751 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
21753 force_decl_die (tdecl);
21754 tdie = lookup_decl_die (tdecl);
21756 if (tdie)
21758 a->dw_attr_val.val_class = dw_val_class_die_ref;
21759 a->dw_attr_val.v.val_die_ref.die = tdie;
21760 a->dw_attr_val.v.val_die_ref.external = 0;
21762 else
21764 remove_AT (die, a->dw_attr);
21765 ix--;
21768 break;
21769 default:
21770 break;
21773 FOR_EACH_CHILD (die, c, resolve_addr (c));
21776 /* Helper routines for optimize_location_lists.
21777 This pass tries to share identical local lists in .debug_loc
21778 section. */
21780 /* Iteratively hash operands of LOC opcode. */
21782 static inline hashval_t
21783 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
21785 dw_val_ref val1 = &loc->dw_loc_oprnd1;
21786 dw_val_ref val2 = &loc->dw_loc_oprnd2;
21788 switch (loc->dw_loc_opc)
21790 case DW_OP_const4u:
21791 case DW_OP_const8u:
21792 if (loc->dtprel)
21793 goto hash_addr;
21794 /* FALLTHRU */
21795 case DW_OP_const1u:
21796 case DW_OP_const1s:
21797 case DW_OP_const2u:
21798 case DW_OP_const2s:
21799 case DW_OP_const4s:
21800 case DW_OP_const8s:
21801 case DW_OP_constu:
21802 case DW_OP_consts:
21803 case DW_OP_pick:
21804 case DW_OP_plus_uconst:
21805 case DW_OP_breg0:
21806 case DW_OP_breg1:
21807 case DW_OP_breg2:
21808 case DW_OP_breg3:
21809 case DW_OP_breg4:
21810 case DW_OP_breg5:
21811 case DW_OP_breg6:
21812 case DW_OP_breg7:
21813 case DW_OP_breg8:
21814 case DW_OP_breg9:
21815 case DW_OP_breg10:
21816 case DW_OP_breg11:
21817 case DW_OP_breg12:
21818 case DW_OP_breg13:
21819 case DW_OP_breg14:
21820 case DW_OP_breg15:
21821 case DW_OP_breg16:
21822 case DW_OP_breg17:
21823 case DW_OP_breg18:
21824 case DW_OP_breg19:
21825 case DW_OP_breg20:
21826 case DW_OP_breg21:
21827 case DW_OP_breg22:
21828 case DW_OP_breg23:
21829 case DW_OP_breg24:
21830 case DW_OP_breg25:
21831 case DW_OP_breg26:
21832 case DW_OP_breg27:
21833 case DW_OP_breg28:
21834 case DW_OP_breg29:
21835 case DW_OP_breg30:
21836 case DW_OP_breg31:
21837 case DW_OP_regx:
21838 case DW_OP_fbreg:
21839 case DW_OP_piece:
21840 case DW_OP_deref_size:
21841 case DW_OP_xderef_size:
21842 hash = iterative_hash_object (val1->v.val_int, hash);
21843 break;
21844 case DW_OP_skip:
21845 case DW_OP_bra:
21847 int offset;
21849 gcc_assert (val1->val_class == dw_val_class_loc);
21850 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
21851 hash = iterative_hash_object (offset, hash);
21853 break;
21854 case DW_OP_implicit_value:
21855 hash = iterative_hash_object (val1->v.val_unsigned, hash);
21856 switch (val2->val_class)
21858 case dw_val_class_const:
21859 hash = iterative_hash_object (val2->v.val_int, hash);
21860 break;
21861 case dw_val_class_vec:
21863 unsigned int elt_size = val2->v.val_vec.elt_size;
21864 unsigned int len = val2->v.val_vec.length;
21866 hash = iterative_hash_object (elt_size, hash);
21867 hash = iterative_hash_object (len, hash);
21868 hash = iterative_hash (val2->v.val_vec.array,
21869 len * elt_size, hash);
21871 break;
21872 case dw_val_class_const_double:
21873 hash = iterative_hash_object (val2->v.val_double.low, hash);
21874 hash = iterative_hash_object (val2->v.val_double.high, hash);
21875 break;
21876 case dw_val_class_addr:
21877 hash = iterative_hash_rtx (val2->v.val_addr, hash);
21878 break;
21879 default:
21880 gcc_unreachable ();
21882 break;
21883 case DW_OP_bregx:
21884 case DW_OP_bit_piece:
21885 hash = iterative_hash_object (val1->v.val_int, hash);
21886 hash = iterative_hash_object (val2->v.val_int, hash);
21887 break;
21888 case DW_OP_addr:
21889 hash_addr:
21890 if (loc->dtprel)
21892 unsigned char dtprel = 0xd1;
21893 hash = iterative_hash_object (dtprel, hash);
21895 hash = iterative_hash_rtx (val1->v.val_addr, hash);
21896 break;
21897 case DW_OP_GNU_implicit_pointer:
21898 hash = iterative_hash_object (val2->v.val_int, hash);
21899 break;
21900 case DW_OP_GNU_entry_value:
21901 hash = hash_loc_operands (val1->v.val_loc, hash);
21902 break;
21903 case DW_OP_GNU_regval_type:
21904 case DW_OP_GNU_deref_type:
21906 unsigned int byte_size
21907 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
21908 unsigned int encoding
21909 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
21910 hash = iterative_hash_object (val1->v.val_int, hash);
21911 hash = iterative_hash_object (byte_size, hash);
21912 hash = iterative_hash_object (encoding, hash);
21914 break;
21915 case DW_OP_GNU_convert:
21916 case DW_OP_GNU_reinterpret:
21917 if (val1->val_class == dw_val_class_unsigned_const)
21919 hash = iterative_hash_object (val1->v.val_unsigned, hash);
21920 break;
21922 /* FALLTHRU */
21923 case DW_OP_GNU_const_type:
21925 unsigned int byte_size
21926 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
21927 unsigned int encoding
21928 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
21929 hash = iterative_hash_object (byte_size, hash);
21930 hash = iterative_hash_object (encoding, hash);
21931 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
21932 break;
21933 hash = iterative_hash_object (val2->val_class, hash);
21934 switch (val2->val_class)
21936 case dw_val_class_const:
21937 hash = iterative_hash_object (val2->v.val_int, hash);
21938 break;
21939 case dw_val_class_vec:
21941 unsigned int elt_size = val2->v.val_vec.elt_size;
21942 unsigned int len = val2->v.val_vec.length;
21944 hash = iterative_hash_object (elt_size, hash);
21945 hash = iterative_hash_object (len, hash);
21946 hash = iterative_hash (val2->v.val_vec.array,
21947 len * elt_size, hash);
21949 break;
21950 case dw_val_class_const_double:
21951 hash = iterative_hash_object (val2->v.val_double.low, hash);
21952 hash = iterative_hash_object (val2->v.val_double.high, hash);
21953 break;
21954 default:
21955 gcc_unreachable ();
21958 break;
21960 default:
21961 /* Other codes have no operands. */
21962 break;
21964 return hash;
21967 /* Iteratively hash the whole DWARF location expression LOC. */
21969 static inline hashval_t
21970 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
21972 dw_loc_descr_ref l;
21973 bool sizes_computed = false;
21974 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
21975 size_of_locs (loc);
21977 for (l = loc; l != NULL; l = l->dw_loc_next)
21979 enum dwarf_location_atom opc = l->dw_loc_opc;
21980 hash = iterative_hash_object (opc, hash);
21981 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
21983 size_of_locs (loc);
21984 sizes_computed = true;
21986 hash = hash_loc_operands (l, hash);
21988 return hash;
21991 /* Compute hash of the whole location list LIST_HEAD. */
21993 static inline void
21994 hash_loc_list (dw_loc_list_ref list_head)
21996 dw_loc_list_ref curr = list_head;
21997 hashval_t hash = 0;
21999 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
22001 hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
22002 hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
22003 if (curr->section)
22004 hash = iterative_hash (curr->section, strlen (curr->section) + 1,
22005 hash);
22006 hash = hash_locs (curr->expr, hash);
22008 list_head->hash = hash;
22011 /* Return true if X and Y opcodes have the same operands. */
22013 static inline bool
22014 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
22016 dw_val_ref valx1 = &x->dw_loc_oprnd1;
22017 dw_val_ref valx2 = &x->dw_loc_oprnd2;
22018 dw_val_ref valy1 = &y->dw_loc_oprnd1;
22019 dw_val_ref valy2 = &y->dw_loc_oprnd2;
22021 switch (x->dw_loc_opc)
22023 case DW_OP_const4u:
22024 case DW_OP_const8u:
22025 if (x->dtprel)
22026 goto hash_addr;
22027 /* FALLTHRU */
22028 case DW_OP_const1u:
22029 case DW_OP_const1s:
22030 case DW_OP_const2u:
22031 case DW_OP_const2s:
22032 case DW_OP_const4s:
22033 case DW_OP_const8s:
22034 case DW_OP_constu:
22035 case DW_OP_consts:
22036 case DW_OP_pick:
22037 case DW_OP_plus_uconst:
22038 case DW_OP_breg0:
22039 case DW_OP_breg1:
22040 case DW_OP_breg2:
22041 case DW_OP_breg3:
22042 case DW_OP_breg4:
22043 case DW_OP_breg5:
22044 case DW_OP_breg6:
22045 case DW_OP_breg7:
22046 case DW_OP_breg8:
22047 case DW_OP_breg9:
22048 case DW_OP_breg10:
22049 case DW_OP_breg11:
22050 case DW_OP_breg12:
22051 case DW_OP_breg13:
22052 case DW_OP_breg14:
22053 case DW_OP_breg15:
22054 case DW_OP_breg16:
22055 case DW_OP_breg17:
22056 case DW_OP_breg18:
22057 case DW_OP_breg19:
22058 case DW_OP_breg20:
22059 case DW_OP_breg21:
22060 case DW_OP_breg22:
22061 case DW_OP_breg23:
22062 case DW_OP_breg24:
22063 case DW_OP_breg25:
22064 case DW_OP_breg26:
22065 case DW_OP_breg27:
22066 case DW_OP_breg28:
22067 case DW_OP_breg29:
22068 case DW_OP_breg30:
22069 case DW_OP_breg31:
22070 case DW_OP_regx:
22071 case DW_OP_fbreg:
22072 case DW_OP_piece:
22073 case DW_OP_deref_size:
22074 case DW_OP_xderef_size:
22075 return valx1->v.val_int == valy1->v.val_int;
22076 case DW_OP_skip:
22077 case DW_OP_bra:
22078 gcc_assert (valx1->val_class == dw_val_class_loc
22079 && valy1->val_class == dw_val_class_loc
22080 && x->dw_loc_addr == y->dw_loc_addr);
22081 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
22082 case DW_OP_implicit_value:
22083 if (valx1->v.val_unsigned != valy1->v.val_unsigned
22084 || valx2->val_class != valy2->val_class)
22085 return false;
22086 switch (valx2->val_class)
22088 case dw_val_class_const:
22089 return valx2->v.val_int == valy2->v.val_int;
22090 case dw_val_class_vec:
22091 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
22092 && valx2->v.val_vec.length == valy2->v.val_vec.length
22093 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
22094 valx2->v.val_vec.elt_size
22095 * valx2->v.val_vec.length) == 0;
22096 case dw_val_class_const_double:
22097 return valx2->v.val_double.low == valy2->v.val_double.low
22098 && valx2->v.val_double.high == valy2->v.val_double.high;
22099 case dw_val_class_addr:
22100 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
22101 default:
22102 gcc_unreachable ();
22104 case DW_OP_bregx:
22105 case DW_OP_bit_piece:
22106 return valx1->v.val_int == valy1->v.val_int
22107 && valx2->v.val_int == valy2->v.val_int;
22108 case DW_OP_addr:
22109 hash_addr:
22110 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
22111 case DW_OP_GNU_implicit_pointer:
22112 return valx1->val_class == dw_val_class_die_ref
22113 && valx1->val_class == valy1->val_class
22114 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
22115 && valx2->v.val_int == valy2->v.val_int;
22116 case DW_OP_GNU_entry_value:
22117 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
22118 case DW_OP_GNU_const_type:
22119 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
22120 || valx2->val_class != valy2->val_class)
22121 return false;
22122 switch (valx2->val_class)
22124 case dw_val_class_const:
22125 return valx2->v.val_int == valy2->v.val_int;
22126 case dw_val_class_vec:
22127 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
22128 && valx2->v.val_vec.length == valy2->v.val_vec.length
22129 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
22130 valx2->v.val_vec.elt_size
22131 * valx2->v.val_vec.length) == 0;
22132 case dw_val_class_const_double:
22133 return valx2->v.val_double.low == valy2->v.val_double.low
22134 && valx2->v.val_double.high == valy2->v.val_double.high;
22135 default:
22136 gcc_unreachable ();
22138 case DW_OP_GNU_regval_type:
22139 case DW_OP_GNU_deref_type:
22140 return valx1->v.val_int == valy1->v.val_int
22141 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
22142 case DW_OP_GNU_convert:
22143 case DW_OP_GNU_reinterpret:
22144 if (valx1->val_class != valy1->val_class)
22145 return false;
22146 if (valx1->val_class == dw_val_class_unsigned_const)
22147 return valx1->v.val_unsigned == valy1->v.val_unsigned;
22148 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
22149 case DW_OP_GNU_parameter_ref:
22150 return valx1->val_class == dw_val_class_die_ref
22151 && valx1->val_class == valy1->val_class
22152 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
22153 default:
22154 /* Other codes have no operands. */
22155 return true;
22159 /* Return true if DWARF location expressions X and Y are the same. */
22161 static inline bool
22162 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
22164 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
22165 if (x->dw_loc_opc != y->dw_loc_opc
22166 || x->dtprel != y->dtprel
22167 || !compare_loc_operands (x, y))
22168 break;
22169 return x == NULL && y == NULL;
22172 /* Return precomputed hash of location list X. */
22174 static hashval_t
22175 loc_list_hash (const void *x)
22177 return ((const struct dw_loc_list_struct *) x)->hash;
22180 /* Return 1 if location lists X and Y are the same. */
22182 static int
22183 loc_list_eq (const void *x, const void *y)
22185 const struct dw_loc_list_struct *a = (const struct dw_loc_list_struct *) x;
22186 const struct dw_loc_list_struct *b = (const struct dw_loc_list_struct *) y;
22187 if (a == b)
22188 return 1;
22189 if (a->hash != b->hash)
22190 return 0;
22191 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
22192 if (strcmp (a->begin, b->begin) != 0
22193 || strcmp (a->end, b->end) != 0
22194 || (a->section == NULL) != (b->section == NULL)
22195 || (a->section && strcmp (a->section, b->section) != 0)
22196 || !compare_locs (a->expr, b->expr))
22197 break;
22198 return a == NULL && b == NULL;
22201 /* Recursively optimize location lists referenced from DIE
22202 children and share them whenever possible. */
22204 static void
22205 optimize_location_lists_1 (dw_die_ref die, htab_t htab)
22207 dw_die_ref c;
22208 dw_attr_ref a;
22209 unsigned ix;
22210 void **slot;
22212 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22213 if (AT_class (a) == dw_val_class_loc_list)
22215 dw_loc_list_ref list = AT_loc_list (a);
22216 /* TODO: perform some optimizations here, before hashing
22217 it and storing into the hash table. */
22218 hash_loc_list (list);
22219 slot = htab_find_slot_with_hash (htab, list, list->hash,
22220 INSERT);
22221 if (*slot == NULL)
22222 *slot = (void *) list;
22223 else
22224 a->dw_attr_val.v.val_loc_list = (dw_loc_list_ref) *slot;
22227 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
22230 /* Optimize location lists referenced from DIE
22231 children and share them whenever possible. */
22233 static void
22234 optimize_location_lists (dw_die_ref die)
22236 htab_t htab = htab_create (500, loc_list_hash, loc_list_eq, NULL);
22237 optimize_location_lists_1 (die, htab);
22238 htab_delete (htab);
22241 /* Output stuff that dwarf requires at the end of every file,
22242 and generate the DWARF-2 debugging info. */
22244 static void
22245 dwarf2out_finish (const char *filename)
22247 limbo_die_node *node, *next_node;
22248 comdat_type_node *ctnode;
22249 htab_t comdat_type_table;
22250 unsigned int i;
22252 /* PCH might result in DW_AT_producer string being restored from the
22253 header compilation, fix it up if needed. */
22254 dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
22255 if (strcmp (AT_string (producer), producer_string) != 0)
22257 struct indirect_string_node *node = find_AT_string (producer_string);
22258 producer->dw_attr_val.v.val_str = node;
22261 gen_scheduled_generic_parms_dies ();
22262 gen_remaining_tmpl_value_param_die_attribute ();
22264 /* Add the name for the main input file now. We delayed this from
22265 dwarf2out_init to avoid complications with PCH. */
22266 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
22267 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
22268 add_comp_dir_attribute (comp_unit_die ());
22269 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
22271 bool p = false;
22272 htab_traverse (file_table, file_table_relative_p, &p);
22273 if (p)
22274 add_comp_dir_attribute (comp_unit_die ());
22277 for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
22279 add_location_or_const_value_attribute (
22280 VEC_index (deferred_locations, deferred_locations_list, i).die,
22281 VEC_index (deferred_locations, deferred_locations_list, i).variable,
22282 false,
22283 DW_AT_location);
22286 /* Traverse the limbo die list, and add parent/child links. The only
22287 dies without parents that should be here are concrete instances of
22288 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
22289 For concrete instances, we can get the parent die from the abstract
22290 instance. */
22291 for (node = limbo_die_list; node; node = next_node)
22293 dw_die_ref die = node->die;
22294 next_node = node->next;
22296 if (die->die_parent == NULL)
22298 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
22300 if (origin && origin->die_parent)
22301 add_child_die (origin->die_parent, die);
22302 else if (is_cu_die (die))
22304 else if (seen_error ())
22305 /* It's OK to be confused by errors in the input. */
22306 add_child_die (comp_unit_die (), die);
22307 else
22309 /* In certain situations, the lexical block containing a
22310 nested function can be optimized away, which results
22311 in the nested function die being orphaned. Likewise
22312 with the return type of that nested function. Force
22313 this to be a child of the containing function.
22315 It may happen that even the containing function got fully
22316 inlined and optimized out. In that case we are lost and
22317 assign the empty child. This should not be big issue as
22318 the function is likely unreachable too. */
22319 gcc_assert (node->created_for);
22321 if (DECL_P (node->created_for))
22322 origin = get_context_die (DECL_CONTEXT (node->created_for));
22323 else if (TYPE_P (node->created_for))
22324 origin = scope_die_for (node->created_for, comp_unit_die ());
22325 else
22326 origin = comp_unit_die ();
22328 add_child_die (origin, die);
22333 limbo_die_list = NULL;
22335 #if ENABLE_ASSERT_CHECKING
22337 dw_die_ref die = comp_unit_die (), c;
22338 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
22340 #endif
22341 resolve_addr (comp_unit_die ());
22342 move_marked_base_types ();
22344 for (node = deferred_asm_name; node; node = node->next)
22346 tree decl = node->created_for;
22347 /* When generating LTO bytecode we can not generate new assembler
22348 names at this point and all important decls got theirs via
22349 free-lang-data. */
22350 if ((!flag_generate_lto || DECL_ASSEMBLER_NAME_SET_P (decl))
22351 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
22353 add_linkage_attr (node->die, decl);
22354 move_linkage_attr (node->die);
22358 deferred_asm_name = NULL;
22360 /* Walk through the list of incomplete types again, trying once more to
22361 emit full debugging info for them. */
22362 retry_incomplete_types ();
22364 if (flag_eliminate_unused_debug_types)
22365 prune_unused_types ();
22367 /* Generate separate COMDAT sections for type DIEs. */
22368 if (use_debug_types)
22370 break_out_comdat_types (comp_unit_die ());
22372 /* Each new type_unit DIE was added to the limbo die list when created.
22373 Since these have all been added to comdat_type_list, clear the
22374 limbo die list. */
22375 limbo_die_list = NULL;
22377 /* For each new comdat type unit, copy declarations for incomplete
22378 types to make the new unit self-contained (i.e., no direct
22379 references to the main compile unit). */
22380 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22381 copy_decls_for_unworthy_types (ctnode->root_die);
22382 copy_decls_for_unworthy_types (comp_unit_die ());
22384 /* In the process of copying declarations from one unit to another,
22385 we may have left some declarations behind that are no longer
22386 referenced. Prune them. */
22387 prune_unused_types ();
22390 /* Generate separate CUs for each of the include files we've seen.
22391 They will go into limbo_die_list. */
22392 if (flag_eliminate_dwarf2_dups)
22393 break_out_includes (comp_unit_die ());
22395 /* Traverse the DIE's and add add sibling attributes to those DIE's
22396 that have children. */
22397 add_sibling_attributes (comp_unit_die ());
22398 for (node = limbo_die_list; node; node = node->next)
22399 add_sibling_attributes (node->die);
22400 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22401 add_sibling_attributes (ctnode->root_die);
22403 /* Output a terminator label for the .text section. */
22404 switch_to_section (text_section);
22405 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
22406 if (cold_text_section)
22408 switch_to_section (cold_text_section);
22409 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
22412 /* We can only use the low/high_pc attributes if all of the code was
22413 in .text. */
22414 if (!have_multiple_function_sections
22415 || (dwarf_version < 3 && dwarf_strict))
22417 /* Don't add if the CU has no associated code. */
22418 if (text_section_used)
22419 add_AT_low_high_pc (comp_unit_die (), text_section_label,
22420 text_end_label);
22422 else
22424 unsigned fde_idx;
22425 dw_fde_ref fde;
22426 bool range_list_added = false;
22428 if (text_section_used)
22429 add_ranges_by_labels (comp_unit_die (), text_section_label,
22430 text_end_label, &range_list_added);
22431 if (cold_text_section_used)
22432 add_ranges_by_labels (comp_unit_die (), cold_text_section_label,
22433 cold_end_label, &range_list_added);
22435 FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, fde_idx, fde)
22437 if (DECL_IGNORED_P (fde->decl))
22438 continue;
22439 if (!fde->in_std_section)
22440 add_ranges_by_labels (comp_unit_die (), fde->dw_fde_begin,
22441 fde->dw_fde_end, &range_list_added);
22442 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
22443 add_ranges_by_labels (comp_unit_die (), fde->dw_fde_second_begin,
22444 fde->dw_fde_second_end, &range_list_added);
22447 if (range_list_added)
22449 /* We need to give .debug_loc and .debug_ranges an appropriate
22450 "base address". Use zero so that these addresses become
22451 absolute. Historically, we've emitted the unexpected
22452 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
22453 Emit both to give time for other tools to adapt. */
22454 add_AT_addr (comp_unit_die (), DW_AT_low_pc, const0_rtx);
22455 if (! dwarf_strict && dwarf_version < 4)
22456 add_AT_addr (comp_unit_die (), DW_AT_entry_pc, const0_rtx);
22458 add_ranges (NULL);
22462 if (debug_info_level >= DINFO_LEVEL_NORMAL)
22463 add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list,
22464 debug_line_section_label);
22466 if (have_macinfo)
22467 add_AT_macptr (comp_unit_die (),
22468 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
22469 macinfo_section_label);
22471 if (have_location_lists)
22472 optimize_location_lists (comp_unit_die ());
22474 /* Output all of the compilation units. We put the main one last so that
22475 the offsets are available to output_pubnames. */
22476 for (node = limbo_die_list; node; node = node->next)
22477 output_comp_unit (node->die, 0);
22479 comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
22480 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22482 void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
22484 /* Don't output duplicate types. */
22485 if (*slot != HTAB_EMPTY_ENTRY)
22486 continue;
22488 /* Add a pointer to the line table for the main compilation unit
22489 so that the debugger can make sense of DW_AT_decl_file
22490 attributes. */
22491 if (debug_info_level >= DINFO_LEVEL_NORMAL)
22492 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
22493 debug_line_section_label);
22495 output_comdat_type_unit (ctnode);
22496 *slot = ctnode;
22498 htab_delete (comdat_type_table);
22500 add_AT_pubnames (comp_unit_die ());
22502 /* Output the main compilation unit if non-empty or if .debug_macinfo
22503 or .debug_macro will be emitted. */
22504 output_comp_unit (comp_unit_die (), have_macinfo);
22506 /* Output the abbreviation table. */
22507 if (abbrev_die_table_in_use != 1)
22509 switch_to_section (debug_abbrev_section);
22510 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
22511 output_abbrev_section ();
22514 /* Output location list section if necessary. */
22515 if (have_location_lists)
22517 /* Output the location lists info. */
22518 switch_to_section (debug_loc_section);
22519 ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
22520 DEBUG_LOC_SECTION_LABEL, 0);
22521 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
22522 output_location_lists (comp_unit_die ());
22525 /* Output public names and types tables if necessary. */
22526 output_pubnames (pubname_table);
22527 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
22528 It shouldn't hurt to emit it always, since pure DWARF2 consumers
22529 simply won't look for the section. */
22530 output_pubnames (pubtype_table);
22532 /* Output the address range information if a CU (.debug_info section)
22533 was emitted. We output an empty table even if we had no functions
22534 to put in it. This because the consumer has no way to tell the
22535 difference between an empty table that we omitted and failure to
22536 generate a table that would have contained data. */
22537 if (info_section_emitted)
22539 unsigned long aranges_length = size_of_aranges ();
22541 switch_to_section (debug_aranges_section);
22542 output_aranges (aranges_length);
22545 /* Output ranges section if necessary. */
22546 if (ranges_table_in_use)
22548 switch_to_section (debug_ranges_section);
22549 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
22550 output_ranges ();
22553 /* Have to end the macro section. */
22554 if (have_macinfo)
22556 switch_to_section (debug_macinfo_section);
22557 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
22558 output_macinfo ();
22559 dw2_asm_output_data (1, 0, "End compilation unit");
22562 /* Output the source line correspondence table. We must do this
22563 even if there is no line information. Otherwise, on an empty
22564 translation unit, we will generate a present, but empty,
22565 .debug_info section. IRIX 6.5 `nm' will then complain when
22566 examining the file. This is done late so that any filenames
22567 used by the debug_info section are marked as 'used'. */
22568 switch_to_section (debug_line_section);
22569 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
22570 if (! DWARF2_ASM_LINE_DEBUG_INFO)
22571 output_line_info ();
22573 /* If we emitted any DW_FORM_strp form attribute, output the string
22574 table too. */
22575 if (debug_str_hash)
22576 htab_traverse (debug_str_hash, output_indirect_string, NULL);
22579 #include "gt-dwarf2out.h"