Merge trunk version 178795 into gupc branch.
[official-gcc.git] / gcc / dwarf2out.c
blob2f745e5db82b7f57e20bd519676c8e8ac6a0a6e8
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
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 "libfuncs.h"
76 #include "except.h"
77 #include "dwarf2.h"
78 #include "dwarf2out.h"
79 #include "dwarf2asm.h"
80 #include "toplev.h"
81 #include "ggc.h"
82 #include "md5.h"
83 #include "tm_p.h"
84 #include "diagnostic.h"
85 #include "tree-pretty-print.h"
86 #include "debug.h"
87 #include "target.h"
88 #include "common/common-target.h"
89 #include "langhooks.h"
90 #include "hashtab.h"
91 #include "cgraph.h"
92 #include "input.h"
93 #include "gimple.h"
94 #include "tree-pass.h"
95 #include "tree-flow.h"
96 #include "cfglayout.h"
97 #include "opts.h"
99 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
100 static rtx last_var_location_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");
881 #ifdef MIPS_DEBUGGING_INFO
882 /* Work around Irix 6 assembler bug whereby labels at the end of a section
883 get a value of 0. Putting .align 0 after the label fixes it. */
884 ASM_OUTPUT_ALIGN (asm_out_file, 0);
885 #endif
887 /* Turn off app to make assembly quicker. */
888 if (flag_debug_asm)
889 app_disable ();
892 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
894 static void
895 dwarf2out_do_cfi_startproc (bool second)
897 int enc;
898 rtx ref;
899 rtx personality = get_personality_function (current_function_decl);
901 fprintf (asm_out_file, "\t.cfi_startproc\n");
903 if (personality)
905 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
906 ref = personality;
908 /* ??? The GAS support isn't entirely consistent. We have to
909 handle indirect support ourselves, but PC-relative is done
910 in the assembler. Further, the assembler can't handle any
911 of the weirder relocation types. */
912 if (enc & DW_EH_PE_indirect)
913 ref = dw2_force_const_mem (ref, true);
915 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
916 output_addr_const (asm_out_file, ref);
917 fputc ('\n', asm_out_file);
920 if (crtl->uses_eh_lsda)
922 char lab[20];
924 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
925 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
926 current_function_funcdef_no);
927 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
928 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
930 if (enc & DW_EH_PE_indirect)
931 ref = dw2_force_const_mem (ref, true);
933 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
934 output_addr_const (asm_out_file, ref);
935 fputc ('\n', asm_out_file);
939 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
940 this allocation may be done before pass_final. */
942 dw_fde_ref
943 dwarf2out_alloc_current_fde (void)
945 dw_fde_ref fde;
947 fde = ggc_alloc_cleared_dw_fde_node ();
948 fde->decl = current_function_decl;
949 fde->funcdef_number = current_function_funcdef_no;
950 fde->fde_index = VEC_length (dw_fde_ref, fde_vec);
951 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
952 fde->uses_eh_lsda = crtl->uses_eh_lsda;
953 fde->nothrow = crtl->nothrow;
954 fde->drap_reg = INVALID_REGNUM;
955 fde->vdrap_reg = INVALID_REGNUM;
957 /* Record the FDE associated with this function. */
958 cfun->fde = fde;
959 VEC_safe_push (dw_fde_ref, gc, fde_vec, fde);
961 return fde;
964 /* Output a marker (i.e. a label) for the beginning of a function, before
965 the prologue. */
967 void
968 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
969 const char *file ATTRIBUTE_UNUSED)
971 char label[MAX_ARTIFICIAL_LABEL_BYTES];
972 char * dup_label;
973 dw_fde_ref fde;
974 section *fnsec;
975 bool do_frame;
977 current_function_func_begin_label = NULL;
979 do_frame = dwarf2out_do_frame ();
981 /* ??? current_function_func_begin_label is also used by except.c for
982 call-site information. We must emit this label if it might be used. */
983 if (!do_frame
984 && (!flag_exceptions
985 || targetm_common.except_unwind_info (&global_options) != UI_TARGET))
986 return;
988 fnsec = function_section (current_function_decl);
989 switch_to_section (fnsec);
990 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
991 current_function_funcdef_no);
992 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
993 current_function_funcdef_no);
994 dup_label = xstrdup (label);
995 current_function_func_begin_label = dup_label;
997 /* We can elide the fde allocation if we're not emitting debug info. */
998 if (!do_frame)
999 return;
1001 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1002 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1003 would include pass_dwarf2_frame. If we've not created the FDE yet,
1004 do so now. */
1005 fde = cfun->fde;
1006 if (fde == NULL)
1007 fde = dwarf2out_alloc_current_fde ();
1009 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1010 fde->dw_fde_begin = dup_label;
1011 fde->dw_fde_current_label = dup_label;
1012 fde->in_std_section = (fnsec == text_section
1013 || (cold_text_section && fnsec == cold_text_section));
1015 /* We only want to output line number information for the genuine dwarf2
1016 prologue case, not the eh frame case. */
1017 #ifdef DWARF2_DEBUGGING_INFO
1018 if (file)
1019 dwarf2out_source_line (line, file, 0, true);
1020 #endif
1022 if (dwarf2out_do_cfi_asm ())
1023 dwarf2out_do_cfi_startproc (false);
1024 else
1026 rtx personality = get_personality_function (current_function_decl);
1027 if (!current_unit_personality)
1028 current_unit_personality = personality;
1030 /* We cannot keep a current personality per function as without CFI
1031 asm, at the point where we emit the CFI data, there is no current
1032 function anymore. */
1033 if (personality && current_unit_personality != personality)
1034 sorry ("multiple EH personalities are supported only with assemblers "
1035 "supporting .cfi_personality directive");
1039 /* Output a marker (i.e. a label) for the end of the generated code
1040 for a function prologue. This gets called *after* the prologue code has
1041 been generated. */
1043 void
1044 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1045 const char *file ATTRIBUTE_UNUSED)
1047 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1049 /* Output a label to mark the endpoint of the code generated for this
1050 function. */
1051 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1052 current_function_funcdef_no);
1053 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1054 current_function_funcdef_no);
1055 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1058 /* Output a marker (i.e. a label) for the beginning of the generated code
1059 for a function epilogue. This gets called *before* the prologue code has
1060 been generated. */
1062 void
1063 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1064 const char *file ATTRIBUTE_UNUSED)
1066 dw_fde_ref fde = cfun->fde;
1067 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1069 if (fde->dw_fde_vms_begin_epilogue)
1070 return;
1072 /* Output a label to mark the endpoint of the code generated for this
1073 function. */
1074 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1075 current_function_funcdef_no);
1076 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1077 current_function_funcdef_no);
1078 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1081 /* Output a marker (i.e. a label) for the absolute end of the generated code
1082 for a function definition. This gets called *after* the epilogue code has
1083 been generated. */
1085 void
1086 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1087 const char *file ATTRIBUTE_UNUSED)
1089 dw_fde_ref fde;
1090 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1092 last_var_location_insn = NULL_RTX;
1094 if (dwarf2out_do_cfi_asm ())
1095 fprintf (asm_out_file, "\t.cfi_endproc\n");
1097 /* Output a label to mark the endpoint of the code generated for this
1098 function. */
1099 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1100 current_function_funcdef_no);
1101 ASM_OUTPUT_LABEL (asm_out_file, label);
1102 fde = cfun->fde;
1103 gcc_assert (fde != NULL);
1104 if (fde->dw_fde_second_begin == NULL)
1105 fde->dw_fde_end = xstrdup (label);
1108 void
1109 dwarf2out_frame_finish (void)
1111 /* Output call frame information. */
1112 if (targetm.debug_unwind_info () == UI_DWARF2)
1113 output_call_frame_info (0);
1115 /* Output another copy for the unwinder. */
1116 if ((flag_unwind_tables || flag_exceptions)
1117 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1118 output_call_frame_info (1);
1121 /* Note that the current function section is being used for code. */
1123 static void
1124 dwarf2out_note_section_used (void)
1126 section *sec = current_function_section ();
1127 if (sec == text_section)
1128 text_section_used = true;
1129 else if (sec == cold_text_section)
1130 cold_text_section_used = true;
1133 static void var_location_switch_text_section (void);
1134 static void set_cur_line_info_table (section *);
1136 void
1137 dwarf2out_switch_text_section (void)
1139 section *sect;
1140 dw_fde_ref fde = cfun->fde;
1142 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1144 if (!in_cold_section_p)
1146 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1147 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1148 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1150 else
1152 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1153 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1154 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1156 have_multiple_function_sections = true;
1158 /* There is no need to mark used sections when not debugging. */
1159 if (cold_text_section != NULL)
1160 dwarf2out_note_section_used ();
1162 if (dwarf2out_do_cfi_asm ())
1163 fprintf (asm_out_file, "\t.cfi_endproc\n");
1165 /* Now do the real section switch. */
1166 sect = current_function_section ();
1167 switch_to_section (sect);
1169 fde->second_in_std_section
1170 = (sect == text_section
1171 || (cold_text_section && sect == cold_text_section));
1173 if (dwarf2out_do_cfi_asm ())
1174 dwarf2out_do_cfi_startproc (true);
1176 var_location_switch_text_section ();
1178 set_cur_line_info_table (sect);
1181 /* And now, the subset of the debugging information support code necessary
1182 for emitting location expressions. */
1184 /* Data about a single source file. */
1185 struct GTY(()) dwarf_file_data {
1186 const char * filename;
1187 int emitted_number;
1190 typedef struct GTY(()) deferred_locations_struct
1192 tree variable;
1193 dw_die_ref die;
1194 } deferred_locations;
1196 DEF_VEC_O(deferred_locations);
1197 DEF_VEC_ALLOC_O(deferred_locations,gc);
1199 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
1201 DEF_VEC_P(dw_die_ref);
1202 DEF_VEC_ALLOC_P(dw_die_ref,heap);
1204 /* Location lists are ranges + location descriptions for that range,
1205 so you can track variables that are in different places over
1206 their entire life. */
1207 typedef struct GTY(()) dw_loc_list_struct {
1208 dw_loc_list_ref dw_loc_next;
1209 const char *begin; /* Label for begin address of range */
1210 const char *end; /* Label for end address of range */
1211 char *ll_symbol; /* Label for beginning of location list.
1212 Only on head of list */
1213 const char *section; /* Section this loclist is relative to */
1214 dw_loc_descr_ref expr;
1215 hashval_t hash;
1216 /* True if all addresses in this and subsequent lists are known to be
1217 resolved. */
1218 bool resolved_addr;
1219 /* True if this list has been replaced by dw_loc_next. */
1220 bool replaced;
1221 bool emitted;
1222 /* True if the range should be emitted even if begin and end
1223 are the same. */
1224 bool force;
1225 } dw_loc_list_node;
1227 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1229 /* Convert a DWARF stack opcode into its string name. */
1231 static const char *
1232 dwarf_stack_op_name (unsigned int op)
1234 switch (op)
1236 case DW_OP_addr:
1237 return "DW_OP_addr";
1238 case DW_OP_deref:
1239 return "DW_OP_deref";
1240 case DW_OP_const1u:
1241 return "DW_OP_const1u";
1242 case DW_OP_const1s:
1243 return "DW_OP_const1s";
1244 case DW_OP_const2u:
1245 return "DW_OP_const2u";
1246 case DW_OP_const2s:
1247 return "DW_OP_const2s";
1248 case DW_OP_const4u:
1249 return "DW_OP_const4u";
1250 case DW_OP_const4s:
1251 return "DW_OP_const4s";
1252 case DW_OP_const8u:
1253 return "DW_OP_const8u";
1254 case DW_OP_const8s:
1255 return "DW_OP_const8s";
1256 case DW_OP_constu:
1257 return "DW_OP_constu";
1258 case DW_OP_consts:
1259 return "DW_OP_consts";
1260 case DW_OP_dup:
1261 return "DW_OP_dup";
1262 case DW_OP_drop:
1263 return "DW_OP_drop";
1264 case DW_OP_over:
1265 return "DW_OP_over";
1266 case DW_OP_pick:
1267 return "DW_OP_pick";
1268 case DW_OP_swap:
1269 return "DW_OP_swap";
1270 case DW_OP_rot:
1271 return "DW_OP_rot";
1272 case DW_OP_xderef:
1273 return "DW_OP_xderef";
1274 case DW_OP_abs:
1275 return "DW_OP_abs";
1276 case DW_OP_and:
1277 return "DW_OP_and";
1278 case DW_OP_div:
1279 return "DW_OP_div";
1280 case DW_OP_minus:
1281 return "DW_OP_minus";
1282 case DW_OP_mod:
1283 return "DW_OP_mod";
1284 case DW_OP_mul:
1285 return "DW_OP_mul";
1286 case DW_OP_neg:
1287 return "DW_OP_neg";
1288 case DW_OP_not:
1289 return "DW_OP_not";
1290 case DW_OP_or:
1291 return "DW_OP_or";
1292 case DW_OP_plus:
1293 return "DW_OP_plus";
1294 case DW_OP_plus_uconst:
1295 return "DW_OP_plus_uconst";
1296 case DW_OP_shl:
1297 return "DW_OP_shl";
1298 case DW_OP_shr:
1299 return "DW_OP_shr";
1300 case DW_OP_shra:
1301 return "DW_OP_shra";
1302 case DW_OP_xor:
1303 return "DW_OP_xor";
1304 case DW_OP_bra:
1305 return "DW_OP_bra";
1306 case DW_OP_eq:
1307 return "DW_OP_eq";
1308 case DW_OP_ge:
1309 return "DW_OP_ge";
1310 case DW_OP_gt:
1311 return "DW_OP_gt";
1312 case DW_OP_le:
1313 return "DW_OP_le";
1314 case DW_OP_lt:
1315 return "DW_OP_lt";
1316 case DW_OP_ne:
1317 return "DW_OP_ne";
1318 case DW_OP_skip:
1319 return "DW_OP_skip";
1320 case DW_OP_lit0:
1321 return "DW_OP_lit0";
1322 case DW_OP_lit1:
1323 return "DW_OP_lit1";
1324 case DW_OP_lit2:
1325 return "DW_OP_lit2";
1326 case DW_OP_lit3:
1327 return "DW_OP_lit3";
1328 case DW_OP_lit4:
1329 return "DW_OP_lit4";
1330 case DW_OP_lit5:
1331 return "DW_OP_lit5";
1332 case DW_OP_lit6:
1333 return "DW_OP_lit6";
1334 case DW_OP_lit7:
1335 return "DW_OP_lit7";
1336 case DW_OP_lit8:
1337 return "DW_OP_lit8";
1338 case DW_OP_lit9:
1339 return "DW_OP_lit9";
1340 case DW_OP_lit10:
1341 return "DW_OP_lit10";
1342 case DW_OP_lit11:
1343 return "DW_OP_lit11";
1344 case DW_OP_lit12:
1345 return "DW_OP_lit12";
1346 case DW_OP_lit13:
1347 return "DW_OP_lit13";
1348 case DW_OP_lit14:
1349 return "DW_OP_lit14";
1350 case DW_OP_lit15:
1351 return "DW_OP_lit15";
1352 case DW_OP_lit16:
1353 return "DW_OP_lit16";
1354 case DW_OP_lit17:
1355 return "DW_OP_lit17";
1356 case DW_OP_lit18:
1357 return "DW_OP_lit18";
1358 case DW_OP_lit19:
1359 return "DW_OP_lit19";
1360 case DW_OP_lit20:
1361 return "DW_OP_lit20";
1362 case DW_OP_lit21:
1363 return "DW_OP_lit21";
1364 case DW_OP_lit22:
1365 return "DW_OP_lit22";
1366 case DW_OP_lit23:
1367 return "DW_OP_lit23";
1368 case DW_OP_lit24:
1369 return "DW_OP_lit24";
1370 case DW_OP_lit25:
1371 return "DW_OP_lit25";
1372 case DW_OP_lit26:
1373 return "DW_OP_lit26";
1374 case DW_OP_lit27:
1375 return "DW_OP_lit27";
1376 case DW_OP_lit28:
1377 return "DW_OP_lit28";
1378 case DW_OP_lit29:
1379 return "DW_OP_lit29";
1380 case DW_OP_lit30:
1381 return "DW_OP_lit30";
1382 case DW_OP_lit31:
1383 return "DW_OP_lit31";
1384 case DW_OP_reg0:
1385 return "DW_OP_reg0";
1386 case DW_OP_reg1:
1387 return "DW_OP_reg1";
1388 case DW_OP_reg2:
1389 return "DW_OP_reg2";
1390 case DW_OP_reg3:
1391 return "DW_OP_reg3";
1392 case DW_OP_reg4:
1393 return "DW_OP_reg4";
1394 case DW_OP_reg5:
1395 return "DW_OP_reg5";
1396 case DW_OP_reg6:
1397 return "DW_OP_reg6";
1398 case DW_OP_reg7:
1399 return "DW_OP_reg7";
1400 case DW_OP_reg8:
1401 return "DW_OP_reg8";
1402 case DW_OP_reg9:
1403 return "DW_OP_reg9";
1404 case DW_OP_reg10:
1405 return "DW_OP_reg10";
1406 case DW_OP_reg11:
1407 return "DW_OP_reg11";
1408 case DW_OP_reg12:
1409 return "DW_OP_reg12";
1410 case DW_OP_reg13:
1411 return "DW_OP_reg13";
1412 case DW_OP_reg14:
1413 return "DW_OP_reg14";
1414 case DW_OP_reg15:
1415 return "DW_OP_reg15";
1416 case DW_OP_reg16:
1417 return "DW_OP_reg16";
1418 case DW_OP_reg17:
1419 return "DW_OP_reg17";
1420 case DW_OP_reg18:
1421 return "DW_OP_reg18";
1422 case DW_OP_reg19:
1423 return "DW_OP_reg19";
1424 case DW_OP_reg20:
1425 return "DW_OP_reg20";
1426 case DW_OP_reg21:
1427 return "DW_OP_reg21";
1428 case DW_OP_reg22:
1429 return "DW_OP_reg22";
1430 case DW_OP_reg23:
1431 return "DW_OP_reg23";
1432 case DW_OP_reg24:
1433 return "DW_OP_reg24";
1434 case DW_OP_reg25:
1435 return "DW_OP_reg25";
1436 case DW_OP_reg26:
1437 return "DW_OP_reg26";
1438 case DW_OP_reg27:
1439 return "DW_OP_reg27";
1440 case DW_OP_reg28:
1441 return "DW_OP_reg28";
1442 case DW_OP_reg29:
1443 return "DW_OP_reg29";
1444 case DW_OP_reg30:
1445 return "DW_OP_reg30";
1446 case DW_OP_reg31:
1447 return "DW_OP_reg31";
1448 case DW_OP_breg0:
1449 return "DW_OP_breg0";
1450 case DW_OP_breg1:
1451 return "DW_OP_breg1";
1452 case DW_OP_breg2:
1453 return "DW_OP_breg2";
1454 case DW_OP_breg3:
1455 return "DW_OP_breg3";
1456 case DW_OP_breg4:
1457 return "DW_OP_breg4";
1458 case DW_OP_breg5:
1459 return "DW_OP_breg5";
1460 case DW_OP_breg6:
1461 return "DW_OP_breg6";
1462 case DW_OP_breg7:
1463 return "DW_OP_breg7";
1464 case DW_OP_breg8:
1465 return "DW_OP_breg8";
1466 case DW_OP_breg9:
1467 return "DW_OP_breg9";
1468 case DW_OP_breg10:
1469 return "DW_OP_breg10";
1470 case DW_OP_breg11:
1471 return "DW_OP_breg11";
1472 case DW_OP_breg12:
1473 return "DW_OP_breg12";
1474 case DW_OP_breg13:
1475 return "DW_OP_breg13";
1476 case DW_OP_breg14:
1477 return "DW_OP_breg14";
1478 case DW_OP_breg15:
1479 return "DW_OP_breg15";
1480 case DW_OP_breg16:
1481 return "DW_OP_breg16";
1482 case DW_OP_breg17:
1483 return "DW_OP_breg17";
1484 case DW_OP_breg18:
1485 return "DW_OP_breg18";
1486 case DW_OP_breg19:
1487 return "DW_OP_breg19";
1488 case DW_OP_breg20:
1489 return "DW_OP_breg20";
1490 case DW_OP_breg21:
1491 return "DW_OP_breg21";
1492 case DW_OP_breg22:
1493 return "DW_OP_breg22";
1494 case DW_OP_breg23:
1495 return "DW_OP_breg23";
1496 case DW_OP_breg24:
1497 return "DW_OP_breg24";
1498 case DW_OP_breg25:
1499 return "DW_OP_breg25";
1500 case DW_OP_breg26:
1501 return "DW_OP_breg26";
1502 case DW_OP_breg27:
1503 return "DW_OP_breg27";
1504 case DW_OP_breg28:
1505 return "DW_OP_breg28";
1506 case DW_OP_breg29:
1507 return "DW_OP_breg29";
1508 case DW_OP_breg30:
1509 return "DW_OP_breg30";
1510 case DW_OP_breg31:
1511 return "DW_OP_breg31";
1512 case DW_OP_regx:
1513 return "DW_OP_regx";
1514 case DW_OP_fbreg:
1515 return "DW_OP_fbreg";
1516 case DW_OP_bregx:
1517 return "DW_OP_bregx";
1518 case DW_OP_piece:
1519 return "DW_OP_piece";
1520 case DW_OP_deref_size:
1521 return "DW_OP_deref_size";
1522 case DW_OP_xderef_size:
1523 return "DW_OP_xderef_size";
1524 case DW_OP_nop:
1525 return "DW_OP_nop";
1527 case DW_OP_push_object_address:
1528 return "DW_OP_push_object_address";
1529 case DW_OP_call2:
1530 return "DW_OP_call2";
1531 case DW_OP_call4:
1532 return "DW_OP_call4";
1533 case DW_OP_call_ref:
1534 return "DW_OP_call_ref";
1535 case DW_OP_implicit_value:
1536 return "DW_OP_implicit_value";
1537 case DW_OP_stack_value:
1538 return "DW_OP_stack_value";
1539 case DW_OP_form_tls_address:
1540 return "DW_OP_form_tls_address";
1541 case DW_OP_call_frame_cfa:
1542 return "DW_OP_call_frame_cfa";
1543 case DW_OP_bit_piece:
1544 return "DW_OP_bit_piece";
1546 case DW_OP_GNU_push_tls_address:
1547 return "DW_OP_GNU_push_tls_address";
1548 case DW_OP_GNU_uninit:
1549 return "DW_OP_GNU_uninit";
1550 case DW_OP_GNU_encoded_addr:
1551 return "DW_OP_GNU_encoded_addr";
1552 case DW_OP_GNU_implicit_pointer:
1553 return "DW_OP_GNU_implicit_pointer";
1554 case DW_OP_GNU_entry_value:
1555 return "DW_OP_GNU_entry_value";
1556 case DW_OP_GNU_const_type:
1557 return "DW_OP_GNU_const_type";
1558 case DW_OP_GNU_regval_type:
1559 return "DW_OP_GNU_regval_type";
1560 case DW_OP_GNU_deref_type:
1561 return "DW_OP_GNU_deref_type";
1562 case DW_OP_GNU_convert:
1563 return "DW_OP_GNU_convert";
1564 case DW_OP_GNU_reinterpret:
1565 return "DW_OP_GNU_reinterpret";
1566 case DW_OP_GNU_parameter_ref:
1567 return "DW_OP_GNU_parameter_ref";
1569 default:
1570 return "OP_<unknown>";
1574 /* Return a pointer to a newly allocated location description. Location
1575 descriptions are simple expression terms that can be strung
1576 together to form more complicated location (address) descriptions. */
1578 static inline dw_loc_descr_ref
1579 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1580 unsigned HOST_WIDE_INT oprnd2)
1582 dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
1584 descr->dw_loc_opc = op;
1585 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1586 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1587 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1588 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1590 return descr;
1593 /* Return a pointer to a newly allocated location description for
1594 REG and OFFSET. */
1596 static inline dw_loc_descr_ref
1597 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1599 if (reg <= 31)
1600 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1601 offset, 0);
1602 else
1603 return new_loc_descr (DW_OP_bregx, reg, offset);
1606 /* Add a location description term to a location description expression. */
1608 static inline void
1609 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1611 dw_loc_descr_ref *d;
1613 /* Find the end of the chain. */
1614 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1617 *d = descr;
1620 /* Compare two location operands for exact equality. */
1622 static bool
1623 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1625 if (a->val_class != b->val_class)
1626 return false;
1627 switch (a->val_class)
1629 case dw_val_class_none:
1630 return true;
1631 case dw_val_class_addr:
1632 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1634 case dw_val_class_offset:
1635 case dw_val_class_unsigned_const:
1636 case dw_val_class_const:
1637 case dw_val_class_range_list:
1638 case dw_val_class_lineptr:
1639 case dw_val_class_macptr:
1640 /* These are all HOST_WIDE_INT, signed or unsigned. */
1641 return a->v.val_unsigned == b->v.val_unsigned;
1643 case dw_val_class_loc:
1644 return a->v.val_loc == b->v.val_loc;
1645 case dw_val_class_loc_list:
1646 return a->v.val_loc_list == b->v.val_loc_list;
1647 case dw_val_class_die_ref:
1648 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1649 case dw_val_class_fde_ref:
1650 return a->v.val_fde_index == b->v.val_fde_index;
1651 case dw_val_class_lbl_id:
1652 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1653 case dw_val_class_str:
1654 return a->v.val_str == b->v.val_str;
1655 case dw_val_class_flag:
1656 return a->v.val_flag == b->v.val_flag;
1657 case dw_val_class_file:
1658 return a->v.val_file == b->v.val_file;
1659 case dw_val_class_decl_ref:
1660 return a->v.val_decl_ref == b->v.val_decl_ref;
1662 case dw_val_class_const_double:
1663 return (a->v.val_double.high == b->v.val_double.high
1664 && a->v.val_double.low == b->v.val_double.low);
1666 case dw_val_class_vec:
1668 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1669 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1671 return (a_len == b_len
1672 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1675 case dw_val_class_data8:
1676 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1678 case dw_val_class_vms_delta:
1679 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1680 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1682 gcc_unreachable ();
1685 /* Compare two location atoms for exact equality. */
1687 static bool
1688 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1690 if (a->dw_loc_opc != b->dw_loc_opc)
1691 return false;
1693 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1694 address size, but since we always allocate cleared storage it
1695 should be zero for other types of locations. */
1696 if (a->dtprel != b->dtprel)
1697 return false;
1699 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1700 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1703 /* Compare two complete location expressions for exact equality. */
1705 bool
1706 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1708 while (1)
1710 if (a == b)
1711 return true;
1712 if (a == NULL || b == NULL)
1713 return false;
1714 if (!loc_descr_equal_p_1 (a, b))
1715 return false;
1717 a = a->dw_loc_next;
1718 b = b->dw_loc_next;
1723 /* Add a constant OFFSET to a location expression. */
1725 static void
1726 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1728 dw_loc_descr_ref loc;
1729 HOST_WIDE_INT *p;
1731 gcc_assert (*list_head != NULL);
1733 if (!offset)
1734 return;
1736 /* Find the end of the chain. */
1737 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1740 p = NULL;
1741 if (loc->dw_loc_opc == DW_OP_fbreg
1742 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1743 p = &loc->dw_loc_oprnd1.v.val_int;
1744 else if (loc->dw_loc_opc == DW_OP_bregx)
1745 p = &loc->dw_loc_oprnd2.v.val_int;
1747 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1748 offset. Don't optimize if an signed integer overflow would happen. */
1749 if (p != NULL
1750 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1751 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1752 *p += offset;
1754 else if (offset > 0)
1755 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1757 else
1759 loc->dw_loc_next = int_loc_descriptor (-offset);
1760 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1764 /* Add a constant OFFSET to a location list. */
1766 static void
1767 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1769 dw_loc_list_ref d;
1770 for (d = list_head; d != NULL; d = d->dw_loc_next)
1771 loc_descr_plus_const (&d->expr, offset);
1774 #define DWARF_REF_SIZE \
1775 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1777 static unsigned long int get_base_type_offset (dw_die_ref);
1779 /* Return the size of a location descriptor. */
1781 static unsigned long
1782 size_of_loc_descr (dw_loc_descr_ref loc)
1784 unsigned long size = 1;
1786 switch (loc->dw_loc_opc)
1788 case DW_OP_addr:
1789 size += DWARF2_ADDR_SIZE;
1790 break;
1791 case DW_OP_const1u:
1792 case DW_OP_const1s:
1793 size += 1;
1794 break;
1795 case DW_OP_const2u:
1796 case DW_OP_const2s:
1797 size += 2;
1798 break;
1799 case DW_OP_const4u:
1800 case DW_OP_const4s:
1801 size += 4;
1802 break;
1803 case DW_OP_const8u:
1804 case DW_OP_const8s:
1805 size += 8;
1806 break;
1807 case DW_OP_constu:
1808 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1809 break;
1810 case DW_OP_consts:
1811 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1812 break;
1813 case DW_OP_pick:
1814 size += 1;
1815 break;
1816 case DW_OP_plus_uconst:
1817 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1818 break;
1819 case DW_OP_skip:
1820 case DW_OP_bra:
1821 size += 2;
1822 break;
1823 case DW_OP_breg0:
1824 case DW_OP_breg1:
1825 case DW_OP_breg2:
1826 case DW_OP_breg3:
1827 case DW_OP_breg4:
1828 case DW_OP_breg5:
1829 case DW_OP_breg6:
1830 case DW_OP_breg7:
1831 case DW_OP_breg8:
1832 case DW_OP_breg9:
1833 case DW_OP_breg10:
1834 case DW_OP_breg11:
1835 case DW_OP_breg12:
1836 case DW_OP_breg13:
1837 case DW_OP_breg14:
1838 case DW_OP_breg15:
1839 case DW_OP_breg16:
1840 case DW_OP_breg17:
1841 case DW_OP_breg18:
1842 case DW_OP_breg19:
1843 case DW_OP_breg20:
1844 case DW_OP_breg21:
1845 case DW_OP_breg22:
1846 case DW_OP_breg23:
1847 case DW_OP_breg24:
1848 case DW_OP_breg25:
1849 case DW_OP_breg26:
1850 case DW_OP_breg27:
1851 case DW_OP_breg28:
1852 case DW_OP_breg29:
1853 case DW_OP_breg30:
1854 case DW_OP_breg31:
1855 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1856 break;
1857 case DW_OP_regx:
1858 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1859 break;
1860 case DW_OP_fbreg:
1861 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1862 break;
1863 case DW_OP_bregx:
1864 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1865 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1866 break;
1867 case DW_OP_piece:
1868 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1869 break;
1870 case DW_OP_bit_piece:
1871 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1872 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1873 break;
1874 case DW_OP_deref_size:
1875 case DW_OP_xderef_size:
1876 size += 1;
1877 break;
1878 case DW_OP_call2:
1879 size += 2;
1880 break;
1881 case DW_OP_call4:
1882 size += 4;
1883 break;
1884 case DW_OP_call_ref:
1885 size += DWARF_REF_SIZE;
1886 break;
1887 case DW_OP_implicit_value:
1888 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1889 + loc->dw_loc_oprnd1.v.val_unsigned;
1890 break;
1891 case DW_OP_GNU_implicit_pointer:
1892 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1893 break;
1894 case DW_OP_GNU_entry_value:
1896 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1897 size += size_of_uleb128 (op_size) + op_size;
1898 break;
1900 case DW_OP_GNU_const_type:
1902 unsigned long o
1903 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1904 size += size_of_uleb128 (o) + 1;
1905 switch (loc->dw_loc_oprnd2.val_class)
1907 case dw_val_class_vec:
1908 size += loc->dw_loc_oprnd2.v.val_vec.length
1909 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1910 break;
1911 case dw_val_class_const:
1912 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1913 break;
1914 case dw_val_class_const_double:
1915 size += 2 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1916 break;
1917 default:
1918 gcc_unreachable ();
1920 break;
1922 case DW_OP_GNU_regval_type:
1924 unsigned long o
1925 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1926 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1927 + size_of_uleb128 (o);
1929 break;
1930 case DW_OP_GNU_deref_type:
1932 unsigned long o
1933 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1934 size += 1 + size_of_uleb128 (o);
1936 break;
1937 case DW_OP_GNU_convert:
1938 case DW_OP_GNU_reinterpret:
1939 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1940 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1941 else
1943 unsigned long o
1944 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1945 size += size_of_uleb128 (o);
1947 break;
1948 case DW_OP_GNU_parameter_ref:
1949 size += 4;
1950 break;
1951 default:
1952 break;
1955 return size;
1958 /* Return the size of a series of location descriptors. */
1960 unsigned long
1961 size_of_locs (dw_loc_descr_ref loc)
1963 dw_loc_descr_ref l;
1964 unsigned long size;
1966 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1967 field, to avoid writing to a PCH file. */
1968 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1970 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1971 break;
1972 size += size_of_loc_descr (l);
1974 if (! l)
1975 return size;
1977 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1979 l->dw_loc_addr = size;
1980 size += size_of_loc_descr (l);
1983 return size;
1986 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1987 static void get_ref_die_offset_label (char *, dw_die_ref);
1988 static unsigned long int get_ref_die_offset (dw_die_ref);
1990 /* Output location description stack opcode's operands (if any).
1991 The for_eh_or_skip parameter controls whether register numbers are
1992 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1993 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1994 info). This should be suppressed for the cases that have not been converted
1995 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1997 static void
1998 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
2000 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2001 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2003 switch (loc->dw_loc_opc)
2005 #ifdef DWARF2_DEBUGGING_INFO
2006 case DW_OP_const2u:
2007 case DW_OP_const2s:
2008 dw2_asm_output_data (2, val1->v.val_int, NULL);
2009 break;
2010 case DW_OP_const4u:
2011 if (loc->dtprel)
2013 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
2014 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
2015 val1->v.val_addr);
2016 fputc ('\n', asm_out_file);
2017 break;
2019 /* FALLTHRU */
2020 case DW_OP_const4s:
2021 dw2_asm_output_data (4, val1->v.val_int, NULL);
2022 break;
2023 case DW_OP_const8u:
2024 if (loc->dtprel)
2026 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
2027 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
2028 val1->v.val_addr);
2029 fputc ('\n', asm_out_file);
2030 break;
2032 /* FALLTHRU */
2033 case DW_OP_const8s:
2034 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2035 dw2_asm_output_data (8, val1->v.val_int, NULL);
2036 break;
2037 case DW_OP_skip:
2038 case DW_OP_bra:
2040 int offset;
2042 gcc_assert (val1->val_class == dw_val_class_loc);
2043 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2045 dw2_asm_output_data (2, offset, NULL);
2047 break;
2048 case DW_OP_implicit_value:
2049 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2050 switch (val2->val_class)
2052 case dw_val_class_const:
2053 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
2054 break;
2055 case dw_val_class_vec:
2057 unsigned int elt_size = val2->v.val_vec.elt_size;
2058 unsigned int len = val2->v.val_vec.length;
2059 unsigned int i;
2060 unsigned char *p;
2062 if (elt_size > sizeof (HOST_WIDE_INT))
2064 elt_size /= 2;
2065 len *= 2;
2067 for (i = 0, p = val2->v.val_vec.array;
2068 i < len;
2069 i++, p += elt_size)
2070 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2071 "fp or vector constant word %u", i);
2073 break;
2074 case dw_val_class_const_double:
2076 unsigned HOST_WIDE_INT first, second;
2078 if (WORDS_BIG_ENDIAN)
2080 first = val2->v.val_double.high;
2081 second = val2->v.val_double.low;
2083 else
2085 first = val2->v.val_double.low;
2086 second = val2->v.val_double.high;
2088 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2089 first, NULL);
2090 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2091 second, NULL);
2093 break;
2094 case dw_val_class_addr:
2095 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
2096 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
2097 break;
2098 default:
2099 gcc_unreachable ();
2101 break;
2102 #else
2103 case DW_OP_const2u:
2104 case DW_OP_const2s:
2105 case DW_OP_const4u:
2106 case DW_OP_const4s:
2107 case DW_OP_const8u:
2108 case DW_OP_const8s:
2109 case DW_OP_skip:
2110 case DW_OP_bra:
2111 case DW_OP_implicit_value:
2112 /* We currently don't make any attempt to make sure these are
2113 aligned properly like we do for the main unwind info, so
2114 don't support emitting things larger than a byte if we're
2115 only doing unwinding. */
2116 gcc_unreachable ();
2117 #endif
2118 case DW_OP_const1u:
2119 case DW_OP_const1s:
2120 dw2_asm_output_data (1, val1->v.val_int, NULL);
2121 break;
2122 case DW_OP_constu:
2123 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2124 break;
2125 case DW_OP_consts:
2126 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2127 break;
2128 case DW_OP_pick:
2129 dw2_asm_output_data (1, val1->v.val_int, NULL);
2130 break;
2131 case DW_OP_plus_uconst:
2132 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2133 break;
2134 case DW_OP_breg0:
2135 case DW_OP_breg1:
2136 case DW_OP_breg2:
2137 case DW_OP_breg3:
2138 case DW_OP_breg4:
2139 case DW_OP_breg5:
2140 case DW_OP_breg6:
2141 case DW_OP_breg7:
2142 case DW_OP_breg8:
2143 case DW_OP_breg9:
2144 case DW_OP_breg10:
2145 case DW_OP_breg11:
2146 case DW_OP_breg12:
2147 case DW_OP_breg13:
2148 case DW_OP_breg14:
2149 case DW_OP_breg15:
2150 case DW_OP_breg16:
2151 case DW_OP_breg17:
2152 case DW_OP_breg18:
2153 case DW_OP_breg19:
2154 case DW_OP_breg20:
2155 case DW_OP_breg21:
2156 case DW_OP_breg22:
2157 case DW_OP_breg23:
2158 case DW_OP_breg24:
2159 case DW_OP_breg25:
2160 case DW_OP_breg26:
2161 case DW_OP_breg27:
2162 case DW_OP_breg28:
2163 case DW_OP_breg29:
2164 case DW_OP_breg30:
2165 case DW_OP_breg31:
2166 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2167 break;
2168 case DW_OP_regx:
2170 unsigned r = val1->v.val_unsigned;
2171 if (for_eh_or_skip >= 0)
2172 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2173 gcc_assert (size_of_uleb128 (r)
2174 == size_of_uleb128 (val1->v.val_unsigned));
2175 dw2_asm_output_data_uleb128 (r, NULL);
2177 break;
2178 case DW_OP_fbreg:
2179 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2180 break;
2181 case DW_OP_bregx:
2183 unsigned r = val1->v.val_unsigned;
2184 if (for_eh_or_skip >= 0)
2185 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2186 gcc_assert (size_of_uleb128 (r)
2187 == size_of_uleb128 (val1->v.val_unsigned));
2188 dw2_asm_output_data_uleb128 (r, NULL);
2189 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2191 break;
2192 case DW_OP_piece:
2193 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2194 break;
2195 case DW_OP_bit_piece:
2196 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2197 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
2198 break;
2199 case DW_OP_deref_size:
2200 case DW_OP_xderef_size:
2201 dw2_asm_output_data (1, val1->v.val_int, NULL);
2202 break;
2204 case DW_OP_addr:
2205 if (loc->dtprel)
2207 if (targetm.asm_out.output_dwarf_dtprel)
2209 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2210 DWARF2_ADDR_SIZE,
2211 val1->v.val_addr);
2212 fputc ('\n', asm_out_file);
2214 else
2215 gcc_unreachable ();
2217 else
2219 #ifdef DWARF2_DEBUGGING_INFO
2220 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2221 #else
2222 gcc_unreachable ();
2223 #endif
2225 break;
2227 case DW_OP_GNU_implicit_pointer:
2229 char label[MAX_ARTIFICIAL_LABEL_BYTES
2230 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2231 gcc_assert (val1->val_class == dw_val_class_die_ref);
2232 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2233 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2234 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2236 break;
2238 case DW_OP_GNU_entry_value:
2239 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2240 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2241 break;
2243 case DW_OP_GNU_const_type:
2245 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2246 gcc_assert (o);
2247 dw2_asm_output_data_uleb128 (o, NULL);
2248 switch (val2->val_class)
2250 case dw_val_class_const:
2251 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2252 dw2_asm_output_data (1, l, NULL);
2253 dw2_asm_output_data (l, val2->v.val_int, NULL);
2254 break;
2255 case dw_val_class_vec:
2257 unsigned int elt_size = val2->v.val_vec.elt_size;
2258 unsigned int len = val2->v.val_vec.length;
2259 unsigned int i;
2260 unsigned char *p;
2262 l = len * elt_size;
2263 dw2_asm_output_data (1, l, NULL);
2264 if (elt_size > sizeof (HOST_WIDE_INT))
2266 elt_size /= 2;
2267 len *= 2;
2269 for (i = 0, p = val2->v.val_vec.array;
2270 i < len;
2271 i++, p += elt_size)
2272 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2273 "fp or vector constant word %u", i);
2275 break;
2276 case dw_val_class_const_double:
2278 unsigned HOST_WIDE_INT first, second;
2279 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2281 dw2_asm_output_data (1, 2 * l, NULL);
2282 if (WORDS_BIG_ENDIAN)
2284 first = val2->v.val_double.high;
2285 second = val2->v.val_double.low;
2287 else
2289 first = val2->v.val_double.low;
2290 second = val2->v.val_double.high;
2292 dw2_asm_output_data (l, first, NULL);
2293 dw2_asm_output_data (l, second, NULL);
2295 break;
2296 default:
2297 gcc_unreachable ();
2300 break;
2301 case DW_OP_GNU_regval_type:
2303 unsigned r = val1->v.val_unsigned;
2304 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2305 gcc_assert (o);
2306 if (for_eh_or_skip >= 0)
2308 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2309 gcc_assert (size_of_uleb128 (r)
2310 == size_of_uleb128 (val1->v.val_unsigned));
2312 dw2_asm_output_data_uleb128 (r, NULL);
2313 dw2_asm_output_data_uleb128 (o, NULL);
2315 break;
2316 case DW_OP_GNU_deref_type:
2318 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2319 gcc_assert (o);
2320 dw2_asm_output_data (1, val1->v.val_int, NULL);
2321 dw2_asm_output_data_uleb128 (o, NULL);
2323 break;
2324 case DW_OP_GNU_convert:
2325 case DW_OP_GNU_reinterpret:
2326 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2327 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2328 else
2330 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2331 gcc_assert (o);
2332 dw2_asm_output_data_uleb128 (o, NULL);
2334 break;
2336 case DW_OP_GNU_parameter_ref:
2338 unsigned long o;
2339 gcc_assert (val1->val_class == dw_val_class_die_ref);
2340 o = get_ref_die_offset (val1->v.val_die_ref.die);
2341 dw2_asm_output_data (4, o, NULL);
2343 break;
2345 default:
2346 /* Other codes have no operands. */
2347 break;
2351 /* Output a sequence of location operations.
2352 The for_eh_or_skip parameter controls whether register numbers are
2353 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2354 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2355 info). This should be suppressed for the cases that have not been converted
2356 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2358 void
2359 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2361 for (; loc != NULL; loc = loc->dw_loc_next)
2363 enum dwarf_location_atom opc = loc->dw_loc_opc;
2364 /* Output the opcode. */
2365 if (for_eh_or_skip >= 0
2366 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2368 unsigned r = (opc - DW_OP_breg0);
2369 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2370 gcc_assert (r <= 31);
2371 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2373 else if (for_eh_or_skip >= 0
2374 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2376 unsigned r = (opc - DW_OP_reg0);
2377 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2378 gcc_assert (r <= 31);
2379 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2382 dw2_asm_output_data (1, opc,
2383 "%s", dwarf_stack_op_name (opc));
2385 /* Output the operand(s) (if any). */
2386 output_loc_operands (loc, for_eh_or_skip);
2390 /* Output location description stack opcode's operands (if any).
2391 The output is single bytes on a line, suitable for .cfi_escape. */
2393 static void
2394 output_loc_operands_raw (dw_loc_descr_ref loc)
2396 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2397 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2399 switch (loc->dw_loc_opc)
2401 case DW_OP_addr:
2402 case DW_OP_implicit_value:
2403 /* We cannot output addresses in .cfi_escape, only bytes. */
2404 gcc_unreachable ();
2406 case DW_OP_const1u:
2407 case DW_OP_const1s:
2408 case DW_OP_pick:
2409 case DW_OP_deref_size:
2410 case DW_OP_xderef_size:
2411 fputc (',', asm_out_file);
2412 dw2_asm_output_data_raw (1, val1->v.val_int);
2413 break;
2415 case DW_OP_const2u:
2416 case DW_OP_const2s:
2417 fputc (',', asm_out_file);
2418 dw2_asm_output_data_raw (2, val1->v.val_int);
2419 break;
2421 case DW_OP_const4u:
2422 case DW_OP_const4s:
2423 fputc (',', asm_out_file);
2424 dw2_asm_output_data_raw (4, val1->v.val_int);
2425 break;
2427 case DW_OP_const8u:
2428 case DW_OP_const8s:
2429 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2430 fputc (',', asm_out_file);
2431 dw2_asm_output_data_raw (8, val1->v.val_int);
2432 break;
2434 case DW_OP_skip:
2435 case DW_OP_bra:
2437 int offset;
2439 gcc_assert (val1->val_class == dw_val_class_loc);
2440 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2442 fputc (',', asm_out_file);
2443 dw2_asm_output_data_raw (2, offset);
2445 break;
2447 case DW_OP_regx:
2449 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2450 gcc_assert (size_of_uleb128 (r)
2451 == size_of_uleb128 (val1->v.val_unsigned));
2452 fputc (',', asm_out_file);
2453 dw2_asm_output_data_uleb128_raw (r);
2455 break;
2457 case DW_OP_constu:
2458 case DW_OP_plus_uconst:
2459 case DW_OP_piece:
2460 fputc (',', asm_out_file);
2461 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2462 break;
2464 case DW_OP_bit_piece:
2465 fputc (',', asm_out_file);
2466 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2467 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2468 break;
2470 case DW_OP_consts:
2471 case DW_OP_breg0:
2472 case DW_OP_breg1:
2473 case DW_OP_breg2:
2474 case DW_OP_breg3:
2475 case DW_OP_breg4:
2476 case DW_OP_breg5:
2477 case DW_OP_breg6:
2478 case DW_OP_breg7:
2479 case DW_OP_breg8:
2480 case DW_OP_breg9:
2481 case DW_OP_breg10:
2482 case DW_OP_breg11:
2483 case DW_OP_breg12:
2484 case DW_OP_breg13:
2485 case DW_OP_breg14:
2486 case DW_OP_breg15:
2487 case DW_OP_breg16:
2488 case DW_OP_breg17:
2489 case DW_OP_breg18:
2490 case DW_OP_breg19:
2491 case DW_OP_breg20:
2492 case DW_OP_breg21:
2493 case DW_OP_breg22:
2494 case DW_OP_breg23:
2495 case DW_OP_breg24:
2496 case DW_OP_breg25:
2497 case DW_OP_breg26:
2498 case DW_OP_breg27:
2499 case DW_OP_breg28:
2500 case DW_OP_breg29:
2501 case DW_OP_breg30:
2502 case DW_OP_breg31:
2503 case DW_OP_fbreg:
2504 fputc (',', asm_out_file);
2505 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2506 break;
2508 case DW_OP_bregx:
2510 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2511 gcc_assert (size_of_uleb128 (r)
2512 == size_of_uleb128 (val1->v.val_unsigned));
2513 fputc (',', asm_out_file);
2514 dw2_asm_output_data_uleb128_raw (r);
2515 fputc (',', asm_out_file);
2516 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2518 break;
2520 case DW_OP_GNU_implicit_pointer:
2521 case DW_OP_GNU_entry_value:
2522 case DW_OP_GNU_const_type:
2523 case DW_OP_GNU_regval_type:
2524 case DW_OP_GNU_deref_type:
2525 case DW_OP_GNU_convert:
2526 case DW_OP_GNU_reinterpret:
2527 case DW_OP_GNU_parameter_ref:
2528 gcc_unreachable ();
2529 break;
2531 default:
2532 /* Other codes have no operands. */
2533 break;
2537 void
2538 output_loc_sequence_raw (dw_loc_descr_ref loc)
2540 while (1)
2542 enum dwarf_location_atom opc = loc->dw_loc_opc;
2543 /* Output the opcode. */
2544 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2546 unsigned r = (opc - DW_OP_breg0);
2547 r = DWARF2_FRAME_REG_OUT (r, 1);
2548 gcc_assert (r <= 31);
2549 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2551 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2553 unsigned r = (opc - DW_OP_reg0);
2554 r = DWARF2_FRAME_REG_OUT (r, 1);
2555 gcc_assert (r <= 31);
2556 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2558 /* Output the opcode. */
2559 fprintf (asm_out_file, "%#x", opc);
2560 output_loc_operands_raw (loc);
2562 if (!loc->dw_loc_next)
2563 break;
2564 loc = loc->dw_loc_next;
2566 fputc (',', asm_out_file);
2570 /* This function builds a dwarf location descriptor sequence from a
2571 dw_cfa_location, adding the given OFFSET to the result of the
2572 expression. */
2574 struct dw_loc_descr_struct *
2575 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2577 struct dw_loc_descr_struct *head, *tmp;
2579 offset += cfa->offset;
2581 if (cfa->indirect)
2583 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2584 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2585 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2586 add_loc_descr (&head, tmp);
2587 if (offset != 0)
2589 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2590 add_loc_descr (&head, tmp);
2593 else
2594 head = new_reg_loc_descr (cfa->reg, offset);
2596 return head;
2599 /* This function builds a dwarf location descriptor sequence for
2600 the address at OFFSET from the CFA when stack is aligned to
2601 ALIGNMENT byte. */
2603 struct dw_loc_descr_struct *
2604 build_cfa_aligned_loc (dw_cfa_location *cfa,
2605 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2607 struct dw_loc_descr_struct *head;
2608 unsigned int dwarf_fp
2609 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2611 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2612 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2614 head = new_reg_loc_descr (dwarf_fp, 0);
2615 add_loc_descr (&head, int_loc_descriptor (alignment));
2616 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2617 loc_descr_plus_const (&head, offset);
2619 else
2620 head = new_reg_loc_descr (dwarf_fp, offset);
2621 return head;
2624 /* And now, the support for symbolic debugging information. */
2626 /* .debug_str support. */
2627 static int output_indirect_string (void **, void *);
2629 static void dwarf2out_init (const char *);
2630 static void dwarf2out_finish (const char *);
2631 static void dwarf2out_assembly_start (void);
2632 static void dwarf2out_define (unsigned int, const char *);
2633 static void dwarf2out_undef (unsigned int, const char *);
2634 static void dwarf2out_start_source_file (unsigned, const char *);
2635 static void dwarf2out_end_source_file (unsigned);
2636 static void dwarf2out_function_decl (tree);
2637 static void dwarf2out_begin_block (unsigned, unsigned);
2638 static void dwarf2out_end_block (unsigned, unsigned);
2639 static bool dwarf2out_ignore_block (const_tree);
2640 static void dwarf2out_global_decl (tree);
2641 static void dwarf2out_type_decl (tree, int);
2642 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2643 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2644 dw_die_ref);
2645 static void dwarf2out_abstract_function (tree);
2646 static void dwarf2out_var_location (rtx);
2647 static void dwarf2out_begin_function (tree);
2648 static void dwarf2out_set_name (tree, tree);
2650 /* The debug hooks structure. */
2652 const struct gcc_debug_hooks dwarf2_debug_hooks =
2654 dwarf2out_init,
2655 dwarf2out_finish,
2656 dwarf2out_assembly_start,
2657 dwarf2out_define,
2658 dwarf2out_undef,
2659 dwarf2out_start_source_file,
2660 dwarf2out_end_source_file,
2661 dwarf2out_begin_block,
2662 dwarf2out_end_block,
2663 dwarf2out_ignore_block,
2664 dwarf2out_source_line,
2665 dwarf2out_begin_prologue,
2666 #if VMS_DEBUGGING_INFO
2667 dwarf2out_vms_end_prologue,
2668 dwarf2out_vms_begin_epilogue,
2669 #else
2670 debug_nothing_int_charstar,
2671 debug_nothing_int_charstar,
2672 #endif
2673 dwarf2out_end_epilogue,
2674 dwarf2out_begin_function,
2675 debug_nothing_int, /* end_function */
2676 dwarf2out_function_decl, /* function_decl */
2677 dwarf2out_global_decl,
2678 dwarf2out_type_decl, /* type_decl */
2679 dwarf2out_imported_module_or_decl,
2680 debug_nothing_tree, /* deferred_inline_function */
2681 /* The DWARF 2 backend tries to reduce debugging bloat by not
2682 emitting the abstract description of inline functions until
2683 something tries to reference them. */
2684 dwarf2out_abstract_function, /* outlining_inline_function */
2685 debug_nothing_rtx, /* label */
2686 debug_nothing_int, /* handle_pch */
2687 dwarf2out_var_location,
2688 dwarf2out_switch_text_section,
2689 dwarf2out_set_name,
2690 1, /* start_end_main_source_file */
2691 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2694 /* NOTE: In the comments in this file, many references are made to
2695 "Debugging Information Entries". This term is abbreviated as `DIE'
2696 throughout the remainder of this file. */
2698 /* An internal representation of the DWARF output is built, and then
2699 walked to generate the DWARF debugging info. The walk of the internal
2700 representation is done after the entire program has been compiled.
2701 The types below are used to describe the internal representation. */
2703 /* Whether to put type DIEs into their own section .debug_types instead
2704 of making them part of the .debug_info section. Only supported for
2705 Dwarf V4 or higher and the user didn't disable them through
2706 -fno-debug-types-section. It is more efficient to put them in a
2707 separate comdat sections since the linker will then be able to
2708 remove duplicates. But not all tools support .debug_types sections
2709 yet. */
2711 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2713 /* Various DIE's use offsets relative to the beginning of the
2714 .debug_info section to refer to each other. */
2716 typedef long int dw_offset;
2718 /* Define typedefs here to avoid circular dependencies. */
2720 typedef struct dw_attr_struct *dw_attr_ref;
2721 typedef struct dw_line_info_struct *dw_line_info_ref;
2722 typedef struct pubname_struct *pubname_ref;
2723 typedef struct dw_ranges_struct *dw_ranges_ref;
2724 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
2725 typedef struct comdat_type_struct *comdat_type_node_ref;
2727 /* The entries in the line_info table more-or-less mirror the opcodes
2728 that are used in the real dwarf line table. Arrays of these entries
2729 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2730 supported. */
2732 enum dw_line_info_opcode {
2733 /* Emit DW_LNE_set_address; the operand is the label index. */
2734 LI_set_address,
2736 /* Emit a row to the matrix with the given line. This may be done
2737 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2738 special opcodes. */
2739 LI_set_line,
2741 /* Emit a DW_LNS_set_file. */
2742 LI_set_file,
2744 /* Emit a DW_LNS_set_column. */
2745 LI_set_column,
2747 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2748 LI_negate_stmt,
2750 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2751 LI_set_prologue_end,
2752 LI_set_epilogue_begin,
2754 /* Emit a DW_LNE_set_discriminator. */
2755 LI_set_discriminator
2758 typedef struct GTY(()) dw_line_info_struct {
2759 enum dw_line_info_opcode opcode;
2760 unsigned int val;
2761 } dw_line_info_entry;
2763 DEF_VEC_O(dw_line_info_entry);
2764 DEF_VEC_ALLOC_O(dw_line_info_entry, gc);
2766 typedef struct GTY(()) dw_line_info_table_struct {
2767 /* The label that marks the end of this section. */
2768 const char *end_label;
2770 /* The values for the last row of the matrix, as collected in the table.
2771 These are used to minimize the changes to the next row. */
2772 unsigned int file_num;
2773 unsigned int line_num;
2774 unsigned int column_num;
2775 int discrim_num;
2776 bool is_stmt;
2777 bool in_use;
2779 VEC(dw_line_info_entry, gc) *entries;
2780 } dw_line_info_table;
2782 typedef dw_line_info_table *dw_line_info_table_p;
2784 DEF_VEC_P(dw_line_info_table_p);
2785 DEF_VEC_ALLOC_P(dw_line_info_table_p, gc);
2787 /* Each DIE attribute has a field specifying the attribute kind,
2788 a link to the next attribute in the chain, and an attribute value.
2789 Attributes are typically linked below the DIE they modify. */
2791 typedef struct GTY(()) dw_attr_struct {
2792 enum dwarf_attribute dw_attr;
2793 dw_val_node dw_attr_val;
2795 dw_attr_node;
2797 DEF_VEC_O(dw_attr_node);
2798 DEF_VEC_ALLOC_O(dw_attr_node,gc);
2800 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2801 The children of each node form a circular list linked by
2802 die_sib. die_child points to the node *before* the "first" child node. */
2804 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
2805 union die_symbol_or_type_node
2807 char * GTY ((tag ("0"))) die_symbol;
2808 comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
2810 GTY ((desc ("use_debug_types"))) die_id;
2811 VEC(dw_attr_node,gc) * die_attr;
2812 dw_die_ref die_parent;
2813 dw_die_ref die_child;
2814 dw_die_ref die_sib;
2815 dw_die_ref die_definition; /* ref from a specification to its definition */
2816 dw_offset die_offset;
2817 unsigned long die_abbrev;
2818 int die_mark;
2819 /* Die is used and must not be pruned as unused. */
2820 int die_perennial_p;
2821 unsigned int decl_id;
2822 enum dwarf_tag die_tag;
2824 die_node;
2826 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2827 #define FOR_EACH_CHILD(die, c, expr) do { \
2828 c = die->die_child; \
2829 if (c) do { \
2830 c = c->die_sib; \
2831 expr; \
2832 } while (c != die->die_child); \
2833 } while (0)
2835 /* The pubname structure */
2837 typedef struct GTY(()) pubname_struct {
2838 dw_die_ref die;
2839 const char *name;
2841 pubname_entry;
2843 DEF_VEC_O(pubname_entry);
2844 DEF_VEC_ALLOC_O(pubname_entry, gc);
2846 struct GTY(()) dw_ranges_struct {
2847 /* If this is positive, it's a block number, otherwise it's a
2848 bitwise-negated index into dw_ranges_by_label. */
2849 int num;
2852 /* A structure to hold a macinfo entry. */
2854 typedef struct GTY(()) macinfo_struct {
2855 unsigned char code;
2856 unsigned HOST_WIDE_INT lineno;
2857 const char *info;
2859 macinfo_entry;
2861 DEF_VEC_O(macinfo_entry);
2862 DEF_VEC_ALLOC_O(macinfo_entry, gc);
2864 struct GTY(()) dw_ranges_by_label_struct {
2865 const char *begin;
2866 const char *end;
2869 /* The comdat type node structure. */
2870 typedef struct GTY(()) comdat_type_struct
2872 dw_die_ref root_die;
2873 dw_die_ref type_die;
2874 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2875 struct comdat_type_struct *next;
2877 comdat_type_node;
2879 /* The limbo die list structure. */
2880 typedef struct GTY(()) limbo_die_struct {
2881 dw_die_ref die;
2882 tree created_for;
2883 struct limbo_die_struct *next;
2885 limbo_die_node;
2887 typedef struct skeleton_chain_struct
2889 dw_die_ref old_die;
2890 dw_die_ref new_die;
2891 struct skeleton_chain_struct *parent;
2893 skeleton_chain_node;
2895 /* Define a macro which returns nonzero for a TYPE_DECL which was
2896 implicitly generated for a type.
2898 Note that, unlike the C front-end (which generates a NULL named
2899 TYPE_DECL node for each complete tagged type, each array type,
2900 and each function type node created) the C++ front-end generates
2901 a _named_ TYPE_DECL node for each tagged type node created.
2902 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2903 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2904 front-end, but for each type, tagged or not. */
2906 #define TYPE_DECL_IS_STUB(decl) \
2907 (DECL_NAME (decl) == NULL_TREE \
2908 || (DECL_ARTIFICIAL (decl) \
2909 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
2910 /* This is necessary for stub decls that \
2911 appear in nested inline functions. */ \
2912 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2913 && (decl_ultimate_origin (decl) \
2914 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2916 /* Information concerning the compilation unit's programming
2917 language, and compiler version. */
2919 /* Fixed size portion of the DWARF compilation unit header. */
2920 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2921 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2923 /* Fixed size portion of the DWARF comdat type unit header. */
2924 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2925 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2926 + DWARF_OFFSET_SIZE)
2928 /* Fixed size portion of public names info. */
2929 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2931 /* Fixed size portion of the address range info. */
2932 #define DWARF_ARANGES_HEADER_SIZE \
2933 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2934 DWARF2_ADDR_SIZE * 2) \
2935 - DWARF_INITIAL_LENGTH_SIZE)
2937 /* Size of padding portion in the address range info. It must be
2938 aligned to twice the pointer size. */
2939 #define DWARF_ARANGES_PAD_SIZE \
2940 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2941 DWARF2_ADDR_SIZE * 2) \
2942 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2944 /* Use assembler line directives if available. */
2945 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2946 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2947 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2948 #else
2949 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2950 #endif
2951 #endif
2953 /* Minimum line offset in a special line info. opcode.
2954 This value was chosen to give a reasonable range of values. */
2955 #define DWARF_LINE_BASE -10
2957 /* First special line opcode - leave room for the standard opcodes. */
2958 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
2960 /* Range of line offsets in a special line info. opcode. */
2961 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
2963 /* Flag that indicates the initial value of the is_stmt_start flag.
2964 In the present implementation, we do not mark any lines as
2965 the beginning of a source statement, because that information
2966 is not made available by the GCC front-end. */
2967 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2969 /* Maximum number of operations per instruction bundle. */
2970 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2971 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2972 #endif
2974 /* This location is used by calc_die_sizes() to keep track
2975 the offset of each DIE within the .debug_info section. */
2976 static unsigned long next_die_offset;
2978 /* Record the root of the DIE's built for the current compilation unit. */
2979 static GTY(()) dw_die_ref single_comp_unit_die;
2981 /* A list of type DIEs that have been separated into comdat sections. */
2982 static GTY(()) comdat_type_node *comdat_type_list;
2984 /* A list of DIEs with a NULL parent waiting to be relocated. */
2985 static GTY(()) limbo_die_node *limbo_die_list;
2987 /* A list of DIEs for which we may have to generate
2988 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
2989 static GTY(()) limbo_die_node *deferred_asm_name;
2991 /* Filenames referenced by this compilation unit. */
2992 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
2994 /* A hash table of references to DIE's that describe declarations.
2995 The key is a DECL_UID() which is a unique number identifying each decl. */
2996 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
2998 /* A hash table of references to DIE's that describe COMMON blocks.
2999 The key is DECL_UID() ^ die_parent. */
3000 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
3002 typedef struct GTY(()) die_arg_entry_struct {
3003 dw_die_ref die;
3004 tree arg;
3005 } die_arg_entry;
3007 DEF_VEC_O(die_arg_entry);
3008 DEF_VEC_ALLOC_O(die_arg_entry,gc);
3010 /* Node of the variable location list. */
3011 struct GTY ((chain_next ("%h.next"))) var_loc_node {
3012 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
3013 EXPR_LIST chain. For small bitsizes, bitsize is encoded
3014 in mode of the EXPR_LIST node and first EXPR_LIST operand
3015 is either NOTE_INSN_VAR_LOCATION for a piece with a known
3016 location or NULL for padding. For larger bitsizes,
3017 mode is 0 and first operand is a CONCAT with bitsize
3018 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
3019 NULL as second operand. */
3020 rtx GTY (()) loc;
3021 const char * GTY (()) label;
3022 struct var_loc_node * GTY (()) next;
3025 /* Variable location list. */
3026 struct GTY (()) var_loc_list_def {
3027 struct var_loc_node * GTY (()) first;
3029 /* Pointer to the last but one or last element of the
3030 chained list. If the list is empty, both first and
3031 last are NULL, if the list contains just one node
3032 or the last node certainly is not redundant, it points
3033 to the last node, otherwise points to the last but one.
3034 Do not mark it for GC because it is marked through the chain. */
3035 struct var_loc_node * GTY ((skip ("%h"))) last;
3037 /* Pointer to the last element before section switch,
3038 if NULL, either sections weren't switched or first
3039 is after section switch. */
3040 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
3042 /* DECL_UID of the variable decl. */
3043 unsigned int decl_id;
3045 typedef struct var_loc_list_def var_loc_list;
3047 /* Call argument location list. */
3048 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
3049 rtx GTY (()) call_arg_loc_note;
3050 const char * GTY (()) label;
3051 tree GTY (()) block;
3052 bool tail_call_p;
3053 rtx GTY (()) symbol_ref;
3054 struct call_arg_loc_node * GTY (()) next;
3058 /* Table of decl location linked lists. */
3059 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
3061 /* Head and tail of call_arg_loc chain. */
3062 static GTY (()) struct call_arg_loc_node *call_arg_locations;
3063 static struct call_arg_loc_node *call_arg_loc_last;
3065 /* Number of call sites in the current function. */
3066 static int call_site_count = -1;
3067 /* Number of tail call sites in the current function. */
3068 static int tail_call_site_count = -1;
3070 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
3071 DIEs. */
3072 static VEC (dw_die_ref, heap) *block_map;
3074 /* A cached location list. */
3075 struct GTY (()) cached_dw_loc_list_def {
3076 /* The DECL_UID of the decl that this entry describes. */
3077 unsigned int decl_id;
3079 /* The cached location list. */
3080 dw_loc_list_ref loc_list;
3082 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
3084 /* Table of cached location lists. */
3085 static GTY ((param_is (cached_dw_loc_list))) htab_t cached_dw_loc_list_table;
3087 /* A pointer to the base of a list of references to DIE's that
3088 are uniquely identified by their tag, presence/absence of
3089 children DIE's, and list of attribute/value pairs. */
3090 static GTY((length ("abbrev_die_table_allocated")))
3091 dw_die_ref *abbrev_die_table;
3093 /* Number of elements currently allocated for abbrev_die_table. */
3094 static GTY(()) unsigned abbrev_die_table_allocated;
3096 /* Number of elements in type_die_table currently in use. */
3097 static GTY(()) unsigned abbrev_die_table_in_use;
3099 /* Size (in elements) of increments by which we may expand the
3100 abbrev_die_table. */
3101 #define ABBREV_DIE_TABLE_INCREMENT 256
3103 /* A global counter for generating labels for line number data. */
3104 static unsigned int line_info_label_num;
3106 /* The current table to which we should emit line number information
3107 for the current function. This will be set up at the beginning of
3108 assembly for the function. */
3109 static dw_line_info_table *cur_line_info_table;
3111 /* The two default tables of line number info. */
3112 static GTY(()) dw_line_info_table *text_section_line_info;
3113 static GTY(()) dw_line_info_table *cold_text_section_line_info;
3115 /* The set of all non-default tables of line number info. */
3116 static GTY(()) VEC (dw_line_info_table_p, gc) *separate_line_info;
3118 /* A flag to tell pubnames/types export if there is an info section to
3119 refer to. */
3120 static bool info_section_emitted;
3122 /* A pointer to the base of a table that contains a list of publicly
3123 accessible names. */
3124 static GTY (()) VEC (pubname_entry, gc) * pubname_table;
3126 /* A pointer to the base of a table that contains a list of publicly
3127 accessible types. */
3128 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
3130 /* A pointer to the base of a table that contains a list of macro
3131 defines/undefines (and file start/end markers). */
3132 static GTY (()) VEC (macinfo_entry, gc) * macinfo_table;
3134 /* Array of dies for which we should generate .debug_ranges info. */
3135 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
3137 /* Number of elements currently allocated for ranges_table. */
3138 static GTY(()) unsigned ranges_table_allocated;
3140 /* Number of elements in ranges_table currently in use. */
3141 static GTY(()) unsigned ranges_table_in_use;
3143 /* Array of pairs of labels referenced in ranges_table. */
3144 static GTY ((length ("ranges_by_label_allocated")))
3145 dw_ranges_by_label_ref ranges_by_label;
3147 /* Number of elements currently allocated for ranges_by_label. */
3148 static GTY(()) unsigned ranges_by_label_allocated;
3150 /* Number of elements in ranges_by_label currently in use. */
3151 static GTY(()) unsigned ranges_by_label_in_use;
3153 /* Size (in elements) of increments by which we may expand the
3154 ranges_table. */
3155 #define RANGES_TABLE_INCREMENT 64
3157 /* Whether we have location lists that need outputting */
3158 static GTY(()) bool have_location_lists;
3160 /* Unique label counter. */
3161 static GTY(()) unsigned int loclabel_num;
3163 /* Unique label counter for point-of-call tables. */
3164 static GTY(()) unsigned int poc_label_num;
3166 /* Record whether the function being analyzed contains inlined functions. */
3167 static int current_function_has_inlines;
3169 /* The last file entry emitted by maybe_emit_file(). */
3170 static GTY(()) struct dwarf_file_data * last_emitted_file;
3172 /* Number of internal labels generated by gen_internal_sym(). */
3173 static GTY(()) int label_num;
3175 /* Cached result of previous call to lookup_filename. */
3176 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
3178 static GTY(()) VEC(die_arg_entry,gc) *tmpl_value_parm_die_table;
3180 /* Instances of generic types for which we need to generate debug
3181 info that describe their generic parameters and arguments. That
3182 generation needs to happen once all types are properly laid out so
3183 we do it at the end of compilation. */
3184 static GTY(()) VEC(tree,gc) *generic_type_instances;
3186 /* Offset from the "steady-state frame pointer" to the frame base,
3187 within the current function. */
3188 static HOST_WIDE_INT frame_pointer_fb_offset;
3189 static bool frame_pointer_fb_offset_valid;
3191 static VEC (dw_die_ref, heap) *base_types;
3193 /* Forward declarations for functions defined in this file. */
3195 static int is_pseudo_reg (const_rtx);
3196 static int is_tagged_type (const_tree);
3197 static const char *dwarf_tag_name (unsigned);
3198 static const char *dwarf_attr_name (unsigned);
3199 static const char *dwarf_form_name (unsigned);
3200 static tree decl_ultimate_origin (const_tree);
3201 static tree decl_class_context (tree);
3202 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
3203 static inline enum dw_val_class AT_class (dw_attr_ref);
3204 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3205 static inline unsigned AT_flag (dw_attr_ref);
3206 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3207 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
3208 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3209 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
3210 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3211 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3212 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3213 unsigned int, unsigned char *);
3214 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3215 static hashval_t debug_str_do_hash (const void *);
3216 static int debug_str_eq (const void *, const void *);
3217 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3218 static inline const char *AT_string (dw_attr_ref);
3219 static enum dwarf_form AT_string_form (dw_attr_ref);
3220 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3221 static void add_AT_specification (dw_die_ref, dw_die_ref);
3222 static inline dw_die_ref AT_ref (dw_attr_ref);
3223 static inline int AT_ref_external (dw_attr_ref);
3224 static inline void set_AT_ref_external (dw_attr_ref, int);
3225 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3226 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3227 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
3228 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3229 dw_loc_list_ref);
3230 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
3231 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
3232 static inline rtx AT_addr (dw_attr_ref);
3233 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3234 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3235 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3236 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3237 unsigned HOST_WIDE_INT);
3238 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3239 unsigned long);
3240 static inline const char *AT_lbl (dw_attr_ref);
3241 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
3242 static const char *get_AT_low_pc (dw_die_ref);
3243 static const char *get_AT_hi_pc (dw_die_ref);
3244 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3245 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3246 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3247 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3248 static bool is_cxx (void);
3249 static bool is_fortran (void);
3250 static bool is_ada (void);
3251 static void remove_AT (dw_die_ref, enum dwarf_attribute);
3252 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3253 static void add_child_die (dw_die_ref, dw_die_ref);
3254 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3255 static dw_die_ref lookup_type_die (tree);
3256 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3257 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3258 static void equate_type_number_to_die (tree, dw_die_ref);
3259 static hashval_t decl_die_table_hash (const void *);
3260 static int decl_die_table_eq (const void *, const void *);
3261 static dw_die_ref lookup_decl_die (tree);
3262 static hashval_t common_block_die_table_hash (const void *);
3263 static int common_block_die_table_eq (const void *, const void *);
3264 static hashval_t decl_loc_table_hash (const void *);
3265 static int decl_loc_table_eq (const void *, const void *);
3266 static var_loc_list *lookup_decl_loc (const_tree);
3267 static void equate_decl_number_to_die (tree, dw_die_ref);
3268 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3269 static void print_spaces (FILE *);
3270 static void print_die (dw_die_ref, FILE *);
3271 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3272 static dw_die_ref pop_compile_unit (dw_die_ref);
3273 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3274 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3275 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3276 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3277 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3278 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3279 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
3280 struct md5_ctx *, int *);
3281 struct checksum_attributes;
3282 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3283 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3284 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3285 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3286 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3287 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3288 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3289 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3290 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3291 static void compute_section_prefix (dw_die_ref);
3292 static int is_type_die (dw_die_ref);
3293 static int is_comdat_die (dw_die_ref);
3294 static int is_symbol_die (dw_die_ref);
3295 static void assign_symbol_names (dw_die_ref);
3296 static void break_out_includes (dw_die_ref);
3297 static int is_declaration_die (dw_die_ref);
3298 static int should_move_die_to_comdat (dw_die_ref);
3299 static dw_die_ref clone_as_declaration (dw_die_ref);
3300 static dw_die_ref clone_die (dw_die_ref);
3301 static dw_die_ref clone_tree (dw_die_ref);
3302 static void copy_declaration_context (dw_die_ref, dw_die_ref);
3303 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3304 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3305 static dw_die_ref generate_skeleton (dw_die_ref);
3306 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3307 dw_die_ref);
3308 static void break_out_comdat_types (dw_die_ref);
3309 static dw_die_ref copy_ancestor_tree (dw_die_ref, dw_die_ref, htab_t);
3310 static void copy_decls_walk (dw_die_ref, dw_die_ref, htab_t);
3311 static void copy_decls_for_unworthy_types (dw_die_ref);
3313 static hashval_t htab_cu_hash (const void *);
3314 static int htab_cu_eq (const void *, const void *);
3315 static void htab_cu_del (void *);
3316 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
3317 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
3318 static void add_sibling_attributes (dw_die_ref);
3319 static void build_abbrev_table (dw_die_ref);
3320 static void output_location_lists (dw_die_ref);
3321 static int constant_size (unsigned HOST_WIDE_INT);
3322 static unsigned long size_of_die (dw_die_ref);
3323 static void calc_die_sizes (dw_die_ref);
3324 static void calc_base_type_die_sizes (void);
3325 static void mark_dies (dw_die_ref);
3326 static void unmark_dies (dw_die_ref);
3327 static void unmark_all_dies (dw_die_ref);
3328 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
3329 static unsigned long size_of_aranges (void);
3330 static enum dwarf_form value_format (dw_attr_ref);
3331 static void output_value_format (dw_attr_ref);
3332 static void output_abbrev_section (void);
3333 static void output_die_symbol (dw_die_ref);
3334 static void output_die (dw_die_ref);
3335 static void output_compilation_unit_header (void);
3336 static void output_comp_unit (dw_die_ref, int);
3337 static void output_comdat_type_unit (comdat_type_node *);
3338 static const char *dwarf2_name (tree, int);
3339 static void add_pubname (tree, dw_die_ref);
3340 static void add_pubname_string (const char *, dw_die_ref);
3341 static void add_pubtype (tree, dw_die_ref);
3342 static void output_pubnames (VEC (pubname_entry,gc) *);
3343 static void output_aranges (unsigned long);
3344 static unsigned int add_ranges_num (int);
3345 static unsigned int add_ranges (const_tree);
3346 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3347 bool *);
3348 static void output_ranges (void);
3349 static dw_line_info_table *new_line_info_table (void);
3350 static void output_line_info (void);
3351 static void output_file_names (void);
3352 static dw_die_ref base_type_die (tree);
3353 static int is_base_type (tree);
3354 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3355 static dw_die_ref modified_type_die (tree, int, dw_die_ref);
3356 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3357 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3358 static int type_is_enum (const_tree);
3359 static unsigned int dbx_reg_number (const_rtx);
3360 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3361 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3362 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3363 enum var_init_status);
3364 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3365 enum var_init_status);
3366 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3367 enum var_init_status);
3368 static int is_based_loc (const_rtx);
3369 static int resolve_one_addr (rtx *, void *);
3370 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3371 enum var_init_status);
3372 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
3373 enum var_init_status);
3374 static dw_loc_list_ref loc_list_from_tree (tree, int);
3375 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
3376 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3377 static tree field_type (const_tree);
3378 static unsigned int simple_type_align_in_bits (const_tree);
3379 static unsigned int simple_decl_align_in_bits (const_tree);
3380 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3381 static HOST_WIDE_INT field_byte_offset (const_tree);
3382 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3383 dw_loc_list_ref);
3384 static void add_data_member_location_attribute (dw_die_ref, tree);
3385 static bool add_const_value_attribute (dw_die_ref, rtx);
3386 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3387 static void insert_double (double_int, unsigned char *);
3388 static void insert_float (const_rtx, unsigned char *);
3389 static rtx rtl_for_decl_location (tree);
3390 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3391 enum dwarf_attribute);
3392 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3393 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3394 static void add_name_attribute (dw_die_ref, const char *);
3395 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3396 static void add_comp_dir_attribute (dw_die_ref);
3397 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
3398 static void add_subscript_info (dw_die_ref, tree, bool);
3399 static void add_byte_size_attribute (dw_die_ref, tree);
3400 static void add_bit_offset_attribute (dw_die_ref, tree);
3401 static void add_bit_size_attribute (dw_die_ref, tree);
3402 static void add_prototyped_attribute (dw_die_ref, tree);
3403 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3404 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3405 static void add_src_coords_attributes (dw_die_ref, tree);
3406 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3407 static void push_decl_scope (tree);
3408 static void pop_decl_scope (void);
3409 static dw_die_ref scope_die_for (tree, dw_die_ref);
3410 static inline int local_scope_p (dw_die_ref);
3411 static inline int class_scope_p (dw_die_ref);
3412 static inline int class_or_namespace_scope_p (dw_die_ref);
3413 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
3414 static void add_calling_convention_attribute (dw_die_ref, tree);
3415 static const char *type_tag (const_tree);
3416 static tree member_declared_type (const_tree);
3417 #if 0
3418 static const char *decl_start_label (tree);
3419 #endif
3420 static void gen_array_type_die (tree, dw_die_ref);
3421 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3422 #if 0
3423 static void gen_entry_point_die (tree, dw_die_ref);
3424 #endif
3425 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3426 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3427 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3428 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3429 static void gen_formal_types_die (tree, dw_die_ref);
3430 static void gen_subprogram_die (tree, dw_die_ref);
3431 static void gen_variable_die (tree, tree, dw_die_ref);
3432 static void gen_const_die (tree, dw_die_ref);
3433 static void gen_label_die (tree, dw_die_ref);
3434 static void gen_lexical_block_die (tree, dw_die_ref, int);
3435 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
3436 static void gen_field_die (tree, dw_die_ref);
3437 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3438 static dw_die_ref gen_compile_unit_die (const char *);
3439 static void gen_inheritance_die (tree, tree, dw_die_ref);
3440 static void gen_member_die (tree, dw_die_ref);
3441 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3442 enum debug_info_usage);
3443 static void gen_subroutine_type_die (tree, dw_die_ref);
3444 static void gen_typedef_die (tree, dw_die_ref);
3445 static void gen_type_die (tree, dw_die_ref);
3446 static void gen_block_die (tree, dw_die_ref, int);
3447 static void decls_for_scope (tree, dw_die_ref, int);
3448 static inline int is_redundant_typedef (const_tree);
3449 static bool is_naming_typedef_decl (const_tree);
3450 static inline dw_die_ref get_context_die (tree);
3451 static void gen_namespace_die (tree, dw_die_ref);
3452 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3453 static dw_die_ref force_decl_die (tree);
3454 static dw_die_ref force_type_die (tree);
3455 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3456 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3457 static struct dwarf_file_data * lookup_filename (const char *);
3458 static void retry_incomplete_types (void);
3459 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3460 static void gen_generic_params_dies (tree);
3461 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3462 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3463 static void splice_child_die (dw_die_ref, dw_die_ref);
3464 static int file_info_cmp (const void *, const void *);
3465 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3466 const char *, const char *);
3467 static void output_loc_list (dw_loc_list_ref);
3468 static char *gen_internal_sym (const char *);
3470 static void prune_unmark_dies (dw_die_ref);
3471 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3472 static void prune_unused_types_mark (dw_die_ref, int);
3473 static void prune_unused_types_walk (dw_die_ref);
3474 static void prune_unused_types_walk_attribs (dw_die_ref);
3475 static void prune_unused_types_prune (dw_die_ref);
3476 static void prune_unused_types (void);
3477 static int maybe_emit_file (struct dwarf_file_data *fd);
3478 static inline const char *AT_vms_delta1 (dw_attr_ref);
3479 static inline const char *AT_vms_delta2 (dw_attr_ref);
3480 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3481 const char *, const char *);
3482 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3483 static void gen_remaining_tmpl_value_param_die_attribute (void);
3484 static bool generic_type_p (tree);
3485 static void schedule_generic_params_dies_gen (tree t);
3486 static void gen_scheduled_generic_parms_dies (void);
3488 /* Section names used to hold DWARF debugging information. */
3489 #ifndef DEBUG_INFO_SECTION
3490 #define DEBUG_INFO_SECTION ".debug_info"
3491 #endif
3492 #ifndef DEBUG_ABBREV_SECTION
3493 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3494 #endif
3495 #ifndef DEBUG_ARANGES_SECTION
3496 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3497 #endif
3498 #ifndef DEBUG_MACINFO_SECTION
3499 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
3500 #endif
3501 #ifndef DEBUG_MACRO_SECTION
3502 #define DEBUG_MACRO_SECTION ".debug_macro"
3503 #endif
3504 #ifndef DEBUG_LINE_SECTION
3505 #define DEBUG_LINE_SECTION ".debug_line"
3506 #endif
3507 #ifndef DEBUG_LOC_SECTION
3508 #define DEBUG_LOC_SECTION ".debug_loc"
3509 #endif
3510 #ifndef DEBUG_PUBNAMES_SECTION
3511 #define DEBUG_PUBNAMES_SECTION ".debug_pubnames"
3512 #endif
3513 #ifndef DEBUG_PUBTYPES_SECTION
3514 #define DEBUG_PUBTYPES_SECTION ".debug_pubtypes"
3515 #endif
3516 #ifndef DEBUG_STR_SECTION
3517 #define DEBUG_STR_SECTION ".debug_str"
3518 #endif
3519 #ifndef DEBUG_RANGES_SECTION
3520 #define DEBUG_RANGES_SECTION ".debug_ranges"
3521 #endif
3523 /* Standard ELF section names for compiled code and data. */
3524 #ifndef TEXT_SECTION_NAME
3525 #define TEXT_SECTION_NAME ".text"
3526 #endif
3528 /* Section flags for .debug_str section. */
3529 #define DEBUG_STR_SECTION_FLAGS \
3530 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3531 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3532 : SECTION_DEBUG)
3534 /* Labels we insert at beginning sections we can reference instead of
3535 the section names themselves. */
3537 #ifndef TEXT_SECTION_LABEL
3538 #define TEXT_SECTION_LABEL "Ltext"
3539 #endif
3540 #ifndef COLD_TEXT_SECTION_LABEL
3541 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3542 #endif
3543 #ifndef DEBUG_LINE_SECTION_LABEL
3544 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3545 #endif
3546 #ifndef DEBUG_INFO_SECTION_LABEL
3547 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3548 #endif
3549 #ifndef DEBUG_ABBREV_SECTION_LABEL
3550 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3551 #endif
3552 #ifndef DEBUG_LOC_SECTION_LABEL
3553 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3554 #endif
3555 #ifndef DEBUG_RANGES_SECTION_LABEL
3556 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3557 #endif
3558 #ifndef DEBUG_MACINFO_SECTION_LABEL
3559 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3560 #endif
3561 #ifndef DEBUG_MACRO_SECTION_LABEL
3562 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3563 #endif
3566 /* Definitions of defaults for formats and names of various special
3567 (artificial) labels which may be generated within this file (when the -g
3568 options is used and DWARF2_DEBUGGING_INFO is in effect.
3569 If necessary, these may be overridden from within the tm.h file, but
3570 typically, overriding these defaults is unnecessary. */
3572 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3573 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3574 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3575 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3576 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3577 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3578 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3579 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3580 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3581 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3583 #ifndef TEXT_END_LABEL
3584 #define TEXT_END_LABEL "Letext"
3585 #endif
3586 #ifndef COLD_END_LABEL
3587 #define COLD_END_LABEL "Letext_cold"
3588 #endif
3589 #ifndef BLOCK_BEGIN_LABEL
3590 #define BLOCK_BEGIN_LABEL "LBB"
3591 #endif
3592 #ifndef BLOCK_END_LABEL
3593 #define BLOCK_END_LABEL "LBE"
3594 #endif
3595 #ifndef LINE_CODE_LABEL
3596 #define LINE_CODE_LABEL "LM"
3597 #endif
3600 /* Return the root of the DIE's built for the current compilation unit. */
3601 static dw_die_ref
3602 comp_unit_die (void)
3604 if (!single_comp_unit_die)
3605 single_comp_unit_die = gen_compile_unit_die (NULL);
3606 return single_comp_unit_die;
3609 /* We allow a language front-end to designate a function that is to be
3610 called to "demangle" any name before it is put into a DIE. */
3612 static const char *(*demangle_name_func) (const char *);
3614 void
3615 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3617 demangle_name_func = func;
3620 /* Test if rtl node points to a pseudo register. */
3622 static inline int
3623 is_pseudo_reg (const_rtx rtl)
3625 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3626 || (GET_CODE (rtl) == SUBREG
3627 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3630 /* Return a reference to a type, with its const and volatile qualifiers
3631 removed. */
3633 static inline tree
3634 type_main_variant (tree type)
3636 type = TYPE_MAIN_VARIANT (type);
3638 /* ??? There really should be only one main variant among any group of
3639 variants of a given type (and all of the MAIN_VARIANT values for all
3640 members of the group should point to that one type) but sometimes the C
3641 front-end messes this up for array types, so we work around that bug
3642 here. */
3643 if (TREE_CODE (type) == ARRAY_TYPE)
3644 while (type != TYPE_MAIN_VARIANT (type))
3645 type = TYPE_MAIN_VARIANT (type);
3647 return type;
3650 /* Return nonzero if the given type node represents a tagged type. */
3652 static inline int
3653 is_tagged_type (const_tree type)
3655 enum tree_code code = TREE_CODE (type);
3657 return (code == RECORD_TYPE || code == UNION_TYPE
3658 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3661 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3663 static void
3664 get_ref_die_offset_label (char *label, dw_die_ref ref)
3666 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3669 /* Return die_offset of a DIE reference to a base type. */
3671 static unsigned long int
3672 get_base_type_offset (dw_die_ref ref)
3674 if (ref->die_offset)
3675 return ref->die_offset;
3676 if (comp_unit_die ()->die_abbrev)
3678 calc_base_type_die_sizes ();
3679 gcc_assert (ref->die_offset);
3681 return ref->die_offset;
3684 /* Return die_offset of a DIE reference other than base type. */
3686 static unsigned long int
3687 get_ref_die_offset (dw_die_ref ref)
3689 gcc_assert (ref->die_offset);
3690 return ref->die_offset;
3693 /* Convert a DIE tag into its string name. */
3695 static const char *
3696 dwarf_tag_name (unsigned int tag)
3698 switch (tag)
3700 case DW_TAG_padding:
3701 return "DW_TAG_padding";
3702 case DW_TAG_array_type:
3703 return "DW_TAG_array_type";
3704 case DW_TAG_class_type:
3705 return "DW_TAG_class_type";
3706 case DW_TAG_entry_point:
3707 return "DW_TAG_entry_point";
3708 case DW_TAG_enumeration_type:
3709 return "DW_TAG_enumeration_type";
3710 case DW_TAG_formal_parameter:
3711 return "DW_TAG_formal_parameter";
3712 case DW_TAG_imported_declaration:
3713 return "DW_TAG_imported_declaration";
3714 case DW_TAG_label:
3715 return "DW_TAG_label";
3716 case DW_TAG_lexical_block:
3717 return "DW_TAG_lexical_block";
3718 case DW_TAG_member:
3719 return "DW_TAG_member";
3720 case DW_TAG_pointer_type:
3721 return "DW_TAG_pointer_type";
3722 case DW_TAG_reference_type:
3723 return "DW_TAG_reference_type";
3724 case DW_TAG_compile_unit:
3725 return "DW_TAG_compile_unit";
3726 case DW_TAG_string_type:
3727 return "DW_TAG_string_type";
3728 case DW_TAG_structure_type:
3729 return "DW_TAG_structure_type";
3730 case DW_TAG_subroutine_type:
3731 return "DW_TAG_subroutine_type";
3732 case DW_TAG_typedef:
3733 return "DW_TAG_typedef";
3734 case DW_TAG_union_type:
3735 return "DW_TAG_union_type";
3736 case DW_TAG_unspecified_parameters:
3737 return "DW_TAG_unspecified_parameters";
3738 case DW_TAG_variant:
3739 return "DW_TAG_variant";
3740 case DW_TAG_common_block:
3741 return "DW_TAG_common_block";
3742 case DW_TAG_common_inclusion:
3743 return "DW_TAG_common_inclusion";
3744 case DW_TAG_inheritance:
3745 return "DW_TAG_inheritance";
3746 case DW_TAG_inlined_subroutine:
3747 return "DW_TAG_inlined_subroutine";
3748 case DW_TAG_module:
3749 return "DW_TAG_module";
3750 case DW_TAG_ptr_to_member_type:
3751 return "DW_TAG_ptr_to_member_type";
3752 case DW_TAG_set_type:
3753 return "DW_TAG_set_type";
3754 case DW_TAG_subrange_type:
3755 return "DW_TAG_subrange_type";
3756 case DW_TAG_with_stmt:
3757 return "DW_TAG_with_stmt";
3758 case DW_TAG_access_declaration:
3759 return "DW_TAG_access_declaration";
3760 case DW_TAG_base_type:
3761 return "DW_TAG_base_type";
3762 case DW_TAG_catch_block:
3763 return "DW_TAG_catch_block";
3764 case DW_TAG_const_type:
3765 return "DW_TAG_const_type";
3766 case DW_TAG_constant:
3767 return "DW_TAG_constant";
3768 case DW_TAG_enumerator:
3769 return "DW_TAG_enumerator";
3770 case DW_TAG_file_type:
3771 return "DW_TAG_file_type";
3772 case DW_TAG_friend:
3773 return "DW_TAG_friend";
3774 case DW_TAG_namelist:
3775 return "DW_TAG_namelist";
3776 case DW_TAG_namelist_item:
3777 return "DW_TAG_namelist_item";
3778 case DW_TAG_packed_type:
3779 return "DW_TAG_packed_type";
3780 case DW_TAG_subprogram:
3781 return "DW_TAG_subprogram";
3782 case DW_TAG_template_type_param:
3783 return "DW_TAG_template_type_param";
3784 case DW_TAG_template_value_param:
3785 return "DW_TAG_template_value_param";
3786 case DW_TAG_thrown_type:
3787 return "DW_TAG_thrown_type";
3788 case DW_TAG_try_block:
3789 return "DW_TAG_try_block";
3790 case DW_TAG_variant_part:
3791 return "DW_TAG_variant_part";
3792 case DW_TAG_variable:
3793 return "DW_TAG_variable";
3794 case DW_TAG_volatile_type:
3795 return "DW_TAG_volatile_type";
3796 case DW_TAG_dwarf_procedure:
3797 return "DW_TAG_dwarf_procedure";
3798 case DW_TAG_restrict_type:
3799 return "DW_TAG_restrict_type";
3800 case DW_TAG_interface_type:
3801 return "DW_TAG_interface_type";
3802 case DW_TAG_namespace:
3803 return "DW_TAG_namespace";
3804 case DW_TAG_imported_module:
3805 return "DW_TAG_imported_module";
3806 case DW_TAG_unspecified_type:
3807 return "DW_TAG_unspecified_type";
3808 case DW_TAG_partial_unit:
3809 return "DW_TAG_partial_unit";
3810 case DW_TAG_imported_unit:
3811 return "DW_TAG_imported_unit";
3812 case DW_TAG_condition:
3813 return "DW_TAG_condition";
3814 case DW_TAG_shared_type:
3815 return "DW_TAG_shared_type";
3816 case DW_TAG_type_unit:
3817 return "DW_TAG_type_unit";
3818 case DW_TAG_rvalue_reference_type:
3819 return "DW_TAG_rvalue_reference_type";
3820 case DW_TAG_template_alias:
3821 return "DW_TAG_template_alias";
3822 case DW_TAG_GNU_template_parameter_pack:
3823 return "DW_TAG_GNU_template_parameter_pack";
3824 case DW_TAG_GNU_formal_parameter_pack:
3825 return "DW_TAG_GNU_formal_parameter_pack";
3826 case DW_TAG_MIPS_loop:
3827 return "DW_TAG_MIPS_loop";
3828 case DW_TAG_format_label:
3829 return "DW_TAG_format_label";
3830 case DW_TAG_function_template:
3831 return "DW_TAG_function_template";
3832 case DW_TAG_class_template:
3833 return "DW_TAG_class_template";
3834 case DW_TAG_GNU_BINCL:
3835 return "DW_TAG_GNU_BINCL";
3836 case DW_TAG_GNU_EINCL:
3837 return "DW_TAG_GNU_EINCL";
3838 case DW_TAG_upc_shared_type:
3839 return "DW_TAG_upc_shared_type";
3840 case DW_TAG_upc_relaxed_type:
3841 return "DW_TAG_upc_relaxed_type";
3842 case DW_TAG_upc_strict_type:
3843 return "DW_TAG_upc_strict_type";
3844 case DW_TAG_GNU_template_template_param:
3845 return "DW_TAG_GNU_template_template_param";
3846 case DW_TAG_GNU_call_site:
3847 return "DW_TAG_GNU_call_site";
3848 case DW_TAG_GNU_call_site_parameter:
3849 return "DW_TAG_GNU_call_site_parameter";
3850 default:
3851 return "DW_TAG_<unknown>";
3855 /* Convert a DWARF attribute code into its string name. */
3857 static const char *
3858 dwarf_attr_name (unsigned int attr)
3860 switch (attr)
3862 case DW_AT_sibling:
3863 return "DW_AT_sibling";
3864 case DW_AT_location:
3865 return "DW_AT_location";
3866 case DW_AT_name:
3867 return "DW_AT_name";
3868 case DW_AT_ordering:
3869 return "DW_AT_ordering";
3870 case DW_AT_subscr_data:
3871 return "DW_AT_subscr_data";
3872 case DW_AT_byte_size:
3873 return "DW_AT_byte_size";
3874 case DW_AT_bit_offset:
3875 return "DW_AT_bit_offset";
3876 case DW_AT_bit_size:
3877 return "DW_AT_bit_size";
3878 case DW_AT_element_list:
3879 return "DW_AT_element_list";
3880 case DW_AT_stmt_list:
3881 return "DW_AT_stmt_list";
3882 case DW_AT_low_pc:
3883 return "DW_AT_low_pc";
3884 case DW_AT_high_pc:
3885 return "DW_AT_high_pc";
3886 case DW_AT_language:
3887 return "DW_AT_language";
3888 case DW_AT_member:
3889 return "DW_AT_member";
3890 case DW_AT_discr:
3891 return "DW_AT_discr";
3892 case DW_AT_discr_value:
3893 return "DW_AT_discr_value";
3894 case DW_AT_visibility:
3895 return "DW_AT_visibility";
3896 case DW_AT_import:
3897 return "DW_AT_import";
3898 case DW_AT_string_length:
3899 return "DW_AT_string_length";
3900 case DW_AT_common_reference:
3901 return "DW_AT_common_reference";
3902 case DW_AT_comp_dir:
3903 return "DW_AT_comp_dir";
3904 case DW_AT_const_value:
3905 return "DW_AT_const_value";
3906 case DW_AT_containing_type:
3907 return "DW_AT_containing_type";
3908 case DW_AT_default_value:
3909 return "DW_AT_default_value";
3910 case DW_AT_inline:
3911 return "DW_AT_inline";
3912 case DW_AT_is_optional:
3913 return "DW_AT_is_optional";
3914 case DW_AT_lower_bound:
3915 return "DW_AT_lower_bound";
3916 case DW_AT_producer:
3917 return "DW_AT_producer";
3918 case DW_AT_prototyped:
3919 return "DW_AT_prototyped";
3920 case DW_AT_return_addr:
3921 return "DW_AT_return_addr";
3922 case DW_AT_start_scope:
3923 return "DW_AT_start_scope";
3924 case DW_AT_bit_stride:
3925 return "DW_AT_bit_stride";
3926 case DW_AT_upper_bound:
3927 return "DW_AT_upper_bound";
3928 case DW_AT_abstract_origin:
3929 return "DW_AT_abstract_origin";
3930 case DW_AT_accessibility:
3931 return "DW_AT_accessibility";
3932 case DW_AT_address_class:
3933 return "DW_AT_address_class";
3934 case DW_AT_artificial:
3935 return "DW_AT_artificial";
3936 case DW_AT_base_types:
3937 return "DW_AT_base_types";
3938 case DW_AT_calling_convention:
3939 return "DW_AT_calling_convention";
3940 case DW_AT_count:
3941 return "DW_AT_count";
3942 case DW_AT_data_member_location:
3943 return "DW_AT_data_member_location";
3944 case DW_AT_decl_column:
3945 return "DW_AT_decl_column";
3946 case DW_AT_decl_file:
3947 return "DW_AT_decl_file";
3948 case DW_AT_decl_line:
3949 return "DW_AT_decl_line";
3950 case DW_AT_declaration:
3951 return "DW_AT_declaration";
3952 case DW_AT_discr_list:
3953 return "DW_AT_discr_list";
3954 case DW_AT_encoding:
3955 return "DW_AT_encoding";
3956 case DW_AT_external:
3957 return "DW_AT_external";
3958 case DW_AT_explicit:
3959 return "DW_AT_explicit";
3960 case DW_AT_frame_base:
3961 return "DW_AT_frame_base";
3962 case DW_AT_friend:
3963 return "DW_AT_friend";
3964 case DW_AT_identifier_case:
3965 return "DW_AT_identifier_case";
3966 case DW_AT_macro_info:
3967 return "DW_AT_macro_info";
3968 case DW_AT_namelist_items:
3969 return "DW_AT_namelist_items";
3970 case DW_AT_priority:
3971 return "DW_AT_priority";
3972 case DW_AT_segment:
3973 return "DW_AT_segment";
3974 case DW_AT_specification:
3975 return "DW_AT_specification";
3976 case DW_AT_static_link:
3977 return "DW_AT_static_link";
3978 case DW_AT_type:
3979 return "DW_AT_type";
3980 case DW_AT_use_location:
3981 return "DW_AT_use_location";
3982 case DW_AT_variable_parameter:
3983 return "DW_AT_variable_parameter";
3984 case DW_AT_virtuality:
3985 return "DW_AT_virtuality";
3986 case DW_AT_vtable_elem_location:
3987 return "DW_AT_vtable_elem_location";
3989 case DW_AT_allocated:
3990 return "DW_AT_allocated";
3991 case DW_AT_associated:
3992 return "DW_AT_associated";
3993 case DW_AT_data_location:
3994 return "DW_AT_data_location";
3995 case DW_AT_byte_stride:
3996 return "DW_AT_byte_stride";
3997 case DW_AT_entry_pc:
3998 return "DW_AT_entry_pc";
3999 case DW_AT_use_UTF8:
4000 return "DW_AT_use_UTF8";
4001 case DW_AT_extension:
4002 return "DW_AT_extension";
4003 case DW_AT_ranges:
4004 return "DW_AT_ranges";
4005 case DW_AT_trampoline:
4006 return "DW_AT_trampoline";
4007 case DW_AT_call_column:
4008 return "DW_AT_call_column";
4009 case DW_AT_call_file:
4010 return "DW_AT_call_file";
4011 case DW_AT_call_line:
4012 return "DW_AT_call_line";
4013 case DW_AT_object_pointer:
4014 return "DW_AT_object_pointer";
4016 case DW_AT_signature:
4017 return "DW_AT_signature";
4018 case DW_AT_main_subprogram:
4019 return "DW_AT_main_subprogram";
4020 case DW_AT_data_bit_offset:
4021 return "DW_AT_data_bit_offset";
4022 case DW_AT_const_expr:
4023 return "DW_AT_const_expr";
4024 case DW_AT_enum_class:
4025 return "DW_AT_enum_class";
4026 case DW_AT_linkage_name:
4027 return "DW_AT_linkage_name";
4029 case DW_AT_MIPS_fde:
4030 return "DW_AT_MIPS_fde";
4031 case DW_AT_MIPS_loop_begin:
4032 return "DW_AT_MIPS_loop_begin";
4033 case DW_AT_MIPS_tail_loop_begin:
4034 return "DW_AT_MIPS_tail_loop_begin";
4035 case DW_AT_MIPS_epilog_begin:
4036 return "DW_AT_MIPS_epilog_begin";
4037 #if VMS_DEBUGGING_INFO
4038 case DW_AT_HP_prologue:
4039 return "DW_AT_HP_prologue";
4040 #else
4041 case DW_AT_MIPS_loop_unroll_factor:
4042 return "DW_AT_MIPS_loop_unroll_factor";
4043 #endif
4044 case DW_AT_MIPS_software_pipeline_depth:
4045 return "DW_AT_MIPS_software_pipeline_depth";
4046 case DW_AT_MIPS_linkage_name:
4047 return "DW_AT_MIPS_linkage_name";
4048 #if VMS_DEBUGGING_INFO
4049 case DW_AT_HP_epilogue:
4050 return "DW_AT_HP_epilogue";
4051 #else
4052 case DW_AT_MIPS_stride:
4053 return "DW_AT_MIPS_stride";
4054 #endif
4055 case DW_AT_MIPS_abstract_name:
4056 return "DW_AT_MIPS_abstract_name";
4057 case DW_AT_MIPS_clone_origin:
4058 return "DW_AT_MIPS_clone_origin";
4059 case DW_AT_MIPS_has_inlines:
4060 return "DW_AT_MIPS_has_inlines";
4062 case DW_AT_sf_names:
4063 return "DW_AT_sf_names";
4064 case DW_AT_src_info:
4065 return "DW_AT_src_info";
4066 case DW_AT_mac_info:
4067 return "DW_AT_mac_info";
4068 case DW_AT_src_coords:
4069 return "DW_AT_src_coords";
4070 case DW_AT_body_begin:
4071 return "DW_AT_body_begin";
4072 case DW_AT_body_end:
4073 return "DW_AT_body_end";
4075 case DW_AT_GNU_vector:
4076 return "DW_AT_GNU_vector";
4077 case DW_AT_GNU_guarded_by:
4078 return "DW_AT_GNU_guarded_by";
4079 case DW_AT_GNU_pt_guarded_by:
4080 return "DW_AT_GNU_pt_guarded_by";
4081 case DW_AT_GNU_guarded:
4082 return "DW_AT_GNU_guarded";
4083 case DW_AT_GNU_pt_guarded:
4084 return "DW_AT_GNU_pt_guarded";
4085 case DW_AT_GNU_locks_excluded:
4086 return "DW_AT_GNU_locks_excluded";
4087 case DW_AT_GNU_exclusive_locks_required:
4088 return "DW_AT_GNU_exclusive_locks_required";
4089 case DW_AT_GNU_shared_locks_required:
4090 return "DW_AT_GNU_shared_locks_required";
4091 case DW_AT_GNU_odr_signature:
4092 return "DW_AT_GNU_odr_signature";
4093 case DW_AT_GNU_template_name:
4094 return "DW_AT_GNU_template_name";
4095 case DW_AT_GNU_call_site_value:
4096 return "DW_AT_GNU_call_site_value";
4097 case DW_AT_GNU_call_site_data_value:
4098 return "DW_AT_GNU_call_site_data_value";
4099 case DW_AT_GNU_call_site_target:
4100 return "DW_AT_GNU_call_site_target";
4101 case DW_AT_GNU_call_site_target_clobbered:
4102 return "DW_AT_GNU_call_site_target_clobbered";
4103 case DW_AT_GNU_tail_call:
4104 return "DW_AT_GNU_tail_call";
4105 case DW_AT_GNU_all_tail_call_sites:
4106 return "DW_AT_GNU_all_tail_call_sites";
4107 case DW_AT_GNU_all_call_sites:
4108 return "DW_AT_GNU_all_call_sites";
4109 case DW_AT_GNU_all_source_call_sites:
4110 return "DW_AT_GNU_all_source_call_sites";
4111 case DW_AT_GNU_macros:
4112 return "DW_AT_GNU_macros";
4114 case DW_AT_GNAT_descriptive_type:
4115 return "DW_AT_GNAT_descriptive_type";
4117 case DW_AT_VMS_rtnbeg_pd_address:
4118 return "DW_AT_VMS_rtnbeg_pd_address";
4120 case DW_AT_upc_threads_scaled:
4121 return "DW_AT_upc_threads_scaled";
4123 default:
4124 return "DW_AT_<unknown>";
4128 /* Convert a DWARF value form code into its string name. */
4130 static const char *
4131 dwarf_form_name (unsigned int form)
4133 switch (form)
4135 case DW_FORM_addr:
4136 return "DW_FORM_addr";
4137 case DW_FORM_block2:
4138 return "DW_FORM_block2";
4139 case DW_FORM_block4:
4140 return "DW_FORM_block4";
4141 case DW_FORM_data2:
4142 return "DW_FORM_data2";
4143 case DW_FORM_data4:
4144 return "DW_FORM_data4";
4145 case DW_FORM_data8:
4146 return "DW_FORM_data8";
4147 case DW_FORM_string:
4148 return "DW_FORM_string";
4149 case DW_FORM_block:
4150 return "DW_FORM_block";
4151 case DW_FORM_block1:
4152 return "DW_FORM_block1";
4153 case DW_FORM_data1:
4154 return "DW_FORM_data1";
4155 case DW_FORM_flag:
4156 return "DW_FORM_flag";
4157 case DW_FORM_sdata:
4158 return "DW_FORM_sdata";
4159 case DW_FORM_strp:
4160 return "DW_FORM_strp";
4161 case DW_FORM_udata:
4162 return "DW_FORM_udata";
4163 case DW_FORM_ref_addr:
4164 return "DW_FORM_ref_addr";
4165 case DW_FORM_ref1:
4166 return "DW_FORM_ref1";
4167 case DW_FORM_ref2:
4168 return "DW_FORM_ref2";
4169 case DW_FORM_ref4:
4170 return "DW_FORM_ref4";
4171 case DW_FORM_ref8:
4172 return "DW_FORM_ref8";
4173 case DW_FORM_ref_udata:
4174 return "DW_FORM_ref_udata";
4175 case DW_FORM_indirect:
4176 return "DW_FORM_indirect";
4177 case DW_FORM_sec_offset:
4178 return "DW_FORM_sec_offset";
4179 case DW_FORM_exprloc:
4180 return "DW_FORM_exprloc";
4181 case DW_FORM_flag_present:
4182 return "DW_FORM_flag_present";
4183 case DW_FORM_ref_sig8:
4184 return "DW_FORM_ref_sig8";
4185 default:
4186 return "DW_FORM_<unknown>";
4190 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
4191 instance of an inlined instance of a decl which is local to an inline
4192 function, so we have to trace all of the way back through the origin chain
4193 to find out what sort of node actually served as the original seed for the
4194 given block. */
4196 static tree
4197 decl_ultimate_origin (const_tree decl)
4199 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4200 return NULL_TREE;
4202 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
4203 nodes in the function to point to themselves; ignore that if
4204 we're trying to output the abstract instance of this function. */
4205 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4206 return NULL_TREE;
4208 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4209 most distant ancestor, this should never happen. */
4210 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4212 return DECL_ABSTRACT_ORIGIN (decl);
4215 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4216 of a virtual function may refer to a base class, so we check the 'this'
4217 parameter. */
4219 static tree
4220 decl_class_context (tree decl)
4222 tree context = NULL_TREE;
4224 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4225 context = DECL_CONTEXT (decl);
4226 else
4227 context = TYPE_MAIN_VARIANT
4228 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4230 if (context && !TYPE_P (context))
4231 context = NULL_TREE;
4233 return context;
4236 /* Add an attribute/value pair to a DIE. */
4238 static inline void
4239 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
4241 /* Maybe this should be an assert? */
4242 if (die == NULL)
4243 return;
4245 if (die->die_attr == NULL)
4246 die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
4247 VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
4250 static inline enum dw_val_class
4251 AT_class (dw_attr_ref a)
4253 return a->dw_attr_val.val_class;
4256 /* Add a flag value attribute to a DIE. */
4258 static inline void
4259 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4261 dw_attr_node attr;
4263 attr.dw_attr = attr_kind;
4264 attr.dw_attr_val.val_class = dw_val_class_flag;
4265 attr.dw_attr_val.v.val_flag = flag;
4266 add_dwarf_attr (die, &attr);
4269 static inline unsigned
4270 AT_flag (dw_attr_ref a)
4272 gcc_assert (a && AT_class (a) == dw_val_class_flag);
4273 return a->dw_attr_val.v.val_flag;
4276 /* Add a signed integer attribute value to a DIE. */
4278 static inline void
4279 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4281 dw_attr_node attr;
4283 attr.dw_attr = attr_kind;
4284 attr.dw_attr_val.val_class = dw_val_class_const;
4285 attr.dw_attr_val.v.val_int = int_val;
4286 add_dwarf_attr (die, &attr);
4289 static inline HOST_WIDE_INT
4290 AT_int (dw_attr_ref a)
4292 gcc_assert (a && AT_class (a) == dw_val_class_const);
4293 return a->dw_attr_val.v.val_int;
4296 /* Add an unsigned integer attribute value to a DIE. */
4298 static inline void
4299 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4300 unsigned HOST_WIDE_INT unsigned_val)
4302 dw_attr_node attr;
4304 attr.dw_attr = attr_kind;
4305 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4306 attr.dw_attr_val.v.val_unsigned = unsigned_val;
4307 add_dwarf_attr (die, &attr);
4310 static inline unsigned HOST_WIDE_INT
4311 AT_unsigned (dw_attr_ref a)
4313 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
4314 return a->dw_attr_val.v.val_unsigned;
4317 /* Add an unsigned double integer attribute value to a DIE. */
4319 static inline void
4320 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
4321 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
4323 dw_attr_node attr;
4325 attr.dw_attr = attr_kind;
4326 attr.dw_attr_val.val_class = dw_val_class_const_double;
4327 attr.dw_attr_val.v.val_double.high = high;
4328 attr.dw_attr_val.v.val_double.low = low;
4329 add_dwarf_attr (die, &attr);
4332 /* Add a floating point attribute value to a DIE and return it. */
4334 static inline void
4335 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4336 unsigned int length, unsigned int elt_size, unsigned char *array)
4338 dw_attr_node attr;
4340 attr.dw_attr = attr_kind;
4341 attr.dw_attr_val.val_class = dw_val_class_vec;
4342 attr.dw_attr_val.v.val_vec.length = length;
4343 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4344 attr.dw_attr_val.v.val_vec.array = array;
4345 add_dwarf_attr (die, &attr);
4348 /* Add an 8-byte data attribute value to a DIE. */
4350 static inline void
4351 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4352 unsigned char data8[8])
4354 dw_attr_node attr;
4356 attr.dw_attr = attr_kind;
4357 attr.dw_attr_val.val_class = dw_val_class_data8;
4358 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4359 add_dwarf_attr (die, &attr);
4362 /* Hash and equality functions for debug_str_hash. */
4364 static hashval_t
4365 debug_str_do_hash (const void *x)
4367 return htab_hash_string (((const struct indirect_string_node *)x)->str);
4370 static int
4371 debug_str_eq (const void *x1, const void *x2)
4373 return strcmp ((((const struct indirect_string_node *)x1)->str),
4374 (const char *)x2) == 0;
4377 /* Add STR to the indirect string hash table. */
4379 static struct indirect_string_node *
4380 find_AT_string (const char *str)
4382 struct indirect_string_node *node;
4383 void **slot;
4385 if (! debug_str_hash)
4386 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
4387 debug_str_eq, NULL);
4389 slot = htab_find_slot_with_hash (debug_str_hash, str,
4390 htab_hash_string (str), INSERT);
4391 if (*slot == NULL)
4393 node = ggc_alloc_cleared_indirect_string_node ();
4394 node->str = ggc_strdup (str);
4395 *slot = node;
4397 else
4398 node = (struct indirect_string_node *) *slot;
4400 node->refcount++;
4401 return node;
4404 /* Add a string attribute value to a DIE. */
4406 static inline void
4407 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4409 dw_attr_node attr;
4410 struct indirect_string_node *node;
4412 node = find_AT_string (str);
4414 attr.dw_attr = attr_kind;
4415 attr.dw_attr_val.val_class = dw_val_class_str;
4416 attr.dw_attr_val.v.val_str = node;
4417 add_dwarf_attr (die, &attr);
4420 static inline const char *
4421 AT_string (dw_attr_ref a)
4423 gcc_assert (a && AT_class (a) == dw_val_class_str);
4424 return a->dw_attr_val.v.val_str->str;
4427 /* Find out whether a string should be output inline in DIE
4428 or out-of-line in .debug_str section. */
4430 static enum dwarf_form
4431 AT_string_form (dw_attr_ref a)
4433 struct indirect_string_node *node;
4434 unsigned int len;
4435 char label[32];
4437 gcc_assert (a && AT_class (a) == dw_val_class_str);
4439 node = a->dw_attr_val.v.val_str;
4440 if (node->form)
4441 return node->form;
4443 len = strlen (node->str) + 1;
4445 /* If the string is shorter or equal to the size of the reference, it is
4446 always better to put it inline. */
4447 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4448 return node->form = DW_FORM_string;
4450 /* If we cannot expect the linker to merge strings in .debug_str
4451 section, only put it into .debug_str if it is worth even in this
4452 single module. */
4453 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4454 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4455 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4456 return node->form = DW_FORM_string;
4458 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4459 ++dw2_string_counter;
4460 node->label = xstrdup (label);
4462 return node->form = DW_FORM_strp;
4465 /* Add a DIE reference attribute value to a DIE. */
4467 static inline void
4468 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4470 dw_attr_node attr;
4472 #ifdef ENABLE_CHECKING
4473 gcc_assert (targ_die != NULL);
4474 #else
4475 /* With LTO we can end up trying to reference something we didn't create
4476 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4477 if (targ_die == NULL)
4478 return;
4479 #endif
4481 attr.dw_attr = attr_kind;
4482 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4483 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4484 attr.dw_attr_val.v.val_die_ref.external = 0;
4485 add_dwarf_attr (die, &attr);
4488 /* Add an AT_specification attribute to a DIE, and also make the back
4489 pointer from the specification to the definition. */
4491 static inline void
4492 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4494 add_AT_die_ref (die, DW_AT_specification, targ_die);
4495 gcc_assert (!targ_die->die_definition);
4496 targ_die->die_definition = die;
4499 static inline dw_die_ref
4500 AT_ref (dw_attr_ref a)
4502 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4503 return a->dw_attr_val.v.val_die_ref.die;
4506 static inline int
4507 AT_ref_external (dw_attr_ref a)
4509 if (a && AT_class (a) == dw_val_class_die_ref)
4510 return a->dw_attr_val.v.val_die_ref.external;
4512 return 0;
4515 static inline void
4516 set_AT_ref_external (dw_attr_ref a, int i)
4518 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4519 a->dw_attr_val.v.val_die_ref.external = i;
4522 /* Add an FDE reference attribute value to a DIE. */
4524 static inline void
4525 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4527 dw_attr_node attr;
4529 attr.dw_attr = attr_kind;
4530 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4531 attr.dw_attr_val.v.val_fde_index = targ_fde;
4532 add_dwarf_attr (die, &attr);
4535 /* Add a location description attribute value to a DIE. */
4537 static inline void
4538 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4540 dw_attr_node attr;
4542 attr.dw_attr = attr_kind;
4543 attr.dw_attr_val.val_class = dw_val_class_loc;
4544 attr.dw_attr_val.v.val_loc = loc;
4545 add_dwarf_attr (die, &attr);
4548 static inline dw_loc_descr_ref
4549 AT_loc (dw_attr_ref a)
4551 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4552 return a->dw_attr_val.v.val_loc;
4555 static inline void
4556 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4558 dw_attr_node attr;
4560 attr.dw_attr = attr_kind;
4561 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4562 attr.dw_attr_val.v.val_loc_list = loc_list;
4563 add_dwarf_attr (die, &attr);
4564 have_location_lists = true;
4567 static inline dw_loc_list_ref
4568 AT_loc_list (dw_attr_ref a)
4570 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4571 return a->dw_attr_val.v.val_loc_list;
4574 static inline dw_loc_list_ref *
4575 AT_loc_list_ptr (dw_attr_ref a)
4577 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4578 return &a->dw_attr_val.v.val_loc_list;
4581 /* Add an address constant attribute value to a DIE. */
4583 static inline void
4584 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
4586 dw_attr_node attr;
4588 attr.dw_attr = attr_kind;
4589 attr.dw_attr_val.val_class = dw_val_class_addr;
4590 attr.dw_attr_val.v.val_addr = addr;
4591 add_dwarf_attr (die, &attr);
4594 /* Get the RTX from to an address DIE attribute. */
4596 static inline rtx
4597 AT_addr (dw_attr_ref a)
4599 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4600 return a->dw_attr_val.v.val_addr;
4603 /* Add a file attribute value to a DIE. */
4605 static inline void
4606 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4607 struct dwarf_file_data *fd)
4609 dw_attr_node attr;
4611 attr.dw_attr = attr_kind;
4612 attr.dw_attr_val.val_class = dw_val_class_file;
4613 attr.dw_attr_val.v.val_file = fd;
4614 add_dwarf_attr (die, &attr);
4617 /* Get the dwarf_file_data from a file DIE attribute. */
4619 static inline struct dwarf_file_data *
4620 AT_file (dw_attr_ref a)
4622 gcc_assert (a && AT_class (a) == dw_val_class_file);
4623 return a->dw_attr_val.v.val_file;
4626 /* Add a vms delta attribute value to a DIE. */
4628 static inline void
4629 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4630 const char *lbl1, const char *lbl2)
4632 dw_attr_node attr;
4634 attr.dw_attr = attr_kind;
4635 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4636 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4637 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4638 add_dwarf_attr (die, &attr);
4641 /* Add a label identifier attribute value to a DIE. */
4643 static inline void
4644 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
4646 dw_attr_node attr;
4648 attr.dw_attr = attr_kind;
4649 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4650 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4651 add_dwarf_attr (die, &attr);
4654 /* Add a section offset attribute value to a DIE, an offset into the
4655 debug_line section. */
4657 static inline void
4658 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4659 const char *label)
4661 dw_attr_node attr;
4663 attr.dw_attr = attr_kind;
4664 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4665 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4666 add_dwarf_attr (die, &attr);
4669 /* Add a section offset attribute value to a DIE, an offset into the
4670 debug_macinfo section. */
4672 static inline void
4673 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4674 const char *label)
4676 dw_attr_node attr;
4678 attr.dw_attr = attr_kind;
4679 attr.dw_attr_val.val_class = dw_val_class_macptr;
4680 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4681 add_dwarf_attr (die, &attr);
4684 /* Add an offset attribute value to a DIE. */
4686 static inline void
4687 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4688 unsigned HOST_WIDE_INT offset)
4690 dw_attr_node attr;
4692 attr.dw_attr = attr_kind;
4693 attr.dw_attr_val.val_class = dw_val_class_offset;
4694 attr.dw_attr_val.v.val_offset = offset;
4695 add_dwarf_attr (die, &attr);
4698 /* Add an range_list attribute value to a DIE. */
4700 static void
4701 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4702 long unsigned int offset)
4704 dw_attr_node attr;
4706 attr.dw_attr = attr_kind;
4707 attr.dw_attr_val.val_class = dw_val_class_range_list;
4708 attr.dw_attr_val.v.val_offset = offset;
4709 add_dwarf_attr (die, &attr);
4712 /* Return the start label of a delta attribute. */
4714 static inline const char *
4715 AT_vms_delta1 (dw_attr_ref a)
4717 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4718 return a->dw_attr_val.v.val_vms_delta.lbl1;
4721 /* Return the end label of a delta attribute. */
4723 static inline const char *
4724 AT_vms_delta2 (dw_attr_ref a)
4726 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4727 return a->dw_attr_val.v.val_vms_delta.lbl2;
4730 static inline const char *
4731 AT_lbl (dw_attr_ref a)
4733 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4734 || AT_class (a) == dw_val_class_lineptr
4735 || AT_class (a) == dw_val_class_macptr));
4736 return a->dw_attr_val.v.val_lbl_id;
4739 /* Get the attribute of type attr_kind. */
4741 static dw_attr_ref
4742 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4744 dw_attr_ref a;
4745 unsigned ix;
4746 dw_die_ref spec = NULL;
4748 if (! die)
4749 return NULL;
4751 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
4752 if (a->dw_attr == attr_kind)
4753 return a;
4754 else if (a->dw_attr == DW_AT_specification
4755 || a->dw_attr == DW_AT_abstract_origin)
4756 spec = AT_ref (a);
4758 if (spec)
4759 return get_AT (spec, attr_kind);
4761 return NULL;
4764 /* Return the "low pc" attribute value, typically associated with a subprogram
4765 DIE. Return null if the "low pc" attribute is either not present, or if it
4766 cannot be represented as an assembler label identifier. */
4768 static inline const char *
4769 get_AT_low_pc (dw_die_ref die)
4771 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4773 return a ? AT_lbl (a) : NULL;
4776 /* Return the "high pc" attribute value, typically associated with a subprogram
4777 DIE. Return null if the "high pc" attribute is either not present, or if it
4778 cannot be represented as an assembler label identifier. */
4780 static inline const char *
4781 get_AT_hi_pc (dw_die_ref die)
4783 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4785 return a ? AT_lbl (a) : NULL;
4788 /* Return the value of the string attribute designated by ATTR_KIND, or
4789 NULL if it is not present. */
4791 static inline const char *
4792 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4794 dw_attr_ref a = get_AT (die, attr_kind);
4796 return a ? AT_string (a) : NULL;
4799 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4800 if it is not present. */
4802 static inline int
4803 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4805 dw_attr_ref a = get_AT (die, attr_kind);
4807 return a ? AT_flag (a) : 0;
4810 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4811 if it is not present. */
4813 static inline unsigned
4814 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4816 dw_attr_ref a = get_AT (die, attr_kind);
4818 return a ? AT_unsigned (a) : 0;
4821 static inline dw_die_ref
4822 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4824 dw_attr_ref a = get_AT (die, attr_kind);
4826 return a ? AT_ref (a) : NULL;
4829 static inline struct dwarf_file_data *
4830 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4832 dw_attr_ref a = get_AT (die, attr_kind);
4834 return a ? AT_file (a) : NULL;
4837 /* Return TRUE if the language is C++. */
4839 static inline bool
4840 is_cxx (void)
4842 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4844 return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
4847 /* Return TRUE if the language is Fortran. */
4849 static inline bool
4850 is_fortran (void)
4852 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4854 return (lang == DW_LANG_Fortran77
4855 || lang == DW_LANG_Fortran90
4856 || lang == DW_LANG_Fortran95);
4859 /* Return TRUE if the language is Ada. */
4861 static inline bool
4862 is_ada (void)
4864 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4866 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4869 /* Remove the specified attribute if present. */
4871 static void
4872 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4874 dw_attr_ref a;
4875 unsigned ix;
4877 if (! die)
4878 return;
4880 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
4881 if (a->dw_attr == attr_kind)
4883 if (AT_class (a) == dw_val_class_str)
4884 if (a->dw_attr_val.v.val_str->refcount)
4885 a->dw_attr_val.v.val_str->refcount--;
4887 /* VEC_ordered_remove should help reduce the number of abbrevs
4888 that are needed. */
4889 VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
4890 return;
4894 /* Remove CHILD from its parent. PREV must have the property that
4895 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4897 static void
4898 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4900 gcc_assert (child->die_parent == prev->die_parent);
4901 gcc_assert (prev->die_sib == child);
4902 if (prev == child)
4904 gcc_assert (child->die_parent->die_child == child);
4905 prev = NULL;
4907 else
4908 prev->die_sib = child->die_sib;
4909 if (child->die_parent->die_child == child)
4910 child->die_parent->die_child = prev;
4913 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4914 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4916 static void
4917 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4919 dw_die_ref parent = old_child->die_parent;
4921 gcc_assert (parent == prev->die_parent);
4922 gcc_assert (prev->die_sib == old_child);
4924 new_child->die_parent = parent;
4925 if (prev == old_child)
4927 gcc_assert (parent->die_child == old_child);
4928 new_child->die_sib = new_child;
4930 else
4932 prev->die_sib = new_child;
4933 new_child->die_sib = old_child->die_sib;
4935 if (old_child->die_parent->die_child == old_child)
4936 old_child->die_parent->die_child = new_child;
4939 /* Move all children from OLD_PARENT to NEW_PARENT. */
4941 static void
4942 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4944 dw_die_ref c;
4945 new_parent->die_child = old_parent->die_child;
4946 old_parent->die_child = NULL;
4947 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4950 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4951 matches TAG. */
4953 static void
4954 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4956 dw_die_ref c;
4958 c = die->die_child;
4959 if (c) do {
4960 dw_die_ref prev = c;
4961 c = c->die_sib;
4962 while (c->die_tag == tag)
4964 remove_child_with_prev (c, prev);
4965 /* Might have removed every child. */
4966 if (c == c->die_sib)
4967 return;
4968 c = c->die_sib;
4970 } while (c != die->die_child);
4973 /* Add a CHILD_DIE as the last child of DIE. */
4975 static void
4976 add_child_die (dw_die_ref die, dw_die_ref child_die)
4978 /* FIXME this should probably be an assert. */
4979 if (! die || ! child_die)
4980 return;
4981 gcc_assert (die != child_die);
4983 child_die->die_parent = die;
4984 if (die->die_child)
4986 child_die->die_sib = die->die_child->die_sib;
4987 die->die_child->die_sib = child_die;
4989 else
4990 child_die->die_sib = child_die;
4991 die->die_child = child_die;
4994 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4995 is the specification, to the end of PARENT's list of children.
4996 This is done by removing and re-adding it. */
4998 static void
4999 splice_child_die (dw_die_ref parent, dw_die_ref child)
5001 dw_die_ref p;
5003 /* We want the declaration DIE from inside the class, not the
5004 specification DIE at toplevel. */
5005 if (child->die_parent != parent)
5007 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5009 if (tmp)
5010 child = tmp;
5013 gcc_assert (child->die_parent == parent
5014 || (child->die_parent
5015 == get_AT_ref (parent, DW_AT_specification)));
5017 for (p = child->die_parent->die_child; ; p = p->die_sib)
5018 if (p->die_sib == child)
5020 remove_child_with_prev (child, p);
5021 break;
5024 add_child_die (parent, child);
5027 /* Return a pointer to a newly created DIE node. */
5029 static inline dw_die_ref
5030 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5032 dw_die_ref die = ggc_alloc_cleared_die_node ();
5034 die->die_tag = tag_value;
5036 if (parent_die != NULL)
5037 add_child_die (parent_die, die);
5038 else
5040 limbo_die_node *limbo_node;
5042 limbo_node = ggc_alloc_cleared_limbo_die_node ();
5043 limbo_node->die = die;
5044 limbo_node->created_for = t;
5045 limbo_node->next = limbo_die_list;
5046 limbo_die_list = limbo_node;
5049 return die;
5052 /* Return the DIE associated with the given type specifier. */
5054 static inline dw_die_ref
5055 lookup_type_die (tree type)
5057 return TYPE_SYMTAB_DIE (type);
5060 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5061 anonymous type named by the typedef TYPE_DIE, return the DIE of the
5062 anonymous type instead the one of the naming typedef. */
5064 static inline dw_die_ref
5065 strip_naming_typedef (tree type, dw_die_ref type_die)
5067 if (type
5068 && TREE_CODE (type) == RECORD_TYPE
5069 && type_die
5070 && type_die->die_tag == DW_TAG_typedef
5071 && is_naming_typedef_decl (TYPE_NAME (type)))
5072 type_die = get_AT_ref (type_die, DW_AT_type);
5073 return type_die;
5076 /* Like lookup_type_die, but if type is an anonymous type named by a
5077 typedef[1], return the DIE of the anonymous type instead the one of
5078 the naming typedef. This is because in gen_typedef_die, we did
5079 equate the anonymous struct named by the typedef with the DIE of
5080 the naming typedef. So by default, lookup_type_die on an anonymous
5081 struct yields the DIE of the naming typedef.
5083 [1]: Read the comment of is_naming_typedef_decl to learn about what
5084 a naming typedef is. */
5086 static inline dw_die_ref
5087 lookup_type_die_strip_naming_typedef (tree type)
5089 dw_die_ref die = lookup_type_die (type);
5090 return strip_naming_typedef (type, die);
5093 /* Equate a DIE to a given type specifier. */
5095 static inline void
5096 equate_type_number_to_die (tree type, dw_die_ref type_die)
5098 TYPE_SYMTAB_DIE (type) = type_die;
5101 /* Returns a hash value for X (which really is a die_struct). */
5103 static hashval_t
5104 decl_die_table_hash (const void *x)
5106 return (hashval_t) ((const_dw_die_ref) x)->decl_id;
5109 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5111 static int
5112 decl_die_table_eq (const void *x, const void *y)
5114 return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
5117 /* Return the DIE associated with a given declaration. */
5119 static inline dw_die_ref
5120 lookup_decl_die (tree decl)
5122 return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
5125 /* Returns a hash value for X (which really is a var_loc_list). */
5127 static hashval_t
5128 decl_loc_table_hash (const void *x)
5130 return (hashval_t) ((const var_loc_list *) x)->decl_id;
5133 /* Return nonzero if decl_id of var_loc_list X is the same as
5134 UID of decl *Y. */
5136 static int
5137 decl_loc_table_eq (const void *x, const void *y)
5139 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
5142 /* Return the var_loc list associated with a given declaration. */
5144 static inline var_loc_list *
5145 lookup_decl_loc (const_tree decl)
5147 if (!decl_loc_table)
5148 return NULL;
5149 return (var_loc_list *)
5150 htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
5153 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
5155 static hashval_t
5156 cached_dw_loc_list_table_hash (const void *x)
5158 return (hashval_t) ((const cached_dw_loc_list *) x)->decl_id;
5161 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5162 UID of decl *Y. */
5164 static int
5165 cached_dw_loc_list_table_eq (const void *x, const void *y)
5167 return (((const cached_dw_loc_list *) x)->decl_id
5168 == DECL_UID ((const_tree) y));
5171 /* Equate a DIE to a particular declaration. */
5173 static void
5174 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5176 unsigned int decl_id = DECL_UID (decl);
5177 void **slot;
5179 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
5180 *slot = decl_die;
5181 decl_die->decl_id = decl_id;
5184 /* Return how many bits covers PIECE EXPR_LIST. */
5186 static int
5187 decl_piece_bitsize (rtx piece)
5189 int ret = (int) GET_MODE (piece);
5190 if (ret)
5191 return ret;
5192 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5193 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5194 return INTVAL (XEXP (XEXP (piece, 0), 0));
5197 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5199 static rtx *
5200 decl_piece_varloc_ptr (rtx piece)
5202 if ((int) GET_MODE (piece))
5203 return &XEXP (piece, 0);
5204 else
5205 return &XEXP (XEXP (piece, 0), 1);
5208 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5209 Next is the chain of following piece nodes. */
5211 static rtx
5212 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5214 if (bitsize <= (int) MAX_MACHINE_MODE)
5215 return alloc_EXPR_LIST (bitsize, loc_note, next);
5216 else
5217 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5218 GEN_INT (bitsize),
5219 loc_note), next);
5222 /* Return rtx that should be stored into loc field for
5223 LOC_NOTE and BITPOS/BITSIZE. */
5225 static rtx
5226 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5227 HOST_WIDE_INT bitsize)
5229 if (bitsize != -1)
5231 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5232 if (bitpos != 0)
5233 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5235 return loc_note;
5238 /* This function either modifies location piece list *DEST in
5239 place (if SRC and INNER is NULL), or copies location piece list
5240 *SRC to *DEST while modifying it. Location BITPOS is modified
5241 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5242 not copied and if needed some padding around it is added.
5243 When modifying in place, DEST should point to EXPR_LIST where
5244 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5245 to the start of the whole list and INNER points to the EXPR_LIST
5246 where earlier pieces cover PIECE_BITPOS bits. */
5248 static void
5249 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5250 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5251 HOST_WIDE_INT bitsize, rtx loc_note)
5253 int diff;
5254 bool copy = inner != NULL;
5256 if (copy)
5258 /* First copy all nodes preceeding the current bitpos. */
5259 while (src != inner)
5261 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5262 decl_piece_bitsize (*src), NULL_RTX);
5263 dest = &XEXP (*dest, 1);
5264 src = &XEXP (*src, 1);
5267 /* Add padding if needed. */
5268 if (bitpos != piece_bitpos)
5270 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5271 copy ? NULL_RTX : *dest);
5272 dest = &XEXP (*dest, 1);
5274 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5276 gcc_assert (!copy);
5277 /* A piece with correct bitpos and bitsize already exist,
5278 just update the location for it and return. */
5279 *decl_piece_varloc_ptr (*dest) = loc_note;
5280 return;
5282 /* Add the piece that changed. */
5283 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5284 dest = &XEXP (*dest, 1);
5285 /* Skip over pieces that overlap it. */
5286 diff = bitpos - piece_bitpos + bitsize;
5287 if (!copy)
5288 src = dest;
5289 while (diff > 0 && *src)
5291 rtx piece = *src;
5292 diff -= decl_piece_bitsize (piece);
5293 if (copy)
5294 src = &XEXP (piece, 1);
5295 else
5297 *src = XEXP (piece, 1);
5298 free_EXPR_LIST_node (piece);
5301 /* Add padding if needed. */
5302 if (diff < 0 && *src)
5304 if (!copy)
5305 dest = src;
5306 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5307 dest = &XEXP (*dest, 1);
5309 if (!copy)
5310 return;
5311 /* Finally copy all nodes following it. */
5312 while (*src)
5314 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5315 decl_piece_bitsize (*src), NULL_RTX);
5316 dest = &XEXP (*dest, 1);
5317 src = &XEXP (*src, 1);
5321 /* Add a variable location node to the linked list for DECL. */
5323 static struct var_loc_node *
5324 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5326 unsigned int decl_id;
5327 var_loc_list *temp;
5328 void **slot;
5329 struct var_loc_node *loc = NULL;
5330 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5332 if (DECL_DEBUG_EXPR_IS_FROM (decl))
5334 tree realdecl = DECL_DEBUG_EXPR (decl);
5335 if (realdecl && handled_component_p (realdecl))
5337 HOST_WIDE_INT maxsize;
5338 tree innerdecl;
5339 innerdecl
5340 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5341 if (!DECL_P (innerdecl)
5342 || DECL_IGNORED_P (innerdecl)
5343 || TREE_STATIC (innerdecl)
5344 || bitsize <= 0
5345 || bitpos + bitsize > 256
5346 || bitsize != maxsize)
5347 return NULL;
5348 decl = innerdecl;
5352 decl_id = DECL_UID (decl);
5353 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5354 if (*slot == NULL)
5356 temp = ggc_alloc_cleared_var_loc_list ();
5357 temp->decl_id = decl_id;
5358 *slot = temp;
5360 else
5361 temp = (var_loc_list *) *slot;
5363 /* For PARM_DECLs try to keep around the original incoming value,
5364 even if that means we'll emit a zero-range .debug_loc entry. */
5365 if (temp->last
5366 && temp->first == temp->last
5367 && TREE_CODE (decl) == PARM_DECL
5368 && GET_CODE (temp->first->loc) == NOTE
5369 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5370 && DECL_INCOMING_RTL (decl)
5371 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5372 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5373 == GET_CODE (DECL_INCOMING_RTL (decl))
5374 && prev_real_insn (temp->first->loc) == NULL_RTX
5375 && (bitsize != -1
5376 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5377 NOTE_VAR_LOCATION_LOC (loc_note))
5378 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5379 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5381 loc = ggc_alloc_cleared_var_loc_node ();
5382 temp->first->next = loc;
5383 temp->last = loc;
5384 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5386 else if (temp->last)
5388 struct var_loc_node *last = temp->last, *unused = NULL;
5389 rtx *piece_loc = NULL, last_loc_note;
5390 int piece_bitpos = 0;
5391 if (last->next)
5393 last = last->next;
5394 gcc_assert (last->next == NULL);
5396 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5398 piece_loc = &last->loc;
5401 int cur_bitsize = decl_piece_bitsize (*piece_loc);
5402 if (piece_bitpos + cur_bitsize > bitpos)
5403 break;
5404 piece_bitpos += cur_bitsize;
5405 piece_loc = &XEXP (*piece_loc, 1);
5407 while (*piece_loc);
5409 /* TEMP->LAST here is either pointer to the last but one or
5410 last element in the chained list, LAST is pointer to the
5411 last element. */
5412 if (label && strcmp (last->label, label) == 0)
5414 /* For SRA optimized variables if there weren't any real
5415 insns since last note, just modify the last node. */
5416 if (piece_loc != NULL)
5418 adjust_piece_list (piece_loc, NULL, NULL,
5419 bitpos, piece_bitpos, bitsize, loc_note);
5420 return NULL;
5422 /* If the last note doesn't cover any instructions, remove it. */
5423 if (temp->last != last)
5425 temp->last->next = NULL;
5426 unused = last;
5427 last = temp->last;
5428 gcc_assert (strcmp (last->label, label) != 0);
5430 else
5432 gcc_assert (temp->first == temp->last
5433 || (temp->first->next == temp->last
5434 && TREE_CODE (decl) == PARM_DECL));
5435 memset (temp->last, '\0', sizeof (*temp->last));
5436 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5437 return temp->last;
5440 if (bitsize == -1 && NOTE_P (last->loc))
5441 last_loc_note = last->loc;
5442 else if (piece_loc != NULL
5443 && *piece_loc != NULL_RTX
5444 && piece_bitpos == bitpos
5445 && decl_piece_bitsize (*piece_loc) == bitsize)
5446 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5447 else
5448 last_loc_note = NULL_RTX;
5449 /* If the current location is the same as the end of the list,
5450 and either both or neither of the locations is uninitialized,
5451 we have nothing to do. */
5452 if (last_loc_note == NULL_RTX
5453 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5454 NOTE_VAR_LOCATION_LOC (loc_note)))
5455 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5456 != NOTE_VAR_LOCATION_STATUS (loc_note))
5457 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5458 == VAR_INIT_STATUS_UNINITIALIZED)
5459 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5460 == VAR_INIT_STATUS_UNINITIALIZED))))
5462 /* Add LOC to the end of list and update LAST. If the last
5463 element of the list has been removed above, reuse its
5464 memory for the new node, otherwise allocate a new one. */
5465 if (unused)
5467 loc = unused;
5468 memset (loc, '\0', sizeof (*loc));
5470 else
5471 loc = ggc_alloc_cleared_var_loc_node ();
5472 if (bitsize == -1 || piece_loc == NULL)
5473 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5474 else
5475 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5476 bitpos, piece_bitpos, bitsize, loc_note);
5477 last->next = loc;
5478 /* Ensure TEMP->LAST will point either to the new last but one
5479 element of the chain, or to the last element in it. */
5480 if (last != temp->last)
5481 temp->last = last;
5483 else if (unused)
5484 ggc_free (unused);
5486 else
5488 loc = ggc_alloc_cleared_var_loc_node ();
5489 temp->first = loc;
5490 temp->last = loc;
5491 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5493 return loc;
5496 /* Keep track of the number of spaces used to indent the
5497 output of the debugging routines that print the structure of
5498 the DIE internal representation. */
5499 static int print_indent;
5501 /* Indent the line the number of spaces given by print_indent. */
5503 static inline void
5504 print_spaces (FILE *outfile)
5506 fprintf (outfile, "%*s", print_indent, "");
5509 /* Print a type signature in hex. */
5511 static inline void
5512 print_signature (FILE *outfile, char *sig)
5514 int i;
5516 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5517 fprintf (outfile, "%02x", sig[i] & 0xff);
5520 /* Print the information associated with a given DIE, and its children.
5521 This routine is a debugging aid only. */
5523 static void
5524 print_die (dw_die_ref die, FILE *outfile)
5526 dw_attr_ref a;
5527 dw_die_ref c;
5528 unsigned ix;
5530 print_spaces (outfile);
5531 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5532 die->die_offset, dwarf_tag_name (die->die_tag),
5533 (void*) die);
5534 print_spaces (outfile);
5535 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5536 fprintf (outfile, " offset: %ld", die->die_offset);
5537 fprintf (outfile, " mark: %d\n", die->die_mark);
5539 if (use_debug_types && die->die_id.die_type_node)
5541 print_spaces (outfile);
5542 fprintf (outfile, " signature: ");
5543 print_signature (outfile, die->die_id.die_type_node->signature);
5544 fprintf (outfile, "\n");
5547 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
5549 print_spaces (outfile);
5550 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5552 switch (AT_class (a))
5554 case dw_val_class_addr:
5555 fprintf (outfile, "address");
5556 break;
5557 case dw_val_class_offset:
5558 fprintf (outfile, "offset");
5559 break;
5560 case dw_val_class_loc:
5561 fprintf (outfile, "location descriptor");
5562 break;
5563 case dw_val_class_loc_list:
5564 fprintf (outfile, "location list -> label:%s",
5565 AT_loc_list (a)->ll_symbol);
5566 break;
5567 case dw_val_class_range_list:
5568 fprintf (outfile, "range list");
5569 break;
5570 case dw_val_class_const:
5571 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5572 break;
5573 case dw_val_class_unsigned_const:
5574 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5575 break;
5576 case dw_val_class_const_double:
5577 fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
5578 HOST_WIDE_INT_PRINT_UNSIGNED")",
5579 a->dw_attr_val.v.val_double.high,
5580 a->dw_attr_val.v.val_double.low);
5581 break;
5582 case dw_val_class_vec:
5583 fprintf (outfile, "floating-point or vector constant");
5584 break;
5585 case dw_val_class_flag:
5586 fprintf (outfile, "%u", AT_flag (a));
5587 break;
5588 case dw_val_class_die_ref:
5589 if (AT_ref (a) != NULL)
5591 if (use_debug_types && AT_ref (a)->die_id.die_type_node)
5593 fprintf (outfile, "die -> signature: ");
5594 print_signature (outfile,
5595 AT_ref (a)->die_id.die_type_node->signature);
5597 else if (! use_debug_types && AT_ref (a)->die_id.die_symbol)
5598 fprintf (outfile, "die -> label: %s",
5599 AT_ref (a)->die_id.die_symbol);
5600 else
5601 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5602 fprintf (outfile, " (%p)", (void *) AT_ref (a));
5604 else
5605 fprintf (outfile, "die -> <null>");
5606 break;
5607 case dw_val_class_vms_delta:
5608 fprintf (outfile, "delta: @slotcount(%s-%s)",
5609 AT_vms_delta2 (a), AT_vms_delta1 (a));
5610 break;
5611 case dw_val_class_lbl_id:
5612 case dw_val_class_lineptr:
5613 case dw_val_class_macptr:
5614 fprintf (outfile, "label: %s", AT_lbl (a));
5615 break;
5616 case dw_val_class_str:
5617 if (AT_string (a) != NULL)
5618 fprintf (outfile, "\"%s\"", AT_string (a));
5619 else
5620 fprintf (outfile, "<null>");
5621 break;
5622 case dw_val_class_file:
5623 fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5624 AT_file (a)->emitted_number);
5625 break;
5626 case dw_val_class_data8:
5628 int i;
5630 for (i = 0; i < 8; i++)
5631 fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
5632 break;
5634 default:
5635 break;
5638 fprintf (outfile, "\n");
5641 if (die->die_child != NULL)
5643 print_indent += 4;
5644 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5645 print_indent -= 4;
5647 if (print_indent == 0)
5648 fprintf (outfile, "\n");
5651 /* Print the information collected for a given DIE. */
5653 DEBUG_FUNCTION void
5654 debug_dwarf_die (dw_die_ref die)
5656 print_die (die, stderr);
5659 /* Print all DWARF information collected for the compilation unit.
5660 This routine is a debugging aid only. */
5662 DEBUG_FUNCTION void
5663 debug_dwarf (void)
5665 print_indent = 0;
5666 print_die (comp_unit_die (), stderr);
5669 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5670 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5671 DIE that marks the start of the DIEs for this include file. */
5673 static dw_die_ref
5674 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5676 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5677 dw_die_ref new_unit = gen_compile_unit_die (filename);
5679 new_unit->die_sib = old_unit;
5680 return new_unit;
5683 /* Close an include-file CU and reopen the enclosing one. */
5685 static dw_die_ref
5686 pop_compile_unit (dw_die_ref old_unit)
5688 dw_die_ref new_unit = old_unit->die_sib;
5690 old_unit->die_sib = NULL;
5691 return new_unit;
5694 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5695 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5697 /* Calculate the checksum of a location expression. */
5699 static inline void
5700 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5702 int tem;
5704 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5705 CHECKSUM (tem);
5706 CHECKSUM (loc->dw_loc_oprnd1);
5707 CHECKSUM (loc->dw_loc_oprnd2);
5710 /* Calculate the checksum of an attribute. */
5712 static void
5713 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5715 dw_loc_descr_ref loc;
5716 rtx r;
5718 CHECKSUM (at->dw_attr);
5720 /* We don't care that this was compiled with a different compiler
5721 snapshot; if the output is the same, that's what matters. */
5722 if (at->dw_attr == DW_AT_producer)
5723 return;
5725 switch (AT_class (at))
5727 case dw_val_class_const:
5728 CHECKSUM (at->dw_attr_val.v.val_int);
5729 break;
5730 case dw_val_class_unsigned_const:
5731 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5732 break;
5733 case dw_val_class_const_double:
5734 CHECKSUM (at->dw_attr_val.v.val_double);
5735 break;
5736 case dw_val_class_vec:
5737 CHECKSUM (at->dw_attr_val.v.val_vec);
5738 break;
5739 case dw_val_class_flag:
5740 CHECKSUM (at->dw_attr_val.v.val_flag);
5741 break;
5742 case dw_val_class_str:
5743 CHECKSUM_STRING (AT_string (at));
5744 break;
5746 case dw_val_class_addr:
5747 r = AT_addr (at);
5748 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5749 CHECKSUM_STRING (XSTR (r, 0));
5750 break;
5752 case dw_val_class_offset:
5753 CHECKSUM (at->dw_attr_val.v.val_offset);
5754 break;
5756 case dw_val_class_loc:
5757 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5758 loc_checksum (loc, ctx);
5759 break;
5761 case dw_val_class_die_ref:
5762 die_checksum (AT_ref (at), ctx, mark);
5763 break;
5765 case dw_val_class_fde_ref:
5766 case dw_val_class_vms_delta:
5767 case dw_val_class_lbl_id:
5768 case dw_val_class_lineptr:
5769 case dw_val_class_macptr:
5770 break;
5772 case dw_val_class_file:
5773 CHECKSUM_STRING (AT_file (at)->filename);
5774 break;
5776 case dw_val_class_data8:
5777 CHECKSUM (at->dw_attr_val.v.val_data8);
5778 break;
5780 default:
5781 break;
5785 /* Calculate the checksum of a DIE. */
5787 static void
5788 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5790 dw_die_ref c;
5791 dw_attr_ref a;
5792 unsigned ix;
5794 /* To avoid infinite recursion. */
5795 if (die->die_mark)
5797 CHECKSUM (die->die_mark);
5798 return;
5800 die->die_mark = ++(*mark);
5802 CHECKSUM (die->die_tag);
5804 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
5805 attr_checksum (a, ctx, mark);
5807 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5810 #undef CHECKSUM
5811 #undef CHECKSUM_STRING
5813 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
5814 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5815 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5816 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5817 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5818 #define CHECKSUM_ATTR(FOO) \
5819 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5821 /* Calculate the checksum of a number in signed LEB128 format. */
5823 static void
5824 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5826 unsigned char byte;
5827 bool more;
5829 while (1)
5831 byte = (value & 0x7f);
5832 value >>= 7;
5833 more = !((value == 0 && (byte & 0x40) == 0)
5834 || (value == -1 && (byte & 0x40) != 0));
5835 if (more)
5836 byte |= 0x80;
5837 CHECKSUM (byte);
5838 if (!more)
5839 break;
5843 /* Calculate the checksum of a number in unsigned LEB128 format. */
5845 static void
5846 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5848 while (1)
5850 unsigned char byte = (value & 0x7f);
5851 value >>= 7;
5852 if (value != 0)
5853 /* More bytes to follow. */
5854 byte |= 0x80;
5855 CHECKSUM (byte);
5856 if (value == 0)
5857 break;
5861 /* Checksum the context of the DIE. This adds the names of any
5862 surrounding namespaces or structures to the checksum. */
5864 static void
5865 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5867 const char *name;
5868 dw_die_ref spec;
5869 int tag = die->die_tag;
5871 if (tag != DW_TAG_namespace
5872 && tag != DW_TAG_structure_type
5873 && tag != DW_TAG_class_type)
5874 return;
5876 name = get_AT_string (die, DW_AT_name);
5878 spec = get_AT_ref (die, DW_AT_specification);
5879 if (spec != NULL)
5880 die = spec;
5882 if (die->die_parent != NULL)
5883 checksum_die_context (die->die_parent, ctx);
5885 CHECKSUM_ULEB128 ('C');
5886 CHECKSUM_ULEB128 (tag);
5887 if (name != NULL)
5888 CHECKSUM_STRING (name);
5891 /* Calculate the checksum of a location expression. */
5893 static inline void
5894 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5896 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5897 were emitted as a DW_FORM_sdata instead of a location expression. */
5898 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5900 CHECKSUM_ULEB128 (DW_FORM_sdata);
5901 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5902 return;
5905 /* Otherwise, just checksum the raw location expression. */
5906 while (loc != NULL)
5908 CHECKSUM_ULEB128 (loc->dw_loc_opc);
5909 CHECKSUM (loc->dw_loc_oprnd1);
5910 CHECKSUM (loc->dw_loc_oprnd2);
5911 loc = loc->dw_loc_next;
5915 /* Calculate the checksum of an attribute. */
5917 static void
5918 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5919 struct md5_ctx *ctx, int *mark)
5921 dw_loc_descr_ref loc;
5922 rtx r;
5924 if (AT_class (at) == dw_val_class_die_ref)
5926 dw_die_ref target_die = AT_ref (at);
5928 /* For pointer and reference types, we checksum only the (qualified)
5929 name of the target type (if there is a name). For friend entries,
5930 we checksum only the (qualified) name of the target type or function.
5931 This allows the checksum to remain the same whether the target type
5932 is complete or not. */
5933 if ((at->dw_attr == DW_AT_type
5934 && (tag == DW_TAG_pointer_type
5935 || tag == DW_TAG_reference_type
5936 || tag == DW_TAG_rvalue_reference_type
5937 || tag == DW_TAG_ptr_to_member_type))
5938 || (at->dw_attr == DW_AT_friend
5939 && tag == DW_TAG_friend))
5941 dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5943 if (name_attr != NULL)
5945 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5947 if (decl == NULL)
5948 decl = target_die;
5949 CHECKSUM_ULEB128 ('N');
5950 CHECKSUM_ULEB128 (at->dw_attr);
5951 if (decl->die_parent != NULL)
5952 checksum_die_context (decl->die_parent, ctx);
5953 CHECKSUM_ULEB128 ('E');
5954 CHECKSUM_STRING (AT_string (name_attr));
5955 return;
5959 /* For all other references to another DIE, we check to see if the
5960 target DIE has already been visited. If it has, we emit a
5961 backward reference; if not, we descend recursively. */
5962 if (target_die->die_mark > 0)
5964 CHECKSUM_ULEB128 ('R');
5965 CHECKSUM_ULEB128 (at->dw_attr);
5966 CHECKSUM_ULEB128 (target_die->die_mark);
5968 else
5970 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5972 if (decl == NULL)
5973 decl = target_die;
5974 target_die->die_mark = ++(*mark);
5975 CHECKSUM_ULEB128 ('T');
5976 CHECKSUM_ULEB128 (at->dw_attr);
5977 if (decl->die_parent != NULL)
5978 checksum_die_context (decl->die_parent, ctx);
5979 die_checksum_ordered (target_die, ctx, mark);
5981 return;
5984 CHECKSUM_ULEB128 ('A');
5985 CHECKSUM_ULEB128 (at->dw_attr);
5987 switch (AT_class (at))
5989 case dw_val_class_const:
5990 CHECKSUM_ULEB128 (DW_FORM_sdata);
5991 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
5992 break;
5994 case dw_val_class_unsigned_const:
5995 CHECKSUM_ULEB128 (DW_FORM_sdata);
5996 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
5997 break;
5999 case dw_val_class_const_double:
6000 CHECKSUM_ULEB128 (DW_FORM_block);
6001 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6002 CHECKSUM (at->dw_attr_val.v.val_double);
6003 break;
6005 case dw_val_class_vec:
6006 CHECKSUM_ULEB128 (DW_FORM_block);
6007 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
6008 CHECKSUM (at->dw_attr_val.v.val_vec);
6009 break;
6011 case dw_val_class_flag:
6012 CHECKSUM_ULEB128 (DW_FORM_flag);
6013 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6014 break;
6016 case dw_val_class_str:
6017 CHECKSUM_ULEB128 (DW_FORM_string);
6018 CHECKSUM_STRING (AT_string (at));
6019 break;
6021 case dw_val_class_addr:
6022 r = AT_addr (at);
6023 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6024 CHECKSUM_ULEB128 (DW_FORM_string);
6025 CHECKSUM_STRING (XSTR (r, 0));
6026 break;
6028 case dw_val_class_offset:
6029 CHECKSUM_ULEB128 (DW_FORM_sdata);
6030 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6031 break;
6033 case dw_val_class_loc:
6034 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6035 loc_checksum_ordered (loc, ctx);
6036 break;
6038 case dw_val_class_fde_ref:
6039 case dw_val_class_lbl_id:
6040 case dw_val_class_lineptr:
6041 case dw_val_class_macptr:
6042 break;
6044 case dw_val_class_file:
6045 CHECKSUM_ULEB128 (DW_FORM_string);
6046 CHECKSUM_STRING (AT_file (at)->filename);
6047 break;
6049 case dw_val_class_data8:
6050 CHECKSUM (at->dw_attr_val.v.val_data8);
6051 break;
6053 default:
6054 break;
6058 struct checksum_attributes
6060 dw_attr_ref at_name;
6061 dw_attr_ref at_type;
6062 dw_attr_ref at_friend;
6063 dw_attr_ref at_accessibility;
6064 dw_attr_ref at_address_class;
6065 dw_attr_ref at_allocated;
6066 dw_attr_ref at_artificial;
6067 dw_attr_ref at_associated;
6068 dw_attr_ref at_binary_scale;
6069 dw_attr_ref at_bit_offset;
6070 dw_attr_ref at_bit_size;
6071 dw_attr_ref at_bit_stride;
6072 dw_attr_ref at_byte_size;
6073 dw_attr_ref at_byte_stride;
6074 dw_attr_ref at_const_value;
6075 dw_attr_ref at_containing_type;
6076 dw_attr_ref at_count;
6077 dw_attr_ref at_data_location;
6078 dw_attr_ref at_data_member_location;
6079 dw_attr_ref at_decimal_scale;
6080 dw_attr_ref at_decimal_sign;
6081 dw_attr_ref at_default_value;
6082 dw_attr_ref at_digit_count;
6083 dw_attr_ref at_discr;
6084 dw_attr_ref at_discr_list;
6085 dw_attr_ref at_discr_value;
6086 dw_attr_ref at_encoding;
6087 dw_attr_ref at_endianity;
6088 dw_attr_ref at_explicit;
6089 dw_attr_ref at_is_optional;
6090 dw_attr_ref at_location;
6091 dw_attr_ref at_lower_bound;
6092 dw_attr_ref at_mutable;
6093 dw_attr_ref at_ordering;
6094 dw_attr_ref at_picture_string;
6095 dw_attr_ref at_prototyped;
6096 dw_attr_ref at_small;
6097 dw_attr_ref at_segment;
6098 dw_attr_ref at_string_length;
6099 dw_attr_ref at_threads_scaled;
6100 dw_attr_ref at_upper_bound;
6101 dw_attr_ref at_use_location;
6102 dw_attr_ref at_use_UTF8;
6103 dw_attr_ref at_variable_parameter;
6104 dw_attr_ref at_virtuality;
6105 dw_attr_ref at_visibility;
6106 dw_attr_ref at_vtable_elem_location;
6109 /* Collect the attributes that we will want to use for the checksum. */
6111 static void
6112 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6114 dw_attr_ref a;
6115 unsigned ix;
6117 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
6119 switch (a->dw_attr)
6121 case DW_AT_name:
6122 attrs->at_name = a;
6123 break;
6124 case DW_AT_type:
6125 attrs->at_type = a;
6126 break;
6127 case DW_AT_friend:
6128 attrs->at_friend = a;
6129 break;
6130 case DW_AT_accessibility:
6131 attrs->at_accessibility = a;
6132 break;
6133 case DW_AT_address_class:
6134 attrs->at_address_class = a;
6135 break;
6136 case DW_AT_allocated:
6137 attrs->at_allocated = a;
6138 break;
6139 case DW_AT_artificial:
6140 attrs->at_artificial = a;
6141 break;
6142 case DW_AT_associated:
6143 attrs->at_associated = a;
6144 break;
6145 case DW_AT_binary_scale:
6146 attrs->at_binary_scale = a;
6147 break;
6148 case DW_AT_bit_offset:
6149 attrs->at_bit_offset = a;
6150 break;
6151 case DW_AT_bit_size:
6152 attrs->at_bit_size = a;
6153 break;
6154 case DW_AT_bit_stride:
6155 attrs->at_bit_stride = a;
6156 break;
6157 case DW_AT_byte_size:
6158 attrs->at_byte_size = a;
6159 break;
6160 case DW_AT_byte_stride:
6161 attrs->at_byte_stride = a;
6162 break;
6163 case DW_AT_const_value:
6164 attrs->at_const_value = a;
6165 break;
6166 case DW_AT_containing_type:
6167 attrs->at_containing_type = a;
6168 break;
6169 case DW_AT_count:
6170 attrs->at_count = a;
6171 break;
6172 case DW_AT_data_location:
6173 attrs->at_data_location = a;
6174 break;
6175 case DW_AT_data_member_location:
6176 attrs->at_data_member_location = a;
6177 break;
6178 case DW_AT_decimal_scale:
6179 attrs->at_decimal_scale = a;
6180 break;
6181 case DW_AT_decimal_sign:
6182 attrs->at_decimal_sign = a;
6183 break;
6184 case DW_AT_default_value:
6185 attrs->at_default_value = a;
6186 break;
6187 case DW_AT_digit_count:
6188 attrs->at_digit_count = a;
6189 break;
6190 case DW_AT_discr:
6191 attrs->at_discr = a;
6192 break;
6193 case DW_AT_discr_list:
6194 attrs->at_discr_list = a;
6195 break;
6196 case DW_AT_discr_value:
6197 attrs->at_discr_value = a;
6198 break;
6199 case DW_AT_encoding:
6200 attrs->at_encoding = a;
6201 break;
6202 case DW_AT_endianity:
6203 attrs->at_endianity = a;
6204 break;
6205 case DW_AT_explicit:
6206 attrs->at_explicit = a;
6207 break;
6208 case DW_AT_is_optional:
6209 attrs->at_is_optional = a;
6210 break;
6211 case DW_AT_location:
6212 attrs->at_location = a;
6213 break;
6214 case DW_AT_lower_bound:
6215 attrs->at_lower_bound = a;
6216 break;
6217 case DW_AT_mutable:
6218 attrs->at_mutable = a;
6219 break;
6220 case DW_AT_ordering:
6221 attrs->at_ordering = a;
6222 break;
6223 case DW_AT_picture_string:
6224 attrs->at_picture_string = a;
6225 break;
6226 case DW_AT_prototyped:
6227 attrs->at_prototyped = a;
6228 break;
6229 case DW_AT_small:
6230 attrs->at_small = a;
6231 break;
6232 case DW_AT_segment:
6233 attrs->at_segment = a;
6234 break;
6235 case DW_AT_string_length:
6236 attrs->at_string_length = a;
6237 break;
6238 case DW_AT_threads_scaled:
6239 attrs->at_threads_scaled = a;
6240 break;
6241 case DW_AT_upper_bound:
6242 attrs->at_upper_bound = a;
6243 break;
6244 case DW_AT_use_location:
6245 attrs->at_use_location = a;
6246 break;
6247 case DW_AT_use_UTF8:
6248 attrs->at_use_UTF8 = a;
6249 break;
6250 case DW_AT_variable_parameter:
6251 attrs->at_variable_parameter = a;
6252 break;
6253 case DW_AT_virtuality:
6254 attrs->at_virtuality = a;
6255 break;
6256 case DW_AT_visibility:
6257 attrs->at_visibility = a;
6258 break;
6259 case DW_AT_vtable_elem_location:
6260 attrs->at_vtable_elem_location = a;
6261 break;
6262 default:
6263 break;
6268 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6270 static void
6271 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6273 dw_die_ref c;
6274 dw_die_ref decl;
6275 struct checksum_attributes attrs;
6277 CHECKSUM_ULEB128 ('D');
6278 CHECKSUM_ULEB128 (die->die_tag);
6280 memset (&attrs, 0, sizeof (attrs));
6282 decl = get_AT_ref (die, DW_AT_specification);
6283 if (decl != NULL)
6284 collect_checksum_attributes (&attrs, decl);
6285 collect_checksum_attributes (&attrs, die);
6287 CHECKSUM_ATTR (attrs.at_name);
6288 CHECKSUM_ATTR (attrs.at_accessibility);
6289 CHECKSUM_ATTR (attrs.at_address_class);
6290 CHECKSUM_ATTR (attrs.at_allocated);
6291 CHECKSUM_ATTR (attrs.at_artificial);
6292 CHECKSUM_ATTR (attrs.at_associated);
6293 CHECKSUM_ATTR (attrs.at_binary_scale);
6294 CHECKSUM_ATTR (attrs.at_bit_offset);
6295 CHECKSUM_ATTR (attrs.at_bit_size);
6296 CHECKSUM_ATTR (attrs.at_bit_stride);
6297 CHECKSUM_ATTR (attrs.at_byte_size);
6298 CHECKSUM_ATTR (attrs.at_byte_stride);
6299 CHECKSUM_ATTR (attrs.at_const_value);
6300 CHECKSUM_ATTR (attrs.at_containing_type);
6301 CHECKSUM_ATTR (attrs.at_count);
6302 CHECKSUM_ATTR (attrs.at_data_location);
6303 CHECKSUM_ATTR (attrs.at_data_member_location);
6304 CHECKSUM_ATTR (attrs.at_decimal_scale);
6305 CHECKSUM_ATTR (attrs.at_decimal_sign);
6306 CHECKSUM_ATTR (attrs.at_default_value);
6307 CHECKSUM_ATTR (attrs.at_digit_count);
6308 CHECKSUM_ATTR (attrs.at_discr);
6309 CHECKSUM_ATTR (attrs.at_discr_list);
6310 CHECKSUM_ATTR (attrs.at_discr_value);
6311 CHECKSUM_ATTR (attrs.at_encoding);
6312 CHECKSUM_ATTR (attrs.at_endianity);
6313 CHECKSUM_ATTR (attrs.at_explicit);
6314 CHECKSUM_ATTR (attrs.at_is_optional);
6315 CHECKSUM_ATTR (attrs.at_location);
6316 CHECKSUM_ATTR (attrs.at_lower_bound);
6317 CHECKSUM_ATTR (attrs.at_mutable);
6318 CHECKSUM_ATTR (attrs.at_ordering);
6319 CHECKSUM_ATTR (attrs.at_picture_string);
6320 CHECKSUM_ATTR (attrs.at_prototyped);
6321 CHECKSUM_ATTR (attrs.at_small);
6322 CHECKSUM_ATTR (attrs.at_segment);
6323 CHECKSUM_ATTR (attrs.at_string_length);
6324 CHECKSUM_ATTR (attrs.at_threads_scaled);
6325 CHECKSUM_ATTR (attrs.at_upper_bound);
6326 CHECKSUM_ATTR (attrs.at_use_location);
6327 CHECKSUM_ATTR (attrs.at_use_UTF8);
6328 CHECKSUM_ATTR (attrs.at_variable_parameter);
6329 CHECKSUM_ATTR (attrs.at_virtuality);
6330 CHECKSUM_ATTR (attrs.at_visibility);
6331 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6332 CHECKSUM_ATTR (attrs.at_type);
6333 CHECKSUM_ATTR (attrs.at_friend);
6335 /* Checksum the child DIEs, except for nested types and member functions. */
6336 c = die->die_child;
6337 if (c) do {
6338 dw_attr_ref name_attr;
6340 c = c->die_sib;
6341 name_attr = get_AT (c, DW_AT_name);
6342 if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
6343 && name_attr != NULL)
6345 CHECKSUM_ULEB128 ('S');
6346 CHECKSUM_ULEB128 (c->die_tag);
6347 CHECKSUM_STRING (AT_string (name_attr));
6349 else
6351 /* Mark this DIE so it gets processed when unmarking. */
6352 if (c->die_mark == 0)
6353 c->die_mark = -1;
6354 die_checksum_ordered (c, ctx, mark);
6356 } while (c != die->die_child);
6358 CHECKSUM_ULEB128 (0);
6361 #undef CHECKSUM
6362 #undef CHECKSUM_STRING
6363 #undef CHECKSUM_ATTR
6364 #undef CHECKSUM_LEB128
6365 #undef CHECKSUM_ULEB128
6367 /* Generate the type signature for DIE. This is computed by generating an
6368 MD5 checksum over the DIE's tag, its relevant attributes, and its
6369 children. Attributes that are references to other DIEs are processed
6370 by recursion, using the MARK field to prevent infinite recursion.
6371 If the DIE is nested inside a namespace or another type, we also
6372 need to include that context in the signature. The lower 64 bits
6373 of the resulting MD5 checksum comprise the signature. */
6375 static void
6376 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6378 int mark;
6379 const char *name;
6380 unsigned char checksum[16];
6381 struct md5_ctx ctx;
6382 dw_die_ref decl;
6384 name = get_AT_string (die, DW_AT_name);
6385 decl = get_AT_ref (die, DW_AT_specification);
6387 /* First, compute a signature for just the type name (and its surrounding
6388 context, if any. This is stored in the type unit DIE for link-time
6389 ODR (one-definition rule) checking. */
6391 if (is_cxx() && name != NULL)
6393 md5_init_ctx (&ctx);
6395 /* Checksum the names of surrounding namespaces and structures. */
6396 if (decl != NULL && decl->die_parent != NULL)
6397 checksum_die_context (decl->die_parent, &ctx);
6399 md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
6400 md5_process_bytes (name, strlen (name) + 1, &ctx);
6401 md5_finish_ctx (&ctx, checksum);
6403 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6406 /* Next, compute the complete type signature. */
6408 md5_init_ctx (&ctx);
6409 mark = 1;
6410 die->die_mark = mark;
6412 /* Checksum the names of surrounding namespaces and structures. */
6413 if (decl != NULL && decl->die_parent != NULL)
6414 checksum_die_context (decl->die_parent, &ctx);
6416 /* Checksum the DIE and its children. */
6417 die_checksum_ordered (die, &ctx, &mark);
6418 unmark_all_dies (die);
6419 md5_finish_ctx (&ctx, checksum);
6421 /* Store the signature in the type node and link the type DIE and the
6422 type node together. */
6423 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6424 DWARF_TYPE_SIGNATURE_SIZE);
6425 die->die_id.die_type_node = type_node;
6426 type_node->type_die = die;
6428 /* If the DIE is a specification, link its declaration to the type node
6429 as well. */
6430 if (decl != NULL)
6431 decl->die_id.die_type_node = type_node;
6434 /* Do the location expressions look same? */
6435 static inline int
6436 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6438 return loc1->dw_loc_opc == loc2->dw_loc_opc
6439 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6440 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6443 /* Do the values look the same? */
6444 static int
6445 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6447 dw_loc_descr_ref loc1, loc2;
6448 rtx r1, r2;
6450 if (v1->val_class != v2->val_class)
6451 return 0;
6453 switch (v1->val_class)
6455 case dw_val_class_const:
6456 return v1->v.val_int == v2->v.val_int;
6457 case dw_val_class_unsigned_const:
6458 return v1->v.val_unsigned == v2->v.val_unsigned;
6459 case dw_val_class_const_double:
6460 return v1->v.val_double.high == v2->v.val_double.high
6461 && v1->v.val_double.low == v2->v.val_double.low;
6462 case dw_val_class_vec:
6463 if (v1->v.val_vec.length != v2->v.val_vec.length
6464 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6465 return 0;
6466 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6467 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6468 return 0;
6469 return 1;
6470 case dw_val_class_flag:
6471 return v1->v.val_flag == v2->v.val_flag;
6472 case dw_val_class_str:
6473 return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
6475 case dw_val_class_addr:
6476 r1 = v1->v.val_addr;
6477 r2 = v2->v.val_addr;
6478 if (GET_CODE (r1) != GET_CODE (r2))
6479 return 0;
6480 return !rtx_equal_p (r1, r2);
6482 case dw_val_class_offset:
6483 return v1->v.val_offset == v2->v.val_offset;
6485 case dw_val_class_loc:
6486 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6487 loc1 && loc2;
6488 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6489 if (!same_loc_p (loc1, loc2, mark))
6490 return 0;
6491 return !loc1 && !loc2;
6493 case dw_val_class_die_ref:
6494 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6496 case dw_val_class_fde_ref:
6497 case dw_val_class_vms_delta:
6498 case dw_val_class_lbl_id:
6499 case dw_val_class_lineptr:
6500 case dw_val_class_macptr:
6501 return 1;
6503 case dw_val_class_file:
6504 return v1->v.val_file == v2->v.val_file;
6506 case dw_val_class_data8:
6507 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6509 default:
6510 return 1;
6514 /* Do the attributes look the same? */
6516 static int
6517 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6519 if (at1->dw_attr != at2->dw_attr)
6520 return 0;
6522 /* We don't care that this was compiled with a different compiler
6523 snapshot; if the output is the same, that's what matters. */
6524 if (at1->dw_attr == DW_AT_producer)
6525 return 1;
6527 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6530 /* Do the dies look the same? */
6532 static int
6533 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6535 dw_die_ref c1, c2;
6536 dw_attr_ref a1;
6537 unsigned ix;
6539 /* To avoid infinite recursion. */
6540 if (die1->die_mark)
6541 return die1->die_mark == die2->die_mark;
6542 die1->die_mark = die2->die_mark = ++(*mark);
6544 if (die1->die_tag != die2->die_tag)
6545 return 0;
6547 if (VEC_length (dw_attr_node, die1->die_attr)
6548 != VEC_length (dw_attr_node, die2->die_attr))
6549 return 0;
6551 FOR_EACH_VEC_ELT (dw_attr_node, die1->die_attr, ix, a1)
6552 if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
6553 return 0;
6555 c1 = die1->die_child;
6556 c2 = die2->die_child;
6557 if (! c1)
6559 if (c2)
6560 return 0;
6562 else
6563 for (;;)
6565 if (!same_die_p (c1, c2, mark))
6566 return 0;
6567 c1 = c1->die_sib;
6568 c2 = c2->die_sib;
6569 if (c1 == die1->die_child)
6571 if (c2 == die2->die_child)
6572 break;
6573 else
6574 return 0;
6578 return 1;
6581 /* Do the dies look the same? Wrapper around same_die_p. */
6583 static int
6584 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6586 int mark = 0;
6587 int ret = same_die_p (die1, die2, &mark);
6589 unmark_all_dies (die1);
6590 unmark_all_dies (die2);
6592 return ret;
6595 /* The prefix to attach to symbols on DIEs in the current comdat debug
6596 info section. */
6597 static char *comdat_symbol_id;
6599 /* The index of the current symbol within the current comdat CU. */
6600 static unsigned int comdat_symbol_number;
6602 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6603 children, and set comdat_symbol_id accordingly. */
6605 static void
6606 compute_section_prefix (dw_die_ref unit_die)
6608 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6609 const char *base = die_name ? lbasename (die_name) : "anonymous";
6610 char *name = XALLOCAVEC (char, strlen (base) + 64);
6611 char *p;
6612 int i, mark;
6613 unsigned char checksum[16];
6614 struct md5_ctx ctx;
6616 /* Compute the checksum of the DIE, then append part of it as hex digits to
6617 the name filename of the unit. */
6619 md5_init_ctx (&ctx);
6620 mark = 0;
6621 die_checksum (unit_die, &ctx, &mark);
6622 unmark_all_dies (unit_die);
6623 md5_finish_ctx (&ctx, checksum);
6625 sprintf (name, "%s.", base);
6626 clean_symbol_name (name);
6628 p = name + strlen (name);
6629 for (i = 0; i < 4; i++)
6631 sprintf (p, "%.2x", checksum[i]);
6632 p += 2;
6635 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6636 comdat_symbol_number = 0;
6639 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6641 static int
6642 is_type_die (dw_die_ref die)
6644 switch (die->die_tag)
6646 case DW_TAG_array_type:
6647 case DW_TAG_class_type:
6648 case DW_TAG_interface_type:
6649 case DW_TAG_enumeration_type:
6650 case DW_TAG_pointer_type:
6651 case DW_TAG_reference_type:
6652 case DW_TAG_rvalue_reference_type:
6653 case DW_TAG_string_type:
6654 case DW_TAG_structure_type:
6655 case DW_TAG_subroutine_type:
6656 case DW_TAG_union_type:
6657 case DW_TAG_ptr_to_member_type:
6658 case DW_TAG_set_type:
6659 case DW_TAG_subrange_type:
6660 case DW_TAG_base_type:
6661 case DW_TAG_const_type:
6662 case DW_TAG_file_type:
6663 case DW_TAG_packed_type:
6664 case DW_TAG_volatile_type:
6665 case DW_TAG_typedef:
6666 return 1;
6667 default:
6668 return 0;
6672 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6673 Basically, we want to choose the bits that are likely to be shared between
6674 compilations (types) and leave out the bits that are specific to individual
6675 compilations (functions). */
6677 static int
6678 is_comdat_die (dw_die_ref c)
6680 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6681 we do for stabs. The advantage is a greater likelihood of sharing between
6682 objects that don't include headers in the same order (and therefore would
6683 put the base types in a different comdat). jason 8/28/00 */
6685 if (c->die_tag == DW_TAG_base_type)
6686 return 0;
6688 if (c->die_tag == DW_TAG_pointer_type
6689 || c->die_tag == DW_TAG_reference_type
6690 || c->die_tag == DW_TAG_rvalue_reference_type
6691 || c->die_tag == DW_TAG_const_type
6692 || c->die_tag == DW_TAG_volatile_type)
6694 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6696 return t ? is_comdat_die (t) : 0;
6699 return is_type_die (c);
6702 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6703 compilation unit. */
6705 static int
6706 is_symbol_die (dw_die_ref c)
6708 return (is_type_die (c)
6709 || is_declaration_die (c)
6710 || c->die_tag == DW_TAG_namespace
6711 || c->die_tag == DW_TAG_module);
6714 /* Returns true iff C is a compile-unit DIE. */
6716 static inline bool
6717 is_cu_die (dw_die_ref c)
6719 return c && c->die_tag == DW_TAG_compile_unit;
6722 static char *
6723 gen_internal_sym (const char *prefix)
6725 char buf[256];
6727 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6728 return xstrdup (buf);
6731 /* Assign symbols to all worthy DIEs under DIE. */
6733 static void
6734 assign_symbol_names (dw_die_ref die)
6736 dw_die_ref c;
6738 if (is_symbol_die (die))
6740 if (comdat_symbol_id)
6742 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6744 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6745 comdat_symbol_id, comdat_symbol_number++);
6746 die->die_id.die_symbol = xstrdup (p);
6748 else
6749 die->die_id.die_symbol = gen_internal_sym ("LDIE");
6752 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6755 struct cu_hash_table_entry
6757 dw_die_ref cu;
6758 unsigned min_comdat_num, max_comdat_num;
6759 struct cu_hash_table_entry *next;
6762 /* Routines to manipulate hash table of CUs. */
6763 static hashval_t
6764 htab_cu_hash (const void *of)
6766 const struct cu_hash_table_entry *const entry =
6767 (const struct cu_hash_table_entry *) of;
6769 return htab_hash_string (entry->cu->die_id.die_symbol);
6772 static int
6773 htab_cu_eq (const void *of1, const void *of2)
6775 const struct cu_hash_table_entry *const entry1 =
6776 (const struct cu_hash_table_entry *) of1;
6777 const struct die_struct *const entry2 = (const struct die_struct *) of2;
6779 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6782 static void
6783 htab_cu_del (void *what)
6785 struct cu_hash_table_entry *next,
6786 *entry = (struct cu_hash_table_entry *) what;
6788 while (entry)
6790 next = entry->next;
6791 free (entry);
6792 entry = next;
6796 /* Check whether we have already seen this CU and set up SYM_NUM
6797 accordingly. */
6798 static int
6799 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
6801 struct cu_hash_table_entry dummy;
6802 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6804 dummy.max_comdat_num = 0;
6806 slot = (struct cu_hash_table_entry **)
6807 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
6808 INSERT);
6809 entry = *slot;
6811 for (; entry; last = entry, entry = entry->next)
6813 if (same_die_p_wrap (cu, entry->cu))
6814 break;
6817 if (entry)
6819 *sym_num = entry->min_comdat_num;
6820 return 1;
6823 entry = XCNEW (struct cu_hash_table_entry);
6824 entry->cu = cu;
6825 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6826 entry->next = *slot;
6827 *slot = entry;
6829 return 0;
6832 /* Record SYM_NUM to record of CU in HTABLE. */
6833 static void
6834 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
6836 struct cu_hash_table_entry **slot, *entry;
6838 slot = (struct cu_hash_table_entry **)
6839 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
6840 NO_INSERT);
6841 entry = *slot;
6843 entry->max_comdat_num = sym_num;
6846 /* Traverse the DIE (which is always comp_unit_die), and set up
6847 additional compilation units for each of the include files we see
6848 bracketed by BINCL/EINCL. */
6850 static void
6851 break_out_includes (dw_die_ref die)
6853 dw_die_ref c;
6854 dw_die_ref unit = NULL;
6855 limbo_die_node *node, **pnode;
6856 htab_t cu_hash_table;
6858 c = die->die_child;
6859 if (c) do {
6860 dw_die_ref prev = c;
6861 c = c->die_sib;
6862 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6863 || (unit && is_comdat_die (c)))
6865 dw_die_ref next = c->die_sib;
6867 /* This DIE is for a secondary CU; remove it from the main one. */
6868 remove_child_with_prev (c, prev);
6870 if (c->die_tag == DW_TAG_GNU_BINCL)
6871 unit = push_new_compile_unit (unit, c);
6872 else if (c->die_tag == DW_TAG_GNU_EINCL)
6873 unit = pop_compile_unit (unit);
6874 else
6875 add_child_die (unit, c);
6876 c = next;
6877 if (c == die->die_child)
6878 break;
6880 } while (c != die->die_child);
6882 #if 0
6883 /* We can only use this in debugging, since the frontend doesn't check
6884 to make sure that we leave every include file we enter. */
6885 gcc_assert (!unit);
6886 #endif
6888 assign_symbol_names (die);
6889 cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6890 for (node = limbo_die_list, pnode = &limbo_die_list;
6891 node;
6892 node = node->next)
6894 int is_dupl;
6896 compute_section_prefix (node->die);
6897 is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6898 &comdat_symbol_number);
6899 assign_symbol_names (node->die);
6900 if (is_dupl)
6901 *pnode = node->next;
6902 else
6904 pnode = &node->next;
6905 record_comdat_symbol_number (node->die, cu_hash_table,
6906 comdat_symbol_number);
6909 htab_delete (cu_hash_table);
6912 /* Return non-zero if this DIE is a declaration. */
6914 static int
6915 is_declaration_die (dw_die_ref die)
6917 dw_attr_ref a;
6918 unsigned ix;
6920 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
6921 if (a->dw_attr == DW_AT_declaration)
6922 return 1;
6924 return 0;
6927 /* Return non-zero if this DIE is nested inside a subprogram. */
6929 static int
6930 is_nested_in_subprogram (dw_die_ref die)
6932 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
6934 if (decl == NULL)
6935 decl = die;
6936 return local_scope_p (decl);
6939 /* Return non-zero if this DIE contains a defining declaration of a
6940 subprogram. */
6942 static int
6943 contains_subprogram_definition (dw_die_ref die)
6945 dw_die_ref c;
6947 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
6948 return 1;
6949 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition(c)) return 1);
6950 return 0;
6953 /* Return non-zero if this is a type DIE that should be moved to a
6954 COMDAT .debug_types section. */
6956 static int
6957 should_move_die_to_comdat (dw_die_ref die)
6959 switch (die->die_tag)
6961 case DW_TAG_class_type:
6962 case DW_TAG_structure_type:
6963 case DW_TAG_enumeration_type:
6964 case DW_TAG_union_type:
6965 /* Don't move declarations, inlined instances, or types nested in a
6966 subprogram. */
6967 if (is_declaration_die (die)
6968 || get_AT (die, DW_AT_abstract_origin)
6969 || is_nested_in_subprogram (die))
6970 return 0;
6971 /* A type definition should never contain a subprogram definition. */
6972 gcc_assert (!contains_subprogram_definition (die));
6973 return 1;
6974 case DW_TAG_array_type:
6975 case DW_TAG_interface_type:
6976 case DW_TAG_pointer_type:
6977 case DW_TAG_reference_type:
6978 case DW_TAG_rvalue_reference_type:
6979 case DW_TAG_string_type:
6980 case DW_TAG_subroutine_type:
6981 case DW_TAG_ptr_to_member_type:
6982 case DW_TAG_set_type:
6983 case DW_TAG_subrange_type:
6984 case DW_TAG_base_type:
6985 case DW_TAG_const_type:
6986 case DW_TAG_file_type:
6987 case DW_TAG_packed_type:
6988 case DW_TAG_volatile_type:
6989 case DW_TAG_typedef:
6990 default:
6991 return 0;
6995 /* Make a clone of DIE. */
6997 static dw_die_ref
6998 clone_die (dw_die_ref die)
7000 dw_die_ref clone;
7001 dw_attr_ref a;
7002 unsigned ix;
7004 clone = ggc_alloc_cleared_die_node ();
7005 clone->die_tag = die->die_tag;
7007 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7008 add_dwarf_attr (clone, a);
7010 return clone;
7013 /* Make a clone of the tree rooted at DIE. */
7015 static dw_die_ref
7016 clone_tree (dw_die_ref die)
7018 dw_die_ref c;
7019 dw_die_ref clone = clone_die (die);
7021 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
7023 return clone;
7026 /* Make a clone of DIE as a declaration. */
7028 static dw_die_ref
7029 clone_as_declaration (dw_die_ref die)
7031 dw_die_ref clone;
7032 dw_die_ref decl;
7033 dw_attr_ref a;
7034 unsigned ix;
7036 /* If the DIE is already a declaration, just clone it. */
7037 if (is_declaration_die (die))
7038 return clone_die (die);
7040 /* If the DIE is a specification, just clone its declaration DIE. */
7041 decl = get_AT_ref (die, DW_AT_specification);
7042 if (decl != NULL)
7043 return clone_die (decl);
7045 clone = ggc_alloc_cleared_die_node ();
7046 clone->die_tag = die->die_tag;
7048 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7050 /* We don't want to copy over all attributes.
7051 For example we don't want DW_AT_byte_size because otherwise we will no
7052 longer have a declaration and GDB will treat it as a definition. */
7054 switch (a->dw_attr)
7056 case DW_AT_artificial:
7057 case DW_AT_containing_type:
7058 case DW_AT_external:
7059 case DW_AT_name:
7060 case DW_AT_type:
7061 case DW_AT_virtuality:
7062 case DW_AT_linkage_name:
7063 case DW_AT_MIPS_linkage_name:
7064 add_dwarf_attr (clone, a);
7065 break;
7066 case DW_AT_byte_size:
7067 default:
7068 break;
7072 if (die->die_id.die_type_node)
7073 add_AT_die_ref (clone, DW_AT_signature, die);
7075 add_AT_flag (clone, DW_AT_declaration, 1);
7076 return clone;
7079 /* Copy the declaration context to the new compile unit DIE. This includes
7080 any surrounding namespace or type declarations. If the DIE has an
7081 AT_specification attribute, it also includes attributes and children
7082 attached to the specification. */
7084 static void
7085 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7087 dw_die_ref decl;
7088 dw_die_ref new_decl;
7090 decl = get_AT_ref (die, DW_AT_specification);
7091 if (decl == NULL)
7092 decl = die;
7093 else
7095 unsigned ix;
7096 dw_die_ref c;
7097 dw_attr_ref a;
7099 /* Copy the type node pointer from the new DIE to the original
7100 declaration DIE so we can forward references later. */
7101 decl->die_id.die_type_node = die->die_id.die_type_node;
7103 remove_AT (die, DW_AT_specification);
7105 FOR_EACH_VEC_ELT (dw_attr_node, decl->die_attr, ix, a)
7107 if (a->dw_attr != DW_AT_name
7108 && a->dw_attr != DW_AT_declaration
7109 && a->dw_attr != DW_AT_external)
7110 add_dwarf_attr (die, a);
7113 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
7116 if (decl->die_parent != NULL
7117 && decl->die_parent->die_tag != DW_TAG_compile_unit
7118 && decl->die_parent->die_tag != DW_TAG_type_unit)
7120 new_decl = copy_ancestor_tree (unit, decl, NULL);
7121 if (new_decl != NULL)
7123 remove_AT (new_decl, DW_AT_signature);
7124 add_AT_specification (die, new_decl);
7129 /* Generate the skeleton ancestor tree for the given NODE, then clone
7130 the DIE and add the clone into the tree. */
7132 static void
7133 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7135 if (node->new_die != NULL)
7136 return;
7138 node->new_die = clone_as_declaration (node->old_die);
7140 if (node->parent != NULL)
7142 generate_skeleton_ancestor_tree (node->parent);
7143 add_child_die (node->parent->new_die, node->new_die);
7147 /* Generate a skeleton tree of DIEs containing any declarations that are
7148 found in the original tree. We traverse the tree looking for declaration
7149 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7151 static void
7152 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7154 skeleton_chain_node node;
7155 dw_die_ref c;
7156 dw_die_ref first;
7157 dw_die_ref prev = NULL;
7158 dw_die_ref next = NULL;
7160 node.parent = parent;
7162 first = c = parent->old_die->die_child;
7163 if (c)
7164 next = c->die_sib;
7165 if (c) do {
7166 if (prev == NULL || prev->die_sib == c)
7167 prev = c;
7168 c = next;
7169 next = (c == first ? NULL : c->die_sib);
7170 node.old_die = c;
7171 node.new_die = NULL;
7172 if (is_declaration_die (c))
7174 /* Clone the existing DIE, move the original to the skeleton
7175 tree (which is in the main CU), and put the clone, with
7176 all the original's children, where the original came from. */
7177 dw_die_ref clone = clone_die (c);
7178 move_all_children (c, clone);
7180 replace_child (c, clone, prev);
7181 generate_skeleton_ancestor_tree (parent);
7182 add_child_die (parent->new_die, c);
7183 node.new_die = c;
7184 c = clone;
7186 generate_skeleton_bottom_up (&node);
7187 } while (next != NULL);
7190 /* Wrapper function for generate_skeleton_bottom_up. */
7192 static dw_die_ref
7193 generate_skeleton (dw_die_ref die)
7195 skeleton_chain_node node;
7197 node.old_die = die;
7198 node.new_die = NULL;
7199 node.parent = NULL;
7201 /* If this type definition is nested inside another type,
7202 always leave at least a declaration in its place. */
7203 if (die->die_parent != NULL && is_type_die (die->die_parent))
7204 node.new_die = clone_as_declaration (die);
7206 generate_skeleton_bottom_up (&node);
7207 return node.new_die;
7210 /* Remove the DIE from its parent, possibly replacing it with a cloned
7211 declaration. The original DIE will be moved to a new compile unit
7212 so that existing references to it follow it to the new location. If
7213 any of the original DIE's descendants is a declaration, we need to
7214 replace the original DIE with a skeleton tree and move the
7215 declarations back into the skeleton tree. */
7217 static dw_die_ref
7218 remove_child_or_replace_with_skeleton (dw_die_ref child, dw_die_ref prev)
7220 dw_die_ref skeleton;
7222 skeleton = generate_skeleton (child);
7223 if (skeleton == NULL)
7224 remove_child_with_prev (child, prev);
7225 else
7227 skeleton->die_id.die_type_node = child->die_id.die_type_node;
7228 replace_child (child, skeleton, prev);
7231 return skeleton;
7234 /* Traverse the DIE and set up additional .debug_types sections for each
7235 type worthy of being placed in a COMDAT section. */
7237 static void
7238 break_out_comdat_types (dw_die_ref die)
7240 dw_die_ref c;
7241 dw_die_ref first;
7242 dw_die_ref prev = NULL;
7243 dw_die_ref next = NULL;
7244 dw_die_ref unit = NULL;
7246 first = c = die->die_child;
7247 if (c)
7248 next = c->die_sib;
7249 if (c) do {
7250 if (prev == NULL || prev->die_sib == c)
7251 prev = c;
7252 c = next;
7253 next = (c == first ? NULL : c->die_sib);
7254 if (should_move_die_to_comdat (c))
7256 dw_die_ref replacement;
7257 comdat_type_node_ref type_node;
7259 /* Create a new type unit DIE as the root for the new tree, and
7260 add it to the list of comdat types. */
7261 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7262 add_AT_unsigned (unit, DW_AT_language,
7263 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7264 type_node = ggc_alloc_cleared_comdat_type_node ();
7265 type_node->root_die = unit;
7266 type_node->next = comdat_type_list;
7267 comdat_type_list = type_node;
7269 /* Generate the type signature. */
7270 generate_type_signature (c, type_node);
7272 /* Copy the declaration context, attributes, and children of the
7273 declaration into the new compile unit DIE. */
7274 copy_declaration_context (unit, c);
7276 /* Remove this DIE from the main CU. */
7277 replacement = remove_child_or_replace_with_skeleton (c, prev);
7279 /* Break out nested types into their own type units. */
7280 break_out_comdat_types (c);
7282 /* Add the DIE to the new compunit. */
7283 add_child_die (unit, c);
7285 if (replacement != NULL)
7286 c = replacement;
7288 else if (c->die_tag == DW_TAG_namespace
7289 || c->die_tag == DW_TAG_class_type
7290 || c->die_tag == DW_TAG_structure_type
7291 || c->die_tag == DW_TAG_union_type)
7293 /* Look for nested types that can be broken out. */
7294 break_out_comdat_types (c);
7296 } while (next != NULL);
7299 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7301 struct decl_table_entry
7303 dw_die_ref orig;
7304 dw_die_ref copy;
7307 /* Routines to manipulate hash table of copied declarations. */
7309 static hashval_t
7310 htab_decl_hash (const void *of)
7312 const struct decl_table_entry *const entry =
7313 (const struct decl_table_entry *) of;
7315 return htab_hash_pointer (entry->orig);
7318 static int
7319 htab_decl_eq (const void *of1, const void *of2)
7321 const struct decl_table_entry *const entry1 =
7322 (const struct decl_table_entry *) of1;
7323 const struct die_struct *const entry2 = (const struct die_struct *) of2;
7325 return entry1->orig == entry2;
7328 static void
7329 htab_decl_del (void *what)
7331 struct decl_table_entry *entry = (struct decl_table_entry *) what;
7333 free (entry);
7336 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7337 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7338 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7339 to check if the ancestor has already been copied into UNIT. */
7341 static dw_die_ref
7342 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
7344 dw_die_ref parent = die->die_parent;
7345 dw_die_ref new_parent = unit;
7346 dw_die_ref copy;
7347 void **slot = NULL;
7348 struct decl_table_entry *entry = NULL;
7350 if (decl_table)
7352 /* Check if the entry has already been copied to UNIT. */
7353 slot = htab_find_slot_with_hash (decl_table, die,
7354 htab_hash_pointer (die), INSERT);
7355 if (*slot != HTAB_EMPTY_ENTRY)
7357 entry = (struct decl_table_entry *) *slot;
7358 return entry->copy;
7361 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7362 entry = XCNEW (struct decl_table_entry);
7363 entry->orig = die;
7364 entry->copy = NULL;
7365 *slot = entry;
7368 if (parent != NULL)
7370 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7371 if (spec != NULL)
7372 parent = spec;
7373 if (parent->die_tag != DW_TAG_compile_unit
7374 && parent->die_tag != DW_TAG_type_unit)
7375 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7378 copy = clone_as_declaration (die);
7379 add_child_die (new_parent, copy);
7381 if (decl_table != NULL)
7383 /* Record the pointer to the copy. */
7384 entry->copy = copy;
7387 return copy;
7390 /* Walk the DIE and its children, looking for references to incomplete
7391 or trivial types that are unmarked (i.e., that are not in the current
7392 type_unit). */
7394 static void
7395 copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
7397 dw_die_ref c;
7398 dw_attr_ref a;
7399 unsigned ix;
7401 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7403 if (AT_class (a) == dw_val_class_die_ref)
7405 dw_die_ref targ = AT_ref (a);
7406 comdat_type_node_ref type_node = targ->die_id.die_type_node;
7407 void **slot;
7408 struct decl_table_entry *entry;
7410 if (targ->die_mark != 0 || type_node != NULL)
7411 continue;
7413 slot = htab_find_slot_with_hash (decl_table, targ,
7414 htab_hash_pointer (targ), INSERT);
7416 if (*slot != HTAB_EMPTY_ENTRY)
7418 /* TARG has already been copied, so we just need to
7419 modify the reference to point to the copy. */
7420 entry = (struct decl_table_entry *) *slot;
7421 a->dw_attr_val.v.val_die_ref.die = entry->copy;
7423 else
7425 dw_die_ref parent = unit;
7426 dw_die_ref copy = clone_tree (targ);
7428 /* Make sure the cloned tree is marked as part of the
7429 type unit. */
7430 mark_dies (copy);
7432 /* Record in DECL_TABLE that TARG has been copied.
7433 Need to do this now, before the recursive call,
7434 because DECL_TABLE may be expanded and SLOT
7435 would no longer be a valid pointer. */
7436 entry = XCNEW (struct decl_table_entry);
7437 entry->orig = targ;
7438 entry->copy = copy;
7439 *slot = entry;
7441 /* If TARG has surrounding context, copy its ancestor tree
7442 into the new type unit. */
7443 if (targ->die_parent != NULL
7444 && targ->die_parent->die_tag != DW_TAG_compile_unit
7445 && targ->die_parent->die_tag != DW_TAG_type_unit)
7446 parent = copy_ancestor_tree (unit, targ->die_parent,
7447 decl_table);
7449 add_child_die (parent, copy);
7450 a->dw_attr_val.v.val_die_ref.die = copy;
7452 /* Make sure the newly-copied DIE is walked. If it was
7453 installed in a previously-added context, it won't
7454 get visited otherwise. */
7455 if (parent != unit)
7457 /* Find the highest point of the newly-added tree,
7458 mark each node along the way, and walk from there. */
7459 parent->die_mark = 1;
7460 while (parent->die_parent
7461 && parent->die_parent->die_mark == 0)
7463 parent = parent->die_parent;
7464 parent->die_mark = 1;
7466 copy_decls_walk (unit, parent, decl_table);
7472 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7475 /* Copy declarations for "unworthy" types into the new comdat section.
7476 Incomplete types, modified types, and certain other types aren't broken
7477 out into comdat sections of their own, so they don't have a signature,
7478 and we need to copy the declaration into the same section so that we
7479 don't have an external reference. */
7481 static void
7482 copy_decls_for_unworthy_types (dw_die_ref unit)
7484 htab_t decl_table;
7486 mark_dies (unit);
7487 decl_table = htab_create (10, htab_decl_hash, htab_decl_eq, htab_decl_del);
7488 copy_decls_walk (unit, unit, decl_table);
7489 htab_delete (decl_table);
7490 unmark_dies (unit);
7493 /* Traverse the DIE and add a sibling attribute if it may have the
7494 effect of speeding up access to siblings. To save some space,
7495 avoid generating sibling attributes for DIE's without children. */
7497 static void
7498 add_sibling_attributes (dw_die_ref die)
7500 dw_die_ref c;
7502 if (! die->die_child)
7503 return;
7505 if (die->die_parent && die != die->die_parent->die_child)
7506 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7508 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7511 /* Output all location lists for the DIE and its children. */
7513 static void
7514 output_location_lists (dw_die_ref die)
7516 dw_die_ref c;
7517 dw_attr_ref a;
7518 unsigned ix;
7520 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7521 if (AT_class (a) == dw_val_class_loc_list)
7522 output_loc_list (AT_loc_list (a));
7524 FOR_EACH_CHILD (die, c, output_location_lists (c));
7527 /* The format of each DIE (and its attribute value pairs) is encoded in an
7528 abbreviation table. This routine builds the abbreviation table and assigns
7529 a unique abbreviation id for each abbreviation entry. The children of each
7530 die are visited recursively. */
7532 static void
7533 build_abbrev_table (dw_die_ref die)
7535 unsigned long abbrev_id;
7536 unsigned int n_alloc;
7537 dw_die_ref c;
7538 dw_attr_ref a;
7539 unsigned ix;
7541 /* Scan the DIE references, and mark as external any that refer to
7542 DIEs from other CUs (i.e. those which are not marked). */
7543 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7544 if (AT_class (a) == dw_val_class_die_ref
7545 && AT_ref (a)->die_mark == 0)
7547 gcc_assert (use_debug_types || AT_ref (a)->die_id.die_symbol);
7548 set_AT_ref_external (a, 1);
7551 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7553 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7554 dw_attr_ref die_a, abbrev_a;
7555 unsigned ix;
7556 bool ok = true;
7558 if (abbrev->die_tag != die->die_tag)
7559 continue;
7560 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7561 continue;
7563 if (VEC_length (dw_attr_node, abbrev->die_attr)
7564 != VEC_length (dw_attr_node, die->die_attr))
7565 continue;
7567 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, die_a)
7569 abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
7570 if ((abbrev_a->dw_attr != die_a->dw_attr)
7571 || (value_format (abbrev_a) != value_format (die_a)))
7573 ok = false;
7574 break;
7577 if (ok)
7578 break;
7581 if (abbrev_id >= abbrev_die_table_in_use)
7583 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7585 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7586 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7587 n_alloc);
7589 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7590 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7591 abbrev_die_table_allocated = n_alloc;
7594 ++abbrev_die_table_in_use;
7595 abbrev_die_table[abbrev_id] = die;
7598 die->die_abbrev = abbrev_id;
7599 FOR_EACH_CHILD (die, c, build_abbrev_table (c));
7602 /* Return the power-of-two number of bytes necessary to represent VALUE. */
7604 static int
7605 constant_size (unsigned HOST_WIDE_INT value)
7607 int log;
7609 if (value == 0)
7610 log = 0;
7611 else
7612 log = floor_log2 (value);
7614 log = log / 8;
7615 log = 1 << (floor_log2 (log) + 1);
7617 return log;
7620 /* Return the size of a DIE as it is represented in the
7621 .debug_info section. */
7623 static unsigned long
7624 size_of_die (dw_die_ref die)
7626 unsigned long size = 0;
7627 dw_attr_ref a;
7628 unsigned ix;
7630 size += size_of_uleb128 (die->die_abbrev);
7631 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7633 switch (AT_class (a))
7635 case dw_val_class_addr:
7636 size += DWARF2_ADDR_SIZE;
7637 break;
7638 case dw_val_class_offset:
7639 size += DWARF_OFFSET_SIZE;
7640 break;
7641 case dw_val_class_loc:
7643 unsigned long lsize = size_of_locs (AT_loc (a));
7645 /* Block length. */
7646 if (dwarf_version >= 4)
7647 size += size_of_uleb128 (lsize);
7648 else
7649 size += constant_size (lsize);
7650 size += lsize;
7652 break;
7653 case dw_val_class_loc_list:
7654 size += DWARF_OFFSET_SIZE;
7655 break;
7656 case dw_val_class_range_list:
7657 size += DWARF_OFFSET_SIZE;
7658 break;
7659 case dw_val_class_const:
7660 size += size_of_sleb128 (AT_int (a));
7661 break;
7662 case dw_val_class_unsigned_const:
7664 int csize = constant_size (AT_unsigned (a));
7665 if (dwarf_version == 3
7666 && a->dw_attr == DW_AT_data_member_location
7667 && csize >= 4)
7668 size += size_of_uleb128 (AT_unsigned (a));
7669 else
7670 size += csize;
7672 break;
7673 case dw_val_class_const_double:
7674 size += 2 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
7675 if (HOST_BITS_PER_WIDE_INT >= 64)
7676 size++; /* block */
7677 break;
7678 case dw_val_class_vec:
7679 size += constant_size (a->dw_attr_val.v.val_vec.length
7680 * a->dw_attr_val.v.val_vec.elt_size)
7681 + a->dw_attr_val.v.val_vec.length
7682 * a->dw_attr_val.v.val_vec.elt_size; /* block */
7683 break;
7684 case dw_val_class_flag:
7685 if (dwarf_version >= 4)
7686 /* Currently all add_AT_flag calls pass in 1 as last argument,
7687 so DW_FORM_flag_present can be used. If that ever changes,
7688 we'll need to use DW_FORM_flag and have some optimization
7689 in build_abbrev_table that will change those to
7690 DW_FORM_flag_present if it is set to 1 in all DIEs using
7691 the same abbrev entry. */
7692 gcc_assert (a->dw_attr_val.v.val_flag == 1);
7693 else
7694 size += 1;
7695 break;
7696 case dw_val_class_die_ref:
7697 if (AT_ref_external (a))
7699 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
7700 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
7701 is sized by target address length, whereas in DWARF3
7702 it's always sized as an offset. */
7703 if (use_debug_types)
7704 size += DWARF_TYPE_SIGNATURE_SIZE;
7705 else if (dwarf_version == 2)
7706 size += DWARF2_ADDR_SIZE;
7707 else
7708 size += DWARF_OFFSET_SIZE;
7710 else
7711 size += DWARF_OFFSET_SIZE;
7712 break;
7713 case dw_val_class_fde_ref:
7714 size += DWARF_OFFSET_SIZE;
7715 break;
7716 case dw_val_class_lbl_id:
7717 size += DWARF2_ADDR_SIZE;
7718 break;
7719 case dw_val_class_lineptr:
7720 case dw_val_class_macptr:
7721 size += DWARF_OFFSET_SIZE;
7722 break;
7723 case dw_val_class_str:
7724 if (AT_string_form (a) == DW_FORM_strp)
7725 size += DWARF_OFFSET_SIZE;
7726 else
7727 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7728 break;
7729 case dw_val_class_file:
7730 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7731 break;
7732 case dw_val_class_data8:
7733 size += 8;
7734 break;
7735 case dw_val_class_vms_delta:
7736 size += DWARF_OFFSET_SIZE;
7737 break;
7738 default:
7739 gcc_unreachable ();
7743 return size;
7746 /* Size the debugging information associated with a given DIE. Visits the
7747 DIE's children recursively. Updates the global variable next_die_offset, on
7748 each time through. Uses the current value of next_die_offset to update the
7749 die_offset field in each DIE. */
7751 static void
7752 calc_die_sizes (dw_die_ref die)
7754 dw_die_ref c;
7756 gcc_assert (die->die_offset == 0
7757 || (unsigned long int) die->die_offset == next_die_offset);
7758 die->die_offset = next_die_offset;
7759 next_die_offset += size_of_die (die);
7761 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
7763 if (die->die_child != NULL)
7764 /* Count the null byte used to terminate sibling lists. */
7765 next_die_offset += 1;
7768 /* Size just the base type children at the start of the CU.
7769 This is needed because build_abbrev needs to size locs
7770 and sizing of type based stack ops needs to know die_offset
7771 values for the base types. */
7773 static void
7774 calc_base_type_die_sizes (void)
7776 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7777 unsigned int i;
7778 dw_die_ref base_type;
7779 #if ENABLE_ASSERT_CHECKING
7780 dw_die_ref prev = comp_unit_die ()->die_child;
7781 #endif
7783 die_offset += size_of_die (comp_unit_die ());
7784 for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
7786 #if ENABLE_ASSERT_CHECKING
7787 gcc_assert (base_type->die_offset == 0
7788 && prev->die_sib == base_type
7789 && base_type->die_child == NULL
7790 && base_type->die_abbrev);
7791 prev = base_type;
7792 #endif
7793 base_type->die_offset = die_offset;
7794 die_offset += size_of_die (base_type);
7798 /* Set the marks for a die and its children. We do this so
7799 that we know whether or not a reference needs to use FORM_ref_addr; only
7800 DIEs in the same CU will be marked. We used to clear out the offset
7801 and use that as the flag, but ran into ordering problems. */
7803 static void
7804 mark_dies (dw_die_ref die)
7806 dw_die_ref c;
7808 gcc_assert (!die->die_mark);
7810 die->die_mark = 1;
7811 FOR_EACH_CHILD (die, c, mark_dies (c));
7814 /* Clear the marks for a die and its children. */
7816 static void
7817 unmark_dies (dw_die_ref die)
7819 dw_die_ref c;
7821 if (! use_debug_types)
7822 gcc_assert (die->die_mark);
7824 die->die_mark = 0;
7825 FOR_EACH_CHILD (die, c, unmark_dies (c));
7828 /* Clear the marks for a die, its children and referred dies. */
7830 static void
7831 unmark_all_dies (dw_die_ref die)
7833 dw_die_ref c;
7834 dw_attr_ref a;
7835 unsigned ix;
7837 if (!die->die_mark)
7838 return;
7839 die->die_mark = 0;
7841 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
7843 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7844 if (AT_class (a) == dw_val_class_die_ref)
7845 unmark_all_dies (AT_ref (a));
7848 /* Return the size of the .debug_pubnames or .debug_pubtypes table
7849 generated for the compilation unit. */
7851 static unsigned long
7852 size_of_pubnames (VEC (pubname_entry, gc) * names)
7854 unsigned long size;
7855 unsigned i;
7856 pubname_ref p;
7858 size = DWARF_PUBNAMES_HEADER_SIZE;
7859 FOR_EACH_VEC_ELT (pubname_entry, names, i, p)
7860 if (names != pubtype_table
7861 || p->die->die_offset != 0
7862 || !flag_eliminate_unused_debug_types)
7863 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
7865 size += DWARF_OFFSET_SIZE;
7866 return size;
7869 /* Return the size of the information in the .debug_aranges section. */
7871 static unsigned long
7872 size_of_aranges (void)
7874 unsigned long size;
7876 size = DWARF_ARANGES_HEADER_SIZE;
7878 /* Count the address/length pair for this compilation unit. */
7879 if (text_section_used)
7880 size += 2 * DWARF2_ADDR_SIZE;
7881 if (cold_text_section_used)
7882 size += 2 * DWARF2_ADDR_SIZE;
7883 if (have_multiple_function_sections)
7885 unsigned fde_idx;
7886 dw_fde_ref fde;
7888 FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, fde_idx, fde)
7890 if (!fde->in_std_section)
7891 size += 2 * DWARF2_ADDR_SIZE;
7892 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
7893 size += 2 * DWARF2_ADDR_SIZE;
7897 /* Count the two zero words used to terminated the address range table. */
7898 size += 2 * DWARF2_ADDR_SIZE;
7899 return size;
7902 /* Select the encoding of an attribute value. */
7904 static enum dwarf_form
7905 value_format (dw_attr_ref a)
7907 switch (a->dw_attr_val.val_class)
7909 case dw_val_class_addr:
7910 /* Only very few attributes allow DW_FORM_addr. */
7911 switch (a->dw_attr)
7913 case DW_AT_low_pc:
7914 case DW_AT_high_pc:
7915 case DW_AT_entry_pc:
7916 case DW_AT_trampoline:
7917 return DW_FORM_addr;
7918 default:
7919 break;
7921 switch (DWARF2_ADDR_SIZE)
7923 case 1:
7924 return DW_FORM_data1;
7925 case 2:
7926 return DW_FORM_data2;
7927 case 4:
7928 return DW_FORM_data4;
7929 case 8:
7930 return DW_FORM_data8;
7931 default:
7932 gcc_unreachable ();
7934 case dw_val_class_range_list:
7935 case dw_val_class_loc_list:
7936 if (dwarf_version >= 4)
7937 return DW_FORM_sec_offset;
7938 /* FALLTHRU */
7939 case dw_val_class_vms_delta:
7940 case dw_val_class_offset:
7941 switch (DWARF_OFFSET_SIZE)
7943 case 4:
7944 return DW_FORM_data4;
7945 case 8:
7946 return DW_FORM_data8;
7947 default:
7948 gcc_unreachable ();
7950 case dw_val_class_loc:
7951 if (dwarf_version >= 4)
7952 return DW_FORM_exprloc;
7953 switch (constant_size (size_of_locs (AT_loc (a))))
7955 case 1:
7956 return DW_FORM_block1;
7957 case 2:
7958 return DW_FORM_block2;
7959 default:
7960 gcc_unreachable ();
7962 case dw_val_class_const:
7963 return DW_FORM_sdata;
7964 case dw_val_class_unsigned_const:
7965 switch (constant_size (AT_unsigned (a)))
7967 case 1:
7968 return DW_FORM_data1;
7969 case 2:
7970 return DW_FORM_data2;
7971 case 4:
7972 /* In DWARF3 DW_AT_data_member_location with
7973 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
7974 constant, so we need to use DW_FORM_udata if we need
7975 a large constant. */
7976 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
7977 return DW_FORM_udata;
7978 return DW_FORM_data4;
7979 case 8:
7980 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
7981 return DW_FORM_udata;
7982 return DW_FORM_data8;
7983 default:
7984 gcc_unreachable ();
7986 case dw_val_class_const_double:
7987 switch (HOST_BITS_PER_WIDE_INT)
7989 case 8:
7990 return DW_FORM_data2;
7991 case 16:
7992 return DW_FORM_data4;
7993 case 32:
7994 return DW_FORM_data8;
7995 case 64:
7996 default:
7997 return DW_FORM_block1;
7999 case dw_val_class_vec:
8000 switch (constant_size (a->dw_attr_val.v.val_vec.length
8001 * a->dw_attr_val.v.val_vec.elt_size))
8003 case 1:
8004 return DW_FORM_block1;
8005 case 2:
8006 return DW_FORM_block2;
8007 case 4:
8008 return DW_FORM_block4;
8009 default:
8010 gcc_unreachable ();
8012 case dw_val_class_flag:
8013 if (dwarf_version >= 4)
8015 /* Currently all add_AT_flag calls pass in 1 as last argument,
8016 so DW_FORM_flag_present can be used. If that ever changes,
8017 we'll need to use DW_FORM_flag and have some optimization
8018 in build_abbrev_table that will change those to
8019 DW_FORM_flag_present if it is set to 1 in all DIEs using
8020 the same abbrev entry. */
8021 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8022 return DW_FORM_flag_present;
8024 return DW_FORM_flag;
8025 case dw_val_class_die_ref:
8026 if (AT_ref_external (a))
8027 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8028 else
8029 return DW_FORM_ref;
8030 case dw_val_class_fde_ref:
8031 return DW_FORM_data;
8032 case dw_val_class_lbl_id:
8033 return DW_FORM_addr;
8034 case dw_val_class_lineptr:
8035 case dw_val_class_macptr:
8036 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8037 case dw_val_class_str:
8038 return AT_string_form (a);
8039 case dw_val_class_file:
8040 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8042 case 1:
8043 return DW_FORM_data1;
8044 case 2:
8045 return DW_FORM_data2;
8046 case 4:
8047 return DW_FORM_data4;
8048 default:
8049 gcc_unreachable ();
8052 case dw_val_class_data8:
8053 return DW_FORM_data8;
8055 default:
8056 gcc_unreachable ();
8060 /* Output the encoding of an attribute value. */
8062 static void
8063 output_value_format (dw_attr_ref a)
8065 enum dwarf_form form = value_format (a);
8067 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8070 /* Output the .debug_abbrev section which defines the DIE abbreviation
8071 table. */
8073 static void
8074 output_abbrev_section (void)
8076 unsigned long abbrev_id;
8078 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8080 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
8081 unsigned ix;
8082 dw_attr_ref a_attr;
8084 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8085 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8086 dwarf_tag_name (abbrev->die_tag));
8088 if (abbrev->die_child != NULL)
8089 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8090 else
8091 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8093 for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
8094 ix++)
8096 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8097 dwarf_attr_name (a_attr->dw_attr));
8098 output_value_format (a_attr);
8101 dw2_asm_output_data (1, 0, NULL);
8102 dw2_asm_output_data (1, 0, NULL);
8105 /* Terminate the table. */
8106 dw2_asm_output_data (1, 0, NULL);
8109 /* Output a symbol we can use to refer to this DIE from another CU. */
8111 static inline void
8112 output_die_symbol (dw_die_ref die)
8114 char *sym = die->die_id.die_symbol;
8116 if (sym == 0)
8117 return;
8119 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8120 /* We make these global, not weak; if the target doesn't support
8121 .linkonce, it doesn't support combining the sections, so debugging
8122 will break. */
8123 targetm.asm_out.globalize_label (asm_out_file, sym);
8125 ASM_OUTPUT_LABEL (asm_out_file, sym);
8128 /* Return a new location list, given the begin and end range, and the
8129 expression. */
8131 static inline dw_loc_list_ref
8132 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8133 const char *section)
8135 dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
8137 retlist->begin = begin;
8138 retlist->end = end;
8139 retlist->expr = expr;
8140 retlist->section = section;
8142 return retlist;
8145 /* Generate a new internal symbol for this location list node, if it
8146 hasn't got one yet. */
8148 static inline void
8149 gen_llsym (dw_loc_list_ref list)
8151 gcc_assert (!list->ll_symbol);
8152 list->ll_symbol = gen_internal_sym ("LLST");
8155 /* Output the location list given to us. */
8157 static void
8158 output_loc_list (dw_loc_list_ref list_head)
8160 dw_loc_list_ref curr = list_head;
8162 if (list_head->emitted)
8163 return;
8164 list_head->emitted = true;
8166 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8168 /* Walk the location list, and output each range + expression. */
8169 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8171 unsigned long size;
8172 /* Don't output an entry that starts and ends at the same address. */
8173 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8174 continue;
8175 if (!have_multiple_function_sections)
8177 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8178 "Location list begin address (%s)",
8179 list_head->ll_symbol);
8180 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8181 "Location list end address (%s)",
8182 list_head->ll_symbol);
8184 else
8186 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8187 "Location list begin address (%s)",
8188 list_head->ll_symbol);
8189 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8190 "Location list end address (%s)",
8191 list_head->ll_symbol);
8193 size = size_of_locs (curr->expr);
8195 /* Output the block length for this list of location operations. */
8196 gcc_assert (size <= 0xffff);
8197 dw2_asm_output_data (2, size, "%s", "Location expression size");
8199 output_loc_sequence (curr->expr, -1);
8202 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8203 "Location list terminator begin (%s)",
8204 list_head->ll_symbol);
8205 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8206 "Location list terminator end (%s)",
8207 list_head->ll_symbol);
8210 /* Output a type signature. */
8212 static inline void
8213 output_signature (const char *sig, const char *name)
8215 int i;
8217 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8218 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8221 /* Output the DIE and its attributes. Called recursively to generate
8222 the definitions of each child DIE. */
8224 static void
8225 output_die (dw_die_ref die)
8227 dw_attr_ref a;
8228 dw_die_ref c;
8229 unsigned long size;
8230 unsigned ix;
8232 /* If someone in another CU might refer to us, set up a symbol for
8233 them to point to. */
8234 if (! use_debug_types && die->die_id.die_symbol)
8235 output_die_symbol (die);
8237 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8238 (unsigned long)die->die_offset,
8239 dwarf_tag_name (die->die_tag));
8241 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8243 const char *name = dwarf_attr_name (a->dw_attr);
8245 switch (AT_class (a))
8247 case dw_val_class_addr:
8248 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8249 break;
8251 case dw_val_class_offset:
8252 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8253 "%s", name);
8254 break;
8256 case dw_val_class_range_list:
8258 char *p = strchr (ranges_section_label, '\0');
8260 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
8261 a->dw_attr_val.v.val_offset);
8262 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8263 debug_ranges_section, "%s", name);
8264 *p = '\0';
8266 break;
8268 case dw_val_class_loc:
8269 size = size_of_locs (AT_loc (a));
8271 /* Output the block length for this list of location operations. */
8272 if (dwarf_version >= 4)
8273 dw2_asm_output_data_uleb128 (size, "%s", name);
8274 else
8275 dw2_asm_output_data (constant_size (size), size, "%s", name);
8277 output_loc_sequence (AT_loc (a), -1);
8278 break;
8280 case dw_val_class_const:
8281 /* ??? It would be slightly more efficient to use a scheme like is
8282 used for unsigned constants below, but gdb 4.x does not sign
8283 extend. Gdb 5.x does sign extend. */
8284 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8285 break;
8287 case dw_val_class_unsigned_const:
8289 int csize = constant_size (AT_unsigned (a));
8290 if (dwarf_version == 3
8291 && a->dw_attr == DW_AT_data_member_location
8292 && csize >= 4)
8293 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8294 else
8295 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8297 break;
8299 case dw_val_class_const_double:
8301 unsigned HOST_WIDE_INT first, second;
8303 if (HOST_BITS_PER_WIDE_INT >= 64)
8304 dw2_asm_output_data (1,
8305 2 * HOST_BITS_PER_WIDE_INT
8306 / HOST_BITS_PER_CHAR,
8307 NULL);
8309 if (WORDS_BIG_ENDIAN)
8311 first = a->dw_attr_val.v.val_double.high;
8312 second = a->dw_attr_val.v.val_double.low;
8314 else
8316 first = a->dw_attr_val.v.val_double.low;
8317 second = a->dw_attr_val.v.val_double.high;
8320 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8321 first, name);
8322 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8323 second, NULL);
8325 break;
8327 case dw_val_class_vec:
8329 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8330 unsigned int len = a->dw_attr_val.v.val_vec.length;
8331 unsigned int i;
8332 unsigned char *p;
8334 dw2_asm_output_data (constant_size (len * elt_size),
8335 len * elt_size, "%s", name);
8336 if (elt_size > sizeof (HOST_WIDE_INT))
8338 elt_size /= 2;
8339 len *= 2;
8341 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8342 i < len;
8343 i++, p += elt_size)
8344 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8345 "fp or vector constant word %u", i);
8346 break;
8349 case dw_val_class_flag:
8350 if (dwarf_version >= 4)
8352 /* Currently all add_AT_flag calls pass in 1 as last argument,
8353 so DW_FORM_flag_present can be used. If that ever changes,
8354 we'll need to use DW_FORM_flag and have some optimization
8355 in build_abbrev_table that will change those to
8356 DW_FORM_flag_present if it is set to 1 in all DIEs using
8357 the same abbrev entry. */
8358 gcc_assert (AT_flag (a) == 1);
8359 if (flag_debug_asm)
8360 fprintf (asm_out_file, "\t\t\t%s %s\n",
8361 ASM_COMMENT_START, name);
8362 break;
8364 dw2_asm_output_data (1, AT_flag (a), "%s", name);
8365 break;
8367 case dw_val_class_loc_list:
8369 char *sym = AT_loc_list (a)->ll_symbol;
8371 gcc_assert (sym);
8372 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8373 "%s", name);
8375 break;
8377 case dw_val_class_die_ref:
8378 if (AT_ref_external (a))
8380 if (use_debug_types)
8382 comdat_type_node_ref type_node =
8383 AT_ref (a)->die_id.die_type_node;
8385 gcc_assert (type_node);
8386 output_signature (type_node->signature, name);
8388 else
8390 char *sym = AT_ref (a)->die_id.die_symbol;
8391 int size;
8393 gcc_assert (sym);
8394 /* In DWARF2, DW_FORM_ref_addr is sized by target address
8395 length, whereas in DWARF3 it's always sized as an
8396 offset. */
8397 if (dwarf_version == 2)
8398 size = DWARF2_ADDR_SIZE;
8399 else
8400 size = DWARF_OFFSET_SIZE;
8401 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8402 name);
8405 else
8407 gcc_assert (AT_ref (a)->die_offset);
8408 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8409 "%s", name);
8411 break;
8413 case dw_val_class_fde_ref:
8415 char l1[20];
8417 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8418 a->dw_attr_val.v.val_fde_index * 2);
8419 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8420 "%s", name);
8422 break;
8424 case dw_val_class_vms_delta:
8425 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
8426 AT_vms_delta2 (a), AT_vms_delta1 (a),
8427 "%s", name);
8428 break;
8430 case dw_val_class_lbl_id:
8431 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8432 break;
8434 case dw_val_class_lineptr:
8435 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8436 debug_line_section, "%s", name);
8437 break;
8439 case dw_val_class_macptr:
8440 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8441 debug_macinfo_section, "%s", name);
8442 break;
8444 case dw_val_class_str:
8445 if (AT_string_form (a) == DW_FORM_strp)
8446 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8447 a->dw_attr_val.v.val_str->label,
8448 debug_str_section,
8449 "%s: \"%s\"", name, AT_string (a));
8450 else
8451 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8452 break;
8454 case dw_val_class_file:
8456 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8458 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8459 a->dw_attr_val.v.val_file->filename);
8460 break;
8463 case dw_val_class_data8:
8465 int i;
8467 for (i = 0; i < 8; i++)
8468 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
8469 i == 0 ? "%s" : NULL, name);
8470 break;
8473 default:
8474 gcc_unreachable ();
8478 FOR_EACH_CHILD (die, c, output_die (c));
8480 /* Add null byte to terminate sibling list. */
8481 if (die->die_child != NULL)
8482 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
8483 (unsigned long) die->die_offset);
8486 /* Output the compilation unit that appears at the beginning of the
8487 .debug_info section, and precedes the DIE descriptions. */
8489 static void
8490 output_compilation_unit_header (void)
8492 int ver = dwarf_version;
8494 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8495 dw2_asm_output_data (4, 0xffffffff,
8496 "Initial length escape value indicating 64-bit DWARF extension");
8497 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8498 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8499 "Length of Compilation Unit Info");
8500 dw2_asm_output_data (2, ver, "DWARF version number");
8501 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8502 debug_abbrev_section,
8503 "Offset Into Abbrev. Section");
8504 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8507 /* Output the compilation unit DIE and its children. */
8509 static void
8510 output_comp_unit (dw_die_ref die, int output_if_empty)
8512 const char *secname;
8513 char *oldsym, *tmp;
8515 /* Unless we are outputting main CU, we may throw away empty ones. */
8516 if (!output_if_empty && die->die_child == NULL)
8517 return;
8519 /* Even if there are no children of this DIE, we must output the information
8520 about the compilation unit. Otherwise, on an empty translation unit, we
8521 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
8522 will then complain when examining the file. First mark all the DIEs in
8523 this CU so we know which get local refs. */
8524 mark_dies (die);
8526 build_abbrev_table (die);
8528 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8529 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8530 calc_die_sizes (die);
8532 oldsym = die->die_id.die_symbol;
8533 if (oldsym)
8535 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8537 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8538 secname = tmp;
8539 die->die_id.die_symbol = NULL;
8540 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8542 else
8544 switch_to_section (debug_info_section);
8545 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
8546 info_section_emitted = true;
8549 /* Output debugging information. */
8550 output_compilation_unit_header ();
8551 output_die (die);
8553 /* Leave the marks on the main CU, so we can check them in
8554 output_pubnames. */
8555 if (oldsym)
8557 unmark_dies (die);
8558 die->die_id.die_symbol = oldsym;
8562 /* Output a comdat type unit DIE and its children. */
8564 static void
8565 output_comdat_type_unit (comdat_type_node *node)
8567 const char *secname;
8568 char *tmp;
8569 int i;
8570 #if defined (OBJECT_FORMAT_ELF)
8571 tree comdat_key;
8572 #endif
8574 /* First mark all the DIEs in this CU so we know which get local refs. */
8575 mark_dies (node->root_die);
8577 build_abbrev_table (node->root_die);
8579 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8580 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
8581 calc_die_sizes (node->root_die);
8583 #if defined (OBJECT_FORMAT_ELF)
8584 secname = ".debug_types";
8585 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
8586 sprintf (tmp, "wt.");
8587 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8588 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
8589 comdat_key = get_identifier (tmp);
8590 targetm.asm_out.named_section (secname,
8591 SECTION_DEBUG | SECTION_LINKONCE,
8592 comdat_key);
8593 #else
8594 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
8595 sprintf (tmp, ".gnu.linkonce.wt.");
8596 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8597 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
8598 secname = tmp;
8599 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8600 #endif
8602 /* Output debugging information. */
8603 output_compilation_unit_header ();
8604 output_signature (node->signature, "Type Signature");
8605 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
8606 "Offset to Type DIE");
8607 output_die (node->root_die);
8609 unmark_dies (node->root_die);
8612 /* Return the DWARF2/3 pubname associated with a decl. */
8614 static const char *
8615 dwarf2_name (tree decl, int scope)
8617 if (DECL_NAMELESS (decl))
8618 return NULL;
8619 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
8622 /* Add a new entry to .debug_pubnames if appropriate. */
8624 static void
8625 add_pubname_string (const char *str, dw_die_ref die)
8627 if (targetm.want_debug_pub_sections)
8629 pubname_entry e;
8631 e.die = die;
8632 e.name = xstrdup (str);
8633 VEC_safe_push (pubname_entry, gc, pubname_table, &e);
8637 static void
8638 add_pubname (tree decl, dw_die_ref die)
8640 if (targetm.want_debug_pub_sections && TREE_PUBLIC (decl))
8642 const char *name = dwarf2_name (decl, 1);
8643 if (name)
8644 add_pubname_string (name, die);
8648 /* Add a new entry to .debug_pubtypes if appropriate. */
8650 static void
8651 add_pubtype (tree decl, dw_die_ref die)
8653 pubname_entry e;
8655 if (!targetm.want_debug_pub_sections)
8656 return;
8658 e.name = NULL;
8659 if ((TREE_PUBLIC (decl)
8660 || is_cu_die (die->die_parent))
8661 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
8663 e.die = die;
8664 if (TYPE_P (decl))
8666 if (TYPE_NAME (decl))
8668 if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
8669 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
8670 else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
8671 && DECL_NAME (TYPE_NAME (decl)))
8672 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
8673 else
8674 e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
8677 else
8679 e.name = dwarf2_name (decl, 1);
8680 if (e.name)
8681 e.name = xstrdup (e.name);
8684 /* If we don't have a name for the type, there's no point in adding
8685 it to the table. */
8686 if (e.name && e.name[0] != '\0')
8687 VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
8691 /* Output the public names table used to speed up access to externally
8692 visible names; or the public types table used to find type definitions. */
8694 static void
8695 output_pubnames (VEC (pubname_entry, gc) * names)
8697 unsigned i;
8698 unsigned long pubnames_length = size_of_pubnames (names);
8699 pubname_ref pub;
8701 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8702 dw2_asm_output_data (4, 0xffffffff,
8703 "Initial length escape value indicating 64-bit DWARF extension");
8704 if (names == pubname_table)
8705 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8706 "Length of Public Names Info");
8707 else
8708 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8709 "Length of Public Type Names Info");
8710 /* Version number for pubnames/pubtypes is still 2, even in DWARF3. */
8711 dw2_asm_output_data (2, 2, "DWARF Version");
8712 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8713 debug_info_section,
8714 "Offset of Compilation Unit Info");
8715 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
8716 "Compilation Unit Length");
8718 FOR_EACH_VEC_ELT (pubname_entry, names, i, pub)
8720 /* We shouldn't see pubnames for DIEs outside of the main CU. */
8721 if (names == pubname_table)
8722 gcc_assert (pub->die->die_mark);
8724 if (names != pubtype_table
8725 || pub->die->die_offset != 0
8726 || !flag_eliminate_unused_debug_types)
8728 dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
8729 "DIE offset");
8731 dw2_asm_output_nstring (pub->name, -1, "external name");
8735 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
8738 /* Output the information that goes into the .debug_aranges table.
8739 Namely, define the beginning and ending address range of the
8740 text section generated for this compilation unit. */
8742 static void
8743 output_aranges (unsigned long aranges_length)
8745 unsigned i;
8747 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8748 dw2_asm_output_data (4, 0xffffffff,
8749 "Initial length escape value indicating 64-bit DWARF extension");
8750 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
8751 "Length of Address Ranges Info");
8752 /* Version number for aranges is still 2, even in DWARF3. */
8753 dw2_asm_output_data (2, 2, "DWARF Version");
8754 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8755 debug_info_section,
8756 "Offset of Compilation Unit Info");
8757 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
8758 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
8760 /* We need to align to twice the pointer size here. */
8761 if (DWARF_ARANGES_PAD_SIZE)
8763 /* Pad using a 2 byte words so that padding is correct for any
8764 pointer size. */
8765 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
8766 2 * DWARF2_ADDR_SIZE);
8767 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
8768 dw2_asm_output_data (2, 0, NULL);
8771 /* It is necessary not to output these entries if the sections were
8772 not used; if the sections were not used, the length will be 0 and
8773 the address may end up as 0 if the section is discarded by ld
8774 --gc-sections, leaving an invalid (0, 0) entry that can be
8775 confused with the terminator. */
8776 if (text_section_used)
8778 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
8779 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
8780 text_section_label, "Length");
8782 if (cold_text_section_used)
8784 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
8785 "Address");
8786 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
8787 cold_text_section_label, "Length");
8790 if (have_multiple_function_sections)
8792 unsigned fde_idx;
8793 dw_fde_ref fde;
8795 FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, fde_idx, fde)
8797 if (!fde->in_std_section)
8799 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
8800 "Address");
8801 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
8802 fde->dw_fde_begin, "Length");
8804 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8806 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
8807 "Address");
8808 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
8809 fde->dw_fde_second_begin, "Length");
8814 /* Output the terminator words. */
8815 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8816 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8819 /* Add a new entry to .debug_ranges. Return the offset at which it
8820 was placed. */
8822 static unsigned int
8823 add_ranges_num (int num)
8825 unsigned int in_use = ranges_table_in_use;
8827 if (in_use == ranges_table_allocated)
8829 ranges_table_allocated += RANGES_TABLE_INCREMENT;
8830 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
8831 ranges_table_allocated);
8832 memset (ranges_table + ranges_table_in_use, 0,
8833 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
8836 ranges_table[in_use].num = num;
8837 ranges_table_in_use = in_use + 1;
8839 return in_use * 2 * DWARF2_ADDR_SIZE;
8842 /* Add a new entry to .debug_ranges corresponding to a block, or a
8843 range terminator if BLOCK is NULL. */
8845 static unsigned int
8846 add_ranges (const_tree block)
8848 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
8851 /* Add a new entry to .debug_ranges corresponding to a pair of
8852 labels. */
8854 static void
8855 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
8856 bool *added)
8858 unsigned int in_use = ranges_by_label_in_use;
8859 unsigned int offset;
8861 if (in_use == ranges_by_label_allocated)
8863 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
8864 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
8865 ranges_by_label,
8866 ranges_by_label_allocated);
8867 memset (ranges_by_label + ranges_by_label_in_use, 0,
8868 RANGES_TABLE_INCREMENT
8869 * sizeof (struct dw_ranges_by_label_struct));
8872 ranges_by_label[in_use].begin = begin;
8873 ranges_by_label[in_use].end = end;
8874 ranges_by_label_in_use = in_use + 1;
8876 offset = add_ranges_num (-(int)in_use - 1);
8877 if (!*added)
8879 add_AT_range_list (die, DW_AT_ranges, offset);
8880 *added = true;
8884 static void
8885 output_ranges (void)
8887 unsigned i;
8888 static const char *const start_fmt = "Offset %#x";
8889 const char *fmt = start_fmt;
8891 for (i = 0; i < ranges_table_in_use; i++)
8893 int block_num = ranges_table[i].num;
8895 if (block_num > 0)
8897 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
8898 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
8900 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
8901 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
8903 /* If all code is in the text section, then the compilation
8904 unit base address defaults to DW_AT_low_pc, which is the
8905 base of the text section. */
8906 if (!have_multiple_function_sections)
8908 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
8909 text_section_label,
8910 fmt, i * 2 * DWARF2_ADDR_SIZE);
8911 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
8912 text_section_label, NULL);
8915 /* Otherwise, the compilation unit base address is zero,
8916 which allows us to use absolute addresses, and not worry
8917 about whether the target supports cross-section
8918 arithmetic. */
8919 else
8921 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
8922 fmt, i * 2 * DWARF2_ADDR_SIZE);
8923 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
8926 fmt = NULL;
8929 /* Negative block_num stands for an index into ranges_by_label. */
8930 else if (block_num < 0)
8932 int lab_idx = - block_num - 1;
8934 if (!have_multiple_function_sections)
8936 gcc_unreachable ();
8937 #if 0
8938 /* If we ever use add_ranges_by_labels () for a single
8939 function section, all we have to do is to take out
8940 the #if 0 above. */
8941 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8942 ranges_by_label[lab_idx].begin,
8943 text_section_label,
8944 fmt, i * 2 * DWARF2_ADDR_SIZE);
8945 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8946 ranges_by_label[lab_idx].end,
8947 text_section_label, NULL);
8948 #endif
8950 else
8952 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
8953 ranges_by_label[lab_idx].begin,
8954 fmt, i * 2 * DWARF2_ADDR_SIZE);
8955 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
8956 ranges_by_label[lab_idx].end,
8957 NULL);
8960 else
8962 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8963 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8964 fmt = start_fmt;
8969 /* Data structure containing information about input files. */
8970 struct file_info
8972 const char *path; /* Complete file name. */
8973 const char *fname; /* File name part. */
8974 int length; /* Length of entire string. */
8975 struct dwarf_file_data * file_idx; /* Index in input file table. */
8976 int dir_idx; /* Index in directory table. */
8979 /* Data structure containing information about directories with source
8980 files. */
8981 struct dir_info
8983 const char *path; /* Path including directory name. */
8984 int length; /* Path length. */
8985 int prefix; /* Index of directory entry which is a prefix. */
8986 int count; /* Number of files in this directory. */
8987 int dir_idx; /* Index of directory used as base. */
8990 /* Callback function for file_info comparison. We sort by looking at
8991 the directories in the path. */
8993 static int
8994 file_info_cmp (const void *p1, const void *p2)
8996 const struct file_info *const s1 = (const struct file_info *) p1;
8997 const struct file_info *const s2 = (const struct file_info *) p2;
8998 const unsigned char *cp1;
8999 const unsigned char *cp2;
9001 /* Take care of file names without directories. We need to make sure that
9002 we return consistent values to qsort since some will get confused if
9003 we return the same value when identical operands are passed in opposite
9004 orders. So if neither has a directory, return 0 and otherwise return
9005 1 or -1 depending on which one has the directory. */
9006 if ((s1->path == s1->fname || s2->path == s2->fname))
9007 return (s2->path == s2->fname) - (s1->path == s1->fname);
9009 cp1 = (const unsigned char *) s1->path;
9010 cp2 = (const unsigned char *) s2->path;
9012 while (1)
9014 ++cp1;
9015 ++cp2;
9016 /* Reached the end of the first path? If so, handle like above. */
9017 if ((cp1 == (const unsigned char *) s1->fname)
9018 || (cp2 == (const unsigned char *) s2->fname))
9019 return ((cp2 == (const unsigned char *) s2->fname)
9020 - (cp1 == (const unsigned char *) s1->fname));
9022 /* Character of current path component the same? */
9023 else if (*cp1 != *cp2)
9024 return *cp1 - *cp2;
9028 struct file_name_acquire_data
9030 struct file_info *files;
9031 int used_files;
9032 int max_files;
9035 /* Traversal function for the hash table. */
9037 static int
9038 file_name_acquire (void ** slot, void *data)
9040 struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
9041 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
9042 struct file_info *fi;
9043 const char *f;
9045 gcc_assert (fnad->max_files >= d->emitted_number);
9047 if (! d->emitted_number)
9048 return 1;
9050 gcc_assert (fnad->max_files != fnad->used_files);
9052 fi = fnad->files + fnad->used_files++;
9054 /* Skip all leading "./". */
9055 f = d->filename;
9056 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9057 f += 2;
9059 /* Create a new array entry. */
9060 fi->path = f;
9061 fi->length = strlen (f);
9062 fi->file_idx = d;
9064 /* Search for the file name part. */
9065 f = strrchr (f, DIR_SEPARATOR);
9066 #if defined (DIR_SEPARATOR_2)
9068 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9070 if (g != NULL)
9072 if (f == NULL || f < g)
9073 f = g;
9076 #endif
9078 fi->fname = f == NULL ? fi->path : f + 1;
9079 return 1;
9082 /* Output the directory table and the file name table. We try to minimize
9083 the total amount of memory needed. A heuristic is used to avoid large
9084 slowdowns with many input files. */
9086 static void
9087 output_file_names (void)
9089 struct file_name_acquire_data fnad;
9090 int numfiles;
9091 struct file_info *files;
9092 struct dir_info *dirs;
9093 int *saved;
9094 int *savehere;
9095 int *backmap;
9096 int ndirs;
9097 int idx_offset;
9098 int i;
9100 if (!last_emitted_file)
9102 dw2_asm_output_data (1, 0, "End directory table");
9103 dw2_asm_output_data (1, 0, "End file name table");
9104 return;
9107 numfiles = last_emitted_file->emitted_number;
9109 /* Allocate the various arrays we need. */
9110 files = XALLOCAVEC (struct file_info, numfiles);
9111 dirs = XALLOCAVEC (struct dir_info, numfiles);
9113 fnad.files = files;
9114 fnad.used_files = 0;
9115 fnad.max_files = numfiles;
9116 htab_traverse (file_table, file_name_acquire, &fnad);
9117 gcc_assert (fnad.used_files == fnad.max_files);
9119 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9121 /* Find all the different directories used. */
9122 dirs[0].path = files[0].path;
9123 dirs[0].length = files[0].fname - files[0].path;
9124 dirs[0].prefix = -1;
9125 dirs[0].count = 1;
9126 dirs[0].dir_idx = 0;
9127 files[0].dir_idx = 0;
9128 ndirs = 1;
9130 for (i = 1; i < numfiles; i++)
9131 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9132 && memcmp (dirs[ndirs - 1].path, files[i].path,
9133 dirs[ndirs - 1].length) == 0)
9135 /* Same directory as last entry. */
9136 files[i].dir_idx = ndirs - 1;
9137 ++dirs[ndirs - 1].count;
9139 else
9141 int j;
9143 /* This is a new directory. */
9144 dirs[ndirs].path = files[i].path;
9145 dirs[ndirs].length = files[i].fname - files[i].path;
9146 dirs[ndirs].count = 1;
9147 dirs[ndirs].dir_idx = ndirs;
9148 files[i].dir_idx = ndirs;
9150 /* Search for a prefix. */
9151 dirs[ndirs].prefix = -1;
9152 for (j = 0; j < ndirs; j++)
9153 if (dirs[j].length < dirs[ndirs].length
9154 && dirs[j].length > 1
9155 && (dirs[ndirs].prefix == -1
9156 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9157 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9158 dirs[ndirs].prefix = j;
9160 ++ndirs;
9163 /* Now to the actual work. We have to find a subset of the directories which
9164 allow expressing the file name using references to the directory table
9165 with the least amount of characters. We do not do an exhaustive search
9166 where we would have to check out every combination of every single
9167 possible prefix. Instead we use a heuristic which provides nearly optimal
9168 results in most cases and never is much off. */
9169 saved = XALLOCAVEC (int, ndirs);
9170 savehere = XALLOCAVEC (int, ndirs);
9172 memset (saved, '\0', ndirs * sizeof (saved[0]));
9173 for (i = 0; i < ndirs; i++)
9175 int j;
9176 int total;
9178 /* We can always save some space for the current directory. But this
9179 does not mean it will be enough to justify adding the directory. */
9180 savehere[i] = dirs[i].length;
9181 total = (savehere[i] - saved[i]) * dirs[i].count;
9183 for (j = i + 1; j < ndirs; j++)
9185 savehere[j] = 0;
9186 if (saved[j] < dirs[i].length)
9188 /* Determine whether the dirs[i] path is a prefix of the
9189 dirs[j] path. */
9190 int k;
9192 k = dirs[j].prefix;
9193 while (k != -1 && k != (int) i)
9194 k = dirs[k].prefix;
9196 if (k == (int) i)
9198 /* Yes it is. We can possibly save some memory by
9199 writing the filenames in dirs[j] relative to
9200 dirs[i]. */
9201 savehere[j] = dirs[i].length;
9202 total += (savehere[j] - saved[j]) * dirs[j].count;
9207 /* Check whether we can save enough to justify adding the dirs[i]
9208 directory. */
9209 if (total > dirs[i].length + 1)
9211 /* It's worthwhile adding. */
9212 for (j = i; j < ndirs; j++)
9213 if (savehere[j] > 0)
9215 /* Remember how much we saved for this directory so far. */
9216 saved[j] = savehere[j];
9218 /* Remember the prefix directory. */
9219 dirs[j].dir_idx = i;
9224 /* Emit the directory name table. */
9225 idx_offset = dirs[0].length > 0 ? 1 : 0;
9226 for (i = 1 - idx_offset; i < ndirs; i++)
9227 dw2_asm_output_nstring (dirs[i].path,
9228 dirs[i].length
9229 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9230 "Directory Entry: %#x", i + idx_offset);
9232 dw2_asm_output_data (1, 0, "End directory table");
9234 /* We have to emit them in the order of emitted_number since that's
9235 used in the debug info generation. To do this efficiently we
9236 generate a back-mapping of the indices first. */
9237 backmap = XALLOCAVEC (int, numfiles);
9238 for (i = 0; i < numfiles; i++)
9239 backmap[files[i].file_idx->emitted_number - 1] = i;
9241 /* Now write all the file names. */
9242 for (i = 0; i < numfiles; i++)
9244 int file_idx = backmap[i];
9245 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9247 #ifdef VMS_DEBUGGING_INFO
9248 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
9250 /* Setting these fields can lead to debugger miscomparisons,
9251 but VMS Debug requires them to be set correctly. */
9253 int ver;
9254 long long cdt;
9255 long siz;
9256 int maxfilelen = strlen (files[file_idx].path)
9257 + dirs[dir_idx].length
9258 + MAX_VMS_VERSION_LEN + 1;
9259 char *filebuf = XALLOCAVEC (char, maxfilelen);
9261 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
9262 snprintf (filebuf, maxfilelen, "%s;%d",
9263 files[file_idx].path + dirs[dir_idx].length, ver);
9265 dw2_asm_output_nstring
9266 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
9268 /* Include directory index. */
9269 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9271 /* Modification time. */
9272 dw2_asm_output_data_uleb128
9273 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
9274 ? cdt : 0,
9275 NULL);
9277 /* File length in bytes. */
9278 dw2_asm_output_data_uleb128
9279 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
9280 ? siz : 0,
9281 NULL);
9282 #else
9283 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
9284 "File Entry: %#x", (unsigned) i + 1);
9286 /* Include directory index. */
9287 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9289 /* Modification time. */
9290 dw2_asm_output_data_uleb128 (0, NULL);
9292 /* File length in bytes. */
9293 dw2_asm_output_data_uleb128 (0, NULL);
9294 #endif /* VMS_DEBUGGING_INFO */
9297 dw2_asm_output_data (1, 0, "End file name table");
9301 /* Output one line number table into the .debug_line section. */
9303 static void
9304 output_one_line_info_table (dw_line_info_table *table)
9306 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9307 unsigned int current_line = 1;
9308 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
9309 dw_line_info_entry *ent;
9310 size_t i;
9312 FOR_EACH_VEC_ELT (dw_line_info_entry, table->entries, i, ent)
9314 switch (ent->opcode)
9316 case LI_set_address:
9317 /* ??? Unfortunately, we have little choice here currently, and
9318 must always use the most general form. GCC does not know the
9319 address delta itself, so we can't use DW_LNS_advance_pc. Many
9320 ports do have length attributes which will give an upper bound
9321 on the address range. We could perhaps use length attributes
9322 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
9323 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
9325 /* This can handle any delta. This takes
9326 4+DWARF2_ADDR_SIZE bytes. */
9327 dw2_asm_output_data (1, 0, "set address %s", line_label);
9328 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9329 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9330 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9331 break;
9333 case LI_set_line:
9334 if (ent->val == current_line)
9336 /* We still need to start a new row, so output a copy insn. */
9337 dw2_asm_output_data (1, DW_LNS_copy,
9338 "copy line %u", current_line);
9340 else
9342 int line_offset = ent->val - current_line;
9343 int line_delta = line_offset - DWARF_LINE_BASE;
9345 current_line = ent->val;
9346 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9348 /* This can handle deltas from -10 to 234, using the current
9349 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
9350 This takes 1 byte. */
9351 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9352 "line %u", current_line);
9354 else
9356 /* This can handle any delta. This takes at least 4 bytes,
9357 depending on the value being encoded. */
9358 dw2_asm_output_data (1, DW_LNS_advance_line,
9359 "advance to line %u", current_line);
9360 dw2_asm_output_data_sleb128 (line_offset, NULL);
9361 dw2_asm_output_data (1, DW_LNS_copy, NULL);
9364 break;
9366 case LI_set_file:
9367 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
9368 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9369 break;
9371 case LI_set_column:
9372 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
9373 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9374 break;
9376 case LI_negate_stmt:
9377 current_is_stmt = !current_is_stmt;
9378 dw2_asm_output_data (1, DW_LNS_negate_stmt,
9379 "is_stmt %d", current_is_stmt);
9380 break;
9382 case LI_set_prologue_end:
9383 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
9384 "set prologue end");
9385 break;
9387 case LI_set_epilogue_begin:
9388 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
9389 "set epilogue begin");
9390 break;
9392 case LI_set_discriminator:
9393 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
9394 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
9395 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
9396 dw2_asm_output_data_uleb128 (ent->val, NULL);
9397 break;
9401 /* Emit debug info for the address of the end of the table. */
9402 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
9403 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9404 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9405 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
9407 dw2_asm_output_data (1, 0, "end sequence");
9408 dw2_asm_output_data_uleb128 (1, NULL);
9409 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9412 /* Output the source line number correspondence information. This
9413 information goes into the .debug_line section. */
9415 static void
9416 output_line_info (void)
9418 char l1[20], l2[20], p1[20], p2[20];
9419 int ver = dwarf_version;
9420 bool saw_one = false;
9421 int opc;
9423 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
9424 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
9425 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
9426 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
9428 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9429 dw2_asm_output_data (4, 0xffffffff,
9430 "Initial length escape value indicating 64-bit DWARF extension");
9431 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
9432 "Length of Source Line Info");
9433 ASM_OUTPUT_LABEL (asm_out_file, l1);
9435 dw2_asm_output_data (2, ver, "DWARF Version");
9436 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
9437 ASM_OUTPUT_LABEL (asm_out_file, p1);
9439 /* Define the architecture-dependent minimum instruction length (in bytes).
9440 In this implementation of DWARF, this field is used for information
9441 purposes only. Since GCC generates assembly language, we have no
9442 a priori knowledge of how many instruction bytes are generated for each
9443 source line, and therefore can use only the DW_LNE_set_address and
9444 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
9445 this as '1', which is "correct enough" for all architectures,
9446 and don't let the target override. */
9447 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
9449 if (ver >= 4)
9450 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
9451 "Maximum Operations Per Instruction");
9452 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
9453 "Default is_stmt_start flag");
9454 dw2_asm_output_data (1, DWARF_LINE_BASE,
9455 "Line Base Value (Special Opcodes)");
9456 dw2_asm_output_data (1, DWARF_LINE_RANGE,
9457 "Line Range Value (Special Opcodes)");
9458 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
9459 "Special Opcode Base");
9461 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
9463 int n_op_args;
9464 switch (opc)
9466 case DW_LNS_advance_pc:
9467 case DW_LNS_advance_line:
9468 case DW_LNS_set_file:
9469 case DW_LNS_set_column:
9470 case DW_LNS_fixed_advance_pc:
9471 case DW_LNS_set_isa:
9472 n_op_args = 1;
9473 break;
9474 default:
9475 n_op_args = 0;
9476 break;
9479 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
9480 opc, n_op_args);
9483 /* Write out the information about the files we use. */
9484 output_file_names ();
9485 ASM_OUTPUT_LABEL (asm_out_file, p2);
9487 if (separate_line_info)
9489 dw_line_info_table *table;
9490 size_t i;
9492 FOR_EACH_VEC_ELT (dw_line_info_table_p, separate_line_info, i, table)
9493 if (table->in_use)
9495 output_one_line_info_table (table);
9496 saw_one = true;
9499 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
9501 output_one_line_info_table (cold_text_section_line_info);
9502 saw_one = true;
9505 /* ??? Some Darwin linkers crash on a .debug_line section with no
9506 sequences. Further, merely a DW_LNE_end_sequence entry is not
9507 sufficient -- the address column must also be initialized.
9508 Make sure to output at least one set_address/end_sequence pair,
9509 choosing .text since that section is always present. */
9510 if (text_section_line_info->in_use || !saw_one)
9511 output_one_line_info_table (text_section_line_info);
9513 /* Output the marker for the end of the line number info. */
9514 ASM_OUTPUT_LABEL (asm_out_file, l2);
9517 /* Given a pointer to a tree node for some base type, return a pointer to
9518 a DIE that describes the given type.
9520 This routine must only be called for GCC type nodes that correspond to
9521 Dwarf base (fundamental) types. */
9523 static dw_die_ref
9524 base_type_die (tree type)
9526 dw_die_ref base_type_result;
9527 enum dwarf_type encoding;
9529 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
9530 return 0;
9532 /* If this is a subtype that should not be emitted as a subrange type,
9533 use the base type. See subrange_type_for_debug_p. */
9534 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
9535 type = TREE_TYPE (type);
9537 switch (TREE_CODE (type))
9539 case INTEGER_TYPE:
9540 if ((dwarf_version >= 4 || !dwarf_strict)
9541 && TYPE_NAME (type)
9542 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
9543 && DECL_IS_BUILTIN (TYPE_NAME (type))
9544 && DECL_NAME (TYPE_NAME (type)))
9546 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
9547 if (strcmp (name, "char16_t") == 0
9548 || strcmp (name, "char32_t") == 0)
9550 encoding = DW_ATE_UTF;
9551 break;
9554 if (TYPE_STRING_FLAG (type))
9556 if (TYPE_UNSIGNED (type))
9557 encoding = DW_ATE_unsigned_char;
9558 else
9559 encoding = DW_ATE_signed_char;
9561 else if (TYPE_UNSIGNED (type))
9562 encoding = DW_ATE_unsigned;
9563 else
9564 encoding = DW_ATE_signed;
9565 break;
9567 case REAL_TYPE:
9568 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
9570 if (dwarf_version >= 3 || !dwarf_strict)
9571 encoding = DW_ATE_decimal_float;
9572 else
9573 encoding = DW_ATE_lo_user;
9575 else
9576 encoding = DW_ATE_float;
9577 break;
9579 case FIXED_POINT_TYPE:
9580 if (!(dwarf_version >= 3 || !dwarf_strict))
9581 encoding = DW_ATE_lo_user;
9582 else if (TYPE_UNSIGNED (type))
9583 encoding = DW_ATE_unsigned_fixed;
9584 else
9585 encoding = DW_ATE_signed_fixed;
9586 break;
9588 /* Dwarf2 doesn't know anything about complex ints, so use
9589 a user defined type for it. */
9590 case COMPLEX_TYPE:
9591 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
9592 encoding = DW_ATE_complex_float;
9593 else
9594 encoding = DW_ATE_lo_user;
9595 break;
9597 case BOOLEAN_TYPE:
9598 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
9599 encoding = DW_ATE_boolean;
9600 break;
9602 default:
9603 /* No other TREE_CODEs are Dwarf fundamental types. */
9604 gcc_unreachable ();
9607 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
9609 add_AT_unsigned (base_type_result, DW_AT_byte_size,
9610 int_size_in_bytes (type));
9611 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
9613 return base_type_result;
9616 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
9617 given input type is a Dwarf "fundamental" type. Otherwise return null. */
9619 static inline int
9620 is_base_type (tree type)
9622 switch (TREE_CODE (type))
9624 case ERROR_MARK:
9625 case VOID_TYPE:
9626 case INTEGER_TYPE:
9627 case REAL_TYPE:
9628 case FIXED_POINT_TYPE:
9629 case COMPLEX_TYPE:
9630 case BOOLEAN_TYPE:
9631 return 1;
9633 case ARRAY_TYPE:
9634 case RECORD_TYPE:
9635 case UNION_TYPE:
9636 case QUAL_UNION_TYPE:
9637 case ENUMERAL_TYPE:
9638 case FUNCTION_TYPE:
9639 case METHOD_TYPE:
9640 case POINTER_TYPE:
9641 case REFERENCE_TYPE:
9642 case NULLPTR_TYPE:
9643 case OFFSET_TYPE:
9644 case LANG_TYPE:
9645 case VECTOR_TYPE:
9646 return 0;
9648 default:
9649 gcc_unreachable ();
9652 return 0;
9655 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
9656 node, return the size in bits for the type if it is a constant, or else
9657 return the alignment for the type if the type's size is not constant, or
9658 else return BITS_PER_WORD if the type actually turns out to be an
9659 ERROR_MARK node. */
9661 static inline unsigned HOST_WIDE_INT
9662 simple_type_size_in_bits (const_tree type)
9664 if (TREE_CODE (type) == ERROR_MARK)
9665 return BITS_PER_WORD;
9666 else if (TYPE_SIZE (type) == NULL_TREE)
9667 return 0;
9668 else if (host_integerp (TYPE_SIZE (type), 1))
9669 return tree_low_cst (TYPE_SIZE (type), 1);
9670 else
9671 return TYPE_ALIGN (type);
9674 /* Similarly, but return a double_int instead of UHWI. */
9676 static inline double_int
9677 double_int_type_size_in_bits (const_tree type)
9679 if (TREE_CODE (type) == ERROR_MARK)
9680 return uhwi_to_double_int (BITS_PER_WORD);
9681 else if (TYPE_SIZE (type) == NULL_TREE)
9682 return double_int_zero;
9683 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
9684 return tree_to_double_int (TYPE_SIZE (type));
9685 else
9686 return uhwi_to_double_int (TYPE_ALIGN (type));
9689 /* Given a pointer to a tree node for a subrange type, return a pointer
9690 to a DIE that describes the given type. */
9692 static dw_die_ref
9693 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
9695 dw_die_ref subrange_die;
9696 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
9698 if (context_die == NULL)
9699 context_die = comp_unit_die ();
9701 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
9703 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
9705 /* The size of the subrange type and its base type do not match,
9706 so we need to generate a size attribute for the subrange type. */
9707 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
9710 if (low)
9711 add_bound_info (subrange_die, DW_AT_lower_bound, low);
9712 if (high)
9713 add_bound_info (subrange_die, DW_AT_upper_bound, high);
9715 return subrange_die;
9718 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
9719 entry that chains various modifiers in front of the given type. */
9721 static dw_die_ref
9722 modified_type_die (tree type, int type_quals,
9723 dw_die_ref context_die)
9725 const int is_const_type = ((type_quals & TYPE_QUAL_CONST) != 0);
9726 const int is_volatile_type = ((type_quals & TYPE_QUAL_VOLATILE) != 0);
9727 enum tree_code code = TREE_CODE (type);
9728 dw_die_ref mod_type_die;
9729 dw_die_ref sub_die = NULL;
9730 tree item_type = NULL;
9731 tree qualified_type;
9732 tree name, low, high;
9734 if (code == ERROR_MARK)
9735 return NULL;
9737 /* See if we already have the appropriately qualified variant of
9738 this type. */
9739 qualified_type
9740 = get_qualified_type (type, type_quals);
9742 if (qualified_type == sizetype
9743 && TYPE_NAME (qualified_type)
9744 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
9746 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
9748 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
9749 && TYPE_PRECISION (t)
9750 == TYPE_PRECISION (qualified_type)
9751 && TYPE_UNSIGNED (t)
9752 == TYPE_UNSIGNED (qualified_type));
9753 qualified_type = t;
9756 /* If we do, then we can just use its DIE, if it exists. */
9757 if (qualified_type)
9759 mod_type_die = lookup_type_die (qualified_type);
9760 if (mod_type_die)
9761 return mod_type_die;
9764 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
9766 /* Handle C typedef types. */
9767 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
9768 && !DECL_ARTIFICIAL (name))
9770 tree dtype = TREE_TYPE (name);
9772 if (qualified_type == dtype)
9774 /* For a named type, use the typedef. */
9775 gen_type_die (qualified_type, context_die);
9776 return lookup_type_die (qualified_type);
9778 else if (type_quals == TYPE_QUALS (dtype))
9779 /* cv-unqualified version of named type. Just use the unnamed
9780 type to which it refers. */
9781 return modified_type_die (DECL_ORIGINAL_TYPE (name), type_quals, context_die);
9782 /* Else cv-qualified version of named type; fall through. */
9784 if ((type_quals & TYPE_QUAL_CONST)
9785 /* If both is_const_type and is_volatile_type, prefer the path
9786 which leads to a qualified type. */
9787 && (!is_volatile_type
9788 || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
9789 || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
9791 mod_type_die = new_die (DW_TAG_const_type, comp_unit_die (), type);
9792 sub_die = modified_type_die (type, type_quals & ~TYPE_QUAL_CONST, context_die);
9794 else if (type_quals & TYPE_QUAL_VOLATILE)
9796 mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die (), type);
9797 sub_die = modified_type_die (type, type_quals & ~TYPE_QUAL_VOLATILE, context_die);
9799 else if (use_upc_dwarf2_extensions
9800 && (type_quals & TYPE_QUAL_SHARED))
9802 HOST_WIDE_INT block_factor = 1;
9804 /* Inside the compiler,
9805 "shared int x;" TYPE_BLOCK_FACTOR is null.
9806 "shared [] int *p;" TYPE_BLOCK_FACTOR is zero.
9807 "shared [10] int x[50];" TYPE_BLOCK_FACTOR is 10 * bitsize(int)
9808 The DWARF2 encoding is as follows:
9809 "shared int x;" DW_AT_count: 1
9810 "shared [] int *p;" <no DW_AT_count attribute>
9811 "shared [10] int x[50];" DW_AT_count: 10
9812 The logic below handles thse various contingencies. */
9814 mod_type_die = new_die (DW_TAG_upc_shared_type,
9815 comp_unit_die (), type);
9817 if (TYPE_HAS_BLOCK_FACTOR (type))
9818 block_factor = TREE_INT_CST_LOW (TYPE_BLOCK_FACTOR (type));
9820 if (block_factor != 0)
9821 add_AT_unsigned (mod_type_die, DW_AT_count, block_factor);
9823 sub_die = modified_type_die (type,
9824 type_quals & ~TYPE_QUAL_SHARED,
9825 context_die);
9827 else if (use_upc_dwarf2_extensions && type_quals & TYPE_QUAL_STRICT)
9829 mod_type_die = new_die (DW_TAG_upc_strict_type,
9830 comp_unit_die (), type);
9831 sub_die = modified_type_die (type,
9832 type_quals & ~TYPE_QUAL_STRICT,
9833 context_die);
9835 else if (use_upc_dwarf2_extensions && type_quals & TYPE_QUAL_RELAXED)
9837 mod_type_die = new_die (DW_TAG_upc_relaxed_type,
9838 comp_unit_die (), type);
9839 sub_die = modified_type_die (type,
9840 type_quals & ~TYPE_QUAL_RELAXED,
9841 context_die);
9843 else if (code == POINTER_TYPE)
9845 mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die (), type);
9846 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9847 simple_type_size_in_bits (type) / BITS_PER_UNIT);
9848 item_type = TREE_TYPE (type);
9849 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
9850 add_AT_unsigned (mod_type_die, DW_AT_address_class,
9851 TYPE_ADDR_SPACE (item_type));
9853 else if (code == REFERENCE_TYPE)
9855 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
9856 mod_type_die = new_die (DW_TAG_rvalue_reference_type, comp_unit_die (),
9857 type);
9858 else
9859 mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die (), type);
9860 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9861 simple_type_size_in_bits (type) / BITS_PER_UNIT);
9862 item_type = TREE_TYPE (type);
9863 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
9864 add_AT_unsigned (mod_type_die, DW_AT_address_class,
9865 TYPE_ADDR_SPACE (item_type));
9867 else if (code == INTEGER_TYPE
9868 && TREE_TYPE (type) != NULL_TREE
9869 && subrange_type_for_debug_p (type, &low, &high))
9871 mod_type_die = subrange_type_die (type, low, high, context_die);
9872 item_type = TREE_TYPE (type);
9874 else if (is_base_type (type))
9875 mod_type_die = base_type_die (type);
9876 else
9878 gen_type_die (type, context_die);
9880 /* We have to get the type_main_variant here (and pass that to the
9881 `lookup_type_die' routine) because the ..._TYPE node we have
9882 might simply be a *copy* of some original type node (where the
9883 copy was created to help us keep track of typedef names) and
9884 that copy might have a different TYPE_UID from the original
9885 ..._TYPE node. */
9886 if (TREE_CODE (type) != VECTOR_TYPE)
9887 return lookup_type_die (type_main_variant (type));
9888 else
9889 /* Vectors have the debugging information in the type,
9890 not the main variant. */
9891 return lookup_type_die (type);
9894 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
9895 don't output a DW_TAG_typedef, since there isn't one in the
9896 user's program; just attach a DW_AT_name to the type.
9897 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
9898 if the base type already has the same name. */
9899 if (name
9900 && ((TREE_CODE (name) != TYPE_DECL
9901 && (qualified_type == TYPE_MAIN_VARIANT (type)
9902 || (!is_const_type && !is_volatile_type)))
9903 || (TREE_CODE (name) == TYPE_DECL
9904 && TREE_TYPE (name) == qualified_type
9905 && DECL_NAME (name))))
9907 if (TREE_CODE (name) == TYPE_DECL)
9908 /* Could just call add_name_and_src_coords_attributes here,
9909 but since this is a builtin type it doesn't have any
9910 useful source coordinates anyway. */
9911 name = DECL_NAME (name);
9912 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
9913 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
9914 if (TYPE_ARTIFICIAL (type))
9915 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
9917 /* This probably indicates a bug. */
9918 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
9919 add_name_attribute (mod_type_die, "__unknown__");
9921 if (qualified_type)
9922 equate_type_number_to_die (qualified_type, mod_type_die);
9924 if (item_type)
9925 /* We must do this after the equate_type_number_to_die call, in case
9926 this is a recursive type. This ensures that the modified_type_die
9927 recursion will terminate even if the type is recursive. Recursive
9928 types are possible in Ada. */
9929 sub_die = modified_type_die (item_type, TYPE_QUALS (item_type), context_die);
9931 if (sub_die != NULL)
9932 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
9934 return mod_type_die;
9937 /* Generate DIEs for the generic parameters of T.
9938 T must be either a generic type or a generic function.
9939 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
9941 static void
9942 gen_generic_params_dies (tree t)
9944 tree parms, args;
9945 int parms_num, i;
9946 dw_die_ref die = NULL;
9948 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
9949 return;
9951 if (TYPE_P (t))
9952 die = lookup_type_die (t);
9953 else if (DECL_P (t))
9954 die = lookup_decl_die (t);
9956 gcc_assert (die);
9958 parms = lang_hooks.get_innermost_generic_parms (t);
9959 if (!parms)
9960 /* T has no generic parameter. It means T is neither a generic type
9961 or function. End of story. */
9962 return;
9964 parms_num = TREE_VEC_LENGTH (parms);
9965 args = lang_hooks.get_innermost_generic_args (t);
9966 for (i = 0; i < parms_num; i++)
9968 tree parm, arg, arg_pack_elems;
9970 parm = TREE_VEC_ELT (parms, i);
9971 arg = TREE_VEC_ELT (args, i);
9972 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
9973 gcc_assert (parm && TREE_VALUE (parm) && arg);
9975 if (parm && TREE_VALUE (parm) && arg)
9977 /* If PARM represents a template parameter pack,
9978 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
9979 by DW_TAG_template_*_parameter DIEs for the argument
9980 pack elements of ARG. Note that ARG would then be
9981 an argument pack. */
9982 if (arg_pack_elems)
9983 template_parameter_pack_die (TREE_VALUE (parm),
9984 arg_pack_elems,
9985 die);
9986 else
9987 generic_parameter_die (TREE_VALUE (parm), arg,
9988 true /* Emit DW_AT_name */, die);
9993 /* Create and return a DIE for PARM which should be
9994 the representation of a generic type parameter.
9995 For instance, in the C++ front end, PARM would be a template parameter.
9996 ARG is the argument to PARM.
9997 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
9998 name of the PARM.
9999 PARENT_DIE is the parent DIE which the new created DIE should be added to,
10000 as a child node. */
10002 static dw_die_ref
10003 generic_parameter_die (tree parm, tree arg,
10004 bool emit_name_p,
10005 dw_die_ref parent_die)
10007 dw_die_ref tmpl_die = NULL;
10008 const char *name = NULL;
10010 if (!parm || !DECL_NAME (parm) || !arg)
10011 return NULL;
10013 /* We support non-type generic parameters and arguments,
10014 type generic parameters and arguments, as well as
10015 generic generic parameters (a.k.a. template template parameters in C++)
10016 and arguments. */
10017 if (TREE_CODE (parm) == PARM_DECL)
10018 /* PARM is a nontype generic parameter */
10019 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10020 else if (TREE_CODE (parm) == TYPE_DECL)
10021 /* PARM is a type generic parameter. */
10022 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10023 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10024 /* PARM is a generic generic parameter.
10025 Its DIE is a GNU extension. It shall have a
10026 DW_AT_name attribute to represent the name of the template template
10027 parameter, and a DW_AT_GNU_template_name attribute to represent the
10028 name of the template template argument. */
10029 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10030 parent_die, parm);
10031 else
10032 gcc_unreachable ();
10034 if (tmpl_die)
10036 tree tmpl_type;
10038 /* If PARM is a generic parameter pack, it means we are
10039 emitting debug info for a template argument pack element.
10040 In other terms, ARG is a template argument pack element.
10041 In that case, we don't emit any DW_AT_name attribute for
10042 the die. */
10043 if (emit_name_p)
10045 name = IDENTIFIER_POINTER (DECL_NAME (parm));
10046 gcc_assert (name);
10047 add_AT_string (tmpl_die, DW_AT_name, name);
10050 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10052 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10053 TMPL_DIE should have a child DW_AT_type attribute that is set
10054 to the type of the argument to PARM, which is ARG.
10055 If PARM is a type generic parameter, TMPL_DIE should have a
10056 child DW_AT_type that is set to ARG. */
10057 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10058 add_type_attribute (tmpl_die, tmpl_type, 0,
10059 TREE_THIS_VOLATILE (tmpl_type),
10060 parent_die);
10062 else
10064 /* So TMPL_DIE is a DIE representing a
10065 a generic generic template parameter, a.k.a template template
10066 parameter in C++ and arg is a template. */
10068 /* The DW_AT_GNU_template_name attribute of the DIE must be set
10069 to the name of the argument. */
10070 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
10071 if (name)
10072 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
10075 if (TREE_CODE (parm) == PARM_DECL)
10076 /* So PARM is a non-type generic parameter.
10077 DWARF3 5.6.8 says we must set a DW_AT_const_value child
10078 attribute of TMPL_DIE which value represents the value
10079 of ARG.
10080 We must be careful here:
10081 The value of ARG might reference some function decls.
10082 We might currently be emitting debug info for a generic
10083 type and types are emitted before function decls, we don't
10084 know if the function decls referenced by ARG will actually be
10085 emitted after cgraph computations.
10086 So must defer the generation of the DW_AT_const_value to
10087 after cgraph is ready. */
10088 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
10091 return tmpl_die;
10094 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
10095 PARM_PACK must be a template parameter pack. The returned DIE
10096 will be child DIE of PARENT_DIE. */
10098 static dw_die_ref
10099 template_parameter_pack_die (tree parm_pack,
10100 tree parm_pack_args,
10101 dw_die_ref parent_die)
10103 dw_die_ref die;
10104 int j;
10106 gcc_assert (parent_die && parm_pack);
10108 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
10109 add_name_and_src_coords_attributes (die, parm_pack);
10110 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
10111 generic_parameter_die (parm_pack,
10112 TREE_VEC_ELT (parm_pack_args, j),
10113 false /* Don't emit DW_AT_name */,
10114 die);
10115 return die;
10118 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
10119 an enumerated type. */
10121 static inline int
10122 type_is_enum (const_tree type)
10124 return TREE_CODE (type) == ENUMERAL_TYPE;
10127 /* Return the DBX register number described by a given RTL node. */
10129 static unsigned int
10130 dbx_reg_number (const_rtx rtl)
10132 unsigned regno = REGNO (rtl);
10134 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10136 #ifdef LEAF_REG_REMAP
10137 if (current_function_uses_only_leaf_regs)
10139 int leaf_reg = LEAF_REG_REMAP (regno);
10140 if (leaf_reg != -1)
10141 regno = (unsigned) leaf_reg;
10143 #endif
10145 return DBX_REGISTER_NUMBER (regno);
10148 /* Optionally add a DW_OP_piece term to a location description expression.
10149 DW_OP_piece is only added if the location description expression already
10150 doesn't end with DW_OP_piece. */
10152 static void
10153 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10155 dw_loc_descr_ref loc;
10157 if (*list_head != NULL)
10159 /* Find the end of the chain. */
10160 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10163 if (loc->dw_loc_opc != DW_OP_piece)
10164 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10168 /* Return a location descriptor that designates a machine register or
10169 zero if there is none. */
10171 static dw_loc_descr_ref
10172 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10174 rtx regs;
10176 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10177 return 0;
10179 /* We only use "frame base" when we're sure we're talking about the
10180 post-prologue local stack frame. We do this by *not* running
10181 register elimination until this point, and recognizing the special
10182 argument pointer and soft frame pointer rtx's.
10183 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
10184 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
10185 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
10187 dw_loc_descr_ref result = NULL;
10189 if (dwarf_version >= 4 || !dwarf_strict)
10191 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
10192 initialized);
10193 if (result)
10194 add_loc_descr (&result,
10195 new_loc_descr (DW_OP_stack_value, 0, 0));
10197 return result;
10200 regs = targetm.dwarf_register_span (rtl);
10202 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
10203 return multiple_reg_loc_descriptor (rtl, regs, initialized);
10204 else
10205 return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
10208 /* Return a location descriptor that designates a machine register for
10209 a given hard register number. */
10211 static dw_loc_descr_ref
10212 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
10214 dw_loc_descr_ref reg_loc_descr;
10216 if (regno <= 31)
10217 reg_loc_descr
10218 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
10219 else
10220 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
10222 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10223 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10225 return reg_loc_descr;
10228 /* Given an RTL of a register, return a location descriptor that
10229 designates a value that spans more than one register. */
10231 static dw_loc_descr_ref
10232 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
10233 enum var_init_status initialized)
10235 int nregs, size, i;
10236 unsigned reg;
10237 dw_loc_descr_ref loc_result = NULL;
10239 reg = REGNO (rtl);
10240 #ifdef LEAF_REG_REMAP
10241 if (current_function_uses_only_leaf_regs)
10243 int leaf_reg = LEAF_REG_REMAP (reg);
10244 if (leaf_reg != -1)
10245 reg = (unsigned) leaf_reg;
10247 #endif
10248 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
10249 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
10251 /* Simple, contiguous registers. */
10252 if (regs == NULL_RTX)
10254 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
10256 loc_result = NULL;
10257 while (nregs--)
10259 dw_loc_descr_ref t;
10261 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
10262 VAR_INIT_STATUS_INITIALIZED);
10263 add_loc_descr (&loc_result, t);
10264 add_loc_descr_op_piece (&loc_result, size);
10265 ++reg;
10267 return loc_result;
10270 /* Now onto stupid register sets in non contiguous locations. */
10272 gcc_assert (GET_CODE (regs) == PARALLEL);
10274 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10275 loc_result = NULL;
10277 for (i = 0; i < XVECLEN (regs, 0); ++i)
10279 dw_loc_descr_ref t;
10281 t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
10282 VAR_INIT_STATUS_INITIALIZED);
10283 add_loc_descr (&loc_result, t);
10284 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10285 add_loc_descr_op_piece (&loc_result, size);
10288 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10289 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10290 return loc_result;
10293 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
10295 /* Return a location descriptor that designates a constant i,
10296 as a compound operation from constant (i >> shift), constant shift
10297 and DW_OP_shl. */
10299 static dw_loc_descr_ref
10300 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10302 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
10303 add_loc_descr (&ret, int_loc_descriptor (shift));
10304 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
10305 return ret;
10308 /* Return a location descriptor that designates a constant. */
10310 static dw_loc_descr_ref
10311 int_loc_descriptor (HOST_WIDE_INT i)
10313 enum dwarf_location_atom op;
10315 /* Pick the smallest representation of a constant, rather than just
10316 defaulting to the LEB encoding. */
10317 if (i >= 0)
10319 int clz = clz_hwi (i);
10320 int ctz = ctz_hwi (i);
10321 if (i <= 31)
10322 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
10323 else if (i <= 0xff)
10324 op = DW_OP_const1u;
10325 else if (i <= 0xffff)
10326 op = DW_OP_const2u;
10327 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10328 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10329 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
10330 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
10331 while DW_OP_const4u is 5 bytes. */
10332 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
10333 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10334 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10335 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
10336 while DW_OP_const4u is 5 bytes. */
10337 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10338 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10339 op = DW_OP_const4u;
10340 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10341 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10342 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
10343 while DW_OP_constu of constant >= 0x100000000 takes at least
10344 6 bytes. */
10345 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10346 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10347 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
10348 >= HOST_BITS_PER_WIDE_INT)
10349 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
10350 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
10351 while DW_OP_constu takes in this case at least 6 bytes. */
10352 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
10353 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10354 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10355 && size_of_uleb128 (i) > 6)
10356 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
10357 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
10358 else
10359 op = DW_OP_constu;
10361 else
10363 if (i >= -0x80)
10364 op = DW_OP_const1s;
10365 else if (i >= -0x8000)
10366 op = DW_OP_const2s;
10367 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10369 if (size_of_int_loc_descriptor (i) < 5)
10371 dw_loc_descr_ref ret = int_loc_descriptor (-i);
10372 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10373 return ret;
10375 op = DW_OP_const4s;
10377 else
10379 if (size_of_int_loc_descriptor (i)
10380 < (unsigned long) 1 + size_of_sleb128 (i))
10382 dw_loc_descr_ref ret = int_loc_descriptor (-i);
10383 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10384 return ret;
10386 op = DW_OP_consts;
10390 return new_loc_descr (op, i, 0);
10393 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
10394 without actually allocating it. */
10396 static unsigned long
10397 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10399 return size_of_int_loc_descriptor (i >> shift)
10400 + size_of_int_loc_descriptor (shift)
10401 + 1;
10404 /* Return size_of_locs (int_loc_descriptor (i)) without
10405 actually allocating it. */
10407 static unsigned long
10408 size_of_int_loc_descriptor (HOST_WIDE_INT i)
10410 unsigned long s;
10412 if (i >= 0)
10414 int clz, ctz;
10415 if (i <= 31)
10416 return 1;
10417 else if (i <= 0xff)
10418 return 2;
10419 else if (i <= 0xffff)
10420 return 3;
10421 clz = clz_hwi (i);
10422 ctz = ctz_hwi (i);
10423 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10424 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10425 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10426 - clz - 5);
10427 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10428 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10429 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10430 - clz - 8);
10431 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10432 return 5;
10433 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
10434 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10435 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10436 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10437 - clz - 8);
10438 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10439 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
10440 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10441 - clz - 16);
10442 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10443 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10444 && s > 6)
10445 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10446 - clz - 32);
10447 else
10448 return 1 + s;
10450 else
10452 if (i >= -0x80)
10453 return 2;
10454 else if (i >= -0x8000)
10455 return 3;
10456 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10458 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
10460 s = size_of_int_loc_descriptor (-i) + 1;
10461 if (s < 5)
10462 return s;
10464 return 5;
10466 else
10468 unsigned long r = 1 + size_of_sleb128 (i);
10469 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
10471 s = size_of_int_loc_descriptor (-i) + 1;
10472 if (s < r)
10473 return s;
10475 return r;
10480 /* Return loc description representing "address" of integer value.
10481 This can appear only as toplevel expression. */
10483 static dw_loc_descr_ref
10484 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
10486 int litsize;
10487 dw_loc_descr_ref loc_result = NULL;
10489 if (!(dwarf_version >= 4 || !dwarf_strict))
10490 return NULL;
10492 litsize = size_of_int_loc_descriptor (i);
10493 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
10494 is more compact. For DW_OP_stack_value we need:
10495 litsize + 1 (DW_OP_stack_value)
10496 and for DW_OP_implicit_value:
10497 1 (DW_OP_implicit_value) + 1 (length) + size. */
10498 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
10500 loc_result = int_loc_descriptor (i);
10501 add_loc_descr (&loc_result,
10502 new_loc_descr (DW_OP_stack_value, 0, 0));
10503 return loc_result;
10506 loc_result = new_loc_descr (DW_OP_implicit_value,
10507 size, 0);
10508 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
10509 loc_result->dw_loc_oprnd2.v.val_int = i;
10510 return loc_result;
10513 /* Return a location descriptor that designates a base+offset location. */
10515 static dw_loc_descr_ref
10516 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
10517 enum var_init_status initialized)
10519 unsigned int regno;
10520 dw_loc_descr_ref result;
10521 dw_fde_ref fde = cfun->fde;
10523 /* We only use "frame base" when we're sure we're talking about the
10524 post-prologue local stack frame. We do this by *not* running
10525 register elimination until this point, and recognizing the special
10526 argument pointer and soft frame pointer rtx's. */
10527 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
10529 rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
10531 if (elim != reg)
10533 if (GET_CODE (elim) == PLUS)
10535 offset += INTVAL (XEXP (elim, 1));
10536 elim = XEXP (elim, 0);
10538 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
10539 && (elim == hard_frame_pointer_rtx
10540 || elim == stack_pointer_rtx))
10541 || elim == (frame_pointer_needed
10542 ? hard_frame_pointer_rtx
10543 : stack_pointer_rtx));
10545 /* If drap register is used to align stack, use frame
10546 pointer + offset to access stack variables. If stack
10547 is aligned without drap, use stack pointer + offset to
10548 access stack variables. */
10549 if (crtl->stack_realign_tried
10550 && reg == frame_pointer_rtx)
10552 int base_reg
10553 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
10554 ? HARD_FRAME_POINTER_REGNUM
10555 : REGNO (elim));
10556 return new_reg_loc_descr (base_reg, offset);
10559 gcc_assert (frame_pointer_fb_offset_valid);
10560 offset += frame_pointer_fb_offset;
10561 return new_loc_descr (DW_OP_fbreg, offset, 0);
10565 regno = DWARF_FRAME_REGNUM (REGNO (reg));
10567 if (!optimize && fde
10568 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
10570 /* Use cfa+offset to represent the location of arguments passed
10571 on the stack when drap is used to align stack.
10572 Only do this when not optimizing, for optimized code var-tracking
10573 is supposed to track where the arguments live and the register
10574 used as vdrap or drap in some spot might be used for something
10575 else in other part of the routine. */
10576 return new_loc_descr (DW_OP_fbreg, offset, 0);
10579 if (regno <= 31)
10580 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
10581 offset, 0);
10582 else
10583 result = new_loc_descr (DW_OP_bregx, regno, offset);
10585 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10586 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10588 return result;
10591 /* Return true if this RTL expression describes a base+offset calculation. */
10593 static inline int
10594 is_based_loc (const_rtx rtl)
10596 return (GET_CODE (rtl) == PLUS
10597 && ((REG_P (XEXP (rtl, 0))
10598 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
10599 && CONST_INT_P (XEXP (rtl, 1)))));
10602 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
10603 failed. */
10605 static dw_loc_descr_ref
10606 tls_mem_loc_descriptor (rtx mem)
10608 tree base;
10609 dw_loc_descr_ref loc_result;
10611 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
10612 return NULL;
10614 base = get_base_address (MEM_EXPR (mem));
10615 if (base == NULL
10616 || TREE_CODE (base) != VAR_DECL
10617 || !DECL_THREAD_LOCAL_P (base))
10618 return NULL;
10620 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
10621 if (loc_result == NULL)
10622 return NULL;
10624 if (MEM_OFFSET (mem))
10625 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
10627 return loc_result;
10630 /* Output debug info about reason why we failed to expand expression as dwarf
10631 expression. */
10633 static void
10634 expansion_failed (tree expr, rtx rtl, char const *reason)
10636 if (dump_file && (dump_flags & TDF_DETAILS))
10638 fprintf (dump_file, "Failed to expand as dwarf: ");
10639 if (expr)
10640 print_generic_expr (dump_file, expr, dump_flags);
10641 if (rtl)
10643 fprintf (dump_file, "\n");
10644 print_rtl (dump_file, rtl);
10646 fprintf (dump_file, "\nReason: %s\n", reason);
10650 /* Helper function for const_ok_for_output, called either directly
10651 or via for_each_rtx. */
10653 static int
10654 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
10656 rtx rtl = *rtlp;
10658 if (GET_CODE (rtl) == UNSPEC)
10660 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
10661 we can't express it in the debug info. */
10662 #ifdef ENABLE_CHECKING
10663 /* Don't complain about TLS UNSPECs, those are just too hard to
10664 delegitimize. */
10665 if (XVECLEN (rtl, 0) != 1
10666 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
10667 || SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0)) == NULL
10668 || TREE_CODE (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))) != VAR_DECL
10669 || !DECL_THREAD_LOCAL_P (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))))
10670 inform (current_function_decl
10671 ? DECL_SOURCE_LOCATION (current_function_decl)
10672 : UNKNOWN_LOCATION,
10673 #if NUM_UNSPEC_VALUES > 0
10674 "non-delegitimized UNSPEC %s (%d) found in variable location",
10675 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
10676 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
10677 XINT (rtl, 1));
10678 #else
10679 "non-delegitimized UNSPEC %d found in variable location",
10680 XINT (rtl, 1));
10681 #endif
10682 #endif
10683 expansion_failed (NULL_TREE, rtl,
10684 "UNSPEC hasn't been delegitimized.\n");
10685 return 1;
10688 if (GET_CODE (rtl) != SYMBOL_REF)
10689 return 0;
10691 if (CONSTANT_POOL_ADDRESS_P (rtl))
10693 bool marked;
10694 get_pool_constant_mark (rtl, &marked);
10695 /* If all references to this pool constant were optimized away,
10696 it was not output and thus we can't represent it. */
10697 if (!marked)
10699 expansion_failed (NULL_TREE, rtl,
10700 "Constant was removed from constant pool.\n");
10701 return 1;
10705 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
10706 return 1;
10708 /* Avoid references to external symbols in debug info, on several targets
10709 the linker might even refuse to link when linking a shared library,
10710 and in many other cases the relocations for .debug_info/.debug_loc are
10711 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
10712 to be defined within the same shared library or executable are fine. */
10713 if (SYMBOL_REF_EXTERNAL_P (rtl))
10715 tree decl = SYMBOL_REF_DECL (rtl);
10717 if (decl == NULL || !targetm.binds_local_p (decl))
10719 expansion_failed (NULL_TREE, rtl,
10720 "Symbol not defined in current TU.\n");
10721 return 1;
10725 return 0;
10728 /* Return true if constant RTL can be emitted in DW_OP_addr or
10729 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
10730 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
10732 static bool
10733 const_ok_for_output (rtx rtl)
10735 if (GET_CODE (rtl) == SYMBOL_REF)
10736 return const_ok_for_output_1 (&rtl, NULL) == 0;
10738 if (GET_CODE (rtl) == CONST)
10739 return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
10741 return true;
10744 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
10745 if possible, NULL otherwise. */
10747 static dw_die_ref
10748 base_type_for_mode (enum machine_mode mode, bool unsignedp)
10750 dw_die_ref type_die;
10751 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
10753 if (type == NULL)
10754 return NULL;
10755 switch (TREE_CODE (type))
10757 case INTEGER_TYPE:
10758 case REAL_TYPE:
10759 break;
10760 default:
10761 return NULL;
10763 type_die = lookup_type_die (type);
10764 if (!type_die)
10765 type_die = modified_type_die (type, TYPE_UNQUALIFIED, comp_unit_die ());
10766 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
10767 return NULL;
10768 return type_die;
10771 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
10772 type matching MODE, or, if MODE is narrower than or as wide as
10773 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
10774 possible. */
10776 static dw_loc_descr_ref
10777 convert_descriptor_to_mode (enum machine_mode mode, dw_loc_descr_ref op)
10779 enum machine_mode outer_mode = mode;
10780 dw_die_ref type_die;
10781 dw_loc_descr_ref cvt;
10783 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
10785 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
10786 return op;
10788 type_die = base_type_for_mode (outer_mode, 1);
10789 if (type_die == NULL)
10790 return NULL;
10791 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10792 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10793 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10794 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10795 add_loc_descr (&op, cvt);
10796 return op;
10799 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
10801 static dw_loc_descr_ref
10802 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
10803 dw_loc_descr_ref op1)
10805 dw_loc_descr_ref ret = op0;
10806 add_loc_descr (&ret, op1);
10807 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
10808 if (STORE_FLAG_VALUE != 1)
10810 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
10811 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
10813 return ret;
10816 /* Return location descriptor for signed comparison OP RTL. */
10818 static dw_loc_descr_ref
10819 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
10820 enum machine_mode mem_mode)
10822 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
10823 dw_loc_descr_ref op0, op1;
10824 int shift;
10826 if (op_mode == VOIDmode)
10827 op_mode = GET_MODE (XEXP (rtl, 1));
10828 if (op_mode == VOIDmode)
10829 return NULL;
10831 if (dwarf_strict
10832 && (GET_MODE_CLASS (op_mode) != MODE_INT
10833 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
10834 return NULL;
10836 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
10837 VAR_INIT_STATUS_INITIALIZED);
10838 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
10839 VAR_INIT_STATUS_INITIALIZED);
10841 if (op0 == NULL || op1 == NULL)
10842 return NULL;
10844 if (GET_MODE_CLASS (op_mode) != MODE_INT
10845 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
10846 return compare_loc_descriptor (op, op0, op1);
10848 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
10850 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
10851 dw_loc_descr_ref cvt;
10853 if (type_die == NULL)
10854 return NULL;
10855 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10856 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10857 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10858 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10859 add_loc_descr (&op0, cvt);
10860 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10861 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10862 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10863 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10864 add_loc_descr (&op1, cvt);
10865 return compare_loc_descriptor (op, op0, op1);
10868 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
10869 /* For eq/ne, if the operands are known to be zero-extended,
10870 there is no need to do the fancy shifting up. */
10871 if (op == DW_OP_eq || op == DW_OP_ne)
10873 dw_loc_descr_ref last0, last1;
10874 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
10876 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
10878 /* deref_size zero extends, and for constants we can check
10879 whether they are zero extended or not. */
10880 if (((last0->dw_loc_opc == DW_OP_deref_size
10881 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
10882 || (CONST_INT_P (XEXP (rtl, 0))
10883 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
10884 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
10885 && ((last1->dw_loc_opc == DW_OP_deref_size
10886 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
10887 || (CONST_INT_P (XEXP (rtl, 1))
10888 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
10889 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
10890 return compare_loc_descriptor (op, op0, op1);
10892 /* EQ/NE comparison against constant in narrower type than
10893 DWARF2_ADDR_SIZE can be performed either as
10894 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
10895 DW_OP_{eq,ne}
10897 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
10898 DW_OP_{eq,ne}. Pick whatever is shorter. */
10899 if (CONST_INT_P (XEXP (rtl, 1))
10900 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
10901 && (size_of_int_loc_descriptor (shift) + 1
10902 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
10903 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
10904 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
10905 & GET_MODE_MASK (op_mode))))
10907 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
10908 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
10909 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
10910 & GET_MODE_MASK (op_mode));
10911 return compare_loc_descriptor (op, op0, op1);
10914 add_loc_descr (&op0, int_loc_descriptor (shift));
10915 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
10916 if (CONST_INT_P (XEXP (rtl, 1)))
10917 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
10918 else
10920 add_loc_descr (&op1, int_loc_descriptor (shift));
10921 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
10923 return compare_loc_descriptor (op, op0, op1);
10926 /* Return location descriptor for unsigned comparison OP RTL. */
10928 static dw_loc_descr_ref
10929 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
10930 enum machine_mode mem_mode)
10932 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
10933 dw_loc_descr_ref op0, op1;
10935 if (op_mode == VOIDmode)
10936 op_mode = GET_MODE (XEXP (rtl, 1));
10937 if (op_mode == VOIDmode)
10938 return NULL;
10939 if (GET_MODE_CLASS (op_mode) != MODE_INT)
10940 return NULL;
10942 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
10943 return NULL;
10945 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
10946 VAR_INIT_STATUS_INITIALIZED);
10947 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
10948 VAR_INIT_STATUS_INITIALIZED);
10950 if (op0 == NULL || op1 == NULL)
10951 return NULL;
10953 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
10955 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
10956 dw_loc_descr_ref last0, last1;
10957 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
10959 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
10961 if (CONST_INT_P (XEXP (rtl, 0)))
10962 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
10963 /* deref_size zero extends, so no need to mask it again. */
10964 else if (last0->dw_loc_opc != DW_OP_deref_size
10965 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
10967 add_loc_descr (&op0, int_loc_descriptor (mask));
10968 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
10970 if (CONST_INT_P (XEXP (rtl, 1)))
10971 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
10972 /* deref_size zero extends, so no need to mask it again. */
10973 else if (last1->dw_loc_opc != DW_OP_deref_size
10974 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
10976 add_loc_descr (&op1, int_loc_descriptor (mask));
10977 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
10980 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
10982 HOST_WIDE_INT bias = 1;
10983 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
10984 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
10985 if (CONST_INT_P (XEXP (rtl, 1)))
10986 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
10987 + INTVAL (XEXP (rtl, 1)));
10988 else
10989 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
10990 bias, 0));
10992 return compare_loc_descriptor (op, op0, op1);
10995 /* Return location descriptor for {U,S}{MIN,MAX}. */
10997 static dw_loc_descr_ref
10998 minmax_loc_descriptor (rtx rtl, enum machine_mode mode,
10999 enum machine_mode mem_mode)
11001 enum dwarf_location_atom op;
11002 dw_loc_descr_ref op0, op1, ret;
11003 dw_loc_descr_ref bra_node, drop_node;
11005 if (dwarf_strict
11006 && (GET_MODE_CLASS (mode) != MODE_INT
11007 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
11008 return NULL;
11010 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11011 VAR_INIT_STATUS_INITIALIZED);
11012 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11013 VAR_INIT_STATUS_INITIALIZED);
11015 if (op0 == NULL || op1 == NULL)
11016 return NULL;
11018 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
11019 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
11020 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
11021 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
11023 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11025 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11026 add_loc_descr (&op0, int_loc_descriptor (mask));
11027 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11028 add_loc_descr (&op1, int_loc_descriptor (mask));
11029 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11031 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11033 HOST_WIDE_INT bias = 1;
11034 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11035 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11036 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11039 else if (GET_MODE_CLASS (mode) == MODE_INT
11040 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11042 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
11043 add_loc_descr (&op0, int_loc_descriptor (shift));
11044 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11045 add_loc_descr (&op1, int_loc_descriptor (shift));
11046 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11048 else if (GET_MODE_CLASS (mode) == MODE_INT
11049 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11051 dw_die_ref type_die = base_type_for_mode (mode, 0);
11052 dw_loc_descr_ref cvt;
11053 if (type_die == NULL)
11054 return NULL;
11055 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11056 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11057 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11058 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11059 add_loc_descr (&op0, cvt);
11060 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11061 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11062 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11063 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11064 add_loc_descr (&op1, cvt);
11067 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
11068 op = DW_OP_lt;
11069 else
11070 op = DW_OP_gt;
11071 ret = op0;
11072 add_loc_descr (&ret, op1);
11073 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11074 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11075 add_loc_descr (&ret, bra_node);
11076 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11077 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11078 add_loc_descr (&ret, drop_node);
11079 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11080 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11081 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
11082 && GET_MODE_CLASS (mode) == MODE_INT
11083 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11084 ret = convert_descriptor_to_mode (mode, ret);
11085 return ret;
11088 /* Helper function for mem_loc_descriptor. Perform OP binary op,
11089 but after converting arguments to type_die, afterwards
11090 convert back to unsigned. */
11092 static dw_loc_descr_ref
11093 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
11094 enum machine_mode mode, enum machine_mode mem_mode)
11096 dw_loc_descr_ref cvt, op0, op1;
11098 if (type_die == NULL)
11099 return NULL;
11100 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11101 VAR_INIT_STATUS_INITIALIZED);
11102 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11103 VAR_INIT_STATUS_INITIALIZED);
11104 if (op0 == NULL || op1 == NULL)
11105 return NULL;
11106 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11107 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11108 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11109 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11110 add_loc_descr (&op0, cvt);
11111 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11112 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11113 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11114 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11115 add_loc_descr (&op1, cvt);
11116 add_loc_descr (&op0, op1);
11117 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
11118 return convert_descriptor_to_mode (mode, op0);
11121 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
11122 const0 is DW_OP_lit0 or corresponding typed constant,
11123 const1 is DW_OP_lit1 or corresponding typed constant
11124 and constMSB is constant with just the MSB bit set
11125 for the mode):
11126 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11127 L1: const0 DW_OP_swap
11128 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
11129 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11130 L3: DW_OP_drop
11131 L4: DW_OP_nop
11133 CTZ is similar:
11134 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11135 L1: const0 DW_OP_swap
11136 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11137 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11138 L3: DW_OP_drop
11139 L4: DW_OP_nop
11141 FFS is similar:
11142 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
11143 L1: const1 DW_OP_swap
11144 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11145 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11146 L3: DW_OP_drop
11147 L4: DW_OP_nop */
11149 static dw_loc_descr_ref
11150 clz_loc_descriptor (rtx rtl, enum machine_mode mode,
11151 enum machine_mode mem_mode)
11153 dw_loc_descr_ref op0, ret, tmp;
11154 HOST_WIDE_INT valv;
11155 dw_loc_descr_ref l1jump, l1label;
11156 dw_loc_descr_ref l2jump, l2label;
11157 dw_loc_descr_ref l3jump, l3label;
11158 dw_loc_descr_ref l4jump, l4label;
11159 rtx msb;
11161 if (GET_MODE_CLASS (mode) != MODE_INT
11162 || GET_MODE (XEXP (rtl, 0)) != mode
11163 || (GET_CODE (rtl) == CLZ
11164 && GET_MODE_BITSIZE (mode) > 2 * HOST_BITS_PER_WIDE_INT))
11165 return NULL;
11167 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11168 VAR_INIT_STATUS_INITIALIZED);
11169 if (op0 == NULL)
11170 return NULL;
11171 ret = op0;
11172 if (GET_CODE (rtl) == CLZ)
11174 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11175 valv = GET_MODE_BITSIZE (mode);
11177 else if (GET_CODE (rtl) == FFS)
11178 valv = 0;
11179 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11180 valv = GET_MODE_BITSIZE (mode);
11181 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11182 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
11183 add_loc_descr (&ret, l1jump);
11184 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11185 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
11186 VAR_INIT_STATUS_INITIALIZED);
11187 if (tmp == NULL)
11188 return NULL;
11189 add_loc_descr (&ret, tmp);
11190 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
11191 add_loc_descr (&ret, l4jump);
11192 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
11193 ? const1_rtx : const0_rtx,
11194 mode, mem_mode,
11195 VAR_INIT_STATUS_INITIALIZED);
11196 if (l1label == NULL)
11197 return NULL;
11198 add_loc_descr (&ret, l1label);
11199 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11200 l2label = new_loc_descr (DW_OP_dup, 0, 0);
11201 add_loc_descr (&ret, l2label);
11202 if (GET_CODE (rtl) != CLZ)
11203 msb = const1_rtx;
11204 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11205 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
11206 << (GET_MODE_BITSIZE (mode) - 1));
11207 else
11208 msb = immed_double_const (0, (unsigned HOST_WIDE_INT) 1
11209 << (GET_MODE_BITSIZE (mode)
11210 - HOST_BITS_PER_WIDE_INT - 1), mode);
11211 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
11212 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11213 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
11214 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
11215 else
11216 tmp = mem_loc_descriptor (msb, mode, mem_mode,
11217 VAR_INIT_STATUS_INITIALIZED);
11218 if (tmp == NULL)
11219 return NULL;
11220 add_loc_descr (&ret, tmp);
11221 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11222 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
11223 add_loc_descr (&ret, l3jump);
11224 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11225 VAR_INIT_STATUS_INITIALIZED);
11226 if (tmp == NULL)
11227 return NULL;
11228 add_loc_descr (&ret, tmp);
11229 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
11230 ? DW_OP_shl : DW_OP_shr, 0, 0));
11231 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11232 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
11233 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11234 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
11235 add_loc_descr (&ret, l2jump);
11236 l3label = new_loc_descr (DW_OP_drop, 0, 0);
11237 add_loc_descr (&ret, l3label);
11238 l4label = new_loc_descr (DW_OP_nop, 0, 0);
11239 add_loc_descr (&ret, l4label);
11240 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11241 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11242 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11243 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11244 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11245 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
11246 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11247 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
11248 return ret;
11251 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
11252 const1 is DW_OP_lit1 or corresponding typed constant):
11253 const0 DW_OP_swap
11254 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11255 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11256 L2: DW_OP_drop
11258 PARITY is similar:
11259 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11260 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11261 L2: DW_OP_drop */
11263 static dw_loc_descr_ref
11264 popcount_loc_descriptor (rtx rtl, enum machine_mode mode,
11265 enum machine_mode mem_mode)
11267 dw_loc_descr_ref op0, ret, tmp;
11268 dw_loc_descr_ref l1jump, l1label;
11269 dw_loc_descr_ref l2jump, l2label;
11271 if (GET_MODE_CLASS (mode) != MODE_INT
11272 || GET_MODE (XEXP (rtl, 0)) != mode)
11273 return NULL;
11275 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11276 VAR_INIT_STATUS_INITIALIZED);
11277 if (op0 == NULL)
11278 return NULL;
11279 ret = op0;
11280 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11281 VAR_INIT_STATUS_INITIALIZED);
11282 if (tmp == NULL)
11283 return NULL;
11284 add_loc_descr (&ret, tmp);
11285 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11286 l1label = new_loc_descr (DW_OP_dup, 0, 0);
11287 add_loc_descr (&ret, l1label);
11288 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11289 add_loc_descr (&ret, l2jump);
11290 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11291 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11292 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11293 VAR_INIT_STATUS_INITIALIZED);
11294 if (tmp == NULL)
11295 return NULL;
11296 add_loc_descr (&ret, tmp);
11297 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11298 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
11299 ? DW_OP_plus : DW_OP_xor, 0, 0));
11300 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11301 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11302 VAR_INIT_STATUS_INITIALIZED);
11303 add_loc_descr (&ret, tmp);
11304 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11305 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11306 add_loc_descr (&ret, l1jump);
11307 l2label = new_loc_descr (DW_OP_drop, 0, 0);
11308 add_loc_descr (&ret, l2label);
11309 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11310 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11311 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11312 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11313 return ret;
11316 /* BSWAP (constS is initial shift count, either 56 or 24):
11317 constS const0
11318 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
11319 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
11320 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
11321 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
11322 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
11324 static dw_loc_descr_ref
11325 bswap_loc_descriptor (rtx rtl, enum machine_mode mode,
11326 enum machine_mode mem_mode)
11328 dw_loc_descr_ref op0, ret, tmp;
11329 dw_loc_descr_ref l1jump, l1label;
11330 dw_loc_descr_ref l2jump, l2label;
11332 if (GET_MODE_CLASS (mode) != MODE_INT
11333 || BITS_PER_UNIT != 8
11334 || (GET_MODE_BITSIZE (mode) != 32
11335 && GET_MODE_BITSIZE (mode) != 64))
11336 return NULL;
11338 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11339 VAR_INIT_STATUS_INITIALIZED);
11340 if (op0 == NULL)
11341 return NULL;
11343 ret = op0;
11344 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11345 mode, mem_mode,
11346 VAR_INIT_STATUS_INITIALIZED);
11347 if (tmp == NULL)
11348 return NULL;
11349 add_loc_descr (&ret, tmp);
11350 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11351 VAR_INIT_STATUS_INITIALIZED);
11352 if (tmp == NULL)
11353 return NULL;
11354 add_loc_descr (&ret, tmp);
11355 l1label = new_loc_descr (DW_OP_pick, 2, 0);
11356 add_loc_descr (&ret, l1label);
11357 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11358 mode, mem_mode,
11359 VAR_INIT_STATUS_INITIALIZED);
11360 add_loc_descr (&ret, tmp);
11361 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
11362 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11363 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11364 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
11365 VAR_INIT_STATUS_INITIALIZED);
11366 if (tmp == NULL)
11367 return NULL;
11368 add_loc_descr (&ret, tmp);
11369 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11370 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
11371 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11372 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11373 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11374 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11375 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11376 VAR_INIT_STATUS_INITIALIZED);
11377 add_loc_descr (&ret, tmp);
11378 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
11379 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11380 add_loc_descr (&ret, l2jump);
11381 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
11382 VAR_INIT_STATUS_INITIALIZED);
11383 add_loc_descr (&ret, tmp);
11384 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11385 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11386 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11387 add_loc_descr (&ret, l1jump);
11388 l2label = new_loc_descr (DW_OP_drop, 0, 0);
11389 add_loc_descr (&ret, l2label);
11390 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11391 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11392 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11393 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11394 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11395 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11396 return ret;
11399 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
11400 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11401 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
11402 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
11404 ROTATERT is similar:
11405 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
11406 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11407 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
11409 static dw_loc_descr_ref
11410 rotate_loc_descriptor (rtx rtl, enum machine_mode mode,
11411 enum machine_mode mem_mode)
11413 rtx rtlop1 = XEXP (rtl, 1);
11414 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
11415 int i;
11417 if (GET_MODE_CLASS (mode) != MODE_INT)
11418 return NULL;
11420 if (GET_MODE (rtlop1) != VOIDmode
11421 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
11422 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
11423 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11424 VAR_INIT_STATUS_INITIALIZED);
11425 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
11426 VAR_INIT_STATUS_INITIALIZED);
11427 if (op0 == NULL || op1 == NULL)
11428 return NULL;
11429 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11430 for (i = 0; i < 2; i++)
11432 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
11433 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
11434 mode, mem_mode,
11435 VAR_INIT_STATUS_INITIALIZED);
11436 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
11437 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11438 ? DW_OP_const4u
11439 : HOST_BITS_PER_WIDE_INT == 64
11440 ? DW_OP_const8u : DW_OP_constu,
11441 GET_MODE_MASK (mode), 0);
11442 else
11443 mask[i] = NULL;
11444 if (mask[i] == NULL)
11445 return NULL;
11446 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
11448 ret = op0;
11449 add_loc_descr (&ret, op1);
11450 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11451 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11452 if (GET_CODE (rtl) == ROTATERT)
11454 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11455 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11456 GET_MODE_BITSIZE (mode), 0));
11458 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11459 if (mask[0] != NULL)
11460 add_loc_descr (&ret, mask[0]);
11461 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11462 if (mask[1] != NULL)
11464 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11465 add_loc_descr (&ret, mask[1]);
11466 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11468 if (GET_CODE (rtl) == ROTATE)
11470 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11471 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11472 GET_MODE_BITSIZE (mode), 0));
11474 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11475 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11476 return ret;
11479 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
11480 for DEBUG_PARAMETER_REF RTL. */
11482 static dw_loc_descr_ref
11483 parameter_ref_descriptor (rtx rtl)
11485 dw_loc_descr_ref ret;
11486 dw_die_ref ref;
11488 if (dwarf_strict)
11489 return NULL;
11490 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
11491 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
11492 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
11493 if (ref)
11495 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11496 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
11497 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
11499 else
11501 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
11502 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
11504 return ret;
11507 /* Helper function to get mode of MEM's address. */
11509 enum machine_mode
11510 get_address_mode (rtx mem)
11512 enum machine_mode mode = GET_MODE (XEXP (mem, 0));
11513 if (mode != VOIDmode)
11514 return mode;
11515 return targetm.addr_space.address_mode (MEM_ADDR_SPACE (mem));
11518 /* The following routine converts the RTL for a variable or parameter
11519 (resident in memory) into an equivalent Dwarf representation of a
11520 mechanism for getting the address of that same variable onto the top of a
11521 hypothetical "address evaluation" stack.
11523 When creating memory location descriptors, we are effectively transforming
11524 the RTL for a memory-resident object into its Dwarf postfix expression
11525 equivalent. This routine recursively descends an RTL tree, turning
11526 it into Dwarf postfix code as it goes.
11528 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
11530 MEM_MODE is the mode of the memory reference, needed to handle some
11531 autoincrement addressing modes.
11533 Return 0 if we can't represent the location. */
11535 dw_loc_descr_ref
11536 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
11537 enum machine_mode mem_mode,
11538 enum var_init_status initialized)
11540 dw_loc_descr_ref mem_loc_result = NULL;
11541 enum dwarf_location_atom op;
11542 dw_loc_descr_ref op0, op1;
11544 if (mode == VOIDmode)
11545 mode = GET_MODE (rtl);
11547 /* Note that for a dynamically sized array, the location we will generate a
11548 description of here will be the lowest numbered location which is
11549 actually within the array. That's *not* necessarily the same as the
11550 zeroth element of the array. */
11552 rtl = targetm.delegitimize_address (rtl);
11554 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
11555 return NULL;
11557 switch (GET_CODE (rtl))
11559 case POST_INC:
11560 case POST_DEC:
11561 case POST_MODIFY:
11562 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
11564 case SUBREG:
11565 /* The case of a subreg may arise when we have a local (register)
11566 variable or a formal (register) parameter which doesn't quite fill
11567 up an entire register. For now, just assume that it is
11568 legitimate to make the Dwarf info refer to the whole register which
11569 contains the given subreg. */
11570 if (!subreg_lowpart_p (rtl))
11571 break;
11572 if (GET_MODE_CLASS (mode) == MODE_INT
11573 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (rtl))) == MODE_INT
11574 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11575 #ifdef POINTERS_EXTEND_UNSIGNED
11576 || (mode == Pmode && mem_mode != VOIDmode)
11577 #endif
11579 && GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))) <= DWARF2_ADDR_SIZE)
11581 mem_loc_result = mem_loc_descriptor (SUBREG_REG (rtl),
11582 GET_MODE (SUBREG_REG (rtl)),
11583 mem_mode, initialized);
11584 break;
11586 if (dwarf_strict)
11587 break;
11588 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))))
11589 break;
11590 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl)))
11591 && (GET_MODE_CLASS (mode) != MODE_INT
11592 || GET_MODE_CLASS (GET_MODE (SUBREG_REG (rtl))) != MODE_INT))
11593 break;
11594 else
11596 dw_die_ref type_die;
11597 dw_loc_descr_ref cvt;
11599 mem_loc_result = mem_loc_descriptor (SUBREG_REG (rtl),
11600 GET_MODE (SUBREG_REG (rtl)),
11601 mem_mode, initialized);
11602 if (mem_loc_result == NULL)
11603 break;
11604 type_die = base_type_for_mode (mode,
11605 GET_MODE_CLASS (mode) == MODE_INT);
11606 if (type_die == NULL)
11608 mem_loc_result = NULL;
11609 break;
11611 if (GET_MODE_SIZE (mode)
11612 != GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))))
11613 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11614 else
11615 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
11616 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11617 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11618 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11619 add_loc_descr (&mem_loc_result, cvt);
11621 break;
11623 case REG:
11624 if (GET_MODE_CLASS (mode) != MODE_INT
11625 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
11626 #ifdef POINTERS_EXTEND_UNSIGNED
11627 && (mode != Pmode || mem_mode == VOIDmode)
11628 #endif
11631 dw_die_ref type_die;
11633 if (dwarf_strict)
11634 break;
11635 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
11636 break;
11637 type_die = base_type_for_mode (mode,
11638 GET_MODE_CLASS (mode) == MODE_INT);
11639 if (type_die == NULL)
11640 break;
11641 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
11642 dbx_reg_number (rtl), 0);
11643 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
11644 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
11645 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
11646 break;
11648 /* Whenever a register number forms a part of the description of the
11649 method for calculating the (dynamic) address of a memory resident
11650 object, DWARF rules require the register number be referred to as
11651 a "base register". This distinction is not based in any way upon
11652 what category of register the hardware believes the given register
11653 belongs to. This is strictly DWARF terminology we're dealing with
11654 here. Note that in cases where the location of a memory-resident
11655 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
11656 OP_CONST (0)) the actual DWARF location descriptor that we generate
11657 may just be OP_BASEREG (basereg). This may look deceptively like
11658 the object in question was allocated to a register (rather than in
11659 memory) so DWARF consumers need to be aware of the subtle
11660 distinction between OP_REG and OP_BASEREG. */
11661 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
11662 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
11663 else if (stack_realign_drap
11664 && crtl->drap_reg
11665 && crtl->args.internal_arg_pointer == rtl
11666 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
11668 /* If RTL is internal_arg_pointer, which has been optimized
11669 out, use DRAP instead. */
11670 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
11671 VAR_INIT_STATUS_INITIALIZED);
11673 break;
11675 case SIGN_EXTEND:
11676 case ZERO_EXTEND:
11677 if (GET_MODE_CLASS (mode) != MODE_INT)
11678 break;
11679 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
11680 mem_mode, VAR_INIT_STATUS_INITIALIZED);
11681 if (op0 == 0)
11682 break;
11683 else if (GET_CODE (rtl) == ZERO_EXTEND
11684 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11685 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
11686 < HOST_BITS_PER_WIDE_INT
11687 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
11688 to expand zero extend as two shifts instead of
11689 masking. */
11690 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
11692 enum machine_mode imode = GET_MODE (XEXP (rtl, 0));
11693 mem_loc_result = op0;
11694 add_loc_descr (&mem_loc_result,
11695 int_loc_descriptor (GET_MODE_MASK (imode)));
11696 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
11698 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11700 int shift = DWARF2_ADDR_SIZE
11701 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
11702 shift *= BITS_PER_UNIT;
11703 if (GET_CODE (rtl) == SIGN_EXTEND)
11704 op = DW_OP_shra;
11705 else
11706 op = DW_OP_shr;
11707 mem_loc_result = op0;
11708 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
11709 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
11710 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
11711 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11713 else if (!dwarf_strict)
11715 dw_die_ref type_die1, type_die2;
11716 dw_loc_descr_ref cvt;
11718 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
11719 GET_CODE (rtl) == ZERO_EXTEND);
11720 if (type_die1 == NULL)
11721 break;
11722 type_die2 = base_type_for_mode (mode, 1);
11723 if (type_die2 == NULL)
11724 break;
11725 mem_loc_result = op0;
11726 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11727 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11728 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
11729 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11730 add_loc_descr (&mem_loc_result, cvt);
11731 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11732 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11733 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
11734 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11735 add_loc_descr (&mem_loc_result, cvt);
11737 break;
11739 case MEM:
11741 rtx new_rtl = avoid_constant_pool_reference (rtl);
11742 if (new_rtl != rtl)
11744 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
11745 initialized);
11746 if (mem_loc_result != NULL)
11747 return mem_loc_result;
11750 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
11751 get_address_mode (rtl), mode,
11752 VAR_INIT_STATUS_INITIALIZED);
11753 if (mem_loc_result == NULL)
11754 mem_loc_result = tls_mem_loc_descriptor (rtl);
11755 if (mem_loc_result != NULL)
11757 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
11758 || GET_MODE_CLASS (mode) != MODE_INT)
11760 dw_die_ref type_die;
11761 dw_loc_descr_ref deref;
11763 if (dwarf_strict)
11764 return NULL;
11765 type_die
11766 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
11767 if (type_die == NULL)
11768 return NULL;
11769 deref = new_loc_descr (DW_OP_GNU_deref_type,
11770 GET_MODE_SIZE (mode), 0);
11771 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
11772 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
11773 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
11774 add_loc_descr (&mem_loc_result, deref);
11776 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11777 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
11778 else
11779 add_loc_descr (&mem_loc_result,
11780 new_loc_descr (DW_OP_deref_size,
11781 GET_MODE_SIZE (mode), 0));
11783 break;
11785 case LO_SUM:
11786 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
11788 case LABEL_REF:
11789 /* Some ports can transform a symbol ref into a label ref, because
11790 the symbol ref is too far away and has to be dumped into a constant
11791 pool. */
11792 case CONST:
11793 case SYMBOL_REF:
11794 if (GET_MODE_CLASS (mode) != MODE_INT
11795 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
11796 #ifdef POINTERS_EXTEND_UNSIGNED
11797 && (mode != Pmode || mem_mode == VOIDmode)
11798 #endif
11800 break;
11801 if (GET_CODE (rtl) == SYMBOL_REF
11802 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11804 dw_loc_descr_ref temp;
11806 /* If this is not defined, we have no way to emit the data. */
11807 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
11808 break;
11810 /* We used to emit DW_OP_addr here, but that's wrong, since
11811 DW_OP_addr should be relocated by the debug info consumer,
11812 while DW_OP_GNU_push_tls_address operand should not. */
11813 temp = new_loc_descr (DWARF2_ADDR_SIZE == 4
11814 ? DW_OP_const4u : DW_OP_const8u, 0, 0);
11815 temp->dw_loc_oprnd1.val_class = dw_val_class_addr;
11816 temp->dw_loc_oprnd1.v.val_addr = rtl;
11817 temp->dtprel = true;
11819 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
11820 add_loc_descr (&mem_loc_result, temp);
11822 break;
11825 if (!const_ok_for_output (rtl))
11826 break;
11828 symref:
11829 mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
11830 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
11831 mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
11832 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
11833 break;
11835 case CONCAT:
11836 case CONCATN:
11837 case VAR_LOCATION:
11838 case DEBUG_IMPLICIT_PTR:
11839 expansion_failed (NULL_TREE, rtl,
11840 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
11841 return 0;
11843 case ENTRY_VALUE:
11844 if (dwarf_strict)
11845 return NULL;
11846 if (REG_P (ENTRY_VALUE_EXP (rtl)))
11848 if (GET_MODE_CLASS (mode) != MODE_INT
11849 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11850 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
11851 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
11852 else
11854 = one_reg_loc_descriptor (dbx_reg_number (ENTRY_VALUE_EXP (rtl)),
11855 VAR_INIT_STATUS_INITIALIZED);
11857 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
11858 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
11860 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
11861 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
11862 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
11863 return NULL;
11865 else
11866 gcc_unreachable ();
11867 if (op0 == NULL)
11868 return NULL;
11869 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
11870 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
11871 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
11872 break;
11874 case DEBUG_PARAMETER_REF:
11875 mem_loc_result = parameter_ref_descriptor (rtl);
11876 break;
11878 case PRE_MODIFY:
11879 /* Extract the PLUS expression nested inside and fall into
11880 PLUS code below. */
11881 rtl = XEXP (rtl, 1);
11882 goto plus;
11884 case PRE_INC:
11885 case PRE_DEC:
11886 /* Turn these into a PLUS expression and fall into the PLUS code
11887 below. */
11888 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
11889 GEN_INT (GET_CODE (rtl) == PRE_INC
11890 ? GET_MODE_UNIT_SIZE (mem_mode)
11891 : -GET_MODE_UNIT_SIZE (mem_mode)));
11893 /* ... fall through ... */
11895 case PLUS:
11896 plus:
11897 if (is_based_loc (rtl)
11898 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11899 && GET_MODE_CLASS (mode) == MODE_INT)
11900 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
11901 INTVAL (XEXP (rtl, 1)),
11902 VAR_INIT_STATUS_INITIALIZED);
11903 else
11905 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11906 VAR_INIT_STATUS_INITIALIZED);
11907 if (mem_loc_result == 0)
11908 break;
11910 if (CONST_INT_P (XEXP (rtl, 1))
11911 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11912 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
11913 else
11915 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11916 VAR_INIT_STATUS_INITIALIZED);
11917 if (op1 == 0)
11918 break;
11919 add_loc_descr (&mem_loc_result, op1);
11920 add_loc_descr (&mem_loc_result,
11921 new_loc_descr (DW_OP_plus, 0, 0));
11924 break;
11926 /* If a pseudo-reg is optimized away, it is possible for it to
11927 be replaced with a MEM containing a multiply or shift. */
11928 case MINUS:
11929 op = DW_OP_minus;
11930 goto do_binop;
11932 case MULT:
11933 op = DW_OP_mul;
11934 goto do_binop;
11936 case DIV:
11937 if (!dwarf_strict
11938 && GET_MODE_CLASS (mode) == MODE_INT
11939 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11941 mem_loc_result = typed_binop (DW_OP_div, rtl,
11942 base_type_for_mode (mode, 0),
11943 mode, mem_mode);
11944 break;
11946 op = DW_OP_div;
11947 goto do_binop;
11949 case UMOD:
11950 op = DW_OP_mod;
11951 goto do_binop;
11953 case ASHIFT:
11954 op = DW_OP_shl;
11955 goto do_shift;
11957 case ASHIFTRT:
11958 op = DW_OP_shra;
11959 goto do_shift;
11961 case LSHIFTRT:
11962 op = DW_OP_shr;
11963 goto do_shift;
11965 do_shift:
11966 if (GET_MODE_CLASS (mode) != MODE_INT)
11967 break;
11968 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11969 VAR_INIT_STATUS_INITIALIZED);
11971 rtx rtlop1 = XEXP (rtl, 1);
11972 if (GET_MODE (rtlop1) != VOIDmode
11973 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
11974 < GET_MODE_BITSIZE (mode))
11975 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
11976 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
11977 VAR_INIT_STATUS_INITIALIZED);
11980 if (op0 == 0 || op1 == 0)
11981 break;
11983 mem_loc_result = op0;
11984 add_loc_descr (&mem_loc_result, op1);
11985 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11986 break;
11988 case AND:
11989 op = DW_OP_and;
11990 goto do_binop;
11992 case IOR:
11993 op = DW_OP_or;
11994 goto do_binop;
11996 case XOR:
11997 op = DW_OP_xor;
11998 goto do_binop;
12000 do_binop:
12001 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12002 VAR_INIT_STATUS_INITIALIZED);
12003 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12004 VAR_INIT_STATUS_INITIALIZED);
12006 if (op0 == 0 || op1 == 0)
12007 break;
12009 mem_loc_result = op0;
12010 add_loc_descr (&mem_loc_result, op1);
12011 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12012 break;
12014 case MOD:
12015 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
12017 mem_loc_result = typed_binop (DW_OP_mod, rtl,
12018 base_type_for_mode (mode, 0),
12019 mode, mem_mode);
12020 break;
12023 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12024 VAR_INIT_STATUS_INITIALIZED);
12025 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12026 VAR_INIT_STATUS_INITIALIZED);
12028 if (op0 == 0 || op1 == 0)
12029 break;
12031 mem_loc_result = op0;
12032 add_loc_descr (&mem_loc_result, op1);
12033 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12034 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12035 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
12036 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
12037 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
12038 break;
12040 case UDIV:
12041 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
12043 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
12045 op = DW_OP_div;
12046 goto do_binop;
12048 mem_loc_result = typed_binop (DW_OP_div, rtl,
12049 base_type_for_mode (mode, 1),
12050 mode, mem_mode);
12052 break;
12054 case NOT:
12055 op = DW_OP_not;
12056 goto do_unop;
12058 case ABS:
12059 op = DW_OP_abs;
12060 goto do_unop;
12062 case NEG:
12063 op = DW_OP_neg;
12064 goto do_unop;
12066 do_unop:
12067 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12068 VAR_INIT_STATUS_INITIALIZED);
12070 if (op0 == 0)
12071 break;
12073 mem_loc_result = op0;
12074 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12075 break;
12077 case CONST_INT:
12078 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12079 #ifdef POINTERS_EXTEND_UNSIGNED
12080 || (mode == Pmode
12081 && mem_mode != VOIDmode
12082 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
12083 #endif
12086 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12087 break;
12089 if (!dwarf_strict
12090 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
12091 || GET_MODE_BITSIZE (mode) == 2 * HOST_BITS_PER_WIDE_INT))
12093 dw_die_ref type_die = base_type_for_mode (mode, 1);
12094 enum machine_mode amode;
12095 if (type_die == NULL)
12096 return NULL;
12097 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
12098 MODE_INT, 0);
12099 if (INTVAL (rtl) >= 0
12100 && amode != BLKmode
12101 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
12102 /* const DW_OP_GNU_convert <XXX> vs.
12103 DW_OP_GNU_const_type <XXX, 1, const>. */
12104 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
12105 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
12107 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12108 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12109 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12110 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12111 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
12112 add_loc_descr (&mem_loc_result, op0);
12113 return mem_loc_result;
12115 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
12116 INTVAL (rtl));
12117 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12118 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12119 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12120 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12121 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12122 else
12124 mem_loc_result->dw_loc_oprnd2.val_class
12125 = dw_val_class_const_double;
12126 mem_loc_result->dw_loc_oprnd2.v.val_double
12127 = shwi_to_double_int (INTVAL (rtl));
12130 break;
12132 case CONST_DOUBLE:
12133 if (!dwarf_strict)
12135 dw_die_ref type_die;
12137 /* Note that a CONST_DOUBLE rtx could represent either an integer
12138 or a floating-point constant. A CONST_DOUBLE is used whenever
12139 the constant requires more than one word in order to be
12140 adequately represented. We output CONST_DOUBLEs as blocks. */
12141 if (mode == VOIDmode
12142 || (GET_MODE (rtl) == VOIDmode
12143 && GET_MODE_BITSIZE (mode) != 2 * HOST_BITS_PER_WIDE_INT))
12144 break;
12145 type_die = base_type_for_mode (mode,
12146 GET_MODE_CLASS (mode) == MODE_INT);
12147 if (type_die == NULL)
12148 return NULL;
12149 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
12150 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12151 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12152 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12153 if (SCALAR_FLOAT_MODE_P (mode))
12155 unsigned int length = GET_MODE_SIZE (mode);
12156 unsigned char *array
12157 = (unsigned char*) ggc_alloc_atomic (length);
12159 insert_float (rtl, array);
12160 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12161 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12162 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12163 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12165 else
12167 mem_loc_result->dw_loc_oprnd2.val_class
12168 = dw_val_class_const_double;
12169 mem_loc_result->dw_loc_oprnd2.v.val_double
12170 = rtx_to_double_int (rtl);
12173 break;
12175 case EQ:
12176 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
12177 break;
12179 case GE:
12180 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12181 break;
12183 case GT:
12184 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12185 break;
12187 case LE:
12188 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12189 break;
12191 case LT:
12192 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12193 break;
12195 case NE:
12196 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
12197 break;
12199 case GEU:
12200 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12201 break;
12203 case GTU:
12204 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12205 break;
12207 case LEU:
12208 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12209 break;
12211 case LTU:
12212 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12213 break;
12215 case UMIN:
12216 case UMAX:
12217 if (GET_MODE_CLASS (mode) != MODE_INT)
12218 break;
12219 /* FALLTHRU */
12220 case SMIN:
12221 case SMAX:
12222 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
12223 break;
12225 case ZERO_EXTRACT:
12226 case SIGN_EXTRACT:
12227 if (CONST_INT_P (XEXP (rtl, 1))
12228 && CONST_INT_P (XEXP (rtl, 2))
12229 && ((unsigned) INTVAL (XEXP (rtl, 1))
12230 + (unsigned) INTVAL (XEXP (rtl, 2))
12231 <= GET_MODE_BITSIZE (mode))
12232 && GET_MODE_CLASS (mode) == MODE_INT
12233 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12234 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
12236 int shift, size;
12237 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12238 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12239 if (op0 == 0)
12240 break;
12241 if (GET_CODE (rtl) == SIGN_EXTRACT)
12242 op = DW_OP_shra;
12243 else
12244 op = DW_OP_shr;
12245 mem_loc_result = op0;
12246 size = INTVAL (XEXP (rtl, 1));
12247 shift = INTVAL (XEXP (rtl, 2));
12248 if (BITS_BIG_ENDIAN)
12249 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12250 - shift - size;
12251 if (shift + size != (int) DWARF2_ADDR_SIZE)
12253 add_loc_descr (&mem_loc_result,
12254 int_loc_descriptor (DWARF2_ADDR_SIZE
12255 - shift - size));
12256 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12258 if (size != (int) DWARF2_ADDR_SIZE)
12260 add_loc_descr (&mem_loc_result,
12261 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
12262 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12265 break;
12267 case IF_THEN_ELSE:
12269 dw_loc_descr_ref op2, bra_node, drop_node;
12270 op0 = mem_loc_descriptor (XEXP (rtl, 0),
12271 GET_MODE (XEXP (rtl, 0)) == VOIDmode
12272 ? word_mode : GET_MODE (XEXP (rtl, 0)),
12273 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12274 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12275 VAR_INIT_STATUS_INITIALIZED);
12276 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
12277 VAR_INIT_STATUS_INITIALIZED);
12278 if (op0 == NULL || op1 == NULL || op2 == NULL)
12279 break;
12281 mem_loc_result = op1;
12282 add_loc_descr (&mem_loc_result, op2);
12283 add_loc_descr (&mem_loc_result, op0);
12284 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12285 add_loc_descr (&mem_loc_result, bra_node);
12286 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
12287 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12288 add_loc_descr (&mem_loc_result, drop_node);
12289 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12290 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12292 break;
12294 case FLOAT_EXTEND:
12295 case FLOAT_TRUNCATE:
12296 case FLOAT:
12297 case UNSIGNED_FLOAT:
12298 case FIX:
12299 case UNSIGNED_FIX:
12300 if (!dwarf_strict)
12302 dw_die_ref type_die;
12303 dw_loc_descr_ref cvt;
12305 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12306 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12307 if (op0 == NULL)
12308 break;
12309 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
12310 && (GET_CODE (rtl) == FLOAT
12311 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
12312 <= DWARF2_ADDR_SIZE))
12314 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12315 GET_CODE (rtl) == UNSIGNED_FLOAT);
12316 if (type_die == NULL)
12317 break;
12318 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12319 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12320 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12321 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12322 add_loc_descr (&op0, cvt);
12324 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
12325 if (type_die == NULL)
12326 break;
12327 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12328 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12329 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12330 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12331 add_loc_descr (&op0, cvt);
12332 if (GET_MODE_CLASS (mode) == MODE_INT
12333 && (GET_CODE (rtl) == FIX
12334 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
12336 op0 = convert_descriptor_to_mode (mode, op0);
12337 if (op0 == NULL)
12338 break;
12340 mem_loc_result = op0;
12342 break;
12344 case CLZ:
12345 case CTZ:
12346 case FFS:
12347 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
12348 break;
12350 case POPCOUNT:
12351 case PARITY:
12352 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
12353 break;
12355 case BSWAP:
12356 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
12357 break;
12359 case ROTATE:
12360 case ROTATERT:
12361 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
12362 break;
12364 case COMPARE:
12365 case TRUNCATE:
12366 /* In theory, we could implement the above. */
12367 /* DWARF cannot represent the unsigned compare operations
12368 natively. */
12369 case SS_MULT:
12370 case US_MULT:
12371 case SS_DIV:
12372 case US_DIV:
12373 case SS_PLUS:
12374 case US_PLUS:
12375 case SS_MINUS:
12376 case US_MINUS:
12377 case SS_NEG:
12378 case US_NEG:
12379 case SS_ABS:
12380 case SS_ASHIFT:
12381 case US_ASHIFT:
12382 case SS_TRUNCATE:
12383 case US_TRUNCATE:
12384 case UNORDERED:
12385 case ORDERED:
12386 case UNEQ:
12387 case UNGE:
12388 case UNGT:
12389 case UNLE:
12390 case UNLT:
12391 case LTGT:
12392 case FRACT_CONVERT:
12393 case UNSIGNED_FRACT_CONVERT:
12394 case SAT_FRACT:
12395 case UNSIGNED_SAT_FRACT:
12396 case SQRT:
12397 case ASM_OPERANDS:
12398 case VEC_MERGE:
12399 case VEC_SELECT:
12400 case VEC_CONCAT:
12401 case VEC_DUPLICATE:
12402 case UNSPEC:
12403 case HIGH:
12404 case FMA:
12405 case STRICT_LOW_PART:
12406 case CONST_VECTOR:
12407 case CONST_FIXED:
12408 case CLRSB:
12409 /* If delegitimize_address couldn't do anything with the UNSPEC, we
12410 can't express it in the debug info. This can happen e.g. with some
12411 TLS UNSPECs. */
12412 break;
12414 case CONST_STRING:
12415 resolve_one_addr (&rtl, NULL);
12416 goto symref;
12418 default:
12419 #ifdef ENABLE_CHECKING
12420 print_rtl (stderr, rtl);
12421 gcc_unreachable ();
12422 #else
12423 break;
12424 #endif
12427 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12428 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12430 return mem_loc_result;
12433 /* Return a descriptor that describes the concatenation of two locations.
12434 This is typically a complex variable. */
12436 static dw_loc_descr_ref
12437 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
12439 dw_loc_descr_ref cc_loc_result = NULL;
12440 dw_loc_descr_ref x0_ref
12441 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12442 dw_loc_descr_ref x1_ref
12443 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12445 if (x0_ref == 0 || x1_ref == 0)
12446 return 0;
12448 cc_loc_result = x0_ref;
12449 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
12451 add_loc_descr (&cc_loc_result, x1_ref);
12452 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
12454 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
12455 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12457 return cc_loc_result;
12460 /* Return a descriptor that describes the concatenation of N
12461 locations. */
12463 static dw_loc_descr_ref
12464 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
12466 unsigned int i;
12467 dw_loc_descr_ref cc_loc_result = NULL;
12468 unsigned int n = XVECLEN (concatn, 0);
12470 for (i = 0; i < n; ++i)
12472 dw_loc_descr_ref ref;
12473 rtx x = XVECEXP (concatn, 0, i);
12475 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12476 if (ref == NULL)
12477 return NULL;
12479 add_loc_descr (&cc_loc_result, ref);
12480 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
12483 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12484 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12486 return cc_loc_result;
12489 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
12490 for DEBUG_IMPLICIT_PTR RTL. */
12492 static dw_loc_descr_ref
12493 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
12495 dw_loc_descr_ref ret;
12496 dw_die_ref ref;
12498 if (dwarf_strict)
12499 return NULL;
12500 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
12501 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
12502 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
12503 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
12504 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
12505 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
12506 if (ref)
12508 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12509 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12510 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12512 else
12514 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12515 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
12517 return ret;
12520 /* Output a proper Dwarf location descriptor for a variable or parameter
12521 which is either allocated in a register or in a memory location. For a
12522 register, we just generate an OP_REG and the register number. For a
12523 memory location we provide a Dwarf postfix expression describing how to
12524 generate the (dynamic) address of the object onto the address stack.
12526 MODE is mode of the decl if this loc_descriptor is going to be used in
12527 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
12528 allowed, VOIDmode otherwise.
12530 If we don't know how to describe it, return 0. */
12532 static dw_loc_descr_ref
12533 loc_descriptor (rtx rtl, enum machine_mode mode,
12534 enum var_init_status initialized)
12536 dw_loc_descr_ref loc_result = NULL;
12538 switch (GET_CODE (rtl))
12540 case SUBREG:
12541 /* The case of a subreg may arise when we have a local (register)
12542 variable or a formal (register) parameter which doesn't quite fill
12543 up an entire register. For now, just assume that it is
12544 legitimate to make the Dwarf info refer to the whole register which
12545 contains the given subreg. */
12546 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
12547 loc_result = loc_descriptor (SUBREG_REG (rtl), mode, initialized);
12548 else
12549 goto do_default;
12550 break;
12552 case REG:
12553 loc_result = reg_loc_descriptor (rtl, initialized);
12554 break;
12556 case MEM:
12557 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
12558 GET_MODE (rtl), initialized);
12559 if (loc_result == NULL)
12560 loc_result = tls_mem_loc_descriptor (rtl);
12561 if (loc_result == NULL)
12563 rtx new_rtl = avoid_constant_pool_reference (rtl);
12564 if (new_rtl != rtl)
12565 loc_result = loc_descriptor (new_rtl, mode, initialized);
12567 break;
12569 case CONCAT:
12570 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
12571 initialized);
12572 break;
12574 case CONCATN:
12575 loc_result = concatn_loc_descriptor (rtl, initialized);
12576 break;
12578 case VAR_LOCATION:
12579 /* Single part. */
12580 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
12582 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
12583 if (GET_CODE (loc) == EXPR_LIST)
12584 loc = XEXP (loc, 0);
12585 loc_result = loc_descriptor (loc, mode, initialized);
12586 break;
12589 rtl = XEXP (rtl, 1);
12590 /* FALLTHRU */
12592 case PARALLEL:
12594 rtvec par_elems = XVEC (rtl, 0);
12595 int num_elem = GET_NUM_ELEM (par_elems);
12596 enum machine_mode mode;
12597 int i;
12599 /* Create the first one, so we have something to add to. */
12600 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
12601 VOIDmode, initialized);
12602 if (loc_result == NULL)
12603 return NULL;
12604 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
12605 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
12606 for (i = 1; i < num_elem; i++)
12608 dw_loc_descr_ref temp;
12610 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
12611 VOIDmode, initialized);
12612 if (temp == NULL)
12613 return NULL;
12614 add_loc_descr (&loc_result, temp);
12615 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
12616 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
12619 break;
12621 case CONST_INT:
12622 if (mode != VOIDmode && mode != BLKmode)
12623 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
12624 INTVAL (rtl));
12625 break;
12627 case CONST_DOUBLE:
12628 if (mode == VOIDmode)
12629 mode = GET_MODE (rtl);
12631 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
12633 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
12635 /* Note that a CONST_DOUBLE rtx could represent either an integer
12636 or a floating-point constant. A CONST_DOUBLE is used whenever
12637 the constant requires more than one word in order to be
12638 adequately represented. We output CONST_DOUBLEs as blocks. */
12639 loc_result = new_loc_descr (DW_OP_implicit_value,
12640 GET_MODE_SIZE (mode), 0);
12641 if (SCALAR_FLOAT_MODE_P (mode))
12643 unsigned int length = GET_MODE_SIZE (mode);
12644 unsigned char *array
12645 = (unsigned char*) ggc_alloc_atomic (length);
12647 insert_float (rtl, array);
12648 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12649 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12650 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12651 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12653 else
12655 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
12656 loc_result->dw_loc_oprnd2.v.val_double
12657 = rtx_to_double_int (rtl);
12660 break;
12662 case CONST_VECTOR:
12663 if (mode == VOIDmode)
12664 mode = GET_MODE (rtl);
12666 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
12668 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
12669 unsigned int length = CONST_VECTOR_NUNITS (rtl);
12670 unsigned char *array = (unsigned char *)
12671 ggc_alloc_atomic (length * elt_size);
12672 unsigned int i;
12673 unsigned char *p;
12675 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
12676 switch (GET_MODE_CLASS (mode))
12678 case MODE_VECTOR_INT:
12679 for (i = 0, p = array; i < length; i++, p += elt_size)
12681 rtx elt = CONST_VECTOR_ELT (rtl, i);
12682 double_int val = rtx_to_double_int (elt);
12684 if (elt_size <= sizeof (HOST_WIDE_INT))
12685 insert_int (double_int_to_shwi (val), elt_size, p);
12686 else
12688 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
12689 insert_double (val, p);
12692 break;
12694 case MODE_VECTOR_FLOAT:
12695 for (i = 0, p = array; i < length; i++, p += elt_size)
12697 rtx elt = CONST_VECTOR_ELT (rtl, i);
12698 insert_float (elt, p);
12700 break;
12702 default:
12703 gcc_unreachable ();
12706 loc_result = new_loc_descr (DW_OP_implicit_value,
12707 length * elt_size, 0);
12708 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12709 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
12710 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
12711 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12713 break;
12715 case CONST:
12716 if (mode == VOIDmode
12717 || GET_CODE (XEXP (rtl, 0)) == CONST_INT
12718 || GET_CODE (XEXP (rtl, 0)) == CONST_DOUBLE
12719 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
12721 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
12722 break;
12724 /* FALLTHROUGH */
12725 case SYMBOL_REF:
12726 if (!const_ok_for_output (rtl))
12727 break;
12728 case LABEL_REF:
12729 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
12730 && (dwarf_version >= 4 || !dwarf_strict))
12732 loc_result = new_loc_descr (DW_OP_addr, 0, 0);
12733 loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
12734 loc_result->dw_loc_oprnd1.v.val_addr = rtl;
12735 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
12736 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
12738 break;
12740 case DEBUG_IMPLICIT_PTR:
12741 loc_result = implicit_ptr_descriptor (rtl, 0);
12742 break;
12744 case PLUS:
12745 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
12746 && CONST_INT_P (XEXP (rtl, 1)))
12748 loc_result
12749 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
12750 break;
12752 /* FALLTHRU */
12753 do_default:
12754 default:
12755 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
12756 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
12757 && dwarf_version >= 4)
12758 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
12760 /* Value expression. */
12761 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
12762 if (loc_result)
12763 add_loc_descr (&loc_result,
12764 new_loc_descr (DW_OP_stack_value, 0, 0));
12766 break;
12769 return loc_result;
12772 /* We need to figure out what section we should use as the base for the
12773 address ranges where a given location is valid.
12774 1. If this particular DECL has a section associated with it, use that.
12775 2. If this function has a section associated with it, use that.
12776 3. Otherwise, use the text section.
12777 XXX: If you split a variable across multiple sections, we won't notice. */
12779 static const char *
12780 secname_for_decl (const_tree decl)
12782 const char *secname;
12784 if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
12786 tree sectree = DECL_SECTION_NAME (decl);
12787 secname = TREE_STRING_POINTER (sectree);
12789 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
12791 tree sectree = DECL_SECTION_NAME (current_function_decl);
12792 secname = TREE_STRING_POINTER (sectree);
12794 else if (cfun && in_cold_section_p)
12795 secname = crtl->subsections.cold_section_label;
12796 else
12797 secname = text_section_label;
12799 return secname;
12802 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
12804 static bool
12805 decl_by_reference_p (tree decl)
12807 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
12808 || TREE_CODE (decl) == VAR_DECL)
12809 && DECL_BY_REFERENCE (decl));
12812 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
12813 for VARLOC. */
12815 static dw_loc_descr_ref
12816 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
12817 enum var_init_status initialized)
12819 int have_address = 0;
12820 dw_loc_descr_ref descr;
12821 enum machine_mode mode;
12823 if (want_address != 2)
12825 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
12826 /* Single part. */
12827 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
12829 varloc = PAT_VAR_LOCATION_LOC (varloc);
12830 if (GET_CODE (varloc) == EXPR_LIST)
12831 varloc = XEXP (varloc, 0);
12832 mode = GET_MODE (varloc);
12833 if (MEM_P (varloc))
12835 rtx addr = XEXP (varloc, 0);
12836 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
12837 mode, initialized);
12838 if (descr)
12839 have_address = 1;
12840 else
12842 rtx x = avoid_constant_pool_reference (varloc);
12843 if (x != varloc)
12844 descr = mem_loc_descriptor (x, mode, VOIDmode,
12845 initialized);
12848 else
12849 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
12851 else
12852 return 0;
12854 else
12856 if (GET_CODE (varloc) == VAR_LOCATION)
12857 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
12858 else
12859 mode = DECL_MODE (loc);
12860 descr = loc_descriptor (varloc, mode, initialized);
12861 have_address = 1;
12864 if (!descr)
12865 return 0;
12867 if (want_address == 2 && !have_address
12868 && (dwarf_version >= 4 || !dwarf_strict))
12870 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
12872 expansion_failed (loc, NULL_RTX,
12873 "DWARF address size mismatch");
12874 return 0;
12876 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
12877 have_address = 1;
12879 /* Show if we can't fill the request for an address. */
12880 if (want_address && !have_address)
12882 expansion_failed (loc, NULL_RTX,
12883 "Want address and only have value");
12884 return 0;
12887 /* If we've got an address and don't want one, dereference. */
12888 if (!want_address && have_address)
12890 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
12891 enum dwarf_location_atom op;
12893 if (size > DWARF2_ADDR_SIZE || size == -1)
12895 expansion_failed (loc, NULL_RTX,
12896 "DWARF address size mismatch");
12897 return 0;
12899 else if (size == DWARF2_ADDR_SIZE)
12900 op = DW_OP_deref;
12901 else
12902 op = DW_OP_deref_size;
12904 add_loc_descr (&descr, new_loc_descr (op, size, 0));
12907 return descr;
12910 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
12911 if it is not possible. */
12913 static dw_loc_descr_ref
12914 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
12916 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
12917 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
12918 else if (dwarf_version >= 3 || !dwarf_strict)
12919 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
12920 else
12921 return NULL;
12924 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
12925 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
12927 static dw_loc_descr_ref
12928 dw_sra_loc_expr (tree decl, rtx loc)
12930 rtx p;
12931 unsigned int padsize = 0;
12932 dw_loc_descr_ref descr, *descr_tail;
12933 unsigned HOST_WIDE_INT decl_size;
12934 rtx varloc;
12935 enum var_init_status initialized;
12937 if (DECL_SIZE (decl) == NULL
12938 || !host_integerp (DECL_SIZE (decl), 1))
12939 return NULL;
12941 decl_size = tree_low_cst (DECL_SIZE (decl), 1);
12942 descr = NULL;
12943 descr_tail = &descr;
12945 for (p = loc; p; p = XEXP (p, 1))
12947 unsigned int bitsize = decl_piece_bitsize (p);
12948 rtx loc_note = *decl_piece_varloc_ptr (p);
12949 dw_loc_descr_ref cur_descr;
12950 dw_loc_descr_ref *tail, last = NULL;
12951 unsigned int opsize = 0;
12953 if (loc_note == NULL_RTX
12954 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
12956 padsize += bitsize;
12957 continue;
12959 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
12960 varloc = NOTE_VAR_LOCATION (loc_note);
12961 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
12962 if (cur_descr == NULL)
12964 padsize += bitsize;
12965 continue;
12968 /* Check that cur_descr either doesn't use
12969 DW_OP_*piece operations, or their sum is equal
12970 to bitsize. Otherwise we can't embed it. */
12971 for (tail = &cur_descr; *tail != NULL;
12972 tail = &(*tail)->dw_loc_next)
12973 if ((*tail)->dw_loc_opc == DW_OP_piece)
12975 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
12976 * BITS_PER_UNIT;
12977 last = *tail;
12979 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
12981 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
12982 last = *tail;
12985 if (last != NULL && opsize != bitsize)
12987 padsize += bitsize;
12988 continue;
12991 /* If there is a hole, add DW_OP_*piece after empty DWARF
12992 expression, which means that those bits are optimized out. */
12993 if (padsize)
12995 if (padsize > decl_size)
12996 return NULL;
12997 decl_size -= padsize;
12998 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
12999 if (*descr_tail == NULL)
13000 return NULL;
13001 descr_tail = &(*descr_tail)->dw_loc_next;
13002 padsize = 0;
13004 *descr_tail = cur_descr;
13005 descr_tail = tail;
13006 if (bitsize > decl_size)
13007 return NULL;
13008 decl_size -= bitsize;
13009 if (last == NULL)
13011 HOST_WIDE_INT offset = 0;
13012 if (GET_CODE (varloc) == VAR_LOCATION
13013 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13015 varloc = PAT_VAR_LOCATION_LOC (varloc);
13016 if (GET_CODE (varloc) == EXPR_LIST)
13017 varloc = XEXP (varloc, 0);
13021 if (GET_CODE (varloc) == CONST
13022 || GET_CODE (varloc) == SIGN_EXTEND
13023 || GET_CODE (varloc) == ZERO_EXTEND)
13024 varloc = XEXP (varloc, 0);
13025 else if (GET_CODE (varloc) == SUBREG)
13026 varloc = SUBREG_REG (varloc);
13027 else
13028 break;
13030 while (1);
13031 /* DW_OP_bit_size offset should be zero for register
13032 or implicit location descriptions and empty location
13033 descriptions, but for memory addresses needs big endian
13034 adjustment. */
13035 if (MEM_P (varloc))
13037 unsigned HOST_WIDE_INT memsize
13038 = MEM_SIZE (varloc) * BITS_PER_UNIT;
13039 if (memsize != bitsize)
13041 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
13042 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
13043 return NULL;
13044 if (memsize < bitsize)
13045 return NULL;
13046 if (BITS_BIG_ENDIAN)
13047 offset = memsize - bitsize;
13051 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
13052 if (*descr_tail == NULL)
13053 return NULL;
13054 descr_tail = &(*descr_tail)->dw_loc_next;
13058 /* If there were any non-empty expressions, add padding till the end of
13059 the decl. */
13060 if (descr != NULL && decl_size != 0)
13062 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
13063 if (*descr_tail == NULL)
13064 return NULL;
13066 return descr;
13069 /* Return the dwarf representation of the location list LOC_LIST of
13070 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
13071 function. */
13073 static dw_loc_list_ref
13074 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
13076 const char *endname, *secname;
13077 rtx varloc;
13078 enum var_init_status initialized;
13079 struct var_loc_node *node;
13080 dw_loc_descr_ref descr;
13081 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13082 dw_loc_list_ref list = NULL;
13083 dw_loc_list_ref *listp = &list;
13085 /* Now that we know what section we are using for a base,
13086 actually construct the list of locations.
13087 The first location information is what is passed to the
13088 function that creates the location list, and the remaining
13089 locations just get added on to that list.
13090 Note that we only know the start address for a location
13091 (IE location changes), so to build the range, we use
13092 the range [current location start, next location start].
13093 This means we have to special case the last node, and generate
13094 a range of [last location start, end of function label]. */
13096 secname = secname_for_decl (decl);
13098 for (node = loc_list->first; node; node = node->next)
13099 if (GET_CODE (node->loc) == EXPR_LIST
13100 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
13102 if (GET_CODE (node->loc) == EXPR_LIST)
13104 /* This requires DW_OP_{,bit_}piece, which is not usable
13105 inside DWARF expressions. */
13106 if (want_address != 2)
13107 continue;
13108 descr = dw_sra_loc_expr (decl, node->loc);
13109 if (descr == NULL)
13110 continue;
13112 else
13114 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13115 varloc = NOTE_VAR_LOCATION (node->loc);
13116 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
13118 if (descr)
13120 bool range_across_switch = false;
13121 /* If section switch happens in between node->label
13122 and node->next->label (or end of function) and
13123 we can't emit it as a single entry list,
13124 emit two ranges, first one ending at the end
13125 of first partition and second one starting at the
13126 beginning of second partition. */
13127 if (node == loc_list->last_before_switch
13128 && (node != loc_list->first || loc_list->first->next)
13129 && current_function_decl)
13131 endname = cfun->fde->dw_fde_end;
13132 range_across_switch = true;
13134 /* The variable has a location between NODE->LABEL and
13135 NODE->NEXT->LABEL. */
13136 else if (node->next)
13137 endname = node->next->label;
13138 /* If the variable has a location at the last label
13139 it keeps its location until the end of function. */
13140 else if (!current_function_decl)
13141 endname = text_end_label;
13142 else
13144 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13145 current_function_funcdef_no);
13146 endname = ggc_strdup (label_id);
13149 *listp = new_loc_list (descr, node->label, endname, secname);
13150 if (TREE_CODE (decl) == PARM_DECL
13151 && node == loc_list->first
13152 && GET_CODE (node->loc) == NOTE
13153 && strcmp (node->label, endname) == 0)
13154 (*listp)->force = true;
13155 listp = &(*listp)->dw_loc_next;
13157 if (range_across_switch)
13159 if (GET_CODE (node->loc) == EXPR_LIST)
13160 descr = dw_sra_loc_expr (decl, node->loc);
13161 else
13163 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13164 varloc = NOTE_VAR_LOCATION (node->loc);
13165 descr = dw_loc_list_1 (decl, varloc, want_address,
13166 initialized);
13168 gcc_assert (descr);
13169 /* The variable has a location between NODE->LABEL and
13170 NODE->NEXT->LABEL. */
13171 if (node->next)
13172 endname = node->next->label;
13173 else
13174 endname = cfun->fde->dw_fde_second_end;
13175 *listp = new_loc_list (descr,
13176 cfun->fde->dw_fde_second_begin,
13177 endname, secname);
13178 listp = &(*listp)->dw_loc_next;
13183 /* Try to avoid the overhead of a location list emitting a location
13184 expression instead, but only if we didn't have more than one
13185 location entry in the first place. If some entries were not
13186 representable, we don't want to pretend a single entry that was
13187 applies to the entire scope in which the variable is
13188 available. */
13189 if (list && loc_list->first->next)
13190 gen_llsym (list);
13192 return list;
13195 /* Return if the loc_list has only single element and thus can be represented
13196 as location description. */
13198 static bool
13199 single_element_loc_list_p (dw_loc_list_ref list)
13201 gcc_assert (!list->dw_loc_next || list->ll_symbol);
13202 return !list->ll_symbol;
13205 /* To each location in list LIST add loc descr REF. */
13207 static void
13208 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
13210 dw_loc_descr_ref copy;
13211 add_loc_descr (&list->expr, ref);
13212 list = list->dw_loc_next;
13213 while (list)
13215 copy = ggc_alloc_dw_loc_descr_node ();
13216 memcpy (copy, ref, sizeof (dw_loc_descr_node));
13217 add_loc_descr (&list->expr, copy);
13218 while (copy->dw_loc_next)
13220 dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
13221 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
13222 copy->dw_loc_next = new_copy;
13223 copy = new_copy;
13225 list = list->dw_loc_next;
13229 /* Given two lists RET and LIST
13230 produce location list that is result of adding expression in LIST
13231 to expression in RET on each possition in program.
13232 Might be destructive on both RET and LIST.
13234 TODO: We handle only simple cases of RET or LIST having at most one
13235 element. General case would inolve sorting the lists in program order
13236 and merging them that will need some additional work.
13237 Adding that will improve quality of debug info especially for SRA-ed
13238 structures. */
13240 static void
13241 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
13243 if (!list)
13244 return;
13245 if (!*ret)
13247 *ret = list;
13248 return;
13250 if (!list->dw_loc_next)
13252 add_loc_descr_to_each (*ret, list->expr);
13253 return;
13255 if (!(*ret)->dw_loc_next)
13257 add_loc_descr_to_each (list, (*ret)->expr);
13258 *ret = list;
13259 return;
13261 expansion_failed (NULL_TREE, NULL_RTX,
13262 "Don't know how to merge two non-trivial"
13263 " location lists.\n");
13264 *ret = NULL;
13265 return;
13268 /* LOC is constant expression. Try a luck, look it up in constant
13269 pool and return its loc_descr of its address. */
13271 static dw_loc_descr_ref
13272 cst_pool_loc_descr (tree loc)
13274 /* Get an RTL for this, if something has been emitted. */
13275 rtx rtl = lookup_constant_def (loc);
13277 if (!rtl || !MEM_P (rtl))
13279 gcc_assert (!rtl);
13280 return 0;
13282 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
13284 /* TODO: We might get more coverage if we was actually delaying expansion
13285 of all expressions till end of compilation when constant pools are fully
13286 populated. */
13287 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
13289 expansion_failed (loc, NULL_RTX,
13290 "CST value in contant pool but not marked.");
13291 return 0;
13293 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13294 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
13297 /* Return dw_loc_list representing address of addr_expr LOC
13298 by looking for innder INDIRECT_REF expression and turing it
13299 into simple arithmetics. */
13301 static dw_loc_list_ref
13302 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
13304 tree obj, offset;
13305 HOST_WIDE_INT bitsize, bitpos, bytepos;
13306 enum machine_mode mode;
13307 int volatilep;
13308 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
13309 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13311 obj = get_inner_reference (TREE_OPERAND (loc, 0),
13312 &bitsize, &bitpos, &offset, &mode,
13313 &unsignedp, &volatilep, false);
13314 STRIP_NOPS (obj);
13315 if (bitpos % BITS_PER_UNIT)
13317 expansion_failed (loc, NULL_RTX, "bitfield access");
13318 return 0;
13320 if (!INDIRECT_REF_P (obj))
13322 expansion_failed (obj,
13323 NULL_RTX, "no indirect ref in inner refrence");
13324 return 0;
13326 if (!offset && !bitpos)
13327 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
13328 else if (toplev
13329 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
13330 && (dwarf_version >= 4 || !dwarf_strict))
13332 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
13333 if (!list_ret)
13334 return 0;
13335 if (offset)
13337 /* Variable offset. */
13338 list_ret1 = loc_list_from_tree (offset, 0);
13339 if (list_ret1 == 0)
13340 return 0;
13341 add_loc_list (&list_ret, list_ret1);
13342 if (!list_ret)
13343 return 0;
13344 add_loc_descr_to_each (list_ret,
13345 new_loc_descr (DW_OP_plus, 0, 0));
13347 bytepos = bitpos / BITS_PER_UNIT;
13348 if (bytepos > 0)
13349 add_loc_descr_to_each (list_ret,
13350 new_loc_descr (DW_OP_plus_uconst,
13351 bytepos, 0));
13352 else if (bytepos < 0)
13353 loc_list_plus_const (list_ret, bytepos);
13354 add_loc_descr_to_each (list_ret,
13355 new_loc_descr (DW_OP_stack_value, 0, 0));
13357 return list_ret;
13361 /* Generate Dwarf location list representing LOC.
13362 If WANT_ADDRESS is false, expression computing LOC will be computed
13363 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
13364 if WANT_ADDRESS is 2, expression computing address useable in location
13365 will be returned (i.e. DW_OP_reg can be used
13366 to refer to register values). */
13368 static dw_loc_list_ref
13369 loc_list_from_tree (tree loc, int want_address)
13371 dw_loc_descr_ref ret = NULL, ret1 = NULL;
13372 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13373 int have_address = 0;
13374 enum dwarf_location_atom op;
13376 /* ??? Most of the time we do not take proper care for sign/zero
13377 extending the values properly. Hopefully this won't be a real
13378 problem... */
13380 switch (TREE_CODE (loc))
13382 case ERROR_MARK:
13383 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
13384 return 0;
13386 case PLACEHOLDER_EXPR:
13387 /* This case involves extracting fields from an object to determine the
13388 position of other fields. We don't try to encode this here. The
13389 only user of this is Ada, which encodes the needed information using
13390 the names of types. */
13391 expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
13392 return 0;
13394 case CALL_EXPR:
13395 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
13396 /* There are no opcodes for these operations. */
13397 return 0;
13399 case PREINCREMENT_EXPR:
13400 case PREDECREMENT_EXPR:
13401 case POSTINCREMENT_EXPR:
13402 case POSTDECREMENT_EXPR:
13403 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
13404 /* There are no opcodes for these operations. */
13405 return 0;
13407 case ADDR_EXPR:
13408 /* If we already want an address, see if there is INDIRECT_REF inside
13409 e.g. for &this->field. */
13410 if (want_address)
13412 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
13413 (loc, want_address == 2);
13414 if (list_ret)
13415 have_address = 1;
13416 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
13417 && (ret = cst_pool_loc_descr (loc)))
13418 have_address = 1;
13420 /* Otherwise, process the argument and look for the address. */
13421 if (!list_ret && !ret)
13422 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
13423 else
13425 if (want_address)
13426 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
13427 return NULL;
13429 break;
13431 case VAR_DECL:
13432 if (DECL_THREAD_LOCAL_P (loc))
13434 rtx rtl;
13435 enum dwarf_location_atom first_op;
13436 enum dwarf_location_atom second_op;
13437 bool dtprel = false;
13439 if (targetm.have_tls)
13441 /* If this is not defined, we have no way to emit the
13442 data. */
13443 if (!targetm.asm_out.output_dwarf_dtprel)
13444 return 0;
13446 /* The way DW_OP_GNU_push_tls_address is specified, we
13447 can only look up addresses of objects in the current
13448 module. We used DW_OP_addr as first op, but that's
13449 wrong, because DW_OP_addr is relocated by the debug
13450 info consumer, while DW_OP_GNU_push_tls_address
13451 operand shouldn't be. */
13452 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
13453 return 0;
13454 first_op = DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u;
13455 dtprel = true;
13456 second_op = DW_OP_GNU_push_tls_address;
13458 else
13460 if (!targetm.emutls.debug_form_tls_address
13461 || !(dwarf_version >= 3 || !dwarf_strict))
13462 return 0;
13463 /* We stuffed the control variable into the DECL_VALUE_EXPR
13464 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
13465 no longer appear in gimple code. We used the control
13466 variable in specific so that we could pick it up here. */
13467 loc = DECL_VALUE_EXPR (loc);
13468 first_op = DW_OP_addr;
13469 second_op = DW_OP_form_tls_address;
13472 rtl = rtl_for_decl_location (loc);
13473 if (rtl == NULL_RTX)
13474 return 0;
13476 if (!MEM_P (rtl))
13477 return 0;
13478 rtl = XEXP (rtl, 0);
13479 if (! CONSTANT_P (rtl))
13480 return 0;
13482 ret = new_loc_descr (first_op, 0, 0);
13483 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
13484 ret->dw_loc_oprnd1.v.val_addr = rtl;
13485 ret->dtprel = dtprel;
13487 ret1 = new_loc_descr (second_op, 0, 0);
13488 add_loc_descr (&ret, ret1);
13490 have_address = 1;
13491 break;
13493 /* FALLTHRU */
13495 case PARM_DECL:
13496 case RESULT_DECL:
13497 if (DECL_HAS_VALUE_EXPR_P (loc))
13498 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
13499 want_address);
13500 /* FALLTHRU */
13502 case FUNCTION_DECL:
13504 rtx rtl;
13505 var_loc_list *loc_list = lookup_decl_loc (loc);
13507 if (loc_list && loc_list->first)
13509 list_ret = dw_loc_list (loc_list, loc, want_address);
13510 have_address = want_address != 0;
13511 break;
13513 rtl = rtl_for_decl_location (loc);
13514 if (rtl == NULL_RTX)
13516 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
13517 return 0;
13519 else if (CONST_INT_P (rtl))
13521 HOST_WIDE_INT val = INTVAL (rtl);
13522 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13523 val &= GET_MODE_MASK (DECL_MODE (loc));
13524 ret = int_loc_descriptor (val);
13526 else if (GET_CODE (rtl) == CONST_STRING)
13528 expansion_failed (loc, NULL_RTX, "CONST_STRING");
13529 return 0;
13531 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
13533 ret = new_loc_descr (DW_OP_addr, 0, 0);
13534 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
13535 ret->dw_loc_oprnd1.v.val_addr = rtl;
13537 else
13539 enum machine_mode mode, mem_mode;
13541 /* Certain constructs can only be represented at top-level. */
13542 if (want_address == 2)
13544 ret = loc_descriptor (rtl, VOIDmode,
13545 VAR_INIT_STATUS_INITIALIZED);
13546 have_address = 1;
13548 else
13550 mode = GET_MODE (rtl);
13551 mem_mode = VOIDmode;
13552 if (MEM_P (rtl))
13554 mem_mode = mode;
13555 mode = get_address_mode (rtl);
13556 rtl = XEXP (rtl, 0);
13557 have_address = 1;
13559 ret = mem_loc_descriptor (rtl, mode, mem_mode,
13560 VAR_INIT_STATUS_INITIALIZED);
13562 if (!ret)
13563 expansion_failed (loc, rtl,
13564 "failed to produce loc descriptor for rtl");
13567 break;
13569 case MEM_REF:
13570 /* ??? FIXME. */
13571 if (!integer_zerop (TREE_OPERAND (loc, 1)))
13572 return 0;
13573 /* Fallthru. */
13574 case INDIRECT_REF:
13575 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13576 have_address = 1;
13577 break;
13579 case COMPOUND_EXPR:
13580 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
13582 CASE_CONVERT:
13583 case VIEW_CONVERT_EXPR:
13584 case SAVE_EXPR:
13585 case MODIFY_EXPR:
13586 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
13588 case COMPONENT_REF:
13589 case BIT_FIELD_REF:
13590 case ARRAY_REF:
13591 case ARRAY_RANGE_REF:
13592 case REALPART_EXPR:
13593 case IMAGPART_EXPR:
13595 tree obj, offset;
13596 HOST_WIDE_INT bitsize, bitpos, bytepos;
13597 enum machine_mode mode;
13598 int volatilep;
13599 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
13601 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
13602 &unsignedp, &volatilep, false);
13604 gcc_assert (obj != loc);
13606 list_ret = loc_list_from_tree (obj,
13607 want_address == 2
13608 && !bitpos && !offset ? 2 : 1);
13609 /* TODO: We can extract value of the small expression via shifting even
13610 for nonzero bitpos. */
13611 if (list_ret == 0)
13612 return 0;
13613 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
13615 expansion_failed (loc, NULL_RTX,
13616 "bitfield access");
13617 return 0;
13620 if (offset != NULL_TREE)
13622 /* Variable offset. */
13623 list_ret1 = loc_list_from_tree (offset, 0);
13624 if (list_ret1 == 0)
13625 return 0;
13626 add_loc_list (&list_ret, list_ret1);
13627 if (!list_ret)
13628 return 0;
13629 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
13632 bytepos = bitpos / BITS_PER_UNIT;
13633 if (bytepos > 0)
13634 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
13635 else if (bytepos < 0)
13636 loc_list_plus_const (list_ret, bytepos);
13638 have_address = 1;
13639 break;
13642 case INTEGER_CST:
13643 if ((want_address || !host_integerp (loc, 0))
13644 && (ret = cst_pool_loc_descr (loc)))
13645 have_address = 1;
13646 else if (want_address == 2
13647 && host_integerp (loc, 0)
13648 && (ret = address_of_int_loc_descriptor
13649 (int_size_in_bytes (TREE_TYPE (loc)),
13650 tree_low_cst (loc, 0))))
13651 have_address = 1;
13652 else if (host_integerp (loc, 0))
13653 ret = int_loc_descriptor (tree_low_cst (loc, 0));
13654 else
13656 expansion_failed (loc, NULL_RTX,
13657 "Integer operand is not host integer");
13658 return 0;
13660 break;
13662 case CONSTRUCTOR:
13663 case REAL_CST:
13664 case STRING_CST:
13665 case COMPLEX_CST:
13666 if ((ret = cst_pool_loc_descr (loc)))
13667 have_address = 1;
13668 else
13669 /* We can construct small constants here using int_loc_descriptor. */
13670 expansion_failed (loc, NULL_RTX,
13671 "constructor or constant not in constant pool");
13672 break;
13674 case TRUTH_AND_EXPR:
13675 case TRUTH_ANDIF_EXPR:
13676 case BIT_AND_EXPR:
13677 op = DW_OP_and;
13678 goto do_binop;
13680 case TRUTH_XOR_EXPR:
13681 case BIT_XOR_EXPR:
13682 op = DW_OP_xor;
13683 goto do_binop;
13685 case TRUTH_OR_EXPR:
13686 case TRUTH_ORIF_EXPR:
13687 case BIT_IOR_EXPR:
13688 op = DW_OP_or;
13689 goto do_binop;
13691 case FLOOR_DIV_EXPR:
13692 case CEIL_DIV_EXPR:
13693 case ROUND_DIV_EXPR:
13694 case TRUNC_DIV_EXPR:
13695 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13696 return 0;
13697 op = DW_OP_div;
13698 goto do_binop;
13700 case MINUS_EXPR:
13701 op = DW_OP_minus;
13702 goto do_binop;
13704 case FLOOR_MOD_EXPR:
13705 case CEIL_MOD_EXPR:
13706 case ROUND_MOD_EXPR:
13707 case TRUNC_MOD_EXPR:
13708 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13710 op = DW_OP_mod;
13711 goto do_binop;
13713 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13714 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
13715 if (list_ret == 0 || list_ret1 == 0)
13716 return 0;
13718 add_loc_list (&list_ret, list_ret1);
13719 if (list_ret == 0)
13720 return 0;
13721 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
13722 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
13723 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
13724 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
13725 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
13726 break;
13728 case MULT_EXPR:
13729 op = DW_OP_mul;
13730 goto do_binop;
13732 case LSHIFT_EXPR:
13733 op = DW_OP_shl;
13734 goto do_binop;
13736 case RSHIFT_EXPR:
13737 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
13738 goto do_binop;
13740 case POINTER_PLUS_EXPR:
13741 case PLUS_EXPR:
13742 if (host_integerp (TREE_OPERAND (loc, 1), 0))
13744 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13745 if (list_ret == 0)
13746 return 0;
13748 loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
13749 break;
13752 op = DW_OP_plus;
13753 goto do_binop;
13755 case LE_EXPR:
13756 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13757 return 0;
13759 op = DW_OP_le;
13760 goto do_binop;
13762 case GE_EXPR:
13763 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13764 return 0;
13766 op = DW_OP_ge;
13767 goto do_binop;
13769 case LT_EXPR:
13770 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13771 return 0;
13773 op = DW_OP_lt;
13774 goto do_binop;
13776 case GT_EXPR:
13777 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13778 return 0;
13780 op = DW_OP_gt;
13781 goto do_binop;
13783 case EQ_EXPR:
13784 op = DW_OP_eq;
13785 goto do_binop;
13787 case NE_EXPR:
13788 op = DW_OP_ne;
13789 goto do_binop;
13791 do_binop:
13792 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13793 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
13794 if (list_ret == 0 || list_ret1 == 0)
13795 return 0;
13797 add_loc_list (&list_ret, list_ret1);
13798 if (list_ret == 0)
13799 return 0;
13800 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
13801 break;
13803 case TRUTH_NOT_EXPR:
13804 case BIT_NOT_EXPR:
13805 op = DW_OP_not;
13806 goto do_unop;
13808 case ABS_EXPR:
13809 op = DW_OP_abs;
13810 goto do_unop;
13812 case NEGATE_EXPR:
13813 op = DW_OP_neg;
13814 goto do_unop;
13816 do_unop:
13817 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13818 if (list_ret == 0)
13819 return 0;
13821 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
13822 break;
13824 case MIN_EXPR:
13825 case MAX_EXPR:
13827 const enum tree_code code =
13828 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
13830 loc = build3 (COND_EXPR, TREE_TYPE (loc),
13831 build2 (code, integer_type_node,
13832 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
13833 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
13836 /* ... fall through ... */
13838 case COND_EXPR:
13840 dw_loc_descr_ref lhs
13841 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
13842 dw_loc_list_ref rhs
13843 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
13844 dw_loc_descr_ref bra_node, jump_node, tmp;
13846 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13847 if (list_ret == 0 || lhs == 0 || rhs == 0)
13848 return 0;
13850 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13851 add_loc_descr_to_each (list_ret, bra_node);
13853 add_loc_list (&list_ret, rhs);
13854 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
13855 add_loc_descr_to_each (list_ret, jump_node);
13857 add_loc_descr_to_each (list_ret, lhs);
13858 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13859 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
13861 /* ??? Need a node to point the skip at. Use a nop. */
13862 tmp = new_loc_descr (DW_OP_nop, 0, 0);
13863 add_loc_descr_to_each (list_ret, tmp);
13864 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13865 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
13867 break;
13869 case FIX_TRUNC_EXPR:
13870 return 0;
13872 default:
13873 /* Leave front-end specific codes as simply unknown. This comes
13874 up, for instance, with the C STMT_EXPR. */
13875 if ((unsigned int) TREE_CODE (loc)
13876 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
13878 expansion_failed (loc, NULL_RTX,
13879 "language specific tree node");
13880 return 0;
13883 #ifdef ENABLE_CHECKING
13884 /* Otherwise this is a generic code; we should just lists all of
13885 these explicitly. We forgot one. */
13886 gcc_unreachable ();
13887 #else
13888 /* In a release build, we want to degrade gracefully: better to
13889 generate incomplete debugging information than to crash. */
13890 return NULL;
13891 #endif
13894 if (!ret && !list_ret)
13895 return 0;
13897 if (want_address == 2 && !have_address
13898 && (dwarf_version >= 4 || !dwarf_strict))
13900 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13902 expansion_failed (loc, NULL_RTX,
13903 "DWARF address size mismatch");
13904 return 0;
13906 if (ret)
13907 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
13908 else
13909 add_loc_descr_to_each (list_ret,
13910 new_loc_descr (DW_OP_stack_value, 0, 0));
13911 have_address = 1;
13913 /* Show if we can't fill the request for an address. */
13914 if (want_address && !have_address)
13916 expansion_failed (loc, NULL_RTX,
13917 "Want address and only have value");
13918 return 0;
13921 gcc_assert (!ret || !list_ret);
13923 /* If we've got an address and don't want one, dereference. */
13924 if (!want_address && have_address)
13926 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13928 if (size > DWARF2_ADDR_SIZE || size == -1)
13930 expansion_failed (loc, NULL_RTX,
13931 "DWARF address size mismatch");
13932 return 0;
13934 else if (size == DWARF2_ADDR_SIZE)
13935 op = DW_OP_deref;
13936 else
13937 op = DW_OP_deref_size;
13939 if (ret)
13940 add_loc_descr (&ret, new_loc_descr (op, size, 0));
13941 else
13942 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
13944 if (ret)
13945 list_ret = new_loc_list (ret, NULL, NULL, NULL);
13947 return list_ret;
13950 /* Same as above but return only single location expression. */
13951 static dw_loc_descr_ref
13952 loc_descriptor_from_tree (tree loc, int want_address)
13954 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
13955 if (!ret)
13956 return NULL;
13957 if (ret->dw_loc_next)
13959 expansion_failed (loc, NULL_RTX,
13960 "Location list where only loc descriptor needed");
13961 return NULL;
13963 return ret->expr;
13966 /* Given a value, round it up to the lowest multiple of `boundary'
13967 which is not less than the value itself. */
13969 static inline HOST_WIDE_INT
13970 ceiling (HOST_WIDE_INT value, unsigned int boundary)
13972 return (((value + boundary - 1) / boundary) * boundary);
13975 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
13976 pointer to the declared type for the relevant field variable, or return
13977 `integer_type_node' if the given node turns out to be an
13978 ERROR_MARK node. */
13980 static inline tree
13981 field_type (const_tree decl)
13983 tree type;
13985 if (TREE_CODE (decl) == ERROR_MARK)
13986 return integer_type_node;
13988 type = DECL_BIT_FIELD_TYPE (decl);
13989 if (type == NULL_TREE)
13990 type = TREE_TYPE (decl);
13992 return type;
13995 /* Given a pointer to a tree node, return the alignment in bits for
13996 it, or else return BITS_PER_WORD if the node actually turns out to
13997 be an ERROR_MARK node. */
13999 static inline unsigned
14000 simple_type_align_in_bits (const_tree type)
14002 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
14005 static inline unsigned
14006 simple_decl_align_in_bits (const_tree decl)
14008 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
14011 /* Return the result of rounding T up to ALIGN. */
14013 static inline double_int
14014 round_up_to_align (double_int t, unsigned int align)
14016 double_int alignd = uhwi_to_double_int (align);
14017 t = double_int_add (t, alignd);
14018 t = double_int_add (t, double_int_minus_one);
14019 t = double_int_div (t, alignd, true, TRUNC_DIV_EXPR);
14020 t = double_int_mul (t, alignd);
14021 return t;
14024 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
14025 lowest addressed byte of the "containing object" for the given FIELD_DECL,
14026 or return 0 if we are unable to determine what that offset is, either
14027 because the argument turns out to be a pointer to an ERROR_MARK node, or
14028 because the offset is actually variable. (We can't handle the latter case
14029 just yet). */
14031 static HOST_WIDE_INT
14032 field_byte_offset (const_tree decl)
14034 double_int object_offset_in_bits;
14035 double_int object_offset_in_bytes;
14036 double_int bitpos_int;
14038 if (TREE_CODE (decl) == ERROR_MARK)
14039 return 0;
14041 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
14043 /* We cannot yet cope with fields whose positions are variable, so
14044 for now, when we see such things, we simply return 0. Someday, we may
14045 be able to handle such cases, but it will be damn difficult. */
14046 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
14047 return 0;
14049 bitpos_int = tree_to_double_int (bit_position (decl));
14051 #ifdef PCC_BITFIELD_TYPE_MATTERS
14052 if (PCC_BITFIELD_TYPE_MATTERS)
14054 tree type;
14055 tree field_size_tree;
14056 double_int deepest_bitpos;
14057 double_int field_size_in_bits;
14058 unsigned int type_align_in_bits;
14059 unsigned int decl_align_in_bits;
14060 double_int type_size_in_bits;
14062 type = field_type (decl);
14063 type_size_in_bits = double_int_type_size_in_bits (type);
14064 type_align_in_bits = simple_type_align_in_bits (type);
14066 field_size_tree = DECL_SIZE (decl);
14068 /* The size could be unspecified if there was an error, or for
14069 a flexible array member. */
14070 if (!field_size_tree)
14071 field_size_tree = bitsize_zero_node;
14073 /* If the size of the field is not constant, use the type size. */
14074 if (TREE_CODE (field_size_tree) == INTEGER_CST)
14075 field_size_in_bits = tree_to_double_int (field_size_tree);
14076 else
14077 field_size_in_bits = type_size_in_bits;
14079 decl_align_in_bits = simple_decl_align_in_bits (decl);
14081 /* The GCC front-end doesn't make any attempt to keep track of the
14082 starting bit offset (relative to the start of the containing
14083 structure type) of the hypothetical "containing object" for a
14084 bit-field. Thus, when computing the byte offset value for the
14085 start of the "containing object" of a bit-field, we must deduce
14086 this information on our own. This can be rather tricky to do in
14087 some cases. For example, handling the following structure type
14088 definition when compiling for an i386/i486 target (which only
14089 aligns long long's to 32-bit boundaries) can be very tricky:
14091 struct S { int field1; long long field2:31; };
14093 Fortunately, there is a simple rule-of-thumb which can be used
14094 in such cases. When compiling for an i386/i486, GCC will
14095 allocate 8 bytes for the structure shown above. It decides to
14096 do this based upon one simple rule for bit-field allocation.
14097 GCC allocates each "containing object" for each bit-field at
14098 the first (i.e. lowest addressed) legitimate alignment boundary
14099 (based upon the required minimum alignment for the declared
14100 type of the field) which it can possibly use, subject to the
14101 condition that there is still enough available space remaining
14102 in the containing object (when allocated at the selected point)
14103 to fully accommodate all of the bits of the bit-field itself.
14105 This simple rule makes it obvious why GCC allocates 8 bytes for
14106 each object of the structure type shown above. When looking
14107 for a place to allocate the "containing object" for `field2',
14108 the compiler simply tries to allocate a 64-bit "containing
14109 object" at each successive 32-bit boundary (starting at zero)
14110 until it finds a place to allocate that 64- bit field such that
14111 at least 31 contiguous (and previously unallocated) bits remain
14112 within that selected 64 bit field. (As it turns out, for the
14113 example above, the compiler finds it is OK to allocate the
14114 "containing object" 64-bit field at bit-offset zero within the
14115 structure type.)
14117 Here we attempt to work backwards from the limited set of facts
14118 we're given, and we try to deduce from those facts, where GCC
14119 must have believed that the containing object started (within
14120 the structure type). The value we deduce is then used (by the
14121 callers of this routine) to generate DW_AT_location and
14122 DW_AT_bit_offset attributes for fields (both bit-fields and, in
14123 the case of DW_AT_location, regular fields as well). */
14125 /* Figure out the bit-distance from the start of the structure to
14126 the "deepest" bit of the bit-field. */
14127 deepest_bitpos = double_int_add (bitpos_int, field_size_in_bits);
14129 /* This is the tricky part. Use some fancy footwork to deduce
14130 where the lowest addressed bit of the containing object must
14131 be. */
14132 object_offset_in_bits
14133 = double_int_sub (deepest_bitpos, type_size_in_bits);
14135 /* Round up to type_align by default. This works best for
14136 bitfields. */
14137 object_offset_in_bits
14138 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
14140 if (double_int_ucmp (object_offset_in_bits, bitpos_int) > 0)
14142 object_offset_in_bits
14143 = double_int_sub (deepest_bitpos, type_size_in_bits);
14145 /* Round up to decl_align instead. */
14146 object_offset_in_bits
14147 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
14150 else
14151 #endif /* PCC_BITFIELD_TYPE_MATTERS */
14152 object_offset_in_bits = bitpos_int;
14154 object_offset_in_bytes
14155 = double_int_div (object_offset_in_bits,
14156 uhwi_to_double_int (BITS_PER_UNIT), true,
14157 TRUNC_DIV_EXPR);
14158 return double_int_to_shwi (object_offset_in_bytes);
14161 /* The following routines define various Dwarf attributes and any data
14162 associated with them. */
14164 /* Add a location description attribute value to a DIE.
14166 This emits location attributes suitable for whole variables and
14167 whole parameters. Note that the location attributes for struct fields are
14168 generated by the routine `data_member_location_attribute' below. */
14170 static inline void
14171 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
14172 dw_loc_list_ref descr)
14174 if (descr == 0)
14175 return;
14176 if (single_element_loc_list_p (descr))
14177 add_AT_loc (die, attr_kind, descr->expr);
14178 else
14179 add_AT_loc_list (die, attr_kind, descr);
14182 /* Add DW_AT_accessibility attribute to DIE if needed. */
14184 static void
14185 add_accessibility_attribute (dw_die_ref die, tree decl)
14187 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
14188 children, otherwise the default is DW_ACCESS_public. In DWARF2
14189 the default has always been DW_ACCESS_public. */
14190 if (TREE_PROTECTED (decl))
14191 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
14192 else if (TREE_PRIVATE (decl))
14194 if (dwarf_version == 2
14195 || die->die_parent == NULL
14196 || die->die_parent->die_tag != DW_TAG_class_type)
14197 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
14199 else if (dwarf_version > 2
14200 && die->die_parent
14201 && die->die_parent->die_tag == DW_TAG_class_type)
14202 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
14205 /* Attach the specialized form of location attribute used for data members of
14206 struct and union types. In the special case of a FIELD_DECL node which
14207 represents a bit-field, the "offset" part of this special location
14208 descriptor must indicate the distance in bytes from the lowest-addressed
14209 byte of the containing struct or union type to the lowest-addressed byte of
14210 the "containing object" for the bit-field. (See the `field_byte_offset'
14211 function above).
14213 For any given bit-field, the "containing object" is a hypothetical object
14214 (of some integral or enum type) within which the given bit-field lives. The
14215 type of this hypothetical "containing object" is always the same as the
14216 declared type of the individual bit-field itself (for GCC anyway... the
14217 DWARF spec doesn't actually mandate this). Note that it is the size (in
14218 bytes) of the hypothetical "containing object" which will be given in the
14219 DW_AT_byte_size attribute for this bit-field. (See the
14220 `byte_size_attribute' function below.) It is also used when calculating the
14221 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
14222 function below.) */
14224 static void
14225 add_data_member_location_attribute (dw_die_ref die, tree decl)
14227 HOST_WIDE_INT offset;
14228 dw_loc_descr_ref loc_descr = 0;
14230 if (TREE_CODE (decl) == TREE_BINFO)
14232 /* We're working on the TAG_inheritance for a base class. */
14233 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
14235 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
14236 aren't at a fixed offset from all (sub)objects of the same
14237 type. We need to extract the appropriate offset from our
14238 vtable. The following dwarf expression means
14240 BaseAddr = ObAddr + *((*ObAddr) - Offset)
14242 This is specific to the V3 ABI, of course. */
14244 dw_loc_descr_ref tmp;
14246 /* Make a copy of the object address. */
14247 tmp = new_loc_descr (DW_OP_dup, 0, 0);
14248 add_loc_descr (&loc_descr, tmp);
14250 /* Extract the vtable address. */
14251 tmp = new_loc_descr (DW_OP_deref, 0, 0);
14252 add_loc_descr (&loc_descr, tmp);
14254 /* Calculate the address of the offset. */
14255 offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
14256 gcc_assert (offset < 0);
14258 tmp = int_loc_descriptor (-offset);
14259 add_loc_descr (&loc_descr, tmp);
14260 tmp = new_loc_descr (DW_OP_minus, 0, 0);
14261 add_loc_descr (&loc_descr, tmp);
14263 /* Extract the offset. */
14264 tmp = new_loc_descr (DW_OP_deref, 0, 0);
14265 add_loc_descr (&loc_descr, tmp);
14267 /* Add it to the object address. */
14268 tmp = new_loc_descr (DW_OP_plus, 0, 0);
14269 add_loc_descr (&loc_descr, tmp);
14271 else
14272 offset = tree_low_cst (BINFO_OFFSET (decl), 0);
14274 else
14275 offset = field_byte_offset (decl);
14277 if (! loc_descr)
14279 if (dwarf_version > 2)
14281 /* Don't need to output a location expression, just the constant. */
14282 if (offset < 0)
14283 add_AT_int (die, DW_AT_data_member_location, offset);
14284 else
14285 add_AT_unsigned (die, DW_AT_data_member_location, offset);
14286 return;
14288 else
14290 enum dwarf_location_atom op;
14292 /* The DWARF2 standard says that we should assume that the structure
14293 address is already on the stack, so we can specify a structure
14294 field address by using DW_OP_plus_uconst. */
14296 #ifdef MIPS_DEBUGGING_INFO
14297 /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
14298 operator correctly. It works only if we leave the offset on the
14299 stack. */
14300 op = DW_OP_constu;
14301 #else
14302 op = DW_OP_plus_uconst;
14303 #endif
14305 loc_descr = new_loc_descr (op, offset, 0);
14309 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
14312 /* Writes integer values to dw_vec_const array. */
14314 static void
14315 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
14317 while (size != 0)
14319 *dest++ = val & 0xff;
14320 val >>= 8;
14321 --size;
14325 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
14327 static HOST_WIDE_INT
14328 extract_int (const unsigned char *src, unsigned int size)
14330 HOST_WIDE_INT val = 0;
14332 src += size;
14333 while (size != 0)
14335 val <<= 8;
14336 val |= *--src & 0xff;
14337 --size;
14339 return val;
14342 /* Writes double_int values to dw_vec_const array. */
14344 static void
14345 insert_double (double_int val, unsigned char *dest)
14347 unsigned char *p0 = dest;
14348 unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
14350 if (WORDS_BIG_ENDIAN)
14352 p0 = p1;
14353 p1 = dest;
14356 insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
14357 insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
14360 /* Writes floating point values to dw_vec_const array. */
14362 static void
14363 insert_float (const_rtx rtl, unsigned char *array)
14365 REAL_VALUE_TYPE rv;
14366 long val[4];
14367 int i;
14369 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
14370 real_to_target (val, &rv, GET_MODE (rtl));
14372 /* real_to_target puts 32-bit pieces in each long. Pack them. */
14373 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
14375 insert_int (val[i], 4, array);
14376 array += 4;
14380 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
14381 does not have a "location" either in memory or in a register. These
14382 things can arise in GNU C when a constant is passed as an actual parameter
14383 to an inlined function. They can also arise in C++ where declared
14384 constants do not necessarily get memory "homes". */
14386 static bool
14387 add_const_value_attribute (dw_die_ref die, rtx rtl)
14389 switch (GET_CODE (rtl))
14391 case CONST_INT:
14393 HOST_WIDE_INT val = INTVAL (rtl);
14395 if (val < 0)
14396 add_AT_int (die, DW_AT_const_value, val);
14397 else
14398 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
14400 return true;
14402 case CONST_DOUBLE:
14403 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
14404 floating-point constant. A CONST_DOUBLE is used whenever the
14405 constant requires more than one word in order to be adequately
14406 represented. */
14408 enum machine_mode mode = GET_MODE (rtl);
14410 if (SCALAR_FLOAT_MODE_P (mode))
14412 unsigned int length = GET_MODE_SIZE (mode);
14413 unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
14415 insert_float (rtl, array);
14416 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
14418 else
14419 add_AT_double (die, DW_AT_const_value,
14420 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
14422 return true;
14424 case CONST_VECTOR:
14426 enum machine_mode mode = GET_MODE (rtl);
14427 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
14428 unsigned int length = CONST_VECTOR_NUNITS (rtl);
14429 unsigned char *array = (unsigned char *) ggc_alloc_atomic
14430 (length * elt_size);
14431 unsigned int i;
14432 unsigned char *p;
14434 switch (GET_MODE_CLASS (mode))
14436 case MODE_VECTOR_INT:
14437 for (i = 0, p = array; i < length; i++, p += elt_size)
14439 rtx elt = CONST_VECTOR_ELT (rtl, i);
14440 double_int val = rtx_to_double_int (elt);
14442 if (elt_size <= sizeof (HOST_WIDE_INT))
14443 insert_int (double_int_to_shwi (val), elt_size, p);
14444 else
14446 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
14447 insert_double (val, p);
14450 break;
14452 case MODE_VECTOR_FLOAT:
14453 for (i = 0, p = array; i < length; i++, p += elt_size)
14455 rtx elt = CONST_VECTOR_ELT (rtl, i);
14456 insert_float (elt, p);
14458 break;
14460 default:
14461 gcc_unreachable ();
14464 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
14466 return true;
14468 case CONST_STRING:
14469 if (dwarf_version >= 4 || !dwarf_strict)
14471 dw_loc_descr_ref loc_result;
14472 resolve_one_addr (&rtl, NULL);
14473 rtl_addr:
14474 loc_result = new_loc_descr (DW_OP_addr, 0, 0);
14475 loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
14476 loc_result->dw_loc_oprnd1.v.val_addr = rtl;
14477 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14478 add_AT_loc (die, DW_AT_location, loc_result);
14479 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
14480 return true;
14482 return false;
14484 case CONST:
14485 if (CONSTANT_P (XEXP (rtl, 0)))
14486 return add_const_value_attribute (die, XEXP (rtl, 0));
14487 /* FALLTHROUGH */
14488 case SYMBOL_REF:
14489 if (!const_ok_for_output (rtl))
14490 return false;
14491 case LABEL_REF:
14492 if (dwarf_version >= 4 || !dwarf_strict)
14493 goto rtl_addr;
14494 return false;
14496 case PLUS:
14497 /* In cases where an inlined instance of an inline function is passed
14498 the address of an `auto' variable (which is local to the caller) we
14499 can get a situation where the DECL_RTL of the artificial local
14500 variable (for the inlining) which acts as a stand-in for the
14501 corresponding formal parameter (of the inline function) will look
14502 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
14503 exactly a compile-time constant expression, but it isn't the address
14504 of the (artificial) local variable either. Rather, it represents the
14505 *value* which the artificial local variable always has during its
14506 lifetime. We currently have no way to represent such quasi-constant
14507 values in Dwarf, so for now we just punt and generate nothing. */
14508 return false;
14510 case HIGH:
14511 case CONST_FIXED:
14512 return false;
14514 case MEM:
14515 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
14516 && MEM_READONLY_P (rtl)
14517 && GET_MODE (rtl) == BLKmode)
14519 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
14520 return true;
14522 return false;
14524 default:
14525 /* No other kinds of rtx should be possible here. */
14526 gcc_unreachable ();
14528 return false;
14531 /* Determine whether the evaluation of EXPR references any variables
14532 or functions which aren't otherwise used (and therefore may not be
14533 output). */
14534 static tree
14535 reference_to_unused (tree * tp, int * walk_subtrees,
14536 void * data ATTRIBUTE_UNUSED)
14538 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
14539 *walk_subtrees = 0;
14541 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
14542 && ! TREE_ASM_WRITTEN (*tp))
14543 return *tp;
14544 /* ??? The C++ FE emits debug information for using decls, so
14545 putting gcc_unreachable here falls over. See PR31899. For now
14546 be conservative. */
14547 else if (!cgraph_global_info_ready
14548 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
14549 return *tp;
14550 else if (TREE_CODE (*tp) == VAR_DECL)
14552 struct varpool_node *node = varpool_get_node (*tp);
14553 if (!node || !node->needed)
14554 return *tp;
14556 else if (TREE_CODE (*tp) == FUNCTION_DECL
14557 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
14559 /* The call graph machinery must have finished analyzing,
14560 optimizing and gimplifying the CU by now.
14561 So if *TP has no call graph node associated
14562 to it, it means *TP will not be emitted. */
14563 if (!cgraph_get_node (*tp))
14564 return *tp;
14566 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
14567 return *tp;
14569 return NULL_TREE;
14572 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
14573 for use in a later add_const_value_attribute call. */
14575 static rtx
14576 rtl_for_decl_init (tree init, tree type)
14578 rtx rtl = NULL_RTX;
14580 STRIP_NOPS (init);
14582 /* If a variable is initialized with a string constant without embedded
14583 zeros, build CONST_STRING. */
14584 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
14586 tree enttype = TREE_TYPE (type);
14587 tree domain = TYPE_DOMAIN (type);
14588 enum machine_mode mode = TYPE_MODE (enttype);
14590 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
14591 && domain
14592 && integer_zerop (TYPE_MIN_VALUE (domain))
14593 && compare_tree_int (TYPE_MAX_VALUE (domain),
14594 TREE_STRING_LENGTH (init) - 1) == 0
14595 && ((size_t) TREE_STRING_LENGTH (init)
14596 == strlen (TREE_STRING_POINTER (init)) + 1))
14598 rtl = gen_rtx_CONST_STRING (VOIDmode,
14599 ggc_strdup (TREE_STRING_POINTER (init)));
14600 rtl = gen_rtx_MEM (BLKmode, rtl);
14601 MEM_READONLY_P (rtl) = 1;
14604 /* Other aggregates, and complex values, could be represented using
14605 CONCAT: FIXME! */
14606 else if (AGGREGATE_TYPE_P (type)
14607 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
14608 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
14609 || TREE_CODE (type) == COMPLEX_TYPE)
14611 /* Vectors only work if their mode is supported by the target.
14612 FIXME: generic vectors ought to work too. */
14613 else if (TREE_CODE (type) == VECTOR_TYPE
14614 && !VECTOR_MODE_P (TYPE_MODE (type)))
14616 /* If the initializer is something that we know will expand into an
14617 immediate RTL constant, expand it now. We must be careful not to
14618 reference variables which won't be output. */
14619 else if (initializer_constant_valid_p (init, type)
14620 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
14622 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
14623 possible. */
14624 if (TREE_CODE (type) == VECTOR_TYPE)
14625 switch (TREE_CODE (init))
14627 case VECTOR_CST:
14628 break;
14629 case CONSTRUCTOR:
14630 if (TREE_CONSTANT (init))
14632 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
14633 bool constant_p = true;
14634 tree value;
14635 unsigned HOST_WIDE_INT ix;
14637 /* Even when ctor is constant, it might contain non-*_CST
14638 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
14639 belong into VECTOR_CST nodes. */
14640 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
14641 if (!CONSTANT_CLASS_P (value))
14643 constant_p = false;
14644 break;
14647 if (constant_p)
14649 init = build_vector_from_ctor (type, elts);
14650 break;
14653 /* FALLTHRU */
14655 default:
14656 return NULL;
14659 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
14661 /* If expand_expr returns a MEM, it wasn't immediate. */
14662 gcc_assert (!rtl || !MEM_P (rtl));
14665 return rtl;
14668 /* Generate RTL for the variable DECL to represent its location. */
14670 static rtx
14671 rtl_for_decl_location (tree decl)
14673 rtx rtl;
14675 /* Here we have to decide where we are going to say the parameter "lives"
14676 (as far as the debugger is concerned). We only have a couple of
14677 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
14679 DECL_RTL normally indicates where the parameter lives during most of the
14680 activation of the function. If optimization is enabled however, this
14681 could be either NULL or else a pseudo-reg. Both of those cases indicate
14682 that the parameter doesn't really live anywhere (as far as the code
14683 generation parts of GCC are concerned) during most of the function's
14684 activation. That will happen (for example) if the parameter is never
14685 referenced within the function.
14687 We could just generate a location descriptor here for all non-NULL
14688 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
14689 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
14690 where DECL_RTL is NULL or is a pseudo-reg.
14692 Note however that we can only get away with using DECL_INCOMING_RTL as
14693 a backup substitute for DECL_RTL in certain limited cases. In cases
14694 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
14695 we can be sure that the parameter was passed using the same type as it is
14696 declared to have within the function, and that its DECL_INCOMING_RTL
14697 points us to a place where a value of that type is passed.
14699 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
14700 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
14701 because in these cases DECL_INCOMING_RTL points us to a value of some
14702 type which is *different* from the type of the parameter itself. Thus,
14703 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
14704 such cases, the debugger would end up (for example) trying to fetch a
14705 `float' from a place which actually contains the first part of a
14706 `double'. That would lead to really incorrect and confusing
14707 output at debug-time.
14709 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
14710 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
14711 are a couple of exceptions however. On little-endian machines we can
14712 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
14713 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
14714 an integral type that is smaller than TREE_TYPE (decl). These cases arise
14715 when (on a little-endian machine) a non-prototyped function has a
14716 parameter declared to be of type `short' or `char'. In such cases,
14717 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
14718 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
14719 passed `int' value. If the debugger then uses that address to fetch
14720 a `short' or a `char' (on a little-endian machine) the result will be
14721 the correct data, so we allow for such exceptional cases below.
14723 Note that our goal here is to describe the place where the given formal
14724 parameter lives during most of the function's activation (i.e. between the
14725 end of the prologue and the start of the epilogue). We'll do that as best
14726 as we can. Note however that if the given formal parameter is modified
14727 sometime during the execution of the function, then a stack backtrace (at
14728 debug-time) will show the function as having been called with the *new*
14729 value rather than the value which was originally passed in. This happens
14730 rarely enough that it is not a major problem, but it *is* a problem, and
14731 I'd like to fix it.
14733 A future version of dwarf2out.c may generate two additional attributes for
14734 any given DW_TAG_formal_parameter DIE which will describe the "passed
14735 type" and the "passed location" for the given formal parameter in addition
14736 to the attributes we now generate to indicate the "declared type" and the
14737 "active location" for each parameter. This additional set of attributes
14738 could be used by debuggers for stack backtraces. Separately, note that
14739 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
14740 This happens (for example) for inlined-instances of inline function formal
14741 parameters which are never referenced. This really shouldn't be
14742 happening. All PARM_DECL nodes should get valid non-NULL
14743 DECL_INCOMING_RTL values. FIXME. */
14745 /* Use DECL_RTL as the "location" unless we find something better. */
14746 rtl = DECL_RTL_IF_SET (decl);
14748 /* When generating abstract instances, ignore everything except
14749 constants, symbols living in memory, and symbols living in
14750 fixed registers. */
14751 if (! reload_completed)
14753 if (rtl
14754 && (CONSTANT_P (rtl)
14755 || (MEM_P (rtl)
14756 && CONSTANT_P (XEXP (rtl, 0)))
14757 || (REG_P (rtl)
14758 && TREE_CODE (decl) == VAR_DECL
14759 && TREE_STATIC (decl))))
14761 rtl = targetm.delegitimize_address (rtl);
14762 return rtl;
14764 rtl = NULL_RTX;
14766 else if (TREE_CODE (decl) == PARM_DECL)
14768 if (rtl == NULL_RTX
14769 || is_pseudo_reg (rtl)
14770 || (MEM_P (rtl)
14771 && is_pseudo_reg (XEXP (rtl, 0))
14772 && DECL_INCOMING_RTL (decl)
14773 && MEM_P (DECL_INCOMING_RTL (decl))
14774 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
14776 tree declared_type = TREE_TYPE (decl);
14777 tree passed_type = DECL_ARG_TYPE (decl);
14778 enum machine_mode dmode = TYPE_MODE (declared_type);
14779 enum machine_mode pmode = TYPE_MODE (passed_type);
14781 /* This decl represents a formal parameter which was optimized out.
14782 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
14783 all cases where (rtl == NULL_RTX) just below. */
14784 if (dmode == pmode)
14785 rtl = DECL_INCOMING_RTL (decl);
14786 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
14787 && SCALAR_INT_MODE_P (dmode)
14788 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
14789 && DECL_INCOMING_RTL (decl))
14791 rtx inc = DECL_INCOMING_RTL (decl);
14792 if (REG_P (inc))
14793 rtl = inc;
14794 else if (MEM_P (inc))
14796 if (BYTES_BIG_ENDIAN)
14797 rtl = adjust_address_nv (inc, dmode,
14798 GET_MODE_SIZE (pmode)
14799 - GET_MODE_SIZE (dmode));
14800 else
14801 rtl = inc;
14806 /* If the parm was passed in registers, but lives on the stack, then
14807 make a big endian correction if the mode of the type of the
14808 parameter is not the same as the mode of the rtl. */
14809 /* ??? This is the same series of checks that are made in dbxout.c before
14810 we reach the big endian correction code there. It isn't clear if all
14811 of these checks are necessary here, but keeping them all is the safe
14812 thing to do. */
14813 else if (MEM_P (rtl)
14814 && XEXP (rtl, 0) != const0_rtx
14815 && ! CONSTANT_P (XEXP (rtl, 0))
14816 /* Not passed in memory. */
14817 && !MEM_P (DECL_INCOMING_RTL (decl))
14818 /* Not passed by invisible reference. */
14819 && (!REG_P (XEXP (rtl, 0))
14820 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
14821 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
14822 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
14823 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
14824 #endif
14826 /* Big endian correction check. */
14827 && BYTES_BIG_ENDIAN
14828 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
14829 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
14830 < UNITS_PER_WORD))
14832 int offset = (UNITS_PER_WORD
14833 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
14835 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
14836 plus_constant (XEXP (rtl, 0), offset));
14839 else if (TREE_CODE (decl) == VAR_DECL
14840 && rtl
14841 && MEM_P (rtl)
14842 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
14843 && BYTES_BIG_ENDIAN)
14845 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
14846 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
14848 /* If a variable is declared "register" yet is smaller than
14849 a register, then if we store the variable to memory, it
14850 looks like we're storing a register-sized value, when in
14851 fact we are not. We need to adjust the offset of the
14852 storage location to reflect the actual value's bytes,
14853 else gdb will not be able to display it. */
14854 if (rsize > dsize)
14855 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
14856 plus_constant (XEXP (rtl, 0), rsize-dsize));
14859 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
14860 and will have been substituted directly into all expressions that use it.
14861 C does not have such a concept, but C++ and other languages do. */
14862 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
14863 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
14865 if (rtl)
14866 rtl = targetm.delegitimize_address (rtl);
14868 /* If we don't look past the constant pool, we risk emitting a
14869 reference to a constant pool entry that isn't referenced from
14870 code, and thus is not emitted. */
14871 if (rtl)
14872 rtl = avoid_constant_pool_reference (rtl);
14874 /* Try harder to get a rtl. If this symbol ends up not being emitted
14875 in the current CU, resolve_addr will remove the expression referencing
14876 it. */
14877 if (rtl == NULL_RTX
14878 && TREE_CODE (decl) == VAR_DECL
14879 && !DECL_EXTERNAL (decl)
14880 && TREE_STATIC (decl)
14881 && DECL_NAME (decl)
14882 && !DECL_HARD_REGISTER (decl)
14883 && DECL_MODE (decl) != VOIDmode)
14885 rtl = make_decl_rtl_for_debug (decl);
14886 if (!MEM_P (rtl)
14887 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
14888 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
14889 rtl = NULL_RTX;
14892 return rtl;
14895 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
14896 returned. If so, the decl for the COMMON block is returned, and the
14897 value is the offset into the common block for the symbol. */
14899 static tree
14900 fortran_common (tree decl, HOST_WIDE_INT *value)
14902 tree val_expr, cvar;
14903 enum machine_mode mode;
14904 HOST_WIDE_INT bitsize, bitpos;
14905 tree offset;
14906 int volatilep = 0, unsignedp = 0;
14908 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
14909 it does not have a value (the offset into the common area), or if it
14910 is thread local (as opposed to global) then it isn't common, and shouldn't
14911 be handled as such. */
14912 if (TREE_CODE (decl) != VAR_DECL
14913 || !TREE_STATIC (decl)
14914 || !DECL_HAS_VALUE_EXPR_P (decl)
14915 || !is_fortran ())
14916 return NULL_TREE;
14918 val_expr = DECL_VALUE_EXPR (decl);
14919 if (TREE_CODE (val_expr) != COMPONENT_REF)
14920 return NULL_TREE;
14922 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
14923 &mode, &unsignedp, &volatilep, true);
14925 if (cvar == NULL_TREE
14926 || TREE_CODE (cvar) != VAR_DECL
14927 || DECL_ARTIFICIAL (cvar)
14928 || !TREE_PUBLIC (cvar))
14929 return NULL_TREE;
14931 *value = 0;
14932 if (offset != NULL)
14934 if (!host_integerp (offset, 0))
14935 return NULL_TREE;
14936 *value = tree_low_cst (offset, 0);
14938 if (bitpos != 0)
14939 *value += bitpos / BITS_PER_UNIT;
14941 return cvar;
14944 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
14945 data attribute for a variable or a parameter. We generate the
14946 DW_AT_const_value attribute only in those cases where the given variable
14947 or parameter does not have a true "location" either in memory or in a
14948 register. This can happen (for example) when a constant is passed as an
14949 actual argument in a call to an inline function. (It's possible that
14950 these things can crop up in other ways also.) Note that one type of
14951 constant value which can be passed into an inlined function is a constant
14952 pointer. This can happen for example if an actual argument in an inlined
14953 function call evaluates to a compile-time constant address.
14955 CACHE_P is true if it is worth caching the location list for DECL,
14956 so that future calls can reuse it rather than regenerate it from scratch.
14957 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
14958 since we will need to refer to them each time the function is inlined. */
14960 static bool
14961 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
14962 enum dwarf_attribute attr)
14964 rtx rtl;
14965 dw_loc_list_ref list;
14966 var_loc_list *loc_list;
14967 cached_dw_loc_list *cache;
14968 void **slot;
14970 if (TREE_CODE (decl) == ERROR_MARK)
14971 return false;
14973 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
14974 || TREE_CODE (decl) == RESULT_DECL);
14976 /* Try to get some constant RTL for this decl, and use that as the value of
14977 the location. */
14979 rtl = rtl_for_decl_location (decl);
14980 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
14981 && add_const_value_attribute (die, rtl))
14982 return true;
14984 /* See if we have single element location list that is equivalent to
14985 a constant value. That way we are better to use add_const_value_attribute
14986 rather than expanding constant value equivalent. */
14987 loc_list = lookup_decl_loc (decl);
14988 if (loc_list
14989 && loc_list->first
14990 && loc_list->first->next == NULL
14991 && NOTE_P (loc_list->first->loc)
14992 && NOTE_VAR_LOCATION (loc_list->first->loc)
14993 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
14995 struct var_loc_node *node;
14997 node = loc_list->first;
14998 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
14999 if (GET_CODE (rtl) == EXPR_LIST)
15000 rtl = XEXP (rtl, 0);
15001 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15002 && add_const_value_attribute (die, rtl))
15003 return true;
15005 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
15006 list several times. See if we've already cached the contents. */
15007 list = NULL;
15008 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
15009 cache_p = false;
15010 if (cache_p)
15012 cache = (cached_dw_loc_list *)
15013 htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
15014 if (cache)
15015 list = cache->loc_list;
15017 if (list == NULL)
15019 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
15020 /* It is usually worth caching this result if the decl is from
15021 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
15022 if (cache_p && list && list->dw_loc_next)
15024 slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
15025 DECL_UID (decl), INSERT);
15026 cache = ggc_alloc_cleared_cached_dw_loc_list ();
15027 cache->decl_id = DECL_UID (decl);
15028 cache->loc_list = list;
15029 *slot = cache;
15032 if (list)
15034 add_AT_location_description (die, attr, list);
15035 return true;
15037 /* None of that worked, so it must not really have a location;
15038 try adding a constant value attribute from the DECL_INITIAL. */
15039 return tree_add_const_value_attribute_for_decl (die, decl);
15042 /* Add VARIABLE and DIE into deferred locations list. */
15044 static void
15045 defer_location (tree variable, dw_die_ref die)
15047 deferred_locations entry;
15048 entry.variable = variable;
15049 entry.die = die;
15050 VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
15053 /* Helper function for tree_add_const_value_attribute. Natively encode
15054 initializer INIT into an array. Return true if successful. */
15056 static bool
15057 native_encode_initializer (tree init, unsigned char *array, int size)
15059 tree type;
15061 if (init == NULL_TREE)
15062 return false;
15064 STRIP_NOPS (init);
15065 switch (TREE_CODE (init))
15067 case STRING_CST:
15068 type = TREE_TYPE (init);
15069 if (TREE_CODE (type) == ARRAY_TYPE)
15071 tree enttype = TREE_TYPE (type);
15072 enum machine_mode mode = TYPE_MODE (enttype);
15074 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
15075 return false;
15076 if (int_size_in_bytes (type) != size)
15077 return false;
15078 if (size > TREE_STRING_LENGTH (init))
15080 memcpy (array, TREE_STRING_POINTER (init),
15081 TREE_STRING_LENGTH (init));
15082 memset (array + TREE_STRING_LENGTH (init),
15083 '\0', size - TREE_STRING_LENGTH (init));
15085 else
15086 memcpy (array, TREE_STRING_POINTER (init), size);
15087 return true;
15089 return false;
15090 case CONSTRUCTOR:
15091 type = TREE_TYPE (init);
15092 if (int_size_in_bytes (type) != size)
15093 return false;
15094 if (TREE_CODE (type) == ARRAY_TYPE)
15096 HOST_WIDE_INT min_index;
15097 unsigned HOST_WIDE_INT cnt;
15098 int curpos = 0, fieldsize;
15099 constructor_elt *ce;
15101 if (TYPE_DOMAIN (type) == NULL_TREE
15102 || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
15103 return false;
15105 fieldsize = int_size_in_bytes (TREE_TYPE (type));
15106 if (fieldsize <= 0)
15107 return false;
15109 min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
15110 memset (array, '\0', size);
15111 FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
15113 tree val = ce->value;
15114 tree index = ce->index;
15115 int pos = curpos;
15116 if (index && TREE_CODE (index) == RANGE_EXPR)
15117 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
15118 * fieldsize;
15119 else if (index)
15120 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
15122 if (val)
15124 STRIP_NOPS (val);
15125 if (!native_encode_initializer (val, array + pos, fieldsize))
15126 return false;
15128 curpos = pos + fieldsize;
15129 if (index && TREE_CODE (index) == RANGE_EXPR)
15131 int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
15132 - tree_low_cst (TREE_OPERAND (index, 0), 0);
15133 while (count-- > 0)
15135 if (val)
15136 memcpy (array + curpos, array + pos, fieldsize);
15137 curpos += fieldsize;
15140 gcc_assert (curpos <= size);
15142 return true;
15144 else if (TREE_CODE (type) == RECORD_TYPE
15145 || TREE_CODE (type) == UNION_TYPE)
15147 tree field = NULL_TREE;
15148 unsigned HOST_WIDE_INT cnt;
15149 constructor_elt *ce;
15151 if (int_size_in_bytes (type) != size)
15152 return false;
15154 if (TREE_CODE (type) == RECORD_TYPE)
15155 field = TYPE_FIELDS (type);
15157 FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
15159 tree val = ce->value;
15160 int pos, fieldsize;
15162 if (ce->index != 0)
15163 field = ce->index;
15165 if (val)
15166 STRIP_NOPS (val);
15168 if (field == NULL_TREE || DECL_BIT_FIELD (field))
15169 return false;
15171 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
15172 && TYPE_DOMAIN (TREE_TYPE (field))
15173 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
15174 return false;
15175 else if (DECL_SIZE_UNIT (field) == NULL_TREE
15176 || !host_integerp (DECL_SIZE_UNIT (field), 0))
15177 return false;
15178 fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
15179 pos = int_byte_position (field);
15180 gcc_assert (pos + fieldsize <= size);
15181 if (val
15182 && !native_encode_initializer (val, array + pos, fieldsize))
15183 return false;
15185 return true;
15187 return false;
15188 case VIEW_CONVERT_EXPR:
15189 case NON_LVALUE_EXPR:
15190 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
15191 default:
15192 return native_encode_expr (init, array, size) == size;
15196 /* Attach a DW_AT_const_value attribute to DIE. The value of the
15197 attribute is the const value T. */
15199 static bool
15200 tree_add_const_value_attribute (dw_die_ref die, tree t)
15202 tree init;
15203 tree type = TREE_TYPE (t);
15204 rtx rtl;
15206 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
15207 return false;
15209 init = t;
15210 gcc_assert (!DECL_P (init));
15212 rtl = rtl_for_decl_init (init, type);
15213 if (rtl)
15214 return add_const_value_attribute (die, rtl);
15215 /* If the host and target are sane, try harder. */
15216 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
15217 && initializer_constant_valid_p (init, type))
15219 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
15220 if (size > 0 && (int) size == size)
15222 unsigned char *array = (unsigned char *)
15223 ggc_alloc_cleared_atomic (size);
15225 if (native_encode_initializer (init, array, size))
15227 add_AT_vec (die, DW_AT_const_value, size, 1, array);
15228 return true;
15232 return false;
15235 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
15236 attribute is the const value of T, where T is an integral constant
15237 variable with static storage duration
15238 (so it can't be a PARM_DECL or a RESULT_DECL). */
15240 static bool
15241 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
15244 if (!decl
15245 || (TREE_CODE (decl) != VAR_DECL
15246 && TREE_CODE (decl) != CONST_DECL)
15247 || (TREE_CODE (decl) == VAR_DECL
15248 && !TREE_STATIC (decl)))
15249 return false;
15251 if (TREE_READONLY (decl)
15252 && ! TREE_THIS_VOLATILE (decl)
15253 && DECL_INITIAL (decl))
15254 /* OK */;
15255 else
15256 return false;
15258 /* Don't add DW_AT_const_value if abstract origin already has one. */
15259 if (get_AT (var_die, DW_AT_const_value))
15260 return false;
15262 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
15265 /* Convert the CFI instructions for the current function into a
15266 location list. This is used for DW_AT_frame_base when we targeting
15267 a dwarf2 consumer that does not support the dwarf3
15268 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
15269 expressions. */
15271 static dw_loc_list_ref
15272 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
15274 int ix;
15275 dw_fde_ref fde;
15276 dw_loc_list_ref list, *list_tail;
15277 dw_cfi_ref cfi;
15278 dw_cfa_location last_cfa, next_cfa;
15279 const char *start_label, *last_label, *section;
15280 dw_cfa_location remember;
15282 fde = cfun->fde;
15283 gcc_assert (fde != NULL);
15285 section = secname_for_decl (current_function_decl);
15286 list_tail = &list;
15287 list = NULL;
15289 memset (&next_cfa, 0, sizeof (next_cfa));
15290 next_cfa.reg = INVALID_REGNUM;
15291 remember = next_cfa;
15293 start_label = fde->dw_fde_begin;
15295 /* ??? Bald assumption that the CIE opcode list does not contain
15296 advance opcodes. */
15297 FOR_EACH_VEC_ELT (dw_cfi_ref, cie_cfi_vec, ix, cfi)
15298 lookup_cfa_1 (cfi, &next_cfa, &remember);
15300 last_cfa = next_cfa;
15301 last_label = start_label;
15303 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
15305 /* If the first partition contained no CFI adjustments, the
15306 CIE opcodes apply to the whole first partition. */
15307 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15308 fde->dw_fde_begin, fde->dw_fde_end, section);
15309 list_tail =&(*list_tail)->dw_loc_next;
15310 start_label = last_label = fde->dw_fde_second_begin;
15313 FOR_EACH_VEC_ELT (dw_cfi_ref, fde->dw_fde_cfi, ix, cfi)
15315 switch (cfi->dw_cfi_opc)
15317 case DW_CFA_set_loc:
15318 case DW_CFA_advance_loc1:
15319 case DW_CFA_advance_loc2:
15320 case DW_CFA_advance_loc4:
15321 if (!cfa_equal_p (&last_cfa, &next_cfa))
15323 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15324 start_label, last_label, section);
15326 list_tail = &(*list_tail)->dw_loc_next;
15327 last_cfa = next_cfa;
15328 start_label = last_label;
15330 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
15331 break;
15333 case DW_CFA_advance_loc:
15334 /* The encoding is complex enough that we should never emit this. */
15335 gcc_unreachable ();
15337 default:
15338 lookup_cfa_1 (cfi, &next_cfa, &remember);
15339 break;
15341 if (ix + 1 == fde->dw_fde_switch_cfi_index)
15343 if (!cfa_equal_p (&last_cfa, &next_cfa))
15345 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15346 start_label, last_label, section);
15348 list_tail = &(*list_tail)->dw_loc_next;
15349 last_cfa = next_cfa;
15350 start_label = last_label;
15352 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15353 start_label, fde->dw_fde_end, section);
15354 list_tail = &(*list_tail)->dw_loc_next;
15355 start_label = last_label = fde->dw_fde_second_begin;
15359 if (!cfa_equal_p (&last_cfa, &next_cfa))
15361 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15362 start_label, last_label, section);
15363 list_tail = &(*list_tail)->dw_loc_next;
15364 start_label = last_label;
15367 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
15368 start_label,
15369 fde->dw_fde_second_begin
15370 ? fde->dw_fde_second_end : fde->dw_fde_end,
15371 section);
15373 if (list && list->dw_loc_next)
15374 gen_llsym (list);
15376 return list;
15379 /* Compute a displacement from the "steady-state frame pointer" to the
15380 frame base (often the same as the CFA), and store it in
15381 frame_pointer_fb_offset. OFFSET is added to the displacement
15382 before the latter is negated. */
15384 static void
15385 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
15387 rtx reg, elim;
15389 #ifdef FRAME_POINTER_CFA_OFFSET
15390 reg = frame_pointer_rtx;
15391 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
15392 #else
15393 reg = arg_pointer_rtx;
15394 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
15395 #endif
15397 elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
15398 if (GET_CODE (elim) == PLUS)
15400 offset += INTVAL (XEXP (elim, 1));
15401 elim = XEXP (elim, 0);
15404 frame_pointer_fb_offset = -offset;
15406 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
15407 in which to eliminate. This is because it's stack pointer isn't
15408 directly accessible as a register within the ISA. To work around
15409 this, assume that while we cannot provide a proper value for
15410 frame_pointer_fb_offset, we won't need one either. */
15411 frame_pointer_fb_offset_valid
15412 = ((SUPPORTS_STACK_ALIGNMENT
15413 && (elim == hard_frame_pointer_rtx
15414 || elim == stack_pointer_rtx))
15415 || elim == (frame_pointer_needed
15416 ? hard_frame_pointer_rtx
15417 : stack_pointer_rtx));
15420 /* Generate a DW_AT_name attribute given some string value to be included as
15421 the value of the attribute. */
15423 static void
15424 add_name_attribute (dw_die_ref die, const char *name_string)
15426 if (name_string != NULL && *name_string != 0)
15428 if (demangle_name_func)
15429 name_string = (*demangle_name_func) (name_string);
15431 add_AT_string (die, DW_AT_name, name_string);
15435 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
15436 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
15437 of TYPE accordingly.
15439 ??? This is a temporary measure until after we're able to generate
15440 regular DWARF for the complex Ada type system. */
15442 static void
15443 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
15444 dw_die_ref context_die)
15446 tree dtype;
15447 dw_die_ref dtype_die;
15449 if (!lang_hooks.types.descriptive_type)
15450 return;
15452 dtype = lang_hooks.types.descriptive_type (type);
15453 if (!dtype)
15454 return;
15456 dtype_die = lookup_type_die (dtype);
15457 if (!dtype_die)
15459 gen_type_die (dtype, context_die);
15460 dtype_die = lookup_type_die (dtype);
15461 gcc_assert (dtype_die);
15464 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
15467 /* Generate a DW_AT_comp_dir attribute for DIE. */
15469 static void
15470 add_comp_dir_attribute (dw_die_ref die)
15472 const char *wd = get_src_pwd ();
15473 char *wd1;
15475 if (wd == NULL)
15476 return;
15478 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
15480 int wdlen;
15482 wdlen = strlen (wd);
15483 wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
15484 strcpy (wd1, wd);
15485 wd1 [wdlen] = DIR_SEPARATOR;
15486 wd1 [wdlen + 1] = 0;
15487 wd = wd1;
15490 add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
15493 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
15494 default. */
15496 static int
15497 lower_bound_default (void)
15499 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
15501 case DW_LANG_C:
15502 case DW_LANG_C89:
15503 case DW_LANG_C99:
15504 case DW_LANG_C_plus_plus:
15505 case DW_LANG_ObjC:
15506 case DW_LANG_ObjC_plus_plus:
15507 case DW_LANG_Java:
15508 return 0;
15509 case DW_LANG_Fortran77:
15510 case DW_LANG_Fortran90:
15511 case DW_LANG_Fortran95:
15512 return 1;
15513 case DW_LANG_UPC:
15514 case DW_LANG_D:
15515 case DW_LANG_Python:
15516 return dwarf_version >= 4 ? 0 : -1;
15517 case DW_LANG_Ada95:
15518 case DW_LANG_Ada83:
15519 case DW_LANG_Cobol74:
15520 case DW_LANG_Cobol85:
15521 case DW_LANG_Pascal83:
15522 case DW_LANG_Modula2:
15523 case DW_LANG_PLI:
15524 return dwarf_version >= 4 ? 1 : -1;
15525 default:
15526 return -1;
15530 /* Given a tree node describing an array bound (either lower or upper) output
15531 a representation for that bound. */
15533 static void
15534 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
15536 switch (TREE_CODE (bound))
15538 case ERROR_MARK:
15539 return;
15541 /* All fixed-bounds are represented by INTEGER_CST nodes. */
15542 case INTEGER_CST:
15544 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
15545 int dflt;
15547 /* Use the default if possible. */
15548 if (bound_attr == DW_AT_lower_bound
15549 && host_integerp (bound, 0)
15550 && (dflt = lower_bound_default ()) != -1
15551 && tree_low_cst (bound, 0) == dflt)
15554 /* Otherwise represent the bound as an unsigned value with the
15555 precision of its type. The precision and signedness of the
15556 type will be necessary to re-interpret it unambiguously. */
15557 else if (prec < HOST_BITS_PER_WIDE_INT)
15559 unsigned HOST_WIDE_INT mask
15560 = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
15561 add_AT_unsigned (subrange_die, bound_attr,
15562 TREE_INT_CST_LOW (bound) & mask);
15564 else if (prec == HOST_BITS_PER_WIDE_INT
15565 || TREE_INT_CST_HIGH (bound) == 0)
15566 add_AT_unsigned (subrange_die, bound_attr,
15567 TREE_INT_CST_LOW (bound));
15568 else
15569 add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
15570 TREE_INT_CST_LOW (bound));
15572 break;
15574 CASE_CONVERT:
15575 case VIEW_CONVERT_EXPR:
15576 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
15577 break;
15579 case SAVE_EXPR:
15580 break;
15582 case VAR_DECL:
15583 case PARM_DECL:
15584 case RESULT_DECL:
15586 dw_die_ref decl_die = lookup_decl_die (bound);
15588 /* ??? Can this happen, or should the variable have been bound
15589 first? Probably it can, since I imagine that we try to create
15590 the types of parameters in the order in which they exist in
15591 the list, and won't have created a forward reference to a
15592 later parameter. */
15593 if (decl_die != NULL)
15595 add_AT_die_ref (subrange_die, bound_attr, decl_die);
15596 break;
15599 /* FALLTHRU */
15601 default:
15603 /* Otherwise try to create a stack operation procedure to
15604 evaluate the value of the array bound. */
15606 dw_die_ref ctx, decl_die;
15607 dw_loc_list_ref list;
15609 list = loc_list_from_tree (bound, 2);
15610 if (list == NULL || single_element_loc_list_p (list))
15612 /* If DW_AT_*bound is not a reference nor constant, it is
15613 a DWARF expression rather than location description.
15614 For that loc_list_from_tree (bound, 0) is needed.
15615 If that fails to give a single element list,
15616 fall back to outputting this as a reference anyway. */
15617 dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
15618 if (list2 && single_element_loc_list_p (list2))
15620 add_AT_loc (subrange_die, bound_attr, list2->expr);
15621 break;
15624 if (list == NULL)
15625 break;
15627 if (current_function_decl == 0)
15628 ctx = comp_unit_die ();
15629 else
15630 ctx = lookup_decl_die (current_function_decl);
15632 decl_die = new_die (DW_TAG_variable, ctx, bound);
15633 add_AT_flag (decl_die, DW_AT_artificial, 1);
15634 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
15635 add_AT_location_description (decl_die, DW_AT_location, list);
15636 add_AT_die_ref (subrange_die, bound_attr, decl_die);
15637 break;
15642 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
15643 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
15644 Note that the block of subscript information for an array type also
15645 includes information about the element type of the given array type. */
15647 static void
15648 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
15650 unsigned dimension_number;
15651 tree lower, upper;
15652 dw_die_ref subrange_die;
15654 for (dimension_number = 0;
15655 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
15656 type = TREE_TYPE (type), dimension_number++)
15658 tree domain = TYPE_DOMAIN (type);
15660 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
15661 break;
15663 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
15664 and (in GNU C only) variable bounds. Handle all three forms
15665 here. */
15666 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
15668 if (use_upc_dwarf2_extensions && TYPE_HAS_THREADS_FACTOR (type))
15670 add_AT_flag (subrange_die, DW_AT_upc_threads_scaled, 1);
15673 if (domain)
15675 /* We have an array type with specified bounds. */
15676 lower = TYPE_MIN_VALUE (domain);
15677 upper = TYPE_MAX_VALUE (domain);
15679 /* Define the index type. */
15680 if (TREE_TYPE (domain))
15682 /* ??? This is probably an Ada unnamed subrange type. Ignore the
15683 TREE_TYPE field. We can't emit debug info for this
15684 because it is an unnamed integral type. */
15685 if (TREE_CODE (domain) == INTEGER_TYPE
15686 && TYPE_NAME (domain) == NULL_TREE
15687 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
15688 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
15690 else
15691 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
15692 type_die);
15695 /* ??? If upper is NULL, the array has unspecified length,
15696 but it does have a lower bound. This happens with Fortran
15697 dimension arr(N:*)
15698 Since the debugger is definitely going to need to know N
15699 to produce useful results, go ahead and output the lower
15700 bound solo, and hope the debugger can cope. */
15702 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
15703 if (upper)
15704 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
15707 /* Otherwise we have an array type with an unspecified length. The
15708 DWARF-2 spec does not say how to handle this; let's just leave out the
15709 bounds. */
15713 static void
15714 add_byte_size_attribute (dw_die_ref die, tree tree_node)
15716 unsigned size;
15718 switch (TREE_CODE (tree_node))
15720 case ERROR_MARK:
15721 size = 0;
15722 break;
15723 case ENUMERAL_TYPE:
15724 case RECORD_TYPE:
15725 case UNION_TYPE:
15726 case QUAL_UNION_TYPE:
15727 size = int_size_in_bytes (tree_node);
15728 break;
15729 case FIELD_DECL:
15730 /* For a data member of a struct or union, the DW_AT_byte_size is
15731 generally given as the number of bytes normally allocated for an
15732 object of the *declared* type of the member itself. This is true
15733 even for bit-fields. */
15734 size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
15735 break;
15736 default:
15737 gcc_unreachable ();
15740 /* Note that `size' might be -1 when we get to this point. If it is, that
15741 indicates that the byte size of the entity in question is variable. We
15742 have no good way of expressing this fact in Dwarf at the present time,
15743 so just let the -1 pass on through. */
15744 add_AT_unsigned (die, DW_AT_byte_size, size);
15747 /* For a FIELD_DECL node which represents a bit-field, output an attribute
15748 which specifies the distance in bits from the highest order bit of the
15749 "containing object" for the bit-field to the highest order bit of the
15750 bit-field itself.
15752 For any given bit-field, the "containing object" is a hypothetical object
15753 (of some integral or enum type) within which the given bit-field lives. The
15754 type of this hypothetical "containing object" is always the same as the
15755 declared type of the individual bit-field itself. The determination of the
15756 exact location of the "containing object" for a bit-field is rather
15757 complicated. It's handled by the `field_byte_offset' function (above).
15759 Note that it is the size (in bytes) of the hypothetical "containing object"
15760 which will be given in the DW_AT_byte_size attribute for this bit-field.
15761 (See `byte_size_attribute' above). */
15763 static inline void
15764 add_bit_offset_attribute (dw_die_ref die, tree decl)
15766 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
15767 tree type = DECL_BIT_FIELD_TYPE (decl);
15768 HOST_WIDE_INT bitpos_int;
15769 HOST_WIDE_INT highest_order_object_bit_offset;
15770 HOST_WIDE_INT highest_order_field_bit_offset;
15771 HOST_WIDE_INT bit_offset;
15773 /* Must be a field and a bit field. */
15774 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
15776 /* We can't yet handle bit-fields whose offsets are variable, so if we
15777 encounter such things, just return without generating any attribute
15778 whatsoever. Likewise for variable or too large size. */
15779 if (! host_integerp (bit_position (decl), 0)
15780 || ! host_integerp (DECL_SIZE (decl), 1))
15781 return;
15783 bitpos_int = int_bit_position (decl);
15785 /* Note that the bit offset is always the distance (in bits) from the
15786 highest-order bit of the "containing object" to the highest-order bit of
15787 the bit-field itself. Since the "high-order end" of any object or field
15788 is different on big-endian and little-endian machines, the computation
15789 below must take account of these differences. */
15790 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
15791 highest_order_field_bit_offset = bitpos_int;
15793 if (! BYTES_BIG_ENDIAN)
15795 highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
15796 highest_order_object_bit_offset += simple_type_size_in_bits (type);
15799 bit_offset
15800 = (! BYTES_BIG_ENDIAN
15801 ? highest_order_object_bit_offset - highest_order_field_bit_offset
15802 : highest_order_field_bit_offset - highest_order_object_bit_offset);
15804 if (bit_offset < 0)
15805 add_AT_int (die, DW_AT_bit_offset, bit_offset);
15806 else
15807 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
15810 /* For a FIELD_DECL node which represents a bit field, output an attribute
15811 which specifies the length in bits of the given field. */
15813 static inline void
15814 add_bit_size_attribute (dw_die_ref die, tree decl)
15816 /* Must be a field and a bit field. */
15817 gcc_assert (TREE_CODE (decl) == FIELD_DECL
15818 && DECL_BIT_FIELD_TYPE (decl));
15820 if (host_integerp (DECL_SIZE (decl), 1))
15821 add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
15824 /* If the compiled language is ANSI C, then add a 'prototyped'
15825 attribute, if arg types are given for the parameters of a function. */
15827 static inline void
15828 add_prototyped_attribute (dw_die_ref die, tree func_type)
15830 if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
15831 && prototype_p (func_type))
15832 add_AT_flag (die, DW_AT_prototyped, 1);
15835 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
15836 by looking in either the type declaration or object declaration
15837 equate table. */
15839 static inline dw_die_ref
15840 add_abstract_origin_attribute (dw_die_ref die, tree origin)
15842 dw_die_ref origin_die = NULL;
15844 if (TREE_CODE (origin) != FUNCTION_DECL)
15846 /* We may have gotten separated from the block for the inlined
15847 function, if we're in an exception handler or some such; make
15848 sure that the abstract function has been written out.
15850 Doing this for nested functions is wrong, however; functions are
15851 distinct units, and our context might not even be inline. */
15852 tree fn = origin;
15854 if (TYPE_P (fn))
15855 fn = TYPE_STUB_DECL (fn);
15857 fn = decl_function_context (fn);
15858 if (fn)
15859 dwarf2out_abstract_function (fn);
15862 if (DECL_P (origin))
15863 origin_die = lookup_decl_die (origin);
15864 else if (TYPE_P (origin))
15865 origin_die = lookup_type_die (origin);
15867 /* XXX: Functions that are never lowered don't always have correct block
15868 trees (in the case of java, they simply have no block tree, in some other
15869 languages). For these functions, there is nothing we can really do to
15870 output correct debug info for inlined functions in all cases. Rather
15871 than die, we'll just produce deficient debug info now, in that we will
15872 have variables without a proper abstract origin. In the future, when all
15873 functions are lowered, we should re-add a gcc_assert (origin_die)
15874 here. */
15876 if (origin_die)
15877 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
15878 return origin_die;
15881 /* We do not currently support the pure_virtual attribute. */
15883 static inline void
15884 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
15886 if (DECL_VINDEX (func_decl))
15888 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
15890 if (host_integerp (DECL_VINDEX (func_decl), 0))
15891 add_AT_loc (die, DW_AT_vtable_elem_location,
15892 new_loc_descr (DW_OP_constu,
15893 tree_low_cst (DECL_VINDEX (func_decl), 0),
15894 0));
15896 /* GNU extension: Record what type this method came from originally. */
15897 if (debug_info_level > DINFO_LEVEL_TERSE
15898 && DECL_CONTEXT (func_decl))
15899 add_AT_die_ref (die, DW_AT_containing_type,
15900 lookup_type_die (DECL_CONTEXT (func_decl)));
15904 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
15905 given decl. This used to be a vendor extension until after DWARF 4
15906 standardized it. */
15908 static void
15909 add_linkage_attr (dw_die_ref die, tree decl)
15911 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
15913 /* Mimic what assemble_name_raw does with a leading '*'. */
15914 if (name[0] == '*')
15915 name = &name[1];
15917 if (dwarf_version >= 4)
15918 add_AT_string (die, DW_AT_linkage_name, name);
15919 else
15920 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
15923 /* Add source coordinate attributes for the given decl. */
15925 static void
15926 add_src_coords_attributes (dw_die_ref die, tree decl)
15928 expanded_location s;
15930 if (DECL_SOURCE_LOCATION (decl) == UNKNOWN_LOCATION)
15931 return;
15932 s = expand_location (DECL_SOURCE_LOCATION (decl));
15933 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
15934 add_AT_unsigned (die, DW_AT_decl_line, s.line);
15937 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
15939 static void
15940 add_linkage_name (dw_die_ref die, tree decl)
15942 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
15943 && TREE_PUBLIC (decl)
15944 && !DECL_ABSTRACT (decl)
15945 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
15946 && die->die_tag != DW_TAG_member)
15948 /* Defer until we have an assembler name set. */
15949 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
15951 limbo_die_node *asm_name;
15953 asm_name = ggc_alloc_cleared_limbo_die_node ();
15954 asm_name->die = die;
15955 asm_name->created_for = decl;
15956 asm_name->next = deferred_asm_name;
15957 deferred_asm_name = asm_name;
15959 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
15960 add_linkage_attr (die, decl);
15964 /* Add a DW_AT_name attribute and source coordinate attribute for the
15965 given decl, but only if it actually has a name. */
15967 static void
15968 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
15970 tree decl_name;
15972 decl_name = DECL_NAME (decl);
15973 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
15975 const char *name = dwarf2_name (decl, 0);
15976 if (name)
15977 add_name_attribute (die, name);
15978 if (! DECL_ARTIFICIAL (decl))
15979 add_src_coords_attributes (die, decl);
15981 add_linkage_name (die, decl);
15984 #ifdef VMS_DEBUGGING_INFO
15985 /* Get the function's name, as described by its RTL. This may be different
15986 from the DECL_NAME name used in the source file. */
15987 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
15989 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
15990 XEXP (DECL_RTL (decl), 0));
15991 VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
15993 #endif /* VMS_DEBUGGING_INFO */
15996 #ifdef VMS_DEBUGGING_INFO
15997 /* Output the debug main pointer die for VMS */
15999 void
16000 dwarf2out_vms_debug_main_pointer (void)
16002 char label[MAX_ARTIFICIAL_LABEL_BYTES];
16003 dw_die_ref die;
16005 /* Allocate the VMS debug main subprogram die. */
16006 die = ggc_alloc_cleared_die_node ();
16007 die->die_tag = DW_TAG_subprogram;
16008 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
16009 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
16010 current_function_funcdef_no);
16011 add_AT_lbl_id (die, DW_AT_entry_pc, label);
16013 /* Make it the first child of comp_unit_die (). */
16014 die->die_parent = comp_unit_die ();
16015 if (comp_unit_die ()->die_child)
16017 die->die_sib = comp_unit_die ()->die_child->die_sib;
16018 comp_unit_die ()->die_child->die_sib = die;
16020 else
16022 die->die_sib = die;
16023 comp_unit_die ()->die_child = die;
16026 #endif /* VMS_DEBUGGING_INFO */
16028 /* Push a new declaration scope. */
16030 static void
16031 push_decl_scope (tree scope)
16033 VEC_safe_push (tree, gc, decl_scope_table, scope);
16036 /* Pop a declaration scope. */
16038 static inline void
16039 pop_decl_scope (void)
16041 VEC_pop (tree, decl_scope_table);
16044 /* Return the DIE for the scope that immediately contains this type.
16045 Non-named types get global scope. Named types nested in other
16046 types get their containing scope if it's open, or global scope
16047 otherwise. All other types (i.e. function-local named types) get
16048 the current active scope. */
16050 static dw_die_ref
16051 scope_die_for (tree t, dw_die_ref context_die)
16053 dw_die_ref scope_die = NULL;
16054 tree containing_scope;
16055 int i;
16057 /* Non-types always go in the current scope. */
16058 gcc_assert (TYPE_P (t));
16060 containing_scope = TYPE_CONTEXT (t);
16062 /* Use the containing namespace if it was passed in (for a declaration). */
16063 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
16065 if (context_die == lookup_decl_die (containing_scope))
16066 /* OK */;
16067 else
16068 containing_scope = NULL_TREE;
16071 /* Ignore function type "scopes" from the C frontend. They mean that
16072 a tagged type is local to a parmlist of a function declarator, but
16073 that isn't useful to DWARF. */
16074 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
16075 containing_scope = NULL_TREE;
16077 if (SCOPE_FILE_SCOPE_P (containing_scope))
16078 scope_die = comp_unit_die ();
16079 else if (TYPE_P (containing_scope))
16081 /* For types, we can just look up the appropriate DIE. But
16082 first we check to see if we're in the middle of emitting it
16083 so we know where the new DIE should go. */
16084 for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
16085 if (VEC_index (tree, decl_scope_table, i) == containing_scope)
16086 break;
16088 if (i < 0)
16090 gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
16091 || TREE_ASM_WRITTEN (containing_scope));
16092 /*We are not in the middle of emitting the type
16093 CONTAINING_SCOPE. Let's see if it's emitted already. */
16094 scope_die = lookup_type_die (containing_scope);
16096 /* If none of the current dies are suitable, we get file scope. */
16097 if (scope_die == NULL)
16098 scope_die = comp_unit_die ();
16100 else
16101 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
16103 else
16104 scope_die = context_die;
16106 return scope_die;
16109 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
16111 static inline int
16112 local_scope_p (dw_die_ref context_die)
16114 for (; context_die; context_die = context_die->die_parent)
16115 if (context_die->die_tag == DW_TAG_inlined_subroutine
16116 || context_die->die_tag == DW_TAG_subprogram)
16117 return 1;
16119 return 0;
16122 /* Returns nonzero if CONTEXT_DIE is a class. */
16124 static inline int
16125 class_scope_p (dw_die_ref context_die)
16127 return (context_die
16128 && (context_die->die_tag == DW_TAG_structure_type
16129 || context_die->die_tag == DW_TAG_class_type
16130 || context_die->die_tag == DW_TAG_interface_type
16131 || context_die->die_tag == DW_TAG_union_type));
16134 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
16135 whether or not to treat a DIE in this context as a declaration. */
16137 static inline int
16138 class_or_namespace_scope_p (dw_die_ref context_die)
16140 return (class_scope_p (context_die)
16141 || (context_die && context_die->die_tag == DW_TAG_namespace));
16144 /* Many forms of DIEs require a "type description" attribute. This
16145 routine locates the proper "type descriptor" die for the type given
16146 by 'type', and adds a DW_AT_type attribute below the given die. */
16148 static void
16149 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
16150 int decl_volatile, dw_die_ref context_die)
16152 enum tree_code code = TREE_CODE (type);
16153 dw_die_ref type_die = NULL;
16154 int type_quals;
16156 /* ??? If this type is an unnamed subrange type of an integral, floating-point
16157 or fixed-point type, use the inner type. This is because we have no
16158 support for unnamed types in base_type_die. This can happen if this is
16159 an Ada subrange type. Correct solution is emit a subrange type die. */
16160 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
16161 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
16162 type = TREE_TYPE (type), code = TREE_CODE (type);
16164 if (code == ERROR_MARK
16165 /* Handle a special case. For functions whose return type is void, we
16166 generate *no* type attribute. (Note that no object may have type
16167 `void', so this only applies to function return types). */
16168 || code == VOID_TYPE)
16169 return;
16171 type_quals = TYPE_QUALS (type)
16172 | (decl_const * TYPE_QUAL_CONST)
16173 | (decl_volatile * TYPE_QUAL_VOLATILE);
16174 type_die = modified_type_die (type, type_quals, context_die);
16176 if (type_die != NULL)
16177 add_AT_die_ref (object_die, DW_AT_type, type_die);
16180 /* Given an object die, add the calling convention attribute for the
16181 function call type. */
16182 static void
16183 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
16185 enum dwarf_calling_convention value = DW_CC_normal;
16187 value = ((enum dwarf_calling_convention)
16188 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
16190 if (is_fortran ()
16191 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
16193 /* DWARF 2 doesn't provide a way to identify a program's source-level
16194 entry point. DW_AT_calling_convention attributes are only meant
16195 to describe functions' calling conventions. However, lacking a
16196 better way to signal the Fortran main program, we used this for
16197 a long time, following existing custom. Now, DWARF 4 has
16198 DW_AT_main_subprogram, which we add below, but some tools still
16199 rely on the old way, which we thus keep. */
16200 value = DW_CC_program;
16202 if (dwarf_version >= 4 || !dwarf_strict)
16203 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
16206 /* Only add the attribute if the backend requests it, and
16207 is not DW_CC_normal. */
16208 if (value && (value != DW_CC_normal))
16209 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
16212 /* Given a tree pointer to a struct, class, union, or enum type node, return
16213 a pointer to the (string) tag name for the given type, or zero if the type
16214 was declared without a tag. */
16216 static const char *
16217 type_tag (const_tree type)
16219 const char *name = 0;
16221 if (TYPE_NAME (type) != 0)
16223 tree t = 0;
16225 /* Find the IDENTIFIER_NODE for the type name. */
16226 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
16227 && !TYPE_NAMELESS (type))
16228 t = TYPE_NAME (type);
16230 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
16231 a TYPE_DECL node, regardless of whether or not a `typedef' was
16232 involved. */
16233 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
16234 && ! DECL_IGNORED_P (TYPE_NAME (type)))
16236 /* We want to be extra verbose. Don't call dwarf_name if
16237 DECL_NAME isn't set. The default hook for decl_printable_name
16238 doesn't like that, and in this context it's correct to return
16239 0, instead of "<anonymous>" or the like. */
16240 if (DECL_NAME (TYPE_NAME (type))
16241 && !DECL_NAMELESS (TYPE_NAME (type)))
16242 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
16245 /* Now get the name as a string, or invent one. */
16246 if (!name && t != 0)
16247 name = IDENTIFIER_POINTER (t);
16250 return (name == 0 || *name == '\0') ? 0 : name;
16253 /* Return the type associated with a data member, make a special check
16254 for bit field types. */
16256 static inline tree
16257 member_declared_type (const_tree member)
16259 return (DECL_BIT_FIELD_TYPE (member)
16260 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
16263 /* Get the decl's label, as described by its RTL. This may be different
16264 from the DECL_NAME name used in the source file. */
16266 #if 0
16267 static const char *
16268 decl_start_label (tree decl)
16270 rtx x;
16271 const char *fnname;
16273 x = DECL_RTL (decl);
16274 gcc_assert (MEM_P (x));
16276 x = XEXP (x, 0);
16277 gcc_assert (GET_CODE (x) == SYMBOL_REF);
16279 fnname = XSTR (x, 0);
16280 return fnname;
16282 #endif
16284 /* These routines generate the internal representation of the DIE's for
16285 the compilation unit. Debugging information is collected by walking
16286 the declaration trees passed in from dwarf2out_decl(). */
16288 static void
16289 gen_array_type_die (tree type, dw_die_ref context_die)
16291 dw_die_ref scope_die = scope_die_for (type, context_die);
16292 dw_die_ref array_die;
16294 /* GNU compilers represent multidimensional array types as sequences of one
16295 dimensional array types whose element types are themselves array types.
16296 We sometimes squish that down to a single array_type DIE with multiple
16297 subscripts in the Dwarf debugging info. The draft Dwarf specification
16298 say that we are allowed to do this kind of compression in C, because
16299 there is no difference between an array of arrays and a multidimensional
16300 array. We don't do this for Ada to remain as close as possible to the
16301 actual representation, which is especially important against the language
16302 flexibilty wrt arrays of variable size. */
16304 bool collapse_nested_arrays = !is_ada ();
16305 tree element_type;
16307 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
16308 DW_TAG_string_type doesn't have DW_AT_type attribute). */
16309 if (TYPE_STRING_FLAG (type)
16310 && TREE_CODE (type) == ARRAY_TYPE
16311 && is_fortran ()
16312 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
16314 HOST_WIDE_INT size;
16316 array_die = new_die (DW_TAG_string_type, scope_die, type);
16317 add_name_attribute (array_die, type_tag (type));
16318 equate_type_number_to_die (type, array_die);
16319 size = int_size_in_bytes (type);
16320 if (size >= 0)
16321 add_AT_unsigned (array_die, DW_AT_byte_size, size);
16322 else if (TYPE_DOMAIN (type) != NULL_TREE
16323 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
16324 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
16326 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
16327 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
16329 size = int_size_in_bytes (TREE_TYPE (szdecl));
16330 if (loc && size > 0)
16332 add_AT_location_description (array_die, DW_AT_string_length, loc);
16333 if (size != DWARF2_ADDR_SIZE)
16334 add_AT_unsigned (array_die, DW_AT_byte_size, size);
16337 return;
16340 /* ??? The SGI dwarf reader fails for array of array of enum types
16341 (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
16342 array type comes before the outer array type. We thus call gen_type_die
16343 before we new_die and must prevent nested array types collapsing for this
16344 target. */
16346 #ifdef MIPS_DEBUGGING_INFO
16347 gen_type_die (TREE_TYPE (type), context_die);
16348 collapse_nested_arrays = false;
16349 #endif
16351 array_die = new_die (DW_TAG_array_type, scope_die, type);
16352 add_name_attribute (array_die, type_tag (type));
16353 add_gnat_descriptive_type_attribute (array_die, type, context_die);
16354 if (TYPE_ARTIFICIAL (type))
16355 add_AT_flag (array_die, DW_AT_artificial, 1);
16356 equate_type_number_to_die (type, array_die);
16358 if (TREE_CODE (type) == VECTOR_TYPE)
16359 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
16361 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
16362 if (is_fortran ()
16363 && TREE_CODE (type) == ARRAY_TYPE
16364 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
16365 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
16366 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16368 #if 0
16369 /* We default the array ordering. SDB will probably do
16370 the right things even if DW_AT_ordering is not present. It's not even
16371 an issue until we start to get into multidimensional arrays anyway. If
16372 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
16373 then we'll have to put the DW_AT_ordering attribute back in. (But if
16374 and when we find out that we need to put these in, we will only do so
16375 for multidimensional arrays. */
16376 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
16377 #endif
16379 #ifdef MIPS_DEBUGGING_INFO
16380 /* The SGI compilers handle arrays of unknown bound by setting
16381 AT_declaration and not emitting any subrange DIEs. */
16382 if (TREE_CODE (type) == ARRAY_TYPE
16383 && ! TYPE_DOMAIN (type))
16384 add_AT_flag (array_die, DW_AT_declaration, 1);
16385 else
16386 #endif
16387 if (TREE_CODE (type) == VECTOR_TYPE)
16389 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
16390 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
16391 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
16392 add_bound_info (subrange_die, DW_AT_upper_bound,
16393 size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
16395 else
16396 add_subscript_info (array_die, type, collapse_nested_arrays);
16398 /* Add representation of the type of the elements of this array type and
16399 emit the corresponding DIE if we haven't done it already. */
16400 element_type = TREE_TYPE (type);
16401 if (collapse_nested_arrays)
16402 while (TREE_CODE (element_type) == ARRAY_TYPE)
16404 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
16405 break;
16406 element_type = TREE_TYPE (element_type);
16409 #ifndef MIPS_DEBUGGING_INFO
16410 gen_type_die (element_type, context_die);
16411 #endif
16413 add_type_attribute (array_die, element_type, 0, 0, context_die);
16415 if (get_AT (array_die, DW_AT_name))
16416 add_pubtype (type, array_die);
16419 static dw_loc_descr_ref
16420 descr_info_loc (tree val, tree base_decl)
16422 HOST_WIDE_INT size;
16423 dw_loc_descr_ref loc, loc2;
16424 enum dwarf_location_atom op;
16426 if (val == base_decl)
16427 return new_loc_descr (DW_OP_push_object_address, 0, 0);
16429 switch (TREE_CODE (val))
16431 CASE_CONVERT:
16432 return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16433 case VAR_DECL:
16434 return loc_descriptor_from_tree (val, 0);
16435 case INTEGER_CST:
16436 if (host_integerp (val, 0))
16437 return int_loc_descriptor (tree_low_cst (val, 0));
16438 break;
16439 case INDIRECT_REF:
16440 size = int_size_in_bytes (TREE_TYPE (val));
16441 if (size < 0)
16442 break;
16443 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16444 if (!loc)
16445 break;
16446 if (size == DWARF2_ADDR_SIZE)
16447 add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
16448 else
16449 add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
16450 return loc;
16451 case POINTER_PLUS_EXPR:
16452 case PLUS_EXPR:
16453 if (host_integerp (TREE_OPERAND (val, 1), 1)
16454 && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
16455 < 16384)
16457 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16458 if (!loc)
16459 break;
16460 loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
16462 else
16464 op = DW_OP_plus;
16465 do_binop:
16466 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16467 if (!loc)
16468 break;
16469 loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
16470 if (!loc2)
16471 break;
16472 add_loc_descr (&loc, loc2);
16473 add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
16475 return loc;
16476 case MINUS_EXPR:
16477 op = DW_OP_minus;
16478 goto do_binop;
16479 case MULT_EXPR:
16480 op = DW_OP_mul;
16481 goto do_binop;
16482 case EQ_EXPR:
16483 op = DW_OP_eq;
16484 goto do_binop;
16485 case NE_EXPR:
16486 op = DW_OP_ne;
16487 goto do_binop;
16488 default:
16489 break;
16491 return NULL;
16494 static void
16495 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
16496 tree val, tree base_decl)
16498 dw_loc_descr_ref loc;
16500 if (host_integerp (val, 0))
16502 add_AT_unsigned (die, attr, tree_low_cst (val, 0));
16503 return;
16506 loc = descr_info_loc (val, base_decl);
16507 if (!loc)
16508 return;
16510 add_AT_loc (die, attr, loc);
16513 /* This routine generates DIE for array with hidden descriptor, details
16514 are filled into *info by a langhook. */
16516 static void
16517 gen_descr_array_type_die (tree type, struct array_descr_info *info,
16518 dw_die_ref context_die)
16520 dw_die_ref scope_die = scope_die_for (type, context_die);
16521 dw_die_ref array_die;
16522 int dim;
16524 array_die = new_die (DW_TAG_array_type, scope_die, type);
16525 add_name_attribute (array_die, type_tag (type));
16526 equate_type_number_to_die (type, array_die);
16528 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
16529 if (is_fortran ()
16530 && info->ndimensions >= 2)
16531 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16533 if (info->data_location)
16534 add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
16535 info->base_decl);
16536 if (info->associated)
16537 add_descr_info_field (array_die, DW_AT_associated, info->associated,
16538 info->base_decl);
16539 if (info->allocated)
16540 add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
16541 info->base_decl);
16543 for (dim = 0; dim < info->ndimensions; dim++)
16545 dw_die_ref subrange_die
16546 = new_die (DW_TAG_subrange_type, array_die, NULL);
16548 if (info->dimen[dim].lower_bound)
16550 /* If it is the default value, omit it. */
16551 int dflt;
16553 if (host_integerp (info->dimen[dim].lower_bound, 0)
16554 && (dflt = lower_bound_default ()) != -1
16555 && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
16557 else
16558 add_descr_info_field (subrange_die, DW_AT_lower_bound,
16559 info->dimen[dim].lower_bound,
16560 info->base_decl);
16562 if (info->dimen[dim].upper_bound)
16563 add_descr_info_field (subrange_die, DW_AT_upper_bound,
16564 info->dimen[dim].upper_bound,
16565 info->base_decl);
16566 if (info->dimen[dim].stride)
16567 add_descr_info_field (subrange_die, DW_AT_byte_stride,
16568 info->dimen[dim].stride,
16569 info->base_decl);
16572 gen_type_die (info->element_type, context_die);
16573 add_type_attribute (array_die, info->element_type, 0, 0, context_die);
16575 if (get_AT (array_die, DW_AT_name))
16576 add_pubtype (type, array_die);
16579 #if 0
16580 static void
16581 gen_entry_point_die (tree decl, dw_die_ref context_die)
16583 tree origin = decl_ultimate_origin (decl);
16584 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
16586 if (origin != NULL)
16587 add_abstract_origin_attribute (decl_die, origin);
16588 else
16590 add_name_and_src_coords_attributes (decl_die, decl);
16591 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
16592 0, 0, context_die);
16595 if (DECL_ABSTRACT (decl))
16596 equate_decl_number_to_die (decl, decl_die);
16597 else
16598 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
16600 #endif
16602 /* Walk through the list of incomplete types again, trying once more to
16603 emit full debugging info for them. */
16605 static void
16606 retry_incomplete_types (void)
16608 int i;
16610 for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
16611 if (should_emit_struct_debug (VEC_index (tree, incomplete_types, i),
16612 DINFO_USAGE_DIR_USE))
16613 gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die ());
16616 /* Determine what tag to use for a record type. */
16618 static enum dwarf_tag
16619 record_type_tag (tree type)
16621 if (! lang_hooks.types.classify_record)
16622 return DW_TAG_structure_type;
16624 switch (lang_hooks.types.classify_record (type))
16626 case RECORD_IS_STRUCT:
16627 return DW_TAG_structure_type;
16629 case RECORD_IS_CLASS:
16630 return DW_TAG_class_type;
16632 case RECORD_IS_INTERFACE:
16633 if (dwarf_version >= 3 || !dwarf_strict)
16634 return DW_TAG_interface_type;
16635 return DW_TAG_structure_type;
16637 default:
16638 gcc_unreachable ();
16642 /* Generate a DIE to represent an enumeration type. Note that these DIEs
16643 include all of the information about the enumeration values also. Each
16644 enumerated type name/value is listed as a child of the enumerated type
16645 DIE. */
16647 static dw_die_ref
16648 gen_enumeration_type_die (tree type, dw_die_ref context_die)
16650 dw_die_ref type_die = lookup_type_die (type);
16652 if (type_die == NULL)
16654 type_die = new_die (DW_TAG_enumeration_type,
16655 scope_die_for (type, context_die), type);
16656 equate_type_number_to_die (type, type_die);
16657 add_name_attribute (type_die, type_tag (type));
16658 add_gnat_descriptive_type_attribute (type_die, type, context_die);
16659 if (TYPE_ARTIFICIAL (type))
16660 add_AT_flag (type_die, DW_AT_artificial, 1);
16661 if (dwarf_version >= 4 || !dwarf_strict)
16663 if (ENUM_IS_SCOPED (type))
16664 add_AT_flag (type_die, DW_AT_enum_class, 1);
16665 if (ENUM_IS_OPAQUE (type))
16666 add_AT_flag (type_die, DW_AT_declaration, 1);
16669 else if (! TYPE_SIZE (type))
16670 return type_die;
16671 else
16672 remove_AT (type_die, DW_AT_declaration);
16674 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
16675 given enum type is incomplete, do not generate the DW_AT_byte_size
16676 attribute or the DW_AT_element_list attribute. */
16677 if (TYPE_SIZE (type))
16679 tree link;
16681 TREE_ASM_WRITTEN (type) = 1;
16682 add_byte_size_attribute (type_die, type);
16683 if (TYPE_STUB_DECL (type) != NULL_TREE)
16685 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
16686 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
16689 /* If the first reference to this type was as the return type of an
16690 inline function, then it may not have a parent. Fix this now. */
16691 if (type_die->die_parent == NULL)
16692 add_child_die (scope_die_for (type, context_die), type_die);
16694 for (link = TYPE_VALUES (type);
16695 link != NULL; link = TREE_CHAIN (link))
16697 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
16698 tree value = TREE_VALUE (link);
16700 add_name_attribute (enum_die,
16701 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
16703 if (TREE_CODE (value) == CONST_DECL)
16704 value = DECL_INITIAL (value);
16706 if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
16707 /* DWARF2 does not provide a way of indicating whether or
16708 not enumeration constants are signed or unsigned. GDB
16709 always assumes the values are signed, so we output all
16710 values as if they were signed. That means that
16711 enumeration constants with very large unsigned values
16712 will appear to have negative values in the debugger. */
16713 add_AT_int (enum_die, DW_AT_const_value,
16714 tree_low_cst (value, tree_int_cst_sgn (value) > 0));
16717 else
16718 add_AT_flag (type_die, DW_AT_declaration, 1);
16720 if (get_AT (type_die, DW_AT_name))
16721 add_pubtype (type, type_die);
16723 return type_die;
16726 /* Generate a DIE to represent either a real live formal parameter decl or to
16727 represent just the type of some formal parameter position in some function
16728 type.
16730 Note that this routine is a bit unusual because its argument may be a
16731 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
16732 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
16733 node. If it's the former then this function is being called to output a
16734 DIE to represent a formal parameter object (or some inlining thereof). If
16735 it's the latter, then this function is only being called to output a
16736 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
16737 argument type of some subprogram type.
16738 If EMIT_NAME_P is true, name and source coordinate attributes
16739 are emitted. */
16741 static dw_die_ref
16742 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
16743 dw_die_ref context_die)
16745 tree node_or_origin = node ? node : origin;
16746 tree ultimate_origin;
16747 dw_die_ref parm_die
16748 = new_die (DW_TAG_formal_parameter, context_die, node);
16750 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
16752 case tcc_declaration:
16753 ultimate_origin = decl_ultimate_origin (node_or_origin);
16754 if (node || ultimate_origin)
16755 origin = ultimate_origin;
16756 if (origin != NULL)
16757 add_abstract_origin_attribute (parm_die, origin);
16758 else if (emit_name_p)
16759 add_name_and_src_coords_attributes (parm_die, node);
16760 if (origin == NULL
16761 || (! DECL_ABSTRACT (node_or_origin)
16762 && variably_modified_type_p (TREE_TYPE (node_or_origin),
16763 decl_function_context
16764 (node_or_origin))))
16766 tree type = TREE_TYPE (node_or_origin);
16767 if (decl_by_reference_p (node_or_origin))
16768 add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
16769 context_die);
16770 else
16771 add_type_attribute (parm_die, type,
16772 TREE_READONLY (node_or_origin),
16773 TREE_THIS_VOLATILE (node_or_origin),
16774 context_die);
16776 if (origin == NULL && DECL_ARTIFICIAL (node))
16777 add_AT_flag (parm_die, DW_AT_artificial, 1);
16779 if (node && node != origin)
16780 equate_decl_number_to_die (node, parm_die);
16781 if (! DECL_ABSTRACT (node_or_origin))
16782 add_location_or_const_value_attribute (parm_die, node_or_origin,
16783 node == NULL, DW_AT_location);
16785 break;
16787 case tcc_type:
16788 /* We were called with some kind of a ..._TYPE node. */
16789 add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
16790 break;
16792 default:
16793 gcc_unreachable ();
16796 return parm_die;
16799 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
16800 children DW_TAG_formal_parameter DIEs representing the arguments of the
16801 parameter pack.
16803 PARM_PACK must be a function parameter pack.
16804 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
16805 must point to the subsequent arguments of the function PACK_ARG belongs to.
16806 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
16807 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
16808 following the last one for which a DIE was generated. */
16810 static dw_die_ref
16811 gen_formal_parameter_pack_die (tree parm_pack,
16812 tree pack_arg,
16813 dw_die_ref subr_die,
16814 tree *next_arg)
16816 tree arg;
16817 dw_die_ref parm_pack_die;
16819 gcc_assert (parm_pack
16820 && lang_hooks.function_parameter_pack_p (parm_pack)
16821 && subr_die);
16823 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
16824 add_src_coords_attributes (parm_pack_die, parm_pack);
16826 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
16828 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
16829 parm_pack))
16830 break;
16831 gen_formal_parameter_die (arg, NULL,
16832 false /* Don't emit name attribute. */,
16833 parm_pack_die);
16835 if (next_arg)
16836 *next_arg = arg;
16837 return parm_pack_die;
16840 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
16841 at the end of an (ANSI prototyped) formal parameters list. */
16843 static void
16844 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
16846 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
16849 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
16850 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
16851 parameters as specified in some function type specification (except for
16852 those which appear as part of a function *definition*). */
16854 static void
16855 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
16857 tree link;
16858 tree formal_type = NULL;
16859 tree first_parm_type;
16860 tree arg;
16862 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
16864 arg = DECL_ARGUMENTS (function_or_method_type);
16865 function_or_method_type = TREE_TYPE (function_or_method_type);
16867 else
16868 arg = NULL_TREE;
16870 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
16872 /* Make our first pass over the list of formal parameter types and output a
16873 DW_TAG_formal_parameter DIE for each one. */
16874 for (link = first_parm_type; link; )
16876 dw_die_ref parm_die;
16878 formal_type = TREE_VALUE (link);
16879 if (formal_type == void_type_node)
16880 break;
16882 /* Output a (nameless) DIE to represent the formal parameter itself. */
16883 parm_die = gen_formal_parameter_die (formal_type, NULL,
16884 true /* Emit name attribute. */,
16885 context_die);
16886 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
16887 && link == first_parm_type)
16889 add_AT_flag (parm_die, DW_AT_artificial, 1);
16890 if (dwarf_version >= 3 || !dwarf_strict)
16891 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
16893 else if (arg && DECL_ARTIFICIAL (arg))
16894 add_AT_flag (parm_die, DW_AT_artificial, 1);
16896 link = TREE_CHAIN (link);
16897 if (arg)
16898 arg = DECL_CHAIN (arg);
16901 /* If this function type has an ellipsis, add a
16902 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
16903 if (formal_type != void_type_node)
16904 gen_unspecified_parameters_die (function_or_method_type, context_die);
16906 /* Make our second (and final) pass over the list of formal parameter types
16907 and output DIEs to represent those types (as necessary). */
16908 for (link = TYPE_ARG_TYPES (function_or_method_type);
16909 link && TREE_VALUE (link);
16910 link = TREE_CHAIN (link))
16911 gen_type_die (TREE_VALUE (link), context_die);
16914 /* We want to generate the DIE for TYPE so that we can generate the
16915 die for MEMBER, which has been defined; we will need to refer back
16916 to the member declaration nested within TYPE. If we're trying to
16917 generate minimal debug info for TYPE, processing TYPE won't do the
16918 trick; we need to attach the member declaration by hand. */
16920 static void
16921 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
16923 gen_type_die (type, context_die);
16925 /* If we're trying to avoid duplicate debug info, we may not have
16926 emitted the member decl for this function. Emit it now. */
16927 if (TYPE_STUB_DECL (type)
16928 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
16929 && ! lookup_decl_die (member))
16931 dw_die_ref type_die;
16932 gcc_assert (!decl_ultimate_origin (member));
16934 push_decl_scope (type);
16935 type_die = lookup_type_die_strip_naming_typedef (type);
16936 if (TREE_CODE (member) == FUNCTION_DECL)
16937 gen_subprogram_die (member, type_die);
16938 else if (TREE_CODE (member) == FIELD_DECL)
16940 /* Ignore the nameless fields that are used to skip bits but handle
16941 C++ anonymous unions and structs. */
16942 if (DECL_NAME (member) != NULL_TREE
16943 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
16944 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
16946 gen_type_die (member_declared_type (member), type_die);
16947 gen_field_die (member, type_die);
16950 else
16951 gen_variable_die (member, NULL_TREE, type_die);
16953 pop_decl_scope ();
16957 /* Generate the DWARF2 info for the "abstract" instance of a function which we
16958 may later generate inlined and/or out-of-line instances of. */
16960 static void
16961 dwarf2out_abstract_function (tree decl)
16963 dw_die_ref old_die;
16964 tree save_fn;
16965 tree context;
16966 int was_abstract;
16967 htab_t old_decl_loc_table;
16968 htab_t old_cached_dw_loc_list_table;
16969 int old_call_site_count, old_tail_call_site_count;
16970 struct call_arg_loc_node *old_call_arg_locations;
16972 /* Make sure we have the actual abstract inline, not a clone. */
16973 decl = DECL_ORIGIN (decl);
16975 old_die = lookup_decl_die (decl);
16976 if (old_die && get_AT (old_die, DW_AT_inline))
16977 /* We've already generated the abstract instance. */
16978 return;
16980 /* We can be called while recursively when seeing block defining inlined subroutine
16981 DIE. Be sure to not clobber the outer location table nor use it or we would
16982 get locations in abstract instantces. */
16983 old_decl_loc_table = decl_loc_table;
16984 decl_loc_table = NULL;
16985 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
16986 cached_dw_loc_list_table = NULL;
16987 old_call_arg_locations = call_arg_locations;
16988 call_arg_locations = NULL;
16989 old_call_site_count = call_site_count;
16990 call_site_count = -1;
16991 old_tail_call_site_count = tail_call_site_count;
16992 tail_call_site_count = -1;
16994 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
16995 we don't get confused by DECL_ABSTRACT. */
16996 if (debug_info_level > DINFO_LEVEL_TERSE)
16998 context = decl_class_context (decl);
16999 if (context)
17000 gen_type_die_for_member
17001 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
17004 /* Pretend we've just finished compiling this function. */
17005 save_fn = current_function_decl;
17006 current_function_decl = decl;
17007 push_cfun (DECL_STRUCT_FUNCTION (decl));
17009 was_abstract = DECL_ABSTRACT (decl);
17010 set_decl_abstract_flags (decl, 1);
17011 dwarf2out_decl (decl);
17012 if (! was_abstract)
17013 set_decl_abstract_flags (decl, 0);
17015 current_function_decl = save_fn;
17016 decl_loc_table = old_decl_loc_table;
17017 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
17018 call_arg_locations = old_call_arg_locations;
17019 call_site_count = old_call_site_count;
17020 tail_call_site_count = old_tail_call_site_count;
17021 pop_cfun ();
17024 /* Helper function of premark_used_types() which gets called through
17025 htab_traverse.
17027 Marks the DIE of a given type in *SLOT as perennial, so it never gets
17028 marked as unused by prune_unused_types. */
17030 static int
17031 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
17033 tree type;
17034 dw_die_ref die;
17036 type = (tree) *slot;
17037 die = lookup_type_die (type);
17038 if (die != NULL)
17039 die->die_perennial_p = 1;
17040 return 1;
17043 /* Helper function of premark_types_used_by_global_vars which gets called
17044 through htab_traverse.
17046 Marks the DIE of a given type in *SLOT as perennial, so it never gets
17047 marked as unused by prune_unused_types. The DIE of the type is marked
17048 only if the global variable using the type will actually be emitted. */
17050 static int
17051 premark_types_used_by_global_vars_helper (void **slot,
17052 void *data ATTRIBUTE_UNUSED)
17054 struct types_used_by_vars_entry *entry;
17055 dw_die_ref die;
17057 entry = (struct types_used_by_vars_entry *) *slot;
17058 gcc_assert (entry->type != NULL
17059 && entry->var_decl != NULL);
17060 die = lookup_type_die (entry->type);
17061 if (die)
17063 /* Ask cgraph if the global variable really is to be emitted.
17064 If yes, then we'll keep the DIE of ENTRY->TYPE. */
17065 struct varpool_node *node = varpool_get_node (entry->var_decl);
17066 if (node && node->needed)
17068 die->die_perennial_p = 1;
17069 /* Keep the parent DIEs as well. */
17070 while ((die = die->die_parent) && die->die_perennial_p == 0)
17071 die->die_perennial_p = 1;
17074 return 1;
17077 /* Mark all members of used_types_hash as perennial. */
17079 static void
17080 premark_used_types (void)
17082 if (cfun && cfun->used_types_hash)
17083 htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
17086 /* Mark all members of types_used_by_vars_entry as perennial. */
17088 static void
17089 premark_types_used_by_global_vars (void)
17091 if (types_used_by_vars_hash)
17092 htab_traverse (types_used_by_vars_hash,
17093 premark_types_used_by_global_vars_helper, NULL);
17096 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
17097 for CA_LOC call arg loc node. */
17099 static dw_die_ref
17100 gen_call_site_die (tree decl, dw_die_ref subr_die,
17101 struct call_arg_loc_node *ca_loc)
17103 dw_die_ref stmt_die = NULL, die;
17104 tree block = ca_loc->block;
17106 while (block
17107 && block != DECL_INITIAL (decl)
17108 && TREE_CODE (block) == BLOCK)
17110 if (VEC_length (dw_die_ref, block_map) > BLOCK_NUMBER (block))
17111 stmt_die = VEC_index (dw_die_ref, block_map, BLOCK_NUMBER (block));
17112 if (stmt_die)
17113 break;
17114 block = BLOCK_SUPERCONTEXT (block);
17116 if (stmt_die == NULL)
17117 stmt_die = subr_die;
17118 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
17119 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
17120 if (ca_loc->tail_call_p)
17121 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
17122 if (ca_loc->symbol_ref)
17124 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
17125 if (tdie)
17126 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
17127 else
17128 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref);
17130 return die;
17133 /* Generate a DIE to represent a declared function (either file-scope or
17134 block-local). */
17136 static void
17137 gen_subprogram_die (tree decl, dw_die_ref context_die)
17139 tree origin = decl_ultimate_origin (decl);
17140 dw_die_ref subr_die;
17141 tree outer_scope;
17142 dw_die_ref old_die = lookup_decl_die (decl);
17143 int declaration = (current_function_decl != decl
17144 || class_or_namespace_scope_p (context_die));
17146 premark_used_types ();
17148 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
17149 started to generate the abstract instance of an inline, decided to output
17150 its containing class, and proceeded to emit the declaration of the inline
17151 from the member list for the class. If so, DECLARATION takes priority;
17152 we'll get back to the abstract instance when done with the class. */
17154 /* The class-scope declaration DIE must be the primary DIE. */
17155 if (origin && declaration && class_or_namespace_scope_p (context_die))
17157 origin = NULL;
17158 gcc_assert (!old_die);
17161 /* Now that the C++ front end lazily declares artificial member fns, we
17162 might need to retrofit the declaration into its class. */
17163 if (!declaration && !origin && !old_die
17164 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
17165 && !class_or_namespace_scope_p (context_die)
17166 && debug_info_level > DINFO_LEVEL_TERSE)
17167 old_die = force_decl_die (decl);
17169 if (origin != NULL)
17171 gcc_assert (!declaration || local_scope_p (context_die));
17173 /* Fixup die_parent for the abstract instance of a nested
17174 inline function. */
17175 if (old_die && old_die->die_parent == NULL)
17176 add_child_die (context_die, old_die);
17178 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17179 add_abstract_origin_attribute (subr_die, origin);
17180 /* This is where the actual code for a cloned function is.
17181 Let's emit linkage name attribute for it. This helps
17182 debuggers to e.g, set breakpoints into
17183 constructors/destructors when the user asks "break
17184 K::K". */
17185 add_linkage_name (subr_die, decl);
17187 else if (old_die)
17189 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17190 struct dwarf_file_data * file_index = lookup_filename (s.file);
17192 if (!get_AT_flag (old_die, DW_AT_declaration)
17193 /* We can have a normal definition following an inline one in the
17194 case of redefinition of GNU C extern inlines.
17195 It seems reasonable to use AT_specification in this case. */
17196 && !get_AT (old_die, DW_AT_inline))
17198 /* Detect and ignore this case, where we are trying to output
17199 something we have already output. */
17200 return;
17203 /* If the definition comes from the same place as the declaration,
17204 maybe use the old DIE. We always want the DIE for this function
17205 that has the *_pc attributes to be under comp_unit_die so the
17206 debugger can find it. We also need to do this for abstract
17207 instances of inlines, since the spec requires the out-of-line copy
17208 to have the same parent. For local class methods, this doesn't
17209 apply; we just use the old DIE. */
17210 if ((is_cu_die (old_die->die_parent) || context_die == NULL)
17211 && (DECL_ARTIFICIAL (decl)
17212 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
17213 && (get_AT_unsigned (old_die, DW_AT_decl_line)
17214 == (unsigned) s.line))))
17216 subr_die = old_die;
17218 /* Clear out the declaration attribute and the formal parameters.
17219 Do not remove all children, because it is possible that this
17220 declaration die was forced using force_decl_die(). In such
17221 cases die that forced declaration die (e.g. TAG_imported_module)
17222 is one of the children that we do not want to remove. */
17223 remove_AT (subr_die, DW_AT_declaration);
17224 remove_AT (subr_die, DW_AT_object_pointer);
17225 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
17227 else
17229 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17230 add_AT_specification (subr_die, old_die);
17231 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
17232 add_AT_file (subr_die, DW_AT_decl_file, file_index);
17233 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
17234 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
17237 else
17239 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17241 if (TREE_PUBLIC (decl))
17242 add_AT_flag (subr_die, DW_AT_external, 1);
17244 add_name_and_src_coords_attributes (subr_die, decl);
17245 if (debug_info_level > DINFO_LEVEL_TERSE)
17247 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
17248 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
17249 0, 0, context_die);
17252 add_pure_or_virtual_attribute (subr_die, decl);
17253 if (DECL_ARTIFICIAL (decl))
17254 add_AT_flag (subr_die, DW_AT_artificial, 1);
17256 add_accessibility_attribute (subr_die, decl);
17259 if (declaration)
17261 if (!old_die || !get_AT (old_die, DW_AT_inline))
17263 add_AT_flag (subr_die, DW_AT_declaration, 1);
17265 /* If this is an explicit function declaration then generate
17266 a DW_AT_explicit attribute. */
17267 if (lang_hooks.decls.function_decl_explicit_p (decl)
17268 && (dwarf_version >= 3 || !dwarf_strict))
17269 add_AT_flag (subr_die, DW_AT_explicit, 1);
17271 /* The first time we see a member function, it is in the context of
17272 the class to which it belongs. We make sure of this by emitting
17273 the class first. The next time is the definition, which is
17274 handled above. The two may come from the same source text.
17276 Note that force_decl_die() forces function declaration die. It is
17277 later reused to represent definition. */
17278 equate_decl_number_to_die (decl, subr_die);
17281 else if (DECL_ABSTRACT (decl))
17283 if (DECL_DECLARED_INLINE_P (decl))
17285 if (cgraph_function_possibly_inlined_p (decl))
17286 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
17287 else
17288 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
17290 else
17292 if (cgraph_function_possibly_inlined_p (decl))
17293 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
17294 else
17295 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
17298 if (DECL_DECLARED_INLINE_P (decl)
17299 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
17300 add_AT_flag (subr_die, DW_AT_artificial, 1);
17302 equate_decl_number_to_die (decl, subr_die);
17304 else if (!DECL_EXTERNAL (decl))
17306 HOST_WIDE_INT cfa_fb_offset;
17308 if (!old_die || !get_AT (old_die, DW_AT_inline))
17309 equate_decl_number_to_die (decl, subr_die);
17311 if (!flag_reorder_blocks_and_partition)
17313 dw_fde_ref fde = cfun->fde;
17314 if (fde->dw_fde_begin)
17316 /* We have already generated the labels. */
17317 add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
17318 add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
17320 else
17322 /* Create start/end labels and add the range. */
17323 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
17324 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
17325 current_function_funcdef_no);
17326 add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
17327 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
17328 current_function_funcdef_no);
17329 add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
17332 #if VMS_DEBUGGING_INFO
17333 /* HP OpenVMS Industry Standard 64: DWARF Extensions
17334 Section 2.3 Prologue and Epilogue Attributes:
17335 When a breakpoint is set on entry to a function, it is generally
17336 desirable for execution to be suspended, not on the very first
17337 instruction of the function, but rather at a point after the
17338 function's frame has been set up, after any language defined local
17339 declaration processing has been completed, and before execution of
17340 the first statement of the function begins. Debuggers generally
17341 cannot properly determine where this point is. Similarly for a
17342 breakpoint set on exit from a function. The prologue and epilogue
17343 attributes allow a compiler to communicate the location(s) to use. */
17346 if (fde->dw_fde_vms_end_prologue)
17347 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
17348 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
17350 if (fde->dw_fde_vms_begin_epilogue)
17351 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
17352 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
17354 #endif
17356 add_pubname (decl, subr_die);
17358 else
17360 /* Generate pubnames entries for the split function code ranges. */
17361 dw_fde_ref fde = cfun->fde;
17363 if (fde->dw_fde_second_begin)
17365 if (dwarf_version >= 3 || !dwarf_strict)
17367 /* We should use ranges for non-contiguous code section
17368 addresses. Use the actual code range for the initial
17369 section, since the HOT/COLD labels might precede an
17370 alignment offset. */
17371 bool range_list_added = false;
17372 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
17373 fde->dw_fde_end, &range_list_added);
17374 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
17375 fde->dw_fde_second_end,
17376 &range_list_added);
17377 add_pubname (decl, subr_die);
17378 if (range_list_added)
17379 add_ranges (NULL);
17381 else
17383 /* There is no real support in DW2 for this .. so we make
17384 a work-around. First, emit the pub name for the segment
17385 containing the function label. Then make and emit a
17386 simplified subprogram DIE for the second segment with the
17387 name pre-fixed by __hot/cold_sect_of_. We use the same
17388 linkage name for the second die so that gdb will find both
17389 sections when given "b foo". */
17390 const char *name = NULL;
17391 tree decl_name = DECL_NAME (decl);
17392 dw_die_ref seg_die;
17394 /* Do the 'primary' section. */
17395 add_AT_lbl_id (subr_die, DW_AT_low_pc,
17396 fde->dw_fde_begin);
17397 add_AT_lbl_id (subr_die, DW_AT_high_pc,
17398 fde->dw_fde_end);
17399 /* Add it. */
17400 add_pubname (decl, subr_die);
17402 /* Build a minimal DIE for the secondary section. */
17403 seg_die = new_die (DW_TAG_subprogram,
17404 subr_die->die_parent, decl);
17406 if (TREE_PUBLIC (decl))
17407 add_AT_flag (seg_die, DW_AT_external, 1);
17409 if (decl_name != NULL
17410 && IDENTIFIER_POINTER (decl_name) != NULL)
17412 name = dwarf2_name (decl, 1);
17413 if (! DECL_ARTIFICIAL (decl))
17414 add_src_coords_attributes (seg_die, decl);
17416 add_linkage_name (seg_die, decl);
17418 gcc_assert (name != NULL);
17419 add_pure_or_virtual_attribute (seg_die, decl);
17420 if (DECL_ARTIFICIAL (decl))
17421 add_AT_flag (seg_die, DW_AT_artificial, 1);
17423 name = concat ("__second_sect_of_", name, NULL);
17424 add_AT_lbl_id (seg_die, DW_AT_low_pc,
17425 fde->dw_fde_second_begin);
17426 add_AT_lbl_id (seg_die, DW_AT_high_pc,
17427 fde->dw_fde_second_end);
17428 add_name_attribute (seg_die, name);
17429 add_pubname_string (name, seg_die);
17432 else
17434 add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
17435 add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
17436 add_pubname (decl, subr_die);
17440 #ifdef MIPS_DEBUGGING_INFO
17441 /* Add a reference to the FDE for this routine. */
17442 add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, cfun->fde->fde_index);
17443 #endif
17445 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
17447 /* We define the "frame base" as the function's CFA. This is more
17448 convenient for several reasons: (1) It's stable across the prologue
17449 and epilogue, which makes it better than just a frame pointer,
17450 (2) With dwarf3, there exists a one-byte encoding that allows us
17451 to reference the .debug_frame data by proxy, but failing that,
17452 (3) We can at least reuse the code inspection and interpretation
17453 code that determines the CFA position at various points in the
17454 function. */
17455 if (dwarf_version >= 3)
17457 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
17458 add_AT_loc (subr_die, DW_AT_frame_base, op);
17460 else
17462 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
17463 if (list->dw_loc_next)
17464 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
17465 else
17466 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
17469 /* Compute a displacement from the "steady-state frame pointer" to
17470 the CFA. The former is what all stack slots and argument slots
17471 will reference in the rtl; the later is what we've told the
17472 debugger about. We'll need to adjust all frame_base references
17473 by this displacement. */
17474 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
17476 if (cfun->static_chain_decl)
17477 add_AT_location_description (subr_die, DW_AT_static_link,
17478 loc_list_from_tree (cfun->static_chain_decl, 2));
17481 /* Generate child dies for template paramaters. */
17482 if (debug_info_level > DINFO_LEVEL_TERSE)
17483 gen_generic_params_dies (decl);
17485 /* Now output descriptions of the arguments for this function. This gets
17486 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
17487 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
17488 `...' at the end of the formal parameter list. In order to find out if
17489 there was a trailing ellipsis or not, we must instead look at the type
17490 associated with the FUNCTION_DECL. This will be a node of type
17491 FUNCTION_TYPE. If the chain of type nodes hanging off of this
17492 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
17493 an ellipsis at the end. */
17495 /* In the case where we are describing a mere function declaration, all we
17496 need to do here (and all we *can* do here) is to describe the *types* of
17497 its formal parameters. */
17498 if (debug_info_level <= DINFO_LEVEL_TERSE)
17500 else if (declaration)
17501 gen_formal_types_die (decl, subr_die);
17502 else
17504 /* Generate DIEs to represent all known formal parameters. */
17505 tree parm = DECL_ARGUMENTS (decl);
17506 tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
17507 tree generic_decl_parm = generic_decl
17508 ? DECL_ARGUMENTS (generic_decl)
17509 : NULL;
17511 /* Now we want to walk the list of parameters of the function and
17512 emit their relevant DIEs.
17514 We consider the case of DECL being an instance of a generic function
17515 as well as it being a normal function.
17517 If DECL is an instance of a generic function we walk the
17518 parameters of the generic function declaration _and_ the parameters of
17519 DECL itself. This is useful because we want to emit specific DIEs for
17520 function parameter packs and those are declared as part of the
17521 generic function declaration. In that particular case,
17522 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
17523 That DIE has children DIEs representing the set of arguments
17524 of the pack. Note that the set of pack arguments can be empty.
17525 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
17526 children DIE.
17528 Otherwise, we just consider the parameters of DECL. */
17529 while (generic_decl_parm || parm)
17531 if (generic_decl_parm
17532 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
17533 gen_formal_parameter_pack_die (generic_decl_parm,
17534 parm, subr_die,
17535 &parm);
17536 else if (parm)
17538 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
17540 if (parm == DECL_ARGUMENTS (decl)
17541 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
17542 && parm_die
17543 && (dwarf_version >= 3 || !dwarf_strict))
17544 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
17546 parm = DECL_CHAIN (parm);
17549 if (generic_decl_parm)
17550 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
17553 /* Decide whether we need an unspecified_parameters DIE at the end.
17554 There are 2 more cases to do this for: 1) the ansi ... declaration -
17555 this is detectable when the end of the arg list is not a
17556 void_type_node 2) an unprototyped function declaration (not a
17557 definition). This just means that we have no info about the
17558 parameters at all. */
17559 if (prototype_p (TREE_TYPE (decl)))
17561 /* This is the prototyped case, check for.... */
17562 if (stdarg_p (TREE_TYPE (decl)))
17563 gen_unspecified_parameters_die (decl, subr_die);
17565 else if (DECL_INITIAL (decl) == NULL_TREE)
17566 gen_unspecified_parameters_die (decl, subr_die);
17569 /* Output Dwarf info for all of the stuff within the body of the function
17570 (if it has one - it may be just a declaration). */
17571 outer_scope = DECL_INITIAL (decl);
17573 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
17574 a function. This BLOCK actually represents the outermost binding contour
17575 for the function, i.e. the contour in which the function's formal
17576 parameters and labels get declared. Curiously, it appears that the front
17577 end doesn't actually put the PARM_DECL nodes for the current function onto
17578 the BLOCK_VARS list for this outer scope, but are strung off of the
17579 DECL_ARGUMENTS list for the function instead.
17581 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
17582 the LABEL_DECL nodes for the function however, and we output DWARF info
17583 for those in decls_for_scope. Just within the `outer_scope' there will be
17584 a BLOCK node representing the function's outermost pair of curly braces,
17585 and any blocks used for the base and member initializers of a C++
17586 constructor function. */
17587 if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
17589 int call_site_note_count = 0;
17590 int tail_call_site_note_count = 0;
17592 /* Emit a DW_TAG_variable DIE for a named return value. */
17593 if (DECL_NAME (DECL_RESULT (decl)))
17594 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
17596 current_function_has_inlines = 0;
17597 decls_for_scope (outer_scope, subr_die, 0);
17599 if (call_arg_locations && !dwarf_strict)
17601 struct call_arg_loc_node *ca_loc;
17602 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
17604 dw_die_ref die = NULL;
17605 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
17606 rtx arg, next_arg;
17608 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
17609 arg; arg = next_arg)
17611 dw_loc_descr_ref reg, val;
17612 enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
17613 dw_die_ref cdie, tdie = NULL;
17615 next_arg = XEXP (arg, 1);
17616 if (REG_P (XEXP (XEXP (arg, 0), 0))
17617 && next_arg
17618 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
17619 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
17620 && REGNO (XEXP (XEXP (arg, 0), 0))
17621 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
17622 next_arg = XEXP (next_arg, 1);
17623 if (mode == VOIDmode)
17625 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
17626 if (mode == VOIDmode)
17627 mode = GET_MODE (XEXP (arg, 0));
17629 if (mode == VOIDmode || mode == BLKmode)
17630 continue;
17631 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
17633 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
17634 tloc = XEXP (XEXP (arg, 0), 1);
17635 continue;
17637 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
17638 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
17640 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
17641 tlocc = XEXP (XEXP (arg, 0), 1);
17642 continue;
17644 reg = NULL;
17645 if (REG_P (XEXP (XEXP (arg, 0), 0)))
17646 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
17647 VAR_INIT_STATUS_INITIALIZED);
17648 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
17650 rtx mem = XEXP (XEXP (arg, 0), 0);
17651 reg = mem_loc_descriptor (XEXP (mem, 0),
17652 get_address_mode (mem),
17653 GET_MODE (mem),
17654 VAR_INIT_STATUS_INITIALIZED);
17656 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
17657 == DEBUG_PARAMETER_REF)
17659 tree tdecl
17660 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
17661 tdie = lookup_decl_die (tdecl);
17662 if (tdie == NULL)
17663 continue;
17665 else
17666 continue;
17667 if (reg == NULL
17668 && GET_CODE (XEXP (XEXP (arg, 0), 0))
17669 != DEBUG_PARAMETER_REF)
17670 continue;
17671 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
17672 VOIDmode,
17673 VAR_INIT_STATUS_INITIALIZED);
17674 if (val == NULL)
17675 continue;
17676 if (die == NULL)
17677 die = gen_call_site_die (decl, subr_die, ca_loc);
17678 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
17679 NULL_TREE);
17680 if (reg != NULL)
17681 add_AT_loc (cdie, DW_AT_location, reg);
17682 else if (tdie != NULL)
17683 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
17684 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
17685 if (next_arg != XEXP (arg, 1))
17687 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
17688 if (mode == VOIDmode)
17689 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
17690 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
17691 0), 1),
17692 mode, VOIDmode,
17693 VAR_INIT_STATUS_INITIALIZED);
17694 if (val != NULL)
17695 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
17698 if (die == NULL
17699 && (ca_loc->symbol_ref || tloc))
17700 die = gen_call_site_die (decl, subr_die, ca_loc);
17701 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
17703 dw_loc_descr_ref tval = NULL;
17705 if (tloc != NULL_RTX)
17706 tval = mem_loc_descriptor (tloc,
17707 GET_MODE (tloc) == VOIDmode
17708 ? Pmode : GET_MODE (tloc),
17709 VOIDmode,
17710 VAR_INIT_STATUS_INITIALIZED);
17711 if (tval)
17712 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
17713 else if (tlocc != NULL_RTX)
17715 tval = mem_loc_descriptor (tlocc,
17716 GET_MODE (tlocc) == VOIDmode
17717 ? Pmode : GET_MODE (tlocc),
17718 VOIDmode,
17719 VAR_INIT_STATUS_INITIALIZED);
17720 if (tval)
17721 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
17722 tval);
17725 if (die != NULL)
17727 call_site_note_count++;
17728 if (ca_loc->tail_call_p)
17729 tail_call_site_note_count++;
17733 call_arg_locations = NULL;
17734 call_arg_loc_last = NULL;
17735 if (tail_call_site_count >= 0
17736 && tail_call_site_count == tail_call_site_note_count
17737 && !dwarf_strict)
17739 if (call_site_count >= 0
17740 && call_site_count == call_site_note_count)
17741 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
17742 else
17743 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
17745 call_site_count = -1;
17746 tail_call_site_count = -1;
17748 /* Add the calling convention attribute if requested. */
17749 add_calling_convention_attribute (subr_die, decl);
17753 /* Returns a hash value for X (which really is a die_struct). */
17755 static hashval_t
17756 common_block_die_table_hash (const void *x)
17758 const_dw_die_ref d = (const_dw_die_ref) x;
17759 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
17762 /* Return nonzero if decl_id and die_parent of die_struct X is the same
17763 as decl_id and die_parent of die_struct Y. */
17765 static int
17766 common_block_die_table_eq (const void *x, const void *y)
17768 const_dw_die_ref d = (const_dw_die_ref) x;
17769 const_dw_die_ref e = (const_dw_die_ref) y;
17770 return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
17773 /* Generate a DIE to represent a declared data object.
17774 Either DECL or ORIGIN must be non-null. */
17776 static void
17777 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
17779 HOST_WIDE_INT off;
17780 tree com_decl;
17781 tree decl_or_origin = decl ? decl : origin;
17782 tree ultimate_origin;
17783 dw_die_ref var_die;
17784 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
17785 dw_die_ref origin_die;
17786 bool declaration = (DECL_EXTERNAL (decl_or_origin)
17787 || class_or_namespace_scope_p (context_die));
17788 bool specialization_p = false;
17790 ultimate_origin = decl_ultimate_origin (decl_or_origin);
17791 if (decl || ultimate_origin)
17792 origin = ultimate_origin;
17793 com_decl = fortran_common (decl_or_origin, &off);
17795 /* Symbol in common gets emitted as a child of the common block, in the form
17796 of a data member. */
17797 if (com_decl)
17799 dw_die_ref com_die;
17800 dw_loc_list_ref loc;
17801 die_node com_die_arg;
17803 var_die = lookup_decl_die (decl_or_origin);
17804 if (var_die)
17806 if (get_AT (var_die, DW_AT_location) == NULL)
17808 loc = loc_list_from_tree (com_decl, off ? 1 : 2);
17809 if (loc)
17811 if (off)
17813 /* Optimize the common case. */
17814 if (single_element_loc_list_p (loc)
17815 && loc->expr->dw_loc_opc == DW_OP_addr
17816 && loc->expr->dw_loc_next == NULL
17817 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
17818 == SYMBOL_REF)
17819 loc->expr->dw_loc_oprnd1.v.val_addr
17820 = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
17821 else
17822 loc_list_plus_const (loc, off);
17824 add_AT_location_description (var_die, DW_AT_location, loc);
17825 remove_AT (var_die, DW_AT_declaration);
17828 return;
17831 if (common_block_die_table == NULL)
17832 common_block_die_table
17833 = htab_create_ggc (10, common_block_die_table_hash,
17834 common_block_die_table_eq, NULL);
17836 com_die_arg.decl_id = DECL_UID (com_decl);
17837 com_die_arg.die_parent = context_die;
17838 com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
17839 loc = loc_list_from_tree (com_decl, 2);
17840 if (com_die == NULL)
17842 const char *cnam
17843 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
17844 void **slot;
17846 com_die = new_die (DW_TAG_common_block, context_die, decl);
17847 add_name_and_src_coords_attributes (com_die, com_decl);
17848 if (loc)
17850 add_AT_location_description (com_die, DW_AT_location, loc);
17851 /* Avoid sharing the same loc descriptor between
17852 DW_TAG_common_block and DW_TAG_variable. */
17853 loc = loc_list_from_tree (com_decl, 2);
17855 else if (DECL_EXTERNAL (decl))
17856 add_AT_flag (com_die, DW_AT_declaration, 1);
17857 add_pubname_string (cnam, com_die); /* ??? needed? */
17858 com_die->decl_id = DECL_UID (com_decl);
17859 slot = htab_find_slot (common_block_die_table, com_die, INSERT);
17860 *slot = (void *) com_die;
17862 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
17864 add_AT_location_description (com_die, DW_AT_location, loc);
17865 loc = loc_list_from_tree (com_decl, 2);
17866 remove_AT (com_die, DW_AT_declaration);
17868 var_die = new_die (DW_TAG_variable, com_die, decl);
17869 add_name_and_src_coords_attributes (var_die, decl);
17870 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
17871 TREE_THIS_VOLATILE (decl), context_die);
17872 add_AT_flag (var_die, DW_AT_external, 1);
17873 if (loc)
17875 if (off)
17877 /* Optimize the common case. */
17878 if (single_element_loc_list_p (loc)
17879 && loc->expr->dw_loc_opc == DW_OP_addr
17880 && loc->expr->dw_loc_next == NULL
17881 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
17882 loc->expr->dw_loc_oprnd1.v.val_addr
17883 = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
17884 else
17885 loc_list_plus_const (loc, off);
17887 add_AT_location_description (var_die, DW_AT_location, loc);
17889 else if (DECL_EXTERNAL (decl))
17890 add_AT_flag (var_die, DW_AT_declaration, 1);
17891 equate_decl_number_to_die (decl, var_die);
17892 return;
17895 /* If the compiler emitted a definition for the DECL declaration
17896 and if we already emitted a DIE for it, don't emit a second
17897 DIE for it again. Allow re-declarations of DECLs that are
17898 inside functions, though. */
17899 if (old_die && declaration && !local_scope_p (context_die))
17900 return;
17902 /* For static data members, the declaration in the class is supposed
17903 to have DW_TAG_member tag; the specification should still be
17904 DW_TAG_variable referencing the DW_TAG_member DIE. */
17905 if (declaration && class_scope_p (context_die))
17906 var_die = new_die (DW_TAG_member, context_die, decl);
17907 else
17908 var_die = new_die (DW_TAG_variable, context_die, decl);
17910 origin_die = NULL;
17911 if (origin != NULL)
17912 origin_die = add_abstract_origin_attribute (var_die, origin);
17914 /* Loop unrolling can create multiple blocks that refer to the same
17915 static variable, so we must test for the DW_AT_declaration flag.
17917 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
17918 copy decls and set the DECL_ABSTRACT flag on them instead of
17919 sharing them.
17921 ??? Duplicated blocks have been rewritten to use .debug_ranges.
17923 ??? The declare_in_namespace support causes us to get two DIEs for one
17924 variable, both of which are declarations. We want to avoid considering
17925 one to be a specification, so we must test that this DIE is not a
17926 declaration. */
17927 else if (old_die && TREE_STATIC (decl) && ! declaration
17928 && get_AT_flag (old_die, DW_AT_declaration) == 1)
17930 /* This is a definition of a C++ class level static. */
17931 add_AT_specification (var_die, old_die);
17932 specialization_p = true;
17933 if (DECL_NAME (decl))
17935 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17936 struct dwarf_file_data * file_index = lookup_filename (s.file);
17938 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
17939 add_AT_file (var_die, DW_AT_decl_file, file_index);
17941 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
17942 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
17944 if (old_die->die_tag == DW_TAG_member)
17945 add_linkage_name (var_die, decl);
17948 else
17949 add_name_and_src_coords_attributes (var_die, decl);
17951 if ((origin == NULL && !specialization_p)
17952 || (origin != NULL
17953 && !DECL_ABSTRACT (decl_or_origin)
17954 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
17955 decl_function_context
17956 (decl_or_origin))))
17958 tree type = TREE_TYPE (decl_or_origin);
17960 if (decl_by_reference_p (decl_or_origin))
17961 add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
17962 else
17963 add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
17964 TREE_THIS_VOLATILE (decl_or_origin), context_die);
17967 if (origin == NULL && !specialization_p)
17969 if (TREE_PUBLIC (decl))
17970 add_AT_flag (var_die, DW_AT_external, 1);
17972 if (DECL_ARTIFICIAL (decl))
17973 add_AT_flag (var_die, DW_AT_artificial, 1);
17975 add_accessibility_attribute (var_die, decl);
17978 if (declaration)
17979 add_AT_flag (var_die, DW_AT_declaration, 1);
17981 if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
17982 equate_decl_number_to_die (decl, var_die);
17984 if (! declaration
17985 && (! DECL_ABSTRACT (decl_or_origin)
17986 /* Local static vars are shared between all clones/inlines,
17987 so emit DW_AT_location on the abstract DIE if DECL_RTL is
17988 already set. */
17989 || (TREE_CODE (decl_or_origin) == VAR_DECL
17990 && TREE_STATIC (decl_or_origin)
17991 && DECL_RTL_SET_P (decl_or_origin)))
17992 /* When abstract origin already has DW_AT_location attribute, no need
17993 to add it again. */
17994 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
17996 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
17997 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
17998 defer_location (decl_or_origin, var_die);
17999 else
18000 add_location_or_const_value_attribute (var_die, decl_or_origin,
18001 decl == NULL, DW_AT_location);
18002 add_pubname (decl_or_origin, var_die);
18004 else
18005 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
18008 /* Generate a DIE to represent a named constant. */
18010 static void
18011 gen_const_die (tree decl, dw_die_ref context_die)
18013 dw_die_ref const_die;
18014 tree type = TREE_TYPE (decl);
18016 const_die = new_die (DW_TAG_constant, context_die, decl);
18017 add_name_and_src_coords_attributes (const_die, decl);
18018 add_type_attribute (const_die, type, 1, 0, context_die);
18019 if (TREE_PUBLIC (decl))
18020 add_AT_flag (const_die, DW_AT_external, 1);
18021 if (DECL_ARTIFICIAL (decl))
18022 add_AT_flag (const_die, DW_AT_artificial, 1);
18023 tree_add_const_value_attribute_for_decl (const_die, decl);
18026 /* Generate a DIE to represent a label identifier. */
18028 static void
18029 gen_label_die (tree decl, dw_die_ref context_die)
18031 tree origin = decl_ultimate_origin (decl);
18032 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
18033 rtx insn;
18034 char label[MAX_ARTIFICIAL_LABEL_BYTES];
18036 if (origin != NULL)
18037 add_abstract_origin_attribute (lbl_die, origin);
18038 else
18039 add_name_and_src_coords_attributes (lbl_die, decl);
18041 if (DECL_ABSTRACT (decl))
18042 equate_decl_number_to_die (decl, lbl_die);
18043 else
18045 insn = DECL_RTL_IF_SET (decl);
18047 /* Deleted labels are programmer specified labels which have been
18048 eliminated because of various optimizations. We still emit them
18049 here so that it is possible to put breakpoints on them. */
18050 if (insn
18051 && (LABEL_P (insn)
18052 || ((NOTE_P (insn)
18053 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
18055 /* When optimization is enabled (via -O) some parts of the compiler
18056 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
18057 represent source-level labels which were explicitly declared by
18058 the user. This really shouldn't be happening though, so catch
18059 it if it ever does happen. */
18060 gcc_assert (!INSN_DELETED_P (insn));
18062 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
18063 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18068 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
18069 attributes to the DIE for a block STMT, to describe where the inlined
18070 function was called from. This is similar to add_src_coords_attributes. */
18072 static inline void
18073 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
18075 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
18077 if (dwarf_version >= 3 || !dwarf_strict)
18079 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
18080 add_AT_unsigned (die, DW_AT_call_line, s.line);
18085 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
18086 Add low_pc and high_pc attributes to the DIE for a block STMT. */
18088 static inline void
18089 add_high_low_attributes (tree stmt, dw_die_ref die)
18091 char label[MAX_ARTIFICIAL_LABEL_BYTES];
18093 if (BLOCK_FRAGMENT_CHAIN (stmt)
18094 && (dwarf_version >= 3 || !dwarf_strict))
18096 tree chain;
18098 if (inlined_function_outer_scope_p (stmt))
18100 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18101 BLOCK_NUMBER (stmt));
18102 add_AT_lbl_id (die, DW_AT_entry_pc, label);
18105 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
18107 chain = BLOCK_FRAGMENT_CHAIN (stmt);
18110 add_ranges (chain);
18111 chain = BLOCK_FRAGMENT_CHAIN (chain);
18113 while (chain);
18114 add_ranges (NULL);
18116 else
18118 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18119 BLOCK_NUMBER (stmt));
18120 add_AT_lbl_id (die, DW_AT_low_pc, label);
18121 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
18122 BLOCK_NUMBER (stmt));
18123 add_AT_lbl_id (die, DW_AT_high_pc, label);
18127 /* Generate a DIE for a lexical block. */
18129 static void
18130 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
18132 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
18134 if (call_arg_locations)
18136 if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
18137 VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
18138 BLOCK_NUMBER (stmt) + 1);
18139 VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), stmt_die);
18142 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
18143 add_high_low_attributes (stmt, stmt_die);
18145 decls_for_scope (stmt, stmt_die, depth);
18148 /* Generate a DIE for an inlined subprogram. */
18150 static void
18151 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
18153 tree decl;
18155 /* The instance of function that is effectively being inlined shall not
18156 be abstract. */
18157 gcc_assert (! BLOCK_ABSTRACT (stmt));
18159 decl = block_ultimate_origin (stmt);
18161 /* Emit info for the abstract instance first, if we haven't yet. We
18162 must emit this even if the block is abstract, otherwise when we
18163 emit the block below (or elsewhere), we may end up trying to emit
18164 a die whose origin die hasn't been emitted, and crashing. */
18165 dwarf2out_abstract_function (decl);
18167 if (! BLOCK_ABSTRACT (stmt))
18169 dw_die_ref subr_die
18170 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
18172 if (call_arg_locations)
18174 if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
18175 VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
18176 BLOCK_NUMBER (stmt) + 1);
18177 VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), subr_die);
18179 add_abstract_origin_attribute (subr_die, decl);
18180 if (TREE_ASM_WRITTEN (stmt))
18181 add_high_low_attributes (stmt, subr_die);
18182 add_call_src_coords_attributes (stmt, subr_die);
18184 decls_for_scope (stmt, subr_die, depth);
18185 current_function_has_inlines = 1;
18189 /* Generate a DIE for a field in a record, or structure. */
18191 static void
18192 gen_field_die (tree decl, dw_die_ref context_die)
18194 dw_die_ref decl_die;
18196 if (TREE_TYPE (decl) == error_mark_node)
18197 return;
18199 decl_die = new_die (DW_TAG_member, context_die, decl);
18200 add_name_and_src_coords_attributes (decl_die, decl);
18201 add_type_attribute (decl_die, member_declared_type (decl), 0, 0, context_die);
18203 if (DECL_BIT_FIELD_TYPE (decl))
18205 add_byte_size_attribute (decl_die, decl);
18206 add_bit_size_attribute (decl_die, decl);
18207 add_bit_offset_attribute (decl_die, decl);
18210 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
18211 add_data_member_location_attribute (decl_die, decl);
18213 if (DECL_ARTIFICIAL (decl))
18214 add_AT_flag (decl_die, DW_AT_artificial, 1);
18216 add_accessibility_attribute (decl_die, decl);
18218 /* Equate decl number to die, so that we can look up this decl later on. */
18219 equate_decl_number_to_die (decl, decl_die);
18222 #if 0
18223 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18224 Use modified_type_die instead.
18225 We keep this code here just in case these types of DIEs may be needed to
18226 represent certain things in other languages (e.g. Pascal) someday. */
18228 static void
18229 gen_pointer_type_die (tree type, dw_die_ref context_die)
18231 dw_die_ref ptr_die
18232 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
18234 equate_type_number_to_die (type, ptr_die);
18235 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18236 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18239 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18240 Use modified_type_die instead.
18241 We keep this code here just in case these types of DIEs may be needed to
18242 represent certain things in other languages (e.g. Pascal) someday. */
18244 static void
18245 gen_reference_type_die (tree type, dw_die_ref context_die)
18247 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
18249 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
18250 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
18251 else
18252 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
18254 equate_type_number_to_die (type, ref_die);
18255 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
18256 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18258 #endif
18260 /* Generate a DIE for a pointer to a member type. */
18262 static void
18263 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
18265 dw_die_ref ptr_die
18266 = new_die (DW_TAG_ptr_to_member_type,
18267 scope_die_for (type, context_die), type);
18269 equate_type_number_to_die (type, ptr_die);
18270 add_AT_die_ref (ptr_die, DW_AT_containing_type,
18271 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
18272 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18275 typedef const char *dchar_p; /* For DEF_VEC_P. */
18276 DEF_VEC_P(dchar_p);
18277 DEF_VEC_ALLOC_P(dchar_p,heap);
18279 static char *producer_string;
18281 /* Return a heap allocated producer string including command line options
18282 if -grecord-gcc-switches. */
18284 static char *
18285 gen_producer_string (void)
18287 size_t j;
18288 VEC(dchar_p, heap) *switches = NULL;
18289 const char *language_string = lang_hooks.name;
18290 char *producer, *tail;
18291 const char *p;
18292 size_t len = dwarf_record_gcc_switches ? 0 : 3;
18293 size_t plen = strlen (language_string) + 1 + strlen (version_string);
18295 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
18296 switch (save_decoded_options[j].opt_index)
18298 case OPT_o:
18299 case OPT_d:
18300 case OPT_dumpbase:
18301 case OPT_dumpdir:
18302 case OPT_auxbase:
18303 case OPT_auxbase_strip:
18304 case OPT_quiet:
18305 case OPT_version:
18306 case OPT_v:
18307 case OPT_w:
18308 case OPT_L:
18309 case OPT_D:
18310 case OPT_I:
18311 case OPT_U:
18312 case OPT_SPECIAL_unknown:
18313 case OPT_SPECIAL_ignore:
18314 case OPT_SPECIAL_program_name:
18315 case OPT_SPECIAL_input_file:
18316 case OPT_grecord_gcc_switches:
18317 case OPT_gno_record_gcc_switches:
18318 case OPT__output_pch_:
18319 case OPT_fdiagnostics_show_location_:
18320 case OPT_fdiagnostics_show_option:
18321 case OPT_fverbose_asm:
18322 case OPT____:
18323 case OPT__sysroot_:
18324 case OPT_nostdinc:
18325 case OPT_nostdinc__:
18326 /* Ignore these. */
18327 continue;
18328 default:
18329 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
18330 == '-');
18331 switch (save_decoded_options[j].canonical_option[0][1])
18333 case 'M':
18334 case 'i':
18335 case 'W':
18336 continue;
18337 case 'f':
18338 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
18339 "dump", 4) == 0)
18340 continue;
18341 break;
18342 default:
18343 break;
18345 VEC_safe_push (dchar_p, heap, switches,
18346 save_decoded_options[j].orig_option_with_args_text);
18347 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
18348 break;
18351 producer = XNEWVEC (char, plen + 1 + len + 1);
18352 tail = producer;
18353 sprintf (tail, "%s %s", language_string, version_string);
18354 tail += plen;
18356 if (!dwarf_record_gcc_switches)
18358 #ifdef MIPS_DEBUGGING_INFO
18359 /* The MIPS/SGI compilers place the 'cc' command line options in the
18360 producer string. The SGI debugger looks for -g, -g1, -g2, or -g3;
18361 if they do not appear in the producer string, the debugger reaches
18362 the conclusion that the object file is stripped and has no debugging
18363 information. To get the MIPS/SGI debugger to believe that there is
18364 debugging information in the object file, we add a -g to the producer
18365 string. */
18366 if (debug_info_level > DINFO_LEVEL_TERSE)
18368 memcpy (tail, " -g", 3);
18369 tail += 3;
18371 #endif
18374 FOR_EACH_VEC_ELT (dchar_p, switches, j, p)
18376 len = strlen (p);
18377 *tail = ' ';
18378 memcpy (tail + 1, p, len);
18379 tail += len + 1;
18382 *tail = '\0';
18383 VEC_free (dchar_p, heap, switches);
18384 return producer;
18387 /* Generate the DIE for the compilation unit. */
18389 static dw_die_ref
18390 gen_compile_unit_die (const char *filename)
18392 dw_die_ref die;
18393 const char *language_string = lang_hooks.name;
18394 int language;
18396 die = new_die (DW_TAG_compile_unit, NULL, NULL);
18398 if (filename)
18400 add_name_attribute (die, filename);
18401 /* Don't add cwd for <built-in>. */
18402 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
18403 add_comp_dir_attribute (die);
18406 if (producer_string == NULL)
18407 producer_string = gen_producer_string ();
18408 add_AT_string (die, DW_AT_producer, producer_string);
18410 /* If our producer is LTO try to figure out a common language to use
18411 from the global list of translation units. */
18412 if (strcmp (language_string, "GNU GIMPLE") == 0)
18414 unsigned i;
18415 tree t;
18416 const char *common_lang = NULL;
18418 FOR_EACH_VEC_ELT (tree, all_translation_units, i, t)
18420 if (!TRANSLATION_UNIT_LANGUAGE (t))
18421 continue;
18422 if (!common_lang)
18423 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
18424 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
18426 else if (strncmp (common_lang, "GNU C", 5) == 0
18427 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
18428 /* Mixing C and C++ is ok, use C++ in that case. */
18429 common_lang = "GNU C++";
18430 else
18432 /* Fall back to C. */
18433 common_lang = NULL;
18434 break;
18438 if (common_lang)
18439 language_string = common_lang;
18442 language = DW_LANG_C89;
18443 if (strcmp (language_string, "GNU C++") == 0)
18444 language = DW_LANG_C_plus_plus;
18445 else if (strcmp (language_string, "GNU F77") == 0)
18446 language = DW_LANG_Fortran77;
18447 else if (strcmp (language_string, "GNU Pascal") == 0)
18448 language = DW_LANG_Pascal83;
18449 else if (use_upc_dwarf2_extensions
18450 && ((strcmp (language_string, "GNU UPC") == 0)
18451 || (strcmp (language_string, "GCC UPC") == 0)))
18452 language = DW_LANG_Upc;
18453 else if (dwarf_version >= 3 || !dwarf_strict)
18455 if (strcmp (language_string, "GNU Ada") == 0)
18456 language = DW_LANG_Ada95;
18457 else if (strcmp (language_string, "GNU Fortran") == 0)
18458 language = DW_LANG_Fortran95;
18459 else if (strcmp (language_string, "GNU Java") == 0)
18460 language = DW_LANG_Java;
18461 else if (strcmp (language_string, "GNU Objective-C") == 0)
18462 language = DW_LANG_ObjC;
18463 else if (strcmp (language_string, "GNU Objective-C++") == 0)
18464 language = DW_LANG_ObjC_plus_plus;
18467 add_AT_unsigned (die, DW_AT_language, language);
18469 switch (language)
18471 case DW_LANG_Fortran77:
18472 case DW_LANG_Fortran90:
18473 case DW_LANG_Fortran95:
18474 /* Fortran has case insensitive identifiers and the front-end
18475 lowercases everything. */
18476 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
18477 break;
18478 default:
18479 /* The default DW_ID_case_sensitive doesn't need to be specified. */
18480 break;
18482 return die;
18485 /* Generate the DIE for a base class. */
18487 static void
18488 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
18490 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
18492 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
18493 add_data_member_location_attribute (die, binfo);
18495 if (BINFO_VIRTUAL_P (binfo))
18496 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
18498 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
18499 children, otherwise the default is DW_ACCESS_public. In DWARF2
18500 the default has always been DW_ACCESS_private. */
18501 if (access == access_public_node)
18503 if (dwarf_version == 2
18504 || context_die->die_tag == DW_TAG_class_type)
18505 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
18507 else if (access == access_protected_node)
18508 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
18509 else if (dwarf_version > 2
18510 && context_die->die_tag != DW_TAG_class_type)
18511 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
18514 /* Generate a DIE for a class member. */
18516 static void
18517 gen_member_die (tree type, dw_die_ref context_die)
18519 tree member;
18520 tree binfo = TYPE_BINFO (type);
18521 dw_die_ref child;
18523 /* If this is not an incomplete type, output descriptions of each of its
18524 members. Note that as we output the DIEs necessary to represent the
18525 members of this record or union type, we will also be trying to output
18526 DIEs to represent the *types* of those members. However the `type'
18527 function (above) will specifically avoid generating type DIEs for member
18528 types *within* the list of member DIEs for this (containing) type except
18529 for those types (of members) which are explicitly marked as also being
18530 members of this (containing) type themselves. The g++ front- end can
18531 force any given type to be treated as a member of some other (containing)
18532 type by setting the TYPE_CONTEXT of the given (member) type to point to
18533 the TREE node representing the appropriate (containing) type. */
18535 /* First output info about the base classes. */
18536 if (binfo)
18538 VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
18539 int i;
18540 tree base;
18542 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
18543 gen_inheritance_die (base,
18544 (accesses ? VEC_index (tree, accesses, i)
18545 : access_public_node), context_die);
18548 /* Now output info about the data members and type members. */
18549 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
18551 /* If we thought we were generating minimal debug info for TYPE
18552 and then changed our minds, some of the member declarations
18553 may have already been defined. Don't define them again, but
18554 do put them in the right order. */
18556 child = lookup_decl_die (member);
18557 if (child)
18558 splice_child_die (context_die, child);
18559 else
18560 gen_decl_die (member, NULL, context_die);
18563 /* Now output info about the function members (if any). */
18564 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
18566 /* Don't include clones in the member list. */
18567 if (DECL_ABSTRACT_ORIGIN (member))
18568 continue;
18570 child = lookup_decl_die (member);
18571 if (child)
18572 splice_child_die (context_die, child);
18573 else
18574 gen_decl_die (member, NULL, context_die);
18578 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
18579 is set, we pretend that the type was never defined, so we only get the
18580 member DIEs needed by later specification DIEs. */
18582 static void
18583 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
18584 enum debug_info_usage usage)
18586 dw_die_ref type_die = lookup_type_die (type);
18587 dw_die_ref scope_die = 0;
18588 int nested = 0;
18589 int complete = (TYPE_SIZE (type)
18590 && (! TYPE_STUB_DECL (type)
18591 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
18592 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
18593 complete = complete && should_emit_struct_debug (type, usage);
18595 if (type_die && ! complete)
18596 return;
18598 if (TYPE_CONTEXT (type) != NULL_TREE
18599 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
18600 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
18601 nested = 1;
18603 scope_die = scope_die_for (type, context_die);
18605 if (! type_die || (nested && is_cu_die (scope_die)))
18606 /* First occurrence of type or toplevel definition of nested class. */
18608 dw_die_ref old_die = type_die;
18610 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
18611 ? record_type_tag (type) : DW_TAG_union_type,
18612 scope_die, type);
18613 equate_type_number_to_die (type, type_die);
18614 if (old_die)
18615 add_AT_specification (type_die, old_die);
18616 else
18618 add_name_attribute (type_die, type_tag (type));
18619 add_gnat_descriptive_type_attribute (type_die, type, context_die);
18620 if (TYPE_ARTIFICIAL (type))
18621 add_AT_flag (type_die, DW_AT_artificial, 1);
18624 else
18625 remove_AT (type_die, DW_AT_declaration);
18627 /* Generate child dies for template paramaters. */
18628 if (debug_info_level > DINFO_LEVEL_TERSE
18629 && COMPLETE_TYPE_P (type))
18630 schedule_generic_params_dies_gen (type);
18632 /* If this type has been completed, then give it a byte_size attribute and
18633 then give a list of members. */
18634 if (complete && !ns_decl)
18636 /* Prevent infinite recursion in cases where the type of some member of
18637 this type is expressed in terms of this type itself. */
18638 TREE_ASM_WRITTEN (type) = 1;
18639 add_byte_size_attribute (type_die, type);
18640 if (TYPE_STUB_DECL (type) != NULL_TREE)
18642 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
18643 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
18646 /* If the first reference to this type was as the return type of an
18647 inline function, then it may not have a parent. Fix this now. */
18648 if (type_die->die_parent == NULL)
18649 add_child_die (scope_die, type_die);
18651 push_decl_scope (type);
18652 gen_member_die (type, type_die);
18653 pop_decl_scope ();
18655 /* GNU extension: Record what type our vtable lives in. */
18656 if (TYPE_VFIELD (type))
18658 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
18660 gen_type_die (vtype, context_die);
18661 add_AT_die_ref (type_die, DW_AT_containing_type,
18662 lookup_type_die (vtype));
18665 else
18667 add_AT_flag (type_die, DW_AT_declaration, 1);
18669 /* We don't need to do this for function-local types. */
18670 if (TYPE_STUB_DECL (type)
18671 && ! decl_function_context (TYPE_STUB_DECL (type)))
18672 VEC_safe_push (tree, gc, incomplete_types, type);
18675 if (get_AT (type_die, DW_AT_name))
18676 add_pubtype (type, type_die);
18679 /* Generate a DIE for a subroutine _type_. */
18681 static void
18682 gen_subroutine_type_die (tree type, dw_die_ref context_die)
18684 tree return_type = TREE_TYPE (type);
18685 dw_die_ref subr_die
18686 = new_die (DW_TAG_subroutine_type,
18687 scope_die_for (type, context_die), type);
18689 equate_type_number_to_die (type, subr_die);
18690 add_prototyped_attribute (subr_die, type);
18691 add_type_attribute (subr_die, return_type, 0, 0, context_die);
18692 gen_formal_types_die (type, subr_die);
18694 if (get_AT (subr_die, DW_AT_name))
18695 add_pubtype (type, subr_die);
18698 /* Generate a DIE for a type definition. */
18700 static void
18701 gen_typedef_die (tree decl, dw_die_ref context_die)
18703 dw_die_ref type_die;
18704 tree origin;
18706 if (TREE_ASM_WRITTEN (decl))
18707 return;
18709 TREE_ASM_WRITTEN (decl) = 1;
18710 type_die = new_die (DW_TAG_typedef, context_die, decl);
18711 origin = decl_ultimate_origin (decl);
18712 if (origin != NULL)
18713 add_abstract_origin_attribute (type_die, origin);
18714 else
18716 tree type;
18718 add_name_and_src_coords_attributes (type_die, decl);
18719 if (DECL_ORIGINAL_TYPE (decl))
18721 type = DECL_ORIGINAL_TYPE (decl);
18723 gcc_assert (type != TREE_TYPE (decl));
18724 equate_type_number_to_die (TREE_TYPE (decl), type_die);
18726 else
18728 type = TREE_TYPE (decl);
18730 if (is_naming_typedef_decl (TYPE_NAME (type)))
18732 /* Here, we are in the case of decl being a typedef naming
18733 an anonymous type, e.g:
18734 typedef struct {...} foo;
18735 In that case TREE_TYPE (decl) is not a typedef variant
18736 type and TYPE_NAME of the anonymous type is set to the
18737 TYPE_DECL of the typedef. This construct is emitted by
18738 the C++ FE.
18740 TYPE is the anonymous struct named by the typedef
18741 DECL. As we need the DW_AT_type attribute of the
18742 DW_TAG_typedef to point to the DIE of TYPE, let's
18743 generate that DIE right away. add_type_attribute
18744 called below will then pick (via lookup_type_die) that
18745 anonymous struct DIE. */
18746 if (!TREE_ASM_WRITTEN (type))
18747 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
18749 /* This is a GNU Extension. We are adding a
18750 DW_AT_linkage_name attribute to the DIE of the
18751 anonymous struct TYPE. The value of that attribute
18752 is the name of the typedef decl naming the anonymous
18753 struct. This greatly eases the work of consumers of
18754 this debug info. */
18755 add_linkage_attr (lookup_type_die (type), decl);
18759 add_type_attribute (type_die, type, TREE_READONLY (decl),
18760 TREE_THIS_VOLATILE (decl), context_die);
18762 if (is_naming_typedef_decl (decl))
18763 /* We want that all subsequent calls to lookup_type_die with
18764 TYPE in argument yield the DW_TAG_typedef we have just
18765 created. */
18766 equate_type_number_to_die (type, type_die);
18768 add_accessibility_attribute (type_die, decl);
18771 if (DECL_ABSTRACT (decl))
18772 equate_decl_number_to_die (decl, type_die);
18774 if (get_AT (type_die, DW_AT_name))
18775 add_pubtype (decl, type_die);
18778 /* Generate a DIE for a struct, class, enum or union type. */
18780 static void
18781 gen_tagged_type_die (tree type,
18782 dw_die_ref context_die,
18783 enum debug_info_usage usage)
18785 int need_pop;
18787 if (type == NULL_TREE
18788 || !is_tagged_type (type))
18789 return;
18791 /* If this is a nested type whose containing class hasn't been written
18792 out yet, writing it out will cover this one, too. This does not apply
18793 to instantiations of member class templates; they need to be added to
18794 the containing class as they are generated. FIXME: This hurts the
18795 idea of combining type decls from multiple TUs, since we can't predict
18796 what set of template instantiations we'll get. */
18797 if (TYPE_CONTEXT (type)
18798 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
18799 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
18801 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
18803 if (TREE_ASM_WRITTEN (type))
18804 return;
18806 /* If that failed, attach ourselves to the stub. */
18807 push_decl_scope (TYPE_CONTEXT (type));
18808 context_die = lookup_type_die (TYPE_CONTEXT (type));
18809 need_pop = 1;
18811 else if (TYPE_CONTEXT (type) != NULL_TREE
18812 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
18814 /* If this type is local to a function that hasn't been written
18815 out yet, use a NULL context for now; it will be fixed up in
18816 decls_for_scope. */
18817 context_die = lookup_decl_die (TYPE_CONTEXT (type));
18818 /* A declaration DIE doesn't count; nested types need to go in the
18819 specification. */
18820 if (context_die && is_declaration_die (context_die))
18821 context_die = NULL;
18822 need_pop = 0;
18824 else
18826 context_die = declare_in_namespace (type, context_die);
18827 need_pop = 0;
18830 if (TREE_CODE (type) == ENUMERAL_TYPE)
18832 /* This might have been written out by the call to
18833 declare_in_namespace. */
18834 if (!TREE_ASM_WRITTEN (type))
18835 gen_enumeration_type_die (type, context_die);
18837 else
18838 gen_struct_or_union_type_die (type, context_die, usage);
18840 if (need_pop)
18841 pop_decl_scope ();
18843 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
18844 it up if it is ever completed. gen_*_type_die will set it for us
18845 when appropriate. */
18848 /* Generate a type description DIE. */
18850 static void
18851 gen_type_die_with_usage (tree type, dw_die_ref context_die,
18852 enum debug_info_usage usage)
18854 struct array_descr_info info;
18856 if (type == NULL_TREE || type == error_mark_node)
18857 return;
18859 if (TYPE_NAME (type) != NULL_TREE
18860 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
18861 && is_redundant_typedef (TYPE_NAME (type))
18862 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
18863 /* The DECL of this type is a typedef we don't want to emit debug
18864 info for but we want debug info for its underlying typedef.
18865 This can happen for e.g, the injected-class-name of a C++
18866 type. */
18867 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
18869 /* If TYPE is a typedef type variant, let's generate debug info
18870 for the parent typedef which TYPE is a type of. */
18871 if (typedef_variant_p (type))
18873 if (TREE_ASM_WRITTEN (type))
18874 return;
18876 /* Prevent broken recursion; we can't hand off to the same type. */
18877 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
18879 /* Use the DIE of the containing namespace as the parent DIE of
18880 the type description DIE we want to generate. */
18881 if (DECL_CONTEXT (TYPE_NAME (type))
18882 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
18883 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
18885 TREE_ASM_WRITTEN (type) = 1;
18887 gen_decl_die (TYPE_NAME (type), NULL, context_die);
18888 return;
18891 /* If type is an anonymous tagged type named by a typedef, let's
18892 generate debug info for the typedef. */
18893 if (is_naming_typedef_decl (TYPE_NAME (type)))
18895 /* Use the DIE of the containing namespace as the parent DIE of
18896 the type description DIE we want to generate. */
18897 if (DECL_CONTEXT (TYPE_NAME (type))
18898 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
18899 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
18901 gen_decl_die (TYPE_NAME (type), NULL, context_die);
18902 return;
18905 /* If this is an array type with hidden descriptor, handle it first. */
18906 if (!TREE_ASM_WRITTEN (type)
18907 && lang_hooks.types.get_array_descr_info
18908 && lang_hooks.types.get_array_descr_info (type, &info)
18909 && (dwarf_version >= 3 || !dwarf_strict))
18911 gen_descr_array_type_die (type, &info, context_die);
18912 TREE_ASM_WRITTEN (type) = 1;
18913 return;
18916 /* We are going to output a DIE to represent the unqualified version
18917 of this type (i.e. without any const or volatile qualifiers) so
18918 get the main variant (i.e. the unqualified version) of this type
18919 now. (Vectors are special because the debugging info is in the
18920 cloned type itself). */
18921 if (TREE_CODE (type) != VECTOR_TYPE)
18922 type = type_main_variant (type);
18924 if (TREE_ASM_WRITTEN (type))
18925 return;
18927 switch (TREE_CODE (type))
18929 case ERROR_MARK:
18930 break;
18932 case POINTER_TYPE:
18933 case REFERENCE_TYPE:
18934 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
18935 ensures that the gen_type_die recursion will terminate even if the
18936 type is recursive. Recursive types are possible in Ada. */
18937 /* ??? We could perhaps do this for all types before the switch
18938 statement. */
18939 TREE_ASM_WRITTEN (type) = 1;
18941 /* For these types, all that is required is that we output a DIE (or a
18942 set of DIEs) to represent the "basis" type. */
18943 gen_type_die_with_usage (TREE_TYPE (type), context_die,
18944 DINFO_USAGE_IND_USE);
18945 break;
18947 case OFFSET_TYPE:
18948 /* This code is used for C++ pointer-to-data-member types.
18949 Output a description of the relevant class type. */
18950 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
18951 DINFO_USAGE_IND_USE);
18953 /* Output a description of the type of the object pointed to. */
18954 gen_type_die_with_usage (TREE_TYPE (type), context_die,
18955 DINFO_USAGE_IND_USE);
18957 /* Now output a DIE to represent this pointer-to-data-member type
18958 itself. */
18959 gen_ptr_to_mbr_type_die (type, context_die);
18960 break;
18962 case FUNCTION_TYPE:
18963 /* Force out return type (in case it wasn't forced out already). */
18964 gen_type_die_with_usage (TREE_TYPE (type), context_die,
18965 DINFO_USAGE_DIR_USE);
18966 gen_subroutine_type_die (type, context_die);
18967 break;
18969 case METHOD_TYPE:
18970 /* Force out return type (in case it wasn't forced out already). */
18971 gen_type_die_with_usage (TREE_TYPE (type), context_die,
18972 DINFO_USAGE_DIR_USE);
18973 gen_subroutine_type_die (type, context_die);
18974 break;
18976 case ARRAY_TYPE:
18977 gen_array_type_die (type, context_die);
18978 break;
18980 case VECTOR_TYPE:
18981 gen_array_type_die (type, context_die);
18982 break;
18984 case ENUMERAL_TYPE:
18985 case RECORD_TYPE:
18986 case UNION_TYPE:
18987 case QUAL_UNION_TYPE:
18988 gen_tagged_type_die (type, context_die, usage);
18989 return;
18991 case VOID_TYPE:
18992 case INTEGER_TYPE:
18993 case REAL_TYPE:
18994 case FIXED_POINT_TYPE:
18995 case COMPLEX_TYPE:
18996 case BOOLEAN_TYPE:
18997 /* No DIEs needed for fundamental types. */
18998 break;
19000 case NULLPTR_TYPE:
19001 case LANG_TYPE:
19002 /* Just use DW_TAG_unspecified_type. */
19004 dw_die_ref type_die = lookup_type_die (type);
19005 if (type_die == NULL)
19007 tree name = TYPE_NAME (type);
19008 if (TREE_CODE (name) == TYPE_DECL)
19009 name = DECL_NAME (name);
19010 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
19011 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
19012 equate_type_number_to_die (type, type_die);
19015 break;
19017 default:
19018 gcc_unreachable ();
19021 TREE_ASM_WRITTEN (type) = 1;
19024 static void
19025 gen_type_die (tree type, dw_die_ref context_die)
19027 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
19030 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
19031 things which are local to the given block. */
19033 static void
19034 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
19036 int must_output_die = 0;
19037 bool inlined_func;
19039 /* Ignore blocks that are NULL. */
19040 if (stmt == NULL_TREE)
19041 return;
19043 inlined_func = inlined_function_outer_scope_p (stmt);
19045 /* If the block is one fragment of a non-contiguous block, do not
19046 process the variables, since they will have been done by the
19047 origin block. Do process subblocks. */
19048 if (BLOCK_FRAGMENT_ORIGIN (stmt))
19050 tree sub;
19052 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
19053 gen_block_die (sub, context_die, depth + 1);
19055 return;
19058 /* Determine if we need to output any Dwarf DIEs at all to represent this
19059 block. */
19060 if (inlined_func)
19061 /* The outer scopes for inlinings *must* always be represented. We
19062 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
19063 must_output_die = 1;
19064 else
19066 /* Determine if this block directly contains any "significant"
19067 local declarations which we will need to output DIEs for. */
19068 if (debug_info_level > DINFO_LEVEL_TERSE)
19069 /* We are not in terse mode so *any* local declaration counts
19070 as being a "significant" one. */
19071 must_output_die = ((BLOCK_VARS (stmt) != NULL
19072 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
19073 && (TREE_USED (stmt)
19074 || TREE_ASM_WRITTEN (stmt)
19075 || BLOCK_ABSTRACT (stmt)));
19076 else if ((TREE_USED (stmt)
19077 || TREE_ASM_WRITTEN (stmt)
19078 || BLOCK_ABSTRACT (stmt))
19079 && !dwarf2out_ignore_block (stmt))
19080 must_output_die = 1;
19083 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
19084 DIE for any block which contains no significant local declarations at
19085 all. Rather, in such cases we just call `decls_for_scope' so that any
19086 needed Dwarf info for any sub-blocks will get properly generated. Note
19087 that in terse mode, our definition of what constitutes a "significant"
19088 local declaration gets restricted to include only inlined function
19089 instances and local (nested) function definitions. */
19090 if (must_output_die)
19092 if (inlined_func)
19094 /* If STMT block is abstract, that means we have been called
19095 indirectly from dwarf2out_abstract_function.
19096 That function rightfully marks the descendent blocks (of
19097 the abstract function it is dealing with) as being abstract,
19098 precisely to prevent us from emitting any
19099 DW_TAG_inlined_subroutine DIE as a descendent
19100 of an abstract function instance. So in that case, we should
19101 not call gen_inlined_subroutine_die.
19103 Later though, when cgraph asks dwarf2out to emit info
19104 for the concrete instance of the function decl into which
19105 the concrete instance of STMT got inlined, the later will lead
19106 to the generation of a DW_TAG_inlined_subroutine DIE. */
19107 if (! BLOCK_ABSTRACT (stmt))
19108 gen_inlined_subroutine_die (stmt, context_die, depth);
19110 else
19111 gen_lexical_block_die (stmt, context_die, depth);
19113 else
19114 decls_for_scope (stmt, context_die, depth);
19117 /* Process variable DECL (or variable with origin ORIGIN) within
19118 block STMT and add it to CONTEXT_DIE. */
19119 static void
19120 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
19122 dw_die_ref die;
19123 tree decl_or_origin = decl ? decl : origin;
19125 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
19126 die = lookup_decl_die (decl_or_origin);
19127 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
19128 && TYPE_DECL_IS_STUB (decl_or_origin))
19129 die = lookup_type_die (TREE_TYPE (decl_or_origin));
19130 else
19131 die = NULL;
19133 if (die != NULL && die->die_parent == NULL)
19134 add_child_die (context_die, die);
19135 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
19136 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
19137 stmt, context_die);
19138 else
19139 gen_decl_die (decl, origin, context_die);
19142 /* Generate all of the decls declared within a given scope and (recursively)
19143 all of its sub-blocks. */
19145 static void
19146 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
19148 tree decl;
19149 unsigned int i;
19150 tree subblocks;
19152 /* Ignore NULL blocks. */
19153 if (stmt == NULL_TREE)
19154 return;
19156 /* Output the DIEs to represent all of the data objects and typedefs
19157 declared directly within this block but not within any nested
19158 sub-blocks. Also, nested function and tag DIEs have been
19159 generated with a parent of NULL; fix that up now. */
19160 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
19161 process_scope_var (stmt, decl, NULL_TREE, context_die);
19162 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
19163 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
19164 context_die);
19166 /* If we're at -g1, we're not interested in subblocks. */
19167 if (debug_info_level <= DINFO_LEVEL_TERSE)
19168 return;
19170 /* Output the DIEs to represent all sub-blocks (and the items declared
19171 therein) of this block. */
19172 for (subblocks = BLOCK_SUBBLOCKS (stmt);
19173 subblocks != NULL;
19174 subblocks = BLOCK_CHAIN (subblocks))
19175 gen_block_die (subblocks, context_die, depth + 1);
19178 /* Is this a typedef we can avoid emitting? */
19180 static inline int
19181 is_redundant_typedef (const_tree decl)
19183 if (TYPE_DECL_IS_STUB (decl))
19184 return 1;
19186 if (DECL_ARTIFICIAL (decl)
19187 && DECL_CONTEXT (decl)
19188 && is_tagged_type (DECL_CONTEXT (decl))
19189 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
19190 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
19191 /* Also ignore the artificial member typedef for the class name. */
19192 return 1;
19194 return 0;
19197 /* Return TRUE if TYPE is a typedef that names a type for linkage
19198 purposes. This kind of typedefs is produced by the C++ FE for
19199 constructs like:
19201 typedef struct {...} foo;
19203 In that case, there is no typedef variant type produced for foo.
19204 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
19205 struct type. */
19207 static bool
19208 is_naming_typedef_decl (const_tree decl)
19210 if (decl == NULL_TREE
19211 || TREE_CODE (decl) != TYPE_DECL
19212 || !is_tagged_type (TREE_TYPE (decl))
19213 || DECL_IS_BUILTIN (decl)
19214 || is_redundant_typedef (decl)
19215 /* It looks like Ada produces TYPE_DECLs that are very similar
19216 to C++ naming typedefs but that have different
19217 semantics. Let's be specific to c++ for now. */
19218 || !is_cxx ())
19219 return FALSE;
19221 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
19222 && TYPE_NAME (TREE_TYPE (decl)) == decl
19223 && (TYPE_STUB_DECL (TREE_TYPE (decl))
19224 != TYPE_NAME (TREE_TYPE (decl))));
19227 /* Returns the DIE for a context. */
19229 static inline dw_die_ref
19230 get_context_die (tree context)
19232 if (context)
19234 /* Find die that represents this context. */
19235 if (TYPE_P (context))
19237 context = TYPE_MAIN_VARIANT (context);
19238 return strip_naming_typedef (context, force_type_die (context));
19240 else
19241 return force_decl_die (context);
19243 return comp_unit_die ();
19246 /* Returns the DIE for decl. A DIE will always be returned. */
19248 static dw_die_ref
19249 force_decl_die (tree decl)
19251 dw_die_ref decl_die;
19252 unsigned saved_external_flag;
19253 tree save_fn = NULL_TREE;
19254 decl_die = lookup_decl_die (decl);
19255 if (!decl_die)
19257 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
19259 decl_die = lookup_decl_die (decl);
19260 if (decl_die)
19261 return decl_die;
19263 switch (TREE_CODE (decl))
19265 case FUNCTION_DECL:
19266 /* Clear current_function_decl, so that gen_subprogram_die thinks
19267 that this is a declaration. At this point, we just want to force
19268 declaration die. */
19269 save_fn = current_function_decl;
19270 current_function_decl = NULL_TREE;
19271 gen_subprogram_die (decl, context_die);
19272 current_function_decl = save_fn;
19273 break;
19275 case VAR_DECL:
19276 /* Set external flag to force declaration die. Restore it after
19277 gen_decl_die() call. */
19278 saved_external_flag = DECL_EXTERNAL (decl);
19279 DECL_EXTERNAL (decl) = 1;
19280 gen_decl_die (decl, NULL, context_die);
19281 DECL_EXTERNAL (decl) = saved_external_flag;
19282 break;
19284 case NAMESPACE_DECL:
19285 if (dwarf_version >= 3 || !dwarf_strict)
19286 dwarf2out_decl (decl);
19287 else
19288 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
19289 decl_die = comp_unit_die ();
19290 break;
19292 case TRANSLATION_UNIT_DECL:
19293 decl_die = comp_unit_die ();
19294 break;
19296 default:
19297 gcc_unreachable ();
19300 /* We should be able to find the DIE now. */
19301 if (!decl_die)
19302 decl_die = lookup_decl_die (decl);
19303 gcc_assert (decl_die);
19306 return decl_die;
19309 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
19310 always returned. */
19312 static dw_die_ref
19313 force_type_die (tree type)
19315 dw_die_ref type_die;
19317 type_die = lookup_type_die (type);
19318 if (!type_die)
19320 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
19322 type_die = modified_type_die (type, TYPE_QUALS (type), context_die);
19323 gcc_assert (type_die);
19325 return type_die;
19328 /* Force out any required namespaces to be able to output DECL,
19329 and return the new context_die for it, if it's changed. */
19331 static dw_die_ref
19332 setup_namespace_context (tree thing, dw_die_ref context_die)
19334 tree context = (DECL_P (thing)
19335 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
19336 if (context && TREE_CODE (context) == NAMESPACE_DECL)
19337 /* Force out the namespace. */
19338 context_die = force_decl_die (context);
19340 return context_die;
19343 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
19344 type) within its namespace, if appropriate.
19346 For compatibility with older debuggers, namespace DIEs only contain
19347 declarations; all definitions are emitted at CU scope. */
19349 static dw_die_ref
19350 declare_in_namespace (tree thing, dw_die_ref context_die)
19352 dw_die_ref ns_context;
19354 if (debug_info_level <= DINFO_LEVEL_TERSE)
19355 return context_die;
19357 /* If this decl is from an inlined function, then don't try to emit it in its
19358 namespace, as we will get confused. It would have already been emitted
19359 when the abstract instance of the inline function was emitted anyways. */
19360 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
19361 return context_die;
19363 ns_context = setup_namespace_context (thing, context_die);
19365 if (ns_context != context_die)
19367 if (is_fortran ())
19368 return ns_context;
19369 if (DECL_P (thing))
19370 gen_decl_die (thing, NULL, ns_context);
19371 else
19372 gen_type_die (thing, ns_context);
19374 return context_die;
19377 /* Generate a DIE for a namespace or namespace alias. */
19379 static void
19380 gen_namespace_die (tree decl, dw_die_ref context_die)
19382 dw_die_ref namespace_die;
19384 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
19385 they are an alias of. */
19386 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
19388 /* Output a real namespace or module. */
19389 context_die = setup_namespace_context (decl, comp_unit_die ());
19390 namespace_die = new_die (is_fortran ()
19391 ? DW_TAG_module : DW_TAG_namespace,
19392 context_die, decl);
19393 /* For Fortran modules defined in different CU don't add src coords. */
19394 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
19396 const char *name = dwarf2_name (decl, 0);
19397 if (name)
19398 add_name_attribute (namespace_die, name);
19400 else
19401 add_name_and_src_coords_attributes (namespace_die, decl);
19402 if (DECL_EXTERNAL (decl))
19403 add_AT_flag (namespace_die, DW_AT_declaration, 1);
19404 equate_decl_number_to_die (decl, namespace_die);
19406 else
19408 /* Output a namespace alias. */
19410 /* Force out the namespace we are an alias of, if necessary. */
19411 dw_die_ref origin_die
19412 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
19414 if (DECL_FILE_SCOPE_P (decl)
19415 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
19416 context_die = setup_namespace_context (decl, comp_unit_die ());
19417 /* Now create the namespace alias DIE. */
19418 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
19419 add_name_and_src_coords_attributes (namespace_die, decl);
19420 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
19421 equate_decl_number_to_die (decl, namespace_die);
19425 /* Generate Dwarf debug information for a decl described by DECL.
19426 The return value is currently only meaningful for PARM_DECLs,
19427 for all other decls it returns NULL. */
19429 static dw_die_ref
19430 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
19432 tree decl_or_origin = decl ? decl : origin;
19433 tree class_origin = NULL, ultimate_origin;
19435 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
19436 return NULL;
19438 switch (TREE_CODE (decl_or_origin))
19440 case ERROR_MARK:
19441 break;
19443 case CONST_DECL:
19444 if (!is_fortran () && !is_ada ())
19446 /* The individual enumerators of an enum type get output when we output
19447 the Dwarf representation of the relevant enum type itself. */
19448 break;
19451 /* Emit its type. */
19452 gen_type_die (TREE_TYPE (decl), context_die);
19454 /* And its containing namespace. */
19455 context_die = declare_in_namespace (decl, context_die);
19457 gen_const_die (decl, context_die);
19458 break;
19460 case FUNCTION_DECL:
19461 /* Don't output any DIEs to represent mere function declarations,
19462 unless they are class members or explicit block externs. */
19463 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
19464 && DECL_FILE_SCOPE_P (decl_or_origin)
19465 && (current_function_decl == NULL_TREE
19466 || DECL_ARTIFICIAL (decl_or_origin)))
19467 break;
19469 #if 0
19470 /* FIXME */
19471 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
19472 on local redeclarations of global functions. That seems broken. */
19473 if (current_function_decl != decl)
19474 /* This is only a declaration. */;
19475 #endif
19477 /* If we're emitting a clone, emit info for the abstract instance. */
19478 if (origin || DECL_ORIGIN (decl) != decl)
19479 dwarf2out_abstract_function (origin
19480 ? DECL_ORIGIN (origin)
19481 : DECL_ABSTRACT_ORIGIN (decl));
19483 /* If we're emitting an out-of-line copy of an inline function,
19484 emit info for the abstract instance and set up to refer to it. */
19485 else if (cgraph_function_possibly_inlined_p (decl)
19486 && ! DECL_ABSTRACT (decl)
19487 && ! class_or_namespace_scope_p (context_die)
19488 /* dwarf2out_abstract_function won't emit a die if this is just
19489 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
19490 that case, because that works only if we have a die. */
19491 && DECL_INITIAL (decl) != NULL_TREE)
19493 dwarf2out_abstract_function (decl);
19494 set_decl_origin_self (decl);
19497 /* Otherwise we're emitting the primary DIE for this decl. */
19498 else if (debug_info_level > DINFO_LEVEL_TERSE)
19500 /* Before we describe the FUNCTION_DECL itself, make sure that we
19501 have its containing type. */
19502 if (!origin)
19503 origin = decl_class_context (decl);
19504 if (origin != NULL_TREE)
19505 gen_type_die (origin, context_die);
19507 /* And its return type. */
19508 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
19510 /* And its virtual context. */
19511 if (DECL_VINDEX (decl) != NULL_TREE)
19512 gen_type_die (DECL_CONTEXT (decl), context_die);
19514 /* Make sure we have a member DIE for decl. */
19515 if (origin != NULL_TREE)
19516 gen_type_die_for_member (origin, decl, context_die);
19518 /* And its containing namespace. */
19519 context_die = declare_in_namespace (decl, context_die);
19522 /* Now output a DIE to represent the function itself. */
19523 if (decl)
19524 gen_subprogram_die (decl, context_die);
19525 break;
19527 case TYPE_DECL:
19528 /* If we are in terse mode, don't generate any DIEs to represent any
19529 actual typedefs. */
19530 if (debug_info_level <= DINFO_LEVEL_TERSE)
19531 break;
19533 /* In the special case of a TYPE_DECL node representing the declaration
19534 of some type tag, if the given TYPE_DECL is marked as having been
19535 instantiated from some other (original) TYPE_DECL node (e.g. one which
19536 was generated within the original definition of an inline function) we
19537 used to generate a special (abbreviated) DW_TAG_structure_type,
19538 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
19539 should be actually referencing those DIEs, as variable DIEs with that
19540 type would be emitted already in the abstract origin, so it was always
19541 removed during unused type prunning. Don't add anything in this
19542 case. */
19543 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
19544 break;
19546 if (is_redundant_typedef (decl))
19547 gen_type_die (TREE_TYPE (decl), context_die);
19548 else
19549 /* Output a DIE to represent the typedef itself. */
19550 gen_typedef_die (decl, context_die);
19551 break;
19553 case LABEL_DECL:
19554 if (debug_info_level >= DINFO_LEVEL_NORMAL)
19555 gen_label_die (decl, context_die);
19556 break;
19558 case VAR_DECL:
19559 case RESULT_DECL:
19560 /* If we are in terse mode, don't generate any DIEs to represent any
19561 variable declarations or definitions. */
19562 if (debug_info_level <= DINFO_LEVEL_TERSE)
19563 break;
19565 /* Output any DIEs that are needed to specify the type of this data
19566 object. */
19567 if (decl_by_reference_p (decl_or_origin))
19568 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
19569 else
19570 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
19572 /* And its containing type. */
19573 class_origin = decl_class_context (decl_or_origin);
19574 if (class_origin != NULL_TREE)
19575 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
19577 /* And its containing namespace. */
19578 context_die = declare_in_namespace (decl_or_origin, context_die);
19580 /* Now output the DIE to represent the data object itself. This gets
19581 complicated because of the possibility that the VAR_DECL really
19582 represents an inlined instance of a formal parameter for an inline
19583 function. */
19584 ultimate_origin = decl_ultimate_origin (decl_or_origin);
19585 if (ultimate_origin != NULL_TREE
19586 && TREE_CODE (ultimate_origin) == PARM_DECL)
19587 gen_formal_parameter_die (decl, origin,
19588 true /* Emit name attribute. */,
19589 context_die);
19590 else
19591 gen_variable_die (decl, origin, context_die);
19592 break;
19594 case FIELD_DECL:
19595 /* Ignore the nameless fields that are used to skip bits but handle C++
19596 anonymous unions and structs. */
19597 if (DECL_NAME (decl) != NULL_TREE
19598 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
19599 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
19601 gen_type_die (member_declared_type (decl), context_die);
19602 gen_field_die (decl, context_die);
19604 break;
19606 case PARM_DECL:
19607 if (DECL_BY_REFERENCE (decl_or_origin))
19608 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
19609 else
19610 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
19611 return gen_formal_parameter_die (decl, origin,
19612 true /* Emit name attribute. */,
19613 context_die);
19615 case NAMESPACE_DECL:
19616 case IMPORTED_DECL:
19617 if (dwarf_version >= 3 || !dwarf_strict)
19618 gen_namespace_die (decl, context_die);
19619 break;
19621 default:
19622 /* Probably some frontend-internal decl. Assume we don't care. */
19623 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
19624 break;
19627 return NULL;
19630 /* Output debug information for global decl DECL. Called from toplev.c after
19631 compilation proper has finished. */
19633 static void
19634 dwarf2out_global_decl (tree decl)
19636 /* Output DWARF2 information for file-scope tentative data object
19637 declarations, file-scope (extern) function declarations (which
19638 had no corresponding body) and file-scope tagged type declarations
19639 and definitions which have not yet been forced out. */
19640 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
19641 dwarf2out_decl (decl);
19644 /* Output debug information for type decl DECL. Called from toplev.c
19645 and from language front ends (to record built-in types). */
19646 static void
19647 dwarf2out_type_decl (tree decl, int local)
19649 if (!local)
19650 dwarf2out_decl (decl);
19653 /* Output debug information for imported module or decl DECL.
19654 NAME is non-NULL name in the lexical block if the decl has been renamed.
19655 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
19656 that DECL belongs to.
19657 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
19658 static void
19659 dwarf2out_imported_module_or_decl_1 (tree decl,
19660 tree name,
19661 tree lexical_block,
19662 dw_die_ref lexical_block_die)
19664 expanded_location xloc;
19665 dw_die_ref imported_die = NULL;
19666 dw_die_ref at_import_die;
19668 if (TREE_CODE (decl) == IMPORTED_DECL)
19670 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
19671 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
19672 gcc_assert (decl);
19674 else
19675 xloc = expand_location (input_location);
19677 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
19679 at_import_die = force_type_die (TREE_TYPE (decl));
19680 /* For namespace N { typedef void T; } using N::T; base_type_die
19681 returns NULL, but DW_TAG_imported_declaration requires
19682 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
19683 if (!at_import_die)
19685 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
19686 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
19687 at_import_die = lookup_type_die (TREE_TYPE (decl));
19688 gcc_assert (at_import_die);
19691 else
19693 at_import_die = lookup_decl_die (decl);
19694 if (!at_import_die)
19696 /* If we're trying to avoid duplicate debug info, we may not have
19697 emitted the member decl for this field. Emit it now. */
19698 if (TREE_CODE (decl) == FIELD_DECL)
19700 tree type = DECL_CONTEXT (decl);
19702 if (TYPE_CONTEXT (type)
19703 && TYPE_P (TYPE_CONTEXT (type))
19704 && !should_emit_struct_debug (TYPE_CONTEXT (type),
19705 DINFO_USAGE_DIR_USE))
19706 return;
19707 gen_type_die_for_member (type, decl,
19708 get_context_die (TYPE_CONTEXT (type)));
19710 at_import_die = force_decl_die (decl);
19714 if (TREE_CODE (decl) == NAMESPACE_DECL)
19716 if (dwarf_version >= 3 || !dwarf_strict)
19717 imported_die = new_die (DW_TAG_imported_module,
19718 lexical_block_die,
19719 lexical_block);
19720 else
19721 return;
19723 else
19724 imported_die = new_die (DW_TAG_imported_declaration,
19725 lexical_block_die,
19726 lexical_block);
19728 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
19729 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
19730 if (name)
19731 add_AT_string (imported_die, DW_AT_name,
19732 IDENTIFIER_POINTER (name));
19733 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
19736 /* Output debug information for imported module or decl DECL.
19737 NAME is non-NULL name in context if the decl has been renamed.
19738 CHILD is true if decl is one of the renamed decls as part of
19739 importing whole module. */
19741 static void
19742 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
19743 bool child)
19745 /* dw_die_ref at_import_die; */
19746 dw_die_ref scope_die;
19748 if (debug_info_level <= DINFO_LEVEL_TERSE)
19749 return;
19751 gcc_assert (decl);
19753 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
19754 We need decl DIE for reference and scope die. First, get DIE for the decl
19755 itself. */
19757 /* Get the scope die for decl context. Use comp_unit_die for global module
19758 or decl. If die is not found for non globals, force new die. */
19759 if (context
19760 && TYPE_P (context)
19761 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
19762 return;
19764 if (!(dwarf_version >= 3 || !dwarf_strict))
19765 return;
19767 scope_die = get_context_die (context);
19769 if (child)
19771 gcc_assert (scope_die->die_child);
19772 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
19773 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
19774 scope_die = scope_die->die_child;
19777 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
19778 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
19782 /* Write the debugging output for DECL. */
19784 void
19785 dwarf2out_decl (tree decl)
19787 dw_die_ref context_die = comp_unit_die ();
19789 switch (TREE_CODE (decl))
19791 case ERROR_MARK:
19792 return;
19794 case FUNCTION_DECL:
19795 /* What we would really like to do here is to filter out all mere
19796 file-scope declarations of file-scope functions which are never
19797 referenced later within this translation unit (and keep all of ones
19798 that *are* referenced later on) but we aren't clairvoyant, so we have
19799 no idea which functions will be referenced in the future (i.e. later
19800 on within the current translation unit). So here we just ignore all
19801 file-scope function declarations which are not also definitions. If
19802 and when the debugger needs to know something about these functions,
19803 it will have to hunt around and find the DWARF information associated
19804 with the definition of the function.
19806 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
19807 nodes represent definitions and which ones represent mere
19808 declarations. We have to check DECL_INITIAL instead. That's because
19809 the C front-end supports some weird semantics for "extern inline"
19810 function definitions. These can get inlined within the current
19811 translation unit (and thus, we need to generate Dwarf info for their
19812 abstract instances so that the Dwarf info for the concrete inlined
19813 instances can have something to refer to) but the compiler never
19814 generates any out-of-lines instances of such things (despite the fact
19815 that they *are* definitions).
19817 The important point is that the C front-end marks these "extern
19818 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
19819 them anyway. Note that the C++ front-end also plays some similar games
19820 for inline function definitions appearing within include files which
19821 also contain `#pragma interface' pragmas. */
19822 if (DECL_INITIAL (decl) == NULL_TREE)
19823 return;
19825 /* If we're a nested function, initially use a parent of NULL; if we're
19826 a plain function, this will be fixed up in decls_for_scope. If
19827 we're a method, it will be ignored, since we already have a DIE. */
19828 if (decl_function_context (decl)
19829 /* But if we're in terse mode, we don't care about scope. */
19830 && debug_info_level > DINFO_LEVEL_TERSE)
19831 context_die = NULL;
19832 break;
19834 case VAR_DECL:
19835 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
19836 declaration and if the declaration was never even referenced from
19837 within this entire compilation unit. We suppress these DIEs in
19838 order to save space in the .debug section (by eliminating entries
19839 which are probably useless). Note that we must not suppress
19840 block-local extern declarations (whether used or not) because that
19841 would screw-up the debugger's name lookup mechanism and cause it to
19842 miss things which really ought to be in scope at a given point. */
19843 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
19844 return;
19846 /* For local statics lookup proper context die. */
19847 if (TREE_STATIC (decl) && decl_function_context (decl))
19848 context_die = lookup_decl_die (DECL_CONTEXT (decl));
19850 /* If we are in terse mode, don't generate any DIEs to represent any
19851 variable declarations or definitions. */
19852 if (debug_info_level <= DINFO_LEVEL_TERSE)
19853 return;
19854 break;
19856 case CONST_DECL:
19857 if (debug_info_level <= DINFO_LEVEL_TERSE)
19858 return;
19859 if (!is_fortran () && !is_ada ())
19860 return;
19861 if (TREE_STATIC (decl) && decl_function_context (decl))
19862 context_die = lookup_decl_die (DECL_CONTEXT (decl));
19863 break;
19865 case NAMESPACE_DECL:
19866 case IMPORTED_DECL:
19867 if (debug_info_level <= DINFO_LEVEL_TERSE)
19868 return;
19869 if (lookup_decl_die (decl) != NULL)
19870 return;
19871 break;
19873 case TYPE_DECL:
19874 /* Don't emit stubs for types unless they are needed by other DIEs. */
19875 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
19876 return;
19878 /* Don't bother trying to generate any DIEs to represent any of the
19879 normal built-in types for the language we are compiling. */
19880 if (DECL_IS_BUILTIN (decl))
19881 return;
19883 /* If we are in terse mode, don't generate any DIEs for types. */
19884 if (debug_info_level <= DINFO_LEVEL_TERSE)
19885 return;
19887 /* If we're a function-scope tag, initially use a parent of NULL;
19888 this will be fixed up in decls_for_scope. */
19889 if (decl_function_context (decl))
19890 context_die = NULL;
19892 break;
19894 default:
19895 return;
19898 gen_decl_die (decl, NULL, context_die);
19901 /* Write the debugging output for DECL. */
19903 static void
19904 dwarf2out_function_decl (tree decl)
19906 dwarf2out_decl (decl);
19907 call_arg_locations = NULL;
19908 call_arg_loc_last = NULL;
19909 call_site_count = -1;
19910 tail_call_site_count = -1;
19911 VEC_free (dw_die_ref, heap, block_map);
19912 htab_empty (decl_loc_table);
19913 htab_empty (cached_dw_loc_list_table);
19916 /* Output a marker (i.e. a label) for the beginning of the generated code for
19917 a lexical block. */
19919 static void
19920 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
19921 unsigned int blocknum)
19923 switch_to_section (current_function_section ());
19924 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
19927 /* Output a marker (i.e. a label) for the end of the generated code for a
19928 lexical block. */
19930 static void
19931 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
19933 switch_to_section (current_function_section ());
19934 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
19937 /* Returns nonzero if it is appropriate not to emit any debugging
19938 information for BLOCK, because it doesn't contain any instructions.
19940 Don't allow this for blocks with nested functions or local classes
19941 as we would end up with orphans, and in the presence of scheduling
19942 we may end up calling them anyway. */
19944 static bool
19945 dwarf2out_ignore_block (const_tree block)
19947 tree decl;
19948 unsigned int i;
19950 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
19951 if (TREE_CODE (decl) == FUNCTION_DECL
19952 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
19953 return 0;
19954 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
19956 decl = BLOCK_NONLOCALIZED_VAR (block, i);
19957 if (TREE_CODE (decl) == FUNCTION_DECL
19958 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
19959 return 0;
19962 return 1;
19965 /* Hash table routines for file_hash. */
19967 static int
19968 file_table_eq (const void *p1_p, const void *p2_p)
19970 const struct dwarf_file_data *const p1 =
19971 (const struct dwarf_file_data *) p1_p;
19972 const char *const p2 = (const char *) p2_p;
19973 return filename_cmp (p1->filename, p2) == 0;
19976 static hashval_t
19977 file_table_hash (const void *p_p)
19979 const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
19980 return htab_hash_string (p->filename);
19983 /* Lookup FILE_NAME (in the list of filenames that we know about here in
19984 dwarf2out.c) and return its "index". The index of each (known) filename is
19985 just a unique number which is associated with only that one filename. We
19986 need such numbers for the sake of generating labels (in the .debug_sfnames
19987 section) and references to those files numbers (in the .debug_srcinfo
19988 and.debug_macinfo sections). If the filename given as an argument is not
19989 found in our current list, add it to the list and assign it the next
19990 available unique index number. In order to speed up searches, we remember
19991 the index of the filename was looked up last. This handles the majority of
19992 all searches. */
19994 static struct dwarf_file_data *
19995 lookup_filename (const char *file_name)
19997 void ** slot;
19998 struct dwarf_file_data * created;
20000 /* Check to see if the file name that was searched on the previous
20001 call matches this file name. If so, return the index. */
20002 if (file_table_last_lookup
20003 && (file_name == file_table_last_lookup->filename
20004 || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
20005 return file_table_last_lookup;
20007 /* Didn't match the previous lookup, search the table. */
20008 slot = htab_find_slot_with_hash (file_table, file_name,
20009 htab_hash_string (file_name), INSERT);
20010 if (*slot)
20011 return (struct dwarf_file_data *) *slot;
20013 created = ggc_alloc_dwarf_file_data ();
20014 created->filename = file_name;
20015 created->emitted_number = 0;
20016 *slot = created;
20017 return created;
20020 /* If the assembler will construct the file table, then translate the compiler
20021 internal file table number into the assembler file table number, and emit
20022 a .file directive if we haven't already emitted one yet. The file table
20023 numbers are different because we prune debug info for unused variables and
20024 types, which may include filenames. */
20026 static int
20027 maybe_emit_file (struct dwarf_file_data * fd)
20029 if (! fd->emitted_number)
20031 if (last_emitted_file)
20032 fd->emitted_number = last_emitted_file->emitted_number + 1;
20033 else
20034 fd->emitted_number = 1;
20035 last_emitted_file = fd;
20037 if (DWARF2_ASM_LINE_DEBUG_INFO)
20039 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
20040 output_quoted_string (asm_out_file,
20041 remap_debug_filename (fd->filename));
20042 fputc ('\n', asm_out_file);
20046 return fd->emitted_number;
20049 /* Schedule generation of a DW_AT_const_value attribute to DIE.
20050 That generation should happen after function debug info has been
20051 generated. The value of the attribute is the constant value of ARG. */
20053 static void
20054 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
20056 die_arg_entry entry;
20058 if (!die || !arg)
20059 return;
20061 if (!tmpl_value_parm_die_table)
20062 tmpl_value_parm_die_table
20063 = VEC_alloc (die_arg_entry, gc, 32);
20065 entry.die = die;
20066 entry.arg = arg;
20067 VEC_safe_push (die_arg_entry, gc,
20068 tmpl_value_parm_die_table,
20069 &entry);
20072 /* Return TRUE if T is an instance of generic type, FALSE
20073 otherwise. */
20075 static bool
20076 generic_type_p (tree t)
20078 if (t == NULL_TREE || !TYPE_P (t))
20079 return false;
20080 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
20083 /* Schedule the generation of the generic parameter dies for the
20084 instance of generic type T. The proper generation itself is later
20085 done by gen_scheduled_generic_parms_dies. */
20087 static void
20088 schedule_generic_params_dies_gen (tree t)
20090 if (!generic_type_p (t))
20091 return;
20093 if (generic_type_instances == NULL)
20094 generic_type_instances = VEC_alloc (tree, gc, 256);
20096 VEC_safe_push (tree, gc, generic_type_instances, t);
20099 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
20100 by append_entry_to_tmpl_value_parm_die_table. This function must
20101 be called after function DIEs have been generated. */
20103 static void
20104 gen_remaining_tmpl_value_param_die_attribute (void)
20106 if (tmpl_value_parm_die_table)
20108 unsigned i;
20109 die_arg_entry *e;
20111 FOR_EACH_VEC_ELT (die_arg_entry, tmpl_value_parm_die_table, i, e)
20112 tree_add_const_value_attribute (e->die, e->arg);
20116 /* Generate generic parameters DIEs for instances of generic types
20117 that have been previously scheduled by
20118 schedule_generic_params_dies_gen. This function must be called
20119 after all the types of the CU have been laid out. */
20121 static void
20122 gen_scheduled_generic_parms_dies (void)
20124 unsigned i;
20125 tree t;
20127 if (generic_type_instances == NULL)
20128 return;
20130 FOR_EACH_VEC_ELT (tree, generic_type_instances, i, t)
20131 gen_generic_params_dies (t);
20135 /* Replace DW_AT_name for the decl with name. */
20137 static void
20138 dwarf2out_set_name (tree decl, tree name)
20140 dw_die_ref die;
20141 dw_attr_ref attr;
20142 const char *dname;
20144 die = TYPE_SYMTAB_DIE (decl);
20145 if (!die)
20146 return;
20148 dname = dwarf2_name (name, 0);
20149 if (!dname)
20150 return;
20152 attr = get_AT (die, DW_AT_name);
20153 if (attr)
20155 struct indirect_string_node *node;
20157 node = find_AT_string (dname);
20158 /* replace the string. */
20159 attr->dw_attr_val.v.val_str = node;
20162 else
20163 add_name_attribute (die, dname);
20166 /* Called by the final INSN scan whenever we see a var location. We
20167 use it to drop labels in the right places, and throw the location in
20168 our lookup table. */
20170 static void
20171 dwarf2out_var_location (rtx loc_note)
20173 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
20174 struct var_loc_node *newloc;
20175 rtx next_real;
20176 static const char *last_label;
20177 static const char *last_postcall_label;
20178 static bool last_in_cold_section_p;
20179 tree decl;
20180 bool var_loc_p;
20182 if (!NOTE_P (loc_note))
20184 if (CALL_P (loc_note))
20186 call_site_count++;
20187 if (SIBLING_CALL_P (loc_note))
20188 tail_call_site_count++;
20190 return;
20193 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
20194 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
20195 return;
20197 next_real = next_real_insn (loc_note);
20199 /* If there are no instructions which would be affected by this note,
20200 don't do anything. */
20201 if (var_loc_p
20202 && next_real == NULL_RTX
20203 && !NOTE_DURING_CALL_P (loc_note))
20204 return;
20206 if (next_real == NULL_RTX)
20207 next_real = get_last_insn ();
20209 /* If there were any real insns between note we processed last time
20210 and this note (or if it is the first note), clear
20211 last_{,postcall_}label so that they are not reused this time. */
20212 if (last_var_location_insn == NULL_RTX
20213 || last_var_location_insn != next_real
20214 || last_in_cold_section_p != in_cold_section_p)
20216 last_label = NULL;
20217 last_postcall_label = NULL;
20220 if (var_loc_p)
20222 decl = NOTE_VAR_LOCATION_DECL (loc_note);
20223 newloc = add_var_loc_to_decl (decl, loc_note,
20224 NOTE_DURING_CALL_P (loc_note)
20225 ? last_postcall_label : last_label);
20226 if (newloc == NULL)
20227 return;
20229 else
20231 decl = NULL_TREE;
20232 newloc = NULL;
20235 /* If there were no real insns between note we processed last time
20236 and this note, use the label we emitted last time. Otherwise
20237 create a new label and emit it. */
20238 if (last_label == NULL)
20240 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
20241 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
20242 loclabel_num++;
20243 last_label = ggc_strdup (loclabel);
20246 if (!var_loc_p)
20248 struct call_arg_loc_node *ca_loc
20249 = ggc_alloc_cleared_call_arg_loc_node ();
20250 rtx prev = prev_real_insn (loc_note), x;
20251 ca_loc->call_arg_loc_note = loc_note;
20252 ca_loc->next = NULL;
20253 ca_loc->label = last_label;
20254 gcc_assert (prev
20255 && (CALL_P (prev)
20256 || (NONJUMP_INSN_P (prev)
20257 && GET_CODE (PATTERN (prev)) == SEQUENCE
20258 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
20259 if (!CALL_P (prev))
20260 prev = XVECEXP (PATTERN (prev), 0, 0);
20261 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
20262 x = PATTERN (prev);
20263 if (GET_CODE (x) == PARALLEL)
20264 x = XVECEXP (x, 0, 0);
20265 if (GET_CODE (x) == SET)
20266 x = SET_SRC (x);
20267 if (GET_CODE (x) == CALL && MEM_P (XEXP (x, 0)))
20269 x = XEXP (XEXP (x, 0), 0);
20270 if (GET_CODE (x) == SYMBOL_REF
20271 && SYMBOL_REF_DECL (x)
20272 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
20273 ca_loc->symbol_ref = x;
20275 ca_loc->block = insn_scope (prev);
20276 if (call_arg_locations)
20277 call_arg_loc_last->next = ca_loc;
20278 else
20279 call_arg_locations = ca_loc;
20280 call_arg_loc_last = ca_loc;
20282 else if (!NOTE_DURING_CALL_P (loc_note))
20283 newloc->label = last_label;
20284 else
20286 if (!last_postcall_label)
20288 sprintf (loclabel, "%s-1", last_label);
20289 last_postcall_label = ggc_strdup (loclabel);
20291 newloc->label = last_postcall_label;
20294 last_var_location_insn = next_real;
20295 last_in_cold_section_p = in_cold_section_p;
20298 /* Note in one location list that text section has changed. */
20300 static int
20301 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
20303 var_loc_list *list = (var_loc_list *) *slot;
20304 if (list->first)
20305 list->last_before_switch
20306 = list->last->next ? list->last->next : list->last;
20307 return 1;
20310 /* Note in all location lists that text section has changed. */
20312 static void
20313 var_location_switch_text_section (void)
20315 if (decl_loc_table == NULL)
20316 return;
20318 htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
20321 /* Create a new line number table. */
20323 static dw_line_info_table *
20324 new_line_info_table (void)
20326 dw_line_info_table *table;
20328 table = ggc_alloc_cleared_dw_line_info_table_struct ();
20329 table->file_num = 1;
20330 table->line_num = 1;
20331 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
20333 return table;
20336 /* Lookup the "current" table into which we emit line info, so
20337 that we don't have to do it for every source line. */
20339 static void
20340 set_cur_line_info_table (section *sec)
20342 dw_line_info_table *table;
20344 if (sec == text_section)
20345 table = text_section_line_info;
20346 else if (sec == cold_text_section)
20348 table = cold_text_section_line_info;
20349 if (!table)
20351 cold_text_section_line_info = table = new_line_info_table ();
20352 table->end_label = cold_end_label;
20355 else
20357 const char *end_label;
20359 if (flag_reorder_blocks_and_partition)
20361 if (in_cold_section_p)
20362 end_label = crtl->subsections.cold_section_end_label;
20363 else
20364 end_label = crtl->subsections.hot_section_end_label;
20366 else
20368 char label[MAX_ARTIFICIAL_LABEL_BYTES];
20369 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
20370 current_function_funcdef_no);
20371 end_label = ggc_strdup (label);
20374 table = new_line_info_table ();
20375 table->end_label = end_label;
20377 VEC_safe_push (dw_line_info_table_p, gc, separate_line_info, table);
20380 cur_line_info_table = table;
20384 /* We need to reset the locations at the beginning of each
20385 function. We can't do this in the end_function hook, because the
20386 declarations that use the locations won't have been output when
20387 that hook is called. Also compute have_multiple_function_sections here. */
20389 static void
20390 dwarf2out_begin_function (tree fun)
20392 section *sec = function_section (fun);
20394 if (sec != text_section)
20395 have_multiple_function_sections = true;
20397 if (flag_reorder_blocks_and_partition && !cold_text_section)
20399 gcc_assert (current_function_decl == fun);
20400 cold_text_section = unlikely_text_section ();
20401 switch_to_section (cold_text_section);
20402 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
20403 switch_to_section (sec);
20406 dwarf2out_note_section_used ();
20407 call_site_count = 0;
20408 tail_call_site_count = 0;
20410 set_cur_line_info_table (sec);
20413 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
20415 static void
20416 push_dw_line_info_entry (dw_line_info_table *table,
20417 enum dw_line_info_opcode opcode, unsigned int val)
20419 dw_line_info_entry e;
20420 e.opcode = opcode;
20421 e.val = val;
20422 VEC_safe_push (dw_line_info_entry, gc, table->entries, &e);
20425 /* Output a label to mark the beginning of a source code line entry
20426 and record information relating to this source line, in
20427 'line_info_table' for later output of the .debug_line section. */
20428 /* ??? The discriminator parameter ought to be unsigned. */
20430 static void
20431 dwarf2out_source_line (unsigned int line, const char *filename,
20432 int discriminator, bool is_stmt)
20434 unsigned int file_num;
20435 dw_line_info_table *table;
20437 if (debug_info_level < DINFO_LEVEL_NORMAL || line == 0)
20438 return;
20440 /* The discriminator column was added in dwarf4. Simplify the below
20441 by simply removing it if we're not supposed to output it. */
20442 if (dwarf_version < 4 && dwarf_strict)
20443 discriminator = 0;
20445 table = cur_line_info_table;
20446 file_num = maybe_emit_file (lookup_filename (filename));
20448 /* ??? TODO: Elide duplicate line number entries. Traditionally,
20449 the debugger has used the second (possibly duplicate) line number
20450 at the beginning of the function to mark the end of the prologue.
20451 We could eliminate any other duplicates within the function. For
20452 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
20453 that second line number entry. */
20454 /* Recall that this end-of-prologue indication is *not* the same thing
20455 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
20456 to which the hook corresponds, follows the last insn that was
20457 emitted by gen_prologue. What we need is to preceed the first insn
20458 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
20459 insn that corresponds to something the user wrote. These may be
20460 very different locations once scheduling is enabled. */
20462 if (0 && file_num == table->file_num
20463 && line == table->line_num
20464 && discriminator == table->discrim_num
20465 && is_stmt == table->is_stmt)
20466 return;
20468 switch_to_section (current_function_section ());
20470 /* If requested, emit something human-readable. */
20471 if (flag_debug_asm)
20472 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
20474 if (DWARF2_ASM_LINE_DEBUG_INFO)
20476 /* Emit the .loc directive understood by GNU as. */
20477 fprintf (asm_out_file, "\t.loc %d %d 0", file_num, line);
20478 if (is_stmt != table->is_stmt)
20479 fprintf (asm_out_file, " is_stmt %d", is_stmt ? 1 : 0);
20480 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
20481 fprintf (asm_out_file, " discriminator %d", discriminator);
20482 fputc ('\n', asm_out_file);
20484 else
20486 unsigned int label_num = ++line_info_label_num;
20488 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
20490 push_dw_line_info_entry (table, LI_set_address, label_num);
20491 if (file_num != table->file_num)
20492 push_dw_line_info_entry (table, LI_set_file, file_num);
20493 if (discriminator != table->discrim_num)
20494 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
20495 if (is_stmt != table->is_stmt)
20496 push_dw_line_info_entry (table, LI_negate_stmt, 0);
20497 push_dw_line_info_entry (table, LI_set_line, line);
20500 table->file_num = file_num;
20501 table->line_num = line;
20502 table->discrim_num = discriminator;
20503 table->is_stmt = is_stmt;
20504 table->in_use = true;
20507 /* Record the beginning of a new source file. */
20509 static void
20510 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
20512 if (flag_eliminate_dwarf2_dups && ! use_debug_types)
20514 /* Record the beginning of the file for break_out_includes. */
20515 dw_die_ref bincl_die;
20517 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
20518 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
20521 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20523 macinfo_entry e;
20524 e.code = DW_MACINFO_start_file;
20525 e.lineno = lineno;
20526 e.info = xstrdup (filename);
20527 VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20531 /* Record the end of a source file. */
20533 static void
20534 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
20536 if (flag_eliminate_dwarf2_dups && ! use_debug_types)
20537 /* Record the end of the file for break_out_includes. */
20538 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
20540 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20542 macinfo_entry e;
20543 e.code = DW_MACINFO_end_file;
20544 e.lineno = lineno;
20545 e.info = NULL;
20546 VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20550 /* Called from debug_define in toplev.c. The `buffer' parameter contains
20551 the tail part of the directive line, i.e. the part which is past the
20552 initial whitespace, #, whitespace, directive-name, whitespace part. */
20554 static void
20555 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
20556 const char *buffer ATTRIBUTE_UNUSED)
20558 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20560 macinfo_entry e;
20561 /* Insert a dummy first entry to be able to optimize the whole
20562 predefined macro block using DW_MACRO_GNU_transparent_include. */
20563 if (VEC_empty (macinfo_entry, macinfo_table) && lineno == 0)
20565 e.code = 0;
20566 e.lineno = 0;
20567 e.info = NULL;
20568 VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20570 e.code = DW_MACINFO_define;
20571 e.lineno = lineno;
20572 e.info = xstrdup (buffer);;
20573 VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20577 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
20578 the tail part of the directive line, i.e. the part which is past the
20579 initial whitespace, #, whitespace, directive-name, whitespace part. */
20581 static void
20582 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
20583 const char *buffer ATTRIBUTE_UNUSED)
20585 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20587 macinfo_entry e;
20588 /* Insert a dummy first entry to be able to optimize the whole
20589 predefined macro block using DW_MACRO_GNU_transparent_include. */
20590 if (VEC_empty (macinfo_entry, macinfo_table) && lineno == 0)
20592 e.code = 0;
20593 e.lineno = 0;
20594 e.info = NULL;
20595 VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20597 e.code = DW_MACINFO_undef;
20598 e.lineno = lineno;
20599 e.info = xstrdup (buffer);
20600 VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20604 /* Routines to manipulate hash table of CUs. */
20606 static hashval_t
20607 htab_macinfo_hash (const void *of)
20609 const macinfo_entry *const entry =
20610 (const macinfo_entry *) of;
20612 return htab_hash_string (entry->info);
20615 static int
20616 htab_macinfo_eq (const void *of1, const void *of2)
20618 const macinfo_entry *const entry1 = (const macinfo_entry *) of1;
20619 const macinfo_entry *const entry2 = (const macinfo_entry *) of2;
20621 return !strcmp (entry1->info, entry2->info);
20624 /* Output a single .debug_macinfo entry. */
20626 static void
20627 output_macinfo_op (macinfo_entry *ref)
20629 int file_num;
20630 size_t len;
20631 struct indirect_string_node *node;
20632 char label[MAX_ARTIFICIAL_LABEL_BYTES];
20633 struct dwarf_file_data *fd;
20635 switch (ref->code)
20637 case DW_MACINFO_start_file:
20638 fd = lookup_filename (ref->info);
20639 if (fd->filename == ref->info)
20640 fd->filename = ggc_strdup (fd->filename);
20641 file_num = maybe_emit_file (fd);
20642 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
20643 dw2_asm_output_data_uleb128 (ref->lineno,
20644 "Included from line number %lu",
20645 (unsigned long) ref->lineno);
20646 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
20647 break;
20648 case DW_MACINFO_end_file:
20649 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
20650 break;
20651 case DW_MACINFO_define:
20652 case DW_MACINFO_undef:
20653 len = strlen (ref->info) + 1;
20654 if (!dwarf_strict
20655 && len > DWARF_OFFSET_SIZE
20656 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
20657 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
20659 ref->code = ref->code == DW_MACINFO_define
20660 ? DW_MACRO_GNU_define_indirect
20661 : DW_MACRO_GNU_undef_indirect;
20662 output_macinfo_op (ref);
20663 return;
20665 dw2_asm_output_data (1, ref->code,
20666 ref->code == DW_MACINFO_define
20667 ? "Define macro" : "Undefine macro");
20668 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
20669 (unsigned long) ref->lineno);
20670 dw2_asm_output_nstring (ref->info, -1, "The macro");
20671 break;
20672 case DW_MACRO_GNU_define_indirect:
20673 case DW_MACRO_GNU_undef_indirect:
20674 node = find_AT_string (ref->info);
20675 if (node->form != DW_FORM_strp)
20677 char label[32];
20678 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
20679 ++dw2_string_counter;
20680 node->label = xstrdup (label);
20681 node->form = DW_FORM_strp;
20683 dw2_asm_output_data (1, ref->code,
20684 ref->code == DW_MACRO_GNU_define_indirect
20685 ? "Define macro indirect"
20686 : "Undefine macro indirect");
20687 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
20688 (unsigned long) ref->lineno);
20689 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
20690 debug_str_section, "The macro: \"%s\"",
20691 ref->info);
20692 break;
20693 case DW_MACRO_GNU_transparent_include:
20694 dw2_asm_output_data (1, ref->code, "Transparent include");
20695 ASM_GENERATE_INTERNAL_LABEL (label,
20696 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
20697 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
20698 break;
20699 default:
20700 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
20701 ASM_COMMENT_START, (unsigned long) ref->code);
20702 break;
20706 /* Attempt to make a sequence of define/undef macinfo ops shareable with
20707 other compilation unit .debug_macinfo sections. IDX is the first
20708 index of a define/undef, return the number of ops that should be
20709 emitted in a comdat .debug_macinfo section and emit
20710 a DW_MACRO_GNU_transparent_include entry referencing it.
20711 If the define/undef entry should be emitted normally, return 0. */
20713 static unsigned
20714 optimize_macinfo_range (unsigned int idx, VEC (macinfo_entry, gc) *files,
20715 htab_t *macinfo_htab)
20717 macinfo_entry *first, *second, *cur, *inc;
20718 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
20719 unsigned char checksum[16];
20720 struct md5_ctx ctx;
20721 char *grp_name, *tail;
20722 const char *base;
20723 unsigned int i, count, encoded_filename_len, linebuf_len;
20724 void **slot;
20726 first = VEC_index (macinfo_entry, macinfo_table, idx);
20727 second = VEC_index (macinfo_entry, macinfo_table, idx + 1);
20729 /* Optimize only if there are at least two consecutive define/undef ops,
20730 and either all of them are before first DW_MACINFO_start_file
20731 with lineno 0 (i.e. predefined macro block), or all of them are
20732 in some included header file. */
20733 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
20734 return 0;
20735 if (VEC_empty (macinfo_entry, files))
20737 if (first->lineno != 0 || second->lineno != 0)
20738 return 0;
20740 else if (first->lineno == 0)
20741 return 0;
20743 /* Find the last define/undef entry that can be grouped together
20744 with first and at the same time compute md5 checksum of their
20745 codes, linenumbers and strings. */
20746 md5_init_ctx (&ctx);
20747 for (i = idx; VEC_iterate (macinfo_entry, macinfo_table, i, cur); i++)
20748 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
20749 break;
20750 else if (first->lineno == 0 && cur->lineno != 0)
20751 break;
20752 else
20754 unsigned char code = cur->code;
20755 md5_process_bytes (&code, 1, &ctx);
20756 checksum_uleb128 (cur->lineno, &ctx);
20757 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
20759 md5_finish_ctx (&ctx, checksum);
20760 count = i - idx;
20762 /* From the containing include filename (if any) pick up just
20763 usable characters from its basename. */
20764 if (first->lineno == 0)
20765 base = "";
20766 else
20767 base = lbasename (VEC_last (macinfo_entry, files)->info);
20768 for (encoded_filename_len = 0, i = 0; base[i]; i++)
20769 if (ISIDNUM (base[i]) || base[i] == '.')
20770 encoded_filename_len++;
20771 /* Count . at the end. */
20772 if (encoded_filename_len)
20773 encoded_filename_len++;
20775 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
20776 linebuf_len = strlen (linebuf);
20778 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
20779 grp_name = XNEWVEC (char, 4 + encoded_filename_len + linebuf_len + 1
20780 + 16 * 2 + 1);
20781 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
20782 tail = grp_name + 4;
20783 if (encoded_filename_len)
20785 for (i = 0; base[i]; i++)
20786 if (ISIDNUM (base[i]) || base[i] == '.')
20787 *tail++ = base[i];
20788 *tail++ = '.';
20790 memcpy (tail, linebuf, linebuf_len);
20791 tail += linebuf_len;
20792 *tail++ = '.';
20793 for (i = 0; i < 16; i++)
20794 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
20796 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
20797 in the empty vector entry before the first define/undef. */
20798 inc = VEC_index (macinfo_entry, macinfo_table, idx - 1);
20799 inc->code = DW_MACRO_GNU_transparent_include;
20800 inc->lineno = 0;
20801 inc->info = grp_name;
20802 if (*macinfo_htab == NULL)
20803 *macinfo_htab = htab_create (10, htab_macinfo_hash, htab_macinfo_eq, NULL);
20804 /* Avoid emitting duplicates. */
20805 slot = htab_find_slot (*macinfo_htab, inc, INSERT);
20806 if (*slot != NULL)
20808 free (CONST_CAST (char *, inc->info));
20809 inc->code = 0;
20810 inc->info = NULL;
20811 /* If such an entry has been used before, just emit
20812 a DW_MACRO_GNU_transparent_include op. */
20813 inc = (macinfo_entry *) *slot;
20814 output_macinfo_op (inc);
20815 /* And clear all macinfo_entry in the range to avoid emitting them
20816 in the second pass. */
20817 for (i = idx;
20818 VEC_iterate (macinfo_entry, macinfo_table, i, cur)
20819 && i < idx + count;
20820 i++)
20822 cur->code = 0;
20823 free (CONST_CAST (char *, cur->info));
20824 cur->info = NULL;
20827 else
20829 *slot = inc;
20830 inc->lineno = htab_elements (*macinfo_htab);
20831 output_macinfo_op (inc);
20833 return count;
20836 /* Output macinfo section(s). */
20838 static void
20839 output_macinfo (void)
20841 unsigned i;
20842 unsigned long length = VEC_length (macinfo_entry, macinfo_table);
20843 macinfo_entry *ref;
20844 VEC (macinfo_entry, gc) *files = NULL;
20845 htab_t macinfo_htab = NULL;
20847 if (! length)
20848 return;
20850 /* output_macinfo* uses these interchangeably. */
20851 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
20852 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
20853 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
20854 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
20856 /* For .debug_macro emit the section header. */
20857 if (!dwarf_strict)
20859 dw2_asm_output_data (2, 4, "DWARF macro version number");
20860 if (DWARF_OFFSET_SIZE == 8)
20861 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
20862 else
20863 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
20864 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_line_section_label,
20865 debug_line_section, NULL);
20868 /* In the first loop, it emits the primary .debug_macinfo section
20869 and after each emitted op the macinfo_entry is cleared.
20870 If a longer range of define/undef ops can be optimized using
20871 DW_MACRO_GNU_transparent_include, the
20872 DW_MACRO_GNU_transparent_include op is emitted and kept in
20873 the vector before the first define/undef in the range and the
20874 whole range of define/undef ops is not emitted and kept. */
20875 for (i = 0; VEC_iterate (macinfo_entry, macinfo_table, i, ref); i++)
20877 switch (ref->code)
20879 case DW_MACINFO_start_file:
20880 VEC_safe_push (macinfo_entry, gc, files, ref);
20881 break;
20882 case DW_MACINFO_end_file:
20883 if (!VEC_empty (macinfo_entry, files))
20885 macinfo_entry *file = VEC_last (macinfo_entry, files);
20886 free (CONST_CAST (char *, file->info));
20887 VEC_pop (macinfo_entry, files);
20889 break;
20890 case DW_MACINFO_define:
20891 case DW_MACINFO_undef:
20892 if (!dwarf_strict
20893 && HAVE_COMDAT_GROUP
20894 && VEC_length (macinfo_entry, files) != 1
20895 && i > 0
20896 && i + 1 < length
20897 && VEC_index (macinfo_entry, macinfo_table, i - 1)->code == 0)
20899 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
20900 if (count)
20902 i += count - 1;
20903 continue;
20906 break;
20907 case 0:
20908 /* A dummy entry may be inserted at the beginning to be able
20909 to optimize the whole block of predefined macros. */
20910 if (i == 0)
20911 continue;
20912 default:
20913 break;
20915 output_macinfo_op (ref);
20916 /* For DW_MACINFO_start_file ref->info has been copied into files
20917 vector. */
20918 if (ref->code != DW_MACINFO_start_file)
20919 free (CONST_CAST (char *, ref->info));
20920 ref->info = NULL;
20921 ref->code = 0;
20924 if (macinfo_htab == NULL)
20925 return;
20927 htab_delete (macinfo_htab);
20929 /* If any DW_MACRO_GNU_transparent_include were used, on those
20930 DW_MACRO_GNU_transparent_include entries terminate the
20931 current chain and switch to a new comdat .debug_macinfo
20932 section and emit the define/undef entries within it. */
20933 for (i = 0; VEC_iterate (macinfo_entry, macinfo_table, i, ref); i++)
20934 switch (ref->code)
20936 case 0:
20937 continue;
20938 case DW_MACRO_GNU_transparent_include:
20940 char label[MAX_ARTIFICIAL_LABEL_BYTES];
20941 tree comdat_key = get_identifier (ref->info);
20942 /* Terminate the previous .debug_macinfo section. */
20943 dw2_asm_output_data (1, 0, "End compilation unit");
20944 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
20945 SECTION_DEBUG
20946 | SECTION_LINKONCE,
20947 comdat_key);
20948 ASM_GENERATE_INTERNAL_LABEL (label,
20949 DEBUG_MACRO_SECTION_LABEL,
20950 ref->lineno);
20951 ASM_OUTPUT_LABEL (asm_out_file, label);
20952 ref->code = 0;
20953 free (CONST_CAST (char *, ref->info));
20954 ref->info = NULL;
20955 dw2_asm_output_data (2, 4, "DWARF macro version number");
20956 if (DWARF_OFFSET_SIZE == 8)
20957 dw2_asm_output_data (1, 1, "Flags: 64-bit");
20958 else
20959 dw2_asm_output_data (1, 0, "Flags: 32-bit");
20961 break;
20962 case DW_MACINFO_define:
20963 case DW_MACINFO_undef:
20964 output_macinfo_op (ref);
20965 ref->code = 0;
20966 free (CONST_CAST (char *, ref->info));
20967 ref->info = NULL;
20968 break;
20969 default:
20970 gcc_unreachable ();
20974 /* Set up for Dwarf output at the start of compilation. */
20976 static void
20977 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
20979 /* Allocate the file_table. */
20980 file_table = htab_create_ggc (50, file_table_hash,
20981 file_table_eq, NULL);
20983 /* Allocate the decl_die_table. */
20984 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
20985 decl_die_table_eq, NULL);
20987 /* Allocate the decl_loc_table. */
20988 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
20989 decl_loc_table_eq, NULL);
20991 /* Allocate the cached_dw_loc_list_table. */
20992 cached_dw_loc_list_table
20993 = htab_create_ggc (10, cached_dw_loc_list_table_hash,
20994 cached_dw_loc_list_table_eq, NULL);
20996 /* Allocate the initial hunk of the decl_scope_table. */
20997 decl_scope_table = VEC_alloc (tree, gc, 256);
20999 /* Allocate the initial hunk of the abbrev_die_table. */
21000 abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
21001 (ABBREV_DIE_TABLE_INCREMENT);
21002 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
21003 /* Zero-th entry is allocated, but unused. */
21004 abbrev_die_table_in_use = 1;
21006 /* Allocate the pubtypes and pubnames vectors. */
21007 pubname_table = VEC_alloc (pubname_entry, gc, 32);
21008 pubtype_table = VEC_alloc (pubname_entry, gc, 32);
21010 incomplete_types = VEC_alloc (tree, gc, 64);
21012 used_rtx_array = VEC_alloc (rtx, gc, 32);
21014 debug_info_section = get_section (DEBUG_INFO_SECTION,
21015 SECTION_DEBUG, NULL);
21016 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
21017 SECTION_DEBUG, NULL);
21018 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
21019 SECTION_DEBUG, NULL);
21020 debug_macinfo_section = get_section (dwarf_strict
21021 ? DEBUG_MACINFO_SECTION
21022 : DEBUG_MACRO_SECTION,
21023 SECTION_DEBUG, NULL);
21024 debug_line_section = get_section (DEBUG_LINE_SECTION,
21025 SECTION_DEBUG, NULL);
21026 debug_loc_section = get_section (DEBUG_LOC_SECTION,
21027 SECTION_DEBUG, NULL);
21028 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
21029 SECTION_DEBUG, NULL);
21030 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
21031 SECTION_DEBUG, NULL);
21032 debug_str_section = get_section (DEBUG_STR_SECTION,
21033 DEBUG_STR_SECTION_FLAGS, NULL);
21034 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
21035 SECTION_DEBUG, NULL);
21036 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
21037 SECTION_DEBUG, NULL);
21039 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
21040 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
21041 DEBUG_ABBREV_SECTION_LABEL, 0);
21042 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
21043 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
21044 COLD_TEXT_SECTION_LABEL, 0);
21045 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
21047 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
21048 DEBUG_INFO_SECTION_LABEL, 0);
21049 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
21050 DEBUG_LINE_SECTION_LABEL, 0);
21051 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
21052 DEBUG_RANGES_SECTION_LABEL, 0);
21053 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
21054 dwarf_strict
21055 ? DEBUG_MACINFO_SECTION_LABEL
21056 : DEBUG_MACRO_SECTION_LABEL, 0);
21058 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21059 macinfo_table = VEC_alloc (macinfo_entry, gc, 64);
21061 switch_to_section (text_section);
21062 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
21064 /* Make sure the line number table for .text always exists. */
21065 text_section_line_info = new_line_info_table ();
21066 text_section_line_info->end_label = text_end_label;
21069 /* Called before cgraph_optimize starts outputtting functions, variables
21070 and toplevel asms into assembly. */
21072 static void
21073 dwarf2out_assembly_start (void)
21075 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
21076 && dwarf2out_do_cfi_asm ()
21077 && (!(flag_unwind_tables || flag_exceptions)
21078 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
21079 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
21082 /* A helper function for dwarf2out_finish called through
21083 htab_traverse. Emit one queued .debug_str string. */
21085 static int
21086 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
21088 struct indirect_string_node *node = (struct indirect_string_node *) *h;
21090 if (node->form == DW_FORM_strp)
21092 switch_to_section (debug_str_section);
21093 ASM_OUTPUT_LABEL (asm_out_file, node->label);
21094 assemble_string (node->str, strlen (node->str) + 1);
21097 return 1;
21100 #if ENABLE_ASSERT_CHECKING
21101 /* Verify that all marks are clear. */
21103 static void
21104 verify_marks_clear (dw_die_ref die)
21106 dw_die_ref c;
21108 gcc_assert (! die->die_mark);
21109 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
21111 #endif /* ENABLE_ASSERT_CHECKING */
21113 /* Clear the marks for a die and its children.
21114 Be cool if the mark isn't set. */
21116 static void
21117 prune_unmark_dies (dw_die_ref die)
21119 dw_die_ref c;
21121 if (die->die_mark)
21122 die->die_mark = 0;
21123 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
21126 /* Given DIE that we're marking as used, find any other dies
21127 it references as attributes and mark them as used. */
21129 static void
21130 prune_unused_types_walk_attribs (dw_die_ref die)
21132 dw_attr_ref a;
21133 unsigned ix;
21135 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
21137 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
21139 /* A reference to another DIE.
21140 Make sure that it will get emitted.
21141 If it was broken out into a comdat group, don't follow it. */
21142 if (! use_debug_types
21143 || a->dw_attr == DW_AT_specification
21144 || a->dw_attr_val.v.val_die_ref.die->die_id.die_type_node == NULL)
21145 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
21147 /* Set the string's refcount to 0 so that prune_unused_types_mark
21148 accounts properly for it. */
21149 if (AT_class (a) == dw_val_class_str)
21150 a->dw_attr_val.v.val_str->refcount = 0;
21154 /* Mark the generic parameters and arguments children DIEs of DIE. */
21156 static void
21157 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
21159 dw_die_ref c;
21161 if (die == NULL || die->die_child == NULL)
21162 return;
21163 c = die->die_child;
21166 switch (c->die_tag)
21168 case DW_TAG_template_type_param:
21169 case DW_TAG_template_value_param:
21170 case DW_TAG_GNU_template_template_param:
21171 case DW_TAG_GNU_template_parameter_pack:
21172 prune_unused_types_mark (c, 1);
21173 break;
21174 default:
21175 break;
21177 c = c->die_sib;
21178 } while (c && c != die->die_child);
21181 /* Mark DIE as being used. If DOKIDS is true, then walk down
21182 to DIE's children. */
21184 static void
21185 prune_unused_types_mark (dw_die_ref die, int dokids)
21187 dw_die_ref c;
21189 if (die->die_mark == 0)
21191 /* We haven't done this node yet. Mark it as used. */
21192 die->die_mark = 1;
21193 /* If this is the DIE of a generic type instantiation,
21194 mark the children DIEs that describe its generic parms and
21195 args. */
21196 prune_unused_types_mark_generic_parms_dies (die);
21198 /* We also have to mark its parents as used.
21199 (But we don't want to mark our parents' kids due to this.) */
21200 if (die->die_parent)
21201 prune_unused_types_mark (die->die_parent, 0);
21203 /* Mark any referenced nodes. */
21204 prune_unused_types_walk_attribs (die);
21206 /* If this node is a specification,
21207 also mark the definition, if it exists. */
21208 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
21209 prune_unused_types_mark (die->die_definition, 1);
21212 if (dokids && die->die_mark != 2)
21214 /* We need to walk the children, but haven't done so yet.
21215 Remember that we've walked the kids. */
21216 die->die_mark = 2;
21218 /* If this is an array type, we need to make sure our
21219 kids get marked, even if they're types. If we're
21220 breaking out types into comdat sections, do this
21221 for all type definitions. */
21222 if (die->die_tag == DW_TAG_array_type
21223 || (use_debug_types
21224 && is_type_die (die) && ! is_declaration_die (die)))
21225 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
21226 else
21227 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
21231 /* For local classes, look if any static member functions were emitted
21232 and if so, mark them. */
21234 static void
21235 prune_unused_types_walk_local_classes (dw_die_ref die)
21237 dw_die_ref c;
21239 if (die->die_mark == 2)
21240 return;
21242 switch (die->die_tag)
21244 case DW_TAG_structure_type:
21245 case DW_TAG_union_type:
21246 case DW_TAG_class_type:
21247 break;
21249 case DW_TAG_subprogram:
21250 if (!get_AT_flag (die, DW_AT_declaration)
21251 || die->die_definition != NULL)
21252 prune_unused_types_mark (die, 1);
21253 return;
21255 default:
21256 return;
21259 /* Mark children. */
21260 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
21263 /* Walk the tree DIE and mark types that we actually use. */
21265 static void
21266 prune_unused_types_walk (dw_die_ref die)
21268 dw_die_ref c;
21270 /* Don't do anything if this node is already marked and
21271 children have been marked as well. */
21272 if (die->die_mark == 2)
21273 return;
21275 switch (die->die_tag)
21277 case DW_TAG_structure_type:
21278 case DW_TAG_union_type:
21279 case DW_TAG_class_type:
21280 if (die->die_perennial_p)
21281 break;
21283 for (c = die->die_parent; c; c = c->die_parent)
21284 if (c->die_tag == DW_TAG_subprogram)
21285 break;
21287 /* Finding used static member functions inside of classes
21288 is needed just for local classes, because for other classes
21289 static member function DIEs with DW_AT_specification
21290 are emitted outside of the DW_TAG_*_type. If we ever change
21291 it, we'd need to call this even for non-local classes. */
21292 if (c)
21293 prune_unused_types_walk_local_classes (die);
21295 /* It's a type node --- don't mark it. */
21296 return;
21298 case DW_TAG_const_type:
21299 case DW_TAG_packed_type:
21300 case DW_TAG_pointer_type:
21301 case DW_TAG_reference_type:
21302 case DW_TAG_rvalue_reference_type:
21303 case DW_TAG_volatile_type:
21304 case DW_TAG_typedef:
21305 case DW_TAG_array_type:
21306 case DW_TAG_interface_type:
21307 case DW_TAG_friend:
21308 case DW_TAG_variant_part:
21309 case DW_TAG_enumeration_type:
21310 case DW_TAG_subroutine_type:
21311 case DW_TAG_string_type:
21312 case DW_TAG_set_type:
21313 case DW_TAG_subrange_type:
21314 case DW_TAG_ptr_to_member_type:
21315 case DW_TAG_file_type:
21316 if (die->die_perennial_p)
21317 break;
21319 /* It's a type node --- don't mark it. */
21320 return;
21322 default:
21323 /* Mark everything else. */
21324 break;
21327 if (die->die_mark == 0)
21329 die->die_mark = 1;
21331 /* Now, mark any dies referenced from here. */
21332 prune_unused_types_walk_attribs (die);
21335 die->die_mark = 2;
21337 /* Mark children. */
21338 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
21341 /* Increment the string counts on strings referred to from DIE's
21342 attributes. */
21344 static void
21345 prune_unused_types_update_strings (dw_die_ref die)
21347 dw_attr_ref a;
21348 unsigned ix;
21350 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
21351 if (AT_class (a) == dw_val_class_str)
21353 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
21354 s->refcount++;
21355 /* Avoid unnecessarily putting strings that are used less than
21356 twice in the hash table. */
21357 if (s->refcount
21358 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
21360 void ** slot;
21361 slot = htab_find_slot_with_hash (debug_str_hash, s->str,
21362 htab_hash_string (s->str),
21363 INSERT);
21364 gcc_assert (*slot == NULL);
21365 *slot = s;
21370 /* Remove from the tree DIE any dies that aren't marked. */
21372 static void
21373 prune_unused_types_prune (dw_die_ref die)
21375 dw_die_ref c;
21377 gcc_assert (die->die_mark);
21378 prune_unused_types_update_strings (die);
21380 if (! die->die_child)
21381 return;
21383 c = die->die_child;
21384 do {
21385 dw_die_ref prev = c;
21386 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
21387 if (c == die->die_child)
21389 /* No marked children between 'prev' and the end of the list. */
21390 if (prev == c)
21391 /* No marked children at all. */
21392 die->die_child = NULL;
21393 else
21395 prev->die_sib = c->die_sib;
21396 die->die_child = prev;
21398 return;
21401 if (c != prev->die_sib)
21402 prev->die_sib = c;
21403 prune_unused_types_prune (c);
21404 } while (c != die->die_child);
21407 /* Remove dies representing declarations that we never use. */
21409 static void
21410 prune_unused_types (void)
21412 unsigned int i;
21413 limbo_die_node *node;
21414 comdat_type_node *ctnode;
21415 pubname_ref pub;
21416 dw_die_ref base_type;
21418 #if ENABLE_ASSERT_CHECKING
21419 /* All the marks should already be clear. */
21420 verify_marks_clear (comp_unit_die ());
21421 for (node = limbo_die_list; node; node = node->next)
21422 verify_marks_clear (node->die);
21423 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21424 verify_marks_clear (ctnode->root_die);
21425 #endif /* ENABLE_ASSERT_CHECKING */
21427 /* Mark types that are used in global variables. */
21428 premark_types_used_by_global_vars ();
21430 /* Set the mark on nodes that are actually used. */
21431 prune_unused_types_walk (comp_unit_die ());
21432 for (node = limbo_die_list; node; node = node->next)
21433 prune_unused_types_walk (node->die);
21434 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21436 prune_unused_types_walk (ctnode->root_die);
21437 prune_unused_types_mark (ctnode->type_die, 1);
21440 /* Also set the mark on nodes referenced from the
21441 pubname_table. */
21442 FOR_EACH_VEC_ELT (pubname_entry, pubname_table, i, pub)
21443 prune_unused_types_mark (pub->die, 1);
21444 for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
21445 prune_unused_types_mark (base_type, 1);
21447 if (debug_str_hash)
21448 htab_empty (debug_str_hash);
21449 prune_unused_types_prune (comp_unit_die ());
21450 for (node = limbo_die_list; node; node = node->next)
21451 prune_unused_types_prune (node->die);
21452 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21453 prune_unused_types_prune (ctnode->root_die);
21455 /* Leave the marks clear. */
21456 prune_unmark_dies (comp_unit_die ());
21457 for (node = limbo_die_list; node; node = node->next)
21458 prune_unmark_dies (node->die);
21459 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21460 prune_unmark_dies (ctnode->root_die);
21463 /* Set the parameter to true if there are any relative pathnames in
21464 the file table. */
21465 static int
21466 file_table_relative_p (void ** slot, void *param)
21468 bool *p = (bool *) param;
21469 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
21470 if (!IS_ABSOLUTE_PATH (d->filename))
21472 *p = true;
21473 return 0;
21475 return 1;
21478 /* Routines to manipulate hash table of comdat type units. */
21480 static hashval_t
21481 htab_ct_hash (const void *of)
21483 hashval_t h;
21484 const comdat_type_node *const type_node = (const comdat_type_node *) of;
21486 memcpy (&h, type_node->signature, sizeof (h));
21487 return h;
21490 static int
21491 htab_ct_eq (const void *of1, const void *of2)
21493 const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
21494 const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
21496 return (! memcmp (type_node_1->signature, type_node_2->signature,
21497 DWARF_TYPE_SIGNATURE_SIZE));
21500 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
21501 to the location it would have been added, should we know its
21502 DECL_ASSEMBLER_NAME when we added other attributes. This will
21503 probably improve compactness of debug info, removing equivalent
21504 abbrevs, and hide any differences caused by deferring the
21505 computation of the assembler name, triggered by e.g. PCH. */
21507 static inline void
21508 move_linkage_attr (dw_die_ref die)
21510 unsigned ix = VEC_length (dw_attr_node, die->die_attr);
21511 dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
21513 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
21514 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
21516 while (--ix > 0)
21518 dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
21520 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
21521 break;
21524 if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
21526 VEC_pop (dw_attr_node, die->die_attr);
21527 VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
21531 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
21532 referenced from typed stack ops and count how often they are used. */
21534 static void
21535 mark_base_types (dw_loc_descr_ref loc)
21537 dw_die_ref base_type = NULL;
21539 for (; loc; loc = loc->dw_loc_next)
21541 switch (loc->dw_loc_opc)
21543 case DW_OP_GNU_regval_type:
21544 case DW_OP_GNU_deref_type:
21545 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
21546 break;
21547 case DW_OP_GNU_convert:
21548 case DW_OP_GNU_reinterpret:
21549 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
21550 continue;
21551 /* FALLTHRU */
21552 case DW_OP_GNU_const_type:
21553 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
21554 break;
21555 case DW_OP_GNU_entry_value:
21556 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
21557 continue;
21558 default:
21559 continue;
21561 gcc_assert (base_type->die_parent == comp_unit_die ());
21562 if (base_type->die_mark)
21563 base_type->die_mark++;
21564 else
21566 VEC_safe_push (dw_die_ref, heap, base_types, base_type);
21567 base_type->die_mark = 1;
21572 /* Comparison function for sorting marked base types. */
21574 static int
21575 base_type_cmp (const void *x, const void *y)
21577 dw_die_ref dx = *(const dw_die_ref *) x;
21578 dw_die_ref dy = *(const dw_die_ref *) y;
21579 unsigned int byte_size1, byte_size2;
21580 unsigned int encoding1, encoding2;
21581 if (dx->die_mark > dy->die_mark)
21582 return -1;
21583 if (dx->die_mark < dy->die_mark)
21584 return 1;
21585 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
21586 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
21587 if (byte_size1 < byte_size2)
21588 return 1;
21589 if (byte_size1 > byte_size2)
21590 return -1;
21591 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
21592 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
21593 if (encoding1 < encoding2)
21594 return 1;
21595 if (encoding1 > encoding2)
21596 return -1;
21597 return 0;
21600 /* Move base types marked by mark_base_types as early as possible
21601 in the CU, sorted by decreasing usage count both to make the
21602 uleb128 references as small as possible and to make sure they
21603 will have die_offset already computed by calc_die_sizes when
21604 sizes of typed stack loc ops is computed. */
21606 static void
21607 move_marked_base_types (void)
21609 unsigned int i;
21610 dw_die_ref base_type, die, c;
21612 if (VEC_empty (dw_die_ref, base_types))
21613 return;
21615 /* Sort by decreasing usage count, they will be added again in that
21616 order later on. */
21617 VEC_qsort (dw_die_ref, base_types, base_type_cmp);
21618 die = comp_unit_die ();
21619 c = die->die_child;
21622 dw_die_ref prev = c;
21623 c = c->die_sib;
21624 while (c->die_mark)
21626 remove_child_with_prev (c, prev);
21627 /* As base types got marked, there must be at least
21628 one node other than DW_TAG_base_type. */
21629 gcc_assert (c != c->die_sib);
21630 c = c->die_sib;
21633 while (c != die->die_child);
21634 gcc_assert (die->die_child);
21635 c = die->die_child;
21636 for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
21638 base_type->die_mark = 0;
21639 base_type->die_sib = c->die_sib;
21640 c->die_sib = base_type;
21641 c = base_type;
21645 /* Helper function for resolve_addr, attempt to resolve
21646 one CONST_STRING, return non-zero if not successful. Similarly verify that
21647 SYMBOL_REFs refer to variables emitted in the current CU. */
21649 static int
21650 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
21652 rtx rtl = *addr;
21654 if (GET_CODE (rtl) == CONST_STRING)
21656 size_t len = strlen (XSTR (rtl, 0)) + 1;
21657 tree t = build_string (len, XSTR (rtl, 0));
21658 tree tlen = size_int (len - 1);
21659 TREE_TYPE (t)
21660 = build_array_type (char_type_node, build_index_type (tlen));
21661 rtl = lookup_constant_def (t);
21662 if (!rtl || !MEM_P (rtl))
21663 return 1;
21664 rtl = XEXP (rtl, 0);
21665 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
21666 *addr = rtl;
21667 return 0;
21670 if (GET_CODE (rtl) == SYMBOL_REF
21671 && SYMBOL_REF_DECL (rtl))
21673 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
21675 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
21676 return 1;
21678 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
21679 return 1;
21682 if (GET_CODE (rtl) == CONST
21683 && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
21684 return 1;
21686 return 0;
21689 /* Helper function for resolve_addr, handle one location
21690 expression, return false if at least one CONST_STRING or SYMBOL_REF in
21691 the location list couldn't be resolved. */
21693 static bool
21694 resolve_addr_in_expr (dw_loc_descr_ref loc)
21696 dw_loc_descr_ref keep = NULL;
21697 for (; loc; loc = loc->dw_loc_next)
21698 switch (loc->dw_loc_opc)
21700 case DW_OP_addr:
21701 if (resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
21702 return false;
21703 break;
21704 case DW_OP_const4u:
21705 case DW_OP_const8u:
21706 if (loc->dtprel
21707 && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
21708 return false;
21709 break;
21710 case DW_OP_plus_uconst:
21711 if (size_of_loc_descr (loc)
21712 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
21714 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
21716 dw_loc_descr_ref repl
21717 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
21718 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
21719 add_loc_descr (&repl, loc->dw_loc_next);
21720 *loc = *repl;
21722 break;
21723 case DW_OP_implicit_value:
21724 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
21725 && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL))
21726 return false;
21727 break;
21728 case DW_OP_GNU_implicit_pointer:
21729 case DW_OP_GNU_parameter_ref:
21730 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
21732 dw_die_ref ref
21733 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
21734 if (ref == NULL)
21735 return false;
21736 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
21737 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
21738 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
21740 break;
21741 case DW_OP_GNU_const_type:
21742 case DW_OP_GNU_regval_type:
21743 case DW_OP_GNU_deref_type:
21744 case DW_OP_GNU_convert:
21745 case DW_OP_GNU_reinterpret:
21746 while (loc->dw_loc_next
21747 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
21749 dw_die_ref base1, base2;
21750 unsigned enc1, enc2, size1, size2;
21751 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
21752 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
21753 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
21754 else if (loc->dw_loc_oprnd1.val_class
21755 == dw_val_class_unsigned_const)
21756 break;
21757 else
21758 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
21759 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
21760 == dw_val_class_unsigned_const)
21761 break;
21762 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
21763 gcc_assert (base1->die_tag == DW_TAG_base_type
21764 && base2->die_tag == DW_TAG_base_type);
21765 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
21766 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
21767 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
21768 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
21769 if (size1 == size2
21770 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
21771 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
21772 && loc != keep)
21773 || enc1 == enc2))
21775 /* Optimize away next DW_OP_GNU_convert after
21776 adjusting LOC's base type die reference. */
21777 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
21778 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
21779 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
21780 else
21781 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
21782 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
21783 continue;
21785 /* Don't change integer DW_OP_GNU_convert after e.g. floating
21786 point typed stack entry. */
21787 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
21788 keep = loc->dw_loc_next;
21789 break;
21791 break;
21792 default:
21793 break;
21795 return true;
21798 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
21799 an address in .rodata section if the string literal is emitted there,
21800 or remove the containing location list or replace DW_AT_const_value
21801 with DW_AT_location and empty location expression, if it isn't found
21802 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
21803 to something that has been emitted in the current CU. */
21805 static void
21806 resolve_addr (dw_die_ref die)
21808 dw_die_ref c;
21809 dw_attr_ref a;
21810 dw_loc_list_ref *curr, *start, loc;
21811 unsigned ix;
21813 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
21814 switch (AT_class (a))
21816 case dw_val_class_loc_list:
21817 start = curr = AT_loc_list_ptr (a);
21818 loc = *curr;
21819 gcc_assert (loc);
21820 /* The same list can be referenced more than once. See if we have
21821 already recorded the result from a previous pass. */
21822 if (loc->replaced)
21823 *curr = loc->dw_loc_next;
21824 else if (!loc->resolved_addr)
21826 /* As things stand, we do not expect or allow one die to
21827 reference a suffix of another die's location list chain.
21828 References must be identical or completely separate.
21829 There is therefore no need to cache the result of this
21830 pass on any list other than the first; doing so
21831 would lead to unnecessary writes. */
21832 while (*curr)
21834 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
21835 if (!resolve_addr_in_expr ((*curr)->expr))
21837 dw_loc_list_ref next = (*curr)->dw_loc_next;
21838 if (next && (*curr)->ll_symbol)
21840 gcc_assert (!next->ll_symbol);
21841 next->ll_symbol = (*curr)->ll_symbol;
21843 *curr = next;
21845 else
21847 mark_base_types ((*curr)->expr);
21848 curr = &(*curr)->dw_loc_next;
21851 if (loc == *start)
21852 loc->resolved_addr = 1;
21853 else
21855 loc->replaced = 1;
21856 loc->dw_loc_next = *start;
21859 if (!*start)
21861 remove_AT (die, a->dw_attr);
21862 ix--;
21864 break;
21865 case dw_val_class_loc:
21867 dw_loc_descr_ref l = AT_loc (a);
21868 /* For -gdwarf-2 don't attempt to optimize
21869 DW_AT_data_member_location containing
21870 DW_OP_plus_uconst - older consumers might
21871 rely on it being that op instead of a more complex,
21872 but shorter, location description. */
21873 if ((dwarf_version > 2
21874 || a->dw_attr != DW_AT_data_member_location
21875 || l == NULL
21876 || l->dw_loc_opc != DW_OP_plus_uconst
21877 || l->dw_loc_next != NULL)
21878 && !resolve_addr_in_expr (l))
21880 remove_AT (die, a->dw_attr);
21881 ix--;
21883 else
21884 mark_base_types (l);
21886 break;
21887 case dw_val_class_addr:
21888 if (a->dw_attr == DW_AT_const_value
21889 && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
21891 remove_AT (die, a->dw_attr);
21892 ix--;
21894 if (die->die_tag == DW_TAG_GNU_call_site
21895 && a->dw_attr == DW_AT_abstract_origin)
21897 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
21898 dw_die_ref tdie = lookup_decl_die (tdecl);
21899 if (tdie == NULL
21900 && DECL_EXTERNAL (tdecl)
21901 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
21903 force_decl_die (tdecl);
21904 tdie = lookup_decl_die (tdecl);
21906 if (tdie)
21908 a->dw_attr_val.val_class = dw_val_class_die_ref;
21909 a->dw_attr_val.v.val_die_ref.die = tdie;
21910 a->dw_attr_val.v.val_die_ref.external = 0;
21912 else
21914 remove_AT (die, a->dw_attr);
21915 ix--;
21918 break;
21919 default:
21920 break;
21923 FOR_EACH_CHILD (die, c, resolve_addr (c));
21926 /* Helper routines for optimize_location_lists.
21927 This pass tries to share identical local lists in .debug_loc
21928 section. */
21930 /* Iteratively hash operands of LOC opcode. */
21932 static inline hashval_t
21933 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
21935 dw_val_ref val1 = &loc->dw_loc_oprnd1;
21936 dw_val_ref val2 = &loc->dw_loc_oprnd2;
21938 switch (loc->dw_loc_opc)
21940 case DW_OP_const4u:
21941 case DW_OP_const8u:
21942 if (loc->dtprel)
21943 goto hash_addr;
21944 /* FALLTHRU */
21945 case DW_OP_const1u:
21946 case DW_OP_const1s:
21947 case DW_OP_const2u:
21948 case DW_OP_const2s:
21949 case DW_OP_const4s:
21950 case DW_OP_const8s:
21951 case DW_OP_constu:
21952 case DW_OP_consts:
21953 case DW_OP_pick:
21954 case DW_OP_plus_uconst:
21955 case DW_OP_breg0:
21956 case DW_OP_breg1:
21957 case DW_OP_breg2:
21958 case DW_OP_breg3:
21959 case DW_OP_breg4:
21960 case DW_OP_breg5:
21961 case DW_OP_breg6:
21962 case DW_OP_breg7:
21963 case DW_OP_breg8:
21964 case DW_OP_breg9:
21965 case DW_OP_breg10:
21966 case DW_OP_breg11:
21967 case DW_OP_breg12:
21968 case DW_OP_breg13:
21969 case DW_OP_breg14:
21970 case DW_OP_breg15:
21971 case DW_OP_breg16:
21972 case DW_OP_breg17:
21973 case DW_OP_breg18:
21974 case DW_OP_breg19:
21975 case DW_OP_breg20:
21976 case DW_OP_breg21:
21977 case DW_OP_breg22:
21978 case DW_OP_breg23:
21979 case DW_OP_breg24:
21980 case DW_OP_breg25:
21981 case DW_OP_breg26:
21982 case DW_OP_breg27:
21983 case DW_OP_breg28:
21984 case DW_OP_breg29:
21985 case DW_OP_breg30:
21986 case DW_OP_breg31:
21987 case DW_OP_regx:
21988 case DW_OP_fbreg:
21989 case DW_OP_piece:
21990 case DW_OP_deref_size:
21991 case DW_OP_xderef_size:
21992 hash = iterative_hash_object (val1->v.val_int, hash);
21993 break;
21994 case DW_OP_skip:
21995 case DW_OP_bra:
21997 int offset;
21999 gcc_assert (val1->val_class == dw_val_class_loc);
22000 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
22001 hash = iterative_hash_object (offset, hash);
22003 break;
22004 case DW_OP_implicit_value:
22005 hash = iterative_hash_object (val1->v.val_unsigned, hash);
22006 switch (val2->val_class)
22008 case dw_val_class_const:
22009 hash = iterative_hash_object (val2->v.val_int, hash);
22010 break;
22011 case dw_val_class_vec:
22013 unsigned int elt_size = val2->v.val_vec.elt_size;
22014 unsigned int len = val2->v.val_vec.length;
22016 hash = iterative_hash_object (elt_size, hash);
22017 hash = iterative_hash_object (len, hash);
22018 hash = iterative_hash (val2->v.val_vec.array,
22019 len * elt_size, hash);
22021 break;
22022 case dw_val_class_const_double:
22023 hash = iterative_hash_object (val2->v.val_double.low, hash);
22024 hash = iterative_hash_object (val2->v.val_double.high, hash);
22025 break;
22026 case dw_val_class_addr:
22027 hash = iterative_hash_rtx (val2->v.val_addr, hash);
22028 break;
22029 default:
22030 gcc_unreachable ();
22032 break;
22033 case DW_OP_bregx:
22034 case DW_OP_bit_piece:
22035 hash = iterative_hash_object (val1->v.val_int, hash);
22036 hash = iterative_hash_object (val2->v.val_int, hash);
22037 break;
22038 case DW_OP_addr:
22039 hash_addr:
22040 if (loc->dtprel)
22042 unsigned char dtprel = 0xd1;
22043 hash = iterative_hash_object (dtprel, hash);
22045 hash = iterative_hash_rtx (val1->v.val_addr, hash);
22046 break;
22047 case DW_OP_GNU_implicit_pointer:
22048 hash = iterative_hash_object (val2->v.val_int, hash);
22049 break;
22050 case DW_OP_GNU_entry_value:
22051 hash = hash_loc_operands (val1->v.val_loc, hash);
22052 break;
22053 case DW_OP_GNU_regval_type:
22054 case DW_OP_GNU_deref_type:
22056 unsigned int byte_size
22057 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
22058 unsigned int encoding
22059 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
22060 hash = iterative_hash_object (val1->v.val_int, hash);
22061 hash = iterative_hash_object (byte_size, hash);
22062 hash = iterative_hash_object (encoding, hash);
22064 break;
22065 case DW_OP_GNU_convert:
22066 case DW_OP_GNU_reinterpret:
22067 if (val1->val_class == dw_val_class_unsigned_const)
22069 hash = iterative_hash_object (val1->v.val_unsigned, hash);
22070 break;
22072 /* FALLTHRU */
22073 case DW_OP_GNU_const_type:
22075 unsigned int byte_size
22076 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
22077 unsigned int encoding
22078 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
22079 hash = iterative_hash_object (byte_size, hash);
22080 hash = iterative_hash_object (encoding, hash);
22081 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
22082 break;
22083 hash = iterative_hash_object (val2->val_class, hash);
22084 switch (val2->val_class)
22086 case dw_val_class_const:
22087 hash = iterative_hash_object (val2->v.val_int, hash);
22088 break;
22089 case dw_val_class_vec:
22091 unsigned int elt_size = val2->v.val_vec.elt_size;
22092 unsigned int len = val2->v.val_vec.length;
22094 hash = iterative_hash_object (elt_size, hash);
22095 hash = iterative_hash_object (len, hash);
22096 hash = iterative_hash (val2->v.val_vec.array,
22097 len * elt_size, hash);
22099 break;
22100 case dw_val_class_const_double:
22101 hash = iterative_hash_object (val2->v.val_double.low, hash);
22102 hash = iterative_hash_object (val2->v.val_double.high, hash);
22103 break;
22104 default:
22105 gcc_unreachable ();
22108 break;
22110 default:
22111 /* Other codes have no operands. */
22112 break;
22114 return hash;
22117 /* Iteratively hash the whole DWARF location expression LOC. */
22119 static inline hashval_t
22120 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
22122 dw_loc_descr_ref l;
22123 bool sizes_computed = false;
22124 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
22125 size_of_locs (loc);
22127 for (l = loc; l != NULL; l = l->dw_loc_next)
22129 enum dwarf_location_atom opc = l->dw_loc_opc;
22130 hash = iterative_hash_object (opc, hash);
22131 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
22133 size_of_locs (loc);
22134 sizes_computed = true;
22136 hash = hash_loc_operands (l, hash);
22138 return hash;
22141 /* Compute hash of the whole location list LIST_HEAD. */
22143 static inline void
22144 hash_loc_list (dw_loc_list_ref list_head)
22146 dw_loc_list_ref curr = list_head;
22147 hashval_t hash = 0;
22149 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
22151 hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
22152 hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
22153 if (curr->section)
22154 hash = iterative_hash (curr->section, strlen (curr->section) + 1,
22155 hash);
22156 hash = hash_locs (curr->expr, hash);
22158 list_head->hash = hash;
22161 /* Return true if X and Y opcodes have the same operands. */
22163 static inline bool
22164 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
22166 dw_val_ref valx1 = &x->dw_loc_oprnd1;
22167 dw_val_ref valx2 = &x->dw_loc_oprnd2;
22168 dw_val_ref valy1 = &y->dw_loc_oprnd1;
22169 dw_val_ref valy2 = &y->dw_loc_oprnd2;
22171 switch (x->dw_loc_opc)
22173 case DW_OP_const4u:
22174 case DW_OP_const8u:
22175 if (x->dtprel)
22176 goto hash_addr;
22177 /* FALLTHRU */
22178 case DW_OP_const1u:
22179 case DW_OP_const1s:
22180 case DW_OP_const2u:
22181 case DW_OP_const2s:
22182 case DW_OP_const4s:
22183 case DW_OP_const8s:
22184 case DW_OP_constu:
22185 case DW_OP_consts:
22186 case DW_OP_pick:
22187 case DW_OP_plus_uconst:
22188 case DW_OP_breg0:
22189 case DW_OP_breg1:
22190 case DW_OP_breg2:
22191 case DW_OP_breg3:
22192 case DW_OP_breg4:
22193 case DW_OP_breg5:
22194 case DW_OP_breg6:
22195 case DW_OP_breg7:
22196 case DW_OP_breg8:
22197 case DW_OP_breg9:
22198 case DW_OP_breg10:
22199 case DW_OP_breg11:
22200 case DW_OP_breg12:
22201 case DW_OP_breg13:
22202 case DW_OP_breg14:
22203 case DW_OP_breg15:
22204 case DW_OP_breg16:
22205 case DW_OP_breg17:
22206 case DW_OP_breg18:
22207 case DW_OP_breg19:
22208 case DW_OP_breg20:
22209 case DW_OP_breg21:
22210 case DW_OP_breg22:
22211 case DW_OP_breg23:
22212 case DW_OP_breg24:
22213 case DW_OP_breg25:
22214 case DW_OP_breg26:
22215 case DW_OP_breg27:
22216 case DW_OP_breg28:
22217 case DW_OP_breg29:
22218 case DW_OP_breg30:
22219 case DW_OP_breg31:
22220 case DW_OP_regx:
22221 case DW_OP_fbreg:
22222 case DW_OP_piece:
22223 case DW_OP_deref_size:
22224 case DW_OP_xderef_size:
22225 return valx1->v.val_int == valy1->v.val_int;
22226 case DW_OP_skip:
22227 case DW_OP_bra:
22228 gcc_assert (valx1->val_class == dw_val_class_loc
22229 && valy1->val_class == dw_val_class_loc
22230 && x->dw_loc_addr == y->dw_loc_addr);
22231 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
22232 case DW_OP_implicit_value:
22233 if (valx1->v.val_unsigned != valy1->v.val_unsigned
22234 || valx2->val_class != valy2->val_class)
22235 return false;
22236 switch (valx2->val_class)
22238 case dw_val_class_const:
22239 return valx2->v.val_int == valy2->v.val_int;
22240 case dw_val_class_vec:
22241 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
22242 && valx2->v.val_vec.length == valy2->v.val_vec.length
22243 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
22244 valx2->v.val_vec.elt_size
22245 * valx2->v.val_vec.length) == 0;
22246 case dw_val_class_const_double:
22247 return valx2->v.val_double.low == valy2->v.val_double.low
22248 && valx2->v.val_double.high == valy2->v.val_double.high;
22249 case dw_val_class_addr:
22250 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
22251 default:
22252 gcc_unreachable ();
22254 case DW_OP_bregx:
22255 case DW_OP_bit_piece:
22256 return valx1->v.val_int == valy1->v.val_int
22257 && valx2->v.val_int == valy2->v.val_int;
22258 case DW_OP_addr:
22259 hash_addr:
22260 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
22261 case DW_OP_GNU_implicit_pointer:
22262 return valx1->val_class == dw_val_class_die_ref
22263 && valx1->val_class == valy1->val_class
22264 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
22265 && valx2->v.val_int == valy2->v.val_int;
22266 case DW_OP_GNU_entry_value:
22267 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
22268 case DW_OP_GNU_const_type:
22269 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
22270 || valx2->val_class != valy2->val_class)
22271 return false;
22272 switch (valx2->val_class)
22274 case dw_val_class_const:
22275 return valx2->v.val_int == valy2->v.val_int;
22276 case dw_val_class_vec:
22277 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
22278 && valx2->v.val_vec.length == valy2->v.val_vec.length
22279 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
22280 valx2->v.val_vec.elt_size
22281 * valx2->v.val_vec.length) == 0;
22282 case dw_val_class_const_double:
22283 return valx2->v.val_double.low == valy2->v.val_double.low
22284 && valx2->v.val_double.high == valy2->v.val_double.high;
22285 default:
22286 gcc_unreachable ();
22288 case DW_OP_GNU_regval_type:
22289 case DW_OP_GNU_deref_type:
22290 return valx1->v.val_int == valy1->v.val_int
22291 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
22292 case DW_OP_GNU_convert:
22293 case DW_OP_GNU_reinterpret:
22294 if (valx1->val_class != valy1->val_class)
22295 return false;
22296 if (valx1->val_class == dw_val_class_unsigned_const)
22297 return valx1->v.val_unsigned == valy1->v.val_unsigned;
22298 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
22299 case DW_OP_GNU_parameter_ref:
22300 return valx1->val_class == dw_val_class_die_ref
22301 && valx1->val_class == valy1->val_class
22302 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
22303 default:
22304 /* Other codes have no operands. */
22305 return true;
22309 /* Return true if DWARF location expressions X and Y are the same. */
22311 static inline bool
22312 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
22314 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
22315 if (x->dw_loc_opc != y->dw_loc_opc
22316 || x->dtprel != y->dtprel
22317 || !compare_loc_operands (x, y))
22318 break;
22319 return x == NULL && y == NULL;
22322 /* Return precomputed hash of location list X. */
22324 static hashval_t
22325 loc_list_hash (const void *x)
22327 return ((const struct dw_loc_list_struct *) x)->hash;
22330 /* Return 1 if location lists X and Y are the same. */
22332 static int
22333 loc_list_eq (const void *x, const void *y)
22335 const struct dw_loc_list_struct *a = (const struct dw_loc_list_struct *) x;
22336 const struct dw_loc_list_struct *b = (const struct dw_loc_list_struct *) y;
22337 if (a == b)
22338 return 1;
22339 if (a->hash != b->hash)
22340 return 0;
22341 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
22342 if (strcmp (a->begin, b->begin) != 0
22343 || strcmp (a->end, b->end) != 0
22344 || (a->section == NULL) != (b->section == NULL)
22345 || (a->section && strcmp (a->section, b->section) != 0)
22346 || !compare_locs (a->expr, b->expr))
22347 break;
22348 return a == NULL && b == NULL;
22351 /* Recursively optimize location lists referenced from DIE
22352 children and share them whenever possible. */
22354 static void
22355 optimize_location_lists_1 (dw_die_ref die, htab_t htab)
22357 dw_die_ref c;
22358 dw_attr_ref a;
22359 unsigned ix;
22360 void **slot;
22362 FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22363 if (AT_class (a) == dw_val_class_loc_list)
22365 dw_loc_list_ref list = AT_loc_list (a);
22366 /* TODO: perform some optimizations here, before hashing
22367 it and storing into the hash table. */
22368 hash_loc_list (list);
22369 slot = htab_find_slot_with_hash (htab, list, list->hash,
22370 INSERT);
22371 if (*slot == NULL)
22372 *slot = (void *) list;
22373 else
22374 a->dw_attr_val.v.val_loc_list = (dw_loc_list_ref) *slot;
22377 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
22380 /* Optimize location lists referenced from DIE
22381 children and share them whenever possible. */
22383 static void
22384 optimize_location_lists (dw_die_ref die)
22386 htab_t htab = htab_create (500, loc_list_hash, loc_list_eq, NULL);
22387 optimize_location_lists_1 (die, htab);
22388 htab_delete (htab);
22391 /* Output stuff that dwarf requires at the end of every file,
22392 and generate the DWARF-2 debugging info. */
22394 static void
22395 dwarf2out_finish (const char *filename)
22397 limbo_die_node *node, *next_node;
22398 comdat_type_node *ctnode;
22399 htab_t comdat_type_table;
22400 unsigned int i;
22402 /* PCH might result in DW_AT_producer string being restored from the
22403 header compilation, fix it up if needed. */
22404 dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
22405 if (strcmp (AT_string (producer), producer_string) != 0)
22407 struct indirect_string_node *node = find_AT_string (producer_string);
22408 producer->dw_attr_val.v.val_str = node;
22411 gen_scheduled_generic_parms_dies ();
22412 gen_remaining_tmpl_value_param_die_attribute ();
22414 /* Add the name for the main input file now. We delayed this from
22415 dwarf2out_init to avoid complications with PCH. */
22416 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
22417 if (!IS_ABSOLUTE_PATH (filename))
22418 add_comp_dir_attribute (comp_unit_die ());
22419 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
22421 bool p = false;
22422 htab_traverse (file_table, file_table_relative_p, &p);
22423 if (p)
22424 add_comp_dir_attribute (comp_unit_die ());
22427 for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
22429 add_location_or_const_value_attribute (
22430 VEC_index (deferred_locations, deferred_locations_list, i)->die,
22431 VEC_index (deferred_locations, deferred_locations_list, i)->variable,
22432 false,
22433 DW_AT_location);
22436 /* Traverse the limbo die list, and add parent/child links. The only
22437 dies without parents that should be here are concrete instances of
22438 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
22439 For concrete instances, we can get the parent die from the abstract
22440 instance. */
22441 for (node = limbo_die_list; node; node = next_node)
22443 dw_die_ref die = node->die;
22444 next_node = node->next;
22446 if (die->die_parent == NULL)
22448 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
22450 if (origin && origin->die_parent)
22451 add_child_die (origin->die_parent, die);
22452 else if (is_cu_die (die))
22454 else if (seen_error ())
22455 /* It's OK to be confused by errors in the input. */
22456 add_child_die (comp_unit_die (), die);
22457 else
22459 /* In certain situations, the lexical block containing a
22460 nested function can be optimized away, which results
22461 in the nested function die being orphaned. Likewise
22462 with the return type of that nested function. Force
22463 this to be a child of the containing function.
22465 It may happen that even the containing function got fully
22466 inlined and optimized out. In that case we are lost and
22467 assign the empty child. This should not be big issue as
22468 the function is likely unreachable too. */
22469 tree context = NULL_TREE;
22471 gcc_assert (node->created_for);
22473 if (DECL_P (node->created_for))
22474 context = DECL_CONTEXT (node->created_for);
22475 else if (TYPE_P (node->created_for))
22476 context = TYPE_CONTEXT (node->created_for);
22478 gcc_assert (context
22479 && (TREE_CODE (context) == FUNCTION_DECL
22480 || TREE_CODE (context) == NAMESPACE_DECL));
22482 origin = lookup_decl_die (context);
22483 if (origin)
22484 add_child_die (origin, die);
22485 else
22486 add_child_die (comp_unit_die (), die);
22491 limbo_die_list = NULL;
22493 #if ENABLE_ASSERT_CHECKING
22495 dw_die_ref die = comp_unit_die (), c;
22496 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
22498 #endif
22499 resolve_addr (comp_unit_die ());
22500 move_marked_base_types ();
22502 for (node = deferred_asm_name; node; node = node->next)
22504 tree decl = node->created_for;
22505 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
22507 add_linkage_attr (node->die, decl);
22508 move_linkage_attr (node->die);
22512 deferred_asm_name = NULL;
22514 /* Walk through the list of incomplete types again, trying once more to
22515 emit full debugging info for them. */
22516 retry_incomplete_types ();
22518 if (flag_eliminate_unused_debug_types)
22519 prune_unused_types ();
22521 /* Generate separate CUs for each of the include files we've seen.
22522 They will go into limbo_die_list. */
22523 if (flag_eliminate_dwarf2_dups && ! use_debug_types)
22524 break_out_includes (comp_unit_die ());
22526 /* Generate separate COMDAT sections for type DIEs. */
22527 if (use_debug_types)
22529 break_out_comdat_types (comp_unit_die ());
22531 /* Each new type_unit DIE was added to the limbo die list when created.
22532 Since these have all been added to comdat_type_list, clear the
22533 limbo die list. */
22534 limbo_die_list = NULL;
22536 /* For each new comdat type unit, copy declarations for incomplete
22537 types to make the new unit self-contained (i.e., no direct
22538 references to the main compile unit). */
22539 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22540 copy_decls_for_unworthy_types (ctnode->root_die);
22541 copy_decls_for_unworthy_types (comp_unit_die ());
22543 /* In the process of copying declarations from one unit to another,
22544 we may have left some declarations behind that are no longer
22545 referenced. Prune them. */
22546 prune_unused_types ();
22549 /* Traverse the DIE's and add add sibling attributes to those DIE's
22550 that have children. */
22551 add_sibling_attributes (comp_unit_die ());
22552 for (node = limbo_die_list; node; node = node->next)
22553 add_sibling_attributes (node->die);
22554 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22555 add_sibling_attributes (ctnode->root_die);
22557 /* Output a terminator label for the .text section. */
22558 switch_to_section (text_section);
22559 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
22560 if (cold_text_section)
22562 switch_to_section (cold_text_section);
22563 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
22566 /* We can only use the low/high_pc attributes if all of the code was
22567 in .text. */
22568 if (!have_multiple_function_sections
22569 || (dwarf_version < 3 && dwarf_strict))
22571 /* Don't add if the CU has no associated code. */
22572 if (text_section_used)
22574 add_AT_lbl_id (comp_unit_die (), DW_AT_low_pc, text_section_label);
22575 add_AT_lbl_id (comp_unit_die (), DW_AT_high_pc, text_end_label);
22578 else
22580 unsigned fde_idx;
22581 dw_fde_ref fde;
22582 bool range_list_added = false;
22584 if (text_section_used)
22585 add_ranges_by_labels (comp_unit_die (), text_section_label,
22586 text_end_label, &range_list_added);
22587 if (cold_text_section_used)
22588 add_ranges_by_labels (comp_unit_die (), cold_text_section_label,
22589 cold_end_label, &range_list_added);
22591 FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, fde_idx, fde)
22593 if (!fde->in_std_section)
22594 add_ranges_by_labels (comp_unit_die (), fde->dw_fde_begin,
22595 fde->dw_fde_end, &range_list_added);
22596 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
22597 add_ranges_by_labels (comp_unit_die (), fde->dw_fde_second_begin,
22598 fde->dw_fde_second_end, &range_list_added);
22601 if (range_list_added)
22603 /* We need to give .debug_loc and .debug_ranges an appropriate
22604 "base address". Use zero so that these addresses become
22605 absolute. Historically, we've emitted the unexpected
22606 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
22607 Emit both to give time for other tools to adapt. */
22608 add_AT_addr (comp_unit_die (), DW_AT_low_pc, const0_rtx);
22609 if (! dwarf_strict && dwarf_version < 4)
22610 add_AT_addr (comp_unit_die (), DW_AT_entry_pc, const0_rtx);
22612 add_ranges (NULL);
22616 if (debug_info_level >= DINFO_LEVEL_NORMAL)
22617 add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list,
22618 debug_line_section_label);
22620 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22621 add_AT_macptr (comp_unit_die (),
22622 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
22623 macinfo_section_label);
22625 if (have_location_lists)
22626 optimize_location_lists (comp_unit_die ());
22628 /* Output all of the compilation units. We put the main one last so that
22629 the offsets are available to output_pubnames. */
22630 for (node = limbo_die_list; node; node = node->next)
22631 output_comp_unit (node->die, 0);
22633 comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
22634 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22636 void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
22638 /* Don't output duplicate types. */
22639 if (*slot != HTAB_EMPTY_ENTRY)
22640 continue;
22642 /* Add a pointer to the line table for the main compilation unit
22643 so that the debugger can make sense of DW_AT_decl_file
22644 attributes. */
22645 if (debug_info_level >= DINFO_LEVEL_NORMAL)
22646 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
22647 debug_line_section_label);
22649 output_comdat_type_unit (ctnode);
22650 *slot = ctnode;
22652 htab_delete (comdat_type_table);
22654 /* Output the main compilation unit if non-empty or if .debug_macinfo
22655 will be emitted. */
22656 output_comp_unit (comp_unit_die (), debug_info_level >= DINFO_LEVEL_VERBOSE);
22658 /* Output the abbreviation table. */
22659 if (abbrev_die_table_in_use != 1)
22661 switch_to_section (debug_abbrev_section);
22662 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
22663 output_abbrev_section ();
22666 /* Output location list section if necessary. */
22667 if (have_location_lists)
22669 /* Output the location lists info. */
22670 switch_to_section (debug_loc_section);
22671 ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
22672 DEBUG_LOC_SECTION_LABEL, 0);
22673 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
22674 output_location_lists (comp_unit_die ());
22677 /* Output public names table if necessary. */
22678 if (!VEC_empty (pubname_entry, pubname_table))
22680 gcc_assert (info_section_emitted);
22681 switch_to_section (debug_pubnames_section);
22682 output_pubnames (pubname_table);
22685 /* Output public types table if necessary. */
22686 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
22687 It shouldn't hurt to emit it always, since pure DWARF2 consumers
22688 simply won't look for the section. */
22689 if (!VEC_empty (pubname_entry, pubtype_table))
22691 bool empty = false;
22693 if (flag_eliminate_unused_debug_types)
22695 /* The pubtypes table might be emptied by pruning unused items. */
22696 unsigned i;
22697 pubname_ref p;
22698 empty = true;
22699 FOR_EACH_VEC_ELT (pubname_entry, pubtype_table, i, p)
22700 if (p->die->die_offset != 0)
22702 empty = false;
22703 break;
22706 if (!empty)
22708 gcc_assert (info_section_emitted);
22709 switch_to_section (debug_pubtypes_section);
22710 output_pubnames (pubtype_table);
22714 /* Output the address range information if a CU (.debug_info section)
22715 was emitted. We output an empty table even if we had no functions
22716 to put in it. This because the consumer has no way to tell the
22717 difference between an empty table that we omitted and failure to
22718 generate a table that would have contained data. */
22719 if (info_section_emitted)
22721 unsigned long aranges_length = size_of_aranges ();
22723 switch_to_section (debug_aranges_section);
22724 output_aranges (aranges_length);
22727 /* Output ranges section if necessary. */
22728 if (ranges_table_in_use)
22730 switch_to_section (debug_ranges_section);
22731 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
22732 output_ranges ();
22735 /* Have to end the macro section. */
22736 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22738 switch_to_section (debug_macinfo_section);
22739 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
22740 if (!VEC_empty (macinfo_entry, macinfo_table))
22741 output_macinfo ();
22742 dw2_asm_output_data (1, 0, "End compilation unit");
22745 /* Output the source line correspondence table. We must do this
22746 even if there is no line information. Otherwise, on an empty
22747 translation unit, we will generate a present, but empty,
22748 .debug_info section. IRIX 6.5 `nm' will then complain when
22749 examining the file. This is done late so that any filenames
22750 used by the debug_info section are marked as 'used'. */
22751 switch_to_section (debug_line_section);
22752 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
22753 if (! DWARF2_ASM_LINE_DEBUG_INFO)
22754 output_line_info ();
22756 /* If we emitted any DW_FORM_strp form attribute, output the string
22757 table too. */
22758 if (debug_str_hash)
22759 htab_traverse (debug_str_hash, output_indirect_string, NULL);
22762 #include "gt-dwarf2out.h"