Remove redundant variable in hash_set.
[official-gcc.git] / gcc / dwarf2out.c
blobf0256aeade405c266536ef8a98a393c908f5ea74
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992, 1993, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
4 Free Software Foundation, Inc.
5 Contributed by Gary Funck (gary@intrepid.com).
6 Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
7 Extensively modified by Jason Merrill (jason@cygnus.com).
9 This file is part of GCC.
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
14 version.
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 for more details.
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3. If not see
23 <http://www.gnu.org/licenses/>. */
25 /* TODO: Emit .debug_line header even when there are no functions, since
26 the file numbers are used by .debug_info. Alternately, leave
27 out locations for types and decls.
28 Avoid talking about ctors and op= for PODs.
29 Factor out common prologue sequences into multiple CIEs. */
31 /* The first part of this file deals with the DWARF 2 frame unwind
32 information, which is also used by the GCC efficient exception handling
33 mechanism. The second part, controlled only by an #ifdef
34 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
35 information. */
37 /* DWARF2 Abbreviation Glossary:
39 CFA = Canonical Frame Address
40 a fixed address on the stack which identifies a call frame.
41 We define it to be the value of SP just before the call insn.
42 The CFA register and offset, which may change during the course
43 of the function, are used to calculate its value at runtime.
45 CFI = Call Frame Instruction
46 an instruction for the DWARF2 abstract machine
48 CIE = Common Information Entry
49 information describing information common to one or more FDEs
51 DIE = Debugging Information Entry
53 FDE = Frame Description Entry
54 information describing the stack call frame, in particular,
55 how to restore registers
57 DW_CFA_... = DWARF2 CFA call frame instruction
58 DW_TAG_... = DWARF2 DIE tag */
60 #include "config.h"
61 #include "system.h"
62 #include "coretypes.h"
63 #include "tm.h"
64 #include "tree.h"
65 #include "version.h"
66 #include "flags.h"
67 #include "rtl.h"
68 #include "hard-reg-set.h"
69 #include "regs.h"
70 #include "insn-config.h"
71 #include "reload.h"
72 #include "function.h"
73 #include "output.h"
74 #include "expr.h"
75 #include "except.h"
76 #include "dwarf2.h"
77 #include "dwarf2out.h"
78 #include "dwarf2asm.h"
79 #include "toplev.h"
80 #include "ggc.h"
81 #include "md5.h"
82 #include "tm_p.h"
83 #include "diagnostic.h"
84 #include "tree-pretty-print.h"
85 #include "debug.h"
86 #include "target.h"
87 #include "common/common-target.h"
88 #include "langhooks.h"
89 #include "hashtab.h"
90 #include "cgraph.h"
91 #include "input.h"
92 #include "gimple.h"
93 #include "ira.h"
94 #include "lra.h"
95 #include "dumpfile.h"
96 #include "opts.h"
98 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
99 static rtx last_var_location_insn;
100 static rtx cached_next_real_insn;
102 #ifdef VMS_DEBUGGING_INFO
103 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
105 /* Define this macro to be a nonzero value if the directory specifications
106 which are output in the debug info should end with a separator. */
107 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
108 /* Define this macro to evaluate to a nonzero value if GCC should refrain
109 from generating indirect strings in DWARF2 debug information, for instance
110 if your target is stuck with an old version of GDB that is unable to
111 process them properly or uses VMS Debug. */
112 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
113 #else
114 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
115 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
116 #endif
118 /* ??? Poison these here until it can be done generically. They've been
119 totally replaced in this file; make sure it stays that way. */
120 #undef DWARF2_UNWIND_INFO
121 #undef DWARF2_FRAME_INFO
122 #if (GCC_VERSION >= 3000)
123 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
124 #endif
126 /* The size of the target's pointer type. */
127 #ifndef PTR_SIZE
128 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
129 #endif
131 /* Array of RTXes referenced by the debugging information, which therefore
132 must be kept around forever. */
133 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
135 /* A pointer to the base of a list of incomplete types which might be
136 completed at some later time. incomplete_types_list needs to be a
137 vec<tree, va_gc> *because we want to tell the garbage collector about
138 it. */
139 static GTY(()) vec<tree, va_gc> *incomplete_types;
141 /* A pointer to the base of a table of references to declaration
142 scopes. This table is a display which tracks the nesting
143 of declaration scopes at the current scope and containing
144 scopes. This table is used to find the proper place to
145 define type declaration DIE's. */
146 static GTY(()) vec<tree, va_gc> *decl_scope_table;
148 /* Pointers to various DWARF2 sections. */
149 static GTY(()) section *debug_info_section;
150 static GTY(()) section *debug_skeleton_info_section;
151 static GTY(()) section *debug_abbrev_section;
152 static GTY(()) section *debug_skeleton_abbrev_section;
153 static GTY(()) section *debug_aranges_section;
154 static GTY(()) section *debug_addr_section;
155 static GTY(()) section *debug_macinfo_section;
156 static GTY(()) section *debug_line_section;
157 static GTY(()) section *debug_skeleton_line_section;
158 static GTY(()) section *debug_loc_section;
159 static GTY(()) section *debug_pubnames_section;
160 static GTY(()) section *debug_pubtypes_section;
161 static GTY(()) section *debug_str_section;
162 static GTY(()) section *debug_str_offsets_section;
163 static GTY(()) section *debug_ranges_section;
164 static GTY(()) section *debug_frame_section;
166 /* Maximum size (in bytes) of an artificially generated label. */
167 #define MAX_ARTIFICIAL_LABEL_BYTES 30
169 /* According to the (draft) DWARF 3 specification, the initial length
170 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
171 bytes are 0xffffffff, followed by the length stored in the next 8
172 bytes.
174 However, the SGI/MIPS ABI uses an initial length which is equal to
175 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
177 #ifndef DWARF_INITIAL_LENGTH_SIZE
178 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
179 #endif
181 /* Round SIZE up to the nearest BOUNDARY. */
182 #define DWARF_ROUND(SIZE,BOUNDARY) \
183 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
185 /* CIE identifier. */
186 #if HOST_BITS_PER_WIDE_INT >= 64
187 #define DWARF_CIE_ID \
188 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
189 #else
190 #define DWARF_CIE_ID DW_CIE_ID
191 #endif
194 /* A vector for a table that contains frame description
195 information for each routine. */
196 #define NOT_INDEXED (-1U)
197 #define NO_INDEX_ASSIGNED (-2U)
199 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
201 struct GTY(()) indirect_string_node {
202 const char *str;
203 unsigned int refcount;
204 enum dwarf_form form;
205 char *label;
206 unsigned int index;
209 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
211 static GTY(()) int dw2_string_counter;
213 /* True if the compilation unit places functions in more than one section. */
214 static GTY(()) bool have_multiple_function_sections = false;
216 /* Whether the default text and cold text sections have been used at all. */
218 static GTY(()) bool text_section_used = false;
219 static GTY(()) bool cold_text_section_used = false;
221 /* The default cold text section. */
222 static GTY(()) section *cold_text_section;
224 /* Forward declarations for functions defined in this file. */
226 static char *stripattributes (const char *);
227 static void output_call_frame_info (int);
228 static void dwarf2out_note_section_used (void);
230 /* Personality decl of current unit. Used only when assembler does not support
231 personality CFI. */
232 static GTY(()) rtx current_unit_personality;
234 /* Data and reference forms for relocatable data. */
235 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
236 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
238 #ifndef DEBUG_FRAME_SECTION
239 #define DEBUG_FRAME_SECTION ".debug_frame"
240 #endif
242 #ifndef FUNC_BEGIN_LABEL
243 #define FUNC_BEGIN_LABEL "LFB"
244 #endif
246 #ifndef FUNC_END_LABEL
247 #define FUNC_END_LABEL "LFE"
248 #endif
250 #ifndef PROLOGUE_END_LABEL
251 #define PROLOGUE_END_LABEL "LPE"
252 #endif
254 #ifndef EPILOGUE_BEGIN_LABEL
255 #define EPILOGUE_BEGIN_LABEL "LEB"
256 #endif
258 #ifndef FRAME_BEGIN_LABEL
259 #define FRAME_BEGIN_LABEL "Lframe"
260 #endif
261 #define CIE_AFTER_SIZE_LABEL "LSCIE"
262 #define CIE_END_LABEL "LECIE"
263 #define FDE_LABEL "LSFDE"
264 #define FDE_AFTER_SIZE_LABEL "LASFDE"
265 #define FDE_END_LABEL "LEFDE"
266 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
267 #define LINE_NUMBER_END_LABEL "LELT"
268 #define LN_PROLOG_AS_LABEL "LASLTP"
269 #define LN_PROLOG_END_LABEL "LELTP"
270 #define DIE_LABEL_PREFIX "DW"
272 /* Match the base name of a file to the base name of a compilation unit. */
274 static int
275 matches_main_base (const char *path)
277 /* Cache the last query. */
278 static const char *last_path = NULL;
279 static int last_match = 0;
280 if (path != last_path)
282 const char *base;
283 int length = base_of_path (path, &base);
284 last_path = path;
285 last_match = (length == main_input_baselength
286 && memcmp (base, main_input_basename, length) == 0);
288 return last_match;
291 #ifdef DEBUG_DEBUG_STRUCT
293 static int
294 dump_struct_debug (tree type, enum debug_info_usage usage,
295 enum debug_struct_file criterion, int generic,
296 int matches, int result)
298 /* Find the type name. */
299 tree type_decl = TYPE_STUB_DECL (type);
300 tree t = type_decl;
301 const char *name = 0;
302 if (TREE_CODE (t) == TYPE_DECL)
303 t = DECL_NAME (t);
304 if (t)
305 name = IDENTIFIER_POINTER (t);
307 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
308 criterion,
309 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
310 matches ? "bas" : "hdr",
311 generic ? "gen" : "ord",
312 usage == DINFO_USAGE_DFN ? ";" :
313 usage == DINFO_USAGE_DIR_USE ? "." : "*",
314 result,
315 (void*) type_decl, name);
316 return result;
318 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
319 dump_struct_debug (type, usage, criterion, generic, matches, result)
321 #else
323 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
324 (result)
326 #endif
328 static bool
329 should_emit_struct_debug (tree type, enum debug_info_usage usage)
331 enum debug_struct_file criterion;
332 tree type_decl;
333 bool generic = lang_hooks.types.generic_p (type);
335 if (generic)
336 criterion = debug_struct_generic[usage];
337 else
338 criterion = debug_struct_ordinary[usage];
340 if (criterion == DINFO_STRUCT_FILE_NONE)
341 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
342 if (criterion == DINFO_STRUCT_FILE_ANY)
343 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
345 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
347 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
348 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
350 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
351 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
352 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
355 /* Return a pointer to a copy of the section string name S with all
356 attributes stripped off, and an asterisk prepended (for assemble_name). */
358 static inline char *
359 stripattributes (const char *s)
361 char *stripped = XNEWVEC (char, strlen (s) + 2);
362 char *p = stripped;
364 *p++ = '*';
366 while (*s && *s != ',')
367 *p++ = *s++;
369 *p = '\0';
370 return stripped;
373 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
374 switch to the data section instead, and write out a synthetic start label
375 for collect2 the first time around. */
377 static void
378 switch_to_eh_frame_section (bool back)
380 tree label;
382 #ifdef EH_FRAME_SECTION_NAME
383 if (eh_frame_section == 0)
385 int flags;
387 if (EH_TABLES_CAN_BE_READ_ONLY)
389 int fde_encoding;
390 int per_encoding;
391 int lsda_encoding;
393 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
394 /*global=*/0);
395 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
396 /*global=*/1);
397 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
398 /*global=*/0);
399 flags = ((! flag_pic
400 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
401 && (fde_encoding & 0x70) != DW_EH_PE_aligned
402 && (per_encoding & 0x70) != DW_EH_PE_absptr
403 && (per_encoding & 0x70) != DW_EH_PE_aligned
404 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
405 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
406 ? 0 : SECTION_WRITE);
408 else
409 flags = SECTION_WRITE;
410 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
412 #endif /* EH_FRAME_SECTION_NAME */
414 if (eh_frame_section)
415 switch_to_section (eh_frame_section);
416 else
418 /* We have no special eh_frame section. Put the information in
419 the data section and emit special labels to guide collect2. */
420 switch_to_section (data_section);
422 if (!back)
424 label = get_file_function_name ("F");
425 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
426 targetm.asm_out.globalize_label (asm_out_file,
427 IDENTIFIER_POINTER (label));
428 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
433 /* Switch [BACK] to the eh or debug frame table section, depending on
434 FOR_EH. */
436 static void
437 switch_to_frame_table_section (int for_eh, bool back)
439 if (for_eh)
440 switch_to_eh_frame_section (back);
441 else
443 if (!debug_frame_section)
444 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
445 SECTION_DEBUG, NULL);
446 switch_to_section (debug_frame_section);
450 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
452 enum dw_cfi_oprnd_type
453 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
455 switch (cfi)
457 case DW_CFA_nop:
458 case DW_CFA_GNU_window_save:
459 case DW_CFA_remember_state:
460 case DW_CFA_restore_state:
461 return dw_cfi_oprnd_unused;
463 case DW_CFA_set_loc:
464 case DW_CFA_advance_loc1:
465 case DW_CFA_advance_loc2:
466 case DW_CFA_advance_loc4:
467 case DW_CFA_MIPS_advance_loc8:
468 return dw_cfi_oprnd_addr;
470 case DW_CFA_offset:
471 case DW_CFA_offset_extended:
472 case DW_CFA_def_cfa:
473 case DW_CFA_offset_extended_sf:
474 case DW_CFA_def_cfa_sf:
475 case DW_CFA_restore:
476 case DW_CFA_restore_extended:
477 case DW_CFA_undefined:
478 case DW_CFA_same_value:
479 case DW_CFA_def_cfa_register:
480 case DW_CFA_register:
481 case DW_CFA_expression:
482 return dw_cfi_oprnd_reg_num;
484 case DW_CFA_def_cfa_offset:
485 case DW_CFA_GNU_args_size:
486 case DW_CFA_def_cfa_offset_sf:
487 return dw_cfi_oprnd_offset;
489 case DW_CFA_def_cfa_expression:
490 return dw_cfi_oprnd_loc;
492 default:
493 gcc_unreachable ();
497 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
499 enum dw_cfi_oprnd_type
500 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
502 switch (cfi)
504 case DW_CFA_def_cfa:
505 case DW_CFA_def_cfa_sf:
506 case DW_CFA_offset:
507 case DW_CFA_offset_extended_sf:
508 case DW_CFA_offset_extended:
509 return dw_cfi_oprnd_offset;
511 case DW_CFA_register:
512 return dw_cfi_oprnd_reg_num;
514 case DW_CFA_expression:
515 return dw_cfi_oprnd_loc;
517 default:
518 return dw_cfi_oprnd_unused;
522 /* Output one FDE. */
524 static void
525 output_fde (dw_fde_ref fde, bool for_eh, bool second,
526 char *section_start_label, int fde_encoding, char *augmentation,
527 bool any_lsda_needed, int lsda_encoding)
529 const char *begin, *end;
530 static unsigned int j;
531 char l1[20], l2[20];
533 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
534 /* empty */ 0);
535 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
536 for_eh + j);
537 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
538 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
539 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
540 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
541 " indicating 64-bit DWARF extension");
542 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
543 "FDE Length");
544 ASM_OUTPUT_LABEL (asm_out_file, l1);
546 if (for_eh)
547 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
548 else
549 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
550 debug_frame_section, "FDE CIE offset");
552 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
553 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
555 if (for_eh)
557 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
558 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
559 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
560 "FDE initial location");
561 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
562 end, begin, "FDE address range");
564 else
566 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
567 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
570 if (augmentation[0])
572 if (any_lsda_needed)
574 int size = size_of_encoded_value (lsda_encoding);
576 if (lsda_encoding == DW_EH_PE_aligned)
578 int offset = ( 4 /* Length */
579 + 4 /* CIE offset */
580 + 2 * size_of_encoded_value (fde_encoding)
581 + 1 /* Augmentation size */ );
582 int pad = -offset & (PTR_SIZE - 1);
584 size += pad;
585 gcc_assert (size_of_uleb128 (size) == 1);
588 dw2_asm_output_data_uleb128 (size, "Augmentation size");
590 if (fde->uses_eh_lsda)
592 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
593 fde->funcdef_number);
594 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
595 gen_rtx_SYMBOL_REF (Pmode, l1),
596 false,
597 "Language Specific Data Area");
599 else
601 if (lsda_encoding == DW_EH_PE_aligned)
602 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
603 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
604 "Language Specific Data Area (none)");
607 else
608 dw2_asm_output_data_uleb128 (0, "Augmentation size");
611 /* Loop through the Call Frame Instructions associated with this FDE. */
612 fde->dw_fde_current_label = begin;
614 size_t from, until, i;
616 from = 0;
617 until = vec_safe_length (fde->dw_fde_cfi);
619 if (fde->dw_fde_second_begin == NULL)
621 else if (!second)
622 until = fde->dw_fde_switch_cfi_index;
623 else
624 from = fde->dw_fde_switch_cfi_index;
626 for (i = from; i < until; i++)
627 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
630 /* If we are to emit a ref/link from function bodies to their frame tables,
631 do it now. This is typically performed to make sure that tables
632 associated with functions are dragged with them and not discarded in
633 garbage collecting links. We need to do this on a per function basis to
634 cope with -ffunction-sections. */
636 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
637 /* Switch to the function section, emit the ref to the tables, and
638 switch *back* into the table section. */
639 switch_to_section (function_section (fde->decl));
640 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
641 switch_to_frame_table_section (for_eh, true);
642 #endif
644 /* Pad the FDE out to an address sized boundary. */
645 ASM_OUTPUT_ALIGN (asm_out_file,
646 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
647 ASM_OUTPUT_LABEL (asm_out_file, l2);
649 j += 2;
652 /* Return true if frame description entry FDE is needed for EH. */
654 static bool
655 fde_needed_for_eh_p (dw_fde_ref fde)
657 if (flag_asynchronous_unwind_tables)
658 return true;
660 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
661 return true;
663 if (fde->uses_eh_lsda)
664 return true;
666 /* If exceptions are enabled, we have collected nothrow info. */
667 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
668 return false;
670 return true;
673 /* Output the call frame information used to record information
674 that relates to calculating the frame pointer, and records the
675 location of saved registers. */
677 static void
678 output_call_frame_info (int for_eh)
680 unsigned int i;
681 dw_fde_ref fde;
682 dw_cfi_ref cfi;
683 char l1[20], l2[20], section_start_label[20];
684 bool any_lsda_needed = false;
685 char augmentation[6];
686 int augmentation_size;
687 int fde_encoding = DW_EH_PE_absptr;
688 int per_encoding = DW_EH_PE_absptr;
689 int lsda_encoding = DW_EH_PE_absptr;
690 int return_reg;
691 rtx personality = NULL;
692 int dw_cie_version;
694 /* Don't emit a CIE if there won't be any FDEs. */
695 if (!fde_vec)
696 return;
698 /* Nothing to do if the assembler's doing it all. */
699 if (dwarf2out_do_cfi_asm ())
700 return;
702 /* If we don't have any functions we'll want to unwind out of, don't emit
703 any EH unwind information. If we make FDEs linkonce, we may have to
704 emit an empty label for an FDE that wouldn't otherwise be emitted. We
705 want to avoid having an FDE kept around when the function it refers to
706 is discarded. Example where this matters: a primary function template
707 in C++ requires EH information, an explicit specialization doesn't. */
708 if (for_eh)
710 bool any_eh_needed = false;
712 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
714 if (fde->uses_eh_lsda)
715 any_eh_needed = any_lsda_needed = true;
716 else if (fde_needed_for_eh_p (fde))
717 any_eh_needed = true;
718 else if (TARGET_USES_WEAK_UNWIND_INFO)
719 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
722 if (!any_eh_needed)
723 return;
726 /* We're going to be generating comments, so turn on app. */
727 if (flag_debug_asm)
728 app_enable ();
730 /* Switch to the proper frame section, first time. */
731 switch_to_frame_table_section (for_eh, false);
733 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
734 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
736 /* Output the CIE. */
737 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
738 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
739 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
740 dw2_asm_output_data (4, 0xffffffff,
741 "Initial length escape value indicating 64-bit DWARF extension");
742 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
743 "Length of Common Information Entry");
744 ASM_OUTPUT_LABEL (asm_out_file, l1);
746 /* Now that the CIE pointer is PC-relative for EH,
747 use 0 to identify the CIE. */
748 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
749 (for_eh ? 0 : DWARF_CIE_ID),
750 "CIE Identifier Tag");
752 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
753 use CIE version 1, unless that would produce incorrect results
754 due to overflowing the return register column. */
755 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
756 dw_cie_version = 1;
757 if (return_reg >= 256 || dwarf_version > 2)
758 dw_cie_version = 3;
759 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
761 augmentation[0] = 0;
762 augmentation_size = 0;
764 personality = current_unit_personality;
765 if (for_eh)
767 char *p;
769 /* Augmentation:
770 z Indicates that a uleb128 is present to size the
771 augmentation section.
772 L Indicates the encoding (and thus presence) of
773 an LSDA pointer in the FDE augmentation.
774 R Indicates a non-default pointer encoding for
775 FDE code pointers.
776 P Indicates the presence of an encoding + language
777 personality routine in the CIE augmentation. */
779 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
780 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
781 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
783 p = augmentation + 1;
784 if (personality)
786 *p++ = 'P';
787 augmentation_size += 1 + size_of_encoded_value (per_encoding);
788 assemble_external_libcall (personality);
790 if (any_lsda_needed)
792 *p++ = 'L';
793 augmentation_size += 1;
795 if (fde_encoding != DW_EH_PE_absptr)
797 *p++ = 'R';
798 augmentation_size += 1;
800 if (p > augmentation + 1)
802 augmentation[0] = 'z';
803 *p = '\0';
806 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
807 if (personality && per_encoding == DW_EH_PE_aligned)
809 int offset = ( 4 /* Length */
810 + 4 /* CIE Id */
811 + 1 /* CIE version */
812 + strlen (augmentation) + 1 /* Augmentation */
813 + size_of_uleb128 (1) /* Code alignment */
814 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
815 + 1 /* RA column */
816 + 1 /* Augmentation size */
817 + 1 /* Personality encoding */ );
818 int pad = -offset & (PTR_SIZE - 1);
820 augmentation_size += pad;
822 /* Augmentations should be small, so there's scarce need to
823 iterate for a solution. Die if we exceed one uleb128 byte. */
824 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
828 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
829 if (dw_cie_version >= 4)
831 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
832 dw2_asm_output_data (1, 0, "CIE Segment Size");
834 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
835 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
836 "CIE Data Alignment Factor");
838 if (dw_cie_version == 1)
839 dw2_asm_output_data (1, return_reg, "CIE RA Column");
840 else
841 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
843 if (augmentation[0])
845 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
846 if (personality)
848 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
849 eh_data_format_name (per_encoding));
850 dw2_asm_output_encoded_addr_rtx (per_encoding,
851 personality,
852 true, NULL);
855 if (any_lsda_needed)
856 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
857 eh_data_format_name (lsda_encoding));
859 if (fde_encoding != DW_EH_PE_absptr)
860 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
861 eh_data_format_name (fde_encoding));
864 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
865 output_cfi (cfi, NULL, for_eh);
867 /* Pad the CIE out to an address sized boundary. */
868 ASM_OUTPUT_ALIGN (asm_out_file,
869 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
870 ASM_OUTPUT_LABEL (asm_out_file, l2);
872 /* Loop through all of the FDE's. */
873 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
875 unsigned int k;
877 /* Don't emit EH unwind info for leaf functions that don't need it. */
878 if (for_eh && !fde_needed_for_eh_p (fde))
879 continue;
881 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
882 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
883 augmentation, any_lsda_needed, lsda_encoding);
886 if (for_eh && targetm.terminate_dw2_eh_frame_info)
887 dw2_asm_output_data (4, 0, "End of Table");
889 /* Turn off app to make assembly quicker. */
890 if (flag_debug_asm)
891 app_disable ();
894 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
896 static void
897 dwarf2out_do_cfi_startproc (bool second)
899 int enc;
900 rtx ref;
901 rtx personality = get_personality_function (current_function_decl);
903 fprintf (asm_out_file, "\t.cfi_startproc\n");
905 if (personality)
907 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
908 ref = personality;
910 /* ??? The GAS support isn't entirely consistent. We have to
911 handle indirect support ourselves, but PC-relative is done
912 in the assembler. Further, the assembler can't handle any
913 of the weirder relocation types. */
914 if (enc & DW_EH_PE_indirect)
915 ref = dw2_force_const_mem (ref, true);
917 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
918 output_addr_const (asm_out_file, ref);
919 fputc ('\n', asm_out_file);
922 if (crtl->uses_eh_lsda)
924 char lab[20];
926 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
927 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
928 current_function_funcdef_no);
929 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
930 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
932 if (enc & DW_EH_PE_indirect)
933 ref = dw2_force_const_mem (ref, true);
935 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
936 output_addr_const (asm_out_file, ref);
937 fputc ('\n', asm_out_file);
941 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
942 this allocation may be done before pass_final. */
944 dw_fde_ref
945 dwarf2out_alloc_current_fde (void)
947 dw_fde_ref fde;
949 fde = ggc_alloc_cleared_dw_fde_node ();
950 fde->decl = current_function_decl;
951 fde->funcdef_number = current_function_funcdef_no;
952 fde->fde_index = vec_safe_length (fde_vec);
953 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
954 fde->uses_eh_lsda = crtl->uses_eh_lsda;
955 fde->nothrow = crtl->nothrow;
956 fde->drap_reg = INVALID_REGNUM;
957 fde->vdrap_reg = INVALID_REGNUM;
959 /* Record the FDE associated with this function. */
960 cfun->fde = fde;
961 vec_safe_push (fde_vec, fde);
963 return fde;
966 /* Output a marker (i.e. a label) for the beginning of a function, before
967 the prologue. */
969 void
970 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
971 const char *file ATTRIBUTE_UNUSED)
973 char label[MAX_ARTIFICIAL_LABEL_BYTES];
974 char * dup_label;
975 dw_fde_ref fde;
976 section *fnsec;
977 bool do_frame;
979 current_function_func_begin_label = NULL;
981 do_frame = dwarf2out_do_frame ();
983 /* ??? current_function_func_begin_label is also used by except.c for
984 call-site information. We must emit this label if it might be used. */
985 if (!do_frame
986 && (!flag_exceptions
987 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
988 return;
990 fnsec = function_section (current_function_decl);
991 switch_to_section (fnsec);
992 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
993 current_function_funcdef_no);
994 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
995 current_function_funcdef_no);
996 dup_label = xstrdup (label);
997 current_function_func_begin_label = dup_label;
999 /* We can elide the fde allocation if we're not emitting debug info. */
1000 if (!do_frame)
1001 return;
1003 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1004 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1005 would include pass_dwarf2_frame. If we've not created the FDE yet,
1006 do so now. */
1007 fde = cfun->fde;
1008 if (fde == NULL)
1009 fde = dwarf2out_alloc_current_fde ();
1011 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1012 fde->dw_fde_begin = dup_label;
1013 fde->dw_fde_current_label = dup_label;
1014 fde->in_std_section = (fnsec == text_section
1015 || (cold_text_section && fnsec == cold_text_section));
1017 /* We only want to output line number information for the genuine dwarf2
1018 prologue case, not the eh frame case. */
1019 #ifdef DWARF2_DEBUGGING_INFO
1020 if (file)
1021 dwarf2out_source_line (line, file, 0, true);
1022 #endif
1024 if (dwarf2out_do_cfi_asm ())
1025 dwarf2out_do_cfi_startproc (false);
1026 else
1028 rtx personality = get_personality_function (current_function_decl);
1029 if (!current_unit_personality)
1030 current_unit_personality = personality;
1032 /* We cannot keep a current personality per function as without CFI
1033 asm, at the point where we emit the CFI data, there is no current
1034 function anymore. */
1035 if (personality && current_unit_personality != personality)
1036 sorry ("multiple EH personalities are supported only with assemblers "
1037 "supporting .cfi_personality directive");
1041 /* Output a marker (i.e. a label) for the end of the generated code
1042 for a function prologue. This gets called *after* the prologue code has
1043 been generated. */
1045 void
1046 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1047 const char *file ATTRIBUTE_UNUSED)
1049 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1051 /* Output a label to mark the endpoint of the code generated for this
1052 function. */
1053 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1054 current_function_funcdef_no);
1055 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1056 current_function_funcdef_no);
1057 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1060 /* Output a marker (i.e. a label) for the beginning of the generated code
1061 for a function epilogue. This gets called *before* the prologue code has
1062 been generated. */
1064 void
1065 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1066 const char *file ATTRIBUTE_UNUSED)
1068 dw_fde_ref fde = cfun->fde;
1069 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1071 if (fde->dw_fde_vms_begin_epilogue)
1072 return;
1074 /* Output a label to mark the endpoint of the code generated for this
1075 function. */
1076 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1077 current_function_funcdef_no);
1078 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1079 current_function_funcdef_no);
1080 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1083 /* Output a marker (i.e. a label) for the absolute end of the generated code
1084 for a function definition. This gets called *after* the epilogue code has
1085 been generated. */
1087 void
1088 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1089 const char *file ATTRIBUTE_UNUSED)
1091 dw_fde_ref fde;
1092 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1094 last_var_location_insn = NULL_RTX;
1095 cached_next_real_insn = NULL_RTX;
1097 if (dwarf2out_do_cfi_asm ())
1098 fprintf (asm_out_file, "\t.cfi_endproc\n");
1100 /* Output a label to mark the endpoint of the code generated for this
1101 function. */
1102 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1103 current_function_funcdef_no);
1104 ASM_OUTPUT_LABEL (asm_out_file, label);
1105 fde = cfun->fde;
1106 gcc_assert (fde != NULL);
1107 if (fde->dw_fde_second_begin == NULL)
1108 fde->dw_fde_end = xstrdup (label);
1111 void
1112 dwarf2out_frame_finish (void)
1114 /* Output call frame information. */
1115 if (targetm.debug_unwind_info () == UI_DWARF2)
1116 output_call_frame_info (0);
1118 /* Output another copy for the unwinder. */
1119 if ((flag_unwind_tables || flag_exceptions)
1120 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1121 output_call_frame_info (1);
1124 /* Note that the current function section is being used for code. */
1126 static void
1127 dwarf2out_note_section_used (void)
1129 section *sec = current_function_section ();
1130 if (sec == text_section)
1131 text_section_used = true;
1132 else if (sec == cold_text_section)
1133 cold_text_section_used = true;
1136 static void var_location_switch_text_section (void);
1137 static void set_cur_line_info_table (section *);
1139 void
1140 dwarf2out_switch_text_section (void)
1142 section *sect;
1143 dw_fde_ref fde = cfun->fde;
1145 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1147 if (!in_cold_section_p)
1149 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1150 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1151 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1153 else
1155 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1156 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1157 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1159 have_multiple_function_sections = true;
1161 /* There is no need to mark used sections when not debugging. */
1162 if (cold_text_section != NULL)
1163 dwarf2out_note_section_used ();
1165 if (dwarf2out_do_cfi_asm ())
1166 fprintf (asm_out_file, "\t.cfi_endproc\n");
1168 /* Now do the real section switch. */
1169 sect = current_function_section ();
1170 switch_to_section (sect);
1172 fde->second_in_std_section
1173 = (sect == text_section
1174 || (cold_text_section && sect == cold_text_section));
1176 if (dwarf2out_do_cfi_asm ())
1177 dwarf2out_do_cfi_startproc (true);
1179 var_location_switch_text_section ();
1181 if (cold_text_section != NULL)
1182 set_cur_line_info_table (sect);
1185 /* And now, the subset of the debugging information support code necessary
1186 for emitting location expressions. */
1188 /* Data about a single source file. */
1189 struct GTY(()) dwarf_file_data {
1190 const char * filename;
1191 int emitted_number;
1194 typedef struct GTY(()) deferred_locations_struct
1196 tree variable;
1197 dw_die_ref die;
1198 } deferred_locations;
1201 static GTY(()) vec<deferred_locations, va_gc> *deferred_locations_list;
1204 /* Describe an entry into the .debug_addr section. */
1206 enum ate_kind {
1207 ate_kind_rtx,
1208 ate_kind_rtx_dtprel,
1209 ate_kind_label
1212 typedef struct GTY(()) addr_table_entry_struct {
1213 enum ate_kind kind;
1214 unsigned int refcount;
1215 unsigned int index;
1216 union addr_table_entry_struct_union
1218 rtx GTY ((tag ("0"))) rtl;
1219 char * GTY ((tag ("1"))) label;
1221 GTY ((desc ("%1.kind"))) addr;
1223 addr_table_entry;
1225 /* Location lists are ranges + location descriptions for that range,
1226 so you can track variables that are in different places over
1227 their entire life. */
1228 typedef struct GTY(()) dw_loc_list_struct {
1229 dw_loc_list_ref dw_loc_next;
1230 const char *begin; /* Label and addr_entry for start of range */
1231 addr_table_entry *begin_entry;
1232 const char *end; /* Label for end of range */
1233 char *ll_symbol; /* Label for beginning of location list.
1234 Only on head of list */
1235 const char *section; /* Section this loclist is relative to */
1236 dw_loc_descr_ref expr;
1237 hashval_t hash;
1238 /* True if all addresses in this and subsequent lists are known to be
1239 resolved. */
1240 bool resolved_addr;
1241 /* True if this list has been replaced by dw_loc_next. */
1242 bool replaced;
1243 bool emitted;
1244 /* True if the range should be emitted even if begin and end
1245 are the same. */
1246 bool force;
1247 } dw_loc_list_node;
1249 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1251 /* Convert a DWARF stack opcode into its string name. */
1253 static const char *
1254 dwarf_stack_op_name (unsigned int op)
1256 const char *name = get_DW_OP_name (op);
1258 if (name != NULL)
1259 return name;
1261 return "OP_<unknown>";
1264 /* Return a pointer to a newly allocated location description. Location
1265 descriptions are simple expression terms that can be strung
1266 together to form more complicated location (address) descriptions. */
1268 static inline dw_loc_descr_ref
1269 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1270 unsigned HOST_WIDE_INT oprnd2)
1272 dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
1274 descr->dw_loc_opc = op;
1275 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1276 descr->dw_loc_oprnd1.val_entry = NULL;
1277 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1278 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1279 descr->dw_loc_oprnd2.val_entry = NULL;
1280 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1282 return descr;
1285 /* Return a pointer to a newly allocated location description for
1286 REG and OFFSET. */
1288 static inline dw_loc_descr_ref
1289 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1291 if (reg <= 31)
1292 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1293 offset, 0);
1294 else
1295 return new_loc_descr (DW_OP_bregx, reg, offset);
1298 /* Add a location description term to a location description expression. */
1300 static inline void
1301 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1303 dw_loc_descr_ref *d;
1305 /* Find the end of the chain. */
1306 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1309 *d = descr;
1312 /* Compare two location operands for exact equality. */
1314 static bool
1315 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1317 if (a->val_class != b->val_class)
1318 return false;
1319 switch (a->val_class)
1321 case dw_val_class_none:
1322 return true;
1323 case dw_val_class_addr:
1324 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1326 case dw_val_class_offset:
1327 case dw_val_class_unsigned_const:
1328 case dw_val_class_const:
1329 case dw_val_class_range_list:
1330 case dw_val_class_lineptr:
1331 case dw_val_class_macptr:
1332 /* These are all HOST_WIDE_INT, signed or unsigned. */
1333 return a->v.val_unsigned == b->v.val_unsigned;
1335 case dw_val_class_loc:
1336 return a->v.val_loc == b->v.val_loc;
1337 case dw_val_class_loc_list:
1338 return a->v.val_loc_list == b->v.val_loc_list;
1339 case dw_val_class_die_ref:
1340 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1341 case dw_val_class_fde_ref:
1342 return a->v.val_fde_index == b->v.val_fde_index;
1343 case dw_val_class_lbl_id:
1344 case dw_val_class_high_pc:
1345 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1346 case dw_val_class_str:
1347 return a->v.val_str == b->v.val_str;
1348 case dw_val_class_flag:
1349 return a->v.val_flag == b->v.val_flag;
1350 case dw_val_class_file:
1351 return a->v.val_file == b->v.val_file;
1352 case dw_val_class_decl_ref:
1353 return a->v.val_decl_ref == b->v.val_decl_ref;
1355 case dw_val_class_const_double:
1356 return (a->v.val_double.high == b->v.val_double.high
1357 && a->v.val_double.low == b->v.val_double.low);
1359 case dw_val_class_vec:
1361 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1362 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1364 return (a_len == b_len
1365 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1368 case dw_val_class_data8:
1369 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1371 case dw_val_class_vms_delta:
1372 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1373 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1375 gcc_unreachable ();
1378 /* Compare two location atoms for exact equality. */
1380 static bool
1381 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1383 if (a->dw_loc_opc != b->dw_loc_opc)
1384 return false;
1386 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1387 address size, but since we always allocate cleared storage it
1388 should be zero for other types of locations. */
1389 if (a->dtprel != b->dtprel)
1390 return false;
1392 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1393 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1396 /* Compare two complete location expressions for exact equality. */
1398 bool
1399 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1401 while (1)
1403 if (a == b)
1404 return true;
1405 if (a == NULL || b == NULL)
1406 return false;
1407 if (!loc_descr_equal_p_1 (a, b))
1408 return false;
1410 a = a->dw_loc_next;
1411 b = b->dw_loc_next;
1416 /* Add a constant OFFSET to a location expression. */
1418 static void
1419 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1421 dw_loc_descr_ref loc;
1422 HOST_WIDE_INT *p;
1424 gcc_assert (*list_head != NULL);
1426 if (!offset)
1427 return;
1429 /* Find the end of the chain. */
1430 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1433 p = NULL;
1434 if (loc->dw_loc_opc == DW_OP_fbreg
1435 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1436 p = &loc->dw_loc_oprnd1.v.val_int;
1437 else if (loc->dw_loc_opc == DW_OP_bregx)
1438 p = &loc->dw_loc_oprnd2.v.val_int;
1440 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1441 offset. Don't optimize if an signed integer overflow would happen. */
1442 if (p != NULL
1443 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1444 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1445 *p += offset;
1447 else if (offset > 0)
1448 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1450 else
1452 loc->dw_loc_next = int_loc_descriptor (-offset);
1453 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1457 /* Add a constant OFFSET to a location list. */
1459 static void
1460 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1462 dw_loc_list_ref d;
1463 for (d = list_head; d != NULL; d = d->dw_loc_next)
1464 loc_descr_plus_const (&d->expr, offset);
1467 #define DWARF_REF_SIZE \
1468 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1470 static unsigned long int get_base_type_offset (dw_die_ref);
1472 /* Return the size of a location descriptor. */
1474 static unsigned long
1475 size_of_loc_descr (dw_loc_descr_ref loc)
1477 unsigned long size = 1;
1479 switch (loc->dw_loc_opc)
1481 case DW_OP_addr:
1482 size += DWARF2_ADDR_SIZE;
1483 break;
1484 case DW_OP_GNU_addr_index:
1485 case DW_OP_GNU_const_index:
1486 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1487 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1488 break;
1489 case DW_OP_const1u:
1490 case DW_OP_const1s:
1491 size += 1;
1492 break;
1493 case DW_OP_const2u:
1494 case DW_OP_const2s:
1495 size += 2;
1496 break;
1497 case DW_OP_const4u:
1498 case DW_OP_const4s:
1499 size += 4;
1500 break;
1501 case DW_OP_const8u:
1502 case DW_OP_const8s:
1503 size += 8;
1504 break;
1505 case DW_OP_constu:
1506 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1507 break;
1508 case DW_OP_consts:
1509 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1510 break;
1511 case DW_OP_pick:
1512 size += 1;
1513 break;
1514 case DW_OP_plus_uconst:
1515 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1516 break;
1517 case DW_OP_skip:
1518 case DW_OP_bra:
1519 size += 2;
1520 break;
1521 case DW_OP_breg0:
1522 case DW_OP_breg1:
1523 case DW_OP_breg2:
1524 case DW_OP_breg3:
1525 case DW_OP_breg4:
1526 case DW_OP_breg5:
1527 case DW_OP_breg6:
1528 case DW_OP_breg7:
1529 case DW_OP_breg8:
1530 case DW_OP_breg9:
1531 case DW_OP_breg10:
1532 case DW_OP_breg11:
1533 case DW_OP_breg12:
1534 case DW_OP_breg13:
1535 case DW_OP_breg14:
1536 case DW_OP_breg15:
1537 case DW_OP_breg16:
1538 case DW_OP_breg17:
1539 case DW_OP_breg18:
1540 case DW_OP_breg19:
1541 case DW_OP_breg20:
1542 case DW_OP_breg21:
1543 case DW_OP_breg22:
1544 case DW_OP_breg23:
1545 case DW_OP_breg24:
1546 case DW_OP_breg25:
1547 case DW_OP_breg26:
1548 case DW_OP_breg27:
1549 case DW_OP_breg28:
1550 case DW_OP_breg29:
1551 case DW_OP_breg30:
1552 case DW_OP_breg31:
1553 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1554 break;
1555 case DW_OP_regx:
1556 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1557 break;
1558 case DW_OP_fbreg:
1559 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1560 break;
1561 case DW_OP_bregx:
1562 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1563 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1564 break;
1565 case DW_OP_piece:
1566 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1567 break;
1568 case DW_OP_bit_piece:
1569 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1570 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1571 break;
1572 case DW_OP_deref_size:
1573 case DW_OP_xderef_size:
1574 size += 1;
1575 break;
1576 case DW_OP_call2:
1577 size += 2;
1578 break;
1579 case DW_OP_call4:
1580 size += 4;
1581 break;
1582 case DW_OP_call_ref:
1583 size += DWARF_REF_SIZE;
1584 break;
1585 case DW_OP_implicit_value:
1586 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1587 + loc->dw_loc_oprnd1.v.val_unsigned;
1588 break;
1589 case DW_OP_GNU_implicit_pointer:
1590 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1591 break;
1592 case DW_OP_GNU_entry_value:
1594 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1595 size += size_of_uleb128 (op_size) + op_size;
1596 break;
1598 case DW_OP_GNU_const_type:
1600 unsigned long o
1601 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1602 size += size_of_uleb128 (o) + 1;
1603 switch (loc->dw_loc_oprnd2.val_class)
1605 case dw_val_class_vec:
1606 size += loc->dw_loc_oprnd2.v.val_vec.length
1607 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1608 break;
1609 case dw_val_class_const:
1610 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1611 break;
1612 case dw_val_class_const_double:
1613 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1614 break;
1615 default:
1616 gcc_unreachable ();
1618 break;
1620 case DW_OP_GNU_regval_type:
1622 unsigned long o
1623 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1624 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1625 + size_of_uleb128 (o);
1627 break;
1628 case DW_OP_GNU_deref_type:
1630 unsigned long o
1631 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1632 size += 1 + size_of_uleb128 (o);
1634 break;
1635 case DW_OP_GNU_convert:
1636 case DW_OP_GNU_reinterpret:
1637 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1638 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1639 else
1641 unsigned long o
1642 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1643 size += size_of_uleb128 (o);
1645 break;
1646 case DW_OP_GNU_parameter_ref:
1647 size += 4;
1648 break;
1649 default:
1650 break;
1653 return size;
1656 /* Return the size of a series of location descriptors. */
1658 unsigned long
1659 size_of_locs (dw_loc_descr_ref loc)
1661 dw_loc_descr_ref l;
1662 unsigned long size;
1664 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1665 field, to avoid writing to a PCH file. */
1666 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1668 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1669 break;
1670 size += size_of_loc_descr (l);
1672 if (! l)
1673 return size;
1675 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1677 l->dw_loc_addr = size;
1678 size += size_of_loc_descr (l);
1681 return size;
1684 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1685 static void get_ref_die_offset_label (char *, dw_die_ref);
1686 static unsigned long int get_ref_die_offset (dw_die_ref);
1688 /* Output location description stack opcode's operands (if any).
1689 The for_eh_or_skip parameter controls whether register numbers are
1690 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1691 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1692 info). This should be suppressed for the cases that have not been converted
1693 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1695 static void
1696 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1698 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1699 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1701 switch (loc->dw_loc_opc)
1703 #ifdef DWARF2_DEBUGGING_INFO
1704 case DW_OP_const2u:
1705 case DW_OP_const2s:
1706 dw2_asm_output_data (2, val1->v.val_int, NULL);
1707 break;
1708 case DW_OP_const4u:
1709 if (loc->dtprel)
1711 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1712 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1713 val1->v.val_addr);
1714 fputc ('\n', asm_out_file);
1715 break;
1717 /* FALLTHRU */
1718 case DW_OP_const4s:
1719 dw2_asm_output_data (4, val1->v.val_int, NULL);
1720 break;
1721 case DW_OP_const8u:
1722 if (loc->dtprel)
1724 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1725 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1726 val1->v.val_addr);
1727 fputc ('\n', asm_out_file);
1728 break;
1730 /* FALLTHRU */
1731 case DW_OP_const8s:
1732 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1733 dw2_asm_output_data (8, val1->v.val_int, NULL);
1734 break;
1735 case DW_OP_skip:
1736 case DW_OP_bra:
1738 int offset;
1740 gcc_assert (val1->val_class == dw_val_class_loc);
1741 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1743 dw2_asm_output_data (2, offset, NULL);
1745 break;
1746 case DW_OP_implicit_value:
1747 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1748 switch (val2->val_class)
1750 case dw_val_class_const:
1751 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1752 break;
1753 case dw_val_class_vec:
1755 unsigned int elt_size = val2->v.val_vec.elt_size;
1756 unsigned int len = val2->v.val_vec.length;
1757 unsigned int i;
1758 unsigned char *p;
1760 if (elt_size > sizeof (HOST_WIDE_INT))
1762 elt_size /= 2;
1763 len *= 2;
1765 for (i = 0, p = val2->v.val_vec.array;
1766 i < len;
1767 i++, p += elt_size)
1768 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1769 "fp or vector constant word %u", i);
1771 break;
1772 case dw_val_class_const_double:
1774 unsigned HOST_WIDE_INT first, second;
1776 if (WORDS_BIG_ENDIAN)
1778 first = val2->v.val_double.high;
1779 second = val2->v.val_double.low;
1781 else
1783 first = val2->v.val_double.low;
1784 second = val2->v.val_double.high;
1786 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1787 first, NULL);
1788 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1789 second, NULL);
1791 break;
1792 case dw_val_class_addr:
1793 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1794 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1795 break;
1796 default:
1797 gcc_unreachable ();
1799 break;
1800 #else
1801 case DW_OP_const2u:
1802 case DW_OP_const2s:
1803 case DW_OP_const4u:
1804 case DW_OP_const4s:
1805 case DW_OP_const8u:
1806 case DW_OP_const8s:
1807 case DW_OP_skip:
1808 case DW_OP_bra:
1809 case DW_OP_implicit_value:
1810 /* We currently don't make any attempt to make sure these are
1811 aligned properly like we do for the main unwind info, so
1812 don't support emitting things larger than a byte if we're
1813 only doing unwinding. */
1814 gcc_unreachable ();
1815 #endif
1816 case DW_OP_const1u:
1817 case DW_OP_const1s:
1818 dw2_asm_output_data (1, val1->v.val_int, NULL);
1819 break;
1820 case DW_OP_constu:
1821 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1822 break;
1823 case DW_OP_consts:
1824 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1825 break;
1826 case DW_OP_pick:
1827 dw2_asm_output_data (1, val1->v.val_int, NULL);
1828 break;
1829 case DW_OP_plus_uconst:
1830 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1831 break;
1832 case DW_OP_breg0:
1833 case DW_OP_breg1:
1834 case DW_OP_breg2:
1835 case DW_OP_breg3:
1836 case DW_OP_breg4:
1837 case DW_OP_breg5:
1838 case DW_OP_breg6:
1839 case DW_OP_breg7:
1840 case DW_OP_breg8:
1841 case DW_OP_breg9:
1842 case DW_OP_breg10:
1843 case DW_OP_breg11:
1844 case DW_OP_breg12:
1845 case DW_OP_breg13:
1846 case DW_OP_breg14:
1847 case DW_OP_breg15:
1848 case DW_OP_breg16:
1849 case DW_OP_breg17:
1850 case DW_OP_breg18:
1851 case DW_OP_breg19:
1852 case DW_OP_breg20:
1853 case DW_OP_breg21:
1854 case DW_OP_breg22:
1855 case DW_OP_breg23:
1856 case DW_OP_breg24:
1857 case DW_OP_breg25:
1858 case DW_OP_breg26:
1859 case DW_OP_breg27:
1860 case DW_OP_breg28:
1861 case DW_OP_breg29:
1862 case DW_OP_breg30:
1863 case DW_OP_breg31:
1864 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1865 break;
1866 case DW_OP_regx:
1868 unsigned r = val1->v.val_unsigned;
1869 if (for_eh_or_skip >= 0)
1870 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1871 gcc_assert (size_of_uleb128 (r)
1872 == size_of_uleb128 (val1->v.val_unsigned));
1873 dw2_asm_output_data_uleb128 (r, NULL);
1875 break;
1876 case DW_OP_fbreg:
1877 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1878 break;
1879 case DW_OP_bregx:
1881 unsigned r = val1->v.val_unsigned;
1882 if (for_eh_or_skip >= 0)
1883 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1884 gcc_assert (size_of_uleb128 (r)
1885 == size_of_uleb128 (val1->v.val_unsigned));
1886 dw2_asm_output_data_uleb128 (r, NULL);
1887 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1889 break;
1890 case DW_OP_piece:
1891 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1892 break;
1893 case DW_OP_bit_piece:
1894 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1895 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1896 break;
1897 case DW_OP_deref_size:
1898 case DW_OP_xderef_size:
1899 dw2_asm_output_data (1, val1->v.val_int, NULL);
1900 break;
1902 case DW_OP_addr:
1903 if (loc->dtprel)
1905 if (targetm.asm_out.output_dwarf_dtprel)
1907 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
1908 DWARF2_ADDR_SIZE,
1909 val1->v.val_addr);
1910 fputc ('\n', asm_out_file);
1912 else
1913 gcc_unreachable ();
1915 else
1917 #ifdef DWARF2_DEBUGGING_INFO
1918 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
1919 #else
1920 gcc_unreachable ();
1921 #endif
1923 break;
1925 case DW_OP_GNU_addr_index:
1926 case DW_OP_GNU_const_index:
1927 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1928 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
1929 "(index into .debug_addr)");
1930 break;
1932 case DW_OP_GNU_implicit_pointer:
1934 char label[MAX_ARTIFICIAL_LABEL_BYTES
1935 + HOST_BITS_PER_WIDE_INT / 2 + 2];
1936 gcc_assert (val1->val_class == dw_val_class_die_ref);
1937 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
1938 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
1939 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1941 break;
1943 case DW_OP_GNU_entry_value:
1944 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
1945 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
1946 break;
1948 case DW_OP_GNU_const_type:
1950 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
1951 gcc_assert (o);
1952 dw2_asm_output_data_uleb128 (o, NULL);
1953 switch (val2->val_class)
1955 case dw_val_class_const:
1956 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
1957 dw2_asm_output_data (1, l, NULL);
1958 dw2_asm_output_data (l, val2->v.val_int, NULL);
1959 break;
1960 case dw_val_class_vec:
1962 unsigned int elt_size = val2->v.val_vec.elt_size;
1963 unsigned int len = val2->v.val_vec.length;
1964 unsigned int i;
1965 unsigned char *p;
1967 l = len * elt_size;
1968 dw2_asm_output_data (1, l, NULL);
1969 if (elt_size > sizeof (HOST_WIDE_INT))
1971 elt_size /= 2;
1972 len *= 2;
1974 for (i = 0, p = val2->v.val_vec.array;
1975 i < len;
1976 i++, p += elt_size)
1977 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1978 "fp or vector constant word %u", i);
1980 break;
1981 case dw_val_class_const_double:
1983 unsigned HOST_WIDE_INT first, second;
1984 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
1986 dw2_asm_output_data (1, 2 * l, NULL);
1987 if (WORDS_BIG_ENDIAN)
1989 first = val2->v.val_double.high;
1990 second = val2->v.val_double.low;
1992 else
1994 first = val2->v.val_double.low;
1995 second = val2->v.val_double.high;
1997 dw2_asm_output_data (l, first, NULL);
1998 dw2_asm_output_data (l, second, NULL);
2000 break;
2001 default:
2002 gcc_unreachable ();
2005 break;
2006 case DW_OP_GNU_regval_type:
2008 unsigned r = val1->v.val_unsigned;
2009 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2010 gcc_assert (o);
2011 if (for_eh_or_skip >= 0)
2013 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2014 gcc_assert (size_of_uleb128 (r)
2015 == size_of_uleb128 (val1->v.val_unsigned));
2017 dw2_asm_output_data_uleb128 (r, NULL);
2018 dw2_asm_output_data_uleb128 (o, NULL);
2020 break;
2021 case DW_OP_GNU_deref_type:
2023 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2024 gcc_assert (o);
2025 dw2_asm_output_data (1, val1->v.val_int, NULL);
2026 dw2_asm_output_data_uleb128 (o, NULL);
2028 break;
2029 case DW_OP_GNU_convert:
2030 case DW_OP_GNU_reinterpret:
2031 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2032 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2033 else
2035 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2036 gcc_assert (o);
2037 dw2_asm_output_data_uleb128 (o, NULL);
2039 break;
2041 case DW_OP_GNU_parameter_ref:
2043 unsigned long o;
2044 gcc_assert (val1->val_class == dw_val_class_die_ref);
2045 o = get_ref_die_offset (val1->v.val_die_ref.die);
2046 dw2_asm_output_data (4, o, NULL);
2048 break;
2050 default:
2051 /* Other codes have no operands. */
2052 break;
2056 /* Output a sequence of location operations.
2057 The for_eh_or_skip parameter controls whether register numbers are
2058 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2059 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2060 info). This should be suppressed for the cases that have not been converted
2061 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2063 void
2064 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2066 for (; loc != NULL; loc = loc->dw_loc_next)
2068 enum dwarf_location_atom opc = loc->dw_loc_opc;
2069 /* Output the opcode. */
2070 if (for_eh_or_skip >= 0
2071 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2073 unsigned r = (opc - DW_OP_breg0);
2074 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2075 gcc_assert (r <= 31);
2076 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2078 else if (for_eh_or_skip >= 0
2079 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2081 unsigned r = (opc - DW_OP_reg0);
2082 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2083 gcc_assert (r <= 31);
2084 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2087 dw2_asm_output_data (1, opc,
2088 "%s", dwarf_stack_op_name (opc));
2090 /* Output the operand(s) (if any). */
2091 output_loc_operands (loc, for_eh_or_skip);
2095 /* Output location description stack opcode's operands (if any).
2096 The output is single bytes on a line, suitable for .cfi_escape. */
2098 static void
2099 output_loc_operands_raw (dw_loc_descr_ref loc)
2101 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2102 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2104 switch (loc->dw_loc_opc)
2106 case DW_OP_addr:
2107 case DW_OP_GNU_addr_index:
2108 case DW_OP_GNU_const_index:
2109 case DW_OP_implicit_value:
2110 /* We cannot output addresses in .cfi_escape, only bytes. */
2111 gcc_unreachable ();
2113 case DW_OP_const1u:
2114 case DW_OP_const1s:
2115 case DW_OP_pick:
2116 case DW_OP_deref_size:
2117 case DW_OP_xderef_size:
2118 fputc (',', asm_out_file);
2119 dw2_asm_output_data_raw (1, val1->v.val_int);
2120 break;
2122 case DW_OP_const2u:
2123 case DW_OP_const2s:
2124 fputc (',', asm_out_file);
2125 dw2_asm_output_data_raw (2, val1->v.val_int);
2126 break;
2128 case DW_OP_const4u:
2129 case DW_OP_const4s:
2130 fputc (',', asm_out_file);
2131 dw2_asm_output_data_raw (4, val1->v.val_int);
2132 break;
2134 case DW_OP_const8u:
2135 case DW_OP_const8s:
2136 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2137 fputc (',', asm_out_file);
2138 dw2_asm_output_data_raw (8, val1->v.val_int);
2139 break;
2141 case DW_OP_skip:
2142 case DW_OP_bra:
2144 int offset;
2146 gcc_assert (val1->val_class == dw_val_class_loc);
2147 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2149 fputc (',', asm_out_file);
2150 dw2_asm_output_data_raw (2, offset);
2152 break;
2154 case DW_OP_regx:
2156 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2157 gcc_assert (size_of_uleb128 (r)
2158 == size_of_uleb128 (val1->v.val_unsigned));
2159 fputc (',', asm_out_file);
2160 dw2_asm_output_data_uleb128_raw (r);
2162 break;
2164 case DW_OP_constu:
2165 case DW_OP_plus_uconst:
2166 case DW_OP_piece:
2167 fputc (',', asm_out_file);
2168 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2169 break;
2171 case DW_OP_bit_piece:
2172 fputc (',', asm_out_file);
2173 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2174 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2175 break;
2177 case DW_OP_consts:
2178 case DW_OP_breg0:
2179 case DW_OP_breg1:
2180 case DW_OP_breg2:
2181 case DW_OP_breg3:
2182 case DW_OP_breg4:
2183 case DW_OP_breg5:
2184 case DW_OP_breg6:
2185 case DW_OP_breg7:
2186 case DW_OP_breg8:
2187 case DW_OP_breg9:
2188 case DW_OP_breg10:
2189 case DW_OP_breg11:
2190 case DW_OP_breg12:
2191 case DW_OP_breg13:
2192 case DW_OP_breg14:
2193 case DW_OP_breg15:
2194 case DW_OP_breg16:
2195 case DW_OP_breg17:
2196 case DW_OP_breg18:
2197 case DW_OP_breg19:
2198 case DW_OP_breg20:
2199 case DW_OP_breg21:
2200 case DW_OP_breg22:
2201 case DW_OP_breg23:
2202 case DW_OP_breg24:
2203 case DW_OP_breg25:
2204 case DW_OP_breg26:
2205 case DW_OP_breg27:
2206 case DW_OP_breg28:
2207 case DW_OP_breg29:
2208 case DW_OP_breg30:
2209 case DW_OP_breg31:
2210 case DW_OP_fbreg:
2211 fputc (',', asm_out_file);
2212 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2213 break;
2215 case DW_OP_bregx:
2217 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2218 gcc_assert (size_of_uleb128 (r)
2219 == size_of_uleb128 (val1->v.val_unsigned));
2220 fputc (',', asm_out_file);
2221 dw2_asm_output_data_uleb128_raw (r);
2222 fputc (',', asm_out_file);
2223 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2225 break;
2227 case DW_OP_GNU_implicit_pointer:
2228 case DW_OP_GNU_entry_value:
2229 case DW_OP_GNU_const_type:
2230 case DW_OP_GNU_regval_type:
2231 case DW_OP_GNU_deref_type:
2232 case DW_OP_GNU_convert:
2233 case DW_OP_GNU_reinterpret:
2234 case DW_OP_GNU_parameter_ref:
2235 gcc_unreachable ();
2236 break;
2238 default:
2239 /* Other codes have no operands. */
2240 break;
2244 void
2245 output_loc_sequence_raw (dw_loc_descr_ref loc)
2247 while (1)
2249 enum dwarf_location_atom opc = loc->dw_loc_opc;
2250 /* Output the opcode. */
2251 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2253 unsigned r = (opc - DW_OP_breg0);
2254 r = DWARF2_FRAME_REG_OUT (r, 1);
2255 gcc_assert (r <= 31);
2256 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2258 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2260 unsigned r = (opc - DW_OP_reg0);
2261 r = DWARF2_FRAME_REG_OUT (r, 1);
2262 gcc_assert (r <= 31);
2263 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2265 /* Output the opcode. */
2266 fprintf (asm_out_file, "%#x", opc);
2267 output_loc_operands_raw (loc);
2269 if (!loc->dw_loc_next)
2270 break;
2271 loc = loc->dw_loc_next;
2273 fputc (',', asm_out_file);
2277 /* This function builds a dwarf location descriptor sequence from a
2278 dw_cfa_location, adding the given OFFSET to the result of the
2279 expression. */
2281 struct dw_loc_descr_struct *
2282 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2284 struct dw_loc_descr_struct *head, *tmp;
2286 offset += cfa->offset;
2288 if (cfa->indirect)
2290 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2291 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2292 head->dw_loc_oprnd1.val_entry = NULL;
2293 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2294 add_loc_descr (&head, tmp);
2295 if (offset != 0)
2297 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2298 add_loc_descr (&head, tmp);
2301 else
2302 head = new_reg_loc_descr (cfa->reg, offset);
2304 return head;
2307 /* This function builds a dwarf location descriptor sequence for
2308 the address at OFFSET from the CFA when stack is aligned to
2309 ALIGNMENT byte. */
2311 struct dw_loc_descr_struct *
2312 build_cfa_aligned_loc (dw_cfa_location *cfa,
2313 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2315 struct dw_loc_descr_struct *head;
2316 unsigned int dwarf_fp
2317 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2319 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2320 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2322 head = new_reg_loc_descr (dwarf_fp, 0);
2323 add_loc_descr (&head, int_loc_descriptor (alignment));
2324 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2325 loc_descr_plus_const (&head, offset);
2327 else
2328 head = new_reg_loc_descr (dwarf_fp, offset);
2329 return head;
2332 /* And now, the support for symbolic debugging information. */
2334 /* .debug_str support. */
2335 static int output_indirect_string (void **, void *);
2337 static void dwarf2out_init (const char *);
2338 static void dwarf2out_finish (const char *);
2339 static void dwarf2out_assembly_start (void);
2340 static void dwarf2out_define (unsigned int, const char *);
2341 static void dwarf2out_undef (unsigned int, const char *);
2342 static void dwarf2out_start_source_file (unsigned, const char *);
2343 static void dwarf2out_end_source_file (unsigned);
2344 static void dwarf2out_function_decl (tree);
2345 static void dwarf2out_begin_block (unsigned, unsigned);
2346 static void dwarf2out_end_block (unsigned, unsigned);
2347 static bool dwarf2out_ignore_block (const_tree);
2348 static void dwarf2out_global_decl (tree);
2349 static void dwarf2out_type_decl (tree, int);
2350 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2351 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2352 dw_die_ref);
2353 static void dwarf2out_abstract_function (tree);
2354 static void dwarf2out_var_location (rtx);
2355 static void dwarf2out_begin_function (tree);
2356 static void dwarf2out_set_name (tree, tree);
2358 /* The debug hooks structure. */
2360 const struct gcc_debug_hooks dwarf2_debug_hooks =
2362 dwarf2out_init,
2363 dwarf2out_finish,
2364 dwarf2out_assembly_start,
2365 dwarf2out_define,
2366 dwarf2out_undef,
2367 dwarf2out_start_source_file,
2368 dwarf2out_end_source_file,
2369 dwarf2out_begin_block,
2370 dwarf2out_end_block,
2371 dwarf2out_ignore_block,
2372 dwarf2out_source_line,
2373 dwarf2out_begin_prologue,
2374 #if VMS_DEBUGGING_INFO
2375 dwarf2out_vms_end_prologue,
2376 dwarf2out_vms_begin_epilogue,
2377 #else
2378 debug_nothing_int_charstar,
2379 debug_nothing_int_charstar,
2380 #endif
2381 dwarf2out_end_epilogue,
2382 dwarf2out_begin_function,
2383 debug_nothing_int, /* end_function */
2384 dwarf2out_function_decl, /* function_decl */
2385 dwarf2out_global_decl,
2386 dwarf2out_type_decl, /* type_decl */
2387 dwarf2out_imported_module_or_decl,
2388 debug_nothing_tree, /* deferred_inline_function */
2389 /* The DWARF 2 backend tries to reduce debugging bloat by not
2390 emitting the abstract description of inline functions until
2391 something tries to reference them. */
2392 dwarf2out_abstract_function, /* outlining_inline_function */
2393 debug_nothing_rtx, /* label */
2394 debug_nothing_int, /* handle_pch */
2395 dwarf2out_var_location,
2396 dwarf2out_switch_text_section,
2397 dwarf2out_set_name,
2398 1, /* start_end_main_source_file */
2399 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2402 /* NOTE: In the comments in this file, many references are made to
2403 "Debugging Information Entries". This term is abbreviated as `DIE'
2404 throughout the remainder of this file. */
2406 /* An internal representation of the DWARF output is built, and then
2407 walked to generate the DWARF debugging info. The walk of the internal
2408 representation is done after the entire program has been compiled.
2409 The types below are used to describe the internal representation. */
2411 /* Whether to put type DIEs into their own section .debug_types instead
2412 of making them part of the .debug_info section. Only supported for
2413 Dwarf V4 or higher and the user didn't disable them through
2414 -fno-debug-types-section. It is more efficient to put them in a
2415 separate comdat sections since the linker will then be able to
2416 remove duplicates. But not all tools support .debug_types sections
2417 yet. */
2419 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2421 /* Various DIE's use offsets relative to the beginning of the
2422 .debug_info section to refer to each other. */
2424 typedef long int dw_offset;
2426 /* Define typedefs here to avoid circular dependencies. */
2428 typedef struct dw_attr_struct *dw_attr_ref;
2429 typedef struct dw_line_info_struct *dw_line_info_ref;
2430 typedef struct pubname_struct *pubname_ref;
2431 typedef struct dw_ranges_struct *dw_ranges_ref;
2432 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
2433 typedef struct comdat_type_struct *comdat_type_node_ref;
2435 /* The entries in the line_info table more-or-less mirror the opcodes
2436 that are used in the real dwarf line table. Arrays of these entries
2437 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2438 supported. */
2440 enum dw_line_info_opcode {
2441 /* Emit DW_LNE_set_address; the operand is the label index. */
2442 LI_set_address,
2444 /* Emit a row to the matrix with the given line. This may be done
2445 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2446 special opcodes. */
2447 LI_set_line,
2449 /* Emit a DW_LNS_set_file. */
2450 LI_set_file,
2452 /* Emit a DW_LNS_set_column. */
2453 LI_set_column,
2455 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2456 LI_negate_stmt,
2458 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2459 LI_set_prologue_end,
2460 LI_set_epilogue_begin,
2462 /* Emit a DW_LNE_set_discriminator. */
2463 LI_set_discriminator
2466 typedef struct GTY(()) dw_line_info_struct {
2467 enum dw_line_info_opcode opcode;
2468 unsigned int val;
2469 } dw_line_info_entry;
2472 typedef struct GTY(()) dw_line_info_table_struct {
2473 /* The label that marks the end of this section. */
2474 const char *end_label;
2476 /* The values for the last row of the matrix, as collected in the table.
2477 These are used to minimize the changes to the next row. */
2478 unsigned int file_num;
2479 unsigned int line_num;
2480 unsigned int column_num;
2481 int discrim_num;
2482 bool is_stmt;
2483 bool in_use;
2485 vec<dw_line_info_entry, va_gc> *entries;
2486 } dw_line_info_table;
2488 typedef dw_line_info_table *dw_line_info_table_p;
2491 /* Each DIE attribute has a field specifying the attribute kind,
2492 a link to the next attribute in the chain, and an attribute value.
2493 Attributes are typically linked below the DIE they modify. */
2495 typedef struct GTY(()) dw_attr_struct {
2496 enum dwarf_attribute dw_attr;
2497 dw_val_node dw_attr_val;
2499 dw_attr_node;
2502 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2503 The children of each node form a circular list linked by
2504 die_sib. die_child points to the node *before* the "first" child node. */
2506 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
2507 union die_symbol_or_type_node
2509 const char * GTY ((tag ("0"))) die_symbol;
2510 comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
2512 GTY ((desc ("%0.comdat_type_p"))) die_id;
2513 vec<dw_attr_node, va_gc> *die_attr;
2514 dw_die_ref die_parent;
2515 dw_die_ref die_child;
2516 dw_die_ref die_sib;
2517 dw_die_ref die_definition; /* ref from a specification to its definition */
2518 dw_offset die_offset;
2519 unsigned long die_abbrev;
2520 int die_mark;
2521 unsigned int decl_id;
2522 enum dwarf_tag die_tag;
2523 /* Die is used and must not be pruned as unused. */
2524 BOOL_BITFIELD die_perennial_p : 1;
2525 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2526 /* Lots of spare bits. */
2528 die_node;
2530 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2531 #define FOR_EACH_CHILD(die, c, expr) do { \
2532 c = die->die_child; \
2533 if (c) do { \
2534 c = c->die_sib; \
2535 expr; \
2536 } while (c != die->die_child); \
2537 } while (0)
2539 /* The pubname structure */
2541 typedef struct GTY(()) pubname_struct {
2542 dw_die_ref die;
2543 const char *name;
2545 pubname_entry;
2548 struct GTY(()) dw_ranges_struct {
2549 /* If this is positive, it's a block number, otherwise it's a
2550 bitwise-negated index into dw_ranges_by_label. */
2551 int num;
2554 /* A structure to hold a macinfo entry. */
2556 typedef struct GTY(()) macinfo_struct {
2557 unsigned char code;
2558 unsigned HOST_WIDE_INT lineno;
2559 const char *info;
2561 macinfo_entry;
2564 struct GTY(()) dw_ranges_by_label_struct {
2565 const char *begin;
2566 const char *end;
2569 /* The comdat type node structure. */
2570 typedef struct GTY(()) comdat_type_struct
2572 dw_die_ref root_die;
2573 dw_die_ref type_die;
2574 dw_die_ref skeleton_die;
2575 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2576 struct comdat_type_struct *next;
2578 comdat_type_node;
2580 /* The limbo die list structure. */
2581 typedef struct GTY(()) limbo_die_struct {
2582 dw_die_ref die;
2583 tree created_for;
2584 struct limbo_die_struct *next;
2586 limbo_die_node;
2588 typedef struct skeleton_chain_struct
2590 dw_die_ref old_die;
2591 dw_die_ref new_die;
2592 struct skeleton_chain_struct *parent;
2594 skeleton_chain_node;
2596 /* Define a macro which returns nonzero for a TYPE_DECL which was
2597 implicitly generated for a type.
2599 Note that, unlike the C front-end (which generates a NULL named
2600 TYPE_DECL node for each complete tagged type, each array type,
2601 and each function type node created) the C++ front-end generates
2602 a _named_ TYPE_DECL node for each tagged type node created.
2603 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2604 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2605 front-end, but for each type, tagged or not. */
2607 #define TYPE_DECL_IS_STUB(decl) \
2608 (DECL_NAME (decl) == NULL_TREE \
2609 || (DECL_ARTIFICIAL (decl) \
2610 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
2611 /* This is necessary for stub decls that \
2612 appear in nested inline functions. */ \
2613 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2614 && (decl_ultimate_origin (decl) \
2615 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2617 /* Information concerning the compilation unit's programming
2618 language, and compiler version. */
2620 /* Fixed size portion of the DWARF compilation unit header. */
2621 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2622 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2624 /* Fixed size portion of the DWARF comdat type unit header. */
2625 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2626 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2627 + DWARF_OFFSET_SIZE)
2629 /* Fixed size portion of public names info. */
2630 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2632 /* Fixed size portion of the address range info. */
2633 #define DWARF_ARANGES_HEADER_SIZE \
2634 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2635 DWARF2_ADDR_SIZE * 2) \
2636 - DWARF_INITIAL_LENGTH_SIZE)
2638 /* Size of padding portion in the address range info. It must be
2639 aligned to twice the pointer size. */
2640 #define DWARF_ARANGES_PAD_SIZE \
2641 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2642 DWARF2_ADDR_SIZE * 2) \
2643 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2645 /* Use assembler line directives if available. */
2646 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2647 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2648 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2649 #else
2650 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2651 #endif
2652 #endif
2654 /* Minimum line offset in a special line info. opcode.
2655 This value was chosen to give a reasonable range of values. */
2656 #define DWARF_LINE_BASE -10
2658 /* First special line opcode - leave room for the standard opcodes. */
2659 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
2661 /* Range of line offsets in a special line info. opcode. */
2662 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
2664 /* Flag that indicates the initial value of the is_stmt_start flag.
2665 In the present implementation, we do not mark any lines as
2666 the beginning of a source statement, because that information
2667 is not made available by the GCC front-end. */
2668 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2670 /* Maximum number of operations per instruction bundle. */
2671 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2672 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2673 #endif
2675 /* This location is used by calc_die_sizes() to keep track
2676 the offset of each DIE within the .debug_info section. */
2677 static unsigned long next_die_offset;
2679 /* Record the root of the DIE's built for the current compilation unit. */
2680 static GTY(()) dw_die_ref single_comp_unit_die;
2682 /* A list of type DIEs that have been separated into comdat sections. */
2683 static GTY(()) comdat_type_node *comdat_type_list;
2685 /* A list of DIEs with a NULL parent waiting to be relocated. */
2686 static GTY(()) limbo_die_node *limbo_die_list;
2688 /* A list of DIEs for which we may have to generate
2689 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
2690 static GTY(()) limbo_die_node *deferred_asm_name;
2692 /* Filenames referenced by this compilation unit. */
2693 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
2695 /* A hash table of references to DIE's that describe declarations.
2696 The key is a DECL_UID() which is a unique number identifying each decl. */
2697 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
2699 /* A hash table of references to DIE's that describe COMMON blocks.
2700 The key is DECL_UID() ^ die_parent. */
2701 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
2703 typedef struct GTY(()) die_arg_entry_struct {
2704 dw_die_ref die;
2705 tree arg;
2706 } die_arg_entry;
2709 /* Node of the variable location list. */
2710 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2711 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2712 EXPR_LIST chain. For small bitsizes, bitsize is encoded
2713 in mode of the EXPR_LIST node and first EXPR_LIST operand
2714 is either NOTE_INSN_VAR_LOCATION for a piece with a known
2715 location or NULL for padding. For larger bitsizes,
2716 mode is 0 and first operand is a CONCAT with bitsize
2717 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2718 NULL as second operand. */
2719 rtx GTY (()) loc;
2720 const char * GTY (()) label;
2721 struct var_loc_node * GTY (()) next;
2724 /* Variable location list. */
2725 struct GTY (()) var_loc_list_def {
2726 struct var_loc_node * GTY (()) first;
2728 /* Pointer to the last but one or last element of the
2729 chained list. If the list is empty, both first and
2730 last are NULL, if the list contains just one node
2731 or the last node certainly is not redundant, it points
2732 to the last node, otherwise points to the last but one.
2733 Do not mark it for GC because it is marked through the chain. */
2734 struct var_loc_node * GTY ((skip ("%h"))) last;
2736 /* Pointer to the last element before section switch,
2737 if NULL, either sections weren't switched or first
2738 is after section switch. */
2739 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2741 /* DECL_UID of the variable decl. */
2742 unsigned int decl_id;
2744 typedef struct var_loc_list_def var_loc_list;
2746 /* Call argument location list. */
2747 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2748 rtx GTY (()) call_arg_loc_note;
2749 const char * GTY (()) label;
2750 tree GTY (()) block;
2751 bool tail_call_p;
2752 rtx GTY (()) symbol_ref;
2753 struct call_arg_loc_node * GTY (()) next;
2757 /* Table of decl location linked lists. */
2758 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
2760 /* Head and tail of call_arg_loc chain. */
2761 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2762 static struct call_arg_loc_node *call_arg_loc_last;
2764 /* Number of call sites in the current function. */
2765 static int call_site_count = -1;
2766 /* Number of tail call sites in the current function. */
2767 static int tail_call_site_count = -1;
2769 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
2770 DIEs. */
2771 static vec<dw_die_ref> block_map;
2773 /* A cached location list. */
2774 struct GTY (()) cached_dw_loc_list_def {
2775 /* The DECL_UID of the decl that this entry describes. */
2776 unsigned int decl_id;
2778 /* The cached location list. */
2779 dw_loc_list_ref loc_list;
2781 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2783 /* Table of cached location lists. */
2784 static GTY ((param_is (cached_dw_loc_list))) htab_t cached_dw_loc_list_table;
2786 /* A pointer to the base of a list of references to DIE's that
2787 are uniquely identified by their tag, presence/absence of
2788 children DIE's, and list of attribute/value pairs. */
2789 static GTY((length ("abbrev_die_table_allocated")))
2790 dw_die_ref *abbrev_die_table;
2792 /* Number of elements currently allocated for abbrev_die_table. */
2793 static GTY(()) unsigned abbrev_die_table_allocated;
2795 /* Number of elements in type_die_table currently in use. */
2796 static GTY(()) unsigned abbrev_die_table_in_use;
2798 /* Size (in elements) of increments by which we may expand the
2799 abbrev_die_table. */
2800 #define ABBREV_DIE_TABLE_INCREMENT 256
2802 /* A global counter for generating labels for line number data. */
2803 static unsigned int line_info_label_num;
2805 /* The current table to which we should emit line number information
2806 for the current function. This will be set up at the beginning of
2807 assembly for the function. */
2808 static dw_line_info_table *cur_line_info_table;
2810 /* The two default tables of line number info. */
2811 static GTY(()) dw_line_info_table *text_section_line_info;
2812 static GTY(()) dw_line_info_table *cold_text_section_line_info;
2814 /* The set of all non-default tables of line number info. */
2815 static GTY(()) vec<dw_line_info_table_p, va_gc> *separate_line_info;
2817 /* A flag to tell pubnames/types export if there is an info section to
2818 refer to. */
2819 static bool info_section_emitted;
2821 /* A pointer to the base of a table that contains a list of publicly
2822 accessible names. */
2823 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
2825 /* A pointer to the base of a table that contains a list of publicly
2826 accessible types. */
2827 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
2829 /* A pointer to the base of a table that contains a list of macro
2830 defines/undefines (and file start/end markers). */
2831 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
2833 /* True if .debug_macinfo or .debug_macros section is going to be
2834 emitted. */
2835 #define have_macinfo \
2836 (debug_info_level >= DINFO_LEVEL_VERBOSE \
2837 && !macinfo_table->is_empty ())
2839 /* Array of dies for which we should generate .debug_ranges info. */
2840 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
2842 /* Number of elements currently allocated for ranges_table. */
2843 static GTY(()) unsigned ranges_table_allocated;
2845 /* Number of elements in ranges_table currently in use. */
2846 static GTY(()) unsigned ranges_table_in_use;
2848 /* Array of pairs of labels referenced in ranges_table. */
2849 static GTY ((length ("ranges_by_label_allocated")))
2850 dw_ranges_by_label_ref ranges_by_label;
2852 /* Number of elements currently allocated for ranges_by_label. */
2853 static GTY(()) unsigned ranges_by_label_allocated;
2855 /* Number of elements in ranges_by_label currently in use. */
2856 static GTY(()) unsigned ranges_by_label_in_use;
2858 /* Size (in elements) of increments by which we may expand the
2859 ranges_table. */
2860 #define RANGES_TABLE_INCREMENT 64
2862 /* Whether we have location lists that need outputting */
2863 static GTY(()) bool have_location_lists;
2865 /* Unique label counter. */
2866 static GTY(()) unsigned int loclabel_num;
2868 /* Unique label counter for point-of-call tables. */
2869 static GTY(()) unsigned int poc_label_num;
2871 /* Record whether the function being analyzed contains inlined functions. */
2872 static int current_function_has_inlines;
2874 /* The last file entry emitted by maybe_emit_file(). */
2875 static GTY(()) struct dwarf_file_data * last_emitted_file;
2877 /* Number of internal labels generated by gen_internal_sym(). */
2878 static GTY(()) int label_num;
2880 /* Cached result of previous call to lookup_filename. */
2881 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
2883 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
2885 /* Instances of generic types for which we need to generate debug
2886 info that describe their generic parameters and arguments. That
2887 generation needs to happen once all types are properly laid out so
2888 we do it at the end of compilation. */
2889 static GTY(()) vec<tree, va_gc> *generic_type_instances;
2891 /* Offset from the "steady-state frame pointer" to the frame base,
2892 within the current function. */
2893 static HOST_WIDE_INT frame_pointer_fb_offset;
2894 static bool frame_pointer_fb_offset_valid;
2896 static vec<dw_die_ref> base_types;
2898 /* Forward declarations for functions defined in this file. */
2900 static int is_pseudo_reg (const_rtx);
2901 static tree type_main_variant (tree);
2902 static int is_tagged_type (const_tree);
2903 static const char *dwarf_tag_name (unsigned);
2904 static const char *dwarf_attr_name (unsigned);
2905 static const char *dwarf_form_name (unsigned);
2906 static tree decl_ultimate_origin (const_tree);
2907 static tree decl_class_context (tree);
2908 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
2909 static inline enum dw_val_class AT_class (dw_attr_ref);
2910 static inline unsigned int AT_index (dw_attr_ref);
2911 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
2912 static inline unsigned AT_flag (dw_attr_ref);
2913 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
2914 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
2915 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
2916 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
2917 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
2918 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2919 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
2920 unsigned int, unsigned char *);
2921 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
2922 static hashval_t debug_str_do_hash (const void *);
2923 static int debug_str_eq (const void *, const void *);
2924 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
2925 static inline const char *AT_string (dw_attr_ref);
2926 static enum dwarf_form AT_string_form (dw_attr_ref);
2927 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
2928 static void add_AT_specification (dw_die_ref, dw_die_ref);
2929 static inline dw_die_ref AT_ref (dw_attr_ref);
2930 static inline int AT_ref_external (dw_attr_ref);
2931 static inline void set_AT_ref_external (dw_attr_ref, int);
2932 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
2933 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
2934 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
2935 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
2936 dw_loc_list_ref);
2937 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
2938 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
2939 static void remove_addr_table_entry (addr_table_entry *);
2940 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
2941 static inline rtx AT_addr (dw_attr_ref);
2942 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
2943 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
2944 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
2945 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
2946 unsigned HOST_WIDE_INT);
2947 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
2948 unsigned long, bool);
2949 static inline const char *AT_lbl (dw_attr_ref);
2950 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
2951 static const char *get_AT_low_pc (dw_die_ref);
2952 static const char *get_AT_hi_pc (dw_die_ref);
2953 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
2954 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
2955 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
2956 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
2957 static bool is_cxx (void);
2958 static bool is_fortran (void);
2959 static bool is_ada (void);
2960 static void remove_AT (dw_die_ref, enum dwarf_attribute);
2961 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
2962 static void add_child_die (dw_die_ref, dw_die_ref);
2963 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
2964 static dw_die_ref lookup_type_die (tree);
2965 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
2966 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
2967 static void equate_type_number_to_die (tree, dw_die_ref);
2968 static hashval_t decl_die_table_hash (const void *);
2969 static int decl_die_table_eq (const void *, const void *);
2970 static dw_die_ref lookup_decl_die (tree);
2971 static hashval_t common_block_die_table_hash (const void *);
2972 static int common_block_die_table_eq (const void *, const void *);
2973 static hashval_t decl_loc_table_hash (const void *);
2974 static int decl_loc_table_eq (const void *, const void *);
2975 static var_loc_list *lookup_decl_loc (const_tree);
2976 static void equate_decl_number_to_die (tree, dw_die_ref);
2977 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
2978 static void print_spaces (FILE *);
2979 static void print_die (dw_die_ref, FILE *);
2980 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
2981 static dw_die_ref pop_compile_unit (dw_die_ref);
2982 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
2983 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
2984 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
2985 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
2986 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
2987 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
2988 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
2989 struct md5_ctx *, int *);
2990 struct checksum_attributes;
2991 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
2992 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
2993 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
2994 static void generate_type_signature (dw_die_ref, comdat_type_node *);
2995 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
2996 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
2997 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
2998 static int same_die_p (dw_die_ref, dw_die_ref, int *);
2999 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3000 static void compute_section_prefix (dw_die_ref);
3001 static int is_type_die (dw_die_ref);
3002 static int is_comdat_die (dw_die_ref);
3003 static int is_symbol_die (dw_die_ref);
3004 static void assign_symbol_names (dw_die_ref);
3005 static void break_out_includes (dw_die_ref);
3006 static int is_declaration_die (dw_die_ref);
3007 static int should_move_die_to_comdat (dw_die_ref);
3008 static dw_die_ref clone_as_declaration (dw_die_ref);
3009 static dw_die_ref clone_die (dw_die_ref);
3010 static dw_die_ref clone_tree (dw_die_ref);
3011 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3012 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3013 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3014 static dw_die_ref generate_skeleton (dw_die_ref);
3015 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3016 dw_die_ref,
3017 dw_die_ref);
3018 static void break_out_comdat_types (dw_die_ref);
3019 static dw_die_ref copy_ancestor_tree (dw_die_ref, dw_die_ref, htab_t);
3020 static void copy_decls_walk (dw_die_ref, dw_die_ref, htab_t);
3021 static void copy_decls_for_unworthy_types (dw_die_ref);
3023 static hashval_t htab_cu_hash (const void *);
3024 static int htab_cu_eq (const void *, const void *);
3025 static void htab_cu_del (void *);
3026 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
3027 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
3028 static void add_sibling_attributes (dw_die_ref);
3029 static void build_abbrev_table (dw_die_ref, htab_t);
3030 static void output_location_lists (dw_die_ref);
3031 static int constant_size (unsigned HOST_WIDE_INT);
3032 static unsigned long size_of_die (dw_die_ref);
3033 static void calc_die_sizes (dw_die_ref);
3034 static void calc_base_type_die_sizes (void);
3035 static void mark_dies (dw_die_ref);
3036 static void unmark_dies (dw_die_ref);
3037 static void unmark_all_dies (dw_die_ref);
3038 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3039 static unsigned long size_of_aranges (void);
3040 static enum dwarf_form value_format (dw_attr_ref);
3041 static void output_value_format (dw_attr_ref);
3042 static void output_abbrev_section (void);
3043 static void output_die_abbrevs (unsigned long, dw_die_ref);
3044 static void output_die_symbol (dw_die_ref);
3045 static void output_die (dw_die_ref);
3046 static void output_compilation_unit_header (void);
3047 static void output_comp_unit (dw_die_ref, int);
3048 static void output_comdat_type_unit (comdat_type_node *);
3049 static const char *dwarf2_name (tree, int);
3050 static void add_pubname (tree, dw_die_ref);
3051 static void add_enumerator_pubname (const char *, dw_die_ref);
3052 static void add_pubname_string (const char *, dw_die_ref);
3053 static void add_pubtype (tree, dw_die_ref);
3054 static void output_pubnames (vec<pubname_entry, va_gc> *);
3055 static void output_aranges (unsigned long);
3056 static unsigned int add_ranges_num (int);
3057 static unsigned int add_ranges (const_tree);
3058 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3059 bool *, bool);
3060 static void output_ranges (void);
3061 static dw_line_info_table *new_line_info_table (void);
3062 static void output_line_info (bool);
3063 static void output_file_names (void);
3064 static dw_die_ref base_type_die (tree);
3065 static int is_base_type (tree);
3066 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3067 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
3068 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3069 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3070 static int type_is_enum (const_tree);
3071 static unsigned int dbx_reg_number (const_rtx);
3072 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3073 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3074 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3075 enum var_init_status);
3076 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3077 enum var_init_status);
3078 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3079 enum var_init_status);
3080 static int is_based_loc (const_rtx);
3081 static int resolve_one_addr (rtx *, void *);
3082 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3083 enum var_init_status);
3084 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
3085 enum var_init_status);
3086 static dw_loc_list_ref loc_list_from_tree (tree, int);
3087 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
3088 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3089 static tree field_type (const_tree);
3090 static unsigned int simple_type_align_in_bits (const_tree);
3091 static unsigned int simple_decl_align_in_bits (const_tree);
3092 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3093 static HOST_WIDE_INT field_byte_offset (const_tree);
3094 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3095 dw_loc_list_ref);
3096 static void add_data_member_location_attribute (dw_die_ref, tree);
3097 static bool add_const_value_attribute (dw_die_ref, rtx);
3098 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3099 static void insert_double (double_int, unsigned char *);
3100 static void insert_float (const_rtx, unsigned char *);
3101 static rtx rtl_for_decl_location (tree);
3102 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3103 enum dwarf_attribute);
3104 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3105 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3106 static void add_name_attribute (dw_die_ref, const char *);
3107 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3108 static void add_comp_dir_attribute (dw_die_ref);
3109 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
3110 static void add_subscript_info (dw_die_ref, tree, bool);
3111 static void add_byte_size_attribute (dw_die_ref, tree);
3112 static void add_bit_offset_attribute (dw_die_ref, tree);
3113 static void add_bit_size_attribute (dw_die_ref, tree);
3114 static void add_prototyped_attribute (dw_die_ref, tree);
3115 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3116 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3117 static void add_src_coords_attributes (dw_die_ref, tree);
3118 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3119 static void push_decl_scope (tree);
3120 static void pop_decl_scope (void);
3121 static dw_die_ref scope_die_for (tree, dw_die_ref);
3122 static inline int local_scope_p (dw_die_ref);
3123 static inline int class_scope_p (dw_die_ref);
3124 static inline int class_or_namespace_scope_p (dw_die_ref);
3125 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
3126 static void add_calling_convention_attribute (dw_die_ref, tree);
3127 static const char *type_tag (const_tree);
3128 static tree member_declared_type (const_tree);
3129 #if 0
3130 static const char *decl_start_label (tree);
3131 #endif
3132 static void gen_array_type_die (tree, dw_die_ref);
3133 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3134 #if 0
3135 static void gen_entry_point_die (tree, dw_die_ref);
3136 #endif
3137 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3138 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3139 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3140 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3141 static void gen_formal_types_die (tree, dw_die_ref);
3142 static void gen_subprogram_die (tree, dw_die_ref);
3143 static void gen_variable_die (tree, tree, dw_die_ref);
3144 static void gen_const_die (tree, dw_die_ref);
3145 static void gen_label_die (tree, dw_die_ref);
3146 static void gen_lexical_block_die (tree, dw_die_ref, int);
3147 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
3148 static void gen_field_die (tree, dw_die_ref);
3149 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3150 static dw_die_ref gen_compile_unit_die (const char *);
3151 static void gen_inheritance_die (tree, tree, dw_die_ref);
3152 static void gen_member_die (tree, dw_die_ref);
3153 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3154 enum debug_info_usage);
3155 static void gen_subroutine_type_die (tree, dw_die_ref);
3156 static void gen_typedef_die (tree, dw_die_ref);
3157 static void gen_type_die (tree, dw_die_ref);
3158 static void gen_block_die (tree, dw_die_ref, int);
3159 static void decls_for_scope (tree, dw_die_ref, int);
3160 static inline int is_redundant_typedef (const_tree);
3161 static bool is_naming_typedef_decl (const_tree);
3162 static inline dw_die_ref get_context_die (tree);
3163 static void gen_namespace_die (tree, dw_die_ref);
3164 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3165 static dw_die_ref force_decl_die (tree);
3166 static dw_die_ref force_type_die (tree);
3167 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3168 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3169 static struct dwarf_file_data * lookup_filename (const char *);
3170 static void retry_incomplete_types (void);
3171 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3172 static void gen_generic_params_dies (tree);
3173 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3174 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3175 static void splice_child_die (dw_die_ref, dw_die_ref);
3176 static int file_info_cmp (const void *, const void *);
3177 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3178 const char *, const char *);
3179 static void output_loc_list (dw_loc_list_ref);
3180 static char *gen_internal_sym (const char *);
3181 static bool want_pubnames (void);
3183 static void prune_unmark_dies (dw_die_ref);
3184 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3185 static void prune_unused_types_mark (dw_die_ref, int);
3186 static void prune_unused_types_walk (dw_die_ref);
3187 static void prune_unused_types_walk_attribs (dw_die_ref);
3188 static void prune_unused_types_prune (dw_die_ref);
3189 static void prune_unused_types (void);
3190 static int maybe_emit_file (struct dwarf_file_data *fd);
3191 static inline const char *AT_vms_delta1 (dw_attr_ref);
3192 static inline const char *AT_vms_delta2 (dw_attr_ref);
3193 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3194 const char *, const char *);
3195 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3196 static void gen_remaining_tmpl_value_param_die_attribute (void);
3197 static bool generic_type_p (tree);
3198 static void schedule_generic_params_dies_gen (tree t);
3199 static void gen_scheduled_generic_parms_dies (void);
3201 /* enum for tracking thread-local variables whose address is really an offset
3202 relative to the TLS pointer, which will need link-time relocation, but will
3203 not need relocation by the DWARF consumer. */
3205 enum dtprel_bool
3207 dtprel_false = 0,
3208 dtprel_true = 1
3211 /* Return the operator to use for an address of a variable. For dtprel_true, we
3212 use DW_OP_const*. For regular variables, which need both link-time
3213 relocation and consumer-level relocation (e.g., to account for shared objects
3214 loaded at a random address), we use DW_OP_addr*. */
3216 static inline enum dwarf_location_atom
3217 dw_addr_op (enum dtprel_bool dtprel)
3219 if (dtprel == dtprel_true)
3220 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3221 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3222 else
3223 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3226 /* Return a pointer to a newly allocated address location description. If
3227 dwarf_split_debug_info is true, then record the address with the appropriate
3228 relocation. */
3229 static inline dw_loc_descr_ref
3230 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3232 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3234 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3235 ref->dw_loc_oprnd1.v.val_addr = addr;
3236 ref->dtprel = dtprel;
3237 if (dwarf_split_debug_info)
3238 ref->dw_loc_oprnd1.val_entry
3239 = add_addr_table_entry (addr,
3240 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3241 else
3242 ref->dw_loc_oprnd1.val_entry = NULL;
3244 return ref;
3247 /* Section names used to hold DWARF debugging information. */
3249 #ifndef DEBUG_INFO_SECTION
3250 #define DEBUG_INFO_SECTION ".debug_info"
3251 #endif
3252 #ifndef DEBUG_DWO_INFO_SECTION
3253 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3254 #endif
3255 #ifndef DEBUG_ABBREV_SECTION
3256 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3257 #endif
3258 #ifndef DEBUG_DWO_ABBREV_SECTION
3259 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3260 #endif
3261 #ifndef DEBUG_ARANGES_SECTION
3262 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3263 #endif
3264 #ifndef DEBUG_ADDR_SECTION
3265 #define DEBUG_ADDR_SECTION ".debug_addr"
3266 #endif
3267 #ifndef DEBUG_NORM_MACINFO_SECTION
3268 #define DEBUG_NORM_MACINFO_SECTION ".debug_macinfo"
3269 #endif
3270 #ifndef DEBUG_DWO_MACINFO_SECTION
3271 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3272 #endif
3273 #ifndef DEBUG_MACINFO_SECTION
3274 #define DEBUG_MACINFO_SECTION \
3275 (!dwarf_split_debug_info \
3276 ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3277 #endif
3278 #ifndef DEBUG_NORM_MACRO_SECTION
3279 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3280 #endif
3281 #ifndef DEBUG_DWO_MACRO_SECTION
3282 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3283 #endif
3284 #ifndef DEBUG_MACRO_SECTION
3285 #define DEBUG_MACRO_SECTION \
3286 (!dwarf_split_debug_info \
3287 ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3288 #endif
3289 #ifndef DEBUG_LINE_SECTION
3290 #define DEBUG_LINE_SECTION ".debug_line"
3291 #endif
3292 #ifndef DEBUG_DWO_LINE_SECTION
3293 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3294 #endif
3295 #ifndef DEBUG_LOC_SECTION
3296 #define DEBUG_LOC_SECTION ".debug_loc"
3297 #endif
3298 #ifndef DEBUG_DWO_LOC_SECTION
3299 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3300 #endif
3301 #ifndef DEBUG_PUBNAMES_SECTION
3302 #define DEBUG_PUBNAMES_SECTION ".debug_pubnames"
3303 #endif
3304 #ifndef DEBUG_PUBTYPES_SECTION
3305 #define DEBUG_PUBTYPES_SECTION ".debug_pubtypes"
3306 #endif
3307 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3308 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3309 #ifndef DEBUG_STR_OFFSETS_SECTION
3310 #define DEBUG_STR_OFFSETS_SECTION \
3311 (!dwarf_split_debug_info \
3312 ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3313 #endif
3314 #define DEBUG_DWO_STR_SECTION ".debug_str.dwo"
3315 #define DEBUG_NORM_STR_SECTION ".debug_str"
3316 #ifndef DEBUG_STR_SECTION
3317 #define DEBUG_STR_SECTION \
3318 (!dwarf_split_debug_info ? (DEBUG_NORM_STR_SECTION) : (DEBUG_DWO_STR_SECTION))
3319 #endif
3320 #ifndef DEBUG_RANGES_SECTION
3321 #define DEBUG_RANGES_SECTION ".debug_ranges"
3322 #endif
3324 /* Standard ELF section names for compiled code and data. */
3325 #ifndef TEXT_SECTION_NAME
3326 #define TEXT_SECTION_NAME ".text"
3327 #endif
3329 /* Section flags for .debug_macinfo/.debug_macro section. */
3330 #define DEBUG_MACRO_SECTION_FLAGS \
3331 (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3333 /* Section flags for .debug_str section. */
3334 #define DEBUG_STR_SECTION_FLAGS \
3335 (dwarf_split_debug_info \
3336 ? SECTION_DEBUG | SECTION_EXCLUDE \
3337 : (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3338 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3339 : SECTION_DEBUG))
3341 /* Labels we insert at beginning sections we can reference instead of
3342 the section names themselves. */
3344 #ifndef TEXT_SECTION_LABEL
3345 #define TEXT_SECTION_LABEL "Ltext"
3346 #endif
3347 #ifndef COLD_TEXT_SECTION_LABEL
3348 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3349 #endif
3350 #ifndef DEBUG_LINE_SECTION_LABEL
3351 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3352 #endif
3353 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3354 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3355 #endif
3356 #ifndef DEBUG_INFO_SECTION_LABEL
3357 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3358 #endif
3359 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3360 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3361 #endif
3362 #ifndef DEBUG_ABBREV_SECTION_LABEL
3363 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3364 #endif
3365 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3366 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3367 #endif
3368 #ifndef DEBUG_ADDR_SECTION_LABEL
3369 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3370 #endif
3371 #ifndef DEBUG_LOC_SECTION_LABEL
3372 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3373 #endif
3374 #ifndef DEBUG_RANGES_SECTION_LABEL
3375 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3376 #endif
3377 #ifndef DEBUG_MACINFO_SECTION_LABEL
3378 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3379 #endif
3380 #ifndef DEBUG_MACRO_SECTION_LABEL
3381 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3382 #endif
3383 #define SKELETON_COMP_DIE_ABBREV 1
3384 #define SKELETON_TYPE_DIE_ABBREV 2
3386 /* Definitions of defaults for formats and names of various special
3387 (artificial) labels which may be generated within this file (when the -g
3388 options is used and DWARF2_DEBUGGING_INFO is in effect.
3389 If necessary, these may be overridden from within the tm.h file, but
3390 typically, overriding these defaults is unnecessary. */
3392 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3393 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3394 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3395 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3396 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3397 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3398 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3399 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3400 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3401 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3402 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3403 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3404 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3405 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3407 #ifndef TEXT_END_LABEL
3408 #define TEXT_END_LABEL "Letext"
3409 #endif
3410 #ifndef COLD_END_LABEL
3411 #define COLD_END_LABEL "Letext_cold"
3412 #endif
3413 #ifndef BLOCK_BEGIN_LABEL
3414 #define BLOCK_BEGIN_LABEL "LBB"
3415 #endif
3416 #ifndef BLOCK_END_LABEL
3417 #define BLOCK_END_LABEL "LBE"
3418 #endif
3419 #ifndef LINE_CODE_LABEL
3420 #define LINE_CODE_LABEL "LM"
3421 #endif
3424 /* Return the root of the DIE's built for the current compilation unit. */
3425 static dw_die_ref
3426 comp_unit_die (void)
3428 if (!single_comp_unit_die)
3429 single_comp_unit_die = gen_compile_unit_die (NULL);
3430 return single_comp_unit_die;
3433 /* We allow a language front-end to designate a function that is to be
3434 called to "demangle" any name before it is put into a DIE. */
3436 static const char *(*demangle_name_func) (const char *);
3438 void
3439 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3441 demangle_name_func = func;
3444 /* Test if rtl node points to a pseudo register. */
3446 static inline int
3447 is_pseudo_reg (const_rtx rtl)
3449 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3450 || (GET_CODE (rtl) == SUBREG
3451 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3454 /* Return a reference to a type, with its const and volatile qualifiers
3455 removed. */
3457 static inline tree
3458 type_main_variant (tree type)
3460 type = TYPE_MAIN_VARIANT (type);
3462 /* ??? There really should be only one main variant among any group of
3463 variants of a given type (and all of the MAIN_VARIANT values for all
3464 members of the group should point to that one type) but sometimes the C
3465 front-end messes this up for array types, so we work around that bug
3466 here. */
3467 if (TREE_CODE (type) == ARRAY_TYPE)
3468 while (type != TYPE_MAIN_VARIANT (type))
3469 type = TYPE_MAIN_VARIANT (type);
3471 return type;
3474 /* Return nonzero if the given type node represents a tagged type. */
3476 static inline int
3477 is_tagged_type (const_tree type)
3479 enum tree_code code = TREE_CODE (type);
3481 return (code == RECORD_TYPE || code == UNION_TYPE
3482 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3485 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3487 static void
3488 get_ref_die_offset_label (char *label, dw_die_ref ref)
3490 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3493 /* Return die_offset of a DIE reference to a base type. */
3495 static unsigned long int
3496 get_base_type_offset (dw_die_ref ref)
3498 if (ref->die_offset)
3499 return ref->die_offset;
3500 if (comp_unit_die ()->die_abbrev)
3502 calc_base_type_die_sizes ();
3503 gcc_assert (ref->die_offset);
3505 return ref->die_offset;
3508 /* Return die_offset of a DIE reference other than base type. */
3510 static unsigned long int
3511 get_ref_die_offset (dw_die_ref ref)
3513 gcc_assert (ref->die_offset);
3514 return ref->die_offset;
3517 /* Convert a DIE tag into its string name. */
3519 static const char *
3520 dwarf_tag_name (unsigned int tag)
3522 const char *name = get_DW_TAG_name (tag);
3524 if (name != NULL)
3525 return name;
3527 return "DW_TAG_<unknown>";
3530 /* Convert a DWARF attribute code into its string name. */
3532 static const char *
3533 dwarf_attr_name (unsigned int attr)
3535 const char *name;
3537 switch (attr)
3539 #if VMS_DEBUGGING_INFO
3540 case DW_AT_HP_prologue:
3541 return "DW_AT_HP_prologue";
3542 #else
3543 case DW_AT_MIPS_loop_unroll_factor:
3544 return "DW_AT_MIPS_loop_unroll_factor";
3545 #endif
3547 #if VMS_DEBUGGING_INFO
3548 case DW_AT_HP_epilogue:
3549 return "DW_AT_HP_epilogue";
3550 #else
3551 case DW_AT_MIPS_stride:
3552 return "DW_AT_MIPS_stride";
3553 #endif
3556 name = get_DW_AT_name (attr);
3558 if (name != NULL)
3559 return name;
3561 return "DW_AT_<unknown>";
3564 /* Convert a DWARF value form code into its string name. */
3566 static const char *
3567 dwarf_form_name (unsigned int form)
3569 const char *name = get_DW_FORM_name (form);
3571 if (name != NULL)
3572 return name;
3574 return "DW_FORM_<unknown>";
3577 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3578 instance of an inlined instance of a decl which is local to an inline
3579 function, so we have to trace all of the way back through the origin chain
3580 to find out what sort of node actually served as the original seed for the
3581 given block. */
3583 static tree
3584 decl_ultimate_origin (const_tree decl)
3586 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3587 return NULL_TREE;
3589 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
3590 nodes in the function to point to themselves; ignore that if
3591 we're trying to output the abstract instance of this function. */
3592 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3593 return NULL_TREE;
3595 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3596 most distant ancestor, this should never happen. */
3597 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3599 return DECL_ABSTRACT_ORIGIN (decl);
3602 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
3603 of a virtual function may refer to a base class, so we check the 'this'
3604 parameter. */
3606 static tree
3607 decl_class_context (tree decl)
3609 tree context = NULL_TREE;
3611 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3612 context = DECL_CONTEXT (decl);
3613 else
3614 context = TYPE_MAIN_VARIANT
3615 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3617 if (context && !TYPE_P (context))
3618 context = NULL_TREE;
3620 return context;
3623 /* Add an attribute/value pair to a DIE. */
3625 static inline void
3626 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
3628 /* Maybe this should be an assert? */
3629 if (die == NULL)
3630 return;
3632 vec_safe_reserve (die->die_attr, 1);
3633 vec_safe_push (die->die_attr, *attr);
3636 static inline enum dw_val_class
3637 AT_class (dw_attr_ref a)
3639 return a->dw_attr_val.val_class;
3642 /* Return the index for any attribute that will be referenced with a
3643 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
3644 are stored in dw_attr_val.v.val_str for reference counting
3645 pruning. */
3647 static inline unsigned int
3648 AT_index (dw_attr_ref a)
3650 if (AT_class (a) == dw_val_class_str)
3651 return a->dw_attr_val.v.val_str->index;
3652 else if (a->dw_attr_val.val_entry != NULL)
3653 return a->dw_attr_val.val_entry->index;
3654 return NOT_INDEXED;
3657 /* Add a flag value attribute to a DIE. */
3659 static inline void
3660 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3662 dw_attr_node attr;
3664 attr.dw_attr = attr_kind;
3665 attr.dw_attr_val.val_class = dw_val_class_flag;
3666 attr.dw_attr_val.val_entry = NULL;
3667 attr.dw_attr_val.v.val_flag = flag;
3668 add_dwarf_attr (die, &attr);
3671 static inline unsigned
3672 AT_flag (dw_attr_ref a)
3674 gcc_assert (a && AT_class (a) == dw_val_class_flag);
3675 return a->dw_attr_val.v.val_flag;
3678 /* Add a signed integer attribute value to a DIE. */
3680 static inline void
3681 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3683 dw_attr_node attr;
3685 attr.dw_attr = attr_kind;
3686 attr.dw_attr_val.val_class = dw_val_class_const;
3687 attr.dw_attr_val.val_entry = NULL;
3688 attr.dw_attr_val.v.val_int = int_val;
3689 add_dwarf_attr (die, &attr);
3692 static inline HOST_WIDE_INT
3693 AT_int (dw_attr_ref a)
3695 gcc_assert (a && AT_class (a) == dw_val_class_const);
3696 return a->dw_attr_val.v.val_int;
3699 /* Add an unsigned integer attribute value to a DIE. */
3701 static inline void
3702 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3703 unsigned HOST_WIDE_INT unsigned_val)
3705 dw_attr_node attr;
3707 attr.dw_attr = attr_kind;
3708 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3709 attr.dw_attr_val.val_entry = NULL;
3710 attr.dw_attr_val.v.val_unsigned = unsigned_val;
3711 add_dwarf_attr (die, &attr);
3714 static inline unsigned HOST_WIDE_INT
3715 AT_unsigned (dw_attr_ref a)
3717 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3718 return a->dw_attr_val.v.val_unsigned;
3721 /* Add an unsigned double integer attribute value to a DIE. */
3723 static inline void
3724 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3725 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3727 dw_attr_node attr;
3729 attr.dw_attr = attr_kind;
3730 attr.dw_attr_val.val_class = dw_val_class_const_double;
3731 attr.dw_attr_val.val_entry = NULL;
3732 attr.dw_attr_val.v.val_double.high = high;
3733 attr.dw_attr_val.v.val_double.low = low;
3734 add_dwarf_attr (die, &attr);
3737 /* Add a floating point attribute value to a DIE and return it. */
3739 static inline void
3740 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3741 unsigned int length, unsigned int elt_size, unsigned char *array)
3743 dw_attr_node attr;
3745 attr.dw_attr = attr_kind;
3746 attr.dw_attr_val.val_class = dw_val_class_vec;
3747 attr.dw_attr_val.val_entry = NULL;
3748 attr.dw_attr_val.v.val_vec.length = length;
3749 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3750 attr.dw_attr_val.v.val_vec.array = array;
3751 add_dwarf_attr (die, &attr);
3754 /* Add an 8-byte data attribute value to a DIE. */
3756 static inline void
3757 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3758 unsigned char data8[8])
3760 dw_attr_node attr;
3762 attr.dw_attr = attr_kind;
3763 attr.dw_attr_val.val_class = dw_val_class_data8;
3764 attr.dw_attr_val.val_entry = NULL;
3765 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3766 add_dwarf_attr (die, &attr);
3769 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
3770 dwarf_split_debug_info, address attributes in dies destined for the
3771 final executable have force_direct set to avoid using indexed
3772 references. */
3774 static inline void
3775 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
3776 bool force_direct)
3778 dw_attr_node attr;
3779 char * lbl_id;
3781 lbl_id = xstrdup (lbl_low);
3782 attr.dw_attr = DW_AT_low_pc;
3783 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3784 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3785 if (dwarf_split_debug_info && !force_direct)
3786 attr.dw_attr_val.val_entry
3787 = add_addr_table_entry (lbl_id, ate_kind_label);
3788 else
3789 attr.dw_attr_val.val_entry = NULL;
3790 add_dwarf_attr (die, &attr);
3792 attr.dw_attr = DW_AT_high_pc;
3793 if (dwarf_version < 4)
3794 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3795 else
3796 attr.dw_attr_val.val_class = dw_val_class_high_pc;
3797 lbl_id = xstrdup (lbl_high);
3798 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3799 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
3800 && dwarf_split_debug_info && !force_direct)
3801 attr.dw_attr_val.val_entry
3802 = add_addr_table_entry (lbl_id, ate_kind_label);
3803 else
3804 attr.dw_attr_val.val_entry = NULL;
3805 add_dwarf_attr (die, &attr);
3808 /* Hash and equality functions for debug_str_hash. */
3810 static hashval_t
3811 debug_str_do_hash (const void *x)
3813 return htab_hash_string (((const struct indirect_string_node *)x)->str);
3816 static int
3817 debug_str_eq (const void *x1, const void *x2)
3819 return strcmp ((((const struct indirect_string_node *)x1)->str),
3820 (const char *)x2) == 0;
3823 /* Add STR to the indirect string hash table. */
3825 static struct indirect_string_node *
3826 find_AT_string (const char *str)
3828 struct indirect_string_node *node;
3829 void **slot;
3831 if (! debug_str_hash)
3832 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
3833 debug_str_eq, NULL);
3835 slot = htab_find_slot_with_hash (debug_str_hash, str,
3836 htab_hash_string (str), INSERT);
3837 if (*slot == NULL)
3839 node = ggc_alloc_cleared_indirect_string_node ();
3840 node->str = ggc_strdup (str);
3841 *slot = node;
3843 else
3844 node = (struct indirect_string_node *) *slot;
3846 node->refcount++;
3847 return node;
3850 /* Add a string attribute value to a DIE. */
3852 static inline void
3853 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
3855 dw_attr_node attr;
3856 struct indirect_string_node *node;
3858 node = find_AT_string (str);
3860 attr.dw_attr = attr_kind;
3861 attr.dw_attr_val.val_class = dw_val_class_str;
3862 attr.dw_attr_val.val_entry = NULL;
3863 attr.dw_attr_val.v.val_str = node;
3864 add_dwarf_attr (die, &attr);
3867 static inline const char *
3868 AT_string (dw_attr_ref a)
3870 gcc_assert (a && AT_class (a) == dw_val_class_str);
3871 return a->dw_attr_val.v.val_str->str;
3874 /* Call this function directly to bypass AT_string_form's logic to put
3875 the string inline in the die. */
3877 static void
3878 set_indirect_string (struct indirect_string_node *node)
3880 char label[32];
3881 /* Already indirect is a no op. */
3882 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
3884 gcc_assert (node->label);
3885 return;
3887 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
3888 ++dw2_string_counter;
3889 node->label = xstrdup (label);
3891 if (!dwarf_split_debug_info)
3893 node->form = DW_FORM_strp;
3894 node->index = NOT_INDEXED;
3896 else
3898 node->form = DW_FORM_GNU_str_index;
3899 node->index = NO_INDEX_ASSIGNED;
3903 /* Find out whether a string should be output inline in DIE
3904 or out-of-line in .debug_str section. */
3906 static enum dwarf_form
3907 find_string_form (struct indirect_string_node *node)
3909 unsigned int len;
3911 if (node->form)
3912 return node->form;
3914 len = strlen (node->str) + 1;
3916 /* If the string is shorter or equal to the size of the reference, it is
3917 always better to put it inline. */
3918 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
3919 return node->form = DW_FORM_string;
3921 /* If we cannot expect the linker to merge strings in .debug_str
3922 section, only put it into .debug_str if it is worth even in this
3923 single module. */
3924 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
3925 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
3926 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
3927 return node->form = DW_FORM_string;
3929 set_indirect_string (node);
3931 return node->form;
3934 /* Find out whether the string referenced from the attribute should be
3935 output inline in DIE or out-of-line in .debug_str section. */
3937 static enum dwarf_form
3938 AT_string_form (dw_attr_ref a)
3940 gcc_assert (a && AT_class (a) == dw_val_class_str);
3941 return find_string_form (a->dw_attr_val.v.val_str);
3944 /* Add a DIE reference attribute value to a DIE. */
3946 static inline void
3947 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
3949 dw_attr_node attr;
3951 #ifdef ENABLE_CHECKING
3952 gcc_assert (targ_die != NULL);
3953 #else
3954 /* With LTO we can end up trying to reference something we didn't create
3955 a DIE for. Avoid crashing later on a NULL referenced DIE. */
3956 if (targ_die == NULL)
3957 return;
3958 #endif
3960 attr.dw_attr = attr_kind;
3961 attr.dw_attr_val.val_class = dw_val_class_die_ref;
3962 attr.dw_attr_val.val_entry = NULL;
3963 attr.dw_attr_val.v.val_die_ref.die = targ_die;
3964 attr.dw_attr_val.v.val_die_ref.external = 0;
3965 add_dwarf_attr (die, &attr);
3968 /* Change DIE reference REF to point to NEW_DIE instead. */
3970 static inline void
3971 change_AT_die_ref (dw_attr_ref ref, dw_die_ref new_die)
3973 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
3974 ref->dw_attr_val.v.val_die_ref.die = new_die;
3975 ref->dw_attr_val.v.val_die_ref.external = 0;
3978 /* Add an AT_specification attribute to a DIE, and also make the back
3979 pointer from the specification to the definition. */
3981 static inline void
3982 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
3984 add_AT_die_ref (die, DW_AT_specification, targ_die);
3985 gcc_assert (!targ_die->die_definition);
3986 targ_die->die_definition = die;
3989 static inline dw_die_ref
3990 AT_ref (dw_attr_ref a)
3992 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
3993 return a->dw_attr_val.v.val_die_ref.die;
3996 static inline int
3997 AT_ref_external (dw_attr_ref a)
3999 if (a && AT_class (a) == dw_val_class_die_ref)
4000 return a->dw_attr_val.v.val_die_ref.external;
4002 return 0;
4005 static inline void
4006 set_AT_ref_external (dw_attr_ref a, int i)
4008 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4009 a->dw_attr_val.v.val_die_ref.external = i;
4012 /* Add an FDE reference attribute value to a DIE. */
4014 static inline void
4015 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4017 dw_attr_node attr;
4019 attr.dw_attr = attr_kind;
4020 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4021 attr.dw_attr_val.val_entry = NULL;
4022 attr.dw_attr_val.v.val_fde_index = targ_fde;
4023 add_dwarf_attr (die, &attr);
4026 /* Add a location description attribute value to a DIE. */
4028 static inline void
4029 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4031 dw_attr_node attr;
4033 attr.dw_attr = attr_kind;
4034 attr.dw_attr_val.val_class = dw_val_class_loc;
4035 attr.dw_attr_val.val_entry = NULL;
4036 attr.dw_attr_val.v.val_loc = loc;
4037 add_dwarf_attr (die, &attr);
4040 static inline dw_loc_descr_ref
4041 AT_loc (dw_attr_ref a)
4043 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4044 return a->dw_attr_val.v.val_loc;
4047 static inline void
4048 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4050 dw_attr_node attr;
4052 attr.dw_attr = attr_kind;
4053 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4054 attr.dw_attr_val.val_entry = NULL;
4055 attr.dw_attr_val.v.val_loc_list = loc_list;
4056 add_dwarf_attr (die, &attr);
4057 have_location_lists = true;
4060 static inline dw_loc_list_ref
4061 AT_loc_list (dw_attr_ref a)
4063 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4064 return a->dw_attr_val.v.val_loc_list;
4067 static inline dw_loc_list_ref *
4068 AT_loc_list_ptr (dw_attr_ref a)
4070 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4071 return &a->dw_attr_val.v.val_loc_list;
4074 /* Table of entries into the .debug_addr section. */
4076 static GTY ((param_is (addr_table_entry))) htab_t addr_index_table;
4078 /* Hash an address_table_entry. */
4080 static hashval_t
4081 addr_table_entry_do_hash (const void *x)
4083 const addr_table_entry *a = (const addr_table_entry *) x;
4084 switch (a->kind)
4086 case ate_kind_rtx:
4087 return iterative_hash_rtx (a->addr.rtl, 0);
4088 case ate_kind_rtx_dtprel:
4089 return iterative_hash_rtx (a->addr.rtl, 1);
4090 case ate_kind_label:
4091 return htab_hash_string (a->addr.label);
4092 default:
4093 gcc_unreachable ();
4097 /* Determine equality for two address_table_entries. */
4099 static int
4100 addr_table_entry_eq (const void *x1, const void *x2)
4102 const addr_table_entry *a1 = (const addr_table_entry *) x1;
4103 const addr_table_entry *a2 = (const addr_table_entry *) x2;
4105 if (a1->kind != a2->kind)
4106 return 0;
4107 switch (a1->kind)
4109 case ate_kind_rtx:
4110 case ate_kind_rtx_dtprel:
4111 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4112 case ate_kind_label:
4113 return strcmp (a1->addr.label, a2->addr.label) == 0;
4114 default:
4115 gcc_unreachable ();
4119 /* Initialize an addr_table_entry. */
4121 void
4122 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4124 e->kind = kind;
4125 switch (kind)
4127 case ate_kind_rtx:
4128 case ate_kind_rtx_dtprel:
4129 e->addr.rtl = (rtx) addr;
4130 break;
4131 case ate_kind_label:
4132 e->addr.label = (char *) addr;
4133 break;
4135 e->refcount = 0;
4136 e->index = NO_INDEX_ASSIGNED;
4139 /* Add attr to the address table entry to the table. Defer setting an
4140 index until output time. */
4142 static addr_table_entry *
4143 add_addr_table_entry (void *addr, enum ate_kind kind)
4145 addr_table_entry *node;
4146 addr_table_entry finder;
4147 void **slot;
4149 gcc_assert (dwarf_split_debug_info);
4150 if (! addr_index_table)
4151 addr_index_table = htab_create_ggc (10, addr_table_entry_do_hash,
4152 addr_table_entry_eq, NULL);
4153 init_addr_table_entry (&finder, kind, addr);
4154 slot = htab_find_slot (addr_index_table, &finder, INSERT);
4156 if (*slot == HTAB_EMPTY_ENTRY)
4158 node = ggc_alloc_cleared_addr_table_entry ();
4159 init_addr_table_entry (node, kind, addr);
4160 *slot = node;
4162 else
4163 node = (addr_table_entry *) *slot;
4165 node->refcount++;
4166 return node;
4169 /* Remove an entry from the addr table by decrementing its refcount.
4170 Strictly, decrementing the refcount would be enough, but the
4171 assertion that the entry is actually in the table has found
4172 bugs. */
4174 static void
4175 remove_addr_table_entry (addr_table_entry *entry)
4177 addr_table_entry *node;
4179 gcc_assert (dwarf_split_debug_info && addr_index_table);
4180 node = (addr_table_entry *) htab_find (addr_index_table, entry);
4181 /* After an index is assigned, the table is frozen. */
4182 gcc_assert (node->refcount > 0 && node->index == NO_INDEX_ASSIGNED);
4183 node->refcount--;
4186 /* Given a location list, remove all addresses it refers to from the
4187 address_table. */
4189 static void
4190 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4192 for (; descr; descr = descr->dw_loc_next)
4193 if (descr->dw_loc_oprnd1.val_entry != NULL)
4195 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4196 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4200 /* A helper function for dwarf2out_finish called through
4201 htab_traverse. Assign an addr_table_entry its index. All entries
4202 must be collected into the table when this function is called,
4203 because the indexing code relies on htab_traverse to traverse nodes
4204 in the same order for each run. */
4206 static int
4207 index_addr_table_entry (void **h, void *v)
4209 addr_table_entry *node = (addr_table_entry *) *h;
4210 unsigned int *index = (unsigned int *) v;
4212 /* Don't index unreferenced nodes. */
4213 if (node->refcount == 0)
4214 return 1;
4216 gcc_assert(node->index == NO_INDEX_ASSIGNED);
4217 node->index = *index;
4218 *index += 1;
4220 return 1;
4223 /* Add an address constant attribute value to a DIE. When using
4224 dwarf_split_debug_info, address attributes in dies destined for the
4225 final executable should be direct references--setting the parameter
4226 force_direct ensures this behavior. */
4228 static inline void
4229 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4230 bool force_direct)
4232 dw_attr_node attr;
4234 attr.dw_attr = attr_kind;
4235 attr.dw_attr_val.val_class = dw_val_class_addr;
4236 attr.dw_attr_val.v.val_addr = addr;
4237 if (dwarf_split_debug_info && !force_direct)
4238 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4239 else
4240 attr.dw_attr_val.val_entry = NULL;
4241 add_dwarf_attr (die, &attr);
4244 /* Get the RTX from to an address DIE attribute. */
4246 static inline rtx
4247 AT_addr (dw_attr_ref a)
4249 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4250 return a->dw_attr_val.v.val_addr;
4253 /* Add a file attribute value to a DIE. */
4255 static inline void
4256 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4257 struct dwarf_file_data *fd)
4259 dw_attr_node attr;
4261 attr.dw_attr = attr_kind;
4262 attr.dw_attr_val.val_class = dw_val_class_file;
4263 attr.dw_attr_val.val_entry = NULL;
4264 attr.dw_attr_val.v.val_file = fd;
4265 add_dwarf_attr (die, &attr);
4268 /* Get the dwarf_file_data from a file DIE attribute. */
4270 static inline struct dwarf_file_data *
4271 AT_file (dw_attr_ref a)
4273 gcc_assert (a && AT_class (a) == dw_val_class_file);
4274 return a->dw_attr_val.v.val_file;
4277 /* Add a vms delta attribute value to a DIE. */
4279 static inline void
4280 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4281 const char *lbl1, const char *lbl2)
4283 dw_attr_node attr;
4285 attr.dw_attr = attr_kind;
4286 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4287 attr.dw_attr_val.val_entry = NULL;
4288 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4289 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4290 add_dwarf_attr (die, &attr);
4293 /* Add a label identifier attribute value to a DIE. */
4295 static inline void
4296 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4297 const char *lbl_id)
4299 dw_attr_node attr;
4301 attr.dw_attr = attr_kind;
4302 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4303 attr.dw_attr_val.val_entry = NULL;
4304 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4305 if (dwarf_split_debug_info)
4306 attr.dw_attr_val.val_entry
4307 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4308 ate_kind_label);
4309 add_dwarf_attr (die, &attr);
4312 /* Add a section offset attribute value to a DIE, an offset into the
4313 debug_line section. */
4315 static inline void
4316 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4317 const char *label)
4319 dw_attr_node attr;
4321 attr.dw_attr = attr_kind;
4322 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4323 attr.dw_attr_val.val_entry = NULL;
4324 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4325 add_dwarf_attr (die, &attr);
4328 /* Add a section offset attribute value to a DIE, an offset into the
4329 debug_macinfo section. */
4331 static inline void
4332 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4333 const char *label)
4335 dw_attr_node attr;
4337 attr.dw_attr = attr_kind;
4338 attr.dw_attr_val.val_class = dw_val_class_macptr;
4339 attr.dw_attr_val.val_entry = NULL;
4340 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4341 add_dwarf_attr (die, &attr);
4344 /* Add an offset attribute value to a DIE. */
4346 static inline void
4347 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4348 unsigned HOST_WIDE_INT offset)
4350 dw_attr_node attr;
4352 attr.dw_attr = attr_kind;
4353 attr.dw_attr_val.val_class = dw_val_class_offset;
4354 attr.dw_attr_val.val_entry = NULL;
4355 attr.dw_attr_val.v.val_offset = offset;
4356 add_dwarf_attr (die, &attr);
4359 /* Add a range_list attribute value to a DIE. When using
4360 dwarf_split_debug_info, address attributes in dies destined for the
4361 final executable should be direct references--setting the parameter
4362 force_direct ensures this behavior. */
4364 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4365 #define RELOCATED_OFFSET (NULL)
4367 static void
4368 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4369 long unsigned int offset, bool force_direct)
4371 dw_attr_node attr;
4373 attr.dw_attr = attr_kind;
4374 attr.dw_attr_val.val_class = dw_val_class_range_list;
4375 /* For the range_list attribute, use val_entry to store whether the
4376 offset should follow split-debug-info or normal semantics. This
4377 value is read in output_range_list_offset. */
4378 if (dwarf_split_debug_info && !force_direct)
4379 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4380 else
4381 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4382 attr.dw_attr_val.v.val_offset = offset;
4383 add_dwarf_attr (die, &attr);
4386 /* Return the start label of a delta attribute. */
4388 static inline const char *
4389 AT_vms_delta1 (dw_attr_ref a)
4391 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4392 return a->dw_attr_val.v.val_vms_delta.lbl1;
4395 /* Return the end label of a delta attribute. */
4397 static inline const char *
4398 AT_vms_delta2 (dw_attr_ref a)
4400 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4401 return a->dw_attr_val.v.val_vms_delta.lbl2;
4404 static inline const char *
4405 AT_lbl (dw_attr_ref a)
4407 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4408 || AT_class (a) == dw_val_class_lineptr
4409 || AT_class (a) == dw_val_class_macptr
4410 || AT_class (a) == dw_val_class_high_pc));
4411 return a->dw_attr_val.v.val_lbl_id;
4414 /* Get the attribute of type attr_kind. */
4416 static dw_attr_ref
4417 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4419 dw_attr_ref a;
4420 unsigned ix;
4421 dw_die_ref spec = NULL;
4423 if (! die)
4424 return NULL;
4426 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4427 if (a->dw_attr == attr_kind)
4428 return a;
4429 else if (a->dw_attr == DW_AT_specification
4430 || a->dw_attr == DW_AT_abstract_origin)
4431 spec = AT_ref (a);
4433 if (spec)
4434 return get_AT (spec, attr_kind);
4436 return NULL;
4439 /* Returns the parent of the declaration of DIE. */
4441 static dw_die_ref
4442 get_die_parent (dw_die_ref die)
4444 dw_die_ref t;
4446 if (!die)
4447 return NULL;
4449 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4450 || (t = get_AT_ref (die, DW_AT_specification)))
4451 die = t;
4453 return die->die_parent;
4456 /* Return the "low pc" attribute value, typically associated with a subprogram
4457 DIE. Return null if the "low pc" attribute is either not present, or if it
4458 cannot be represented as an assembler label identifier. */
4460 static inline const char *
4461 get_AT_low_pc (dw_die_ref die)
4463 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4465 return a ? AT_lbl (a) : NULL;
4468 /* Return the "high pc" attribute value, typically associated with a subprogram
4469 DIE. Return null if the "high pc" attribute is either not present, or if it
4470 cannot be represented as an assembler label identifier. */
4472 static inline const char *
4473 get_AT_hi_pc (dw_die_ref die)
4475 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4477 return a ? AT_lbl (a) : NULL;
4480 /* Return the value of the string attribute designated by ATTR_KIND, or
4481 NULL if it is not present. */
4483 static inline const char *
4484 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4486 dw_attr_ref a = get_AT (die, attr_kind);
4488 return a ? AT_string (a) : NULL;
4491 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4492 if it is not present. */
4494 static inline int
4495 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4497 dw_attr_ref a = get_AT (die, attr_kind);
4499 return a ? AT_flag (a) : 0;
4502 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4503 if it is not present. */
4505 static inline unsigned
4506 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4508 dw_attr_ref a = get_AT (die, attr_kind);
4510 return a ? AT_unsigned (a) : 0;
4513 static inline dw_die_ref
4514 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4516 dw_attr_ref a = get_AT (die, attr_kind);
4518 return a ? AT_ref (a) : NULL;
4521 static inline struct dwarf_file_data *
4522 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4524 dw_attr_ref a = get_AT (die, attr_kind);
4526 return a ? AT_file (a) : NULL;
4529 /* Return TRUE if the language is C++. */
4531 static inline bool
4532 is_cxx (void)
4534 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4536 return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
4539 /* Return TRUE if the language is Fortran. */
4541 static inline bool
4542 is_fortran (void)
4544 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4546 return (lang == DW_LANG_Fortran77
4547 || lang == DW_LANG_Fortran90
4548 || lang == DW_LANG_Fortran95);
4551 /* Return TRUE if the language is Ada. */
4553 static inline bool
4554 is_ada (void)
4556 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4558 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4561 /* Remove the specified attribute if present. */
4563 static void
4564 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4566 dw_attr_ref a;
4567 unsigned ix;
4569 if (! die)
4570 return;
4572 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4573 if (a->dw_attr == attr_kind)
4575 if (AT_class (a) == dw_val_class_str)
4576 if (a->dw_attr_val.v.val_str->refcount)
4577 a->dw_attr_val.v.val_str->refcount--;
4579 /* vec::ordered_remove should help reduce the number of abbrevs
4580 that are needed. */
4581 die->die_attr->ordered_remove (ix);
4582 return;
4586 /* Remove CHILD from its parent. PREV must have the property that
4587 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4589 static void
4590 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4592 gcc_assert (child->die_parent == prev->die_parent);
4593 gcc_assert (prev->die_sib == child);
4594 if (prev == child)
4596 gcc_assert (child->die_parent->die_child == child);
4597 prev = NULL;
4599 else
4600 prev->die_sib = child->die_sib;
4601 if (child->die_parent->die_child == child)
4602 child->die_parent->die_child = prev;
4605 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4606 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4608 static void
4609 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4611 dw_die_ref parent = old_child->die_parent;
4613 gcc_assert (parent == prev->die_parent);
4614 gcc_assert (prev->die_sib == old_child);
4616 new_child->die_parent = parent;
4617 if (prev == old_child)
4619 gcc_assert (parent->die_child == old_child);
4620 new_child->die_sib = new_child;
4622 else
4624 prev->die_sib = new_child;
4625 new_child->die_sib = old_child->die_sib;
4627 if (old_child->die_parent->die_child == old_child)
4628 old_child->die_parent->die_child = new_child;
4631 /* Move all children from OLD_PARENT to NEW_PARENT. */
4633 static void
4634 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4636 dw_die_ref c;
4637 new_parent->die_child = old_parent->die_child;
4638 old_parent->die_child = NULL;
4639 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4642 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4643 matches TAG. */
4645 static void
4646 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4648 dw_die_ref c;
4650 c = die->die_child;
4651 if (c) do {
4652 dw_die_ref prev = c;
4653 c = c->die_sib;
4654 while (c->die_tag == tag)
4656 remove_child_with_prev (c, prev);
4657 /* Might have removed every child. */
4658 if (c == c->die_sib)
4659 return;
4660 c = c->die_sib;
4662 } while (c != die->die_child);
4665 /* Add a CHILD_DIE as the last child of DIE. */
4667 static void
4668 add_child_die (dw_die_ref die, dw_die_ref child_die)
4670 /* FIXME this should probably be an assert. */
4671 if (! die || ! child_die)
4672 return;
4673 gcc_assert (die != child_die);
4675 child_die->die_parent = die;
4676 if (die->die_child)
4678 child_die->die_sib = die->die_child->die_sib;
4679 die->die_child->die_sib = child_die;
4681 else
4682 child_die->die_sib = child_die;
4683 die->die_child = child_die;
4686 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4687 is the specification, to the end of PARENT's list of children.
4688 This is done by removing and re-adding it. */
4690 static void
4691 splice_child_die (dw_die_ref parent, dw_die_ref child)
4693 dw_die_ref p;
4695 /* We want the declaration DIE from inside the class, not the
4696 specification DIE at toplevel. */
4697 if (child->die_parent != parent)
4699 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4701 if (tmp)
4702 child = tmp;
4705 gcc_assert (child->die_parent == parent
4706 || (child->die_parent
4707 == get_AT_ref (parent, DW_AT_specification)));
4709 for (p = child->die_parent->die_child; ; p = p->die_sib)
4710 if (p->die_sib == child)
4712 remove_child_with_prev (child, p);
4713 break;
4716 add_child_die (parent, child);
4719 /* Return a pointer to a newly created DIE node. */
4721 static inline dw_die_ref
4722 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4724 dw_die_ref die = ggc_alloc_cleared_die_node ();
4726 die->die_tag = tag_value;
4728 if (parent_die != NULL)
4729 add_child_die (parent_die, die);
4730 else
4732 limbo_die_node *limbo_node;
4734 limbo_node = ggc_alloc_cleared_limbo_die_node ();
4735 limbo_node->die = die;
4736 limbo_node->created_for = t;
4737 limbo_node->next = limbo_die_list;
4738 limbo_die_list = limbo_node;
4741 return die;
4744 /* Return the DIE associated with the given type specifier. */
4746 static inline dw_die_ref
4747 lookup_type_die (tree type)
4749 return TYPE_SYMTAB_DIE (type);
4752 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4753 anonymous type named by the typedef TYPE_DIE, return the DIE of the
4754 anonymous type instead the one of the naming typedef. */
4756 static inline dw_die_ref
4757 strip_naming_typedef (tree type, dw_die_ref type_die)
4759 if (type
4760 && TREE_CODE (type) == RECORD_TYPE
4761 && type_die
4762 && type_die->die_tag == DW_TAG_typedef
4763 && is_naming_typedef_decl (TYPE_NAME (type)))
4764 type_die = get_AT_ref (type_die, DW_AT_type);
4765 return type_die;
4768 /* Like lookup_type_die, but if type is an anonymous type named by a
4769 typedef[1], return the DIE of the anonymous type instead the one of
4770 the naming typedef. This is because in gen_typedef_die, we did
4771 equate the anonymous struct named by the typedef with the DIE of
4772 the naming typedef. So by default, lookup_type_die on an anonymous
4773 struct yields the DIE of the naming typedef.
4775 [1]: Read the comment of is_naming_typedef_decl to learn about what
4776 a naming typedef is. */
4778 static inline dw_die_ref
4779 lookup_type_die_strip_naming_typedef (tree type)
4781 dw_die_ref die = lookup_type_die (type);
4782 return strip_naming_typedef (type, die);
4785 /* Equate a DIE to a given type specifier. */
4787 static inline void
4788 equate_type_number_to_die (tree type, dw_die_ref type_die)
4790 TYPE_SYMTAB_DIE (type) = type_die;
4793 /* Returns a hash value for X (which really is a die_struct). */
4795 static hashval_t
4796 decl_die_table_hash (const void *x)
4798 return (hashval_t) ((const_dw_die_ref) x)->decl_id;
4801 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
4803 static int
4804 decl_die_table_eq (const void *x, const void *y)
4806 return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
4809 /* Return the DIE associated with a given declaration. */
4811 static inline dw_die_ref
4812 lookup_decl_die (tree decl)
4814 return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
4817 /* Returns a hash value for X (which really is a var_loc_list). */
4819 static hashval_t
4820 decl_loc_table_hash (const void *x)
4822 return (hashval_t) ((const var_loc_list *) x)->decl_id;
4825 /* Return nonzero if decl_id of var_loc_list X is the same as
4826 UID of decl *Y. */
4828 static int
4829 decl_loc_table_eq (const void *x, const void *y)
4831 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
4834 /* Return the var_loc list associated with a given declaration. */
4836 static inline var_loc_list *
4837 lookup_decl_loc (const_tree decl)
4839 if (!decl_loc_table)
4840 return NULL;
4841 return (var_loc_list *)
4842 htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
4845 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
4847 static hashval_t
4848 cached_dw_loc_list_table_hash (const void *x)
4850 return (hashval_t) ((const cached_dw_loc_list *) x)->decl_id;
4853 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
4854 UID of decl *Y. */
4856 static int
4857 cached_dw_loc_list_table_eq (const void *x, const void *y)
4859 return (((const cached_dw_loc_list *) x)->decl_id
4860 == DECL_UID ((const_tree) y));
4863 /* Equate a DIE to a particular declaration. */
4865 static void
4866 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
4868 unsigned int decl_id = DECL_UID (decl);
4869 void **slot;
4871 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
4872 *slot = decl_die;
4873 decl_die->decl_id = decl_id;
4876 /* Return how many bits covers PIECE EXPR_LIST. */
4878 static int
4879 decl_piece_bitsize (rtx piece)
4881 int ret = (int) GET_MODE (piece);
4882 if (ret)
4883 return ret;
4884 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
4885 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
4886 return INTVAL (XEXP (XEXP (piece, 0), 0));
4889 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
4891 static rtx *
4892 decl_piece_varloc_ptr (rtx piece)
4894 if ((int) GET_MODE (piece))
4895 return &XEXP (piece, 0);
4896 else
4897 return &XEXP (XEXP (piece, 0), 1);
4900 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
4901 Next is the chain of following piece nodes. */
4903 static rtx
4904 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
4906 if (bitsize <= (int) MAX_MACHINE_MODE)
4907 return alloc_EXPR_LIST (bitsize, loc_note, next);
4908 else
4909 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
4910 GEN_INT (bitsize),
4911 loc_note), next);
4914 /* Return rtx that should be stored into loc field for
4915 LOC_NOTE and BITPOS/BITSIZE. */
4917 static rtx
4918 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
4919 HOST_WIDE_INT bitsize)
4921 if (bitsize != -1)
4923 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
4924 if (bitpos != 0)
4925 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
4927 return loc_note;
4930 /* This function either modifies location piece list *DEST in
4931 place (if SRC and INNER is NULL), or copies location piece list
4932 *SRC to *DEST while modifying it. Location BITPOS is modified
4933 to contain LOC_NOTE, any pieces overlapping it are removed resp.
4934 not copied and if needed some padding around it is added.
4935 When modifying in place, DEST should point to EXPR_LIST where
4936 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
4937 to the start of the whole list and INNER points to the EXPR_LIST
4938 where earlier pieces cover PIECE_BITPOS bits. */
4940 static void
4941 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
4942 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
4943 HOST_WIDE_INT bitsize, rtx loc_note)
4945 int diff;
4946 bool copy = inner != NULL;
4948 if (copy)
4950 /* First copy all nodes preceding the current bitpos. */
4951 while (src != inner)
4953 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
4954 decl_piece_bitsize (*src), NULL_RTX);
4955 dest = &XEXP (*dest, 1);
4956 src = &XEXP (*src, 1);
4959 /* Add padding if needed. */
4960 if (bitpos != piece_bitpos)
4962 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
4963 copy ? NULL_RTX : *dest);
4964 dest = &XEXP (*dest, 1);
4966 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
4968 gcc_assert (!copy);
4969 /* A piece with correct bitpos and bitsize already exist,
4970 just update the location for it and return. */
4971 *decl_piece_varloc_ptr (*dest) = loc_note;
4972 return;
4974 /* Add the piece that changed. */
4975 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
4976 dest = &XEXP (*dest, 1);
4977 /* Skip over pieces that overlap it. */
4978 diff = bitpos - piece_bitpos + bitsize;
4979 if (!copy)
4980 src = dest;
4981 while (diff > 0 && *src)
4983 rtx piece = *src;
4984 diff -= decl_piece_bitsize (piece);
4985 if (copy)
4986 src = &XEXP (piece, 1);
4987 else
4989 *src = XEXP (piece, 1);
4990 free_EXPR_LIST_node (piece);
4993 /* Add padding if needed. */
4994 if (diff < 0 && *src)
4996 if (!copy)
4997 dest = src;
4998 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
4999 dest = &XEXP (*dest, 1);
5001 if (!copy)
5002 return;
5003 /* Finally copy all nodes following it. */
5004 while (*src)
5006 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5007 decl_piece_bitsize (*src), NULL_RTX);
5008 dest = &XEXP (*dest, 1);
5009 src = &XEXP (*src, 1);
5013 /* Add a variable location node to the linked list for DECL. */
5015 static struct var_loc_node *
5016 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5018 unsigned int decl_id;
5019 var_loc_list *temp;
5020 void **slot;
5021 struct var_loc_node *loc = NULL;
5022 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5024 if (DECL_DEBUG_EXPR_IS_FROM (decl))
5026 tree realdecl = DECL_DEBUG_EXPR (decl);
5027 if (realdecl
5028 && (handled_component_p (realdecl)
5029 || (TREE_CODE (realdecl) == MEM_REF
5030 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR)))
5032 HOST_WIDE_INT maxsize;
5033 tree innerdecl;
5034 innerdecl
5035 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5036 if (!DECL_P (innerdecl)
5037 || DECL_IGNORED_P (innerdecl)
5038 || TREE_STATIC (innerdecl)
5039 || bitsize <= 0
5040 || bitpos + bitsize > 256
5041 || bitsize != maxsize)
5042 return NULL;
5043 decl = innerdecl;
5047 decl_id = DECL_UID (decl);
5048 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5049 if (*slot == NULL)
5051 temp = ggc_alloc_cleared_var_loc_list ();
5052 temp->decl_id = decl_id;
5053 *slot = temp;
5055 else
5056 temp = (var_loc_list *) *slot;
5058 /* For PARM_DECLs try to keep around the original incoming value,
5059 even if that means we'll emit a zero-range .debug_loc entry. */
5060 if (temp->last
5061 && temp->first == temp->last
5062 && TREE_CODE (decl) == PARM_DECL
5063 && GET_CODE (temp->first->loc) == NOTE
5064 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5065 && DECL_INCOMING_RTL (decl)
5066 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5067 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5068 == GET_CODE (DECL_INCOMING_RTL (decl))
5069 && prev_real_insn (temp->first->loc) == NULL_RTX
5070 && (bitsize != -1
5071 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5072 NOTE_VAR_LOCATION_LOC (loc_note))
5073 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5074 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5076 loc = ggc_alloc_cleared_var_loc_node ();
5077 temp->first->next = loc;
5078 temp->last = loc;
5079 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5081 else if (temp->last)
5083 struct var_loc_node *last = temp->last, *unused = NULL;
5084 rtx *piece_loc = NULL, last_loc_note;
5085 int piece_bitpos = 0;
5086 if (last->next)
5088 last = last->next;
5089 gcc_assert (last->next == NULL);
5091 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5093 piece_loc = &last->loc;
5096 int cur_bitsize = decl_piece_bitsize (*piece_loc);
5097 if (piece_bitpos + cur_bitsize > bitpos)
5098 break;
5099 piece_bitpos += cur_bitsize;
5100 piece_loc = &XEXP (*piece_loc, 1);
5102 while (*piece_loc);
5104 /* TEMP->LAST here is either pointer to the last but one or
5105 last element in the chained list, LAST is pointer to the
5106 last element. */
5107 if (label && strcmp (last->label, label) == 0)
5109 /* For SRA optimized variables if there weren't any real
5110 insns since last note, just modify the last node. */
5111 if (piece_loc != NULL)
5113 adjust_piece_list (piece_loc, NULL, NULL,
5114 bitpos, piece_bitpos, bitsize, loc_note);
5115 return NULL;
5117 /* If the last note doesn't cover any instructions, remove it. */
5118 if (temp->last != last)
5120 temp->last->next = NULL;
5121 unused = last;
5122 last = temp->last;
5123 gcc_assert (strcmp (last->label, label) != 0);
5125 else
5127 gcc_assert (temp->first == temp->last
5128 || (temp->first->next == temp->last
5129 && TREE_CODE (decl) == PARM_DECL));
5130 memset (temp->last, '\0', sizeof (*temp->last));
5131 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5132 return temp->last;
5135 if (bitsize == -1 && NOTE_P (last->loc))
5136 last_loc_note = last->loc;
5137 else if (piece_loc != NULL
5138 && *piece_loc != NULL_RTX
5139 && piece_bitpos == bitpos
5140 && decl_piece_bitsize (*piece_loc) == bitsize)
5141 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5142 else
5143 last_loc_note = NULL_RTX;
5144 /* If the current location is the same as the end of the list,
5145 and either both or neither of the locations is uninitialized,
5146 we have nothing to do. */
5147 if (last_loc_note == NULL_RTX
5148 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5149 NOTE_VAR_LOCATION_LOC (loc_note)))
5150 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5151 != NOTE_VAR_LOCATION_STATUS (loc_note))
5152 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5153 == VAR_INIT_STATUS_UNINITIALIZED)
5154 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5155 == VAR_INIT_STATUS_UNINITIALIZED))))
5157 /* Add LOC to the end of list and update LAST. If the last
5158 element of the list has been removed above, reuse its
5159 memory for the new node, otherwise allocate a new one. */
5160 if (unused)
5162 loc = unused;
5163 memset (loc, '\0', sizeof (*loc));
5165 else
5166 loc = ggc_alloc_cleared_var_loc_node ();
5167 if (bitsize == -1 || piece_loc == NULL)
5168 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5169 else
5170 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5171 bitpos, piece_bitpos, bitsize, loc_note);
5172 last->next = loc;
5173 /* Ensure TEMP->LAST will point either to the new last but one
5174 element of the chain, or to the last element in it. */
5175 if (last != temp->last)
5176 temp->last = last;
5178 else if (unused)
5179 ggc_free (unused);
5181 else
5183 loc = ggc_alloc_cleared_var_loc_node ();
5184 temp->first = loc;
5185 temp->last = loc;
5186 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5188 return loc;
5191 /* Keep track of the number of spaces used to indent the
5192 output of the debugging routines that print the structure of
5193 the DIE internal representation. */
5194 static int print_indent;
5196 /* Indent the line the number of spaces given by print_indent. */
5198 static inline void
5199 print_spaces (FILE *outfile)
5201 fprintf (outfile, "%*s", print_indent, "");
5204 /* Print a type signature in hex. */
5206 static inline void
5207 print_signature (FILE *outfile, char *sig)
5209 int i;
5211 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5212 fprintf (outfile, "%02x", sig[i] & 0xff);
5215 /* Print the information associated with a given DIE, and its children.
5216 This routine is a debugging aid only. */
5218 static void
5219 print_die (dw_die_ref die, FILE *outfile)
5221 dw_attr_ref a;
5222 dw_die_ref c;
5223 unsigned ix;
5225 print_spaces (outfile);
5226 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5227 die->die_offset, dwarf_tag_name (die->die_tag),
5228 (void*) die);
5229 print_spaces (outfile);
5230 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5231 fprintf (outfile, " offset: %ld", die->die_offset);
5232 fprintf (outfile, " mark: %d\n", die->die_mark);
5234 if (die->comdat_type_p)
5236 print_spaces (outfile);
5237 fprintf (outfile, " signature: ");
5238 print_signature (outfile, die->die_id.die_type_node->signature);
5239 fprintf (outfile, "\n");
5242 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5244 print_spaces (outfile);
5245 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5247 switch (AT_class (a))
5249 case dw_val_class_addr:
5250 fprintf (outfile, "address");
5251 break;
5252 case dw_val_class_offset:
5253 fprintf (outfile, "offset");
5254 break;
5255 case dw_val_class_loc:
5256 fprintf (outfile, "location descriptor");
5257 break;
5258 case dw_val_class_loc_list:
5259 fprintf (outfile, "location list -> label:%s",
5260 AT_loc_list (a)->ll_symbol);
5261 break;
5262 case dw_val_class_range_list:
5263 fprintf (outfile, "range list");
5264 break;
5265 case dw_val_class_const:
5266 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5267 break;
5268 case dw_val_class_unsigned_const:
5269 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5270 break;
5271 case dw_val_class_const_double:
5272 fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
5273 HOST_WIDE_INT_PRINT_UNSIGNED")",
5274 a->dw_attr_val.v.val_double.high,
5275 a->dw_attr_val.v.val_double.low);
5276 break;
5277 case dw_val_class_vec:
5278 fprintf (outfile, "floating-point or vector constant");
5279 break;
5280 case dw_val_class_flag:
5281 fprintf (outfile, "%u", AT_flag (a));
5282 break;
5283 case dw_val_class_die_ref:
5284 if (AT_ref (a) != NULL)
5286 if (AT_ref (a)->comdat_type_p)
5288 fprintf (outfile, "die -> signature: ");
5289 print_signature (outfile,
5290 AT_ref (a)->die_id.die_type_node->signature);
5292 else if (AT_ref (a)->die_id.die_symbol)
5293 fprintf (outfile, "die -> label: %s",
5294 AT_ref (a)->die_id.die_symbol);
5295 else
5296 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5297 fprintf (outfile, " (%p)", (void *) AT_ref (a));
5299 else
5300 fprintf (outfile, "die -> <null>");
5301 break;
5302 case dw_val_class_vms_delta:
5303 fprintf (outfile, "delta: @slotcount(%s-%s)",
5304 AT_vms_delta2 (a), AT_vms_delta1 (a));
5305 break;
5306 case dw_val_class_lbl_id:
5307 case dw_val_class_lineptr:
5308 case dw_val_class_macptr:
5309 case dw_val_class_high_pc:
5310 fprintf (outfile, "label: %s", AT_lbl (a));
5311 break;
5312 case dw_val_class_str:
5313 if (AT_string (a) != NULL)
5314 fprintf (outfile, "\"%s\"", AT_string (a));
5315 else
5316 fprintf (outfile, "<null>");
5317 break;
5318 case dw_val_class_file:
5319 fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5320 AT_file (a)->emitted_number);
5321 break;
5322 case dw_val_class_data8:
5324 int i;
5326 for (i = 0; i < 8; i++)
5327 fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
5328 break;
5330 default:
5331 break;
5334 fprintf (outfile, "\n");
5337 if (die->die_child != NULL)
5339 print_indent += 4;
5340 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5341 print_indent -= 4;
5343 if (print_indent == 0)
5344 fprintf (outfile, "\n");
5347 /* Print the information collected for a given DIE. */
5349 DEBUG_FUNCTION void
5350 debug_dwarf_die (dw_die_ref die)
5352 print_die (die, stderr);
5355 /* Print all DWARF information collected for the compilation unit.
5356 This routine is a debugging aid only. */
5358 DEBUG_FUNCTION void
5359 debug_dwarf (void)
5361 print_indent = 0;
5362 print_die (comp_unit_die (), stderr);
5365 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5366 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5367 DIE that marks the start of the DIEs for this include file. */
5369 static dw_die_ref
5370 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5372 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5373 dw_die_ref new_unit = gen_compile_unit_die (filename);
5375 new_unit->die_sib = old_unit;
5376 return new_unit;
5379 /* Close an include-file CU and reopen the enclosing one. */
5381 static dw_die_ref
5382 pop_compile_unit (dw_die_ref old_unit)
5384 dw_die_ref new_unit = old_unit->die_sib;
5386 old_unit->die_sib = NULL;
5387 return new_unit;
5390 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5391 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5393 /* Calculate the checksum of a location expression. */
5395 static inline void
5396 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5398 int tem;
5400 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5401 CHECKSUM (tem);
5402 CHECKSUM (loc->dw_loc_oprnd1);
5403 CHECKSUM (loc->dw_loc_oprnd2);
5406 /* Calculate the checksum of an attribute. */
5408 static void
5409 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5411 dw_loc_descr_ref loc;
5412 rtx r;
5414 CHECKSUM (at->dw_attr);
5416 /* We don't care that this was compiled with a different compiler
5417 snapshot; if the output is the same, that's what matters. */
5418 if (at->dw_attr == DW_AT_producer)
5419 return;
5421 switch (AT_class (at))
5423 case dw_val_class_const:
5424 CHECKSUM (at->dw_attr_val.v.val_int);
5425 break;
5426 case dw_val_class_unsigned_const:
5427 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5428 break;
5429 case dw_val_class_const_double:
5430 CHECKSUM (at->dw_attr_val.v.val_double);
5431 break;
5432 case dw_val_class_vec:
5433 CHECKSUM (at->dw_attr_val.v.val_vec);
5434 break;
5435 case dw_val_class_flag:
5436 CHECKSUM (at->dw_attr_val.v.val_flag);
5437 break;
5438 case dw_val_class_str:
5439 CHECKSUM_STRING (AT_string (at));
5440 break;
5442 case dw_val_class_addr:
5443 r = AT_addr (at);
5444 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5445 CHECKSUM_STRING (XSTR (r, 0));
5446 break;
5448 case dw_val_class_offset:
5449 CHECKSUM (at->dw_attr_val.v.val_offset);
5450 break;
5452 case dw_val_class_loc:
5453 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5454 loc_checksum (loc, ctx);
5455 break;
5457 case dw_val_class_die_ref:
5458 die_checksum (AT_ref (at), ctx, mark);
5459 break;
5461 case dw_val_class_fde_ref:
5462 case dw_val_class_vms_delta:
5463 case dw_val_class_lbl_id:
5464 case dw_val_class_lineptr:
5465 case dw_val_class_macptr:
5466 case dw_val_class_high_pc:
5467 break;
5469 case dw_val_class_file:
5470 CHECKSUM_STRING (AT_file (at)->filename);
5471 break;
5473 case dw_val_class_data8:
5474 CHECKSUM (at->dw_attr_val.v.val_data8);
5475 break;
5477 default:
5478 break;
5482 /* Calculate the checksum of a DIE. */
5484 static void
5485 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5487 dw_die_ref c;
5488 dw_attr_ref a;
5489 unsigned ix;
5491 /* To avoid infinite recursion. */
5492 if (die->die_mark)
5494 CHECKSUM (die->die_mark);
5495 return;
5497 die->die_mark = ++(*mark);
5499 CHECKSUM (die->die_tag);
5501 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5502 attr_checksum (a, ctx, mark);
5504 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5507 #undef CHECKSUM
5508 #undef CHECKSUM_STRING
5510 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
5511 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5512 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5513 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5514 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5515 #define CHECKSUM_ATTR(FOO) \
5516 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5518 /* Calculate the checksum of a number in signed LEB128 format. */
5520 static void
5521 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5523 unsigned char byte;
5524 bool more;
5526 while (1)
5528 byte = (value & 0x7f);
5529 value >>= 7;
5530 more = !((value == 0 && (byte & 0x40) == 0)
5531 || (value == -1 && (byte & 0x40) != 0));
5532 if (more)
5533 byte |= 0x80;
5534 CHECKSUM (byte);
5535 if (!more)
5536 break;
5540 /* Calculate the checksum of a number in unsigned LEB128 format. */
5542 static void
5543 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5545 while (1)
5547 unsigned char byte = (value & 0x7f);
5548 value >>= 7;
5549 if (value != 0)
5550 /* More bytes to follow. */
5551 byte |= 0x80;
5552 CHECKSUM (byte);
5553 if (value == 0)
5554 break;
5558 /* Checksum the context of the DIE. This adds the names of any
5559 surrounding namespaces or structures to the checksum. */
5561 static void
5562 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5564 const char *name;
5565 dw_die_ref spec;
5566 int tag = die->die_tag;
5568 if (tag != DW_TAG_namespace
5569 && tag != DW_TAG_structure_type
5570 && tag != DW_TAG_class_type)
5571 return;
5573 name = get_AT_string (die, DW_AT_name);
5575 spec = get_AT_ref (die, DW_AT_specification);
5576 if (spec != NULL)
5577 die = spec;
5579 if (die->die_parent != NULL)
5580 checksum_die_context (die->die_parent, ctx);
5582 CHECKSUM_ULEB128 ('C');
5583 CHECKSUM_ULEB128 (tag);
5584 if (name != NULL)
5585 CHECKSUM_STRING (name);
5588 /* Calculate the checksum of a location expression. */
5590 static inline void
5591 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5593 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5594 were emitted as a DW_FORM_sdata instead of a location expression. */
5595 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5597 CHECKSUM_ULEB128 (DW_FORM_sdata);
5598 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5599 return;
5602 /* Otherwise, just checksum the raw location expression. */
5603 while (loc != NULL)
5605 CHECKSUM_ULEB128 (loc->dw_loc_opc);
5606 CHECKSUM (loc->dw_loc_oprnd1);
5607 CHECKSUM (loc->dw_loc_oprnd2);
5608 loc = loc->dw_loc_next;
5612 /* Calculate the checksum of an attribute. */
5614 static void
5615 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5616 struct md5_ctx *ctx, int *mark)
5618 dw_loc_descr_ref loc;
5619 rtx r;
5621 if (AT_class (at) == dw_val_class_die_ref)
5623 dw_die_ref target_die = AT_ref (at);
5625 /* For pointer and reference types, we checksum only the (qualified)
5626 name of the target type (if there is a name). For friend entries,
5627 we checksum only the (qualified) name of the target type or function.
5628 This allows the checksum to remain the same whether the target type
5629 is complete or not. */
5630 if ((at->dw_attr == DW_AT_type
5631 && (tag == DW_TAG_pointer_type
5632 || tag == DW_TAG_reference_type
5633 || tag == DW_TAG_rvalue_reference_type
5634 || tag == DW_TAG_ptr_to_member_type))
5635 || (at->dw_attr == DW_AT_friend
5636 && tag == DW_TAG_friend))
5638 dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5640 if (name_attr != NULL)
5642 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5644 if (decl == NULL)
5645 decl = target_die;
5646 CHECKSUM_ULEB128 ('N');
5647 CHECKSUM_ULEB128 (at->dw_attr);
5648 if (decl->die_parent != NULL)
5649 checksum_die_context (decl->die_parent, ctx);
5650 CHECKSUM_ULEB128 ('E');
5651 CHECKSUM_STRING (AT_string (name_attr));
5652 return;
5656 /* For all other references to another DIE, we check to see if the
5657 target DIE has already been visited. If it has, we emit a
5658 backward reference; if not, we descend recursively. */
5659 if (target_die->die_mark > 0)
5661 CHECKSUM_ULEB128 ('R');
5662 CHECKSUM_ULEB128 (at->dw_attr);
5663 CHECKSUM_ULEB128 (target_die->die_mark);
5665 else
5667 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5669 if (decl == NULL)
5670 decl = target_die;
5671 target_die->die_mark = ++(*mark);
5672 CHECKSUM_ULEB128 ('T');
5673 CHECKSUM_ULEB128 (at->dw_attr);
5674 if (decl->die_parent != NULL)
5675 checksum_die_context (decl->die_parent, ctx);
5676 die_checksum_ordered (target_die, ctx, mark);
5678 return;
5681 CHECKSUM_ULEB128 ('A');
5682 CHECKSUM_ULEB128 (at->dw_attr);
5684 switch (AT_class (at))
5686 case dw_val_class_const:
5687 CHECKSUM_ULEB128 (DW_FORM_sdata);
5688 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
5689 break;
5691 case dw_val_class_unsigned_const:
5692 CHECKSUM_ULEB128 (DW_FORM_sdata);
5693 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
5694 break;
5696 case dw_val_class_const_double:
5697 CHECKSUM_ULEB128 (DW_FORM_block);
5698 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
5699 CHECKSUM (at->dw_attr_val.v.val_double);
5700 break;
5702 case dw_val_class_vec:
5703 CHECKSUM_ULEB128 (DW_FORM_block);
5704 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
5705 CHECKSUM (at->dw_attr_val.v.val_vec);
5706 break;
5708 case dw_val_class_flag:
5709 CHECKSUM_ULEB128 (DW_FORM_flag);
5710 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
5711 break;
5713 case dw_val_class_str:
5714 CHECKSUM_ULEB128 (DW_FORM_string);
5715 CHECKSUM_STRING (AT_string (at));
5716 break;
5718 case dw_val_class_addr:
5719 r = AT_addr (at);
5720 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5721 CHECKSUM_ULEB128 (DW_FORM_string);
5722 CHECKSUM_STRING (XSTR (r, 0));
5723 break;
5725 case dw_val_class_offset:
5726 CHECKSUM_ULEB128 (DW_FORM_sdata);
5727 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
5728 break;
5730 case dw_val_class_loc:
5731 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5732 loc_checksum_ordered (loc, ctx);
5733 break;
5735 case dw_val_class_fde_ref:
5736 case dw_val_class_lbl_id:
5737 case dw_val_class_lineptr:
5738 case dw_val_class_macptr:
5739 case dw_val_class_high_pc:
5740 break;
5742 case dw_val_class_file:
5743 CHECKSUM_ULEB128 (DW_FORM_string);
5744 CHECKSUM_STRING (AT_file (at)->filename);
5745 break;
5747 case dw_val_class_data8:
5748 CHECKSUM (at->dw_attr_val.v.val_data8);
5749 break;
5751 default:
5752 break;
5756 struct checksum_attributes
5758 dw_attr_ref at_name;
5759 dw_attr_ref at_type;
5760 dw_attr_ref at_friend;
5761 dw_attr_ref at_accessibility;
5762 dw_attr_ref at_address_class;
5763 dw_attr_ref at_allocated;
5764 dw_attr_ref at_artificial;
5765 dw_attr_ref at_associated;
5766 dw_attr_ref at_binary_scale;
5767 dw_attr_ref at_bit_offset;
5768 dw_attr_ref at_bit_size;
5769 dw_attr_ref at_bit_stride;
5770 dw_attr_ref at_byte_size;
5771 dw_attr_ref at_byte_stride;
5772 dw_attr_ref at_const_value;
5773 dw_attr_ref at_containing_type;
5774 dw_attr_ref at_count;
5775 dw_attr_ref at_data_location;
5776 dw_attr_ref at_data_member_location;
5777 dw_attr_ref at_decimal_scale;
5778 dw_attr_ref at_decimal_sign;
5779 dw_attr_ref at_default_value;
5780 dw_attr_ref at_digit_count;
5781 dw_attr_ref at_discr;
5782 dw_attr_ref at_discr_list;
5783 dw_attr_ref at_discr_value;
5784 dw_attr_ref at_encoding;
5785 dw_attr_ref at_endianity;
5786 dw_attr_ref at_explicit;
5787 dw_attr_ref at_is_optional;
5788 dw_attr_ref at_location;
5789 dw_attr_ref at_lower_bound;
5790 dw_attr_ref at_mutable;
5791 dw_attr_ref at_ordering;
5792 dw_attr_ref at_picture_string;
5793 dw_attr_ref at_prototyped;
5794 dw_attr_ref at_small;
5795 dw_attr_ref at_segment;
5796 dw_attr_ref at_string_length;
5797 dw_attr_ref at_threads_scaled;
5798 dw_attr_ref at_upper_bound;
5799 dw_attr_ref at_use_location;
5800 dw_attr_ref at_use_UTF8;
5801 dw_attr_ref at_variable_parameter;
5802 dw_attr_ref at_virtuality;
5803 dw_attr_ref at_visibility;
5804 dw_attr_ref at_vtable_elem_location;
5807 /* Collect the attributes that we will want to use for the checksum. */
5809 static void
5810 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
5812 dw_attr_ref a;
5813 unsigned ix;
5815 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5817 switch (a->dw_attr)
5819 case DW_AT_name:
5820 attrs->at_name = a;
5821 break;
5822 case DW_AT_type:
5823 attrs->at_type = a;
5824 break;
5825 case DW_AT_friend:
5826 attrs->at_friend = a;
5827 break;
5828 case DW_AT_accessibility:
5829 attrs->at_accessibility = a;
5830 break;
5831 case DW_AT_address_class:
5832 attrs->at_address_class = a;
5833 break;
5834 case DW_AT_allocated:
5835 attrs->at_allocated = a;
5836 break;
5837 case DW_AT_artificial:
5838 attrs->at_artificial = a;
5839 break;
5840 case DW_AT_associated:
5841 attrs->at_associated = a;
5842 break;
5843 case DW_AT_binary_scale:
5844 attrs->at_binary_scale = a;
5845 break;
5846 case DW_AT_bit_offset:
5847 attrs->at_bit_offset = a;
5848 break;
5849 case DW_AT_bit_size:
5850 attrs->at_bit_size = a;
5851 break;
5852 case DW_AT_bit_stride:
5853 attrs->at_bit_stride = a;
5854 break;
5855 case DW_AT_byte_size:
5856 attrs->at_byte_size = a;
5857 break;
5858 case DW_AT_byte_stride:
5859 attrs->at_byte_stride = a;
5860 break;
5861 case DW_AT_const_value:
5862 attrs->at_const_value = a;
5863 break;
5864 case DW_AT_containing_type:
5865 attrs->at_containing_type = a;
5866 break;
5867 case DW_AT_count:
5868 attrs->at_count = a;
5869 break;
5870 case DW_AT_data_location:
5871 attrs->at_data_location = a;
5872 break;
5873 case DW_AT_data_member_location:
5874 attrs->at_data_member_location = a;
5875 break;
5876 case DW_AT_decimal_scale:
5877 attrs->at_decimal_scale = a;
5878 break;
5879 case DW_AT_decimal_sign:
5880 attrs->at_decimal_sign = a;
5881 break;
5882 case DW_AT_default_value:
5883 attrs->at_default_value = a;
5884 break;
5885 case DW_AT_digit_count:
5886 attrs->at_digit_count = a;
5887 break;
5888 case DW_AT_discr:
5889 attrs->at_discr = a;
5890 break;
5891 case DW_AT_discr_list:
5892 attrs->at_discr_list = a;
5893 break;
5894 case DW_AT_discr_value:
5895 attrs->at_discr_value = a;
5896 break;
5897 case DW_AT_encoding:
5898 attrs->at_encoding = a;
5899 break;
5900 case DW_AT_endianity:
5901 attrs->at_endianity = a;
5902 break;
5903 case DW_AT_explicit:
5904 attrs->at_explicit = a;
5905 break;
5906 case DW_AT_is_optional:
5907 attrs->at_is_optional = a;
5908 break;
5909 case DW_AT_location:
5910 attrs->at_location = a;
5911 break;
5912 case DW_AT_lower_bound:
5913 attrs->at_lower_bound = a;
5914 break;
5915 case DW_AT_mutable:
5916 attrs->at_mutable = a;
5917 break;
5918 case DW_AT_ordering:
5919 attrs->at_ordering = a;
5920 break;
5921 case DW_AT_picture_string:
5922 attrs->at_picture_string = a;
5923 break;
5924 case DW_AT_prototyped:
5925 attrs->at_prototyped = a;
5926 break;
5927 case DW_AT_small:
5928 attrs->at_small = a;
5929 break;
5930 case DW_AT_segment:
5931 attrs->at_segment = a;
5932 break;
5933 case DW_AT_string_length:
5934 attrs->at_string_length = a;
5935 break;
5936 case DW_AT_threads_scaled:
5937 attrs->at_threads_scaled = a;
5938 break;
5939 case DW_AT_upper_bound:
5940 attrs->at_upper_bound = a;
5941 break;
5942 case DW_AT_use_location:
5943 attrs->at_use_location = a;
5944 break;
5945 case DW_AT_use_UTF8:
5946 attrs->at_use_UTF8 = a;
5947 break;
5948 case DW_AT_variable_parameter:
5949 attrs->at_variable_parameter = a;
5950 break;
5951 case DW_AT_virtuality:
5952 attrs->at_virtuality = a;
5953 break;
5954 case DW_AT_visibility:
5955 attrs->at_visibility = a;
5956 break;
5957 case DW_AT_vtable_elem_location:
5958 attrs->at_vtable_elem_location = a;
5959 break;
5960 default:
5961 break;
5966 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
5968 static void
5969 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5971 dw_die_ref c;
5972 dw_die_ref decl;
5973 struct checksum_attributes attrs;
5975 CHECKSUM_ULEB128 ('D');
5976 CHECKSUM_ULEB128 (die->die_tag);
5978 memset (&attrs, 0, sizeof (attrs));
5980 decl = get_AT_ref (die, DW_AT_specification);
5981 if (decl != NULL)
5982 collect_checksum_attributes (&attrs, decl);
5983 collect_checksum_attributes (&attrs, die);
5985 CHECKSUM_ATTR (attrs.at_name);
5986 CHECKSUM_ATTR (attrs.at_accessibility);
5987 CHECKSUM_ATTR (attrs.at_address_class);
5988 CHECKSUM_ATTR (attrs.at_allocated);
5989 CHECKSUM_ATTR (attrs.at_artificial);
5990 CHECKSUM_ATTR (attrs.at_associated);
5991 CHECKSUM_ATTR (attrs.at_binary_scale);
5992 CHECKSUM_ATTR (attrs.at_bit_offset);
5993 CHECKSUM_ATTR (attrs.at_bit_size);
5994 CHECKSUM_ATTR (attrs.at_bit_stride);
5995 CHECKSUM_ATTR (attrs.at_byte_size);
5996 CHECKSUM_ATTR (attrs.at_byte_stride);
5997 CHECKSUM_ATTR (attrs.at_const_value);
5998 CHECKSUM_ATTR (attrs.at_containing_type);
5999 CHECKSUM_ATTR (attrs.at_count);
6000 CHECKSUM_ATTR (attrs.at_data_location);
6001 CHECKSUM_ATTR (attrs.at_data_member_location);
6002 CHECKSUM_ATTR (attrs.at_decimal_scale);
6003 CHECKSUM_ATTR (attrs.at_decimal_sign);
6004 CHECKSUM_ATTR (attrs.at_default_value);
6005 CHECKSUM_ATTR (attrs.at_digit_count);
6006 CHECKSUM_ATTR (attrs.at_discr);
6007 CHECKSUM_ATTR (attrs.at_discr_list);
6008 CHECKSUM_ATTR (attrs.at_discr_value);
6009 CHECKSUM_ATTR (attrs.at_encoding);
6010 CHECKSUM_ATTR (attrs.at_endianity);
6011 CHECKSUM_ATTR (attrs.at_explicit);
6012 CHECKSUM_ATTR (attrs.at_is_optional);
6013 CHECKSUM_ATTR (attrs.at_location);
6014 CHECKSUM_ATTR (attrs.at_lower_bound);
6015 CHECKSUM_ATTR (attrs.at_mutable);
6016 CHECKSUM_ATTR (attrs.at_ordering);
6017 CHECKSUM_ATTR (attrs.at_picture_string);
6018 CHECKSUM_ATTR (attrs.at_prototyped);
6019 CHECKSUM_ATTR (attrs.at_small);
6020 CHECKSUM_ATTR (attrs.at_segment);
6021 CHECKSUM_ATTR (attrs.at_string_length);
6022 CHECKSUM_ATTR (attrs.at_threads_scaled);
6023 CHECKSUM_ATTR (attrs.at_upper_bound);
6024 CHECKSUM_ATTR (attrs.at_use_location);
6025 CHECKSUM_ATTR (attrs.at_use_UTF8);
6026 CHECKSUM_ATTR (attrs.at_variable_parameter);
6027 CHECKSUM_ATTR (attrs.at_virtuality);
6028 CHECKSUM_ATTR (attrs.at_visibility);
6029 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6030 CHECKSUM_ATTR (attrs.at_type);
6031 CHECKSUM_ATTR (attrs.at_friend);
6033 /* Checksum the child DIEs, except for nested types and member functions. */
6034 c = die->die_child;
6035 if (c) do {
6036 dw_attr_ref name_attr;
6038 c = c->die_sib;
6039 name_attr = get_AT (c, DW_AT_name);
6040 if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
6041 && name_attr != NULL)
6043 CHECKSUM_ULEB128 ('S');
6044 CHECKSUM_ULEB128 (c->die_tag);
6045 CHECKSUM_STRING (AT_string (name_attr));
6047 else
6049 /* Mark this DIE so it gets processed when unmarking. */
6050 if (c->die_mark == 0)
6051 c->die_mark = -1;
6052 die_checksum_ordered (c, ctx, mark);
6054 } while (c != die->die_child);
6056 CHECKSUM_ULEB128 (0);
6059 #undef CHECKSUM
6060 #undef CHECKSUM_STRING
6061 #undef CHECKSUM_ATTR
6062 #undef CHECKSUM_LEB128
6063 #undef CHECKSUM_ULEB128
6065 /* Generate the type signature for DIE. This is computed by generating an
6066 MD5 checksum over the DIE's tag, its relevant attributes, and its
6067 children. Attributes that are references to other DIEs are processed
6068 by recursion, using the MARK field to prevent infinite recursion.
6069 If the DIE is nested inside a namespace or another type, we also
6070 need to include that context in the signature. The lower 64 bits
6071 of the resulting MD5 checksum comprise the signature. */
6073 static void
6074 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6076 int mark;
6077 const char *name;
6078 unsigned char checksum[16];
6079 struct md5_ctx ctx;
6080 dw_die_ref decl;
6081 dw_die_ref parent;
6083 name = get_AT_string (die, DW_AT_name);
6084 decl = get_AT_ref (die, DW_AT_specification);
6085 parent = get_die_parent (die);
6087 /* First, compute a signature for just the type name (and its surrounding
6088 context, if any. This is stored in the type unit DIE for link-time
6089 ODR (one-definition rule) checking. */
6091 if (is_cxx() && name != NULL)
6093 md5_init_ctx (&ctx);
6095 /* Checksum the names of surrounding namespaces and structures. */
6096 if (parent != NULL)
6097 checksum_die_context (parent, &ctx);
6099 md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
6100 md5_process_bytes (name, strlen (name) + 1, &ctx);
6101 md5_finish_ctx (&ctx, checksum);
6103 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6106 /* Next, compute the complete type signature. */
6108 md5_init_ctx (&ctx);
6109 mark = 1;
6110 die->die_mark = mark;
6112 /* Checksum the names of surrounding namespaces and structures. */
6113 if (parent != NULL)
6114 checksum_die_context (parent, &ctx);
6116 /* Checksum the DIE and its children. */
6117 die_checksum_ordered (die, &ctx, &mark);
6118 unmark_all_dies (die);
6119 md5_finish_ctx (&ctx, checksum);
6121 /* Store the signature in the type node and link the type DIE and the
6122 type node together. */
6123 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6124 DWARF_TYPE_SIGNATURE_SIZE);
6125 die->comdat_type_p = true;
6126 die->die_id.die_type_node = type_node;
6127 type_node->type_die = die;
6129 /* If the DIE is a specification, link its declaration to the type node
6130 as well. */
6131 if (decl != NULL)
6133 decl->comdat_type_p = true;
6134 decl->die_id.die_type_node = type_node;
6138 /* Do the location expressions look same? */
6139 static inline int
6140 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6142 return loc1->dw_loc_opc == loc2->dw_loc_opc
6143 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6144 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6147 /* Do the values look the same? */
6148 static int
6149 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6151 dw_loc_descr_ref loc1, loc2;
6152 rtx r1, r2;
6154 if (v1->val_class != v2->val_class)
6155 return 0;
6157 switch (v1->val_class)
6159 case dw_val_class_const:
6160 return v1->v.val_int == v2->v.val_int;
6161 case dw_val_class_unsigned_const:
6162 return v1->v.val_unsigned == v2->v.val_unsigned;
6163 case dw_val_class_const_double:
6164 return v1->v.val_double.high == v2->v.val_double.high
6165 && v1->v.val_double.low == v2->v.val_double.low;
6166 case dw_val_class_vec:
6167 if (v1->v.val_vec.length != v2->v.val_vec.length
6168 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6169 return 0;
6170 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6171 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6172 return 0;
6173 return 1;
6174 case dw_val_class_flag:
6175 return v1->v.val_flag == v2->v.val_flag;
6176 case dw_val_class_str:
6177 return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
6179 case dw_val_class_addr:
6180 r1 = v1->v.val_addr;
6181 r2 = v2->v.val_addr;
6182 if (GET_CODE (r1) != GET_CODE (r2))
6183 return 0;
6184 return !rtx_equal_p (r1, r2);
6186 case dw_val_class_offset:
6187 return v1->v.val_offset == v2->v.val_offset;
6189 case dw_val_class_loc:
6190 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6191 loc1 && loc2;
6192 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6193 if (!same_loc_p (loc1, loc2, mark))
6194 return 0;
6195 return !loc1 && !loc2;
6197 case dw_val_class_die_ref:
6198 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6200 case dw_val_class_fde_ref:
6201 case dw_val_class_vms_delta:
6202 case dw_val_class_lbl_id:
6203 case dw_val_class_lineptr:
6204 case dw_val_class_macptr:
6205 case dw_val_class_high_pc:
6206 return 1;
6208 case dw_val_class_file:
6209 return v1->v.val_file == v2->v.val_file;
6211 case dw_val_class_data8:
6212 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6214 default:
6215 return 1;
6219 /* Do the attributes look the same? */
6221 static int
6222 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6224 if (at1->dw_attr != at2->dw_attr)
6225 return 0;
6227 /* We don't care that this was compiled with a different compiler
6228 snapshot; if the output is the same, that's what matters. */
6229 if (at1->dw_attr == DW_AT_producer)
6230 return 1;
6232 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6235 /* Do the dies look the same? */
6237 static int
6238 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6240 dw_die_ref c1, c2;
6241 dw_attr_ref a1;
6242 unsigned ix;
6244 /* To avoid infinite recursion. */
6245 if (die1->die_mark)
6246 return die1->die_mark == die2->die_mark;
6247 die1->die_mark = die2->die_mark = ++(*mark);
6249 if (die1->die_tag != die2->die_tag)
6250 return 0;
6252 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6253 return 0;
6255 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6256 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6257 return 0;
6259 c1 = die1->die_child;
6260 c2 = die2->die_child;
6261 if (! c1)
6263 if (c2)
6264 return 0;
6266 else
6267 for (;;)
6269 if (!same_die_p (c1, c2, mark))
6270 return 0;
6271 c1 = c1->die_sib;
6272 c2 = c2->die_sib;
6273 if (c1 == die1->die_child)
6275 if (c2 == die2->die_child)
6276 break;
6277 else
6278 return 0;
6282 return 1;
6285 /* Do the dies look the same? Wrapper around same_die_p. */
6287 static int
6288 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6290 int mark = 0;
6291 int ret = same_die_p (die1, die2, &mark);
6293 unmark_all_dies (die1);
6294 unmark_all_dies (die2);
6296 return ret;
6299 /* The prefix to attach to symbols on DIEs in the current comdat debug
6300 info section. */
6301 static const char *comdat_symbol_id;
6303 /* The index of the current symbol within the current comdat CU. */
6304 static unsigned int comdat_symbol_number;
6306 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6307 children, and set comdat_symbol_id accordingly. */
6309 static void
6310 compute_section_prefix (dw_die_ref unit_die)
6312 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6313 const char *base = die_name ? lbasename (die_name) : "anonymous";
6314 char *name = XALLOCAVEC (char, strlen (base) + 64);
6315 char *p;
6316 int i, mark;
6317 unsigned char checksum[16];
6318 struct md5_ctx ctx;
6320 /* Compute the checksum of the DIE, then append part of it as hex digits to
6321 the name filename of the unit. */
6323 md5_init_ctx (&ctx);
6324 mark = 0;
6325 die_checksum (unit_die, &ctx, &mark);
6326 unmark_all_dies (unit_die);
6327 md5_finish_ctx (&ctx, checksum);
6329 sprintf (name, "%s.", base);
6330 clean_symbol_name (name);
6332 p = name + strlen (name);
6333 for (i = 0; i < 4; i++)
6335 sprintf (p, "%.2x", checksum[i]);
6336 p += 2;
6339 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6340 comdat_symbol_number = 0;
6343 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6345 static int
6346 is_type_die (dw_die_ref die)
6348 switch (die->die_tag)
6350 case DW_TAG_array_type:
6351 case DW_TAG_class_type:
6352 case DW_TAG_interface_type:
6353 case DW_TAG_enumeration_type:
6354 case DW_TAG_pointer_type:
6355 case DW_TAG_reference_type:
6356 case DW_TAG_rvalue_reference_type:
6357 case DW_TAG_string_type:
6358 case DW_TAG_structure_type:
6359 case DW_TAG_subroutine_type:
6360 case DW_TAG_union_type:
6361 case DW_TAG_ptr_to_member_type:
6362 case DW_TAG_set_type:
6363 case DW_TAG_subrange_type:
6364 case DW_TAG_base_type:
6365 case DW_TAG_const_type:
6366 case DW_TAG_file_type:
6367 case DW_TAG_packed_type:
6368 case DW_TAG_volatile_type:
6369 case DW_TAG_typedef:
6370 return 1;
6371 default:
6372 return 0;
6376 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6377 Basically, we want to choose the bits that are likely to be shared between
6378 compilations (types) and leave out the bits that are specific to individual
6379 compilations (functions). */
6381 static int
6382 is_comdat_die (dw_die_ref c)
6384 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6385 we do for stabs. The advantage is a greater likelihood of sharing between
6386 objects that don't include headers in the same order (and therefore would
6387 put the base types in a different comdat). jason 8/28/00 */
6389 if (c->die_tag == DW_TAG_base_type)
6390 return 0;
6392 if (c->die_tag == DW_TAG_pointer_type
6393 || c->die_tag == DW_TAG_reference_type
6394 || c->die_tag == DW_TAG_rvalue_reference_type
6395 || c->die_tag == DW_TAG_const_type
6396 || c->die_tag == DW_TAG_volatile_type)
6398 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6400 return t ? is_comdat_die (t) : 0;
6403 return is_type_die (c);
6406 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6407 compilation unit. */
6409 static int
6410 is_symbol_die (dw_die_ref c)
6412 return (is_type_die (c)
6413 || is_declaration_die (c)
6414 || c->die_tag == DW_TAG_namespace
6415 || c->die_tag == DW_TAG_module);
6418 /* Returns true iff C is a compile-unit DIE. */
6420 static inline bool
6421 is_cu_die (dw_die_ref c)
6423 return c && c->die_tag == DW_TAG_compile_unit;
6426 /* Returns true iff C is a unit DIE of some sort. */
6428 static inline bool
6429 is_unit_die (dw_die_ref c)
6431 return c && (c->die_tag == DW_TAG_compile_unit
6432 || c->die_tag == DW_TAG_partial_unit
6433 || c->die_tag == DW_TAG_type_unit);
6436 /* Returns true iff C is a namespace DIE. */
6438 static inline bool
6439 is_namespace_die (dw_die_ref c)
6441 return c && c->die_tag == DW_TAG_namespace;
6444 /* Returns true iff C is a class or structure DIE. */
6446 static inline bool
6447 is_class_die (dw_die_ref c)
6449 return c && (c->die_tag == DW_TAG_class_type
6450 || c->die_tag == DW_TAG_structure_type);
6453 static char *
6454 gen_internal_sym (const char *prefix)
6456 char buf[256];
6458 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6459 return xstrdup (buf);
6462 /* Assign symbols to all worthy DIEs under DIE. */
6464 static void
6465 assign_symbol_names (dw_die_ref die)
6467 dw_die_ref c;
6469 if (is_symbol_die (die) && !die->comdat_type_p)
6471 if (comdat_symbol_id)
6473 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6475 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6476 comdat_symbol_id, comdat_symbol_number++);
6477 die->die_id.die_symbol = xstrdup (p);
6479 else
6480 die->die_id.die_symbol = gen_internal_sym ("LDIE");
6483 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6486 struct cu_hash_table_entry
6488 dw_die_ref cu;
6489 unsigned min_comdat_num, max_comdat_num;
6490 struct cu_hash_table_entry *next;
6493 /* Routines to manipulate hash table of CUs. */
6494 static hashval_t
6495 htab_cu_hash (const void *of)
6497 const struct cu_hash_table_entry *const entry =
6498 (const struct cu_hash_table_entry *) of;
6500 return htab_hash_string (entry->cu->die_id.die_symbol);
6503 static int
6504 htab_cu_eq (const void *of1, const void *of2)
6506 const struct cu_hash_table_entry *const entry1 =
6507 (const struct cu_hash_table_entry *) of1;
6508 const struct die_struct *const entry2 = (const struct die_struct *) of2;
6510 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6513 static void
6514 htab_cu_del (void *what)
6516 struct cu_hash_table_entry *next,
6517 *entry = (struct cu_hash_table_entry *) what;
6519 while (entry)
6521 next = entry->next;
6522 free (entry);
6523 entry = next;
6527 /* Check whether we have already seen this CU and set up SYM_NUM
6528 accordingly. */
6529 static int
6530 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
6532 struct cu_hash_table_entry dummy;
6533 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6535 dummy.max_comdat_num = 0;
6537 slot = (struct cu_hash_table_entry **)
6538 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
6539 INSERT);
6540 entry = *slot;
6542 for (; entry; last = entry, entry = entry->next)
6544 if (same_die_p_wrap (cu, entry->cu))
6545 break;
6548 if (entry)
6550 *sym_num = entry->min_comdat_num;
6551 return 1;
6554 entry = XCNEW (struct cu_hash_table_entry);
6555 entry->cu = cu;
6556 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6557 entry->next = *slot;
6558 *slot = entry;
6560 return 0;
6563 /* Record SYM_NUM to record of CU in HTABLE. */
6564 static void
6565 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
6567 struct cu_hash_table_entry **slot, *entry;
6569 slot = (struct cu_hash_table_entry **)
6570 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
6571 NO_INSERT);
6572 entry = *slot;
6574 entry->max_comdat_num = sym_num;
6577 /* Traverse the DIE (which is always comp_unit_die), and set up
6578 additional compilation units for each of the include files we see
6579 bracketed by BINCL/EINCL. */
6581 static void
6582 break_out_includes (dw_die_ref die)
6584 dw_die_ref c;
6585 dw_die_ref unit = NULL;
6586 limbo_die_node *node, **pnode;
6587 htab_t cu_hash_table;
6589 c = die->die_child;
6590 if (c) do {
6591 dw_die_ref prev = c;
6592 c = c->die_sib;
6593 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6594 || (unit && is_comdat_die (c)))
6596 dw_die_ref next = c->die_sib;
6598 /* This DIE is for a secondary CU; remove it from the main one. */
6599 remove_child_with_prev (c, prev);
6601 if (c->die_tag == DW_TAG_GNU_BINCL)
6602 unit = push_new_compile_unit (unit, c);
6603 else if (c->die_tag == DW_TAG_GNU_EINCL)
6604 unit = pop_compile_unit (unit);
6605 else
6606 add_child_die (unit, c);
6607 c = next;
6608 if (c == die->die_child)
6609 break;
6611 } while (c != die->die_child);
6613 #if 0
6614 /* We can only use this in debugging, since the frontend doesn't check
6615 to make sure that we leave every include file we enter. */
6616 gcc_assert (!unit);
6617 #endif
6619 assign_symbol_names (die);
6620 cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6621 for (node = limbo_die_list, pnode = &limbo_die_list;
6622 node;
6623 node = node->next)
6625 int is_dupl;
6627 compute_section_prefix (node->die);
6628 is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6629 &comdat_symbol_number);
6630 assign_symbol_names (node->die);
6631 if (is_dupl)
6632 *pnode = node->next;
6633 else
6635 pnode = &node->next;
6636 record_comdat_symbol_number (node->die, cu_hash_table,
6637 comdat_symbol_number);
6640 htab_delete (cu_hash_table);
6643 /* Return non-zero if this DIE is a declaration. */
6645 static int
6646 is_declaration_die (dw_die_ref die)
6648 dw_attr_ref a;
6649 unsigned ix;
6651 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6652 if (a->dw_attr == DW_AT_declaration)
6653 return 1;
6655 return 0;
6658 /* Return non-zero if this DIE is nested inside a subprogram. */
6660 static int
6661 is_nested_in_subprogram (dw_die_ref die)
6663 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
6665 if (decl == NULL)
6666 decl = die;
6667 return local_scope_p (decl);
6670 /* Return non-zero if this DIE contains a defining declaration of a
6671 subprogram. */
6673 static int
6674 contains_subprogram_definition (dw_die_ref die)
6676 dw_die_ref c;
6678 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
6679 return 1;
6680 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition(c)) return 1);
6681 return 0;
6684 /* Return non-zero if this is a type DIE that should be moved to a
6685 COMDAT .debug_types section. */
6687 static int
6688 should_move_die_to_comdat (dw_die_ref die)
6690 switch (die->die_tag)
6692 case DW_TAG_class_type:
6693 case DW_TAG_structure_type:
6694 case DW_TAG_enumeration_type:
6695 case DW_TAG_union_type:
6696 /* Don't move declarations, inlined instances, or types nested in a
6697 subprogram. */
6698 if (is_declaration_die (die)
6699 || get_AT (die, DW_AT_abstract_origin)
6700 || is_nested_in_subprogram (die))
6701 return 0;
6702 /* A type definition should never contain a subprogram definition. */
6703 gcc_assert (!contains_subprogram_definition (die));
6704 return 1;
6705 case DW_TAG_array_type:
6706 case DW_TAG_interface_type:
6707 case DW_TAG_pointer_type:
6708 case DW_TAG_reference_type:
6709 case DW_TAG_rvalue_reference_type:
6710 case DW_TAG_string_type:
6711 case DW_TAG_subroutine_type:
6712 case DW_TAG_ptr_to_member_type:
6713 case DW_TAG_set_type:
6714 case DW_TAG_subrange_type:
6715 case DW_TAG_base_type:
6716 case DW_TAG_const_type:
6717 case DW_TAG_file_type:
6718 case DW_TAG_packed_type:
6719 case DW_TAG_volatile_type:
6720 case DW_TAG_typedef:
6721 default:
6722 return 0;
6726 /* Make a clone of DIE. */
6728 static dw_die_ref
6729 clone_die (dw_die_ref die)
6731 dw_die_ref clone;
6732 dw_attr_ref a;
6733 unsigned ix;
6735 clone = ggc_alloc_cleared_die_node ();
6736 clone->die_tag = die->die_tag;
6738 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6739 add_dwarf_attr (clone, a);
6741 return clone;
6744 /* Make a clone of the tree rooted at DIE. */
6746 static dw_die_ref
6747 clone_tree (dw_die_ref die)
6749 dw_die_ref c;
6750 dw_die_ref clone = clone_die (die);
6752 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
6754 return clone;
6757 /* Make a clone of DIE as a declaration. */
6759 static dw_die_ref
6760 clone_as_declaration (dw_die_ref die)
6762 dw_die_ref clone;
6763 dw_die_ref decl;
6764 dw_attr_ref a;
6765 unsigned ix;
6767 /* If the DIE is already a declaration, just clone it. */
6768 if (is_declaration_die (die))
6769 return clone_die (die);
6771 /* If the DIE is a specification, just clone its declaration DIE. */
6772 decl = get_AT_ref (die, DW_AT_specification);
6773 if (decl != NULL)
6775 clone = clone_die (decl);
6776 if (die->comdat_type_p)
6777 add_AT_die_ref (clone, DW_AT_signature, die);
6778 return clone;
6781 clone = ggc_alloc_cleared_die_node ();
6782 clone->die_tag = die->die_tag;
6784 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6786 /* We don't want to copy over all attributes.
6787 For example we don't want DW_AT_byte_size because otherwise we will no
6788 longer have a declaration and GDB will treat it as a definition. */
6790 switch (a->dw_attr)
6792 case DW_AT_artificial:
6793 case DW_AT_containing_type:
6794 case DW_AT_external:
6795 case DW_AT_name:
6796 case DW_AT_type:
6797 case DW_AT_virtuality:
6798 case DW_AT_linkage_name:
6799 case DW_AT_MIPS_linkage_name:
6800 add_dwarf_attr (clone, a);
6801 break;
6802 case DW_AT_byte_size:
6803 default:
6804 break;
6808 if (die->comdat_type_p)
6809 add_AT_die_ref (clone, DW_AT_signature, die);
6811 add_AT_flag (clone, DW_AT_declaration, 1);
6812 return clone;
6815 /* Copy the declaration context to the new type unit DIE. This includes
6816 any surrounding namespace or type declarations. If the DIE has an
6817 AT_specification attribute, it also includes attributes and children
6818 attached to the specification, and returns a pointer to the original
6819 parent of the declaration DIE. Returns NULL otherwise. */
6821 static dw_die_ref
6822 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
6824 dw_die_ref decl;
6825 dw_die_ref new_decl;
6826 dw_die_ref orig_parent = NULL;
6828 decl = get_AT_ref (die, DW_AT_specification);
6829 if (decl == NULL)
6830 decl = die;
6831 else
6833 unsigned ix;
6834 dw_die_ref c;
6835 dw_attr_ref a;
6837 /* The original DIE will be changed to a declaration, and must
6838 be moved to be a child of the original declaration DIE. */
6839 orig_parent = decl->die_parent;
6841 /* Copy the type node pointer from the new DIE to the original
6842 declaration DIE so we can forward references later. */
6843 decl->comdat_type_p = true;
6844 decl->die_id.die_type_node = die->die_id.die_type_node;
6846 remove_AT (die, DW_AT_specification);
6848 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
6850 if (a->dw_attr != DW_AT_name
6851 && a->dw_attr != DW_AT_declaration
6852 && a->dw_attr != DW_AT_external)
6853 add_dwarf_attr (die, a);
6856 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
6859 if (decl->die_parent != NULL
6860 && !is_unit_die (decl->die_parent))
6862 new_decl = copy_ancestor_tree (unit, decl, NULL);
6863 if (new_decl != NULL)
6865 remove_AT (new_decl, DW_AT_signature);
6866 add_AT_specification (die, new_decl);
6870 return orig_parent;
6873 /* Generate the skeleton ancestor tree for the given NODE, then clone
6874 the DIE and add the clone into the tree. */
6876 static void
6877 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
6879 if (node->new_die != NULL)
6880 return;
6882 node->new_die = clone_as_declaration (node->old_die);
6884 if (node->parent != NULL)
6886 generate_skeleton_ancestor_tree (node->parent);
6887 add_child_die (node->parent->new_die, node->new_die);
6891 /* Generate a skeleton tree of DIEs containing any declarations that are
6892 found in the original tree. We traverse the tree looking for declaration
6893 DIEs, and construct the skeleton from the bottom up whenever we find one. */
6895 static void
6896 generate_skeleton_bottom_up (skeleton_chain_node *parent)
6898 skeleton_chain_node node;
6899 dw_die_ref c;
6900 dw_die_ref first;
6901 dw_die_ref prev = NULL;
6902 dw_die_ref next = NULL;
6904 node.parent = parent;
6906 first = c = parent->old_die->die_child;
6907 if (c)
6908 next = c->die_sib;
6909 if (c) do {
6910 if (prev == NULL || prev->die_sib == c)
6911 prev = c;
6912 c = next;
6913 next = (c == first ? NULL : c->die_sib);
6914 node.old_die = c;
6915 node.new_die = NULL;
6916 if (is_declaration_die (c))
6918 /* Clone the existing DIE, move the original to the skeleton
6919 tree (which is in the main CU), and put the clone, with
6920 all the original's children, where the original came from. */
6921 dw_die_ref clone = clone_die (c);
6922 move_all_children (c, clone);
6924 replace_child (c, clone, prev);
6925 generate_skeleton_ancestor_tree (parent);
6926 add_child_die (parent->new_die, c);
6927 node.new_die = c;
6928 c = clone;
6930 generate_skeleton_bottom_up (&node);
6931 } while (next != NULL);
6934 /* Wrapper function for generate_skeleton_bottom_up. */
6936 static dw_die_ref
6937 generate_skeleton (dw_die_ref die)
6939 skeleton_chain_node node;
6941 node.old_die = die;
6942 node.new_die = NULL;
6943 node.parent = NULL;
6945 /* If this type definition is nested inside another type,
6946 always leave at least a declaration in its place. */
6947 if (die->die_parent != NULL && is_type_die (die->die_parent))
6948 node.new_die = clone_as_declaration (die);
6950 generate_skeleton_bottom_up (&node);
6951 return node.new_die;
6954 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
6955 declaration. The original DIE is moved to a new compile unit so that
6956 existing references to it follow it to the new location. If any of the
6957 original DIE's descendants is a declaration, we need to replace the
6958 original DIE with a skeleton tree and move the declarations back into the
6959 skeleton tree. */
6961 static dw_die_ref
6962 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
6963 dw_die_ref prev)
6965 dw_die_ref skeleton, orig_parent;
6967 /* Copy the declaration context to the type unit DIE. If the returned
6968 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
6969 that DIE. */
6970 orig_parent = copy_declaration_context (unit, child);
6972 skeleton = generate_skeleton (child);
6973 if (skeleton == NULL)
6974 remove_child_with_prev (child, prev);
6975 else
6977 skeleton->comdat_type_p = true;
6978 skeleton->die_id.die_type_node = child->die_id.die_type_node;
6980 /* If the original DIE was a specification, we need to put
6981 the skeleton under the parent DIE of the declaration.
6982 This leaves the original declaration in the tree, but
6983 it will be pruned later since there are no longer any
6984 references to it. */
6985 if (orig_parent != NULL)
6987 remove_child_with_prev (child, prev);
6988 add_child_die (orig_parent, skeleton);
6990 else
6991 replace_child (child, skeleton, prev);
6994 return skeleton;
6997 /* Traverse the DIE and set up additional .debug_types sections for each
6998 type worthy of being placed in a COMDAT section. */
7000 static void
7001 break_out_comdat_types (dw_die_ref die)
7003 dw_die_ref c;
7004 dw_die_ref first;
7005 dw_die_ref prev = NULL;
7006 dw_die_ref next = NULL;
7007 dw_die_ref unit = NULL;
7009 first = c = die->die_child;
7010 if (c)
7011 next = c->die_sib;
7012 if (c) do {
7013 if (prev == NULL || prev->die_sib == c)
7014 prev = c;
7015 c = next;
7016 next = (c == first ? NULL : c->die_sib);
7017 if (should_move_die_to_comdat (c))
7019 dw_die_ref replacement;
7020 comdat_type_node_ref type_node;
7022 /* Create a new type unit DIE as the root for the new tree, and
7023 add it to the list of comdat types. */
7024 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7025 add_AT_unsigned (unit, DW_AT_language,
7026 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7027 type_node = ggc_alloc_cleared_comdat_type_node ();
7028 type_node->root_die = unit;
7029 type_node->next = comdat_type_list;
7030 comdat_type_list = type_node;
7032 /* Generate the type signature. */
7033 generate_type_signature (c, type_node);
7035 /* Copy the declaration context, attributes, and children of the
7036 declaration into the new type unit DIE, then remove this DIE
7037 from the main CU (or replace it with a skeleton if necessary). */
7038 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7039 type_node->skeleton_die = replacement;
7041 /* Break out nested types into their own type units. */
7042 break_out_comdat_types (c);
7044 /* Add the DIE to the new compunit. */
7045 add_child_die (unit, c);
7047 if (replacement != NULL)
7048 c = replacement;
7050 else if (c->die_tag == DW_TAG_namespace
7051 || c->die_tag == DW_TAG_class_type
7052 || c->die_tag == DW_TAG_structure_type
7053 || c->die_tag == DW_TAG_union_type)
7055 /* Look for nested types that can be broken out. */
7056 break_out_comdat_types (c);
7058 } while (next != NULL);
7061 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7063 struct decl_table_entry
7065 dw_die_ref orig;
7066 dw_die_ref copy;
7069 /* Routines to manipulate hash table of copied declarations. */
7071 static hashval_t
7072 htab_decl_hash (const void *of)
7074 const struct decl_table_entry *const entry =
7075 (const struct decl_table_entry *) of;
7077 return htab_hash_pointer (entry->orig);
7080 static int
7081 htab_decl_eq (const void *of1, const void *of2)
7083 const struct decl_table_entry *const entry1 =
7084 (const struct decl_table_entry *) of1;
7085 const struct die_struct *const entry2 = (const struct die_struct *) of2;
7087 return entry1->orig == entry2;
7090 static void
7091 htab_decl_del (void *what)
7093 struct decl_table_entry *entry = (struct decl_table_entry *) what;
7095 free (entry);
7098 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7099 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7100 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7101 to check if the ancestor has already been copied into UNIT. */
7103 static dw_die_ref
7104 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
7106 dw_die_ref parent = die->die_parent;
7107 dw_die_ref new_parent = unit;
7108 dw_die_ref copy;
7109 void **slot = NULL;
7110 struct decl_table_entry *entry = NULL;
7112 if (decl_table)
7114 /* Check if the entry has already been copied to UNIT. */
7115 slot = htab_find_slot_with_hash (decl_table, die,
7116 htab_hash_pointer (die), INSERT);
7117 if (*slot != HTAB_EMPTY_ENTRY)
7119 entry = (struct decl_table_entry *) *slot;
7120 return entry->copy;
7123 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7124 entry = XCNEW (struct decl_table_entry);
7125 entry->orig = die;
7126 entry->copy = NULL;
7127 *slot = entry;
7130 if (parent != NULL)
7132 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7133 if (spec != NULL)
7134 parent = spec;
7135 if (!is_unit_die (parent))
7136 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7139 copy = clone_as_declaration (die);
7140 add_child_die (new_parent, copy);
7142 if (decl_table != NULL)
7144 /* Record the pointer to the copy. */
7145 entry->copy = copy;
7148 return copy;
7151 /* Like clone_tree, but additionally enter all the children into
7152 the hash table decl_table. */
7154 static dw_die_ref
7155 clone_tree_hash (dw_die_ref die, htab_t decl_table)
7157 dw_die_ref c;
7158 dw_die_ref clone = clone_die (die);
7159 struct decl_table_entry *entry;
7160 void **slot = htab_find_slot_with_hash (decl_table, die,
7161 htab_hash_pointer (die), INSERT);
7162 /* Assert that DIE isn't in the hash table yet. If it would be there
7163 before, the ancestors would be necessarily there as well, therefore
7164 clone_tree_hash wouldn't be called. */
7165 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7166 entry = XCNEW (struct decl_table_entry);
7167 entry->orig = die;
7168 entry->copy = clone;
7169 *slot = entry;
7171 FOR_EACH_CHILD (die, c,
7172 add_child_die (clone, clone_tree_hash (c, decl_table)));
7174 return clone;
7177 /* Walk the DIE and its children, looking for references to incomplete
7178 or trivial types that are unmarked (i.e., that are not in the current
7179 type_unit). */
7181 static void
7182 copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
7184 dw_die_ref c;
7185 dw_attr_ref a;
7186 unsigned ix;
7188 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7190 if (AT_class (a) == dw_val_class_die_ref)
7192 dw_die_ref targ = AT_ref (a);
7193 void **slot;
7194 struct decl_table_entry *entry;
7196 if (targ->die_mark != 0 || targ->comdat_type_p)
7197 continue;
7199 slot = htab_find_slot_with_hash (decl_table, targ,
7200 htab_hash_pointer (targ), INSERT);
7202 if (*slot != HTAB_EMPTY_ENTRY)
7204 /* TARG has already been copied, so we just need to
7205 modify the reference to point to the copy. */
7206 entry = (struct decl_table_entry *) *slot;
7207 a->dw_attr_val.v.val_die_ref.die = entry->copy;
7209 else
7211 dw_die_ref parent = unit;
7212 dw_die_ref copy = clone_die (targ);
7214 /* Record in DECL_TABLE that TARG has been copied.
7215 Need to do this now, before the recursive call,
7216 because DECL_TABLE may be expanded and SLOT
7217 would no longer be a valid pointer. */
7218 entry = XCNEW (struct decl_table_entry);
7219 entry->orig = targ;
7220 entry->copy = copy;
7221 *slot = entry;
7223 FOR_EACH_CHILD (targ, c,
7224 add_child_die (copy,
7225 clone_tree_hash (c, decl_table)));
7227 /* Make sure the cloned tree is marked as part of the
7228 type unit. */
7229 mark_dies (copy);
7231 /* If TARG has surrounding context, copy its ancestor tree
7232 into the new type unit. */
7233 if (targ->die_parent != NULL
7234 && !is_unit_die (targ->die_parent))
7235 parent = copy_ancestor_tree (unit, targ->die_parent,
7236 decl_table);
7238 add_child_die (parent, copy);
7239 a->dw_attr_val.v.val_die_ref.die = copy;
7241 /* Make sure the newly-copied DIE is walked. If it was
7242 installed in a previously-added context, it won't
7243 get visited otherwise. */
7244 if (parent != unit)
7246 /* Find the highest point of the newly-added tree,
7247 mark each node along the way, and walk from there. */
7248 parent->die_mark = 1;
7249 while (parent->die_parent
7250 && parent->die_parent->die_mark == 0)
7252 parent = parent->die_parent;
7253 parent->die_mark = 1;
7255 copy_decls_walk (unit, parent, decl_table);
7261 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7264 /* Copy declarations for "unworthy" types into the new comdat section.
7265 Incomplete types, modified types, and certain other types aren't broken
7266 out into comdat sections of their own, so they don't have a signature,
7267 and we need to copy the declaration into the same section so that we
7268 don't have an external reference. */
7270 static void
7271 copy_decls_for_unworthy_types (dw_die_ref unit)
7273 htab_t decl_table;
7275 mark_dies (unit);
7276 decl_table = htab_create (10, htab_decl_hash, htab_decl_eq, htab_decl_del);
7277 copy_decls_walk (unit, unit, decl_table);
7278 htab_delete (decl_table);
7279 unmark_dies (unit);
7282 /* Traverse the DIE and add a sibling attribute if it may have the
7283 effect of speeding up access to siblings. To save some space,
7284 avoid generating sibling attributes for DIE's without children. */
7286 static void
7287 add_sibling_attributes (dw_die_ref die)
7289 dw_die_ref c;
7291 if (! die->die_child)
7292 return;
7294 if (die->die_parent && die != die->die_parent->die_child)
7295 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7297 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7300 /* Output all location lists for the DIE and its children. */
7302 static void
7303 output_location_lists (dw_die_ref die)
7305 dw_die_ref c;
7306 dw_attr_ref a;
7307 unsigned ix;
7309 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7310 if (AT_class (a) == dw_val_class_loc_list)
7311 output_loc_list (AT_loc_list (a));
7313 FOR_EACH_CHILD (die, c, output_location_lists (c));
7316 /* We want to limit the number of external references, because they are
7317 larger than local references: a relocation takes multiple words, and
7318 even a sig8 reference is always eight bytes, whereas a local reference
7319 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7320 So if we encounter multiple external references to the same type DIE, we
7321 make a local typedef stub for it and redirect all references there.
7323 This is the element of the hash table for keeping track of these
7324 references. */
7326 struct external_ref
7328 dw_die_ref type;
7329 dw_die_ref stub;
7330 unsigned n_refs;
7333 /* Hash an external_ref. */
7335 static hashval_t
7336 hash_external_ref (const void *p)
7338 const struct external_ref *r = (const struct external_ref *)p;
7339 return htab_hash_pointer (r->type);
7342 /* Compare external_refs. */
7344 static int
7345 external_ref_eq (const void *p1, const void *p2)
7347 const struct external_ref *r1 = (const struct external_ref *)p1;
7348 const struct external_ref *r2 = (const struct external_ref *)p2;
7349 return r1->type == r2->type;
7352 /* Return a pointer to the external_ref for references to DIE. */
7354 static struct external_ref *
7355 lookup_external_ref (htab_t map, dw_die_ref die)
7357 struct external_ref ref, *ref_p;
7358 void ** slot;
7360 ref.type = die;
7361 slot = htab_find_slot (map, &ref, INSERT);
7362 if (*slot != HTAB_EMPTY_ENTRY)
7363 return (struct external_ref *) *slot;
7365 ref_p = XCNEW (struct external_ref);
7366 ref_p->type = die;
7367 *slot = ref_p;
7368 return ref_p;
7371 /* Subroutine of optimize_external_refs, below.
7373 If we see a type skeleton, record it as our stub. If we see external
7374 references, remember how many we've seen. */
7376 static void
7377 optimize_external_refs_1 (dw_die_ref die, htab_t map)
7379 dw_die_ref c;
7380 dw_attr_ref a;
7381 unsigned ix;
7382 struct external_ref *ref_p;
7384 if (is_type_die (die)
7385 && (c = get_AT_ref (die, DW_AT_signature)))
7387 /* This is a local skeleton; use it for local references. */
7388 ref_p = lookup_external_ref (map, c);
7389 ref_p->stub = die;
7392 /* Scan the DIE references, and remember any that refer to DIEs from
7393 other CUs (i.e. those which are not marked). */
7394 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7395 if (AT_class (a) == dw_val_class_die_ref
7396 && (c = AT_ref (a))->die_mark == 0
7397 && is_type_die (c))
7399 ref_p = lookup_external_ref (map, c);
7400 ref_p->n_refs++;
7403 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7406 /* htab_traverse callback function for optimize_external_refs, below. SLOT
7407 points to an external_ref, DATA is the CU we're processing. If we don't
7408 already have a local stub, and we have multiple refs, build a stub. */
7410 static int
7411 build_local_stub (void **slot, void *data)
7413 struct external_ref *ref_p = (struct external_ref *)*slot;
7415 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7417 /* We have multiple references to this type, so build a small stub.
7418 Both of these forms are a bit dodgy from the perspective of the
7419 DWARF standard, since technically they should have names. */
7420 dw_die_ref cu = (dw_die_ref) data;
7421 dw_die_ref type = ref_p->type;
7422 dw_die_ref stub = NULL;
7424 if (type->comdat_type_p)
7426 /* If we refer to this type via sig8, use AT_signature. */
7427 stub = new_die (type->die_tag, cu, NULL_TREE);
7428 add_AT_die_ref (stub, DW_AT_signature, type);
7430 else
7432 /* Otherwise, use a typedef with no name. */
7433 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7434 add_AT_die_ref (stub, DW_AT_type, type);
7437 stub->die_mark++;
7438 ref_p->stub = stub;
7440 return 1;
7443 /* DIE is a unit; look through all the DIE references to see if there are
7444 any external references to types, and if so, create local stubs for
7445 them which will be applied in build_abbrev_table. This is useful because
7446 references to local DIEs are smaller. */
7448 static htab_t
7449 optimize_external_refs (dw_die_ref die)
7451 htab_t map = htab_create (10, hash_external_ref, external_ref_eq, free);
7452 optimize_external_refs_1 (die, map);
7453 htab_traverse (map, build_local_stub, die);
7454 return map;
7457 /* The format of each DIE (and its attribute value pairs) is encoded in an
7458 abbreviation table. This routine builds the abbreviation table and assigns
7459 a unique abbreviation id for each abbreviation entry. The children of each
7460 die are visited recursively. */
7462 static void
7463 build_abbrev_table (dw_die_ref die, htab_t extern_map)
7465 unsigned long abbrev_id;
7466 unsigned int n_alloc;
7467 dw_die_ref c;
7468 dw_attr_ref a;
7469 unsigned ix;
7471 /* Scan the DIE references, and replace any that refer to
7472 DIEs from other CUs (i.e. those which are not marked) with
7473 the local stubs we built in optimize_external_refs. */
7474 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7475 if (AT_class (a) == dw_val_class_die_ref
7476 && (c = AT_ref (a))->die_mark == 0)
7478 struct external_ref *ref_p;
7479 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
7481 ref_p = lookup_external_ref (extern_map, c);
7482 if (ref_p->stub && ref_p->stub != die)
7483 change_AT_die_ref (a, ref_p->stub);
7484 else
7485 /* We aren't changing this reference, so mark it external. */
7486 set_AT_ref_external (a, 1);
7489 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7491 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7492 dw_attr_ref die_a, abbrev_a;
7493 unsigned ix;
7494 bool ok = true;
7496 if (abbrev->die_tag != die->die_tag)
7497 continue;
7498 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7499 continue;
7501 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
7502 continue;
7504 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
7506 abbrev_a = &(*abbrev->die_attr)[ix];
7507 if ((abbrev_a->dw_attr != die_a->dw_attr)
7508 || (value_format (abbrev_a) != value_format (die_a)))
7510 ok = false;
7511 break;
7514 if (ok)
7515 break;
7518 if (abbrev_id >= abbrev_die_table_in_use)
7520 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7522 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7523 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7524 n_alloc);
7526 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7527 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7528 abbrev_die_table_allocated = n_alloc;
7531 ++abbrev_die_table_in_use;
7532 abbrev_die_table[abbrev_id] = die;
7535 die->die_abbrev = abbrev_id;
7536 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
7539 /* Return the power-of-two number of bytes necessary to represent VALUE. */
7541 static int
7542 constant_size (unsigned HOST_WIDE_INT value)
7544 int log;
7546 if (value == 0)
7547 log = 0;
7548 else
7549 log = floor_log2 (value);
7551 log = log / 8;
7552 log = 1 << (floor_log2 (log) + 1);
7554 return log;
7557 /* Return the size of a DIE as it is represented in the
7558 .debug_info section. */
7560 static unsigned long
7561 size_of_die (dw_die_ref die)
7563 unsigned long size = 0;
7564 dw_attr_ref a;
7565 unsigned ix;
7566 enum dwarf_form form;
7568 size += size_of_uleb128 (die->die_abbrev);
7569 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7571 switch (AT_class (a))
7573 case dw_val_class_addr:
7574 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7576 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7577 size += size_of_uleb128 (AT_index (a));
7579 else
7580 size += DWARF2_ADDR_SIZE;
7581 break;
7582 case dw_val_class_offset:
7583 size += DWARF_OFFSET_SIZE;
7584 break;
7585 case dw_val_class_loc:
7587 unsigned long lsize = size_of_locs (AT_loc (a));
7589 /* Block length. */
7590 if (dwarf_version >= 4)
7591 size += size_of_uleb128 (lsize);
7592 else
7593 size += constant_size (lsize);
7594 size += lsize;
7596 break;
7597 case dw_val_class_loc_list:
7598 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7600 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7601 size += size_of_uleb128 (AT_index (a));
7603 else
7604 size += DWARF_OFFSET_SIZE;
7605 break;
7606 case dw_val_class_range_list:
7607 size += DWARF_OFFSET_SIZE;
7608 break;
7609 case dw_val_class_const:
7610 size += size_of_sleb128 (AT_int (a));
7611 break;
7612 case dw_val_class_unsigned_const:
7614 int csize = constant_size (AT_unsigned (a));
7615 if (dwarf_version == 3
7616 && a->dw_attr == DW_AT_data_member_location
7617 && csize >= 4)
7618 size += size_of_uleb128 (AT_unsigned (a));
7619 else
7620 size += csize;
7622 break;
7623 case dw_val_class_const_double:
7624 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
7625 if (HOST_BITS_PER_WIDE_INT >= 64)
7626 size++; /* block */
7627 break;
7628 case dw_val_class_vec:
7629 size += constant_size (a->dw_attr_val.v.val_vec.length
7630 * a->dw_attr_val.v.val_vec.elt_size)
7631 + a->dw_attr_val.v.val_vec.length
7632 * a->dw_attr_val.v.val_vec.elt_size; /* block */
7633 break;
7634 case dw_val_class_flag:
7635 if (dwarf_version >= 4)
7636 /* Currently all add_AT_flag calls pass in 1 as last argument,
7637 so DW_FORM_flag_present can be used. If that ever changes,
7638 we'll need to use DW_FORM_flag and have some optimization
7639 in build_abbrev_table that will change those to
7640 DW_FORM_flag_present if it is set to 1 in all DIEs using
7641 the same abbrev entry. */
7642 gcc_assert (a->dw_attr_val.v.val_flag == 1);
7643 else
7644 size += 1;
7645 break;
7646 case dw_val_class_die_ref:
7647 if (AT_ref_external (a))
7649 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
7650 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
7651 is sized by target address length, whereas in DWARF3
7652 it's always sized as an offset. */
7653 if (use_debug_types)
7654 size += DWARF_TYPE_SIGNATURE_SIZE;
7655 else if (dwarf_version == 2)
7656 size += DWARF2_ADDR_SIZE;
7657 else
7658 size += DWARF_OFFSET_SIZE;
7660 else
7661 size += DWARF_OFFSET_SIZE;
7662 break;
7663 case dw_val_class_fde_ref:
7664 size += DWARF_OFFSET_SIZE;
7665 break;
7666 case dw_val_class_lbl_id:
7667 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7669 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7670 size += size_of_uleb128 (AT_index (a));
7672 else
7673 size += DWARF2_ADDR_SIZE;
7674 break;
7675 case dw_val_class_lineptr:
7676 case dw_val_class_macptr:
7677 size += DWARF_OFFSET_SIZE;
7678 break;
7679 case dw_val_class_str:
7680 form = AT_string_form (a);
7681 if (form == DW_FORM_strp)
7682 size += DWARF_OFFSET_SIZE;
7683 else if (form == DW_FORM_GNU_str_index)
7684 size += size_of_uleb128 (AT_index (a));
7685 else
7686 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7687 break;
7688 case dw_val_class_file:
7689 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7690 break;
7691 case dw_val_class_data8:
7692 size += 8;
7693 break;
7694 case dw_val_class_vms_delta:
7695 size += DWARF_OFFSET_SIZE;
7696 break;
7697 case dw_val_class_high_pc:
7698 size += DWARF2_ADDR_SIZE;
7699 break;
7700 default:
7701 gcc_unreachable ();
7705 return size;
7708 /* Size the debugging information associated with a given DIE. Visits the
7709 DIE's children recursively. Updates the global variable next_die_offset, on
7710 each time through. Uses the current value of next_die_offset to update the
7711 die_offset field in each DIE. */
7713 static void
7714 calc_die_sizes (dw_die_ref die)
7716 dw_die_ref c;
7718 gcc_assert (die->die_offset == 0
7719 || (unsigned long int) die->die_offset == next_die_offset);
7720 die->die_offset = next_die_offset;
7721 next_die_offset += size_of_die (die);
7723 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
7725 if (die->die_child != NULL)
7726 /* Count the null byte used to terminate sibling lists. */
7727 next_die_offset += 1;
7730 /* Size just the base type children at the start of the CU.
7731 This is needed because build_abbrev needs to size locs
7732 and sizing of type based stack ops needs to know die_offset
7733 values for the base types. */
7735 static void
7736 calc_base_type_die_sizes (void)
7738 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7739 unsigned int i;
7740 dw_die_ref base_type;
7741 #if ENABLE_ASSERT_CHECKING
7742 dw_die_ref prev = comp_unit_die ()->die_child;
7743 #endif
7745 die_offset += size_of_die (comp_unit_die ());
7746 for (i = 0; base_types.iterate (i, &base_type); i++)
7748 #if ENABLE_ASSERT_CHECKING
7749 gcc_assert (base_type->die_offset == 0
7750 && prev->die_sib == base_type
7751 && base_type->die_child == NULL
7752 && base_type->die_abbrev);
7753 prev = base_type;
7754 #endif
7755 base_type->die_offset = die_offset;
7756 die_offset += size_of_die (base_type);
7760 /* Set the marks for a die and its children. We do this so
7761 that we know whether or not a reference needs to use FORM_ref_addr; only
7762 DIEs in the same CU will be marked. We used to clear out the offset
7763 and use that as the flag, but ran into ordering problems. */
7765 static void
7766 mark_dies (dw_die_ref die)
7768 dw_die_ref c;
7770 gcc_assert (!die->die_mark);
7772 die->die_mark = 1;
7773 FOR_EACH_CHILD (die, c, mark_dies (c));
7776 /* Clear the marks for a die and its children. */
7778 static void
7779 unmark_dies (dw_die_ref die)
7781 dw_die_ref c;
7783 if (! use_debug_types)
7784 gcc_assert (die->die_mark);
7786 die->die_mark = 0;
7787 FOR_EACH_CHILD (die, c, unmark_dies (c));
7790 /* Clear the marks for a die, its children and referred dies. */
7792 static void
7793 unmark_all_dies (dw_die_ref die)
7795 dw_die_ref c;
7796 dw_attr_ref a;
7797 unsigned ix;
7799 if (!die->die_mark)
7800 return;
7801 die->die_mark = 0;
7803 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
7805 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7806 if (AT_class (a) == dw_val_class_die_ref)
7807 unmark_all_dies (AT_ref (a));
7810 /* Return the size of the .debug_pubnames or .debug_pubtypes table
7811 generated for the compilation unit. */
7813 static unsigned long
7814 size_of_pubnames (vec<pubname_entry, va_gc> *names)
7816 unsigned long size;
7817 unsigned i;
7818 pubname_ref p;
7820 size = DWARF_PUBNAMES_HEADER_SIZE;
7821 FOR_EACH_VEC_ELT (*names, i, p)
7822 if (names != pubtype_table
7823 || p->die->die_offset != 0
7824 || !flag_eliminate_unused_debug_types)
7825 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
7827 size += DWARF_OFFSET_SIZE;
7828 return size;
7831 /* Return the size of the information in the .debug_aranges section. */
7833 static unsigned long
7834 size_of_aranges (void)
7836 unsigned long size;
7838 size = DWARF_ARANGES_HEADER_SIZE;
7840 /* Count the address/length pair for this compilation unit. */
7841 if (text_section_used)
7842 size += 2 * DWARF2_ADDR_SIZE;
7843 if (cold_text_section_used)
7844 size += 2 * DWARF2_ADDR_SIZE;
7845 if (have_multiple_function_sections)
7847 unsigned fde_idx;
7848 dw_fde_ref fde;
7850 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
7852 if (!fde->in_std_section)
7853 size += 2 * DWARF2_ADDR_SIZE;
7854 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
7855 size += 2 * DWARF2_ADDR_SIZE;
7859 /* Count the two zero words used to terminated the address range table. */
7860 size += 2 * DWARF2_ADDR_SIZE;
7861 return size;
7864 /* Select the encoding of an attribute value. */
7866 static enum dwarf_form
7867 value_format (dw_attr_ref a)
7869 switch (AT_class (a))
7871 case dw_val_class_addr:
7872 /* Only very few attributes allow DW_FORM_addr. */
7873 switch (a->dw_attr)
7875 case DW_AT_low_pc:
7876 case DW_AT_high_pc:
7877 case DW_AT_entry_pc:
7878 case DW_AT_trampoline:
7879 return (AT_index (a) == NOT_INDEXED
7880 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
7881 default:
7882 break;
7884 switch (DWARF2_ADDR_SIZE)
7886 case 1:
7887 return DW_FORM_data1;
7888 case 2:
7889 return DW_FORM_data2;
7890 case 4:
7891 return DW_FORM_data4;
7892 case 8:
7893 return DW_FORM_data8;
7894 default:
7895 gcc_unreachable ();
7897 case dw_val_class_range_list:
7898 case dw_val_class_loc_list:
7899 if (dwarf_version >= 4)
7900 return DW_FORM_sec_offset;
7901 /* FALLTHRU */
7902 case dw_val_class_vms_delta:
7903 case dw_val_class_offset:
7904 switch (DWARF_OFFSET_SIZE)
7906 case 4:
7907 return DW_FORM_data4;
7908 case 8:
7909 return DW_FORM_data8;
7910 default:
7911 gcc_unreachable ();
7913 case dw_val_class_loc:
7914 if (dwarf_version >= 4)
7915 return DW_FORM_exprloc;
7916 switch (constant_size (size_of_locs (AT_loc (a))))
7918 case 1:
7919 return DW_FORM_block1;
7920 case 2:
7921 return DW_FORM_block2;
7922 case 4:
7923 return DW_FORM_block4;
7924 default:
7925 gcc_unreachable ();
7927 case dw_val_class_const:
7928 return DW_FORM_sdata;
7929 case dw_val_class_unsigned_const:
7930 switch (constant_size (AT_unsigned (a)))
7932 case 1:
7933 return DW_FORM_data1;
7934 case 2:
7935 return DW_FORM_data2;
7936 case 4:
7937 /* In DWARF3 DW_AT_data_member_location with
7938 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
7939 constant, so we need to use DW_FORM_udata if we need
7940 a large constant. */
7941 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
7942 return DW_FORM_udata;
7943 return DW_FORM_data4;
7944 case 8:
7945 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
7946 return DW_FORM_udata;
7947 return DW_FORM_data8;
7948 default:
7949 gcc_unreachable ();
7951 case dw_val_class_const_double:
7952 switch (HOST_BITS_PER_WIDE_INT)
7954 case 8:
7955 return DW_FORM_data2;
7956 case 16:
7957 return DW_FORM_data4;
7958 case 32:
7959 return DW_FORM_data8;
7960 case 64:
7961 default:
7962 return DW_FORM_block1;
7964 case dw_val_class_vec:
7965 switch (constant_size (a->dw_attr_val.v.val_vec.length
7966 * a->dw_attr_val.v.val_vec.elt_size))
7968 case 1:
7969 return DW_FORM_block1;
7970 case 2:
7971 return DW_FORM_block2;
7972 case 4:
7973 return DW_FORM_block4;
7974 default:
7975 gcc_unreachable ();
7977 case dw_val_class_flag:
7978 if (dwarf_version >= 4)
7980 /* Currently all add_AT_flag calls pass in 1 as last argument,
7981 so DW_FORM_flag_present can be used. If that ever changes,
7982 we'll need to use DW_FORM_flag and have some optimization
7983 in build_abbrev_table that will change those to
7984 DW_FORM_flag_present if it is set to 1 in all DIEs using
7985 the same abbrev entry. */
7986 gcc_assert (a->dw_attr_val.v.val_flag == 1);
7987 return DW_FORM_flag_present;
7989 return DW_FORM_flag;
7990 case dw_val_class_die_ref:
7991 if (AT_ref_external (a))
7992 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
7993 else
7994 return DW_FORM_ref;
7995 case dw_val_class_fde_ref:
7996 return DW_FORM_data;
7997 case dw_val_class_lbl_id:
7998 return (AT_index (a) == NOT_INDEXED
7999 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8000 case dw_val_class_lineptr:
8001 case dw_val_class_macptr:
8002 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8003 case dw_val_class_str:
8004 return AT_string_form (a);
8005 case dw_val_class_file:
8006 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8008 case 1:
8009 return DW_FORM_data1;
8010 case 2:
8011 return DW_FORM_data2;
8012 case 4:
8013 return DW_FORM_data4;
8014 default:
8015 gcc_unreachable ();
8018 case dw_val_class_data8:
8019 return DW_FORM_data8;
8021 case dw_val_class_high_pc:
8022 switch (DWARF2_ADDR_SIZE)
8024 case 1:
8025 return DW_FORM_data1;
8026 case 2:
8027 return DW_FORM_data2;
8028 case 4:
8029 return DW_FORM_data4;
8030 case 8:
8031 return DW_FORM_data8;
8032 default:
8033 gcc_unreachable ();
8036 default:
8037 gcc_unreachable ();
8041 /* Output the encoding of an attribute value. */
8043 static void
8044 output_value_format (dw_attr_ref a)
8046 enum dwarf_form form = value_format (a);
8048 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8051 /* Given a die and id, produce the appropriate abbreviations. */
8053 static void
8054 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8056 unsigned ix;
8057 dw_attr_ref a_attr;
8059 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8060 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8061 dwarf_tag_name (abbrev->die_tag));
8063 if (abbrev->die_child != NULL)
8064 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8065 else
8066 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8068 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8070 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8071 dwarf_attr_name (a_attr->dw_attr));
8072 output_value_format (a_attr);
8075 dw2_asm_output_data (1, 0, NULL);
8076 dw2_asm_output_data (1, 0, NULL);
8080 /* Output the .debug_abbrev section which defines the DIE abbreviation
8081 table. */
8083 static void
8084 output_abbrev_section (void)
8086 unsigned long abbrev_id;
8088 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8089 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8091 /* Terminate the table. */
8092 dw2_asm_output_data (1, 0, NULL);
8095 /* Output a symbol we can use to refer to this DIE from another CU. */
8097 static inline void
8098 output_die_symbol (dw_die_ref die)
8100 const char *sym = die->die_id.die_symbol;
8102 gcc_assert (!die->comdat_type_p);
8104 if (sym == 0)
8105 return;
8107 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8108 /* We make these global, not weak; if the target doesn't support
8109 .linkonce, it doesn't support combining the sections, so debugging
8110 will break. */
8111 targetm.asm_out.globalize_label (asm_out_file, sym);
8113 ASM_OUTPUT_LABEL (asm_out_file, sym);
8116 /* Return a new location list, given the begin and end range, and the
8117 expression. */
8119 static inline dw_loc_list_ref
8120 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8121 const char *section)
8123 dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
8125 retlist->begin = begin;
8126 retlist->begin_entry = NULL;
8127 retlist->end = end;
8128 retlist->expr = expr;
8129 retlist->section = section;
8131 return retlist;
8134 /* Generate a new internal symbol for this location list node, if it
8135 hasn't got one yet. */
8137 static inline void
8138 gen_llsym (dw_loc_list_ref list)
8140 gcc_assert (!list->ll_symbol);
8141 list->ll_symbol = gen_internal_sym ("LLST");
8144 /* Output the location list given to us. */
8146 static void
8147 output_loc_list (dw_loc_list_ref list_head)
8149 dw_loc_list_ref curr = list_head;
8151 if (list_head->emitted)
8152 return;
8153 list_head->emitted = true;
8155 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8157 /* Walk the location list, and output each range + expression. */
8158 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8160 unsigned long size;
8161 /* Don't output an entry that starts and ends at the same address. */
8162 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8163 continue;
8164 size = size_of_locs (curr->expr);
8165 /* If the expression is too large, drop it on the floor. We could
8166 perhaps put it into DW_TAG_dwarf_procedure and refer to that
8167 in the expression, but >= 64KB expressions for a single value
8168 in a single range are unlikely very useful. */
8169 if (size > 0xffff)
8170 continue;
8171 if (dwarf_split_debug_info)
8173 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8174 "Location list start/length entry (%s)",
8175 list_head->ll_symbol);
8176 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8177 "Location list range start index (%s)",
8178 curr->begin);
8179 /* The length field is 4 bytes. If we ever need to support
8180 an 8-byte length, we can add a new DW_LLE code or fall back
8181 to DW_LLE_GNU_start_end_entry. */
8182 dw2_asm_output_delta (4, curr->end, curr->begin,
8183 "Location list range length (%s)",
8184 list_head->ll_symbol);
8186 else if (!have_multiple_function_sections)
8188 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8189 "Location list begin address (%s)",
8190 list_head->ll_symbol);
8191 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8192 "Location list end address (%s)",
8193 list_head->ll_symbol);
8195 else
8197 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8198 "Location list begin address (%s)",
8199 list_head->ll_symbol);
8200 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8201 "Location list end address (%s)",
8202 list_head->ll_symbol);
8205 /* Output the block length for this list of location operations. */
8206 gcc_assert (size <= 0xffff);
8207 dw2_asm_output_data (2, size, "%s", "Location expression size");
8209 output_loc_sequence (curr->expr, -1);
8212 if (dwarf_split_debug_info)
8213 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8214 "Location list terminator (%s)",
8215 list_head->ll_symbol);
8216 else
8218 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8219 "Location list terminator begin (%s)",
8220 list_head->ll_symbol);
8221 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8222 "Location list terminator end (%s)",
8223 list_head->ll_symbol);
8227 /* Output a range_list offset into the debug_range section. Emit a
8228 relocated reference if val_entry is NULL, otherwise, emit an
8229 indirect reference. */
8231 static void
8232 output_range_list_offset (dw_attr_ref a)
8234 const char *name = dwarf_attr_name (a->dw_attr);
8236 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8238 char *p = strchr (ranges_section_label, '\0');
8239 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8240 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8241 debug_ranges_section, "%s", name);
8242 *p = '\0';
8244 else
8245 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8246 "%s (offset from %s)", name, ranges_section_label);
8249 /* Output the offset into the debug_loc section. */
8251 static void
8252 output_loc_list_offset (dw_attr_ref a)
8254 char *sym = AT_loc_list (a)->ll_symbol;
8256 gcc_assert (sym);
8257 if (dwarf_split_debug_info)
8258 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8259 "%s", dwarf_attr_name (a->dw_attr));
8260 else
8261 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8262 "%s", dwarf_attr_name (a->dw_attr));
8265 /* Output an attribute's index or value appropriately. */
8267 static void
8268 output_attr_index_or_value (dw_attr_ref a)
8270 const char *name = dwarf_attr_name (a->dw_attr);
8272 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8274 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8275 return;
8277 switch (AT_class (a))
8279 case dw_val_class_addr:
8280 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8281 break;
8282 case dw_val_class_high_pc:
8283 case dw_val_class_lbl_id:
8284 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8285 break;
8286 case dw_val_class_loc_list:
8287 output_loc_list_offset (a);
8288 break;
8289 default:
8290 gcc_unreachable ();
8294 /* Output a type signature. */
8296 static inline void
8297 output_signature (const char *sig, const char *name)
8299 int i;
8301 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8302 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8305 /* Output the DIE and its attributes. Called recursively to generate
8306 the definitions of each child DIE. */
8308 static void
8309 output_die (dw_die_ref die)
8311 dw_attr_ref a;
8312 dw_die_ref c;
8313 unsigned long size;
8314 unsigned ix;
8316 /* If someone in another CU might refer to us, set up a symbol for
8317 them to point to. */
8318 if (! die->comdat_type_p && die->die_id.die_symbol)
8319 output_die_symbol (die);
8321 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8322 (unsigned long)die->die_offset,
8323 dwarf_tag_name (die->die_tag));
8325 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8327 const char *name = dwarf_attr_name (a->dw_attr);
8329 switch (AT_class (a))
8331 case dw_val_class_addr:
8332 output_attr_index_or_value (a);
8333 break;
8335 case dw_val_class_offset:
8336 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8337 "%s", name);
8338 break;
8340 case dw_val_class_range_list:
8341 output_range_list_offset (a);
8342 break;
8344 case dw_val_class_loc:
8345 size = size_of_locs (AT_loc (a));
8347 /* Output the block length for this list of location operations. */
8348 if (dwarf_version >= 4)
8349 dw2_asm_output_data_uleb128 (size, "%s", name);
8350 else
8351 dw2_asm_output_data (constant_size (size), size, "%s", name);
8353 output_loc_sequence (AT_loc (a), -1);
8354 break;
8356 case dw_val_class_const:
8357 /* ??? It would be slightly more efficient to use a scheme like is
8358 used for unsigned constants below, but gdb 4.x does not sign
8359 extend. Gdb 5.x does sign extend. */
8360 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8361 break;
8363 case dw_val_class_unsigned_const:
8365 int csize = constant_size (AT_unsigned (a));
8366 if (dwarf_version == 3
8367 && a->dw_attr == DW_AT_data_member_location
8368 && csize >= 4)
8369 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8370 else
8371 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8373 break;
8375 case dw_val_class_const_double:
8377 unsigned HOST_WIDE_INT first, second;
8379 if (HOST_BITS_PER_WIDE_INT >= 64)
8380 dw2_asm_output_data (1,
8381 HOST_BITS_PER_DOUBLE_INT
8382 / HOST_BITS_PER_CHAR,
8383 NULL);
8385 if (WORDS_BIG_ENDIAN)
8387 first = a->dw_attr_val.v.val_double.high;
8388 second = a->dw_attr_val.v.val_double.low;
8390 else
8392 first = a->dw_attr_val.v.val_double.low;
8393 second = a->dw_attr_val.v.val_double.high;
8396 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8397 first, "%s", name);
8398 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8399 second, NULL);
8401 break;
8403 case dw_val_class_vec:
8405 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8406 unsigned int len = a->dw_attr_val.v.val_vec.length;
8407 unsigned int i;
8408 unsigned char *p;
8410 dw2_asm_output_data (constant_size (len * elt_size),
8411 len * elt_size, "%s", name);
8412 if (elt_size > sizeof (HOST_WIDE_INT))
8414 elt_size /= 2;
8415 len *= 2;
8417 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8418 i < len;
8419 i++, p += elt_size)
8420 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8421 "fp or vector constant word %u", i);
8422 break;
8425 case dw_val_class_flag:
8426 if (dwarf_version >= 4)
8428 /* Currently all add_AT_flag calls pass in 1 as last argument,
8429 so DW_FORM_flag_present can be used. If that ever changes,
8430 we'll need to use DW_FORM_flag and have some optimization
8431 in build_abbrev_table that will change those to
8432 DW_FORM_flag_present if it is set to 1 in all DIEs using
8433 the same abbrev entry. */
8434 gcc_assert (AT_flag (a) == 1);
8435 if (flag_debug_asm)
8436 fprintf (asm_out_file, "\t\t\t%s %s\n",
8437 ASM_COMMENT_START, name);
8438 break;
8440 dw2_asm_output_data (1, AT_flag (a), "%s", name);
8441 break;
8443 case dw_val_class_loc_list:
8444 output_attr_index_or_value (a);
8445 break;
8447 case dw_val_class_die_ref:
8448 if (AT_ref_external (a))
8450 if (AT_ref (a)->comdat_type_p)
8452 comdat_type_node_ref type_node =
8453 AT_ref (a)->die_id.die_type_node;
8455 gcc_assert (type_node);
8456 output_signature (type_node->signature, name);
8458 else
8460 const char *sym = AT_ref (a)->die_id.die_symbol;
8461 int size;
8463 gcc_assert (sym);
8464 /* In DWARF2, DW_FORM_ref_addr is sized by target address
8465 length, whereas in DWARF3 it's always sized as an
8466 offset. */
8467 if (dwarf_version == 2)
8468 size = DWARF2_ADDR_SIZE;
8469 else
8470 size = DWARF_OFFSET_SIZE;
8471 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8472 name);
8475 else
8477 gcc_assert (AT_ref (a)->die_offset);
8478 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8479 "%s", name);
8481 break;
8483 case dw_val_class_fde_ref:
8485 char l1[20];
8487 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8488 a->dw_attr_val.v.val_fde_index * 2);
8489 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8490 "%s", name);
8492 break;
8494 case dw_val_class_vms_delta:
8495 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
8496 AT_vms_delta2 (a), AT_vms_delta1 (a),
8497 "%s", name);
8498 break;
8500 case dw_val_class_lbl_id:
8501 output_attr_index_or_value (a);
8502 break;
8504 case dw_val_class_lineptr:
8505 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8506 debug_line_section, "%s", name);
8507 break;
8509 case dw_val_class_macptr:
8510 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8511 debug_macinfo_section, "%s", name);
8512 break;
8514 case dw_val_class_str:
8515 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
8516 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8517 a->dw_attr_val.v.val_str->label,
8518 debug_str_section,
8519 "%s: \"%s\"", name, AT_string (a));
8520 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
8521 dw2_asm_output_data_uleb128 (AT_index (a),
8522 "%s: \"%s\"", name, AT_string (a));
8523 else
8524 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8525 break;
8527 case dw_val_class_file:
8529 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8531 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8532 a->dw_attr_val.v.val_file->filename);
8533 break;
8536 case dw_val_class_data8:
8538 int i;
8540 for (i = 0; i < 8; i++)
8541 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
8542 i == 0 ? "%s" : NULL, name);
8543 break;
8546 case dw_val_class_high_pc:
8547 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
8548 get_AT_low_pc (die), "DW_AT_high_pc");
8549 break;
8551 default:
8552 gcc_unreachable ();
8556 FOR_EACH_CHILD (die, c, output_die (c));
8558 /* Add null byte to terminate sibling list. */
8559 if (die->die_child != NULL)
8560 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
8561 (unsigned long) die->die_offset);
8564 /* Output the compilation unit that appears at the beginning of the
8565 .debug_info section, and precedes the DIE descriptions. */
8567 static void
8568 output_compilation_unit_header (void)
8570 int ver = dwarf_version;
8572 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8573 dw2_asm_output_data (4, 0xffffffff,
8574 "Initial length escape value indicating 64-bit DWARF extension");
8575 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8576 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8577 "Length of Compilation Unit Info");
8578 dw2_asm_output_data (2, ver, "DWARF version number");
8579 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8580 debug_abbrev_section,
8581 "Offset Into Abbrev. Section");
8582 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8585 /* Output the compilation unit DIE and its children. */
8587 static void
8588 output_comp_unit (dw_die_ref die, int output_if_empty)
8590 const char *secname, *oldsym;
8591 char *tmp;
8592 htab_t extern_map;
8594 /* Unless we are outputting main CU, we may throw away empty ones. */
8595 if (!output_if_empty && die->die_child == NULL)
8596 return;
8598 /* Even if there are no children of this DIE, we must output the information
8599 about the compilation unit. Otherwise, on an empty translation unit, we
8600 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
8601 will then complain when examining the file. First mark all the DIEs in
8602 this CU so we know which get local refs. */
8603 mark_dies (die);
8605 extern_map = optimize_external_refs (die);
8607 build_abbrev_table (die, extern_map);
8609 htab_delete (extern_map);
8611 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8612 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8613 calc_die_sizes (die);
8615 oldsym = die->die_id.die_symbol;
8616 if (oldsym)
8618 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8620 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8621 secname = tmp;
8622 die->die_id.die_symbol = NULL;
8623 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8625 else
8627 switch_to_section (debug_info_section);
8628 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
8629 info_section_emitted = true;
8632 /* Output debugging information. */
8633 output_compilation_unit_header ();
8634 output_die (die);
8636 /* Leave the marks on the main CU, so we can check them in
8637 output_pubnames. */
8638 if (oldsym)
8640 unmark_dies (die);
8641 die->die_id.die_symbol = oldsym;
8645 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
8646 and .debug_pubtypes. This is configured per-target, but can be
8647 overridden by the -gpubnames or -gno-pubnames options. */
8649 static inline bool
8650 want_pubnames (void)
8652 return (debug_generate_pub_sections != -1
8653 ? debug_generate_pub_sections
8654 : targetm.want_debug_pub_sections);
8657 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
8659 static void
8660 add_AT_pubnames (dw_die_ref die)
8662 if (want_pubnames ())
8663 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
8666 /* Helper function to generate top-level dies for skeleton debug_info and
8667 debug_types. */
8669 static void
8670 add_top_level_skeleton_die_attrs (dw_die_ref die)
8672 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
8673 dw_attr_ref attr;
8675 add_comp_dir_attribute (die);
8676 add_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
8677 /* The specification suggests that these attributes be inline to avoid
8678 having a .debug_str section. We know that they exist in the die because
8679 we just added them. */
8680 attr = get_AT (die, DW_AT_GNU_dwo_name);
8681 attr->dw_attr_val.v.val_str->form = DW_FORM_string;
8682 attr = get_AT (die, DW_AT_comp_dir);
8683 attr->dw_attr_val.v.val_str->form = DW_FORM_string;
8685 add_AT_pubnames (die);
8686 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
8689 /* Return the single type-unit die for skeleton type units. */
8691 static dw_die_ref
8692 get_skeleton_type_unit (void)
8694 /* For dwarf_split_debug_sections with use_type info, all type units in the
8695 skeleton sections have identical dies (but different headers). This
8696 single die will be output many times. */
8698 static dw_die_ref skeleton_type_unit = NULL;
8700 if (skeleton_type_unit == NULL)
8702 skeleton_type_unit = new_die (DW_TAG_type_unit, NULL, NULL);
8703 add_top_level_skeleton_die_attrs (skeleton_type_unit);
8704 skeleton_type_unit->die_abbrev = SKELETON_TYPE_DIE_ABBREV;
8706 return skeleton_type_unit;
8709 /* Output skeleton debug sections that point to the dwo file. */
8711 static void
8712 output_skeleton_debug_sections (dw_die_ref comp_unit)
8714 /* These attributes will be found in the full debug_info section. */
8715 remove_AT (comp_unit, DW_AT_producer);
8716 remove_AT (comp_unit, DW_AT_language);
8718 /* Add attributes common to skeleton compile_units and type_units. */
8719 add_top_level_skeleton_die_attrs (comp_unit);
8721 switch_to_section (debug_skeleton_info_section);
8722 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
8724 /* Produce the skeleton compilation-unit header. This one differs enough from
8725 a normal CU header that it's better not to call output_compilation_unit
8726 header. */
8727 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8728 dw2_asm_output_data (4, 0xffffffff,
8729 "Initial length escape value indicating 64-bit DWARF extension");
8731 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8732 DWARF_COMPILE_UNIT_HEADER_SIZE
8733 - DWARF_INITIAL_LENGTH_SIZE
8734 + size_of_die (comp_unit),
8735 "Length of Compilation Unit Info");
8736 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
8737 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
8738 debug_abbrev_section,
8739 "Offset Into Abbrev. Section");
8740 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8742 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
8743 output_die (comp_unit);
8745 /* Build the skeleton debug_abbrev section. */
8746 switch_to_section (debug_skeleton_abbrev_section);
8747 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
8749 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
8750 if (use_debug_types)
8751 output_die_abbrevs (SKELETON_TYPE_DIE_ABBREV, get_skeleton_type_unit ());
8753 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
8756 /* Output a comdat type unit DIE and its children. */
8758 static void
8759 output_comdat_type_unit (comdat_type_node *node)
8761 const char *secname;
8762 char *tmp;
8763 int i;
8764 #if defined (OBJECT_FORMAT_ELF)
8765 tree comdat_key;
8766 #endif
8767 htab_t extern_map;
8769 /* First mark all the DIEs in this CU so we know which get local refs. */
8770 mark_dies (node->root_die);
8772 extern_map = optimize_external_refs (node->root_die);
8774 build_abbrev_table (node->root_die, extern_map);
8776 htab_delete (extern_map);
8778 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8779 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
8780 calc_die_sizes (node->root_die);
8782 #if defined (OBJECT_FORMAT_ELF)
8783 if (!dwarf_split_debug_info)
8784 secname = ".debug_types";
8785 else
8786 secname = ".debug_types.dwo";
8788 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
8789 sprintf (tmp, "wt.");
8790 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8791 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
8792 comdat_key = get_identifier (tmp);
8793 targetm.asm_out.named_section (secname,
8794 SECTION_DEBUG | SECTION_LINKONCE,
8795 comdat_key);
8796 #else
8797 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
8798 sprintf (tmp, ".gnu.linkonce.wt.");
8799 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8800 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
8801 secname = tmp;
8802 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8803 #endif
8805 /* Output debugging information. */
8806 output_compilation_unit_header ();
8807 output_signature (node->signature, "Type Signature");
8808 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
8809 "Offset to Type DIE");
8810 output_die (node->root_die);
8812 unmark_dies (node->root_die);
8814 #if defined (OBJECT_FORMAT_ELF)
8815 if (dwarf_split_debug_info)
8817 /* Produce the skeleton type-unit header. */
8818 const char *secname = ".debug_types";
8820 targetm.asm_out.named_section (secname,
8821 SECTION_DEBUG | SECTION_LINKONCE,
8822 comdat_key);
8823 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8824 dw2_asm_output_data (4, 0xffffffff,
8825 "Initial length escape value indicating 64-bit DWARF extension");
8827 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8828 DWARF_COMPILE_UNIT_HEADER_SIZE
8829 - DWARF_INITIAL_LENGTH_SIZE
8830 + size_of_die (get_skeleton_type_unit ())
8831 + DWARF_TYPE_SIGNATURE_SIZE + DWARF_OFFSET_SIZE,
8832 "Length of Type Unit Info");
8833 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
8834 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8835 debug_skeleton_abbrev_section_label,
8836 debug_abbrev_section,
8837 "Offset Into Abbrev. Section");
8838 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8839 output_signature (node->signature, "Type Signature");
8840 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, "Offset to Type DIE");
8842 output_die (get_skeleton_type_unit ());
8844 #endif
8847 /* Return the DWARF2/3 pubname associated with a decl. */
8849 static const char *
8850 dwarf2_name (tree decl, int scope)
8852 if (DECL_NAMELESS (decl))
8853 return NULL;
8854 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
8857 /* Add a new entry to .debug_pubnames if appropriate. */
8859 static void
8860 add_pubname_string (const char *str, dw_die_ref die)
8862 pubname_entry e;
8864 e.die = die;
8865 e.name = xstrdup (str);
8866 vec_safe_push (pubname_table, e);
8869 static void
8870 add_pubname (tree decl, dw_die_ref die)
8872 if (!want_pubnames ())
8873 return;
8875 /* Don't add items to the table when we expect that the consumer will have
8876 just read the enclosing die. For example, if the consumer is looking at a
8877 class_member, it will either be inside the class already, or will have just
8878 looked up the class to find the member. Either way, searching the class is
8879 faster than searching the index. */
8880 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
8881 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
8883 const char *name = dwarf2_name (decl, 1);
8885 if (name)
8886 add_pubname_string (name, die);
8890 /* Add an enumerator to the pubnames section. */
8892 static void
8893 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
8895 pubname_entry e;
8897 gcc_assert (scope_name);
8898 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
8899 e.die = die;
8900 vec_safe_push (pubname_table, e);
8903 /* Add a new entry to .debug_pubtypes if appropriate. */
8905 static void
8906 add_pubtype (tree decl, dw_die_ref die)
8908 pubname_entry e;
8910 if (!want_pubnames ())
8911 return;
8913 if ((TREE_PUBLIC (decl)
8914 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
8915 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
8917 tree scope = NULL;
8918 const char *scope_name = "";
8919 const char *sep = is_cxx () ? "::" : ".";
8920 const char *name;
8922 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
8923 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
8925 scope_name = lang_hooks.dwarf_name (scope, 1);
8926 if (scope_name != NULL && scope_name[0] != '\0')
8927 scope_name = concat (scope_name, sep, NULL);
8928 else
8929 scope_name = "";
8932 if (TYPE_P (decl))
8933 name = type_tag (decl);
8934 else
8935 name = lang_hooks.dwarf_name (decl, 1);
8937 /* If we don't have a name for the type, there's no point in adding
8938 it to the table. */
8939 if (name != NULL && name[0] != '\0')
8941 e.die = die;
8942 e.name = concat (scope_name, name, NULL);
8943 vec_safe_push (pubtype_table, e);
8946 /* Although it might be more consistent to add the pubinfo for the
8947 enumerators as their dies are created, they should only be added if the
8948 enum type meets the criteria above. So rather than re-check the parent
8949 enum type whenever an enumerator die is created, just output them all
8950 here. This isn't protected by the name conditional because anonymous
8951 enums don't have names. */
8952 if (die->die_tag == DW_TAG_enumeration_type)
8954 dw_die_ref c;
8956 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
8961 /* Output the public names table used to speed up access to externally
8962 visible names; or the public types table used to find type definitions. */
8964 static void
8965 output_pubnames (vec<pubname_entry, va_gc> *names)
8967 unsigned i;
8968 unsigned long pubnames_length = size_of_pubnames (names);
8969 pubname_ref pub;
8971 if (!want_pubnames () || !info_section_emitted)
8972 return;
8973 if (names == pubname_table)
8974 switch_to_section (debug_pubnames_section);
8975 else
8976 switch_to_section (debug_pubtypes_section);
8977 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8978 dw2_asm_output_data (4, 0xffffffff,
8979 "Initial length escape value indicating 64-bit DWARF extension");
8980 if (names == pubname_table)
8981 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8982 "Length of Public Names Info");
8983 else
8984 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8985 "Length of Public Type Names Info");
8986 /* Version number for pubnames/pubtypes is still 2, even in DWARF3. */
8987 dw2_asm_output_data (2, 2, "DWARF Version");
8988 if (dwarf_split_debug_info)
8989 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
8990 debug_skeleton_info_section,
8991 "Offset of Compilation Unit Info");
8992 else
8993 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8994 debug_info_section,
8995 "Offset of Compilation Unit Info");
8996 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
8997 "Compilation Unit Length");
8999 FOR_EACH_VEC_ELT (*names, i, pub)
9001 /* Enumerator names are part of the pubname table, but the parent
9002 DW_TAG_enumeration_type die may have been pruned. Don't output
9003 them if that is the case. */
9004 if (pub->die->die_tag == DW_TAG_enumerator && !pub->die->die_mark)
9005 continue;
9007 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9008 if (names == pubname_table)
9009 gcc_assert (pub->die->die_mark);
9011 if (names != pubtype_table
9012 || pub->die->die_offset != 0
9013 || !flag_eliminate_unused_debug_types)
9015 dw_offset die_offset = pub->die->die_offset;
9017 /* If we're putting types in their own .debug_types sections,
9018 the .debug_pubtypes table will still point to the compile
9019 unit (not the type unit), so we want to use the offset of
9020 the skeleton DIE (if there is one). */
9021 if (pub->die->comdat_type_p && names == pubtype_table)
9023 comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
9025 if (type_node != NULL)
9026 die_offset = (type_node->skeleton_die != NULL
9027 ? type_node->skeleton_die->die_offset
9028 : 0);
9031 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9033 dw2_asm_output_nstring (pub->name, -1, "external name");
9037 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9040 /* Output the information that goes into the .debug_aranges table.
9041 Namely, define the beginning and ending address range of the
9042 text section generated for this compilation unit. */
9044 static void
9045 output_aranges (unsigned long aranges_length)
9047 unsigned i;
9049 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9050 dw2_asm_output_data (4, 0xffffffff,
9051 "Initial length escape value indicating 64-bit DWARF extension");
9052 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9053 "Length of Address Ranges Info");
9054 /* Version number for aranges is still 2, even in DWARF3. */
9055 dw2_asm_output_data (2, 2, "DWARF Version");
9056 if (dwarf_split_debug_info)
9057 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9058 debug_skeleton_info_section,
9059 "Offset of Compilation Unit Info");
9060 else
9061 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9062 debug_info_section,
9063 "Offset of Compilation Unit Info");
9064 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9065 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9067 /* We need to align to twice the pointer size here. */
9068 if (DWARF_ARANGES_PAD_SIZE)
9070 /* Pad using a 2 byte words so that padding is correct for any
9071 pointer size. */
9072 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9073 2 * DWARF2_ADDR_SIZE);
9074 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9075 dw2_asm_output_data (2, 0, NULL);
9078 /* It is necessary not to output these entries if the sections were
9079 not used; if the sections were not used, the length will be 0 and
9080 the address may end up as 0 if the section is discarded by ld
9081 --gc-sections, leaving an invalid (0, 0) entry that can be
9082 confused with the terminator. */
9083 if (text_section_used)
9085 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9086 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9087 text_section_label, "Length");
9089 if (cold_text_section_used)
9091 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9092 "Address");
9093 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9094 cold_text_section_label, "Length");
9097 if (have_multiple_function_sections)
9099 unsigned fde_idx;
9100 dw_fde_ref fde;
9102 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9104 if (DECL_IGNORED_P (fde->decl))
9105 continue;
9106 if (!fde->in_std_section)
9108 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9109 "Address");
9110 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9111 fde->dw_fde_begin, "Length");
9113 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9115 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9116 "Address");
9117 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9118 fde->dw_fde_second_begin, "Length");
9123 /* Output the terminator words. */
9124 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9125 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9128 /* Add a new entry to .debug_ranges. Return the offset at which it
9129 was placed. */
9131 static unsigned int
9132 add_ranges_num (int num)
9134 unsigned int in_use = ranges_table_in_use;
9136 if (in_use == ranges_table_allocated)
9138 ranges_table_allocated += RANGES_TABLE_INCREMENT;
9139 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9140 ranges_table_allocated);
9141 memset (ranges_table + ranges_table_in_use, 0,
9142 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9145 ranges_table[in_use].num = num;
9146 ranges_table_in_use = in_use + 1;
9148 return in_use * 2 * DWARF2_ADDR_SIZE;
9151 /* Add a new entry to .debug_ranges corresponding to a block, or a
9152 range terminator if BLOCK is NULL. */
9154 static unsigned int
9155 add_ranges (const_tree block)
9157 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9160 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9161 When using dwarf_split_debug_info, address attributes in dies destined
9162 for the final executable should be direct references--setting the
9163 parameter force_direct ensures this behavior. */
9165 static void
9166 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9167 bool *added, bool force_direct)
9169 unsigned int in_use = ranges_by_label_in_use;
9170 unsigned int offset;
9172 if (in_use == ranges_by_label_allocated)
9174 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9175 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9176 ranges_by_label,
9177 ranges_by_label_allocated);
9178 memset (ranges_by_label + ranges_by_label_in_use, 0,
9179 RANGES_TABLE_INCREMENT
9180 * sizeof (struct dw_ranges_by_label_struct));
9183 ranges_by_label[in_use].begin = begin;
9184 ranges_by_label[in_use].end = end;
9185 ranges_by_label_in_use = in_use + 1;
9187 offset = add_ranges_num (-(int)in_use - 1);
9188 if (!*added)
9190 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9191 *added = true;
9195 static void
9196 output_ranges (void)
9198 unsigned i;
9199 static const char *const start_fmt = "Offset %#x";
9200 const char *fmt = start_fmt;
9202 for (i = 0; i < ranges_table_in_use; i++)
9204 int block_num = ranges_table[i].num;
9206 if (block_num > 0)
9208 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9209 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9211 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9212 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9214 /* If all code is in the text section, then the compilation
9215 unit base address defaults to DW_AT_low_pc, which is the
9216 base of the text section. */
9217 if (!have_multiple_function_sections)
9219 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9220 text_section_label,
9221 fmt, i * 2 * DWARF2_ADDR_SIZE);
9222 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9223 text_section_label, NULL);
9226 /* Otherwise, the compilation unit base address is zero,
9227 which allows us to use absolute addresses, and not worry
9228 about whether the target supports cross-section
9229 arithmetic. */
9230 else
9232 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9233 fmt, i * 2 * DWARF2_ADDR_SIZE);
9234 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9237 fmt = NULL;
9240 /* Negative block_num stands for an index into ranges_by_label. */
9241 else if (block_num < 0)
9243 int lab_idx = - block_num - 1;
9245 if (!have_multiple_function_sections)
9247 gcc_unreachable ();
9248 #if 0
9249 /* If we ever use add_ranges_by_labels () for a single
9250 function section, all we have to do is to take out
9251 the #if 0 above. */
9252 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9253 ranges_by_label[lab_idx].begin,
9254 text_section_label,
9255 fmt, i * 2 * DWARF2_ADDR_SIZE);
9256 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9257 ranges_by_label[lab_idx].end,
9258 text_section_label, NULL);
9259 #endif
9261 else
9263 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9264 ranges_by_label[lab_idx].begin,
9265 fmt, i * 2 * DWARF2_ADDR_SIZE);
9266 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9267 ranges_by_label[lab_idx].end,
9268 NULL);
9271 else
9273 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9274 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9275 fmt = start_fmt;
9280 /* Data structure containing information about input files. */
9281 struct file_info
9283 const char *path; /* Complete file name. */
9284 const char *fname; /* File name part. */
9285 int length; /* Length of entire string. */
9286 struct dwarf_file_data * file_idx; /* Index in input file table. */
9287 int dir_idx; /* Index in directory table. */
9290 /* Data structure containing information about directories with source
9291 files. */
9292 struct dir_info
9294 const char *path; /* Path including directory name. */
9295 int length; /* Path length. */
9296 int prefix; /* Index of directory entry which is a prefix. */
9297 int count; /* Number of files in this directory. */
9298 int dir_idx; /* Index of directory used as base. */
9301 /* Callback function for file_info comparison. We sort by looking at
9302 the directories in the path. */
9304 static int
9305 file_info_cmp (const void *p1, const void *p2)
9307 const struct file_info *const s1 = (const struct file_info *) p1;
9308 const struct file_info *const s2 = (const struct file_info *) p2;
9309 const unsigned char *cp1;
9310 const unsigned char *cp2;
9312 /* Take care of file names without directories. We need to make sure that
9313 we return consistent values to qsort since some will get confused if
9314 we return the same value when identical operands are passed in opposite
9315 orders. So if neither has a directory, return 0 and otherwise return
9316 1 or -1 depending on which one has the directory. */
9317 if ((s1->path == s1->fname || s2->path == s2->fname))
9318 return (s2->path == s2->fname) - (s1->path == s1->fname);
9320 cp1 = (const unsigned char *) s1->path;
9321 cp2 = (const unsigned char *) s2->path;
9323 while (1)
9325 ++cp1;
9326 ++cp2;
9327 /* Reached the end of the first path? If so, handle like above. */
9328 if ((cp1 == (const unsigned char *) s1->fname)
9329 || (cp2 == (const unsigned char *) s2->fname))
9330 return ((cp2 == (const unsigned char *) s2->fname)
9331 - (cp1 == (const unsigned char *) s1->fname));
9333 /* Character of current path component the same? */
9334 else if (*cp1 != *cp2)
9335 return *cp1 - *cp2;
9339 struct file_name_acquire_data
9341 struct file_info *files;
9342 int used_files;
9343 int max_files;
9346 /* Traversal function for the hash table. */
9348 static int
9349 file_name_acquire (void ** slot, void *data)
9351 struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
9352 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
9353 struct file_info *fi;
9354 const char *f;
9356 gcc_assert (fnad->max_files >= d->emitted_number);
9358 if (! d->emitted_number)
9359 return 1;
9361 gcc_assert (fnad->max_files != fnad->used_files);
9363 fi = fnad->files + fnad->used_files++;
9365 /* Skip all leading "./". */
9366 f = d->filename;
9367 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9368 f += 2;
9370 /* Create a new array entry. */
9371 fi->path = f;
9372 fi->length = strlen (f);
9373 fi->file_idx = d;
9375 /* Search for the file name part. */
9376 f = strrchr (f, DIR_SEPARATOR);
9377 #if defined (DIR_SEPARATOR_2)
9379 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9381 if (g != NULL)
9383 if (f == NULL || f < g)
9384 f = g;
9387 #endif
9389 fi->fname = f == NULL ? fi->path : f + 1;
9390 return 1;
9393 /* Output the directory table and the file name table. We try to minimize
9394 the total amount of memory needed. A heuristic is used to avoid large
9395 slowdowns with many input files. */
9397 static void
9398 output_file_names (void)
9400 struct file_name_acquire_data fnad;
9401 int numfiles;
9402 struct file_info *files;
9403 struct dir_info *dirs;
9404 int *saved;
9405 int *savehere;
9406 int *backmap;
9407 int ndirs;
9408 int idx_offset;
9409 int i;
9411 if (!last_emitted_file)
9413 dw2_asm_output_data (1, 0, "End directory table");
9414 dw2_asm_output_data (1, 0, "End file name table");
9415 return;
9418 numfiles = last_emitted_file->emitted_number;
9420 /* Allocate the various arrays we need. */
9421 files = XALLOCAVEC (struct file_info, numfiles);
9422 dirs = XALLOCAVEC (struct dir_info, numfiles);
9424 fnad.files = files;
9425 fnad.used_files = 0;
9426 fnad.max_files = numfiles;
9427 htab_traverse (file_table, file_name_acquire, &fnad);
9428 gcc_assert (fnad.used_files == fnad.max_files);
9430 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9432 /* Find all the different directories used. */
9433 dirs[0].path = files[0].path;
9434 dirs[0].length = files[0].fname - files[0].path;
9435 dirs[0].prefix = -1;
9436 dirs[0].count = 1;
9437 dirs[0].dir_idx = 0;
9438 files[0].dir_idx = 0;
9439 ndirs = 1;
9441 for (i = 1; i < numfiles; i++)
9442 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9443 && memcmp (dirs[ndirs - 1].path, files[i].path,
9444 dirs[ndirs - 1].length) == 0)
9446 /* Same directory as last entry. */
9447 files[i].dir_idx = ndirs - 1;
9448 ++dirs[ndirs - 1].count;
9450 else
9452 int j;
9454 /* This is a new directory. */
9455 dirs[ndirs].path = files[i].path;
9456 dirs[ndirs].length = files[i].fname - files[i].path;
9457 dirs[ndirs].count = 1;
9458 dirs[ndirs].dir_idx = ndirs;
9459 files[i].dir_idx = ndirs;
9461 /* Search for a prefix. */
9462 dirs[ndirs].prefix = -1;
9463 for (j = 0; j < ndirs; j++)
9464 if (dirs[j].length < dirs[ndirs].length
9465 && dirs[j].length > 1
9466 && (dirs[ndirs].prefix == -1
9467 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9468 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9469 dirs[ndirs].prefix = j;
9471 ++ndirs;
9474 /* Now to the actual work. We have to find a subset of the directories which
9475 allow expressing the file name using references to the directory table
9476 with the least amount of characters. We do not do an exhaustive search
9477 where we would have to check out every combination of every single
9478 possible prefix. Instead we use a heuristic which provides nearly optimal
9479 results in most cases and never is much off. */
9480 saved = XALLOCAVEC (int, ndirs);
9481 savehere = XALLOCAVEC (int, ndirs);
9483 memset (saved, '\0', ndirs * sizeof (saved[0]));
9484 for (i = 0; i < ndirs; i++)
9486 int j;
9487 int total;
9489 /* We can always save some space for the current directory. But this
9490 does not mean it will be enough to justify adding the directory. */
9491 savehere[i] = dirs[i].length;
9492 total = (savehere[i] - saved[i]) * dirs[i].count;
9494 for (j = i + 1; j < ndirs; j++)
9496 savehere[j] = 0;
9497 if (saved[j] < dirs[i].length)
9499 /* Determine whether the dirs[i] path is a prefix of the
9500 dirs[j] path. */
9501 int k;
9503 k = dirs[j].prefix;
9504 while (k != -1 && k != (int) i)
9505 k = dirs[k].prefix;
9507 if (k == (int) i)
9509 /* Yes it is. We can possibly save some memory by
9510 writing the filenames in dirs[j] relative to
9511 dirs[i]. */
9512 savehere[j] = dirs[i].length;
9513 total += (savehere[j] - saved[j]) * dirs[j].count;
9518 /* Check whether we can save enough to justify adding the dirs[i]
9519 directory. */
9520 if (total > dirs[i].length + 1)
9522 /* It's worthwhile adding. */
9523 for (j = i; j < ndirs; j++)
9524 if (savehere[j] > 0)
9526 /* Remember how much we saved for this directory so far. */
9527 saved[j] = savehere[j];
9529 /* Remember the prefix directory. */
9530 dirs[j].dir_idx = i;
9535 /* Emit the directory name table. */
9536 idx_offset = dirs[0].length > 0 ? 1 : 0;
9537 for (i = 1 - idx_offset; i < ndirs; i++)
9538 dw2_asm_output_nstring (dirs[i].path,
9539 dirs[i].length
9540 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9541 "Directory Entry: %#x", i + idx_offset);
9543 dw2_asm_output_data (1, 0, "End directory table");
9545 /* We have to emit them in the order of emitted_number since that's
9546 used in the debug info generation. To do this efficiently we
9547 generate a back-mapping of the indices first. */
9548 backmap = XALLOCAVEC (int, numfiles);
9549 for (i = 0; i < numfiles; i++)
9550 backmap[files[i].file_idx->emitted_number - 1] = i;
9552 /* Now write all the file names. */
9553 for (i = 0; i < numfiles; i++)
9555 int file_idx = backmap[i];
9556 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9558 #ifdef VMS_DEBUGGING_INFO
9559 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
9561 /* Setting these fields can lead to debugger miscomparisons,
9562 but VMS Debug requires them to be set correctly. */
9564 int ver;
9565 long long cdt;
9566 long siz;
9567 int maxfilelen = strlen (files[file_idx].path)
9568 + dirs[dir_idx].length
9569 + MAX_VMS_VERSION_LEN + 1;
9570 char *filebuf = XALLOCAVEC (char, maxfilelen);
9572 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
9573 snprintf (filebuf, maxfilelen, "%s;%d",
9574 files[file_idx].path + dirs[dir_idx].length, ver);
9576 dw2_asm_output_nstring
9577 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
9579 /* Include directory index. */
9580 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9582 /* Modification time. */
9583 dw2_asm_output_data_uleb128
9584 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
9585 ? cdt : 0,
9586 NULL);
9588 /* File length in bytes. */
9589 dw2_asm_output_data_uleb128
9590 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
9591 ? siz : 0,
9592 NULL);
9593 #else
9594 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
9595 "File Entry: %#x", (unsigned) i + 1);
9597 /* Include directory index. */
9598 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9600 /* Modification time. */
9601 dw2_asm_output_data_uleb128 (0, NULL);
9603 /* File length in bytes. */
9604 dw2_asm_output_data_uleb128 (0, NULL);
9605 #endif /* VMS_DEBUGGING_INFO */
9608 dw2_asm_output_data (1, 0, "End file name table");
9612 /* Output one line number table into the .debug_line section. */
9614 static void
9615 output_one_line_info_table (dw_line_info_table *table)
9617 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9618 unsigned int current_line = 1;
9619 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
9620 dw_line_info_entry *ent;
9621 size_t i;
9623 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
9625 switch (ent->opcode)
9627 case LI_set_address:
9628 /* ??? Unfortunately, we have little choice here currently, and
9629 must always use the most general form. GCC does not know the
9630 address delta itself, so we can't use DW_LNS_advance_pc. Many
9631 ports do have length attributes which will give an upper bound
9632 on the address range. We could perhaps use length attributes
9633 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
9634 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
9636 /* This can handle any delta. This takes
9637 4+DWARF2_ADDR_SIZE bytes. */
9638 dw2_asm_output_data (1, 0, "set address %s", line_label);
9639 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9640 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9641 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9642 break;
9644 case LI_set_line:
9645 if (ent->val == current_line)
9647 /* We still need to start a new row, so output a copy insn. */
9648 dw2_asm_output_data (1, DW_LNS_copy,
9649 "copy line %u", current_line);
9651 else
9653 int line_offset = ent->val - current_line;
9654 int line_delta = line_offset - DWARF_LINE_BASE;
9656 current_line = ent->val;
9657 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9659 /* This can handle deltas from -10 to 234, using the current
9660 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
9661 This takes 1 byte. */
9662 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9663 "line %u", current_line);
9665 else
9667 /* This can handle any delta. This takes at least 4 bytes,
9668 depending on the value being encoded. */
9669 dw2_asm_output_data (1, DW_LNS_advance_line,
9670 "advance to line %u", current_line);
9671 dw2_asm_output_data_sleb128 (line_offset, NULL);
9672 dw2_asm_output_data (1, DW_LNS_copy, NULL);
9675 break;
9677 case LI_set_file:
9678 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
9679 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9680 break;
9682 case LI_set_column:
9683 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
9684 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9685 break;
9687 case LI_negate_stmt:
9688 current_is_stmt = !current_is_stmt;
9689 dw2_asm_output_data (1, DW_LNS_negate_stmt,
9690 "is_stmt %d", current_is_stmt);
9691 break;
9693 case LI_set_prologue_end:
9694 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
9695 "set prologue end");
9696 break;
9698 case LI_set_epilogue_begin:
9699 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
9700 "set epilogue begin");
9701 break;
9703 case LI_set_discriminator:
9704 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
9705 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
9706 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
9707 dw2_asm_output_data_uleb128 (ent->val, NULL);
9708 break;
9712 /* Emit debug info for the address of the end of the table. */
9713 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
9714 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9715 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9716 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
9718 dw2_asm_output_data (1, 0, "end sequence");
9719 dw2_asm_output_data_uleb128 (1, NULL);
9720 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9723 /* Output the source line number correspondence information. This
9724 information goes into the .debug_line section. */
9726 static void
9727 output_line_info (bool prologue_only)
9729 char l1[20], l2[20], p1[20], p2[20];
9730 int ver = dwarf_version;
9731 bool saw_one = false;
9732 int opc;
9734 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
9735 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
9736 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
9737 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
9739 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9740 dw2_asm_output_data (4, 0xffffffff,
9741 "Initial length escape value indicating 64-bit DWARF extension");
9742 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
9743 "Length of Source Line Info");
9744 ASM_OUTPUT_LABEL (asm_out_file, l1);
9746 dw2_asm_output_data (2, ver, "DWARF Version");
9747 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
9748 ASM_OUTPUT_LABEL (asm_out_file, p1);
9750 /* Define the architecture-dependent minimum instruction length (in bytes).
9751 In this implementation of DWARF, this field is used for information
9752 purposes only. Since GCC generates assembly language, we have no
9753 a priori knowledge of how many instruction bytes are generated for each
9754 source line, and therefore can use only the DW_LNE_set_address and
9755 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
9756 this as '1', which is "correct enough" for all architectures,
9757 and don't let the target override. */
9758 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
9760 if (ver >= 4)
9761 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
9762 "Maximum Operations Per Instruction");
9763 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
9764 "Default is_stmt_start flag");
9765 dw2_asm_output_data (1, DWARF_LINE_BASE,
9766 "Line Base Value (Special Opcodes)");
9767 dw2_asm_output_data (1, DWARF_LINE_RANGE,
9768 "Line Range Value (Special Opcodes)");
9769 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
9770 "Special Opcode Base");
9772 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
9774 int n_op_args;
9775 switch (opc)
9777 case DW_LNS_advance_pc:
9778 case DW_LNS_advance_line:
9779 case DW_LNS_set_file:
9780 case DW_LNS_set_column:
9781 case DW_LNS_fixed_advance_pc:
9782 case DW_LNS_set_isa:
9783 n_op_args = 1;
9784 break;
9785 default:
9786 n_op_args = 0;
9787 break;
9790 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
9791 opc, n_op_args);
9794 /* Write out the information about the files we use. */
9795 output_file_names ();
9796 ASM_OUTPUT_LABEL (asm_out_file, p2);
9797 if (prologue_only)
9799 /* Output the marker for the end of the line number info. */
9800 ASM_OUTPUT_LABEL (asm_out_file, l2);
9801 return;
9804 if (separate_line_info)
9806 dw_line_info_table *table;
9807 size_t i;
9809 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
9810 if (table->in_use)
9812 output_one_line_info_table (table);
9813 saw_one = true;
9816 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
9818 output_one_line_info_table (cold_text_section_line_info);
9819 saw_one = true;
9822 /* ??? Some Darwin linkers crash on a .debug_line section with no
9823 sequences. Further, merely a DW_LNE_end_sequence entry is not
9824 sufficient -- the address column must also be initialized.
9825 Make sure to output at least one set_address/end_sequence pair,
9826 choosing .text since that section is always present. */
9827 if (text_section_line_info->in_use || !saw_one)
9828 output_one_line_info_table (text_section_line_info);
9830 /* Output the marker for the end of the line number info. */
9831 ASM_OUTPUT_LABEL (asm_out_file, l2);
9834 /* Given a pointer to a tree node for some base type, return a pointer to
9835 a DIE that describes the given type.
9837 This routine must only be called for GCC type nodes that correspond to
9838 Dwarf base (fundamental) types. */
9840 static dw_die_ref
9841 base_type_die (tree type)
9843 dw_die_ref base_type_result;
9844 enum dwarf_type encoding;
9846 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
9847 return 0;
9849 /* If this is a subtype that should not be emitted as a subrange type,
9850 use the base type. See subrange_type_for_debug_p. */
9851 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
9852 type = TREE_TYPE (type);
9854 switch (TREE_CODE (type))
9856 case INTEGER_TYPE:
9857 if ((dwarf_version >= 4 || !dwarf_strict)
9858 && TYPE_NAME (type)
9859 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
9860 && DECL_IS_BUILTIN (TYPE_NAME (type))
9861 && DECL_NAME (TYPE_NAME (type)))
9863 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
9864 if (strcmp (name, "char16_t") == 0
9865 || strcmp (name, "char32_t") == 0)
9867 encoding = DW_ATE_UTF;
9868 break;
9871 if (TYPE_STRING_FLAG (type))
9873 if (TYPE_UNSIGNED (type))
9874 encoding = DW_ATE_unsigned_char;
9875 else
9876 encoding = DW_ATE_signed_char;
9878 else if (TYPE_UNSIGNED (type))
9879 encoding = DW_ATE_unsigned;
9880 else
9881 encoding = DW_ATE_signed;
9882 break;
9884 case REAL_TYPE:
9885 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
9887 if (dwarf_version >= 3 || !dwarf_strict)
9888 encoding = DW_ATE_decimal_float;
9889 else
9890 encoding = DW_ATE_lo_user;
9892 else
9893 encoding = DW_ATE_float;
9894 break;
9896 case FIXED_POINT_TYPE:
9897 if (!(dwarf_version >= 3 || !dwarf_strict))
9898 encoding = DW_ATE_lo_user;
9899 else if (TYPE_UNSIGNED (type))
9900 encoding = DW_ATE_unsigned_fixed;
9901 else
9902 encoding = DW_ATE_signed_fixed;
9903 break;
9905 /* Dwarf2 doesn't know anything about complex ints, so use
9906 a user defined type for it. */
9907 case COMPLEX_TYPE:
9908 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
9909 encoding = DW_ATE_complex_float;
9910 else
9911 encoding = DW_ATE_lo_user;
9912 break;
9914 case BOOLEAN_TYPE:
9915 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
9916 encoding = DW_ATE_boolean;
9917 break;
9919 default:
9920 /* No other TREE_CODEs are Dwarf fundamental types. */
9921 gcc_unreachable ();
9924 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
9926 add_AT_unsigned (base_type_result, DW_AT_byte_size,
9927 int_size_in_bytes (type));
9928 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
9929 add_pubtype (type, base_type_result);
9931 return base_type_result;
9934 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
9935 given input type is a Dwarf "fundamental" type. Otherwise return null. */
9937 static inline int
9938 is_base_type (tree type)
9940 switch (TREE_CODE (type))
9942 case ERROR_MARK:
9943 case VOID_TYPE:
9944 case INTEGER_TYPE:
9945 case REAL_TYPE:
9946 case FIXED_POINT_TYPE:
9947 case COMPLEX_TYPE:
9948 case BOOLEAN_TYPE:
9949 return 1;
9951 case ARRAY_TYPE:
9952 case RECORD_TYPE:
9953 case UNION_TYPE:
9954 case QUAL_UNION_TYPE:
9955 case ENUMERAL_TYPE:
9956 case FUNCTION_TYPE:
9957 case METHOD_TYPE:
9958 case POINTER_TYPE:
9959 case REFERENCE_TYPE:
9960 case NULLPTR_TYPE:
9961 case OFFSET_TYPE:
9962 case LANG_TYPE:
9963 case VECTOR_TYPE:
9964 return 0;
9966 default:
9967 gcc_unreachable ();
9970 return 0;
9973 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
9974 node, return the size in bits for the type if it is a constant, or else
9975 return the alignment for the type if the type's size is not constant, or
9976 else return BITS_PER_WORD if the type actually turns out to be an
9977 ERROR_MARK node. */
9979 static inline unsigned HOST_WIDE_INT
9980 simple_type_size_in_bits (const_tree type)
9982 if (TREE_CODE (type) == ERROR_MARK)
9983 return BITS_PER_WORD;
9984 else if (TYPE_SIZE (type) == NULL_TREE)
9985 return 0;
9986 else if (host_integerp (TYPE_SIZE (type), 1))
9987 return tree_low_cst (TYPE_SIZE (type), 1);
9988 else
9989 return TYPE_ALIGN (type);
9992 /* Similarly, but return a double_int instead of UHWI. */
9994 static inline double_int
9995 double_int_type_size_in_bits (const_tree type)
9997 if (TREE_CODE (type) == ERROR_MARK)
9998 return double_int::from_uhwi (BITS_PER_WORD);
9999 else if (TYPE_SIZE (type) == NULL_TREE)
10000 return double_int_zero;
10001 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10002 return tree_to_double_int (TYPE_SIZE (type));
10003 else
10004 return double_int::from_uhwi (TYPE_ALIGN (type));
10007 /* Given a pointer to a tree node for a subrange type, return a pointer
10008 to a DIE that describes the given type. */
10010 static dw_die_ref
10011 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10013 dw_die_ref subrange_die;
10014 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10016 if (context_die == NULL)
10017 context_die = comp_unit_die ();
10019 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10021 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10023 /* The size of the subrange type and its base type do not match,
10024 so we need to generate a size attribute for the subrange type. */
10025 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10028 if (low)
10029 add_bound_info (subrange_die, DW_AT_lower_bound, low);
10030 if (high)
10031 add_bound_info (subrange_die, DW_AT_upper_bound, high);
10033 return subrange_die;
10036 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10037 entry that chains various modifiers in front of the given type. */
10039 static dw_die_ref
10040 modified_type_die (tree type, int is_const_type, int is_volatile_type,
10041 dw_die_ref context_die)
10043 enum tree_code code = TREE_CODE (type);
10044 dw_die_ref mod_type_die;
10045 dw_die_ref sub_die = NULL;
10046 tree item_type = NULL;
10047 tree qualified_type;
10048 tree name, low, high;
10049 dw_die_ref mod_scope;
10051 if (code == ERROR_MARK)
10052 return NULL;
10054 /* See if we already have the appropriately qualified variant of
10055 this type. */
10056 qualified_type
10057 = get_qualified_type (type,
10058 ((is_const_type ? TYPE_QUAL_CONST : 0)
10059 | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
10061 if (qualified_type == sizetype
10062 && TYPE_NAME (qualified_type)
10063 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10065 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10067 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10068 && TYPE_PRECISION (t)
10069 == TYPE_PRECISION (qualified_type)
10070 && TYPE_UNSIGNED (t)
10071 == TYPE_UNSIGNED (qualified_type));
10072 qualified_type = t;
10075 /* If we do, then we can just use its DIE, if it exists. */
10076 if (qualified_type)
10078 mod_type_die = lookup_type_die (qualified_type);
10079 if (mod_type_die)
10080 return mod_type_die;
10083 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10085 /* Handle C typedef types. */
10086 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10087 && !DECL_ARTIFICIAL (name))
10089 tree dtype = TREE_TYPE (name);
10091 if (qualified_type == dtype)
10093 /* For a named type, use the typedef. */
10094 gen_type_die (qualified_type, context_die);
10095 return lookup_type_die (qualified_type);
10097 else if (is_const_type < TYPE_READONLY (dtype)
10098 || is_volatile_type < TYPE_VOLATILE (dtype)
10099 || (is_const_type <= TYPE_READONLY (dtype)
10100 && is_volatile_type <= TYPE_VOLATILE (dtype)
10101 && DECL_ORIGINAL_TYPE (name) != type))
10102 /* cv-unqualified version of named type. Just use the unnamed
10103 type to which it refers. */
10104 return modified_type_die (DECL_ORIGINAL_TYPE (name),
10105 is_const_type, is_volatile_type,
10106 context_die);
10107 /* Else cv-qualified version of named type; fall through. */
10110 mod_scope = scope_die_for (type, context_die);
10112 if (is_const_type
10113 /* If both is_const_type and is_volatile_type, prefer the path
10114 which leads to a qualified type. */
10115 && (!is_volatile_type
10116 || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
10117 || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
10119 mod_type_die = new_die (DW_TAG_const_type, mod_scope, type);
10120 sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
10122 else if (is_volatile_type)
10124 mod_type_die = new_die (DW_TAG_volatile_type, mod_scope, type);
10125 sub_die = modified_type_die (type, is_const_type, 0, context_die);
10127 else if (code == POINTER_TYPE)
10129 mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10130 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10131 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10132 item_type = TREE_TYPE (type);
10133 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10134 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10135 TYPE_ADDR_SPACE (item_type));
10137 else if (code == REFERENCE_TYPE)
10139 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10140 mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10141 type);
10142 else
10143 mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10144 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10145 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10146 item_type = TREE_TYPE (type);
10147 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10148 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10149 TYPE_ADDR_SPACE (item_type));
10151 else if (code == INTEGER_TYPE
10152 && TREE_TYPE (type) != NULL_TREE
10153 && subrange_type_for_debug_p (type, &low, &high))
10155 mod_type_die = subrange_type_die (type, low, high, context_die);
10156 item_type = TREE_TYPE (type);
10158 else if (is_base_type (type))
10159 mod_type_die = base_type_die (type);
10160 else
10162 gen_type_die (type, context_die);
10164 /* We have to get the type_main_variant here (and pass that to the
10165 `lookup_type_die' routine) because the ..._TYPE node we have
10166 might simply be a *copy* of some original type node (where the
10167 copy was created to help us keep track of typedef names) and
10168 that copy might have a different TYPE_UID from the original
10169 ..._TYPE node. */
10170 if (TREE_CODE (type) != VECTOR_TYPE)
10171 return lookup_type_die (type_main_variant (type));
10172 else
10173 /* Vectors have the debugging information in the type,
10174 not the main variant. */
10175 return lookup_type_die (type);
10178 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
10179 don't output a DW_TAG_typedef, since there isn't one in the
10180 user's program; just attach a DW_AT_name to the type.
10181 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10182 if the base type already has the same name. */
10183 if (name
10184 && ((TREE_CODE (name) != TYPE_DECL
10185 && (qualified_type == TYPE_MAIN_VARIANT (type)
10186 || (!is_const_type && !is_volatile_type)))
10187 || (TREE_CODE (name) == TYPE_DECL
10188 && TREE_TYPE (name) == qualified_type
10189 && DECL_NAME (name))))
10191 if (TREE_CODE (name) == TYPE_DECL)
10192 /* Could just call add_name_and_src_coords_attributes here,
10193 but since this is a builtin type it doesn't have any
10194 useful source coordinates anyway. */
10195 name = DECL_NAME (name);
10196 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10198 /* This probably indicates a bug. */
10199 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10201 name = TYPE_NAME (type);
10202 if (name
10203 && TREE_CODE (name) == TYPE_DECL)
10204 name = DECL_NAME (name);
10205 add_name_attribute (mod_type_die,
10206 name ? IDENTIFIER_POINTER (name) : "__unknown__");
10209 if (qualified_type)
10210 equate_type_number_to_die (qualified_type, mod_type_die);
10212 if (item_type)
10213 /* We must do this after the equate_type_number_to_die call, in case
10214 this is a recursive type. This ensures that the modified_type_die
10215 recursion will terminate even if the type is recursive. Recursive
10216 types are possible in Ada. */
10217 sub_die = modified_type_die (item_type,
10218 TYPE_READONLY (item_type),
10219 TYPE_VOLATILE (item_type),
10220 context_die);
10222 if (sub_die != NULL)
10223 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10225 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10226 if (TYPE_ARTIFICIAL (type))
10227 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10229 return mod_type_die;
10232 /* Generate DIEs for the generic parameters of T.
10233 T must be either a generic type or a generic function.
10234 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
10236 static void
10237 gen_generic_params_dies (tree t)
10239 tree parms, args;
10240 int parms_num, i;
10241 dw_die_ref die = NULL;
10243 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10244 return;
10246 if (TYPE_P (t))
10247 die = lookup_type_die (t);
10248 else if (DECL_P (t))
10249 die = lookup_decl_die (t);
10251 gcc_assert (die);
10253 parms = lang_hooks.get_innermost_generic_parms (t);
10254 if (!parms)
10255 /* T has no generic parameter. It means T is neither a generic type
10256 or function. End of story. */
10257 return;
10259 parms_num = TREE_VEC_LENGTH (parms);
10260 args = lang_hooks.get_innermost_generic_args (t);
10261 for (i = 0; i < parms_num; i++)
10263 tree parm, arg, arg_pack_elems;
10265 parm = TREE_VEC_ELT (parms, i);
10266 arg = TREE_VEC_ELT (args, i);
10267 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
10268 gcc_assert (parm && TREE_VALUE (parm) && arg);
10270 if (parm && TREE_VALUE (parm) && arg)
10272 /* If PARM represents a template parameter pack,
10273 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
10274 by DW_TAG_template_*_parameter DIEs for the argument
10275 pack elements of ARG. Note that ARG would then be
10276 an argument pack. */
10277 if (arg_pack_elems)
10278 template_parameter_pack_die (TREE_VALUE (parm),
10279 arg_pack_elems,
10280 die);
10281 else
10282 generic_parameter_die (TREE_VALUE (parm), arg,
10283 true /* Emit DW_AT_name */, die);
10288 /* Create and return a DIE for PARM which should be
10289 the representation of a generic type parameter.
10290 For instance, in the C++ front end, PARM would be a template parameter.
10291 ARG is the argument to PARM.
10292 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
10293 name of the PARM.
10294 PARENT_DIE is the parent DIE which the new created DIE should be added to,
10295 as a child node. */
10297 static dw_die_ref
10298 generic_parameter_die (tree parm, tree arg,
10299 bool emit_name_p,
10300 dw_die_ref parent_die)
10302 dw_die_ref tmpl_die = NULL;
10303 const char *name = NULL;
10305 if (!parm || !DECL_NAME (parm) || !arg)
10306 return NULL;
10308 /* We support non-type generic parameters and arguments,
10309 type generic parameters and arguments, as well as
10310 generic generic parameters (a.k.a. template template parameters in C++)
10311 and arguments. */
10312 if (TREE_CODE (parm) == PARM_DECL)
10313 /* PARM is a nontype generic parameter */
10314 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10315 else if (TREE_CODE (parm) == TYPE_DECL)
10316 /* PARM is a type generic parameter. */
10317 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10318 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10319 /* PARM is a generic generic parameter.
10320 Its DIE is a GNU extension. It shall have a
10321 DW_AT_name attribute to represent the name of the template template
10322 parameter, and a DW_AT_GNU_template_name attribute to represent the
10323 name of the template template argument. */
10324 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10325 parent_die, parm);
10326 else
10327 gcc_unreachable ();
10329 if (tmpl_die)
10331 tree tmpl_type;
10333 /* If PARM is a generic parameter pack, it means we are
10334 emitting debug info for a template argument pack element.
10335 In other terms, ARG is a template argument pack element.
10336 In that case, we don't emit any DW_AT_name attribute for
10337 the die. */
10338 if (emit_name_p)
10340 name = IDENTIFIER_POINTER (DECL_NAME (parm));
10341 gcc_assert (name);
10342 add_AT_string (tmpl_die, DW_AT_name, name);
10345 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10347 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10348 TMPL_DIE should have a child DW_AT_type attribute that is set
10349 to the type of the argument to PARM, which is ARG.
10350 If PARM is a type generic parameter, TMPL_DIE should have a
10351 child DW_AT_type that is set to ARG. */
10352 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10353 add_type_attribute (tmpl_die, tmpl_type, 0,
10354 TREE_THIS_VOLATILE (tmpl_type),
10355 parent_die);
10357 else
10359 /* So TMPL_DIE is a DIE representing a
10360 a generic generic template parameter, a.k.a template template
10361 parameter in C++ and arg is a template. */
10363 /* The DW_AT_GNU_template_name attribute of the DIE must be set
10364 to the name of the argument. */
10365 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
10366 if (name)
10367 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
10370 if (TREE_CODE (parm) == PARM_DECL)
10371 /* So PARM is a non-type generic parameter.
10372 DWARF3 5.6.8 says we must set a DW_AT_const_value child
10373 attribute of TMPL_DIE which value represents the value
10374 of ARG.
10375 We must be careful here:
10376 The value of ARG might reference some function decls.
10377 We might currently be emitting debug info for a generic
10378 type and types are emitted before function decls, we don't
10379 know if the function decls referenced by ARG will actually be
10380 emitted after cgraph computations.
10381 So must defer the generation of the DW_AT_const_value to
10382 after cgraph is ready. */
10383 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
10386 return tmpl_die;
10389 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
10390 PARM_PACK must be a template parameter pack. The returned DIE
10391 will be child DIE of PARENT_DIE. */
10393 static dw_die_ref
10394 template_parameter_pack_die (tree parm_pack,
10395 tree parm_pack_args,
10396 dw_die_ref parent_die)
10398 dw_die_ref die;
10399 int j;
10401 gcc_assert (parent_die && parm_pack);
10403 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
10404 add_name_and_src_coords_attributes (die, parm_pack);
10405 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
10406 generic_parameter_die (parm_pack,
10407 TREE_VEC_ELT (parm_pack_args, j),
10408 false /* Don't emit DW_AT_name */,
10409 die);
10410 return die;
10413 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
10414 an enumerated type. */
10416 static inline int
10417 type_is_enum (const_tree type)
10419 return TREE_CODE (type) == ENUMERAL_TYPE;
10422 /* Return the DBX register number described by a given RTL node. */
10424 static unsigned int
10425 dbx_reg_number (const_rtx rtl)
10427 unsigned regno = REGNO (rtl);
10429 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10431 #ifdef LEAF_REG_REMAP
10432 if (crtl->uses_only_leaf_regs)
10434 int leaf_reg = LEAF_REG_REMAP (regno);
10435 if (leaf_reg != -1)
10436 regno = (unsigned) leaf_reg;
10438 #endif
10440 regno = DBX_REGISTER_NUMBER (regno);
10441 gcc_assert (regno != INVALID_REGNUM);
10442 return regno;
10445 /* Optionally add a DW_OP_piece term to a location description expression.
10446 DW_OP_piece is only added if the location description expression already
10447 doesn't end with DW_OP_piece. */
10449 static void
10450 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10452 dw_loc_descr_ref loc;
10454 if (*list_head != NULL)
10456 /* Find the end of the chain. */
10457 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10460 if (loc->dw_loc_opc != DW_OP_piece)
10461 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10465 /* Return a location descriptor that designates a machine register or
10466 zero if there is none. */
10468 static dw_loc_descr_ref
10469 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10471 rtx regs;
10473 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10474 return 0;
10476 /* We only use "frame base" when we're sure we're talking about the
10477 post-prologue local stack frame. We do this by *not* running
10478 register elimination until this point, and recognizing the special
10479 argument pointer and soft frame pointer rtx's.
10480 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
10481 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
10482 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
10484 dw_loc_descr_ref result = NULL;
10486 if (dwarf_version >= 4 || !dwarf_strict)
10488 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
10489 initialized);
10490 if (result)
10491 add_loc_descr (&result,
10492 new_loc_descr (DW_OP_stack_value, 0, 0));
10494 return result;
10497 regs = targetm.dwarf_register_span (rtl);
10499 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
10500 return multiple_reg_loc_descriptor (rtl, regs, initialized);
10501 else
10502 return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
10505 /* Return a location descriptor that designates a machine register for
10506 a given hard register number. */
10508 static dw_loc_descr_ref
10509 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
10511 dw_loc_descr_ref reg_loc_descr;
10513 if (regno <= 31)
10514 reg_loc_descr
10515 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
10516 else
10517 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
10519 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10520 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10522 return reg_loc_descr;
10525 /* Given an RTL of a register, return a location descriptor that
10526 designates a value that spans more than one register. */
10528 static dw_loc_descr_ref
10529 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
10530 enum var_init_status initialized)
10532 int nregs, size, i;
10533 unsigned reg;
10534 dw_loc_descr_ref loc_result = NULL;
10536 reg = REGNO (rtl);
10537 #ifdef LEAF_REG_REMAP
10538 if (crtl->uses_only_leaf_regs)
10540 int leaf_reg = LEAF_REG_REMAP (reg);
10541 if (leaf_reg != -1)
10542 reg = (unsigned) leaf_reg;
10544 #endif
10545 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
10546 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
10548 /* Simple, contiguous registers. */
10549 if (regs == NULL_RTX)
10551 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
10553 loc_result = NULL;
10554 while (nregs--)
10556 dw_loc_descr_ref t;
10558 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
10559 VAR_INIT_STATUS_INITIALIZED);
10560 add_loc_descr (&loc_result, t);
10561 add_loc_descr_op_piece (&loc_result, size);
10562 ++reg;
10564 return loc_result;
10567 /* Now onto stupid register sets in non contiguous locations. */
10569 gcc_assert (GET_CODE (regs) == PARALLEL);
10571 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10572 loc_result = NULL;
10574 for (i = 0; i < XVECLEN (regs, 0); ++i)
10576 dw_loc_descr_ref t;
10578 t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
10579 VAR_INIT_STATUS_INITIALIZED);
10580 add_loc_descr (&loc_result, t);
10581 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10582 add_loc_descr_op_piece (&loc_result, size);
10585 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10586 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10587 return loc_result;
10590 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
10592 /* Return a location descriptor that designates a constant i,
10593 as a compound operation from constant (i >> shift), constant shift
10594 and DW_OP_shl. */
10596 static dw_loc_descr_ref
10597 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10599 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
10600 add_loc_descr (&ret, int_loc_descriptor (shift));
10601 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
10602 return ret;
10605 /* Return a location descriptor that designates a constant. */
10607 static dw_loc_descr_ref
10608 int_loc_descriptor (HOST_WIDE_INT i)
10610 enum dwarf_location_atom op;
10612 /* Pick the smallest representation of a constant, rather than just
10613 defaulting to the LEB encoding. */
10614 if (i >= 0)
10616 int clz = clz_hwi (i);
10617 int ctz = ctz_hwi (i);
10618 if (i <= 31)
10619 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
10620 else if (i <= 0xff)
10621 op = DW_OP_const1u;
10622 else if (i <= 0xffff)
10623 op = DW_OP_const2u;
10624 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10625 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10626 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
10627 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
10628 while DW_OP_const4u is 5 bytes. */
10629 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
10630 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10631 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10632 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
10633 while DW_OP_const4u is 5 bytes. */
10634 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10635 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10636 op = DW_OP_const4u;
10637 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10638 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10639 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
10640 while DW_OP_constu of constant >= 0x100000000 takes at least
10641 6 bytes. */
10642 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10643 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10644 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
10645 >= HOST_BITS_PER_WIDE_INT)
10646 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
10647 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
10648 while DW_OP_constu takes in this case at least 6 bytes. */
10649 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
10650 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10651 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10652 && size_of_uleb128 (i) > 6)
10653 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
10654 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
10655 else
10656 op = DW_OP_constu;
10658 else
10660 if (i >= -0x80)
10661 op = DW_OP_const1s;
10662 else if (i >= -0x8000)
10663 op = DW_OP_const2s;
10664 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10666 if (size_of_int_loc_descriptor (i) < 5)
10668 dw_loc_descr_ref ret = int_loc_descriptor (-i);
10669 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10670 return ret;
10672 op = DW_OP_const4s;
10674 else
10676 if (size_of_int_loc_descriptor (i)
10677 < (unsigned long) 1 + size_of_sleb128 (i))
10679 dw_loc_descr_ref ret = int_loc_descriptor (-i);
10680 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10681 return ret;
10683 op = DW_OP_consts;
10687 return new_loc_descr (op, i, 0);
10690 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
10691 without actually allocating it. */
10693 static unsigned long
10694 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10696 return size_of_int_loc_descriptor (i >> shift)
10697 + size_of_int_loc_descriptor (shift)
10698 + 1;
10701 /* Return size_of_locs (int_loc_descriptor (i)) without
10702 actually allocating it. */
10704 static unsigned long
10705 size_of_int_loc_descriptor (HOST_WIDE_INT i)
10707 unsigned long s;
10709 if (i >= 0)
10711 int clz, ctz;
10712 if (i <= 31)
10713 return 1;
10714 else if (i <= 0xff)
10715 return 2;
10716 else if (i <= 0xffff)
10717 return 3;
10718 clz = clz_hwi (i);
10719 ctz = ctz_hwi (i);
10720 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10721 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10722 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10723 - clz - 5);
10724 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10725 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10726 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10727 - clz - 8);
10728 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10729 return 5;
10730 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
10731 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10732 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10733 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10734 - clz - 8);
10735 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10736 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
10737 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10738 - clz - 16);
10739 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10740 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10741 && s > 6)
10742 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10743 - clz - 32);
10744 else
10745 return 1 + s;
10747 else
10749 if (i >= -0x80)
10750 return 2;
10751 else if (i >= -0x8000)
10752 return 3;
10753 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10755 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
10757 s = size_of_int_loc_descriptor (-i) + 1;
10758 if (s < 5)
10759 return s;
10761 return 5;
10763 else
10765 unsigned long r = 1 + size_of_sleb128 (i);
10766 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
10768 s = size_of_int_loc_descriptor (-i) + 1;
10769 if (s < r)
10770 return s;
10772 return r;
10777 /* Return loc description representing "address" of integer value.
10778 This can appear only as toplevel expression. */
10780 static dw_loc_descr_ref
10781 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
10783 int litsize;
10784 dw_loc_descr_ref loc_result = NULL;
10786 if (!(dwarf_version >= 4 || !dwarf_strict))
10787 return NULL;
10789 litsize = size_of_int_loc_descriptor (i);
10790 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
10791 is more compact. For DW_OP_stack_value we need:
10792 litsize + 1 (DW_OP_stack_value)
10793 and for DW_OP_implicit_value:
10794 1 (DW_OP_implicit_value) + 1 (length) + size. */
10795 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
10797 loc_result = int_loc_descriptor (i);
10798 add_loc_descr (&loc_result,
10799 new_loc_descr (DW_OP_stack_value, 0, 0));
10800 return loc_result;
10803 loc_result = new_loc_descr (DW_OP_implicit_value,
10804 size, 0);
10805 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
10806 loc_result->dw_loc_oprnd2.v.val_int = i;
10807 return loc_result;
10810 /* Return a location descriptor that designates a base+offset location. */
10812 static dw_loc_descr_ref
10813 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
10814 enum var_init_status initialized)
10816 unsigned int regno;
10817 dw_loc_descr_ref result;
10818 dw_fde_ref fde = cfun->fde;
10820 /* We only use "frame base" when we're sure we're talking about the
10821 post-prologue local stack frame. We do this by *not* running
10822 register elimination until this point, and recognizing the special
10823 argument pointer and soft frame pointer rtx's. */
10824 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
10826 rtx elim = (ira_use_lra_p
10827 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
10828 : eliminate_regs (reg, VOIDmode, NULL_RTX));
10830 if (elim != reg)
10832 if (GET_CODE (elim) == PLUS)
10834 offset += INTVAL (XEXP (elim, 1));
10835 elim = XEXP (elim, 0);
10837 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
10838 && (elim == hard_frame_pointer_rtx
10839 || elim == stack_pointer_rtx))
10840 || elim == (frame_pointer_needed
10841 ? hard_frame_pointer_rtx
10842 : stack_pointer_rtx));
10844 /* If drap register is used to align stack, use frame
10845 pointer + offset to access stack variables. If stack
10846 is aligned without drap, use stack pointer + offset to
10847 access stack variables. */
10848 if (crtl->stack_realign_tried
10849 && reg == frame_pointer_rtx)
10851 int base_reg
10852 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
10853 ? HARD_FRAME_POINTER_REGNUM
10854 : REGNO (elim));
10855 return new_reg_loc_descr (base_reg, offset);
10858 gcc_assert (frame_pointer_fb_offset_valid);
10859 offset += frame_pointer_fb_offset;
10860 return new_loc_descr (DW_OP_fbreg, offset, 0);
10864 regno = DWARF_FRAME_REGNUM (REGNO (reg));
10866 if (!optimize && fde
10867 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
10869 /* Use cfa+offset to represent the location of arguments passed
10870 on the stack when drap is used to align stack.
10871 Only do this when not optimizing, for optimized code var-tracking
10872 is supposed to track where the arguments live and the register
10873 used as vdrap or drap in some spot might be used for something
10874 else in other part of the routine. */
10875 return new_loc_descr (DW_OP_fbreg, offset, 0);
10878 if (regno <= 31)
10879 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
10880 offset, 0);
10881 else
10882 result = new_loc_descr (DW_OP_bregx, regno, offset);
10884 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10885 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10887 return result;
10890 /* Return true if this RTL expression describes a base+offset calculation. */
10892 static inline int
10893 is_based_loc (const_rtx rtl)
10895 return (GET_CODE (rtl) == PLUS
10896 && ((REG_P (XEXP (rtl, 0))
10897 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
10898 && CONST_INT_P (XEXP (rtl, 1)))));
10901 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
10902 failed. */
10904 static dw_loc_descr_ref
10905 tls_mem_loc_descriptor (rtx mem)
10907 tree base;
10908 dw_loc_descr_ref loc_result;
10910 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
10911 return NULL;
10913 base = get_base_address (MEM_EXPR (mem));
10914 if (base == NULL
10915 || TREE_CODE (base) != VAR_DECL
10916 || !DECL_THREAD_LOCAL_P (base))
10917 return NULL;
10919 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
10920 if (loc_result == NULL)
10921 return NULL;
10923 if (MEM_OFFSET (mem))
10924 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
10926 return loc_result;
10929 /* Output debug info about reason why we failed to expand expression as dwarf
10930 expression. */
10932 static void
10933 expansion_failed (tree expr, rtx rtl, char const *reason)
10935 if (dump_file && (dump_flags & TDF_DETAILS))
10937 fprintf (dump_file, "Failed to expand as dwarf: ");
10938 if (expr)
10939 print_generic_expr (dump_file, expr, dump_flags);
10940 if (rtl)
10942 fprintf (dump_file, "\n");
10943 print_rtl (dump_file, rtl);
10945 fprintf (dump_file, "\nReason: %s\n", reason);
10949 /* Helper function for const_ok_for_output, called either directly
10950 or via for_each_rtx. */
10952 static int
10953 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
10955 rtx rtl = *rtlp;
10957 if (GET_CODE (rtl) == UNSPEC)
10959 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
10960 we can't express it in the debug info. */
10961 #ifdef ENABLE_CHECKING
10962 /* Don't complain about TLS UNSPECs, those are just too hard to
10963 delegitimize. Note this could be a non-decl SYMBOL_REF such as
10964 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
10965 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
10966 if (XVECLEN (rtl, 0) == 0
10967 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
10968 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
10969 inform (current_function_decl
10970 ? DECL_SOURCE_LOCATION (current_function_decl)
10971 : UNKNOWN_LOCATION,
10972 #if NUM_UNSPEC_VALUES > 0
10973 "non-delegitimized UNSPEC %s (%d) found in variable location",
10974 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
10975 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
10976 XINT (rtl, 1));
10977 #else
10978 "non-delegitimized UNSPEC %d found in variable location",
10979 XINT (rtl, 1));
10980 #endif
10981 #endif
10982 expansion_failed (NULL_TREE, rtl,
10983 "UNSPEC hasn't been delegitimized.\n");
10984 return 1;
10987 if (targetm.const_not_ok_for_debug_p (rtl))
10989 expansion_failed (NULL_TREE, rtl,
10990 "Expression rejected for debug by the backend.\n");
10991 return 1;
10994 if (GET_CODE (rtl) != SYMBOL_REF)
10995 return 0;
10997 if (CONSTANT_POOL_ADDRESS_P (rtl))
10999 bool marked;
11000 get_pool_constant_mark (rtl, &marked);
11001 /* If all references to this pool constant were optimized away,
11002 it was not output and thus we can't represent it. */
11003 if (!marked)
11005 expansion_failed (NULL_TREE, rtl,
11006 "Constant was removed from constant pool.\n");
11007 return 1;
11011 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11012 return 1;
11014 /* Avoid references to external symbols in debug info, on several targets
11015 the linker might even refuse to link when linking a shared library,
11016 and in many other cases the relocations for .debug_info/.debug_loc are
11017 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
11018 to be defined within the same shared library or executable are fine. */
11019 if (SYMBOL_REF_EXTERNAL_P (rtl))
11021 tree decl = SYMBOL_REF_DECL (rtl);
11023 if (decl == NULL || !targetm.binds_local_p (decl))
11025 expansion_failed (NULL_TREE, rtl,
11026 "Symbol not defined in current TU.\n");
11027 return 1;
11031 return 0;
11034 /* Return true if constant RTL can be emitted in DW_OP_addr or
11035 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
11036 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
11038 static bool
11039 const_ok_for_output (rtx rtl)
11041 if (GET_CODE (rtl) == SYMBOL_REF)
11042 return const_ok_for_output_1 (&rtl, NULL) == 0;
11044 if (GET_CODE (rtl) == CONST)
11045 return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
11047 return true;
11050 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11051 if possible, NULL otherwise. */
11053 static dw_die_ref
11054 base_type_for_mode (enum machine_mode mode, bool unsignedp)
11056 dw_die_ref type_die;
11057 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11059 if (type == NULL)
11060 return NULL;
11061 switch (TREE_CODE (type))
11063 case INTEGER_TYPE:
11064 case REAL_TYPE:
11065 break;
11066 default:
11067 return NULL;
11069 type_die = lookup_type_die (type);
11070 if (!type_die)
11071 type_die = modified_type_die (type, false, false, comp_unit_die ());
11072 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11073 return NULL;
11074 return type_die;
11077 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11078 type matching MODE, or, if MODE is narrower than or as wide as
11079 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
11080 possible. */
11082 static dw_loc_descr_ref
11083 convert_descriptor_to_mode (enum machine_mode mode, dw_loc_descr_ref op)
11085 enum machine_mode outer_mode = mode;
11086 dw_die_ref type_die;
11087 dw_loc_descr_ref cvt;
11089 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11091 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11092 return op;
11094 type_die = base_type_for_mode (outer_mode, 1);
11095 if (type_die == NULL)
11096 return NULL;
11097 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11098 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11099 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11100 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11101 add_loc_descr (&op, cvt);
11102 return op;
11105 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
11107 static dw_loc_descr_ref
11108 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11109 dw_loc_descr_ref op1)
11111 dw_loc_descr_ref ret = op0;
11112 add_loc_descr (&ret, op1);
11113 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11114 if (STORE_FLAG_VALUE != 1)
11116 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11117 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11119 return ret;
11122 /* Return location descriptor for signed comparison OP RTL. */
11124 static dw_loc_descr_ref
11125 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11126 enum machine_mode mem_mode)
11128 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11129 dw_loc_descr_ref op0, op1;
11130 int shift;
11132 if (op_mode == VOIDmode)
11133 op_mode = GET_MODE (XEXP (rtl, 1));
11134 if (op_mode == VOIDmode)
11135 return NULL;
11137 if (dwarf_strict
11138 && (GET_MODE_CLASS (op_mode) != MODE_INT
11139 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11140 return NULL;
11142 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11143 VAR_INIT_STATUS_INITIALIZED);
11144 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11145 VAR_INIT_STATUS_INITIALIZED);
11147 if (op0 == NULL || op1 == NULL)
11148 return NULL;
11150 if (GET_MODE_CLASS (op_mode) != MODE_INT
11151 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11152 return compare_loc_descriptor (op, op0, op1);
11154 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11156 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11157 dw_loc_descr_ref cvt;
11159 if (type_die == NULL)
11160 return NULL;
11161 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11162 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11163 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11164 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11165 add_loc_descr (&op0, cvt);
11166 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11167 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11168 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11169 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11170 add_loc_descr (&op1, cvt);
11171 return compare_loc_descriptor (op, op0, op1);
11174 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11175 /* For eq/ne, if the operands are known to be zero-extended,
11176 there is no need to do the fancy shifting up. */
11177 if (op == DW_OP_eq || op == DW_OP_ne)
11179 dw_loc_descr_ref last0, last1;
11180 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11182 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11184 /* deref_size zero extends, and for constants we can check
11185 whether they are zero extended or not. */
11186 if (((last0->dw_loc_opc == DW_OP_deref_size
11187 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11188 || (CONST_INT_P (XEXP (rtl, 0))
11189 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11190 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11191 && ((last1->dw_loc_opc == DW_OP_deref_size
11192 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11193 || (CONST_INT_P (XEXP (rtl, 1))
11194 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11195 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11196 return compare_loc_descriptor (op, op0, op1);
11198 /* EQ/NE comparison against constant in narrower type than
11199 DWARF2_ADDR_SIZE can be performed either as
11200 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11201 DW_OP_{eq,ne}
11203 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11204 DW_OP_{eq,ne}. Pick whatever is shorter. */
11205 if (CONST_INT_P (XEXP (rtl, 1))
11206 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11207 && (size_of_int_loc_descriptor (shift) + 1
11208 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11209 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11210 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11211 & GET_MODE_MASK (op_mode))))
11213 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
11214 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11215 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11216 & GET_MODE_MASK (op_mode));
11217 return compare_loc_descriptor (op, op0, op1);
11220 add_loc_descr (&op0, int_loc_descriptor (shift));
11221 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11222 if (CONST_INT_P (XEXP (rtl, 1)))
11223 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11224 else
11226 add_loc_descr (&op1, int_loc_descriptor (shift));
11227 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11229 return compare_loc_descriptor (op, op0, op1);
11232 /* Return location descriptor for unsigned comparison OP RTL. */
11234 static dw_loc_descr_ref
11235 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11236 enum machine_mode mem_mode)
11238 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11239 dw_loc_descr_ref op0, op1;
11241 if (op_mode == VOIDmode)
11242 op_mode = GET_MODE (XEXP (rtl, 1));
11243 if (op_mode == VOIDmode)
11244 return NULL;
11245 if (GET_MODE_CLASS (op_mode) != MODE_INT)
11246 return NULL;
11248 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11249 return NULL;
11251 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11252 VAR_INIT_STATUS_INITIALIZED);
11253 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11254 VAR_INIT_STATUS_INITIALIZED);
11256 if (op0 == NULL || op1 == NULL)
11257 return NULL;
11259 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
11261 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
11262 dw_loc_descr_ref last0, last1;
11263 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11265 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11267 if (CONST_INT_P (XEXP (rtl, 0)))
11268 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
11269 /* deref_size zero extends, so no need to mask it again. */
11270 else if (last0->dw_loc_opc != DW_OP_deref_size
11271 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11273 add_loc_descr (&op0, int_loc_descriptor (mask));
11274 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11276 if (CONST_INT_P (XEXP (rtl, 1)))
11277 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11278 /* deref_size zero extends, so no need to mask it again. */
11279 else if (last1->dw_loc_opc != DW_OP_deref_size
11280 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11282 add_loc_descr (&op1, int_loc_descriptor (mask));
11283 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11286 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11288 HOST_WIDE_INT bias = 1;
11289 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11290 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11291 if (CONST_INT_P (XEXP (rtl, 1)))
11292 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
11293 + INTVAL (XEXP (rtl, 1)));
11294 else
11295 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
11296 bias, 0));
11298 return compare_loc_descriptor (op, op0, op1);
11301 /* Return location descriptor for {U,S}{MIN,MAX}. */
11303 static dw_loc_descr_ref
11304 minmax_loc_descriptor (rtx rtl, enum machine_mode mode,
11305 enum machine_mode mem_mode)
11307 enum dwarf_location_atom op;
11308 dw_loc_descr_ref op0, op1, ret;
11309 dw_loc_descr_ref bra_node, drop_node;
11311 if (dwarf_strict
11312 && (GET_MODE_CLASS (mode) != MODE_INT
11313 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
11314 return NULL;
11316 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11317 VAR_INIT_STATUS_INITIALIZED);
11318 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11319 VAR_INIT_STATUS_INITIALIZED);
11321 if (op0 == NULL || op1 == NULL)
11322 return NULL;
11324 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
11325 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
11326 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
11327 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
11329 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11331 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11332 add_loc_descr (&op0, int_loc_descriptor (mask));
11333 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11334 add_loc_descr (&op1, int_loc_descriptor (mask));
11335 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11337 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11339 HOST_WIDE_INT bias = 1;
11340 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11341 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11342 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11345 else if (GET_MODE_CLASS (mode) == MODE_INT
11346 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11348 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
11349 add_loc_descr (&op0, int_loc_descriptor (shift));
11350 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11351 add_loc_descr (&op1, int_loc_descriptor (shift));
11352 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11354 else if (GET_MODE_CLASS (mode) == MODE_INT
11355 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11357 dw_die_ref type_die = base_type_for_mode (mode, 0);
11358 dw_loc_descr_ref cvt;
11359 if (type_die == NULL)
11360 return NULL;
11361 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11362 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11363 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11364 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11365 add_loc_descr (&op0, cvt);
11366 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11367 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11368 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11369 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11370 add_loc_descr (&op1, cvt);
11373 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
11374 op = DW_OP_lt;
11375 else
11376 op = DW_OP_gt;
11377 ret = op0;
11378 add_loc_descr (&ret, op1);
11379 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11380 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11381 add_loc_descr (&ret, bra_node);
11382 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11383 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11384 add_loc_descr (&ret, drop_node);
11385 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11386 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11387 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
11388 && GET_MODE_CLASS (mode) == MODE_INT
11389 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11390 ret = convert_descriptor_to_mode (mode, ret);
11391 return ret;
11394 /* Helper function for mem_loc_descriptor. Perform OP binary op,
11395 but after converting arguments to type_die, afterwards
11396 convert back to unsigned. */
11398 static dw_loc_descr_ref
11399 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
11400 enum machine_mode mode, enum machine_mode mem_mode)
11402 dw_loc_descr_ref cvt, op0, op1;
11404 if (type_die == NULL)
11405 return NULL;
11406 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11407 VAR_INIT_STATUS_INITIALIZED);
11408 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11409 VAR_INIT_STATUS_INITIALIZED);
11410 if (op0 == NULL || op1 == NULL)
11411 return NULL;
11412 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11413 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11414 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11415 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11416 add_loc_descr (&op0, cvt);
11417 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11418 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11419 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11420 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11421 add_loc_descr (&op1, cvt);
11422 add_loc_descr (&op0, op1);
11423 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
11424 return convert_descriptor_to_mode (mode, op0);
11427 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
11428 const0 is DW_OP_lit0 or corresponding typed constant,
11429 const1 is DW_OP_lit1 or corresponding typed constant
11430 and constMSB is constant with just the MSB bit set
11431 for the mode):
11432 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11433 L1: const0 DW_OP_swap
11434 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
11435 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11436 L3: DW_OP_drop
11437 L4: DW_OP_nop
11439 CTZ is similar:
11440 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11441 L1: const0 DW_OP_swap
11442 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11443 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11444 L3: DW_OP_drop
11445 L4: DW_OP_nop
11447 FFS is similar:
11448 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
11449 L1: const1 DW_OP_swap
11450 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11451 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11452 L3: DW_OP_drop
11453 L4: DW_OP_nop */
11455 static dw_loc_descr_ref
11456 clz_loc_descriptor (rtx rtl, enum machine_mode mode,
11457 enum machine_mode mem_mode)
11459 dw_loc_descr_ref op0, ret, tmp;
11460 HOST_WIDE_INT valv;
11461 dw_loc_descr_ref l1jump, l1label;
11462 dw_loc_descr_ref l2jump, l2label;
11463 dw_loc_descr_ref l3jump, l3label;
11464 dw_loc_descr_ref l4jump, l4label;
11465 rtx msb;
11467 if (GET_MODE_CLASS (mode) != MODE_INT
11468 || GET_MODE (XEXP (rtl, 0)) != mode
11469 || (GET_CODE (rtl) == CLZ
11470 && GET_MODE_BITSIZE (mode) > HOST_BITS_PER_DOUBLE_INT))
11471 return NULL;
11473 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11474 VAR_INIT_STATUS_INITIALIZED);
11475 if (op0 == NULL)
11476 return NULL;
11477 ret = op0;
11478 if (GET_CODE (rtl) == CLZ)
11480 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11481 valv = GET_MODE_BITSIZE (mode);
11483 else if (GET_CODE (rtl) == FFS)
11484 valv = 0;
11485 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11486 valv = GET_MODE_BITSIZE (mode);
11487 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11488 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
11489 add_loc_descr (&ret, l1jump);
11490 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11491 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
11492 VAR_INIT_STATUS_INITIALIZED);
11493 if (tmp == NULL)
11494 return NULL;
11495 add_loc_descr (&ret, tmp);
11496 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
11497 add_loc_descr (&ret, l4jump);
11498 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
11499 ? const1_rtx : const0_rtx,
11500 mode, mem_mode,
11501 VAR_INIT_STATUS_INITIALIZED);
11502 if (l1label == NULL)
11503 return NULL;
11504 add_loc_descr (&ret, l1label);
11505 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11506 l2label = new_loc_descr (DW_OP_dup, 0, 0);
11507 add_loc_descr (&ret, l2label);
11508 if (GET_CODE (rtl) != CLZ)
11509 msb = const1_rtx;
11510 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11511 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
11512 << (GET_MODE_BITSIZE (mode) - 1));
11513 else
11514 msb = immed_double_const (0, (unsigned HOST_WIDE_INT) 1
11515 << (GET_MODE_BITSIZE (mode)
11516 - HOST_BITS_PER_WIDE_INT - 1), mode);
11517 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
11518 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11519 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
11520 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
11521 else
11522 tmp = mem_loc_descriptor (msb, mode, mem_mode,
11523 VAR_INIT_STATUS_INITIALIZED);
11524 if (tmp == NULL)
11525 return NULL;
11526 add_loc_descr (&ret, tmp);
11527 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11528 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
11529 add_loc_descr (&ret, l3jump);
11530 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11531 VAR_INIT_STATUS_INITIALIZED);
11532 if (tmp == NULL)
11533 return NULL;
11534 add_loc_descr (&ret, tmp);
11535 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
11536 ? DW_OP_shl : DW_OP_shr, 0, 0));
11537 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11538 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
11539 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11540 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
11541 add_loc_descr (&ret, l2jump);
11542 l3label = new_loc_descr (DW_OP_drop, 0, 0);
11543 add_loc_descr (&ret, l3label);
11544 l4label = new_loc_descr (DW_OP_nop, 0, 0);
11545 add_loc_descr (&ret, l4label);
11546 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11547 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11548 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11549 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11550 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11551 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
11552 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11553 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
11554 return ret;
11557 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
11558 const1 is DW_OP_lit1 or corresponding typed constant):
11559 const0 DW_OP_swap
11560 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11561 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11562 L2: DW_OP_drop
11564 PARITY is similar:
11565 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11566 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11567 L2: DW_OP_drop */
11569 static dw_loc_descr_ref
11570 popcount_loc_descriptor (rtx rtl, enum machine_mode mode,
11571 enum machine_mode mem_mode)
11573 dw_loc_descr_ref op0, ret, tmp;
11574 dw_loc_descr_ref l1jump, l1label;
11575 dw_loc_descr_ref l2jump, l2label;
11577 if (GET_MODE_CLASS (mode) != MODE_INT
11578 || GET_MODE (XEXP (rtl, 0)) != mode)
11579 return NULL;
11581 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11582 VAR_INIT_STATUS_INITIALIZED);
11583 if (op0 == NULL)
11584 return NULL;
11585 ret = op0;
11586 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11587 VAR_INIT_STATUS_INITIALIZED);
11588 if (tmp == NULL)
11589 return NULL;
11590 add_loc_descr (&ret, tmp);
11591 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11592 l1label = new_loc_descr (DW_OP_dup, 0, 0);
11593 add_loc_descr (&ret, l1label);
11594 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11595 add_loc_descr (&ret, l2jump);
11596 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11597 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11598 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11599 VAR_INIT_STATUS_INITIALIZED);
11600 if (tmp == NULL)
11601 return NULL;
11602 add_loc_descr (&ret, tmp);
11603 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11604 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
11605 ? DW_OP_plus : DW_OP_xor, 0, 0));
11606 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11607 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11608 VAR_INIT_STATUS_INITIALIZED);
11609 add_loc_descr (&ret, tmp);
11610 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11611 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11612 add_loc_descr (&ret, l1jump);
11613 l2label = new_loc_descr (DW_OP_drop, 0, 0);
11614 add_loc_descr (&ret, l2label);
11615 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11616 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11617 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11618 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11619 return ret;
11622 /* BSWAP (constS is initial shift count, either 56 or 24):
11623 constS const0
11624 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
11625 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
11626 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
11627 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
11628 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
11630 static dw_loc_descr_ref
11631 bswap_loc_descriptor (rtx rtl, enum machine_mode mode,
11632 enum machine_mode mem_mode)
11634 dw_loc_descr_ref op0, ret, tmp;
11635 dw_loc_descr_ref l1jump, l1label;
11636 dw_loc_descr_ref l2jump, l2label;
11638 if (GET_MODE_CLASS (mode) != MODE_INT
11639 || BITS_PER_UNIT != 8
11640 || (GET_MODE_BITSIZE (mode) != 32
11641 && GET_MODE_BITSIZE (mode) != 64))
11642 return NULL;
11644 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11645 VAR_INIT_STATUS_INITIALIZED);
11646 if (op0 == NULL)
11647 return NULL;
11649 ret = op0;
11650 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11651 mode, mem_mode,
11652 VAR_INIT_STATUS_INITIALIZED);
11653 if (tmp == NULL)
11654 return NULL;
11655 add_loc_descr (&ret, tmp);
11656 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11657 VAR_INIT_STATUS_INITIALIZED);
11658 if (tmp == NULL)
11659 return NULL;
11660 add_loc_descr (&ret, tmp);
11661 l1label = new_loc_descr (DW_OP_pick, 2, 0);
11662 add_loc_descr (&ret, l1label);
11663 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11664 mode, mem_mode,
11665 VAR_INIT_STATUS_INITIALIZED);
11666 add_loc_descr (&ret, tmp);
11667 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
11668 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11669 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11670 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
11671 VAR_INIT_STATUS_INITIALIZED);
11672 if (tmp == NULL)
11673 return NULL;
11674 add_loc_descr (&ret, tmp);
11675 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11676 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
11677 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11678 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11679 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11680 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11681 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11682 VAR_INIT_STATUS_INITIALIZED);
11683 add_loc_descr (&ret, tmp);
11684 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
11685 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11686 add_loc_descr (&ret, l2jump);
11687 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
11688 VAR_INIT_STATUS_INITIALIZED);
11689 add_loc_descr (&ret, tmp);
11690 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11691 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11692 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11693 add_loc_descr (&ret, l1jump);
11694 l2label = new_loc_descr (DW_OP_drop, 0, 0);
11695 add_loc_descr (&ret, l2label);
11696 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11697 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11698 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11699 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11700 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11701 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11702 return ret;
11705 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
11706 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11707 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
11708 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
11710 ROTATERT is similar:
11711 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
11712 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11713 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
11715 static dw_loc_descr_ref
11716 rotate_loc_descriptor (rtx rtl, enum machine_mode mode,
11717 enum machine_mode mem_mode)
11719 rtx rtlop1 = XEXP (rtl, 1);
11720 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
11721 int i;
11723 if (GET_MODE_CLASS (mode) != MODE_INT)
11724 return NULL;
11726 if (GET_MODE (rtlop1) != VOIDmode
11727 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
11728 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
11729 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11730 VAR_INIT_STATUS_INITIALIZED);
11731 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
11732 VAR_INIT_STATUS_INITIALIZED);
11733 if (op0 == NULL || op1 == NULL)
11734 return NULL;
11735 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11736 for (i = 0; i < 2; i++)
11738 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
11739 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
11740 mode, mem_mode,
11741 VAR_INIT_STATUS_INITIALIZED);
11742 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
11743 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11744 ? DW_OP_const4u
11745 : HOST_BITS_PER_WIDE_INT == 64
11746 ? DW_OP_const8u : DW_OP_constu,
11747 GET_MODE_MASK (mode), 0);
11748 else
11749 mask[i] = NULL;
11750 if (mask[i] == NULL)
11751 return NULL;
11752 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
11754 ret = op0;
11755 add_loc_descr (&ret, op1);
11756 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11757 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11758 if (GET_CODE (rtl) == ROTATERT)
11760 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11761 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11762 GET_MODE_BITSIZE (mode), 0));
11764 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11765 if (mask[0] != NULL)
11766 add_loc_descr (&ret, mask[0]);
11767 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11768 if (mask[1] != NULL)
11770 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11771 add_loc_descr (&ret, mask[1]);
11772 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11774 if (GET_CODE (rtl) == ROTATE)
11776 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11777 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11778 GET_MODE_BITSIZE (mode), 0));
11780 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11781 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11782 return ret;
11785 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
11786 for DEBUG_PARAMETER_REF RTL. */
11788 static dw_loc_descr_ref
11789 parameter_ref_descriptor (rtx rtl)
11791 dw_loc_descr_ref ret;
11792 dw_die_ref ref;
11794 if (dwarf_strict)
11795 return NULL;
11796 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
11797 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
11798 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
11799 if (ref)
11801 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11802 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
11803 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
11805 else
11807 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
11808 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
11810 return ret;
11813 /* The following routine converts the RTL for a variable or parameter
11814 (resident in memory) into an equivalent Dwarf representation of a
11815 mechanism for getting the address of that same variable onto the top of a
11816 hypothetical "address evaluation" stack.
11818 When creating memory location descriptors, we are effectively transforming
11819 the RTL for a memory-resident object into its Dwarf postfix expression
11820 equivalent. This routine recursively descends an RTL tree, turning
11821 it into Dwarf postfix code as it goes.
11823 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
11825 MEM_MODE is the mode of the memory reference, needed to handle some
11826 autoincrement addressing modes.
11828 Return 0 if we can't represent the location. */
11830 dw_loc_descr_ref
11831 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
11832 enum machine_mode mem_mode,
11833 enum var_init_status initialized)
11835 dw_loc_descr_ref mem_loc_result = NULL;
11836 enum dwarf_location_atom op;
11837 dw_loc_descr_ref op0, op1;
11839 if (mode == VOIDmode)
11840 mode = GET_MODE (rtl);
11842 /* Note that for a dynamically sized array, the location we will generate a
11843 description of here will be the lowest numbered location which is
11844 actually within the array. That's *not* necessarily the same as the
11845 zeroth element of the array. */
11847 rtl = targetm.delegitimize_address (rtl);
11849 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
11850 return NULL;
11852 switch (GET_CODE (rtl))
11854 case POST_INC:
11855 case POST_DEC:
11856 case POST_MODIFY:
11857 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
11859 case SUBREG:
11860 /* The case of a subreg may arise when we have a local (register)
11861 variable or a formal (register) parameter which doesn't quite fill
11862 up an entire register. For now, just assume that it is
11863 legitimate to make the Dwarf info refer to the whole register which
11864 contains the given subreg. */
11865 if (!subreg_lowpart_p (rtl))
11866 break;
11867 if (GET_MODE_CLASS (mode) == MODE_INT
11868 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (rtl))) == MODE_INT
11869 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11870 #ifdef POINTERS_EXTEND_UNSIGNED
11871 || (mode == Pmode && mem_mode != VOIDmode)
11872 #endif
11874 && GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))) <= DWARF2_ADDR_SIZE)
11876 mem_loc_result = mem_loc_descriptor (SUBREG_REG (rtl),
11877 GET_MODE (SUBREG_REG (rtl)),
11878 mem_mode, initialized);
11879 break;
11881 if (dwarf_strict)
11882 break;
11883 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))))
11884 break;
11885 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl)))
11886 && (GET_MODE_CLASS (mode) != MODE_INT
11887 || GET_MODE_CLASS (GET_MODE (SUBREG_REG (rtl))) != MODE_INT))
11888 break;
11889 else
11891 dw_die_ref type_die;
11892 dw_loc_descr_ref cvt;
11894 mem_loc_result = mem_loc_descriptor (SUBREG_REG (rtl),
11895 GET_MODE (SUBREG_REG (rtl)),
11896 mem_mode, initialized);
11897 if (mem_loc_result == NULL)
11898 break;
11899 type_die = base_type_for_mode (mode,
11900 GET_MODE_CLASS (mode) == MODE_INT);
11901 if (type_die == NULL)
11903 mem_loc_result = NULL;
11904 break;
11906 if (GET_MODE_SIZE (mode)
11907 != GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))))
11908 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11909 else
11910 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
11911 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11912 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11913 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11914 add_loc_descr (&mem_loc_result, cvt);
11916 break;
11918 case REG:
11919 if (GET_MODE_CLASS (mode) != MODE_INT
11920 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
11921 && rtl != arg_pointer_rtx
11922 && rtl != frame_pointer_rtx
11923 #ifdef POINTERS_EXTEND_UNSIGNED
11924 && (mode != Pmode || mem_mode == VOIDmode)
11925 #endif
11928 dw_die_ref type_die;
11930 if (dwarf_strict)
11931 break;
11932 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
11933 break;
11934 type_die = base_type_for_mode (mode,
11935 GET_MODE_CLASS (mode) == MODE_INT);
11936 if (type_die == NULL)
11937 break;
11938 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
11939 dbx_reg_number (rtl), 0);
11940 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
11941 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
11942 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
11943 break;
11945 /* Whenever a register number forms a part of the description of the
11946 method for calculating the (dynamic) address of a memory resident
11947 object, DWARF rules require the register number be referred to as
11948 a "base register". This distinction is not based in any way upon
11949 what category of register the hardware believes the given register
11950 belongs to. This is strictly DWARF terminology we're dealing with
11951 here. Note that in cases where the location of a memory-resident
11952 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
11953 OP_CONST (0)) the actual DWARF location descriptor that we generate
11954 may just be OP_BASEREG (basereg). This may look deceptively like
11955 the object in question was allocated to a register (rather than in
11956 memory) so DWARF consumers need to be aware of the subtle
11957 distinction between OP_REG and OP_BASEREG. */
11958 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
11959 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
11960 else if (stack_realign_drap
11961 && crtl->drap_reg
11962 && crtl->args.internal_arg_pointer == rtl
11963 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
11965 /* If RTL is internal_arg_pointer, which has been optimized
11966 out, use DRAP instead. */
11967 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
11968 VAR_INIT_STATUS_INITIALIZED);
11970 break;
11972 case SIGN_EXTEND:
11973 case ZERO_EXTEND:
11974 if (GET_MODE_CLASS (mode) != MODE_INT)
11975 break;
11976 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
11977 mem_mode, VAR_INIT_STATUS_INITIALIZED);
11978 if (op0 == 0)
11979 break;
11980 else if (GET_CODE (rtl) == ZERO_EXTEND
11981 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11982 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
11983 < HOST_BITS_PER_WIDE_INT
11984 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
11985 to expand zero extend as two shifts instead of
11986 masking. */
11987 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
11989 enum machine_mode imode = GET_MODE (XEXP (rtl, 0));
11990 mem_loc_result = op0;
11991 add_loc_descr (&mem_loc_result,
11992 int_loc_descriptor (GET_MODE_MASK (imode)));
11993 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
11995 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11997 int shift = DWARF2_ADDR_SIZE
11998 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
11999 shift *= BITS_PER_UNIT;
12000 if (GET_CODE (rtl) == SIGN_EXTEND)
12001 op = DW_OP_shra;
12002 else
12003 op = DW_OP_shr;
12004 mem_loc_result = op0;
12005 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12006 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12007 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12008 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12010 else if (!dwarf_strict)
12012 dw_die_ref type_die1, type_die2;
12013 dw_loc_descr_ref cvt;
12015 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12016 GET_CODE (rtl) == ZERO_EXTEND);
12017 if (type_die1 == NULL)
12018 break;
12019 type_die2 = base_type_for_mode (mode, 1);
12020 if (type_die2 == NULL)
12021 break;
12022 mem_loc_result = op0;
12023 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12024 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12025 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12026 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12027 add_loc_descr (&mem_loc_result, cvt);
12028 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12029 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12030 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12031 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12032 add_loc_descr (&mem_loc_result, cvt);
12034 break;
12036 case MEM:
12038 rtx new_rtl = avoid_constant_pool_reference (rtl);
12039 if (new_rtl != rtl)
12041 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12042 initialized);
12043 if (mem_loc_result != NULL)
12044 return mem_loc_result;
12047 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12048 get_address_mode (rtl), mode,
12049 VAR_INIT_STATUS_INITIALIZED);
12050 if (mem_loc_result == NULL)
12051 mem_loc_result = tls_mem_loc_descriptor (rtl);
12052 if (mem_loc_result != NULL)
12054 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12055 || GET_MODE_CLASS (mode) != MODE_INT)
12057 dw_die_ref type_die;
12058 dw_loc_descr_ref deref;
12060 if (dwarf_strict)
12061 return NULL;
12062 type_die
12063 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12064 if (type_die == NULL)
12065 return NULL;
12066 deref = new_loc_descr (DW_OP_GNU_deref_type,
12067 GET_MODE_SIZE (mode), 0);
12068 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12069 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12070 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12071 add_loc_descr (&mem_loc_result, deref);
12073 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12074 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12075 else
12076 add_loc_descr (&mem_loc_result,
12077 new_loc_descr (DW_OP_deref_size,
12078 GET_MODE_SIZE (mode), 0));
12080 break;
12082 case LO_SUM:
12083 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12085 case LABEL_REF:
12086 /* Some ports can transform a symbol ref into a label ref, because
12087 the symbol ref is too far away and has to be dumped into a constant
12088 pool. */
12089 case CONST:
12090 case SYMBOL_REF:
12091 if (GET_MODE_CLASS (mode) != MODE_INT
12092 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12093 #ifdef POINTERS_EXTEND_UNSIGNED
12094 && (mode != Pmode || mem_mode == VOIDmode)
12095 #endif
12097 break;
12098 if (GET_CODE (rtl) == SYMBOL_REF
12099 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12101 dw_loc_descr_ref temp;
12103 /* If this is not defined, we have no way to emit the data. */
12104 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12105 break;
12107 temp = new_addr_loc_descr (rtl, dtprel_true);
12109 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12110 add_loc_descr (&mem_loc_result, temp);
12112 break;
12115 if (!const_ok_for_output (rtl))
12116 break;
12118 symref:
12119 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12120 vec_safe_push (used_rtx_array, rtl);
12121 break;
12123 case CONCAT:
12124 case CONCATN:
12125 case VAR_LOCATION:
12126 case DEBUG_IMPLICIT_PTR:
12127 expansion_failed (NULL_TREE, rtl,
12128 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12129 return 0;
12131 case ENTRY_VALUE:
12132 if (dwarf_strict)
12133 return NULL;
12134 if (REG_P (ENTRY_VALUE_EXP (rtl)))
12136 if (GET_MODE_CLASS (mode) != MODE_INT
12137 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12138 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12139 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12140 else
12142 = one_reg_loc_descriptor (dbx_reg_number (ENTRY_VALUE_EXP (rtl)),
12143 VAR_INIT_STATUS_INITIALIZED);
12145 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12146 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12148 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12149 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12150 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12151 return NULL;
12153 else
12154 gcc_unreachable ();
12155 if (op0 == NULL)
12156 return NULL;
12157 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12158 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12159 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12160 break;
12162 case DEBUG_PARAMETER_REF:
12163 mem_loc_result = parameter_ref_descriptor (rtl);
12164 break;
12166 case PRE_MODIFY:
12167 /* Extract the PLUS expression nested inside and fall into
12168 PLUS code below. */
12169 rtl = XEXP (rtl, 1);
12170 goto plus;
12172 case PRE_INC:
12173 case PRE_DEC:
12174 /* Turn these into a PLUS expression and fall into the PLUS code
12175 below. */
12176 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12177 GEN_INT (GET_CODE (rtl) == PRE_INC
12178 ? GET_MODE_UNIT_SIZE (mem_mode)
12179 : -GET_MODE_UNIT_SIZE (mem_mode)));
12181 /* ... fall through ... */
12183 case PLUS:
12184 plus:
12185 if (is_based_loc (rtl)
12186 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12187 || XEXP (rtl, 0) == arg_pointer_rtx
12188 || XEXP (rtl, 0) == frame_pointer_rtx)
12189 && GET_MODE_CLASS (mode) == MODE_INT)
12190 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12191 INTVAL (XEXP (rtl, 1)),
12192 VAR_INIT_STATUS_INITIALIZED);
12193 else
12195 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12196 VAR_INIT_STATUS_INITIALIZED);
12197 if (mem_loc_result == 0)
12198 break;
12200 if (CONST_INT_P (XEXP (rtl, 1))
12201 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12202 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12203 else
12205 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12206 VAR_INIT_STATUS_INITIALIZED);
12207 if (op1 == 0)
12208 break;
12209 add_loc_descr (&mem_loc_result, op1);
12210 add_loc_descr (&mem_loc_result,
12211 new_loc_descr (DW_OP_plus, 0, 0));
12214 break;
12216 /* If a pseudo-reg is optimized away, it is possible for it to
12217 be replaced with a MEM containing a multiply or shift. */
12218 case MINUS:
12219 op = DW_OP_minus;
12220 goto do_binop;
12222 case MULT:
12223 op = DW_OP_mul;
12224 goto do_binop;
12226 case DIV:
12227 if (!dwarf_strict
12228 && GET_MODE_CLASS (mode) == MODE_INT
12229 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12231 mem_loc_result = typed_binop (DW_OP_div, rtl,
12232 base_type_for_mode (mode, 0),
12233 mode, mem_mode);
12234 break;
12236 op = DW_OP_div;
12237 goto do_binop;
12239 case UMOD:
12240 op = DW_OP_mod;
12241 goto do_binop;
12243 case ASHIFT:
12244 op = DW_OP_shl;
12245 goto do_shift;
12247 case ASHIFTRT:
12248 op = DW_OP_shra;
12249 goto do_shift;
12251 case LSHIFTRT:
12252 op = DW_OP_shr;
12253 goto do_shift;
12255 do_shift:
12256 if (GET_MODE_CLASS (mode) != MODE_INT)
12257 break;
12258 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12259 VAR_INIT_STATUS_INITIALIZED);
12261 rtx rtlop1 = XEXP (rtl, 1);
12262 if (GET_MODE (rtlop1) != VOIDmode
12263 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
12264 < GET_MODE_BITSIZE (mode))
12265 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12266 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12267 VAR_INIT_STATUS_INITIALIZED);
12270 if (op0 == 0 || op1 == 0)
12271 break;
12273 mem_loc_result = op0;
12274 add_loc_descr (&mem_loc_result, op1);
12275 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12276 break;
12278 case AND:
12279 op = DW_OP_and;
12280 goto do_binop;
12282 case IOR:
12283 op = DW_OP_or;
12284 goto do_binop;
12286 case XOR:
12287 op = DW_OP_xor;
12288 goto do_binop;
12290 do_binop:
12291 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12292 VAR_INIT_STATUS_INITIALIZED);
12293 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12294 VAR_INIT_STATUS_INITIALIZED);
12296 if (op0 == 0 || op1 == 0)
12297 break;
12299 mem_loc_result = op0;
12300 add_loc_descr (&mem_loc_result, op1);
12301 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12302 break;
12304 case MOD:
12305 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
12307 mem_loc_result = typed_binop (DW_OP_mod, rtl,
12308 base_type_for_mode (mode, 0),
12309 mode, mem_mode);
12310 break;
12313 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12314 VAR_INIT_STATUS_INITIALIZED);
12315 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12316 VAR_INIT_STATUS_INITIALIZED);
12318 if (op0 == 0 || op1 == 0)
12319 break;
12321 mem_loc_result = op0;
12322 add_loc_descr (&mem_loc_result, op1);
12323 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12324 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12325 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
12326 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
12327 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
12328 break;
12330 case UDIV:
12331 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
12333 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
12335 op = DW_OP_div;
12336 goto do_binop;
12338 mem_loc_result = typed_binop (DW_OP_div, rtl,
12339 base_type_for_mode (mode, 1),
12340 mode, mem_mode);
12342 break;
12344 case NOT:
12345 op = DW_OP_not;
12346 goto do_unop;
12348 case ABS:
12349 op = DW_OP_abs;
12350 goto do_unop;
12352 case NEG:
12353 op = DW_OP_neg;
12354 goto do_unop;
12356 do_unop:
12357 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12358 VAR_INIT_STATUS_INITIALIZED);
12360 if (op0 == 0)
12361 break;
12363 mem_loc_result = op0;
12364 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12365 break;
12367 case CONST_INT:
12368 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12369 #ifdef POINTERS_EXTEND_UNSIGNED
12370 || (mode == Pmode
12371 && mem_mode != VOIDmode
12372 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
12373 #endif
12376 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12377 break;
12379 if (!dwarf_strict
12380 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
12381 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
12383 dw_die_ref type_die = base_type_for_mode (mode, 1);
12384 enum machine_mode amode;
12385 if (type_die == NULL)
12386 return NULL;
12387 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
12388 MODE_INT, 0);
12389 if (INTVAL (rtl) >= 0
12390 && amode != BLKmode
12391 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
12392 /* const DW_OP_GNU_convert <XXX> vs.
12393 DW_OP_GNU_const_type <XXX, 1, const>. */
12394 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
12395 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
12397 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12398 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12399 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12400 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12401 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
12402 add_loc_descr (&mem_loc_result, op0);
12403 return mem_loc_result;
12405 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
12406 INTVAL (rtl));
12407 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12408 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12409 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12410 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12411 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12412 else
12414 mem_loc_result->dw_loc_oprnd2.val_class
12415 = dw_val_class_const_double;
12416 mem_loc_result->dw_loc_oprnd2.v.val_double
12417 = double_int::from_shwi (INTVAL (rtl));
12420 break;
12422 case CONST_DOUBLE:
12423 if (!dwarf_strict)
12425 dw_die_ref type_die;
12427 /* Note that a CONST_DOUBLE rtx could represent either an integer
12428 or a floating-point constant. A CONST_DOUBLE is used whenever
12429 the constant requires more than one word in order to be
12430 adequately represented. We output CONST_DOUBLEs as blocks. */
12431 if (mode == VOIDmode
12432 || (GET_MODE (rtl) == VOIDmode
12433 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
12434 break;
12435 type_die = base_type_for_mode (mode,
12436 GET_MODE_CLASS (mode) == MODE_INT);
12437 if (type_die == NULL)
12438 return NULL;
12439 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
12440 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12441 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12442 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12443 if (SCALAR_FLOAT_MODE_P (mode))
12445 unsigned int length = GET_MODE_SIZE (mode);
12446 unsigned char *array
12447 = (unsigned char*) ggc_alloc_atomic (length);
12449 insert_float (rtl, array);
12450 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12451 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12452 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12453 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12455 else
12457 mem_loc_result->dw_loc_oprnd2.val_class
12458 = dw_val_class_const_double;
12459 mem_loc_result->dw_loc_oprnd2.v.val_double
12460 = rtx_to_double_int (rtl);
12463 break;
12465 case EQ:
12466 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
12467 break;
12469 case GE:
12470 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12471 break;
12473 case GT:
12474 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12475 break;
12477 case LE:
12478 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12479 break;
12481 case LT:
12482 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12483 break;
12485 case NE:
12486 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
12487 break;
12489 case GEU:
12490 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12491 break;
12493 case GTU:
12494 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12495 break;
12497 case LEU:
12498 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12499 break;
12501 case LTU:
12502 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12503 break;
12505 case UMIN:
12506 case UMAX:
12507 if (GET_MODE_CLASS (mode) != MODE_INT)
12508 break;
12509 /* FALLTHRU */
12510 case SMIN:
12511 case SMAX:
12512 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
12513 break;
12515 case ZERO_EXTRACT:
12516 case SIGN_EXTRACT:
12517 if (CONST_INT_P (XEXP (rtl, 1))
12518 && CONST_INT_P (XEXP (rtl, 2))
12519 && ((unsigned) INTVAL (XEXP (rtl, 1))
12520 + (unsigned) INTVAL (XEXP (rtl, 2))
12521 <= GET_MODE_BITSIZE (mode))
12522 && GET_MODE_CLASS (mode) == MODE_INT
12523 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12524 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
12526 int shift, size;
12527 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12528 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12529 if (op0 == 0)
12530 break;
12531 if (GET_CODE (rtl) == SIGN_EXTRACT)
12532 op = DW_OP_shra;
12533 else
12534 op = DW_OP_shr;
12535 mem_loc_result = op0;
12536 size = INTVAL (XEXP (rtl, 1));
12537 shift = INTVAL (XEXP (rtl, 2));
12538 if (BITS_BIG_ENDIAN)
12539 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12540 - shift - size;
12541 if (shift + size != (int) DWARF2_ADDR_SIZE)
12543 add_loc_descr (&mem_loc_result,
12544 int_loc_descriptor (DWARF2_ADDR_SIZE
12545 - shift - size));
12546 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12548 if (size != (int) DWARF2_ADDR_SIZE)
12550 add_loc_descr (&mem_loc_result,
12551 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
12552 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12555 break;
12557 case IF_THEN_ELSE:
12559 dw_loc_descr_ref op2, bra_node, drop_node;
12560 op0 = mem_loc_descriptor (XEXP (rtl, 0),
12561 GET_MODE (XEXP (rtl, 0)) == VOIDmode
12562 ? word_mode : GET_MODE (XEXP (rtl, 0)),
12563 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12564 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12565 VAR_INIT_STATUS_INITIALIZED);
12566 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
12567 VAR_INIT_STATUS_INITIALIZED);
12568 if (op0 == NULL || op1 == NULL || op2 == NULL)
12569 break;
12571 mem_loc_result = op1;
12572 add_loc_descr (&mem_loc_result, op2);
12573 add_loc_descr (&mem_loc_result, op0);
12574 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12575 add_loc_descr (&mem_loc_result, bra_node);
12576 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
12577 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12578 add_loc_descr (&mem_loc_result, drop_node);
12579 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12580 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12582 break;
12584 case FLOAT_EXTEND:
12585 case FLOAT_TRUNCATE:
12586 case FLOAT:
12587 case UNSIGNED_FLOAT:
12588 case FIX:
12589 case UNSIGNED_FIX:
12590 if (!dwarf_strict)
12592 dw_die_ref type_die;
12593 dw_loc_descr_ref cvt;
12595 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12596 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12597 if (op0 == NULL)
12598 break;
12599 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
12600 && (GET_CODE (rtl) == FLOAT
12601 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
12602 <= DWARF2_ADDR_SIZE))
12604 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12605 GET_CODE (rtl) == UNSIGNED_FLOAT);
12606 if (type_die == NULL)
12607 break;
12608 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12609 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12610 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12611 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12612 add_loc_descr (&op0, cvt);
12614 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
12615 if (type_die == NULL)
12616 break;
12617 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12618 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12619 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12620 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12621 add_loc_descr (&op0, cvt);
12622 if (GET_MODE_CLASS (mode) == MODE_INT
12623 && (GET_CODE (rtl) == FIX
12624 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
12626 op0 = convert_descriptor_to_mode (mode, op0);
12627 if (op0 == NULL)
12628 break;
12630 mem_loc_result = op0;
12632 break;
12634 case CLZ:
12635 case CTZ:
12636 case FFS:
12637 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
12638 break;
12640 case POPCOUNT:
12641 case PARITY:
12642 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
12643 break;
12645 case BSWAP:
12646 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
12647 break;
12649 case ROTATE:
12650 case ROTATERT:
12651 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
12652 break;
12654 case COMPARE:
12655 case TRUNCATE:
12656 /* In theory, we could implement the above. */
12657 /* DWARF cannot represent the unsigned compare operations
12658 natively. */
12659 case SS_MULT:
12660 case US_MULT:
12661 case SS_DIV:
12662 case US_DIV:
12663 case SS_PLUS:
12664 case US_PLUS:
12665 case SS_MINUS:
12666 case US_MINUS:
12667 case SS_NEG:
12668 case US_NEG:
12669 case SS_ABS:
12670 case SS_ASHIFT:
12671 case US_ASHIFT:
12672 case SS_TRUNCATE:
12673 case US_TRUNCATE:
12674 case UNORDERED:
12675 case ORDERED:
12676 case UNEQ:
12677 case UNGE:
12678 case UNGT:
12679 case UNLE:
12680 case UNLT:
12681 case LTGT:
12682 case FRACT_CONVERT:
12683 case UNSIGNED_FRACT_CONVERT:
12684 case SAT_FRACT:
12685 case UNSIGNED_SAT_FRACT:
12686 case SQRT:
12687 case ASM_OPERANDS:
12688 case VEC_MERGE:
12689 case VEC_SELECT:
12690 case VEC_CONCAT:
12691 case VEC_DUPLICATE:
12692 case UNSPEC:
12693 case HIGH:
12694 case FMA:
12695 case STRICT_LOW_PART:
12696 case CONST_VECTOR:
12697 case CONST_FIXED:
12698 case CLRSB:
12699 /* If delegitimize_address couldn't do anything with the UNSPEC, we
12700 can't express it in the debug info. This can happen e.g. with some
12701 TLS UNSPECs. */
12702 break;
12704 case CONST_STRING:
12705 resolve_one_addr (&rtl, NULL);
12706 goto symref;
12708 default:
12709 #ifdef ENABLE_CHECKING
12710 print_rtl (stderr, rtl);
12711 gcc_unreachable ();
12712 #else
12713 break;
12714 #endif
12717 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12718 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12720 return mem_loc_result;
12723 /* Return a descriptor that describes the concatenation of two locations.
12724 This is typically a complex variable. */
12726 static dw_loc_descr_ref
12727 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
12729 dw_loc_descr_ref cc_loc_result = NULL;
12730 dw_loc_descr_ref x0_ref
12731 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12732 dw_loc_descr_ref x1_ref
12733 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12735 if (x0_ref == 0 || x1_ref == 0)
12736 return 0;
12738 cc_loc_result = x0_ref;
12739 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
12741 add_loc_descr (&cc_loc_result, x1_ref);
12742 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
12744 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
12745 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12747 return cc_loc_result;
12750 /* Return a descriptor that describes the concatenation of N
12751 locations. */
12753 static dw_loc_descr_ref
12754 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
12756 unsigned int i;
12757 dw_loc_descr_ref cc_loc_result = NULL;
12758 unsigned int n = XVECLEN (concatn, 0);
12760 for (i = 0; i < n; ++i)
12762 dw_loc_descr_ref ref;
12763 rtx x = XVECEXP (concatn, 0, i);
12765 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12766 if (ref == NULL)
12767 return NULL;
12769 add_loc_descr (&cc_loc_result, ref);
12770 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
12773 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12774 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12776 return cc_loc_result;
12779 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
12780 for DEBUG_IMPLICIT_PTR RTL. */
12782 static dw_loc_descr_ref
12783 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
12785 dw_loc_descr_ref ret;
12786 dw_die_ref ref;
12788 if (dwarf_strict)
12789 return NULL;
12790 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
12791 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
12792 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
12793 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
12794 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
12795 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
12796 if (ref)
12798 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12799 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12800 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12802 else
12804 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12805 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
12807 return ret;
12810 /* Output a proper Dwarf location descriptor for a variable or parameter
12811 which is either allocated in a register or in a memory location. For a
12812 register, we just generate an OP_REG and the register number. For a
12813 memory location we provide a Dwarf postfix expression describing how to
12814 generate the (dynamic) address of the object onto the address stack.
12816 MODE is mode of the decl if this loc_descriptor is going to be used in
12817 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
12818 allowed, VOIDmode otherwise.
12820 If we don't know how to describe it, return 0. */
12822 static dw_loc_descr_ref
12823 loc_descriptor (rtx rtl, enum machine_mode mode,
12824 enum var_init_status initialized)
12826 dw_loc_descr_ref loc_result = NULL;
12828 switch (GET_CODE (rtl))
12830 case SUBREG:
12831 /* The case of a subreg may arise when we have a local (register)
12832 variable or a formal (register) parameter which doesn't quite fill
12833 up an entire register. For now, just assume that it is
12834 legitimate to make the Dwarf info refer to the whole register which
12835 contains the given subreg. */
12836 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
12837 loc_result = loc_descriptor (SUBREG_REG (rtl),
12838 GET_MODE (SUBREG_REG (rtl)), initialized);
12839 else
12840 goto do_default;
12841 break;
12843 case REG:
12844 loc_result = reg_loc_descriptor (rtl, initialized);
12845 break;
12847 case MEM:
12848 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
12849 GET_MODE (rtl), initialized);
12850 if (loc_result == NULL)
12851 loc_result = tls_mem_loc_descriptor (rtl);
12852 if (loc_result == NULL)
12854 rtx new_rtl = avoid_constant_pool_reference (rtl);
12855 if (new_rtl != rtl)
12856 loc_result = loc_descriptor (new_rtl, mode, initialized);
12858 break;
12860 case CONCAT:
12861 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
12862 initialized);
12863 break;
12865 case CONCATN:
12866 loc_result = concatn_loc_descriptor (rtl, initialized);
12867 break;
12869 case VAR_LOCATION:
12870 /* Single part. */
12871 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
12873 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
12874 if (GET_CODE (loc) == EXPR_LIST)
12875 loc = XEXP (loc, 0);
12876 loc_result = loc_descriptor (loc, mode, initialized);
12877 break;
12880 rtl = XEXP (rtl, 1);
12881 /* FALLTHRU */
12883 case PARALLEL:
12885 rtvec par_elems = XVEC (rtl, 0);
12886 int num_elem = GET_NUM_ELEM (par_elems);
12887 enum machine_mode mode;
12888 int i;
12890 /* Create the first one, so we have something to add to. */
12891 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
12892 VOIDmode, initialized);
12893 if (loc_result == NULL)
12894 return NULL;
12895 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
12896 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
12897 for (i = 1; i < num_elem; i++)
12899 dw_loc_descr_ref temp;
12901 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
12902 VOIDmode, initialized);
12903 if (temp == NULL)
12904 return NULL;
12905 add_loc_descr (&loc_result, temp);
12906 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
12907 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
12910 break;
12912 case CONST_INT:
12913 if (mode != VOIDmode && mode != BLKmode)
12914 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
12915 INTVAL (rtl));
12916 break;
12918 case CONST_DOUBLE:
12919 if (mode == VOIDmode)
12920 mode = GET_MODE (rtl);
12922 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
12924 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
12926 /* Note that a CONST_DOUBLE rtx could represent either an integer
12927 or a floating-point constant. A CONST_DOUBLE is used whenever
12928 the constant requires more than one word in order to be
12929 adequately represented. We output CONST_DOUBLEs as blocks. */
12930 loc_result = new_loc_descr (DW_OP_implicit_value,
12931 GET_MODE_SIZE (mode), 0);
12932 if (SCALAR_FLOAT_MODE_P (mode))
12934 unsigned int length = GET_MODE_SIZE (mode);
12935 unsigned char *array
12936 = (unsigned char*) ggc_alloc_atomic (length);
12938 insert_float (rtl, array);
12939 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12940 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12941 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12942 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12944 else
12946 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
12947 loc_result->dw_loc_oprnd2.v.val_double
12948 = rtx_to_double_int (rtl);
12951 break;
12953 case CONST_VECTOR:
12954 if (mode == VOIDmode)
12955 mode = GET_MODE (rtl);
12957 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
12959 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
12960 unsigned int length = CONST_VECTOR_NUNITS (rtl);
12961 unsigned char *array = (unsigned char *)
12962 ggc_alloc_atomic (length * elt_size);
12963 unsigned int i;
12964 unsigned char *p;
12966 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
12967 switch (GET_MODE_CLASS (mode))
12969 case MODE_VECTOR_INT:
12970 for (i = 0, p = array; i < length; i++, p += elt_size)
12972 rtx elt = CONST_VECTOR_ELT (rtl, i);
12973 double_int val = rtx_to_double_int (elt);
12975 if (elt_size <= sizeof (HOST_WIDE_INT))
12976 insert_int (val.to_shwi (), elt_size, p);
12977 else
12979 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
12980 insert_double (val, p);
12983 break;
12985 case MODE_VECTOR_FLOAT:
12986 for (i = 0, p = array; i < length; i++, p += elt_size)
12988 rtx elt = CONST_VECTOR_ELT (rtl, i);
12989 insert_float (elt, p);
12991 break;
12993 default:
12994 gcc_unreachable ();
12997 loc_result = new_loc_descr (DW_OP_implicit_value,
12998 length * elt_size, 0);
12999 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13000 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13001 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13002 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13004 break;
13006 case CONST:
13007 if (mode == VOIDmode
13008 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13009 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13010 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13012 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13013 break;
13015 /* FALLTHROUGH */
13016 case SYMBOL_REF:
13017 if (!const_ok_for_output (rtl))
13018 break;
13019 case LABEL_REF:
13020 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13021 && (dwarf_version >= 4 || !dwarf_strict))
13023 loc_result = new_addr_loc_descr (rtl, dtprel_false);
13024 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13025 vec_safe_push (used_rtx_array, rtl);
13027 break;
13029 case DEBUG_IMPLICIT_PTR:
13030 loc_result = implicit_ptr_descriptor (rtl, 0);
13031 break;
13033 case PLUS:
13034 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13035 && CONST_INT_P (XEXP (rtl, 1)))
13037 loc_result
13038 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13039 break;
13041 /* FALLTHRU */
13042 do_default:
13043 default:
13044 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13045 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13046 && dwarf_version >= 4)
13047 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13049 /* Value expression. */
13050 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13051 if (loc_result)
13052 add_loc_descr (&loc_result,
13053 new_loc_descr (DW_OP_stack_value, 0, 0));
13055 break;
13058 return loc_result;
13061 /* We need to figure out what section we should use as the base for the
13062 address ranges where a given location is valid.
13063 1. If this particular DECL has a section associated with it, use that.
13064 2. If this function has a section associated with it, use that.
13065 3. Otherwise, use the text section.
13066 XXX: If you split a variable across multiple sections, we won't notice. */
13068 static const char *
13069 secname_for_decl (const_tree decl)
13071 const char *secname;
13073 if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
13075 tree sectree = DECL_SECTION_NAME (decl);
13076 secname = TREE_STRING_POINTER (sectree);
13078 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13080 tree sectree = DECL_SECTION_NAME (current_function_decl);
13081 secname = TREE_STRING_POINTER (sectree);
13083 else if (cfun && in_cold_section_p)
13084 secname = crtl->subsections.cold_section_label;
13085 else
13086 secname = text_section_label;
13088 return secname;
13091 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
13093 static bool
13094 decl_by_reference_p (tree decl)
13096 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13097 || TREE_CODE (decl) == VAR_DECL)
13098 && DECL_BY_REFERENCE (decl));
13101 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13102 for VARLOC. */
13104 static dw_loc_descr_ref
13105 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13106 enum var_init_status initialized)
13108 int have_address = 0;
13109 dw_loc_descr_ref descr;
13110 enum machine_mode mode;
13112 if (want_address != 2)
13114 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13115 /* Single part. */
13116 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13118 varloc = PAT_VAR_LOCATION_LOC (varloc);
13119 if (GET_CODE (varloc) == EXPR_LIST)
13120 varloc = XEXP (varloc, 0);
13121 mode = GET_MODE (varloc);
13122 if (MEM_P (varloc))
13124 rtx addr = XEXP (varloc, 0);
13125 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13126 mode, initialized);
13127 if (descr)
13128 have_address = 1;
13129 else
13131 rtx x = avoid_constant_pool_reference (varloc);
13132 if (x != varloc)
13133 descr = mem_loc_descriptor (x, mode, VOIDmode,
13134 initialized);
13137 else
13138 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13140 else
13141 return 0;
13143 else
13145 if (GET_CODE (varloc) == VAR_LOCATION)
13146 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13147 else
13148 mode = DECL_MODE (loc);
13149 descr = loc_descriptor (varloc, mode, initialized);
13150 have_address = 1;
13153 if (!descr)
13154 return 0;
13156 if (want_address == 2 && !have_address
13157 && (dwarf_version >= 4 || !dwarf_strict))
13159 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13161 expansion_failed (loc, NULL_RTX,
13162 "DWARF address size mismatch");
13163 return 0;
13165 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13166 have_address = 1;
13168 /* Show if we can't fill the request for an address. */
13169 if (want_address && !have_address)
13171 expansion_failed (loc, NULL_RTX,
13172 "Want address and only have value");
13173 return 0;
13176 /* If we've got an address and don't want one, dereference. */
13177 if (!want_address && have_address)
13179 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13180 enum dwarf_location_atom op;
13182 if (size > DWARF2_ADDR_SIZE || size == -1)
13184 expansion_failed (loc, NULL_RTX,
13185 "DWARF address size mismatch");
13186 return 0;
13188 else if (size == DWARF2_ADDR_SIZE)
13189 op = DW_OP_deref;
13190 else
13191 op = DW_OP_deref_size;
13193 add_loc_descr (&descr, new_loc_descr (op, size, 0));
13196 return descr;
13199 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
13200 if it is not possible. */
13202 static dw_loc_descr_ref
13203 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
13205 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
13206 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
13207 else if (dwarf_version >= 3 || !dwarf_strict)
13208 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
13209 else
13210 return NULL;
13213 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13214 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
13216 static dw_loc_descr_ref
13217 dw_sra_loc_expr (tree decl, rtx loc)
13219 rtx p;
13220 unsigned int padsize = 0;
13221 dw_loc_descr_ref descr, *descr_tail;
13222 unsigned HOST_WIDE_INT decl_size;
13223 rtx varloc;
13224 enum var_init_status initialized;
13226 if (DECL_SIZE (decl) == NULL
13227 || !host_integerp (DECL_SIZE (decl), 1))
13228 return NULL;
13230 decl_size = tree_low_cst (DECL_SIZE (decl), 1);
13231 descr = NULL;
13232 descr_tail = &descr;
13234 for (p = loc; p; p = XEXP (p, 1))
13236 unsigned int bitsize = decl_piece_bitsize (p);
13237 rtx loc_note = *decl_piece_varloc_ptr (p);
13238 dw_loc_descr_ref cur_descr;
13239 dw_loc_descr_ref *tail, last = NULL;
13240 unsigned int opsize = 0;
13242 if (loc_note == NULL_RTX
13243 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
13245 padsize += bitsize;
13246 continue;
13248 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
13249 varloc = NOTE_VAR_LOCATION (loc_note);
13250 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
13251 if (cur_descr == NULL)
13253 padsize += bitsize;
13254 continue;
13257 /* Check that cur_descr either doesn't use
13258 DW_OP_*piece operations, or their sum is equal
13259 to bitsize. Otherwise we can't embed it. */
13260 for (tail = &cur_descr; *tail != NULL;
13261 tail = &(*tail)->dw_loc_next)
13262 if ((*tail)->dw_loc_opc == DW_OP_piece)
13264 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
13265 * BITS_PER_UNIT;
13266 last = *tail;
13268 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
13270 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
13271 last = *tail;
13274 if (last != NULL && opsize != bitsize)
13276 padsize += bitsize;
13277 continue;
13280 /* If there is a hole, add DW_OP_*piece after empty DWARF
13281 expression, which means that those bits are optimized out. */
13282 if (padsize)
13284 if (padsize > decl_size)
13285 return NULL;
13286 decl_size -= padsize;
13287 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
13288 if (*descr_tail == NULL)
13289 return NULL;
13290 descr_tail = &(*descr_tail)->dw_loc_next;
13291 padsize = 0;
13293 *descr_tail = cur_descr;
13294 descr_tail = tail;
13295 if (bitsize > decl_size)
13296 return NULL;
13297 decl_size -= bitsize;
13298 if (last == NULL)
13300 HOST_WIDE_INT offset = 0;
13301 if (GET_CODE (varloc) == VAR_LOCATION
13302 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13304 varloc = PAT_VAR_LOCATION_LOC (varloc);
13305 if (GET_CODE (varloc) == EXPR_LIST)
13306 varloc = XEXP (varloc, 0);
13310 if (GET_CODE (varloc) == CONST
13311 || GET_CODE (varloc) == SIGN_EXTEND
13312 || GET_CODE (varloc) == ZERO_EXTEND)
13313 varloc = XEXP (varloc, 0);
13314 else if (GET_CODE (varloc) == SUBREG)
13315 varloc = SUBREG_REG (varloc);
13316 else
13317 break;
13319 while (1);
13320 /* DW_OP_bit_size offset should be zero for register
13321 or implicit location descriptions and empty location
13322 descriptions, but for memory addresses needs big endian
13323 adjustment. */
13324 if (MEM_P (varloc))
13326 unsigned HOST_WIDE_INT memsize
13327 = MEM_SIZE (varloc) * BITS_PER_UNIT;
13328 if (memsize != bitsize)
13330 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
13331 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
13332 return NULL;
13333 if (memsize < bitsize)
13334 return NULL;
13335 if (BITS_BIG_ENDIAN)
13336 offset = memsize - bitsize;
13340 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
13341 if (*descr_tail == NULL)
13342 return NULL;
13343 descr_tail = &(*descr_tail)->dw_loc_next;
13347 /* If there were any non-empty expressions, add padding till the end of
13348 the decl. */
13349 if (descr != NULL && decl_size != 0)
13351 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
13352 if (*descr_tail == NULL)
13353 return NULL;
13355 return descr;
13358 /* Return the dwarf representation of the location list LOC_LIST of
13359 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
13360 function. */
13362 static dw_loc_list_ref
13363 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
13365 const char *endname, *secname;
13366 rtx varloc;
13367 enum var_init_status initialized;
13368 struct var_loc_node *node;
13369 dw_loc_descr_ref descr;
13370 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13371 dw_loc_list_ref list = NULL;
13372 dw_loc_list_ref *listp = &list;
13374 /* Now that we know what section we are using for a base,
13375 actually construct the list of locations.
13376 The first location information is what is passed to the
13377 function that creates the location list, and the remaining
13378 locations just get added on to that list.
13379 Note that we only know the start address for a location
13380 (IE location changes), so to build the range, we use
13381 the range [current location start, next location start].
13382 This means we have to special case the last node, and generate
13383 a range of [last location start, end of function label]. */
13385 secname = secname_for_decl (decl);
13387 for (node = loc_list->first; node; node = node->next)
13388 if (GET_CODE (node->loc) == EXPR_LIST
13389 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
13391 if (GET_CODE (node->loc) == EXPR_LIST)
13393 /* This requires DW_OP_{,bit_}piece, which is not usable
13394 inside DWARF expressions. */
13395 if (want_address != 2)
13396 continue;
13397 descr = dw_sra_loc_expr (decl, node->loc);
13398 if (descr == NULL)
13399 continue;
13401 else
13403 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13404 varloc = NOTE_VAR_LOCATION (node->loc);
13405 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
13407 if (descr)
13409 bool range_across_switch = false;
13410 /* If section switch happens in between node->label
13411 and node->next->label (or end of function) and
13412 we can't emit it as a single entry list,
13413 emit two ranges, first one ending at the end
13414 of first partition and second one starting at the
13415 beginning of second partition. */
13416 if (node == loc_list->last_before_switch
13417 && (node != loc_list->first || loc_list->first->next)
13418 && current_function_decl)
13420 endname = cfun->fde->dw_fde_end;
13421 range_across_switch = true;
13423 /* The variable has a location between NODE->LABEL and
13424 NODE->NEXT->LABEL. */
13425 else if (node->next)
13426 endname = node->next->label;
13427 /* If the variable has a location at the last label
13428 it keeps its location until the end of function. */
13429 else if (!current_function_decl)
13430 endname = text_end_label;
13431 else
13433 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13434 current_function_funcdef_no);
13435 endname = ggc_strdup (label_id);
13438 *listp = new_loc_list (descr, node->label, endname, secname);
13439 if (TREE_CODE (decl) == PARM_DECL
13440 && node == loc_list->first
13441 && GET_CODE (node->loc) == NOTE
13442 && strcmp (node->label, endname) == 0)
13443 (*listp)->force = true;
13444 listp = &(*listp)->dw_loc_next;
13446 if (range_across_switch)
13448 if (GET_CODE (node->loc) == EXPR_LIST)
13449 descr = dw_sra_loc_expr (decl, node->loc);
13450 else
13452 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13453 varloc = NOTE_VAR_LOCATION (node->loc);
13454 descr = dw_loc_list_1 (decl, varloc, want_address,
13455 initialized);
13457 gcc_assert (descr);
13458 /* The variable has a location between NODE->LABEL and
13459 NODE->NEXT->LABEL. */
13460 if (node->next)
13461 endname = node->next->label;
13462 else
13463 endname = cfun->fde->dw_fde_second_end;
13464 *listp = new_loc_list (descr,
13465 cfun->fde->dw_fde_second_begin,
13466 endname, secname);
13467 listp = &(*listp)->dw_loc_next;
13472 /* Try to avoid the overhead of a location list emitting a location
13473 expression instead, but only if we didn't have more than one
13474 location entry in the first place. If some entries were not
13475 representable, we don't want to pretend a single entry that was
13476 applies to the entire scope in which the variable is
13477 available. */
13478 if (list && loc_list->first->next)
13479 gen_llsym (list);
13481 return list;
13484 /* Return if the loc_list has only single element and thus can be represented
13485 as location description. */
13487 static bool
13488 single_element_loc_list_p (dw_loc_list_ref list)
13490 gcc_assert (!list->dw_loc_next || list->ll_symbol);
13491 return !list->ll_symbol;
13494 /* To each location in list LIST add loc descr REF. */
13496 static void
13497 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
13499 dw_loc_descr_ref copy;
13500 add_loc_descr (&list->expr, ref);
13501 list = list->dw_loc_next;
13502 while (list)
13504 copy = ggc_alloc_dw_loc_descr_node ();
13505 memcpy (copy, ref, sizeof (dw_loc_descr_node));
13506 add_loc_descr (&list->expr, copy);
13507 while (copy->dw_loc_next)
13509 dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
13510 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
13511 copy->dw_loc_next = new_copy;
13512 copy = new_copy;
13514 list = list->dw_loc_next;
13518 /* Given two lists RET and LIST
13519 produce location list that is result of adding expression in LIST
13520 to expression in RET on each position in program.
13521 Might be destructive on both RET and LIST.
13523 TODO: We handle only simple cases of RET or LIST having at most one
13524 element. General case would inolve sorting the lists in program order
13525 and merging them that will need some additional work.
13526 Adding that will improve quality of debug info especially for SRA-ed
13527 structures. */
13529 static void
13530 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
13532 if (!list)
13533 return;
13534 if (!*ret)
13536 *ret = list;
13537 return;
13539 if (!list->dw_loc_next)
13541 add_loc_descr_to_each (*ret, list->expr);
13542 return;
13544 if (!(*ret)->dw_loc_next)
13546 add_loc_descr_to_each (list, (*ret)->expr);
13547 *ret = list;
13548 return;
13550 expansion_failed (NULL_TREE, NULL_RTX,
13551 "Don't know how to merge two non-trivial"
13552 " location lists.\n");
13553 *ret = NULL;
13554 return;
13557 /* LOC is constant expression. Try a luck, look it up in constant
13558 pool and return its loc_descr of its address. */
13560 static dw_loc_descr_ref
13561 cst_pool_loc_descr (tree loc)
13563 /* Get an RTL for this, if something has been emitted. */
13564 rtx rtl = lookup_constant_def (loc);
13566 if (!rtl || !MEM_P (rtl))
13568 gcc_assert (!rtl);
13569 return 0;
13571 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
13573 /* TODO: We might get more coverage if we was actually delaying expansion
13574 of all expressions till end of compilation when constant pools are fully
13575 populated. */
13576 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
13578 expansion_failed (loc, NULL_RTX,
13579 "CST value in contant pool but not marked.");
13580 return 0;
13582 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13583 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
13586 /* Return dw_loc_list representing address of addr_expr LOC
13587 by looking for inner INDIRECT_REF expression and turning
13588 it into simple arithmetics. */
13590 static dw_loc_list_ref
13591 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
13593 tree obj, offset;
13594 HOST_WIDE_INT bitsize, bitpos, bytepos;
13595 enum machine_mode mode;
13596 int unsignedp, volatilep = 0;
13597 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13599 obj = get_inner_reference (TREE_OPERAND (loc, 0),
13600 &bitsize, &bitpos, &offset, &mode,
13601 &unsignedp, &volatilep, false);
13602 STRIP_NOPS (obj);
13603 if (bitpos % BITS_PER_UNIT)
13605 expansion_failed (loc, NULL_RTX, "bitfield access");
13606 return 0;
13608 if (!INDIRECT_REF_P (obj))
13610 expansion_failed (obj,
13611 NULL_RTX, "no indirect ref in inner refrence");
13612 return 0;
13614 if (!offset && !bitpos)
13615 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
13616 else if (toplev
13617 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
13618 && (dwarf_version >= 4 || !dwarf_strict))
13620 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
13621 if (!list_ret)
13622 return 0;
13623 if (offset)
13625 /* Variable offset. */
13626 list_ret1 = loc_list_from_tree (offset, 0);
13627 if (list_ret1 == 0)
13628 return 0;
13629 add_loc_list (&list_ret, list_ret1);
13630 if (!list_ret)
13631 return 0;
13632 add_loc_descr_to_each (list_ret,
13633 new_loc_descr (DW_OP_plus, 0, 0));
13635 bytepos = bitpos / BITS_PER_UNIT;
13636 if (bytepos > 0)
13637 add_loc_descr_to_each (list_ret,
13638 new_loc_descr (DW_OP_plus_uconst,
13639 bytepos, 0));
13640 else if (bytepos < 0)
13641 loc_list_plus_const (list_ret, bytepos);
13642 add_loc_descr_to_each (list_ret,
13643 new_loc_descr (DW_OP_stack_value, 0, 0));
13645 return list_ret;
13649 /* Generate Dwarf location list representing LOC.
13650 If WANT_ADDRESS is false, expression computing LOC will be computed
13651 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
13652 if WANT_ADDRESS is 2, expression computing address useable in location
13653 will be returned (i.e. DW_OP_reg can be used
13654 to refer to register values). */
13656 static dw_loc_list_ref
13657 loc_list_from_tree (tree loc, int want_address)
13659 dw_loc_descr_ref ret = NULL, ret1 = NULL;
13660 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13661 int have_address = 0;
13662 enum dwarf_location_atom op;
13664 /* ??? Most of the time we do not take proper care for sign/zero
13665 extending the values properly. Hopefully this won't be a real
13666 problem... */
13668 switch (TREE_CODE (loc))
13670 case ERROR_MARK:
13671 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
13672 return 0;
13674 case PLACEHOLDER_EXPR:
13675 /* This case involves extracting fields from an object to determine the
13676 position of other fields. We don't try to encode this here. The
13677 only user of this is Ada, which encodes the needed information using
13678 the names of types. */
13679 expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
13680 return 0;
13682 case CALL_EXPR:
13683 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
13684 /* There are no opcodes for these operations. */
13685 return 0;
13687 case PREINCREMENT_EXPR:
13688 case PREDECREMENT_EXPR:
13689 case POSTINCREMENT_EXPR:
13690 case POSTDECREMENT_EXPR:
13691 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
13692 /* There are no opcodes for these operations. */
13693 return 0;
13695 case ADDR_EXPR:
13696 /* If we already want an address, see if there is INDIRECT_REF inside
13697 e.g. for &this->field. */
13698 if (want_address)
13700 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
13701 (loc, want_address == 2);
13702 if (list_ret)
13703 have_address = 1;
13704 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
13705 && (ret = cst_pool_loc_descr (loc)))
13706 have_address = 1;
13708 /* Otherwise, process the argument and look for the address. */
13709 if (!list_ret && !ret)
13710 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
13711 else
13713 if (want_address)
13714 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
13715 return NULL;
13717 break;
13719 case VAR_DECL:
13720 if (DECL_THREAD_LOCAL_P (loc))
13722 rtx rtl;
13723 enum dwarf_location_atom tls_op;
13724 enum dtprel_bool dtprel = dtprel_false;
13726 if (targetm.have_tls)
13728 /* If this is not defined, we have no way to emit the
13729 data. */
13730 if (!targetm.asm_out.output_dwarf_dtprel)
13731 return 0;
13733 /* The way DW_OP_GNU_push_tls_address is specified, we
13734 can only look up addresses of objects in the current
13735 module. We used DW_OP_addr as first op, but that's
13736 wrong, because DW_OP_addr is relocated by the debug
13737 info consumer, while DW_OP_GNU_push_tls_address
13738 operand shouldn't be. */
13739 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
13740 return 0;
13741 dtprel = dtprel_true;
13742 tls_op = DW_OP_GNU_push_tls_address;
13744 else
13746 if (!targetm.emutls.debug_form_tls_address
13747 || !(dwarf_version >= 3 || !dwarf_strict))
13748 return 0;
13749 /* We stuffed the control variable into the DECL_VALUE_EXPR
13750 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
13751 no longer appear in gimple code. We used the control
13752 variable in specific so that we could pick it up here. */
13753 loc = DECL_VALUE_EXPR (loc);
13754 tls_op = DW_OP_form_tls_address;
13757 rtl = rtl_for_decl_location (loc);
13758 if (rtl == NULL_RTX)
13759 return 0;
13761 if (!MEM_P (rtl))
13762 return 0;
13763 rtl = XEXP (rtl, 0);
13764 if (! CONSTANT_P (rtl))
13765 return 0;
13767 ret = new_addr_loc_descr (rtl, dtprel);
13768 ret1 = new_loc_descr (tls_op, 0, 0);
13769 add_loc_descr (&ret, ret1);
13771 have_address = 1;
13772 break;
13774 /* FALLTHRU */
13776 case PARM_DECL:
13777 case RESULT_DECL:
13778 if (DECL_HAS_VALUE_EXPR_P (loc))
13779 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
13780 want_address);
13781 /* FALLTHRU */
13783 case FUNCTION_DECL:
13785 rtx rtl;
13786 var_loc_list *loc_list = lookup_decl_loc (loc);
13788 if (loc_list && loc_list->first)
13790 list_ret = dw_loc_list (loc_list, loc, want_address);
13791 have_address = want_address != 0;
13792 break;
13794 rtl = rtl_for_decl_location (loc);
13795 if (rtl == NULL_RTX)
13797 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
13798 return 0;
13800 else if (CONST_INT_P (rtl))
13802 HOST_WIDE_INT val = INTVAL (rtl);
13803 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13804 val &= GET_MODE_MASK (DECL_MODE (loc));
13805 ret = int_loc_descriptor (val);
13807 else if (GET_CODE (rtl) == CONST_STRING)
13809 expansion_failed (loc, NULL_RTX, "CONST_STRING");
13810 return 0;
13812 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
13813 ret = new_addr_loc_descr (rtl, dtprel_false);
13814 else
13816 enum machine_mode mode, mem_mode;
13818 /* Certain constructs can only be represented at top-level. */
13819 if (want_address == 2)
13821 ret = loc_descriptor (rtl, VOIDmode,
13822 VAR_INIT_STATUS_INITIALIZED);
13823 have_address = 1;
13825 else
13827 mode = GET_MODE (rtl);
13828 mem_mode = VOIDmode;
13829 if (MEM_P (rtl))
13831 mem_mode = mode;
13832 mode = get_address_mode (rtl);
13833 rtl = XEXP (rtl, 0);
13834 have_address = 1;
13836 ret = mem_loc_descriptor (rtl, mode, mem_mode,
13837 VAR_INIT_STATUS_INITIALIZED);
13839 if (!ret)
13840 expansion_failed (loc, rtl,
13841 "failed to produce loc descriptor for rtl");
13844 break;
13846 case MEM_REF:
13847 /* ??? FIXME. */
13848 if (!integer_zerop (TREE_OPERAND (loc, 1)))
13849 return 0;
13850 /* Fallthru. */
13851 case INDIRECT_REF:
13852 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13853 have_address = 1;
13854 break;
13856 case COMPOUND_EXPR:
13857 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
13859 CASE_CONVERT:
13860 case VIEW_CONVERT_EXPR:
13861 case SAVE_EXPR:
13862 case MODIFY_EXPR:
13863 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
13865 case COMPONENT_REF:
13866 case BIT_FIELD_REF:
13867 case ARRAY_REF:
13868 case ARRAY_RANGE_REF:
13869 case REALPART_EXPR:
13870 case IMAGPART_EXPR:
13872 tree obj, offset;
13873 HOST_WIDE_INT bitsize, bitpos, bytepos;
13874 enum machine_mode mode;
13875 int unsignedp, volatilep = 0;
13877 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
13878 &unsignedp, &volatilep, false);
13880 gcc_assert (obj != loc);
13882 list_ret = loc_list_from_tree (obj,
13883 want_address == 2
13884 && !bitpos && !offset ? 2 : 1);
13885 /* TODO: We can extract value of the small expression via shifting even
13886 for nonzero bitpos. */
13887 if (list_ret == 0)
13888 return 0;
13889 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
13891 expansion_failed (loc, NULL_RTX,
13892 "bitfield access");
13893 return 0;
13896 if (offset != NULL_TREE)
13898 /* Variable offset. */
13899 list_ret1 = loc_list_from_tree (offset, 0);
13900 if (list_ret1 == 0)
13901 return 0;
13902 add_loc_list (&list_ret, list_ret1);
13903 if (!list_ret)
13904 return 0;
13905 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
13908 bytepos = bitpos / BITS_PER_UNIT;
13909 if (bytepos > 0)
13910 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
13911 else if (bytepos < 0)
13912 loc_list_plus_const (list_ret, bytepos);
13914 have_address = 1;
13915 break;
13918 case INTEGER_CST:
13919 if ((want_address || !host_integerp (loc, 0))
13920 && (ret = cst_pool_loc_descr (loc)))
13921 have_address = 1;
13922 else if (want_address == 2
13923 && host_integerp (loc, 0)
13924 && (ret = address_of_int_loc_descriptor
13925 (int_size_in_bytes (TREE_TYPE (loc)),
13926 tree_low_cst (loc, 0))))
13927 have_address = 1;
13928 else if (host_integerp (loc, 0))
13929 ret = int_loc_descriptor (tree_low_cst (loc, 0));
13930 else
13932 expansion_failed (loc, NULL_RTX,
13933 "Integer operand is not host integer");
13934 return 0;
13936 break;
13938 case CONSTRUCTOR:
13939 case REAL_CST:
13940 case STRING_CST:
13941 case COMPLEX_CST:
13942 if ((ret = cst_pool_loc_descr (loc)))
13943 have_address = 1;
13944 else
13945 /* We can construct small constants here using int_loc_descriptor. */
13946 expansion_failed (loc, NULL_RTX,
13947 "constructor or constant not in constant pool");
13948 break;
13950 case TRUTH_AND_EXPR:
13951 case TRUTH_ANDIF_EXPR:
13952 case BIT_AND_EXPR:
13953 op = DW_OP_and;
13954 goto do_binop;
13956 case TRUTH_XOR_EXPR:
13957 case BIT_XOR_EXPR:
13958 op = DW_OP_xor;
13959 goto do_binop;
13961 case TRUTH_OR_EXPR:
13962 case TRUTH_ORIF_EXPR:
13963 case BIT_IOR_EXPR:
13964 op = DW_OP_or;
13965 goto do_binop;
13967 case FLOOR_DIV_EXPR:
13968 case CEIL_DIV_EXPR:
13969 case ROUND_DIV_EXPR:
13970 case TRUNC_DIV_EXPR:
13971 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13972 return 0;
13973 op = DW_OP_div;
13974 goto do_binop;
13976 case MINUS_EXPR:
13977 op = DW_OP_minus;
13978 goto do_binop;
13980 case FLOOR_MOD_EXPR:
13981 case CEIL_MOD_EXPR:
13982 case ROUND_MOD_EXPR:
13983 case TRUNC_MOD_EXPR:
13984 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13986 op = DW_OP_mod;
13987 goto do_binop;
13989 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13990 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
13991 if (list_ret == 0 || list_ret1 == 0)
13992 return 0;
13994 add_loc_list (&list_ret, list_ret1);
13995 if (list_ret == 0)
13996 return 0;
13997 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
13998 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
13999 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14000 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14001 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14002 break;
14004 case MULT_EXPR:
14005 op = DW_OP_mul;
14006 goto do_binop;
14008 case LSHIFT_EXPR:
14009 op = DW_OP_shl;
14010 goto do_binop;
14012 case RSHIFT_EXPR:
14013 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14014 goto do_binop;
14016 case POINTER_PLUS_EXPR:
14017 case PLUS_EXPR:
14018 if (host_integerp (TREE_OPERAND (loc, 1), 0))
14020 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14021 if (list_ret == 0)
14022 return 0;
14024 loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
14025 break;
14028 op = DW_OP_plus;
14029 goto do_binop;
14031 case LE_EXPR:
14032 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14033 return 0;
14035 op = DW_OP_le;
14036 goto do_binop;
14038 case GE_EXPR:
14039 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14040 return 0;
14042 op = DW_OP_ge;
14043 goto do_binop;
14045 case LT_EXPR:
14046 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14047 return 0;
14049 op = DW_OP_lt;
14050 goto do_binop;
14052 case GT_EXPR:
14053 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14054 return 0;
14056 op = DW_OP_gt;
14057 goto do_binop;
14059 case EQ_EXPR:
14060 op = DW_OP_eq;
14061 goto do_binop;
14063 case NE_EXPR:
14064 op = DW_OP_ne;
14065 goto do_binop;
14067 do_binop:
14068 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14069 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14070 if (list_ret == 0 || list_ret1 == 0)
14071 return 0;
14073 add_loc_list (&list_ret, list_ret1);
14074 if (list_ret == 0)
14075 return 0;
14076 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14077 break;
14079 case TRUTH_NOT_EXPR:
14080 case BIT_NOT_EXPR:
14081 op = DW_OP_not;
14082 goto do_unop;
14084 case ABS_EXPR:
14085 op = DW_OP_abs;
14086 goto do_unop;
14088 case NEGATE_EXPR:
14089 op = DW_OP_neg;
14090 goto do_unop;
14092 do_unop:
14093 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14094 if (list_ret == 0)
14095 return 0;
14097 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14098 break;
14100 case MIN_EXPR:
14101 case MAX_EXPR:
14103 const enum tree_code code =
14104 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14106 loc = build3 (COND_EXPR, TREE_TYPE (loc),
14107 build2 (code, integer_type_node,
14108 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14109 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
14112 /* ... fall through ... */
14114 case COND_EXPR:
14116 dw_loc_descr_ref lhs
14117 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
14118 dw_loc_list_ref rhs
14119 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
14120 dw_loc_descr_ref bra_node, jump_node, tmp;
14122 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14123 if (list_ret == 0 || lhs == 0 || rhs == 0)
14124 return 0;
14126 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14127 add_loc_descr_to_each (list_ret, bra_node);
14129 add_loc_list (&list_ret, rhs);
14130 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
14131 add_loc_descr_to_each (list_ret, jump_node);
14133 add_loc_descr_to_each (list_ret, lhs);
14134 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14135 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
14137 /* ??? Need a node to point the skip at. Use a nop. */
14138 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14139 add_loc_descr_to_each (list_ret, tmp);
14140 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14141 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
14143 break;
14145 case FIX_TRUNC_EXPR:
14146 return 0;
14148 default:
14149 /* Leave front-end specific codes as simply unknown. This comes
14150 up, for instance, with the C STMT_EXPR. */
14151 if ((unsigned int) TREE_CODE (loc)
14152 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
14154 expansion_failed (loc, NULL_RTX,
14155 "language specific tree node");
14156 return 0;
14159 #ifdef ENABLE_CHECKING
14160 /* Otherwise this is a generic code; we should just lists all of
14161 these explicitly. We forgot one. */
14162 gcc_unreachable ();
14163 #else
14164 /* In a release build, we want to degrade gracefully: better to
14165 generate incomplete debugging information than to crash. */
14166 return NULL;
14167 #endif
14170 if (!ret && !list_ret)
14171 return 0;
14173 if (want_address == 2 && !have_address
14174 && (dwarf_version >= 4 || !dwarf_strict))
14176 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14178 expansion_failed (loc, NULL_RTX,
14179 "DWARF address size mismatch");
14180 return 0;
14182 if (ret)
14183 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
14184 else
14185 add_loc_descr_to_each (list_ret,
14186 new_loc_descr (DW_OP_stack_value, 0, 0));
14187 have_address = 1;
14189 /* Show if we can't fill the request for an address. */
14190 if (want_address && !have_address)
14192 expansion_failed (loc, NULL_RTX,
14193 "Want address and only have value");
14194 return 0;
14197 gcc_assert (!ret || !list_ret);
14199 /* If we've got an address and don't want one, dereference. */
14200 if (!want_address && have_address)
14202 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14204 if (size > DWARF2_ADDR_SIZE || size == -1)
14206 expansion_failed (loc, NULL_RTX,
14207 "DWARF address size mismatch");
14208 return 0;
14210 else if (size == DWARF2_ADDR_SIZE)
14211 op = DW_OP_deref;
14212 else
14213 op = DW_OP_deref_size;
14215 if (ret)
14216 add_loc_descr (&ret, new_loc_descr (op, size, 0));
14217 else
14218 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
14220 if (ret)
14221 list_ret = new_loc_list (ret, NULL, NULL, NULL);
14223 return list_ret;
14226 /* Same as above but return only single location expression. */
14227 static dw_loc_descr_ref
14228 loc_descriptor_from_tree (tree loc, int want_address)
14230 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
14231 if (!ret)
14232 return NULL;
14233 if (ret->dw_loc_next)
14235 expansion_failed (loc, NULL_RTX,
14236 "Location list where only loc descriptor needed");
14237 return NULL;
14239 return ret->expr;
14242 /* Given a value, round it up to the lowest multiple of `boundary'
14243 which is not less than the value itself. */
14245 static inline HOST_WIDE_INT
14246 ceiling (HOST_WIDE_INT value, unsigned int boundary)
14248 return (((value + boundary - 1) / boundary) * boundary);
14251 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
14252 pointer to the declared type for the relevant field variable, or return
14253 `integer_type_node' if the given node turns out to be an
14254 ERROR_MARK node. */
14256 static inline tree
14257 field_type (const_tree decl)
14259 tree type;
14261 if (TREE_CODE (decl) == ERROR_MARK)
14262 return integer_type_node;
14264 type = DECL_BIT_FIELD_TYPE (decl);
14265 if (type == NULL_TREE)
14266 type = TREE_TYPE (decl);
14268 return type;
14271 /* Given a pointer to a tree node, return the alignment in bits for
14272 it, or else return BITS_PER_WORD if the node actually turns out to
14273 be an ERROR_MARK node. */
14275 static inline unsigned
14276 simple_type_align_in_bits (const_tree type)
14278 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
14281 static inline unsigned
14282 simple_decl_align_in_bits (const_tree decl)
14284 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
14287 /* Return the result of rounding T up to ALIGN. */
14289 static inline double_int
14290 round_up_to_align (double_int t, unsigned int align)
14292 double_int alignd = double_int::from_uhwi (align);
14293 t += alignd;
14294 t += double_int_minus_one;
14295 t = t.div (alignd, true, TRUNC_DIV_EXPR);
14296 t *= alignd;
14297 return t;
14300 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
14301 lowest addressed byte of the "containing object" for the given FIELD_DECL,
14302 or return 0 if we are unable to determine what that offset is, either
14303 because the argument turns out to be a pointer to an ERROR_MARK node, or
14304 because the offset is actually variable. (We can't handle the latter case
14305 just yet). */
14307 static HOST_WIDE_INT
14308 field_byte_offset (const_tree decl)
14310 double_int object_offset_in_bits;
14311 double_int object_offset_in_bytes;
14312 double_int bitpos_int;
14314 if (TREE_CODE (decl) == ERROR_MARK)
14315 return 0;
14317 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
14319 /* We cannot yet cope with fields whose positions are variable, so
14320 for now, when we see such things, we simply return 0. Someday, we may
14321 be able to handle such cases, but it will be damn difficult. */
14322 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
14323 return 0;
14325 bitpos_int = tree_to_double_int (bit_position (decl));
14327 #ifdef PCC_BITFIELD_TYPE_MATTERS
14328 if (PCC_BITFIELD_TYPE_MATTERS)
14330 tree type;
14331 tree field_size_tree;
14332 double_int deepest_bitpos;
14333 double_int field_size_in_bits;
14334 unsigned int type_align_in_bits;
14335 unsigned int decl_align_in_bits;
14336 double_int type_size_in_bits;
14338 type = field_type (decl);
14339 type_size_in_bits = double_int_type_size_in_bits (type);
14340 type_align_in_bits = simple_type_align_in_bits (type);
14342 field_size_tree = DECL_SIZE (decl);
14344 /* The size could be unspecified if there was an error, or for
14345 a flexible array member. */
14346 if (!field_size_tree)
14347 field_size_tree = bitsize_zero_node;
14349 /* If the size of the field is not constant, use the type size. */
14350 if (TREE_CODE (field_size_tree) == INTEGER_CST)
14351 field_size_in_bits = tree_to_double_int (field_size_tree);
14352 else
14353 field_size_in_bits = type_size_in_bits;
14355 decl_align_in_bits = simple_decl_align_in_bits (decl);
14357 /* The GCC front-end doesn't make any attempt to keep track of the
14358 starting bit offset (relative to the start of the containing
14359 structure type) of the hypothetical "containing object" for a
14360 bit-field. Thus, when computing the byte offset value for the
14361 start of the "containing object" of a bit-field, we must deduce
14362 this information on our own. This can be rather tricky to do in
14363 some cases. For example, handling the following structure type
14364 definition when compiling for an i386/i486 target (which only
14365 aligns long long's to 32-bit boundaries) can be very tricky:
14367 struct S { int field1; long long field2:31; };
14369 Fortunately, there is a simple rule-of-thumb which can be used
14370 in such cases. When compiling for an i386/i486, GCC will
14371 allocate 8 bytes for the structure shown above. It decides to
14372 do this based upon one simple rule for bit-field allocation.
14373 GCC allocates each "containing object" for each bit-field at
14374 the first (i.e. lowest addressed) legitimate alignment boundary
14375 (based upon the required minimum alignment for the declared
14376 type of the field) which it can possibly use, subject to the
14377 condition that there is still enough available space remaining
14378 in the containing object (when allocated at the selected point)
14379 to fully accommodate all of the bits of the bit-field itself.
14381 This simple rule makes it obvious why GCC allocates 8 bytes for
14382 each object of the structure type shown above. When looking
14383 for a place to allocate the "containing object" for `field2',
14384 the compiler simply tries to allocate a 64-bit "containing
14385 object" at each successive 32-bit boundary (starting at zero)
14386 until it finds a place to allocate that 64- bit field such that
14387 at least 31 contiguous (and previously unallocated) bits remain
14388 within that selected 64 bit field. (As it turns out, for the
14389 example above, the compiler finds it is OK to allocate the
14390 "containing object" 64-bit field at bit-offset zero within the
14391 structure type.)
14393 Here we attempt to work backwards from the limited set of facts
14394 we're given, and we try to deduce from those facts, where GCC
14395 must have believed that the containing object started (within
14396 the structure type). The value we deduce is then used (by the
14397 callers of this routine) to generate DW_AT_location and
14398 DW_AT_bit_offset attributes for fields (both bit-fields and, in
14399 the case of DW_AT_location, regular fields as well). */
14401 /* Figure out the bit-distance from the start of the structure to
14402 the "deepest" bit of the bit-field. */
14403 deepest_bitpos = bitpos_int + field_size_in_bits;
14405 /* This is the tricky part. Use some fancy footwork to deduce
14406 where the lowest addressed bit of the containing object must
14407 be. */
14408 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14410 /* Round up to type_align by default. This works best for
14411 bitfields. */
14412 object_offset_in_bits
14413 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
14415 if (object_offset_in_bits.ugt (bitpos_int))
14417 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14419 /* Round up to decl_align instead. */
14420 object_offset_in_bits
14421 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
14424 else
14425 #endif /* PCC_BITFIELD_TYPE_MATTERS */
14426 object_offset_in_bits = bitpos_int;
14428 object_offset_in_bytes
14429 = object_offset_in_bits.div (double_int::from_uhwi (BITS_PER_UNIT),
14430 true, TRUNC_DIV_EXPR);
14431 return object_offset_in_bytes.to_shwi ();
14434 /* The following routines define various Dwarf attributes and any data
14435 associated with them. */
14437 /* Add a location description attribute value to a DIE.
14439 This emits location attributes suitable for whole variables and
14440 whole parameters. Note that the location attributes for struct fields are
14441 generated by the routine `data_member_location_attribute' below. */
14443 static inline void
14444 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
14445 dw_loc_list_ref descr)
14447 if (descr == 0)
14448 return;
14449 if (single_element_loc_list_p (descr))
14450 add_AT_loc (die, attr_kind, descr->expr);
14451 else
14452 add_AT_loc_list (die, attr_kind, descr);
14455 /* Add DW_AT_accessibility attribute to DIE if needed. */
14457 static void
14458 add_accessibility_attribute (dw_die_ref die, tree decl)
14460 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
14461 children, otherwise the default is DW_ACCESS_public. In DWARF2
14462 the default has always been DW_ACCESS_public. */
14463 if (TREE_PROTECTED (decl))
14464 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
14465 else if (TREE_PRIVATE (decl))
14467 if (dwarf_version == 2
14468 || die->die_parent == NULL
14469 || die->die_parent->die_tag != DW_TAG_class_type)
14470 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
14472 else if (dwarf_version > 2
14473 && die->die_parent
14474 && die->die_parent->die_tag == DW_TAG_class_type)
14475 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
14478 /* Attach the specialized form of location attribute used for data members of
14479 struct and union types. In the special case of a FIELD_DECL node which
14480 represents a bit-field, the "offset" part of this special location
14481 descriptor must indicate the distance in bytes from the lowest-addressed
14482 byte of the containing struct or union type to the lowest-addressed byte of
14483 the "containing object" for the bit-field. (See the `field_byte_offset'
14484 function above).
14486 For any given bit-field, the "containing object" is a hypothetical object
14487 (of some integral or enum type) within which the given bit-field lives. The
14488 type of this hypothetical "containing object" is always the same as the
14489 declared type of the individual bit-field itself (for GCC anyway... the
14490 DWARF spec doesn't actually mandate this). Note that it is the size (in
14491 bytes) of the hypothetical "containing object" which will be given in the
14492 DW_AT_byte_size attribute for this bit-field. (See the
14493 `byte_size_attribute' function below.) It is also used when calculating the
14494 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
14495 function below.) */
14497 static void
14498 add_data_member_location_attribute (dw_die_ref die, tree decl)
14500 HOST_WIDE_INT offset;
14501 dw_loc_descr_ref loc_descr = 0;
14503 if (TREE_CODE (decl) == TREE_BINFO)
14505 /* We're working on the TAG_inheritance for a base class. */
14506 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
14508 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
14509 aren't at a fixed offset from all (sub)objects of the same
14510 type. We need to extract the appropriate offset from our
14511 vtable. The following dwarf expression means
14513 BaseAddr = ObAddr + *((*ObAddr) - Offset)
14515 This is specific to the V3 ABI, of course. */
14517 dw_loc_descr_ref tmp;
14519 /* Make a copy of the object address. */
14520 tmp = new_loc_descr (DW_OP_dup, 0, 0);
14521 add_loc_descr (&loc_descr, tmp);
14523 /* Extract the vtable address. */
14524 tmp = new_loc_descr (DW_OP_deref, 0, 0);
14525 add_loc_descr (&loc_descr, tmp);
14527 /* Calculate the address of the offset. */
14528 offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
14529 gcc_assert (offset < 0);
14531 tmp = int_loc_descriptor (-offset);
14532 add_loc_descr (&loc_descr, tmp);
14533 tmp = new_loc_descr (DW_OP_minus, 0, 0);
14534 add_loc_descr (&loc_descr, tmp);
14536 /* Extract the offset. */
14537 tmp = new_loc_descr (DW_OP_deref, 0, 0);
14538 add_loc_descr (&loc_descr, tmp);
14540 /* Add it to the object address. */
14541 tmp = new_loc_descr (DW_OP_plus, 0, 0);
14542 add_loc_descr (&loc_descr, tmp);
14544 else
14545 offset = tree_low_cst (BINFO_OFFSET (decl), 0);
14547 else
14548 offset = field_byte_offset (decl);
14550 if (! loc_descr)
14552 if (dwarf_version > 2)
14554 /* Don't need to output a location expression, just the constant. */
14555 if (offset < 0)
14556 add_AT_int (die, DW_AT_data_member_location, offset);
14557 else
14558 add_AT_unsigned (die, DW_AT_data_member_location, offset);
14559 return;
14561 else
14563 enum dwarf_location_atom op;
14565 /* The DWARF2 standard says that we should assume that the structure
14566 address is already on the stack, so we can specify a structure
14567 field address by using DW_OP_plus_uconst. */
14568 op = DW_OP_plus_uconst;
14569 loc_descr = new_loc_descr (op, offset, 0);
14573 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
14576 /* Writes integer values to dw_vec_const array. */
14578 static void
14579 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
14581 while (size != 0)
14583 *dest++ = val & 0xff;
14584 val >>= 8;
14585 --size;
14589 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
14591 static HOST_WIDE_INT
14592 extract_int (const unsigned char *src, unsigned int size)
14594 HOST_WIDE_INT val = 0;
14596 src += size;
14597 while (size != 0)
14599 val <<= 8;
14600 val |= *--src & 0xff;
14601 --size;
14603 return val;
14606 /* Writes double_int values to dw_vec_const array. */
14608 static void
14609 insert_double (double_int val, unsigned char *dest)
14611 unsigned char *p0 = dest;
14612 unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
14614 if (WORDS_BIG_ENDIAN)
14616 p0 = p1;
14617 p1 = dest;
14620 insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
14621 insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
14624 /* Writes floating point values to dw_vec_const array. */
14626 static void
14627 insert_float (const_rtx rtl, unsigned char *array)
14629 REAL_VALUE_TYPE rv;
14630 long val[4];
14631 int i;
14633 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
14634 real_to_target (val, &rv, GET_MODE (rtl));
14636 /* real_to_target puts 32-bit pieces in each long. Pack them. */
14637 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
14639 insert_int (val[i], 4, array);
14640 array += 4;
14644 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
14645 does not have a "location" either in memory or in a register. These
14646 things can arise in GNU C when a constant is passed as an actual parameter
14647 to an inlined function. They can also arise in C++ where declared
14648 constants do not necessarily get memory "homes". */
14650 static bool
14651 add_const_value_attribute (dw_die_ref die, rtx rtl)
14653 switch (GET_CODE (rtl))
14655 case CONST_INT:
14657 HOST_WIDE_INT val = INTVAL (rtl);
14659 if (val < 0)
14660 add_AT_int (die, DW_AT_const_value, val);
14661 else
14662 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
14664 return true;
14666 case CONST_DOUBLE:
14667 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
14668 floating-point constant. A CONST_DOUBLE is used whenever the
14669 constant requires more than one word in order to be adequately
14670 represented. */
14672 enum machine_mode mode = GET_MODE (rtl);
14674 if (SCALAR_FLOAT_MODE_P (mode))
14676 unsigned int length = GET_MODE_SIZE (mode);
14677 unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
14679 insert_float (rtl, array);
14680 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
14682 else
14683 add_AT_double (die, DW_AT_const_value,
14684 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
14686 return true;
14688 case CONST_VECTOR:
14690 enum machine_mode mode = GET_MODE (rtl);
14691 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
14692 unsigned int length = CONST_VECTOR_NUNITS (rtl);
14693 unsigned char *array = (unsigned char *) ggc_alloc_atomic
14694 (length * elt_size);
14695 unsigned int i;
14696 unsigned char *p;
14698 switch (GET_MODE_CLASS (mode))
14700 case MODE_VECTOR_INT:
14701 for (i = 0, p = array; i < length; i++, p += elt_size)
14703 rtx elt = CONST_VECTOR_ELT (rtl, i);
14704 double_int val = rtx_to_double_int (elt);
14706 if (elt_size <= sizeof (HOST_WIDE_INT))
14707 insert_int (val.to_shwi (), elt_size, p);
14708 else
14710 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
14711 insert_double (val, p);
14714 break;
14716 case MODE_VECTOR_FLOAT:
14717 for (i = 0, p = array; i < length; i++, p += elt_size)
14719 rtx elt = CONST_VECTOR_ELT (rtl, i);
14720 insert_float (elt, p);
14722 break;
14724 default:
14725 gcc_unreachable ();
14728 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
14730 return true;
14732 case CONST_STRING:
14733 if (dwarf_version >= 4 || !dwarf_strict)
14735 dw_loc_descr_ref loc_result;
14736 resolve_one_addr (&rtl, NULL);
14737 rtl_addr:
14738 loc_result = new_addr_loc_descr (rtl, dtprel_false);
14739 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14740 add_AT_loc (die, DW_AT_location, loc_result);
14741 vec_safe_push (used_rtx_array, rtl);
14742 return true;
14744 return false;
14746 case CONST:
14747 if (CONSTANT_P (XEXP (rtl, 0)))
14748 return add_const_value_attribute (die, XEXP (rtl, 0));
14749 /* FALLTHROUGH */
14750 case SYMBOL_REF:
14751 if (!const_ok_for_output (rtl))
14752 return false;
14753 case LABEL_REF:
14754 if (dwarf_version >= 4 || !dwarf_strict)
14755 goto rtl_addr;
14756 return false;
14758 case PLUS:
14759 /* In cases where an inlined instance of an inline function is passed
14760 the address of an `auto' variable (which is local to the caller) we
14761 can get a situation where the DECL_RTL of the artificial local
14762 variable (for the inlining) which acts as a stand-in for the
14763 corresponding formal parameter (of the inline function) will look
14764 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
14765 exactly a compile-time constant expression, but it isn't the address
14766 of the (artificial) local variable either. Rather, it represents the
14767 *value* which the artificial local variable always has during its
14768 lifetime. We currently have no way to represent such quasi-constant
14769 values in Dwarf, so for now we just punt and generate nothing. */
14770 return false;
14772 case HIGH:
14773 case CONST_FIXED:
14774 return false;
14776 case MEM:
14777 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
14778 && MEM_READONLY_P (rtl)
14779 && GET_MODE (rtl) == BLKmode)
14781 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
14782 return true;
14784 return false;
14786 default:
14787 /* No other kinds of rtx should be possible here. */
14788 gcc_unreachable ();
14790 return false;
14793 /* Determine whether the evaluation of EXPR references any variables
14794 or functions which aren't otherwise used (and therefore may not be
14795 output). */
14796 static tree
14797 reference_to_unused (tree * tp, int * walk_subtrees,
14798 void * data ATTRIBUTE_UNUSED)
14800 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
14801 *walk_subtrees = 0;
14803 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
14804 && ! TREE_ASM_WRITTEN (*tp))
14805 return *tp;
14806 /* ??? The C++ FE emits debug information for using decls, so
14807 putting gcc_unreachable here falls over. See PR31899. For now
14808 be conservative. */
14809 else if (!cgraph_global_info_ready
14810 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
14811 return *tp;
14812 else if (TREE_CODE (*tp) == VAR_DECL)
14814 struct varpool_node *node = varpool_get_node (*tp);
14815 if (!node || !node->analyzed)
14816 return *tp;
14818 else if (TREE_CODE (*tp) == FUNCTION_DECL
14819 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
14821 /* The call graph machinery must have finished analyzing,
14822 optimizing and gimplifying the CU by now.
14823 So if *TP has no call graph node associated
14824 to it, it means *TP will not be emitted. */
14825 if (!cgraph_get_node (*tp))
14826 return *tp;
14828 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
14829 return *tp;
14831 return NULL_TREE;
14834 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
14835 for use in a later add_const_value_attribute call. */
14837 static rtx
14838 rtl_for_decl_init (tree init, tree type)
14840 rtx rtl = NULL_RTX;
14842 STRIP_NOPS (init);
14844 /* If a variable is initialized with a string constant without embedded
14845 zeros, build CONST_STRING. */
14846 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
14848 tree enttype = TREE_TYPE (type);
14849 tree domain = TYPE_DOMAIN (type);
14850 enum machine_mode mode = TYPE_MODE (enttype);
14852 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
14853 && domain
14854 && integer_zerop (TYPE_MIN_VALUE (domain))
14855 && compare_tree_int (TYPE_MAX_VALUE (domain),
14856 TREE_STRING_LENGTH (init) - 1) == 0
14857 && ((size_t) TREE_STRING_LENGTH (init)
14858 == strlen (TREE_STRING_POINTER (init)) + 1))
14860 rtl = gen_rtx_CONST_STRING (VOIDmode,
14861 ggc_strdup (TREE_STRING_POINTER (init)));
14862 rtl = gen_rtx_MEM (BLKmode, rtl);
14863 MEM_READONLY_P (rtl) = 1;
14866 /* Other aggregates, and complex values, could be represented using
14867 CONCAT: FIXME! */
14868 else if (AGGREGATE_TYPE_P (type)
14869 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
14870 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
14871 || TREE_CODE (type) == COMPLEX_TYPE)
14873 /* Vectors only work if their mode is supported by the target.
14874 FIXME: generic vectors ought to work too. */
14875 else if (TREE_CODE (type) == VECTOR_TYPE
14876 && !VECTOR_MODE_P (TYPE_MODE (type)))
14878 /* If the initializer is something that we know will expand into an
14879 immediate RTL constant, expand it now. We must be careful not to
14880 reference variables which won't be output. */
14881 else if (initializer_constant_valid_p (init, type)
14882 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
14884 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
14885 possible. */
14886 if (TREE_CODE (type) == VECTOR_TYPE)
14887 switch (TREE_CODE (init))
14889 case VECTOR_CST:
14890 break;
14891 case CONSTRUCTOR:
14892 if (TREE_CONSTANT (init))
14894 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
14895 bool constant_p = true;
14896 tree value;
14897 unsigned HOST_WIDE_INT ix;
14899 /* Even when ctor is constant, it might contain non-*_CST
14900 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
14901 belong into VECTOR_CST nodes. */
14902 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
14903 if (!CONSTANT_CLASS_P (value))
14905 constant_p = false;
14906 break;
14909 if (constant_p)
14911 init = build_vector_from_ctor (type, elts);
14912 break;
14915 /* FALLTHRU */
14917 default:
14918 return NULL;
14921 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
14923 /* If expand_expr returns a MEM, it wasn't immediate. */
14924 gcc_assert (!rtl || !MEM_P (rtl));
14927 return rtl;
14930 /* Generate RTL for the variable DECL to represent its location. */
14932 static rtx
14933 rtl_for_decl_location (tree decl)
14935 rtx rtl;
14937 /* Here we have to decide where we are going to say the parameter "lives"
14938 (as far as the debugger is concerned). We only have a couple of
14939 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
14941 DECL_RTL normally indicates where the parameter lives during most of the
14942 activation of the function. If optimization is enabled however, this
14943 could be either NULL or else a pseudo-reg. Both of those cases indicate
14944 that the parameter doesn't really live anywhere (as far as the code
14945 generation parts of GCC are concerned) during most of the function's
14946 activation. That will happen (for example) if the parameter is never
14947 referenced within the function.
14949 We could just generate a location descriptor here for all non-NULL
14950 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
14951 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
14952 where DECL_RTL is NULL or is a pseudo-reg.
14954 Note however that we can only get away with using DECL_INCOMING_RTL as
14955 a backup substitute for DECL_RTL in certain limited cases. In cases
14956 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
14957 we can be sure that the parameter was passed using the same type as it is
14958 declared to have within the function, and that its DECL_INCOMING_RTL
14959 points us to a place where a value of that type is passed.
14961 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
14962 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
14963 because in these cases DECL_INCOMING_RTL points us to a value of some
14964 type which is *different* from the type of the parameter itself. Thus,
14965 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
14966 such cases, the debugger would end up (for example) trying to fetch a
14967 `float' from a place which actually contains the first part of a
14968 `double'. That would lead to really incorrect and confusing
14969 output at debug-time.
14971 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
14972 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
14973 are a couple of exceptions however. On little-endian machines we can
14974 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
14975 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
14976 an integral type that is smaller than TREE_TYPE (decl). These cases arise
14977 when (on a little-endian machine) a non-prototyped function has a
14978 parameter declared to be of type `short' or `char'. In such cases,
14979 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
14980 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
14981 passed `int' value. If the debugger then uses that address to fetch
14982 a `short' or a `char' (on a little-endian machine) the result will be
14983 the correct data, so we allow for such exceptional cases below.
14985 Note that our goal here is to describe the place where the given formal
14986 parameter lives during most of the function's activation (i.e. between the
14987 end of the prologue and the start of the epilogue). We'll do that as best
14988 as we can. Note however that if the given formal parameter is modified
14989 sometime during the execution of the function, then a stack backtrace (at
14990 debug-time) will show the function as having been called with the *new*
14991 value rather than the value which was originally passed in. This happens
14992 rarely enough that it is not a major problem, but it *is* a problem, and
14993 I'd like to fix it.
14995 A future version of dwarf2out.c may generate two additional attributes for
14996 any given DW_TAG_formal_parameter DIE which will describe the "passed
14997 type" and the "passed location" for the given formal parameter in addition
14998 to the attributes we now generate to indicate the "declared type" and the
14999 "active location" for each parameter. This additional set of attributes
15000 could be used by debuggers for stack backtraces. Separately, note that
15001 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15002 This happens (for example) for inlined-instances of inline function formal
15003 parameters which are never referenced. This really shouldn't be
15004 happening. All PARM_DECL nodes should get valid non-NULL
15005 DECL_INCOMING_RTL values. FIXME. */
15007 /* Use DECL_RTL as the "location" unless we find something better. */
15008 rtl = DECL_RTL_IF_SET (decl);
15010 /* When generating abstract instances, ignore everything except
15011 constants, symbols living in memory, and symbols living in
15012 fixed registers. */
15013 if (! reload_completed)
15015 if (rtl
15016 && (CONSTANT_P (rtl)
15017 || (MEM_P (rtl)
15018 && CONSTANT_P (XEXP (rtl, 0)))
15019 || (REG_P (rtl)
15020 && TREE_CODE (decl) == VAR_DECL
15021 && TREE_STATIC (decl))))
15023 rtl = targetm.delegitimize_address (rtl);
15024 return rtl;
15026 rtl = NULL_RTX;
15028 else if (TREE_CODE (decl) == PARM_DECL)
15030 if (rtl == NULL_RTX
15031 || is_pseudo_reg (rtl)
15032 || (MEM_P (rtl)
15033 && is_pseudo_reg (XEXP (rtl, 0))
15034 && DECL_INCOMING_RTL (decl)
15035 && MEM_P (DECL_INCOMING_RTL (decl))
15036 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15038 tree declared_type = TREE_TYPE (decl);
15039 tree passed_type = DECL_ARG_TYPE (decl);
15040 enum machine_mode dmode = TYPE_MODE (declared_type);
15041 enum machine_mode pmode = TYPE_MODE (passed_type);
15043 /* This decl represents a formal parameter which was optimized out.
15044 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15045 all cases where (rtl == NULL_RTX) just below. */
15046 if (dmode == pmode)
15047 rtl = DECL_INCOMING_RTL (decl);
15048 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15049 && SCALAR_INT_MODE_P (dmode)
15050 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15051 && DECL_INCOMING_RTL (decl))
15053 rtx inc = DECL_INCOMING_RTL (decl);
15054 if (REG_P (inc))
15055 rtl = inc;
15056 else if (MEM_P (inc))
15058 if (BYTES_BIG_ENDIAN)
15059 rtl = adjust_address_nv (inc, dmode,
15060 GET_MODE_SIZE (pmode)
15061 - GET_MODE_SIZE (dmode));
15062 else
15063 rtl = inc;
15068 /* If the parm was passed in registers, but lives on the stack, then
15069 make a big endian correction if the mode of the type of the
15070 parameter is not the same as the mode of the rtl. */
15071 /* ??? This is the same series of checks that are made in dbxout.c before
15072 we reach the big endian correction code there. It isn't clear if all
15073 of these checks are necessary here, but keeping them all is the safe
15074 thing to do. */
15075 else if (MEM_P (rtl)
15076 && XEXP (rtl, 0) != const0_rtx
15077 && ! CONSTANT_P (XEXP (rtl, 0))
15078 /* Not passed in memory. */
15079 && !MEM_P (DECL_INCOMING_RTL (decl))
15080 /* Not passed by invisible reference. */
15081 && (!REG_P (XEXP (rtl, 0))
15082 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15083 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15084 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15085 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15086 #endif
15088 /* Big endian correction check. */
15089 && BYTES_BIG_ENDIAN
15090 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15091 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15092 < UNITS_PER_WORD))
15094 enum machine_mode addr_mode = get_address_mode (rtl);
15095 int offset = (UNITS_PER_WORD
15096 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15098 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15099 plus_constant (addr_mode, XEXP (rtl, 0), offset));
15102 else if (TREE_CODE (decl) == VAR_DECL
15103 && rtl
15104 && MEM_P (rtl)
15105 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
15106 && BYTES_BIG_ENDIAN)
15108 enum machine_mode addr_mode = get_address_mode (rtl);
15109 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
15110 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
15112 /* If a variable is declared "register" yet is smaller than
15113 a register, then if we store the variable to memory, it
15114 looks like we're storing a register-sized value, when in
15115 fact we are not. We need to adjust the offset of the
15116 storage location to reflect the actual value's bytes,
15117 else gdb will not be able to display it. */
15118 if (rsize > dsize)
15119 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15120 plus_constant (addr_mode, XEXP (rtl, 0),
15121 rsize - dsize));
15124 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
15125 and will have been substituted directly into all expressions that use it.
15126 C does not have such a concept, but C++ and other languages do. */
15127 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
15128 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
15130 if (rtl)
15131 rtl = targetm.delegitimize_address (rtl);
15133 /* If we don't look past the constant pool, we risk emitting a
15134 reference to a constant pool entry that isn't referenced from
15135 code, and thus is not emitted. */
15136 if (rtl)
15137 rtl = avoid_constant_pool_reference (rtl);
15139 /* Try harder to get a rtl. If this symbol ends up not being emitted
15140 in the current CU, resolve_addr will remove the expression referencing
15141 it. */
15142 if (rtl == NULL_RTX
15143 && TREE_CODE (decl) == VAR_DECL
15144 && !DECL_EXTERNAL (decl)
15145 && TREE_STATIC (decl)
15146 && DECL_NAME (decl)
15147 && !DECL_HARD_REGISTER (decl)
15148 && DECL_MODE (decl) != VOIDmode)
15150 rtl = make_decl_rtl_for_debug (decl);
15151 if (!MEM_P (rtl)
15152 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
15153 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
15154 rtl = NULL_RTX;
15157 return rtl;
15160 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
15161 returned. If so, the decl for the COMMON block is returned, and the
15162 value is the offset into the common block for the symbol. */
15164 static tree
15165 fortran_common (tree decl, HOST_WIDE_INT *value)
15167 tree val_expr, cvar;
15168 enum machine_mode mode;
15169 HOST_WIDE_INT bitsize, bitpos;
15170 tree offset;
15171 int unsignedp, volatilep = 0;
15173 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
15174 it does not have a value (the offset into the common area), or if it
15175 is thread local (as opposed to global) then it isn't common, and shouldn't
15176 be handled as such. */
15177 if (TREE_CODE (decl) != VAR_DECL
15178 || !TREE_STATIC (decl)
15179 || !DECL_HAS_VALUE_EXPR_P (decl)
15180 || !is_fortran ())
15181 return NULL_TREE;
15183 val_expr = DECL_VALUE_EXPR (decl);
15184 if (TREE_CODE (val_expr) != COMPONENT_REF)
15185 return NULL_TREE;
15187 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
15188 &mode, &unsignedp, &volatilep, true);
15190 if (cvar == NULL_TREE
15191 || TREE_CODE (cvar) != VAR_DECL
15192 || DECL_ARTIFICIAL (cvar)
15193 || !TREE_PUBLIC (cvar))
15194 return NULL_TREE;
15196 *value = 0;
15197 if (offset != NULL)
15199 if (!host_integerp (offset, 0))
15200 return NULL_TREE;
15201 *value = tree_low_cst (offset, 0);
15203 if (bitpos != 0)
15204 *value += bitpos / BITS_PER_UNIT;
15206 return cvar;
15209 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
15210 data attribute for a variable or a parameter. We generate the
15211 DW_AT_const_value attribute only in those cases where the given variable
15212 or parameter does not have a true "location" either in memory or in a
15213 register. This can happen (for example) when a constant is passed as an
15214 actual argument in a call to an inline function. (It's possible that
15215 these things can crop up in other ways also.) Note that one type of
15216 constant value which can be passed into an inlined function is a constant
15217 pointer. This can happen for example if an actual argument in an inlined
15218 function call evaluates to a compile-time constant address.
15220 CACHE_P is true if it is worth caching the location list for DECL,
15221 so that future calls can reuse it rather than regenerate it from scratch.
15222 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
15223 since we will need to refer to them each time the function is inlined. */
15225 static bool
15226 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
15227 enum dwarf_attribute attr)
15229 rtx rtl;
15230 dw_loc_list_ref list;
15231 var_loc_list *loc_list;
15232 cached_dw_loc_list *cache;
15233 void **slot;
15235 if (TREE_CODE (decl) == ERROR_MARK)
15236 return false;
15238 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
15239 || TREE_CODE (decl) == RESULT_DECL);
15241 /* Try to get some constant RTL for this decl, and use that as the value of
15242 the location. */
15244 rtl = rtl_for_decl_location (decl);
15245 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15246 && add_const_value_attribute (die, rtl))
15247 return true;
15249 /* See if we have single element location list that is equivalent to
15250 a constant value. That way we are better to use add_const_value_attribute
15251 rather than expanding constant value equivalent. */
15252 loc_list = lookup_decl_loc (decl);
15253 if (loc_list
15254 && loc_list->first
15255 && loc_list->first->next == NULL
15256 && NOTE_P (loc_list->first->loc)
15257 && NOTE_VAR_LOCATION (loc_list->first->loc)
15258 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
15260 struct var_loc_node *node;
15262 node = loc_list->first;
15263 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
15264 if (GET_CODE (rtl) == EXPR_LIST)
15265 rtl = XEXP (rtl, 0);
15266 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15267 && add_const_value_attribute (die, rtl))
15268 return true;
15270 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
15271 list several times. See if we've already cached the contents. */
15272 list = NULL;
15273 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
15274 cache_p = false;
15275 if (cache_p)
15277 cache = (cached_dw_loc_list *)
15278 htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
15279 if (cache)
15280 list = cache->loc_list;
15282 if (list == NULL)
15284 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
15285 /* It is usually worth caching this result if the decl is from
15286 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
15287 if (cache_p && list && list->dw_loc_next)
15289 slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
15290 DECL_UID (decl), INSERT);
15291 cache = ggc_alloc_cleared_cached_dw_loc_list ();
15292 cache->decl_id = DECL_UID (decl);
15293 cache->loc_list = list;
15294 *slot = cache;
15297 if (list)
15299 add_AT_location_description (die, attr, list);
15300 return true;
15302 /* None of that worked, so it must not really have a location;
15303 try adding a constant value attribute from the DECL_INITIAL. */
15304 return tree_add_const_value_attribute_for_decl (die, decl);
15307 /* Add VARIABLE and DIE into deferred locations list. */
15309 static void
15310 defer_location (tree variable, dw_die_ref die)
15312 deferred_locations entry;
15313 entry.variable = variable;
15314 entry.die = die;
15315 vec_safe_push (deferred_locations_list, entry);
15318 /* Helper function for tree_add_const_value_attribute. Natively encode
15319 initializer INIT into an array. Return true if successful. */
15321 static bool
15322 native_encode_initializer (tree init, unsigned char *array, int size)
15324 tree type;
15326 if (init == NULL_TREE)
15327 return false;
15329 STRIP_NOPS (init);
15330 switch (TREE_CODE (init))
15332 case STRING_CST:
15333 type = TREE_TYPE (init);
15334 if (TREE_CODE (type) == ARRAY_TYPE)
15336 tree enttype = TREE_TYPE (type);
15337 enum machine_mode mode = TYPE_MODE (enttype);
15339 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
15340 return false;
15341 if (int_size_in_bytes (type) != size)
15342 return false;
15343 if (size > TREE_STRING_LENGTH (init))
15345 memcpy (array, TREE_STRING_POINTER (init),
15346 TREE_STRING_LENGTH (init));
15347 memset (array + TREE_STRING_LENGTH (init),
15348 '\0', size - TREE_STRING_LENGTH (init));
15350 else
15351 memcpy (array, TREE_STRING_POINTER (init), size);
15352 return true;
15354 return false;
15355 case CONSTRUCTOR:
15356 type = TREE_TYPE (init);
15357 if (int_size_in_bytes (type) != size)
15358 return false;
15359 if (TREE_CODE (type) == ARRAY_TYPE)
15361 HOST_WIDE_INT min_index;
15362 unsigned HOST_WIDE_INT cnt;
15363 int curpos = 0, fieldsize;
15364 constructor_elt *ce;
15366 if (TYPE_DOMAIN (type) == NULL_TREE
15367 || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
15368 return false;
15370 fieldsize = int_size_in_bytes (TREE_TYPE (type));
15371 if (fieldsize <= 0)
15372 return false;
15374 min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
15375 memset (array, '\0', size);
15376 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15378 tree val = ce->value;
15379 tree index = ce->index;
15380 int pos = curpos;
15381 if (index && TREE_CODE (index) == RANGE_EXPR)
15382 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
15383 * fieldsize;
15384 else if (index)
15385 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
15387 if (val)
15389 STRIP_NOPS (val);
15390 if (!native_encode_initializer (val, array + pos, fieldsize))
15391 return false;
15393 curpos = pos + fieldsize;
15394 if (index && TREE_CODE (index) == RANGE_EXPR)
15396 int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
15397 - tree_low_cst (TREE_OPERAND (index, 0), 0);
15398 while (count-- > 0)
15400 if (val)
15401 memcpy (array + curpos, array + pos, fieldsize);
15402 curpos += fieldsize;
15405 gcc_assert (curpos <= size);
15407 return true;
15409 else if (TREE_CODE (type) == RECORD_TYPE
15410 || TREE_CODE (type) == UNION_TYPE)
15412 tree field = NULL_TREE;
15413 unsigned HOST_WIDE_INT cnt;
15414 constructor_elt *ce;
15416 if (int_size_in_bytes (type) != size)
15417 return false;
15419 if (TREE_CODE (type) == RECORD_TYPE)
15420 field = TYPE_FIELDS (type);
15422 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15424 tree val = ce->value;
15425 int pos, fieldsize;
15427 if (ce->index != 0)
15428 field = ce->index;
15430 if (val)
15431 STRIP_NOPS (val);
15433 if (field == NULL_TREE || DECL_BIT_FIELD (field))
15434 return false;
15436 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
15437 && TYPE_DOMAIN (TREE_TYPE (field))
15438 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
15439 return false;
15440 else if (DECL_SIZE_UNIT (field) == NULL_TREE
15441 || !host_integerp (DECL_SIZE_UNIT (field), 0))
15442 return false;
15443 fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
15444 pos = int_byte_position (field);
15445 gcc_assert (pos + fieldsize <= size);
15446 if (val
15447 && !native_encode_initializer (val, array + pos, fieldsize))
15448 return false;
15450 return true;
15452 return false;
15453 case VIEW_CONVERT_EXPR:
15454 case NON_LVALUE_EXPR:
15455 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
15456 default:
15457 return native_encode_expr (init, array, size) == size;
15461 /* Attach a DW_AT_const_value attribute to DIE. The value of the
15462 attribute is the const value T. */
15464 static bool
15465 tree_add_const_value_attribute (dw_die_ref die, tree t)
15467 tree init;
15468 tree type = TREE_TYPE (t);
15469 rtx rtl;
15471 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
15472 return false;
15474 init = t;
15475 gcc_assert (!DECL_P (init));
15477 rtl = rtl_for_decl_init (init, type);
15478 if (rtl)
15479 return add_const_value_attribute (die, rtl);
15480 /* If the host and target are sane, try harder. */
15481 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
15482 && initializer_constant_valid_p (init, type))
15484 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
15485 if (size > 0 && (int) size == size)
15487 unsigned char *array = (unsigned char *)
15488 ggc_alloc_cleared_atomic (size);
15490 if (native_encode_initializer (init, array, size))
15492 add_AT_vec (die, DW_AT_const_value, size, 1, array);
15493 return true;
15497 return false;
15500 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
15501 attribute is the const value of T, where T is an integral constant
15502 variable with static storage duration
15503 (so it can't be a PARM_DECL or a RESULT_DECL). */
15505 static bool
15506 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
15509 if (!decl
15510 || (TREE_CODE (decl) != VAR_DECL
15511 && TREE_CODE (decl) != CONST_DECL)
15512 || (TREE_CODE (decl) == VAR_DECL
15513 && !TREE_STATIC (decl)))
15514 return false;
15516 if (TREE_READONLY (decl)
15517 && ! TREE_THIS_VOLATILE (decl)
15518 && DECL_INITIAL (decl))
15519 /* OK */;
15520 else
15521 return false;
15523 /* Don't add DW_AT_const_value if abstract origin already has one. */
15524 if (get_AT (var_die, DW_AT_const_value))
15525 return false;
15527 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
15530 /* Convert the CFI instructions for the current function into a
15531 location list. This is used for DW_AT_frame_base when we targeting
15532 a dwarf2 consumer that does not support the dwarf3
15533 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
15534 expressions. */
15536 static dw_loc_list_ref
15537 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
15539 int ix;
15540 dw_fde_ref fde;
15541 dw_loc_list_ref list, *list_tail;
15542 dw_cfi_ref cfi;
15543 dw_cfa_location last_cfa, next_cfa;
15544 const char *start_label, *last_label, *section;
15545 dw_cfa_location remember;
15547 fde = cfun->fde;
15548 gcc_assert (fde != NULL);
15550 section = secname_for_decl (current_function_decl);
15551 list_tail = &list;
15552 list = NULL;
15554 memset (&next_cfa, 0, sizeof (next_cfa));
15555 next_cfa.reg = INVALID_REGNUM;
15556 remember = next_cfa;
15558 start_label = fde->dw_fde_begin;
15560 /* ??? Bald assumption that the CIE opcode list does not contain
15561 advance opcodes. */
15562 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
15563 lookup_cfa_1 (cfi, &next_cfa, &remember);
15565 last_cfa = next_cfa;
15566 last_label = start_label;
15568 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
15570 /* If the first partition contained no CFI adjustments, the
15571 CIE opcodes apply to the whole first partition. */
15572 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15573 fde->dw_fde_begin, fde->dw_fde_end, section);
15574 list_tail =&(*list_tail)->dw_loc_next;
15575 start_label = last_label = fde->dw_fde_second_begin;
15578 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
15580 switch (cfi->dw_cfi_opc)
15582 case DW_CFA_set_loc:
15583 case DW_CFA_advance_loc1:
15584 case DW_CFA_advance_loc2:
15585 case DW_CFA_advance_loc4:
15586 if (!cfa_equal_p (&last_cfa, &next_cfa))
15588 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15589 start_label, last_label, section);
15591 list_tail = &(*list_tail)->dw_loc_next;
15592 last_cfa = next_cfa;
15593 start_label = last_label;
15595 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
15596 break;
15598 case DW_CFA_advance_loc:
15599 /* The encoding is complex enough that we should never emit this. */
15600 gcc_unreachable ();
15602 default:
15603 lookup_cfa_1 (cfi, &next_cfa, &remember);
15604 break;
15606 if (ix + 1 == fde->dw_fde_switch_cfi_index)
15608 if (!cfa_equal_p (&last_cfa, &next_cfa))
15610 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15611 start_label, last_label, section);
15613 list_tail = &(*list_tail)->dw_loc_next;
15614 last_cfa = next_cfa;
15615 start_label = last_label;
15617 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15618 start_label, fde->dw_fde_end, section);
15619 list_tail = &(*list_tail)->dw_loc_next;
15620 start_label = last_label = fde->dw_fde_second_begin;
15624 if (!cfa_equal_p (&last_cfa, &next_cfa))
15626 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15627 start_label, last_label, section);
15628 list_tail = &(*list_tail)->dw_loc_next;
15629 start_label = last_label;
15632 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
15633 start_label,
15634 fde->dw_fde_second_begin
15635 ? fde->dw_fde_second_end : fde->dw_fde_end,
15636 section);
15638 if (list && list->dw_loc_next)
15639 gen_llsym (list);
15641 return list;
15644 /* Compute a displacement from the "steady-state frame pointer" to the
15645 frame base (often the same as the CFA), and store it in
15646 frame_pointer_fb_offset. OFFSET is added to the displacement
15647 before the latter is negated. */
15649 static void
15650 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
15652 rtx reg, elim;
15654 #ifdef FRAME_POINTER_CFA_OFFSET
15655 reg = frame_pointer_rtx;
15656 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
15657 #else
15658 reg = arg_pointer_rtx;
15659 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
15660 #endif
15662 elim = (ira_use_lra_p
15663 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
15664 : eliminate_regs (reg, VOIDmode, NULL_RTX));
15665 if (GET_CODE (elim) == PLUS)
15667 offset += INTVAL (XEXP (elim, 1));
15668 elim = XEXP (elim, 0);
15671 frame_pointer_fb_offset = -offset;
15673 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
15674 in which to eliminate. This is because it's stack pointer isn't
15675 directly accessible as a register within the ISA. To work around
15676 this, assume that while we cannot provide a proper value for
15677 frame_pointer_fb_offset, we won't need one either. */
15678 frame_pointer_fb_offset_valid
15679 = ((SUPPORTS_STACK_ALIGNMENT
15680 && (elim == hard_frame_pointer_rtx
15681 || elim == stack_pointer_rtx))
15682 || elim == (frame_pointer_needed
15683 ? hard_frame_pointer_rtx
15684 : stack_pointer_rtx));
15687 /* Generate a DW_AT_name attribute given some string value to be included as
15688 the value of the attribute. */
15690 static void
15691 add_name_attribute (dw_die_ref die, const char *name_string)
15693 if (name_string != NULL && *name_string != 0)
15695 if (demangle_name_func)
15696 name_string = (*demangle_name_func) (name_string);
15698 add_AT_string (die, DW_AT_name, name_string);
15702 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
15703 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
15704 of TYPE accordingly.
15706 ??? This is a temporary measure until after we're able to generate
15707 regular DWARF for the complex Ada type system. */
15709 static void
15710 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
15711 dw_die_ref context_die)
15713 tree dtype;
15714 dw_die_ref dtype_die;
15716 if (!lang_hooks.types.descriptive_type)
15717 return;
15719 dtype = lang_hooks.types.descriptive_type (type);
15720 if (!dtype)
15721 return;
15723 dtype_die = lookup_type_die (dtype);
15724 if (!dtype_die)
15726 gen_type_die (dtype, context_die);
15727 dtype_die = lookup_type_die (dtype);
15728 gcc_assert (dtype_die);
15731 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
15734 /* Generate a DW_AT_comp_dir attribute for DIE. */
15736 static void
15737 add_comp_dir_attribute (dw_die_ref die)
15739 const char *wd = get_src_pwd ();
15740 char *wd1;
15742 if (wd == NULL)
15743 return;
15745 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
15747 int wdlen;
15749 wdlen = strlen (wd);
15750 wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
15751 strcpy (wd1, wd);
15752 wd1 [wdlen] = DIR_SEPARATOR;
15753 wd1 [wdlen + 1] = 0;
15754 wd = wd1;
15757 add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
15760 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
15761 default. */
15763 static int
15764 lower_bound_default (void)
15766 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
15768 case DW_LANG_C:
15769 case DW_LANG_C89:
15770 case DW_LANG_C99:
15771 case DW_LANG_C_plus_plus:
15772 case DW_LANG_ObjC:
15773 case DW_LANG_ObjC_plus_plus:
15774 case DW_LANG_Java:
15775 return 0;
15776 case DW_LANG_Fortran77:
15777 case DW_LANG_Fortran90:
15778 case DW_LANG_Fortran95:
15779 return 1;
15780 case DW_LANG_UPC:
15781 case DW_LANG_D:
15782 case DW_LANG_Python:
15783 return dwarf_version >= 4 ? 0 : -1;
15784 case DW_LANG_Ada95:
15785 case DW_LANG_Ada83:
15786 case DW_LANG_Cobol74:
15787 case DW_LANG_Cobol85:
15788 case DW_LANG_Pascal83:
15789 case DW_LANG_Modula2:
15790 case DW_LANG_PLI:
15791 return dwarf_version >= 4 ? 1 : -1;
15792 default:
15793 return -1;
15797 /* Given a tree node describing an array bound (either lower or upper) output
15798 a representation for that bound. */
15800 static void
15801 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
15803 switch (TREE_CODE (bound))
15805 case ERROR_MARK:
15806 return;
15808 /* All fixed-bounds are represented by INTEGER_CST nodes. */
15809 case INTEGER_CST:
15811 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
15812 int dflt;
15814 /* Use the default if possible. */
15815 if (bound_attr == DW_AT_lower_bound
15816 && host_integerp (bound, 0)
15817 && (dflt = lower_bound_default ()) != -1
15818 && tree_low_cst (bound, 0) == dflt)
15821 /* Otherwise represent the bound as an unsigned value with the
15822 precision of its type. The precision and signedness of the
15823 type will be necessary to re-interpret it unambiguously. */
15824 else if (prec < HOST_BITS_PER_WIDE_INT)
15826 unsigned HOST_WIDE_INT mask
15827 = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
15828 add_AT_unsigned (subrange_die, bound_attr,
15829 TREE_INT_CST_LOW (bound) & mask);
15831 else if (prec == HOST_BITS_PER_WIDE_INT
15832 || TREE_INT_CST_HIGH (bound) == 0)
15833 add_AT_unsigned (subrange_die, bound_attr,
15834 TREE_INT_CST_LOW (bound));
15835 else
15836 add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
15837 TREE_INT_CST_LOW (bound));
15839 break;
15841 CASE_CONVERT:
15842 case VIEW_CONVERT_EXPR:
15843 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
15844 break;
15846 case SAVE_EXPR:
15847 break;
15849 case VAR_DECL:
15850 case PARM_DECL:
15851 case RESULT_DECL:
15853 dw_die_ref decl_die = lookup_decl_die (bound);
15855 /* ??? Can this happen, or should the variable have been bound
15856 first? Probably it can, since I imagine that we try to create
15857 the types of parameters in the order in which they exist in
15858 the list, and won't have created a forward reference to a
15859 later parameter. */
15860 if (decl_die != NULL)
15862 add_AT_die_ref (subrange_die, bound_attr, decl_die);
15863 break;
15866 /* FALLTHRU */
15868 default:
15870 /* Otherwise try to create a stack operation procedure to
15871 evaluate the value of the array bound. */
15873 dw_die_ref ctx, decl_die;
15874 dw_loc_list_ref list;
15876 list = loc_list_from_tree (bound, 2);
15877 if (list == NULL || single_element_loc_list_p (list))
15879 /* If DW_AT_*bound is not a reference nor constant, it is
15880 a DWARF expression rather than location description.
15881 For that loc_list_from_tree (bound, 0) is needed.
15882 If that fails to give a single element list,
15883 fall back to outputting this as a reference anyway. */
15884 dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
15885 if (list2 && single_element_loc_list_p (list2))
15887 add_AT_loc (subrange_die, bound_attr, list2->expr);
15888 break;
15891 if (list == NULL)
15892 break;
15894 if (current_function_decl == 0)
15895 ctx = comp_unit_die ();
15896 else
15897 ctx = lookup_decl_die (current_function_decl);
15899 decl_die = new_die (DW_TAG_variable, ctx, bound);
15900 add_AT_flag (decl_die, DW_AT_artificial, 1);
15901 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
15902 add_AT_location_description (decl_die, DW_AT_location, list);
15903 add_AT_die_ref (subrange_die, bound_attr, decl_die);
15904 break;
15909 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
15910 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
15911 Note that the block of subscript information for an array type also
15912 includes information about the element type of the given array type. */
15914 static void
15915 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
15917 unsigned dimension_number;
15918 tree lower, upper;
15919 dw_die_ref subrange_die;
15921 for (dimension_number = 0;
15922 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
15923 type = TREE_TYPE (type), dimension_number++)
15925 tree domain = TYPE_DOMAIN (type);
15927 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
15928 break;
15930 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
15931 and (in GNU C only) variable bounds. Handle all three forms
15932 here. */
15933 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
15934 if (domain)
15936 /* We have an array type with specified bounds. */
15937 lower = TYPE_MIN_VALUE (domain);
15938 upper = TYPE_MAX_VALUE (domain);
15940 /* Define the index type. */
15941 if (TREE_TYPE (domain))
15943 /* ??? This is probably an Ada unnamed subrange type. Ignore the
15944 TREE_TYPE field. We can't emit debug info for this
15945 because it is an unnamed integral type. */
15946 if (TREE_CODE (domain) == INTEGER_TYPE
15947 && TYPE_NAME (domain) == NULL_TREE
15948 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
15949 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
15951 else
15952 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
15953 type_die);
15956 /* ??? If upper is NULL, the array has unspecified length,
15957 but it does have a lower bound. This happens with Fortran
15958 dimension arr(N:*)
15959 Since the debugger is definitely going to need to know N
15960 to produce useful results, go ahead and output the lower
15961 bound solo, and hope the debugger can cope. */
15963 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
15964 if (upper)
15965 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
15968 /* Otherwise we have an array type with an unspecified length. The
15969 DWARF-2 spec does not say how to handle this; let's just leave out the
15970 bounds. */
15974 static void
15975 add_byte_size_attribute (dw_die_ref die, tree tree_node)
15977 dw_die_ref decl_die;
15978 unsigned size;
15980 switch (TREE_CODE (tree_node))
15982 case ERROR_MARK:
15983 size = 0;
15984 break;
15985 case ENUMERAL_TYPE:
15986 case RECORD_TYPE:
15987 case UNION_TYPE:
15988 case QUAL_UNION_TYPE:
15989 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
15990 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
15992 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
15993 return;
15995 size = int_size_in_bytes (tree_node);
15996 break;
15997 case FIELD_DECL:
15998 /* For a data member of a struct or union, the DW_AT_byte_size is
15999 generally given as the number of bytes normally allocated for an
16000 object of the *declared* type of the member itself. This is true
16001 even for bit-fields. */
16002 size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
16003 break;
16004 default:
16005 gcc_unreachable ();
16008 /* Note that `size' might be -1 when we get to this point. If it is, that
16009 indicates that the byte size of the entity in question is variable. We
16010 have no good way of expressing this fact in Dwarf at the present time,
16011 so just let the -1 pass on through. */
16012 add_AT_unsigned (die, DW_AT_byte_size, size);
16015 /* For a FIELD_DECL node which represents a bit-field, output an attribute
16016 which specifies the distance in bits from the highest order bit of the
16017 "containing object" for the bit-field to the highest order bit of the
16018 bit-field itself.
16020 For any given bit-field, the "containing object" is a hypothetical object
16021 (of some integral or enum type) within which the given bit-field lives. The
16022 type of this hypothetical "containing object" is always the same as the
16023 declared type of the individual bit-field itself. The determination of the
16024 exact location of the "containing object" for a bit-field is rather
16025 complicated. It's handled by the `field_byte_offset' function (above).
16027 Note that it is the size (in bytes) of the hypothetical "containing object"
16028 which will be given in the DW_AT_byte_size attribute for this bit-field.
16029 (See `byte_size_attribute' above). */
16031 static inline void
16032 add_bit_offset_attribute (dw_die_ref die, tree decl)
16034 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
16035 tree type = DECL_BIT_FIELD_TYPE (decl);
16036 HOST_WIDE_INT bitpos_int;
16037 HOST_WIDE_INT highest_order_object_bit_offset;
16038 HOST_WIDE_INT highest_order_field_bit_offset;
16039 HOST_WIDE_INT bit_offset;
16041 /* Must be a field and a bit field. */
16042 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
16044 /* We can't yet handle bit-fields whose offsets are variable, so if we
16045 encounter such things, just return without generating any attribute
16046 whatsoever. Likewise for variable or too large size. */
16047 if (! host_integerp (bit_position (decl), 0)
16048 || ! host_integerp (DECL_SIZE (decl), 1))
16049 return;
16051 bitpos_int = int_bit_position (decl);
16053 /* Note that the bit offset is always the distance (in bits) from the
16054 highest-order bit of the "containing object" to the highest-order bit of
16055 the bit-field itself. Since the "high-order end" of any object or field
16056 is different on big-endian and little-endian machines, the computation
16057 below must take account of these differences. */
16058 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
16059 highest_order_field_bit_offset = bitpos_int;
16061 if (! BYTES_BIG_ENDIAN)
16063 highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
16064 highest_order_object_bit_offset += simple_type_size_in_bits (type);
16067 bit_offset
16068 = (! BYTES_BIG_ENDIAN
16069 ? highest_order_object_bit_offset - highest_order_field_bit_offset
16070 : highest_order_field_bit_offset - highest_order_object_bit_offset);
16072 if (bit_offset < 0)
16073 add_AT_int (die, DW_AT_bit_offset, bit_offset);
16074 else
16075 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
16078 /* For a FIELD_DECL node which represents a bit field, output an attribute
16079 which specifies the length in bits of the given field. */
16081 static inline void
16082 add_bit_size_attribute (dw_die_ref die, tree decl)
16084 /* Must be a field and a bit field. */
16085 gcc_assert (TREE_CODE (decl) == FIELD_DECL
16086 && DECL_BIT_FIELD_TYPE (decl));
16088 if (host_integerp (DECL_SIZE (decl), 1))
16089 add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
16092 /* If the compiled language is ANSI C, then add a 'prototyped'
16093 attribute, if arg types are given for the parameters of a function. */
16095 static inline void
16096 add_prototyped_attribute (dw_die_ref die, tree func_type)
16098 if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
16099 && prototype_p (func_type))
16100 add_AT_flag (die, DW_AT_prototyped, 1);
16103 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
16104 by looking in either the type declaration or object declaration
16105 equate table. */
16107 static inline dw_die_ref
16108 add_abstract_origin_attribute (dw_die_ref die, tree origin)
16110 dw_die_ref origin_die = NULL;
16112 if (TREE_CODE (origin) != FUNCTION_DECL)
16114 /* We may have gotten separated from the block for the inlined
16115 function, if we're in an exception handler or some such; make
16116 sure that the abstract function has been written out.
16118 Doing this for nested functions is wrong, however; functions are
16119 distinct units, and our context might not even be inline. */
16120 tree fn = origin;
16122 if (TYPE_P (fn))
16123 fn = TYPE_STUB_DECL (fn);
16125 fn = decl_function_context (fn);
16126 if (fn)
16127 dwarf2out_abstract_function (fn);
16130 if (DECL_P (origin))
16131 origin_die = lookup_decl_die (origin);
16132 else if (TYPE_P (origin))
16133 origin_die = lookup_type_die (origin);
16135 /* XXX: Functions that are never lowered don't always have correct block
16136 trees (in the case of java, they simply have no block tree, in some other
16137 languages). For these functions, there is nothing we can really do to
16138 output correct debug info for inlined functions in all cases. Rather
16139 than die, we'll just produce deficient debug info now, in that we will
16140 have variables without a proper abstract origin. In the future, when all
16141 functions are lowered, we should re-add a gcc_assert (origin_die)
16142 here. */
16144 if (origin_die)
16145 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
16146 return origin_die;
16149 /* We do not currently support the pure_virtual attribute. */
16151 static inline void
16152 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
16154 if (DECL_VINDEX (func_decl))
16156 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
16158 if (host_integerp (DECL_VINDEX (func_decl), 0))
16159 add_AT_loc (die, DW_AT_vtable_elem_location,
16160 new_loc_descr (DW_OP_constu,
16161 tree_low_cst (DECL_VINDEX (func_decl), 0),
16162 0));
16164 /* GNU extension: Record what type this method came from originally. */
16165 if (debug_info_level > DINFO_LEVEL_TERSE
16166 && DECL_CONTEXT (func_decl))
16167 add_AT_die_ref (die, DW_AT_containing_type,
16168 lookup_type_die (DECL_CONTEXT (func_decl)));
16172 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
16173 given decl. This used to be a vendor extension until after DWARF 4
16174 standardized it. */
16176 static void
16177 add_linkage_attr (dw_die_ref die, tree decl)
16179 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
16181 /* Mimic what assemble_name_raw does with a leading '*'. */
16182 if (name[0] == '*')
16183 name = &name[1];
16185 if (dwarf_version >= 4)
16186 add_AT_string (die, DW_AT_linkage_name, name);
16187 else
16188 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
16191 /* Add source coordinate attributes for the given decl. */
16193 static void
16194 add_src_coords_attributes (dw_die_ref die, tree decl)
16196 expanded_location s;
16198 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
16199 return;
16200 s = expand_location (DECL_SOURCE_LOCATION (decl));
16201 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
16202 add_AT_unsigned (die, DW_AT_decl_line, s.line);
16205 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
16207 static void
16208 add_linkage_name (dw_die_ref die, tree decl)
16210 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
16211 && TREE_PUBLIC (decl)
16212 && !DECL_ABSTRACT (decl)
16213 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
16214 && die->die_tag != DW_TAG_member)
16216 /* Defer until we have an assembler name set. */
16217 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
16219 limbo_die_node *asm_name;
16221 asm_name = ggc_alloc_cleared_limbo_die_node ();
16222 asm_name->die = die;
16223 asm_name->created_for = decl;
16224 asm_name->next = deferred_asm_name;
16225 deferred_asm_name = asm_name;
16227 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
16228 add_linkage_attr (die, decl);
16232 /* Add a DW_AT_name attribute and source coordinate attribute for the
16233 given decl, but only if it actually has a name. */
16235 static void
16236 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
16238 tree decl_name;
16240 decl_name = DECL_NAME (decl);
16241 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
16243 const char *name = dwarf2_name (decl, 0);
16244 if (name)
16245 add_name_attribute (die, name);
16246 if (! DECL_ARTIFICIAL (decl))
16247 add_src_coords_attributes (die, decl);
16249 add_linkage_name (die, decl);
16252 #ifdef VMS_DEBUGGING_INFO
16253 /* Get the function's name, as described by its RTL. This may be different
16254 from the DECL_NAME name used in the source file. */
16255 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
16257 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
16258 XEXP (DECL_RTL (decl), 0), false);
16259 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
16261 #endif /* VMS_DEBUGGING_INFO */
16264 #ifdef VMS_DEBUGGING_INFO
16265 /* Output the debug main pointer die for VMS */
16267 void
16268 dwarf2out_vms_debug_main_pointer (void)
16270 char label[MAX_ARTIFICIAL_LABEL_BYTES];
16271 dw_die_ref die;
16273 /* Allocate the VMS debug main subprogram die. */
16274 die = ggc_alloc_cleared_die_node ();
16275 die->die_tag = DW_TAG_subprogram;
16276 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
16277 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
16278 current_function_funcdef_no);
16279 add_AT_lbl_id (die, DW_AT_entry_pc, label);
16281 /* Make it the first child of comp_unit_die (). */
16282 die->die_parent = comp_unit_die ();
16283 if (comp_unit_die ()->die_child)
16285 die->die_sib = comp_unit_die ()->die_child->die_sib;
16286 comp_unit_die ()->die_child->die_sib = die;
16288 else
16290 die->die_sib = die;
16291 comp_unit_die ()->die_child = die;
16294 #endif /* VMS_DEBUGGING_INFO */
16296 /* Push a new declaration scope. */
16298 static void
16299 push_decl_scope (tree scope)
16301 vec_safe_push (decl_scope_table, scope);
16304 /* Pop a declaration scope. */
16306 static inline void
16307 pop_decl_scope (void)
16309 decl_scope_table->pop ();
16312 /* walk_tree helper function for uses_local_type, below. */
16314 static tree
16315 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
16317 if (!TYPE_P (*tp))
16318 *walk_subtrees = 0;
16319 else
16321 tree name = TYPE_NAME (*tp);
16322 if (name && DECL_P (name) && decl_function_context (name))
16323 return *tp;
16325 return NULL_TREE;
16328 /* If TYPE involves a function-local type (including a local typedef to a
16329 non-local type), returns that type; otherwise returns NULL_TREE. */
16331 static tree
16332 uses_local_type (tree type)
16334 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
16335 return used;
16338 /* Return the DIE for the scope that immediately contains this type.
16339 Non-named types that do not involve a function-local type get global
16340 scope. Named types nested in namespaces or other types get their
16341 containing scope. All other types (i.e. function-local named types) get
16342 the current active scope. */
16344 static dw_die_ref
16345 scope_die_for (tree t, dw_die_ref context_die)
16347 dw_die_ref scope_die = NULL;
16348 tree containing_scope;
16350 /* Non-types always go in the current scope. */
16351 gcc_assert (TYPE_P (t));
16353 /* Use the scope of the typedef, rather than the scope of the type
16354 it refers to. */
16355 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
16356 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
16357 else
16358 containing_scope = TYPE_CONTEXT (t);
16360 /* Use the containing namespace if there is one. */
16361 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
16363 if (context_die == lookup_decl_die (containing_scope))
16364 /* OK */;
16365 else if (debug_info_level > DINFO_LEVEL_TERSE)
16366 context_die = get_context_die (containing_scope);
16367 else
16368 containing_scope = NULL_TREE;
16371 /* Ignore function type "scopes" from the C frontend. They mean that
16372 a tagged type is local to a parmlist of a function declarator, but
16373 that isn't useful to DWARF. */
16374 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
16375 containing_scope = NULL_TREE;
16377 if (SCOPE_FILE_SCOPE_P (containing_scope))
16379 /* If T uses a local type keep it local as well, to avoid references
16380 to function-local DIEs from outside the function. */
16381 if (current_function_decl && uses_local_type (t))
16382 scope_die = context_die;
16383 else
16384 scope_die = comp_unit_die ();
16386 else if (TYPE_P (containing_scope))
16388 /* For types, we can just look up the appropriate DIE. */
16389 if (debug_info_level > DINFO_LEVEL_TERSE)
16390 scope_die = get_context_die (containing_scope);
16391 else
16393 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
16394 if (scope_die == NULL)
16395 scope_die = comp_unit_die ();
16398 else
16399 scope_die = context_die;
16401 return scope_die;
16404 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
16406 static inline int
16407 local_scope_p (dw_die_ref context_die)
16409 for (; context_die; context_die = context_die->die_parent)
16410 if (context_die->die_tag == DW_TAG_inlined_subroutine
16411 || context_die->die_tag == DW_TAG_subprogram)
16412 return 1;
16414 return 0;
16417 /* Returns nonzero if CONTEXT_DIE is a class. */
16419 static inline int
16420 class_scope_p (dw_die_ref context_die)
16422 return (context_die
16423 && (context_die->die_tag == DW_TAG_structure_type
16424 || context_die->die_tag == DW_TAG_class_type
16425 || context_die->die_tag == DW_TAG_interface_type
16426 || context_die->die_tag == DW_TAG_union_type));
16429 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
16430 whether or not to treat a DIE in this context as a declaration. */
16432 static inline int
16433 class_or_namespace_scope_p (dw_die_ref context_die)
16435 return (class_scope_p (context_die)
16436 || (context_die && context_die->die_tag == DW_TAG_namespace));
16439 /* Many forms of DIEs require a "type description" attribute. This
16440 routine locates the proper "type descriptor" die for the type given
16441 by 'type', and adds a DW_AT_type attribute below the given die. */
16443 static void
16444 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
16445 int decl_volatile, dw_die_ref context_die)
16447 enum tree_code code = TREE_CODE (type);
16448 dw_die_ref type_die = NULL;
16450 /* ??? If this type is an unnamed subrange type of an integral, floating-point
16451 or fixed-point type, use the inner type. This is because we have no
16452 support for unnamed types in base_type_die. This can happen if this is
16453 an Ada subrange type. Correct solution is emit a subrange type die. */
16454 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
16455 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
16456 type = TREE_TYPE (type), code = TREE_CODE (type);
16458 if (code == ERROR_MARK
16459 /* Handle a special case. For functions whose return type is void, we
16460 generate *no* type attribute. (Note that no object may have type
16461 `void', so this only applies to function return types). */
16462 || code == VOID_TYPE)
16463 return;
16465 type_die = modified_type_die (type,
16466 decl_const || TYPE_READONLY (type),
16467 decl_volatile || TYPE_VOLATILE (type),
16468 context_die);
16470 if (type_die != NULL)
16471 add_AT_die_ref (object_die, DW_AT_type, type_die);
16474 /* Given an object die, add the calling convention attribute for the
16475 function call type. */
16476 static void
16477 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
16479 enum dwarf_calling_convention value = DW_CC_normal;
16481 value = ((enum dwarf_calling_convention)
16482 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
16484 if (is_fortran ()
16485 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
16487 /* DWARF 2 doesn't provide a way to identify a program's source-level
16488 entry point. DW_AT_calling_convention attributes are only meant
16489 to describe functions' calling conventions. However, lacking a
16490 better way to signal the Fortran main program, we used this for
16491 a long time, following existing custom. Now, DWARF 4 has
16492 DW_AT_main_subprogram, which we add below, but some tools still
16493 rely on the old way, which we thus keep. */
16494 value = DW_CC_program;
16496 if (dwarf_version >= 4 || !dwarf_strict)
16497 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
16500 /* Only add the attribute if the backend requests it, and
16501 is not DW_CC_normal. */
16502 if (value && (value != DW_CC_normal))
16503 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
16506 /* Given a tree pointer to a struct, class, union, or enum type node, return
16507 a pointer to the (string) tag name for the given type, or zero if the type
16508 was declared without a tag. */
16510 static const char *
16511 type_tag (const_tree type)
16513 const char *name = 0;
16515 if (TYPE_NAME (type) != 0)
16517 tree t = 0;
16519 /* Find the IDENTIFIER_NODE for the type name. */
16520 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
16521 && !TYPE_NAMELESS (type))
16522 t = TYPE_NAME (type);
16524 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
16525 a TYPE_DECL node, regardless of whether or not a `typedef' was
16526 involved. */
16527 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
16528 && ! DECL_IGNORED_P (TYPE_NAME (type)))
16530 /* We want to be extra verbose. Don't call dwarf_name if
16531 DECL_NAME isn't set. The default hook for decl_printable_name
16532 doesn't like that, and in this context it's correct to return
16533 0, instead of "<anonymous>" or the like. */
16534 if (DECL_NAME (TYPE_NAME (type))
16535 && !DECL_NAMELESS (TYPE_NAME (type)))
16536 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
16539 /* Now get the name as a string, or invent one. */
16540 if (!name && t != 0)
16541 name = IDENTIFIER_POINTER (t);
16544 return (name == 0 || *name == '\0') ? 0 : name;
16547 /* Return the type associated with a data member, make a special check
16548 for bit field types. */
16550 static inline tree
16551 member_declared_type (const_tree member)
16553 return (DECL_BIT_FIELD_TYPE (member)
16554 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
16557 /* Get the decl's label, as described by its RTL. This may be different
16558 from the DECL_NAME name used in the source file. */
16560 #if 0
16561 static const char *
16562 decl_start_label (tree decl)
16564 rtx x;
16565 const char *fnname;
16567 x = DECL_RTL (decl);
16568 gcc_assert (MEM_P (x));
16570 x = XEXP (x, 0);
16571 gcc_assert (GET_CODE (x) == SYMBOL_REF);
16573 fnname = XSTR (x, 0);
16574 return fnname;
16576 #endif
16578 /* These routines generate the internal representation of the DIE's for
16579 the compilation unit. Debugging information is collected by walking
16580 the declaration trees passed in from dwarf2out_decl(). */
16582 static void
16583 gen_array_type_die (tree type, dw_die_ref context_die)
16585 dw_die_ref scope_die = scope_die_for (type, context_die);
16586 dw_die_ref array_die;
16588 /* GNU compilers represent multidimensional array types as sequences of one
16589 dimensional array types whose element types are themselves array types.
16590 We sometimes squish that down to a single array_type DIE with multiple
16591 subscripts in the Dwarf debugging info. The draft Dwarf specification
16592 say that we are allowed to do this kind of compression in C, because
16593 there is no difference between an array of arrays and a multidimensional
16594 array. We don't do this for Ada to remain as close as possible to the
16595 actual representation, which is especially important against the language
16596 flexibilty wrt arrays of variable size. */
16598 bool collapse_nested_arrays = !is_ada ();
16599 tree element_type;
16601 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
16602 DW_TAG_string_type doesn't have DW_AT_type attribute). */
16603 if (TYPE_STRING_FLAG (type)
16604 && TREE_CODE (type) == ARRAY_TYPE
16605 && is_fortran ()
16606 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
16608 HOST_WIDE_INT size;
16610 array_die = new_die (DW_TAG_string_type, scope_die, type);
16611 add_name_attribute (array_die, type_tag (type));
16612 equate_type_number_to_die (type, array_die);
16613 size = int_size_in_bytes (type);
16614 if (size >= 0)
16615 add_AT_unsigned (array_die, DW_AT_byte_size, size);
16616 else if (TYPE_DOMAIN (type) != NULL_TREE
16617 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
16618 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
16620 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
16621 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
16623 size = int_size_in_bytes (TREE_TYPE (szdecl));
16624 if (loc && size > 0)
16626 add_AT_location_description (array_die, DW_AT_string_length, loc);
16627 if (size != DWARF2_ADDR_SIZE)
16628 add_AT_unsigned (array_die, DW_AT_byte_size, size);
16631 return;
16634 array_die = new_die (DW_TAG_array_type, scope_die, type);
16635 add_name_attribute (array_die, type_tag (type));
16636 equate_type_number_to_die (type, array_die);
16638 if (TREE_CODE (type) == VECTOR_TYPE)
16639 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
16641 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
16642 if (is_fortran ()
16643 && TREE_CODE (type) == ARRAY_TYPE
16644 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
16645 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
16646 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16648 #if 0
16649 /* We default the array ordering. SDB will probably do
16650 the right things even if DW_AT_ordering is not present. It's not even
16651 an issue until we start to get into multidimensional arrays anyway. If
16652 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
16653 then we'll have to put the DW_AT_ordering attribute back in. (But if
16654 and when we find out that we need to put these in, we will only do so
16655 for multidimensional arrays. */
16656 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
16657 #endif
16659 if (TREE_CODE (type) == VECTOR_TYPE)
16661 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
16662 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
16663 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
16664 add_bound_info (subrange_die, DW_AT_upper_bound,
16665 size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
16667 else
16668 add_subscript_info (array_die, type, collapse_nested_arrays);
16670 /* Add representation of the type of the elements of this array type and
16671 emit the corresponding DIE if we haven't done it already. */
16672 element_type = TREE_TYPE (type);
16673 if (collapse_nested_arrays)
16674 while (TREE_CODE (element_type) == ARRAY_TYPE)
16676 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
16677 break;
16678 element_type = TREE_TYPE (element_type);
16681 add_type_attribute (array_die, element_type, 0, 0, context_die);
16683 add_gnat_descriptive_type_attribute (array_die, type, context_die);
16684 if (TYPE_ARTIFICIAL (type))
16685 add_AT_flag (array_die, DW_AT_artificial, 1);
16687 if (get_AT (array_die, DW_AT_name))
16688 add_pubtype (type, array_die);
16691 static dw_loc_descr_ref
16692 descr_info_loc (tree val, tree base_decl)
16694 HOST_WIDE_INT size;
16695 dw_loc_descr_ref loc, loc2;
16696 enum dwarf_location_atom op;
16698 if (val == base_decl)
16699 return new_loc_descr (DW_OP_push_object_address, 0, 0);
16701 switch (TREE_CODE (val))
16703 CASE_CONVERT:
16704 return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16705 case VAR_DECL:
16706 return loc_descriptor_from_tree (val, 0);
16707 case INTEGER_CST:
16708 if (host_integerp (val, 0))
16709 return int_loc_descriptor (tree_low_cst (val, 0));
16710 break;
16711 case INDIRECT_REF:
16712 size = int_size_in_bytes (TREE_TYPE (val));
16713 if (size < 0)
16714 break;
16715 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16716 if (!loc)
16717 break;
16718 if (size == DWARF2_ADDR_SIZE)
16719 add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
16720 else
16721 add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
16722 return loc;
16723 case POINTER_PLUS_EXPR:
16724 case PLUS_EXPR:
16725 if (host_integerp (TREE_OPERAND (val, 1), 1)
16726 && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
16727 < 16384)
16729 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16730 if (!loc)
16731 break;
16732 loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
16734 else
16736 op = DW_OP_plus;
16737 do_binop:
16738 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16739 if (!loc)
16740 break;
16741 loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
16742 if (!loc2)
16743 break;
16744 add_loc_descr (&loc, loc2);
16745 add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
16747 return loc;
16748 case MINUS_EXPR:
16749 op = DW_OP_minus;
16750 goto do_binop;
16751 case MULT_EXPR:
16752 op = DW_OP_mul;
16753 goto do_binop;
16754 case EQ_EXPR:
16755 op = DW_OP_eq;
16756 goto do_binop;
16757 case NE_EXPR:
16758 op = DW_OP_ne;
16759 goto do_binop;
16760 default:
16761 break;
16763 return NULL;
16766 static void
16767 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
16768 tree val, tree base_decl)
16770 dw_loc_descr_ref loc;
16772 if (host_integerp (val, 0))
16774 add_AT_unsigned (die, attr, tree_low_cst (val, 0));
16775 return;
16778 loc = descr_info_loc (val, base_decl);
16779 if (!loc)
16780 return;
16782 add_AT_loc (die, attr, loc);
16785 /* This routine generates DIE for array with hidden descriptor, details
16786 are filled into *info by a langhook. */
16788 static void
16789 gen_descr_array_type_die (tree type, struct array_descr_info *info,
16790 dw_die_ref context_die)
16792 dw_die_ref scope_die = scope_die_for (type, context_die);
16793 dw_die_ref array_die;
16794 int dim;
16796 array_die = new_die (DW_TAG_array_type, scope_die, type);
16797 add_name_attribute (array_die, type_tag (type));
16798 equate_type_number_to_die (type, array_die);
16800 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
16801 if (is_fortran ()
16802 && info->ndimensions >= 2)
16803 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16805 if (info->data_location)
16806 add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
16807 info->base_decl);
16808 if (info->associated)
16809 add_descr_info_field (array_die, DW_AT_associated, info->associated,
16810 info->base_decl);
16811 if (info->allocated)
16812 add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
16813 info->base_decl);
16815 for (dim = 0; dim < info->ndimensions; dim++)
16817 dw_die_ref subrange_die
16818 = new_die (DW_TAG_subrange_type, array_die, NULL);
16820 if (info->dimen[dim].lower_bound)
16822 /* If it is the default value, omit it. */
16823 int dflt;
16825 if (host_integerp (info->dimen[dim].lower_bound, 0)
16826 && (dflt = lower_bound_default ()) != -1
16827 && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
16829 else
16830 add_descr_info_field (subrange_die, DW_AT_lower_bound,
16831 info->dimen[dim].lower_bound,
16832 info->base_decl);
16834 if (info->dimen[dim].upper_bound)
16835 add_descr_info_field (subrange_die, DW_AT_upper_bound,
16836 info->dimen[dim].upper_bound,
16837 info->base_decl);
16838 if (info->dimen[dim].stride)
16839 add_descr_info_field (subrange_die, DW_AT_byte_stride,
16840 info->dimen[dim].stride,
16841 info->base_decl);
16844 gen_type_die (info->element_type, context_die);
16845 add_type_attribute (array_die, info->element_type, 0, 0, context_die);
16847 if (get_AT (array_die, DW_AT_name))
16848 add_pubtype (type, array_die);
16851 #if 0
16852 static void
16853 gen_entry_point_die (tree decl, dw_die_ref context_die)
16855 tree origin = decl_ultimate_origin (decl);
16856 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
16858 if (origin != NULL)
16859 add_abstract_origin_attribute (decl_die, origin);
16860 else
16862 add_name_and_src_coords_attributes (decl_die, decl);
16863 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
16864 0, 0, context_die);
16867 if (DECL_ABSTRACT (decl))
16868 equate_decl_number_to_die (decl, decl_die);
16869 else
16870 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
16872 #endif
16874 /* Walk through the list of incomplete types again, trying once more to
16875 emit full debugging info for them. */
16877 static void
16878 retry_incomplete_types (void)
16880 int i;
16882 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
16883 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
16884 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
16887 /* Determine what tag to use for a record type. */
16889 static enum dwarf_tag
16890 record_type_tag (tree type)
16892 if (! lang_hooks.types.classify_record)
16893 return DW_TAG_structure_type;
16895 switch (lang_hooks.types.classify_record (type))
16897 case RECORD_IS_STRUCT:
16898 return DW_TAG_structure_type;
16900 case RECORD_IS_CLASS:
16901 return DW_TAG_class_type;
16903 case RECORD_IS_INTERFACE:
16904 if (dwarf_version >= 3 || !dwarf_strict)
16905 return DW_TAG_interface_type;
16906 return DW_TAG_structure_type;
16908 default:
16909 gcc_unreachable ();
16913 /* Generate a DIE to represent an enumeration type. Note that these DIEs
16914 include all of the information about the enumeration values also. Each
16915 enumerated type name/value is listed as a child of the enumerated type
16916 DIE. */
16918 static dw_die_ref
16919 gen_enumeration_type_die (tree type, dw_die_ref context_die)
16921 dw_die_ref type_die = lookup_type_die (type);
16923 if (type_die == NULL)
16925 type_die = new_die (DW_TAG_enumeration_type,
16926 scope_die_for (type, context_die), type);
16927 equate_type_number_to_die (type, type_die);
16928 add_name_attribute (type_die, type_tag (type));
16929 if (dwarf_version >= 4 || !dwarf_strict)
16931 if (ENUM_IS_SCOPED (type))
16932 add_AT_flag (type_die, DW_AT_enum_class, 1);
16933 if (ENUM_IS_OPAQUE (type))
16934 add_AT_flag (type_die, DW_AT_declaration, 1);
16937 else if (! TYPE_SIZE (type))
16938 return type_die;
16939 else
16940 remove_AT (type_die, DW_AT_declaration);
16942 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
16943 given enum type is incomplete, do not generate the DW_AT_byte_size
16944 attribute or the DW_AT_element_list attribute. */
16945 if (TYPE_SIZE (type))
16947 tree link;
16949 TREE_ASM_WRITTEN (type) = 1;
16950 add_byte_size_attribute (type_die, type);
16951 if (TYPE_STUB_DECL (type) != NULL_TREE)
16953 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
16954 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
16957 /* If the first reference to this type was as the return type of an
16958 inline function, then it may not have a parent. Fix this now. */
16959 if (type_die->die_parent == NULL)
16960 add_child_die (scope_die_for (type, context_die), type_die);
16962 for (link = TYPE_VALUES (type);
16963 link != NULL; link = TREE_CHAIN (link))
16965 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
16966 tree value = TREE_VALUE (link);
16968 add_name_attribute (enum_die,
16969 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
16971 if (TREE_CODE (value) == CONST_DECL)
16972 value = DECL_INITIAL (value);
16974 if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
16975 /* DWARF2 does not provide a way of indicating whether or
16976 not enumeration constants are signed or unsigned. GDB
16977 always assumes the values are signed, so we output all
16978 values as if they were signed. That means that
16979 enumeration constants with very large unsigned values
16980 will appear to have negative values in the debugger. */
16981 add_AT_int (enum_die, DW_AT_const_value,
16982 tree_low_cst (value, tree_int_cst_sgn (value) > 0));
16985 add_gnat_descriptive_type_attribute (type_die, type, context_die);
16986 if (TYPE_ARTIFICIAL (type))
16987 add_AT_flag (type_die, DW_AT_artificial, 1);
16989 else
16990 add_AT_flag (type_die, DW_AT_declaration, 1);
16992 add_pubtype (type, type_die);
16994 return type_die;
16997 /* Generate a DIE to represent either a real live formal parameter decl or to
16998 represent just the type of some formal parameter position in some function
16999 type.
17001 Note that this routine is a bit unusual because its argument may be a
17002 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17003 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17004 node. If it's the former then this function is being called to output a
17005 DIE to represent a formal parameter object (or some inlining thereof). If
17006 it's the latter, then this function is only being called to output a
17007 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17008 argument type of some subprogram type.
17009 If EMIT_NAME_P is true, name and source coordinate attributes
17010 are emitted. */
17012 static dw_die_ref
17013 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17014 dw_die_ref context_die)
17016 tree node_or_origin = node ? node : origin;
17017 tree ultimate_origin;
17018 dw_die_ref parm_die
17019 = new_die (DW_TAG_formal_parameter, context_die, node);
17021 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
17023 case tcc_declaration:
17024 ultimate_origin = decl_ultimate_origin (node_or_origin);
17025 if (node || ultimate_origin)
17026 origin = ultimate_origin;
17027 if (origin != NULL)
17028 add_abstract_origin_attribute (parm_die, origin);
17029 else if (emit_name_p)
17030 add_name_and_src_coords_attributes (parm_die, node);
17031 if (origin == NULL
17032 || (! DECL_ABSTRACT (node_or_origin)
17033 && variably_modified_type_p (TREE_TYPE (node_or_origin),
17034 decl_function_context
17035 (node_or_origin))))
17037 tree type = TREE_TYPE (node_or_origin);
17038 if (decl_by_reference_p (node_or_origin))
17039 add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
17040 context_die);
17041 else
17042 add_type_attribute (parm_die, type,
17043 TREE_READONLY (node_or_origin),
17044 TREE_THIS_VOLATILE (node_or_origin),
17045 context_die);
17047 if (origin == NULL && DECL_ARTIFICIAL (node))
17048 add_AT_flag (parm_die, DW_AT_artificial, 1);
17050 if (node && node != origin)
17051 equate_decl_number_to_die (node, parm_die);
17052 if (! DECL_ABSTRACT (node_or_origin))
17053 add_location_or_const_value_attribute (parm_die, node_or_origin,
17054 node == NULL, DW_AT_location);
17056 break;
17058 case tcc_type:
17059 /* We were called with some kind of a ..._TYPE node. */
17060 add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
17061 break;
17063 default:
17064 gcc_unreachable ();
17067 return parm_die;
17070 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
17071 children DW_TAG_formal_parameter DIEs representing the arguments of the
17072 parameter pack.
17074 PARM_PACK must be a function parameter pack.
17075 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
17076 must point to the subsequent arguments of the function PACK_ARG belongs to.
17077 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
17078 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
17079 following the last one for which a DIE was generated. */
17081 static dw_die_ref
17082 gen_formal_parameter_pack_die (tree parm_pack,
17083 tree pack_arg,
17084 dw_die_ref subr_die,
17085 tree *next_arg)
17087 tree arg;
17088 dw_die_ref parm_pack_die;
17090 gcc_assert (parm_pack
17091 && lang_hooks.function_parameter_pack_p (parm_pack)
17092 && subr_die);
17094 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
17095 add_src_coords_attributes (parm_pack_die, parm_pack);
17097 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
17099 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
17100 parm_pack))
17101 break;
17102 gen_formal_parameter_die (arg, NULL,
17103 false /* Don't emit name attribute. */,
17104 parm_pack_die);
17106 if (next_arg)
17107 *next_arg = arg;
17108 return parm_pack_die;
17111 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
17112 at the end of an (ANSI prototyped) formal parameters list. */
17114 static void
17115 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
17117 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
17120 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
17121 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
17122 parameters as specified in some function type specification (except for
17123 those which appear as part of a function *definition*). */
17125 static void
17126 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
17128 tree link;
17129 tree formal_type = NULL;
17130 tree first_parm_type;
17131 tree arg;
17133 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
17135 arg = DECL_ARGUMENTS (function_or_method_type);
17136 function_or_method_type = TREE_TYPE (function_or_method_type);
17138 else
17139 arg = NULL_TREE;
17141 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
17143 /* Make our first pass over the list of formal parameter types and output a
17144 DW_TAG_formal_parameter DIE for each one. */
17145 for (link = first_parm_type; link; )
17147 dw_die_ref parm_die;
17149 formal_type = TREE_VALUE (link);
17150 if (formal_type == void_type_node)
17151 break;
17153 /* Output a (nameless) DIE to represent the formal parameter itself. */
17154 parm_die = gen_formal_parameter_die (formal_type, NULL,
17155 true /* Emit name attribute. */,
17156 context_die);
17157 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
17158 && link == first_parm_type)
17160 add_AT_flag (parm_die, DW_AT_artificial, 1);
17161 if (dwarf_version >= 3 || !dwarf_strict)
17162 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
17164 else if (arg && DECL_ARTIFICIAL (arg))
17165 add_AT_flag (parm_die, DW_AT_artificial, 1);
17167 link = TREE_CHAIN (link);
17168 if (arg)
17169 arg = DECL_CHAIN (arg);
17172 /* If this function type has an ellipsis, add a
17173 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
17174 if (formal_type != void_type_node)
17175 gen_unspecified_parameters_die (function_or_method_type, context_die);
17177 /* Make our second (and final) pass over the list of formal parameter types
17178 and output DIEs to represent those types (as necessary). */
17179 for (link = TYPE_ARG_TYPES (function_or_method_type);
17180 link && TREE_VALUE (link);
17181 link = TREE_CHAIN (link))
17182 gen_type_die (TREE_VALUE (link), context_die);
17185 /* We want to generate the DIE for TYPE so that we can generate the
17186 die for MEMBER, which has been defined; we will need to refer back
17187 to the member declaration nested within TYPE. If we're trying to
17188 generate minimal debug info for TYPE, processing TYPE won't do the
17189 trick; we need to attach the member declaration by hand. */
17191 static void
17192 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
17194 gen_type_die (type, context_die);
17196 /* If we're trying to avoid duplicate debug info, we may not have
17197 emitted the member decl for this function. Emit it now. */
17198 if (TYPE_STUB_DECL (type)
17199 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
17200 && ! lookup_decl_die (member))
17202 dw_die_ref type_die;
17203 gcc_assert (!decl_ultimate_origin (member));
17205 push_decl_scope (type);
17206 type_die = lookup_type_die_strip_naming_typedef (type);
17207 if (TREE_CODE (member) == FUNCTION_DECL)
17208 gen_subprogram_die (member, type_die);
17209 else if (TREE_CODE (member) == FIELD_DECL)
17211 /* Ignore the nameless fields that are used to skip bits but handle
17212 C++ anonymous unions and structs. */
17213 if (DECL_NAME (member) != NULL_TREE
17214 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
17215 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
17217 gen_type_die (member_declared_type (member), type_die);
17218 gen_field_die (member, type_die);
17221 else
17222 gen_variable_die (member, NULL_TREE, type_die);
17224 pop_decl_scope ();
17228 /* Forward declare these functions, because they are mutually recursive
17229 with their set_block_* pairing functions. */
17230 static void set_decl_origin_self (tree);
17231 static void set_decl_abstract_flags (tree, int);
17233 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
17234 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
17235 that it points to the node itself, thus indicating that the node is its
17236 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
17237 the given node is NULL, recursively descend the decl/block tree which
17238 it is the root of, and for each other ..._DECL or BLOCK node contained
17239 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
17240 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
17241 values to point to themselves. */
17243 static void
17244 set_block_origin_self (tree stmt)
17246 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
17248 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
17251 tree local_decl;
17253 for (local_decl = BLOCK_VARS (stmt);
17254 local_decl != NULL_TREE;
17255 local_decl = DECL_CHAIN (local_decl))
17256 if (! DECL_EXTERNAL (local_decl))
17257 set_decl_origin_self (local_decl); /* Potential recursion. */
17261 tree subblock;
17263 for (subblock = BLOCK_SUBBLOCKS (stmt);
17264 subblock != NULL_TREE;
17265 subblock = BLOCK_CHAIN (subblock))
17266 set_block_origin_self (subblock); /* Recurse. */
17271 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
17272 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
17273 node to so that it points to the node itself, thus indicating that the
17274 node represents its own (abstract) origin. Additionally, if the
17275 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
17276 the decl/block tree of which the given node is the root of, and for
17277 each other ..._DECL or BLOCK node contained therein whose
17278 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
17279 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
17280 point to themselves. */
17282 static void
17283 set_decl_origin_self (tree decl)
17285 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
17287 DECL_ABSTRACT_ORIGIN (decl) = decl;
17288 if (TREE_CODE (decl) == FUNCTION_DECL)
17290 tree arg;
17292 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17293 DECL_ABSTRACT_ORIGIN (arg) = arg;
17294 if (DECL_INITIAL (decl) != NULL_TREE
17295 && DECL_INITIAL (decl) != error_mark_node)
17296 set_block_origin_self (DECL_INITIAL (decl));
17301 /* Given a pointer to some BLOCK node, and a boolean value to set the
17302 "abstract" flags to, set that value into the BLOCK_ABSTRACT flag for
17303 the given block, and for all local decls and all local sub-blocks
17304 (recursively) which are contained therein. */
17306 static void
17307 set_block_abstract_flags (tree stmt, int setting)
17309 tree local_decl;
17310 tree subblock;
17311 unsigned int i;
17313 BLOCK_ABSTRACT (stmt) = setting;
17315 for (local_decl = BLOCK_VARS (stmt);
17316 local_decl != NULL_TREE;
17317 local_decl = DECL_CHAIN (local_decl))
17318 if (! DECL_EXTERNAL (local_decl))
17319 set_decl_abstract_flags (local_decl, setting);
17321 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
17323 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
17324 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
17325 || TREE_CODE (local_decl) == PARM_DECL)
17326 set_decl_abstract_flags (local_decl, setting);
17329 for (subblock = BLOCK_SUBBLOCKS (stmt);
17330 subblock != NULL_TREE;
17331 subblock = BLOCK_CHAIN (subblock))
17332 set_block_abstract_flags (subblock, setting);
17335 /* Given a pointer to some ..._DECL node, and a boolean value to set the
17336 "abstract" flags to, set that value into the DECL_ABSTRACT flag for the
17337 given decl, and (in the case where the decl is a FUNCTION_DECL) also
17338 set the abstract flags for all of the parameters, local vars, local
17339 blocks and sub-blocks (recursively) to the same setting. */
17341 static void
17342 set_decl_abstract_flags (tree decl, int setting)
17344 DECL_ABSTRACT (decl) = setting;
17345 if (TREE_CODE (decl) == FUNCTION_DECL)
17347 tree arg;
17349 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17350 DECL_ABSTRACT (arg) = setting;
17351 if (DECL_INITIAL (decl) != NULL_TREE
17352 && DECL_INITIAL (decl) != error_mark_node)
17353 set_block_abstract_flags (DECL_INITIAL (decl), setting);
17357 /* Generate the DWARF2 info for the "abstract" instance of a function which we
17358 may later generate inlined and/or out-of-line instances of. */
17360 static void
17361 dwarf2out_abstract_function (tree decl)
17363 dw_die_ref old_die;
17364 tree save_fn;
17365 tree context;
17366 int was_abstract;
17367 htab_t old_decl_loc_table;
17368 htab_t old_cached_dw_loc_list_table;
17369 int old_call_site_count, old_tail_call_site_count;
17370 struct call_arg_loc_node *old_call_arg_locations;
17372 /* Make sure we have the actual abstract inline, not a clone. */
17373 decl = DECL_ORIGIN (decl);
17375 old_die = lookup_decl_die (decl);
17376 if (old_die && get_AT (old_die, DW_AT_inline))
17377 /* We've already generated the abstract instance. */
17378 return;
17380 /* We can be called while recursively when seeing block defining inlined subroutine
17381 DIE. Be sure to not clobber the outer location table nor use it or we would
17382 get locations in abstract instantces. */
17383 old_decl_loc_table = decl_loc_table;
17384 decl_loc_table = NULL;
17385 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
17386 cached_dw_loc_list_table = NULL;
17387 old_call_arg_locations = call_arg_locations;
17388 call_arg_locations = NULL;
17389 old_call_site_count = call_site_count;
17390 call_site_count = -1;
17391 old_tail_call_site_count = tail_call_site_count;
17392 tail_call_site_count = -1;
17394 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
17395 we don't get confused by DECL_ABSTRACT. */
17396 if (debug_info_level > DINFO_LEVEL_TERSE)
17398 context = decl_class_context (decl);
17399 if (context)
17400 gen_type_die_for_member
17401 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
17404 /* Pretend we've just finished compiling this function. */
17405 save_fn = current_function_decl;
17406 current_function_decl = decl;
17408 was_abstract = DECL_ABSTRACT (decl);
17409 set_decl_abstract_flags (decl, 1);
17410 dwarf2out_decl (decl);
17411 if (! was_abstract)
17412 set_decl_abstract_flags (decl, 0);
17414 current_function_decl = save_fn;
17415 decl_loc_table = old_decl_loc_table;
17416 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
17417 call_arg_locations = old_call_arg_locations;
17418 call_site_count = old_call_site_count;
17419 tail_call_site_count = old_tail_call_site_count;
17422 /* Helper function of premark_used_types() which gets called through
17423 htab_traverse.
17425 Marks the DIE of a given type in *SLOT as perennial, so it never gets
17426 marked as unused by prune_unused_types. */
17428 static int
17429 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
17431 tree type;
17432 dw_die_ref die;
17434 type = (tree) *slot;
17435 die = lookup_type_die (type);
17436 if (die != NULL)
17437 die->die_perennial_p = 1;
17438 return 1;
17441 /* Helper function of premark_types_used_by_global_vars which gets called
17442 through htab_traverse.
17444 Marks the DIE of a given type in *SLOT as perennial, so it never gets
17445 marked as unused by prune_unused_types. The DIE of the type is marked
17446 only if the global variable using the type will actually be emitted. */
17448 static int
17449 premark_types_used_by_global_vars_helper (void **slot,
17450 void *data ATTRIBUTE_UNUSED)
17452 struct types_used_by_vars_entry *entry;
17453 dw_die_ref die;
17455 entry = (struct types_used_by_vars_entry *) *slot;
17456 gcc_assert (entry->type != NULL
17457 && entry->var_decl != NULL);
17458 die = lookup_type_die (entry->type);
17459 if (die)
17461 /* Ask cgraph if the global variable really is to be emitted.
17462 If yes, then we'll keep the DIE of ENTRY->TYPE. */
17463 struct varpool_node *node = varpool_get_node (entry->var_decl);
17464 if (node && node->analyzed)
17466 die->die_perennial_p = 1;
17467 /* Keep the parent DIEs as well. */
17468 while ((die = die->die_parent) && die->die_perennial_p == 0)
17469 die->die_perennial_p = 1;
17472 return 1;
17475 /* Mark all members of used_types_hash as perennial. */
17477 static void
17478 premark_used_types (struct function *fun)
17480 if (fun && fun->used_types_hash)
17481 htab_traverse (fun->used_types_hash, premark_used_types_helper, NULL);
17484 /* Mark all members of types_used_by_vars_entry as perennial. */
17486 static void
17487 premark_types_used_by_global_vars (void)
17489 if (types_used_by_vars_hash)
17490 htab_traverse (types_used_by_vars_hash,
17491 premark_types_used_by_global_vars_helper, NULL);
17494 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
17495 for CA_LOC call arg loc node. */
17497 static dw_die_ref
17498 gen_call_site_die (tree decl, dw_die_ref subr_die,
17499 struct call_arg_loc_node *ca_loc)
17501 dw_die_ref stmt_die = NULL, die;
17502 tree block = ca_loc->block;
17504 while (block
17505 && block != DECL_INITIAL (decl)
17506 && TREE_CODE (block) == BLOCK)
17508 if (block_map.length () > BLOCK_NUMBER (block))
17509 stmt_die = block_map[BLOCK_NUMBER (block)];
17510 if (stmt_die)
17511 break;
17512 block = BLOCK_SUPERCONTEXT (block);
17514 if (stmt_die == NULL)
17515 stmt_die = subr_die;
17516 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
17517 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
17518 if (ca_loc->tail_call_p)
17519 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
17520 if (ca_loc->symbol_ref)
17522 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
17523 if (tdie)
17524 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
17525 else
17526 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
17528 return die;
17531 /* Generate a DIE to represent a declared function (either file-scope or
17532 block-local). */
17534 static void
17535 gen_subprogram_die (tree decl, dw_die_ref context_die)
17537 tree origin = decl_ultimate_origin (decl);
17538 dw_die_ref subr_die;
17539 tree outer_scope;
17540 dw_die_ref old_die = lookup_decl_die (decl);
17541 int declaration = (current_function_decl != decl
17542 || class_or_namespace_scope_p (context_die));
17544 premark_used_types (DECL_STRUCT_FUNCTION (decl));
17546 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
17547 started to generate the abstract instance of an inline, decided to output
17548 its containing class, and proceeded to emit the declaration of the inline
17549 from the member list for the class. If so, DECLARATION takes priority;
17550 we'll get back to the abstract instance when done with the class. */
17552 /* The class-scope declaration DIE must be the primary DIE. */
17553 if (origin && declaration && class_or_namespace_scope_p (context_die))
17555 origin = NULL;
17556 gcc_assert (!old_die);
17559 /* Now that the C++ front end lazily declares artificial member fns, we
17560 might need to retrofit the declaration into its class. */
17561 if (!declaration && !origin && !old_die
17562 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
17563 && !class_or_namespace_scope_p (context_die)
17564 && debug_info_level > DINFO_LEVEL_TERSE)
17565 old_die = force_decl_die (decl);
17567 if (origin != NULL)
17569 gcc_assert (!declaration || local_scope_p (context_die));
17571 /* Fixup die_parent for the abstract instance of a nested
17572 inline function. */
17573 if (old_die && old_die->die_parent == NULL)
17574 add_child_die (context_die, old_die);
17576 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17577 add_abstract_origin_attribute (subr_die, origin);
17578 /* This is where the actual code for a cloned function is.
17579 Let's emit linkage name attribute for it. This helps
17580 debuggers to e.g, set breakpoints into
17581 constructors/destructors when the user asks "break
17582 K::K". */
17583 add_linkage_name (subr_die, decl);
17585 else if (old_die)
17587 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17588 struct dwarf_file_data * file_index = lookup_filename (s.file);
17590 if (!get_AT_flag (old_die, DW_AT_declaration)
17591 /* We can have a normal definition following an inline one in the
17592 case of redefinition of GNU C extern inlines.
17593 It seems reasonable to use AT_specification in this case. */
17594 && !get_AT (old_die, DW_AT_inline))
17596 /* Detect and ignore this case, where we are trying to output
17597 something we have already output. */
17598 return;
17601 /* If the definition comes from the same place as the declaration,
17602 maybe use the old DIE. We always want the DIE for this function
17603 that has the *_pc attributes to be under comp_unit_die so the
17604 debugger can find it. We also need to do this for abstract
17605 instances of inlines, since the spec requires the out-of-line copy
17606 to have the same parent. For local class methods, this doesn't
17607 apply; we just use the old DIE. */
17608 if ((is_cu_die (old_die->die_parent) || context_die == NULL)
17609 && (DECL_ARTIFICIAL (decl)
17610 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
17611 && (get_AT_unsigned (old_die, DW_AT_decl_line)
17612 == (unsigned) s.line))))
17614 subr_die = old_die;
17616 /* Clear out the declaration attribute and the formal parameters.
17617 Do not remove all children, because it is possible that this
17618 declaration die was forced using force_decl_die(). In such
17619 cases die that forced declaration die (e.g. TAG_imported_module)
17620 is one of the children that we do not want to remove. */
17621 remove_AT (subr_die, DW_AT_declaration);
17622 remove_AT (subr_die, DW_AT_object_pointer);
17623 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
17625 else
17627 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17628 add_AT_specification (subr_die, old_die);
17629 add_pubname (decl, subr_die);
17630 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
17631 add_AT_file (subr_die, DW_AT_decl_file, file_index);
17632 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
17633 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
17636 else
17638 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17640 if (TREE_PUBLIC (decl))
17641 add_AT_flag (subr_die, DW_AT_external, 1);
17643 add_name_and_src_coords_attributes (subr_die, decl);
17644 add_pubname (decl, subr_die);
17645 if (debug_info_level > DINFO_LEVEL_TERSE)
17647 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
17648 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
17649 0, 0, context_die);
17652 add_pure_or_virtual_attribute (subr_die, decl);
17653 if (DECL_ARTIFICIAL (decl))
17654 add_AT_flag (subr_die, DW_AT_artificial, 1);
17656 add_accessibility_attribute (subr_die, decl);
17659 if (declaration)
17661 if (!old_die || !get_AT (old_die, DW_AT_inline))
17663 add_AT_flag (subr_die, DW_AT_declaration, 1);
17665 /* If this is an explicit function declaration then generate
17666 a DW_AT_explicit attribute. */
17667 if (lang_hooks.decls.function_decl_explicit_p (decl)
17668 && (dwarf_version >= 3 || !dwarf_strict))
17669 add_AT_flag (subr_die, DW_AT_explicit, 1);
17671 /* The first time we see a member function, it is in the context of
17672 the class to which it belongs. We make sure of this by emitting
17673 the class first. The next time is the definition, which is
17674 handled above. The two may come from the same source text.
17676 Note that force_decl_die() forces function declaration die. It is
17677 later reused to represent definition. */
17678 equate_decl_number_to_die (decl, subr_die);
17681 else if (DECL_ABSTRACT (decl))
17683 if (DECL_DECLARED_INLINE_P (decl))
17685 if (cgraph_function_possibly_inlined_p (decl))
17686 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
17687 else
17688 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
17690 else
17692 if (cgraph_function_possibly_inlined_p (decl))
17693 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
17694 else
17695 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
17698 if (DECL_DECLARED_INLINE_P (decl)
17699 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
17700 add_AT_flag (subr_die, DW_AT_artificial, 1);
17702 equate_decl_number_to_die (decl, subr_die);
17704 else if (!DECL_EXTERNAL (decl))
17706 HOST_WIDE_INT cfa_fb_offset;
17707 struct function *fun = DECL_STRUCT_FUNCTION (decl);
17709 if (!old_die || !get_AT (old_die, DW_AT_inline))
17710 equate_decl_number_to_die (decl, subr_die);
17712 gcc_checking_assert (fun);
17713 if (!flag_reorder_blocks_and_partition)
17715 dw_fde_ref fde = fun->fde;
17716 if (fde->dw_fde_begin)
17718 /* We have already generated the labels. */
17719 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
17720 fde->dw_fde_end, false);
17722 else
17724 /* Create start/end labels and add the range. */
17725 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
17726 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
17727 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
17728 current_function_funcdef_no);
17729 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
17730 current_function_funcdef_no);
17731 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
17732 false);
17735 #if VMS_DEBUGGING_INFO
17736 /* HP OpenVMS Industry Standard 64: DWARF Extensions
17737 Section 2.3 Prologue and Epilogue Attributes:
17738 When a breakpoint is set on entry to a function, it is generally
17739 desirable for execution to be suspended, not on the very first
17740 instruction of the function, but rather at a point after the
17741 function's frame has been set up, after any language defined local
17742 declaration processing has been completed, and before execution of
17743 the first statement of the function begins. Debuggers generally
17744 cannot properly determine where this point is. Similarly for a
17745 breakpoint set on exit from a function. The prologue and epilogue
17746 attributes allow a compiler to communicate the location(s) to use. */
17749 if (fde->dw_fde_vms_end_prologue)
17750 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
17751 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
17753 if (fde->dw_fde_vms_begin_epilogue)
17754 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
17755 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
17757 #endif
17760 else
17762 /* Generate pubnames entries for the split function code ranges. */
17763 dw_fde_ref fde = fun->fde;
17765 if (fde->dw_fde_second_begin)
17767 if (dwarf_version >= 3 || !dwarf_strict)
17769 /* We should use ranges for non-contiguous code section
17770 addresses. Use the actual code range for the initial
17771 section, since the HOT/COLD labels might precede an
17772 alignment offset. */
17773 bool range_list_added = false;
17774 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
17775 fde->dw_fde_end, &range_list_added,
17776 false);
17777 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
17778 fde->dw_fde_second_end,
17779 &range_list_added, false);
17780 if (range_list_added)
17781 add_ranges (NULL);
17783 else
17785 /* There is no real support in DW2 for this .. so we make
17786 a work-around. First, emit the pub name for the segment
17787 containing the function label. Then make and emit a
17788 simplified subprogram DIE for the second segment with the
17789 name pre-fixed by __hot/cold_sect_of_. We use the same
17790 linkage name for the second die so that gdb will find both
17791 sections when given "b foo". */
17792 const char *name = NULL;
17793 tree decl_name = DECL_NAME (decl);
17794 dw_die_ref seg_die;
17796 /* Do the 'primary' section. */
17797 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
17798 fde->dw_fde_end, false);
17800 /* Build a minimal DIE for the secondary section. */
17801 seg_die = new_die (DW_TAG_subprogram,
17802 subr_die->die_parent, decl);
17804 if (TREE_PUBLIC (decl))
17805 add_AT_flag (seg_die, DW_AT_external, 1);
17807 if (decl_name != NULL
17808 && IDENTIFIER_POINTER (decl_name) != NULL)
17810 name = dwarf2_name (decl, 1);
17811 if (! DECL_ARTIFICIAL (decl))
17812 add_src_coords_attributes (seg_die, decl);
17814 add_linkage_name (seg_die, decl);
17816 gcc_assert (name != NULL);
17817 add_pure_or_virtual_attribute (seg_die, decl);
17818 if (DECL_ARTIFICIAL (decl))
17819 add_AT_flag (seg_die, DW_AT_artificial, 1);
17821 name = concat ("__second_sect_of_", name, NULL);
17822 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
17823 fde->dw_fde_second_end, false);
17824 add_name_attribute (seg_die, name);
17825 if (want_pubnames ())
17826 add_pubname_string (name, seg_die);
17829 else
17830 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
17831 false);
17834 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
17836 /* We define the "frame base" as the function's CFA. This is more
17837 convenient for several reasons: (1) It's stable across the prologue
17838 and epilogue, which makes it better than just a frame pointer,
17839 (2) With dwarf3, there exists a one-byte encoding that allows us
17840 to reference the .debug_frame data by proxy, but failing that,
17841 (3) We can at least reuse the code inspection and interpretation
17842 code that determines the CFA position at various points in the
17843 function. */
17844 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
17846 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
17847 add_AT_loc (subr_die, DW_AT_frame_base, op);
17849 else
17851 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
17852 if (list->dw_loc_next)
17853 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
17854 else
17855 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
17858 /* Compute a displacement from the "steady-state frame pointer" to
17859 the CFA. The former is what all stack slots and argument slots
17860 will reference in the rtl; the later is what we've told the
17861 debugger about. We'll need to adjust all frame_base references
17862 by this displacement. */
17863 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
17865 if (fun->static_chain_decl)
17866 add_AT_location_description (subr_die, DW_AT_static_link,
17867 loc_list_from_tree (fun->static_chain_decl, 2));
17870 /* Generate child dies for template paramaters. */
17871 if (debug_info_level > DINFO_LEVEL_TERSE)
17872 gen_generic_params_dies (decl);
17874 /* Now output descriptions of the arguments for this function. This gets
17875 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
17876 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
17877 `...' at the end of the formal parameter list. In order to find out if
17878 there was a trailing ellipsis or not, we must instead look at the type
17879 associated with the FUNCTION_DECL. This will be a node of type
17880 FUNCTION_TYPE. If the chain of type nodes hanging off of this
17881 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
17882 an ellipsis at the end. */
17884 /* In the case where we are describing a mere function declaration, all we
17885 need to do here (and all we *can* do here) is to describe the *types* of
17886 its formal parameters. */
17887 if (debug_info_level <= DINFO_LEVEL_TERSE)
17889 else if (declaration)
17890 gen_formal_types_die (decl, subr_die);
17891 else
17893 /* Generate DIEs to represent all known formal parameters. */
17894 tree parm = DECL_ARGUMENTS (decl);
17895 tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
17896 tree generic_decl_parm = generic_decl
17897 ? DECL_ARGUMENTS (generic_decl)
17898 : NULL;
17900 /* Now we want to walk the list of parameters of the function and
17901 emit their relevant DIEs.
17903 We consider the case of DECL being an instance of a generic function
17904 as well as it being a normal function.
17906 If DECL is an instance of a generic function we walk the
17907 parameters of the generic function declaration _and_ the parameters of
17908 DECL itself. This is useful because we want to emit specific DIEs for
17909 function parameter packs and those are declared as part of the
17910 generic function declaration. In that particular case,
17911 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
17912 That DIE has children DIEs representing the set of arguments
17913 of the pack. Note that the set of pack arguments can be empty.
17914 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
17915 children DIE.
17917 Otherwise, we just consider the parameters of DECL. */
17918 while (generic_decl_parm || parm)
17920 if (generic_decl_parm
17921 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
17922 gen_formal_parameter_pack_die (generic_decl_parm,
17923 parm, subr_die,
17924 &parm);
17925 else if (parm)
17927 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
17929 if (parm == DECL_ARGUMENTS (decl)
17930 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
17931 && parm_die
17932 && (dwarf_version >= 3 || !dwarf_strict))
17933 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
17935 parm = DECL_CHAIN (parm);
17938 if (generic_decl_parm)
17939 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
17942 /* Decide whether we need an unspecified_parameters DIE at the end.
17943 There are 2 more cases to do this for: 1) the ansi ... declaration -
17944 this is detectable when the end of the arg list is not a
17945 void_type_node 2) an unprototyped function declaration (not a
17946 definition). This just means that we have no info about the
17947 parameters at all. */
17948 if (prototype_p (TREE_TYPE (decl)))
17950 /* This is the prototyped case, check for.... */
17951 if (stdarg_p (TREE_TYPE (decl)))
17952 gen_unspecified_parameters_die (decl, subr_die);
17954 else if (DECL_INITIAL (decl) == NULL_TREE)
17955 gen_unspecified_parameters_die (decl, subr_die);
17958 /* Output Dwarf info for all of the stuff within the body of the function
17959 (if it has one - it may be just a declaration). */
17960 outer_scope = DECL_INITIAL (decl);
17962 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
17963 a function. This BLOCK actually represents the outermost binding contour
17964 for the function, i.e. the contour in which the function's formal
17965 parameters and labels get declared. Curiously, it appears that the front
17966 end doesn't actually put the PARM_DECL nodes for the current function onto
17967 the BLOCK_VARS list for this outer scope, but are strung off of the
17968 DECL_ARGUMENTS list for the function instead.
17970 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
17971 the LABEL_DECL nodes for the function however, and we output DWARF info
17972 for those in decls_for_scope. Just within the `outer_scope' there will be
17973 a BLOCK node representing the function's outermost pair of curly braces,
17974 and any blocks used for the base and member initializers of a C++
17975 constructor function. */
17976 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
17978 int call_site_note_count = 0;
17979 int tail_call_site_note_count = 0;
17981 /* Emit a DW_TAG_variable DIE for a named return value. */
17982 if (DECL_NAME (DECL_RESULT (decl)))
17983 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
17985 current_function_has_inlines = 0;
17986 decls_for_scope (outer_scope, subr_die, 0);
17988 if (call_arg_locations && !dwarf_strict)
17990 struct call_arg_loc_node *ca_loc;
17991 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
17993 dw_die_ref die = NULL;
17994 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
17995 rtx arg, next_arg;
17997 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
17998 arg; arg = next_arg)
18000 dw_loc_descr_ref reg, val;
18001 enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
18002 dw_die_ref cdie, tdie = NULL;
18004 next_arg = XEXP (arg, 1);
18005 if (REG_P (XEXP (XEXP (arg, 0), 0))
18006 && next_arg
18007 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
18008 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
18009 && REGNO (XEXP (XEXP (arg, 0), 0))
18010 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
18011 next_arg = XEXP (next_arg, 1);
18012 if (mode == VOIDmode)
18014 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
18015 if (mode == VOIDmode)
18016 mode = GET_MODE (XEXP (arg, 0));
18018 if (mode == VOIDmode || mode == BLKmode)
18019 continue;
18020 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
18022 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18023 tloc = XEXP (XEXP (arg, 0), 1);
18024 continue;
18026 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
18027 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
18029 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18030 tlocc = XEXP (XEXP (arg, 0), 1);
18031 continue;
18033 reg = NULL;
18034 if (REG_P (XEXP (XEXP (arg, 0), 0)))
18035 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
18036 VAR_INIT_STATUS_INITIALIZED);
18037 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
18039 rtx mem = XEXP (XEXP (arg, 0), 0);
18040 reg = mem_loc_descriptor (XEXP (mem, 0),
18041 get_address_mode (mem),
18042 GET_MODE (mem),
18043 VAR_INIT_STATUS_INITIALIZED);
18045 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
18046 == DEBUG_PARAMETER_REF)
18048 tree tdecl
18049 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
18050 tdie = lookup_decl_die (tdecl);
18051 if (tdie == NULL)
18052 continue;
18054 else
18055 continue;
18056 if (reg == NULL
18057 && GET_CODE (XEXP (XEXP (arg, 0), 0))
18058 != DEBUG_PARAMETER_REF)
18059 continue;
18060 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
18061 VOIDmode,
18062 VAR_INIT_STATUS_INITIALIZED);
18063 if (val == NULL)
18064 continue;
18065 if (die == NULL)
18066 die = gen_call_site_die (decl, subr_die, ca_loc);
18067 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
18068 NULL_TREE);
18069 if (reg != NULL)
18070 add_AT_loc (cdie, DW_AT_location, reg);
18071 else if (tdie != NULL)
18072 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
18073 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
18074 if (next_arg != XEXP (arg, 1))
18076 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
18077 if (mode == VOIDmode)
18078 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
18079 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
18080 0), 1),
18081 mode, VOIDmode,
18082 VAR_INIT_STATUS_INITIALIZED);
18083 if (val != NULL)
18084 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
18087 if (die == NULL
18088 && (ca_loc->symbol_ref || tloc))
18089 die = gen_call_site_die (decl, subr_die, ca_loc);
18090 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
18092 dw_loc_descr_ref tval = NULL;
18094 if (tloc != NULL_RTX)
18095 tval = mem_loc_descriptor (tloc,
18096 GET_MODE (tloc) == VOIDmode
18097 ? Pmode : GET_MODE (tloc),
18098 VOIDmode,
18099 VAR_INIT_STATUS_INITIALIZED);
18100 if (tval)
18101 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
18102 else if (tlocc != NULL_RTX)
18104 tval = mem_loc_descriptor (tlocc,
18105 GET_MODE (tlocc) == VOIDmode
18106 ? Pmode : GET_MODE (tlocc),
18107 VOIDmode,
18108 VAR_INIT_STATUS_INITIALIZED);
18109 if (tval)
18110 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
18111 tval);
18114 if (die != NULL)
18116 call_site_note_count++;
18117 if (ca_loc->tail_call_p)
18118 tail_call_site_note_count++;
18122 call_arg_locations = NULL;
18123 call_arg_loc_last = NULL;
18124 if (tail_call_site_count >= 0
18125 && tail_call_site_count == tail_call_site_note_count
18126 && !dwarf_strict)
18128 if (call_site_count >= 0
18129 && call_site_count == call_site_note_count)
18130 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
18131 else
18132 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
18134 call_site_count = -1;
18135 tail_call_site_count = -1;
18137 /* Add the calling convention attribute if requested. */
18138 add_calling_convention_attribute (subr_die, decl);
18142 /* Returns a hash value for X (which really is a die_struct). */
18144 static hashval_t
18145 common_block_die_table_hash (const void *x)
18147 const_dw_die_ref d = (const_dw_die_ref) x;
18148 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
18151 /* Return nonzero if decl_id and die_parent of die_struct X is the same
18152 as decl_id and die_parent of die_struct Y. */
18154 static int
18155 common_block_die_table_eq (const void *x, const void *y)
18157 const_dw_die_ref d = (const_dw_die_ref) x;
18158 const_dw_die_ref e = (const_dw_die_ref) y;
18159 return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
18162 /* Generate a DIE to represent a declared data object.
18163 Either DECL or ORIGIN must be non-null. */
18165 static void
18166 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
18168 HOST_WIDE_INT off = 0;
18169 tree com_decl;
18170 tree decl_or_origin = decl ? decl : origin;
18171 tree ultimate_origin;
18172 dw_die_ref var_die;
18173 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
18174 dw_die_ref origin_die;
18175 bool declaration = (DECL_EXTERNAL (decl_or_origin)
18176 || class_or_namespace_scope_p (context_die));
18177 bool specialization_p = false;
18179 ultimate_origin = decl_ultimate_origin (decl_or_origin);
18180 if (decl || ultimate_origin)
18181 origin = ultimate_origin;
18182 com_decl = fortran_common (decl_or_origin, &off);
18184 /* Symbol in common gets emitted as a child of the common block, in the form
18185 of a data member. */
18186 if (com_decl)
18188 dw_die_ref com_die;
18189 dw_loc_list_ref loc;
18190 die_node com_die_arg;
18192 var_die = lookup_decl_die (decl_or_origin);
18193 if (var_die)
18195 if (get_AT (var_die, DW_AT_location) == NULL)
18197 loc = loc_list_from_tree (com_decl, off ? 1 : 2);
18198 if (loc)
18200 if (off)
18202 /* Optimize the common case. */
18203 if (single_element_loc_list_p (loc)
18204 && loc->expr->dw_loc_opc == DW_OP_addr
18205 && loc->expr->dw_loc_next == NULL
18206 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
18207 == SYMBOL_REF)
18209 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18210 loc->expr->dw_loc_oprnd1.v.val_addr
18211 = plus_constant (GET_MODE (x), x , off);
18213 else
18214 loc_list_plus_const (loc, off);
18216 add_AT_location_description (var_die, DW_AT_location, loc);
18217 remove_AT (var_die, DW_AT_declaration);
18220 return;
18223 if (common_block_die_table == NULL)
18224 common_block_die_table
18225 = htab_create_ggc (10, common_block_die_table_hash,
18226 common_block_die_table_eq, NULL);
18228 com_die_arg.decl_id = DECL_UID (com_decl);
18229 com_die_arg.die_parent = context_die;
18230 com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
18231 loc = loc_list_from_tree (com_decl, 2);
18232 if (com_die == NULL)
18234 const char *cnam
18235 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
18236 void **slot;
18238 com_die = new_die (DW_TAG_common_block, context_die, decl);
18239 add_name_and_src_coords_attributes (com_die, com_decl);
18240 if (loc)
18242 add_AT_location_description (com_die, DW_AT_location, loc);
18243 /* Avoid sharing the same loc descriptor between
18244 DW_TAG_common_block and DW_TAG_variable. */
18245 loc = loc_list_from_tree (com_decl, 2);
18247 else if (DECL_EXTERNAL (decl))
18248 add_AT_flag (com_die, DW_AT_declaration, 1);
18249 if (want_pubnames ())
18250 add_pubname_string (cnam, com_die); /* ??? needed? */
18251 com_die->decl_id = DECL_UID (com_decl);
18252 slot = htab_find_slot (common_block_die_table, com_die, INSERT);
18253 *slot = (void *) com_die;
18255 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
18257 add_AT_location_description (com_die, DW_AT_location, loc);
18258 loc = loc_list_from_tree (com_decl, 2);
18259 remove_AT (com_die, DW_AT_declaration);
18261 var_die = new_die (DW_TAG_variable, com_die, decl);
18262 add_name_and_src_coords_attributes (var_die, decl);
18263 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
18264 TREE_THIS_VOLATILE (decl), context_die);
18265 add_AT_flag (var_die, DW_AT_external, 1);
18266 if (loc)
18268 if (off)
18270 /* Optimize the common case. */
18271 if (single_element_loc_list_p (loc)
18272 && loc->expr->dw_loc_opc == DW_OP_addr
18273 && loc->expr->dw_loc_next == NULL
18274 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
18276 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18277 loc->expr->dw_loc_oprnd1.v.val_addr
18278 = plus_constant (GET_MODE (x), x, off);
18280 else
18281 loc_list_plus_const (loc, off);
18283 add_AT_location_description (var_die, DW_AT_location, loc);
18285 else if (DECL_EXTERNAL (decl))
18286 add_AT_flag (var_die, DW_AT_declaration, 1);
18287 equate_decl_number_to_die (decl, var_die);
18288 return;
18291 /* If the compiler emitted a definition for the DECL declaration
18292 and if we already emitted a DIE for it, don't emit a second
18293 DIE for it again. Allow re-declarations of DECLs that are
18294 inside functions, though. */
18295 if (old_die && declaration && !local_scope_p (context_die))
18296 return;
18298 /* For static data members, the declaration in the class is supposed
18299 to have DW_TAG_member tag; the specification should still be
18300 DW_TAG_variable referencing the DW_TAG_member DIE. */
18301 if (declaration && class_scope_p (context_die))
18302 var_die = new_die (DW_TAG_member, context_die, decl);
18303 else
18304 var_die = new_die (DW_TAG_variable, context_die, decl);
18306 origin_die = NULL;
18307 if (origin != NULL)
18308 origin_die = add_abstract_origin_attribute (var_die, origin);
18310 /* Loop unrolling can create multiple blocks that refer to the same
18311 static variable, so we must test for the DW_AT_declaration flag.
18313 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
18314 copy decls and set the DECL_ABSTRACT flag on them instead of
18315 sharing them.
18317 ??? Duplicated blocks have been rewritten to use .debug_ranges.
18319 ??? The declare_in_namespace support causes us to get two DIEs for one
18320 variable, both of which are declarations. We want to avoid considering
18321 one to be a specification, so we must test that this DIE is not a
18322 declaration. */
18323 else if (old_die && TREE_STATIC (decl) && ! declaration
18324 && get_AT_flag (old_die, DW_AT_declaration) == 1)
18326 /* This is a definition of a C++ class level static. */
18327 add_AT_specification (var_die, old_die);
18328 specialization_p = true;
18329 if (DECL_NAME (decl))
18331 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18332 struct dwarf_file_data * file_index = lookup_filename (s.file);
18334 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18335 add_AT_file (var_die, DW_AT_decl_file, file_index);
18337 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18338 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
18340 if (old_die->die_tag == DW_TAG_member)
18341 add_linkage_name (var_die, decl);
18344 else
18345 add_name_and_src_coords_attributes (var_die, decl);
18347 if ((origin == NULL && !specialization_p)
18348 || (origin != NULL
18349 && !DECL_ABSTRACT (decl_or_origin)
18350 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
18351 decl_function_context
18352 (decl_or_origin))))
18354 tree type = TREE_TYPE (decl_or_origin);
18356 if (decl_by_reference_p (decl_or_origin))
18357 add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
18358 else
18359 add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
18360 TREE_THIS_VOLATILE (decl_or_origin), context_die);
18363 if (origin == NULL && !specialization_p)
18365 if (TREE_PUBLIC (decl))
18366 add_AT_flag (var_die, DW_AT_external, 1);
18368 if (DECL_ARTIFICIAL (decl))
18369 add_AT_flag (var_die, DW_AT_artificial, 1);
18371 add_accessibility_attribute (var_die, decl);
18374 if (declaration)
18375 add_AT_flag (var_die, DW_AT_declaration, 1);
18377 if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
18378 equate_decl_number_to_die (decl, var_die);
18380 if (! declaration
18381 && (! DECL_ABSTRACT (decl_or_origin)
18382 /* Local static vars are shared between all clones/inlines,
18383 so emit DW_AT_location on the abstract DIE if DECL_RTL is
18384 already set. */
18385 || (TREE_CODE (decl_or_origin) == VAR_DECL
18386 && TREE_STATIC (decl_or_origin)
18387 && DECL_RTL_SET_P (decl_or_origin)))
18388 /* When abstract origin already has DW_AT_location attribute, no need
18389 to add it again. */
18390 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
18392 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
18393 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
18394 defer_location (decl_or_origin, var_die);
18395 else
18396 add_location_or_const_value_attribute (var_die, decl_or_origin,
18397 decl == NULL, DW_AT_location);
18398 add_pubname (decl_or_origin, var_die);
18400 else
18401 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
18404 /* Generate a DIE to represent a named constant. */
18406 static void
18407 gen_const_die (tree decl, dw_die_ref context_die)
18409 dw_die_ref const_die;
18410 tree type = TREE_TYPE (decl);
18412 const_die = new_die (DW_TAG_constant, context_die, decl);
18413 add_name_and_src_coords_attributes (const_die, decl);
18414 add_type_attribute (const_die, type, 1, 0, context_die);
18415 if (TREE_PUBLIC (decl))
18416 add_AT_flag (const_die, DW_AT_external, 1);
18417 if (DECL_ARTIFICIAL (decl))
18418 add_AT_flag (const_die, DW_AT_artificial, 1);
18419 tree_add_const_value_attribute_for_decl (const_die, decl);
18422 /* Generate a DIE to represent a label identifier. */
18424 static void
18425 gen_label_die (tree decl, dw_die_ref context_die)
18427 tree origin = decl_ultimate_origin (decl);
18428 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
18429 rtx insn;
18430 char label[MAX_ARTIFICIAL_LABEL_BYTES];
18432 if (origin != NULL)
18433 add_abstract_origin_attribute (lbl_die, origin);
18434 else
18435 add_name_and_src_coords_attributes (lbl_die, decl);
18437 if (DECL_ABSTRACT (decl))
18438 equate_decl_number_to_die (decl, lbl_die);
18439 else
18441 insn = DECL_RTL_IF_SET (decl);
18443 /* Deleted labels are programmer specified labels which have been
18444 eliminated because of various optimizations. We still emit them
18445 here so that it is possible to put breakpoints on them. */
18446 if (insn
18447 && (LABEL_P (insn)
18448 || ((NOTE_P (insn)
18449 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
18451 /* When optimization is enabled (via -O) some parts of the compiler
18452 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
18453 represent source-level labels which were explicitly declared by
18454 the user. This really shouldn't be happening though, so catch
18455 it if it ever does happen. */
18456 gcc_assert (!INSN_DELETED_P (insn));
18458 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
18459 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18461 else if (insn
18462 && NOTE_P (insn)
18463 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
18464 && CODE_LABEL_NUMBER (insn) != -1)
18466 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
18467 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18472 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
18473 attributes to the DIE for a block STMT, to describe where the inlined
18474 function was called from. This is similar to add_src_coords_attributes. */
18476 static inline void
18477 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
18479 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
18481 if (dwarf_version >= 3 || !dwarf_strict)
18483 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
18484 add_AT_unsigned (die, DW_AT_call_line, s.line);
18489 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
18490 Add low_pc and high_pc attributes to the DIE for a block STMT. */
18492 static inline void
18493 add_high_low_attributes (tree stmt, dw_die_ref die)
18495 char label[MAX_ARTIFICIAL_LABEL_BYTES];
18497 if (BLOCK_FRAGMENT_CHAIN (stmt)
18498 && (dwarf_version >= 3 || !dwarf_strict))
18500 tree chain, superblock = NULL_TREE;
18501 dw_die_ref pdie;
18502 dw_attr_ref attr = NULL;
18504 if (inlined_function_outer_scope_p (stmt))
18506 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18507 BLOCK_NUMBER (stmt));
18508 add_AT_lbl_id (die, DW_AT_entry_pc, label);
18511 /* Optimize duplicate .debug_ranges lists or even tails of
18512 lists. If this BLOCK has same ranges as its supercontext,
18513 lookup DW_AT_ranges attribute in the supercontext (and
18514 recursively so), verify that the ranges_table contains the
18515 right values and use it instead of adding a new .debug_range. */
18516 for (chain = stmt, pdie = die;
18517 BLOCK_SAME_RANGE (chain);
18518 chain = BLOCK_SUPERCONTEXT (chain))
18520 dw_attr_ref new_attr;
18522 pdie = pdie->die_parent;
18523 if (pdie == NULL)
18524 break;
18525 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
18526 break;
18527 new_attr = get_AT (pdie, DW_AT_ranges);
18528 if (new_attr == NULL
18529 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
18530 break;
18531 attr = new_attr;
18532 superblock = BLOCK_SUPERCONTEXT (chain);
18534 if (attr != NULL
18535 && (ranges_table[attr->dw_attr_val.v.val_offset
18536 / 2 / DWARF2_ADDR_SIZE].num
18537 == BLOCK_NUMBER (superblock))
18538 && BLOCK_FRAGMENT_CHAIN (superblock))
18540 unsigned long off = attr->dw_attr_val.v.val_offset
18541 / 2 / DWARF2_ADDR_SIZE;
18542 unsigned long supercnt = 0, thiscnt = 0;
18543 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
18544 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
18546 ++supercnt;
18547 gcc_checking_assert (ranges_table[off + supercnt].num
18548 == BLOCK_NUMBER (chain));
18550 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
18551 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
18552 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
18553 ++thiscnt;
18554 gcc_assert (supercnt >= thiscnt);
18555 add_AT_range_list (die, DW_AT_ranges,
18556 ((off + supercnt - thiscnt)
18557 * 2 * DWARF2_ADDR_SIZE),
18558 false);
18559 return;
18562 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
18564 chain = BLOCK_FRAGMENT_CHAIN (stmt);
18567 add_ranges (chain);
18568 chain = BLOCK_FRAGMENT_CHAIN (chain);
18570 while (chain);
18571 add_ranges (NULL);
18573 else
18575 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
18576 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18577 BLOCK_NUMBER (stmt));
18578 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
18579 BLOCK_NUMBER (stmt));
18580 add_AT_low_high_pc (die, label, label_high, false);
18584 /* Generate a DIE for a lexical block. */
18586 static void
18587 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
18589 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
18591 if (call_arg_locations)
18593 if (block_map.length () <= BLOCK_NUMBER (stmt))
18594 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
18595 block_map[BLOCK_NUMBER (stmt)] = stmt_die;
18598 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
18599 add_high_low_attributes (stmt, stmt_die);
18601 decls_for_scope (stmt, stmt_die, depth);
18604 /* Generate a DIE for an inlined subprogram. */
18606 static void
18607 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
18609 tree decl;
18611 /* The instance of function that is effectively being inlined shall not
18612 be abstract. */
18613 gcc_assert (! BLOCK_ABSTRACT (stmt));
18615 decl = block_ultimate_origin (stmt);
18617 /* Emit info for the abstract instance first, if we haven't yet. We
18618 must emit this even if the block is abstract, otherwise when we
18619 emit the block below (or elsewhere), we may end up trying to emit
18620 a die whose origin die hasn't been emitted, and crashing. */
18621 dwarf2out_abstract_function (decl);
18623 if (! BLOCK_ABSTRACT (stmt))
18625 dw_die_ref subr_die
18626 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
18628 if (call_arg_locations)
18630 if (block_map.length () <= BLOCK_NUMBER (stmt))
18631 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
18632 block_map[BLOCK_NUMBER (stmt)] = subr_die;
18634 add_abstract_origin_attribute (subr_die, decl);
18635 if (TREE_ASM_WRITTEN (stmt))
18636 add_high_low_attributes (stmt, subr_die);
18637 add_call_src_coords_attributes (stmt, subr_die);
18639 decls_for_scope (stmt, subr_die, depth);
18640 current_function_has_inlines = 1;
18644 /* Generate a DIE for a field in a record, or structure. */
18646 static void
18647 gen_field_die (tree decl, dw_die_ref context_die)
18649 dw_die_ref decl_die;
18651 if (TREE_TYPE (decl) == error_mark_node)
18652 return;
18654 decl_die = new_die (DW_TAG_member, context_die, decl);
18655 add_name_and_src_coords_attributes (decl_die, decl);
18656 add_type_attribute (decl_die, member_declared_type (decl),
18657 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
18658 context_die);
18660 if (DECL_BIT_FIELD_TYPE (decl))
18662 add_byte_size_attribute (decl_die, decl);
18663 add_bit_size_attribute (decl_die, decl);
18664 add_bit_offset_attribute (decl_die, decl);
18667 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
18668 add_data_member_location_attribute (decl_die, decl);
18670 if (DECL_ARTIFICIAL (decl))
18671 add_AT_flag (decl_die, DW_AT_artificial, 1);
18673 add_accessibility_attribute (decl_die, decl);
18675 /* Equate decl number to die, so that we can look up this decl later on. */
18676 equate_decl_number_to_die (decl, decl_die);
18679 #if 0
18680 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18681 Use modified_type_die instead.
18682 We keep this code here just in case these types of DIEs may be needed to
18683 represent certain things in other languages (e.g. Pascal) someday. */
18685 static void
18686 gen_pointer_type_die (tree type, dw_die_ref context_die)
18688 dw_die_ref ptr_die
18689 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
18691 equate_type_number_to_die (type, ptr_die);
18692 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18693 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18696 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18697 Use modified_type_die instead.
18698 We keep this code here just in case these types of DIEs may be needed to
18699 represent certain things in other languages (e.g. Pascal) someday. */
18701 static void
18702 gen_reference_type_die (tree type, dw_die_ref context_die)
18704 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
18706 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
18707 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
18708 else
18709 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
18711 equate_type_number_to_die (type, ref_die);
18712 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
18713 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18715 #endif
18717 /* Generate a DIE for a pointer to a member type. */
18719 static void
18720 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
18722 dw_die_ref ptr_die
18723 = new_die (DW_TAG_ptr_to_member_type,
18724 scope_die_for (type, context_die), type);
18726 equate_type_number_to_die (type, ptr_die);
18727 add_AT_die_ref (ptr_die, DW_AT_containing_type,
18728 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
18729 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18732 typedef const char *dchar_p; /* For DEF_VEC_P. */
18734 static char *producer_string;
18736 /* Return a heap allocated producer string including command line options
18737 if -grecord-gcc-switches. */
18739 static char *
18740 gen_producer_string (void)
18742 size_t j;
18743 vec<dchar_p> switches = vNULL;
18744 const char *language_string = lang_hooks.name;
18745 char *producer, *tail;
18746 const char *p;
18747 size_t len = dwarf_record_gcc_switches ? 0 : 3;
18748 size_t plen = strlen (language_string) + 1 + strlen (version_string);
18750 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
18751 switch (save_decoded_options[j].opt_index)
18753 case OPT_o:
18754 case OPT_d:
18755 case OPT_dumpbase:
18756 case OPT_dumpdir:
18757 case OPT_auxbase:
18758 case OPT_auxbase_strip:
18759 case OPT_quiet:
18760 case OPT_version:
18761 case OPT_v:
18762 case OPT_w:
18763 case OPT_L:
18764 case OPT_D:
18765 case OPT_I:
18766 case OPT_U:
18767 case OPT_SPECIAL_unknown:
18768 case OPT_SPECIAL_ignore:
18769 case OPT_SPECIAL_program_name:
18770 case OPT_SPECIAL_input_file:
18771 case OPT_grecord_gcc_switches:
18772 case OPT_gno_record_gcc_switches:
18773 case OPT__output_pch_:
18774 case OPT_fdiagnostics_show_location_:
18775 case OPT_fdiagnostics_show_option:
18776 case OPT_fdiagnostics_show_caret:
18777 case OPT_fverbose_asm:
18778 case OPT____:
18779 case OPT__sysroot_:
18780 case OPT_nostdinc:
18781 case OPT_nostdinc__:
18782 /* Ignore these. */
18783 continue;
18784 default:
18785 if (cl_options[save_decoded_options[j].opt_index].flags
18786 & CL_NO_DWARF_RECORD)
18787 continue;
18788 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
18789 == '-');
18790 switch (save_decoded_options[j].canonical_option[0][1])
18792 case 'M':
18793 case 'i':
18794 case 'W':
18795 continue;
18796 case 'f':
18797 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
18798 "dump", 4) == 0)
18799 continue;
18800 break;
18801 default:
18802 break;
18804 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
18805 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
18806 break;
18809 producer = XNEWVEC (char, plen + 1 + len + 1);
18810 tail = producer;
18811 sprintf (tail, "%s %s", language_string, version_string);
18812 tail += plen;
18814 FOR_EACH_VEC_ELT (switches, j, p)
18816 len = strlen (p);
18817 *tail = ' ';
18818 memcpy (tail + 1, p, len);
18819 tail += len + 1;
18822 *tail = '\0';
18823 switches.release ();
18824 return producer;
18827 /* Generate the DIE for the compilation unit. */
18829 static dw_die_ref
18830 gen_compile_unit_die (const char *filename)
18832 dw_die_ref die;
18833 const char *language_string = lang_hooks.name;
18834 int language;
18836 die = new_die (DW_TAG_compile_unit, NULL, NULL);
18838 if (filename)
18840 add_name_attribute (die, filename);
18841 /* Don't add cwd for <built-in>. */
18842 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
18843 add_comp_dir_attribute (die);
18846 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
18848 /* If our producer is LTO try to figure out a common language to use
18849 from the global list of translation units. */
18850 if (strcmp (language_string, "GNU GIMPLE") == 0)
18852 unsigned i;
18853 tree t;
18854 const char *common_lang = NULL;
18856 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
18858 if (!TRANSLATION_UNIT_LANGUAGE (t))
18859 continue;
18860 if (!common_lang)
18861 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
18862 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
18864 else if (strncmp (common_lang, "GNU C", 5) == 0
18865 && strncmp(TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
18866 /* Mixing C and C++ is ok, use C++ in that case. */
18867 common_lang = "GNU C++";
18868 else
18870 /* Fall back to C. */
18871 common_lang = NULL;
18872 break;
18876 if (common_lang)
18877 language_string = common_lang;
18880 language = DW_LANG_C89;
18881 if (strcmp (language_string, "GNU C++") == 0)
18882 language = DW_LANG_C_plus_plus;
18883 else if (strcmp (language_string, "GNU F77") == 0)
18884 language = DW_LANG_Fortran77;
18885 else if (strcmp (language_string, "GNU Pascal") == 0)
18886 language = DW_LANG_Pascal83;
18887 else if (dwarf_version >= 3 || !dwarf_strict)
18889 if (strcmp (language_string, "GNU Ada") == 0)
18890 language = DW_LANG_Ada95;
18891 else if (strcmp (language_string, "GNU Fortran") == 0)
18892 language = DW_LANG_Fortran95;
18893 else if (strcmp (language_string, "GNU Java") == 0)
18894 language = DW_LANG_Java;
18895 else if (strcmp (language_string, "GNU Objective-C") == 0)
18896 language = DW_LANG_ObjC;
18897 else if (strcmp (language_string, "GNU Objective-C++") == 0)
18898 language = DW_LANG_ObjC_plus_plus;
18899 else if (dwarf_version >= 5 || !dwarf_strict)
18901 if (strcmp (language_string, "GNU Go") == 0)
18902 language = DW_LANG_Go;
18905 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
18906 else if (strcmp (language_string, "GNU Fortran") == 0)
18907 language = DW_LANG_Fortran90;
18909 add_AT_unsigned (die, DW_AT_language, language);
18911 switch (language)
18913 case DW_LANG_Fortran77:
18914 case DW_LANG_Fortran90:
18915 case DW_LANG_Fortran95:
18916 /* Fortran has case insensitive identifiers and the front-end
18917 lowercases everything. */
18918 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
18919 break;
18920 default:
18921 /* The default DW_ID_case_sensitive doesn't need to be specified. */
18922 break;
18924 return die;
18927 /* Generate the DIE for a base class. */
18929 static void
18930 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
18932 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
18934 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
18935 add_data_member_location_attribute (die, binfo);
18937 if (BINFO_VIRTUAL_P (binfo))
18938 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
18940 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
18941 children, otherwise the default is DW_ACCESS_public. In DWARF2
18942 the default has always been DW_ACCESS_private. */
18943 if (access == access_public_node)
18945 if (dwarf_version == 2
18946 || context_die->die_tag == DW_TAG_class_type)
18947 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
18949 else if (access == access_protected_node)
18950 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
18951 else if (dwarf_version > 2
18952 && context_die->die_tag != DW_TAG_class_type)
18953 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
18956 /* Generate a DIE for a class member. */
18958 static void
18959 gen_member_die (tree type, dw_die_ref context_die)
18961 tree member;
18962 tree binfo = TYPE_BINFO (type);
18963 dw_die_ref child;
18965 /* If this is not an incomplete type, output descriptions of each of its
18966 members. Note that as we output the DIEs necessary to represent the
18967 members of this record or union type, we will also be trying to output
18968 DIEs to represent the *types* of those members. However the `type'
18969 function (above) will specifically avoid generating type DIEs for member
18970 types *within* the list of member DIEs for this (containing) type except
18971 for those types (of members) which are explicitly marked as also being
18972 members of this (containing) type themselves. The g++ front- end can
18973 force any given type to be treated as a member of some other (containing)
18974 type by setting the TYPE_CONTEXT of the given (member) type to point to
18975 the TREE node representing the appropriate (containing) type. */
18977 /* First output info about the base classes. */
18978 if (binfo)
18980 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
18981 int i;
18982 tree base;
18984 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
18985 gen_inheritance_die (base,
18986 (accesses ? (*accesses)[i] : access_public_node),
18987 context_die);
18990 /* Now output info about the data members and type members. */
18991 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
18993 /* If we thought we were generating minimal debug info for TYPE
18994 and then changed our minds, some of the member declarations
18995 may have already been defined. Don't define them again, but
18996 do put them in the right order. */
18998 child = lookup_decl_die (member);
18999 if (child)
19000 splice_child_die (context_die, child);
19001 else
19002 gen_decl_die (member, NULL, context_die);
19005 /* Now output info about the function members (if any). */
19006 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19008 /* Don't include clones in the member list. */
19009 if (DECL_ABSTRACT_ORIGIN (member))
19010 continue;
19012 child = lookup_decl_die (member);
19013 if (child)
19014 splice_child_die (context_die, child);
19015 else
19016 gen_decl_die (member, NULL, context_die);
19020 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
19021 is set, we pretend that the type was never defined, so we only get the
19022 member DIEs needed by later specification DIEs. */
19024 static void
19025 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19026 enum debug_info_usage usage)
19028 dw_die_ref type_die = lookup_type_die (type);
19029 dw_die_ref scope_die = 0;
19030 int nested = 0;
19031 int complete = (TYPE_SIZE (type)
19032 && (! TYPE_STUB_DECL (type)
19033 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19034 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19035 complete = complete && should_emit_struct_debug (type, usage);
19037 if (type_die && ! complete)
19038 return;
19040 if (TYPE_CONTEXT (type) != NULL_TREE
19041 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19042 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19043 nested = 1;
19045 scope_die = scope_die_for (type, context_die);
19047 if (! type_die || (nested && is_cu_die (scope_die)))
19048 /* First occurrence of type or toplevel definition of nested class. */
19050 dw_die_ref old_die = type_die;
19052 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
19053 ? record_type_tag (type) : DW_TAG_union_type,
19054 scope_die, type);
19055 equate_type_number_to_die (type, type_die);
19056 if (old_die)
19057 add_AT_specification (type_die, old_die);
19058 else
19059 add_name_attribute (type_die, type_tag (type));
19061 else
19062 remove_AT (type_die, DW_AT_declaration);
19064 /* Generate child dies for template paramaters. */
19065 if (debug_info_level > DINFO_LEVEL_TERSE
19066 && COMPLETE_TYPE_P (type))
19067 schedule_generic_params_dies_gen (type);
19069 /* If this type has been completed, then give it a byte_size attribute and
19070 then give a list of members. */
19071 if (complete && !ns_decl)
19073 /* Prevent infinite recursion in cases where the type of some member of
19074 this type is expressed in terms of this type itself. */
19075 TREE_ASM_WRITTEN (type) = 1;
19076 add_byte_size_attribute (type_die, type);
19077 if (TYPE_STUB_DECL (type) != NULL_TREE)
19079 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19080 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19083 /* If the first reference to this type was as the return type of an
19084 inline function, then it may not have a parent. Fix this now. */
19085 if (type_die->die_parent == NULL)
19086 add_child_die (scope_die, type_die);
19088 push_decl_scope (type);
19089 gen_member_die (type, type_die);
19090 pop_decl_scope ();
19092 add_gnat_descriptive_type_attribute (type_die, type, context_die);
19093 if (TYPE_ARTIFICIAL (type))
19094 add_AT_flag (type_die, DW_AT_artificial, 1);
19096 /* GNU extension: Record what type our vtable lives in. */
19097 if (TYPE_VFIELD (type))
19099 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
19101 gen_type_die (vtype, context_die);
19102 add_AT_die_ref (type_die, DW_AT_containing_type,
19103 lookup_type_die (vtype));
19106 else
19108 add_AT_flag (type_die, DW_AT_declaration, 1);
19110 /* We don't need to do this for function-local types. */
19111 if (TYPE_STUB_DECL (type)
19112 && ! decl_function_context (TYPE_STUB_DECL (type)))
19113 vec_safe_push (incomplete_types, type);
19116 if (get_AT (type_die, DW_AT_name))
19117 add_pubtype (type, type_die);
19120 /* Generate a DIE for a subroutine _type_. */
19122 static void
19123 gen_subroutine_type_die (tree type, dw_die_ref context_die)
19125 tree return_type = TREE_TYPE (type);
19126 dw_die_ref subr_die
19127 = new_die (DW_TAG_subroutine_type,
19128 scope_die_for (type, context_die), type);
19130 equate_type_number_to_die (type, subr_die);
19131 add_prototyped_attribute (subr_die, type);
19132 add_type_attribute (subr_die, return_type, 0, 0, context_die);
19133 gen_formal_types_die (type, subr_die);
19135 if (get_AT (subr_die, DW_AT_name))
19136 add_pubtype (type, subr_die);
19139 /* Generate a DIE for a type definition. */
19141 static void
19142 gen_typedef_die (tree decl, dw_die_ref context_die)
19144 dw_die_ref type_die;
19145 tree origin;
19147 if (TREE_ASM_WRITTEN (decl))
19148 return;
19150 TREE_ASM_WRITTEN (decl) = 1;
19151 type_die = new_die (DW_TAG_typedef, context_die, decl);
19152 origin = decl_ultimate_origin (decl);
19153 if (origin != NULL)
19154 add_abstract_origin_attribute (type_die, origin);
19155 else
19157 tree type;
19159 add_name_and_src_coords_attributes (type_die, decl);
19160 if (DECL_ORIGINAL_TYPE (decl))
19162 type = DECL_ORIGINAL_TYPE (decl);
19164 gcc_assert (type != TREE_TYPE (decl));
19165 equate_type_number_to_die (TREE_TYPE (decl), type_die);
19167 else
19169 type = TREE_TYPE (decl);
19171 if (is_naming_typedef_decl (TYPE_NAME (type)))
19173 /* Here, we are in the case of decl being a typedef naming
19174 an anonymous type, e.g:
19175 typedef struct {...} foo;
19176 In that case TREE_TYPE (decl) is not a typedef variant
19177 type and TYPE_NAME of the anonymous type is set to the
19178 TYPE_DECL of the typedef. This construct is emitted by
19179 the C++ FE.
19181 TYPE is the anonymous struct named by the typedef
19182 DECL. As we need the DW_AT_type attribute of the
19183 DW_TAG_typedef to point to the DIE of TYPE, let's
19184 generate that DIE right away. add_type_attribute
19185 called below will then pick (via lookup_type_die) that
19186 anonymous struct DIE. */
19187 if (!TREE_ASM_WRITTEN (type))
19188 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
19190 /* This is a GNU Extension. We are adding a
19191 DW_AT_linkage_name attribute to the DIE of the
19192 anonymous struct TYPE. The value of that attribute
19193 is the name of the typedef decl naming the anonymous
19194 struct. This greatly eases the work of consumers of
19195 this debug info. */
19196 add_linkage_attr (lookup_type_die (type), decl);
19200 add_type_attribute (type_die, type, TREE_READONLY (decl),
19201 TREE_THIS_VOLATILE (decl), context_die);
19203 if (is_naming_typedef_decl (decl))
19204 /* We want that all subsequent calls to lookup_type_die with
19205 TYPE in argument yield the DW_TAG_typedef we have just
19206 created. */
19207 equate_type_number_to_die (type, type_die);
19209 add_accessibility_attribute (type_die, decl);
19212 if (DECL_ABSTRACT (decl))
19213 equate_decl_number_to_die (decl, type_die);
19215 if (get_AT (type_die, DW_AT_name))
19216 add_pubtype (decl, type_die);
19219 /* Generate a DIE for a struct, class, enum or union type. */
19221 static void
19222 gen_tagged_type_die (tree type,
19223 dw_die_ref context_die,
19224 enum debug_info_usage usage)
19226 int need_pop;
19228 if (type == NULL_TREE
19229 || !is_tagged_type (type))
19230 return;
19232 /* If this is a nested type whose containing class hasn't been written
19233 out yet, writing it out will cover this one, too. This does not apply
19234 to instantiations of member class templates; they need to be added to
19235 the containing class as they are generated. FIXME: This hurts the
19236 idea of combining type decls from multiple TUs, since we can't predict
19237 what set of template instantiations we'll get. */
19238 if (TYPE_CONTEXT (type)
19239 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19240 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
19242 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
19244 if (TREE_ASM_WRITTEN (type))
19245 return;
19247 /* If that failed, attach ourselves to the stub. */
19248 push_decl_scope (TYPE_CONTEXT (type));
19249 context_die = lookup_type_die (TYPE_CONTEXT (type));
19250 need_pop = 1;
19252 else if (TYPE_CONTEXT (type) != NULL_TREE
19253 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
19255 /* If this type is local to a function that hasn't been written
19256 out yet, use a NULL context for now; it will be fixed up in
19257 decls_for_scope. */
19258 context_die = lookup_decl_die (TYPE_CONTEXT (type));
19259 /* A declaration DIE doesn't count; nested types need to go in the
19260 specification. */
19261 if (context_die && is_declaration_die (context_die))
19262 context_die = NULL;
19263 need_pop = 0;
19265 else
19267 context_die = declare_in_namespace (type, context_die);
19268 need_pop = 0;
19271 if (TREE_CODE (type) == ENUMERAL_TYPE)
19273 /* This might have been written out by the call to
19274 declare_in_namespace. */
19275 if (!TREE_ASM_WRITTEN (type))
19276 gen_enumeration_type_die (type, context_die);
19278 else
19279 gen_struct_or_union_type_die (type, context_die, usage);
19281 if (need_pop)
19282 pop_decl_scope ();
19284 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
19285 it up if it is ever completed. gen_*_type_die will set it for us
19286 when appropriate. */
19289 /* Generate a type description DIE. */
19291 static void
19292 gen_type_die_with_usage (tree type, dw_die_ref context_die,
19293 enum debug_info_usage usage)
19295 struct array_descr_info info;
19297 if (type == NULL_TREE || type == error_mark_node)
19298 return;
19300 if (TYPE_NAME (type) != NULL_TREE
19301 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
19302 && is_redundant_typedef (TYPE_NAME (type))
19303 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
19304 /* The DECL of this type is a typedef we don't want to emit debug
19305 info for but we want debug info for its underlying typedef.
19306 This can happen for e.g, the injected-class-name of a C++
19307 type. */
19308 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
19310 /* If TYPE is a typedef type variant, let's generate debug info
19311 for the parent typedef which TYPE is a type of. */
19312 if (typedef_variant_p (type))
19314 if (TREE_ASM_WRITTEN (type))
19315 return;
19317 /* Prevent broken recursion; we can't hand off to the same type. */
19318 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
19320 /* Give typedefs the right scope. */
19321 context_die = scope_die_for (type, context_die);
19323 TREE_ASM_WRITTEN (type) = 1;
19325 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19326 return;
19329 /* If type is an anonymous tagged type named by a typedef, let's
19330 generate debug info for the typedef. */
19331 if (is_naming_typedef_decl (TYPE_NAME (type)))
19333 /* Use the DIE of the containing namespace as the parent DIE of
19334 the type description DIE we want to generate. */
19335 if (DECL_CONTEXT (TYPE_NAME (type))
19336 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
19337 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
19339 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19340 return;
19343 /* If this is an array type with hidden descriptor, handle it first. */
19344 if (!TREE_ASM_WRITTEN (type)
19345 && lang_hooks.types.get_array_descr_info
19346 && lang_hooks.types.get_array_descr_info (type, &info)
19347 && (dwarf_version >= 3 || !dwarf_strict))
19349 gen_descr_array_type_die (type, &info, context_die);
19350 TREE_ASM_WRITTEN (type) = 1;
19351 return;
19354 /* We are going to output a DIE to represent the unqualified version
19355 of this type (i.e. without any const or volatile qualifiers) so
19356 get the main variant (i.e. the unqualified version) of this type
19357 now. (Vectors are special because the debugging info is in the
19358 cloned type itself). */
19359 if (TREE_CODE (type) != VECTOR_TYPE)
19360 type = type_main_variant (type);
19362 if (TREE_ASM_WRITTEN (type))
19363 return;
19365 switch (TREE_CODE (type))
19367 case ERROR_MARK:
19368 break;
19370 case POINTER_TYPE:
19371 case REFERENCE_TYPE:
19372 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
19373 ensures that the gen_type_die recursion will terminate even if the
19374 type is recursive. Recursive types are possible in Ada. */
19375 /* ??? We could perhaps do this for all types before the switch
19376 statement. */
19377 TREE_ASM_WRITTEN (type) = 1;
19379 /* For these types, all that is required is that we output a DIE (or a
19380 set of DIEs) to represent the "basis" type. */
19381 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19382 DINFO_USAGE_IND_USE);
19383 break;
19385 case OFFSET_TYPE:
19386 /* This code is used for C++ pointer-to-data-member types.
19387 Output a description of the relevant class type. */
19388 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
19389 DINFO_USAGE_IND_USE);
19391 /* Output a description of the type of the object pointed to. */
19392 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19393 DINFO_USAGE_IND_USE);
19395 /* Now output a DIE to represent this pointer-to-data-member type
19396 itself. */
19397 gen_ptr_to_mbr_type_die (type, context_die);
19398 break;
19400 case FUNCTION_TYPE:
19401 /* Force out return type (in case it wasn't forced out already). */
19402 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19403 DINFO_USAGE_DIR_USE);
19404 gen_subroutine_type_die (type, context_die);
19405 break;
19407 case METHOD_TYPE:
19408 /* Force out return type (in case it wasn't forced out already). */
19409 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19410 DINFO_USAGE_DIR_USE);
19411 gen_subroutine_type_die (type, context_die);
19412 break;
19414 case ARRAY_TYPE:
19415 gen_array_type_die (type, context_die);
19416 break;
19418 case VECTOR_TYPE:
19419 gen_array_type_die (type, context_die);
19420 break;
19422 case ENUMERAL_TYPE:
19423 case RECORD_TYPE:
19424 case UNION_TYPE:
19425 case QUAL_UNION_TYPE:
19426 gen_tagged_type_die (type, context_die, usage);
19427 return;
19429 case VOID_TYPE:
19430 case INTEGER_TYPE:
19431 case REAL_TYPE:
19432 case FIXED_POINT_TYPE:
19433 case COMPLEX_TYPE:
19434 case BOOLEAN_TYPE:
19435 /* No DIEs needed for fundamental types. */
19436 break;
19438 case NULLPTR_TYPE:
19439 case LANG_TYPE:
19440 /* Just use DW_TAG_unspecified_type. */
19442 dw_die_ref type_die = lookup_type_die (type);
19443 if (type_die == NULL)
19445 tree name = TYPE_NAME (type);
19446 if (TREE_CODE (name) == TYPE_DECL)
19447 name = DECL_NAME (name);
19448 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
19449 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
19450 equate_type_number_to_die (type, type_die);
19453 break;
19455 default:
19456 gcc_unreachable ();
19459 TREE_ASM_WRITTEN (type) = 1;
19462 static void
19463 gen_type_die (tree type, dw_die_ref context_die)
19465 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
19468 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
19469 things which are local to the given block. */
19471 static void
19472 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
19474 int must_output_die = 0;
19475 bool inlined_func;
19477 /* Ignore blocks that are NULL. */
19478 if (stmt == NULL_TREE)
19479 return;
19481 inlined_func = inlined_function_outer_scope_p (stmt);
19483 /* If the block is one fragment of a non-contiguous block, do not
19484 process the variables, since they will have been done by the
19485 origin block. Do process subblocks. */
19486 if (BLOCK_FRAGMENT_ORIGIN (stmt))
19488 tree sub;
19490 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
19491 gen_block_die (sub, context_die, depth + 1);
19493 return;
19496 /* Determine if we need to output any Dwarf DIEs at all to represent this
19497 block. */
19498 if (inlined_func)
19499 /* The outer scopes for inlinings *must* always be represented. We
19500 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
19501 must_output_die = 1;
19502 else
19504 /* Determine if this block directly contains any "significant"
19505 local declarations which we will need to output DIEs for. */
19506 if (debug_info_level > DINFO_LEVEL_TERSE)
19507 /* We are not in terse mode so *any* local declaration counts
19508 as being a "significant" one. */
19509 must_output_die = ((BLOCK_VARS (stmt) != NULL
19510 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
19511 && (TREE_USED (stmt)
19512 || TREE_ASM_WRITTEN (stmt)
19513 || BLOCK_ABSTRACT (stmt)));
19514 else if ((TREE_USED (stmt)
19515 || TREE_ASM_WRITTEN (stmt)
19516 || BLOCK_ABSTRACT (stmt))
19517 && !dwarf2out_ignore_block (stmt))
19518 must_output_die = 1;
19521 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
19522 DIE for any block which contains no significant local declarations at
19523 all. Rather, in such cases we just call `decls_for_scope' so that any
19524 needed Dwarf info for any sub-blocks will get properly generated. Note
19525 that in terse mode, our definition of what constitutes a "significant"
19526 local declaration gets restricted to include only inlined function
19527 instances and local (nested) function definitions. */
19528 if (must_output_die)
19530 if (inlined_func)
19532 /* If STMT block is abstract, that means we have been called
19533 indirectly from dwarf2out_abstract_function.
19534 That function rightfully marks the descendent blocks (of
19535 the abstract function it is dealing with) as being abstract,
19536 precisely to prevent us from emitting any
19537 DW_TAG_inlined_subroutine DIE as a descendent
19538 of an abstract function instance. So in that case, we should
19539 not call gen_inlined_subroutine_die.
19541 Later though, when cgraph asks dwarf2out to emit info
19542 for the concrete instance of the function decl into which
19543 the concrete instance of STMT got inlined, the later will lead
19544 to the generation of a DW_TAG_inlined_subroutine DIE. */
19545 if (! BLOCK_ABSTRACT (stmt))
19546 gen_inlined_subroutine_die (stmt, context_die, depth);
19548 else
19549 gen_lexical_block_die (stmt, context_die, depth);
19551 else
19552 decls_for_scope (stmt, context_die, depth);
19555 /* Process variable DECL (or variable with origin ORIGIN) within
19556 block STMT and add it to CONTEXT_DIE. */
19557 static void
19558 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
19560 dw_die_ref die;
19561 tree decl_or_origin = decl ? decl : origin;
19563 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
19564 die = lookup_decl_die (decl_or_origin);
19565 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
19566 && TYPE_DECL_IS_STUB (decl_or_origin))
19567 die = lookup_type_die (TREE_TYPE (decl_or_origin));
19568 else
19569 die = NULL;
19571 if (die != NULL && die->die_parent == NULL)
19572 add_child_die (context_die, die);
19573 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
19574 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
19575 stmt, context_die);
19576 else
19577 gen_decl_die (decl, origin, context_die);
19580 /* Generate all of the decls declared within a given scope and (recursively)
19581 all of its sub-blocks. */
19583 static void
19584 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
19586 tree decl;
19587 unsigned int i;
19588 tree subblocks;
19590 /* Ignore NULL blocks. */
19591 if (stmt == NULL_TREE)
19592 return;
19594 /* Output the DIEs to represent all of the data objects and typedefs
19595 declared directly within this block but not within any nested
19596 sub-blocks. Also, nested function and tag DIEs have been
19597 generated with a parent of NULL; fix that up now. */
19598 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
19599 process_scope_var (stmt, decl, NULL_TREE, context_die);
19600 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
19601 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
19602 context_die);
19604 /* If we're at -g1, we're not interested in subblocks. */
19605 if (debug_info_level <= DINFO_LEVEL_TERSE)
19606 return;
19608 /* Output the DIEs to represent all sub-blocks (and the items declared
19609 therein) of this block. */
19610 for (subblocks = BLOCK_SUBBLOCKS (stmt);
19611 subblocks != NULL;
19612 subblocks = BLOCK_CHAIN (subblocks))
19613 gen_block_die (subblocks, context_die, depth + 1);
19616 /* Is this a typedef we can avoid emitting? */
19618 static inline int
19619 is_redundant_typedef (const_tree decl)
19621 if (TYPE_DECL_IS_STUB (decl))
19622 return 1;
19624 if (DECL_ARTIFICIAL (decl)
19625 && DECL_CONTEXT (decl)
19626 && is_tagged_type (DECL_CONTEXT (decl))
19627 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
19628 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
19629 /* Also ignore the artificial member typedef for the class name. */
19630 return 1;
19632 return 0;
19635 /* Return TRUE if TYPE is a typedef that names a type for linkage
19636 purposes. This kind of typedefs is produced by the C++ FE for
19637 constructs like:
19639 typedef struct {...} foo;
19641 In that case, there is no typedef variant type produced for foo.
19642 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
19643 struct type. */
19645 static bool
19646 is_naming_typedef_decl (const_tree decl)
19648 if (decl == NULL_TREE
19649 || TREE_CODE (decl) != TYPE_DECL
19650 || !is_tagged_type (TREE_TYPE (decl))
19651 || DECL_IS_BUILTIN (decl)
19652 || is_redundant_typedef (decl)
19653 /* It looks like Ada produces TYPE_DECLs that are very similar
19654 to C++ naming typedefs but that have different
19655 semantics. Let's be specific to c++ for now. */
19656 || !is_cxx ())
19657 return FALSE;
19659 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
19660 && TYPE_NAME (TREE_TYPE (decl)) == decl
19661 && (TYPE_STUB_DECL (TREE_TYPE (decl))
19662 != TYPE_NAME (TREE_TYPE (decl))));
19665 /* Returns the DIE for a context. */
19667 static inline dw_die_ref
19668 get_context_die (tree context)
19670 if (context)
19672 /* Find die that represents this context. */
19673 if (TYPE_P (context))
19675 context = TYPE_MAIN_VARIANT (context);
19676 return strip_naming_typedef (context, force_type_die (context));
19678 else
19679 return force_decl_die (context);
19681 return comp_unit_die ();
19684 /* Returns the DIE for decl. A DIE will always be returned. */
19686 static dw_die_ref
19687 force_decl_die (tree decl)
19689 dw_die_ref decl_die;
19690 unsigned saved_external_flag;
19691 tree save_fn = NULL_TREE;
19692 decl_die = lookup_decl_die (decl);
19693 if (!decl_die)
19695 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
19697 decl_die = lookup_decl_die (decl);
19698 if (decl_die)
19699 return decl_die;
19701 switch (TREE_CODE (decl))
19703 case FUNCTION_DECL:
19704 /* Clear current_function_decl, so that gen_subprogram_die thinks
19705 that this is a declaration. At this point, we just want to force
19706 declaration die. */
19707 save_fn = current_function_decl;
19708 current_function_decl = NULL_TREE;
19709 gen_subprogram_die (decl, context_die);
19710 current_function_decl = save_fn;
19711 break;
19713 case VAR_DECL:
19714 /* Set external flag to force declaration die. Restore it after
19715 gen_decl_die() call. */
19716 saved_external_flag = DECL_EXTERNAL (decl);
19717 DECL_EXTERNAL (decl) = 1;
19718 gen_decl_die (decl, NULL, context_die);
19719 DECL_EXTERNAL (decl) = saved_external_flag;
19720 break;
19722 case NAMESPACE_DECL:
19723 if (dwarf_version >= 3 || !dwarf_strict)
19724 dwarf2out_decl (decl);
19725 else
19726 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
19727 decl_die = comp_unit_die ();
19728 break;
19730 case TRANSLATION_UNIT_DECL:
19731 decl_die = comp_unit_die ();
19732 break;
19734 default:
19735 gcc_unreachable ();
19738 /* We should be able to find the DIE now. */
19739 if (!decl_die)
19740 decl_die = lookup_decl_die (decl);
19741 gcc_assert (decl_die);
19744 return decl_die;
19747 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
19748 always returned. */
19750 static dw_die_ref
19751 force_type_die (tree type)
19753 dw_die_ref type_die;
19755 type_die = lookup_type_die (type);
19756 if (!type_die)
19758 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
19760 type_die = modified_type_die (type, TYPE_READONLY (type),
19761 TYPE_VOLATILE (type), context_die);
19762 gcc_assert (type_die);
19764 return type_die;
19767 /* Force out any required namespaces to be able to output DECL,
19768 and return the new context_die for it, if it's changed. */
19770 static dw_die_ref
19771 setup_namespace_context (tree thing, dw_die_ref context_die)
19773 tree context = (DECL_P (thing)
19774 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
19775 if (context && TREE_CODE (context) == NAMESPACE_DECL)
19776 /* Force out the namespace. */
19777 context_die = force_decl_die (context);
19779 return context_die;
19782 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
19783 type) within its namespace, if appropriate.
19785 For compatibility with older debuggers, namespace DIEs only contain
19786 declarations; all definitions are emitted at CU scope. */
19788 static dw_die_ref
19789 declare_in_namespace (tree thing, dw_die_ref context_die)
19791 dw_die_ref ns_context;
19793 if (debug_info_level <= DINFO_LEVEL_TERSE)
19794 return context_die;
19796 /* If this decl is from an inlined function, then don't try to emit it in its
19797 namespace, as we will get confused. It would have already been emitted
19798 when the abstract instance of the inline function was emitted anyways. */
19799 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
19800 return context_die;
19802 ns_context = setup_namespace_context (thing, context_die);
19804 if (ns_context != context_die)
19806 if (is_fortran ())
19807 return ns_context;
19808 if (DECL_P (thing))
19809 gen_decl_die (thing, NULL, ns_context);
19810 else
19811 gen_type_die (thing, ns_context);
19813 return context_die;
19816 /* Generate a DIE for a namespace or namespace alias. */
19818 static void
19819 gen_namespace_die (tree decl, dw_die_ref context_die)
19821 dw_die_ref namespace_die;
19823 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
19824 they are an alias of. */
19825 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
19827 /* Output a real namespace or module. */
19828 context_die = setup_namespace_context (decl, comp_unit_die ());
19829 namespace_die = new_die (is_fortran ()
19830 ? DW_TAG_module : DW_TAG_namespace,
19831 context_die, decl);
19832 /* For Fortran modules defined in different CU don't add src coords. */
19833 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
19835 const char *name = dwarf2_name (decl, 0);
19836 if (name)
19837 add_name_attribute (namespace_die, name);
19839 else
19840 add_name_and_src_coords_attributes (namespace_die, decl);
19841 if (DECL_EXTERNAL (decl))
19842 add_AT_flag (namespace_die, DW_AT_declaration, 1);
19843 equate_decl_number_to_die (decl, namespace_die);
19845 else
19847 /* Output a namespace alias. */
19849 /* Force out the namespace we are an alias of, if necessary. */
19850 dw_die_ref origin_die
19851 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
19853 if (DECL_FILE_SCOPE_P (decl)
19854 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
19855 context_die = setup_namespace_context (decl, comp_unit_die ());
19856 /* Now create the namespace alias DIE. */
19857 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
19858 add_name_and_src_coords_attributes (namespace_die, decl);
19859 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
19860 equate_decl_number_to_die (decl, namespace_die);
19862 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
19863 if (want_pubnames ())
19864 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
19867 /* Generate Dwarf debug information for a decl described by DECL.
19868 The return value is currently only meaningful for PARM_DECLs,
19869 for all other decls it returns NULL. */
19871 static dw_die_ref
19872 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
19874 tree decl_or_origin = decl ? decl : origin;
19875 tree class_origin = NULL, ultimate_origin;
19877 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
19878 return NULL;
19880 switch (TREE_CODE (decl_or_origin))
19882 case ERROR_MARK:
19883 break;
19885 case CONST_DECL:
19886 if (!is_fortran () && !is_ada ())
19888 /* The individual enumerators of an enum type get output when we output
19889 the Dwarf representation of the relevant enum type itself. */
19890 break;
19893 /* Emit its type. */
19894 gen_type_die (TREE_TYPE (decl), context_die);
19896 /* And its containing namespace. */
19897 context_die = declare_in_namespace (decl, context_die);
19899 gen_const_die (decl, context_die);
19900 break;
19902 case FUNCTION_DECL:
19903 /* Don't output any DIEs to represent mere function declarations,
19904 unless they are class members or explicit block externs. */
19905 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
19906 && DECL_FILE_SCOPE_P (decl_or_origin)
19907 && (current_function_decl == NULL_TREE
19908 || DECL_ARTIFICIAL (decl_or_origin)))
19909 break;
19911 #if 0
19912 /* FIXME */
19913 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
19914 on local redeclarations of global functions. That seems broken. */
19915 if (current_function_decl != decl)
19916 /* This is only a declaration. */;
19917 #endif
19919 /* If we're emitting a clone, emit info for the abstract instance. */
19920 if (origin || DECL_ORIGIN (decl) != decl)
19921 dwarf2out_abstract_function (origin
19922 ? DECL_ORIGIN (origin)
19923 : DECL_ABSTRACT_ORIGIN (decl));
19925 /* If we're emitting an out-of-line copy of an inline function,
19926 emit info for the abstract instance and set up to refer to it. */
19927 else if (cgraph_function_possibly_inlined_p (decl)
19928 && ! DECL_ABSTRACT (decl)
19929 && ! class_or_namespace_scope_p (context_die)
19930 /* dwarf2out_abstract_function won't emit a die if this is just
19931 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
19932 that case, because that works only if we have a die. */
19933 && DECL_INITIAL (decl) != NULL_TREE)
19935 dwarf2out_abstract_function (decl);
19936 set_decl_origin_self (decl);
19939 /* Otherwise we're emitting the primary DIE for this decl. */
19940 else if (debug_info_level > DINFO_LEVEL_TERSE)
19942 /* Before we describe the FUNCTION_DECL itself, make sure that we
19943 have its containing type. */
19944 if (!origin)
19945 origin = decl_class_context (decl);
19946 if (origin != NULL_TREE)
19947 gen_type_die (origin, context_die);
19949 /* And its return type. */
19950 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
19952 /* And its virtual context. */
19953 if (DECL_VINDEX (decl) != NULL_TREE)
19954 gen_type_die (DECL_CONTEXT (decl), context_die);
19956 /* Make sure we have a member DIE for decl. */
19957 if (origin != NULL_TREE)
19958 gen_type_die_for_member (origin, decl, context_die);
19960 /* And its containing namespace. */
19961 context_die = declare_in_namespace (decl, context_die);
19964 /* Now output a DIE to represent the function itself. */
19965 if (decl)
19966 gen_subprogram_die (decl, context_die);
19967 break;
19969 case TYPE_DECL:
19970 /* If we are in terse mode, don't generate any DIEs to represent any
19971 actual typedefs. */
19972 if (debug_info_level <= DINFO_LEVEL_TERSE)
19973 break;
19975 /* In the special case of a TYPE_DECL node representing the declaration
19976 of some type tag, if the given TYPE_DECL is marked as having been
19977 instantiated from some other (original) TYPE_DECL node (e.g. one which
19978 was generated within the original definition of an inline function) we
19979 used to generate a special (abbreviated) DW_TAG_structure_type,
19980 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
19981 should be actually referencing those DIEs, as variable DIEs with that
19982 type would be emitted already in the abstract origin, so it was always
19983 removed during unused type prunning. Don't add anything in this
19984 case. */
19985 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
19986 break;
19988 if (is_redundant_typedef (decl))
19989 gen_type_die (TREE_TYPE (decl), context_die);
19990 else
19991 /* Output a DIE to represent the typedef itself. */
19992 gen_typedef_die (decl, context_die);
19993 break;
19995 case LABEL_DECL:
19996 if (debug_info_level >= DINFO_LEVEL_NORMAL)
19997 gen_label_die (decl, context_die);
19998 break;
20000 case VAR_DECL:
20001 case RESULT_DECL:
20002 /* If we are in terse mode, don't generate any DIEs to represent any
20003 variable declarations or definitions. */
20004 if (debug_info_level <= DINFO_LEVEL_TERSE)
20005 break;
20007 /* Output any DIEs that are needed to specify the type of this data
20008 object. */
20009 if (decl_by_reference_p (decl_or_origin))
20010 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20011 else
20012 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20014 /* And its containing type. */
20015 class_origin = decl_class_context (decl_or_origin);
20016 if (class_origin != NULL_TREE)
20017 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
20019 /* And its containing namespace. */
20020 context_die = declare_in_namespace (decl_or_origin, context_die);
20022 /* Now output the DIE to represent the data object itself. This gets
20023 complicated because of the possibility that the VAR_DECL really
20024 represents an inlined instance of a formal parameter for an inline
20025 function. */
20026 ultimate_origin = decl_ultimate_origin (decl_or_origin);
20027 if (ultimate_origin != NULL_TREE
20028 && TREE_CODE (ultimate_origin) == PARM_DECL)
20029 gen_formal_parameter_die (decl, origin,
20030 true /* Emit name attribute. */,
20031 context_die);
20032 else
20033 gen_variable_die (decl, origin, context_die);
20034 break;
20036 case FIELD_DECL:
20037 /* Ignore the nameless fields that are used to skip bits but handle C++
20038 anonymous unions and structs. */
20039 if (DECL_NAME (decl) != NULL_TREE
20040 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
20041 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
20043 gen_type_die (member_declared_type (decl), context_die);
20044 gen_field_die (decl, context_die);
20046 break;
20048 case PARM_DECL:
20049 if (DECL_BY_REFERENCE (decl_or_origin))
20050 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20051 else
20052 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20053 return gen_formal_parameter_die (decl, origin,
20054 true /* Emit name attribute. */,
20055 context_die);
20057 case NAMESPACE_DECL:
20058 case IMPORTED_DECL:
20059 if (dwarf_version >= 3 || !dwarf_strict)
20060 gen_namespace_die (decl, context_die);
20061 break;
20063 default:
20064 /* Probably some frontend-internal decl. Assume we don't care. */
20065 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
20066 break;
20069 return NULL;
20072 /* Output debug information for global decl DECL. Called from toplev.c after
20073 compilation proper has finished. */
20075 static void
20076 dwarf2out_global_decl (tree decl)
20078 /* Output DWARF2 information for file-scope tentative data object
20079 declarations, file-scope (extern) function declarations (which
20080 had no corresponding body) and file-scope tagged type declarations
20081 and definitions which have not yet been forced out. */
20082 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
20083 dwarf2out_decl (decl);
20086 /* Output debug information for type decl DECL. Called from toplev.c
20087 and from language front ends (to record built-in types). */
20088 static void
20089 dwarf2out_type_decl (tree decl, int local)
20091 if (!local)
20092 dwarf2out_decl (decl);
20095 /* Output debug information for imported module or decl DECL.
20096 NAME is non-NULL name in the lexical block if the decl has been renamed.
20097 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
20098 that DECL belongs to.
20099 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
20100 static void
20101 dwarf2out_imported_module_or_decl_1 (tree decl,
20102 tree name,
20103 tree lexical_block,
20104 dw_die_ref lexical_block_die)
20106 expanded_location xloc;
20107 dw_die_ref imported_die = NULL;
20108 dw_die_ref at_import_die;
20110 if (TREE_CODE (decl) == IMPORTED_DECL)
20112 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
20113 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
20114 gcc_assert (decl);
20116 else
20117 xloc = expand_location (input_location);
20119 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
20121 at_import_die = force_type_die (TREE_TYPE (decl));
20122 /* For namespace N { typedef void T; } using N::T; base_type_die
20123 returns NULL, but DW_TAG_imported_declaration requires
20124 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
20125 if (!at_import_die)
20127 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
20128 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
20129 at_import_die = lookup_type_die (TREE_TYPE (decl));
20130 gcc_assert (at_import_die);
20133 else
20135 at_import_die = lookup_decl_die (decl);
20136 if (!at_import_die)
20138 /* If we're trying to avoid duplicate debug info, we may not have
20139 emitted the member decl for this field. Emit it now. */
20140 if (TREE_CODE (decl) == FIELD_DECL)
20142 tree type = DECL_CONTEXT (decl);
20144 if (TYPE_CONTEXT (type)
20145 && TYPE_P (TYPE_CONTEXT (type))
20146 && !should_emit_struct_debug (TYPE_CONTEXT (type),
20147 DINFO_USAGE_DIR_USE))
20148 return;
20149 gen_type_die_for_member (type, decl,
20150 get_context_die (TYPE_CONTEXT (type)));
20152 at_import_die = force_decl_die (decl);
20156 if (TREE_CODE (decl) == NAMESPACE_DECL)
20158 if (dwarf_version >= 3 || !dwarf_strict)
20159 imported_die = new_die (DW_TAG_imported_module,
20160 lexical_block_die,
20161 lexical_block);
20162 else
20163 return;
20165 else
20166 imported_die = new_die (DW_TAG_imported_declaration,
20167 lexical_block_die,
20168 lexical_block);
20170 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
20171 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
20172 if (name)
20173 add_AT_string (imported_die, DW_AT_name,
20174 IDENTIFIER_POINTER (name));
20175 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
20178 /* Output debug information for imported module or decl DECL.
20179 NAME is non-NULL name in context if the decl has been renamed.
20180 CHILD is true if decl is one of the renamed decls as part of
20181 importing whole module. */
20183 static void
20184 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
20185 bool child)
20187 /* dw_die_ref at_import_die; */
20188 dw_die_ref scope_die;
20190 if (debug_info_level <= DINFO_LEVEL_TERSE)
20191 return;
20193 gcc_assert (decl);
20195 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
20196 We need decl DIE for reference and scope die. First, get DIE for the decl
20197 itself. */
20199 /* Get the scope die for decl context. Use comp_unit_die for global module
20200 or decl. If die is not found for non globals, force new die. */
20201 if (context
20202 && TYPE_P (context)
20203 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
20204 return;
20206 if (!(dwarf_version >= 3 || !dwarf_strict))
20207 return;
20209 scope_die = get_context_die (context);
20211 if (child)
20213 gcc_assert (scope_die->die_child);
20214 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
20215 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
20216 scope_die = scope_die->die_child;
20219 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
20220 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
20224 /* Write the debugging output for DECL. */
20226 void
20227 dwarf2out_decl (tree decl)
20229 dw_die_ref context_die = comp_unit_die ();
20231 switch (TREE_CODE (decl))
20233 case ERROR_MARK:
20234 return;
20236 case FUNCTION_DECL:
20237 /* What we would really like to do here is to filter out all mere
20238 file-scope declarations of file-scope functions which are never
20239 referenced later within this translation unit (and keep all of ones
20240 that *are* referenced later on) but we aren't clairvoyant, so we have
20241 no idea which functions will be referenced in the future (i.e. later
20242 on within the current translation unit). So here we just ignore all
20243 file-scope function declarations which are not also definitions. If
20244 and when the debugger needs to know something about these functions,
20245 it will have to hunt around and find the DWARF information associated
20246 with the definition of the function.
20248 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
20249 nodes represent definitions and which ones represent mere
20250 declarations. We have to check DECL_INITIAL instead. That's because
20251 the C front-end supports some weird semantics for "extern inline"
20252 function definitions. These can get inlined within the current
20253 translation unit (and thus, we need to generate Dwarf info for their
20254 abstract instances so that the Dwarf info for the concrete inlined
20255 instances can have something to refer to) but the compiler never
20256 generates any out-of-lines instances of such things (despite the fact
20257 that they *are* definitions).
20259 The important point is that the C front-end marks these "extern
20260 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
20261 them anyway. Note that the C++ front-end also plays some similar games
20262 for inline function definitions appearing within include files which
20263 also contain `#pragma interface' pragmas.
20265 If we are called from dwarf2out_abstract_function output a DIE
20266 anyway. We can end up here this way with early inlining and LTO
20267 where the inlined function is output in a different LTRANS unit
20268 or not at all. */
20269 if (DECL_INITIAL (decl) == NULL_TREE
20270 && ! DECL_ABSTRACT (decl))
20271 return;
20273 /* If we're a nested function, initially use a parent of NULL; if we're
20274 a plain function, this will be fixed up in decls_for_scope. If
20275 we're a method, it will be ignored, since we already have a DIE. */
20276 if (decl_function_context (decl)
20277 /* But if we're in terse mode, we don't care about scope. */
20278 && debug_info_level > DINFO_LEVEL_TERSE)
20279 context_die = NULL;
20280 break;
20282 case VAR_DECL:
20283 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
20284 declaration and if the declaration was never even referenced from
20285 within this entire compilation unit. We suppress these DIEs in
20286 order to save space in the .debug section (by eliminating entries
20287 which are probably useless). Note that we must not suppress
20288 block-local extern declarations (whether used or not) because that
20289 would screw-up the debugger's name lookup mechanism and cause it to
20290 miss things which really ought to be in scope at a given point. */
20291 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
20292 return;
20294 /* For local statics lookup proper context die. */
20295 if (TREE_STATIC (decl)
20296 && DECL_CONTEXT (decl)
20297 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
20298 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20300 /* If we are in terse mode, don't generate any DIEs to represent any
20301 variable declarations or definitions. */
20302 if (debug_info_level <= DINFO_LEVEL_TERSE)
20303 return;
20304 break;
20306 case CONST_DECL:
20307 if (debug_info_level <= DINFO_LEVEL_TERSE)
20308 return;
20309 if (!is_fortran () && !is_ada ())
20310 return;
20311 if (TREE_STATIC (decl) && decl_function_context (decl))
20312 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20313 break;
20315 case NAMESPACE_DECL:
20316 case IMPORTED_DECL:
20317 if (debug_info_level <= DINFO_LEVEL_TERSE)
20318 return;
20319 if (lookup_decl_die (decl) != NULL)
20320 return;
20321 break;
20323 case TYPE_DECL:
20324 /* Don't emit stubs for types unless they are needed by other DIEs. */
20325 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
20326 return;
20328 /* Don't bother trying to generate any DIEs to represent any of the
20329 normal built-in types for the language we are compiling. */
20330 if (DECL_IS_BUILTIN (decl))
20331 return;
20333 /* If we are in terse mode, don't generate any DIEs for types. */
20334 if (debug_info_level <= DINFO_LEVEL_TERSE)
20335 return;
20337 /* If we're a function-scope tag, initially use a parent of NULL;
20338 this will be fixed up in decls_for_scope. */
20339 if (decl_function_context (decl))
20340 context_die = NULL;
20342 break;
20344 default:
20345 return;
20348 gen_decl_die (decl, NULL, context_die);
20351 /* Write the debugging output for DECL. */
20353 static void
20354 dwarf2out_function_decl (tree decl)
20356 dwarf2out_decl (decl);
20357 call_arg_locations = NULL;
20358 call_arg_loc_last = NULL;
20359 call_site_count = -1;
20360 tail_call_site_count = -1;
20361 block_map.release ();
20362 htab_empty (decl_loc_table);
20363 htab_empty (cached_dw_loc_list_table);
20366 /* Output a marker (i.e. a label) for the beginning of the generated code for
20367 a lexical block. */
20369 static void
20370 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
20371 unsigned int blocknum)
20373 switch_to_section (current_function_section ());
20374 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
20377 /* Output a marker (i.e. a label) for the end of the generated code for a
20378 lexical block. */
20380 static void
20381 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
20383 switch_to_section (current_function_section ());
20384 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
20387 /* Returns nonzero if it is appropriate not to emit any debugging
20388 information for BLOCK, because it doesn't contain any instructions.
20390 Don't allow this for blocks with nested functions or local classes
20391 as we would end up with orphans, and in the presence of scheduling
20392 we may end up calling them anyway. */
20394 static bool
20395 dwarf2out_ignore_block (const_tree block)
20397 tree decl;
20398 unsigned int i;
20400 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
20401 if (TREE_CODE (decl) == FUNCTION_DECL
20402 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20403 return 0;
20404 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
20406 decl = BLOCK_NONLOCALIZED_VAR (block, i);
20407 if (TREE_CODE (decl) == FUNCTION_DECL
20408 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20409 return 0;
20412 return 1;
20415 /* Hash table routines for file_hash. */
20417 static int
20418 file_table_eq (const void *p1_p, const void *p2_p)
20420 const struct dwarf_file_data *const p1 =
20421 (const struct dwarf_file_data *) p1_p;
20422 const char *const p2 = (const char *) p2_p;
20423 return filename_cmp (p1->filename, p2) == 0;
20426 static hashval_t
20427 file_table_hash (const void *p_p)
20429 const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
20430 return htab_hash_string (p->filename);
20433 /* Lookup FILE_NAME (in the list of filenames that we know about here in
20434 dwarf2out.c) and return its "index". The index of each (known) filename is
20435 just a unique number which is associated with only that one filename. We
20436 need such numbers for the sake of generating labels (in the .debug_sfnames
20437 section) and references to those files numbers (in the .debug_srcinfo
20438 and.debug_macinfo sections). If the filename given as an argument is not
20439 found in our current list, add it to the list and assign it the next
20440 available unique index number. In order to speed up searches, we remember
20441 the index of the filename was looked up last. This handles the majority of
20442 all searches. */
20444 static struct dwarf_file_data *
20445 lookup_filename (const char *file_name)
20447 void ** slot;
20448 struct dwarf_file_data * created;
20450 /* Check to see if the file name that was searched on the previous
20451 call matches this file name. If so, return the index. */
20452 if (file_table_last_lookup
20453 && (file_name == file_table_last_lookup->filename
20454 || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
20455 return file_table_last_lookup;
20457 /* Didn't match the previous lookup, search the table. */
20458 slot = htab_find_slot_with_hash (file_table, file_name,
20459 htab_hash_string (file_name), INSERT);
20460 if (*slot)
20461 return (struct dwarf_file_data *) *slot;
20463 created = ggc_alloc_dwarf_file_data ();
20464 created->filename = file_name;
20465 created->emitted_number = 0;
20466 *slot = created;
20467 return created;
20470 /* If the assembler will construct the file table, then translate the compiler
20471 internal file table number into the assembler file table number, and emit
20472 a .file directive if we haven't already emitted one yet. The file table
20473 numbers are different because we prune debug info for unused variables and
20474 types, which may include filenames. */
20476 static int
20477 maybe_emit_file (struct dwarf_file_data * fd)
20479 if (! fd->emitted_number)
20481 if (last_emitted_file)
20482 fd->emitted_number = last_emitted_file->emitted_number + 1;
20483 else
20484 fd->emitted_number = 1;
20485 last_emitted_file = fd;
20487 if (DWARF2_ASM_LINE_DEBUG_INFO)
20489 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
20490 output_quoted_string (asm_out_file,
20491 remap_debug_filename (fd->filename));
20492 fputc ('\n', asm_out_file);
20496 return fd->emitted_number;
20499 /* Schedule generation of a DW_AT_const_value attribute to DIE.
20500 That generation should happen after function debug info has been
20501 generated. The value of the attribute is the constant value of ARG. */
20503 static void
20504 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
20506 die_arg_entry entry;
20508 if (!die || !arg)
20509 return;
20511 if (!tmpl_value_parm_die_table)
20512 vec_alloc (tmpl_value_parm_die_table, 32);
20514 entry.die = die;
20515 entry.arg = arg;
20516 vec_safe_push (tmpl_value_parm_die_table, entry);
20519 /* Return TRUE if T is an instance of generic type, FALSE
20520 otherwise. */
20522 static bool
20523 generic_type_p (tree t)
20525 if (t == NULL_TREE || !TYPE_P (t))
20526 return false;
20527 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
20530 /* Schedule the generation of the generic parameter dies for the
20531 instance of generic type T. The proper generation itself is later
20532 done by gen_scheduled_generic_parms_dies. */
20534 static void
20535 schedule_generic_params_dies_gen (tree t)
20537 if (!generic_type_p (t))
20538 return;
20540 if (!generic_type_instances)
20541 vec_alloc (generic_type_instances, 256);
20543 vec_safe_push (generic_type_instances, t);
20546 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
20547 by append_entry_to_tmpl_value_parm_die_table. This function must
20548 be called after function DIEs have been generated. */
20550 static void
20551 gen_remaining_tmpl_value_param_die_attribute (void)
20553 if (tmpl_value_parm_die_table)
20555 unsigned i;
20556 die_arg_entry *e;
20558 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
20559 tree_add_const_value_attribute (e->die, e->arg);
20563 /* Generate generic parameters DIEs for instances of generic types
20564 that have been previously scheduled by
20565 schedule_generic_params_dies_gen. This function must be called
20566 after all the types of the CU have been laid out. */
20568 static void
20569 gen_scheduled_generic_parms_dies (void)
20571 unsigned i;
20572 tree t;
20574 if (!generic_type_instances)
20575 return;
20577 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
20578 gen_generic_params_dies (t);
20582 /* Replace DW_AT_name for the decl with name. */
20584 static void
20585 dwarf2out_set_name (tree decl, tree name)
20587 dw_die_ref die;
20588 dw_attr_ref attr;
20589 const char *dname;
20591 die = TYPE_SYMTAB_DIE (decl);
20592 if (!die)
20593 return;
20595 dname = dwarf2_name (name, 0);
20596 if (!dname)
20597 return;
20599 attr = get_AT (die, DW_AT_name);
20600 if (attr)
20602 struct indirect_string_node *node;
20604 node = find_AT_string (dname);
20605 /* replace the string. */
20606 attr->dw_attr_val.v.val_str = node;
20609 else
20610 add_name_attribute (die, dname);
20613 /* Called by the final INSN scan whenever we see a var location. We
20614 use it to drop labels in the right places, and throw the location in
20615 our lookup table. */
20617 static void
20618 dwarf2out_var_location (rtx loc_note)
20620 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
20621 struct var_loc_node *newloc;
20622 rtx next_real, next_note;
20623 static const char *last_label;
20624 static const char *last_postcall_label;
20625 static bool last_in_cold_section_p;
20626 static rtx expected_next_loc_note;
20627 tree decl;
20628 bool var_loc_p;
20630 if (!NOTE_P (loc_note))
20632 if (CALL_P (loc_note))
20634 call_site_count++;
20635 if (SIBLING_CALL_P (loc_note))
20636 tail_call_site_count++;
20638 return;
20641 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
20642 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
20643 return;
20645 /* Optimize processing a large consecutive sequence of location
20646 notes so we don't spend too much time in next_real_insn. If the
20647 next insn is another location note, remember the next_real_insn
20648 calculation for next time. */
20649 next_real = cached_next_real_insn;
20650 if (next_real)
20652 if (expected_next_loc_note != loc_note)
20653 next_real = NULL_RTX;
20656 next_note = NEXT_INSN (loc_note);
20657 if (! next_note
20658 || INSN_DELETED_P (next_note)
20659 || GET_CODE (next_note) != NOTE
20660 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
20661 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
20662 next_note = NULL_RTX;
20664 if (! next_real)
20665 next_real = next_real_insn (loc_note);
20667 if (next_note)
20669 expected_next_loc_note = next_note;
20670 cached_next_real_insn = next_real;
20672 else
20673 cached_next_real_insn = NULL_RTX;
20675 /* If there are no instructions which would be affected by this note,
20676 don't do anything. */
20677 if (var_loc_p
20678 && next_real == NULL_RTX
20679 && !NOTE_DURING_CALL_P (loc_note))
20680 return;
20682 if (next_real == NULL_RTX)
20683 next_real = get_last_insn ();
20685 /* If there were any real insns between note we processed last time
20686 and this note (or if it is the first note), clear
20687 last_{,postcall_}label so that they are not reused this time. */
20688 if (last_var_location_insn == NULL_RTX
20689 || last_var_location_insn != next_real
20690 || last_in_cold_section_p != in_cold_section_p)
20692 last_label = NULL;
20693 last_postcall_label = NULL;
20696 if (var_loc_p)
20698 decl = NOTE_VAR_LOCATION_DECL (loc_note);
20699 newloc = add_var_loc_to_decl (decl, loc_note,
20700 NOTE_DURING_CALL_P (loc_note)
20701 ? last_postcall_label : last_label);
20702 if (newloc == NULL)
20703 return;
20705 else
20707 decl = NULL_TREE;
20708 newloc = NULL;
20711 /* If there were no real insns between note we processed last time
20712 and this note, use the label we emitted last time. Otherwise
20713 create a new label and emit it. */
20714 if (last_label == NULL)
20716 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
20717 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
20718 loclabel_num++;
20719 last_label = ggc_strdup (loclabel);
20722 if (!var_loc_p)
20724 struct call_arg_loc_node *ca_loc
20725 = ggc_alloc_cleared_call_arg_loc_node ();
20726 rtx prev = prev_real_insn (loc_note), x;
20727 ca_loc->call_arg_loc_note = loc_note;
20728 ca_loc->next = NULL;
20729 ca_loc->label = last_label;
20730 gcc_assert (prev
20731 && (CALL_P (prev)
20732 || (NONJUMP_INSN_P (prev)
20733 && GET_CODE (PATTERN (prev)) == SEQUENCE
20734 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
20735 if (!CALL_P (prev))
20736 prev = XVECEXP (PATTERN (prev), 0, 0);
20737 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
20738 x = get_call_rtx_from (PATTERN (prev));
20739 if (x)
20741 x = XEXP (XEXP (x, 0), 0);
20742 if (GET_CODE (x) == SYMBOL_REF
20743 && SYMBOL_REF_DECL (x)
20744 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
20745 ca_loc->symbol_ref = x;
20747 ca_loc->block = insn_scope (prev);
20748 if (call_arg_locations)
20749 call_arg_loc_last->next = ca_loc;
20750 else
20751 call_arg_locations = ca_loc;
20752 call_arg_loc_last = ca_loc;
20754 else if (!NOTE_DURING_CALL_P (loc_note))
20755 newloc->label = last_label;
20756 else
20758 if (!last_postcall_label)
20760 sprintf (loclabel, "%s-1", last_label);
20761 last_postcall_label = ggc_strdup (loclabel);
20763 newloc->label = last_postcall_label;
20766 last_var_location_insn = next_real;
20767 last_in_cold_section_p = in_cold_section_p;
20770 /* Note in one location list that text section has changed. */
20772 static int
20773 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
20775 var_loc_list *list = (var_loc_list *) *slot;
20776 if (list->first)
20777 list->last_before_switch
20778 = list->last->next ? list->last->next : list->last;
20779 return 1;
20782 /* Note in all location lists that text section has changed. */
20784 static void
20785 var_location_switch_text_section (void)
20787 if (decl_loc_table == NULL)
20788 return;
20790 htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
20793 /* Create a new line number table. */
20795 static dw_line_info_table *
20796 new_line_info_table (void)
20798 dw_line_info_table *table;
20800 table = ggc_alloc_cleared_dw_line_info_table_struct ();
20801 table->file_num = 1;
20802 table->line_num = 1;
20803 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
20805 return table;
20808 /* Lookup the "current" table into which we emit line info, so
20809 that we don't have to do it for every source line. */
20811 static void
20812 set_cur_line_info_table (section *sec)
20814 dw_line_info_table *table;
20816 if (sec == text_section)
20817 table = text_section_line_info;
20818 else if (sec == cold_text_section)
20820 table = cold_text_section_line_info;
20821 if (!table)
20823 cold_text_section_line_info = table = new_line_info_table ();
20824 table->end_label = cold_end_label;
20827 else
20829 const char *end_label;
20831 if (flag_reorder_blocks_and_partition)
20833 if (in_cold_section_p)
20834 end_label = crtl->subsections.cold_section_end_label;
20835 else
20836 end_label = crtl->subsections.hot_section_end_label;
20838 else
20840 char label[MAX_ARTIFICIAL_LABEL_BYTES];
20841 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
20842 current_function_funcdef_no);
20843 end_label = ggc_strdup (label);
20846 table = new_line_info_table ();
20847 table->end_label = end_label;
20849 vec_safe_push (separate_line_info, table);
20852 if (DWARF2_ASM_LINE_DEBUG_INFO)
20853 table->is_stmt = (cur_line_info_table
20854 ? cur_line_info_table->is_stmt
20855 : DWARF_LINE_DEFAULT_IS_STMT_START);
20856 cur_line_info_table = table;
20860 /* We need to reset the locations at the beginning of each
20861 function. We can't do this in the end_function hook, because the
20862 declarations that use the locations won't have been output when
20863 that hook is called. Also compute have_multiple_function_sections here. */
20865 static void
20866 dwarf2out_begin_function (tree fun)
20868 section *sec = function_section (fun);
20870 if (sec != text_section)
20871 have_multiple_function_sections = true;
20873 if (flag_reorder_blocks_and_partition && !cold_text_section)
20875 gcc_assert (current_function_decl == fun);
20876 cold_text_section = unlikely_text_section ();
20877 switch_to_section (cold_text_section);
20878 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
20879 switch_to_section (sec);
20882 dwarf2out_note_section_used ();
20883 call_site_count = 0;
20884 tail_call_site_count = 0;
20886 set_cur_line_info_table (sec);
20889 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
20891 static void
20892 push_dw_line_info_entry (dw_line_info_table *table,
20893 enum dw_line_info_opcode opcode, unsigned int val)
20895 dw_line_info_entry e;
20896 e.opcode = opcode;
20897 e.val = val;
20898 vec_safe_push (table->entries, e);
20901 /* Output a label to mark the beginning of a source code line entry
20902 and record information relating to this source line, in
20903 'line_info_table' for later output of the .debug_line section. */
20904 /* ??? The discriminator parameter ought to be unsigned. */
20906 static void
20907 dwarf2out_source_line (unsigned int line, const char *filename,
20908 int discriminator, bool is_stmt)
20910 unsigned int file_num;
20911 dw_line_info_table *table;
20913 if (debug_info_level < DINFO_LEVEL_NORMAL || line == 0)
20914 return;
20916 /* The discriminator column was added in dwarf4. Simplify the below
20917 by simply removing it if we're not supposed to output it. */
20918 if (dwarf_version < 4 && dwarf_strict)
20919 discriminator = 0;
20921 table = cur_line_info_table;
20922 file_num = maybe_emit_file (lookup_filename (filename));
20924 /* ??? TODO: Elide duplicate line number entries. Traditionally,
20925 the debugger has used the second (possibly duplicate) line number
20926 at the beginning of the function to mark the end of the prologue.
20927 We could eliminate any other duplicates within the function. For
20928 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
20929 that second line number entry. */
20930 /* Recall that this end-of-prologue indication is *not* the same thing
20931 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
20932 to which the hook corresponds, follows the last insn that was
20933 emitted by gen_prologue. What we need is to precede the first insn
20934 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
20935 insn that corresponds to something the user wrote. These may be
20936 very different locations once scheduling is enabled. */
20938 if (0 && file_num == table->file_num
20939 && line == table->line_num
20940 && discriminator == table->discrim_num
20941 && is_stmt == table->is_stmt)
20942 return;
20944 switch_to_section (current_function_section ());
20946 /* If requested, emit something human-readable. */
20947 if (flag_debug_asm)
20948 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
20950 if (DWARF2_ASM_LINE_DEBUG_INFO)
20952 /* Emit the .loc directive understood by GNU as. */
20953 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
20954 file_num, line, is_stmt, discriminator */
20955 fputs ("\t.loc ", asm_out_file);
20956 fprint_ul (asm_out_file, file_num);
20957 putc (' ', asm_out_file);
20958 fprint_ul (asm_out_file, line);
20959 putc (' ', asm_out_file);
20960 putc ('0', asm_out_file);
20962 if (is_stmt != table->is_stmt)
20964 fputs (" is_stmt ", asm_out_file);
20965 putc (is_stmt ? '1' : '0', asm_out_file);
20967 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
20969 gcc_assert (discriminator > 0);
20970 fputs (" discriminator ", asm_out_file);
20971 fprint_ul (asm_out_file, (unsigned long) discriminator);
20973 putc ('\n', asm_out_file);
20975 else
20977 unsigned int label_num = ++line_info_label_num;
20979 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
20981 push_dw_line_info_entry (table, LI_set_address, label_num);
20982 if (file_num != table->file_num)
20983 push_dw_line_info_entry (table, LI_set_file, file_num);
20984 if (discriminator != table->discrim_num)
20985 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
20986 if (is_stmt != table->is_stmt)
20987 push_dw_line_info_entry (table, LI_negate_stmt, 0);
20988 push_dw_line_info_entry (table, LI_set_line, line);
20991 table->file_num = file_num;
20992 table->line_num = line;
20993 table->discrim_num = discriminator;
20994 table->is_stmt = is_stmt;
20995 table->in_use = true;
20998 /* Record the beginning of a new source file. */
21000 static void
21001 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
21003 if (flag_eliminate_dwarf2_dups)
21005 /* Record the beginning of the file for break_out_includes. */
21006 dw_die_ref bincl_die;
21008 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
21009 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
21012 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21014 macinfo_entry e;
21015 e.code = DW_MACINFO_start_file;
21016 e.lineno = lineno;
21017 e.info = ggc_strdup (filename);
21018 vec_safe_push (macinfo_table, e);
21022 /* Record the end of a source file. */
21024 static void
21025 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
21027 if (flag_eliminate_dwarf2_dups)
21028 /* Record the end of the file for break_out_includes. */
21029 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
21031 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21033 macinfo_entry e;
21034 e.code = DW_MACINFO_end_file;
21035 e.lineno = lineno;
21036 e.info = NULL;
21037 vec_safe_push (macinfo_table, e);
21041 /* Called from debug_define in toplev.c. The `buffer' parameter contains
21042 the tail part of the directive line, i.e. the part which is past the
21043 initial whitespace, #, whitespace, directive-name, whitespace part. */
21045 static void
21046 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
21047 const char *buffer ATTRIBUTE_UNUSED)
21049 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21051 macinfo_entry e;
21052 /* Insert a dummy first entry to be able to optimize the whole
21053 predefined macro block using DW_MACRO_GNU_transparent_include. */
21054 if (macinfo_table->is_empty () && lineno <= 1)
21056 e.code = 0;
21057 e.lineno = 0;
21058 e.info = NULL;
21059 vec_safe_push (macinfo_table, e);
21061 e.code = DW_MACINFO_define;
21062 e.lineno = lineno;
21063 e.info = ggc_strdup (buffer);
21064 vec_safe_push (macinfo_table, e);
21068 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
21069 the tail part of the directive line, i.e. the part which is past the
21070 initial whitespace, #, whitespace, directive-name, whitespace part. */
21072 static void
21073 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
21074 const char *buffer ATTRIBUTE_UNUSED)
21076 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21078 macinfo_entry e;
21079 /* Insert a dummy first entry to be able to optimize the whole
21080 predefined macro block using DW_MACRO_GNU_transparent_include. */
21081 if (macinfo_table->is_empty () && lineno <= 1)
21083 e.code = 0;
21084 e.lineno = 0;
21085 e.info = NULL;
21086 vec_safe_push (macinfo_table, e);
21088 e.code = DW_MACINFO_undef;
21089 e.lineno = lineno;
21090 e.info = ggc_strdup (buffer);
21091 vec_safe_push (macinfo_table, e);
21095 /* Routines to manipulate hash table of CUs. */
21097 static hashval_t
21098 htab_macinfo_hash (const void *of)
21100 const macinfo_entry *const entry =
21101 (const macinfo_entry *) of;
21103 return htab_hash_string (entry->info);
21106 static int
21107 htab_macinfo_eq (const void *of1, const void *of2)
21109 const macinfo_entry *const entry1 = (const macinfo_entry *) of1;
21110 const macinfo_entry *const entry2 = (const macinfo_entry *) of2;
21112 return !strcmp (entry1->info, entry2->info);
21115 /* Output a single .debug_macinfo entry. */
21117 static void
21118 output_macinfo_op (macinfo_entry *ref)
21120 int file_num;
21121 size_t len;
21122 struct indirect_string_node *node;
21123 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21124 struct dwarf_file_data *fd;
21126 switch (ref->code)
21128 case DW_MACINFO_start_file:
21129 fd = lookup_filename (ref->info);
21130 file_num = maybe_emit_file (fd);
21131 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
21132 dw2_asm_output_data_uleb128 (ref->lineno,
21133 "Included from line number %lu",
21134 (unsigned long) ref->lineno);
21135 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
21136 break;
21137 case DW_MACINFO_end_file:
21138 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
21139 break;
21140 case DW_MACINFO_define:
21141 case DW_MACINFO_undef:
21142 len = strlen (ref->info) + 1;
21143 if (!dwarf_strict
21144 && len > DWARF_OFFSET_SIZE
21145 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21146 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21148 ref->code = ref->code == DW_MACINFO_define
21149 ? DW_MACRO_GNU_define_indirect
21150 : DW_MACRO_GNU_undef_indirect;
21151 output_macinfo_op (ref);
21152 return;
21154 dw2_asm_output_data (1, ref->code,
21155 ref->code == DW_MACINFO_define
21156 ? "Define macro" : "Undefine macro");
21157 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21158 (unsigned long) ref->lineno);
21159 dw2_asm_output_nstring (ref->info, -1, "The macro");
21160 break;
21161 case DW_MACRO_GNU_define_indirect:
21162 case DW_MACRO_GNU_undef_indirect:
21163 node = find_AT_string (ref->info);
21164 gcc_assert (node
21165 && ((node->form == DW_FORM_strp)
21166 || (node->form == DW_FORM_GNU_str_index)));
21167 dw2_asm_output_data (1, ref->code,
21168 ref->code == DW_MACRO_GNU_define_indirect
21169 ? "Define macro indirect"
21170 : "Undefine macro indirect");
21171 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21172 (unsigned long) ref->lineno);
21173 if (node->form == DW_FORM_strp)
21174 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
21175 debug_str_section, "The macro: \"%s\"",
21176 ref->info);
21177 else
21178 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
21179 ref->info);
21180 break;
21181 case DW_MACRO_GNU_transparent_include:
21182 dw2_asm_output_data (1, ref->code, "Transparent include");
21183 ASM_GENERATE_INTERNAL_LABEL (label,
21184 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
21185 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
21186 break;
21187 default:
21188 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
21189 ASM_COMMENT_START, (unsigned long) ref->code);
21190 break;
21194 /* Attempt to make a sequence of define/undef macinfo ops shareable with
21195 other compilation unit .debug_macinfo sections. IDX is the first
21196 index of a define/undef, return the number of ops that should be
21197 emitted in a comdat .debug_macinfo section and emit
21198 a DW_MACRO_GNU_transparent_include entry referencing it.
21199 If the define/undef entry should be emitted normally, return 0. */
21201 static unsigned
21202 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
21203 htab_t *macinfo_htab)
21205 macinfo_entry *first, *second, *cur, *inc;
21206 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
21207 unsigned char checksum[16];
21208 struct md5_ctx ctx;
21209 char *grp_name, *tail;
21210 const char *base;
21211 unsigned int i, count, encoded_filename_len, linebuf_len;
21212 void **slot;
21214 first = &(*macinfo_table)[idx];
21215 second = &(*macinfo_table)[idx + 1];
21217 /* Optimize only if there are at least two consecutive define/undef ops,
21218 and either all of them are before first DW_MACINFO_start_file
21219 with lineno {0,1} (i.e. predefined macro block), or all of them are
21220 in some included header file. */
21221 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
21222 return 0;
21223 if (vec_safe_is_empty (files))
21225 if (first->lineno > 1 || second->lineno > 1)
21226 return 0;
21228 else if (first->lineno == 0)
21229 return 0;
21231 /* Find the last define/undef entry that can be grouped together
21232 with first and at the same time compute md5 checksum of their
21233 codes, linenumbers and strings. */
21234 md5_init_ctx (&ctx);
21235 for (i = idx; macinfo_table->iterate (i, &cur); i++)
21236 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
21237 break;
21238 else if (vec_safe_is_empty (files) && cur->lineno > 1)
21239 break;
21240 else
21242 unsigned char code = cur->code;
21243 md5_process_bytes (&code, 1, &ctx);
21244 checksum_uleb128 (cur->lineno, &ctx);
21245 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
21247 md5_finish_ctx (&ctx, checksum);
21248 count = i - idx;
21250 /* From the containing include filename (if any) pick up just
21251 usable characters from its basename. */
21252 if (vec_safe_is_empty (files))
21253 base = "";
21254 else
21255 base = lbasename (files->last ().info);
21256 for (encoded_filename_len = 0, i = 0; base[i]; i++)
21257 if (ISIDNUM (base[i]) || base[i] == '.')
21258 encoded_filename_len++;
21259 /* Count . at the end. */
21260 if (encoded_filename_len)
21261 encoded_filename_len++;
21263 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
21264 linebuf_len = strlen (linebuf);
21266 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
21267 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
21268 + 16 * 2 + 1);
21269 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
21270 tail = grp_name + 4;
21271 if (encoded_filename_len)
21273 for (i = 0; base[i]; i++)
21274 if (ISIDNUM (base[i]) || base[i] == '.')
21275 *tail++ = base[i];
21276 *tail++ = '.';
21278 memcpy (tail, linebuf, linebuf_len);
21279 tail += linebuf_len;
21280 *tail++ = '.';
21281 for (i = 0; i < 16; i++)
21282 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
21284 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
21285 in the empty vector entry before the first define/undef. */
21286 inc = &(*macinfo_table)[idx - 1];
21287 inc->code = DW_MACRO_GNU_transparent_include;
21288 inc->lineno = 0;
21289 inc->info = ggc_strdup (grp_name);
21290 if (*macinfo_htab == NULL)
21291 *macinfo_htab = htab_create (10, htab_macinfo_hash, htab_macinfo_eq, NULL);
21292 /* Avoid emitting duplicates. */
21293 slot = htab_find_slot (*macinfo_htab, inc, INSERT);
21294 if (*slot != NULL)
21296 inc->code = 0;
21297 inc->info = NULL;
21298 /* If such an entry has been used before, just emit
21299 a DW_MACRO_GNU_transparent_include op. */
21300 inc = (macinfo_entry *) *slot;
21301 output_macinfo_op (inc);
21302 /* And clear all macinfo_entry in the range to avoid emitting them
21303 in the second pass. */
21304 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
21306 cur->code = 0;
21307 cur->info = NULL;
21310 else
21312 *slot = inc;
21313 inc->lineno = htab_elements (*macinfo_htab);
21314 output_macinfo_op (inc);
21316 return count;
21319 /* Save any strings needed by the macinfo table in the debug str
21320 table. All strings must be collected into the table by the time
21321 index_string is called. */
21323 static void
21324 save_macinfo_strings (void)
21326 unsigned len;
21327 unsigned i;
21328 macinfo_entry *ref;
21330 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
21332 switch (ref->code)
21334 /* Match the logic in output_macinfo_op to decide on
21335 indirect strings. */
21336 case DW_MACINFO_define:
21337 case DW_MACINFO_undef:
21338 len = strlen (ref->info) + 1;
21339 if (!dwarf_strict
21340 && len > DWARF_OFFSET_SIZE
21341 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21342 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21343 set_indirect_string (find_AT_string (ref->info));
21344 break;
21345 case DW_MACRO_GNU_define_indirect:
21346 case DW_MACRO_GNU_undef_indirect:
21347 set_indirect_string (find_AT_string (ref->info));
21348 break;
21349 default:
21350 break;
21355 /* Output macinfo section(s). */
21357 static void
21358 output_macinfo (void)
21360 unsigned i;
21361 unsigned long length = vec_safe_length (macinfo_table);
21362 macinfo_entry *ref;
21363 vec<macinfo_entry, va_gc> *files = NULL;
21364 htab_t macinfo_htab = NULL;
21366 if (! length)
21367 return;
21369 /* output_macinfo* uses these interchangeably. */
21370 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
21371 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
21372 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
21373 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
21375 /* For .debug_macro emit the section header. */
21376 if (!dwarf_strict)
21378 dw2_asm_output_data (2, 4, "DWARF macro version number");
21379 if (DWARF_OFFSET_SIZE == 8)
21380 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
21381 else
21382 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
21383 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
21384 (!dwarf_split_debug_info ? debug_line_section_label
21385 : debug_skeleton_line_section_label),
21386 debug_line_section, NULL);
21389 /* In the first loop, it emits the primary .debug_macinfo section
21390 and after each emitted op the macinfo_entry is cleared.
21391 If a longer range of define/undef ops can be optimized using
21392 DW_MACRO_GNU_transparent_include, the
21393 DW_MACRO_GNU_transparent_include op is emitted and kept in
21394 the vector before the first define/undef in the range and the
21395 whole range of define/undef ops is not emitted and kept. */
21396 for (i = 0; macinfo_table->iterate (i, &ref); i++)
21398 switch (ref->code)
21400 case DW_MACINFO_start_file:
21401 vec_safe_push (files, *ref);
21402 break;
21403 case DW_MACINFO_end_file:
21404 if (!vec_safe_is_empty (files))
21405 files->pop ();
21406 break;
21407 case DW_MACINFO_define:
21408 case DW_MACINFO_undef:
21409 if (!dwarf_strict
21410 && HAVE_COMDAT_GROUP
21411 && vec_safe_length (files) != 1
21412 && i > 0
21413 && i + 1 < length
21414 && (*macinfo_table)[i - 1].code == 0)
21416 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
21417 if (count)
21419 i += count - 1;
21420 continue;
21423 break;
21424 case 0:
21425 /* A dummy entry may be inserted at the beginning to be able
21426 to optimize the whole block of predefined macros. */
21427 if (i == 0)
21428 continue;
21429 default:
21430 break;
21432 output_macinfo_op (ref);
21433 ref->info = NULL;
21434 ref->code = 0;
21437 if (macinfo_htab == NULL)
21438 return;
21440 htab_delete (macinfo_htab);
21442 /* If any DW_MACRO_GNU_transparent_include were used, on those
21443 DW_MACRO_GNU_transparent_include entries terminate the
21444 current chain and switch to a new comdat .debug_macinfo
21445 section and emit the define/undef entries within it. */
21446 for (i = 0; macinfo_table->iterate (i, &ref); i++)
21447 switch (ref->code)
21449 case 0:
21450 continue;
21451 case DW_MACRO_GNU_transparent_include:
21453 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21454 tree comdat_key = get_identifier (ref->info);
21455 /* Terminate the previous .debug_macinfo section. */
21456 dw2_asm_output_data (1, 0, "End compilation unit");
21457 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
21458 SECTION_DEBUG
21459 | SECTION_LINKONCE,
21460 comdat_key);
21461 ASM_GENERATE_INTERNAL_LABEL (label,
21462 DEBUG_MACRO_SECTION_LABEL,
21463 ref->lineno);
21464 ASM_OUTPUT_LABEL (asm_out_file, label);
21465 ref->code = 0;
21466 ref->info = NULL;
21467 dw2_asm_output_data (2, 4, "DWARF macro version number");
21468 if (DWARF_OFFSET_SIZE == 8)
21469 dw2_asm_output_data (1, 1, "Flags: 64-bit");
21470 else
21471 dw2_asm_output_data (1, 0, "Flags: 32-bit");
21473 break;
21474 case DW_MACINFO_define:
21475 case DW_MACINFO_undef:
21476 output_macinfo_op (ref);
21477 ref->code = 0;
21478 ref->info = NULL;
21479 break;
21480 default:
21481 gcc_unreachable ();
21485 /* Set up for Dwarf output at the start of compilation. */
21487 static void
21488 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
21490 /* Allocate the file_table. */
21491 file_table = htab_create_ggc (50, file_table_hash,
21492 file_table_eq, NULL);
21494 /* Allocate the decl_die_table. */
21495 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
21496 decl_die_table_eq, NULL);
21498 /* Allocate the decl_loc_table. */
21499 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
21500 decl_loc_table_eq, NULL);
21502 /* Allocate the cached_dw_loc_list_table. */
21503 cached_dw_loc_list_table
21504 = htab_create_ggc (10, cached_dw_loc_list_table_hash,
21505 cached_dw_loc_list_table_eq, NULL);
21507 /* Allocate the initial hunk of the decl_scope_table. */
21508 vec_alloc (decl_scope_table, 256);
21510 /* Allocate the initial hunk of the abbrev_die_table. */
21511 abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
21512 (ABBREV_DIE_TABLE_INCREMENT);
21513 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
21514 /* Zero-th entry is allocated, but unused. */
21515 abbrev_die_table_in_use = 1;
21517 /* Allocate the pubtypes and pubnames vectors. */
21518 vec_alloc (pubname_table, 32);
21519 vec_alloc (pubtype_table, 32);
21521 vec_alloc (incomplete_types, 64);
21523 vec_alloc (used_rtx_array, 32);
21525 if (!dwarf_split_debug_info)
21527 debug_info_section = get_section (DEBUG_INFO_SECTION,
21528 SECTION_DEBUG, NULL);
21529 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
21530 SECTION_DEBUG, NULL);
21531 debug_loc_section = get_section (DEBUG_LOC_SECTION,
21532 SECTION_DEBUG, NULL);
21534 else
21536 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
21537 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
21538 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
21539 SECTION_DEBUG | SECTION_EXCLUDE,
21540 NULL);
21541 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
21542 SECTION_DEBUG, NULL);
21543 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
21544 SECTION_DEBUG, NULL);
21545 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
21546 SECTION_DEBUG, NULL);
21547 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
21548 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
21550 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
21551 the main .o, but the skeleton_line goes into the split off dwo. */
21552 debug_skeleton_line_section
21553 = get_section (DEBUG_DWO_LINE_SECTION,
21554 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
21555 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
21556 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
21557 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
21558 SECTION_DEBUG | SECTION_EXCLUDE,
21559 NULL);
21560 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
21561 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
21562 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
21563 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
21565 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
21566 SECTION_DEBUG, NULL);
21567 debug_macinfo_section = get_section (dwarf_strict
21568 ? DEBUG_MACINFO_SECTION
21569 : DEBUG_MACRO_SECTION,
21570 DEBUG_MACRO_SECTION_FLAGS, NULL);
21571 debug_line_section = get_section (DEBUG_LINE_SECTION,
21572 SECTION_DEBUG, NULL);
21573 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
21574 SECTION_DEBUG, NULL);
21575 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
21576 SECTION_DEBUG, NULL);
21577 debug_str_section = get_section (DEBUG_STR_SECTION,
21578 DEBUG_STR_SECTION_FLAGS, NULL);
21579 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
21580 SECTION_DEBUG, NULL);
21581 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
21582 SECTION_DEBUG, NULL);
21584 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
21585 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
21586 DEBUG_ABBREV_SECTION_LABEL, 0);
21587 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
21588 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
21589 COLD_TEXT_SECTION_LABEL, 0);
21590 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
21592 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
21593 DEBUG_INFO_SECTION_LABEL, 0);
21594 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
21595 DEBUG_LINE_SECTION_LABEL, 0);
21596 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
21597 DEBUG_RANGES_SECTION_LABEL, 0);
21598 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
21599 DEBUG_ADDR_SECTION_LABEL, 0);
21600 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
21601 dwarf_strict
21602 ? DEBUG_MACINFO_SECTION_LABEL
21603 : DEBUG_MACRO_SECTION_LABEL, 0);
21604 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
21606 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21607 vec_alloc (macinfo_table, 64);
21609 switch_to_section (text_section);
21610 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
21612 /* Make sure the line number table for .text always exists. */
21613 text_section_line_info = new_line_info_table ();
21614 text_section_line_info->end_label = text_end_label;
21617 /* Called before compile () starts outputtting functions, variables
21618 and toplevel asms into assembly. */
21620 static void
21621 dwarf2out_assembly_start (void)
21623 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
21624 && dwarf2out_do_cfi_asm ()
21625 && (!(flag_unwind_tables || flag_exceptions)
21626 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
21627 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
21630 /* A helper function for dwarf2out_finish called through
21631 htab_traverse. Assign a string its index. All strings must be
21632 collected into the table by the time index_string is called,
21633 because the indexing code relies on htab_traverse to traverse nodes
21634 in the same order for each run. */
21636 static int
21637 index_string (void **h, void *v)
21639 struct indirect_string_node *node = (struct indirect_string_node *) *h;
21640 unsigned int *index = (unsigned int *) v;
21642 find_string_form (node);
21643 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
21645 gcc_assert(node->index == NO_INDEX_ASSIGNED);
21646 node->index = *index;
21647 *index += 1;
21649 return 1;
21652 /* A helper function for output_indirect_strings called through
21653 htab_traverse. Output the offset to a string and update the
21654 current offset. */
21656 static int
21657 output_index_string_offset (void **h, void *v)
21659 struct indirect_string_node *node = (struct indirect_string_node *) *h;
21660 unsigned int *offset = (unsigned int *) v;
21662 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
21664 /* Assert that this node has been assigned an index. */
21665 gcc_assert (node->index != NO_INDEX_ASSIGNED
21666 && node->index != NOT_INDEXED);
21667 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
21668 "indexed string 0x%x: %s", node->index, node->str);
21669 *offset += strlen (node->str) + 1;
21671 return 1;
21674 /* A helper function for dwarf2out_finish called through
21675 htab_traverse. Output the indexed string. */
21677 static int
21678 output_index_string (void **h, void *v)
21680 struct indirect_string_node *node = (struct indirect_string_node *) *h;
21681 unsigned int *cur_idx = (unsigned int *) v;
21683 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
21685 /* Assert that the strings are output in the same order as their
21686 indexes were assigned. */
21687 gcc_assert (*cur_idx == node->index);
21688 ASM_OUTPUT_LABEL (asm_out_file, node->label);
21689 assemble_string (node->str, strlen (node->str) + 1);
21690 *cur_idx += 1;
21692 return 1;
21695 /* A helper function for dwarf2out_finish called through
21696 htab_traverse. Emit one queued .debug_str string. */
21698 static int
21699 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
21701 struct indirect_string_node *node = (struct indirect_string_node *) *h;
21703 if (node->form == DW_FORM_strp && node->refcount > 0)
21705 ASM_OUTPUT_LABEL (asm_out_file, node->label);
21706 assemble_string (node->str, strlen (node->str) + 1);
21709 return 1;
21712 /* Output the indexed string table. */
21714 static void
21715 output_indirect_strings (void)
21717 if (!dwarf_split_debug_info)
21719 switch_to_section (debug_str_section);
21720 htab_traverse (debug_str_hash, output_indirect_string, NULL);
21722 else
21724 unsigned int offset = 0;
21725 unsigned int cur_idx = 0;
21727 switch_to_section (debug_str_offsets_section);
21728 htab_traverse_noresize (debug_str_hash,
21729 output_index_string_offset,
21730 &offset);
21731 switch_to_section (debug_str_section);
21732 htab_traverse_noresize (debug_str_hash,
21733 output_index_string,
21734 &cur_idx);
21738 /* Callback for htab_traverse to assign an index to an entry in the
21739 table, and to write that entry to the .debug_addr section. */
21741 static int
21742 output_addr_table_entry (void **slot, void *data)
21744 addr_table_entry *entry = (addr_table_entry *) *slot;
21745 unsigned int *cur_index = (unsigned int *)data;
21747 if (entry->refcount == 0)
21749 gcc_assert (entry->index == NO_INDEX_ASSIGNED
21750 || entry->index == NOT_INDEXED);
21751 return 1;
21754 gcc_assert (entry->index == *cur_index);
21755 (*cur_index)++;
21757 switch (entry->kind)
21759 case ate_kind_rtx:
21760 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
21761 "0x%x", entry->index);
21762 break;
21763 case ate_kind_rtx_dtprel:
21764 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
21765 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
21766 DWARF2_ADDR_SIZE,
21767 entry->addr.rtl);
21768 fputc ('\n', asm_out_file);
21769 break;
21770 case ate_kind_label:
21771 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
21772 "0x%x", entry->index);
21773 break;
21774 default:
21775 gcc_unreachable ();
21777 return 1;
21780 /* Produce the .debug_addr section. */
21782 static void
21783 output_addr_table (void)
21785 unsigned int index = 0;
21786 if (addr_index_table == NULL || htab_size (addr_index_table) == 0)
21787 return;
21789 switch_to_section (debug_addr_section);
21790 htab_traverse_noresize (addr_index_table, output_addr_table_entry, &index);
21793 #if ENABLE_ASSERT_CHECKING
21794 /* Verify that all marks are clear. */
21796 static void
21797 verify_marks_clear (dw_die_ref die)
21799 dw_die_ref c;
21801 gcc_assert (! die->die_mark);
21802 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
21804 #endif /* ENABLE_ASSERT_CHECKING */
21806 /* Clear the marks for a die and its children.
21807 Be cool if the mark isn't set. */
21809 static void
21810 prune_unmark_dies (dw_die_ref die)
21812 dw_die_ref c;
21814 if (die->die_mark)
21815 die->die_mark = 0;
21816 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
21819 /* Given DIE that we're marking as used, find any other dies
21820 it references as attributes and mark them as used. */
21822 static void
21823 prune_unused_types_walk_attribs (dw_die_ref die)
21825 dw_attr_ref a;
21826 unsigned ix;
21828 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
21830 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
21832 /* A reference to another DIE.
21833 Make sure that it will get emitted.
21834 If it was broken out into a comdat group, don't follow it. */
21835 if (! AT_ref (a)->comdat_type_p
21836 || a->dw_attr == DW_AT_specification)
21837 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
21839 /* Set the string's refcount to 0 so that prune_unused_types_mark
21840 accounts properly for it. */
21841 if (AT_class (a) == dw_val_class_str)
21842 a->dw_attr_val.v.val_str->refcount = 0;
21846 /* Mark the generic parameters and arguments children DIEs of DIE. */
21848 static void
21849 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
21851 dw_die_ref c;
21853 if (die == NULL || die->die_child == NULL)
21854 return;
21855 c = die->die_child;
21858 switch (c->die_tag)
21860 case DW_TAG_template_type_param:
21861 case DW_TAG_template_value_param:
21862 case DW_TAG_GNU_template_template_param:
21863 case DW_TAG_GNU_template_parameter_pack:
21864 prune_unused_types_mark (c, 1);
21865 break;
21866 default:
21867 break;
21869 c = c->die_sib;
21870 } while (c && c != die->die_child);
21873 /* Mark DIE as being used. If DOKIDS is true, then walk down
21874 to DIE's children. */
21876 static void
21877 prune_unused_types_mark (dw_die_ref die, int dokids)
21879 dw_die_ref c;
21881 if (die->die_mark == 0)
21883 /* We haven't done this node yet. Mark it as used. */
21884 die->die_mark = 1;
21885 /* If this is the DIE of a generic type instantiation,
21886 mark the children DIEs that describe its generic parms and
21887 args. */
21888 prune_unused_types_mark_generic_parms_dies (die);
21890 /* We also have to mark its parents as used.
21891 (But we don't want to mark our parents' kids due to this.) */
21892 if (die->die_parent)
21893 prune_unused_types_mark (die->die_parent, 0);
21895 /* Mark any referenced nodes. */
21896 prune_unused_types_walk_attribs (die);
21898 /* If this node is a specification,
21899 also mark the definition, if it exists. */
21900 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
21901 prune_unused_types_mark (die->die_definition, 1);
21904 if (dokids && die->die_mark != 2)
21906 /* We need to walk the children, but haven't done so yet.
21907 Remember that we've walked the kids. */
21908 die->die_mark = 2;
21910 /* If this is an array type, we need to make sure our
21911 kids get marked, even if they're types. If we're
21912 breaking out types into comdat sections, do this
21913 for all type definitions. */
21914 if (die->die_tag == DW_TAG_array_type
21915 || (use_debug_types
21916 && is_type_die (die) && ! is_declaration_die (die)))
21917 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
21918 else
21919 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
21923 /* For local classes, look if any static member functions were emitted
21924 and if so, mark them. */
21926 static void
21927 prune_unused_types_walk_local_classes (dw_die_ref die)
21929 dw_die_ref c;
21931 if (die->die_mark == 2)
21932 return;
21934 switch (die->die_tag)
21936 case DW_TAG_structure_type:
21937 case DW_TAG_union_type:
21938 case DW_TAG_class_type:
21939 break;
21941 case DW_TAG_subprogram:
21942 if (!get_AT_flag (die, DW_AT_declaration)
21943 || die->die_definition != NULL)
21944 prune_unused_types_mark (die, 1);
21945 return;
21947 default:
21948 return;
21951 /* Mark children. */
21952 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
21955 /* Walk the tree DIE and mark types that we actually use. */
21957 static void
21958 prune_unused_types_walk (dw_die_ref die)
21960 dw_die_ref c;
21962 /* Don't do anything if this node is already marked and
21963 children have been marked as well. */
21964 if (die->die_mark == 2)
21965 return;
21967 switch (die->die_tag)
21969 case DW_TAG_structure_type:
21970 case DW_TAG_union_type:
21971 case DW_TAG_class_type:
21972 if (die->die_perennial_p)
21973 break;
21975 for (c = die->die_parent; c; c = c->die_parent)
21976 if (c->die_tag == DW_TAG_subprogram)
21977 break;
21979 /* Finding used static member functions inside of classes
21980 is needed just for local classes, because for other classes
21981 static member function DIEs with DW_AT_specification
21982 are emitted outside of the DW_TAG_*_type. If we ever change
21983 it, we'd need to call this even for non-local classes. */
21984 if (c)
21985 prune_unused_types_walk_local_classes (die);
21987 /* It's a type node --- don't mark it. */
21988 return;
21990 case DW_TAG_const_type:
21991 case DW_TAG_packed_type:
21992 case DW_TAG_pointer_type:
21993 case DW_TAG_reference_type:
21994 case DW_TAG_rvalue_reference_type:
21995 case DW_TAG_volatile_type:
21996 case DW_TAG_typedef:
21997 case DW_TAG_array_type:
21998 case DW_TAG_interface_type:
21999 case DW_TAG_friend:
22000 case DW_TAG_variant_part:
22001 case DW_TAG_enumeration_type:
22002 case DW_TAG_subroutine_type:
22003 case DW_TAG_string_type:
22004 case DW_TAG_set_type:
22005 case DW_TAG_subrange_type:
22006 case DW_TAG_ptr_to_member_type:
22007 case DW_TAG_file_type:
22008 if (die->die_perennial_p)
22009 break;
22011 /* It's a type node --- don't mark it. */
22012 return;
22014 default:
22015 /* Mark everything else. */
22016 break;
22019 if (die->die_mark == 0)
22021 die->die_mark = 1;
22023 /* Now, mark any dies referenced from here. */
22024 prune_unused_types_walk_attribs (die);
22027 die->die_mark = 2;
22029 /* Mark children. */
22030 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22033 /* Increment the string counts on strings referred to from DIE's
22034 attributes. */
22036 static void
22037 prune_unused_types_update_strings (dw_die_ref die)
22039 dw_attr_ref a;
22040 unsigned ix;
22042 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22043 if (AT_class (a) == dw_val_class_str)
22045 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22046 s->refcount++;
22047 /* Avoid unnecessarily putting strings that are used less than
22048 twice in the hash table. */
22049 if (s->refcount
22050 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22052 void ** slot;
22053 slot = htab_find_slot_with_hash (debug_str_hash, s->str,
22054 htab_hash_string (s->str),
22055 INSERT);
22056 gcc_assert (*slot == NULL);
22057 *slot = s;
22062 /* Remove from the tree DIE any dies that aren't marked. */
22064 static void
22065 prune_unused_types_prune (dw_die_ref die)
22067 dw_die_ref c;
22068 int pruned = 0;
22070 gcc_assert (die->die_mark);
22071 prune_unused_types_update_strings (die);
22073 if (! die->die_child)
22074 return;
22076 c = die->die_child;
22077 do {
22078 dw_die_ref prev = c;
22079 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
22080 if (c == die->die_child)
22082 /* No marked children between 'prev' and the end of the list. */
22083 if (prev == c)
22084 /* No marked children at all. */
22085 die->die_child = NULL;
22086 else
22088 prev->die_sib = c->die_sib;
22089 die->die_child = prev;
22091 pruned = 1;
22092 goto finished;
22095 if (c != prev->die_sib)
22097 prev->die_sib = c;
22098 pruned = 1;
22100 prune_unused_types_prune (c);
22101 } while (c != die->die_child);
22103 finished:
22104 /* If we pruned children, and this is a class, mark it as a
22105 declaration to inform debuggers that this is not a complete
22106 class definition. */
22107 if (pruned && die->die_mark == 1 && class_scope_p (die)
22108 && ! is_declaration_die (die))
22109 add_AT_flag (die, DW_AT_declaration, 1);
22112 /* Remove dies representing declarations that we never use. */
22114 static void
22115 prune_unused_types (void)
22117 unsigned int i;
22118 limbo_die_node *node;
22119 comdat_type_node *ctnode;
22120 pubname_ref pub;
22121 dw_die_ref base_type;
22123 #if ENABLE_ASSERT_CHECKING
22124 /* All the marks should already be clear. */
22125 verify_marks_clear (comp_unit_die ());
22126 for (node = limbo_die_list; node; node = node->next)
22127 verify_marks_clear (node->die);
22128 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22129 verify_marks_clear (ctnode->root_die);
22130 #endif /* ENABLE_ASSERT_CHECKING */
22132 /* Mark types that are used in global variables. */
22133 premark_types_used_by_global_vars ();
22135 /* Set the mark on nodes that are actually used. */
22136 prune_unused_types_walk (comp_unit_die ());
22137 for (node = limbo_die_list; node; node = node->next)
22138 prune_unused_types_walk (node->die);
22139 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22141 prune_unused_types_walk (ctnode->root_die);
22142 prune_unused_types_mark (ctnode->type_die, 1);
22145 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
22146 are unusual in that they are pubnames that are the children of pubtypes.
22147 They should only be marked via their parent DW_TAG_enumeration_type die,
22148 not as roots in themselves. */
22149 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
22150 if (pub->die->die_tag != DW_TAG_enumerator)
22151 prune_unused_types_mark (pub->die, 1);
22152 for (i = 0; base_types.iterate (i, &base_type); i++)
22153 prune_unused_types_mark (base_type, 1);
22155 if (debug_str_hash)
22156 htab_empty (debug_str_hash);
22157 prune_unused_types_prune (comp_unit_die ());
22158 for (node = limbo_die_list; node; node = node->next)
22159 prune_unused_types_prune (node->die);
22160 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22161 prune_unused_types_prune (ctnode->root_die);
22163 /* Leave the marks clear. */
22164 prune_unmark_dies (comp_unit_die ());
22165 for (node = limbo_die_list; node; node = node->next)
22166 prune_unmark_dies (node->die);
22167 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22168 prune_unmark_dies (ctnode->root_die);
22171 /* Set the parameter to true if there are any relative pathnames in
22172 the file table. */
22173 static int
22174 file_table_relative_p (void ** slot, void *param)
22176 bool *p = (bool *) param;
22177 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
22178 if (!IS_ABSOLUTE_PATH (d->filename))
22180 *p = true;
22181 return 0;
22183 return 1;
22186 /* Routines to manipulate hash table of comdat type units. */
22188 static hashval_t
22189 htab_ct_hash (const void *of)
22191 hashval_t h;
22192 const comdat_type_node *const type_node = (const comdat_type_node *) of;
22194 memcpy (&h, type_node->signature, sizeof (h));
22195 return h;
22198 static int
22199 htab_ct_eq (const void *of1, const void *of2)
22201 const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
22202 const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
22204 return (! memcmp (type_node_1->signature, type_node_2->signature,
22205 DWARF_TYPE_SIGNATURE_SIZE));
22208 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
22209 to the location it would have been added, should we know its
22210 DECL_ASSEMBLER_NAME when we added other attributes. This will
22211 probably improve compactness of debug info, removing equivalent
22212 abbrevs, and hide any differences caused by deferring the
22213 computation of the assembler name, triggered by e.g. PCH. */
22215 static inline void
22216 move_linkage_attr (dw_die_ref die)
22218 unsigned ix = vec_safe_length (die->die_attr);
22219 dw_attr_node linkage = (*die->die_attr)[ix - 1];
22221 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
22222 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
22224 while (--ix > 0)
22226 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
22228 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
22229 break;
22232 if (ix != vec_safe_length (die->die_attr) - 1)
22234 die->die_attr->pop ();
22235 die->die_attr->quick_insert (ix, linkage);
22239 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
22240 referenced from typed stack ops and count how often they are used. */
22242 static void
22243 mark_base_types (dw_loc_descr_ref loc)
22245 dw_die_ref base_type = NULL;
22247 for (; loc; loc = loc->dw_loc_next)
22249 switch (loc->dw_loc_opc)
22251 case DW_OP_GNU_regval_type:
22252 case DW_OP_GNU_deref_type:
22253 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
22254 break;
22255 case DW_OP_GNU_convert:
22256 case DW_OP_GNU_reinterpret:
22257 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
22258 continue;
22259 /* FALLTHRU */
22260 case DW_OP_GNU_const_type:
22261 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
22262 break;
22263 case DW_OP_GNU_entry_value:
22264 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
22265 continue;
22266 default:
22267 continue;
22269 gcc_assert (base_type->die_parent == comp_unit_die ());
22270 if (base_type->die_mark)
22271 base_type->die_mark++;
22272 else
22274 base_types.safe_push (base_type);
22275 base_type->die_mark = 1;
22280 /* Comparison function for sorting marked base types. */
22282 static int
22283 base_type_cmp (const void *x, const void *y)
22285 dw_die_ref dx = *(const dw_die_ref *) x;
22286 dw_die_ref dy = *(const dw_die_ref *) y;
22287 unsigned int byte_size1, byte_size2;
22288 unsigned int encoding1, encoding2;
22289 if (dx->die_mark > dy->die_mark)
22290 return -1;
22291 if (dx->die_mark < dy->die_mark)
22292 return 1;
22293 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
22294 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
22295 if (byte_size1 < byte_size2)
22296 return 1;
22297 if (byte_size1 > byte_size2)
22298 return -1;
22299 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
22300 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
22301 if (encoding1 < encoding2)
22302 return 1;
22303 if (encoding1 > encoding2)
22304 return -1;
22305 return 0;
22308 /* Move base types marked by mark_base_types as early as possible
22309 in the CU, sorted by decreasing usage count both to make the
22310 uleb128 references as small as possible and to make sure they
22311 will have die_offset already computed by calc_die_sizes when
22312 sizes of typed stack loc ops is computed. */
22314 static void
22315 move_marked_base_types (void)
22317 unsigned int i;
22318 dw_die_ref base_type, die, c;
22320 if (base_types.is_empty ())
22321 return;
22323 /* Sort by decreasing usage count, they will be added again in that
22324 order later on. */
22325 base_types.qsort (base_type_cmp);
22326 die = comp_unit_die ();
22327 c = die->die_child;
22330 dw_die_ref prev = c;
22331 c = c->die_sib;
22332 while (c->die_mark)
22334 remove_child_with_prev (c, prev);
22335 /* As base types got marked, there must be at least
22336 one node other than DW_TAG_base_type. */
22337 gcc_assert (c != c->die_sib);
22338 c = c->die_sib;
22341 while (c != die->die_child);
22342 gcc_assert (die->die_child);
22343 c = die->die_child;
22344 for (i = 0; base_types.iterate (i, &base_type); i++)
22346 base_type->die_mark = 0;
22347 base_type->die_sib = c->die_sib;
22348 c->die_sib = base_type;
22349 c = base_type;
22353 /* Helper function for resolve_addr, attempt to resolve
22354 one CONST_STRING, return non-zero if not successful. Similarly verify that
22355 SYMBOL_REFs refer to variables emitted in the current CU. */
22357 static int
22358 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
22360 rtx rtl = *addr;
22362 if (GET_CODE (rtl) == CONST_STRING)
22364 size_t len = strlen (XSTR (rtl, 0)) + 1;
22365 tree t = build_string (len, XSTR (rtl, 0));
22366 tree tlen = size_int (len - 1);
22367 TREE_TYPE (t)
22368 = build_array_type (char_type_node, build_index_type (tlen));
22369 rtl = lookup_constant_def (t);
22370 if (!rtl || !MEM_P (rtl))
22371 return 1;
22372 rtl = XEXP (rtl, 0);
22373 vec_safe_push (used_rtx_array, rtl);
22374 *addr = rtl;
22375 return 0;
22378 if (GET_CODE (rtl) == SYMBOL_REF
22379 && SYMBOL_REF_DECL (rtl))
22381 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
22383 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
22384 return 1;
22386 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22387 return 1;
22390 if (GET_CODE (rtl) == CONST
22391 && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
22392 return 1;
22394 return 0;
22397 /* Helper function for resolve_addr, handle one location
22398 expression, return false if at least one CONST_STRING or SYMBOL_REF in
22399 the location list couldn't be resolved. */
22401 static bool
22402 resolve_addr_in_expr (dw_loc_descr_ref loc)
22404 dw_loc_descr_ref keep = NULL;
22405 for (; loc; loc = loc->dw_loc_next)
22406 switch (loc->dw_loc_opc)
22408 case DW_OP_addr:
22409 if (resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
22410 return false;
22411 break;
22412 case DW_OP_GNU_addr_index:
22413 case DW_OP_GNU_const_index:
22415 if ((loc->dw_loc_opc == DW_OP_GNU_addr_index
22416 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
22417 && resolve_one_addr (&loc->dw_loc_oprnd1.val_entry->addr.rtl,
22418 NULL))
22419 return false;
22421 break;
22422 case DW_OP_const4u:
22423 case DW_OP_const8u:
22424 if (loc->dtprel
22425 && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
22426 return false;
22427 break;
22428 case DW_OP_plus_uconst:
22429 if (size_of_loc_descr (loc)
22430 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
22432 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
22434 dw_loc_descr_ref repl
22435 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
22436 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
22437 add_loc_descr (&repl, loc->dw_loc_next);
22438 *loc = *repl;
22440 break;
22441 case DW_OP_implicit_value:
22442 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
22443 && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL))
22444 return false;
22445 break;
22446 case DW_OP_GNU_implicit_pointer:
22447 case DW_OP_GNU_parameter_ref:
22448 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
22450 dw_die_ref ref
22451 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
22452 if (ref == NULL)
22453 return false;
22454 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
22455 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
22456 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
22458 break;
22459 case DW_OP_GNU_const_type:
22460 case DW_OP_GNU_regval_type:
22461 case DW_OP_GNU_deref_type:
22462 case DW_OP_GNU_convert:
22463 case DW_OP_GNU_reinterpret:
22464 while (loc->dw_loc_next
22465 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
22467 dw_die_ref base1, base2;
22468 unsigned enc1, enc2, size1, size2;
22469 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
22470 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
22471 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
22472 else if (loc->dw_loc_oprnd1.val_class
22473 == dw_val_class_unsigned_const)
22474 break;
22475 else
22476 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
22477 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
22478 == dw_val_class_unsigned_const)
22479 break;
22480 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
22481 gcc_assert (base1->die_tag == DW_TAG_base_type
22482 && base2->die_tag == DW_TAG_base_type);
22483 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
22484 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
22485 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
22486 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
22487 if (size1 == size2
22488 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
22489 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
22490 && loc != keep)
22491 || enc1 == enc2))
22493 /* Optimize away next DW_OP_GNU_convert after
22494 adjusting LOC's base type die reference. */
22495 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
22496 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
22497 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
22498 else
22499 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
22500 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
22501 continue;
22503 /* Don't change integer DW_OP_GNU_convert after e.g. floating
22504 point typed stack entry. */
22505 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
22506 keep = loc->dw_loc_next;
22507 break;
22509 break;
22510 default:
22511 break;
22513 return true;
22516 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
22517 an address in .rodata section if the string literal is emitted there,
22518 or remove the containing location list or replace DW_AT_const_value
22519 with DW_AT_location and empty location expression, if it isn't found
22520 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
22521 to something that has been emitted in the current CU. */
22523 static void
22524 resolve_addr (dw_die_ref die)
22526 dw_die_ref c;
22527 dw_attr_ref a;
22528 dw_loc_list_ref *curr, *start, loc;
22529 unsigned ix;
22531 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22532 switch (AT_class (a))
22534 case dw_val_class_loc_list:
22535 start = curr = AT_loc_list_ptr (a);
22536 loc = *curr;
22537 gcc_assert (loc);
22538 /* The same list can be referenced more than once. See if we have
22539 already recorded the result from a previous pass. */
22540 if (loc->replaced)
22541 *curr = loc->dw_loc_next;
22542 else if (!loc->resolved_addr)
22544 /* As things stand, we do not expect or allow one die to
22545 reference a suffix of another die's location list chain.
22546 References must be identical or completely separate.
22547 There is therefore no need to cache the result of this
22548 pass on any list other than the first; doing so
22549 would lead to unnecessary writes. */
22550 while (*curr)
22552 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
22553 if (!resolve_addr_in_expr ((*curr)->expr))
22555 dw_loc_list_ref next = (*curr)->dw_loc_next;
22556 dw_loc_descr_ref l = (*curr)->expr;
22558 if (next && (*curr)->ll_symbol)
22560 gcc_assert (!next->ll_symbol);
22561 next->ll_symbol = (*curr)->ll_symbol;
22563 if (dwarf_split_debug_info)
22564 remove_loc_list_addr_table_entries (l);
22565 *curr = next;
22567 else
22569 mark_base_types ((*curr)->expr);
22570 curr = &(*curr)->dw_loc_next;
22573 if (loc == *start)
22574 loc->resolved_addr = 1;
22575 else
22577 loc->replaced = 1;
22578 if (dwarf_split_debug_info)
22579 remove_loc_list_addr_table_entries (loc->expr);
22580 loc->dw_loc_next = *start;
22583 if (!*start)
22585 remove_AT (die, a->dw_attr);
22586 ix--;
22588 break;
22589 case dw_val_class_loc:
22591 dw_loc_descr_ref l = AT_loc (a);
22592 /* For -gdwarf-2 don't attempt to optimize
22593 DW_AT_data_member_location containing
22594 DW_OP_plus_uconst - older consumers might
22595 rely on it being that op instead of a more complex,
22596 but shorter, location description. */
22597 if ((dwarf_version > 2
22598 || a->dw_attr != DW_AT_data_member_location
22599 || l == NULL
22600 || l->dw_loc_opc != DW_OP_plus_uconst
22601 || l->dw_loc_next != NULL)
22602 && !resolve_addr_in_expr (l))
22604 if (dwarf_split_debug_info)
22605 remove_loc_list_addr_table_entries (l);
22606 remove_AT (die, a->dw_attr);
22607 ix--;
22609 else
22610 mark_base_types (l);
22612 break;
22613 case dw_val_class_addr:
22614 if (a->dw_attr == DW_AT_const_value
22615 && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
22617 if (AT_index (a) != NOT_INDEXED)
22618 remove_addr_table_entry (a->dw_attr_val.val_entry);
22619 remove_AT (die, a->dw_attr);
22620 ix--;
22622 if (die->die_tag == DW_TAG_GNU_call_site
22623 && a->dw_attr == DW_AT_abstract_origin)
22625 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
22626 dw_die_ref tdie = lookup_decl_die (tdecl);
22627 if (tdie == NULL
22628 && DECL_EXTERNAL (tdecl)
22629 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
22631 force_decl_die (tdecl);
22632 tdie = lookup_decl_die (tdecl);
22634 if (tdie)
22636 a->dw_attr_val.val_class = dw_val_class_die_ref;
22637 a->dw_attr_val.v.val_die_ref.die = tdie;
22638 a->dw_attr_val.v.val_die_ref.external = 0;
22640 else
22642 if (AT_index (a) != NOT_INDEXED)
22643 remove_addr_table_entry (a->dw_attr_val.val_entry);
22644 remove_AT (die, a->dw_attr);
22645 ix--;
22648 break;
22649 default:
22650 break;
22653 FOR_EACH_CHILD (die, c, resolve_addr (c));
22656 /* Helper routines for optimize_location_lists.
22657 This pass tries to share identical local lists in .debug_loc
22658 section. */
22660 /* Iteratively hash operands of LOC opcode. */
22662 static inline hashval_t
22663 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
22665 dw_val_ref val1 = &loc->dw_loc_oprnd1;
22666 dw_val_ref val2 = &loc->dw_loc_oprnd2;
22668 switch (loc->dw_loc_opc)
22670 case DW_OP_const4u:
22671 case DW_OP_const8u:
22672 if (loc->dtprel)
22673 goto hash_addr;
22674 /* FALLTHRU */
22675 case DW_OP_const1u:
22676 case DW_OP_const1s:
22677 case DW_OP_const2u:
22678 case DW_OP_const2s:
22679 case DW_OP_const4s:
22680 case DW_OP_const8s:
22681 case DW_OP_constu:
22682 case DW_OP_consts:
22683 case DW_OP_pick:
22684 case DW_OP_plus_uconst:
22685 case DW_OP_breg0:
22686 case DW_OP_breg1:
22687 case DW_OP_breg2:
22688 case DW_OP_breg3:
22689 case DW_OP_breg4:
22690 case DW_OP_breg5:
22691 case DW_OP_breg6:
22692 case DW_OP_breg7:
22693 case DW_OP_breg8:
22694 case DW_OP_breg9:
22695 case DW_OP_breg10:
22696 case DW_OP_breg11:
22697 case DW_OP_breg12:
22698 case DW_OP_breg13:
22699 case DW_OP_breg14:
22700 case DW_OP_breg15:
22701 case DW_OP_breg16:
22702 case DW_OP_breg17:
22703 case DW_OP_breg18:
22704 case DW_OP_breg19:
22705 case DW_OP_breg20:
22706 case DW_OP_breg21:
22707 case DW_OP_breg22:
22708 case DW_OP_breg23:
22709 case DW_OP_breg24:
22710 case DW_OP_breg25:
22711 case DW_OP_breg26:
22712 case DW_OP_breg27:
22713 case DW_OP_breg28:
22714 case DW_OP_breg29:
22715 case DW_OP_breg30:
22716 case DW_OP_breg31:
22717 case DW_OP_regx:
22718 case DW_OP_fbreg:
22719 case DW_OP_piece:
22720 case DW_OP_deref_size:
22721 case DW_OP_xderef_size:
22722 hash = iterative_hash_object (val1->v.val_int, hash);
22723 break;
22724 case DW_OP_skip:
22725 case DW_OP_bra:
22727 int offset;
22729 gcc_assert (val1->val_class == dw_val_class_loc);
22730 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
22731 hash = iterative_hash_object (offset, hash);
22733 break;
22734 case DW_OP_implicit_value:
22735 hash = iterative_hash_object (val1->v.val_unsigned, hash);
22736 switch (val2->val_class)
22738 case dw_val_class_const:
22739 hash = iterative_hash_object (val2->v.val_int, hash);
22740 break;
22741 case dw_val_class_vec:
22743 unsigned int elt_size = val2->v.val_vec.elt_size;
22744 unsigned int len = val2->v.val_vec.length;
22746 hash = iterative_hash_object (elt_size, hash);
22747 hash = iterative_hash_object (len, hash);
22748 hash = iterative_hash (val2->v.val_vec.array,
22749 len * elt_size, hash);
22751 break;
22752 case dw_val_class_const_double:
22753 hash = iterative_hash_object (val2->v.val_double.low, hash);
22754 hash = iterative_hash_object (val2->v.val_double.high, hash);
22755 break;
22756 case dw_val_class_addr:
22757 hash = iterative_hash_rtx (val2->v.val_addr, hash);
22758 break;
22759 default:
22760 gcc_unreachable ();
22762 break;
22763 case DW_OP_bregx:
22764 case DW_OP_bit_piece:
22765 hash = iterative_hash_object (val1->v.val_int, hash);
22766 hash = iterative_hash_object (val2->v.val_int, hash);
22767 break;
22768 case DW_OP_addr:
22769 hash_addr:
22770 if (loc->dtprel)
22772 unsigned char dtprel = 0xd1;
22773 hash = iterative_hash_object (dtprel, hash);
22775 hash = iterative_hash_rtx (val1->v.val_addr, hash);
22776 break;
22777 case DW_OP_GNU_addr_index:
22778 case DW_OP_GNU_const_index:
22780 if (loc->dtprel)
22782 unsigned char dtprel = 0xd1;
22783 hash = iterative_hash_object (dtprel, hash);
22785 hash = iterative_hash_rtx (val1->val_entry->addr.rtl, hash);
22787 break;
22788 case DW_OP_GNU_implicit_pointer:
22789 hash = iterative_hash_object (val2->v.val_int, hash);
22790 break;
22791 case DW_OP_GNU_entry_value:
22792 hash = hash_loc_operands (val1->v.val_loc, hash);
22793 break;
22794 case DW_OP_GNU_regval_type:
22795 case DW_OP_GNU_deref_type:
22797 unsigned int byte_size
22798 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
22799 unsigned int encoding
22800 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
22801 hash = iterative_hash_object (val1->v.val_int, hash);
22802 hash = iterative_hash_object (byte_size, hash);
22803 hash = iterative_hash_object (encoding, hash);
22805 break;
22806 case DW_OP_GNU_convert:
22807 case DW_OP_GNU_reinterpret:
22808 if (val1->val_class == dw_val_class_unsigned_const)
22810 hash = iterative_hash_object (val1->v.val_unsigned, hash);
22811 break;
22813 /* FALLTHRU */
22814 case DW_OP_GNU_const_type:
22816 unsigned int byte_size
22817 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
22818 unsigned int encoding
22819 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
22820 hash = iterative_hash_object (byte_size, hash);
22821 hash = iterative_hash_object (encoding, hash);
22822 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
22823 break;
22824 hash = iterative_hash_object (val2->val_class, hash);
22825 switch (val2->val_class)
22827 case dw_val_class_const:
22828 hash = iterative_hash_object (val2->v.val_int, hash);
22829 break;
22830 case dw_val_class_vec:
22832 unsigned int elt_size = val2->v.val_vec.elt_size;
22833 unsigned int len = val2->v.val_vec.length;
22835 hash = iterative_hash_object (elt_size, hash);
22836 hash = iterative_hash_object (len, hash);
22837 hash = iterative_hash (val2->v.val_vec.array,
22838 len * elt_size, hash);
22840 break;
22841 case dw_val_class_const_double:
22842 hash = iterative_hash_object (val2->v.val_double.low, hash);
22843 hash = iterative_hash_object (val2->v.val_double.high, hash);
22844 break;
22845 default:
22846 gcc_unreachable ();
22849 break;
22851 default:
22852 /* Other codes have no operands. */
22853 break;
22855 return hash;
22858 /* Iteratively hash the whole DWARF location expression LOC. */
22860 static inline hashval_t
22861 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
22863 dw_loc_descr_ref l;
22864 bool sizes_computed = false;
22865 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
22866 size_of_locs (loc);
22868 for (l = loc; l != NULL; l = l->dw_loc_next)
22870 enum dwarf_location_atom opc = l->dw_loc_opc;
22871 hash = iterative_hash_object (opc, hash);
22872 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
22874 size_of_locs (loc);
22875 sizes_computed = true;
22877 hash = hash_loc_operands (l, hash);
22879 return hash;
22882 /* Compute hash of the whole location list LIST_HEAD. */
22884 static inline void
22885 hash_loc_list (dw_loc_list_ref list_head)
22887 dw_loc_list_ref curr = list_head;
22888 hashval_t hash = 0;
22890 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
22892 hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
22893 hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
22894 if (curr->section)
22895 hash = iterative_hash (curr->section, strlen (curr->section) + 1,
22896 hash);
22897 hash = hash_locs (curr->expr, hash);
22899 list_head->hash = hash;
22902 /* Return true if X and Y opcodes have the same operands. */
22904 static inline bool
22905 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
22907 dw_val_ref valx1 = &x->dw_loc_oprnd1;
22908 dw_val_ref valx2 = &x->dw_loc_oprnd2;
22909 dw_val_ref valy1 = &y->dw_loc_oprnd1;
22910 dw_val_ref valy2 = &y->dw_loc_oprnd2;
22912 switch (x->dw_loc_opc)
22914 case DW_OP_const4u:
22915 case DW_OP_const8u:
22916 if (x->dtprel)
22917 goto hash_addr;
22918 /* FALLTHRU */
22919 case DW_OP_const1u:
22920 case DW_OP_const1s:
22921 case DW_OP_const2u:
22922 case DW_OP_const2s:
22923 case DW_OP_const4s:
22924 case DW_OP_const8s:
22925 case DW_OP_constu:
22926 case DW_OP_consts:
22927 case DW_OP_pick:
22928 case DW_OP_plus_uconst:
22929 case DW_OP_breg0:
22930 case DW_OP_breg1:
22931 case DW_OP_breg2:
22932 case DW_OP_breg3:
22933 case DW_OP_breg4:
22934 case DW_OP_breg5:
22935 case DW_OP_breg6:
22936 case DW_OP_breg7:
22937 case DW_OP_breg8:
22938 case DW_OP_breg9:
22939 case DW_OP_breg10:
22940 case DW_OP_breg11:
22941 case DW_OP_breg12:
22942 case DW_OP_breg13:
22943 case DW_OP_breg14:
22944 case DW_OP_breg15:
22945 case DW_OP_breg16:
22946 case DW_OP_breg17:
22947 case DW_OP_breg18:
22948 case DW_OP_breg19:
22949 case DW_OP_breg20:
22950 case DW_OP_breg21:
22951 case DW_OP_breg22:
22952 case DW_OP_breg23:
22953 case DW_OP_breg24:
22954 case DW_OP_breg25:
22955 case DW_OP_breg26:
22956 case DW_OP_breg27:
22957 case DW_OP_breg28:
22958 case DW_OP_breg29:
22959 case DW_OP_breg30:
22960 case DW_OP_breg31:
22961 case DW_OP_regx:
22962 case DW_OP_fbreg:
22963 case DW_OP_piece:
22964 case DW_OP_deref_size:
22965 case DW_OP_xderef_size:
22966 return valx1->v.val_int == valy1->v.val_int;
22967 case DW_OP_skip:
22968 case DW_OP_bra:
22969 /* If splitting debug info, the use of DW_OP_GNU_addr_index
22970 can cause irrelevant differences in dw_loc_addr. */
22971 gcc_assert (valx1->val_class == dw_val_class_loc
22972 && valy1->val_class == dw_val_class_loc
22973 && (dwarf_split_debug_info
22974 || x->dw_loc_addr == y->dw_loc_addr));
22975 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
22976 case DW_OP_implicit_value:
22977 if (valx1->v.val_unsigned != valy1->v.val_unsigned
22978 || valx2->val_class != valy2->val_class)
22979 return false;
22980 switch (valx2->val_class)
22982 case dw_val_class_const:
22983 return valx2->v.val_int == valy2->v.val_int;
22984 case dw_val_class_vec:
22985 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
22986 && valx2->v.val_vec.length == valy2->v.val_vec.length
22987 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
22988 valx2->v.val_vec.elt_size
22989 * valx2->v.val_vec.length) == 0;
22990 case dw_val_class_const_double:
22991 return valx2->v.val_double.low == valy2->v.val_double.low
22992 && valx2->v.val_double.high == valy2->v.val_double.high;
22993 case dw_val_class_addr:
22994 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
22995 default:
22996 gcc_unreachable ();
22998 case DW_OP_bregx:
22999 case DW_OP_bit_piece:
23000 return valx1->v.val_int == valy1->v.val_int
23001 && valx2->v.val_int == valy2->v.val_int;
23002 case DW_OP_addr:
23003 hash_addr:
23004 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
23005 case DW_OP_GNU_addr_index:
23006 case DW_OP_GNU_const_index:
23008 rtx ax1 = valx1->val_entry->addr.rtl;
23009 rtx ay1 = valy1->val_entry->addr.rtl;
23010 return rtx_equal_p (ax1, ay1);
23012 case DW_OP_GNU_implicit_pointer:
23013 return valx1->val_class == dw_val_class_die_ref
23014 && valx1->val_class == valy1->val_class
23015 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
23016 && valx2->v.val_int == valy2->v.val_int;
23017 case DW_OP_GNU_entry_value:
23018 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
23019 case DW_OP_GNU_const_type:
23020 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
23021 || valx2->val_class != valy2->val_class)
23022 return false;
23023 switch (valx2->val_class)
23025 case dw_val_class_const:
23026 return valx2->v.val_int == valy2->v.val_int;
23027 case dw_val_class_vec:
23028 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23029 && valx2->v.val_vec.length == valy2->v.val_vec.length
23030 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23031 valx2->v.val_vec.elt_size
23032 * valx2->v.val_vec.length) == 0;
23033 case dw_val_class_const_double:
23034 return valx2->v.val_double.low == valy2->v.val_double.low
23035 && valx2->v.val_double.high == valy2->v.val_double.high;
23036 default:
23037 gcc_unreachable ();
23039 case DW_OP_GNU_regval_type:
23040 case DW_OP_GNU_deref_type:
23041 return valx1->v.val_int == valy1->v.val_int
23042 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
23043 case DW_OP_GNU_convert:
23044 case DW_OP_GNU_reinterpret:
23045 if (valx1->val_class != valy1->val_class)
23046 return false;
23047 if (valx1->val_class == dw_val_class_unsigned_const)
23048 return valx1->v.val_unsigned == valy1->v.val_unsigned;
23049 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
23050 case DW_OP_GNU_parameter_ref:
23051 return valx1->val_class == dw_val_class_die_ref
23052 && valx1->val_class == valy1->val_class
23053 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
23054 default:
23055 /* Other codes have no operands. */
23056 return true;
23060 /* Return true if DWARF location expressions X and Y are the same. */
23062 static inline bool
23063 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
23065 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
23066 if (x->dw_loc_opc != y->dw_loc_opc
23067 || x->dtprel != y->dtprel
23068 || !compare_loc_operands (x, y))
23069 break;
23070 return x == NULL && y == NULL;
23073 /* Return precomputed hash of location list X. */
23075 static hashval_t
23076 loc_list_hash (const void *x)
23078 return ((const struct dw_loc_list_struct *) x)->hash;
23081 /* Return 1 if location lists X and Y are the same. */
23083 static int
23084 loc_list_eq (const void *x, const void *y)
23086 const struct dw_loc_list_struct *a = (const struct dw_loc_list_struct *) x;
23087 const struct dw_loc_list_struct *b = (const struct dw_loc_list_struct *) y;
23088 if (a == b)
23089 return 1;
23090 if (a->hash != b->hash)
23091 return 0;
23092 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
23093 if (strcmp (a->begin, b->begin) != 0
23094 || strcmp (a->end, b->end) != 0
23095 || (a->section == NULL) != (b->section == NULL)
23096 || (a->section && strcmp (a->section, b->section) != 0)
23097 || !compare_locs (a->expr, b->expr))
23098 break;
23099 return a == NULL && b == NULL;
23102 /* Recursively optimize location lists referenced from DIE
23103 children and share them whenever possible. */
23105 static void
23106 optimize_location_lists_1 (dw_die_ref die, htab_t htab)
23108 dw_die_ref c;
23109 dw_attr_ref a;
23110 unsigned ix;
23111 void **slot;
23113 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23114 if (AT_class (a) == dw_val_class_loc_list)
23116 dw_loc_list_ref list = AT_loc_list (a);
23117 /* TODO: perform some optimizations here, before hashing
23118 it and storing into the hash table. */
23119 hash_loc_list (list);
23120 slot = htab_find_slot_with_hash (htab, list, list->hash,
23121 INSERT);
23122 if (*slot == NULL)
23123 *slot = (void *) list;
23124 else
23125 a->dw_attr_val.v.val_loc_list = (dw_loc_list_ref) *slot;
23128 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
23132 /* Recursively assign each location list a unique index into the debug_addr
23133 section. */
23135 static void
23136 index_location_lists (dw_die_ref die)
23138 dw_die_ref c;
23139 dw_attr_ref a;
23140 unsigned ix;
23142 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23143 if (AT_class (a) == dw_val_class_loc_list)
23145 dw_loc_list_ref list = AT_loc_list (a);
23146 dw_loc_list_ref curr;
23147 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
23149 /* Don't index an entry that has already been indexed
23150 or won't be output. */
23151 if (curr->begin_entry != NULL
23152 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
23153 continue;
23155 curr->begin_entry
23156 = add_addr_table_entry (xstrdup (curr->begin),
23157 ate_kind_label);
23161 FOR_EACH_CHILD (die, c, index_location_lists (c));
23164 /* Optimize location lists referenced from DIE
23165 children and share them whenever possible. */
23167 static void
23168 optimize_location_lists (dw_die_ref die)
23170 htab_t htab = htab_create (500, loc_list_hash, loc_list_eq, NULL);
23171 optimize_location_lists_1 (die, htab);
23172 htab_delete (htab);
23175 /* Output stuff that dwarf requires at the end of every file,
23176 and generate the DWARF-2 debugging info. */
23178 static void
23179 dwarf2out_finish (const char *filename)
23181 limbo_die_node *node, *next_node;
23182 comdat_type_node *ctnode;
23183 htab_t comdat_type_table;
23184 unsigned int i;
23185 dw_die_ref main_comp_unit_die;
23187 /* PCH might result in DW_AT_producer string being restored from the
23188 header compilation, so always fill it with empty string initially
23189 and overwrite only here. */
23190 dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
23191 producer_string = gen_producer_string ();
23192 producer->dw_attr_val.v.val_str->refcount--;
23193 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
23195 gen_scheduled_generic_parms_dies ();
23196 gen_remaining_tmpl_value_param_die_attribute ();
23198 /* Add the name for the main input file now. We delayed this from
23199 dwarf2out_init to avoid complications with PCH. */
23200 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
23201 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
23202 add_comp_dir_attribute (comp_unit_die ());
23203 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
23205 bool p = false;
23206 htab_traverse (file_table, file_table_relative_p, &p);
23207 if (p)
23208 add_comp_dir_attribute (comp_unit_die ());
23211 if (deferred_locations_list)
23212 for (i = 0; i < deferred_locations_list->length (); i++)
23214 add_location_or_const_value_attribute (
23215 (*deferred_locations_list)[i].die,
23216 (*deferred_locations_list)[i].variable,
23217 false,
23218 DW_AT_location);
23221 /* Traverse the limbo die list, and add parent/child links. The only
23222 dies without parents that should be here are concrete instances of
23223 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
23224 For concrete instances, we can get the parent die from the abstract
23225 instance. */
23226 for (node = limbo_die_list; node; node = next_node)
23228 dw_die_ref die = node->die;
23229 next_node = node->next;
23231 if (die->die_parent == NULL)
23233 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
23235 if (origin && origin->die_parent)
23236 add_child_die (origin->die_parent, die);
23237 else if (is_cu_die (die))
23239 else if (seen_error ())
23240 /* It's OK to be confused by errors in the input. */
23241 add_child_die (comp_unit_die (), die);
23242 else
23244 /* In certain situations, the lexical block containing a
23245 nested function can be optimized away, which results
23246 in the nested function die being orphaned. Likewise
23247 with the return type of that nested function. Force
23248 this to be a child of the containing function.
23250 It may happen that even the containing function got fully
23251 inlined and optimized out. In that case we are lost and
23252 assign the empty child. This should not be big issue as
23253 the function is likely unreachable too. */
23254 gcc_assert (node->created_for);
23256 if (DECL_P (node->created_for))
23257 origin = get_context_die (DECL_CONTEXT (node->created_for));
23258 else if (TYPE_P (node->created_for))
23259 origin = scope_die_for (node->created_for, comp_unit_die ());
23260 else
23261 origin = comp_unit_die ();
23263 add_child_die (origin, die);
23268 limbo_die_list = NULL;
23270 #if ENABLE_ASSERT_CHECKING
23272 dw_die_ref die = comp_unit_die (), c;
23273 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
23275 #endif
23276 resolve_addr (comp_unit_die ());
23277 move_marked_base_types ();
23279 for (node = deferred_asm_name; node; node = node->next)
23281 tree decl = node->created_for;
23282 /* When generating LTO bytecode we can not generate new assembler
23283 names at this point and all important decls got theirs via
23284 free-lang-data. */
23285 if ((!flag_generate_lto || DECL_ASSEMBLER_NAME_SET_P (decl))
23286 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
23288 add_linkage_attr (node->die, decl);
23289 move_linkage_attr (node->die);
23293 deferred_asm_name = NULL;
23295 /* Walk through the list of incomplete types again, trying once more to
23296 emit full debugging info for them. */
23297 retry_incomplete_types ();
23299 if (flag_eliminate_unused_debug_types)
23300 prune_unused_types ();
23302 /* Generate separate COMDAT sections for type DIEs. */
23303 if (use_debug_types)
23305 break_out_comdat_types (comp_unit_die ());
23307 /* Each new type_unit DIE was added to the limbo die list when created.
23308 Since these have all been added to comdat_type_list, clear the
23309 limbo die list. */
23310 limbo_die_list = NULL;
23312 /* For each new comdat type unit, copy declarations for incomplete
23313 types to make the new unit self-contained (i.e., no direct
23314 references to the main compile unit). */
23315 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23316 copy_decls_for_unworthy_types (ctnode->root_die);
23317 copy_decls_for_unworthy_types (comp_unit_die ());
23319 /* In the process of copying declarations from one unit to another,
23320 we may have left some declarations behind that are no longer
23321 referenced. Prune them. */
23322 prune_unused_types ();
23325 /* Generate separate CUs for each of the include files we've seen.
23326 They will go into limbo_die_list. */
23327 if (flag_eliminate_dwarf2_dups)
23328 break_out_includes (comp_unit_die ());
23330 /* Traverse the DIE's and add add sibling attributes to those DIE's
23331 that have children. */
23332 add_sibling_attributes (comp_unit_die ());
23333 for (node = limbo_die_list; node; node = node->next)
23334 add_sibling_attributes (node->die);
23335 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23336 add_sibling_attributes (ctnode->root_die);
23338 /* When splitting DWARF info, we put some attributes in the
23339 skeleton compile_unit DIE that remains in the .o, while
23340 most attributes go in the DWO compile_unit_die. */
23341 if (dwarf_split_debug_info)
23342 main_comp_unit_die = gen_compile_unit_die (NULL);
23343 else
23344 main_comp_unit_die = comp_unit_die ();
23346 /* Output a terminator label for the .text section. */
23347 switch_to_section (text_section);
23348 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
23349 if (cold_text_section)
23351 switch_to_section (cold_text_section);
23352 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
23355 /* We can only use the low/high_pc attributes if all of the code was
23356 in .text. */
23357 if (!have_multiple_function_sections
23358 || (dwarf_version < 3 && dwarf_strict))
23360 /* Don't add if the CU has no associated code. */
23361 if (text_section_used)
23362 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
23363 text_end_label, true);
23365 else
23367 unsigned fde_idx;
23368 dw_fde_ref fde;
23369 bool range_list_added = false;
23371 if (text_section_used)
23372 add_ranges_by_labels (main_comp_unit_die, text_section_label,
23373 text_end_label, &range_list_added, true);
23374 if (cold_text_section_used)
23375 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
23376 cold_end_label, &range_list_added, true);
23378 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
23380 if (DECL_IGNORED_P (fde->decl))
23381 continue;
23382 if (!fde->in_std_section)
23383 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
23384 fde->dw_fde_end, &range_list_added,
23385 true);
23386 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
23387 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
23388 fde->dw_fde_second_end, &range_list_added,
23389 true);
23392 if (range_list_added)
23394 /* We need to give .debug_loc and .debug_ranges an appropriate
23395 "base address". Use zero so that these addresses become
23396 absolute. Historically, we've emitted the unexpected
23397 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
23398 Emit both to give time for other tools to adapt. */
23399 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
23400 if (! dwarf_strict && dwarf_version < 4)
23401 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
23403 add_ranges (NULL);
23407 if (debug_info_level >= DINFO_LEVEL_NORMAL)
23408 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
23409 debug_line_section_label);
23411 if (have_macinfo)
23412 add_AT_macptr (comp_unit_die (),
23413 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
23414 macinfo_section_label);
23416 if (dwarf_split_debug_info && addr_index_table != NULL)
23418 /* optimize_location_lists calculates the size of the lists,
23419 so index them first, and assign indices to the entries.
23420 Although optimize_location_lists will remove entries from
23421 the table, it only does so for duplicates, and therefore
23422 only reduces ref_counts to 1. */
23423 unsigned int index = 0;
23424 index_location_lists (comp_unit_die ());
23425 htab_traverse_noresize (addr_index_table,
23426 index_addr_table_entry, &index);
23428 if (have_location_lists)
23429 optimize_location_lists (comp_unit_die ());
23431 save_macinfo_strings ();
23432 if (dwarf_split_debug_info)
23434 unsigned int index = 0;
23435 htab_traverse_noresize (debug_str_hash, index_string, &index);
23438 /* Output all of the compilation units. We put the main one last so that
23439 the offsets are available to output_pubnames. */
23440 for (node = limbo_die_list; node; node = node->next)
23441 output_comp_unit (node->die, 0);
23443 comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
23444 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23446 void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
23448 /* Don't output duplicate types. */
23449 if (*slot != HTAB_EMPTY_ENTRY)
23450 continue;
23452 /* Add a pointer to the line table for the main compilation unit
23453 so that the debugger can make sense of DW_AT_decl_file
23454 attributes. */
23455 if (debug_info_level >= DINFO_LEVEL_NORMAL)
23456 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
23457 (!dwarf_split_debug_info
23458 ? debug_line_section_label
23459 : debug_skeleton_line_section_label));
23461 output_comdat_type_unit (ctnode);
23462 *slot = ctnode;
23464 htab_delete (comdat_type_table);
23466 /* The AT_pubnames attribute needs to go in all skeleton dies, including
23467 both the main_cu and all skeleton TUs. Making this call unconditional
23468 would end up either adding a second copy of the AT_pubnames attribute, or
23469 requiring a special case in add_top_level_skeleton_die_attrs. */
23470 if (!dwarf_split_debug_info)
23471 add_AT_pubnames (comp_unit_die ());
23473 if (dwarf_split_debug_info)
23475 int mark;
23476 unsigned char checksum[16];
23477 struct md5_ctx ctx;
23479 /* Compute a checksum of the comp_unit to use as the dwo_id. */
23480 md5_init_ctx (&ctx);
23481 mark = 0;
23482 die_checksum (comp_unit_die (), &ctx, &mark);
23483 unmark_all_dies (comp_unit_die ());
23484 md5_finish_ctx (&ctx, checksum);
23486 /* Use the first 8 bytes of the checksum as the dwo_id,
23487 and add it to both comp-unit DIEs. */
23488 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
23489 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
23491 /* Add the base offset of the ranges table to the skeleton
23492 comp-unit DIE. */
23493 if (ranges_table_in_use)
23494 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
23495 ranges_section_label);
23497 switch_to_section (debug_addr_section);
23498 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
23499 output_addr_table ();
23502 /* Output the main compilation unit if non-empty or if .debug_macinfo
23503 or .debug_macro will be emitted. */
23504 output_comp_unit (comp_unit_die (), have_macinfo);
23506 if (dwarf_split_debug_info && info_section_emitted)
23507 output_skeleton_debug_sections (main_comp_unit_die);
23509 /* Output the abbreviation table. */
23510 if (abbrev_die_table_in_use != 1)
23512 switch_to_section (debug_abbrev_section);
23513 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
23514 output_abbrev_section ();
23517 /* Output location list section if necessary. */
23518 if (have_location_lists)
23520 /* Output the location lists info. */
23521 switch_to_section (debug_loc_section);
23522 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
23523 output_location_lists (comp_unit_die ());
23526 /* Output public names and types tables if necessary. */
23527 output_pubnames (pubname_table);
23528 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
23529 It shouldn't hurt to emit it always, since pure DWARF2 consumers
23530 simply won't look for the section. */
23531 output_pubnames (pubtype_table);
23533 /* Output the address range information if a CU (.debug_info section)
23534 was emitted. We output an empty table even if we had no functions
23535 to put in it. This because the consumer has no way to tell the
23536 difference between an empty table that we omitted and failure to
23537 generate a table that would have contained data. */
23538 if (info_section_emitted)
23540 unsigned long aranges_length = size_of_aranges ();
23542 switch_to_section (debug_aranges_section);
23543 output_aranges (aranges_length);
23546 /* Output ranges section if necessary. */
23547 if (ranges_table_in_use)
23549 switch_to_section (debug_ranges_section);
23550 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
23551 output_ranges ();
23554 /* Have to end the macro section. */
23555 if (have_macinfo)
23557 switch_to_section (debug_macinfo_section);
23558 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
23559 output_macinfo ();
23560 dw2_asm_output_data (1, 0, "End compilation unit");
23563 /* Output the source line correspondence table. We must do this
23564 even if there is no line information. Otherwise, on an empty
23565 translation unit, we will generate a present, but empty,
23566 .debug_info section. IRIX 6.5 `nm' will then complain when
23567 examining the file. This is done late so that any filenames
23568 used by the debug_info section are marked as 'used'. */
23569 switch_to_section (debug_line_section);
23570 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
23571 if (! DWARF2_ASM_LINE_DEBUG_INFO)
23572 output_line_info (false);
23574 if (dwarf_split_debug_info && info_section_emitted)
23576 switch_to_section (debug_skeleton_line_section);
23577 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
23578 output_line_info (true);
23581 /* If we emitted any indirect strings, output the string table too. */
23582 if (debug_str_hash)
23583 output_indirect_strings ();
23586 #include "gt-dwarf2out.h"