Mark ChangeLog
[official-gcc.git] / gcc / dwarf2out.c
blobc68453e984b6765c357c192c4a023352c906f440
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2013 Free Software Foundation, Inc.
3 Contributed by Gary Funck (gary@intrepid.com).
4 Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
5 Extensively modified by Jason Merrill (jason@cygnus.com).
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* TODO: Emit .debug_line header even when there are no functions, since
24 the file numbers are used by .debug_info. Alternately, leave
25 out locations for types and decls.
26 Avoid talking about ctors and op= for PODs.
27 Factor out common prologue sequences into multiple CIEs. */
29 /* The first part of this file deals with the DWARF 2 frame unwind
30 information, which is also used by the GCC efficient exception handling
31 mechanism. The second part, controlled only by an #ifdef
32 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
33 information. */
35 /* DWARF2 Abbreviation Glossary:
37 CFA = Canonical Frame Address
38 a fixed address on the stack which identifies a call frame.
39 We define it to be the value of SP just before the call insn.
40 The CFA register and offset, which may change during the course
41 of the function, are used to calculate its value at runtime.
43 CFI = Call Frame Instruction
44 an instruction for the DWARF2 abstract machine
46 CIE = Common Information Entry
47 information describing information common to one or more FDEs
49 DIE = Debugging Information Entry
51 FDE = Frame Description Entry
52 information describing the stack call frame, in particular,
53 how to restore registers
55 DW_CFA_... = DWARF2 CFA call frame instruction
56 DW_TAG_... = DWARF2 DIE tag */
58 #include "config.h"
59 #include "system.h"
60 #include "coretypes.h"
61 #include "tm.h"
62 #include "tree.h"
63 #include "version.h"
64 #include "flags.h"
65 #include "rtl.h"
66 #include "hard-reg-set.h"
67 #include "regs.h"
68 #include "insn-config.h"
69 #include "reload.h"
70 #include "function.h"
71 #include "output.h"
72 #include "expr.h"
73 #include "except.h"
74 #include "dwarf2.h"
75 #include "dwarf2out.h"
76 #include "dwarf2asm.h"
77 #include "toplev.h"
78 #include "ggc.h"
79 #include "md5.h"
80 #include "tm_p.h"
81 #include "diagnostic.h"
82 #include "tree-pretty-print.h"
83 #include "debug.h"
84 #include "target.h"
85 #include "common/common-target.h"
86 #include "langhooks.h"
87 #include "hashtab.h"
88 #include "cgraph.h"
89 #include "input.h"
90 #include "gimple.h"
91 #include "ira.h"
92 #include "lra.h"
93 #include "dumpfile.h"
94 #include "opts.h"
96 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
97 static rtx last_var_location_insn;
98 static rtx cached_next_real_insn;
100 #ifdef VMS_DEBUGGING_INFO
101 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
103 /* Define this macro to be a nonzero value if the directory specifications
104 which are output in the debug info should end with a separator. */
105 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
106 /* Define this macro to evaluate to a nonzero value if GCC should refrain
107 from generating indirect strings in DWARF2 debug information, for instance
108 if your target is stuck with an old version of GDB that is unable to
109 process them properly or uses VMS Debug. */
110 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
111 #else
112 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
113 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
114 #endif
116 /* ??? Poison these here until it can be done generically. They've been
117 totally replaced in this file; make sure it stays that way. */
118 #undef DWARF2_UNWIND_INFO
119 #undef DWARF2_FRAME_INFO
120 #if (GCC_VERSION >= 3000)
121 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
122 #endif
124 /* The size of the target's pointer type. */
125 #ifndef PTR_SIZE
126 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
127 #endif
129 /* Array of RTXes referenced by the debugging information, which therefore
130 must be kept around forever. */
131 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
133 /* A pointer to the base of a list of incomplete types which might be
134 completed at some later time. incomplete_types_list needs to be a
135 vec<tree, va_gc> *because we want to tell the garbage collector about
136 it. */
137 static GTY(()) vec<tree, va_gc> *incomplete_types;
139 /* A pointer to the base of a table of references to declaration
140 scopes. This table is a display which tracks the nesting
141 of declaration scopes at the current scope and containing
142 scopes. This table is used to find the proper place to
143 define type declaration DIE's. */
144 static GTY(()) vec<tree, va_gc> *decl_scope_table;
146 /* Pointers to various DWARF2 sections. */
147 static GTY(()) section *debug_info_section;
148 static GTY(()) section *debug_skeleton_info_section;
149 static GTY(()) section *debug_abbrev_section;
150 static GTY(()) section *debug_skeleton_abbrev_section;
151 static GTY(()) section *debug_aranges_section;
152 static GTY(()) section *debug_addr_section;
153 static GTY(()) section *debug_macinfo_section;
154 static GTY(()) section *debug_line_section;
155 static GTY(()) section *debug_skeleton_line_section;
156 static GTY(()) section *debug_loc_section;
157 static GTY(()) section *debug_pubnames_section;
158 static GTY(()) section *debug_pubtypes_section;
159 static GTY(()) section *debug_str_section;
160 static GTY(()) section *debug_str_offsets_section;
161 static GTY(()) section *debug_ranges_section;
162 static GTY(()) section *debug_frame_section;
164 /* Maximum size (in bytes) of an artificially generated label. */
165 #define MAX_ARTIFICIAL_LABEL_BYTES 30
167 /* According to the (draft) DWARF 3 specification, the initial length
168 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
169 bytes are 0xffffffff, followed by the length stored in the next 8
170 bytes.
172 However, the SGI/MIPS ABI uses an initial length which is equal to
173 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
175 #ifndef DWARF_INITIAL_LENGTH_SIZE
176 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
177 #endif
179 /* Round SIZE up to the nearest BOUNDARY. */
180 #define DWARF_ROUND(SIZE,BOUNDARY) \
181 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
183 /* CIE identifier. */
184 #if HOST_BITS_PER_WIDE_INT >= 64
185 #define DWARF_CIE_ID \
186 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
187 #else
188 #define DWARF_CIE_ID DW_CIE_ID
189 #endif
192 /* A vector for a table that contains frame description
193 information for each routine. */
194 #define NOT_INDEXED (-1U)
195 #define NO_INDEX_ASSIGNED (-2U)
197 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
199 struct GTY(()) indirect_string_node {
200 const char *str;
201 unsigned int refcount;
202 enum dwarf_form form;
203 char *label;
204 unsigned int index;
207 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
209 static GTY(()) int dw2_string_counter;
211 /* True if the compilation unit places functions in more than one section. */
212 static GTY(()) bool have_multiple_function_sections = false;
214 /* Whether the default text and cold text sections have been used at all. */
216 static GTY(()) bool text_section_used = false;
217 static GTY(()) bool cold_text_section_used = false;
219 /* The default cold text section. */
220 static GTY(()) section *cold_text_section;
222 /* Forward declarations for functions defined in this file. */
224 static char *stripattributes (const char *);
225 static void output_call_frame_info (int);
226 static void dwarf2out_note_section_used (void);
228 /* Personality decl of current unit. Used only when assembler does not support
229 personality CFI. */
230 static GTY(()) rtx current_unit_personality;
232 /* Data and reference forms for relocatable data. */
233 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
234 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
236 #ifndef DEBUG_FRAME_SECTION
237 #define DEBUG_FRAME_SECTION ".debug_frame"
238 #endif
240 #ifndef FUNC_BEGIN_LABEL
241 #define FUNC_BEGIN_LABEL "LFB"
242 #endif
244 #ifndef FUNC_END_LABEL
245 #define FUNC_END_LABEL "LFE"
246 #endif
248 #ifndef PROLOGUE_END_LABEL
249 #define PROLOGUE_END_LABEL "LPE"
250 #endif
252 #ifndef EPILOGUE_BEGIN_LABEL
253 #define EPILOGUE_BEGIN_LABEL "LEB"
254 #endif
256 #ifndef FRAME_BEGIN_LABEL
257 #define FRAME_BEGIN_LABEL "Lframe"
258 #endif
259 #define CIE_AFTER_SIZE_LABEL "LSCIE"
260 #define CIE_END_LABEL "LECIE"
261 #define FDE_LABEL "LSFDE"
262 #define FDE_AFTER_SIZE_LABEL "LASFDE"
263 #define FDE_END_LABEL "LEFDE"
264 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
265 #define LINE_NUMBER_END_LABEL "LELT"
266 #define LN_PROLOG_AS_LABEL "LASLTP"
267 #define LN_PROLOG_END_LABEL "LELTP"
268 #define DIE_LABEL_PREFIX "DW"
270 /* Match the base name of a file to the base name of a compilation unit. */
272 static int
273 matches_main_base (const char *path)
275 /* Cache the last query. */
276 static const char *last_path = NULL;
277 static int last_match = 0;
278 if (path != last_path)
280 const char *base;
281 int length = base_of_path (path, &base);
282 last_path = path;
283 last_match = (length == main_input_baselength
284 && memcmp (base, main_input_basename, length) == 0);
286 return last_match;
289 #ifdef DEBUG_DEBUG_STRUCT
291 static int
292 dump_struct_debug (tree type, enum debug_info_usage usage,
293 enum debug_struct_file criterion, int generic,
294 int matches, int result)
296 /* Find the type name. */
297 tree type_decl = TYPE_STUB_DECL (type);
298 tree t = type_decl;
299 const char *name = 0;
300 if (TREE_CODE (t) == TYPE_DECL)
301 t = DECL_NAME (t);
302 if (t)
303 name = IDENTIFIER_POINTER (t);
305 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
306 criterion,
307 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
308 matches ? "bas" : "hdr",
309 generic ? "gen" : "ord",
310 usage == DINFO_USAGE_DFN ? ";" :
311 usage == DINFO_USAGE_DIR_USE ? "." : "*",
312 result,
313 (void*) type_decl, name);
314 return result;
316 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
317 dump_struct_debug (type, usage, criterion, generic, matches, result)
319 #else
321 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
322 (result)
324 #endif
326 static bool
327 should_emit_struct_debug (tree type, enum debug_info_usage usage)
329 enum debug_struct_file criterion;
330 tree type_decl;
331 bool generic = lang_hooks.types.generic_p (type);
333 if (generic)
334 criterion = debug_struct_generic[usage];
335 else
336 criterion = debug_struct_ordinary[usage];
338 if (criterion == DINFO_STRUCT_FILE_NONE)
339 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
340 if (criterion == DINFO_STRUCT_FILE_ANY)
341 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
343 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
345 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
346 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
348 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
349 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
350 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
353 /* Return a pointer to a copy of the section string name S with all
354 attributes stripped off, and an asterisk prepended (for assemble_name). */
356 static inline char *
357 stripattributes (const char *s)
359 char *stripped = XNEWVEC (char, strlen (s) + 2);
360 char *p = stripped;
362 *p++ = '*';
364 while (*s && *s != ',')
365 *p++ = *s++;
367 *p = '\0';
368 return stripped;
371 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
372 switch to the data section instead, and write out a synthetic start label
373 for collect2 the first time around. */
375 static void
376 switch_to_eh_frame_section (bool back)
378 tree label;
380 #ifdef EH_FRAME_SECTION_NAME
381 if (eh_frame_section == 0)
383 int flags;
385 if (EH_TABLES_CAN_BE_READ_ONLY)
387 int fde_encoding;
388 int per_encoding;
389 int lsda_encoding;
391 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
392 /*global=*/0);
393 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
394 /*global=*/1);
395 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
396 /*global=*/0);
397 flags = ((! flag_pic
398 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
399 && (fde_encoding & 0x70) != DW_EH_PE_aligned
400 && (per_encoding & 0x70) != DW_EH_PE_absptr
401 && (per_encoding & 0x70) != DW_EH_PE_aligned
402 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
403 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
404 ? 0 : SECTION_WRITE);
406 else
407 flags = SECTION_WRITE;
408 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
410 #endif /* EH_FRAME_SECTION_NAME */
412 if (eh_frame_section)
413 switch_to_section (eh_frame_section);
414 else
416 /* We have no special eh_frame section. Put the information in
417 the data section and emit special labels to guide collect2. */
418 switch_to_section (data_section);
420 if (!back)
422 label = get_file_function_name ("F");
423 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
424 targetm.asm_out.globalize_label (asm_out_file,
425 IDENTIFIER_POINTER (label));
426 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
431 /* Switch [BACK] to the eh or debug frame table section, depending on
432 FOR_EH. */
434 static void
435 switch_to_frame_table_section (int for_eh, bool back)
437 if (for_eh)
438 switch_to_eh_frame_section (back);
439 else
441 if (!debug_frame_section)
442 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
443 SECTION_DEBUG, NULL);
444 switch_to_section (debug_frame_section);
448 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
450 enum dw_cfi_oprnd_type
451 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
453 switch (cfi)
455 case DW_CFA_nop:
456 case DW_CFA_GNU_window_save:
457 case DW_CFA_remember_state:
458 case DW_CFA_restore_state:
459 return dw_cfi_oprnd_unused;
461 case DW_CFA_set_loc:
462 case DW_CFA_advance_loc1:
463 case DW_CFA_advance_loc2:
464 case DW_CFA_advance_loc4:
465 case DW_CFA_MIPS_advance_loc8:
466 return dw_cfi_oprnd_addr;
468 case DW_CFA_offset:
469 case DW_CFA_offset_extended:
470 case DW_CFA_def_cfa:
471 case DW_CFA_offset_extended_sf:
472 case DW_CFA_def_cfa_sf:
473 case DW_CFA_restore:
474 case DW_CFA_restore_extended:
475 case DW_CFA_undefined:
476 case DW_CFA_same_value:
477 case DW_CFA_def_cfa_register:
478 case DW_CFA_register:
479 case DW_CFA_expression:
480 return dw_cfi_oprnd_reg_num;
482 case DW_CFA_def_cfa_offset:
483 case DW_CFA_GNU_args_size:
484 case DW_CFA_def_cfa_offset_sf:
485 return dw_cfi_oprnd_offset;
487 case DW_CFA_def_cfa_expression:
488 return dw_cfi_oprnd_loc;
490 default:
491 gcc_unreachable ();
495 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
497 enum dw_cfi_oprnd_type
498 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
500 switch (cfi)
502 case DW_CFA_def_cfa:
503 case DW_CFA_def_cfa_sf:
504 case DW_CFA_offset:
505 case DW_CFA_offset_extended_sf:
506 case DW_CFA_offset_extended:
507 return dw_cfi_oprnd_offset;
509 case DW_CFA_register:
510 return dw_cfi_oprnd_reg_num;
512 case DW_CFA_expression:
513 return dw_cfi_oprnd_loc;
515 default:
516 return dw_cfi_oprnd_unused;
520 /* Output one FDE. */
522 static void
523 output_fde (dw_fde_ref fde, bool for_eh, bool second,
524 char *section_start_label, int fde_encoding, char *augmentation,
525 bool any_lsda_needed, int lsda_encoding)
527 const char *begin, *end;
528 static unsigned int j;
529 char l1[20], l2[20];
531 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
532 /* empty */ 0);
533 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
534 for_eh + j);
535 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
536 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
537 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
538 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
539 " indicating 64-bit DWARF extension");
540 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
541 "FDE Length");
542 ASM_OUTPUT_LABEL (asm_out_file, l1);
544 if (for_eh)
545 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
546 else
547 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
548 debug_frame_section, "FDE CIE offset");
550 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
551 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
553 if (for_eh)
555 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
556 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
557 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
558 "FDE initial location");
559 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
560 end, begin, "FDE address range");
562 else
564 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
565 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
568 if (augmentation[0])
570 if (any_lsda_needed)
572 int size = size_of_encoded_value (lsda_encoding);
574 if (lsda_encoding == DW_EH_PE_aligned)
576 int offset = ( 4 /* Length */
577 + 4 /* CIE offset */
578 + 2 * size_of_encoded_value (fde_encoding)
579 + 1 /* Augmentation size */ );
580 int pad = -offset & (PTR_SIZE - 1);
582 size += pad;
583 gcc_assert (size_of_uleb128 (size) == 1);
586 dw2_asm_output_data_uleb128 (size, "Augmentation size");
588 if (fde->uses_eh_lsda)
590 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
591 fde->funcdef_number);
592 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
593 gen_rtx_SYMBOL_REF (Pmode, l1),
594 false,
595 "Language Specific Data Area");
597 else
599 if (lsda_encoding == DW_EH_PE_aligned)
600 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
601 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
602 "Language Specific Data Area (none)");
605 else
606 dw2_asm_output_data_uleb128 (0, "Augmentation size");
609 /* Loop through the Call Frame Instructions associated with this FDE. */
610 fde->dw_fde_current_label = begin;
612 size_t from, until, i;
614 from = 0;
615 until = vec_safe_length (fde->dw_fde_cfi);
617 if (fde->dw_fde_second_begin == NULL)
619 else if (!second)
620 until = fde->dw_fde_switch_cfi_index;
621 else
622 from = fde->dw_fde_switch_cfi_index;
624 for (i = from; i < until; i++)
625 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
628 /* If we are to emit a ref/link from function bodies to their frame tables,
629 do it now. This is typically performed to make sure that tables
630 associated with functions are dragged with them and not discarded in
631 garbage collecting links. We need to do this on a per function basis to
632 cope with -ffunction-sections. */
634 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
635 /* Switch to the function section, emit the ref to the tables, and
636 switch *back* into the table section. */
637 switch_to_section (function_section (fde->decl));
638 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
639 switch_to_frame_table_section (for_eh, true);
640 #endif
642 /* Pad the FDE out to an address sized boundary. */
643 ASM_OUTPUT_ALIGN (asm_out_file,
644 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
645 ASM_OUTPUT_LABEL (asm_out_file, l2);
647 j += 2;
650 /* Return true if frame description entry FDE is needed for EH. */
652 static bool
653 fde_needed_for_eh_p (dw_fde_ref fde)
655 if (flag_asynchronous_unwind_tables)
656 return true;
658 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
659 return true;
661 if (fde->uses_eh_lsda)
662 return true;
664 /* If exceptions are enabled, we have collected nothrow info. */
665 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
666 return false;
668 return true;
671 /* Output the call frame information used to record information
672 that relates to calculating the frame pointer, and records the
673 location of saved registers. */
675 static void
676 output_call_frame_info (int for_eh)
678 unsigned int i;
679 dw_fde_ref fde;
680 dw_cfi_ref cfi;
681 char l1[20], l2[20], section_start_label[20];
682 bool any_lsda_needed = false;
683 char augmentation[6];
684 int augmentation_size;
685 int fde_encoding = DW_EH_PE_absptr;
686 int per_encoding = DW_EH_PE_absptr;
687 int lsda_encoding = DW_EH_PE_absptr;
688 int return_reg;
689 rtx personality = NULL;
690 int dw_cie_version;
692 /* Don't emit a CIE if there won't be any FDEs. */
693 if (!fde_vec)
694 return;
696 /* Nothing to do if the assembler's doing it all. */
697 if (dwarf2out_do_cfi_asm ())
698 return;
700 /* If we don't have any functions we'll want to unwind out of, don't emit
701 any EH unwind information. If we make FDEs linkonce, we may have to
702 emit an empty label for an FDE that wouldn't otherwise be emitted. We
703 want to avoid having an FDE kept around when the function it refers to
704 is discarded. Example where this matters: a primary function template
705 in C++ requires EH information, an explicit specialization doesn't. */
706 if (for_eh)
708 bool any_eh_needed = false;
710 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
712 if (fde->uses_eh_lsda)
713 any_eh_needed = any_lsda_needed = true;
714 else if (fde_needed_for_eh_p (fde))
715 any_eh_needed = true;
716 else if (TARGET_USES_WEAK_UNWIND_INFO)
717 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
720 if (!any_eh_needed)
721 return;
724 /* We're going to be generating comments, so turn on app. */
725 if (flag_debug_asm)
726 app_enable ();
728 /* Switch to the proper frame section, first time. */
729 switch_to_frame_table_section (for_eh, false);
731 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
732 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
734 /* Output the CIE. */
735 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
736 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
737 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
738 dw2_asm_output_data (4, 0xffffffff,
739 "Initial length escape value indicating 64-bit DWARF extension");
740 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
741 "Length of Common Information Entry");
742 ASM_OUTPUT_LABEL (asm_out_file, l1);
744 /* Now that the CIE pointer is PC-relative for EH,
745 use 0 to identify the CIE. */
746 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
747 (for_eh ? 0 : DWARF_CIE_ID),
748 "CIE Identifier Tag");
750 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
751 use CIE version 1, unless that would produce incorrect results
752 due to overflowing the return register column. */
753 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
754 dw_cie_version = 1;
755 if (return_reg >= 256 || dwarf_version > 2)
756 dw_cie_version = 3;
757 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
759 augmentation[0] = 0;
760 augmentation_size = 0;
762 personality = current_unit_personality;
763 if (for_eh)
765 char *p;
767 /* Augmentation:
768 z Indicates that a uleb128 is present to size the
769 augmentation section.
770 L Indicates the encoding (and thus presence) of
771 an LSDA pointer in the FDE augmentation.
772 R Indicates a non-default pointer encoding for
773 FDE code pointers.
774 P Indicates the presence of an encoding + language
775 personality routine in the CIE augmentation. */
777 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
778 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
779 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
781 p = augmentation + 1;
782 if (personality)
784 *p++ = 'P';
785 augmentation_size += 1 + size_of_encoded_value (per_encoding);
786 assemble_external_libcall (personality);
788 if (any_lsda_needed)
790 *p++ = 'L';
791 augmentation_size += 1;
793 if (fde_encoding != DW_EH_PE_absptr)
795 *p++ = 'R';
796 augmentation_size += 1;
798 if (p > augmentation + 1)
800 augmentation[0] = 'z';
801 *p = '\0';
804 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
805 if (personality && per_encoding == DW_EH_PE_aligned)
807 int offset = ( 4 /* Length */
808 + 4 /* CIE Id */
809 + 1 /* CIE version */
810 + strlen (augmentation) + 1 /* Augmentation */
811 + size_of_uleb128 (1) /* Code alignment */
812 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
813 + 1 /* RA column */
814 + 1 /* Augmentation size */
815 + 1 /* Personality encoding */ );
816 int pad = -offset & (PTR_SIZE - 1);
818 augmentation_size += pad;
820 /* Augmentations should be small, so there's scarce need to
821 iterate for a solution. Die if we exceed one uleb128 byte. */
822 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
826 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
827 if (dw_cie_version >= 4)
829 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
830 dw2_asm_output_data (1, 0, "CIE Segment Size");
832 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
833 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
834 "CIE Data Alignment Factor");
836 if (dw_cie_version == 1)
837 dw2_asm_output_data (1, return_reg, "CIE RA Column");
838 else
839 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
841 if (augmentation[0])
843 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
844 if (personality)
846 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
847 eh_data_format_name (per_encoding));
848 dw2_asm_output_encoded_addr_rtx (per_encoding,
849 personality,
850 true, NULL);
853 if (any_lsda_needed)
854 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
855 eh_data_format_name (lsda_encoding));
857 if (fde_encoding != DW_EH_PE_absptr)
858 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
859 eh_data_format_name (fde_encoding));
862 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
863 output_cfi (cfi, NULL, for_eh);
865 /* Pad the CIE out to an address sized boundary. */
866 ASM_OUTPUT_ALIGN (asm_out_file,
867 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
868 ASM_OUTPUT_LABEL (asm_out_file, l2);
870 /* Loop through all of the FDE's. */
871 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
873 unsigned int k;
875 /* Don't emit EH unwind info for leaf functions that don't need it. */
876 if (for_eh && !fde_needed_for_eh_p (fde))
877 continue;
879 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
880 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
881 augmentation, any_lsda_needed, lsda_encoding);
884 if (for_eh && targetm.terminate_dw2_eh_frame_info)
885 dw2_asm_output_data (4, 0, "End of Table");
887 /* Turn off app to make assembly quicker. */
888 if (flag_debug_asm)
889 app_disable ();
892 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
894 static void
895 dwarf2out_do_cfi_startproc (bool second)
897 int enc;
898 rtx ref;
899 rtx personality = get_personality_function (current_function_decl);
901 fprintf (asm_out_file, "\t.cfi_startproc\n");
903 if (personality)
905 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
906 ref = personality;
908 /* ??? The GAS support isn't entirely consistent. We have to
909 handle indirect support ourselves, but PC-relative is done
910 in the assembler. Further, the assembler can't handle any
911 of the weirder relocation types. */
912 if (enc & DW_EH_PE_indirect)
913 ref = dw2_force_const_mem (ref, true);
915 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
916 output_addr_const (asm_out_file, ref);
917 fputc ('\n', asm_out_file);
920 if (crtl->uses_eh_lsda)
922 char lab[20];
924 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
925 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
926 current_function_funcdef_no);
927 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
928 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
930 if (enc & DW_EH_PE_indirect)
931 ref = dw2_force_const_mem (ref, true);
933 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
934 output_addr_const (asm_out_file, ref);
935 fputc ('\n', asm_out_file);
939 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
940 this allocation may be done before pass_final. */
942 dw_fde_ref
943 dwarf2out_alloc_current_fde (void)
945 dw_fde_ref fde;
947 fde = ggc_alloc_cleared_dw_fde_node ();
948 fde->decl = current_function_decl;
949 fde->funcdef_number = current_function_funcdef_no;
950 fde->fde_index = vec_safe_length (fde_vec);
951 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
952 fde->uses_eh_lsda = crtl->uses_eh_lsda;
953 fde->nothrow = crtl->nothrow;
954 fde->drap_reg = INVALID_REGNUM;
955 fde->vdrap_reg = INVALID_REGNUM;
957 /* Record the FDE associated with this function. */
958 cfun->fde = fde;
959 vec_safe_push (fde_vec, fde);
961 return fde;
964 /* Output a marker (i.e. a label) for the beginning of a function, before
965 the prologue. */
967 void
968 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
969 const char *file ATTRIBUTE_UNUSED)
971 char label[MAX_ARTIFICIAL_LABEL_BYTES];
972 char * dup_label;
973 dw_fde_ref fde;
974 section *fnsec;
975 bool do_frame;
977 current_function_func_begin_label = NULL;
979 do_frame = dwarf2out_do_frame ();
981 /* ??? current_function_func_begin_label is also used by except.c for
982 call-site information. We must emit this label if it might be used. */
983 if (!do_frame
984 && (!flag_exceptions
985 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
986 return;
988 fnsec = function_section (current_function_decl);
989 switch_to_section (fnsec);
990 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
991 current_function_funcdef_no);
992 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
993 current_function_funcdef_no);
994 dup_label = xstrdup (label);
995 current_function_func_begin_label = dup_label;
997 /* We can elide the fde allocation if we're not emitting debug info. */
998 if (!do_frame)
999 return;
1001 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1002 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1003 would include pass_dwarf2_frame. If we've not created the FDE yet,
1004 do so now. */
1005 fde = cfun->fde;
1006 if (fde == NULL)
1007 fde = dwarf2out_alloc_current_fde ();
1009 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1010 fde->dw_fde_begin = dup_label;
1011 fde->dw_fde_current_label = dup_label;
1012 fde->in_std_section = (fnsec == text_section
1013 || (cold_text_section && fnsec == cold_text_section));
1015 /* We only want to output line number information for the genuine dwarf2
1016 prologue case, not the eh frame case. */
1017 #ifdef DWARF2_DEBUGGING_INFO
1018 if (file)
1019 dwarf2out_source_line (line, file, 0, true);
1020 #endif
1022 if (dwarf2out_do_cfi_asm ())
1023 dwarf2out_do_cfi_startproc (false);
1024 else
1026 rtx personality = get_personality_function (current_function_decl);
1027 if (!current_unit_personality)
1028 current_unit_personality = personality;
1030 /* We cannot keep a current personality per function as without CFI
1031 asm, at the point where we emit the CFI data, there is no current
1032 function anymore. */
1033 if (personality && current_unit_personality != personality)
1034 sorry ("multiple EH personalities are supported only with assemblers "
1035 "supporting .cfi_personality directive");
1039 /* Output a marker (i.e. a label) for the end of the generated code
1040 for a function prologue. This gets called *after* the prologue code has
1041 been generated. */
1043 void
1044 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1045 const char *file ATTRIBUTE_UNUSED)
1047 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1049 /* Output a label to mark the endpoint of the code generated for this
1050 function. */
1051 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1052 current_function_funcdef_no);
1053 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1054 current_function_funcdef_no);
1055 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1058 /* Output a marker (i.e. a label) for the beginning of the generated code
1059 for a function epilogue. This gets called *before* the prologue code has
1060 been generated. */
1062 void
1063 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1064 const char *file ATTRIBUTE_UNUSED)
1066 dw_fde_ref fde = cfun->fde;
1067 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1069 if (fde->dw_fde_vms_begin_epilogue)
1070 return;
1072 /* Output a label to mark the endpoint of the code generated for this
1073 function. */
1074 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1075 current_function_funcdef_no);
1076 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1077 current_function_funcdef_no);
1078 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1081 /* Output a marker (i.e. a label) for the absolute end of the generated code
1082 for a function definition. This gets called *after* the epilogue code has
1083 been generated. */
1085 void
1086 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1087 const char *file ATTRIBUTE_UNUSED)
1089 dw_fde_ref fde;
1090 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1092 last_var_location_insn = NULL_RTX;
1093 cached_next_real_insn = NULL_RTX;
1095 if (dwarf2out_do_cfi_asm ())
1096 fprintf (asm_out_file, "\t.cfi_endproc\n");
1098 /* Output a label to mark the endpoint of the code generated for this
1099 function. */
1100 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1101 current_function_funcdef_no);
1102 ASM_OUTPUT_LABEL (asm_out_file, label);
1103 fde = cfun->fde;
1104 gcc_assert (fde != NULL);
1105 if (fde->dw_fde_second_begin == NULL)
1106 fde->dw_fde_end = xstrdup (label);
1109 void
1110 dwarf2out_frame_finish (void)
1112 /* Output call frame information. */
1113 if (targetm.debug_unwind_info () == UI_DWARF2)
1114 output_call_frame_info (0);
1116 /* Output another copy for the unwinder. */
1117 if ((flag_unwind_tables || flag_exceptions)
1118 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1119 output_call_frame_info (1);
1122 /* Note that the current function section is being used for code. */
1124 static void
1125 dwarf2out_note_section_used (void)
1127 section *sec = current_function_section ();
1128 if (sec == text_section)
1129 text_section_used = true;
1130 else if (sec == cold_text_section)
1131 cold_text_section_used = true;
1134 static void var_location_switch_text_section (void);
1135 static void set_cur_line_info_table (section *);
1137 void
1138 dwarf2out_switch_text_section (void)
1140 section *sect;
1141 dw_fde_ref fde = cfun->fde;
1143 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1145 if (!in_cold_section_p)
1147 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1148 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1149 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1151 else
1153 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1154 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1155 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1157 have_multiple_function_sections = true;
1159 /* There is no need to mark used sections when not debugging. */
1160 if (cold_text_section != NULL)
1161 dwarf2out_note_section_used ();
1163 if (dwarf2out_do_cfi_asm ())
1164 fprintf (asm_out_file, "\t.cfi_endproc\n");
1166 /* Now do the real section switch. */
1167 sect = current_function_section ();
1168 switch_to_section (sect);
1170 fde->second_in_std_section
1171 = (sect == text_section
1172 || (cold_text_section && sect == cold_text_section));
1174 if (dwarf2out_do_cfi_asm ())
1175 dwarf2out_do_cfi_startproc (true);
1177 var_location_switch_text_section ();
1179 if (cold_text_section != NULL)
1180 set_cur_line_info_table (sect);
1183 /* And now, the subset of the debugging information support code necessary
1184 for emitting location expressions. */
1186 /* Data about a single source file. */
1187 struct GTY(()) dwarf_file_data {
1188 const char * filename;
1189 int emitted_number;
1192 typedef struct GTY(()) deferred_locations_struct
1194 tree variable;
1195 dw_die_ref die;
1196 } deferred_locations;
1199 static GTY(()) vec<deferred_locations, va_gc> *deferred_locations_list;
1202 /* Describe an entry into the .debug_addr section. */
1204 enum ate_kind {
1205 ate_kind_rtx,
1206 ate_kind_rtx_dtprel,
1207 ate_kind_label
1210 typedef struct GTY(()) addr_table_entry_struct {
1211 enum ate_kind kind;
1212 unsigned int refcount;
1213 unsigned int index;
1214 union addr_table_entry_struct_union
1216 rtx GTY ((tag ("0"))) rtl;
1217 char * GTY ((tag ("1"))) label;
1219 GTY ((desc ("%1.kind"))) addr;
1221 addr_table_entry;
1223 /* Location lists are ranges + location descriptions for that range,
1224 so you can track variables that are in different places over
1225 their entire life. */
1226 typedef struct GTY(()) dw_loc_list_struct {
1227 dw_loc_list_ref dw_loc_next;
1228 const char *begin; /* Label and addr_entry for start of range */
1229 addr_table_entry *begin_entry;
1230 const char *end; /* Label for end of range */
1231 char *ll_symbol; /* Label for beginning of location list.
1232 Only on head of list */
1233 const char *section; /* Section this loclist is relative to */
1234 dw_loc_descr_ref expr;
1235 hashval_t hash;
1236 /* True if all addresses in this and subsequent lists are known to be
1237 resolved. */
1238 bool resolved_addr;
1239 /* True if this list has been replaced by dw_loc_next. */
1240 bool replaced;
1241 bool emitted;
1242 /* True if the range should be emitted even if begin and end
1243 are the same. */
1244 bool force;
1245 } dw_loc_list_node;
1247 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1249 /* Convert a DWARF stack opcode into its string name. */
1251 static const char *
1252 dwarf_stack_op_name (unsigned int op)
1254 const char *name = get_DW_OP_name (op);
1256 if (name != NULL)
1257 return name;
1259 return "OP_<unknown>";
1262 /* Return a pointer to a newly allocated location description. Location
1263 descriptions are simple expression terms that can be strung
1264 together to form more complicated location (address) descriptions. */
1266 static inline dw_loc_descr_ref
1267 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1268 unsigned HOST_WIDE_INT oprnd2)
1270 dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
1272 descr->dw_loc_opc = op;
1273 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1274 descr->dw_loc_oprnd1.val_entry = NULL;
1275 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1276 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1277 descr->dw_loc_oprnd2.val_entry = NULL;
1278 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1280 return descr;
1283 /* Return a pointer to a newly allocated location description for
1284 REG and OFFSET. */
1286 static inline dw_loc_descr_ref
1287 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1289 if (reg <= 31)
1290 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1291 offset, 0);
1292 else
1293 return new_loc_descr (DW_OP_bregx, reg, offset);
1296 /* Add a location description term to a location description expression. */
1298 static inline void
1299 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1301 dw_loc_descr_ref *d;
1303 /* Find the end of the chain. */
1304 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1307 *d = descr;
1310 /* Compare two location operands for exact equality. */
1312 static bool
1313 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1315 if (a->val_class != b->val_class)
1316 return false;
1317 switch (a->val_class)
1319 case dw_val_class_none:
1320 return true;
1321 case dw_val_class_addr:
1322 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1324 case dw_val_class_offset:
1325 case dw_val_class_unsigned_const:
1326 case dw_val_class_const:
1327 case dw_val_class_range_list:
1328 case dw_val_class_lineptr:
1329 case dw_val_class_macptr:
1330 /* These are all HOST_WIDE_INT, signed or unsigned. */
1331 return a->v.val_unsigned == b->v.val_unsigned;
1333 case dw_val_class_loc:
1334 return a->v.val_loc == b->v.val_loc;
1335 case dw_val_class_loc_list:
1336 return a->v.val_loc_list == b->v.val_loc_list;
1337 case dw_val_class_die_ref:
1338 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1339 case dw_val_class_fde_ref:
1340 return a->v.val_fde_index == b->v.val_fde_index;
1341 case dw_val_class_lbl_id:
1342 case dw_val_class_high_pc:
1343 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1344 case dw_val_class_str:
1345 return a->v.val_str == b->v.val_str;
1346 case dw_val_class_flag:
1347 return a->v.val_flag == b->v.val_flag;
1348 case dw_val_class_file:
1349 return a->v.val_file == b->v.val_file;
1350 case dw_val_class_decl_ref:
1351 return a->v.val_decl_ref == b->v.val_decl_ref;
1353 case dw_val_class_const_double:
1354 return (a->v.val_double.high == b->v.val_double.high
1355 && a->v.val_double.low == b->v.val_double.low);
1357 case dw_val_class_vec:
1359 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1360 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1362 return (a_len == b_len
1363 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1366 case dw_val_class_data8:
1367 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1369 case dw_val_class_vms_delta:
1370 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1371 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1373 gcc_unreachable ();
1376 /* Compare two location atoms for exact equality. */
1378 static bool
1379 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1381 if (a->dw_loc_opc != b->dw_loc_opc)
1382 return false;
1384 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1385 address size, but since we always allocate cleared storage it
1386 should be zero for other types of locations. */
1387 if (a->dtprel != b->dtprel)
1388 return false;
1390 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1391 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1394 /* Compare two complete location expressions for exact equality. */
1396 bool
1397 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1399 while (1)
1401 if (a == b)
1402 return true;
1403 if (a == NULL || b == NULL)
1404 return false;
1405 if (!loc_descr_equal_p_1 (a, b))
1406 return false;
1408 a = a->dw_loc_next;
1409 b = b->dw_loc_next;
1414 /* Add a constant OFFSET to a location expression. */
1416 static void
1417 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1419 dw_loc_descr_ref loc;
1420 HOST_WIDE_INT *p;
1422 gcc_assert (*list_head != NULL);
1424 if (!offset)
1425 return;
1427 /* Find the end of the chain. */
1428 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1431 p = NULL;
1432 if (loc->dw_loc_opc == DW_OP_fbreg
1433 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1434 p = &loc->dw_loc_oprnd1.v.val_int;
1435 else if (loc->dw_loc_opc == DW_OP_bregx)
1436 p = &loc->dw_loc_oprnd2.v.val_int;
1438 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1439 offset. Don't optimize if an signed integer overflow would happen. */
1440 if (p != NULL
1441 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1442 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1443 *p += offset;
1445 else if (offset > 0)
1446 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1448 else
1450 loc->dw_loc_next = int_loc_descriptor (-offset);
1451 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1455 /* Add a constant OFFSET to a location list. */
1457 static void
1458 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1460 dw_loc_list_ref d;
1461 for (d = list_head; d != NULL; d = d->dw_loc_next)
1462 loc_descr_plus_const (&d->expr, offset);
1465 #define DWARF_REF_SIZE \
1466 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1468 static unsigned long int get_base_type_offset (dw_die_ref);
1470 /* Return the size of a location descriptor. */
1472 static unsigned long
1473 size_of_loc_descr (dw_loc_descr_ref loc)
1475 unsigned long size = 1;
1477 switch (loc->dw_loc_opc)
1479 case DW_OP_addr:
1480 size += DWARF2_ADDR_SIZE;
1481 break;
1482 case DW_OP_GNU_addr_index:
1483 case DW_OP_GNU_const_index:
1484 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1485 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1486 break;
1487 case DW_OP_const1u:
1488 case DW_OP_const1s:
1489 size += 1;
1490 break;
1491 case DW_OP_const2u:
1492 case DW_OP_const2s:
1493 size += 2;
1494 break;
1495 case DW_OP_const4u:
1496 case DW_OP_const4s:
1497 size += 4;
1498 break;
1499 case DW_OP_const8u:
1500 case DW_OP_const8s:
1501 size += 8;
1502 break;
1503 case DW_OP_constu:
1504 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1505 break;
1506 case DW_OP_consts:
1507 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1508 break;
1509 case DW_OP_pick:
1510 size += 1;
1511 break;
1512 case DW_OP_plus_uconst:
1513 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1514 break;
1515 case DW_OP_skip:
1516 case DW_OP_bra:
1517 size += 2;
1518 break;
1519 case DW_OP_breg0:
1520 case DW_OP_breg1:
1521 case DW_OP_breg2:
1522 case DW_OP_breg3:
1523 case DW_OP_breg4:
1524 case DW_OP_breg5:
1525 case DW_OP_breg6:
1526 case DW_OP_breg7:
1527 case DW_OP_breg8:
1528 case DW_OP_breg9:
1529 case DW_OP_breg10:
1530 case DW_OP_breg11:
1531 case DW_OP_breg12:
1532 case DW_OP_breg13:
1533 case DW_OP_breg14:
1534 case DW_OP_breg15:
1535 case DW_OP_breg16:
1536 case DW_OP_breg17:
1537 case DW_OP_breg18:
1538 case DW_OP_breg19:
1539 case DW_OP_breg20:
1540 case DW_OP_breg21:
1541 case DW_OP_breg22:
1542 case DW_OP_breg23:
1543 case DW_OP_breg24:
1544 case DW_OP_breg25:
1545 case DW_OP_breg26:
1546 case DW_OP_breg27:
1547 case DW_OP_breg28:
1548 case DW_OP_breg29:
1549 case DW_OP_breg30:
1550 case DW_OP_breg31:
1551 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1552 break;
1553 case DW_OP_regx:
1554 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1555 break;
1556 case DW_OP_fbreg:
1557 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1558 break;
1559 case DW_OP_bregx:
1560 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1561 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1562 break;
1563 case DW_OP_piece:
1564 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1565 break;
1566 case DW_OP_bit_piece:
1567 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1568 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1569 break;
1570 case DW_OP_deref_size:
1571 case DW_OP_xderef_size:
1572 size += 1;
1573 break;
1574 case DW_OP_call2:
1575 size += 2;
1576 break;
1577 case DW_OP_call4:
1578 size += 4;
1579 break;
1580 case DW_OP_call_ref:
1581 size += DWARF_REF_SIZE;
1582 break;
1583 case DW_OP_implicit_value:
1584 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1585 + loc->dw_loc_oprnd1.v.val_unsigned;
1586 break;
1587 case DW_OP_GNU_implicit_pointer:
1588 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1589 break;
1590 case DW_OP_GNU_entry_value:
1592 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1593 size += size_of_uleb128 (op_size) + op_size;
1594 break;
1596 case DW_OP_GNU_const_type:
1598 unsigned long o
1599 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1600 size += size_of_uleb128 (o) + 1;
1601 switch (loc->dw_loc_oprnd2.val_class)
1603 case dw_val_class_vec:
1604 size += loc->dw_loc_oprnd2.v.val_vec.length
1605 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1606 break;
1607 case dw_val_class_const:
1608 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1609 break;
1610 case dw_val_class_const_double:
1611 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1612 break;
1613 default:
1614 gcc_unreachable ();
1616 break;
1618 case DW_OP_GNU_regval_type:
1620 unsigned long o
1621 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1622 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1623 + size_of_uleb128 (o);
1625 break;
1626 case DW_OP_GNU_deref_type:
1628 unsigned long o
1629 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1630 size += 1 + size_of_uleb128 (o);
1632 break;
1633 case DW_OP_GNU_convert:
1634 case DW_OP_GNU_reinterpret:
1635 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1636 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1637 else
1639 unsigned long o
1640 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1641 size += size_of_uleb128 (o);
1643 break;
1644 case DW_OP_GNU_parameter_ref:
1645 size += 4;
1646 break;
1647 default:
1648 break;
1651 return size;
1654 /* Return the size of a series of location descriptors. */
1656 unsigned long
1657 size_of_locs (dw_loc_descr_ref loc)
1659 dw_loc_descr_ref l;
1660 unsigned long size;
1662 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1663 field, to avoid writing to a PCH file. */
1664 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1666 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1667 break;
1668 size += size_of_loc_descr (l);
1670 if (! l)
1671 return size;
1673 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1675 l->dw_loc_addr = size;
1676 size += size_of_loc_descr (l);
1679 return size;
1682 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1683 static void get_ref_die_offset_label (char *, dw_die_ref);
1684 static unsigned long int get_ref_die_offset (dw_die_ref);
1686 /* Output location description stack opcode's operands (if any).
1687 The for_eh_or_skip parameter controls whether register numbers are
1688 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1689 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1690 info). This should be suppressed for the cases that have not been converted
1691 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1693 static void
1694 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1696 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1697 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1699 switch (loc->dw_loc_opc)
1701 #ifdef DWARF2_DEBUGGING_INFO
1702 case DW_OP_const2u:
1703 case DW_OP_const2s:
1704 dw2_asm_output_data (2, val1->v.val_int, NULL);
1705 break;
1706 case DW_OP_const4u:
1707 if (loc->dtprel)
1709 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1710 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1711 val1->v.val_addr);
1712 fputc ('\n', asm_out_file);
1713 break;
1715 /* FALLTHRU */
1716 case DW_OP_const4s:
1717 dw2_asm_output_data (4, val1->v.val_int, NULL);
1718 break;
1719 case DW_OP_const8u:
1720 if (loc->dtprel)
1722 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1723 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1724 val1->v.val_addr);
1725 fputc ('\n', asm_out_file);
1726 break;
1728 /* FALLTHRU */
1729 case DW_OP_const8s:
1730 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1731 dw2_asm_output_data (8, val1->v.val_int, NULL);
1732 break;
1733 case DW_OP_skip:
1734 case DW_OP_bra:
1736 int offset;
1738 gcc_assert (val1->val_class == dw_val_class_loc);
1739 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1741 dw2_asm_output_data (2, offset, NULL);
1743 break;
1744 case DW_OP_implicit_value:
1745 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1746 switch (val2->val_class)
1748 case dw_val_class_const:
1749 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1750 break;
1751 case dw_val_class_vec:
1753 unsigned int elt_size = val2->v.val_vec.elt_size;
1754 unsigned int len = val2->v.val_vec.length;
1755 unsigned int i;
1756 unsigned char *p;
1758 if (elt_size > sizeof (HOST_WIDE_INT))
1760 elt_size /= 2;
1761 len *= 2;
1763 for (i = 0, p = val2->v.val_vec.array;
1764 i < len;
1765 i++, p += elt_size)
1766 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1767 "fp or vector constant word %u", i);
1769 break;
1770 case dw_val_class_const_double:
1772 unsigned HOST_WIDE_INT first, second;
1774 if (WORDS_BIG_ENDIAN)
1776 first = val2->v.val_double.high;
1777 second = val2->v.val_double.low;
1779 else
1781 first = val2->v.val_double.low;
1782 second = val2->v.val_double.high;
1784 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1785 first, NULL);
1786 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1787 second, NULL);
1789 break;
1790 case dw_val_class_addr:
1791 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1792 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1793 break;
1794 default:
1795 gcc_unreachable ();
1797 break;
1798 #else
1799 case DW_OP_const2u:
1800 case DW_OP_const2s:
1801 case DW_OP_const4u:
1802 case DW_OP_const4s:
1803 case DW_OP_const8u:
1804 case DW_OP_const8s:
1805 case DW_OP_skip:
1806 case DW_OP_bra:
1807 case DW_OP_implicit_value:
1808 /* We currently don't make any attempt to make sure these are
1809 aligned properly like we do for the main unwind info, so
1810 don't support emitting things larger than a byte if we're
1811 only doing unwinding. */
1812 gcc_unreachable ();
1813 #endif
1814 case DW_OP_const1u:
1815 case DW_OP_const1s:
1816 dw2_asm_output_data (1, val1->v.val_int, NULL);
1817 break;
1818 case DW_OP_constu:
1819 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1820 break;
1821 case DW_OP_consts:
1822 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1823 break;
1824 case DW_OP_pick:
1825 dw2_asm_output_data (1, val1->v.val_int, NULL);
1826 break;
1827 case DW_OP_plus_uconst:
1828 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1829 break;
1830 case DW_OP_breg0:
1831 case DW_OP_breg1:
1832 case DW_OP_breg2:
1833 case DW_OP_breg3:
1834 case DW_OP_breg4:
1835 case DW_OP_breg5:
1836 case DW_OP_breg6:
1837 case DW_OP_breg7:
1838 case DW_OP_breg8:
1839 case DW_OP_breg9:
1840 case DW_OP_breg10:
1841 case DW_OP_breg11:
1842 case DW_OP_breg12:
1843 case DW_OP_breg13:
1844 case DW_OP_breg14:
1845 case DW_OP_breg15:
1846 case DW_OP_breg16:
1847 case DW_OP_breg17:
1848 case DW_OP_breg18:
1849 case DW_OP_breg19:
1850 case DW_OP_breg20:
1851 case DW_OP_breg21:
1852 case DW_OP_breg22:
1853 case DW_OP_breg23:
1854 case DW_OP_breg24:
1855 case DW_OP_breg25:
1856 case DW_OP_breg26:
1857 case DW_OP_breg27:
1858 case DW_OP_breg28:
1859 case DW_OP_breg29:
1860 case DW_OP_breg30:
1861 case DW_OP_breg31:
1862 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1863 break;
1864 case DW_OP_regx:
1866 unsigned r = val1->v.val_unsigned;
1867 if (for_eh_or_skip >= 0)
1868 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1869 gcc_assert (size_of_uleb128 (r)
1870 == size_of_uleb128 (val1->v.val_unsigned));
1871 dw2_asm_output_data_uleb128 (r, NULL);
1873 break;
1874 case DW_OP_fbreg:
1875 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1876 break;
1877 case DW_OP_bregx:
1879 unsigned r = val1->v.val_unsigned;
1880 if (for_eh_or_skip >= 0)
1881 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1882 gcc_assert (size_of_uleb128 (r)
1883 == size_of_uleb128 (val1->v.val_unsigned));
1884 dw2_asm_output_data_uleb128 (r, NULL);
1885 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1887 break;
1888 case DW_OP_piece:
1889 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1890 break;
1891 case DW_OP_bit_piece:
1892 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1893 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1894 break;
1895 case DW_OP_deref_size:
1896 case DW_OP_xderef_size:
1897 dw2_asm_output_data (1, val1->v.val_int, NULL);
1898 break;
1900 case DW_OP_addr:
1901 if (loc->dtprel)
1903 if (targetm.asm_out.output_dwarf_dtprel)
1905 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
1906 DWARF2_ADDR_SIZE,
1907 val1->v.val_addr);
1908 fputc ('\n', asm_out_file);
1910 else
1911 gcc_unreachable ();
1913 else
1915 #ifdef DWARF2_DEBUGGING_INFO
1916 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
1917 #else
1918 gcc_unreachable ();
1919 #endif
1921 break;
1923 case DW_OP_GNU_addr_index:
1924 case DW_OP_GNU_const_index:
1925 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1926 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
1927 "(index into .debug_addr)");
1928 break;
1930 case DW_OP_GNU_implicit_pointer:
1932 char label[MAX_ARTIFICIAL_LABEL_BYTES
1933 + HOST_BITS_PER_WIDE_INT / 2 + 2];
1934 gcc_assert (val1->val_class == dw_val_class_die_ref);
1935 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
1936 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
1937 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1939 break;
1941 case DW_OP_GNU_entry_value:
1942 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
1943 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
1944 break;
1946 case DW_OP_GNU_const_type:
1948 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
1949 gcc_assert (o);
1950 dw2_asm_output_data_uleb128 (o, NULL);
1951 switch (val2->val_class)
1953 case dw_val_class_const:
1954 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
1955 dw2_asm_output_data (1, l, NULL);
1956 dw2_asm_output_data (l, val2->v.val_int, NULL);
1957 break;
1958 case dw_val_class_vec:
1960 unsigned int elt_size = val2->v.val_vec.elt_size;
1961 unsigned int len = val2->v.val_vec.length;
1962 unsigned int i;
1963 unsigned char *p;
1965 l = len * elt_size;
1966 dw2_asm_output_data (1, l, NULL);
1967 if (elt_size > sizeof (HOST_WIDE_INT))
1969 elt_size /= 2;
1970 len *= 2;
1972 for (i = 0, p = val2->v.val_vec.array;
1973 i < len;
1974 i++, p += elt_size)
1975 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1976 "fp or vector constant word %u", i);
1978 break;
1979 case dw_val_class_const_double:
1981 unsigned HOST_WIDE_INT first, second;
1982 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
1984 dw2_asm_output_data (1, 2 * l, NULL);
1985 if (WORDS_BIG_ENDIAN)
1987 first = val2->v.val_double.high;
1988 second = val2->v.val_double.low;
1990 else
1992 first = val2->v.val_double.low;
1993 second = val2->v.val_double.high;
1995 dw2_asm_output_data (l, first, NULL);
1996 dw2_asm_output_data (l, second, NULL);
1998 break;
1999 default:
2000 gcc_unreachable ();
2003 break;
2004 case DW_OP_GNU_regval_type:
2006 unsigned r = val1->v.val_unsigned;
2007 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2008 gcc_assert (o);
2009 if (for_eh_or_skip >= 0)
2011 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2012 gcc_assert (size_of_uleb128 (r)
2013 == size_of_uleb128 (val1->v.val_unsigned));
2015 dw2_asm_output_data_uleb128 (r, NULL);
2016 dw2_asm_output_data_uleb128 (o, NULL);
2018 break;
2019 case DW_OP_GNU_deref_type:
2021 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2022 gcc_assert (o);
2023 dw2_asm_output_data (1, val1->v.val_int, NULL);
2024 dw2_asm_output_data_uleb128 (o, NULL);
2026 break;
2027 case DW_OP_GNU_convert:
2028 case DW_OP_GNU_reinterpret:
2029 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2030 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2031 else
2033 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2034 gcc_assert (o);
2035 dw2_asm_output_data_uleb128 (o, NULL);
2037 break;
2039 case DW_OP_GNU_parameter_ref:
2041 unsigned long o;
2042 gcc_assert (val1->val_class == dw_val_class_die_ref);
2043 o = get_ref_die_offset (val1->v.val_die_ref.die);
2044 dw2_asm_output_data (4, o, NULL);
2046 break;
2048 default:
2049 /* Other codes have no operands. */
2050 break;
2054 /* Output a sequence of location operations.
2055 The for_eh_or_skip parameter controls whether register numbers are
2056 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2057 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2058 info). This should be suppressed for the cases that have not been converted
2059 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2061 void
2062 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2064 for (; loc != NULL; loc = loc->dw_loc_next)
2066 enum dwarf_location_atom opc = loc->dw_loc_opc;
2067 /* Output the opcode. */
2068 if (for_eh_or_skip >= 0
2069 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2071 unsigned r = (opc - DW_OP_breg0);
2072 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2073 gcc_assert (r <= 31);
2074 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2076 else if (for_eh_or_skip >= 0
2077 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2079 unsigned r = (opc - DW_OP_reg0);
2080 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2081 gcc_assert (r <= 31);
2082 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2085 dw2_asm_output_data (1, opc,
2086 "%s", dwarf_stack_op_name (opc));
2088 /* Output the operand(s) (if any). */
2089 output_loc_operands (loc, for_eh_or_skip);
2093 /* Output location description stack opcode's operands (if any).
2094 The output is single bytes on a line, suitable for .cfi_escape. */
2096 static void
2097 output_loc_operands_raw (dw_loc_descr_ref loc)
2099 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2100 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2102 switch (loc->dw_loc_opc)
2104 case DW_OP_addr:
2105 case DW_OP_GNU_addr_index:
2106 case DW_OP_GNU_const_index:
2107 case DW_OP_implicit_value:
2108 /* We cannot output addresses in .cfi_escape, only bytes. */
2109 gcc_unreachable ();
2111 case DW_OP_const1u:
2112 case DW_OP_const1s:
2113 case DW_OP_pick:
2114 case DW_OP_deref_size:
2115 case DW_OP_xderef_size:
2116 fputc (',', asm_out_file);
2117 dw2_asm_output_data_raw (1, val1->v.val_int);
2118 break;
2120 case DW_OP_const2u:
2121 case DW_OP_const2s:
2122 fputc (',', asm_out_file);
2123 dw2_asm_output_data_raw (2, val1->v.val_int);
2124 break;
2126 case DW_OP_const4u:
2127 case DW_OP_const4s:
2128 fputc (',', asm_out_file);
2129 dw2_asm_output_data_raw (4, val1->v.val_int);
2130 break;
2132 case DW_OP_const8u:
2133 case DW_OP_const8s:
2134 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2135 fputc (',', asm_out_file);
2136 dw2_asm_output_data_raw (8, val1->v.val_int);
2137 break;
2139 case DW_OP_skip:
2140 case DW_OP_bra:
2142 int offset;
2144 gcc_assert (val1->val_class == dw_val_class_loc);
2145 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2147 fputc (',', asm_out_file);
2148 dw2_asm_output_data_raw (2, offset);
2150 break;
2152 case DW_OP_regx:
2154 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2155 gcc_assert (size_of_uleb128 (r)
2156 == size_of_uleb128 (val1->v.val_unsigned));
2157 fputc (',', asm_out_file);
2158 dw2_asm_output_data_uleb128_raw (r);
2160 break;
2162 case DW_OP_constu:
2163 case DW_OP_plus_uconst:
2164 case DW_OP_piece:
2165 fputc (',', asm_out_file);
2166 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2167 break;
2169 case DW_OP_bit_piece:
2170 fputc (',', asm_out_file);
2171 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2172 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2173 break;
2175 case DW_OP_consts:
2176 case DW_OP_breg0:
2177 case DW_OP_breg1:
2178 case DW_OP_breg2:
2179 case DW_OP_breg3:
2180 case DW_OP_breg4:
2181 case DW_OP_breg5:
2182 case DW_OP_breg6:
2183 case DW_OP_breg7:
2184 case DW_OP_breg8:
2185 case DW_OP_breg9:
2186 case DW_OP_breg10:
2187 case DW_OP_breg11:
2188 case DW_OP_breg12:
2189 case DW_OP_breg13:
2190 case DW_OP_breg14:
2191 case DW_OP_breg15:
2192 case DW_OP_breg16:
2193 case DW_OP_breg17:
2194 case DW_OP_breg18:
2195 case DW_OP_breg19:
2196 case DW_OP_breg20:
2197 case DW_OP_breg21:
2198 case DW_OP_breg22:
2199 case DW_OP_breg23:
2200 case DW_OP_breg24:
2201 case DW_OP_breg25:
2202 case DW_OP_breg26:
2203 case DW_OP_breg27:
2204 case DW_OP_breg28:
2205 case DW_OP_breg29:
2206 case DW_OP_breg30:
2207 case DW_OP_breg31:
2208 case DW_OP_fbreg:
2209 fputc (',', asm_out_file);
2210 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2211 break;
2213 case DW_OP_bregx:
2215 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2216 gcc_assert (size_of_uleb128 (r)
2217 == size_of_uleb128 (val1->v.val_unsigned));
2218 fputc (',', asm_out_file);
2219 dw2_asm_output_data_uleb128_raw (r);
2220 fputc (',', asm_out_file);
2221 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2223 break;
2225 case DW_OP_GNU_implicit_pointer:
2226 case DW_OP_GNU_entry_value:
2227 case DW_OP_GNU_const_type:
2228 case DW_OP_GNU_regval_type:
2229 case DW_OP_GNU_deref_type:
2230 case DW_OP_GNU_convert:
2231 case DW_OP_GNU_reinterpret:
2232 case DW_OP_GNU_parameter_ref:
2233 gcc_unreachable ();
2234 break;
2236 default:
2237 /* Other codes have no operands. */
2238 break;
2242 void
2243 output_loc_sequence_raw (dw_loc_descr_ref loc)
2245 while (1)
2247 enum dwarf_location_atom opc = loc->dw_loc_opc;
2248 /* Output the opcode. */
2249 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2251 unsigned r = (opc - DW_OP_breg0);
2252 r = DWARF2_FRAME_REG_OUT (r, 1);
2253 gcc_assert (r <= 31);
2254 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2256 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2258 unsigned r = (opc - DW_OP_reg0);
2259 r = DWARF2_FRAME_REG_OUT (r, 1);
2260 gcc_assert (r <= 31);
2261 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2263 /* Output the opcode. */
2264 fprintf (asm_out_file, "%#x", opc);
2265 output_loc_operands_raw (loc);
2267 if (!loc->dw_loc_next)
2268 break;
2269 loc = loc->dw_loc_next;
2271 fputc (',', asm_out_file);
2275 /* This function builds a dwarf location descriptor sequence from a
2276 dw_cfa_location, adding the given OFFSET to the result of the
2277 expression. */
2279 struct dw_loc_descr_struct *
2280 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2282 struct dw_loc_descr_struct *head, *tmp;
2284 offset += cfa->offset;
2286 if (cfa->indirect)
2288 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2289 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2290 head->dw_loc_oprnd1.val_entry = NULL;
2291 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2292 add_loc_descr (&head, tmp);
2293 if (offset != 0)
2295 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2296 add_loc_descr (&head, tmp);
2299 else
2300 head = new_reg_loc_descr (cfa->reg, offset);
2302 return head;
2305 /* This function builds a dwarf location descriptor sequence for
2306 the address at OFFSET from the CFA when stack is aligned to
2307 ALIGNMENT byte. */
2309 struct dw_loc_descr_struct *
2310 build_cfa_aligned_loc (dw_cfa_location *cfa,
2311 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2313 struct dw_loc_descr_struct *head;
2314 unsigned int dwarf_fp
2315 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2317 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2318 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2320 head = new_reg_loc_descr (dwarf_fp, 0);
2321 add_loc_descr (&head, int_loc_descriptor (alignment));
2322 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2323 loc_descr_plus_const (&head, offset);
2325 else
2326 head = new_reg_loc_descr (dwarf_fp, offset);
2327 return head;
2330 /* And now, the support for symbolic debugging information. */
2332 /* .debug_str support. */
2333 static int output_indirect_string (void **, void *);
2335 static void dwarf2out_init (const char *);
2336 static void dwarf2out_finish (const char *);
2337 static void dwarf2out_assembly_start (void);
2338 static void dwarf2out_define (unsigned int, const char *);
2339 static void dwarf2out_undef (unsigned int, const char *);
2340 static void dwarf2out_start_source_file (unsigned, const char *);
2341 static void dwarf2out_end_source_file (unsigned);
2342 static void dwarf2out_function_decl (tree);
2343 static void dwarf2out_begin_block (unsigned, unsigned);
2344 static void dwarf2out_end_block (unsigned, unsigned);
2345 static bool dwarf2out_ignore_block (const_tree);
2346 static void dwarf2out_global_decl (tree);
2347 static void dwarf2out_type_decl (tree, int);
2348 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2349 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2350 dw_die_ref);
2351 static void dwarf2out_abstract_function (tree);
2352 static void dwarf2out_var_location (rtx);
2353 static void dwarf2out_begin_function (tree);
2354 static void dwarf2out_end_function (unsigned int);
2355 static void dwarf2out_set_name (tree, tree);
2357 /* The debug hooks structure. */
2359 const struct gcc_debug_hooks dwarf2_debug_hooks =
2361 dwarf2out_init,
2362 dwarf2out_finish,
2363 dwarf2out_assembly_start,
2364 dwarf2out_define,
2365 dwarf2out_undef,
2366 dwarf2out_start_source_file,
2367 dwarf2out_end_source_file,
2368 dwarf2out_begin_block,
2369 dwarf2out_end_block,
2370 dwarf2out_ignore_block,
2371 dwarf2out_source_line,
2372 dwarf2out_begin_prologue,
2373 #if VMS_DEBUGGING_INFO
2374 dwarf2out_vms_end_prologue,
2375 dwarf2out_vms_begin_epilogue,
2376 #else
2377 debug_nothing_int_charstar,
2378 debug_nothing_int_charstar,
2379 #endif
2380 dwarf2out_end_epilogue,
2381 dwarf2out_begin_function,
2382 dwarf2out_end_function, /* end_function */
2383 dwarf2out_function_decl, /* function_decl */
2384 dwarf2out_global_decl,
2385 dwarf2out_type_decl, /* type_decl */
2386 dwarf2out_imported_module_or_decl,
2387 debug_nothing_tree, /* deferred_inline_function */
2388 /* The DWARF 2 backend tries to reduce debugging bloat by not
2389 emitting the abstract description of inline functions until
2390 something tries to reference them. */
2391 dwarf2out_abstract_function, /* outlining_inline_function */
2392 debug_nothing_rtx, /* label */
2393 debug_nothing_int, /* handle_pch */
2394 dwarf2out_var_location,
2395 dwarf2out_switch_text_section,
2396 dwarf2out_set_name,
2397 1, /* start_end_main_source_file */
2398 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2401 /* NOTE: In the comments in this file, many references are made to
2402 "Debugging Information Entries". This term is abbreviated as `DIE'
2403 throughout the remainder of this file. */
2405 /* An internal representation of the DWARF output is built, and then
2406 walked to generate the DWARF debugging info. The walk of the internal
2407 representation is done after the entire program has been compiled.
2408 The types below are used to describe the internal representation. */
2410 /* Whether to put type DIEs into their own section .debug_types instead
2411 of making them part of the .debug_info section. Only supported for
2412 Dwarf V4 or higher and the user didn't disable them through
2413 -fno-debug-types-section. It is more efficient to put them in a
2414 separate comdat sections since the linker will then be able to
2415 remove duplicates. But not all tools support .debug_types sections
2416 yet. */
2418 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2420 /* Various DIE's use offsets relative to the beginning of the
2421 .debug_info section to refer to each other. */
2423 typedef long int dw_offset;
2425 /* Define typedefs here to avoid circular dependencies. */
2427 typedef struct dw_attr_struct *dw_attr_ref;
2428 typedef struct dw_line_info_struct *dw_line_info_ref;
2429 typedef struct pubname_struct *pubname_ref;
2430 typedef struct dw_ranges_struct *dw_ranges_ref;
2431 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
2432 typedef struct comdat_type_struct *comdat_type_node_ref;
2434 /* The entries in the line_info table more-or-less mirror the opcodes
2435 that are used in the real dwarf line table. Arrays of these entries
2436 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2437 supported. */
2439 enum dw_line_info_opcode {
2440 /* Emit DW_LNE_set_address; the operand is the label index. */
2441 LI_set_address,
2443 /* Emit a row to the matrix with the given line. This may be done
2444 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2445 special opcodes. */
2446 LI_set_line,
2448 /* Emit a DW_LNS_set_file. */
2449 LI_set_file,
2451 /* Emit a DW_LNS_set_column. */
2452 LI_set_column,
2454 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2455 LI_negate_stmt,
2457 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2458 LI_set_prologue_end,
2459 LI_set_epilogue_begin,
2461 /* Emit a DW_LNE_set_discriminator. */
2462 LI_set_discriminator
2465 typedef struct GTY(()) dw_line_info_struct {
2466 enum dw_line_info_opcode opcode;
2467 unsigned int val;
2468 } dw_line_info_entry;
2471 typedef struct GTY(()) dw_line_info_table_struct {
2472 /* The label that marks the end of this section. */
2473 const char *end_label;
2475 /* The values for the last row of the matrix, as collected in the table.
2476 These are used to minimize the changes to the next row. */
2477 unsigned int file_num;
2478 unsigned int line_num;
2479 unsigned int column_num;
2480 int discrim_num;
2481 bool is_stmt;
2482 bool in_use;
2484 vec<dw_line_info_entry, va_gc> *entries;
2485 } dw_line_info_table;
2487 typedef dw_line_info_table *dw_line_info_table_p;
2490 /* Each DIE attribute has a field specifying the attribute kind,
2491 a link to the next attribute in the chain, and an attribute value.
2492 Attributes are typically linked below the DIE they modify. */
2494 typedef struct GTY(()) dw_attr_struct {
2495 enum dwarf_attribute dw_attr;
2496 dw_val_node dw_attr_val;
2498 dw_attr_node;
2501 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2502 The children of each node form a circular list linked by
2503 die_sib. die_child points to the node *before* the "first" child node. */
2505 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
2506 union die_symbol_or_type_node
2508 const char * GTY ((tag ("0"))) die_symbol;
2509 comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
2511 GTY ((desc ("%0.comdat_type_p"))) die_id;
2512 vec<dw_attr_node, va_gc> *die_attr;
2513 dw_die_ref die_parent;
2514 dw_die_ref die_child;
2515 dw_die_ref die_sib;
2516 dw_die_ref die_definition; /* ref from a specification to its definition */
2517 dw_offset die_offset;
2518 unsigned long die_abbrev;
2519 int die_mark;
2520 unsigned int decl_id;
2521 enum dwarf_tag die_tag;
2522 /* Die is used and must not be pruned as unused. */
2523 BOOL_BITFIELD die_perennial_p : 1;
2524 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2525 /* Lots of spare bits. */
2527 die_node;
2529 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2530 #define FOR_EACH_CHILD(die, c, expr) do { \
2531 c = die->die_child; \
2532 if (c) do { \
2533 c = c->die_sib; \
2534 expr; \
2535 } while (c != die->die_child); \
2536 } while (0)
2538 /* The pubname structure */
2540 typedef struct GTY(()) pubname_struct {
2541 dw_die_ref die;
2542 const char *name;
2544 pubname_entry;
2547 struct GTY(()) dw_ranges_struct {
2548 /* If this is positive, it's a block number, otherwise it's a
2549 bitwise-negated index into dw_ranges_by_label. */
2550 int num;
2553 /* A structure to hold a macinfo entry. */
2555 typedef struct GTY(()) macinfo_struct {
2556 unsigned char code;
2557 unsigned HOST_WIDE_INT lineno;
2558 const char *info;
2560 macinfo_entry;
2563 struct GTY(()) dw_ranges_by_label_struct {
2564 const char *begin;
2565 const char *end;
2568 /* The comdat type node structure. */
2569 typedef struct GTY(()) comdat_type_struct
2571 dw_die_ref root_die;
2572 dw_die_ref type_die;
2573 dw_die_ref skeleton_die;
2574 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2575 struct comdat_type_struct *next;
2577 comdat_type_node;
2579 /* The limbo die list structure. */
2580 typedef struct GTY(()) limbo_die_struct {
2581 dw_die_ref die;
2582 tree created_for;
2583 struct limbo_die_struct *next;
2585 limbo_die_node;
2587 typedef struct skeleton_chain_struct
2589 dw_die_ref old_die;
2590 dw_die_ref new_die;
2591 struct skeleton_chain_struct *parent;
2593 skeleton_chain_node;
2595 /* Define a macro which returns nonzero for a TYPE_DECL which was
2596 implicitly generated for a type.
2598 Note that, unlike the C front-end (which generates a NULL named
2599 TYPE_DECL node for each complete tagged type, each array type,
2600 and each function type node created) the C++ front-end generates
2601 a _named_ TYPE_DECL node for each tagged type node created.
2602 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2603 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2604 front-end, but for each type, tagged or not. */
2606 #define TYPE_DECL_IS_STUB(decl) \
2607 (DECL_NAME (decl) == NULL_TREE \
2608 || (DECL_ARTIFICIAL (decl) \
2609 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
2610 /* This is necessary for stub decls that \
2611 appear in nested inline functions. */ \
2612 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2613 && (decl_ultimate_origin (decl) \
2614 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2616 /* Information concerning the compilation unit's programming
2617 language, and compiler version. */
2619 /* Fixed size portion of the DWARF compilation unit header. */
2620 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2621 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2623 /* Fixed size portion of the DWARF comdat type unit header. */
2624 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2625 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2626 + DWARF_OFFSET_SIZE)
2628 /* Fixed size portion of public names info. */
2629 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2631 /* Fixed size portion of the address range info. */
2632 #define DWARF_ARANGES_HEADER_SIZE \
2633 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2634 DWARF2_ADDR_SIZE * 2) \
2635 - DWARF_INITIAL_LENGTH_SIZE)
2637 /* Size of padding portion in the address range info. It must be
2638 aligned to twice the pointer size. */
2639 #define DWARF_ARANGES_PAD_SIZE \
2640 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2641 DWARF2_ADDR_SIZE * 2) \
2642 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2644 /* Use assembler line directives if available. */
2645 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2646 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2647 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2648 #else
2649 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2650 #endif
2651 #endif
2653 /* Minimum line offset in a special line info. opcode.
2654 This value was chosen to give a reasonable range of values. */
2655 #define DWARF_LINE_BASE -10
2657 /* First special line opcode - leave room for the standard opcodes. */
2658 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
2660 /* Range of line offsets in a special line info. opcode. */
2661 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
2663 /* Flag that indicates the initial value of the is_stmt_start flag.
2664 In the present implementation, we do not mark any lines as
2665 the beginning of a source statement, because that information
2666 is not made available by the GCC front-end. */
2667 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2669 /* Maximum number of operations per instruction bundle. */
2670 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2671 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2672 #endif
2674 /* This location is used by calc_die_sizes() to keep track
2675 the offset of each DIE within the .debug_info section. */
2676 static unsigned long next_die_offset;
2678 /* Record the root of the DIE's built for the current compilation unit. */
2679 static GTY(()) dw_die_ref single_comp_unit_die;
2681 /* A list of type DIEs that have been separated into comdat sections. */
2682 static GTY(()) comdat_type_node *comdat_type_list;
2684 /* A list of DIEs with a NULL parent waiting to be relocated. */
2685 static GTY(()) limbo_die_node *limbo_die_list;
2687 /* A list of DIEs for which we may have to generate
2688 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
2689 static GTY(()) limbo_die_node *deferred_asm_name;
2691 /* Filenames referenced by this compilation unit. */
2692 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
2694 /* A hash table of references to DIE's that describe declarations.
2695 The key is a DECL_UID() which is a unique number identifying each decl. */
2696 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
2698 /* A hash table of references to DIE's that describe COMMON blocks.
2699 The key is DECL_UID() ^ die_parent. */
2700 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
2702 typedef struct GTY(()) die_arg_entry_struct {
2703 dw_die_ref die;
2704 tree arg;
2705 } die_arg_entry;
2708 /* Node of the variable location list. */
2709 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2710 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2711 EXPR_LIST chain. For small bitsizes, bitsize is encoded
2712 in mode of the EXPR_LIST node and first EXPR_LIST operand
2713 is either NOTE_INSN_VAR_LOCATION for a piece with a known
2714 location or NULL for padding. For larger bitsizes,
2715 mode is 0 and first operand is a CONCAT with bitsize
2716 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2717 NULL as second operand. */
2718 rtx GTY (()) loc;
2719 const char * GTY (()) label;
2720 struct var_loc_node * GTY (()) next;
2723 /* Variable location list. */
2724 struct GTY (()) var_loc_list_def {
2725 struct var_loc_node * GTY (()) first;
2727 /* Pointer to the last but one or last element of the
2728 chained list. If the list is empty, both first and
2729 last are NULL, if the list contains just one node
2730 or the last node certainly is not redundant, it points
2731 to the last node, otherwise points to the last but one.
2732 Do not mark it for GC because it is marked through the chain. */
2733 struct var_loc_node * GTY ((skip ("%h"))) last;
2735 /* Pointer to the last element before section switch,
2736 if NULL, either sections weren't switched or first
2737 is after section switch. */
2738 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2740 /* DECL_UID of the variable decl. */
2741 unsigned int decl_id;
2743 typedef struct var_loc_list_def var_loc_list;
2745 /* Call argument location list. */
2746 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2747 rtx GTY (()) call_arg_loc_note;
2748 const char * GTY (()) label;
2749 tree GTY (()) block;
2750 bool tail_call_p;
2751 rtx GTY (()) symbol_ref;
2752 struct call_arg_loc_node * GTY (()) next;
2756 /* Table of decl location linked lists. */
2757 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
2759 /* Head and tail of call_arg_loc chain. */
2760 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2761 static struct call_arg_loc_node *call_arg_loc_last;
2763 /* Number of call sites in the current function. */
2764 static int call_site_count = -1;
2765 /* Number of tail call sites in the current function. */
2766 static int tail_call_site_count = -1;
2768 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
2769 DIEs. */
2770 static vec<dw_die_ref> block_map;
2772 /* A cached location list. */
2773 struct GTY (()) cached_dw_loc_list_def {
2774 /* The DECL_UID of the decl that this entry describes. */
2775 unsigned int decl_id;
2777 /* The cached location list. */
2778 dw_loc_list_ref loc_list;
2780 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2782 /* Table of cached location lists. */
2783 static GTY ((param_is (cached_dw_loc_list))) htab_t cached_dw_loc_list_table;
2785 /* A pointer to the base of a list of references to DIE's that
2786 are uniquely identified by their tag, presence/absence of
2787 children DIE's, and list of attribute/value pairs. */
2788 static GTY((length ("abbrev_die_table_allocated")))
2789 dw_die_ref *abbrev_die_table;
2791 /* Number of elements currently allocated for abbrev_die_table. */
2792 static GTY(()) unsigned abbrev_die_table_allocated;
2794 /* Number of elements in type_die_table currently in use. */
2795 static GTY(()) unsigned abbrev_die_table_in_use;
2797 /* Size (in elements) of increments by which we may expand the
2798 abbrev_die_table. */
2799 #define ABBREV_DIE_TABLE_INCREMENT 256
2801 /* A global counter for generating labels for line number data. */
2802 static unsigned int line_info_label_num;
2804 /* The current table to which we should emit line number information
2805 for the current function. This will be set up at the beginning of
2806 assembly for the function. */
2807 static dw_line_info_table *cur_line_info_table;
2809 /* The two default tables of line number info. */
2810 static GTY(()) dw_line_info_table *text_section_line_info;
2811 static GTY(()) dw_line_info_table *cold_text_section_line_info;
2813 /* The set of all non-default tables of line number info. */
2814 static GTY(()) vec<dw_line_info_table_p, va_gc> *separate_line_info;
2816 /* A flag to tell pubnames/types export if there is an info section to
2817 refer to. */
2818 static bool info_section_emitted;
2820 /* A pointer to the base of a table that contains a list of publicly
2821 accessible names. */
2822 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
2824 /* A pointer to the base of a table that contains a list of publicly
2825 accessible types. */
2826 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
2828 /* A pointer to the base of a table that contains a list of macro
2829 defines/undefines (and file start/end markers). */
2830 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
2832 /* True if .debug_macinfo or .debug_macros section is going to be
2833 emitted. */
2834 #define have_macinfo \
2835 (debug_info_level >= DINFO_LEVEL_VERBOSE \
2836 && !macinfo_table->is_empty ())
2838 /* Array of dies for which we should generate .debug_ranges info. */
2839 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
2841 /* Number of elements currently allocated for ranges_table. */
2842 static GTY(()) unsigned ranges_table_allocated;
2844 /* Number of elements in ranges_table currently in use. */
2845 static GTY(()) unsigned ranges_table_in_use;
2847 /* Array of pairs of labels referenced in ranges_table. */
2848 static GTY ((length ("ranges_by_label_allocated")))
2849 dw_ranges_by_label_ref ranges_by_label;
2851 /* Number of elements currently allocated for ranges_by_label. */
2852 static GTY(()) unsigned ranges_by_label_allocated;
2854 /* Number of elements in ranges_by_label currently in use. */
2855 static GTY(()) unsigned ranges_by_label_in_use;
2857 /* Size (in elements) of increments by which we may expand the
2858 ranges_table. */
2859 #define RANGES_TABLE_INCREMENT 64
2861 /* Whether we have location lists that need outputting */
2862 static GTY(()) bool have_location_lists;
2864 /* Unique label counter. */
2865 static GTY(()) unsigned int loclabel_num;
2867 /* Unique label counter for point-of-call tables. */
2868 static GTY(()) unsigned int poc_label_num;
2870 /* Record whether the function being analyzed contains inlined functions. */
2871 static int current_function_has_inlines;
2873 /* The last file entry emitted by maybe_emit_file(). */
2874 static GTY(()) struct dwarf_file_data * last_emitted_file;
2876 /* Number of internal labels generated by gen_internal_sym(). */
2877 static GTY(()) int label_num;
2879 /* Cached result of previous call to lookup_filename. */
2880 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
2882 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
2884 /* Instances of generic types for which we need to generate debug
2885 info that describe their generic parameters and arguments. That
2886 generation needs to happen once all types are properly laid out so
2887 we do it at the end of compilation. */
2888 static GTY(()) vec<tree, va_gc> *generic_type_instances;
2890 /* Offset from the "steady-state frame pointer" to the frame base,
2891 within the current function. */
2892 static HOST_WIDE_INT frame_pointer_fb_offset;
2893 static bool frame_pointer_fb_offset_valid;
2895 static vec<dw_die_ref> base_types;
2897 /* Forward declarations for functions defined in this file. */
2899 static int is_pseudo_reg (const_rtx);
2900 static tree type_main_variant (tree);
2901 static int is_tagged_type (const_tree);
2902 static const char *dwarf_tag_name (unsigned);
2903 static const char *dwarf_attr_name (unsigned);
2904 static const char *dwarf_form_name (unsigned);
2905 static tree decl_ultimate_origin (const_tree);
2906 static tree decl_class_context (tree);
2907 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
2908 static inline enum dw_val_class AT_class (dw_attr_ref);
2909 static inline unsigned int AT_index (dw_attr_ref);
2910 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
2911 static inline unsigned AT_flag (dw_attr_ref);
2912 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
2913 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
2914 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
2915 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
2916 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
2917 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2918 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
2919 unsigned int, unsigned char *);
2920 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
2921 static hashval_t debug_str_do_hash (const void *);
2922 static int debug_str_eq (const void *, const void *);
2923 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
2924 static inline const char *AT_string (dw_attr_ref);
2925 static enum dwarf_form AT_string_form (dw_attr_ref);
2926 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
2927 static void add_AT_specification (dw_die_ref, dw_die_ref);
2928 static inline dw_die_ref AT_ref (dw_attr_ref);
2929 static inline int AT_ref_external (dw_attr_ref);
2930 static inline void set_AT_ref_external (dw_attr_ref, int);
2931 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
2932 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
2933 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
2934 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
2935 dw_loc_list_ref);
2936 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
2937 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
2938 static void remove_addr_table_entry (addr_table_entry *);
2939 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
2940 static inline rtx AT_addr (dw_attr_ref);
2941 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
2942 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
2943 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
2944 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
2945 unsigned HOST_WIDE_INT);
2946 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
2947 unsigned long, bool);
2948 static inline const char *AT_lbl (dw_attr_ref);
2949 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
2950 static const char *get_AT_low_pc (dw_die_ref);
2951 static const char *get_AT_hi_pc (dw_die_ref);
2952 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
2953 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
2954 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
2955 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
2956 static bool is_cxx (void);
2957 static bool is_fortran (void);
2958 static bool is_ada (void);
2959 static void remove_AT (dw_die_ref, enum dwarf_attribute);
2960 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
2961 static void add_child_die (dw_die_ref, dw_die_ref);
2962 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
2963 static dw_die_ref lookup_type_die (tree);
2964 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
2965 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
2966 static void equate_type_number_to_die (tree, dw_die_ref);
2967 static hashval_t decl_die_table_hash (const void *);
2968 static int decl_die_table_eq (const void *, const void *);
2969 static dw_die_ref lookup_decl_die (tree);
2970 static hashval_t common_block_die_table_hash (const void *);
2971 static int common_block_die_table_eq (const void *, const void *);
2972 static hashval_t decl_loc_table_hash (const void *);
2973 static int decl_loc_table_eq (const void *, const void *);
2974 static var_loc_list *lookup_decl_loc (const_tree);
2975 static void equate_decl_number_to_die (tree, dw_die_ref);
2976 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
2977 static void print_spaces (FILE *);
2978 static void print_die (dw_die_ref, FILE *);
2979 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
2980 static dw_die_ref pop_compile_unit (dw_die_ref);
2981 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
2982 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
2983 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
2984 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
2985 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
2986 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
2987 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
2988 struct md5_ctx *, int *);
2989 struct checksum_attributes;
2990 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
2991 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
2992 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
2993 static void generate_type_signature (dw_die_ref, comdat_type_node *);
2994 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
2995 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
2996 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
2997 static int same_die_p (dw_die_ref, dw_die_ref, int *);
2998 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
2999 static void compute_section_prefix (dw_die_ref);
3000 static int is_type_die (dw_die_ref);
3001 static int is_comdat_die (dw_die_ref);
3002 static int is_symbol_die (dw_die_ref);
3003 static void assign_symbol_names (dw_die_ref);
3004 static void break_out_includes (dw_die_ref);
3005 static int is_declaration_die (dw_die_ref);
3006 static int should_move_die_to_comdat (dw_die_ref);
3007 static dw_die_ref clone_as_declaration (dw_die_ref);
3008 static dw_die_ref clone_die (dw_die_ref);
3009 static dw_die_ref clone_tree (dw_die_ref);
3010 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3011 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3012 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3013 static dw_die_ref generate_skeleton (dw_die_ref);
3014 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3015 dw_die_ref,
3016 dw_die_ref);
3017 static void break_out_comdat_types (dw_die_ref);
3018 static dw_die_ref copy_ancestor_tree (dw_die_ref, dw_die_ref, htab_t);
3019 static void copy_decls_walk (dw_die_ref, dw_die_ref, htab_t);
3020 static void copy_decls_for_unworthy_types (dw_die_ref);
3022 static hashval_t htab_cu_hash (const void *);
3023 static int htab_cu_eq (const void *, const void *);
3024 static void htab_cu_del (void *);
3025 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
3026 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
3027 static void add_sibling_attributes (dw_die_ref);
3028 static void build_abbrev_table (dw_die_ref, htab_t);
3029 static void output_location_lists (dw_die_ref);
3030 static int constant_size (unsigned HOST_WIDE_INT);
3031 static unsigned long size_of_die (dw_die_ref);
3032 static void calc_die_sizes (dw_die_ref);
3033 static void calc_base_type_die_sizes (void);
3034 static void mark_dies (dw_die_ref);
3035 static void unmark_dies (dw_die_ref);
3036 static void unmark_all_dies (dw_die_ref);
3037 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3038 static unsigned long size_of_aranges (void);
3039 static enum dwarf_form value_format (dw_attr_ref);
3040 static void output_value_format (dw_attr_ref);
3041 static void output_abbrev_section (void);
3042 static void output_die_abbrevs (unsigned long, dw_die_ref);
3043 static void output_die_symbol (dw_die_ref);
3044 static void output_die (dw_die_ref);
3045 static void output_compilation_unit_header (void);
3046 static void output_comp_unit (dw_die_ref, int);
3047 static void output_comdat_type_unit (comdat_type_node *);
3048 static const char *dwarf2_name (tree, int);
3049 static void add_pubname (tree, dw_die_ref);
3050 static void add_enumerator_pubname (const char *, dw_die_ref);
3051 static void add_pubname_string (const char *, dw_die_ref);
3052 static void add_pubtype (tree, dw_die_ref);
3053 static void output_pubnames (vec<pubname_entry, va_gc> *);
3054 static void output_aranges (unsigned long);
3055 static unsigned int add_ranges_num (int);
3056 static unsigned int add_ranges (const_tree);
3057 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3058 bool *, bool);
3059 static void output_ranges (void);
3060 static dw_line_info_table *new_line_info_table (void);
3061 static void output_line_info (bool);
3062 static void output_file_names (void);
3063 static dw_die_ref base_type_die (tree);
3064 static int is_base_type (tree);
3065 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3066 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
3067 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3068 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3069 static int type_is_enum (const_tree);
3070 static unsigned int dbx_reg_number (const_rtx);
3071 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3072 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3073 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3074 enum var_init_status);
3075 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3076 enum var_init_status);
3077 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3078 enum var_init_status);
3079 static int is_based_loc (const_rtx);
3080 static int resolve_one_addr (rtx *, void *);
3081 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3082 enum var_init_status);
3083 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
3084 enum var_init_status);
3085 static dw_loc_list_ref loc_list_from_tree (tree, int);
3086 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
3087 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3088 static tree field_type (const_tree);
3089 static unsigned int simple_type_align_in_bits (const_tree);
3090 static unsigned int simple_decl_align_in_bits (const_tree);
3091 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3092 static HOST_WIDE_INT field_byte_offset (const_tree);
3093 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3094 dw_loc_list_ref);
3095 static void add_data_member_location_attribute (dw_die_ref, tree);
3096 static bool add_const_value_attribute (dw_die_ref, rtx);
3097 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3098 static void insert_double (double_int, unsigned char *);
3099 static void insert_float (const_rtx, unsigned char *);
3100 static rtx rtl_for_decl_location (tree);
3101 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3102 enum dwarf_attribute);
3103 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3104 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3105 static void add_name_attribute (dw_die_ref, const char *);
3106 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3107 static void add_comp_dir_attribute (dw_die_ref);
3108 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
3109 static void add_subscript_info (dw_die_ref, tree, bool);
3110 static void add_byte_size_attribute (dw_die_ref, tree);
3111 static void add_bit_offset_attribute (dw_die_ref, tree);
3112 static void add_bit_size_attribute (dw_die_ref, tree);
3113 static void add_prototyped_attribute (dw_die_ref, tree);
3114 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3115 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3116 static void add_src_coords_attributes (dw_die_ref, tree);
3117 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3118 static void push_decl_scope (tree);
3119 static void pop_decl_scope (void);
3120 static dw_die_ref scope_die_for (tree, dw_die_ref);
3121 static inline int local_scope_p (dw_die_ref);
3122 static inline int class_scope_p (dw_die_ref);
3123 static inline int class_or_namespace_scope_p (dw_die_ref);
3124 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
3125 static void add_calling_convention_attribute (dw_die_ref, tree);
3126 static const char *type_tag (const_tree);
3127 static tree member_declared_type (const_tree);
3128 #if 0
3129 static const char *decl_start_label (tree);
3130 #endif
3131 static void gen_array_type_die (tree, dw_die_ref);
3132 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3133 #if 0
3134 static void gen_entry_point_die (tree, dw_die_ref);
3135 #endif
3136 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3137 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3138 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3139 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3140 static void gen_formal_types_die (tree, dw_die_ref);
3141 static void gen_subprogram_die (tree, dw_die_ref);
3142 static void gen_variable_die (tree, tree, dw_die_ref);
3143 static void gen_const_die (tree, dw_die_ref);
3144 static void gen_label_die (tree, dw_die_ref);
3145 static void gen_lexical_block_die (tree, dw_die_ref, int);
3146 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
3147 static void gen_field_die (tree, dw_die_ref);
3148 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3149 static dw_die_ref gen_compile_unit_die (const char *);
3150 static void gen_inheritance_die (tree, tree, dw_die_ref);
3151 static void gen_member_die (tree, dw_die_ref);
3152 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3153 enum debug_info_usage);
3154 static void gen_subroutine_type_die (tree, dw_die_ref);
3155 static void gen_typedef_die (tree, dw_die_ref);
3156 static void gen_type_die (tree, dw_die_ref);
3157 static void gen_block_die (tree, dw_die_ref, int);
3158 static void decls_for_scope (tree, dw_die_ref, int);
3159 static inline int is_redundant_typedef (const_tree);
3160 static bool is_naming_typedef_decl (const_tree);
3161 static inline dw_die_ref get_context_die (tree);
3162 static void gen_namespace_die (tree, dw_die_ref);
3163 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3164 static dw_die_ref force_decl_die (tree);
3165 static dw_die_ref force_type_die (tree);
3166 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3167 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3168 static struct dwarf_file_data * lookup_filename (const char *);
3169 static void retry_incomplete_types (void);
3170 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3171 static void gen_generic_params_dies (tree);
3172 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3173 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3174 static void splice_child_die (dw_die_ref, dw_die_ref);
3175 static int file_info_cmp (const void *, const void *);
3176 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3177 const char *, const char *);
3178 static void output_loc_list (dw_loc_list_ref);
3179 static char *gen_internal_sym (const char *);
3180 static bool want_pubnames (void);
3182 static void prune_unmark_dies (dw_die_ref);
3183 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3184 static void prune_unused_types_mark (dw_die_ref, int);
3185 static void prune_unused_types_walk (dw_die_ref);
3186 static void prune_unused_types_walk_attribs (dw_die_ref);
3187 static void prune_unused_types_prune (dw_die_ref);
3188 static void prune_unused_types (void);
3189 static int maybe_emit_file (struct dwarf_file_data *fd);
3190 static inline const char *AT_vms_delta1 (dw_attr_ref);
3191 static inline const char *AT_vms_delta2 (dw_attr_ref);
3192 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3193 const char *, const char *);
3194 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3195 static void gen_remaining_tmpl_value_param_die_attribute (void);
3196 static bool generic_type_p (tree);
3197 static void schedule_generic_params_dies_gen (tree t);
3198 static void gen_scheduled_generic_parms_dies (void);
3200 /* enum for tracking thread-local variables whose address is really an offset
3201 relative to the TLS pointer, which will need link-time relocation, but will
3202 not need relocation by the DWARF consumer. */
3204 enum dtprel_bool
3206 dtprel_false = 0,
3207 dtprel_true = 1
3210 /* Return the operator to use for an address of a variable. For dtprel_true, we
3211 use DW_OP_const*. For regular variables, which need both link-time
3212 relocation and consumer-level relocation (e.g., to account for shared objects
3213 loaded at a random address), we use DW_OP_addr*. */
3215 static inline enum dwarf_location_atom
3216 dw_addr_op (enum dtprel_bool dtprel)
3218 if (dtprel == dtprel_true)
3219 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3220 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3221 else
3222 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3225 /* Return a pointer to a newly allocated address location description. If
3226 dwarf_split_debug_info is true, then record the address with the appropriate
3227 relocation. */
3228 static inline dw_loc_descr_ref
3229 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3231 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3233 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3234 ref->dw_loc_oprnd1.v.val_addr = addr;
3235 ref->dtprel = dtprel;
3236 if (dwarf_split_debug_info)
3237 ref->dw_loc_oprnd1.val_entry
3238 = add_addr_table_entry (addr,
3239 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3240 else
3241 ref->dw_loc_oprnd1.val_entry = NULL;
3243 return ref;
3246 /* Section names used to hold DWARF debugging information. */
3248 #ifndef DEBUG_INFO_SECTION
3249 #define DEBUG_INFO_SECTION ".debug_info"
3250 #endif
3251 #ifndef DEBUG_DWO_INFO_SECTION
3252 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3253 #endif
3254 #ifndef DEBUG_ABBREV_SECTION
3255 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3256 #endif
3257 #ifndef DEBUG_DWO_ABBREV_SECTION
3258 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3259 #endif
3260 #ifndef DEBUG_ARANGES_SECTION
3261 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3262 #endif
3263 #ifndef DEBUG_ADDR_SECTION
3264 #define DEBUG_ADDR_SECTION ".debug_addr"
3265 #endif
3266 #ifndef DEBUG_NORM_MACINFO_SECTION
3267 #define DEBUG_NORM_MACINFO_SECTION ".debug_macinfo"
3268 #endif
3269 #ifndef DEBUG_DWO_MACINFO_SECTION
3270 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3271 #endif
3272 #ifndef DEBUG_MACINFO_SECTION
3273 #define DEBUG_MACINFO_SECTION \
3274 (!dwarf_split_debug_info \
3275 ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3276 #endif
3277 #ifndef DEBUG_NORM_MACRO_SECTION
3278 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3279 #endif
3280 #ifndef DEBUG_DWO_MACRO_SECTION
3281 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3282 #endif
3283 #ifndef DEBUG_MACRO_SECTION
3284 #define DEBUG_MACRO_SECTION \
3285 (!dwarf_split_debug_info \
3286 ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3287 #endif
3288 #ifndef DEBUG_LINE_SECTION
3289 #define DEBUG_LINE_SECTION ".debug_line"
3290 #endif
3291 #ifndef DEBUG_DWO_LINE_SECTION
3292 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3293 #endif
3294 #ifndef DEBUG_LOC_SECTION
3295 #define DEBUG_LOC_SECTION ".debug_loc"
3296 #endif
3297 #ifndef DEBUG_DWO_LOC_SECTION
3298 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3299 #endif
3300 #ifndef DEBUG_PUBNAMES_SECTION
3301 #define DEBUG_PUBNAMES_SECTION ".debug_pubnames"
3302 #endif
3303 #ifndef DEBUG_PUBTYPES_SECTION
3304 #define DEBUG_PUBTYPES_SECTION ".debug_pubtypes"
3305 #endif
3306 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3307 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3308 #ifndef DEBUG_STR_OFFSETS_SECTION
3309 #define DEBUG_STR_OFFSETS_SECTION \
3310 (!dwarf_split_debug_info \
3311 ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3312 #endif
3313 #define DEBUG_DWO_STR_SECTION ".debug_str.dwo"
3314 #define DEBUG_NORM_STR_SECTION ".debug_str"
3315 #ifndef DEBUG_STR_SECTION
3316 #define DEBUG_STR_SECTION \
3317 (!dwarf_split_debug_info ? (DEBUG_NORM_STR_SECTION) : (DEBUG_DWO_STR_SECTION))
3318 #endif
3319 #ifndef DEBUG_RANGES_SECTION
3320 #define DEBUG_RANGES_SECTION ".debug_ranges"
3321 #endif
3323 /* Standard ELF section names for compiled code and data. */
3324 #ifndef TEXT_SECTION_NAME
3325 #define TEXT_SECTION_NAME ".text"
3326 #endif
3328 /* Section flags for .debug_macinfo/.debug_macro section. */
3329 #define DEBUG_MACRO_SECTION_FLAGS \
3330 (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3332 /* Section flags for .debug_str section. */
3333 #define DEBUG_STR_SECTION_FLAGS \
3334 (dwarf_split_debug_info \
3335 ? SECTION_DEBUG | SECTION_EXCLUDE \
3336 : (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3337 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3338 : SECTION_DEBUG))
3340 /* Labels we insert at beginning sections we can reference instead of
3341 the section names themselves. */
3343 #ifndef TEXT_SECTION_LABEL
3344 #define TEXT_SECTION_LABEL "Ltext"
3345 #endif
3346 #ifndef COLD_TEXT_SECTION_LABEL
3347 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3348 #endif
3349 #ifndef DEBUG_LINE_SECTION_LABEL
3350 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3351 #endif
3352 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3353 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3354 #endif
3355 #ifndef DEBUG_INFO_SECTION_LABEL
3356 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3357 #endif
3358 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3359 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3360 #endif
3361 #ifndef DEBUG_ABBREV_SECTION_LABEL
3362 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3363 #endif
3364 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3365 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3366 #endif
3367 #ifndef DEBUG_ADDR_SECTION_LABEL
3368 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3369 #endif
3370 #ifndef DEBUG_LOC_SECTION_LABEL
3371 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3372 #endif
3373 #ifndef DEBUG_RANGES_SECTION_LABEL
3374 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3375 #endif
3376 #ifndef DEBUG_MACINFO_SECTION_LABEL
3377 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3378 #endif
3379 #ifndef DEBUG_MACRO_SECTION_LABEL
3380 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3381 #endif
3382 #define SKELETON_COMP_DIE_ABBREV 1
3383 #define SKELETON_TYPE_DIE_ABBREV 2
3385 /* Definitions of defaults for formats and names of various special
3386 (artificial) labels which may be generated within this file (when the -g
3387 options is used and DWARF2_DEBUGGING_INFO is in effect.
3388 If necessary, these may be overridden from within the tm.h file, but
3389 typically, overriding these defaults is unnecessary. */
3391 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3392 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3393 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3394 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3395 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3396 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3397 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3398 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3399 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3400 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3401 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3402 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3403 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3404 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3406 #ifndef TEXT_END_LABEL
3407 #define TEXT_END_LABEL "Letext"
3408 #endif
3409 #ifndef COLD_END_LABEL
3410 #define COLD_END_LABEL "Letext_cold"
3411 #endif
3412 #ifndef BLOCK_BEGIN_LABEL
3413 #define BLOCK_BEGIN_LABEL "LBB"
3414 #endif
3415 #ifndef BLOCK_END_LABEL
3416 #define BLOCK_END_LABEL "LBE"
3417 #endif
3418 #ifndef LINE_CODE_LABEL
3419 #define LINE_CODE_LABEL "LM"
3420 #endif
3423 /* Return the root of the DIE's built for the current compilation unit. */
3424 static dw_die_ref
3425 comp_unit_die (void)
3427 if (!single_comp_unit_die)
3428 single_comp_unit_die = gen_compile_unit_die (NULL);
3429 return single_comp_unit_die;
3432 /* We allow a language front-end to designate a function that is to be
3433 called to "demangle" any name before it is put into a DIE. */
3435 static const char *(*demangle_name_func) (const char *);
3437 void
3438 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3440 demangle_name_func = func;
3443 /* Test if rtl node points to a pseudo register. */
3445 static inline int
3446 is_pseudo_reg (const_rtx rtl)
3448 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3449 || (GET_CODE (rtl) == SUBREG
3450 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3453 /* Return a reference to a type, with its const and volatile qualifiers
3454 removed. */
3456 static inline tree
3457 type_main_variant (tree type)
3459 type = TYPE_MAIN_VARIANT (type);
3461 /* ??? There really should be only one main variant among any group of
3462 variants of a given type (and all of the MAIN_VARIANT values for all
3463 members of the group should point to that one type) but sometimes the C
3464 front-end messes this up for array types, so we work around that bug
3465 here. */
3466 if (TREE_CODE (type) == ARRAY_TYPE)
3467 while (type != TYPE_MAIN_VARIANT (type))
3468 type = TYPE_MAIN_VARIANT (type);
3470 return type;
3473 /* Return nonzero if the given type node represents a tagged type. */
3475 static inline int
3476 is_tagged_type (const_tree type)
3478 enum tree_code code = TREE_CODE (type);
3480 return (code == RECORD_TYPE || code == UNION_TYPE
3481 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3484 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3486 static void
3487 get_ref_die_offset_label (char *label, dw_die_ref ref)
3489 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3492 /* Return die_offset of a DIE reference to a base type. */
3494 static unsigned long int
3495 get_base_type_offset (dw_die_ref ref)
3497 if (ref->die_offset)
3498 return ref->die_offset;
3499 if (comp_unit_die ()->die_abbrev)
3501 calc_base_type_die_sizes ();
3502 gcc_assert (ref->die_offset);
3504 return ref->die_offset;
3507 /* Return die_offset of a DIE reference other than base type. */
3509 static unsigned long int
3510 get_ref_die_offset (dw_die_ref ref)
3512 gcc_assert (ref->die_offset);
3513 return ref->die_offset;
3516 /* Convert a DIE tag into its string name. */
3518 static const char *
3519 dwarf_tag_name (unsigned int tag)
3521 const char *name = get_DW_TAG_name (tag);
3523 if (name != NULL)
3524 return name;
3526 return "DW_TAG_<unknown>";
3529 /* Convert a DWARF attribute code into its string name. */
3531 static const char *
3532 dwarf_attr_name (unsigned int attr)
3534 const char *name;
3536 switch (attr)
3538 #if VMS_DEBUGGING_INFO
3539 case DW_AT_HP_prologue:
3540 return "DW_AT_HP_prologue";
3541 #else
3542 case DW_AT_MIPS_loop_unroll_factor:
3543 return "DW_AT_MIPS_loop_unroll_factor";
3544 #endif
3546 #if VMS_DEBUGGING_INFO
3547 case DW_AT_HP_epilogue:
3548 return "DW_AT_HP_epilogue";
3549 #else
3550 case DW_AT_MIPS_stride:
3551 return "DW_AT_MIPS_stride";
3552 #endif
3555 name = get_DW_AT_name (attr);
3557 if (name != NULL)
3558 return name;
3560 return "DW_AT_<unknown>";
3563 /* Convert a DWARF value form code into its string name. */
3565 static const char *
3566 dwarf_form_name (unsigned int form)
3568 const char *name = get_DW_FORM_name (form);
3570 if (name != NULL)
3571 return name;
3573 return "DW_FORM_<unknown>";
3576 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3577 instance of an inlined instance of a decl which is local to an inline
3578 function, so we have to trace all of the way back through the origin chain
3579 to find out what sort of node actually served as the original seed for the
3580 given block. */
3582 static tree
3583 decl_ultimate_origin (const_tree decl)
3585 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3586 return NULL_TREE;
3588 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
3589 nodes in the function to point to themselves; ignore that if
3590 we're trying to output the abstract instance of this function. */
3591 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3592 return NULL_TREE;
3594 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3595 most distant ancestor, this should never happen. */
3596 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3598 return DECL_ABSTRACT_ORIGIN (decl);
3601 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
3602 of a virtual function may refer to a base class, so we check the 'this'
3603 parameter. */
3605 static tree
3606 decl_class_context (tree decl)
3608 tree context = NULL_TREE;
3610 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3611 context = DECL_CONTEXT (decl);
3612 else
3613 context = TYPE_MAIN_VARIANT
3614 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3616 if (context && !TYPE_P (context))
3617 context = NULL_TREE;
3619 return context;
3622 /* Add an attribute/value pair to a DIE. */
3624 static inline void
3625 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
3627 /* Maybe this should be an assert? */
3628 if (die == NULL)
3629 return;
3631 vec_safe_reserve (die->die_attr, 1);
3632 vec_safe_push (die->die_attr, *attr);
3635 static inline enum dw_val_class
3636 AT_class (dw_attr_ref a)
3638 return a->dw_attr_val.val_class;
3641 /* Return the index for any attribute that will be referenced with a
3642 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
3643 are stored in dw_attr_val.v.val_str for reference counting
3644 pruning. */
3646 static inline unsigned int
3647 AT_index (dw_attr_ref a)
3649 if (AT_class (a) == dw_val_class_str)
3650 return a->dw_attr_val.v.val_str->index;
3651 else if (a->dw_attr_val.val_entry != NULL)
3652 return a->dw_attr_val.val_entry->index;
3653 return NOT_INDEXED;
3656 /* Add a flag value attribute to a DIE. */
3658 static inline void
3659 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3661 dw_attr_node attr;
3663 attr.dw_attr = attr_kind;
3664 attr.dw_attr_val.val_class = dw_val_class_flag;
3665 attr.dw_attr_val.val_entry = NULL;
3666 attr.dw_attr_val.v.val_flag = flag;
3667 add_dwarf_attr (die, &attr);
3670 static inline unsigned
3671 AT_flag (dw_attr_ref a)
3673 gcc_assert (a && AT_class (a) == dw_val_class_flag);
3674 return a->dw_attr_val.v.val_flag;
3677 /* Add a signed integer attribute value to a DIE. */
3679 static inline void
3680 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3682 dw_attr_node attr;
3684 attr.dw_attr = attr_kind;
3685 attr.dw_attr_val.val_class = dw_val_class_const;
3686 attr.dw_attr_val.val_entry = NULL;
3687 attr.dw_attr_val.v.val_int = int_val;
3688 add_dwarf_attr (die, &attr);
3691 static inline HOST_WIDE_INT
3692 AT_int (dw_attr_ref a)
3694 gcc_assert (a && AT_class (a) == dw_val_class_const);
3695 return a->dw_attr_val.v.val_int;
3698 /* Add an unsigned integer attribute value to a DIE. */
3700 static inline void
3701 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3702 unsigned HOST_WIDE_INT unsigned_val)
3704 dw_attr_node attr;
3706 attr.dw_attr = attr_kind;
3707 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3708 attr.dw_attr_val.val_entry = NULL;
3709 attr.dw_attr_val.v.val_unsigned = unsigned_val;
3710 add_dwarf_attr (die, &attr);
3713 static inline unsigned HOST_WIDE_INT
3714 AT_unsigned (dw_attr_ref a)
3716 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3717 return a->dw_attr_val.v.val_unsigned;
3720 /* Add an unsigned double integer attribute value to a DIE. */
3722 static inline void
3723 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3724 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3726 dw_attr_node attr;
3728 attr.dw_attr = attr_kind;
3729 attr.dw_attr_val.val_class = dw_val_class_const_double;
3730 attr.dw_attr_val.val_entry = NULL;
3731 attr.dw_attr_val.v.val_double.high = high;
3732 attr.dw_attr_val.v.val_double.low = low;
3733 add_dwarf_attr (die, &attr);
3736 /* Add a floating point attribute value to a DIE and return it. */
3738 static inline void
3739 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3740 unsigned int length, unsigned int elt_size, unsigned char *array)
3742 dw_attr_node attr;
3744 attr.dw_attr = attr_kind;
3745 attr.dw_attr_val.val_class = dw_val_class_vec;
3746 attr.dw_attr_val.val_entry = NULL;
3747 attr.dw_attr_val.v.val_vec.length = length;
3748 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3749 attr.dw_attr_val.v.val_vec.array = array;
3750 add_dwarf_attr (die, &attr);
3753 /* Add an 8-byte data attribute value to a DIE. */
3755 static inline void
3756 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3757 unsigned char data8[8])
3759 dw_attr_node attr;
3761 attr.dw_attr = attr_kind;
3762 attr.dw_attr_val.val_class = dw_val_class_data8;
3763 attr.dw_attr_val.val_entry = NULL;
3764 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3765 add_dwarf_attr (die, &attr);
3768 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
3769 dwarf_split_debug_info, address attributes in dies destined for the
3770 final executable have force_direct set to avoid using indexed
3771 references. */
3773 static inline void
3774 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
3775 bool force_direct)
3777 dw_attr_node attr;
3778 char * lbl_id;
3780 lbl_id = xstrdup (lbl_low);
3781 attr.dw_attr = DW_AT_low_pc;
3782 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3783 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3784 if (dwarf_split_debug_info && !force_direct)
3785 attr.dw_attr_val.val_entry
3786 = add_addr_table_entry (lbl_id, ate_kind_label);
3787 else
3788 attr.dw_attr_val.val_entry = NULL;
3789 add_dwarf_attr (die, &attr);
3791 attr.dw_attr = DW_AT_high_pc;
3792 if (dwarf_version < 4)
3793 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3794 else
3795 attr.dw_attr_val.val_class = dw_val_class_high_pc;
3796 lbl_id = xstrdup (lbl_high);
3797 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3798 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
3799 && dwarf_split_debug_info && !force_direct)
3800 attr.dw_attr_val.val_entry
3801 = add_addr_table_entry (lbl_id, ate_kind_label);
3802 else
3803 attr.dw_attr_val.val_entry = NULL;
3804 add_dwarf_attr (die, &attr);
3807 /* Hash and equality functions for debug_str_hash. */
3809 static hashval_t
3810 debug_str_do_hash (const void *x)
3812 return htab_hash_string (((const struct indirect_string_node *)x)->str);
3815 static int
3816 debug_str_eq (const void *x1, const void *x2)
3818 return strcmp ((((const struct indirect_string_node *)x1)->str),
3819 (const char *)x2) == 0;
3822 /* Add STR to the indirect string hash table. */
3824 static struct indirect_string_node *
3825 find_AT_string (const char *str)
3827 struct indirect_string_node *node;
3828 void **slot;
3830 if (! debug_str_hash)
3831 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
3832 debug_str_eq, NULL);
3834 slot = htab_find_slot_with_hash (debug_str_hash, str,
3835 htab_hash_string (str), INSERT);
3836 if (*slot == NULL)
3838 node = ggc_alloc_cleared_indirect_string_node ();
3839 node->str = ggc_strdup (str);
3840 *slot = node;
3842 else
3843 node = (struct indirect_string_node *) *slot;
3845 node->refcount++;
3846 return node;
3849 /* Add a string attribute value to a DIE. */
3851 static inline void
3852 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
3854 dw_attr_node attr;
3855 struct indirect_string_node *node;
3857 node = find_AT_string (str);
3859 attr.dw_attr = attr_kind;
3860 attr.dw_attr_val.val_class = dw_val_class_str;
3861 attr.dw_attr_val.val_entry = NULL;
3862 attr.dw_attr_val.v.val_str = node;
3863 add_dwarf_attr (die, &attr);
3866 static inline const char *
3867 AT_string (dw_attr_ref a)
3869 gcc_assert (a && AT_class (a) == dw_val_class_str);
3870 return a->dw_attr_val.v.val_str->str;
3873 /* Call this function directly to bypass AT_string_form's logic to put
3874 the string inline in the die. */
3876 static void
3877 set_indirect_string (struct indirect_string_node *node)
3879 char label[32];
3880 /* Already indirect is a no op. */
3881 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
3883 gcc_assert (node->label);
3884 return;
3886 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
3887 ++dw2_string_counter;
3888 node->label = xstrdup (label);
3890 if (!dwarf_split_debug_info)
3892 node->form = DW_FORM_strp;
3893 node->index = NOT_INDEXED;
3895 else
3897 node->form = DW_FORM_GNU_str_index;
3898 node->index = NO_INDEX_ASSIGNED;
3902 /* Find out whether a string should be output inline in DIE
3903 or out-of-line in .debug_str section. */
3905 static enum dwarf_form
3906 find_string_form (struct indirect_string_node *node)
3908 unsigned int len;
3910 if (node->form)
3911 return node->form;
3913 len = strlen (node->str) + 1;
3915 /* If the string is shorter or equal to the size of the reference, it is
3916 always better to put it inline. */
3917 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
3918 return node->form = DW_FORM_string;
3920 /* If we cannot expect the linker to merge strings in .debug_str
3921 section, only put it into .debug_str if it is worth even in this
3922 single module. */
3923 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
3924 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
3925 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
3926 return node->form = DW_FORM_string;
3928 set_indirect_string (node);
3930 return node->form;
3933 /* Find out whether the string referenced from the attribute should be
3934 output inline in DIE or out-of-line in .debug_str section. */
3936 static enum dwarf_form
3937 AT_string_form (dw_attr_ref a)
3939 gcc_assert (a && AT_class (a) == dw_val_class_str);
3940 return find_string_form (a->dw_attr_val.v.val_str);
3943 /* Add a DIE reference attribute value to a DIE. */
3945 static inline void
3946 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
3948 dw_attr_node attr;
3950 #ifdef ENABLE_CHECKING
3951 gcc_assert (targ_die != NULL);
3952 #else
3953 /* With LTO we can end up trying to reference something we didn't create
3954 a DIE for. Avoid crashing later on a NULL referenced DIE. */
3955 if (targ_die == NULL)
3956 return;
3957 #endif
3959 attr.dw_attr = attr_kind;
3960 attr.dw_attr_val.val_class = dw_val_class_die_ref;
3961 attr.dw_attr_val.val_entry = NULL;
3962 attr.dw_attr_val.v.val_die_ref.die = targ_die;
3963 attr.dw_attr_val.v.val_die_ref.external = 0;
3964 add_dwarf_attr (die, &attr);
3967 /* Change DIE reference REF to point to NEW_DIE instead. */
3969 static inline void
3970 change_AT_die_ref (dw_attr_ref ref, dw_die_ref new_die)
3972 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
3973 ref->dw_attr_val.v.val_die_ref.die = new_die;
3974 ref->dw_attr_val.v.val_die_ref.external = 0;
3977 /* Add an AT_specification attribute to a DIE, and also make the back
3978 pointer from the specification to the definition. */
3980 static inline void
3981 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
3983 add_AT_die_ref (die, DW_AT_specification, targ_die);
3984 gcc_assert (!targ_die->die_definition);
3985 targ_die->die_definition = die;
3988 static inline dw_die_ref
3989 AT_ref (dw_attr_ref a)
3991 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
3992 return a->dw_attr_val.v.val_die_ref.die;
3995 static inline int
3996 AT_ref_external (dw_attr_ref a)
3998 if (a && AT_class (a) == dw_val_class_die_ref)
3999 return a->dw_attr_val.v.val_die_ref.external;
4001 return 0;
4004 static inline void
4005 set_AT_ref_external (dw_attr_ref a, int i)
4007 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4008 a->dw_attr_val.v.val_die_ref.external = i;
4011 /* Add an FDE reference attribute value to a DIE. */
4013 static inline void
4014 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4016 dw_attr_node attr;
4018 attr.dw_attr = attr_kind;
4019 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4020 attr.dw_attr_val.val_entry = NULL;
4021 attr.dw_attr_val.v.val_fde_index = targ_fde;
4022 add_dwarf_attr (die, &attr);
4025 /* Add a location description attribute value to a DIE. */
4027 static inline void
4028 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4030 dw_attr_node attr;
4032 attr.dw_attr = attr_kind;
4033 attr.dw_attr_val.val_class = dw_val_class_loc;
4034 attr.dw_attr_val.val_entry = NULL;
4035 attr.dw_attr_val.v.val_loc = loc;
4036 add_dwarf_attr (die, &attr);
4039 static inline dw_loc_descr_ref
4040 AT_loc (dw_attr_ref a)
4042 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4043 return a->dw_attr_val.v.val_loc;
4046 static inline void
4047 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4049 dw_attr_node attr;
4051 attr.dw_attr = attr_kind;
4052 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4053 attr.dw_attr_val.val_entry = NULL;
4054 attr.dw_attr_val.v.val_loc_list = loc_list;
4055 add_dwarf_attr (die, &attr);
4056 have_location_lists = true;
4059 static inline dw_loc_list_ref
4060 AT_loc_list (dw_attr_ref a)
4062 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4063 return a->dw_attr_val.v.val_loc_list;
4066 static inline dw_loc_list_ref *
4067 AT_loc_list_ptr (dw_attr_ref a)
4069 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4070 return &a->dw_attr_val.v.val_loc_list;
4073 /* Table of entries into the .debug_addr section. */
4075 static GTY ((param_is (addr_table_entry))) htab_t addr_index_table;
4077 /* Hash an address_table_entry. */
4079 static hashval_t
4080 addr_table_entry_do_hash (const void *x)
4082 const addr_table_entry *a = (const addr_table_entry *) x;
4083 switch (a->kind)
4085 case ate_kind_rtx:
4086 return iterative_hash_rtx (a->addr.rtl, 0);
4087 case ate_kind_rtx_dtprel:
4088 return iterative_hash_rtx (a->addr.rtl, 1);
4089 case ate_kind_label:
4090 return htab_hash_string (a->addr.label);
4091 default:
4092 gcc_unreachable ();
4096 /* Determine equality for two address_table_entries. */
4098 static int
4099 addr_table_entry_eq (const void *x1, const void *x2)
4101 const addr_table_entry *a1 = (const addr_table_entry *) x1;
4102 const addr_table_entry *a2 = (const addr_table_entry *) x2;
4104 if (a1->kind != a2->kind)
4105 return 0;
4106 switch (a1->kind)
4108 case ate_kind_rtx:
4109 case ate_kind_rtx_dtprel:
4110 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4111 case ate_kind_label:
4112 return strcmp (a1->addr.label, a2->addr.label) == 0;
4113 default:
4114 gcc_unreachable ();
4118 /* Initialize an addr_table_entry. */
4120 void
4121 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4123 e->kind = kind;
4124 switch (kind)
4126 case ate_kind_rtx:
4127 case ate_kind_rtx_dtprel:
4128 e->addr.rtl = (rtx) addr;
4129 break;
4130 case ate_kind_label:
4131 e->addr.label = (char *) addr;
4132 break;
4134 e->refcount = 0;
4135 e->index = NO_INDEX_ASSIGNED;
4138 /* Add attr to the address table entry to the table. Defer setting an
4139 index until output time. */
4141 static addr_table_entry *
4142 add_addr_table_entry (void *addr, enum ate_kind kind)
4144 addr_table_entry *node;
4145 addr_table_entry finder;
4146 void **slot;
4148 gcc_assert (dwarf_split_debug_info);
4149 if (! addr_index_table)
4150 addr_index_table = htab_create_ggc (10, addr_table_entry_do_hash,
4151 addr_table_entry_eq, NULL);
4152 init_addr_table_entry (&finder, kind, addr);
4153 slot = htab_find_slot (addr_index_table, &finder, INSERT);
4155 if (*slot == HTAB_EMPTY_ENTRY)
4157 node = ggc_alloc_cleared_addr_table_entry ();
4158 init_addr_table_entry (node, kind, addr);
4159 *slot = node;
4161 else
4162 node = (addr_table_entry *) *slot;
4164 node->refcount++;
4165 return node;
4168 /* Remove an entry from the addr table by decrementing its refcount.
4169 Strictly, decrementing the refcount would be enough, but the
4170 assertion that the entry is actually in the table has found
4171 bugs. */
4173 static void
4174 remove_addr_table_entry (addr_table_entry *entry)
4176 addr_table_entry *node;
4178 gcc_assert (dwarf_split_debug_info && addr_index_table);
4179 node = (addr_table_entry *) htab_find (addr_index_table, entry);
4180 /* After an index is assigned, the table is frozen. */
4181 gcc_assert (node->refcount > 0 && node->index == NO_INDEX_ASSIGNED);
4182 node->refcount--;
4185 /* Given a location list, remove all addresses it refers to from the
4186 address_table. */
4188 static void
4189 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4191 for (; descr; descr = descr->dw_loc_next)
4192 if (descr->dw_loc_oprnd1.val_entry != NULL)
4194 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4195 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4199 /* A helper function for dwarf2out_finish called through
4200 htab_traverse. Assign an addr_table_entry its index. All entries
4201 must be collected into the table when this function is called,
4202 because the indexing code relies on htab_traverse to traverse nodes
4203 in the same order for each run. */
4205 static int
4206 index_addr_table_entry (void **h, void *v)
4208 addr_table_entry *node = (addr_table_entry *) *h;
4209 unsigned int *index = (unsigned int *) v;
4211 /* Don't index unreferenced nodes. */
4212 if (node->refcount == 0)
4213 return 1;
4215 gcc_assert(node->index == NO_INDEX_ASSIGNED);
4216 node->index = *index;
4217 *index += 1;
4219 return 1;
4222 /* Add an address constant attribute value to a DIE. When using
4223 dwarf_split_debug_info, address attributes in dies destined for the
4224 final executable should be direct references--setting the parameter
4225 force_direct ensures this behavior. */
4227 static inline void
4228 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4229 bool force_direct)
4231 dw_attr_node attr;
4233 attr.dw_attr = attr_kind;
4234 attr.dw_attr_val.val_class = dw_val_class_addr;
4235 attr.dw_attr_val.v.val_addr = addr;
4236 if (dwarf_split_debug_info && !force_direct)
4237 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4238 else
4239 attr.dw_attr_val.val_entry = NULL;
4240 add_dwarf_attr (die, &attr);
4243 /* Get the RTX from to an address DIE attribute. */
4245 static inline rtx
4246 AT_addr (dw_attr_ref a)
4248 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4249 return a->dw_attr_val.v.val_addr;
4252 /* Add a file attribute value to a DIE. */
4254 static inline void
4255 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4256 struct dwarf_file_data *fd)
4258 dw_attr_node attr;
4260 attr.dw_attr = attr_kind;
4261 attr.dw_attr_val.val_class = dw_val_class_file;
4262 attr.dw_attr_val.val_entry = NULL;
4263 attr.dw_attr_val.v.val_file = fd;
4264 add_dwarf_attr (die, &attr);
4267 /* Get the dwarf_file_data from a file DIE attribute. */
4269 static inline struct dwarf_file_data *
4270 AT_file (dw_attr_ref a)
4272 gcc_assert (a && AT_class (a) == dw_val_class_file);
4273 return a->dw_attr_val.v.val_file;
4276 /* Add a vms delta attribute value to a DIE. */
4278 static inline void
4279 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4280 const char *lbl1, const char *lbl2)
4282 dw_attr_node attr;
4284 attr.dw_attr = attr_kind;
4285 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4286 attr.dw_attr_val.val_entry = NULL;
4287 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4288 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4289 add_dwarf_attr (die, &attr);
4292 /* Add a label identifier attribute value to a DIE. */
4294 static inline void
4295 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4296 const char *lbl_id)
4298 dw_attr_node attr;
4300 attr.dw_attr = attr_kind;
4301 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4302 attr.dw_attr_val.val_entry = NULL;
4303 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4304 if (dwarf_split_debug_info)
4305 attr.dw_attr_val.val_entry
4306 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4307 ate_kind_label);
4308 add_dwarf_attr (die, &attr);
4311 /* Add a section offset attribute value to a DIE, an offset into the
4312 debug_line section. */
4314 static inline void
4315 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4316 const char *label)
4318 dw_attr_node attr;
4320 attr.dw_attr = attr_kind;
4321 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4322 attr.dw_attr_val.val_entry = NULL;
4323 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4324 add_dwarf_attr (die, &attr);
4327 /* Add a section offset attribute value to a DIE, an offset into the
4328 debug_macinfo section. */
4330 static inline void
4331 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4332 const char *label)
4334 dw_attr_node attr;
4336 attr.dw_attr = attr_kind;
4337 attr.dw_attr_val.val_class = dw_val_class_macptr;
4338 attr.dw_attr_val.val_entry = NULL;
4339 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4340 add_dwarf_attr (die, &attr);
4343 /* Add an offset attribute value to a DIE. */
4345 static inline void
4346 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4347 unsigned HOST_WIDE_INT offset)
4349 dw_attr_node attr;
4351 attr.dw_attr = attr_kind;
4352 attr.dw_attr_val.val_class = dw_val_class_offset;
4353 attr.dw_attr_val.val_entry = NULL;
4354 attr.dw_attr_val.v.val_offset = offset;
4355 add_dwarf_attr (die, &attr);
4358 /* Add a range_list attribute value to a DIE. When using
4359 dwarf_split_debug_info, address attributes in dies destined for the
4360 final executable should be direct references--setting the parameter
4361 force_direct ensures this behavior. */
4363 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4364 #define RELOCATED_OFFSET (NULL)
4366 static void
4367 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4368 long unsigned int offset, bool force_direct)
4370 dw_attr_node attr;
4372 attr.dw_attr = attr_kind;
4373 attr.dw_attr_val.val_class = dw_val_class_range_list;
4374 /* For the range_list attribute, use val_entry to store whether the
4375 offset should follow split-debug-info or normal semantics. This
4376 value is read in output_range_list_offset. */
4377 if (dwarf_split_debug_info && !force_direct)
4378 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4379 else
4380 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4381 attr.dw_attr_val.v.val_offset = offset;
4382 add_dwarf_attr (die, &attr);
4385 /* Return the start label of a delta attribute. */
4387 static inline const char *
4388 AT_vms_delta1 (dw_attr_ref a)
4390 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4391 return a->dw_attr_val.v.val_vms_delta.lbl1;
4394 /* Return the end label of a delta attribute. */
4396 static inline const char *
4397 AT_vms_delta2 (dw_attr_ref a)
4399 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4400 return a->dw_attr_val.v.val_vms_delta.lbl2;
4403 static inline const char *
4404 AT_lbl (dw_attr_ref a)
4406 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4407 || AT_class (a) == dw_val_class_lineptr
4408 || AT_class (a) == dw_val_class_macptr
4409 || AT_class (a) == dw_val_class_high_pc));
4410 return a->dw_attr_val.v.val_lbl_id;
4413 /* Get the attribute of type attr_kind. */
4415 static dw_attr_ref
4416 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4418 dw_attr_ref a;
4419 unsigned ix;
4420 dw_die_ref spec = NULL;
4422 if (! die)
4423 return NULL;
4425 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4426 if (a->dw_attr == attr_kind)
4427 return a;
4428 else if (a->dw_attr == DW_AT_specification
4429 || a->dw_attr == DW_AT_abstract_origin)
4430 spec = AT_ref (a);
4432 if (spec)
4433 return get_AT (spec, attr_kind);
4435 return NULL;
4438 /* Returns the parent of the declaration of DIE. */
4440 static dw_die_ref
4441 get_die_parent (dw_die_ref die)
4443 dw_die_ref t;
4445 if (!die)
4446 return NULL;
4448 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4449 || (t = get_AT_ref (die, DW_AT_specification)))
4450 die = t;
4452 return die->die_parent;
4455 /* Return the "low pc" attribute value, typically associated with a subprogram
4456 DIE. Return null if the "low pc" attribute is either not present, or if it
4457 cannot be represented as an assembler label identifier. */
4459 static inline const char *
4460 get_AT_low_pc (dw_die_ref die)
4462 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4464 return a ? AT_lbl (a) : NULL;
4467 /* Return the "high pc" attribute value, typically associated with a subprogram
4468 DIE. Return null if the "high pc" attribute is either not present, or if it
4469 cannot be represented as an assembler label identifier. */
4471 static inline const char *
4472 get_AT_hi_pc (dw_die_ref die)
4474 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4476 return a ? AT_lbl (a) : NULL;
4479 /* Return the value of the string attribute designated by ATTR_KIND, or
4480 NULL if it is not present. */
4482 static inline const char *
4483 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4485 dw_attr_ref a = get_AT (die, attr_kind);
4487 return a ? AT_string (a) : NULL;
4490 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4491 if it is not present. */
4493 static inline int
4494 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4496 dw_attr_ref a = get_AT (die, attr_kind);
4498 return a ? AT_flag (a) : 0;
4501 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4502 if it is not present. */
4504 static inline unsigned
4505 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4507 dw_attr_ref a = get_AT (die, attr_kind);
4509 return a ? AT_unsigned (a) : 0;
4512 static inline dw_die_ref
4513 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4515 dw_attr_ref a = get_AT (die, attr_kind);
4517 return a ? AT_ref (a) : NULL;
4520 static inline struct dwarf_file_data *
4521 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4523 dw_attr_ref a = get_AT (die, attr_kind);
4525 return a ? AT_file (a) : NULL;
4528 /* Return TRUE if the language is C++. */
4530 static inline bool
4531 is_cxx (void)
4533 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4535 return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
4538 /* Return TRUE if the language is Fortran. */
4540 static inline bool
4541 is_fortran (void)
4543 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4545 return (lang == DW_LANG_Fortran77
4546 || lang == DW_LANG_Fortran90
4547 || lang == DW_LANG_Fortran95);
4550 /* Return TRUE if the language is Ada. */
4552 static inline bool
4553 is_ada (void)
4555 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4557 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4560 /* Remove the specified attribute if present. */
4562 static void
4563 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4565 dw_attr_ref a;
4566 unsigned ix;
4568 if (! die)
4569 return;
4571 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4572 if (a->dw_attr == attr_kind)
4574 if (AT_class (a) == dw_val_class_str)
4575 if (a->dw_attr_val.v.val_str->refcount)
4576 a->dw_attr_val.v.val_str->refcount--;
4578 /* vec::ordered_remove should help reduce the number of abbrevs
4579 that are needed. */
4580 die->die_attr->ordered_remove (ix);
4581 return;
4585 /* Remove CHILD from its parent. PREV must have the property that
4586 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4588 static void
4589 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4591 gcc_assert (child->die_parent == prev->die_parent);
4592 gcc_assert (prev->die_sib == child);
4593 if (prev == child)
4595 gcc_assert (child->die_parent->die_child == child);
4596 prev = NULL;
4598 else
4599 prev->die_sib = child->die_sib;
4600 if (child->die_parent->die_child == child)
4601 child->die_parent->die_child = prev;
4604 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4605 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4607 static void
4608 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4610 dw_die_ref parent = old_child->die_parent;
4612 gcc_assert (parent == prev->die_parent);
4613 gcc_assert (prev->die_sib == old_child);
4615 new_child->die_parent = parent;
4616 if (prev == old_child)
4618 gcc_assert (parent->die_child == old_child);
4619 new_child->die_sib = new_child;
4621 else
4623 prev->die_sib = new_child;
4624 new_child->die_sib = old_child->die_sib;
4626 if (old_child->die_parent->die_child == old_child)
4627 old_child->die_parent->die_child = new_child;
4630 /* Move all children from OLD_PARENT to NEW_PARENT. */
4632 static void
4633 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4635 dw_die_ref c;
4636 new_parent->die_child = old_parent->die_child;
4637 old_parent->die_child = NULL;
4638 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4641 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4642 matches TAG. */
4644 static void
4645 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4647 dw_die_ref c;
4649 c = die->die_child;
4650 if (c) do {
4651 dw_die_ref prev = c;
4652 c = c->die_sib;
4653 while (c->die_tag == tag)
4655 remove_child_with_prev (c, prev);
4656 /* Might have removed every child. */
4657 if (c == c->die_sib)
4658 return;
4659 c = c->die_sib;
4661 } while (c != die->die_child);
4664 /* Add a CHILD_DIE as the last child of DIE. */
4666 static void
4667 add_child_die (dw_die_ref die, dw_die_ref child_die)
4669 /* FIXME this should probably be an assert. */
4670 if (! die || ! child_die)
4671 return;
4672 gcc_assert (die != child_die);
4674 child_die->die_parent = die;
4675 if (die->die_child)
4677 child_die->die_sib = die->die_child->die_sib;
4678 die->die_child->die_sib = child_die;
4680 else
4681 child_die->die_sib = child_die;
4682 die->die_child = child_die;
4685 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4686 is the specification, to the end of PARENT's list of children.
4687 This is done by removing and re-adding it. */
4689 static void
4690 splice_child_die (dw_die_ref parent, dw_die_ref child)
4692 dw_die_ref p;
4694 /* We want the declaration DIE from inside the class, not the
4695 specification DIE at toplevel. */
4696 if (child->die_parent != parent)
4698 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4700 if (tmp)
4701 child = tmp;
4704 gcc_assert (child->die_parent == parent
4705 || (child->die_parent
4706 == get_AT_ref (parent, DW_AT_specification)));
4708 for (p = child->die_parent->die_child; ; p = p->die_sib)
4709 if (p->die_sib == child)
4711 remove_child_with_prev (child, p);
4712 break;
4715 add_child_die (parent, child);
4718 /* Return a pointer to a newly created DIE node. */
4720 static inline dw_die_ref
4721 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4723 dw_die_ref die = ggc_alloc_cleared_die_node ();
4725 die->die_tag = tag_value;
4727 if (parent_die != NULL)
4728 add_child_die (parent_die, die);
4729 else
4731 limbo_die_node *limbo_node;
4733 limbo_node = ggc_alloc_cleared_limbo_die_node ();
4734 limbo_node->die = die;
4735 limbo_node->created_for = t;
4736 limbo_node->next = limbo_die_list;
4737 limbo_die_list = limbo_node;
4740 return die;
4743 /* Return the DIE associated with the given type specifier. */
4745 static inline dw_die_ref
4746 lookup_type_die (tree type)
4748 return TYPE_SYMTAB_DIE (type);
4751 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4752 anonymous type named by the typedef TYPE_DIE, return the DIE of the
4753 anonymous type instead the one of the naming typedef. */
4755 static inline dw_die_ref
4756 strip_naming_typedef (tree type, dw_die_ref type_die)
4758 if (type
4759 && TREE_CODE (type) == RECORD_TYPE
4760 && type_die
4761 && type_die->die_tag == DW_TAG_typedef
4762 && is_naming_typedef_decl (TYPE_NAME (type)))
4763 type_die = get_AT_ref (type_die, DW_AT_type);
4764 return type_die;
4767 /* Like lookup_type_die, but if type is an anonymous type named by a
4768 typedef[1], return the DIE of the anonymous type instead the one of
4769 the naming typedef. This is because in gen_typedef_die, we did
4770 equate the anonymous struct named by the typedef with the DIE of
4771 the naming typedef. So by default, lookup_type_die on an anonymous
4772 struct yields the DIE of the naming typedef.
4774 [1]: Read the comment of is_naming_typedef_decl to learn about what
4775 a naming typedef is. */
4777 static inline dw_die_ref
4778 lookup_type_die_strip_naming_typedef (tree type)
4780 dw_die_ref die = lookup_type_die (type);
4781 return strip_naming_typedef (type, die);
4784 /* Equate a DIE to a given type specifier. */
4786 static inline void
4787 equate_type_number_to_die (tree type, dw_die_ref type_die)
4789 TYPE_SYMTAB_DIE (type) = type_die;
4792 /* Returns a hash value for X (which really is a die_struct). */
4794 static hashval_t
4795 decl_die_table_hash (const void *x)
4797 return (hashval_t) ((const_dw_die_ref) x)->decl_id;
4800 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
4802 static int
4803 decl_die_table_eq (const void *x, const void *y)
4805 return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
4808 /* Return the DIE associated with a given declaration. */
4810 static inline dw_die_ref
4811 lookup_decl_die (tree decl)
4813 return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
4816 /* Returns a hash value for X (which really is a var_loc_list). */
4818 static hashval_t
4819 decl_loc_table_hash (const void *x)
4821 return (hashval_t) ((const var_loc_list *) x)->decl_id;
4824 /* Return nonzero if decl_id of var_loc_list X is the same as
4825 UID of decl *Y. */
4827 static int
4828 decl_loc_table_eq (const void *x, const void *y)
4830 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
4833 /* Return the var_loc list associated with a given declaration. */
4835 static inline var_loc_list *
4836 lookup_decl_loc (const_tree decl)
4838 if (!decl_loc_table)
4839 return NULL;
4840 return (var_loc_list *)
4841 htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
4844 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
4846 static hashval_t
4847 cached_dw_loc_list_table_hash (const void *x)
4849 return (hashval_t) ((const cached_dw_loc_list *) x)->decl_id;
4852 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
4853 UID of decl *Y. */
4855 static int
4856 cached_dw_loc_list_table_eq (const void *x, const void *y)
4858 return (((const cached_dw_loc_list *) x)->decl_id
4859 == DECL_UID ((const_tree) y));
4862 /* Equate a DIE to a particular declaration. */
4864 static void
4865 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
4867 unsigned int decl_id = DECL_UID (decl);
4868 void **slot;
4870 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
4871 *slot = decl_die;
4872 decl_die->decl_id = decl_id;
4875 /* Return how many bits covers PIECE EXPR_LIST. */
4877 static int
4878 decl_piece_bitsize (rtx piece)
4880 int ret = (int) GET_MODE (piece);
4881 if (ret)
4882 return ret;
4883 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
4884 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
4885 return INTVAL (XEXP (XEXP (piece, 0), 0));
4888 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
4890 static rtx *
4891 decl_piece_varloc_ptr (rtx piece)
4893 if ((int) GET_MODE (piece))
4894 return &XEXP (piece, 0);
4895 else
4896 return &XEXP (XEXP (piece, 0), 1);
4899 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
4900 Next is the chain of following piece nodes. */
4902 static rtx
4903 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
4905 if (bitsize <= (int) MAX_MACHINE_MODE)
4906 return alloc_EXPR_LIST (bitsize, loc_note, next);
4907 else
4908 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
4909 GEN_INT (bitsize),
4910 loc_note), next);
4913 /* Return rtx that should be stored into loc field for
4914 LOC_NOTE and BITPOS/BITSIZE. */
4916 static rtx
4917 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
4918 HOST_WIDE_INT bitsize)
4920 if (bitsize != -1)
4922 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
4923 if (bitpos != 0)
4924 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
4926 return loc_note;
4929 /* This function either modifies location piece list *DEST in
4930 place (if SRC and INNER is NULL), or copies location piece list
4931 *SRC to *DEST while modifying it. Location BITPOS is modified
4932 to contain LOC_NOTE, any pieces overlapping it are removed resp.
4933 not copied and if needed some padding around it is added.
4934 When modifying in place, DEST should point to EXPR_LIST where
4935 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
4936 to the start of the whole list and INNER points to the EXPR_LIST
4937 where earlier pieces cover PIECE_BITPOS bits. */
4939 static void
4940 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
4941 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
4942 HOST_WIDE_INT bitsize, rtx loc_note)
4944 int diff;
4945 bool copy = inner != NULL;
4947 if (copy)
4949 /* First copy all nodes preceding the current bitpos. */
4950 while (src != inner)
4952 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
4953 decl_piece_bitsize (*src), NULL_RTX);
4954 dest = &XEXP (*dest, 1);
4955 src = &XEXP (*src, 1);
4958 /* Add padding if needed. */
4959 if (bitpos != piece_bitpos)
4961 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
4962 copy ? NULL_RTX : *dest);
4963 dest = &XEXP (*dest, 1);
4965 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
4967 gcc_assert (!copy);
4968 /* A piece with correct bitpos and bitsize already exist,
4969 just update the location for it and return. */
4970 *decl_piece_varloc_ptr (*dest) = loc_note;
4971 return;
4973 /* Add the piece that changed. */
4974 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
4975 dest = &XEXP (*dest, 1);
4976 /* Skip over pieces that overlap it. */
4977 diff = bitpos - piece_bitpos + bitsize;
4978 if (!copy)
4979 src = dest;
4980 while (diff > 0 && *src)
4982 rtx piece = *src;
4983 diff -= decl_piece_bitsize (piece);
4984 if (copy)
4985 src = &XEXP (piece, 1);
4986 else
4988 *src = XEXP (piece, 1);
4989 free_EXPR_LIST_node (piece);
4992 /* Add padding if needed. */
4993 if (diff < 0 && *src)
4995 if (!copy)
4996 dest = src;
4997 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
4998 dest = &XEXP (*dest, 1);
5000 if (!copy)
5001 return;
5002 /* Finally copy all nodes following it. */
5003 while (*src)
5005 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5006 decl_piece_bitsize (*src), NULL_RTX);
5007 dest = &XEXP (*dest, 1);
5008 src = &XEXP (*src, 1);
5012 /* Add a variable location node to the linked list for DECL. */
5014 static struct var_loc_node *
5015 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5017 unsigned int decl_id;
5018 var_loc_list *temp;
5019 void **slot;
5020 struct var_loc_node *loc = NULL;
5021 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5023 if (DECL_DEBUG_EXPR_IS_FROM (decl))
5025 tree realdecl = DECL_DEBUG_EXPR (decl);
5026 if (realdecl
5027 && (handled_component_p (realdecl)
5028 || (TREE_CODE (realdecl) == MEM_REF
5029 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR)))
5031 HOST_WIDE_INT maxsize;
5032 tree innerdecl;
5033 innerdecl
5034 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5035 if (!DECL_P (innerdecl)
5036 || DECL_IGNORED_P (innerdecl)
5037 || TREE_STATIC (innerdecl)
5038 || bitsize <= 0
5039 || bitpos + bitsize > 256
5040 || bitsize != maxsize)
5041 return NULL;
5042 decl = innerdecl;
5046 decl_id = DECL_UID (decl);
5047 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5048 if (*slot == NULL)
5050 temp = ggc_alloc_cleared_var_loc_list ();
5051 temp->decl_id = decl_id;
5052 *slot = temp;
5054 else
5055 temp = (var_loc_list *) *slot;
5057 /* For PARM_DECLs try to keep around the original incoming value,
5058 even if that means we'll emit a zero-range .debug_loc entry. */
5059 if (temp->last
5060 && temp->first == temp->last
5061 && TREE_CODE (decl) == PARM_DECL
5062 && GET_CODE (temp->first->loc) == NOTE
5063 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5064 && DECL_INCOMING_RTL (decl)
5065 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5066 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5067 == GET_CODE (DECL_INCOMING_RTL (decl))
5068 && prev_real_insn (temp->first->loc) == NULL_RTX
5069 && (bitsize != -1
5070 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5071 NOTE_VAR_LOCATION_LOC (loc_note))
5072 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5073 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5075 loc = ggc_alloc_cleared_var_loc_node ();
5076 temp->first->next = loc;
5077 temp->last = loc;
5078 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5080 else if (temp->last)
5082 struct var_loc_node *last = temp->last, *unused = NULL;
5083 rtx *piece_loc = NULL, last_loc_note;
5084 int piece_bitpos = 0;
5085 if (last->next)
5087 last = last->next;
5088 gcc_assert (last->next == NULL);
5090 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5092 piece_loc = &last->loc;
5095 int cur_bitsize = decl_piece_bitsize (*piece_loc);
5096 if (piece_bitpos + cur_bitsize > bitpos)
5097 break;
5098 piece_bitpos += cur_bitsize;
5099 piece_loc = &XEXP (*piece_loc, 1);
5101 while (*piece_loc);
5103 /* TEMP->LAST here is either pointer to the last but one or
5104 last element in the chained list, LAST is pointer to the
5105 last element. */
5106 if (label && strcmp (last->label, label) == 0)
5108 /* For SRA optimized variables if there weren't any real
5109 insns since last note, just modify the last node. */
5110 if (piece_loc != NULL)
5112 adjust_piece_list (piece_loc, NULL, NULL,
5113 bitpos, piece_bitpos, bitsize, loc_note);
5114 return NULL;
5116 /* If the last note doesn't cover any instructions, remove it. */
5117 if (temp->last != last)
5119 temp->last->next = NULL;
5120 unused = last;
5121 last = temp->last;
5122 gcc_assert (strcmp (last->label, label) != 0);
5124 else
5126 gcc_assert (temp->first == temp->last
5127 || (temp->first->next == temp->last
5128 && TREE_CODE (decl) == PARM_DECL));
5129 memset (temp->last, '\0', sizeof (*temp->last));
5130 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5131 return temp->last;
5134 if (bitsize == -1 && NOTE_P (last->loc))
5135 last_loc_note = last->loc;
5136 else if (piece_loc != NULL
5137 && *piece_loc != NULL_RTX
5138 && piece_bitpos == bitpos
5139 && decl_piece_bitsize (*piece_loc) == bitsize)
5140 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5141 else
5142 last_loc_note = NULL_RTX;
5143 /* If the current location is the same as the end of the list,
5144 and either both or neither of the locations is uninitialized,
5145 we have nothing to do. */
5146 if (last_loc_note == NULL_RTX
5147 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5148 NOTE_VAR_LOCATION_LOC (loc_note)))
5149 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5150 != NOTE_VAR_LOCATION_STATUS (loc_note))
5151 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5152 == VAR_INIT_STATUS_UNINITIALIZED)
5153 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5154 == VAR_INIT_STATUS_UNINITIALIZED))))
5156 /* Add LOC to the end of list and update LAST. If the last
5157 element of the list has been removed above, reuse its
5158 memory for the new node, otherwise allocate a new one. */
5159 if (unused)
5161 loc = unused;
5162 memset (loc, '\0', sizeof (*loc));
5164 else
5165 loc = ggc_alloc_cleared_var_loc_node ();
5166 if (bitsize == -1 || piece_loc == NULL)
5167 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5168 else
5169 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5170 bitpos, piece_bitpos, bitsize, loc_note);
5171 last->next = loc;
5172 /* Ensure TEMP->LAST will point either to the new last but one
5173 element of the chain, or to the last element in it. */
5174 if (last != temp->last)
5175 temp->last = last;
5177 else if (unused)
5178 ggc_free (unused);
5180 else
5182 loc = ggc_alloc_cleared_var_loc_node ();
5183 temp->first = loc;
5184 temp->last = loc;
5185 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5187 return loc;
5190 /* Keep track of the number of spaces used to indent the
5191 output of the debugging routines that print the structure of
5192 the DIE internal representation. */
5193 static int print_indent;
5195 /* Indent the line the number of spaces given by print_indent. */
5197 static inline void
5198 print_spaces (FILE *outfile)
5200 fprintf (outfile, "%*s", print_indent, "");
5203 /* Print a type signature in hex. */
5205 static inline void
5206 print_signature (FILE *outfile, char *sig)
5208 int i;
5210 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5211 fprintf (outfile, "%02x", sig[i] & 0xff);
5214 /* Print the information associated with a given DIE, and its children.
5215 This routine is a debugging aid only. */
5217 static void
5218 print_die (dw_die_ref die, FILE *outfile)
5220 dw_attr_ref a;
5221 dw_die_ref c;
5222 unsigned ix;
5224 print_spaces (outfile);
5225 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5226 die->die_offset, dwarf_tag_name (die->die_tag),
5227 (void*) die);
5228 print_spaces (outfile);
5229 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5230 fprintf (outfile, " offset: %ld", die->die_offset);
5231 fprintf (outfile, " mark: %d\n", die->die_mark);
5233 if (die->comdat_type_p)
5235 print_spaces (outfile);
5236 fprintf (outfile, " signature: ");
5237 print_signature (outfile, die->die_id.die_type_node->signature);
5238 fprintf (outfile, "\n");
5241 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5243 print_spaces (outfile);
5244 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5246 switch (AT_class (a))
5248 case dw_val_class_addr:
5249 fprintf (outfile, "address");
5250 break;
5251 case dw_val_class_offset:
5252 fprintf (outfile, "offset");
5253 break;
5254 case dw_val_class_loc:
5255 fprintf (outfile, "location descriptor");
5256 break;
5257 case dw_val_class_loc_list:
5258 fprintf (outfile, "location list -> label:%s",
5259 AT_loc_list (a)->ll_symbol);
5260 break;
5261 case dw_val_class_range_list:
5262 fprintf (outfile, "range list");
5263 break;
5264 case dw_val_class_const:
5265 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5266 break;
5267 case dw_val_class_unsigned_const:
5268 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5269 break;
5270 case dw_val_class_const_double:
5271 fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
5272 HOST_WIDE_INT_PRINT_UNSIGNED")",
5273 a->dw_attr_val.v.val_double.high,
5274 a->dw_attr_val.v.val_double.low);
5275 break;
5276 case dw_val_class_vec:
5277 fprintf (outfile, "floating-point or vector constant");
5278 break;
5279 case dw_val_class_flag:
5280 fprintf (outfile, "%u", AT_flag (a));
5281 break;
5282 case dw_val_class_die_ref:
5283 if (AT_ref (a) != NULL)
5285 if (AT_ref (a)->comdat_type_p)
5287 fprintf (outfile, "die -> signature: ");
5288 print_signature (outfile,
5289 AT_ref (a)->die_id.die_type_node->signature);
5291 else if (AT_ref (a)->die_id.die_symbol)
5292 fprintf (outfile, "die -> label: %s",
5293 AT_ref (a)->die_id.die_symbol);
5294 else
5295 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5296 fprintf (outfile, " (%p)", (void *) AT_ref (a));
5298 else
5299 fprintf (outfile, "die -> <null>");
5300 break;
5301 case dw_val_class_vms_delta:
5302 fprintf (outfile, "delta: @slotcount(%s-%s)",
5303 AT_vms_delta2 (a), AT_vms_delta1 (a));
5304 break;
5305 case dw_val_class_lbl_id:
5306 case dw_val_class_lineptr:
5307 case dw_val_class_macptr:
5308 case dw_val_class_high_pc:
5309 fprintf (outfile, "label: %s", AT_lbl (a));
5310 break;
5311 case dw_val_class_str:
5312 if (AT_string (a) != NULL)
5313 fprintf (outfile, "\"%s\"", AT_string (a));
5314 else
5315 fprintf (outfile, "<null>");
5316 break;
5317 case dw_val_class_file:
5318 fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5319 AT_file (a)->emitted_number);
5320 break;
5321 case dw_val_class_data8:
5323 int i;
5325 for (i = 0; i < 8; i++)
5326 fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
5327 break;
5329 default:
5330 break;
5333 fprintf (outfile, "\n");
5336 if (die->die_child != NULL)
5338 print_indent += 4;
5339 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5340 print_indent -= 4;
5342 if (print_indent == 0)
5343 fprintf (outfile, "\n");
5346 /* Print the information collected for a given DIE. */
5348 DEBUG_FUNCTION void
5349 debug_dwarf_die (dw_die_ref die)
5351 print_die (die, stderr);
5354 /* Print all DWARF information collected for the compilation unit.
5355 This routine is a debugging aid only. */
5357 DEBUG_FUNCTION void
5358 debug_dwarf (void)
5360 print_indent = 0;
5361 print_die (comp_unit_die (), stderr);
5364 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5365 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5366 DIE that marks the start of the DIEs for this include file. */
5368 static dw_die_ref
5369 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5371 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5372 dw_die_ref new_unit = gen_compile_unit_die (filename);
5374 new_unit->die_sib = old_unit;
5375 return new_unit;
5378 /* Close an include-file CU and reopen the enclosing one. */
5380 static dw_die_ref
5381 pop_compile_unit (dw_die_ref old_unit)
5383 dw_die_ref new_unit = old_unit->die_sib;
5385 old_unit->die_sib = NULL;
5386 return new_unit;
5389 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5390 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5392 /* Calculate the checksum of a location expression. */
5394 static inline void
5395 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5397 int tem;
5399 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5400 CHECKSUM (tem);
5401 CHECKSUM (loc->dw_loc_oprnd1);
5402 CHECKSUM (loc->dw_loc_oprnd2);
5405 /* Calculate the checksum of an attribute. */
5407 static void
5408 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5410 dw_loc_descr_ref loc;
5411 rtx r;
5413 CHECKSUM (at->dw_attr);
5415 /* We don't care that this was compiled with a different compiler
5416 snapshot; if the output is the same, that's what matters. */
5417 if (at->dw_attr == DW_AT_producer)
5418 return;
5420 switch (AT_class (at))
5422 case dw_val_class_const:
5423 CHECKSUM (at->dw_attr_val.v.val_int);
5424 break;
5425 case dw_val_class_unsigned_const:
5426 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5427 break;
5428 case dw_val_class_const_double:
5429 CHECKSUM (at->dw_attr_val.v.val_double);
5430 break;
5431 case dw_val_class_vec:
5432 CHECKSUM (at->dw_attr_val.v.val_vec);
5433 break;
5434 case dw_val_class_flag:
5435 CHECKSUM (at->dw_attr_val.v.val_flag);
5436 break;
5437 case dw_val_class_str:
5438 CHECKSUM_STRING (AT_string (at));
5439 break;
5441 case dw_val_class_addr:
5442 r = AT_addr (at);
5443 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5444 CHECKSUM_STRING (XSTR (r, 0));
5445 break;
5447 case dw_val_class_offset:
5448 CHECKSUM (at->dw_attr_val.v.val_offset);
5449 break;
5451 case dw_val_class_loc:
5452 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5453 loc_checksum (loc, ctx);
5454 break;
5456 case dw_val_class_die_ref:
5457 die_checksum (AT_ref (at), ctx, mark);
5458 break;
5460 case dw_val_class_fde_ref:
5461 case dw_val_class_vms_delta:
5462 case dw_val_class_lbl_id:
5463 case dw_val_class_lineptr:
5464 case dw_val_class_macptr:
5465 case dw_val_class_high_pc:
5466 break;
5468 case dw_val_class_file:
5469 CHECKSUM_STRING (AT_file (at)->filename);
5470 break;
5472 case dw_val_class_data8:
5473 CHECKSUM (at->dw_attr_val.v.val_data8);
5474 break;
5476 default:
5477 break;
5481 /* Calculate the checksum of a DIE. */
5483 static void
5484 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5486 dw_die_ref c;
5487 dw_attr_ref a;
5488 unsigned ix;
5490 /* To avoid infinite recursion. */
5491 if (die->die_mark)
5493 CHECKSUM (die->die_mark);
5494 return;
5496 die->die_mark = ++(*mark);
5498 CHECKSUM (die->die_tag);
5500 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5501 attr_checksum (a, ctx, mark);
5503 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5506 #undef CHECKSUM
5507 #undef CHECKSUM_STRING
5509 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
5510 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5511 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5512 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5513 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5514 #define CHECKSUM_ATTR(FOO) \
5515 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5517 /* Calculate the checksum of a number in signed LEB128 format. */
5519 static void
5520 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5522 unsigned char byte;
5523 bool more;
5525 while (1)
5527 byte = (value & 0x7f);
5528 value >>= 7;
5529 more = !((value == 0 && (byte & 0x40) == 0)
5530 || (value == -1 && (byte & 0x40) != 0));
5531 if (more)
5532 byte |= 0x80;
5533 CHECKSUM (byte);
5534 if (!more)
5535 break;
5539 /* Calculate the checksum of a number in unsigned LEB128 format. */
5541 static void
5542 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5544 while (1)
5546 unsigned char byte = (value & 0x7f);
5547 value >>= 7;
5548 if (value != 0)
5549 /* More bytes to follow. */
5550 byte |= 0x80;
5551 CHECKSUM (byte);
5552 if (value == 0)
5553 break;
5557 /* Checksum the context of the DIE. This adds the names of any
5558 surrounding namespaces or structures to the checksum. */
5560 static void
5561 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5563 const char *name;
5564 dw_die_ref spec;
5565 int tag = die->die_tag;
5567 if (tag != DW_TAG_namespace
5568 && tag != DW_TAG_structure_type
5569 && tag != DW_TAG_class_type)
5570 return;
5572 name = get_AT_string (die, DW_AT_name);
5574 spec = get_AT_ref (die, DW_AT_specification);
5575 if (spec != NULL)
5576 die = spec;
5578 if (die->die_parent != NULL)
5579 checksum_die_context (die->die_parent, ctx);
5581 CHECKSUM_ULEB128 ('C');
5582 CHECKSUM_ULEB128 (tag);
5583 if (name != NULL)
5584 CHECKSUM_STRING (name);
5587 /* Calculate the checksum of a location expression. */
5589 static inline void
5590 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5592 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5593 were emitted as a DW_FORM_sdata instead of a location expression. */
5594 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5596 CHECKSUM_ULEB128 (DW_FORM_sdata);
5597 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5598 return;
5601 /* Otherwise, just checksum the raw location expression. */
5602 while (loc != NULL)
5604 CHECKSUM_ULEB128 (loc->dw_loc_opc);
5605 CHECKSUM (loc->dw_loc_oprnd1);
5606 CHECKSUM (loc->dw_loc_oprnd2);
5607 loc = loc->dw_loc_next;
5611 /* Calculate the checksum of an attribute. */
5613 static void
5614 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5615 struct md5_ctx *ctx, int *mark)
5617 dw_loc_descr_ref loc;
5618 rtx r;
5620 if (AT_class (at) == dw_val_class_die_ref)
5622 dw_die_ref target_die = AT_ref (at);
5624 /* For pointer and reference types, we checksum only the (qualified)
5625 name of the target type (if there is a name). For friend entries,
5626 we checksum only the (qualified) name of the target type or function.
5627 This allows the checksum to remain the same whether the target type
5628 is complete or not. */
5629 if ((at->dw_attr == DW_AT_type
5630 && (tag == DW_TAG_pointer_type
5631 || tag == DW_TAG_reference_type
5632 || tag == DW_TAG_rvalue_reference_type
5633 || tag == DW_TAG_ptr_to_member_type))
5634 || (at->dw_attr == DW_AT_friend
5635 && tag == DW_TAG_friend))
5637 dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5639 if (name_attr != NULL)
5641 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5643 if (decl == NULL)
5644 decl = target_die;
5645 CHECKSUM_ULEB128 ('N');
5646 CHECKSUM_ULEB128 (at->dw_attr);
5647 if (decl->die_parent != NULL)
5648 checksum_die_context (decl->die_parent, ctx);
5649 CHECKSUM_ULEB128 ('E');
5650 CHECKSUM_STRING (AT_string (name_attr));
5651 return;
5655 /* For all other references to another DIE, we check to see if the
5656 target DIE has already been visited. If it has, we emit a
5657 backward reference; if not, we descend recursively. */
5658 if (target_die->die_mark > 0)
5660 CHECKSUM_ULEB128 ('R');
5661 CHECKSUM_ULEB128 (at->dw_attr);
5662 CHECKSUM_ULEB128 (target_die->die_mark);
5664 else
5666 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5668 if (decl == NULL)
5669 decl = target_die;
5670 target_die->die_mark = ++(*mark);
5671 CHECKSUM_ULEB128 ('T');
5672 CHECKSUM_ULEB128 (at->dw_attr);
5673 if (decl->die_parent != NULL)
5674 checksum_die_context (decl->die_parent, ctx);
5675 die_checksum_ordered (target_die, ctx, mark);
5677 return;
5680 CHECKSUM_ULEB128 ('A');
5681 CHECKSUM_ULEB128 (at->dw_attr);
5683 switch (AT_class (at))
5685 case dw_val_class_const:
5686 CHECKSUM_ULEB128 (DW_FORM_sdata);
5687 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
5688 break;
5690 case dw_val_class_unsigned_const:
5691 CHECKSUM_ULEB128 (DW_FORM_sdata);
5692 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
5693 break;
5695 case dw_val_class_const_double:
5696 CHECKSUM_ULEB128 (DW_FORM_block);
5697 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
5698 CHECKSUM (at->dw_attr_val.v.val_double);
5699 break;
5701 case dw_val_class_vec:
5702 CHECKSUM_ULEB128 (DW_FORM_block);
5703 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
5704 CHECKSUM (at->dw_attr_val.v.val_vec);
5705 break;
5707 case dw_val_class_flag:
5708 CHECKSUM_ULEB128 (DW_FORM_flag);
5709 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
5710 break;
5712 case dw_val_class_str:
5713 CHECKSUM_ULEB128 (DW_FORM_string);
5714 CHECKSUM_STRING (AT_string (at));
5715 break;
5717 case dw_val_class_addr:
5718 r = AT_addr (at);
5719 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5720 CHECKSUM_ULEB128 (DW_FORM_string);
5721 CHECKSUM_STRING (XSTR (r, 0));
5722 break;
5724 case dw_val_class_offset:
5725 CHECKSUM_ULEB128 (DW_FORM_sdata);
5726 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
5727 break;
5729 case dw_val_class_loc:
5730 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5731 loc_checksum_ordered (loc, ctx);
5732 break;
5734 case dw_val_class_fde_ref:
5735 case dw_val_class_lbl_id:
5736 case dw_val_class_lineptr:
5737 case dw_val_class_macptr:
5738 case dw_val_class_high_pc:
5739 break;
5741 case dw_val_class_file:
5742 CHECKSUM_ULEB128 (DW_FORM_string);
5743 CHECKSUM_STRING (AT_file (at)->filename);
5744 break;
5746 case dw_val_class_data8:
5747 CHECKSUM (at->dw_attr_val.v.val_data8);
5748 break;
5750 default:
5751 break;
5755 struct checksum_attributes
5757 dw_attr_ref at_name;
5758 dw_attr_ref at_type;
5759 dw_attr_ref at_friend;
5760 dw_attr_ref at_accessibility;
5761 dw_attr_ref at_address_class;
5762 dw_attr_ref at_allocated;
5763 dw_attr_ref at_artificial;
5764 dw_attr_ref at_associated;
5765 dw_attr_ref at_binary_scale;
5766 dw_attr_ref at_bit_offset;
5767 dw_attr_ref at_bit_size;
5768 dw_attr_ref at_bit_stride;
5769 dw_attr_ref at_byte_size;
5770 dw_attr_ref at_byte_stride;
5771 dw_attr_ref at_const_value;
5772 dw_attr_ref at_containing_type;
5773 dw_attr_ref at_count;
5774 dw_attr_ref at_data_location;
5775 dw_attr_ref at_data_member_location;
5776 dw_attr_ref at_decimal_scale;
5777 dw_attr_ref at_decimal_sign;
5778 dw_attr_ref at_default_value;
5779 dw_attr_ref at_digit_count;
5780 dw_attr_ref at_discr;
5781 dw_attr_ref at_discr_list;
5782 dw_attr_ref at_discr_value;
5783 dw_attr_ref at_encoding;
5784 dw_attr_ref at_endianity;
5785 dw_attr_ref at_explicit;
5786 dw_attr_ref at_is_optional;
5787 dw_attr_ref at_location;
5788 dw_attr_ref at_lower_bound;
5789 dw_attr_ref at_mutable;
5790 dw_attr_ref at_ordering;
5791 dw_attr_ref at_picture_string;
5792 dw_attr_ref at_prototyped;
5793 dw_attr_ref at_small;
5794 dw_attr_ref at_segment;
5795 dw_attr_ref at_string_length;
5796 dw_attr_ref at_threads_scaled;
5797 dw_attr_ref at_upper_bound;
5798 dw_attr_ref at_use_location;
5799 dw_attr_ref at_use_UTF8;
5800 dw_attr_ref at_variable_parameter;
5801 dw_attr_ref at_virtuality;
5802 dw_attr_ref at_visibility;
5803 dw_attr_ref at_vtable_elem_location;
5806 /* Collect the attributes that we will want to use for the checksum. */
5808 static void
5809 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
5811 dw_attr_ref a;
5812 unsigned ix;
5814 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5816 switch (a->dw_attr)
5818 case DW_AT_name:
5819 attrs->at_name = a;
5820 break;
5821 case DW_AT_type:
5822 attrs->at_type = a;
5823 break;
5824 case DW_AT_friend:
5825 attrs->at_friend = a;
5826 break;
5827 case DW_AT_accessibility:
5828 attrs->at_accessibility = a;
5829 break;
5830 case DW_AT_address_class:
5831 attrs->at_address_class = a;
5832 break;
5833 case DW_AT_allocated:
5834 attrs->at_allocated = a;
5835 break;
5836 case DW_AT_artificial:
5837 attrs->at_artificial = a;
5838 break;
5839 case DW_AT_associated:
5840 attrs->at_associated = a;
5841 break;
5842 case DW_AT_binary_scale:
5843 attrs->at_binary_scale = a;
5844 break;
5845 case DW_AT_bit_offset:
5846 attrs->at_bit_offset = a;
5847 break;
5848 case DW_AT_bit_size:
5849 attrs->at_bit_size = a;
5850 break;
5851 case DW_AT_bit_stride:
5852 attrs->at_bit_stride = a;
5853 break;
5854 case DW_AT_byte_size:
5855 attrs->at_byte_size = a;
5856 break;
5857 case DW_AT_byte_stride:
5858 attrs->at_byte_stride = a;
5859 break;
5860 case DW_AT_const_value:
5861 attrs->at_const_value = a;
5862 break;
5863 case DW_AT_containing_type:
5864 attrs->at_containing_type = a;
5865 break;
5866 case DW_AT_count:
5867 attrs->at_count = a;
5868 break;
5869 case DW_AT_data_location:
5870 attrs->at_data_location = a;
5871 break;
5872 case DW_AT_data_member_location:
5873 attrs->at_data_member_location = a;
5874 break;
5875 case DW_AT_decimal_scale:
5876 attrs->at_decimal_scale = a;
5877 break;
5878 case DW_AT_decimal_sign:
5879 attrs->at_decimal_sign = a;
5880 break;
5881 case DW_AT_default_value:
5882 attrs->at_default_value = a;
5883 break;
5884 case DW_AT_digit_count:
5885 attrs->at_digit_count = a;
5886 break;
5887 case DW_AT_discr:
5888 attrs->at_discr = a;
5889 break;
5890 case DW_AT_discr_list:
5891 attrs->at_discr_list = a;
5892 break;
5893 case DW_AT_discr_value:
5894 attrs->at_discr_value = a;
5895 break;
5896 case DW_AT_encoding:
5897 attrs->at_encoding = a;
5898 break;
5899 case DW_AT_endianity:
5900 attrs->at_endianity = a;
5901 break;
5902 case DW_AT_explicit:
5903 attrs->at_explicit = a;
5904 break;
5905 case DW_AT_is_optional:
5906 attrs->at_is_optional = a;
5907 break;
5908 case DW_AT_location:
5909 attrs->at_location = a;
5910 break;
5911 case DW_AT_lower_bound:
5912 attrs->at_lower_bound = a;
5913 break;
5914 case DW_AT_mutable:
5915 attrs->at_mutable = a;
5916 break;
5917 case DW_AT_ordering:
5918 attrs->at_ordering = a;
5919 break;
5920 case DW_AT_picture_string:
5921 attrs->at_picture_string = a;
5922 break;
5923 case DW_AT_prototyped:
5924 attrs->at_prototyped = a;
5925 break;
5926 case DW_AT_small:
5927 attrs->at_small = a;
5928 break;
5929 case DW_AT_segment:
5930 attrs->at_segment = a;
5931 break;
5932 case DW_AT_string_length:
5933 attrs->at_string_length = a;
5934 break;
5935 case DW_AT_threads_scaled:
5936 attrs->at_threads_scaled = a;
5937 break;
5938 case DW_AT_upper_bound:
5939 attrs->at_upper_bound = a;
5940 break;
5941 case DW_AT_use_location:
5942 attrs->at_use_location = a;
5943 break;
5944 case DW_AT_use_UTF8:
5945 attrs->at_use_UTF8 = a;
5946 break;
5947 case DW_AT_variable_parameter:
5948 attrs->at_variable_parameter = a;
5949 break;
5950 case DW_AT_virtuality:
5951 attrs->at_virtuality = a;
5952 break;
5953 case DW_AT_visibility:
5954 attrs->at_visibility = a;
5955 break;
5956 case DW_AT_vtable_elem_location:
5957 attrs->at_vtable_elem_location = a;
5958 break;
5959 default:
5960 break;
5965 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
5967 static void
5968 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5970 dw_die_ref c;
5971 dw_die_ref decl;
5972 struct checksum_attributes attrs;
5974 CHECKSUM_ULEB128 ('D');
5975 CHECKSUM_ULEB128 (die->die_tag);
5977 memset (&attrs, 0, sizeof (attrs));
5979 decl = get_AT_ref (die, DW_AT_specification);
5980 if (decl != NULL)
5981 collect_checksum_attributes (&attrs, decl);
5982 collect_checksum_attributes (&attrs, die);
5984 CHECKSUM_ATTR (attrs.at_name);
5985 CHECKSUM_ATTR (attrs.at_accessibility);
5986 CHECKSUM_ATTR (attrs.at_address_class);
5987 CHECKSUM_ATTR (attrs.at_allocated);
5988 CHECKSUM_ATTR (attrs.at_artificial);
5989 CHECKSUM_ATTR (attrs.at_associated);
5990 CHECKSUM_ATTR (attrs.at_binary_scale);
5991 CHECKSUM_ATTR (attrs.at_bit_offset);
5992 CHECKSUM_ATTR (attrs.at_bit_size);
5993 CHECKSUM_ATTR (attrs.at_bit_stride);
5994 CHECKSUM_ATTR (attrs.at_byte_size);
5995 CHECKSUM_ATTR (attrs.at_byte_stride);
5996 CHECKSUM_ATTR (attrs.at_const_value);
5997 CHECKSUM_ATTR (attrs.at_containing_type);
5998 CHECKSUM_ATTR (attrs.at_count);
5999 CHECKSUM_ATTR (attrs.at_data_location);
6000 CHECKSUM_ATTR (attrs.at_data_member_location);
6001 CHECKSUM_ATTR (attrs.at_decimal_scale);
6002 CHECKSUM_ATTR (attrs.at_decimal_sign);
6003 CHECKSUM_ATTR (attrs.at_default_value);
6004 CHECKSUM_ATTR (attrs.at_digit_count);
6005 CHECKSUM_ATTR (attrs.at_discr);
6006 CHECKSUM_ATTR (attrs.at_discr_list);
6007 CHECKSUM_ATTR (attrs.at_discr_value);
6008 CHECKSUM_ATTR (attrs.at_encoding);
6009 CHECKSUM_ATTR (attrs.at_endianity);
6010 CHECKSUM_ATTR (attrs.at_explicit);
6011 CHECKSUM_ATTR (attrs.at_is_optional);
6012 CHECKSUM_ATTR (attrs.at_location);
6013 CHECKSUM_ATTR (attrs.at_lower_bound);
6014 CHECKSUM_ATTR (attrs.at_mutable);
6015 CHECKSUM_ATTR (attrs.at_ordering);
6016 CHECKSUM_ATTR (attrs.at_picture_string);
6017 CHECKSUM_ATTR (attrs.at_prototyped);
6018 CHECKSUM_ATTR (attrs.at_small);
6019 CHECKSUM_ATTR (attrs.at_segment);
6020 CHECKSUM_ATTR (attrs.at_string_length);
6021 CHECKSUM_ATTR (attrs.at_threads_scaled);
6022 CHECKSUM_ATTR (attrs.at_upper_bound);
6023 CHECKSUM_ATTR (attrs.at_use_location);
6024 CHECKSUM_ATTR (attrs.at_use_UTF8);
6025 CHECKSUM_ATTR (attrs.at_variable_parameter);
6026 CHECKSUM_ATTR (attrs.at_virtuality);
6027 CHECKSUM_ATTR (attrs.at_visibility);
6028 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6029 CHECKSUM_ATTR (attrs.at_type);
6030 CHECKSUM_ATTR (attrs.at_friend);
6032 /* Checksum the child DIEs, except for nested types and member functions. */
6033 c = die->die_child;
6034 if (c) do {
6035 dw_attr_ref name_attr;
6037 c = c->die_sib;
6038 name_attr = get_AT (c, DW_AT_name);
6039 if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
6040 && name_attr != NULL)
6042 CHECKSUM_ULEB128 ('S');
6043 CHECKSUM_ULEB128 (c->die_tag);
6044 CHECKSUM_STRING (AT_string (name_attr));
6046 else
6048 /* Mark this DIE so it gets processed when unmarking. */
6049 if (c->die_mark == 0)
6050 c->die_mark = -1;
6051 die_checksum_ordered (c, ctx, mark);
6053 } while (c != die->die_child);
6055 CHECKSUM_ULEB128 (0);
6058 #undef CHECKSUM
6059 #undef CHECKSUM_STRING
6060 #undef CHECKSUM_ATTR
6061 #undef CHECKSUM_LEB128
6062 #undef CHECKSUM_ULEB128
6064 /* Generate the type signature for DIE. This is computed by generating an
6065 MD5 checksum over the DIE's tag, its relevant attributes, and its
6066 children. Attributes that are references to other DIEs are processed
6067 by recursion, using the MARK field to prevent infinite recursion.
6068 If the DIE is nested inside a namespace or another type, we also
6069 need to include that context in the signature. The lower 64 bits
6070 of the resulting MD5 checksum comprise the signature. */
6072 static void
6073 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6075 int mark;
6076 const char *name;
6077 unsigned char checksum[16];
6078 struct md5_ctx ctx;
6079 dw_die_ref decl;
6080 dw_die_ref parent;
6082 name = get_AT_string (die, DW_AT_name);
6083 decl = get_AT_ref (die, DW_AT_specification);
6084 parent = get_die_parent (die);
6086 /* First, compute a signature for just the type name (and its surrounding
6087 context, if any. This is stored in the type unit DIE for link-time
6088 ODR (one-definition rule) checking. */
6090 if (is_cxx() && name != NULL)
6092 md5_init_ctx (&ctx);
6094 /* Checksum the names of surrounding namespaces and structures. */
6095 if (parent != NULL)
6096 checksum_die_context (parent, &ctx);
6098 md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
6099 md5_process_bytes (name, strlen (name) + 1, &ctx);
6100 md5_finish_ctx (&ctx, checksum);
6102 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6105 /* Next, compute the complete type signature. */
6107 md5_init_ctx (&ctx);
6108 mark = 1;
6109 die->die_mark = mark;
6111 /* Checksum the names of surrounding namespaces and structures. */
6112 if (parent != NULL)
6113 checksum_die_context (parent, &ctx);
6115 /* Checksum the DIE and its children. */
6116 die_checksum_ordered (die, &ctx, &mark);
6117 unmark_all_dies (die);
6118 md5_finish_ctx (&ctx, checksum);
6120 /* Store the signature in the type node and link the type DIE and the
6121 type node together. */
6122 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6123 DWARF_TYPE_SIGNATURE_SIZE);
6124 die->comdat_type_p = true;
6125 die->die_id.die_type_node = type_node;
6126 type_node->type_die = die;
6128 /* If the DIE is a specification, link its declaration to the type node
6129 as well. */
6130 if (decl != NULL)
6132 decl->comdat_type_p = true;
6133 decl->die_id.die_type_node = type_node;
6137 /* Do the location expressions look same? */
6138 static inline int
6139 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6141 return loc1->dw_loc_opc == loc2->dw_loc_opc
6142 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6143 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6146 /* Do the values look the same? */
6147 static int
6148 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6150 dw_loc_descr_ref loc1, loc2;
6151 rtx r1, r2;
6153 if (v1->val_class != v2->val_class)
6154 return 0;
6156 switch (v1->val_class)
6158 case dw_val_class_const:
6159 return v1->v.val_int == v2->v.val_int;
6160 case dw_val_class_unsigned_const:
6161 return v1->v.val_unsigned == v2->v.val_unsigned;
6162 case dw_val_class_const_double:
6163 return v1->v.val_double.high == v2->v.val_double.high
6164 && v1->v.val_double.low == v2->v.val_double.low;
6165 case dw_val_class_vec:
6166 if (v1->v.val_vec.length != v2->v.val_vec.length
6167 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6168 return 0;
6169 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6170 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6171 return 0;
6172 return 1;
6173 case dw_val_class_flag:
6174 return v1->v.val_flag == v2->v.val_flag;
6175 case dw_val_class_str:
6176 return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
6178 case dw_val_class_addr:
6179 r1 = v1->v.val_addr;
6180 r2 = v2->v.val_addr;
6181 if (GET_CODE (r1) != GET_CODE (r2))
6182 return 0;
6183 return !rtx_equal_p (r1, r2);
6185 case dw_val_class_offset:
6186 return v1->v.val_offset == v2->v.val_offset;
6188 case dw_val_class_loc:
6189 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6190 loc1 && loc2;
6191 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6192 if (!same_loc_p (loc1, loc2, mark))
6193 return 0;
6194 return !loc1 && !loc2;
6196 case dw_val_class_die_ref:
6197 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6199 case dw_val_class_fde_ref:
6200 case dw_val_class_vms_delta:
6201 case dw_val_class_lbl_id:
6202 case dw_val_class_lineptr:
6203 case dw_val_class_macptr:
6204 case dw_val_class_high_pc:
6205 return 1;
6207 case dw_val_class_file:
6208 return v1->v.val_file == v2->v.val_file;
6210 case dw_val_class_data8:
6211 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6213 default:
6214 return 1;
6218 /* Do the attributes look the same? */
6220 static int
6221 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6223 if (at1->dw_attr != at2->dw_attr)
6224 return 0;
6226 /* We don't care that this was compiled with a different compiler
6227 snapshot; if the output is the same, that's what matters. */
6228 if (at1->dw_attr == DW_AT_producer)
6229 return 1;
6231 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6234 /* Do the dies look the same? */
6236 static int
6237 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6239 dw_die_ref c1, c2;
6240 dw_attr_ref a1;
6241 unsigned ix;
6243 /* To avoid infinite recursion. */
6244 if (die1->die_mark)
6245 return die1->die_mark == die2->die_mark;
6246 die1->die_mark = die2->die_mark = ++(*mark);
6248 if (die1->die_tag != die2->die_tag)
6249 return 0;
6251 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6252 return 0;
6254 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6255 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6256 return 0;
6258 c1 = die1->die_child;
6259 c2 = die2->die_child;
6260 if (! c1)
6262 if (c2)
6263 return 0;
6265 else
6266 for (;;)
6268 if (!same_die_p (c1, c2, mark))
6269 return 0;
6270 c1 = c1->die_sib;
6271 c2 = c2->die_sib;
6272 if (c1 == die1->die_child)
6274 if (c2 == die2->die_child)
6275 break;
6276 else
6277 return 0;
6281 return 1;
6284 /* Do the dies look the same? Wrapper around same_die_p. */
6286 static int
6287 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6289 int mark = 0;
6290 int ret = same_die_p (die1, die2, &mark);
6292 unmark_all_dies (die1);
6293 unmark_all_dies (die2);
6295 return ret;
6298 /* The prefix to attach to symbols on DIEs in the current comdat debug
6299 info section. */
6300 static const char *comdat_symbol_id;
6302 /* The index of the current symbol within the current comdat CU. */
6303 static unsigned int comdat_symbol_number;
6305 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6306 children, and set comdat_symbol_id accordingly. */
6308 static void
6309 compute_section_prefix (dw_die_ref unit_die)
6311 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6312 const char *base = die_name ? lbasename (die_name) : "anonymous";
6313 char *name = XALLOCAVEC (char, strlen (base) + 64);
6314 char *p;
6315 int i, mark;
6316 unsigned char checksum[16];
6317 struct md5_ctx ctx;
6319 /* Compute the checksum of the DIE, then append part of it as hex digits to
6320 the name filename of the unit. */
6322 md5_init_ctx (&ctx);
6323 mark = 0;
6324 die_checksum (unit_die, &ctx, &mark);
6325 unmark_all_dies (unit_die);
6326 md5_finish_ctx (&ctx, checksum);
6328 sprintf (name, "%s.", base);
6329 clean_symbol_name (name);
6331 p = name + strlen (name);
6332 for (i = 0; i < 4; i++)
6334 sprintf (p, "%.2x", checksum[i]);
6335 p += 2;
6338 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6339 comdat_symbol_number = 0;
6342 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6344 static int
6345 is_type_die (dw_die_ref die)
6347 switch (die->die_tag)
6349 case DW_TAG_array_type:
6350 case DW_TAG_class_type:
6351 case DW_TAG_interface_type:
6352 case DW_TAG_enumeration_type:
6353 case DW_TAG_pointer_type:
6354 case DW_TAG_reference_type:
6355 case DW_TAG_rvalue_reference_type:
6356 case DW_TAG_string_type:
6357 case DW_TAG_structure_type:
6358 case DW_TAG_subroutine_type:
6359 case DW_TAG_union_type:
6360 case DW_TAG_ptr_to_member_type:
6361 case DW_TAG_set_type:
6362 case DW_TAG_subrange_type:
6363 case DW_TAG_base_type:
6364 case DW_TAG_const_type:
6365 case DW_TAG_file_type:
6366 case DW_TAG_packed_type:
6367 case DW_TAG_volatile_type:
6368 case DW_TAG_typedef:
6369 return 1;
6370 default:
6371 return 0;
6375 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6376 Basically, we want to choose the bits that are likely to be shared between
6377 compilations (types) and leave out the bits that are specific to individual
6378 compilations (functions). */
6380 static int
6381 is_comdat_die (dw_die_ref c)
6383 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6384 we do for stabs. The advantage is a greater likelihood of sharing between
6385 objects that don't include headers in the same order (and therefore would
6386 put the base types in a different comdat). jason 8/28/00 */
6388 if (c->die_tag == DW_TAG_base_type)
6389 return 0;
6391 if (c->die_tag == DW_TAG_pointer_type
6392 || c->die_tag == DW_TAG_reference_type
6393 || c->die_tag == DW_TAG_rvalue_reference_type
6394 || c->die_tag == DW_TAG_const_type
6395 || c->die_tag == DW_TAG_volatile_type)
6397 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6399 return t ? is_comdat_die (t) : 0;
6402 return is_type_die (c);
6405 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6406 compilation unit. */
6408 static int
6409 is_symbol_die (dw_die_ref c)
6411 return (is_type_die (c)
6412 || is_declaration_die (c)
6413 || c->die_tag == DW_TAG_namespace
6414 || c->die_tag == DW_TAG_module);
6417 /* Returns true iff C is a compile-unit DIE. */
6419 static inline bool
6420 is_cu_die (dw_die_ref c)
6422 return c && c->die_tag == DW_TAG_compile_unit;
6425 /* Returns true iff C is a unit DIE of some sort. */
6427 static inline bool
6428 is_unit_die (dw_die_ref c)
6430 return c && (c->die_tag == DW_TAG_compile_unit
6431 || c->die_tag == DW_TAG_partial_unit
6432 || c->die_tag == DW_TAG_type_unit);
6435 /* Returns true iff C is a namespace DIE. */
6437 static inline bool
6438 is_namespace_die (dw_die_ref c)
6440 return c && c->die_tag == DW_TAG_namespace;
6443 /* Returns true iff C is a class or structure DIE. */
6445 static inline bool
6446 is_class_die (dw_die_ref c)
6448 return c && (c->die_tag == DW_TAG_class_type
6449 || c->die_tag == DW_TAG_structure_type);
6452 static char *
6453 gen_internal_sym (const char *prefix)
6455 char buf[256];
6457 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6458 return xstrdup (buf);
6461 /* Assign symbols to all worthy DIEs under DIE. */
6463 static void
6464 assign_symbol_names (dw_die_ref die)
6466 dw_die_ref c;
6468 if (is_symbol_die (die) && !die->comdat_type_p)
6470 if (comdat_symbol_id)
6472 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6474 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6475 comdat_symbol_id, comdat_symbol_number++);
6476 die->die_id.die_symbol = xstrdup (p);
6478 else
6479 die->die_id.die_symbol = gen_internal_sym ("LDIE");
6482 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6485 struct cu_hash_table_entry
6487 dw_die_ref cu;
6488 unsigned min_comdat_num, max_comdat_num;
6489 struct cu_hash_table_entry *next;
6492 /* Routines to manipulate hash table of CUs. */
6493 static hashval_t
6494 htab_cu_hash (const void *of)
6496 const struct cu_hash_table_entry *const entry =
6497 (const struct cu_hash_table_entry *) of;
6499 return htab_hash_string (entry->cu->die_id.die_symbol);
6502 static int
6503 htab_cu_eq (const void *of1, const void *of2)
6505 const struct cu_hash_table_entry *const entry1 =
6506 (const struct cu_hash_table_entry *) of1;
6507 const struct die_struct *const entry2 = (const struct die_struct *) of2;
6509 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6512 static void
6513 htab_cu_del (void *what)
6515 struct cu_hash_table_entry *next,
6516 *entry = (struct cu_hash_table_entry *) what;
6518 while (entry)
6520 next = entry->next;
6521 free (entry);
6522 entry = next;
6526 /* Check whether we have already seen this CU and set up SYM_NUM
6527 accordingly. */
6528 static int
6529 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
6531 struct cu_hash_table_entry dummy;
6532 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6534 dummy.max_comdat_num = 0;
6536 slot = (struct cu_hash_table_entry **)
6537 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
6538 INSERT);
6539 entry = *slot;
6541 for (; entry; last = entry, entry = entry->next)
6543 if (same_die_p_wrap (cu, entry->cu))
6544 break;
6547 if (entry)
6549 *sym_num = entry->min_comdat_num;
6550 return 1;
6553 entry = XCNEW (struct cu_hash_table_entry);
6554 entry->cu = cu;
6555 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6556 entry->next = *slot;
6557 *slot = entry;
6559 return 0;
6562 /* Record SYM_NUM to record of CU in HTABLE. */
6563 static void
6564 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
6566 struct cu_hash_table_entry **slot, *entry;
6568 slot = (struct cu_hash_table_entry **)
6569 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
6570 NO_INSERT);
6571 entry = *slot;
6573 entry->max_comdat_num = sym_num;
6576 /* Traverse the DIE (which is always comp_unit_die), and set up
6577 additional compilation units for each of the include files we see
6578 bracketed by BINCL/EINCL. */
6580 static void
6581 break_out_includes (dw_die_ref die)
6583 dw_die_ref c;
6584 dw_die_ref unit = NULL;
6585 limbo_die_node *node, **pnode;
6586 htab_t cu_hash_table;
6588 c = die->die_child;
6589 if (c) do {
6590 dw_die_ref prev = c;
6591 c = c->die_sib;
6592 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6593 || (unit && is_comdat_die (c)))
6595 dw_die_ref next = c->die_sib;
6597 /* This DIE is for a secondary CU; remove it from the main one. */
6598 remove_child_with_prev (c, prev);
6600 if (c->die_tag == DW_TAG_GNU_BINCL)
6601 unit = push_new_compile_unit (unit, c);
6602 else if (c->die_tag == DW_TAG_GNU_EINCL)
6603 unit = pop_compile_unit (unit);
6604 else
6605 add_child_die (unit, c);
6606 c = next;
6607 if (c == die->die_child)
6608 break;
6610 } while (c != die->die_child);
6612 #if 0
6613 /* We can only use this in debugging, since the frontend doesn't check
6614 to make sure that we leave every include file we enter. */
6615 gcc_assert (!unit);
6616 #endif
6618 assign_symbol_names (die);
6619 cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6620 for (node = limbo_die_list, pnode = &limbo_die_list;
6621 node;
6622 node = node->next)
6624 int is_dupl;
6626 compute_section_prefix (node->die);
6627 is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6628 &comdat_symbol_number);
6629 assign_symbol_names (node->die);
6630 if (is_dupl)
6631 *pnode = node->next;
6632 else
6634 pnode = &node->next;
6635 record_comdat_symbol_number (node->die, cu_hash_table,
6636 comdat_symbol_number);
6639 htab_delete (cu_hash_table);
6642 /* Return non-zero if this DIE is a declaration. */
6644 static int
6645 is_declaration_die (dw_die_ref die)
6647 dw_attr_ref a;
6648 unsigned ix;
6650 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6651 if (a->dw_attr == DW_AT_declaration)
6652 return 1;
6654 return 0;
6657 /* Return non-zero if this DIE is nested inside a subprogram. */
6659 static int
6660 is_nested_in_subprogram (dw_die_ref die)
6662 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
6664 if (decl == NULL)
6665 decl = die;
6666 return local_scope_p (decl);
6669 /* Return non-zero if this DIE contains a defining declaration of a
6670 subprogram. */
6672 static int
6673 contains_subprogram_definition (dw_die_ref die)
6675 dw_die_ref c;
6677 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
6678 return 1;
6679 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition(c)) return 1);
6680 return 0;
6683 /* Return non-zero if this is a type DIE that should be moved to a
6684 COMDAT .debug_types section. */
6686 static int
6687 should_move_die_to_comdat (dw_die_ref die)
6689 switch (die->die_tag)
6691 case DW_TAG_class_type:
6692 case DW_TAG_structure_type:
6693 case DW_TAG_enumeration_type:
6694 case DW_TAG_union_type:
6695 /* Don't move declarations, inlined instances, or types nested in a
6696 subprogram. */
6697 if (is_declaration_die (die)
6698 || get_AT (die, DW_AT_abstract_origin)
6699 || is_nested_in_subprogram (die))
6700 return 0;
6701 /* A type definition should never contain a subprogram definition. */
6702 gcc_assert (!contains_subprogram_definition (die));
6703 return 1;
6704 case DW_TAG_array_type:
6705 case DW_TAG_interface_type:
6706 case DW_TAG_pointer_type:
6707 case DW_TAG_reference_type:
6708 case DW_TAG_rvalue_reference_type:
6709 case DW_TAG_string_type:
6710 case DW_TAG_subroutine_type:
6711 case DW_TAG_ptr_to_member_type:
6712 case DW_TAG_set_type:
6713 case DW_TAG_subrange_type:
6714 case DW_TAG_base_type:
6715 case DW_TAG_const_type:
6716 case DW_TAG_file_type:
6717 case DW_TAG_packed_type:
6718 case DW_TAG_volatile_type:
6719 case DW_TAG_typedef:
6720 default:
6721 return 0;
6725 /* Make a clone of DIE. */
6727 static dw_die_ref
6728 clone_die (dw_die_ref die)
6730 dw_die_ref clone;
6731 dw_attr_ref a;
6732 unsigned ix;
6734 clone = ggc_alloc_cleared_die_node ();
6735 clone->die_tag = die->die_tag;
6737 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6738 add_dwarf_attr (clone, a);
6740 return clone;
6743 /* Make a clone of the tree rooted at DIE. */
6745 static dw_die_ref
6746 clone_tree (dw_die_ref die)
6748 dw_die_ref c;
6749 dw_die_ref clone = clone_die (die);
6751 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
6753 return clone;
6756 /* Make a clone of DIE as a declaration. */
6758 static dw_die_ref
6759 clone_as_declaration (dw_die_ref die)
6761 dw_die_ref clone;
6762 dw_die_ref decl;
6763 dw_attr_ref a;
6764 unsigned ix;
6766 /* If the DIE is already a declaration, just clone it. */
6767 if (is_declaration_die (die))
6768 return clone_die (die);
6770 /* If the DIE is a specification, just clone its declaration DIE. */
6771 decl = get_AT_ref (die, DW_AT_specification);
6772 if (decl != NULL)
6774 clone = clone_die (decl);
6775 if (die->comdat_type_p)
6776 add_AT_die_ref (clone, DW_AT_signature, die);
6777 return clone;
6780 clone = ggc_alloc_cleared_die_node ();
6781 clone->die_tag = die->die_tag;
6783 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6785 /* We don't want to copy over all attributes.
6786 For example we don't want DW_AT_byte_size because otherwise we will no
6787 longer have a declaration and GDB will treat it as a definition. */
6789 switch (a->dw_attr)
6791 case DW_AT_artificial:
6792 case DW_AT_containing_type:
6793 case DW_AT_external:
6794 case DW_AT_name:
6795 case DW_AT_type:
6796 case DW_AT_virtuality:
6797 case DW_AT_linkage_name:
6798 case DW_AT_MIPS_linkage_name:
6799 add_dwarf_attr (clone, a);
6800 break;
6801 case DW_AT_byte_size:
6802 default:
6803 break;
6807 if (die->comdat_type_p)
6808 add_AT_die_ref (clone, DW_AT_signature, die);
6810 add_AT_flag (clone, DW_AT_declaration, 1);
6811 return clone;
6814 /* Copy the declaration context to the new type unit DIE. This includes
6815 any surrounding namespace or type declarations. If the DIE has an
6816 AT_specification attribute, it also includes attributes and children
6817 attached to the specification, and returns a pointer to the original
6818 parent of the declaration DIE. Returns NULL otherwise. */
6820 static dw_die_ref
6821 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
6823 dw_die_ref decl;
6824 dw_die_ref new_decl;
6825 dw_die_ref orig_parent = NULL;
6827 decl = get_AT_ref (die, DW_AT_specification);
6828 if (decl == NULL)
6829 decl = die;
6830 else
6832 unsigned ix;
6833 dw_die_ref c;
6834 dw_attr_ref a;
6836 /* The original DIE will be changed to a declaration, and must
6837 be moved to be a child of the original declaration DIE. */
6838 orig_parent = decl->die_parent;
6840 /* Copy the type node pointer from the new DIE to the original
6841 declaration DIE so we can forward references later. */
6842 decl->comdat_type_p = true;
6843 decl->die_id.die_type_node = die->die_id.die_type_node;
6845 remove_AT (die, DW_AT_specification);
6847 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
6849 if (a->dw_attr != DW_AT_name
6850 && a->dw_attr != DW_AT_declaration
6851 && a->dw_attr != DW_AT_external)
6852 add_dwarf_attr (die, a);
6855 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
6858 if (decl->die_parent != NULL
6859 && !is_unit_die (decl->die_parent))
6861 new_decl = copy_ancestor_tree (unit, decl, NULL);
6862 if (new_decl != NULL)
6864 remove_AT (new_decl, DW_AT_signature);
6865 add_AT_specification (die, new_decl);
6869 return orig_parent;
6872 /* Generate the skeleton ancestor tree for the given NODE, then clone
6873 the DIE and add the clone into the tree. */
6875 static void
6876 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
6878 if (node->new_die != NULL)
6879 return;
6881 node->new_die = clone_as_declaration (node->old_die);
6883 if (node->parent != NULL)
6885 generate_skeleton_ancestor_tree (node->parent);
6886 add_child_die (node->parent->new_die, node->new_die);
6890 /* Generate a skeleton tree of DIEs containing any declarations that are
6891 found in the original tree. We traverse the tree looking for declaration
6892 DIEs, and construct the skeleton from the bottom up whenever we find one. */
6894 static void
6895 generate_skeleton_bottom_up (skeleton_chain_node *parent)
6897 skeleton_chain_node node;
6898 dw_die_ref c;
6899 dw_die_ref first;
6900 dw_die_ref prev = NULL;
6901 dw_die_ref next = NULL;
6903 node.parent = parent;
6905 first = c = parent->old_die->die_child;
6906 if (c)
6907 next = c->die_sib;
6908 if (c) do {
6909 if (prev == NULL || prev->die_sib == c)
6910 prev = c;
6911 c = next;
6912 next = (c == first ? NULL : c->die_sib);
6913 node.old_die = c;
6914 node.new_die = NULL;
6915 if (is_declaration_die (c))
6917 /* Clone the existing DIE, move the original to the skeleton
6918 tree (which is in the main CU), and put the clone, with
6919 all the original's children, where the original came from. */
6920 dw_die_ref clone = clone_die (c);
6921 move_all_children (c, clone);
6923 replace_child (c, clone, prev);
6924 generate_skeleton_ancestor_tree (parent);
6925 add_child_die (parent->new_die, c);
6926 node.new_die = c;
6927 c = clone;
6929 generate_skeleton_bottom_up (&node);
6930 } while (next != NULL);
6933 /* Wrapper function for generate_skeleton_bottom_up. */
6935 static dw_die_ref
6936 generate_skeleton (dw_die_ref die)
6938 skeleton_chain_node node;
6940 node.old_die = die;
6941 node.new_die = NULL;
6942 node.parent = NULL;
6944 /* If this type definition is nested inside another type,
6945 always leave at least a declaration in its place. */
6946 if (die->die_parent != NULL && is_type_die (die->die_parent))
6947 node.new_die = clone_as_declaration (die);
6949 generate_skeleton_bottom_up (&node);
6950 return node.new_die;
6953 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
6954 declaration. The original DIE is moved to a new compile unit so that
6955 existing references to it follow it to the new location. If any of the
6956 original DIE's descendants is a declaration, we need to replace the
6957 original DIE with a skeleton tree and move the declarations back into the
6958 skeleton tree. */
6960 static dw_die_ref
6961 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
6962 dw_die_ref prev)
6964 dw_die_ref skeleton, orig_parent;
6966 /* Copy the declaration context to the type unit DIE. If the returned
6967 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
6968 that DIE. */
6969 orig_parent = copy_declaration_context (unit, child);
6971 skeleton = generate_skeleton (child);
6972 if (skeleton == NULL)
6973 remove_child_with_prev (child, prev);
6974 else
6976 skeleton->comdat_type_p = true;
6977 skeleton->die_id.die_type_node = child->die_id.die_type_node;
6979 /* If the original DIE was a specification, we need to put
6980 the skeleton under the parent DIE of the declaration.
6981 This leaves the original declaration in the tree, but
6982 it will be pruned later since there are no longer any
6983 references to it. */
6984 if (orig_parent != NULL)
6986 remove_child_with_prev (child, prev);
6987 add_child_die (orig_parent, skeleton);
6989 else
6990 replace_child (child, skeleton, prev);
6993 return skeleton;
6996 /* Traverse the DIE and set up additional .debug_types sections for each
6997 type worthy of being placed in a COMDAT section. */
6999 static void
7000 break_out_comdat_types (dw_die_ref die)
7002 dw_die_ref c;
7003 dw_die_ref first;
7004 dw_die_ref prev = NULL;
7005 dw_die_ref next = NULL;
7006 dw_die_ref unit = NULL;
7008 first = c = die->die_child;
7009 if (c)
7010 next = c->die_sib;
7011 if (c) do {
7012 if (prev == NULL || prev->die_sib == c)
7013 prev = c;
7014 c = next;
7015 next = (c == first ? NULL : c->die_sib);
7016 if (should_move_die_to_comdat (c))
7018 dw_die_ref replacement;
7019 comdat_type_node_ref type_node;
7021 /* Create a new type unit DIE as the root for the new tree, and
7022 add it to the list of comdat types. */
7023 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7024 add_AT_unsigned (unit, DW_AT_language,
7025 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7026 type_node = ggc_alloc_cleared_comdat_type_node ();
7027 type_node->root_die = unit;
7028 type_node->next = comdat_type_list;
7029 comdat_type_list = type_node;
7031 /* Generate the type signature. */
7032 generate_type_signature (c, type_node);
7034 /* Copy the declaration context, attributes, and children of the
7035 declaration into the new type unit DIE, then remove this DIE
7036 from the main CU (or replace it with a skeleton if necessary). */
7037 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7038 type_node->skeleton_die = replacement;
7040 /* Break out nested types into their own type units. */
7041 break_out_comdat_types (c);
7043 /* Add the DIE to the new compunit. */
7044 add_child_die (unit, c);
7046 if (replacement != NULL)
7047 c = replacement;
7049 else if (c->die_tag == DW_TAG_namespace
7050 || c->die_tag == DW_TAG_class_type
7051 || c->die_tag == DW_TAG_structure_type
7052 || c->die_tag == DW_TAG_union_type)
7054 /* Look for nested types that can be broken out. */
7055 break_out_comdat_types (c);
7057 } while (next != NULL);
7060 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7062 struct decl_table_entry
7064 dw_die_ref orig;
7065 dw_die_ref copy;
7068 /* Routines to manipulate hash table of copied declarations. */
7070 static hashval_t
7071 htab_decl_hash (const void *of)
7073 const struct decl_table_entry *const entry =
7074 (const struct decl_table_entry *) of;
7076 return htab_hash_pointer (entry->orig);
7079 static int
7080 htab_decl_eq (const void *of1, const void *of2)
7082 const struct decl_table_entry *const entry1 =
7083 (const struct decl_table_entry *) of1;
7084 const struct die_struct *const entry2 = (const struct die_struct *) of2;
7086 return entry1->orig == entry2;
7089 static void
7090 htab_decl_del (void *what)
7092 struct decl_table_entry *entry = (struct decl_table_entry *) what;
7094 free (entry);
7097 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7098 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7099 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7100 to check if the ancestor has already been copied into UNIT. */
7102 static dw_die_ref
7103 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
7105 dw_die_ref parent = die->die_parent;
7106 dw_die_ref new_parent = unit;
7107 dw_die_ref copy;
7108 void **slot = NULL;
7109 struct decl_table_entry *entry = NULL;
7111 if (decl_table)
7113 /* Check if the entry has already been copied to UNIT. */
7114 slot = htab_find_slot_with_hash (decl_table, die,
7115 htab_hash_pointer (die), INSERT);
7116 if (*slot != HTAB_EMPTY_ENTRY)
7118 entry = (struct decl_table_entry *) *slot;
7119 return entry->copy;
7122 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7123 entry = XCNEW (struct decl_table_entry);
7124 entry->orig = die;
7125 entry->copy = NULL;
7126 *slot = entry;
7129 if (parent != NULL)
7131 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7132 if (spec != NULL)
7133 parent = spec;
7134 if (!is_unit_die (parent))
7135 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7138 copy = clone_as_declaration (die);
7139 add_child_die (new_parent, copy);
7141 if (decl_table != NULL)
7143 /* Record the pointer to the copy. */
7144 entry->copy = copy;
7147 return copy;
7150 /* Like clone_tree, but additionally enter all the children into
7151 the hash table decl_table. */
7153 static dw_die_ref
7154 clone_tree_hash (dw_die_ref die, htab_t decl_table)
7156 dw_die_ref c;
7157 dw_die_ref clone = clone_die (die);
7158 struct decl_table_entry *entry;
7159 void **slot = htab_find_slot_with_hash (decl_table, die,
7160 htab_hash_pointer (die), INSERT);
7161 /* Assert that DIE isn't in the hash table yet. If it would be there
7162 before, the ancestors would be necessarily there as well, therefore
7163 clone_tree_hash wouldn't be called. */
7164 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7165 entry = XCNEW (struct decl_table_entry);
7166 entry->orig = die;
7167 entry->copy = clone;
7168 *slot = entry;
7170 FOR_EACH_CHILD (die, c,
7171 add_child_die (clone, clone_tree_hash (c, decl_table)));
7173 return clone;
7176 /* Walk the DIE and its children, looking for references to incomplete
7177 or trivial types that are unmarked (i.e., that are not in the current
7178 type_unit). */
7180 static void
7181 copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
7183 dw_die_ref c;
7184 dw_attr_ref a;
7185 unsigned ix;
7187 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7189 if (AT_class (a) == dw_val_class_die_ref)
7191 dw_die_ref targ = AT_ref (a);
7192 void **slot;
7193 struct decl_table_entry *entry;
7195 if (targ->die_mark != 0 || targ->comdat_type_p)
7196 continue;
7198 slot = htab_find_slot_with_hash (decl_table, targ,
7199 htab_hash_pointer (targ), INSERT);
7201 if (*slot != HTAB_EMPTY_ENTRY)
7203 /* TARG has already been copied, so we just need to
7204 modify the reference to point to the copy. */
7205 entry = (struct decl_table_entry *) *slot;
7206 a->dw_attr_val.v.val_die_ref.die = entry->copy;
7208 else
7210 dw_die_ref parent = unit;
7211 dw_die_ref copy = clone_die (targ);
7213 /* Record in DECL_TABLE that TARG has been copied.
7214 Need to do this now, before the recursive call,
7215 because DECL_TABLE may be expanded and SLOT
7216 would no longer be a valid pointer. */
7217 entry = XCNEW (struct decl_table_entry);
7218 entry->orig = targ;
7219 entry->copy = copy;
7220 *slot = entry;
7222 FOR_EACH_CHILD (targ, c,
7223 add_child_die (copy,
7224 clone_tree_hash (c, decl_table)));
7226 /* Make sure the cloned tree is marked as part of the
7227 type unit. */
7228 mark_dies (copy);
7230 /* If TARG has surrounding context, copy its ancestor tree
7231 into the new type unit. */
7232 if (targ->die_parent != NULL
7233 && !is_unit_die (targ->die_parent))
7234 parent = copy_ancestor_tree (unit, targ->die_parent,
7235 decl_table);
7237 add_child_die (parent, copy);
7238 a->dw_attr_val.v.val_die_ref.die = copy;
7240 /* Make sure the newly-copied DIE is walked. If it was
7241 installed in a previously-added context, it won't
7242 get visited otherwise. */
7243 if (parent != unit)
7245 /* Find the highest point of the newly-added tree,
7246 mark each node along the way, and walk from there. */
7247 parent->die_mark = 1;
7248 while (parent->die_parent
7249 && parent->die_parent->die_mark == 0)
7251 parent = parent->die_parent;
7252 parent->die_mark = 1;
7254 copy_decls_walk (unit, parent, decl_table);
7260 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7263 /* Copy declarations for "unworthy" types into the new comdat section.
7264 Incomplete types, modified types, and certain other types aren't broken
7265 out into comdat sections of their own, so they don't have a signature,
7266 and we need to copy the declaration into the same section so that we
7267 don't have an external reference. */
7269 static void
7270 copy_decls_for_unworthy_types (dw_die_ref unit)
7272 htab_t decl_table;
7274 mark_dies (unit);
7275 decl_table = htab_create (10, htab_decl_hash, htab_decl_eq, htab_decl_del);
7276 copy_decls_walk (unit, unit, decl_table);
7277 htab_delete (decl_table);
7278 unmark_dies (unit);
7281 /* Traverse the DIE and add a sibling attribute if it may have the
7282 effect of speeding up access to siblings. To save some space,
7283 avoid generating sibling attributes for DIE's without children. */
7285 static void
7286 add_sibling_attributes (dw_die_ref die)
7288 dw_die_ref c;
7290 if (! die->die_child)
7291 return;
7293 if (die->die_parent && die != die->die_parent->die_child)
7294 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7296 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7299 /* Output all location lists for the DIE and its children. */
7301 static void
7302 output_location_lists (dw_die_ref die)
7304 dw_die_ref c;
7305 dw_attr_ref a;
7306 unsigned ix;
7308 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7309 if (AT_class (a) == dw_val_class_loc_list)
7310 output_loc_list (AT_loc_list (a));
7312 FOR_EACH_CHILD (die, c, output_location_lists (c));
7315 /* We want to limit the number of external references, because they are
7316 larger than local references: a relocation takes multiple words, and
7317 even a sig8 reference is always eight bytes, whereas a local reference
7318 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7319 So if we encounter multiple external references to the same type DIE, we
7320 make a local typedef stub for it and redirect all references there.
7322 This is the element of the hash table for keeping track of these
7323 references. */
7325 struct external_ref
7327 dw_die_ref type;
7328 dw_die_ref stub;
7329 unsigned n_refs;
7332 /* Hash an external_ref. */
7334 static hashval_t
7335 hash_external_ref (const void *p)
7337 const struct external_ref *r = (const struct external_ref *)p;
7338 return htab_hash_pointer (r->type);
7341 /* Compare external_refs. */
7343 static int
7344 external_ref_eq (const void *p1, const void *p2)
7346 const struct external_ref *r1 = (const struct external_ref *)p1;
7347 const struct external_ref *r2 = (const struct external_ref *)p2;
7348 return r1->type == r2->type;
7351 /* Return a pointer to the external_ref for references to DIE. */
7353 static struct external_ref *
7354 lookup_external_ref (htab_t map, dw_die_ref die)
7356 struct external_ref ref, *ref_p;
7357 void ** slot;
7359 ref.type = die;
7360 slot = htab_find_slot (map, &ref, INSERT);
7361 if (*slot != HTAB_EMPTY_ENTRY)
7362 return (struct external_ref *) *slot;
7364 ref_p = XCNEW (struct external_ref);
7365 ref_p->type = die;
7366 *slot = ref_p;
7367 return ref_p;
7370 /* Subroutine of optimize_external_refs, below.
7372 If we see a type skeleton, record it as our stub. If we see external
7373 references, remember how many we've seen. */
7375 static void
7376 optimize_external_refs_1 (dw_die_ref die, htab_t map)
7378 dw_die_ref c;
7379 dw_attr_ref a;
7380 unsigned ix;
7381 struct external_ref *ref_p;
7383 if (is_type_die (die)
7384 && (c = get_AT_ref (die, DW_AT_signature)))
7386 /* This is a local skeleton; use it for local references. */
7387 ref_p = lookup_external_ref (map, c);
7388 ref_p->stub = die;
7391 /* Scan the DIE references, and remember any that refer to DIEs from
7392 other CUs (i.e. those which are not marked). */
7393 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7394 if (AT_class (a) == dw_val_class_die_ref
7395 && (c = AT_ref (a))->die_mark == 0
7396 && is_type_die (c))
7398 ref_p = lookup_external_ref (map, c);
7399 ref_p->n_refs++;
7402 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7405 /* htab_traverse callback function for optimize_external_refs, below. SLOT
7406 points to an external_ref, DATA is the CU we're processing. If we don't
7407 already have a local stub, and we have multiple refs, build a stub. */
7409 static int
7410 build_local_stub (void **slot, void *data)
7412 struct external_ref *ref_p = (struct external_ref *)*slot;
7414 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7416 /* We have multiple references to this type, so build a small stub.
7417 Both of these forms are a bit dodgy from the perspective of the
7418 DWARF standard, since technically they should have names. */
7419 dw_die_ref cu = (dw_die_ref) data;
7420 dw_die_ref type = ref_p->type;
7421 dw_die_ref stub = NULL;
7423 if (type->comdat_type_p)
7425 /* If we refer to this type via sig8, use AT_signature. */
7426 stub = new_die (type->die_tag, cu, NULL_TREE);
7427 add_AT_die_ref (stub, DW_AT_signature, type);
7429 else
7431 /* Otherwise, use a typedef with no name. */
7432 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7433 add_AT_die_ref (stub, DW_AT_type, type);
7436 stub->die_mark++;
7437 ref_p->stub = stub;
7439 return 1;
7442 /* DIE is a unit; look through all the DIE references to see if there are
7443 any external references to types, and if so, create local stubs for
7444 them which will be applied in build_abbrev_table. This is useful because
7445 references to local DIEs are smaller. */
7447 static htab_t
7448 optimize_external_refs (dw_die_ref die)
7450 htab_t map = htab_create (10, hash_external_ref, external_ref_eq, free);
7451 optimize_external_refs_1 (die, map);
7452 htab_traverse (map, build_local_stub, die);
7453 return map;
7456 /* The format of each DIE (and its attribute value pairs) is encoded in an
7457 abbreviation table. This routine builds the abbreviation table and assigns
7458 a unique abbreviation id for each abbreviation entry. The children of each
7459 die are visited recursively. */
7461 static void
7462 build_abbrev_table (dw_die_ref die, htab_t extern_map)
7464 unsigned long abbrev_id;
7465 unsigned int n_alloc;
7466 dw_die_ref c;
7467 dw_attr_ref a;
7468 unsigned ix;
7470 /* Scan the DIE references, and replace any that refer to
7471 DIEs from other CUs (i.e. those which are not marked) with
7472 the local stubs we built in optimize_external_refs. */
7473 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7474 if (AT_class (a) == dw_val_class_die_ref
7475 && (c = AT_ref (a))->die_mark == 0)
7477 struct external_ref *ref_p;
7478 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
7480 ref_p = lookup_external_ref (extern_map, c);
7481 if (ref_p->stub && ref_p->stub != die)
7482 change_AT_die_ref (a, ref_p->stub);
7483 else
7484 /* We aren't changing this reference, so mark it external. */
7485 set_AT_ref_external (a, 1);
7488 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7490 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7491 dw_attr_ref die_a, abbrev_a;
7492 unsigned ix;
7493 bool ok = true;
7495 if (abbrev->die_tag != die->die_tag)
7496 continue;
7497 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7498 continue;
7500 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
7501 continue;
7503 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
7505 abbrev_a = &(*abbrev->die_attr)[ix];
7506 if ((abbrev_a->dw_attr != die_a->dw_attr)
7507 || (value_format (abbrev_a) != value_format (die_a)))
7509 ok = false;
7510 break;
7513 if (ok)
7514 break;
7517 if (abbrev_id >= abbrev_die_table_in_use)
7519 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7521 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7522 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7523 n_alloc);
7525 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7526 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7527 abbrev_die_table_allocated = n_alloc;
7530 ++abbrev_die_table_in_use;
7531 abbrev_die_table[abbrev_id] = die;
7534 die->die_abbrev = abbrev_id;
7535 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
7538 /* Return the power-of-two number of bytes necessary to represent VALUE. */
7540 static int
7541 constant_size (unsigned HOST_WIDE_INT value)
7543 int log;
7545 if (value == 0)
7546 log = 0;
7547 else
7548 log = floor_log2 (value);
7550 log = log / 8;
7551 log = 1 << (floor_log2 (log) + 1);
7553 return log;
7556 /* Return the size of a DIE as it is represented in the
7557 .debug_info section. */
7559 static unsigned long
7560 size_of_die (dw_die_ref die)
7562 unsigned long size = 0;
7563 dw_attr_ref a;
7564 unsigned ix;
7565 enum dwarf_form form;
7567 size += size_of_uleb128 (die->die_abbrev);
7568 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7570 switch (AT_class (a))
7572 case dw_val_class_addr:
7573 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7575 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7576 size += size_of_uleb128 (AT_index (a));
7578 else
7579 size += DWARF2_ADDR_SIZE;
7580 break;
7581 case dw_val_class_offset:
7582 size += DWARF_OFFSET_SIZE;
7583 break;
7584 case dw_val_class_loc:
7586 unsigned long lsize = size_of_locs (AT_loc (a));
7588 /* Block length. */
7589 if (dwarf_version >= 4)
7590 size += size_of_uleb128 (lsize);
7591 else
7592 size += constant_size (lsize);
7593 size += lsize;
7595 break;
7596 case dw_val_class_loc_list:
7597 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7599 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7600 size += size_of_uleb128 (AT_index (a));
7602 else
7603 size += DWARF_OFFSET_SIZE;
7604 break;
7605 case dw_val_class_range_list:
7606 size += DWARF_OFFSET_SIZE;
7607 break;
7608 case dw_val_class_const:
7609 size += size_of_sleb128 (AT_int (a));
7610 break;
7611 case dw_val_class_unsigned_const:
7613 int csize = constant_size (AT_unsigned (a));
7614 if (dwarf_version == 3
7615 && a->dw_attr == DW_AT_data_member_location
7616 && csize >= 4)
7617 size += size_of_uleb128 (AT_unsigned (a));
7618 else
7619 size += csize;
7621 break;
7622 case dw_val_class_const_double:
7623 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
7624 if (HOST_BITS_PER_WIDE_INT >= 64)
7625 size++; /* block */
7626 break;
7627 case dw_val_class_vec:
7628 size += constant_size (a->dw_attr_val.v.val_vec.length
7629 * a->dw_attr_val.v.val_vec.elt_size)
7630 + a->dw_attr_val.v.val_vec.length
7631 * a->dw_attr_val.v.val_vec.elt_size; /* block */
7632 break;
7633 case dw_val_class_flag:
7634 if (dwarf_version >= 4)
7635 /* Currently all add_AT_flag calls pass in 1 as last argument,
7636 so DW_FORM_flag_present can be used. If that ever changes,
7637 we'll need to use DW_FORM_flag and have some optimization
7638 in build_abbrev_table that will change those to
7639 DW_FORM_flag_present if it is set to 1 in all DIEs using
7640 the same abbrev entry. */
7641 gcc_assert (a->dw_attr_val.v.val_flag == 1);
7642 else
7643 size += 1;
7644 break;
7645 case dw_val_class_die_ref:
7646 if (AT_ref_external (a))
7648 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
7649 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
7650 is sized by target address length, whereas in DWARF3
7651 it's always sized as an offset. */
7652 if (use_debug_types)
7653 size += DWARF_TYPE_SIGNATURE_SIZE;
7654 else if (dwarf_version == 2)
7655 size += DWARF2_ADDR_SIZE;
7656 else
7657 size += DWARF_OFFSET_SIZE;
7659 else
7660 size += DWARF_OFFSET_SIZE;
7661 break;
7662 case dw_val_class_fde_ref:
7663 size += DWARF_OFFSET_SIZE;
7664 break;
7665 case dw_val_class_lbl_id:
7666 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7668 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7669 size += size_of_uleb128 (AT_index (a));
7671 else
7672 size += DWARF2_ADDR_SIZE;
7673 break;
7674 case dw_val_class_lineptr:
7675 case dw_val_class_macptr:
7676 size += DWARF_OFFSET_SIZE;
7677 break;
7678 case dw_val_class_str:
7679 form = AT_string_form (a);
7680 if (form == DW_FORM_strp)
7681 size += DWARF_OFFSET_SIZE;
7682 else if (form == DW_FORM_GNU_str_index)
7683 size += size_of_uleb128 (AT_index (a));
7684 else
7685 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7686 break;
7687 case dw_val_class_file:
7688 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7689 break;
7690 case dw_val_class_data8:
7691 size += 8;
7692 break;
7693 case dw_val_class_vms_delta:
7694 size += DWARF_OFFSET_SIZE;
7695 break;
7696 case dw_val_class_high_pc:
7697 size += DWARF2_ADDR_SIZE;
7698 break;
7699 default:
7700 gcc_unreachable ();
7704 return size;
7707 /* Size the debugging information associated with a given DIE. Visits the
7708 DIE's children recursively. Updates the global variable next_die_offset, on
7709 each time through. Uses the current value of next_die_offset to update the
7710 die_offset field in each DIE. */
7712 static void
7713 calc_die_sizes (dw_die_ref die)
7715 dw_die_ref c;
7717 gcc_assert (die->die_offset == 0
7718 || (unsigned long int) die->die_offset == next_die_offset);
7719 die->die_offset = next_die_offset;
7720 next_die_offset += size_of_die (die);
7722 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
7724 if (die->die_child != NULL)
7725 /* Count the null byte used to terminate sibling lists. */
7726 next_die_offset += 1;
7729 /* Size just the base type children at the start of the CU.
7730 This is needed because build_abbrev needs to size locs
7731 and sizing of type based stack ops needs to know die_offset
7732 values for the base types. */
7734 static void
7735 calc_base_type_die_sizes (void)
7737 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7738 unsigned int i;
7739 dw_die_ref base_type;
7740 #if ENABLE_ASSERT_CHECKING
7741 dw_die_ref prev = comp_unit_die ()->die_child;
7742 #endif
7744 die_offset += size_of_die (comp_unit_die ());
7745 for (i = 0; base_types.iterate (i, &base_type); i++)
7747 #if ENABLE_ASSERT_CHECKING
7748 gcc_assert (base_type->die_offset == 0
7749 && prev->die_sib == base_type
7750 && base_type->die_child == NULL
7751 && base_type->die_abbrev);
7752 prev = base_type;
7753 #endif
7754 base_type->die_offset = die_offset;
7755 die_offset += size_of_die (base_type);
7759 /* Set the marks for a die and its children. We do this so
7760 that we know whether or not a reference needs to use FORM_ref_addr; only
7761 DIEs in the same CU will be marked. We used to clear out the offset
7762 and use that as the flag, but ran into ordering problems. */
7764 static void
7765 mark_dies (dw_die_ref die)
7767 dw_die_ref c;
7769 gcc_assert (!die->die_mark);
7771 die->die_mark = 1;
7772 FOR_EACH_CHILD (die, c, mark_dies (c));
7775 /* Clear the marks for a die and its children. */
7777 static void
7778 unmark_dies (dw_die_ref die)
7780 dw_die_ref c;
7782 if (! use_debug_types)
7783 gcc_assert (die->die_mark);
7785 die->die_mark = 0;
7786 FOR_EACH_CHILD (die, c, unmark_dies (c));
7789 /* Clear the marks for a die, its children and referred dies. */
7791 static void
7792 unmark_all_dies (dw_die_ref die)
7794 dw_die_ref c;
7795 dw_attr_ref a;
7796 unsigned ix;
7798 if (!die->die_mark)
7799 return;
7800 die->die_mark = 0;
7802 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
7804 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7805 if (AT_class (a) == dw_val_class_die_ref)
7806 unmark_all_dies (AT_ref (a));
7809 /* Return the size of the .debug_pubnames or .debug_pubtypes table
7810 generated for the compilation unit. */
7812 static unsigned long
7813 size_of_pubnames (vec<pubname_entry, va_gc> *names)
7815 unsigned long size;
7816 unsigned i;
7817 pubname_ref p;
7819 size = DWARF_PUBNAMES_HEADER_SIZE;
7820 FOR_EACH_VEC_ELT (*names, i, p)
7821 if (names != pubtype_table
7822 || p->die->die_offset != 0
7823 || !flag_eliminate_unused_debug_types)
7824 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
7826 size += DWARF_OFFSET_SIZE;
7827 return size;
7830 /* Return the size of the information in the .debug_aranges section. */
7832 static unsigned long
7833 size_of_aranges (void)
7835 unsigned long size;
7837 size = DWARF_ARANGES_HEADER_SIZE;
7839 /* Count the address/length pair for this compilation unit. */
7840 if (text_section_used)
7841 size += 2 * DWARF2_ADDR_SIZE;
7842 if (cold_text_section_used)
7843 size += 2 * DWARF2_ADDR_SIZE;
7844 if (have_multiple_function_sections)
7846 unsigned fde_idx;
7847 dw_fde_ref fde;
7849 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
7851 if (DECL_IGNORED_P (fde->decl))
7852 continue;
7853 if (!fde->in_std_section)
7854 size += 2 * DWARF2_ADDR_SIZE;
7855 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
7856 size += 2 * DWARF2_ADDR_SIZE;
7860 /* Count the two zero words used to terminated the address range table. */
7861 size += 2 * DWARF2_ADDR_SIZE;
7862 return size;
7865 /* Select the encoding of an attribute value. */
7867 static enum dwarf_form
7868 value_format (dw_attr_ref a)
7870 switch (AT_class (a))
7872 case dw_val_class_addr:
7873 /* Only very few attributes allow DW_FORM_addr. */
7874 switch (a->dw_attr)
7876 case DW_AT_low_pc:
7877 case DW_AT_high_pc:
7878 case DW_AT_entry_pc:
7879 case DW_AT_trampoline:
7880 return (AT_index (a) == NOT_INDEXED
7881 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
7882 default:
7883 break;
7885 switch (DWARF2_ADDR_SIZE)
7887 case 1:
7888 return DW_FORM_data1;
7889 case 2:
7890 return DW_FORM_data2;
7891 case 4:
7892 return DW_FORM_data4;
7893 case 8:
7894 return DW_FORM_data8;
7895 default:
7896 gcc_unreachable ();
7898 case dw_val_class_range_list:
7899 case dw_val_class_loc_list:
7900 if (dwarf_version >= 4)
7901 return DW_FORM_sec_offset;
7902 /* FALLTHRU */
7903 case dw_val_class_vms_delta:
7904 case dw_val_class_offset:
7905 switch (DWARF_OFFSET_SIZE)
7907 case 4:
7908 return DW_FORM_data4;
7909 case 8:
7910 return DW_FORM_data8;
7911 default:
7912 gcc_unreachable ();
7914 case dw_val_class_loc:
7915 if (dwarf_version >= 4)
7916 return DW_FORM_exprloc;
7917 switch (constant_size (size_of_locs (AT_loc (a))))
7919 case 1:
7920 return DW_FORM_block1;
7921 case 2:
7922 return DW_FORM_block2;
7923 case 4:
7924 return DW_FORM_block4;
7925 default:
7926 gcc_unreachable ();
7928 case dw_val_class_const:
7929 return DW_FORM_sdata;
7930 case dw_val_class_unsigned_const:
7931 switch (constant_size (AT_unsigned (a)))
7933 case 1:
7934 return DW_FORM_data1;
7935 case 2:
7936 return DW_FORM_data2;
7937 case 4:
7938 /* In DWARF3 DW_AT_data_member_location with
7939 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
7940 constant, so we need to use DW_FORM_udata if we need
7941 a large constant. */
7942 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
7943 return DW_FORM_udata;
7944 return DW_FORM_data4;
7945 case 8:
7946 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
7947 return DW_FORM_udata;
7948 return DW_FORM_data8;
7949 default:
7950 gcc_unreachable ();
7952 case dw_val_class_const_double:
7953 switch (HOST_BITS_PER_WIDE_INT)
7955 case 8:
7956 return DW_FORM_data2;
7957 case 16:
7958 return DW_FORM_data4;
7959 case 32:
7960 return DW_FORM_data8;
7961 case 64:
7962 default:
7963 return DW_FORM_block1;
7965 case dw_val_class_vec:
7966 switch (constant_size (a->dw_attr_val.v.val_vec.length
7967 * a->dw_attr_val.v.val_vec.elt_size))
7969 case 1:
7970 return DW_FORM_block1;
7971 case 2:
7972 return DW_FORM_block2;
7973 case 4:
7974 return DW_FORM_block4;
7975 default:
7976 gcc_unreachable ();
7978 case dw_val_class_flag:
7979 if (dwarf_version >= 4)
7981 /* Currently all add_AT_flag calls pass in 1 as last argument,
7982 so DW_FORM_flag_present can be used. If that ever changes,
7983 we'll need to use DW_FORM_flag and have some optimization
7984 in build_abbrev_table that will change those to
7985 DW_FORM_flag_present if it is set to 1 in all DIEs using
7986 the same abbrev entry. */
7987 gcc_assert (a->dw_attr_val.v.val_flag == 1);
7988 return DW_FORM_flag_present;
7990 return DW_FORM_flag;
7991 case dw_val_class_die_ref:
7992 if (AT_ref_external (a))
7993 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
7994 else
7995 return DW_FORM_ref;
7996 case dw_val_class_fde_ref:
7997 return DW_FORM_data;
7998 case dw_val_class_lbl_id:
7999 return (AT_index (a) == NOT_INDEXED
8000 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8001 case dw_val_class_lineptr:
8002 case dw_val_class_macptr:
8003 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8004 case dw_val_class_str:
8005 return AT_string_form (a);
8006 case dw_val_class_file:
8007 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8009 case 1:
8010 return DW_FORM_data1;
8011 case 2:
8012 return DW_FORM_data2;
8013 case 4:
8014 return DW_FORM_data4;
8015 default:
8016 gcc_unreachable ();
8019 case dw_val_class_data8:
8020 return DW_FORM_data8;
8022 case dw_val_class_high_pc:
8023 switch (DWARF2_ADDR_SIZE)
8025 case 1:
8026 return DW_FORM_data1;
8027 case 2:
8028 return DW_FORM_data2;
8029 case 4:
8030 return DW_FORM_data4;
8031 case 8:
8032 return DW_FORM_data8;
8033 default:
8034 gcc_unreachable ();
8037 default:
8038 gcc_unreachable ();
8042 /* Output the encoding of an attribute value. */
8044 static void
8045 output_value_format (dw_attr_ref a)
8047 enum dwarf_form form = value_format (a);
8049 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8052 /* Given a die and id, produce the appropriate abbreviations. */
8054 static void
8055 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8057 unsigned ix;
8058 dw_attr_ref a_attr;
8060 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8061 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8062 dwarf_tag_name (abbrev->die_tag));
8064 if (abbrev->die_child != NULL)
8065 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8066 else
8067 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8069 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8071 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8072 dwarf_attr_name (a_attr->dw_attr));
8073 output_value_format (a_attr);
8076 dw2_asm_output_data (1, 0, NULL);
8077 dw2_asm_output_data (1, 0, NULL);
8081 /* Output the .debug_abbrev section which defines the DIE abbreviation
8082 table. */
8084 static void
8085 output_abbrev_section (void)
8087 unsigned long abbrev_id;
8089 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8090 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8092 /* Terminate the table. */
8093 dw2_asm_output_data (1, 0, NULL);
8096 /* Output a symbol we can use to refer to this DIE from another CU. */
8098 static inline void
8099 output_die_symbol (dw_die_ref die)
8101 const char *sym = die->die_id.die_symbol;
8103 gcc_assert (!die->comdat_type_p);
8105 if (sym == 0)
8106 return;
8108 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8109 /* We make these global, not weak; if the target doesn't support
8110 .linkonce, it doesn't support combining the sections, so debugging
8111 will break. */
8112 targetm.asm_out.globalize_label (asm_out_file, sym);
8114 ASM_OUTPUT_LABEL (asm_out_file, sym);
8117 /* Return a new location list, given the begin and end range, and the
8118 expression. */
8120 static inline dw_loc_list_ref
8121 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8122 const char *section)
8124 dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
8126 retlist->begin = begin;
8127 retlist->begin_entry = NULL;
8128 retlist->end = end;
8129 retlist->expr = expr;
8130 retlist->section = section;
8132 return retlist;
8135 /* Generate a new internal symbol for this location list node, if it
8136 hasn't got one yet. */
8138 static inline void
8139 gen_llsym (dw_loc_list_ref list)
8141 gcc_assert (!list->ll_symbol);
8142 list->ll_symbol = gen_internal_sym ("LLST");
8145 /* Output the location list given to us. */
8147 static void
8148 output_loc_list (dw_loc_list_ref list_head)
8150 dw_loc_list_ref curr = list_head;
8152 if (list_head->emitted)
8153 return;
8154 list_head->emitted = true;
8156 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8158 /* Walk the location list, and output each range + expression. */
8159 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8161 unsigned long size;
8162 /* Don't output an entry that starts and ends at the same address. */
8163 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8164 continue;
8165 size = size_of_locs (curr->expr);
8166 /* If the expression is too large, drop it on the floor. We could
8167 perhaps put it into DW_TAG_dwarf_procedure and refer to that
8168 in the expression, but >= 64KB expressions for a single value
8169 in a single range are unlikely very useful. */
8170 if (size > 0xffff)
8171 continue;
8172 if (dwarf_split_debug_info)
8174 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8175 "Location list start/length entry (%s)",
8176 list_head->ll_symbol);
8177 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8178 "Location list range start index (%s)",
8179 curr->begin);
8180 /* The length field is 4 bytes. If we ever need to support
8181 an 8-byte length, we can add a new DW_LLE code or fall back
8182 to DW_LLE_GNU_start_end_entry. */
8183 dw2_asm_output_delta (4, curr->end, curr->begin,
8184 "Location list range length (%s)",
8185 list_head->ll_symbol);
8187 else if (!have_multiple_function_sections)
8189 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8190 "Location list begin address (%s)",
8191 list_head->ll_symbol);
8192 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8193 "Location list end address (%s)",
8194 list_head->ll_symbol);
8196 else
8198 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8199 "Location list begin address (%s)",
8200 list_head->ll_symbol);
8201 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8202 "Location list end address (%s)",
8203 list_head->ll_symbol);
8206 /* Output the block length for this list of location operations. */
8207 gcc_assert (size <= 0xffff);
8208 dw2_asm_output_data (2, size, "%s", "Location expression size");
8210 output_loc_sequence (curr->expr, -1);
8213 if (dwarf_split_debug_info)
8214 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8215 "Location list terminator (%s)",
8216 list_head->ll_symbol);
8217 else
8219 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8220 "Location list terminator begin (%s)",
8221 list_head->ll_symbol);
8222 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8223 "Location list terminator end (%s)",
8224 list_head->ll_symbol);
8228 /* Output a range_list offset into the debug_range section. Emit a
8229 relocated reference if val_entry is NULL, otherwise, emit an
8230 indirect reference. */
8232 static void
8233 output_range_list_offset (dw_attr_ref a)
8235 const char *name = dwarf_attr_name (a->dw_attr);
8237 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8239 char *p = strchr (ranges_section_label, '\0');
8240 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8241 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8242 debug_ranges_section, "%s", name);
8243 *p = '\0';
8245 else
8246 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8247 "%s (offset from %s)", name, ranges_section_label);
8250 /* Output the offset into the debug_loc section. */
8252 static void
8253 output_loc_list_offset (dw_attr_ref a)
8255 char *sym = AT_loc_list (a)->ll_symbol;
8257 gcc_assert (sym);
8258 if (dwarf_split_debug_info)
8259 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8260 "%s", dwarf_attr_name (a->dw_attr));
8261 else
8262 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8263 "%s", dwarf_attr_name (a->dw_attr));
8266 /* Output an attribute's index or value appropriately. */
8268 static void
8269 output_attr_index_or_value (dw_attr_ref a)
8271 const char *name = dwarf_attr_name (a->dw_attr);
8273 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8275 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8276 return;
8278 switch (AT_class (a))
8280 case dw_val_class_addr:
8281 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8282 break;
8283 case dw_val_class_high_pc:
8284 case dw_val_class_lbl_id:
8285 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8286 break;
8287 case dw_val_class_loc_list:
8288 output_loc_list_offset (a);
8289 break;
8290 default:
8291 gcc_unreachable ();
8295 /* Output a type signature. */
8297 static inline void
8298 output_signature (const char *sig, const char *name)
8300 int i;
8302 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8303 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8306 /* Output the DIE and its attributes. Called recursively to generate
8307 the definitions of each child DIE. */
8309 static void
8310 output_die (dw_die_ref die)
8312 dw_attr_ref a;
8313 dw_die_ref c;
8314 unsigned long size;
8315 unsigned ix;
8317 /* If someone in another CU might refer to us, set up a symbol for
8318 them to point to. */
8319 if (! die->comdat_type_p && die->die_id.die_symbol)
8320 output_die_symbol (die);
8322 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8323 (unsigned long)die->die_offset,
8324 dwarf_tag_name (die->die_tag));
8326 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8328 const char *name = dwarf_attr_name (a->dw_attr);
8330 switch (AT_class (a))
8332 case dw_val_class_addr:
8333 output_attr_index_or_value (a);
8334 break;
8336 case dw_val_class_offset:
8337 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8338 "%s", name);
8339 break;
8341 case dw_val_class_range_list:
8342 output_range_list_offset (a);
8343 break;
8345 case dw_val_class_loc:
8346 size = size_of_locs (AT_loc (a));
8348 /* Output the block length for this list of location operations. */
8349 if (dwarf_version >= 4)
8350 dw2_asm_output_data_uleb128 (size, "%s", name);
8351 else
8352 dw2_asm_output_data (constant_size (size), size, "%s", name);
8354 output_loc_sequence (AT_loc (a), -1);
8355 break;
8357 case dw_val_class_const:
8358 /* ??? It would be slightly more efficient to use a scheme like is
8359 used for unsigned constants below, but gdb 4.x does not sign
8360 extend. Gdb 5.x does sign extend. */
8361 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8362 break;
8364 case dw_val_class_unsigned_const:
8366 int csize = constant_size (AT_unsigned (a));
8367 if (dwarf_version == 3
8368 && a->dw_attr == DW_AT_data_member_location
8369 && csize >= 4)
8370 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8371 else
8372 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8374 break;
8376 case dw_val_class_const_double:
8378 unsigned HOST_WIDE_INT first, second;
8380 if (HOST_BITS_PER_WIDE_INT >= 64)
8381 dw2_asm_output_data (1,
8382 HOST_BITS_PER_DOUBLE_INT
8383 / HOST_BITS_PER_CHAR,
8384 NULL);
8386 if (WORDS_BIG_ENDIAN)
8388 first = a->dw_attr_val.v.val_double.high;
8389 second = a->dw_attr_val.v.val_double.low;
8391 else
8393 first = a->dw_attr_val.v.val_double.low;
8394 second = a->dw_attr_val.v.val_double.high;
8397 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8398 first, "%s", name);
8399 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8400 second, NULL);
8402 break;
8404 case dw_val_class_vec:
8406 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8407 unsigned int len = a->dw_attr_val.v.val_vec.length;
8408 unsigned int i;
8409 unsigned char *p;
8411 dw2_asm_output_data (constant_size (len * elt_size),
8412 len * elt_size, "%s", name);
8413 if (elt_size > sizeof (HOST_WIDE_INT))
8415 elt_size /= 2;
8416 len *= 2;
8418 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8419 i < len;
8420 i++, p += elt_size)
8421 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8422 "fp or vector constant word %u", i);
8423 break;
8426 case dw_val_class_flag:
8427 if (dwarf_version >= 4)
8429 /* Currently all add_AT_flag calls pass in 1 as last argument,
8430 so DW_FORM_flag_present can be used. If that ever changes,
8431 we'll need to use DW_FORM_flag and have some optimization
8432 in build_abbrev_table that will change those to
8433 DW_FORM_flag_present if it is set to 1 in all DIEs using
8434 the same abbrev entry. */
8435 gcc_assert (AT_flag (a) == 1);
8436 if (flag_debug_asm)
8437 fprintf (asm_out_file, "\t\t\t%s %s\n",
8438 ASM_COMMENT_START, name);
8439 break;
8441 dw2_asm_output_data (1, AT_flag (a), "%s", name);
8442 break;
8444 case dw_val_class_loc_list:
8445 output_attr_index_or_value (a);
8446 break;
8448 case dw_val_class_die_ref:
8449 if (AT_ref_external (a))
8451 if (AT_ref (a)->comdat_type_p)
8453 comdat_type_node_ref type_node =
8454 AT_ref (a)->die_id.die_type_node;
8456 gcc_assert (type_node);
8457 output_signature (type_node->signature, name);
8459 else
8461 const char *sym = AT_ref (a)->die_id.die_symbol;
8462 int size;
8464 gcc_assert (sym);
8465 /* In DWARF2, DW_FORM_ref_addr is sized by target address
8466 length, whereas in DWARF3 it's always sized as an
8467 offset. */
8468 if (dwarf_version == 2)
8469 size = DWARF2_ADDR_SIZE;
8470 else
8471 size = DWARF_OFFSET_SIZE;
8472 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8473 name);
8476 else
8478 gcc_assert (AT_ref (a)->die_offset);
8479 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8480 "%s", name);
8482 break;
8484 case dw_val_class_fde_ref:
8486 char l1[20];
8488 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8489 a->dw_attr_val.v.val_fde_index * 2);
8490 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8491 "%s", name);
8493 break;
8495 case dw_val_class_vms_delta:
8496 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
8497 AT_vms_delta2 (a), AT_vms_delta1 (a),
8498 "%s", name);
8499 break;
8501 case dw_val_class_lbl_id:
8502 output_attr_index_or_value (a);
8503 break;
8505 case dw_val_class_lineptr:
8506 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8507 debug_line_section, "%s", name);
8508 break;
8510 case dw_val_class_macptr:
8511 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8512 debug_macinfo_section, "%s", name);
8513 break;
8515 case dw_val_class_str:
8516 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
8517 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8518 a->dw_attr_val.v.val_str->label,
8519 debug_str_section,
8520 "%s: \"%s\"", name, AT_string (a));
8521 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
8522 dw2_asm_output_data_uleb128 (AT_index (a),
8523 "%s: \"%s\"", name, AT_string (a));
8524 else
8525 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8526 break;
8528 case dw_val_class_file:
8530 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8532 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8533 a->dw_attr_val.v.val_file->filename);
8534 break;
8537 case dw_val_class_data8:
8539 int i;
8541 for (i = 0; i < 8; i++)
8542 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
8543 i == 0 ? "%s" : NULL, name);
8544 break;
8547 case dw_val_class_high_pc:
8548 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
8549 get_AT_low_pc (die), "DW_AT_high_pc");
8550 break;
8552 default:
8553 gcc_unreachable ();
8557 FOR_EACH_CHILD (die, c, output_die (c));
8559 /* Add null byte to terminate sibling list. */
8560 if (die->die_child != NULL)
8561 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
8562 (unsigned long) die->die_offset);
8565 /* Output the compilation unit that appears at the beginning of the
8566 .debug_info section, and precedes the DIE descriptions. */
8568 static void
8569 output_compilation_unit_header (void)
8571 int ver = dwarf_version;
8573 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8574 dw2_asm_output_data (4, 0xffffffff,
8575 "Initial length escape value indicating 64-bit DWARF extension");
8576 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8577 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8578 "Length of Compilation Unit Info");
8579 dw2_asm_output_data (2, ver, "DWARF version number");
8580 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8581 debug_abbrev_section,
8582 "Offset Into Abbrev. Section");
8583 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8586 /* Output the compilation unit DIE and its children. */
8588 static void
8589 output_comp_unit (dw_die_ref die, int output_if_empty)
8591 const char *secname, *oldsym;
8592 char *tmp;
8593 htab_t extern_map;
8595 /* Unless we are outputting main CU, we may throw away empty ones. */
8596 if (!output_if_empty && die->die_child == NULL)
8597 return;
8599 /* Even if there are no children of this DIE, we must output the information
8600 about the compilation unit. Otherwise, on an empty translation unit, we
8601 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
8602 will then complain when examining the file. First mark all the DIEs in
8603 this CU so we know which get local refs. */
8604 mark_dies (die);
8606 extern_map = optimize_external_refs (die);
8608 build_abbrev_table (die, extern_map);
8610 htab_delete (extern_map);
8612 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8613 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8614 calc_die_sizes (die);
8616 oldsym = die->die_id.die_symbol;
8617 if (oldsym)
8619 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8621 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8622 secname = tmp;
8623 die->die_id.die_symbol = NULL;
8624 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8626 else
8628 switch_to_section (debug_info_section);
8629 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
8630 info_section_emitted = true;
8633 /* Output debugging information. */
8634 output_compilation_unit_header ();
8635 output_die (die);
8637 /* Leave the marks on the main CU, so we can check them in
8638 output_pubnames. */
8639 if (oldsym)
8641 unmark_dies (die);
8642 die->die_id.die_symbol = oldsym;
8646 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
8647 and .debug_pubtypes. This is configured per-target, but can be
8648 overridden by the -gpubnames or -gno-pubnames options. */
8650 static inline bool
8651 want_pubnames (void)
8653 return (debug_generate_pub_sections != -1
8654 ? debug_generate_pub_sections
8655 : targetm.want_debug_pub_sections);
8658 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
8660 static void
8661 add_AT_pubnames (dw_die_ref die)
8663 if (want_pubnames ())
8664 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
8667 /* Helper function to generate top-level dies for skeleton debug_info and
8668 debug_types. */
8670 static void
8671 add_top_level_skeleton_die_attrs (dw_die_ref die)
8673 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
8674 dw_attr_ref attr;
8676 add_comp_dir_attribute (die);
8677 add_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
8678 /* The specification suggests that these attributes be inline to avoid
8679 having a .debug_str section. We know that they exist in the die because
8680 we just added them. */
8681 attr = get_AT (die, DW_AT_GNU_dwo_name);
8682 attr->dw_attr_val.v.val_str->form = DW_FORM_string;
8683 attr = get_AT (die, DW_AT_comp_dir);
8684 attr->dw_attr_val.v.val_str->form = DW_FORM_string;
8686 add_AT_pubnames (die);
8687 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
8690 /* Return the single type-unit die for skeleton type units. */
8692 static dw_die_ref
8693 get_skeleton_type_unit (void)
8695 /* For dwarf_split_debug_sections with use_type info, all type units in the
8696 skeleton sections have identical dies (but different headers). This
8697 single die will be output many times. */
8699 static dw_die_ref skeleton_type_unit = NULL;
8701 if (skeleton_type_unit == NULL)
8703 skeleton_type_unit = new_die (DW_TAG_type_unit, NULL, NULL);
8704 add_top_level_skeleton_die_attrs (skeleton_type_unit);
8705 skeleton_type_unit->die_abbrev = SKELETON_TYPE_DIE_ABBREV;
8707 return skeleton_type_unit;
8710 /* Output skeleton debug sections that point to the dwo file. */
8712 static void
8713 output_skeleton_debug_sections (dw_die_ref comp_unit)
8715 /* These attributes will be found in the full debug_info section. */
8716 remove_AT (comp_unit, DW_AT_producer);
8717 remove_AT (comp_unit, DW_AT_language);
8719 /* Add attributes common to skeleton compile_units and type_units. */
8720 add_top_level_skeleton_die_attrs (comp_unit);
8722 switch_to_section (debug_skeleton_info_section);
8723 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
8725 /* Produce the skeleton compilation-unit header. This one differs enough from
8726 a normal CU header that it's better not to call output_compilation_unit
8727 header. */
8728 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8729 dw2_asm_output_data (4, 0xffffffff,
8730 "Initial length escape value indicating 64-bit DWARF extension");
8732 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8733 DWARF_COMPILE_UNIT_HEADER_SIZE
8734 - DWARF_INITIAL_LENGTH_SIZE
8735 + size_of_die (comp_unit),
8736 "Length of Compilation Unit Info");
8737 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
8738 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
8739 debug_abbrev_section,
8740 "Offset Into Abbrev. Section");
8741 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8743 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
8744 output_die (comp_unit);
8746 /* Build the skeleton debug_abbrev section. */
8747 switch_to_section (debug_skeleton_abbrev_section);
8748 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
8750 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
8751 if (use_debug_types)
8752 output_die_abbrevs (SKELETON_TYPE_DIE_ABBREV, get_skeleton_type_unit ());
8754 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
8757 /* Output a comdat type unit DIE and its children. */
8759 static void
8760 output_comdat_type_unit (comdat_type_node *node)
8762 const char *secname;
8763 char *tmp;
8764 int i;
8765 #if defined (OBJECT_FORMAT_ELF)
8766 tree comdat_key;
8767 #endif
8768 htab_t extern_map;
8770 /* First mark all the DIEs in this CU so we know which get local refs. */
8771 mark_dies (node->root_die);
8773 extern_map = optimize_external_refs (node->root_die);
8775 build_abbrev_table (node->root_die, extern_map);
8777 htab_delete (extern_map);
8779 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8780 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
8781 calc_die_sizes (node->root_die);
8783 #if defined (OBJECT_FORMAT_ELF)
8784 if (!dwarf_split_debug_info)
8785 secname = ".debug_types";
8786 else
8787 secname = ".debug_types.dwo";
8789 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
8790 sprintf (tmp, "wt.");
8791 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8792 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
8793 comdat_key = get_identifier (tmp);
8794 targetm.asm_out.named_section (secname,
8795 SECTION_DEBUG | SECTION_LINKONCE,
8796 comdat_key);
8797 #else
8798 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
8799 sprintf (tmp, ".gnu.linkonce.wt.");
8800 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8801 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
8802 secname = tmp;
8803 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8804 #endif
8806 /* Output debugging information. */
8807 output_compilation_unit_header ();
8808 output_signature (node->signature, "Type Signature");
8809 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
8810 "Offset to Type DIE");
8811 output_die (node->root_die);
8813 unmark_dies (node->root_die);
8815 #if defined (OBJECT_FORMAT_ELF)
8816 if (dwarf_split_debug_info)
8818 /* Produce the skeleton type-unit header. */
8819 const char *secname = ".debug_types";
8821 targetm.asm_out.named_section (secname,
8822 SECTION_DEBUG | SECTION_LINKONCE,
8823 comdat_key);
8824 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8825 dw2_asm_output_data (4, 0xffffffff,
8826 "Initial length escape value indicating 64-bit DWARF extension");
8828 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8829 DWARF_COMPILE_UNIT_HEADER_SIZE
8830 - DWARF_INITIAL_LENGTH_SIZE
8831 + size_of_die (get_skeleton_type_unit ())
8832 + DWARF_TYPE_SIGNATURE_SIZE + DWARF_OFFSET_SIZE,
8833 "Length of Type Unit Info");
8834 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
8835 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8836 debug_skeleton_abbrev_section_label,
8837 debug_abbrev_section,
8838 "Offset Into Abbrev. Section");
8839 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8840 output_signature (node->signature, "Type Signature");
8841 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, "Offset to Type DIE");
8843 output_die (get_skeleton_type_unit ());
8845 #endif
8848 /* Return the DWARF2/3 pubname associated with a decl. */
8850 static const char *
8851 dwarf2_name (tree decl, int scope)
8853 if (DECL_NAMELESS (decl))
8854 return NULL;
8855 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
8858 /* Add a new entry to .debug_pubnames if appropriate. */
8860 static void
8861 add_pubname_string (const char *str, dw_die_ref die)
8863 pubname_entry e;
8865 e.die = die;
8866 e.name = xstrdup (str);
8867 vec_safe_push (pubname_table, e);
8870 static void
8871 add_pubname (tree decl, dw_die_ref die)
8873 if (!want_pubnames ())
8874 return;
8876 /* Don't add items to the table when we expect that the consumer will have
8877 just read the enclosing die. For example, if the consumer is looking at a
8878 class_member, it will either be inside the class already, or will have just
8879 looked up the class to find the member. Either way, searching the class is
8880 faster than searching the index. */
8881 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
8882 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
8884 const char *name = dwarf2_name (decl, 1);
8886 if (name)
8887 add_pubname_string (name, die);
8891 /* Add an enumerator to the pubnames section. */
8893 static void
8894 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
8896 pubname_entry e;
8898 gcc_assert (scope_name);
8899 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
8900 e.die = die;
8901 vec_safe_push (pubname_table, e);
8904 /* Add a new entry to .debug_pubtypes if appropriate. */
8906 static void
8907 add_pubtype (tree decl, dw_die_ref die)
8909 pubname_entry e;
8911 if (!want_pubnames ())
8912 return;
8914 if ((TREE_PUBLIC (decl)
8915 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
8916 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
8918 tree scope = NULL;
8919 const char *scope_name = "";
8920 const char *sep = is_cxx () ? "::" : ".";
8921 const char *name;
8923 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
8924 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
8926 scope_name = lang_hooks.dwarf_name (scope, 1);
8927 if (scope_name != NULL && scope_name[0] != '\0')
8928 scope_name = concat (scope_name, sep, NULL);
8929 else
8930 scope_name = "";
8933 if (TYPE_P (decl))
8934 name = type_tag (decl);
8935 else
8936 name = lang_hooks.dwarf_name (decl, 1);
8938 /* If we don't have a name for the type, there's no point in adding
8939 it to the table. */
8940 if (name != NULL && name[0] != '\0')
8942 e.die = die;
8943 e.name = concat (scope_name, name, NULL);
8944 vec_safe_push (pubtype_table, e);
8947 /* Although it might be more consistent to add the pubinfo for the
8948 enumerators as their dies are created, they should only be added if the
8949 enum type meets the criteria above. So rather than re-check the parent
8950 enum type whenever an enumerator die is created, just output them all
8951 here. This isn't protected by the name conditional because anonymous
8952 enums don't have names. */
8953 if (die->die_tag == DW_TAG_enumeration_type)
8955 dw_die_ref c;
8957 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
8962 /* Output the public names table used to speed up access to externally
8963 visible names; or the public types table used to find type definitions. */
8965 static void
8966 output_pubnames (vec<pubname_entry, va_gc> *names)
8968 unsigned i;
8969 unsigned long pubnames_length = size_of_pubnames (names);
8970 pubname_ref pub;
8972 if (!want_pubnames () || !info_section_emitted)
8973 return;
8974 if (names == pubname_table)
8975 switch_to_section (debug_pubnames_section);
8976 else
8977 switch_to_section (debug_pubtypes_section);
8978 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8979 dw2_asm_output_data (4, 0xffffffff,
8980 "Initial length escape value indicating 64-bit DWARF extension");
8981 if (names == pubname_table)
8982 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8983 "Length of Public Names Info");
8984 else
8985 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8986 "Length of Public Type Names Info");
8987 /* Version number for pubnames/pubtypes is still 2, even in DWARF3. */
8988 dw2_asm_output_data (2, 2, "DWARF Version");
8989 if (dwarf_split_debug_info)
8990 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
8991 debug_skeleton_info_section,
8992 "Offset of Compilation Unit Info");
8993 else
8994 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8995 debug_info_section,
8996 "Offset of Compilation Unit Info");
8997 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
8998 "Compilation Unit Length");
9000 FOR_EACH_VEC_ELT (*names, i, pub)
9002 /* Enumerator names are part of the pubname table, but the parent
9003 DW_TAG_enumeration_type die may have been pruned. Don't output
9004 them if that is the case. */
9005 if (pub->die->die_tag == DW_TAG_enumerator && !pub->die->die_mark)
9006 continue;
9008 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9009 if (names == pubname_table)
9010 gcc_assert (pub->die->die_mark);
9012 if (names != pubtype_table
9013 || pub->die->die_offset != 0
9014 || !flag_eliminate_unused_debug_types)
9016 dw_offset die_offset = pub->die->die_offset;
9018 /* If we're putting types in their own .debug_types sections,
9019 the .debug_pubtypes table will still point to the compile
9020 unit (not the type unit), so we want to use the offset of
9021 the skeleton DIE (if there is one). */
9022 if (pub->die->comdat_type_p && names == pubtype_table)
9024 comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
9026 if (type_node != NULL)
9027 die_offset = (type_node->skeleton_die != NULL
9028 ? type_node->skeleton_die->die_offset
9029 : 0);
9032 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9034 dw2_asm_output_nstring (pub->name, -1, "external name");
9038 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9041 /* Output the information that goes into the .debug_aranges table.
9042 Namely, define the beginning and ending address range of the
9043 text section generated for this compilation unit. */
9045 static void
9046 output_aranges (unsigned long aranges_length)
9048 unsigned i;
9050 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9051 dw2_asm_output_data (4, 0xffffffff,
9052 "Initial length escape value indicating 64-bit DWARF extension");
9053 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9054 "Length of Address Ranges Info");
9055 /* Version number for aranges is still 2, even in DWARF3. */
9056 dw2_asm_output_data (2, 2, "DWARF Version");
9057 if (dwarf_split_debug_info)
9058 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9059 debug_skeleton_info_section,
9060 "Offset of Compilation Unit Info");
9061 else
9062 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9063 debug_info_section,
9064 "Offset of Compilation Unit Info");
9065 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9066 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9068 /* We need to align to twice the pointer size here. */
9069 if (DWARF_ARANGES_PAD_SIZE)
9071 /* Pad using a 2 byte words so that padding is correct for any
9072 pointer size. */
9073 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9074 2 * DWARF2_ADDR_SIZE);
9075 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9076 dw2_asm_output_data (2, 0, NULL);
9079 /* It is necessary not to output these entries if the sections were
9080 not used; if the sections were not used, the length will be 0 and
9081 the address may end up as 0 if the section is discarded by ld
9082 --gc-sections, leaving an invalid (0, 0) entry that can be
9083 confused with the terminator. */
9084 if (text_section_used)
9086 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9087 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9088 text_section_label, "Length");
9090 if (cold_text_section_used)
9092 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9093 "Address");
9094 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9095 cold_text_section_label, "Length");
9098 if (have_multiple_function_sections)
9100 unsigned fde_idx;
9101 dw_fde_ref fde;
9103 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9105 if (DECL_IGNORED_P (fde->decl))
9106 continue;
9107 if (!fde->in_std_section)
9109 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9110 "Address");
9111 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9112 fde->dw_fde_begin, "Length");
9114 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9116 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9117 "Address");
9118 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9119 fde->dw_fde_second_begin, "Length");
9124 /* Output the terminator words. */
9125 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9126 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9129 /* Add a new entry to .debug_ranges. Return the offset at which it
9130 was placed. */
9132 static unsigned int
9133 add_ranges_num (int num)
9135 unsigned int in_use = ranges_table_in_use;
9137 if (in_use == ranges_table_allocated)
9139 ranges_table_allocated += RANGES_TABLE_INCREMENT;
9140 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9141 ranges_table_allocated);
9142 memset (ranges_table + ranges_table_in_use, 0,
9143 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9146 ranges_table[in_use].num = num;
9147 ranges_table_in_use = in_use + 1;
9149 return in_use * 2 * DWARF2_ADDR_SIZE;
9152 /* Add a new entry to .debug_ranges corresponding to a block, or a
9153 range terminator if BLOCK is NULL. */
9155 static unsigned int
9156 add_ranges (const_tree block)
9158 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9161 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9162 When using dwarf_split_debug_info, address attributes in dies destined
9163 for the final executable should be direct references--setting the
9164 parameter force_direct ensures this behavior. */
9166 static void
9167 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9168 bool *added, bool force_direct)
9170 unsigned int in_use = ranges_by_label_in_use;
9171 unsigned int offset;
9173 if (in_use == ranges_by_label_allocated)
9175 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9176 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9177 ranges_by_label,
9178 ranges_by_label_allocated);
9179 memset (ranges_by_label + ranges_by_label_in_use, 0,
9180 RANGES_TABLE_INCREMENT
9181 * sizeof (struct dw_ranges_by_label_struct));
9184 ranges_by_label[in_use].begin = begin;
9185 ranges_by_label[in_use].end = end;
9186 ranges_by_label_in_use = in_use + 1;
9188 offset = add_ranges_num (-(int)in_use - 1);
9189 if (!*added)
9191 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9192 *added = true;
9196 static void
9197 output_ranges (void)
9199 unsigned i;
9200 static const char *const start_fmt = "Offset %#x";
9201 const char *fmt = start_fmt;
9203 for (i = 0; i < ranges_table_in_use; i++)
9205 int block_num = ranges_table[i].num;
9207 if (block_num > 0)
9209 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9210 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9212 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9213 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9215 /* If all code is in the text section, then the compilation
9216 unit base address defaults to DW_AT_low_pc, which is the
9217 base of the text section. */
9218 if (!have_multiple_function_sections)
9220 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9221 text_section_label,
9222 fmt, i * 2 * DWARF2_ADDR_SIZE);
9223 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9224 text_section_label, NULL);
9227 /* Otherwise, the compilation unit base address is zero,
9228 which allows us to use absolute addresses, and not worry
9229 about whether the target supports cross-section
9230 arithmetic. */
9231 else
9233 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9234 fmt, i * 2 * DWARF2_ADDR_SIZE);
9235 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9238 fmt = NULL;
9241 /* Negative block_num stands for an index into ranges_by_label. */
9242 else if (block_num < 0)
9244 int lab_idx = - block_num - 1;
9246 if (!have_multiple_function_sections)
9248 gcc_unreachable ();
9249 #if 0
9250 /* If we ever use add_ranges_by_labels () for a single
9251 function section, all we have to do is to take out
9252 the #if 0 above. */
9253 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9254 ranges_by_label[lab_idx].begin,
9255 text_section_label,
9256 fmt, i * 2 * DWARF2_ADDR_SIZE);
9257 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9258 ranges_by_label[lab_idx].end,
9259 text_section_label, NULL);
9260 #endif
9262 else
9264 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9265 ranges_by_label[lab_idx].begin,
9266 fmt, i * 2 * DWARF2_ADDR_SIZE);
9267 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9268 ranges_by_label[lab_idx].end,
9269 NULL);
9272 else
9274 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9275 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9276 fmt = start_fmt;
9281 /* Data structure containing information about input files. */
9282 struct file_info
9284 const char *path; /* Complete file name. */
9285 const char *fname; /* File name part. */
9286 int length; /* Length of entire string. */
9287 struct dwarf_file_data * file_idx; /* Index in input file table. */
9288 int dir_idx; /* Index in directory table. */
9291 /* Data structure containing information about directories with source
9292 files. */
9293 struct dir_info
9295 const char *path; /* Path including directory name. */
9296 int length; /* Path length. */
9297 int prefix; /* Index of directory entry which is a prefix. */
9298 int count; /* Number of files in this directory. */
9299 int dir_idx; /* Index of directory used as base. */
9302 /* Callback function for file_info comparison. We sort by looking at
9303 the directories in the path. */
9305 static int
9306 file_info_cmp (const void *p1, const void *p2)
9308 const struct file_info *const s1 = (const struct file_info *) p1;
9309 const struct file_info *const s2 = (const struct file_info *) p2;
9310 const unsigned char *cp1;
9311 const unsigned char *cp2;
9313 /* Take care of file names without directories. We need to make sure that
9314 we return consistent values to qsort since some will get confused if
9315 we return the same value when identical operands are passed in opposite
9316 orders. So if neither has a directory, return 0 and otherwise return
9317 1 or -1 depending on which one has the directory. */
9318 if ((s1->path == s1->fname || s2->path == s2->fname))
9319 return (s2->path == s2->fname) - (s1->path == s1->fname);
9321 cp1 = (const unsigned char *) s1->path;
9322 cp2 = (const unsigned char *) s2->path;
9324 while (1)
9326 ++cp1;
9327 ++cp2;
9328 /* Reached the end of the first path? If so, handle like above. */
9329 if ((cp1 == (const unsigned char *) s1->fname)
9330 || (cp2 == (const unsigned char *) s2->fname))
9331 return ((cp2 == (const unsigned char *) s2->fname)
9332 - (cp1 == (const unsigned char *) s1->fname));
9334 /* Character of current path component the same? */
9335 else if (*cp1 != *cp2)
9336 return *cp1 - *cp2;
9340 struct file_name_acquire_data
9342 struct file_info *files;
9343 int used_files;
9344 int max_files;
9347 /* Traversal function for the hash table. */
9349 static int
9350 file_name_acquire (void ** slot, void *data)
9352 struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
9353 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
9354 struct file_info *fi;
9355 const char *f;
9357 gcc_assert (fnad->max_files >= d->emitted_number);
9359 if (! d->emitted_number)
9360 return 1;
9362 gcc_assert (fnad->max_files != fnad->used_files);
9364 fi = fnad->files + fnad->used_files++;
9366 /* Skip all leading "./". */
9367 f = d->filename;
9368 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9369 f += 2;
9371 /* Create a new array entry. */
9372 fi->path = f;
9373 fi->length = strlen (f);
9374 fi->file_idx = d;
9376 /* Search for the file name part. */
9377 f = strrchr (f, DIR_SEPARATOR);
9378 #if defined (DIR_SEPARATOR_2)
9380 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9382 if (g != NULL)
9384 if (f == NULL || f < g)
9385 f = g;
9388 #endif
9390 fi->fname = f == NULL ? fi->path : f + 1;
9391 return 1;
9394 /* Output the directory table and the file name table. We try to minimize
9395 the total amount of memory needed. A heuristic is used to avoid large
9396 slowdowns with many input files. */
9398 static void
9399 output_file_names (void)
9401 struct file_name_acquire_data fnad;
9402 int numfiles;
9403 struct file_info *files;
9404 struct dir_info *dirs;
9405 int *saved;
9406 int *savehere;
9407 int *backmap;
9408 int ndirs;
9409 int idx_offset;
9410 int i;
9412 if (!last_emitted_file)
9414 dw2_asm_output_data (1, 0, "End directory table");
9415 dw2_asm_output_data (1, 0, "End file name table");
9416 return;
9419 numfiles = last_emitted_file->emitted_number;
9421 /* Allocate the various arrays we need. */
9422 files = XALLOCAVEC (struct file_info, numfiles);
9423 dirs = XALLOCAVEC (struct dir_info, numfiles);
9425 fnad.files = files;
9426 fnad.used_files = 0;
9427 fnad.max_files = numfiles;
9428 htab_traverse (file_table, file_name_acquire, &fnad);
9429 gcc_assert (fnad.used_files == fnad.max_files);
9431 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9433 /* Find all the different directories used. */
9434 dirs[0].path = files[0].path;
9435 dirs[0].length = files[0].fname - files[0].path;
9436 dirs[0].prefix = -1;
9437 dirs[0].count = 1;
9438 dirs[0].dir_idx = 0;
9439 files[0].dir_idx = 0;
9440 ndirs = 1;
9442 for (i = 1; i < numfiles; i++)
9443 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9444 && memcmp (dirs[ndirs - 1].path, files[i].path,
9445 dirs[ndirs - 1].length) == 0)
9447 /* Same directory as last entry. */
9448 files[i].dir_idx = ndirs - 1;
9449 ++dirs[ndirs - 1].count;
9451 else
9453 int j;
9455 /* This is a new directory. */
9456 dirs[ndirs].path = files[i].path;
9457 dirs[ndirs].length = files[i].fname - files[i].path;
9458 dirs[ndirs].count = 1;
9459 dirs[ndirs].dir_idx = ndirs;
9460 files[i].dir_idx = ndirs;
9462 /* Search for a prefix. */
9463 dirs[ndirs].prefix = -1;
9464 for (j = 0; j < ndirs; j++)
9465 if (dirs[j].length < dirs[ndirs].length
9466 && dirs[j].length > 1
9467 && (dirs[ndirs].prefix == -1
9468 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9469 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9470 dirs[ndirs].prefix = j;
9472 ++ndirs;
9475 /* Now to the actual work. We have to find a subset of the directories which
9476 allow expressing the file name using references to the directory table
9477 with the least amount of characters. We do not do an exhaustive search
9478 where we would have to check out every combination of every single
9479 possible prefix. Instead we use a heuristic which provides nearly optimal
9480 results in most cases and never is much off. */
9481 saved = XALLOCAVEC (int, ndirs);
9482 savehere = XALLOCAVEC (int, ndirs);
9484 memset (saved, '\0', ndirs * sizeof (saved[0]));
9485 for (i = 0; i < ndirs; i++)
9487 int j;
9488 int total;
9490 /* We can always save some space for the current directory. But this
9491 does not mean it will be enough to justify adding the directory. */
9492 savehere[i] = dirs[i].length;
9493 total = (savehere[i] - saved[i]) * dirs[i].count;
9495 for (j = i + 1; j < ndirs; j++)
9497 savehere[j] = 0;
9498 if (saved[j] < dirs[i].length)
9500 /* Determine whether the dirs[i] path is a prefix of the
9501 dirs[j] path. */
9502 int k;
9504 k = dirs[j].prefix;
9505 while (k != -1 && k != (int) i)
9506 k = dirs[k].prefix;
9508 if (k == (int) i)
9510 /* Yes it is. We can possibly save some memory by
9511 writing the filenames in dirs[j] relative to
9512 dirs[i]. */
9513 savehere[j] = dirs[i].length;
9514 total += (savehere[j] - saved[j]) * dirs[j].count;
9519 /* Check whether we can save enough to justify adding the dirs[i]
9520 directory. */
9521 if (total > dirs[i].length + 1)
9523 /* It's worthwhile adding. */
9524 for (j = i; j < ndirs; j++)
9525 if (savehere[j] > 0)
9527 /* Remember how much we saved for this directory so far. */
9528 saved[j] = savehere[j];
9530 /* Remember the prefix directory. */
9531 dirs[j].dir_idx = i;
9536 /* Emit the directory name table. */
9537 idx_offset = dirs[0].length > 0 ? 1 : 0;
9538 for (i = 1 - idx_offset; i < ndirs; i++)
9539 dw2_asm_output_nstring (dirs[i].path,
9540 dirs[i].length
9541 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9542 "Directory Entry: %#x", i + idx_offset);
9544 dw2_asm_output_data (1, 0, "End directory table");
9546 /* We have to emit them in the order of emitted_number since that's
9547 used in the debug info generation. To do this efficiently we
9548 generate a back-mapping of the indices first. */
9549 backmap = XALLOCAVEC (int, numfiles);
9550 for (i = 0; i < numfiles; i++)
9551 backmap[files[i].file_idx->emitted_number - 1] = i;
9553 /* Now write all the file names. */
9554 for (i = 0; i < numfiles; i++)
9556 int file_idx = backmap[i];
9557 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9559 #ifdef VMS_DEBUGGING_INFO
9560 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
9562 /* Setting these fields can lead to debugger miscomparisons,
9563 but VMS Debug requires them to be set correctly. */
9565 int ver;
9566 long long cdt;
9567 long siz;
9568 int maxfilelen = strlen (files[file_idx].path)
9569 + dirs[dir_idx].length
9570 + MAX_VMS_VERSION_LEN + 1;
9571 char *filebuf = XALLOCAVEC (char, maxfilelen);
9573 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
9574 snprintf (filebuf, maxfilelen, "%s;%d",
9575 files[file_idx].path + dirs[dir_idx].length, ver);
9577 dw2_asm_output_nstring
9578 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
9580 /* Include directory index. */
9581 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9583 /* Modification time. */
9584 dw2_asm_output_data_uleb128
9585 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
9586 ? cdt : 0,
9587 NULL);
9589 /* File length in bytes. */
9590 dw2_asm_output_data_uleb128
9591 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
9592 ? siz : 0,
9593 NULL);
9594 #else
9595 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
9596 "File Entry: %#x", (unsigned) i + 1);
9598 /* Include directory index. */
9599 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9601 /* Modification time. */
9602 dw2_asm_output_data_uleb128 (0, NULL);
9604 /* File length in bytes. */
9605 dw2_asm_output_data_uleb128 (0, NULL);
9606 #endif /* VMS_DEBUGGING_INFO */
9609 dw2_asm_output_data (1, 0, "End file name table");
9613 /* Output one line number table into the .debug_line section. */
9615 static void
9616 output_one_line_info_table (dw_line_info_table *table)
9618 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9619 unsigned int current_line = 1;
9620 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
9621 dw_line_info_entry *ent;
9622 size_t i;
9624 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
9626 switch (ent->opcode)
9628 case LI_set_address:
9629 /* ??? Unfortunately, we have little choice here currently, and
9630 must always use the most general form. GCC does not know the
9631 address delta itself, so we can't use DW_LNS_advance_pc. Many
9632 ports do have length attributes which will give an upper bound
9633 on the address range. We could perhaps use length attributes
9634 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
9635 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
9637 /* This can handle any delta. This takes
9638 4+DWARF2_ADDR_SIZE bytes. */
9639 dw2_asm_output_data (1, 0, "set address %s", line_label);
9640 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9641 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9642 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9643 break;
9645 case LI_set_line:
9646 if (ent->val == current_line)
9648 /* We still need to start a new row, so output a copy insn. */
9649 dw2_asm_output_data (1, DW_LNS_copy,
9650 "copy line %u", current_line);
9652 else
9654 int line_offset = ent->val - current_line;
9655 int line_delta = line_offset - DWARF_LINE_BASE;
9657 current_line = ent->val;
9658 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9660 /* This can handle deltas from -10 to 234, using the current
9661 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
9662 This takes 1 byte. */
9663 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9664 "line %u", current_line);
9666 else
9668 /* This can handle any delta. This takes at least 4 bytes,
9669 depending on the value being encoded. */
9670 dw2_asm_output_data (1, DW_LNS_advance_line,
9671 "advance to line %u", current_line);
9672 dw2_asm_output_data_sleb128 (line_offset, NULL);
9673 dw2_asm_output_data (1, DW_LNS_copy, NULL);
9676 break;
9678 case LI_set_file:
9679 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
9680 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9681 break;
9683 case LI_set_column:
9684 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
9685 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9686 break;
9688 case LI_negate_stmt:
9689 current_is_stmt = !current_is_stmt;
9690 dw2_asm_output_data (1, DW_LNS_negate_stmt,
9691 "is_stmt %d", current_is_stmt);
9692 break;
9694 case LI_set_prologue_end:
9695 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
9696 "set prologue end");
9697 break;
9699 case LI_set_epilogue_begin:
9700 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
9701 "set epilogue begin");
9702 break;
9704 case LI_set_discriminator:
9705 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
9706 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
9707 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
9708 dw2_asm_output_data_uleb128 (ent->val, NULL);
9709 break;
9713 /* Emit debug info for the address of the end of the table. */
9714 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
9715 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9716 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9717 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
9719 dw2_asm_output_data (1, 0, "end sequence");
9720 dw2_asm_output_data_uleb128 (1, NULL);
9721 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9724 /* Output the source line number correspondence information. This
9725 information goes into the .debug_line section. */
9727 static void
9728 output_line_info (bool prologue_only)
9730 char l1[20], l2[20], p1[20], p2[20];
9731 int ver = dwarf_version;
9732 bool saw_one = false;
9733 int opc;
9735 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
9736 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
9737 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
9738 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
9740 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9741 dw2_asm_output_data (4, 0xffffffff,
9742 "Initial length escape value indicating 64-bit DWARF extension");
9743 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
9744 "Length of Source Line Info");
9745 ASM_OUTPUT_LABEL (asm_out_file, l1);
9747 dw2_asm_output_data (2, ver, "DWARF Version");
9748 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
9749 ASM_OUTPUT_LABEL (asm_out_file, p1);
9751 /* Define the architecture-dependent minimum instruction length (in bytes).
9752 In this implementation of DWARF, this field is used for information
9753 purposes only. Since GCC generates assembly language, we have no
9754 a priori knowledge of how many instruction bytes are generated for each
9755 source line, and therefore can use only the DW_LNE_set_address and
9756 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
9757 this as '1', which is "correct enough" for all architectures,
9758 and don't let the target override. */
9759 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
9761 if (ver >= 4)
9762 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
9763 "Maximum Operations Per Instruction");
9764 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
9765 "Default is_stmt_start flag");
9766 dw2_asm_output_data (1, DWARF_LINE_BASE,
9767 "Line Base Value (Special Opcodes)");
9768 dw2_asm_output_data (1, DWARF_LINE_RANGE,
9769 "Line Range Value (Special Opcodes)");
9770 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
9771 "Special Opcode Base");
9773 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
9775 int n_op_args;
9776 switch (opc)
9778 case DW_LNS_advance_pc:
9779 case DW_LNS_advance_line:
9780 case DW_LNS_set_file:
9781 case DW_LNS_set_column:
9782 case DW_LNS_fixed_advance_pc:
9783 case DW_LNS_set_isa:
9784 n_op_args = 1;
9785 break;
9786 default:
9787 n_op_args = 0;
9788 break;
9791 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
9792 opc, n_op_args);
9795 /* Write out the information about the files we use. */
9796 output_file_names ();
9797 ASM_OUTPUT_LABEL (asm_out_file, p2);
9798 if (prologue_only)
9800 /* Output the marker for the end of the line number info. */
9801 ASM_OUTPUT_LABEL (asm_out_file, l2);
9802 return;
9805 if (separate_line_info)
9807 dw_line_info_table *table;
9808 size_t i;
9810 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
9811 if (table->in_use)
9813 output_one_line_info_table (table);
9814 saw_one = true;
9817 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
9819 output_one_line_info_table (cold_text_section_line_info);
9820 saw_one = true;
9823 /* ??? Some Darwin linkers crash on a .debug_line section with no
9824 sequences. Further, merely a DW_LNE_end_sequence entry is not
9825 sufficient -- the address column must also be initialized.
9826 Make sure to output at least one set_address/end_sequence pair,
9827 choosing .text since that section is always present. */
9828 if (text_section_line_info->in_use || !saw_one)
9829 output_one_line_info_table (text_section_line_info);
9831 /* Output the marker for the end of the line number info. */
9832 ASM_OUTPUT_LABEL (asm_out_file, l2);
9835 /* Given a pointer to a tree node for some base type, return a pointer to
9836 a DIE that describes the given type.
9838 This routine must only be called for GCC type nodes that correspond to
9839 Dwarf base (fundamental) types. */
9841 static dw_die_ref
9842 base_type_die (tree type)
9844 dw_die_ref base_type_result;
9845 enum dwarf_type encoding;
9847 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
9848 return 0;
9850 /* If this is a subtype that should not be emitted as a subrange type,
9851 use the base type. See subrange_type_for_debug_p. */
9852 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
9853 type = TREE_TYPE (type);
9855 switch (TREE_CODE (type))
9857 case INTEGER_TYPE:
9858 if ((dwarf_version >= 4 || !dwarf_strict)
9859 && TYPE_NAME (type)
9860 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
9861 && DECL_IS_BUILTIN (TYPE_NAME (type))
9862 && DECL_NAME (TYPE_NAME (type)))
9864 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
9865 if (strcmp (name, "char16_t") == 0
9866 || strcmp (name, "char32_t") == 0)
9868 encoding = DW_ATE_UTF;
9869 break;
9872 if (TYPE_STRING_FLAG (type))
9874 if (TYPE_UNSIGNED (type))
9875 encoding = DW_ATE_unsigned_char;
9876 else
9877 encoding = DW_ATE_signed_char;
9879 else if (TYPE_UNSIGNED (type))
9880 encoding = DW_ATE_unsigned;
9881 else
9882 encoding = DW_ATE_signed;
9883 break;
9885 case REAL_TYPE:
9886 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
9888 if (dwarf_version >= 3 || !dwarf_strict)
9889 encoding = DW_ATE_decimal_float;
9890 else
9891 encoding = DW_ATE_lo_user;
9893 else
9894 encoding = DW_ATE_float;
9895 break;
9897 case FIXED_POINT_TYPE:
9898 if (!(dwarf_version >= 3 || !dwarf_strict))
9899 encoding = DW_ATE_lo_user;
9900 else if (TYPE_UNSIGNED (type))
9901 encoding = DW_ATE_unsigned_fixed;
9902 else
9903 encoding = DW_ATE_signed_fixed;
9904 break;
9906 /* Dwarf2 doesn't know anything about complex ints, so use
9907 a user defined type for it. */
9908 case COMPLEX_TYPE:
9909 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
9910 encoding = DW_ATE_complex_float;
9911 else
9912 encoding = DW_ATE_lo_user;
9913 break;
9915 case BOOLEAN_TYPE:
9916 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
9917 encoding = DW_ATE_boolean;
9918 break;
9920 default:
9921 /* No other TREE_CODEs are Dwarf fundamental types. */
9922 gcc_unreachable ();
9925 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
9927 add_AT_unsigned (base_type_result, DW_AT_byte_size,
9928 int_size_in_bytes (type));
9929 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
9930 add_pubtype (type, base_type_result);
9932 return base_type_result;
9935 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
9936 given input type is a Dwarf "fundamental" type. Otherwise return null. */
9938 static inline int
9939 is_base_type (tree type)
9941 switch (TREE_CODE (type))
9943 case ERROR_MARK:
9944 case VOID_TYPE:
9945 case INTEGER_TYPE:
9946 case REAL_TYPE:
9947 case FIXED_POINT_TYPE:
9948 case COMPLEX_TYPE:
9949 case BOOLEAN_TYPE:
9950 return 1;
9952 case ARRAY_TYPE:
9953 case RECORD_TYPE:
9954 case UNION_TYPE:
9955 case QUAL_UNION_TYPE:
9956 case ENUMERAL_TYPE:
9957 case FUNCTION_TYPE:
9958 case METHOD_TYPE:
9959 case POINTER_TYPE:
9960 case REFERENCE_TYPE:
9961 case NULLPTR_TYPE:
9962 case OFFSET_TYPE:
9963 case LANG_TYPE:
9964 case VECTOR_TYPE:
9965 return 0;
9967 default:
9968 gcc_unreachable ();
9971 return 0;
9974 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
9975 node, return the size in bits for the type if it is a constant, or else
9976 return the alignment for the type if the type's size is not constant, or
9977 else return BITS_PER_WORD if the type actually turns out to be an
9978 ERROR_MARK node. */
9980 static inline unsigned HOST_WIDE_INT
9981 simple_type_size_in_bits (const_tree type)
9983 if (TREE_CODE (type) == ERROR_MARK)
9984 return BITS_PER_WORD;
9985 else if (TYPE_SIZE (type) == NULL_TREE)
9986 return 0;
9987 else if (host_integerp (TYPE_SIZE (type), 1))
9988 return tree_low_cst (TYPE_SIZE (type), 1);
9989 else
9990 return TYPE_ALIGN (type);
9993 /* Similarly, but return a double_int instead of UHWI. */
9995 static inline double_int
9996 double_int_type_size_in_bits (const_tree type)
9998 if (TREE_CODE (type) == ERROR_MARK)
9999 return double_int::from_uhwi (BITS_PER_WORD);
10000 else if (TYPE_SIZE (type) == NULL_TREE)
10001 return double_int_zero;
10002 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10003 return tree_to_double_int (TYPE_SIZE (type));
10004 else
10005 return double_int::from_uhwi (TYPE_ALIGN (type));
10008 /* Given a pointer to a tree node for a subrange type, return a pointer
10009 to a DIE that describes the given type. */
10011 static dw_die_ref
10012 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10014 dw_die_ref subrange_die;
10015 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10017 if (context_die == NULL)
10018 context_die = comp_unit_die ();
10020 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10022 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10024 /* The size of the subrange type and its base type do not match,
10025 so we need to generate a size attribute for the subrange type. */
10026 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10029 if (low)
10030 add_bound_info (subrange_die, DW_AT_lower_bound, low);
10031 if (high)
10032 add_bound_info (subrange_die, DW_AT_upper_bound, high);
10034 return subrange_die;
10037 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10038 entry that chains various modifiers in front of the given type. */
10040 static dw_die_ref
10041 modified_type_die (tree type, int is_const_type, int is_volatile_type,
10042 dw_die_ref context_die)
10044 enum tree_code code = TREE_CODE (type);
10045 dw_die_ref mod_type_die;
10046 dw_die_ref sub_die = NULL;
10047 tree item_type = NULL;
10048 tree qualified_type;
10049 tree name, low, high;
10050 dw_die_ref mod_scope;
10052 if (code == ERROR_MARK)
10053 return NULL;
10055 /* See if we already have the appropriately qualified variant of
10056 this type. */
10057 qualified_type
10058 = get_qualified_type (type,
10059 ((is_const_type ? TYPE_QUAL_CONST : 0)
10060 | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
10062 if (qualified_type == sizetype
10063 && TYPE_NAME (qualified_type)
10064 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10066 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10068 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10069 && TYPE_PRECISION (t)
10070 == TYPE_PRECISION (qualified_type)
10071 && TYPE_UNSIGNED (t)
10072 == TYPE_UNSIGNED (qualified_type));
10073 qualified_type = t;
10076 /* If we do, then we can just use its DIE, if it exists. */
10077 if (qualified_type)
10079 mod_type_die = lookup_type_die (qualified_type);
10080 if (mod_type_die)
10081 return mod_type_die;
10084 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10086 /* Handle C typedef types. */
10087 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10088 && !DECL_ARTIFICIAL (name))
10090 tree dtype = TREE_TYPE (name);
10092 if (qualified_type == dtype)
10094 /* For a named type, use the typedef. */
10095 gen_type_die (qualified_type, context_die);
10096 return lookup_type_die (qualified_type);
10098 else if (is_const_type < TYPE_READONLY (dtype)
10099 || is_volatile_type < TYPE_VOLATILE (dtype)
10100 || (is_const_type <= TYPE_READONLY (dtype)
10101 && is_volatile_type <= TYPE_VOLATILE (dtype)
10102 && DECL_ORIGINAL_TYPE (name) != type))
10103 /* cv-unqualified version of named type. Just use the unnamed
10104 type to which it refers. */
10105 return modified_type_die (DECL_ORIGINAL_TYPE (name),
10106 is_const_type, is_volatile_type,
10107 context_die);
10108 /* Else cv-qualified version of named type; fall through. */
10111 mod_scope = scope_die_for (type, context_die);
10113 if (is_const_type
10114 /* If both is_const_type and is_volatile_type, prefer the path
10115 which leads to a qualified type. */
10116 && (!is_volatile_type
10117 || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
10118 || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
10120 mod_type_die = new_die (DW_TAG_const_type, mod_scope, type);
10121 sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
10123 else if (is_volatile_type)
10125 mod_type_die = new_die (DW_TAG_volatile_type, mod_scope, type);
10126 sub_die = modified_type_die (type, is_const_type, 0, context_die);
10128 else if (code == POINTER_TYPE)
10130 mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10131 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10132 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10133 item_type = TREE_TYPE (type);
10134 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10135 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10136 TYPE_ADDR_SPACE (item_type));
10138 else if (code == REFERENCE_TYPE)
10140 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10141 mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10142 type);
10143 else
10144 mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10145 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10146 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10147 item_type = TREE_TYPE (type);
10148 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10149 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10150 TYPE_ADDR_SPACE (item_type));
10152 else if (code == INTEGER_TYPE
10153 && TREE_TYPE (type) != NULL_TREE
10154 && subrange_type_for_debug_p (type, &low, &high))
10156 mod_type_die = subrange_type_die (type, low, high, context_die);
10157 item_type = TREE_TYPE (type);
10159 else if (is_base_type (type))
10160 mod_type_die = base_type_die (type);
10161 else
10163 gen_type_die (type, context_die);
10165 /* We have to get the type_main_variant here (and pass that to the
10166 `lookup_type_die' routine) because the ..._TYPE node we have
10167 might simply be a *copy* of some original type node (where the
10168 copy was created to help us keep track of typedef names) and
10169 that copy might have a different TYPE_UID from the original
10170 ..._TYPE node. */
10171 if (TREE_CODE (type) != VECTOR_TYPE)
10172 return lookup_type_die (type_main_variant (type));
10173 else
10174 /* Vectors have the debugging information in the type,
10175 not the main variant. */
10176 return lookup_type_die (type);
10179 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
10180 don't output a DW_TAG_typedef, since there isn't one in the
10181 user's program; just attach a DW_AT_name to the type.
10182 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10183 if the base type already has the same name. */
10184 if (name
10185 && ((TREE_CODE (name) != TYPE_DECL
10186 && (qualified_type == TYPE_MAIN_VARIANT (type)
10187 || (!is_const_type && !is_volatile_type)))
10188 || (TREE_CODE (name) == TYPE_DECL
10189 && TREE_TYPE (name) == qualified_type
10190 && DECL_NAME (name))))
10192 if (TREE_CODE (name) == TYPE_DECL)
10193 /* Could just call add_name_and_src_coords_attributes here,
10194 but since this is a builtin type it doesn't have any
10195 useful source coordinates anyway. */
10196 name = DECL_NAME (name);
10197 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10199 /* This probably indicates a bug. */
10200 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10202 name = TYPE_NAME (type);
10203 if (name
10204 && TREE_CODE (name) == TYPE_DECL)
10205 name = DECL_NAME (name);
10206 add_name_attribute (mod_type_die,
10207 name ? IDENTIFIER_POINTER (name) : "__unknown__");
10210 if (qualified_type)
10211 equate_type_number_to_die (qualified_type, mod_type_die);
10213 if (item_type)
10214 /* We must do this after the equate_type_number_to_die call, in case
10215 this is a recursive type. This ensures that the modified_type_die
10216 recursion will terminate even if the type is recursive. Recursive
10217 types are possible in Ada. */
10218 sub_die = modified_type_die (item_type,
10219 TYPE_READONLY (item_type),
10220 TYPE_VOLATILE (item_type),
10221 context_die);
10223 if (sub_die != NULL)
10224 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10226 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10227 if (TYPE_ARTIFICIAL (type))
10228 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10230 return mod_type_die;
10233 /* Generate DIEs for the generic parameters of T.
10234 T must be either a generic type or a generic function.
10235 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
10237 static void
10238 gen_generic_params_dies (tree t)
10240 tree parms, args;
10241 int parms_num, i;
10242 dw_die_ref die = NULL;
10244 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10245 return;
10247 if (TYPE_P (t))
10248 die = lookup_type_die (t);
10249 else if (DECL_P (t))
10250 die = lookup_decl_die (t);
10252 gcc_assert (die);
10254 parms = lang_hooks.get_innermost_generic_parms (t);
10255 if (!parms)
10256 /* T has no generic parameter. It means T is neither a generic type
10257 or function. End of story. */
10258 return;
10260 parms_num = TREE_VEC_LENGTH (parms);
10261 args = lang_hooks.get_innermost_generic_args (t);
10262 for (i = 0; i < parms_num; i++)
10264 tree parm, arg, arg_pack_elems;
10266 parm = TREE_VEC_ELT (parms, i);
10267 arg = TREE_VEC_ELT (args, i);
10268 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
10269 gcc_assert (parm && TREE_VALUE (parm) && arg);
10271 if (parm && TREE_VALUE (parm) && arg)
10273 /* If PARM represents a template parameter pack,
10274 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
10275 by DW_TAG_template_*_parameter DIEs for the argument
10276 pack elements of ARG. Note that ARG would then be
10277 an argument pack. */
10278 if (arg_pack_elems)
10279 template_parameter_pack_die (TREE_VALUE (parm),
10280 arg_pack_elems,
10281 die);
10282 else
10283 generic_parameter_die (TREE_VALUE (parm), arg,
10284 true /* Emit DW_AT_name */, die);
10289 /* Create and return a DIE for PARM which should be
10290 the representation of a generic type parameter.
10291 For instance, in the C++ front end, PARM would be a template parameter.
10292 ARG is the argument to PARM.
10293 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
10294 name of the PARM.
10295 PARENT_DIE is the parent DIE which the new created DIE should be added to,
10296 as a child node. */
10298 static dw_die_ref
10299 generic_parameter_die (tree parm, tree arg,
10300 bool emit_name_p,
10301 dw_die_ref parent_die)
10303 dw_die_ref tmpl_die = NULL;
10304 const char *name = NULL;
10306 if (!parm || !DECL_NAME (parm) || !arg)
10307 return NULL;
10309 /* We support non-type generic parameters and arguments,
10310 type generic parameters and arguments, as well as
10311 generic generic parameters (a.k.a. template template parameters in C++)
10312 and arguments. */
10313 if (TREE_CODE (parm) == PARM_DECL)
10314 /* PARM is a nontype generic parameter */
10315 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10316 else if (TREE_CODE (parm) == TYPE_DECL)
10317 /* PARM is a type generic parameter. */
10318 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10319 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10320 /* PARM is a generic generic parameter.
10321 Its DIE is a GNU extension. It shall have a
10322 DW_AT_name attribute to represent the name of the template template
10323 parameter, and a DW_AT_GNU_template_name attribute to represent the
10324 name of the template template argument. */
10325 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10326 parent_die, parm);
10327 else
10328 gcc_unreachable ();
10330 if (tmpl_die)
10332 tree tmpl_type;
10334 /* If PARM is a generic parameter pack, it means we are
10335 emitting debug info for a template argument pack element.
10336 In other terms, ARG is a template argument pack element.
10337 In that case, we don't emit any DW_AT_name attribute for
10338 the die. */
10339 if (emit_name_p)
10341 name = IDENTIFIER_POINTER (DECL_NAME (parm));
10342 gcc_assert (name);
10343 add_AT_string (tmpl_die, DW_AT_name, name);
10346 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10348 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10349 TMPL_DIE should have a child DW_AT_type attribute that is set
10350 to the type of the argument to PARM, which is ARG.
10351 If PARM is a type generic parameter, TMPL_DIE should have a
10352 child DW_AT_type that is set to ARG. */
10353 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10354 add_type_attribute (tmpl_die, tmpl_type, 0,
10355 TREE_THIS_VOLATILE (tmpl_type),
10356 parent_die);
10358 else
10360 /* So TMPL_DIE is a DIE representing a
10361 a generic generic template parameter, a.k.a template template
10362 parameter in C++ and arg is a template. */
10364 /* The DW_AT_GNU_template_name attribute of the DIE must be set
10365 to the name of the argument. */
10366 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
10367 if (name)
10368 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
10371 if (TREE_CODE (parm) == PARM_DECL)
10372 /* So PARM is a non-type generic parameter.
10373 DWARF3 5.6.8 says we must set a DW_AT_const_value child
10374 attribute of TMPL_DIE which value represents the value
10375 of ARG.
10376 We must be careful here:
10377 The value of ARG might reference some function decls.
10378 We might currently be emitting debug info for a generic
10379 type and types are emitted before function decls, we don't
10380 know if the function decls referenced by ARG will actually be
10381 emitted after cgraph computations.
10382 So must defer the generation of the DW_AT_const_value to
10383 after cgraph is ready. */
10384 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
10387 return tmpl_die;
10390 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
10391 PARM_PACK must be a template parameter pack. The returned DIE
10392 will be child DIE of PARENT_DIE. */
10394 static dw_die_ref
10395 template_parameter_pack_die (tree parm_pack,
10396 tree parm_pack_args,
10397 dw_die_ref parent_die)
10399 dw_die_ref die;
10400 int j;
10402 gcc_assert (parent_die && parm_pack);
10404 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
10405 add_name_and_src_coords_attributes (die, parm_pack);
10406 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
10407 generic_parameter_die (parm_pack,
10408 TREE_VEC_ELT (parm_pack_args, j),
10409 false /* Don't emit DW_AT_name */,
10410 die);
10411 return die;
10414 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
10415 an enumerated type. */
10417 static inline int
10418 type_is_enum (const_tree type)
10420 return TREE_CODE (type) == ENUMERAL_TYPE;
10423 /* Return the DBX register number described by a given RTL node. */
10425 static unsigned int
10426 dbx_reg_number (const_rtx rtl)
10428 unsigned regno = REGNO (rtl);
10430 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10432 #ifdef LEAF_REG_REMAP
10433 if (crtl->uses_only_leaf_regs)
10435 int leaf_reg = LEAF_REG_REMAP (regno);
10436 if (leaf_reg != -1)
10437 regno = (unsigned) leaf_reg;
10439 #endif
10441 regno = DBX_REGISTER_NUMBER (regno);
10442 gcc_assert (regno != INVALID_REGNUM);
10443 return regno;
10446 /* Optionally add a DW_OP_piece term to a location description expression.
10447 DW_OP_piece is only added if the location description expression already
10448 doesn't end with DW_OP_piece. */
10450 static void
10451 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10453 dw_loc_descr_ref loc;
10455 if (*list_head != NULL)
10457 /* Find the end of the chain. */
10458 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10461 if (loc->dw_loc_opc != DW_OP_piece)
10462 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10466 /* Return a location descriptor that designates a machine register or
10467 zero if there is none. */
10469 static dw_loc_descr_ref
10470 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10472 rtx regs;
10474 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10475 return 0;
10477 /* We only use "frame base" when we're sure we're talking about the
10478 post-prologue local stack frame. We do this by *not* running
10479 register elimination until this point, and recognizing the special
10480 argument pointer and soft frame pointer rtx's.
10481 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
10482 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
10483 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
10485 dw_loc_descr_ref result = NULL;
10487 if (dwarf_version >= 4 || !dwarf_strict)
10489 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
10490 initialized);
10491 if (result)
10492 add_loc_descr (&result,
10493 new_loc_descr (DW_OP_stack_value, 0, 0));
10495 return result;
10498 regs = targetm.dwarf_register_span (rtl);
10500 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
10501 return multiple_reg_loc_descriptor (rtl, regs, initialized);
10502 else
10504 unsigned int dbx_regnum = dbx_reg_number (rtl);
10505 if (dbx_regnum == IGNORED_DWARF_REGNUM)
10506 return 0;
10507 return one_reg_loc_descriptor (dbx_regnum, initialized);
10511 /* Return a location descriptor that designates a machine register for
10512 a given hard register number. */
10514 static dw_loc_descr_ref
10515 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
10517 dw_loc_descr_ref reg_loc_descr;
10519 if (regno <= 31)
10520 reg_loc_descr
10521 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
10522 else
10523 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
10525 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10526 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10528 return reg_loc_descr;
10531 /* Given an RTL of a register, return a location descriptor that
10532 designates a value that spans more than one register. */
10534 static dw_loc_descr_ref
10535 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
10536 enum var_init_status initialized)
10538 int nregs, size, i;
10539 unsigned reg;
10540 dw_loc_descr_ref loc_result = NULL;
10542 reg = REGNO (rtl);
10543 #ifdef LEAF_REG_REMAP
10544 if (crtl->uses_only_leaf_regs)
10546 int leaf_reg = LEAF_REG_REMAP (reg);
10547 if (leaf_reg != -1)
10548 reg = (unsigned) leaf_reg;
10550 #endif
10551 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
10552 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
10554 /* Simple, contiguous registers. */
10555 if (regs == NULL_RTX)
10557 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
10559 loc_result = NULL;
10560 while (nregs--)
10562 dw_loc_descr_ref t;
10564 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
10565 VAR_INIT_STATUS_INITIALIZED);
10566 add_loc_descr (&loc_result, t);
10567 add_loc_descr_op_piece (&loc_result, size);
10568 ++reg;
10570 return loc_result;
10573 /* Now onto stupid register sets in non contiguous locations. */
10575 gcc_assert (GET_CODE (regs) == PARALLEL);
10577 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10578 loc_result = NULL;
10580 for (i = 0; i < XVECLEN (regs, 0); ++i)
10582 dw_loc_descr_ref t;
10584 t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
10585 VAR_INIT_STATUS_INITIALIZED);
10586 add_loc_descr (&loc_result, t);
10587 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10588 add_loc_descr_op_piece (&loc_result, size);
10591 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10592 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10593 return loc_result;
10596 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
10598 /* Return a location descriptor that designates a constant i,
10599 as a compound operation from constant (i >> shift), constant shift
10600 and DW_OP_shl. */
10602 static dw_loc_descr_ref
10603 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10605 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
10606 add_loc_descr (&ret, int_loc_descriptor (shift));
10607 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
10608 return ret;
10611 /* Return a location descriptor that designates a constant. */
10613 static dw_loc_descr_ref
10614 int_loc_descriptor (HOST_WIDE_INT i)
10616 enum dwarf_location_atom op;
10618 /* Pick the smallest representation of a constant, rather than just
10619 defaulting to the LEB encoding. */
10620 if (i >= 0)
10622 int clz = clz_hwi (i);
10623 int ctz = ctz_hwi (i);
10624 if (i <= 31)
10625 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
10626 else if (i <= 0xff)
10627 op = DW_OP_const1u;
10628 else if (i <= 0xffff)
10629 op = DW_OP_const2u;
10630 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10631 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10632 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
10633 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
10634 while DW_OP_const4u is 5 bytes. */
10635 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
10636 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10637 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10638 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
10639 while DW_OP_const4u is 5 bytes. */
10640 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10641 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10642 op = DW_OP_const4u;
10643 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10644 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10645 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
10646 while DW_OP_constu of constant >= 0x100000000 takes at least
10647 6 bytes. */
10648 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10649 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10650 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
10651 >= HOST_BITS_PER_WIDE_INT)
10652 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
10653 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
10654 while DW_OP_constu takes in this case at least 6 bytes. */
10655 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
10656 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10657 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10658 && size_of_uleb128 (i) > 6)
10659 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
10660 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
10661 else
10662 op = DW_OP_constu;
10664 else
10666 if (i >= -0x80)
10667 op = DW_OP_const1s;
10668 else if (i >= -0x8000)
10669 op = DW_OP_const2s;
10670 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10672 if (size_of_int_loc_descriptor (i) < 5)
10674 dw_loc_descr_ref ret = int_loc_descriptor (-i);
10675 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10676 return ret;
10678 op = DW_OP_const4s;
10680 else
10682 if (size_of_int_loc_descriptor (i)
10683 < (unsigned long) 1 + size_of_sleb128 (i))
10685 dw_loc_descr_ref ret = int_loc_descriptor (-i);
10686 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10687 return ret;
10689 op = DW_OP_consts;
10693 return new_loc_descr (op, i, 0);
10696 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
10697 without actually allocating it. */
10699 static unsigned long
10700 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10702 return size_of_int_loc_descriptor (i >> shift)
10703 + size_of_int_loc_descriptor (shift)
10704 + 1;
10707 /* Return size_of_locs (int_loc_descriptor (i)) without
10708 actually allocating it. */
10710 static unsigned long
10711 size_of_int_loc_descriptor (HOST_WIDE_INT i)
10713 unsigned long s;
10715 if (i >= 0)
10717 int clz, ctz;
10718 if (i <= 31)
10719 return 1;
10720 else if (i <= 0xff)
10721 return 2;
10722 else if (i <= 0xffff)
10723 return 3;
10724 clz = clz_hwi (i);
10725 ctz = ctz_hwi (i);
10726 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10727 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10728 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10729 - clz - 5);
10730 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10731 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10732 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10733 - clz - 8);
10734 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10735 return 5;
10736 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
10737 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10738 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10739 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10740 - clz - 8);
10741 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10742 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
10743 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10744 - clz - 16);
10745 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10746 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10747 && s > 6)
10748 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10749 - clz - 32);
10750 else
10751 return 1 + s;
10753 else
10755 if (i >= -0x80)
10756 return 2;
10757 else if (i >= -0x8000)
10758 return 3;
10759 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10761 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
10763 s = size_of_int_loc_descriptor (-i) + 1;
10764 if (s < 5)
10765 return s;
10767 return 5;
10769 else
10771 unsigned long r = 1 + size_of_sleb128 (i);
10772 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
10774 s = size_of_int_loc_descriptor (-i) + 1;
10775 if (s < r)
10776 return s;
10778 return r;
10783 /* Return loc description representing "address" of integer value.
10784 This can appear only as toplevel expression. */
10786 static dw_loc_descr_ref
10787 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
10789 int litsize;
10790 dw_loc_descr_ref loc_result = NULL;
10792 if (!(dwarf_version >= 4 || !dwarf_strict))
10793 return NULL;
10795 litsize = size_of_int_loc_descriptor (i);
10796 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
10797 is more compact. For DW_OP_stack_value we need:
10798 litsize + 1 (DW_OP_stack_value)
10799 and for DW_OP_implicit_value:
10800 1 (DW_OP_implicit_value) + 1 (length) + size. */
10801 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
10803 loc_result = int_loc_descriptor (i);
10804 add_loc_descr (&loc_result,
10805 new_loc_descr (DW_OP_stack_value, 0, 0));
10806 return loc_result;
10809 loc_result = new_loc_descr (DW_OP_implicit_value,
10810 size, 0);
10811 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
10812 loc_result->dw_loc_oprnd2.v.val_int = i;
10813 return loc_result;
10816 /* Return a location descriptor that designates a base+offset location. */
10818 static dw_loc_descr_ref
10819 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
10820 enum var_init_status initialized)
10822 unsigned int regno;
10823 dw_loc_descr_ref result;
10824 dw_fde_ref fde = cfun->fde;
10826 /* We only use "frame base" when we're sure we're talking about the
10827 post-prologue local stack frame. We do this by *not* running
10828 register elimination until this point, and recognizing the special
10829 argument pointer and soft frame pointer rtx's. */
10830 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
10832 rtx elim = (ira_use_lra_p
10833 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
10834 : eliminate_regs (reg, VOIDmode, NULL_RTX));
10836 if (elim != reg)
10838 if (GET_CODE (elim) == PLUS)
10840 offset += INTVAL (XEXP (elim, 1));
10841 elim = XEXP (elim, 0);
10843 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
10844 && (elim == hard_frame_pointer_rtx
10845 || elim == stack_pointer_rtx))
10846 || elim == (frame_pointer_needed
10847 ? hard_frame_pointer_rtx
10848 : stack_pointer_rtx));
10850 /* If drap register is used to align stack, use frame
10851 pointer + offset to access stack variables. If stack
10852 is aligned without drap, use stack pointer + offset to
10853 access stack variables. */
10854 if (crtl->stack_realign_tried
10855 && reg == frame_pointer_rtx)
10857 int base_reg
10858 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
10859 ? HARD_FRAME_POINTER_REGNUM
10860 : REGNO (elim));
10861 return new_reg_loc_descr (base_reg, offset);
10864 gcc_assert (frame_pointer_fb_offset_valid);
10865 offset += frame_pointer_fb_offset;
10866 return new_loc_descr (DW_OP_fbreg, offset, 0);
10870 regno = REGNO (reg);
10871 #ifdef LEAF_REG_REMAP
10872 if (crtl->uses_only_leaf_regs)
10874 int leaf_reg = LEAF_REG_REMAP (regno);
10875 if (leaf_reg != -1)
10876 regno = (unsigned) leaf_reg;
10878 #endif
10879 regno = DWARF_FRAME_REGNUM (regno);
10881 if (!optimize && fde
10882 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
10884 /* Use cfa+offset to represent the location of arguments passed
10885 on the stack when drap is used to align stack.
10886 Only do this when not optimizing, for optimized code var-tracking
10887 is supposed to track where the arguments live and the register
10888 used as vdrap or drap in some spot might be used for something
10889 else in other part of the routine. */
10890 return new_loc_descr (DW_OP_fbreg, offset, 0);
10893 if (regno <= 31)
10894 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
10895 offset, 0);
10896 else
10897 result = new_loc_descr (DW_OP_bregx, regno, offset);
10899 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10900 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10902 return result;
10905 /* Return true if this RTL expression describes a base+offset calculation. */
10907 static inline int
10908 is_based_loc (const_rtx rtl)
10910 return (GET_CODE (rtl) == PLUS
10911 && ((REG_P (XEXP (rtl, 0))
10912 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
10913 && CONST_INT_P (XEXP (rtl, 1)))));
10916 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
10917 failed. */
10919 static dw_loc_descr_ref
10920 tls_mem_loc_descriptor (rtx mem)
10922 tree base;
10923 dw_loc_descr_ref loc_result;
10925 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
10926 return NULL;
10928 base = get_base_address (MEM_EXPR (mem));
10929 if (base == NULL
10930 || TREE_CODE (base) != VAR_DECL
10931 || !DECL_THREAD_LOCAL_P (base))
10932 return NULL;
10934 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
10935 if (loc_result == NULL)
10936 return NULL;
10938 if (MEM_OFFSET (mem))
10939 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
10941 return loc_result;
10944 /* Output debug info about reason why we failed to expand expression as dwarf
10945 expression. */
10947 static void
10948 expansion_failed (tree expr, rtx rtl, char const *reason)
10950 if (dump_file && (dump_flags & TDF_DETAILS))
10952 fprintf (dump_file, "Failed to expand as dwarf: ");
10953 if (expr)
10954 print_generic_expr (dump_file, expr, dump_flags);
10955 if (rtl)
10957 fprintf (dump_file, "\n");
10958 print_rtl (dump_file, rtl);
10960 fprintf (dump_file, "\nReason: %s\n", reason);
10964 /* Helper function for const_ok_for_output, called either directly
10965 or via for_each_rtx. */
10967 static int
10968 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
10970 rtx rtl = *rtlp;
10972 if (GET_CODE (rtl) == UNSPEC)
10974 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
10975 we can't express it in the debug info. */
10976 #ifdef ENABLE_CHECKING
10977 /* Don't complain about TLS UNSPECs, those are just too hard to
10978 delegitimize. Note this could be a non-decl SYMBOL_REF such as
10979 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
10980 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
10981 if (XVECLEN (rtl, 0) == 0
10982 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
10983 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
10984 inform (current_function_decl
10985 ? DECL_SOURCE_LOCATION (current_function_decl)
10986 : UNKNOWN_LOCATION,
10987 #if NUM_UNSPEC_VALUES > 0
10988 "non-delegitimized UNSPEC %s (%d) found in variable location",
10989 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
10990 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
10991 XINT (rtl, 1));
10992 #else
10993 "non-delegitimized UNSPEC %d found in variable location",
10994 XINT (rtl, 1));
10995 #endif
10996 #endif
10997 expansion_failed (NULL_TREE, rtl,
10998 "UNSPEC hasn't been delegitimized.\n");
10999 return 1;
11002 if (targetm.const_not_ok_for_debug_p (rtl))
11004 expansion_failed (NULL_TREE, rtl,
11005 "Expression rejected for debug by the backend.\n");
11006 return 1;
11009 if (GET_CODE (rtl) != SYMBOL_REF)
11010 return 0;
11012 if (CONSTANT_POOL_ADDRESS_P (rtl))
11014 bool marked;
11015 get_pool_constant_mark (rtl, &marked);
11016 /* If all references to this pool constant were optimized away,
11017 it was not output and thus we can't represent it. */
11018 if (!marked)
11020 expansion_failed (NULL_TREE, rtl,
11021 "Constant was removed from constant pool.\n");
11022 return 1;
11026 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11027 return 1;
11029 /* Avoid references to external symbols in debug info, on several targets
11030 the linker might even refuse to link when linking a shared library,
11031 and in many other cases the relocations for .debug_info/.debug_loc are
11032 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
11033 to be defined within the same shared library or executable are fine. */
11034 if (SYMBOL_REF_EXTERNAL_P (rtl))
11036 tree decl = SYMBOL_REF_DECL (rtl);
11038 if (decl == NULL || !targetm.binds_local_p (decl))
11040 expansion_failed (NULL_TREE, rtl,
11041 "Symbol not defined in current TU.\n");
11042 return 1;
11046 return 0;
11049 /* Return true if constant RTL can be emitted in DW_OP_addr or
11050 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
11051 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
11053 static bool
11054 const_ok_for_output (rtx rtl)
11056 if (GET_CODE (rtl) == SYMBOL_REF)
11057 return const_ok_for_output_1 (&rtl, NULL) == 0;
11059 if (GET_CODE (rtl) == CONST)
11060 return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
11062 return true;
11065 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11066 if possible, NULL otherwise. */
11068 static dw_die_ref
11069 base_type_for_mode (enum machine_mode mode, bool unsignedp)
11071 dw_die_ref type_die;
11072 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11074 if (type == NULL)
11075 return NULL;
11076 switch (TREE_CODE (type))
11078 case INTEGER_TYPE:
11079 case REAL_TYPE:
11080 break;
11081 default:
11082 return NULL;
11084 type_die = lookup_type_die (type);
11085 if (!type_die)
11086 type_die = modified_type_die (type, false, false, comp_unit_die ());
11087 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11088 return NULL;
11089 return type_die;
11092 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11093 type matching MODE, or, if MODE is narrower than or as wide as
11094 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
11095 possible. */
11097 static dw_loc_descr_ref
11098 convert_descriptor_to_mode (enum machine_mode mode, dw_loc_descr_ref op)
11100 enum machine_mode outer_mode = mode;
11101 dw_die_ref type_die;
11102 dw_loc_descr_ref cvt;
11104 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11106 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11107 return op;
11109 type_die = base_type_for_mode (outer_mode, 1);
11110 if (type_die == NULL)
11111 return NULL;
11112 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11113 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11114 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11115 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11116 add_loc_descr (&op, cvt);
11117 return op;
11120 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
11122 static dw_loc_descr_ref
11123 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11124 dw_loc_descr_ref op1)
11126 dw_loc_descr_ref ret = op0;
11127 add_loc_descr (&ret, op1);
11128 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11129 if (STORE_FLAG_VALUE != 1)
11131 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11132 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11134 return ret;
11137 /* Return location descriptor for signed comparison OP RTL. */
11139 static dw_loc_descr_ref
11140 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11141 enum machine_mode mem_mode)
11143 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11144 dw_loc_descr_ref op0, op1;
11145 int shift;
11147 if (op_mode == VOIDmode)
11148 op_mode = GET_MODE (XEXP (rtl, 1));
11149 if (op_mode == VOIDmode)
11150 return NULL;
11152 if (dwarf_strict
11153 && (GET_MODE_CLASS (op_mode) != MODE_INT
11154 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11155 return NULL;
11157 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11158 VAR_INIT_STATUS_INITIALIZED);
11159 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11160 VAR_INIT_STATUS_INITIALIZED);
11162 if (op0 == NULL || op1 == NULL)
11163 return NULL;
11165 if (GET_MODE_CLASS (op_mode) != MODE_INT
11166 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11167 return compare_loc_descriptor (op, op0, op1);
11169 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11171 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11172 dw_loc_descr_ref cvt;
11174 if (type_die == NULL)
11175 return NULL;
11176 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11177 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11178 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11179 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11180 add_loc_descr (&op0, cvt);
11181 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11182 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11183 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11184 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11185 add_loc_descr (&op1, cvt);
11186 return compare_loc_descriptor (op, op0, op1);
11189 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11190 /* For eq/ne, if the operands are known to be zero-extended,
11191 there is no need to do the fancy shifting up. */
11192 if (op == DW_OP_eq || op == DW_OP_ne)
11194 dw_loc_descr_ref last0, last1;
11195 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11197 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11199 /* deref_size zero extends, and for constants we can check
11200 whether they are zero extended or not. */
11201 if (((last0->dw_loc_opc == DW_OP_deref_size
11202 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11203 || (CONST_INT_P (XEXP (rtl, 0))
11204 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11205 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11206 && ((last1->dw_loc_opc == DW_OP_deref_size
11207 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11208 || (CONST_INT_P (XEXP (rtl, 1))
11209 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11210 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11211 return compare_loc_descriptor (op, op0, op1);
11213 /* EQ/NE comparison against constant in narrower type than
11214 DWARF2_ADDR_SIZE can be performed either as
11215 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11216 DW_OP_{eq,ne}
11218 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11219 DW_OP_{eq,ne}. Pick whatever is shorter. */
11220 if (CONST_INT_P (XEXP (rtl, 1))
11221 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11222 && (size_of_int_loc_descriptor (shift) + 1
11223 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11224 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11225 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11226 & GET_MODE_MASK (op_mode))))
11228 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
11229 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11230 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11231 & GET_MODE_MASK (op_mode));
11232 return compare_loc_descriptor (op, op0, op1);
11235 add_loc_descr (&op0, int_loc_descriptor (shift));
11236 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11237 if (CONST_INT_P (XEXP (rtl, 1)))
11238 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11239 else
11241 add_loc_descr (&op1, int_loc_descriptor (shift));
11242 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11244 return compare_loc_descriptor (op, op0, op1);
11247 /* Return location descriptor for unsigned comparison OP RTL. */
11249 static dw_loc_descr_ref
11250 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11251 enum machine_mode mem_mode)
11253 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11254 dw_loc_descr_ref op0, op1;
11256 if (op_mode == VOIDmode)
11257 op_mode = GET_MODE (XEXP (rtl, 1));
11258 if (op_mode == VOIDmode)
11259 return NULL;
11260 if (GET_MODE_CLASS (op_mode) != MODE_INT)
11261 return NULL;
11263 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11264 return NULL;
11266 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11267 VAR_INIT_STATUS_INITIALIZED);
11268 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11269 VAR_INIT_STATUS_INITIALIZED);
11271 if (op0 == NULL || op1 == NULL)
11272 return NULL;
11274 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
11276 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
11277 dw_loc_descr_ref last0, last1;
11278 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11280 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11282 if (CONST_INT_P (XEXP (rtl, 0)))
11283 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
11284 /* deref_size zero extends, so no need to mask it again. */
11285 else if (last0->dw_loc_opc != DW_OP_deref_size
11286 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11288 add_loc_descr (&op0, int_loc_descriptor (mask));
11289 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11291 if (CONST_INT_P (XEXP (rtl, 1)))
11292 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11293 /* deref_size zero extends, so no need to mask it again. */
11294 else if (last1->dw_loc_opc != DW_OP_deref_size
11295 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11297 add_loc_descr (&op1, int_loc_descriptor (mask));
11298 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11301 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11303 HOST_WIDE_INT bias = 1;
11304 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11305 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11306 if (CONST_INT_P (XEXP (rtl, 1)))
11307 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
11308 + INTVAL (XEXP (rtl, 1)));
11309 else
11310 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
11311 bias, 0));
11313 return compare_loc_descriptor (op, op0, op1);
11316 /* Return location descriptor for {U,S}{MIN,MAX}. */
11318 static dw_loc_descr_ref
11319 minmax_loc_descriptor (rtx rtl, enum machine_mode mode,
11320 enum machine_mode mem_mode)
11322 enum dwarf_location_atom op;
11323 dw_loc_descr_ref op0, op1, ret;
11324 dw_loc_descr_ref bra_node, drop_node;
11326 if (dwarf_strict
11327 && (GET_MODE_CLASS (mode) != MODE_INT
11328 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
11329 return NULL;
11331 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11332 VAR_INIT_STATUS_INITIALIZED);
11333 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11334 VAR_INIT_STATUS_INITIALIZED);
11336 if (op0 == NULL || op1 == NULL)
11337 return NULL;
11339 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
11340 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
11341 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
11342 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
11344 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11346 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11347 add_loc_descr (&op0, int_loc_descriptor (mask));
11348 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11349 add_loc_descr (&op1, int_loc_descriptor (mask));
11350 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11352 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11354 HOST_WIDE_INT bias = 1;
11355 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11356 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11357 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11360 else if (GET_MODE_CLASS (mode) == MODE_INT
11361 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11363 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
11364 add_loc_descr (&op0, int_loc_descriptor (shift));
11365 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11366 add_loc_descr (&op1, int_loc_descriptor (shift));
11367 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11369 else if (GET_MODE_CLASS (mode) == MODE_INT
11370 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11372 dw_die_ref type_die = base_type_for_mode (mode, 0);
11373 dw_loc_descr_ref cvt;
11374 if (type_die == NULL)
11375 return NULL;
11376 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11377 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11378 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11379 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11380 add_loc_descr (&op0, cvt);
11381 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11382 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11383 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11384 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11385 add_loc_descr (&op1, cvt);
11388 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
11389 op = DW_OP_lt;
11390 else
11391 op = DW_OP_gt;
11392 ret = op0;
11393 add_loc_descr (&ret, op1);
11394 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11395 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11396 add_loc_descr (&ret, bra_node);
11397 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11398 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11399 add_loc_descr (&ret, drop_node);
11400 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11401 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11402 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
11403 && GET_MODE_CLASS (mode) == MODE_INT
11404 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11405 ret = convert_descriptor_to_mode (mode, ret);
11406 return ret;
11409 /* Helper function for mem_loc_descriptor. Perform OP binary op,
11410 but after converting arguments to type_die, afterwards
11411 convert back to unsigned. */
11413 static dw_loc_descr_ref
11414 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
11415 enum machine_mode mode, enum machine_mode mem_mode)
11417 dw_loc_descr_ref cvt, op0, op1;
11419 if (type_die == NULL)
11420 return NULL;
11421 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11422 VAR_INIT_STATUS_INITIALIZED);
11423 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11424 VAR_INIT_STATUS_INITIALIZED);
11425 if (op0 == NULL || op1 == NULL)
11426 return NULL;
11427 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11428 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11429 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11430 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11431 add_loc_descr (&op0, cvt);
11432 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11433 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11434 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11435 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11436 add_loc_descr (&op1, cvt);
11437 add_loc_descr (&op0, op1);
11438 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
11439 return convert_descriptor_to_mode (mode, op0);
11442 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
11443 const0 is DW_OP_lit0 or corresponding typed constant,
11444 const1 is DW_OP_lit1 or corresponding typed constant
11445 and constMSB is constant with just the MSB bit set
11446 for the mode):
11447 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11448 L1: const0 DW_OP_swap
11449 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
11450 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11451 L3: DW_OP_drop
11452 L4: DW_OP_nop
11454 CTZ is similar:
11455 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11456 L1: const0 DW_OP_swap
11457 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11458 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11459 L3: DW_OP_drop
11460 L4: DW_OP_nop
11462 FFS is similar:
11463 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
11464 L1: const1 DW_OP_swap
11465 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11466 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11467 L3: DW_OP_drop
11468 L4: DW_OP_nop */
11470 static dw_loc_descr_ref
11471 clz_loc_descriptor (rtx rtl, enum machine_mode mode,
11472 enum machine_mode mem_mode)
11474 dw_loc_descr_ref op0, ret, tmp;
11475 HOST_WIDE_INT valv;
11476 dw_loc_descr_ref l1jump, l1label;
11477 dw_loc_descr_ref l2jump, l2label;
11478 dw_loc_descr_ref l3jump, l3label;
11479 dw_loc_descr_ref l4jump, l4label;
11480 rtx msb;
11482 if (GET_MODE_CLASS (mode) != MODE_INT
11483 || GET_MODE (XEXP (rtl, 0)) != mode
11484 || (GET_CODE (rtl) == CLZ
11485 && GET_MODE_BITSIZE (mode) > HOST_BITS_PER_DOUBLE_INT))
11486 return NULL;
11488 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11489 VAR_INIT_STATUS_INITIALIZED);
11490 if (op0 == NULL)
11491 return NULL;
11492 ret = op0;
11493 if (GET_CODE (rtl) == CLZ)
11495 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11496 valv = GET_MODE_BITSIZE (mode);
11498 else if (GET_CODE (rtl) == FFS)
11499 valv = 0;
11500 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11501 valv = GET_MODE_BITSIZE (mode);
11502 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11503 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
11504 add_loc_descr (&ret, l1jump);
11505 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11506 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
11507 VAR_INIT_STATUS_INITIALIZED);
11508 if (tmp == NULL)
11509 return NULL;
11510 add_loc_descr (&ret, tmp);
11511 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
11512 add_loc_descr (&ret, l4jump);
11513 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
11514 ? const1_rtx : const0_rtx,
11515 mode, mem_mode,
11516 VAR_INIT_STATUS_INITIALIZED);
11517 if (l1label == NULL)
11518 return NULL;
11519 add_loc_descr (&ret, l1label);
11520 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11521 l2label = new_loc_descr (DW_OP_dup, 0, 0);
11522 add_loc_descr (&ret, l2label);
11523 if (GET_CODE (rtl) != CLZ)
11524 msb = const1_rtx;
11525 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11526 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
11527 << (GET_MODE_BITSIZE (mode) - 1));
11528 else
11529 msb = immed_double_const (0, (unsigned HOST_WIDE_INT) 1
11530 << (GET_MODE_BITSIZE (mode)
11531 - HOST_BITS_PER_WIDE_INT - 1), mode);
11532 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
11533 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11534 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
11535 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
11536 else
11537 tmp = mem_loc_descriptor (msb, mode, mem_mode,
11538 VAR_INIT_STATUS_INITIALIZED);
11539 if (tmp == NULL)
11540 return NULL;
11541 add_loc_descr (&ret, tmp);
11542 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11543 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
11544 add_loc_descr (&ret, l3jump);
11545 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11546 VAR_INIT_STATUS_INITIALIZED);
11547 if (tmp == NULL)
11548 return NULL;
11549 add_loc_descr (&ret, tmp);
11550 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
11551 ? DW_OP_shl : DW_OP_shr, 0, 0));
11552 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11553 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
11554 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11555 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
11556 add_loc_descr (&ret, l2jump);
11557 l3label = new_loc_descr (DW_OP_drop, 0, 0);
11558 add_loc_descr (&ret, l3label);
11559 l4label = new_loc_descr (DW_OP_nop, 0, 0);
11560 add_loc_descr (&ret, l4label);
11561 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11562 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11563 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11564 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11565 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11566 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
11567 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11568 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
11569 return ret;
11572 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
11573 const1 is DW_OP_lit1 or corresponding typed constant):
11574 const0 DW_OP_swap
11575 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11576 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11577 L2: DW_OP_drop
11579 PARITY is similar:
11580 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11581 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11582 L2: DW_OP_drop */
11584 static dw_loc_descr_ref
11585 popcount_loc_descriptor (rtx rtl, enum machine_mode mode,
11586 enum machine_mode mem_mode)
11588 dw_loc_descr_ref op0, ret, tmp;
11589 dw_loc_descr_ref l1jump, l1label;
11590 dw_loc_descr_ref l2jump, l2label;
11592 if (GET_MODE_CLASS (mode) != MODE_INT
11593 || GET_MODE (XEXP (rtl, 0)) != mode)
11594 return NULL;
11596 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11597 VAR_INIT_STATUS_INITIALIZED);
11598 if (op0 == NULL)
11599 return NULL;
11600 ret = op0;
11601 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11602 VAR_INIT_STATUS_INITIALIZED);
11603 if (tmp == NULL)
11604 return NULL;
11605 add_loc_descr (&ret, tmp);
11606 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11607 l1label = new_loc_descr (DW_OP_dup, 0, 0);
11608 add_loc_descr (&ret, l1label);
11609 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11610 add_loc_descr (&ret, l2jump);
11611 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11612 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11613 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11614 VAR_INIT_STATUS_INITIALIZED);
11615 if (tmp == NULL)
11616 return NULL;
11617 add_loc_descr (&ret, tmp);
11618 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11619 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
11620 ? DW_OP_plus : DW_OP_xor, 0, 0));
11621 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11622 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11623 VAR_INIT_STATUS_INITIALIZED);
11624 add_loc_descr (&ret, tmp);
11625 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11626 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11627 add_loc_descr (&ret, l1jump);
11628 l2label = new_loc_descr (DW_OP_drop, 0, 0);
11629 add_loc_descr (&ret, l2label);
11630 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11631 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11632 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11633 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11634 return ret;
11637 /* BSWAP (constS is initial shift count, either 56 or 24):
11638 constS const0
11639 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
11640 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
11641 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
11642 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
11643 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
11645 static dw_loc_descr_ref
11646 bswap_loc_descriptor (rtx rtl, enum machine_mode mode,
11647 enum machine_mode mem_mode)
11649 dw_loc_descr_ref op0, ret, tmp;
11650 dw_loc_descr_ref l1jump, l1label;
11651 dw_loc_descr_ref l2jump, l2label;
11653 if (GET_MODE_CLASS (mode) != MODE_INT
11654 || BITS_PER_UNIT != 8
11655 || (GET_MODE_BITSIZE (mode) != 32
11656 && GET_MODE_BITSIZE (mode) != 64))
11657 return NULL;
11659 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11660 VAR_INIT_STATUS_INITIALIZED);
11661 if (op0 == NULL)
11662 return NULL;
11664 ret = op0;
11665 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11666 mode, mem_mode,
11667 VAR_INIT_STATUS_INITIALIZED);
11668 if (tmp == NULL)
11669 return NULL;
11670 add_loc_descr (&ret, tmp);
11671 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11672 VAR_INIT_STATUS_INITIALIZED);
11673 if (tmp == NULL)
11674 return NULL;
11675 add_loc_descr (&ret, tmp);
11676 l1label = new_loc_descr (DW_OP_pick, 2, 0);
11677 add_loc_descr (&ret, l1label);
11678 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11679 mode, mem_mode,
11680 VAR_INIT_STATUS_INITIALIZED);
11681 add_loc_descr (&ret, tmp);
11682 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
11683 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11684 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11685 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
11686 VAR_INIT_STATUS_INITIALIZED);
11687 if (tmp == NULL)
11688 return NULL;
11689 add_loc_descr (&ret, tmp);
11690 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11691 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
11692 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11693 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11694 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11695 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11696 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11697 VAR_INIT_STATUS_INITIALIZED);
11698 add_loc_descr (&ret, tmp);
11699 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
11700 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11701 add_loc_descr (&ret, l2jump);
11702 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
11703 VAR_INIT_STATUS_INITIALIZED);
11704 add_loc_descr (&ret, tmp);
11705 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11706 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11707 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11708 add_loc_descr (&ret, l1jump);
11709 l2label = new_loc_descr (DW_OP_drop, 0, 0);
11710 add_loc_descr (&ret, l2label);
11711 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11712 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11713 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11714 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11715 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11716 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11717 return ret;
11720 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
11721 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11722 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
11723 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
11725 ROTATERT is similar:
11726 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
11727 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11728 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
11730 static dw_loc_descr_ref
11731 rotate_loc_descriptor (rtx rtl, enum machine_mode mode,
11732 enum machine_mode mem_mode)
11734 rtx rtlop1 = XEXP (rtl, 1);
11735 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
11736 int i;
11738 if (GET_MODE_CLASS (mode) != MODE_INT)
11739 return NULL;
11741 if (GET_MODE (rtlop1) != VOIDmode
11742 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
11743 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
11744 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11745 VAR_INIT_STATUS_INITIALIZED);
11746 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
11747 VAR_INIT_STATUS_INITIALIZED);
11748 if (op0 == NULL || op1 == NULL)
11749 return NULL;
11750 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11751 for (i = 0; i < 2; i++)
11753 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
11754 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
11755 mode, mem_mode,
11756 VAR_INIT_STATUS_INITIALIZED);
11757 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
11758 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11759 ? DW_OP_const4u
11760 : HOST_BITS_PER_WIDE_INT == 64
11761 ? DW_OP_const8u : DW_OP_constu,
11762 GET_MODE_MASK (mode), 0);
11763 else
11764 mask[i] = NULL;
11765 if (mask[i] == NULL)
11766 return NULL;
11767 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
11769 ret = op0;
11770 add_loc_descr (&ret, op1);
11771 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11772 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11773 if (GET_CODE (rtl) == ROTATERT)
11775 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11776 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11777 GET_MODE_BITSIZE (mode), 0));
11779 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11780 if (mask[0] != NULL)
11781 add_loc_descr (&ret, mask[0]);
11782 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11783 if (mask[1] != NULL)
11785 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11786 add_loc_descr (&ret, mask[1]);
11787 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11789 if (GET_CODE (rtl) == ROTATE)
11791 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11792 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11793 GET_MODE_BITSIZE (mode), 0));
11795 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11796 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11797 return ret;
11800 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
11801 for DEBUG_PARAMETER_REF RTL. */
11803 static dw_loc_descr_ref
11804 parameter_ref_descriptor (rtx rtl)
11806 dw_loc_descr_ref ret;
11807 dw_die_ref ref;
11809 if (dwarf_strict)
11810 return NULL;
11811 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
11812 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
11813 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
11814 if (ref)
11816 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11817 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
11818 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
11820 else
11822 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
11823 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
11825 return ret;
11828 /* The following routine converts the RTL for a variable or parameter
11829 (resident in memory) into an equivalent Dwarf representation of a
11830 mechanism for getting the address of that same variable onto the top of a
11831 hypothetical "address evaluation" stack.
11833 When creating memory location descriptors, we are effectively transforming
11834 the RTL for a memory-resident object into its Dwarf postfix expression
11835 equivalent. This routine recursively descends an RTL tree, turning
11836 it into Dwarf postfix code as it goes.
11838 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
11840 MEM_MODE is the mode of the memory reference, needed to handle some
11841 autoincrement addressing modes.
11843 Return 0 if we can't represent the location. */
11845 dw_loc_descr_ref
11846 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
11847 enum machine_mode mem_mode,
11848 enum var_init_status initialized)
11850 dw_loc_descr_ref mem_loc_result = NULL;
11851 enum dwarf_location_atom op;
11852 dw_loc_descr_ref op0, op1;
11853 rtx inner = NULL_RTX;
11855 if (mode == VOIDmode)
11856 mode = GET_MODE (rtl);
11858 /* Note that for a dynamically sized array, the location we will generate a
11859 description of here will be the lowest numbered location which is
11860 actually within the array. That's *not* necessarily the same as the
11861 zeroth element of the array. */
11863 rtl = targetm.delegitimize_address (rtl);
11865 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
11866 return NULL;
11868 switch (GET_CODE (rtl))
11870 case POST_INC:
11871 case POST_DEC:
11872 case POST_MODIFY:
11873 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
11875 case SUBREG:
11876 /* The case of a subreg may arise when we have a local (register)
11877 variable or a formal (register) parameter which doesn't quite fill
11878 up an entire register. For now, just assume that it is
11879 legitimate to make the Dwarf info refer to the whole register which
11880 contains the given subreg. */
11881 if (!subreg_lowpart_p (rtl))
11882 break;
11883 inner = SUBREG_REG (rtl);
11884 case TRUNCATE:
11885 if (inner == NULL_RTX)
11886 inner = XEXP (rtl, 0);
11887 if (GET_MODE_CLASS (mode) == MODE_INT
11888 && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
11889 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11890 #ifdef POINTERS_EXTEND_UNSIGNED
11891 || (mode == Pmode && mem_mode != VOIDmode)
11892 #endif
11894 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
11896 mem_loc_result = mem_loc_descriptor (inner,
11897 GET_MODE (inner),
11898 mem_mode, initialized);
11899 break;
11901 if (dwarf_strict)
11902 break;
11903 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
11904 break;
11905 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
11906 && (GET_MODE_CLASS (mode) != MODE_INT
11907 || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
11908 break;
11909 else
11911 dw_die_ref type_die;
11912 dw_loc_descr_ref cvt;
11914 mem_loc_result = mem_loc_descriptor (inner,
11915 GET_MODE (inner),
11916 mem_mode, initialized);
11917 if (mem_loc_result == NULL)
11918 break;
11919 type_die = base_type_for_mode (mode,
11920 GET_MODE_CLASS (mode) == MODE_INT);
11921 if (type_die == NULL)
11923 mem_loc_result = NULL;
11924 break;
11926 if (GET_MODE_SIZE (mode)
11927 != GET_MODE_SIZE (GET_MODE (inner)))
11928 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11929 else
11930 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
11931 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11932 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11933 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11934 add_loc_descr (&mem_loc_result, cvt);
11936 break;
11938 case REG:
11939 if (GET_MODE_CLASS (mode) != MODE_INT
11940 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
11941 && rtl != arg_pointer_rtx
11942 && rtl != frame_pointer_rtx
11943 #ifdef POINTERS_EXTEND_UNSIGNED
11944 && (mode != Pmode || mem_mode == VOIDmode)
11945 #endif
11948 dw_die_ref type_die;
11949 unsigned int dbx_regnum;
11951 if (dwarf_strict)
11952 break;
11953 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
11954 break;
11955 type_die = base_type_for_mode (mode,
11956 GET_MODE_CLASS (mode) == MODE_INT);
11957 if (type_die == NULL)
11958 break;
11960 dbx_regnum = dbx_reg_number (rtl);
11961 if (dbx_regnum == IGNORED_DWARF_REGNUM)
11962 break;
11963 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
11964 dbx_regnum, 0);
11965 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
11966 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
11967 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
11968 break;
11970 /* Whenever a register number forms a part of the description of the
11971 method for calculating the (dynamic) address of a memory resident
11972 object, DWARF rules require the register number be referred to as
11973 a "base register". This distinction is not based in any way upon
11974 what category of register the hardware believes the given register
11975 belongs to. This is strictly DWARF terminology we're dealing with
11976 here. Note that in cases where the location of a memory-resident
11977 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
11978 OP_CONST (0)) the actual DWARF location descriptor that we generate
11979 may just be OP_BASEREG (basereg). This may look deceptively like
11980 the object in question was allocated to a register (rather than in
11981 memory) so DWARF consumers need to be aware of the subtle
11982 distinction between OP_REG and OP_BASEREG. */
11983 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
11984 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
11985 else if (stack_realign_drap
11986 && crtl->drap_reg
11987 && crtl->args.internal_arg_pointer == rtl
11988 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
11990 /* If RTL is internal_arg_pointer, which has been optimized
11991 out, use DRAP instead. */
11992 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
11993 VAR_INIT_STATUS_INITIALIZED);
11995 break;
11997 case SIGN_EXTEND:
11998 case ZERO_EXTEND:
11999 if (GET_MODE_CLASS (mode) != MODE_INT)
12000 break;
12001 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12002 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12003 if (op0 == 0)
12004 break;
12005 else if (GET_CODE (rtl) == ZERO_EXTEND
12006 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12007 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12008 < HOST_BITS_PER_WIDE_INT
12009 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12010 to expand zero extend as two shifts instead of
12011 masking. */
12012 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12014 enum machine_mode imode = GET_MODE (XEXP (rtl, 0));
12015 mem_loc_result = op0;
12016 add_loc_descr (&mem_loc_result,
12017 int_loc_descriptor (GET_MODE_MASK (imode)));
12018 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12020 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12022 int shift = DWARF2_ADDR_SIZE
12023 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12024 shift *= BITS_PER_UNIT;
12025 if (GET_CODE (rtl) == SIGN_EXTEND)
12026 op = DW_OP_shra;
12027 else
12028 op = DW_OP_shr;
12029 mem_loc_result = op0;
12030 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12031 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12032 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12033 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12035 else if (!dwarf_strict)
12037 dw_die_ref type_die1, type_die2;
12038 dw_loc_descr_ref cvt;
12040 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12041 GET_CODE (rtl) == ZERO_EXTEND);
12042 if (type_die1 == NULL)
12043 break;
12044 type_die2 = base_type_for_mode (mode, 1);
12045 if (type_die2 == NULL)
12046 break;
12047 mem_loc_result = op0;
12048 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12049 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12050 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12051 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12052 add_loc_descr (&mem_loc_result, cvt);
12053 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12054 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12055 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12056 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12057 add_loc_descr (&mem_loc_result, cvt);
12059 break;
12061 case MEM:
12063 rtx new_rtl = avoid_constant_pool_reference (rtl);
12064 if (new_rtl != rtl)
12066 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12067 initialized);
12068 if (mem_loc_result != NULL)
12069 return mem_loc_result;
12072 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12073 get_address_mode (rtl), mode,
12074 VAR_INIT_STATUS_INITIALIZED);
12075 if (mem_loc_result == NULL)
12076 mem_loc_result = tls_mem_loc_descriptor (rtl);
12077 if (mem_loc_result != NULL)
12079 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12080 || GET_MODE_CLASS (mode) != MODE_INT)
12082 dw_die_ref type_die;
12083 dw_loc_descr_ref deref;
12085 if (dwarf_strict)
12086 return NULL;
12087 type_die
12088 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12089 if (type_die == NULL)
12090 return NULL;
12091 deref = new_loc_descr (DW_OP_GNU_deref_type,
12092 GET_MODE_SIZE (mode), 0);
12093 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12094 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12095 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12096 add_loc_descr (&mem_loc_result, deref);
12098 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12099 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12100 else
12101 add_loc_descr (&mem_loc_result,
12102 new_loc_descr (DW_OP_deref_size,
12103 GET_MODE_SIZE (mode), 0));
12105 break;
12107 case LO_SUM:
12108 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12110 case LABEL_REF:
12111 /* Some ports can transform a symbol ref into a label ref, because
12112 the symbol ref is too far away and has to be dumped into a constant
12113 pool. */
12114 case CONST:
12115 case SYMBOL_REF:
12116 if (GET_MODE_CLASS (mode) != MODE_INT
12117 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12118 #ifdef POINTERS_EXTEND_UNSIGNED
12119 && (mode != Pmode || mem_mode == VOIDmode)
12120 #endif
12122 break;
12123 if (GET_CODE (rtl) == SYMBOL_REF
12124 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12126 dw_loc_descr_ref temp;
12128 /* If this is not defined, we have no way to emit the data. */
12129 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12130 break;
12132 temp = new_addr_loc_descr (rtl, dtprel_true);
12134 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12135 add_loc_descr (&mem_loc_result, temp);
12137 break;
12140 if (!const_ok_for_output (rtl))
12141 break;
12143 symref:
12144 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12145 vec_safe_push (used_rtx_array, rtl);
12146 break;
12148 case CONCAT:
12149 case CONCATN:
12150 case VAR_LOCATION:
12151 case DEBUG_IMPLICIT_PTR:
12152 expansion_failed (NULL_TREE, rtl,
12153 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12154 return 0;
12156 case ENTRY_VALUE:
12157 if (dwarf_strict)
12158 return NULL;
12159 if (REG_P (ENTRY_VALUE_EXP (rtl)))
12161 if (GET_MODE_CLASS (mode) != MODE_INT
12162 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12163 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12164 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12165 else
12167 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12168 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12169 return NULL;
12170 op0 = one_reg_loc_descriptor (dbx_regnum,
12171 VAR_INIT_STATUS_INITIALIZED);
12174 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12175 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12177 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12178 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12179 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12180 return NULL;
12182 else
12183 gcc_unreachable ();
12184 if (op0 == NULL)
12185 return NULL;
12186 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12187 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12188 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12189 break;
12191 case DEBUG_PARAMETER_REF:
12192 mem_loc_result = parameter_ref_descriptor (rtl);
12193 break;
12195 case PRE_MODIFY:
12196 /* Extract the PLUS expression nested inside and fall into
12197 PLUS code below. */
12198 rtl = XEXP (rtl, 1);
12199 goto plus;
12201 case PRE_INC:
12202 case PRE_DEC:
12203 /* Turn these into a PLUS expression and fall into the PLUS code
12204 below. */
12205 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12206 GEN_INT (GET_CODE (rtl) == PRE_INC
12207 ? GET_MODE_UNIT_SIZE (mem_mode)
12208 : -GET_MODE_UNIT_SIZE (mem_mode)));
12210 /* ... fall through ... */
12212 case PLUS:
12213 plus:
12214 if (is_based_loc (rtl)
12215 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12216 || XEXP (rtl, 0) == arg_pointer_rtx
12217 || XEXP (rtl, 0) == frame_pointer_rtx)
12218 && GET_MODE_CLASS (mode) == MODE_INT)
12219 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12220 INTVAL (XEXP (rtl, 1)),
12221 VAR_INIT_STATUS_INITIALIZED);
12222 else
12224 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12225 VAR_INIT_STATUS_INITIALIZED);
12226 if (mem_loc_result == 0)
12227 break;
12229 if (CONST_INT_P (XEXP (rtl, 1))
12230 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12231 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12232 else
12234 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12235 VAR_INIT_STATUS_INITIALIZED);
12236 if (op1 == 0)
12237 return NULL;
12238 add_loc_descr (&mem_loc_result, op1);
12239 add_loc_descr (&mem_loc_result,
12240 new_loc_descr (DW_OP_plus, 0, 0));
12243 break;
12245 /* If a pseudo-reg is optimized away, it is possible for it to
12246 be replaced with a MEM containing a multiply or shift. */
12247 case MINUS:
12248 op = DW_OP_minus;
12249 goto do_binop;
12251 case MULT:
12252 op = DW_OP_mul;
12253 goto do_binop;
12255 case DIV:
12256 if (!dwarf_strict
12257 && GET_MODE_CLASS (mode) == MODE_INT
12258 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12260 mem_loc_result = typed_binop (DW_OP_div, rtl,
12261 base_type_for_mode (mode, 0),
12262 mode, mem_mode);
12263 break;
12265 op = DW_OP_div;
12266 goto do_binop;
12268 case UMOD:
12269 op = DW_OP_mod;
12270 goto do_binop;
12272 case ASHIFT:
12273 op = DW_OP_shl;
12274 goto do_shift;
12276 case ASHIFTRT:
12277 op = DW_OP_shra;
12278 goto do_shift;
12280 case LSHIFTRT:
12281 op = DW_OP_shr;
12282 goto do_shift;
12284 do_shift:
12285 if (GET_MODE_CLASS (mode) != MODE_INT)
12286 break;
12287 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12288 VAR_INIT_STATUS_INITIALIZED);
12290 rtx rtlop1 = XEXP (rtl, 1);
12291 if (GET_MODE (rtlop1) != VOIDmode
12292 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
12293 < GET_MODE_BITSIZE (mode))
12294 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12295 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12296 VAR_INIT_STATUS_INITIALIZED);
12299 if (op0 == 0 || op1 == 0)
12300 break;
12302 mem_loc_result = op0;
12303 add_loc_descr (&mem_loc_result, op1);
12304 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12305 break;
12307 case AND:
12308 op = DW_OP_and;
12309 goto do_binop;
12311 case IOR:
12312 op = DW_OP_or;
12313 goto do_binop;
12315 case XOR:
12316 op = DW_OP_xor;
12317 goto do_binop;
12319 do_binop:
12320 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12321 VAR_INIT_STATUS_INITIALIZED);
12322 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12323 VAR_INIT_STATUS_INITIALIZED);
12325 if (op0 == 0 || op1 == 0)
12326 break;
12328 mem_loc_result = op0;
12329 add_loc_descr (&mem_loc_result, op1);
12330 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12331 break;
12333 case MOD:
12334 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
12336 mem_loc_result = typed_binop (DW_OP_mod, rtl,
12337 base_type_for_mode (mode, 0),
12338 mode, mem_mode);
12339 break;
12342 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12343 VAR_INIT_STATUS_INITIALIZED);
12344 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12345 VAR_INIT_STATUS_INITIALIZED);
12347 if (op0 == 0 || op1 == 0)
12348 break;
12350 mem_loc_result = op0;
12351 add_loc_descr (&mem_loc_result, op1);
12352 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12353 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12354 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
12355 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
12356 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
12357 break;
12359 case UDIV:
12360 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
12362 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
12364 op = DW_OP_div;
12365 goto do_binop;
12367 mem_loc_result = typed_binop (DW_OP_div, rtl,
12368 base_type_for_mode (mode, 1),
12369 mode, mem_mode);
12371 break;
12373 case NOT:
12374 op = DW_OP_not;
12375 goto do_unop;
12377 case ABS:
12378 op = DW_OP_abs;
12379 goto do_unop;
12381 case NEG:
12382 op = DW_OP_neg;
12383 goto do_unop;
12385 do_unop:
12386 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12387 VAR_INIT_STATUS_INITIALIZED);
12389 if (op0 == 0)
12390 break;
12392 mem_loc_result = op0;
12393 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12394 break;
12396 case CONST_INT:
12397 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12398 #ifdef POINTERS_EXTEND_UNSIGNED
12399 || (mode == Pmode
12400 && mem_mode != VOIDmode
12401 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
12402 #endif
12405 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12406 break;
12408 if (!dwarf_strict
12409 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
12410 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
12412 dw_die_ref type_die = base_type_for_mode (mode, 1);
12413 enum machine_mode amode;
12414 if (type_die == NULL)
12415 return NULL;
12416 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
12417 MODE_INT, 0);
12418 if (INTVAL (rtl) >= 0
12419 && amode != BLKmode
12420 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
12421 /* const DW_OP_GNU_convert <XXX> vs.
12422 DW_OP_GNU_const_type <XXX, 1, const>. */
12423 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
12424 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
12426 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12427 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12428 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12429 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12430 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
12431 add_loc_descr (&mem_loc_result, op0);
12432 return mem_loc_result;
12434 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
12435 INTVAL (rtl));
12436 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12437 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12438 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12439 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12440 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12441 else
12443 mem_loc_result->dw_loc_oprnd2.val_class
12444 = dw_val_class_const_double;
12445 mem_loc_result->dw_loc_oprnd2.v.val_double
12446 = double_int::from_shwi (INTVAL (rtl));
12449 break;
12451 case CONST_DOUBLE:
12452 if (!dwarf_strict)
12454 dw_die_ref type_die;
12456 /* Note that a CONST_DOUBLE rtx could represent either an integer
12457 or a floating-point constant. A CONST_DOUBLE is used whenever
12458 the constant requires more than one word in order to be
12459 adequately represented. We output CONST_DOUBLEs as blocks. */
12460 if (mode == VOIDmode
12461 || (GET_MODE (rtl) == VOIDmode
12462 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
12463 break;
12464 type_die = base_type_for_mode (mode,
12465 GET_MODE_CLASS (mode) == MODE_INT);
12466 if (type_die == NULL)
12467 return NULL;
12468 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
12469 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12470 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12471 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12472 if (SCALAR_FLOAT_MODE_P (mode))
12474 unsigned int length = GET_MODE_SIZE (mode);
12475 unsigned char *array
12476 = (unsigned char*) ggc_alloc_atomic (length);
12478 insert_float (rtl, array);
12479 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12480 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12481 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12482 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12484 else
12486 mem_loc_result->dw_loc_oprnd2.val_class
12487 = dw_val_class_const_double;
12488 mem_loc_result->dw_loc_oprnd2.v.val_double
12489 = rtx_to_double_int (rtl);
12492 break;
12494 case EQ:
12495 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
12496 break;
12498 case GE:
12499 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12500 break;
12502 case GT:
12503 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12504 break;
12506 case LE:
12507 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12508 break;
12510 case LT:
12511 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12512 break;
12514 case NE:
12515 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
12516 break;
12518 case GEU:
12519 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12520 break;
12522 case GTU:
12523 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12524 break;
12526 case LEU:
12527 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12528 break;
12530 case LTU:
12531 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12532 break;
12534 case UMIN:
12535 case UMAX:
12536 if (GET_MODE_CLASS (mode) != MODE_INT)
12537 break;
12538 /* FALLTHRU */
12539 case SMIN:
12540 case SMAX:
12541 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
12542 break;
12544 case ZERO_EXTRACT:
12545 case SIGN_EXTRACT:
12546 if (CONST_INT_P (XEXP (rtl, 1))
12547 && CONST_INT_P (XEXP (rtl, 2))
12548 && ((unsigned) INTVAL (XEXP (rtl, 1))
12549 + (unsigned) INTVAL (XEXP (rtl, 2))
12550 <= GET_MODE_BITSIZE (mode))
12551 && GET_MODE_CLASS (mode) == MODE_INT
12552 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12553 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
12555 int shift, size;
12556 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12557 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12558 if (op0 == 0)
12559 break;
12560 if (GET_CODE (rtl) == SIGN_EXTRACT)
12561 op = DW_OP_shra;
12562 else
12563 op = DW_OP_shr;
12564 mem_loc_result = op0;
12565 size = INTVAL (XEXP (rtl, 1));
12566 shift = INTVAL (XEXP (rtl, 2));
12567 if (BITS_BIG_ENDIAN)
12568 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12569 - shift - size;
12570 if (shift + size != (int) DWARF2_ADDR_SIZE)
12572 add_loc_descr (&mem_loc_result,
12573 int_loc_descriptor (DWARF2_ADDR_SIZE
12574 - shift - size));
12575 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12577 if (size != (int) DWARF2_ADDR_SIZE)
12579 add_loc_descr (&mem_loc_result,
12580 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
12581 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12584 break;
12586 case IF_THEN_ELSE:
12588 dw_loc_descr_ref op2, bra_node, drop_node;
12589 op0 = mem_loc_descriptor (XEXP (rtl, 0),
12590 GET_MODE (XEXP (rtl, 0)) == VOIDmode
12591 ? word_mode : GET_MODE (XEXP (rtl, 0)),
12592 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12593 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12594 VAR_INIT_STATUS_INITIALIZED);
12595 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
12596 VAR_INIT_STATUS_INITIALIZED);
12597 if (op0 == NULL || op1 == NULL || op2 == NULL)
12598 break;
12600 mem_loc_result = op1;
12601 add_loc_descr (&mem_loc_result, op2);
12602 add_loc_descr (&mem_loc_result, op0);
12603 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12604 add_loc_descr (&mem_loc_result, bra_node);
12605 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
12606 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12607 add_loc_descr (&mem_loc_result, drop_node);
12608 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12609 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12611 break;
12613 case FLOAT_EXTEND:
12614 case FLOAT_TRUNCATE:
12615 case FLOAT:
12616 case UNSIGNED_FLOAT:
12617 case FIX:
12618 case UNSIGNED_FIX:
12619 if (!dwarf_strict)
12621 dw_die_ref type_die;
12622 dw_loc_descr_ref cvt;
12624 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12625 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12626 if (op0 == NULL)
12627 break;
12628 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
12629 && (GET_CODE (rtl) == FLOAT
12630 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
12631 <= DWARF2_ADDR_SIZE))
12633 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12634 GET_CODE (rtl) == UNSIGNED_FLOAT);
12635 if (type_die == NULL)
12636 break;
12637 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12638 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12639 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12640 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12641 add_loc_descr (&op0, cvt);
12643 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
12644 if (type_die == NULL)
12645 break;
12646 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12647 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12648 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12649 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12650 add_loc_descr (&op0, cvt);
12651 if (GET_MODE_CLASS (mode) == MODE_INT
12652 && (GET_CODE (rtl) == FIX
12653 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
12655 op0 = convert_descriptor_to_mode (mode, op0);
12656 if (op0 == NULL)
12657 break;
12659 mem_loc_result = op0;
12661 break;
12663 case CLZ:
12664 case CTZ:
12665 case FFS:
12666 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
12667 break;
12669 case POPCOUNT:
12670 case PARITY:
12671 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
12672 break;
12674 case BSWAP:
12675 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
12676 break;
12678 case ROTATE:
12679 case ROTATERT:
12680 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
12681 break;
12683 case COMPARE:
12684 /* In theory, we could implement the above. */
12685 /* DWARF cannot represent the unsigned compare operations
12686 natively. */
12687 case SS_MULT:
12688 case US_MULT:
12689 case SS_DIV:
12690 case US_DIV:
12691 case SS_PLUS:
12692 case US_PLUS:
12693 case SS_MINUS:
12694 case US_MINUS:
12695 case SS_NEG:
12696 case US_NEG:
12697 case SS_ABS:
12698 case SS_ASHIFT:
12699 case US_ASHIFT:
12700 case SS_TRUNCATE:
12701 case US_TRUNCATE:
12702 case UNORDERED:
12703 case ORDERED:
12704 case UNEQ:
12705 case UNGE:
12706 case UNGT:
12707 case UNLE:
12708 case UNLT:
12709 case LTGT:
12710 case FRACT_CONVERT:
12711 case UNSIGNED_FRACT_CONVERT:
12712 case SAT_FRACT:
12713 case UNSIGNED_SAT_FRACT:
12714 case SQRT:
12715 case ASM_OPERANDS:
12716 case VEC_MERGE:
12717 case VEC_SELECT:
12718 case VEC_CONCAT:
12719 case VEC_DUPLICATE:
12720 case UNSPEC:
12721 case HIGH:
12722 case FMA:
12723 case STRICT_LOW_PART:
12724 case CONST_VECTOR:
12725 case CONST_FIXED:
12726 case CLRSB:
12727 case CLOBBER:
12728 /* If delegitimize_address couldn't do anything with the UNSPEC, we
12729 can't express it in the debug info. This can happen e.g. with some
12730 TLS UNSPECs. */
12731 break;
12733 case CONST_STRING:
12734 resolve_one_addr (&rtl, NULL);
12735 goto symref;
12737 default:
12738 #ifdef ENABLE_CHECKING
12739 print_rtl (stderr, rtl);
12740 gcc_unreachable ();
12741 #else
12742 break;
12743 #endif
12746 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12747 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12749 return mem_loc_result;
12752 /* Return a descriptor that describes the concatenation of two locations.
12753 This is typically a complex variable. */
12755 static dw_loc_descr_ref
12756 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
12758 dw_loc_descr_ref cc_loc_result = NULL;
12759 dw_loc_descr_ref x0_ref
12760 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12761 dw_loc_descr_ref x1_ref
12762 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12764 if (x0_ref == 0 || x1_ref == 0)
12765 return 0;
12767 cc_loc_result = x0_ref;
12768 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
12770 add_loc_descr (&cc_loc_result, x1_ref);
12771 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
12773 if (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 /* Return a descriptor that describes the concatenation of N
12780 locations. */
12782 static dw_loc_descr_ref
12783 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
12785 unsigned int i;
12786 dw_loc_descr_ref cc_loc_result = NULL;
12787 unsigned int n = XVECLEN (concatn, 0);
12789 for (i = 0; i < n; ++i)
12791 dw_loc_descr_ref ref;
12792 rtx x = XVECEXP (concatn, 0, i);
12794 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12795 if (ref == NULL)
12796 return NULL;
12798 add_loc_descr (&cc_loc_result, ref);
12799 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
12802 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12803 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12805 return cc_loc_result;
12808 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
12809 for DEBUG_IMPLICIT_PTR RTL. */
12811 static dw_loc_descr_ref
12812 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
12814 dw_loc_descr_ref ret;
12815 dw_die_ref ref;
12817 if (dwarf_strict)
12818 return NULL;
12819 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
12820 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
12821 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
12822 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
12823 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
12824 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
12825 if (ref)
12827 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12828 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12829 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12831 else
12833 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12834 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
12836 return ret;
12839 /* Output a proper Dwarf location descriptor for a variable or parameter
12840 which is either allocated in a register or in a memory location. For a
12841 register, we just generate an OP_REG and the register number. For a
12842 memory location we provide a Dwarf postfix expression describing how to
12843 generate the (dynamic) address of the object onto the address stack.
12845 MODE is mode of the decl if this loc_descriptor is going to be used in
12846 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
12847 allowed, VOIDmode otherwise.
12849 If we don't know how to describe it, return 0. */
12851 static dw_loc_descr_ref
12852 loc_descriptor (rtx rtl, enum machine_mode mode,
12853 enum var_init_status initialized)
12855 dw_loc_descr_ref loc_result = NULL;
12857 switch (GET_CODE (rtl))
12859 case SUBREG:
12860 /* The case of a subreg may arise when we have a local (register)
12861 variable or a formal (register) parameter which doesn't quite fill
12862 up an entire register. For now, just assume that it is
12863 legitimate to make the Dwarf info refer to the whole register which
12864 contains the given subreg. */
12865 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
12866 loc_result = loc_descriptor (SUBREG_REG (rtl),
12867 GET_MODE (SUBREG_REG (rtl)), initialized);
12868 else
12869 goto do_default;
12870 break;
12872 case REG:
12873 loc_result = reg_loc_descriptor (rtl, initialized);
12874 break;
12876 case MEM:
12877 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
12878 GET_MODE (rtl), initialized);
12879 if (loc_result == NULL)
12880 loc_result = tls_mem_loc_descriptor (rtl);
12881 if (loc_result == NULL)
12883 rtx new_rtl = avoid_constant_pool_reference (rtl);
12884 if (new_rtl != rtl)
12885 loc_result = loc_descriptor (new_rtl, mode, initialized);
12887 break;
12889 case CONCAT:
12890 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
12891 initialized);
12892 break;
12894 case CONCATN:
12895 loc_result = concatn_loc_descriptor (rtl, initialized);
12896 break;
12898 case VAR_LOCATION:
12899 /* Single part. */
12900 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
12902 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
12903 if (GET_CODE (loc) == EXPR_LIST)
12904 loc = XEXP (loc, 0);
12905 loc_result = loc_descriptor (loc, mode, initialized);
12906 break;
12909 rtl = XEXP (rtl, 1);
12910 /* FALLTHRU */
12912 case PARALLEL:
12914 rtvec par_elems = XVEC (rtl, 0);
12915 int num_elem = GET_NUM_ELEM (par_elems);
12916 enum machine_mode mode;
12917 int i;
12919 /* Create the first one, so we have something to add to. */
12920 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
12921 VOIDmode, initialized);
12922 if (loc_result == NULL)
12923 return NULL;
12924 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
12925 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
12926 for (i = 1; i < num_elem; i++)
12928 dw_loc_descr_ref temp;
12930 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
12931 VOIDmode, initialized);
12932 if (temp == NULL)
12933 return NULL;
12934 add_loc_descr (&loc_result, temp);
12935 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
12936 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
12939 break;
12941 case CONST_INT:
12942 if (mode != VOIDmode && mode != BLKmode)
12943 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
12944 INTVAL (rtl));
12945 break;
12947 case CONST_DOUBLE:
12948 if (mode == VOIDmode)
12949 mode = GET_MODE (rtl);
12951 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
12953 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
12955 /* Note that a CONST_DOUBLE rtx could represent either an integer
12956 or a floating-point constant. A CONST_DOUBLE is used whenever
12957 the constant requires more than one word in order to be
12958 adequately represented. We output CONST_DOUBLEs as blocks. */
12959 loc_result = new_loc_descr (DW_OP_implicit_value,
12960 GET_MODE_SIZE (mode), 0);
12961 if (SCALAR_FLOAT_MODE_P (mode))
12963 unsigned int length = GET_MODE_SIZE (mode);
12964 unsigned char *array
12965 = (unsigned char*) ggc_alloc_atomic (length);
12967 insert_float (rtl, array);
12968 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12969 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12970 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12971 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12973 else
12975 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
12976 loc_result->dw_loc_oprnd2.v.val_double
12977 = rtx_to_double_int (rtl);
12980 break;
12982 case CONST_VECTOR:
12983 if (mode == VOIDmode)
12984 mode = GET_MODE (rtl);
12986 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
12988 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
12989 unsigned int length = CONST_VECTOR_NUNITS (rtl);
12990 unsigned char *array = (unsigned char *)
12991 ggc_alloc_atomic (length * elt_size);
12992 unsigned int i;
12993 unsigned char *p;
12995 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
12996 switch (GET_MODE_CLASS (mode))
12998 case MODE_VECTOR_INT:
12999 for (i = 0, p = array; i < length; i++, p += elt_size)
13001 rtx elt = CONST_VECTOR_ELT (rtl, i);
13002 double_int val = rtx_to_double_int (elt);
13004 if (elt_size <= sizeof (HOST_WIDE_INT))
13005 insert_int (val.to_shwi (), elt_size, p);
13006 else
13008 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
13009 insert_double (val, p);
13012 break;
13014 case MODE_VECTOR_FLOAT:
13015 for (i = 0, p = array; i < length; i++, p += elt_size)
13017 rtx elt = CONST_VECTOR_ELT (rtl, i);
13018 insert_float (elt, p);
13020 break;
13022 default:
13023 gcc_unreachable ();
13026 loc_result = new_loc_descr (DW_OP_implicit_value,
13027 length * elt_size, 0);
13028 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13029 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13030 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13031 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13033 break;
13035 case CONST:
13036 if (mode == VOIDmode
13037 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13038 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13039 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13041 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13042 break;
13044 /* FALLTHROUGH */
13045 case SYMBOL_REF:
13046 if (!const_ok_for_output (rtl))
13047 break;
13048 case LABEL_REF:
13049 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13050 && (dwarf_version >= 4 || !dwarf_strict))
13052 loc_result = new_addr_loc_descr (rtl, dtprel_false);
13053 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13054 vec_safe_push (used_rtx_array, rtl);
13056 break;
13058 case DEBUG_IMPLICIT_PTR:
13059 loc_result = implicit_ptr_descriptor (rtl, 0);
13060 break;
13062 case PLUS:
13063 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13064 && CONST_INT_P (XEXP (rtl, 1)))
13066 loc_result
13067 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13068 break;
13070 /* FALLTHRU */
13071 do_default:
13072 default:
13073 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13074 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13075 && dwarf_version >= 4)
13076 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13078 /* Value expression. */
13079 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13080 if (loc_result)
13081 add_loc_descr (&loc_result,
13082 new_loc_descr (DW_OP_stack_value, 0, 0));
13084 break;
13087 return loc_result;
13090 /* We need to figure out what section we should use as the base for the
13091 address ranges where a given location is valid.
13092 1. If this particular DECL has a section associated with it, use that.
13093 2. If this function has a section associated with it, use that.
13094 3. Otherwise, use the text section.
13095 XXX: If you split a variable across multiple sections, we won't notice. */
13097 static const char *
13098 secname_for_decl (const_tree decl)
13100 const char *secname;
13102 if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
13104 tree sectree = DECL_SECTION_NAME (decl);
13105 secname = TREE_STRING_POINTER (sectree);
13107 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13109 tree sectree = DECL_SECTION_NAME (current_function_decl);
13110 secname = TREE_STRING_POINTER (sectree);
13112 else if (cfun && in_cold_section_p)
13113 secname = crtl->subsections.cold_section_label;
13114 else
13115 secname = text_section_label;
13117 return secname;
13120 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
13122 static bool
13123 decl_by_reference_p (tree decl)
13125 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13126 || TREE_CODE (decl) == VAR_DECL)
13127 && DECL_BY_REFERENCE (decl));
13130 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13131 for VARLOC. */
13133 static dw_loc_descr_ref
13134 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13135 enum var_init_status initialized)
13137 int have_address = 0;
13138 dw_loc_descr_ref descr;
13139 enum machine_mode mode;
13141 if (want_address != 2)
13143 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13144 /* Single part. */
13145 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13147 varloc = PAT_VAR_LOCATION_LOC (varloc);
13148 if (GET_CODE (varloc) == EXPR_LIST)
13149 varloc = XEXP (varloc, 0);
13150 mode = GET_MODE (varloc);
13151 if (MEM_P (varloc))
13153 rtx addr = XEXP (varloc, 0);
13154 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13155 mode, initialized);
13156 if (descr)
13157 have_address = 1;
13158 else
13160 rtx x = avoid_constant_pool_reference (varloc);
13161 if (x != varloc)
13162 descr = mem_loc_descriptor (x, mode, VOIDmode,
13163 initialized);
13166 else
13167 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13169 else
13170 return 0;
13172 else
13174 if (GET_CODE (varloc) == VAR_LOCATION)
13175 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13176 else
13177 mode = DECL_MODE (loc);
13178 descr = loc_descriptor (varloc, mode, initialized);
13179 have_address = 1;
13182 if (!descr)
13183 return 0;
13185 if (want_address == 2 && !have_address
13186 && (dwarf_version >= 4 || !dwarf_strict))
13188 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13190 expansion_failed (loc, NULL_RTX,
13191 "DWARF address size mismatch");
13192 return 0;
13194 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13195 have_address = 1;
13197 /* Show if we can't fill the request for an address. */
13198 if (want_address && !have_address)
13200 expansion_failed (loc, NULL_RTX,
13201 "Want address and only have value");
13202 return 0;
13205 /* If we've got an address and don't want one, dereference. */
13206 if (!want_address && have_address)
13208 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13209 enum dwarf_location_atom op;
13211 if (size > DWARF2_ADDR_SIZE || size == -1)
13213 expansion_failed (loc, NULL_RTX,
13214 "DWARF address size mismatch");
13215 return 0;
13217 else if (size == DWARF2_ADDR_SIZE)
13218 op = DW_OP_deref;
13219 else
13220 op = DW_OP_deref_size;
13222 add_loc_descr (&descr, new_loc_descr (op, size, 0));
13225 return descr;
13228 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
13229 if it is not possible. */
13231 static dw_loc_descr_ref
13232 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
13234 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
13235 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
13236 else if (dwarf_version >= 3 || !dwarf_strict)
13237 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
13238 else
13239 return NULL;
13242 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13243 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
13245 static dw_loc_descr_ref
13246 dw_sra_loc_expr (tree decl, rtx loc)
13248 rtx p;
13249 unsigned int padsize = 0;
13250 dw_loc_descr_ref descr, *descr_tail;
13251 unsigned HOST_WIDE_INT decl_size;
13252 rtx varloc;
13253 enum var_init_status initialized;
13255 if (DECL_SIZE (decl) == NULL
13256 || !host_integerp (DECL_SIZE (decl), 1))
13257 return NULL;
13259 decl_size = tree_low_cst (DECL_SIZE (decl), 1);
13260 descr = NULL;
13261 descr_tail = &descr;
13263 for (p = loc; p; p = XEXP (p, 1))
13265 unsigned int bitsize = decl_piece_bitsize (p);
13266 rtx loc_note = *decl_piece_varloc_ptr (p);
13267 dw_loc_descr_ref cur_descr;
13268 dw_loc_descr_ref *tail, last = NULL;
13269 unsigned int opsize = 0;
13271 if (loc_note == NULL_RTX
13272 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
13274 padsize += bitsize;
13275 continue;
13277 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
13278 varloc = NOTE_VAR_LOCATION (loc_note);
13279 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
13280 if (cur_descr == NULL)
13282 padsize += bitsize;
13283 continue;
13286 /* Check that cur_descr either doesn't use
13287 DW_OP_*piece operations, or their sum is equal
13288 to bitsize. Otherwise we can't embed it. */
13289 for (tail = &cur_descr; *tail != NULL;
13290 tail = &(*tail)->dw_loc_next)
13291 if ((*tail)->dw_loc_opc == DW_OP_piece)
13293 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
13294 * BITS_PER_UNIT;
13295 last = *tail;
13297 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
13299 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
13300 last = *tail;
13303 if (last != NULL && opsize != bitsize)
13305 padsize += bitsize;
13306 continue;
13309 /* If there is a hole, add DW_OP_*piece after empty DWARF
13310 expression, which means that those bits are optimized out. */
13311 if (padsize)
13313 if (padsize > decl_size)
13314 return NULL;
13315 decl_size -= padsize;
13316 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
13317 if (*descr_tail == NULL)
13318 return NULL;
13319 descr_tail = &(*descr_tail)->dw_loc_next;
13320 padsize = 0;
13322 *descr_tail = cur_descr;
13323 descr_tail = tail;
13324 if (bitsize > decl_size)
13325 return NULL;
13326 decl_size -= bitsize;
13327 if (last == NULL)
13329 HOST_WIDE_INT offset = 0;
13330 if (GET_CODE (varloc) == VAR_LOCATION
13331 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13333 varloc = PAT_VAR_LOCATION_LOC (varloc);
13334 if (GET_CODE (varloc) == EXPR_LIST)
13335 varloc = XEXP (varloc, 0);
13339 if (GET_CODE (varloc) == CONST
13340 || GET_CODE (varloc) == SIGN_EXTEND
13341 || GET_CODE (varloc) == ZERO_EXTEND)
13342 varloc = XEXP (varloc, 0);
13343 else if (GET_CODE (varloc) == SUBREG)
13344 varloc = SUBREG_REG (varloc);
13345 else
13346 break;
13348 while (1);
13349 /* DW_OP_bit_size offset should be zero for register
13350 or implicit location descriptions and empty location
13351 descriptions, but for memory addresses needs big endian
13352 adjustment. */
13353 if (MEM_P (varloc))
13355 unsigned HOST_WIDE_INT memsize
13356 = MEM_SIZE (varloc) * BITS_PER_UNIT;
13357 if (memsize != bitsize)
13359 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
13360 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
13361 return NULL;
13362 if (memsize < bitsize)
13363 return NULL;
13364 if (BITS_BIG_ENDIAN)
13365 offset = memsize - bitsize;
13369 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
13370 if (*descr_tail == NULL)
13371 return NULL;
13372 descr_tail = &(*descr_tail)->dw_loc_next;
13376 /* If there were any non-empty expressions, add padding till the end of
13377 the decl. */
13378 if (descr != NULL && decl_size != 0)
13380 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
13381 if (*descr_tail == NULL)
13382 return NULL;
13384 return descr;
13387 /* Return the dwarf representation of the location list LOC_LIST of
13388 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
13389 function. */
13391 static dw_loc_list_ref
13392 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
13394 const char *endname, *secname;
13395 rtx varloc;
13396 enum var_init_status initialized;
13397 struct var_loc_node *node;
13398 dw_loc_descr_ref descr;
13399 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13400 dw_loc_list_ref list = NULL;
13401 dw_loc_list_ref *listp = &list;
13403 /* Now that we know what section we are using for a base,
13404 actually construct the list of locations.
13405 The first location information is what is passed to the
13406 function that creates the location list, and the remaining
13407 locations just get added on to that list.
13408 Note that we only know the start address for a location
13409 (IE location changes), so to build the range, we use
13410 the range [current location start, next location start].
13411 This means we have to special case the last node, and generate
13412 a range of [last location start, end of function label]. */
13414 secname = secname_for_decl (decl);
13416 for (node = loc_list->first; node; node = node->next)
13417 if (GET_CODE (node->loc) == EXPR_LIST
13418 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
13420 if (GET_CODE (node->loc) == EXPR_LIST)
13422 /* This requires DW_OP_{,bit_}piece, which is not usable
13423 inside DWARF expressions. */
13424 if (want_address != 2)
13425 continue;
13426 descr = dw_sra_loc_expr (decl, node->loc);
13427 if (descr == NULL)
13428 continue;
13430 else
13432 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13433 varloc = NOTE_VAR_LOCATION (node->loc);
13434 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
13436 if (descr)
13438 bool range_across_switch = false;
13439 /* If section switch happens in between node->label
13440 and node->next->label (or end of function) and
13441 we can't emit it as a single entry list,
13442 emit two ranges, first one ending at the end
13443 of first partition and second one starting at the
13444 beginning of second partition. */
13445 if (node == loc_list->last_before_switch
13446 && (node != loc_list->first || loc_list->first->next)
13447 && current_function_decl)
13449 endname = cfun->fde->dw_fde_end;
13450 range_across_switch = true;
13452 /* The variable has a location between NODE->LABEL and
13453 NODE->NEXT->LABEL. */
13454 else if (node->next)
13455 endname = node->next->label;
13456 /* If the variable has a location at the last label
13457 it keeps its location until the end of function. */
13458 else if (!current_function_decl)
13459 endname = text_end_label;
13460 else
13462 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13463 current_function_funcdef_no);
13464 endname = ggc_strdup (label_id);
13467 *listp = new_loc_list (descr, node->label, endname, secname);
13468 if (TREE_CODE (decl) == PARM_DECL
13469 && node == loc_list->first
13470 && GET_CODE (node->loc) == NOTE
13471 && strcmp (node->label, endname) == 0)
13472 (*listp)->force = true;
13473 listp = &(*listp)->dw_loc_next;
13475 if (range_across_switch)
13477 if (GET_CODE (node->loc) == EXPR_LIST)
13478 descr = dw_sra_loc_expr (decl, node->loc);
13479 else
13481 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13482 varloc = NOTE_VAR_LOCATION (node->loc);
13483 descr = dw_loc_list_1 (decl, varloc, want_address,
13484 initialized);
13486 gcc_assert (descr);
13487 /* The variable has a location between NODE->LABEL and
13488 NODE->NEXT->LABEL. */
13489 if (node->next)
13490 endname = node->next->label;
13491 else
13492 endname = cfun->fde->dw_fde_second_end;
13493 *listp = new_loc_list (descr,
13494 cfun->fde->dw_fde_second_begin,
13495 endname, secname);
13496 listp = &(*listp)->dw_loc_next;
13501 /* Try to avoid the overhead of a location list emitting a location
13502 expression instead, but only if we didn't have more than one
13503 location entry in the first place. If some entries were not
13504 representable, we don't want to pretend a single entry that was
13505 applies to the entire scope in which the variable is
13506 available. */
13507 if (list && loc_list->first->next)
13508 gen_llsym (list);
13510 return list;
13513 /* Return if the loc_list has only single element and thus can be represented
13514 as location description. */
13516 static bool
13517 single_element_loc_list_p (dw_loc_list_ref list)
13519 gcc_assert (!list->dw_loc_next || list->ll_symbol);
13520 return !list->ll_symbol;
13523 /* To each location in list LIST add loc descr REF. */
13525 static void
13526 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
13528 dw_loc_descr_ref copy;
13529 add_loc_descr (&list->expr, ref);
13530 list = list->dw_loc_next;
13531 while (list)
13533 copy = ggc_alloc_dw_loc_descr_node ();
13534 memcpy (copy, ref, sizeof (dw_loc_descr_node));
13535 add_loc_descr (&list->expr, copy);
13536 while (copy->dw_loc_next)
13538 dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
13539 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
13540 copy->dw_loc_next = new_copy;
13541 copy = new_copy;
13543 list = list->dw_loc_next;
13547 /* Given two lists RET and LIST
13548 produce location list that is result of adding expression in LIST
13549 to expression in RET on each position in program.
13550 Might be destructive on both RET and LIST.
13552 TODO: We handle only simple cases of RET or LIST having at most one
13553 element. General case would inolve sorting the lists in program order
13554 and merging them that will need some additional work.
13555 Adding that will improve quality of debug info especially for SRA-ed
13556 structures. */
13558 static void
13559 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
13561 if (!list)
13562 return;
13563 if (!*ret)
13565 *ret = list;
13566 return;
13568 if (!list->dw_loc_next)
13570 add_loc_descr_to_each (*ret, list->expr);
13571 return;
13573 if (!(*ret)->dw_loc_next)
13575 add_loc_descr_to_each (list, (*ret)->expr);
13576 *ret = list;
13577 return;
13579 expansion_failed (NULL_TREE, NULL_RTX,
13580 "Don't know how to merge two non-trivial"
13581 " location lists.\n");
13582 *ret = NULL;
13583 return;
13586 /* LOC is constant expression. Try a luck, look it up in constant
13587 pool and return its loc_descr of its address. */
13589 static dw_loc_descr_ref
13590 cst_pool_loc_descr (tree loc)
13592 /* Get an RTL for this, if something has been emitted. */
13593 rtx rtl = lookup_constant_def (loc);
13595 if (!rtl || !MEM_P (rtl))
13597 gcc_assert (!rtl);
13598 return 0;
13600 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
13602 /* TODO: We might get more coverage if we was actually delaying expansion
13603 of all expressions till end of compilation when constant pools are fully
13604 populated. */
13605 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
13607 expansion_failed (loc, NULL_RTX,
13608 "CST value in contant pool but not marked.");
13609 return 0;
13611 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13612 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
13615 /* Return dw_loc_list representing address of addr_expr LOC
13616 by looking for inner INDIRECT_REF expression and turning
13617 it into simple arithmetics. */
13619 static dw_loc_list_ref
13620 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
13622 tree obj, offset;
13623 HOST_WIDE_INT bitsize, bitpos, bytepos;
13624 enum machine_mode mode;
13625 int unsignedp, volatilep = 0;
13626 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13628 obj = get_inner_reference (TREE_OPERAND (loc, 0),
13629 &bitsize, &bitpos, &offset, &mode,
13630 &unsignedp, &volatilep, false);
13631 STRIP_NOPS (obj);
13632 if (bitpos % BITS_PER_UNIT)
13634 expansion_failed (loc, NULL_RTX, "bitfield access");
13635 return 0;
13637 if (!INDIRECT_REF_P (obj))
13639 expansion_failed (obj,
13640 NULL_RTX, "no indirect ref in inner refrence");
13641 return 0;
13643 if (!offset && !bitpos)
13644 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
13645 else if (toplev
13646 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
13647 && (dwarf_version >= 4 || !dwarf_strict))
13649 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
13650 if (!list_ret)
13651 return 0;
13652 if (offset)
13654 /* Variable offset. */
13655 list_ret1 = loc_list_from_tree (offset, 0);
13656 if (list_ret1 == 0)
13657 return 0;
13658 add_loc_list (&list_ret, list_ret1);
13659 if (!list_ret)
13660 return 0;
13661 add_loc_descr_to_each (list_ret,
13662 new_loc_descr (DW_OP_plus, 0, 0));
13664 bytepos = bitpos / BITS_PER_UNIT;
13665 if (bytepos > 0)
13666 add_loc_descr_to_each (list_ret,
13667 new_loc_descr (DW_OP_plus_uconst,
13668 bytepos, 0));
13669 else if (bytepos < 0)
13670 loc_list_plus_const (list_ret, bytepos);
13671 add_loc_descr_to_each (list_ret,
13672 new_loc_descr (DW_OP_stack_value, 0, 0));
13674 return list_ret;
13678 /* Generate Dwarf location list representing LOC.
13679 If WANT_ADDRESS is false, expression computing LOC will be computed
13680 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
13681 if WANT_ADDRESS is 2, expression computing address useable in location
13682 will be returned (i.e. DW_OP_reg can be used
13683 to refer to register values). */
13685 static dw_loc_list_ref
13686 loc_list_from_tree (tree loc, int want_address)
13688 dw_loc_descr_ref ret = NULL, ret1 = NULL;
13689 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13690 int have_address = 0;
13691 enum dwarf_location_atom op;
13693 /* ??? Most of the time we do not take proper care for sign/zero
13694 extending the values properly. Hopefully this won't be a real
13695 problem... */
13697 switch (TREE_CODE (loc))
13699 case ERROR_MARK:
13700 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
13701 return 0;
13703 case PLACEHOLDER_EXPR:
13704 /* This case involves extracting fields from an object to determine the
13705 position of other fields. We don't try to encode this here. The
13706 only user of this is Ada, which encodes the needed information using
13707 the names of types. */
13708 expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
13709 return 0;
13711 case CALL_EXPR:
13712 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
13713 /* There are no opcodes for these operations. */
13714 return 0;
13716 case PREINCREMENT_EXPR:
13717 case PREDECREMENT_EXPR:
13718 case POSTINCREMENT_EXPR:
13719 case POSTDECREMENT_EXPR:
13720 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
13721 /* There are no opcodes for these operations. */
13722 return 0;
13724 case ADDR_EXPR:
13725 /* If we already want an address, see if there is INDIRECT_REF inside
13726 e.g. for &this->field. */
13727 if (want_address)
13729 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
13730 (loc, want_address == 2);
13731 if (list_ret)
13732 have_address = 1;
13733 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
13734 && (ret = cst_pool_loc_descr (loc)))
13735 have_address = 1;
13737 /* Otherwise, process the argument and look for the address. */
13738 if (!list_ret && !ret)
13739 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
13740 else
13742 if (want_address)
13743 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
13744 return NULL;
13746 break;
13748 case VAR_DECL:
13749 if (DECL_THREAD_LOCAL_P (loc))
13751 rtx rtl;
13752 enum dwarf_location_atom tls_op;
13753 enum dtprel_bool dtprel = dtprel_false;
13755 if (targetm.have_tls)
13757 /* If this is not defined, we have no way to emit the
13758 data. */
13759 if (!targetm.asm_out.output_dwarf_dtprel)
13760 return 0;
13762 /* The way DW_OP_GNU_push_tls_address is specified, we
13763 can only look up addresses of objects in the current
13764 module. We used DW_OP_addr as first op, but that's
13765 wrong, because DW_OP_addr is relocated by the debug
13766 info consumer, while DW_OP_GNU_push_tls_address
13767 operand shouldn't be. */
13768 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
13769 return 0;
13770 dtprel = dtprel_true;
13771 tls_op = DW_OP_GNU_push_tls_address;
13773 else
13775 if (!targetm.emutls.debug_form_tls_address
13776 || !(dwarf_version >= 3 || !dwarf_strict))
13777 return 0;
13778 /* We stuffed the control variable into the DECL_VALUE_EXPR
13779 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
13780 no longer appear in gimple code. We used the control
13781 variable in specific so that we could pick it up here. */
13782 loc = DECL_VALUE_EXPR (loc);
13783 tls_op = DW_OP_form_tls_address;
13786 rtl = rtl_for_decl_location (loc);
13787 if (rtl == NULL_RTX)
13788 return 0;
13790 if (!MEM_P (rtl))
13791 return 0;
13792 rtl = XEXP (rtl, 0);
13793 if (! CONSTANT_P (rtl))
13794 return 0;
13796 ret = new_addr_loc_descr (rtl, dtprel);
13797 ret1 = new_loc_descr (tls_op, 0, 0);
13798 add_loc_descr (&ret, ret1);
13800 have_address = 1;
13801 break;
13803 /* FALLTHRU */
13805 case PARM_DECL:
13806 case RESULT_DECL:
13807 if (DECL_HAS_VALUE_EXPR_P (loc))
13808 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
13809 want_address);
13810 /* FALLTHRU */
13812 case FUNCTION_DECL:
13814 rtx rtl;
13815 var_loc_list *loc_list = lookup_decl_loc (loc);
13817 if (loc_list && loc_list->first)
13819 list_ret = dw_loc_list (loc_list, loc, want_address);
13820 have_address = want_address != 0;
13821 break;
13823 rtl = rtl_for_decl_location (loc);
13824 if (rtl == NULL_RTX)
13826 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
13827 return 0;
13829 else if (CONST_INT_P (rtl))
13831 HOST_WIDE_INT val = INTVAL (rtl);
13832 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13833 val &= GET_MODE_MASK (DECL_MODE (loc));
13834 ret = int_loc_descriptor (val);
13836 else if (GET_CODE (rtl) == CONST_STRING)
13838 expansion_failed (loc, NULL_RTX, "CONST_STRING");
13839 return 0;
13841 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
13842 ret = new_addr_loc_descr (rtl, dtprel_false);
13843 else
13845 enum machine_mode mode, mem_mode;
13847 /* Certain constructs can only be represented at top-level. */
13848 if (want_address == 2)
13850 ret = loc_descriptor (rtl, VOIDmode,
13851 VAR_INIT_STATUS_INITIALIZED);
13852 have_address = 1;
13854 else
13856 mode = GET_MODE (rtl);
13857 mem_mode = VOIDmode;
13858 if (MEM_P (rtl))
13860 mem_mode = mode;
13861 mode = get_address_mode (rtl);
13862 rtl = XEXP (rtl, 0);
13863 have_address = 1;
13865 ret = mem_loc_descriptor (rtl, mode, mem_mode,
13866 VAR_INIT_STATUS_INITIALIZED);
13868 if (!ret)
13869 expansion_failed (loc, rtl,
13870 "failed to produce loc descriptor for rtl");
13873 break;
13875 case MEM_REF:
13876 /* ??? FIXME. */
13877 if (!integer_zerop (TREE_OPERAND (loc, 1)))
13878 return 0;
13879 /* Fallthru. */
13880 case INDIRECT_REF:
13881 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13882 have_address = 1;
13883 break;
13885 case TARGET_MEM_REF:
13886 case SSA_NAME:
13887 return NULL;
13889 case COMPOUND_EXPR:
13890 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
13892 CASE_CONVERT:
13893 case VIEW_CONVERT_EXPR:
13894 case SAVE_EXPR:
13895 case MODIFY_EXPR:
13896 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
13898 case COMPONENT_REF:
13899 case BIT_FIELD_REF:
13900 case ARRAY_REF:
13901 case ARRAY_RANGE_REF:
13902 case REALPART_EXPR:
13903 case IMAGPART_EXPR:
13905 tree obj, offset;
13906 HOST_WIDE_INT bitsize, bitpos, bytepos;
13907 enum machine_mode mode;
13908 int unsignedp, volatilep = 0;
13910 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
13911 &unsignedp, &volatilep, false);
13913 gcc_assert (obj != loc);
13915 list_ret = loc_list_from_tree (obj,
13916 want_address == 2
13917 && !bitpos && !offset ? 2 : 1);
13918 /* TODO: We can extract value of the small expression via shifting even
13919 for nonzero bitpos. */
13920 if (list_ret == 0)
13921 return 0;
13922 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
13924 expansion_failed (loc, NULL_RTX,
13925 "bitfield access");
13926 return 0;
13929 if (offset != NULL_TREE)
13931 /* Variable offset. */
13932 list_ret1 = loc_list_from_tree (offset, 0);
13933 if (list_ret1 == 0)
13934 return 0;
13935 add_loc_list (&list_ret, list_ret1);
13936 if (!list_ret)
13937 return 0;
13938 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
13941 bytepos = bitpos / BITS_PER_UNIT;
13942 if (bytepos > 0)
13943 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
13944 else if (bytepos < 0)
13945 loc_list_plus_const (list_ret, bytepos);
13947 have_address = 1;
13948 break;
13951 case INTEGER_CST:
13952 if ((want_address || !host_integerp (loc, 0))
13953 && (ret = cst_pool_loc_descr (loc)))
13954 have_address = 1;
13955 else if (want_address == 2
13956 && host_integerp (loc, 0)
13957 && (ret = address_of_int_loc_descriptor
13958 (int_size_in_bytes (TREE_TYPE (loc)),
13959 tree_low_cst (loc, 0))))
13960 have_address = 1;
13961 else if (host_integerp (loc, 0))
13962 ret = int_loc_descriptor (tree_low_cst (loc, 0));
13963 else
13965 expansion_failed (loc, NULL_RTX,
13966 "Integer operand is not host integer");
13967 return 0;
13969 break;
13971 case CONSTRUCTOR:
13972 case REAL_CST:
13973 case STRING_CST:
13974 case COMPLEX_CST:
13975 if ((ret = cst_pool_loc_descr (loc)))
13976 have_address = 1;
13977 else
13978 /* We can construct small constants here using int_loc_descriptor. */
13979 expansion_failed (loc, NULL_RTX,
13980 "constructor or constant not in constant pool");
13981 break;
13983 case TRUTH_AND_EXPR:
13984 case TRUTH_ANDIF_EXPR:
13985 case BIT_AND_EXPR:
13986 op = DW_OP_and;
13987 goto do_binop;
13989 case TRUTH_XOR_EXPR:
13990 case BIT_XOR_EXPR:
13991 op = DW_OP_xor;
13992 goto do_binop;
13994 case TRUTH_OR_EXPR:
13995 case TRUTH_ORIF_EXPR:
13996 case BIT_IOR_EXPR:
13997 op = DW_OP_or;
13998 goto do_binop;
14000 case FLOOR_DIV_EXPR:
14001 case CEIL_DIV_EXPR:
14002 case ROUND_DIV_EXPR:
14003 case TRUNC_DIV_EXPR:
14004 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14005 return 0;
14006 op = DW_OP_div;
14007 goto do_binop;
14009 case MINUS_EXPR:
14010 op = DW_OP_minus;
14011 goto do_binop;
14013 case FLOOR_MOD_EXPR:
14014 case CEIL_MOD_EXPR:
14015 case ROUND_MOD_EXPR:
14016 case TRUNC_MOD_EXPR:
14017 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14019 op = DW_OP_mod;
14020 goto do_binop;
14022 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14023 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14024 if (list_ret == 0 || list_ret1 == 0)
14025 return 0;
14027 add_loc_list (&list_ret, list_ret1);
14028 if (list_ret == 0)
14029 return 0;
14030 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14031 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14032 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14033 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14034 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14035 break;
14037 case MULT_EXPR:
14038 op = DW_OP_mul;
14039 goto do_binop;
14041 case LSHIFT_EXPR:
14042 op = DW_OP_shl;
14043 goto do_binop;
14045 case RSHIFT_EXPR:
14046 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14047 goto do_binop;
14049 case POINTER_PLUS_EXPR:
14050 case PLUS_EXPR:
14051 if (host_integerp (TREE_OPERAND (loc, 1), 0))
14053 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14054 if (list_ret == 0)
14055 return 0;
14057 loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
14058 break;
14061 op = DW_OP_plus;
14062 goto do_binop;
14064 case LE_EXPR:
14065 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14066 return 0;
14068 op = DW_OP_le;
14069 goto do_binop;
14071 case GE_EXPR:
14072 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14073 return 0;
14075 op = DW_OP_ge;
14076 goto do_binop;
14078 case LT_EXPR:
14079 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14080 return 0;
14082 op = DW_OP_lt;
14083 goto do_binop;
14085 case GT_EXPR:
14086 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14087 return 0;
14089 op = DW_OP_gt;
14090 goto do_binop;
14092 case EQ_EXPR:
14093 op = DW_OP_eq;
14094 goto do_binop;
14096 case NE_EXPR:
14097 op = DW_OP_ne;
14098 goto do_binop;
14100 do_binop:
14101 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14102 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14103 if (list_ret == 0 || list_ret1 == 0)
14104 return 0;
14106 add_loc_list (&list_ret, list_ret1);
14107 if (list_ret == 0)
14108 return 0;
14109 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14110 break;
14112 case TRUTH_NOT_EXPR:
14113 case BIT_NOT_EXPR:
14114 op = DW_OP_not;
14115 goto do_unop;
14117 case ABS_EXPR:
14118 op = DW_OP_abs;
14119 goto do_unop;
14121 case NEGATE_EXPR:
14122 op = DW_OP_neg;
14123 goto do_unop;
14125 do_unop:
14126 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14127 if (list_ret == 0)
14128 return 0;
14130 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14131 break;
14133 case MIN_EXPR:
14134 case MAX_EXPR:
14136 const enum tree_code code =
14137 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14139 loc = build3 (COND_EXPR, TREE_TYPE (loc),
14140 build2 (code, integer_type_node,
14141 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14142 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
14145 /* ... fall through ... */
14147 case COND_EXPR:
14149 dw_loc_descr_ref lhs
14150 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
14151 dw_loc_list_ref rhs
14152 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
14153 dw_loc_descr_ref bra_node, jump_node, tmp;
14155 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14156 if (list_ret == 0 || lhs == 0 || rhs == 0)
14157 return 0;
14159 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14160 add_loc_descr_to_each (list_ret, bra_node);
14162 add_loc_list (&list_ret, rhs);
14163 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
14164 add_loc_descr_to_each (list_ret, jump_node);
14166 add_loc_descr_to_each (list_ret, lhs);
14167 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14168 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
14170 /* ??? Need a node to point the skip at. Use a nop. */
14171 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14172 add_loc_descr_to_each (list_ret, tmp);
14173 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14174 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
14176 break;
14178 case FIX_TRUNC_EXPR:
14179 return 0;
14181 default:
14182 /* Leave front-end specific codes as simply unknown. This comes
14183 up, for instance, with the C STMT_EXPR. */
14184 if ((unsigned int) TREE_CODE (loc)
14185 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
14187 expansion_failed (loc, NULL_RTX,
14188 "language specific tree node");
14189 return 0;
14192 #ifdef ENABLE_CHECKING
14193 /* Otherwise this is a generic code; we should just lists all of
14194 these explicitly. We forgot one. */
14195 gcc_unreachable ();
14196 #else
14197 /* In a release build, we want to degrade gracefully: better to
14198 generate incomplete debugging information than to crash. */
14199 return NULL;
14200 #endif
14203 if (!ret && !list_ret)
14204 return 0;
14206 if (want_address == 2 && !have_address
14207 && (dwarf_version >= 4 || !dwarf_strict))
14209 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14211 expansion_failed (loc, NULL_RTX,
14212 "DWARF address size mismatch");
14213 return 0;
14215 if (ret)
14216 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
14217 else
14218 add_loc_descr_to_each (list_ret,
14219 new_loc_descr (DW_OP_stack_value, 0, 0));
14220 have_address = 1;
14222 /* Show if we can't fill the request for an address. */
14223 if (want_address && !have_address)
14225 expansion_failed (loc, NULL_RTX,
14226 "Want address and only have value");
14227 return 0;
14230 gcc_assert (!ret || !list_ret);
14232 /* If we've got an address and don't want one, dereference. */
14233 if (!want_address && have_address)
14235 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14237 if (size > DWARF2_ADDR_SIZE || size == -1)
14239 expansion_failed (loc, NULL_RTX,
14240 "DWARF address size mismatch");
14241 return 0;
14243 else if (size == DWARF2_ADDR_SIZE)
14244 op = DW_OP_deref;
14245 else
14246 op = DW_OP_deref_size;
14248 if (ret)
14249 add_loc_descr (&ret, new_loc_descr (op, size, 0));
14250 else
14251 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
14253 if (ret)
14254 list_ret = new_loc_list (ret, NULL, NULL, NULL);
14256 return list_ret;
14259 /* Same as above but return only single location expression. */
14260 static dw_loc_descr_ref
14261 loc_descriptor_from_tree (tree loc, int want_address)
14263 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
14264 if (!ret)
14265 return NULL;
14266 if (ret->dw_loc_next)
14268 expansion_failed (loc, NULL_RTX,
14269 "Location list where only loc descriptor needed");
14270 return NULL;
14272 return ret->expr;
14275 /* Given a value, round it up to the lowest multiple of `boundary'
14276 which is not less than the value itself. */
14278 static inline HOST_WIDE_INT
14279 ceiling (HOST_WIDE_INT value, unsigned int boundary)
14281 return (((value + boundary - 1) / boundary) * boundary);
14284 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
14285 pointer to the declared type for the relevant field variable, or return
14286 `integer_type_node' if the given node turns out to be an
14287 ERROR_MARK node. */
14289 static inline tree
14290 field_type (const_tree decl)
14292 tree type;
14294 if (TREE_CODE (decl) == ERROR_MARK)
14295 return integer_type_node;
14297 type = DECL_BIT_FIELD_TYPE (decl);
14298 if (type == NULL_TREE)
14299 type = TREE_TYPE (decl);
14301 return type;
14304 /* Given a pointer to a tree node, return the alignment in bits for
14305 it, or else return BITS_PER_WORD if the node actually turns out to
14306 be an ERROR_MARK node. */
14308 static inline unsigned
14309 simple_type_align_in_bits (const_tree type)
14311 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
14314 static inline unsigned
14315 simple_decl_align_in_bits (const_tree decl)
14317 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
14320 /* Return the result of rounding T up to ALIGN. */
14322 static inline double_int
14323 round_up_to_align (double_int t, unsigned int align)
14325 double_int alignd = double_int::from_uhwi (align);
14326 t += alignd;
14327 t += double_int_minus_one;
14328 t = t.div (alignd, true, TRUNC_DIV_EXPR);
14329 t *= alignd;
14330 return t;
14333 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
14334 lowest addressed byte of the "containing object" for the given FIELD_DECL,
14335 or return 0 if we are unable to determine what that offset is, either
14336 because the argument turns out to be a pointer to an ERROR_MARK node, or
14337 because the offset is actually variable. (We can't handle the latter case
14338 just yet). */
14340 static HOST_WIDE_INT
14341 field_byte_offset (const_tree decl)
14343 double_int object_offset_in_bits;
14344 double_int object_offset_in_bytes;
14345 double_int bitpos_int;
14347 if (TREE_CODE (decl) == ERROR_MARK)
14348 return 0;
14350 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
14352 /* We cannot yet cope with fields whose positions are variable, so
14353 for now, when we see such things, we simply return 0. Someday, we may
14354 be able to handle such cases, but it will be damn difficult. */
14355 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
14356 return 0;
14358 bitpos_int = tree_to_double_int (bit_position (decl));
14360 #ifdef PCC_BITFIELD_TYPE_MATTERS
14361 if (PCC_BITFIELD_TYPE_MATTERS)
14363 tree type;
14364 tree field_size_tree;
14365 double_int deepest_bitpos;
14366 double_int field_size_in_bits;
14367 unsigned int type_align_in_bits;
14368 unsigned int decl_align_in_bits;
14369 double_int type_size_in_bits;
14371 type = field_type (decl);
14372 type_size_in_bits = double_int_type_size_in_bits (type);
14373 type_align_in_bits = simple_type_align_in_bits (type);
14375 field_size_tree = DECL_SIZE (decl);
14377 /* The size could be unspecified if there was an error, or for
14378 a flexible array member. */
14379 if (!field_size_tree)
14380 field_size_tree = bitsize_zero_node;
14382 /* If the size of the field is not constant, use the type size. */
14383 if (TREE_CODE (field_size_tree) == INTEGER_CST)
14384 field_size_in_bits = tree_to_double_int (field_size_tree);
14385 else
14386 field_size_in_bits = type_size_in_bits;
14388 decl_align_in_bits = simple_decl_align_in_bits (decl);
14390 /* The GCC front-end doesn't make any attempt to keep track of the
14391 starting bit offset (relative to the start of the containing
14392 structure type) of the hypothetical "containing object" for a
14393 bit-field. Thus, when computing the byte offset value for the
14394 start of the "containing object" of a bit-field, we must deduce
14395 this information on our own. This can be rather tricky to do in
14396 some cases. For example, handling the following structure type
14397 definition when compiling for an i386/i486 target (which only
14398 aligns long long's to 32-bit boundaries) can be very tricky:
14400 struct S { int field1; long long field2:31; };
14402 Fortunately, there is a simple rule-of-thumb which can be used
14403 in such cases. When compiling for an i386/i486, GCC will
14404 allocate 8 bytes for the structure shown above. It decides to
14405 do this based upon one simple rule for bit-field allocation.
14406 GCC allocates each "containing object" for each bit-field at
14407 the first (i.e. lowest addressed) legitimate alignment boundary
14408 (based upon the required minimum alignment for the declared
14409 type of the field) which it can possibly use, subject to the
14410 condition that there is still enough available space remaining
14411 in the containing object (when allocated at the selected point)
14412 to fully accommodate all of the bits of the bit-field itself.
14414 This simple rule makes it obvious why GCC allocates 8 bytes for
14415 each object of the structure type shown above. When looking
14416 for a place to allocate the "containing object" for `field2',
14417 the compiler simply tries to allocate a 64-bit "containing
14418 object" at each successive 32-bit boundary (starting at zero)
14419 until it finds a place to allocate that 64- bit field such that
14420 at least 31 contiguous (and previously unallocated) bits remain
14421 within that selected 64 bit field. (As it turns out, for the
14422 example above, the compiler finds it is OK to allocate the
14423 "containing object" 64-bit field at bit-offset zero within the
14424 structure type.)
14426 Here we attempt to work backwards from the limited set of facts
14427 we're given, and we try to deduce from those facts, where GCC
14428 must have believed that the containing object started (within
14429 the structure type). The value we deduce is then used (by the
14430 callers of this routine) to generate DW_AT_location and
14431 DW_AT_bit_offset attributes for fields (both bit-fields and, in
14432 the case of DW_AT_location, regular fields as well). */
14434 /* Figure out the bit-distance from the start of the structure to
14435 the "deepest" bit of the bit-field. */
14436 deepest_bitpos = bitpos_int + field_size_in_bits;
14438 /* This is the tricky part. Use some fancy footwork to deduce
14439 where the lowest addressed bit of the containing object must
14440 be. */
14441 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14443 /* Round up to type_align by default. This works best for
14444 bitfields. */
14445 object_offset_in_bits
14446 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
14448 if (object_offset_in_bits.ugt (bitpos_int))
14450 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14452 /* Round up to decl_align instead. */
14453 object_offset_in_bits
14454 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
14457 else
14458 #endif /* PCC_BITFIELD_TYPE_MATTERS */
14459 object_offset_in_bits = bitpos_int;
14461 object_offset_in_bytes
14462 = object_offset_in_bits.div (double_int::from_uhwi (BITS_PER_UNIT),
14463 true, TRUNC_DIV_EXPR);
14464 return object_offset_in_bytes.to_shwi ();
14467 /* The following routines define various Dwarf attributes and any data
14468 associated with them. */
14470 /* Add a location description attribute value to a DIE.
14472 This emits location attributes suitable for whole variables and
14473 whole parameters. Note that the location attributes for struct fields are
14474 generated by the routine `data_member_location_attribute' below. */
14476 static inline void
14477 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
14478 dw_loc_list_ref descr)
14480 if (descr == 0)
14481 return;
14482 if (single_element_loc_list_p (descr))
14483 add_AT_loc (die, attr_kind, descr->expr);
14484 else
14485 add_AT_loc_list (die, attr_kind, descr);
14488 /* Add DW_AT_accessibility attribute to DIE if needed. */
14490 static void
14491 add_accessibility_attribute (dw_die_ref die, tree decl)
14493 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
14494 children, otherwise the default is DW_ACCESS_public. In DWARF2
14495 the default has always been DW_ACCESS_public. */
14496 if (TREE_PROTECTED (decl))
14497 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
14498 else if (TREE_PRIVATE (decl))
14500 if (dwarf_version == 2
14501 || die->die_parent == NULL
14502 || die->die_parent->die_tag != DW_TAG_class_type)
14503 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
14505 else if (dwarf_version > 2
14506 && die->die_parent
14507 && die->die_parent->die_tag == DW_TAG_class_type)
14508 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
14511 /* Attach the specialized form of location attribute used for data members of
14512 struct and union types. In the special case of a FIELD_DECL node which
14513 represents a bit-field, the "offset" part of this special location
14514 descriptor must indicate the distance in bytes from the lowest-addressed
14515 byte of the containing struct or union type to the lowest-addressed byte of
14516 the "containing object" for the bit-field. (See the `field_byte_offset'
14517 function above).
14519 For any given bit-field, the "containing object" is a hypothetical object
14520 (of some integral or enum type) within which the given bit-field lives. The
14521 type of this hypothetical "containing object" is always the same as the
14522 declared type of the individual bit-field itself (for GCC anyway... the
14523 DWARF spec doesn't actually mandate this). Note that it is the size (in
14524 bytes) of the hypothetical "containing object" which will be given in the
14525 DW_AT_byte_size attribute for this bit-field. (See the
14526 `byte_size_attribute' function below.) It is also used when calculating the
14527 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
14528 function below.) */
14530 static void
14531 add_data_member_location_attribute (dw_die_ref die, tree decl)
14533 HOST_WIDE_INT offset;
14534 dw_loc_descr_ref loc_descr = 0;
14536 if (TREE_CODE (decl) == TREE_BINFO)
14538 /* We're working on the TAG_inheritance for a base class. */
14539 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
14541 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
14542 aren't at a fixed offset from all (sub)objects of the same
14543 type. We need to extract the appropriate offset from our
14544 vtable. The following dwarf expression means
14546 BaseAddr = ObAddr + *((*ObAddr) - Offset)
14548 This is specific to the V3 ABI, of course. */
14550 dw_loc_descr_ref tmp;
14552 /* Make a copy of the object address. */
14553 tmp = new_loc_descr (DW_OP_dup, 0, 0);
14554 add_loc_descr (&loc_descr, tmp);
14556 /* Extract the vtable address. */
14557 tmp = new_loc_descr (DW_OP_deref, 0, 0);
14558 add_loc_descr (&loc_descr, tmp);
14560 /* Calculate the address of the offset. */
14561 offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
14562 gcc_assert (offset < 0);
14564 tmp = int_loc_descriptor (-offset);
14565 add_loc_descr (&loc_descr, tmp);
14566 tmp = new_loc_descr (DW_OP_minus, 0, 0);
14567 add_loc_descr (&loc_descr, tmp);
14569 /* Extract the offset. */
14570 tmp = new_loc_descr (DW_OP_deref, 0, 0);
14571 add_loc_descr (&loc_descr, tmp);
14573 /* Add it to the object address. */
14574 tmp = new_loc_descr (DW_OP_plus, 0, 0);
14575 add_loc_descr (&loc_descr, tmp);
14577 else
14578 offset = tree_low_cst (BINFO_OFFSET (decl), 0);
14580 else
14581 offset = field_byte_offset (decl);
14583 if (! loc_descr)
14585 if (dwarf_version > 2)
14587 /* Don't need to output a location expression, just the constant. */
14588 if (offset < 0)
14589 add_AT_int (die, DW_AT_data_member_location, offset);
14590 else
14591 add_AT_unsigned (die, DW_AT_data_member_location, offset);
14592 return;
14594 else
14596 enum dwarf_location_atom op;
14598 /* The DWARF2 standard says that we should assume that the structure
14599 address is already on the stack, so we can specify a structure
14600 field address by using DW_OP_plus_uconst. */
14601 op = DW_OP_plus_uconst;
14602 loc_descr = new_loc_descr (op, offset, 0);
14606 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
14609 /* Writes integer values to dw_vec_const array. */
14611 static void
14612 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
14614 while (size != 0)
14616 *dest++ = val & 0xff;
14617 val >>= 8;
14618 --size;
14622 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
14624 static HOST_WIDE_INT
14625 extract_int (const unsigned char *src, unsigned int size)
14627 HOST_WIDE_INT val = 0;
14629 src += size;
14630 while (size != 0)
14632 val <<= 8;
14633 val |= *--src & 0xff;
14634 --size;
14636 return val;
14639 /* Writes double_int values to dw_vec_const array. */
14641 static void
14642 insert_double (double_int val, unsigned char *dest)
14644 unsigned char *p0 = dest;
14645 unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
14647 if (WORDS_BIG_ENDIAN)
14649 p0 = p1;
14650 p1 = dest;
14653 insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
14654 insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
14657 /* Writes floating point values to dw_vec_const array. */
14659 static void
14660 insert_float (const_rtx rtl, unsigned char *array)
14662 REAL_VALUE_TYPE rv;
14663 long val[4];
14664 int i;
14666 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
14667 real_to_target (val, &rv, GET_MODE (rtl));
14669 /* real_to_target puts 32-bit pieces in each long. Pack them. */
14670 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
14672 insert_int (val[i], 4, array);
14673 array += 4;
14677 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
14678 does not have a "location" either in memory or in a register. These
14679 things can arise in GNU C when a constant is passed as an actual parameter
14680 to an inlined function. They can also arise in C++ where declared
14681 constants do not necessarily get memory "homes". */
14683 static bool
14684 add_const_value_attribute (dw_die_ref die, rtx rtl)
14686 switch (GET_CODE (rtl))
14688 case CONST_INT:
14690 HOST_WIDE_INT val = INTVAL (rtl);
14692 if (val < 0)
14693 add_AT_int (die, DW_AT_const_value, val);
14694 else
14695 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
14697 return true;
14699 case CONST_DOUBLE:
14700 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
14701 floating-point constant. A CONST_DOUBLE is used whenever the
14702 constant requires more than one word in order to be adequately
14703 represented. */
14705 enum machine_mode mode = GET_MODE (rtl);
14707 if (SCALAR_FLOAT_MODE_P (mode))
14709 unsigned int length = GET_MODE_SIZE (mode);
14710 unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
14712 insert_float (rtl, array);
14713 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
14715 else
14716 add_AT_double (die, DW_AT_const_value,
14717 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
14719 return true;
14721 case CONST_VECTOR:
14723 enum machine_mode mode = GET_MODE (rtl);
14724 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
14725 unsigned int length = CONST_VECTOR_NUNITS (rtl);
14726 unsigned char *array = (unsigned char *) ggc_alloc_atomic
14727 (length * elt_size);
14728 unsigned int i;
14729 unsigned char *p;
14731 switch (GET_MODE_CLASS (mode))
14733 case MODE_VECTOR_INT:
14734 for (i = 0, p = array; i < length; i++, p += elt_size)
14736 rtx elt = CONST_VECTOR_ELT (rtl, i);
14737 double_int val = rtx_to_double_int (elt);
14739 if (elt_size <= sizeof (HOST_WIDE_INT))
14740 insert_int (val.to_shwi (), elt_size, p);
14741 else
14743 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
14744 insert_double (val, p);
14747 break;
14749 case MODE_VECTOR_FLOAT:
14750 for (i = 0, p = array; i < length; i++, p += elt_size)
14752 rtx elt = CONST_VECTOR_ELT (rtl, i);
14753 insert_float (elt, p);
14755 break;
14757 default:
14758 gcc_unreachable ();
14761 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
14763 return true;
14765 case CONST_STRING:
14766 if (dwarf_version >= 4 || !dwarf_strict)
14768 dw_loc_descr_ref loc_result;
14769 resolve_one_addr (&rtl, NULL);
14770 rtl_addr:
14771 loc_result = new_addr_loc_descr (rtl, dtprel_false);
14772 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14773 add_AT_loc (die, DW_AT_location, loc_result);
14774 vec_safe_push (used_rtx_array, rtl);
14775 return true;
14777 return false;
14779 case CONST:
14780 if (CONSTANT_P (XEXP (rtl, 0)))
14781 return add_const_value_attribute (die, XEXP (rtl, 0));
14782 /* FALLTHROUGH */
14783 case SYMBOL_REF:
14784 if (!const_ok_for_output (rtl))
14785 return false;
14786 case LABEL_REF:
14787 if (dwarf_version >= 4 || !dwarf_strict)
14788 goto rtl_addr;
14789 return false;
14791 case PLUS:
14792 /* In cases where an inlined instance of an inline function is passed
14793 the address of an `auto' variable (which is local to the caller) we
14794 can get a situation where the DECL_RTL of the artificial local
14795 variable (for the inlining) which acts as a stand-in for the
14796 corresponding formal parameter (of the inline function) will look
14797 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
14798 exactly a compile-time constant expression, but it isn't the address
14799 of the (artificial) local variable either. Rather, it represents the
14800 *value* which the artificial local variable always has during its
14801 lifetime. We currently have no way to represent such quasi-constant
14802 values in Dwarf, so for now we just punt and generate nothing. */
14803 return false;
14805 case HIGH:
14806 case CONST_FIXED:
14807 return false;
14809 case MEM:
14810 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
14811 && MEM_READONLY_P (rtl)
14812 && GET_MODE (rtl) == BLKmode)
14814 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
14815 return true;
14817 return false;
14819 default:
14820 /* No other kinds of rtx should be possible here. */
14821 gcc_unreachable ();
14823 return false;
14826 /* Determine whether the evaluation of EXPR references any variables
14827 or functions which aren't otherwise used (and therefore may not be
14828 output). */
14829 static tree
14830 reference_to_unused (tree * tp, int * walk_subtrees,
14831 void * data ATTRIBUTE_UNUSED)
14833 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
14834 *walk_subtrees = 0;
14836 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
14837 && ! TREE_ASM_WRITTEN (*tp))
14838 return *tp;
14839 /* ??? The C++ FE emits debug information for using decls, so
14840 putting gcc_unreachable here falls over. See PR31899. For now
14841 be conservative. */
14842 else if (!cgraph_global_info_ready
14843 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
14844 return *tp;
14845 else if (TREE_CODE (*tp) == VAR_DECL)
14847 struct varpool_node *node = varpool_get_node (*tp);
14848 if (!node || !node->analyzed)
14849 return *tp;
14851 else if (TREE_CODE (*tp) == FUNCTION_DECL
14852 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
14854 /* The call graph machinery must have finished analyzing,
14855 optimizing and gimplifying the CU by now.
14856 So if *TP has no call graph node associated
14857 to it, it means *TP will not be emitted. */
14858 if (!cgraph_get_node (*tp))
14859 return *tp;
14861 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
14862 return *tp;
14864 return NULL_TREE;
14867 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
14868 for use in a later add_const_value_attribute call. */
14870 static rtx
14871 rtl_for_decl_init (tree init, tree type)
14873 rtx rtl = NULL_RTX;
14875 STRIP_NOPS (init);
14877 /* If a variable is initialized with a string constant without embedded
14878 zeros, build CONST_STRING. */
14879 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
14881 tree enttype = TREE_TYPE (type);
14882 tree domain = TYPE_DOMAIN (type);
14883 enum machine_mode mode = TYPE_MODE (enttype);
14885 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
14886 && domain
14887 && integer_zerop (TYPE_MIN_VALUE (domain))
14888 && compare_tree_int (TYPE_MAX_VALUE (domain),
14889 TREE_STRING_LENGTH (init) - 1) == 0
14890 && ((size_t) TREE_STRING_LENGTH (init)
14891 == strlen (TREE_STRING_POINTER (init)) + 1))
14893 rtl = gen_rtx_CONST_STRING (VOIDmode,
14894 ggc_strdup (TREE_STRING_POINTER (init)));
14895 rtl = gen_rtx_MEM (BLKmode, rtl);
14896 MEM_READONLY_P (rtl) = 1;
14899 /* Other aggregates, and complex values, could be represented using
14900 CONCAT: FIXME! */
14901 else if (AGGREGATE_TYPE_P (type)
14902 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
14903 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
14904 || TREE_CODE (type) == COMPLEX_TYPE)
14906 /* Vectors only work if their mode is supported by the target.
14907 FIXME: generic vectors ought to work too. */
14908 else if (TREE_CODE (type) == VECTOR_TYPE
14909 && !VECTOR_MODE_P (TYPE_MODE (type)))
14911 /* If the initializer is something that we know will expand into an
14912 immediate RTL constant, expand it now. We must be careful not to
14913 reference variables which won't be output. */
14914 else if (initializer_constant_valid_p (init, type)
14915 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
14917 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
14918 possible. */
14919 if (TREE_CODE (type) == VECTOR_TYPE)
14920 switch (TREE_CODE (init))
14922 case VECTOR_CST:
14923 break;
14924 case CONSTRUCTOR:
14925 if (TREE_CONSTANT (init))
14927 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
14928 bool constant_p = true;
14929 tree value;
14930 unsigned HOST_WIDE_INT ix;
14932 /* Even when ctor is constant, it might contain non-*_CST
14933 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
14934 belong into VECTOR_CST nodes. */
14935 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
14936 if (!CONSTANT_CLASS_P (value))
14938 constant_p = false;
14939 break;
14942 if (constant_p)
14944 init = build_vector_from_ctor (type, elts);
14945 break;
14948 /* FALLTHRU */
14950 default:
14951 return NULL;
14954 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
14956 /* If expand_expr returns a MEM, it wasn't immediate. */
14957 gcc_assert (!rtl || !MEM_P (rtl));
14960 return rtl;
14963 /* Generate RTL for the variable DECL to represent its location. */
14965 static rtx
14966 rtl_for_decl_location (tree decl)
14968 rtx rtl;
14970 /* Here we have to decide where we are going to say the parameter "lives"
14971 (as far as the debugger is concerned). We only have a couple of
14972 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
14974 DECL_RTL normally indicates where the parameter lives during most of the
14975 activation of the function. If optimization is enabled however, this
14976 could be either NULL or else a pseudo-reg. Both of those cases indicate
14977 that the parameter doesn't really live anywhere (as far as the code
14978 generation parts of GCC are concerned) during most of the function's
14979 activation. That will happen (for example) if the parameter is never
14980 referenced within the function.
14982 We could just generate a location descriptor here for all non-NULL
14983 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
14984 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
14985 where DECL_RTL is NULL or is a pseudo-reg.
14987 Note however that we can only get away with using DECL_INCOMING_RTL as
14988 a backup substitute for DECL_RTL in certain limited cases. In cases
14989 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
14990 we can be sure that the parameter was passed using the same type as it is
14991 declared to have within the function, and that its DECL_INCOMING_RTL
14992 points us to a place where a value of that type is passed.
14994 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
14995 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
14996 because in these cases DECL_INCOMING_RTL points us to a value of some
14997 type which is *different* from the type of the parameter itself. Thus,
14998 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
14999 such cases, the debugger would end up (for example) trying to fetch a
15000 `float' from a place which actually contains the first part of a
15001 `double'. That would lead to really incorrect and confusing
15002 output at debug-time.
15004 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15005 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
15006 are a couple of exceptions however. On little-endian machines we can
15007 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15008 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15009 an integral type that is smaller than TREE_TYPE (decl). These cases arise
15010 when (on a little-endian machine) a non-prototyped function has a
15011 parameter declared to be of type `short' or `char'. In such cases,
15012 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15013 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15014 passed `int' value. If the debugger then uses that address to fetch
15015 a `short' or a `char' (on a little-endian machine) the result will be
15016 the correct data, so we allow for such exceptional cases below.
15018 Note that our goal here is to describe the place where the given formal
15019 parameter lives during most of the function's activation (i.e. between the
15020 end of the prologue and the start of the epilogue). We'll do that as best
15021 as we can. Note however that if the given formal parameter is modified
15022 sometime during the execution of the function, then a stack backtrace (at
15023 debug-time) will show the function as having been called with the *new*
15024 value rather than the value which was originally passed in. This happens
15025 rarely enough that it is not a major problem, but it *is* a problem, and
15026 I'd like to fix it.
15028 A future version of dwarf2out.c may generate two additional attributes for
15029 any given DW_TAG_formal_parameter DIE which will describe the "passed
15030 type" and the "passed location" for the given formal parameter in addition
15031 to the attributes we now generate to indicate the "declared type" and the
15032 "active location" for each parameter. This additional set of attributes
15033 could be used by debuggers for stack backtraces. Separately, note that
15034 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15035 This happens (for example) for inlined-instances of inline function formal
15036 parameters which are never referenced. This really shouldn't be
15037 happening. All PARM_DECL nodes should get valid non-NULL
15038 DECL_INCOMING_RTL values. FIXME. */
15040 /* Use DECL_RTL as the "location" unless we find something better. */
15041 rtl = DECL_RTL_IF_SET (decl);
15043 /* When generating abstract instances, ignore everything except
15044 constants, symbols living in memory, and symbols living in
15045 fixed registers. */
15046 if (! reload_completed)
15048 if (rtl
15049 && (CONSTANT_P (rtl)
15050 || (MEM_P (rtl)
15051 && CONSTANT_P (XEXP (rtl, 0)))
15052 || (REG_P (rtl)
15053 && TREE_CODE (decl) == VAR_DECL
15054 && TREE_STATIC (decl))))
15056 rtl = targetm.delegitimize_address (rtl);
15057 return rtl;
15059 rtl = NULL_RTX;
15061 else if (TREE_CODE (decl) == PARM_DECL)
15063 if (rtl == NULL_RTX
15064 || is_pseudo_reg (rtl)
15065 || (MEM_P (rtl)
15066 && is_pseudo_reg (XEXP (rtl, 0))
15067 && DECL_INCOMING_RTL (decl)
15068 && MEM_P (DECL_INCOMING_RTL (decl))
15069 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15071 tree declared_type = TREE_TYPE (decl);
15072 tree passed_type = DECL_ARG_TYPE (decl);
15073 enum machine_mode dmode = TYPE_MODE (declared_type);
15074 enum machine_mode pmode = TYPE_MODE (passed_type);
15076 /* This decl represents a formal parameter which was optimized out.
15077 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15078 all cases where (rtl == NULL_RTX) just below. */
15079 if (dmode == pmode)
15080 rtl = DECL_INCOMING_RTL (decl);
15081 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15082 && SCALAR_INT_MODE_P (dmode)
15083 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15084 && DECL_INCOMING_RTL (decl))
15086 rtx inc = DECL_INCOMING_RTL (decl);
15087 if (REG_P (inc))
15088 rtl = inc;
15089 else if (MEM_P (inc))
15091 if (BYTES_BIG_ENDIAN)
15092 rtl = adjust_address_nv (inc, dmode,
15093 GET_MODE_SIZE (pmode)
15094 - GET_MODE_SIZE (dmode));
15095 else
15096 rtl = inc;
15101 /* If the parm was passed in registers, but lives on the stack, then
15102 make a big endian correction if the mode of the type of the
15103 parameter is not the same as the mode of the rtl. */
15104 /* ??? This is the same series of checks that are made in dbxout.c before
15105 we reach the big endian correction code there. It isn't clear if all
15106 of these checks are necessary here, but keeping them all is the safe
15107 thing to do. */
15108 else if (MEM_P (rtl)
15109 && XEXP (rtl, 0) != const0_rtx
15110 && ! CONSTANT_P (XEXP (rtl, 0))
15111 /* Not passed in memory. */
15112 && !MEM_P (DECL_INCOMING_RTL (decl))
15113 /* Not passed by invisible reference. */
15114 && (!REG_P (XEXP (rtl, 0))
15115 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15116 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15117 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15118 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15119 #endif
15121 /* Big endian correction check. */
15122 && BYTES_BIG_ENDIAN
15123 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15124 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15125 < UNITS_PER_WORD))
15127 enum machine_mode addr_mode = get_address_mode (rtl);
15128 int offset = (UNITS_PER_WORD
15129 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15131 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15132 plus_constant (addr_mode, XEXP (rtl, 0), offset));
15135 else if (TREE_CODE (decl) == VAR_DECL
15136 && rtl
15137 && MEM_P (rtl)
15138 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
15139 && BYTES_BIG_ENDIAN)
15141 enum machine_mode addr_mode = get_address_mode (rtl);
15142 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
15143 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
15145 /* If a variable is declared "register" yet is smaller than
15146 a register, then if we store the variable to memory, it
15147 looks like we're storing a register-sized value, when in
15148 fact we are not. We need to adjust the offset of the
15149 storage location to reflect the actual value's bytes,
15150 else gdb will not be able to display it. */
15151 if (rsize > dsize)
15152 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15153 plus_constant (addr_mode, XEXP (rtl, 0),
15154 rsize - dsize));
15157 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
15158 and will have been substituted directly into all expressions that use it.
15159 C does not have such a concept, but C++ and other languages do. */
15160 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
15161 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
15163 if (rtl)
15164 rtl = targetm.delegitimize_address (rtl);
15166 /* If we don't look past the constant pool, we risk emitting a
15167 reference to a constant pool entry that isn't referenced from
15168 code, and thus is not emitted. */
15169 if (rtl)
15170 rtl = avoid_constant_pool_reference (rtl);
15172 /* Try harder to get a rtl. If this symbol ends up not being emitted
15173 in the current CU, resolve_addr will remove the expression referencing
15174 it. */
15175 if (rtl == NULL_RTX
15176 && TREE_CODE (decl) == VAR_DECL
15177 && !DECL_EXTERNAL (decl)
15178 && TREE_STATIC (decl)
15179 && DECL_NAME (decl)
15180 && !DECL_HARD_REGISTER (decl)
15181 && DECL_MODE (decl) != VOIDmode)
15183 rtl = make_decl_rtl_for_debug (decl);
15184 if (!MEM_P (rtl)
15185 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
15186 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
15187 rtl = NULL_RTX;
15190 return rtl;
15193 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
15194 returned. If so, the decl for the COMMON block is returned, and the
15195 value is the offset into the common block for the symbol. */
15197 static tree
15198 fortran_common (tree decl, HOST_WIDE_INT *value)
15200 tree val_expr, cvar;
15201 enum machine_mode mode;
15202 HOST_WIDE_INT bitsize, bitpos;
15203 tree offset;
15204 int unsignedp, volatilep = 0;
15206 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
15207 it does not have a value (the offset into the common area), or if it
15208 is thread local (as opposed to global) then it isn't common, and shouldn't
15209 be handled as such. */
15210 if (TREE_CODE (decl) != VAR_DECL
15211 || !TREE_STATIC (decl)
15212 || !DECL_HAS_VALUE_EXPR_P (decl)
15213 || !is_fortran ())
15214 return NULL_TREE;
15216 val_expr = DECL_VALUE_EXPR (decl);
15217 if (TREE_CODE (val_expr) != COMPONENT_REF)
15218 return NULL_TREE;
15220 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
15221 &mode, &unsignedp, &volatilep, true);
15223 if (cvar == NULL_TREE
15224 || TREE_CODE (cvar) != VAR_DECL
15225 || DECL_ARTIFICIAL (cvar)
15226 || !TREE_PUBLIC (cvar))
15227 return NULL_TREE;
15229 *value = 0;
15230 if (offset != NULL)
15232 if (!host_integerp (offset, 0))
15233 return NULL_TREE;
15234 *value = tree_low_cst (offset, 0);
15236 if (bitpos != 0)
15237 *value += bitpos / BITS_PER_UNIT;
15239 return cvar;
15242 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
15243 data attribute for a variable or a parameter. We generate the
15244 DW_AT_const_value attribute only in those cases where the given variable
15245 or parameter does not have a true "location" either in memory or in a
15246 register. This can happen (for example) when a constant is passed as an
15247 actual argument in a call to an inline function. (It's possible that
15248 these things can crop up in other ways also.) Note that one type of
15249 constant value which can be passed into an inlined function is a constant
15250 pointer. This can happen for example if an actual argument in an inlined
15251 function call evaluates to a compile-time constant address.
15253 CACHE_P is true if it is worth caching the location list for DECL,
15254 so that future calls can reuse it rather than regenerate it from scratch.
15255 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
15256 since we will need to refer to them each time the function is inlined. */
15258 static bool
15259 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
15260 enum dwarf_attribute attr)
15262 rtx rtl;
15263 dw_loc_list_ref list;
15264 var_loc_list *loc_list;
15265 cached_dw_loc_list *cache;
15266 void **slot;
15268 if (TREE_CODE (decl) == ERROR_MARK)
15269 return false;
15271 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
15272 || TREE_CODE (decl) == RESULT_DECL);
15274 /* Try to get some constant RTL for this decl, and use that as the value of
15275 the location. */
15277 rtl = rtl_for_decl_location (decl);
15278 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15279 && add_const_value_attribute (die, rtl))
15280 return true;
15282 /* See if we have single element location list that is equivalent to
15283 a constant value. That way we are better to use add_const_value_attribute
15284 rather than expanding constant value equivalent. */
15285 loc_list = lookup_decl_loc (decl);
15286 if (loc_list
15287 && loc_list->first
15288 && loc_list->first->next == NULL
15289 && NOTE_P (loc_list->first->loc)
15290 && NOTE_VAR_LOCATION (loc_list->first->loc)
15291 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
15293 struct var_loc_node *node;
15295 node = loc_list->first;
15296 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
15297 if (GET_CODE (rtl) == EXPR_LIST)
15298 rtl = XEXP (rtl, 0);
15299 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15300 && add_const_value_attribute (die, rtl))
15301 return true;
15303 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
15304 list several times. See if we've already cached the contents. */
15305 list = NULL;
15306 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
15307 cache_p = false;
15308 if (cache_p)
15310 cache = (cached_dw_loc_list *)
15311 htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
15312 if (cache)
15313 list = cache->loc_list;
15315 if (list == NULL)
15317 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
15318 /* It is usually worth caching this result if the decl is from
15319 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
15320 if (cache_p && list && list->dw_loc_next)
15322 slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
15323 DECL_UID (decl), INSERT);
15324 cache = ggc_alloc_cleared_cached_dw_loc_list ();
15325 cache->decl_id = DECL_UID (decl);
15326 cache->loc_list = list;
15327 *slot = cache;
15330 if (list)
15332 add_AT_location_description (die, attr, list);
15333 return true;
15335 /* None of that worked, so it must not really have a location;
15336 try adding a constant value attribute from the DECL_INITIAL. */
15337 return tree_add_const_value_attribute_for_decl (die, decl);
15340 /* Add VARIABLE and DIE into deferred locations list. */
15342 static void
15343 defer_location (tree variable, dw_die_ref die)
15345 deferred_locations entry;
15346 entry.variable = variable;
15347 entry.die = die;
15348 vec_safe_push (deferred_locations_list, entry);
15351 /* Helper function for tree_add_const_value_attribute. Natively encode
15352 initializer INIT into an array. Return true if successful. */
15354 static bool
15355 native_encode_initializer (tree init, unsigned char *array, int size)
15357 tree type;
15359 if (init == NULL_TREE)
15360 return false;
15362 STRIP_NOPS (init);
15363 switch (TREE_CODE (init))
15365 case STRING_CST:
15366 type = TREE_TYPE (init);
15367 if (TREE_CODE (type) == ARRAY_TYPE)
15369 tree enttype = TREE_TYPE (type);
15370 enum machine_mode mode = TYPE_MODE (enttype);
15372 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
15373 return false;
15374 if (int_size_in_bytes (type) != size)
15375 return false;
15376 if (size > TREE_STRING_LENGTH (init))
15378 memcpy (array, TREE_STRING_POINTER (init),
15379 TREE_STRING_LENGTH (init));
15380 memset (array + TREE_STRING_LENGTH (init),
15381 '\0', size - TREE_STRING_LENGTH (init));
15383 else
15384 memcpy (array, TREE_STRING_POINTER (init), size);
15385 return true;
15387 return false;
15388 case CONSTRUCTOR:
15389 type = TREE_TYPE (init);
15390 if (int_size_in_bytes (type) != size)
15391 return false;
15392 if (TREE_CODE (type) == ARRAY_TYPE)
15394 HOST_WIDE_INT min_index;
15395 unsigned HOST_WIDE_INT cnt;
15396 int curpos = 0, fieldsize;
15397 constructor_elt *ce;
15399 if (TYPE_DOMAIN (type) == NULL_TREE
15400 || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
15401 return false;
15403 fieldsize = int_size_in_bytes (TREE_TYPE (type));
15404 if (fieldsize <= 0)
15405 return false;
15407 min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
15408 memset (array, '\0', size);
15409 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15411 tree val = ce->value;
15412 tree index = ce->index;
15413 int pos = curpos;
15414 if (index && TREE_CODE (index) == RANGE_EXPR)
15415 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
15416 * fieldsize;
15417 else if (index)
15418 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
15420 if (val)
15422 STRIP_NOPS (val);
15423 if (!native_encode_initializer (val, array + pos, fieldsize))
15424 return false;
15426 curpos = pos + fieldsize;
15427 if (index && TREE_CODE (index) == RANGE_EXPR)
15429 int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
15430 - tree_low_cst (TREE_OPERAND (index, 0), 0);
15431 while (count-- > 0)
15433 if (val)
15434 memcpy (array + curpos, array + pos, fieldsize);
15435 curpos += fieldsize;
15438 gcc_assert (curpos <= size);
15440 return true;
15442 else if (TREE_CODE (type) == RECORD_TYPE
15443 || TREE_CODE (type) == UNION_TYPE)
15445 tree field = NULL_TREE;
15446 unsigned HOST_WIDE_INT cnt;
15447 constructor_elt *ce;
15449 if (int_size_in_bytes (type) != size)
15450 return false;
15452 if (TREE_CODE (type) == RECORD_TYPE)
15453 field = TYPE_FIELDS (type);
15455 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15457 tree val = ce->value;
15458 int pos, fieldsize;
15460 if (ce->index != 0)
15461 field = ce->index;
15463 if (val)
15464 STRIP_NOPS (val);
15466 if (field == NULL_TREE || DECL_BIT_FIELD (field))
15467 return false;
15469 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
15470 && TYPE_DOMAIN (TREE_TYPE (field))
15471 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
15472 return false;
15473 else if (DECL_SIZE_UNIT (field) == NULL_TREE
15474 || !host_integerp (DECL_SIZE_UNIT (field), 0))
15475 return false;
15476 fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
15477 pos = int_byte_position (field);
15478 gcc_assert (pos + fieldsize <= size);
15479 if (val
15480 && !native_encode_initializer (val, array + pos, fieldsize))
15481 return false;
15483 return true;
15485 return false;
15486 case VIEW_CONVERT_EXPR:
15487 case NON_LVALUE_EXPR:
15488 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
15489 default:
15490 return native_encode_expr (init, array, size) == size;
15494 /* Attach a DW_AT_const_value attribute to DIE. The value of the
15495 attribute is the const value T. */
15497 static bool
15498 tree_add_const_value_attribute (dw_die_ref die, tree t)
15500 tree init;
15501 tree type = TREE_TYPE (t);
15502 rtx rtl;
15504 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
15505 return false;
15507 init = t;
15508 gcc_assert (!DECL_P (init));
15510 rtl = rtl_for_decl_init (init, type);
15511 if (rtl)
15512 return add_const_value_attribute (die, rtl);
15513 /* If the host and target are sane, try harder. */
15514 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
15515 && initializer_constant_valid_p (init, type))
15517 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
15518 if (size > 0 && (int) size == size)
15520 unsigned char *array = (unsigned char *)
15521 ggc_alloc_cleared_atomic (size);
15523 if (native_encode_initializer (init, array, size))
15525 add_AT_vec (die, DW_AT_const_value, size, 1, array);
15526 return true;
15530 return false;
15533 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
15534 attribute is the const value of T, where T is an integral constant
15535 variable with static storage duration
15536 (so it can't be a PARM_DECL or a RESULT_DECL). */
15538 static bool
15539 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
15542 if (!decl
15543 || (TREE_CODE (decl) != VAR_DECL
15544 && TREE_CODE (decl) != CONST_DECL)
15545 || (TREE_CODE (decl) == VAR_DECL
15546 && !TREE_STATIC (decl)))
15547 return false;
15549 if (TREE_READONLY (decl)
15550 && ! TREE_THIS_VOLATILE (decl)
15551 && DECL_INITIAL (decl))
15552 /* OK */;
15553 else
15554 return false;
15556 /* Don't add DW_AT_const_value if abstract origin already has one. */
15557 if (get_AT (var_die, DW_AT_const_value))
15558 return false;
15560 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
15563 /* Convert the CFI instructions for the current function into a
15564 location list. This is used for DW_AT_frame_base when we targeting
15565 a dwarf2 consumer that does not support the dwarf3
15566 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
15567 expressions. */
15569 static dw_loc_list_ref
15570 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
15572 int ix;
15573 dw_fde_ref fde;
15574 dw_loc_list_ref list, *list_tail;
15575 dw_cfi_ref cfi;
15576 dw_cfa_location last_cfa, next_cfa;
15577 const char *start_label, *last_label, *section;
15578 dw_cfa_location remember;
15580 fde = cfun->fde;
15581 gcc_assert (fde != NULL);
15583 section = secname_for_decl (current_function_decl);
15584 list_tail = &list;
15585 list = NULL;
15587 memset (&next_cfa, 0, sizeof (next_cfa));
15588 next_cfa.reg = INVALID_REGNUM;
15589 remember = next_cfa;
15591 start_label = fde->dw_fde_begin;
15593 /* ??? Bald assumption that the CIE opcode list does not contain
15594 advance opcodes. */
15595 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
15596 lookup_cfa_1 (cfi, &next_cfa, &remember);
15598 last_cfa = next_cfa;
15599 last_label = start_label;
15601 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
15603 /* If the first partition contained no CFI adjustments, the
15604 CIE opcodes apply to the whole first partition. */
15605 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15606 fde->dw_fde_begin, fde->dw_fde_end, section);
15607 list_tail =&(*list_tail)->dw_loc_next;
15608 start_label = last_label = fde->dw_fde_second_begin;
15611 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
15613 switch (cfi->dw_cfi_opc)
15615 case DW_CFA_set_loc:
15616 case DW_CFA_advance_loc1:
15617 case DW_CFA_advance_loc2:
15618 case DW_CFA_advance_loc4:
15619 if (!cfa_equal_p (&last_cfa, &next_cfa))
15621 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15622 start_label, last_label, section);
15624 list_tail = &(*list_tail)->dw_loc_next;
15625 last_cfa = next_cfa;
15626 start_label = last_label;
15628 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
15629 break;
15631 case DW_CFA_advance_loc:
15632 /* The encoding is complex enough that we should never emit this. */
15633 gcc_unreachable ();
15635 default:
15636 lookup_cfa_1 (cfi, &next_cfa, &remember);
15637 break;
15639 if (ix + 1 == fde->dw_fde_switch_cfi_index)
15641 if (!cfa_equal_p (&last_cfa, &next_cfa))
15643 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15644 start_label, last_label, section);
15646 list_tail = &(*list_tail)->dw_loc_next;
15647 last_cfa = next_cfa;
15648 start_label = last_label;
15650 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15651 start_label, fde->dw_fde_end, section);
15652 list_tail = &(*list_tail)->dw_loc_next;
15653 start_label = last_label = fde->dw_fde_second_begin;
15657 if (!cfa_equal_p (&last_cfa, &next_cfa))
15659 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15660 start_label, last_label, section);
15661 list_tail = &(*list_tail)->dw_loc_next;
15662 start_label = last_label;
15665 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
15666 start_label,
15667 fde->dw_fde_second_begin
15668 ? fde->dw_fde_second_end : fde->dw_fde_end,
15669 section);
15671 if (list && list->dw_loc_next)
15672 gen_llsym (list);
15674 return list;
15677 /* Compute a displacement from the "steady-state frame pointer" to the
15678 frame base (often the same as the CFA), and store it in
15679 frame_pointer_fb_offset. OFFSET is added to the displacement
15680 before the latter is negated. */
15682 static void
15683 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
15685 rtx reg, elim;
15687 #ifdef FRAME_POINTER_CFA_OFFSET
15688 reg = frame_pointer_rtx;
15689 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
15690 #else
15691 reg = arg_pointer_rtx;
15692 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
15693 #endif
15695 elim = (ira_use_lra_p
15696 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
15697 : eliminate_regs (reg, VOIDmode, NULL_RTX));
15698 if (GET_CODE (elim) == PLUS)
15700 offset += INTVAL (XEXP (elim, 1));
15701 elim = XEXP (elim, 0);
15704 frame_pointer_fb_offset = -offset;
15706 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
15707 in which to eliminate. This is because it's stack pointer isn't
15708 directly accessible as a register within the ISA. To work around
15709 this, assume that while we cannot provide a proper value for
15710 frame_pointer_fb_offset, we won't need one either. */
15711 frame_pointer_fb_offset_valid
15712 = ((SUPPORTS_STACK_ALIGNMENT
15713 && (elim == hard_frame_pointer_rtx
15714 || elim == stack_pointer_rtx))
15715 || elim == (frame_pointer_needed
15716 ? hard_frame_pointer_rtx
15717 : stack_pointer_rtx));
15720 /* Generate a DW_AT_name attribute given some string value to be included as
15721 the value of the attribute. */
15723 static void
15724 add_name_attribute (dw_die_ref die, const char *name_string)
15726 if (name_string != NULL && *name_string != 0)
15728 if (demangle_name_func)
15729 name_string = (*demangle_name_func) (name_string);
15731 add_AT_string (die, DW_AT_name, name_string);
15735 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
15736 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
15737 of TYPE accordingly.
15739 ??? This is a temporary measure until after we're able to generate
15740 regular DWARF for the complex Ada type system. */
15742 static void
15743 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
15744 dw_die_ref context_die)
15746 tree dtype;
15747 dw_die_ref dtype_die;
15749 if (!lang_hooks.types.descriptive_type)
15750 return;
15752 dtype = lang_hooks.types.descriptive_type (type);
15753 if (!dtype)
15754 return;
15756 dtype_die = lookup_type_die (dtype);
15757 if (!dtype_die)
15759 gen_type_die (dtype, context_die);
15760 dtype_die = lookup_type_die (dtype);
15761 gcc_assert (dtype_die);
15764 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
15767 /* Generate a DW_AT_comp_dir attribute for DIE. */
15769 static void
15770 add_comp_dir_attribute (dw_die_ref die)
15772 const char *wd = get_src_pwd ();
15773 char *wd1;
15775 if (wd == NULL)
15776 return;
15778 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
15780 int wdlen;
15782 wdlen = strlen (wd);
15783 wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
15784 strcpy (wd1, wd);
15785 wd1 [wdlen] = DIR_SEPARATOR;
15786 wd1 [wdlen + 1] = 0;
15787 wd = wd1;
15790 add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
15793 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
15794 default. */
15796 static int
15797 lower_bound_default (void)
15799 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
15801 case DW_LANG_C:
15802 case DW_LANG_C89:
15803 case DW_LANG_C99:
15804 case DW_LANG_C_plus_plus:
15805 case DW_LANG_ObjC:
15806 case DW_LANG_ObjC_plus_plus:
15807 case DW_LANG_Java:
15808 return 0;
15809 case DW_LANG_Fortran77:
15810 case DW_LANG_Fortran90:
15811 case DW_LANG_Fortran95:
15812 return 1;
15813 case DW_LANG_UPC:
15814 case DW_LANG_D:
15815 case DW_LANG_Python:
15816 return dwarf_version >= 4 ? 0 : -1;
15817 case DW_LANG_Ada95:
15818 case DW_LANG_Ada83:
15819 case DW_LANG_Cobol74:
15820 case DW_LANG_Cobol85:
15821 case DW_LANG_Pascal83:
15822 case DW_LANG_Modula2:
15823 case DW_LANG_PLI:
15824 return dwarf_version >= 4 ? 1 : -1;
15825 default:
15826 return -1;
15830 /* Given a tree node describing an array bound (either lower or upper) output
15831 a representation for that bound. */
15833 static void
15834 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
15836 switch (TREE_CODE (bound))
15838 case ERROR_MARK:
15839 return;
15841 /* All fixed-bounds are represented by INTEGER_CST nodes. */
15842 case INTEGER_CST:
15844 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
15845 int dflt;
15847 /* Use the default if possible. */
15848 if (bound_attr == DW_AT_lower_bound
15849 && host_integerp (bound, 0)
15850 && (dflt = lower_bound_default ()) != -1
15851 && tree_low_cst (bound, 0) == dflt)
15854 /* Otherwise represent the bound as an unsigned value with the
15855 precision of its type. The precision and signedness of the
15856 type will be necessary to re-interpret it unambiguously. */
15857 else if (prec < HOST_BITS_PER_WIDE_INT)
15859 unsigned HOST_WIDE_INT mask
15860 = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
15861 add_AT_unsigned (subrange_die, bound_attr,
15862 TREE_INT_CST_LOW (bound) & mask);
15864 else if (prec == HOST_BITS_PER_WIDE_INT
15865 || TREE_INT_CST_HIGH (bound) == 0)
15866 add_AT_unsigned (subrange_die, bound_attr,
15867 TREE_INT_CST_LOW (bound));
15868 else
15869 add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
15870 TREE_INT_CST_LOW (bound));
15872 break;
15874 CASE_CONVERT:
15875 case VIEW_CONVERT_EXPR:
15876 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
15877 break;
15879 case SAVE_EXPR:
15880 break;
15882 case VAR_DECL:
15883 case PARM_DECL:
15884 case RESULT_DECL:
15886 dw_die_ref decl_die = lookup_decl_die (bound);
15888 /* ??? Can this happen, or should the variable have been bound
15889 first? Probably it can, since I imagine that we try to create
15890 the types of parameters in the order in which they exist in
15891 the list, and won't have created a forward reference to a
15892 later parameter. */
15893 if (decl_die != NULL)
15895 add_AT_die_ref (subrange_die, bound_attr, decl_die);
15896 break;
15899 /* FALLTHRU */
15901 default:
15903 /* Otherwise try to create a stack operation procedure to
15904 evaluate the value of the array bound. */
15906 dw_die_ref ctx, decl_die;
15907 dw_loc_list_ref list;
15909 list = loc_list_from_tree (bound, 2);
15910 if (list == NULL || single_element_loc_list_p (list))
15912 /* If DW_AT_*bound is not a reference nor constant, it is
15913 a DWARF expression rather than location description.
15914 For that loc_list_from_tree (bound, 0) is needed.
15915 If that fails to give a single element list,
15916 fall back to outputting this as a reference anyway. */
15917 dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
15918 if (list2 && single_element_loc_list_p (list2))
15920 add_AT_loc (subrange_die, bound_attr, list2->expr);
15921 break;
15924 if (list == NULL)
15925 break;
15927 if (current_function_decl == 0)
15928 ctx = comp_unit_die ();
15929 else
15930 ctx = lookup_decl_die (current_function_decl);
15932 decl_die = new_die (DW_TAG_variable, ctx, bound);
15933 add_AT_flag (decl_die, DW_AT_artificial, 1);
15934 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
15935 add_AT_location_description (decl_die, DW_AT_location, list);
15936 add_AT_die_ref (subrange_die, bound_attr, decl_die);
15937 break;
15942 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
15943 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
15944 Note that the block of subscript information for an array type also
15945 includes information about the element type of the given array type. */
15947 static void
15948 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
15950 unsigned dimension_number;
15951 tree lower, upper;
15952 dw_die_ref subrange_die;
15954 for (dimension_number = 0;
15955 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
15956 type = TREE_TYPE (type), dimension_number++)
15958 tree domain = TYPE_DOMAIN (type);
15960 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
15961 break;
15963 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
15964 and (in GNU C only) variable bounds. Handle all three forms
15965 here. */
15966 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
15967 if (domain)
15969 /* We have an array type with specified bounds. */
15970 lower = TYPE_MIN_VALUE (domain);
15971 upper = TYPE_MAX_VALUE (domain);
15973 /* Define the index type. */
15974 if (TREE_TYPE (domain))
15976 /* ??? This is probably an Ada unnamed subrange type. Ignore the
15977 TREE_TYPE field. We can't emit debug info for this
15978 because it is an unnamed integral type. */
15979 if (TREE_CODE (domain) == INTEGER_TYPE
15980 && TYPE_NAME (domain) == NULL_TREE
15981 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
15982 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
15984 else
15985 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
15986 type_die);
15989 /* ??? If upper is NULL, the array has unspecified length,
15990 but it does have a lower bound. This happens with Fortran
15991 dimension arr(N:*)
15992 Since the debugger is definitely going to need to know N
15993 to produce useful results, go ahead and output the lower
15994 bound solo, and hope the debugger can cope. */
15996 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
15997 if (upper)
15998 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
16001 /* Otherwise we have an array type with an unspecified length. The
16002 DWARF-2 spec does not say how to handle this; let's just leave out the
16003 bounds. */
16007 static void
16008 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16010 dw_die_ref decl_die;
16011 unsigned size;
16013 switch (TREE_CODE (tree_node))
16015 case ERROR_MARK:
16016 size = 0;
16017 break;
16018 case ENUMERAL_TYPE:
16019 case RECORD_TYPE:
16020 case UNION_TYPE:
16021 case QUAL_UNION_TYPE:
16022 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
16023 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
16025 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
16026 return;
16028 size = int_size_in_bytes (tree_node);
16029 break;
16030 case FIELD_DECL:
16031 /* For a data member of a struct or union, the DW_AT_byte_size is
16032 generally given as the number of bytes normally allocated for an
16033 object of the *declared* type of the member itself. This is true
16034 even for bit-fields. */
16035 size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
16036 break;
16037 default:
16038 gcc_unreachable ();
16041 /* Note that `size' might be -1 when we get to this point. If it is, that
16042 indicates that the byte size of the entity in question is variable. We
16043 have no good way of expressing this fact in Dwarf at the present time,
16044 so just let the -1 pass on through. */
16045 add_AT_unsigned (die, DW_AT_byte_size, size);
16048 /* For a FIELD_DECL node which represents a bit-field, output an attribute
16049 which specifies the distance in bits from the highest order bit of the
16050 "containing object" for the bit-field to the highest order bit of the
16051 bit-field itself.
16053 For any given bit-field, the "containing object" is a hypothetical object
16054 (of some integral or enum type) within which the given bit-field lives. The
16055 type of this hypothetical "containing object" is always the same as the
16056 declared type of the individual bit-field itself. The determination of the
16057 exact location of the "containing object" for a bit-field is rather
16058 complicated. It's handled by the `field_byte_offset' function (above).
16060 Note that it is the size (in bytes) of the hypothetical "containing object"
16061 which will be given in the DW_AT_byte_size attribute for this bit-field.
16062 (See `byte_size_attribute' above). */
16064 static inline void
16065 add_bit_offset_attribute (dw_die_ref die, tree decl)
16067 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
16068 tree type = DECL_BIT_FIELD_TYPE (decl);
16069 HOST_WIDE_INT bitpos_int;
16070 HOST_WIDE_INT highest_order_object_bit_offset;
16071 HOST_WIDE_INT highest_order_field_bit_offset;
16072 HOST_WIDE_INT bit_offset;
16074 /* Must be a field and a bit field. */
16075 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
16077 /* We can't yet handle bit-fields whose offsets are variable, so if we
16078 encounter such things, just return without generating any attribute
16079 whatsoever. Likewise for variable or too large size. */
16080 if (! host_integerp (bit_position (decl), 0)
16081 || ! host_integerp (DECL_SIZE (decl), 1))
16082 return;
16084 bitpos_int = int_bit_position (decl);
16086 /* Note that the bit offset is always the distance (in bits) from the
16087 highest-order bit of the "containing object" to the highest-order bit of
16088 the bit-field itself. Since the "high-order end" of any object or field
16089 is different on big-endian and little-endian machines, the computation
16090 below must take account of these differences. */
16091 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
16092 highest_order_field_bit_offset = bitpos_int;
16094 if (! BYTES_BIG_ENDIAN)
16096 highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
16097 highest_order_object_bit_offset += simple_type_size_in_bits (type);
16100 bit_offset
16101 = (! BYTES_BIG_ENDIAN
16102 ? highest_order_object_bit_offset - highest_order_field_bit_offset
16103 : highest_order_field_bit_offset - highest_order_object_bit_offset);
16105 if (bit_offset < 0)
16106 add_AT_int (die, DW_AT_bit_offset, bit_offset);
16107 else
16108 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
16111 /* For a FIELD_DECL node which represents a bit field, output an attribute
16112 which specifies the length in bits of the given field. */
16114 static inline void
16115 add_bit_size_attribute (dw_die_ref die, tree decl)
16117 /* Must be a field and a bit field. */
16118 gcc_assert (TREE_CODE (decl) == FIELD_DECL
16119 && DECL_BIT_FIELD_TYPE (decl));
16121 if (host_integerp (DECL_SIZE (decl), 1))
16122 add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
16125 /* If the compiled language is ANSI C, then add a 'prototyped'
16126 attribute, if arg types are given for the parameters of a function. */
16128 static inline void
16129 add_prototyped_attribute (dw_die_ref die, tree func_type)
16131 if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
16132 && prototype_p (func_type))
16133 add_AT_flag (die, DW_AT_prototyped, 1);
16136 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
16137 by looking in either the type declaration or object declaration
16138 equate table. */
16140 static inline dw_die_ref
16141 add_abstract_origin_attribute (dw_die_ref die, tree origin)
16143 dw_die_ref origin_die = NULL;
16145 if (TREE_CODE (origin) != FUNCTION_DECL)
16147 /* We may have gotten separated from the block for the inlined
16148 function, if we're in an exception handler or some such; make
16149 sure that the abstract function has been written out.
16151 Doing this for nested functions is wrong, however; functions are
16152 distinct units, and our context might not even be inline. */
16153 tree fn = origin;
16155 if (TYPE_P (fn))
16156 fn = TYPE_STUB_DECL (fn);
16158 fn = decl_function_context (fn);
16159 if (fn)
16160 dwarf2out_abstract_function (fn);
16163 if (DECL_P (origin))
16164 origin_die = lookup_decl_die (origin);
16165 else if (TYPE_P (origin))
16166 origin_die = lookup_type_die (origin);
16168 /* XXX: Functions that are never lowered don't always have correct block
16169 trees (in the case of java, they simply have no block tree, in some other
16170 languages). For these functions, there is nothing we can really do to
16171 output correct debug info for inlined functions in all cases. Rather
16172 than die, we'll just produce deficient debug info now, in that we will
16173 have variables without a proper abstract origin. In the future, when all
16174 functions are lowered, we should re-add a gcc_assert (origin_die)
16175 here. */
16177 if (origin_die)
16178 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
16179 return origin_die;
16182 /* We do not currently support the pure_virtual attribute. */
16184 static inline void
16185 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
16187 if (DECL_VINDEX (func_decl))
16189 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
16191 if (host_integerp (DECL_VINDEX (func_decl), 0))
16192 add_AT_loc (die, DW_AT_vtable_elem_location,
16193 new_loc_descr (DW_OP_constu,
16194 tree_low_cst (DECL_VINDEX (func_decl), 0),
16195 0));
16197 /* GNU extension: Record what type this method came from originally. */
16198 if (debug_info_level > DINFO_LEVEL_TERSE
16199 && DECL_CONTEXT (func_decl))
16200 add_AT_die_ref (die, DW_AT_containing_type,
16201 lookup_type_die (DECL_CONTEXT (func_decl)));
16205 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
16206 given decl. This used to be a vendor extension until after DWARF 4
16207 standardized it. */
16209 static void
16210 add_linkage_attr (dw_die_ref die, tree decl)
16212 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
16214 /* Mimic what assemble_name_raw does with a leading '*'. */
16215 if (name[0] == '*')
16216 name = &name[1];
16218 if (dwarf_version >= 4)
16219 add_AT_string (die, DW_AT_linkage_name, name);
16220 else
16221 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
16224 /* Add source coordinate attributes for the given decl. */
16226 static void
16227 add_src_coords_attributes (dw_die_ref die, tree decl)
16229 expanded_location s;
16231 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
16232 return;
16233 s = expand_location (DECL_SOURCE_LOCATION (decl));
16234 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
16235 add_AT_unsigned (die, DW_AT_decl_line, s.line);
16238 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
16240 static void
16241 add_linkage_name (dw_die_ref die, tree decl)
16243 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
16244 && TREE_PUBLIC (decl)
16245 && !DECL_ABSTRACT (decl)
16246 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
16247 && die->die_tag != DW_TAG_member)
16249 /* Defer until we have an assembler name set. */
16250 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
16252 limbo_die_node *asm_name;
16254 asm_name = ggc_alloc_cleared_limbo_die_node ();
16255 asm_name->die = die;
16256 asm_name->created_for = decl;
16257 asm_name->next = deferred_asm_name;
16258 deferred_asm_name = asm_name;
16260 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
16261 add_linkage_attr (die, decl);
16265 /* Add a DW_AT_name attribute and source coordinate attribute for the
16266 given decl, but only if it actually has a name. */
16268 static void
16269 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
16271 tree decl_name;
16273 decl_name = DECL_NAME (decl);
16274 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
16276 const char *name = dwarf2_name (decl, 0);
16277 if (name)
16278 add_name_attribute (die, name);
16279 if (! DECL_ARTIFICIAL (decl))
16280 add_src_coords_attributes (die, decl);
16282 add_linkage_name (die, decl);
16285 #ifdef VMS_DEBUGGING_INFO
16286 /* Get the function's name, as described by its RTL. This may be different
16287 from the DECL_NAME name used in the source file. */
16288 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
16290 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
16291 XEXP (DECL_RTL (decl), 0), false);
16292 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
16294 #endif /* VMS_DEBUGGING_INFO */
16297 #ifdef VMS_DEBUGGING_INFO
16298 /* Output the debug main pointer die for VMS */
16300 void
16301 dwarf2out_vms_debug_main_pointer (void)
16303 char label[MAX_ARTIFICIAL_LABEL_BYTES];
16304 dw_die_ref die;
16306 /* Allocate the VMS debug main subprogram die. */
16307 die = ggc_alloc_cleared_die_node ();
16308 die->die_tag = DW_TAG_subprogram;
16309 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
16310 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
16311 current_function_funcdef_no);
16312 add_AT_lbl_id (die, DW_AT_entry_pc, label);
16314 /* Make it the first child of comp_unit_die (). */
16315 die->die_parent = comp_unit_die ();
16316 if (comp_unit_die ()->die_child)
16318 die->die_sib = comp_unit_die ()->die_child->die_sib;
16319 comp_unit_die ()->die_child->die_sib = die;
16321 else
16323 die->die_sib = die;
16324 comp_unit_die ()->die_child = die;
16327 #endif /* VMS_DEBUGGING_INFO */
16329 /* Push a new declaration scope. */
16331 static void
16332 push_decl_scope (tree scope)
16334 vec_safe_push (decl_scope_table, scope);
16337 /* Pop a declaration scope. */
16339 static inline void
16340 pop_decl_scope (void)
16342 decl_scope_table->pop ();
16345 /* walk_tree helper function for uses_local_type, below. */
16347 static tree
16348 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
16350 if (!TYPE_P (*tp))
16351 *walk_subtrees = 0;
16352 else
16354 tree name = TYPE_NAME (*tp);
16355 if (name && DECL_P (name) && decl_function_context (name))
16356 return *tp;
16358 return NULL_TREE;
16361 /* If TYPE involves a function-local type (including a local typedef to a
16362 non-local type), returns that type; otherwise returns NULL_TREE. */
16364 static tree
16365 uses_local_type (tree type)
16367 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
16368 return used;
16371 /* Return the DIE for the scope that immediately contains this type.
16372 Non-named types that do not involve a function-local type get global
16373 scope. Named types nested in namespaces or other types get their
16374 containing scope. All other types (i.e. function-local named types) get
16375 the current active scope. */
16377 static dw_die_ref
16378 scope_die_for (tree t, dw_die_ref context_die)
16380 dw_die_ref scope_die = NULL;
16381 tree containing_scope;
16383 /* Non-types always go in the current scope. */
16384 gcc_assert (TYPE_P (t));
16386 /* Use the scope of the typedef, rather than the scope of the type
16387 it refers to. */
16388 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
16389 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
16390 else
16391 containing_scope = TYPE_CONTEXT (t);
16393 /* Use the containing namespace if there is one. */
16394 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
16396 if (context_die == lookup_decl_die (containing_scope))
16397 /* OK */;
16398 else if (debug_info_level > DINFO_LEVEL_TERSE)
16399 context_die = get_context_die (containing_scope);
16400 else
16401 containing_scope = NULL_TREE;
16404 /* Ignore function type "scopes" from the C frontend. They mean that
16405 a tagged type is local to a parmlist of a function declarator, but
16406 that isn't useful to DWARF. */
16407 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
16408 containing_scope = NULL_TREE;
16410 if (SCOPE_FILE_SCOPE_P (containing_scope))
16412 /* If T uses a local type keep it local as well, to avoid references
16413 to function-local DIEs from outside the function. */
16414 if (current_function_decl && uses_local_type (t))
16415 scope_die = context_die;
16416 else
16417 scope_die = comp_unit_die ();
16419 else if (TYPE_P (containing_scope))
16421 /* For types, we can just look up the appropriate DIE. */
16422 if (debug_info_level > DINFO_LEVEL_TERSE)
16423 scope_die = get_context_die (containing_scope);
16424 else
16426 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
16427 if (scope_die == NULL)
16428 scope_die = comp_unit_die ();
16431 else
16432 scope_die = context_die;
16434 return scope_die;
16437 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
16439 static inline int
16440 local_scope_p (dw_die_ref context_die)
16442 for (; context_die; context_die = context_die->die_parent)
16443 if (context_die->die_tag == DW_TAG_inlined_subroutine
16444 || context_die->die_tag == DW_TAG_subprogram)
16445 return 1;
16447 return 0;
16450 /* Returns nonzero if CONTEXT_DIE is a class. */
16452 static inline int
16453 class_scope_p (dw_die_ref context_die)
16455 return (context_die
16456 && (context_die->die_tag == DW_TAG_structure_type
16457 || context_die->die_tag == DW_TAG_class_type
16458 || context_die->die_tag == DW_TAG_interface_type
16459 || context_die->die_tag == DW_TAG_union_type));
16462 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
16463 whether or not to treat a DIE in this context as a declaration. */
16465 static inline int
16466 class_or_namespace_scope_p (dw_die_ref context_die)
16468 return (class_scope_p (context_die)
16469 || (context_die && context_die->die_tag == DW_TAG_namespace));
16472 /* Many forms of DIEs require a "type description" attribute. This
16473 routine locates the proper "type descriptor" die for the type given
16474 by 'type', and adds a DW_AT_type attribute below the given die. */
16476 static void
16477 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
16478 int decl_volatile, dw_die_ref context_die)
16480 enum tree_code code = TREE_CODE (type);
16481 dw_die_ref type_die = NULL;
16483 /* ??? If this type is an unnamed subrange type of an integral, floating-point
16484 or fixed-point type, use the inner type. This is because we have no
16485 support for unnamed types in base_type_die. This can happen if this is
16486 an Ada subrange type. Correct solution is emit a subrange type die. */
16487 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
16488 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
16489 type = TREE_TYPE (type), code = TREE_CODE (type);
16491 if (code == ERROR_MARK
16492 /* Handle a special case. For functions whose return type is void, we
16493 generate *no* type attribute. (Note that no object may have type
16494 `void', so this only applies to function return types). */
16495 || code == VOID_TYPE)
16496 return;
16498 type_die = modified_type_die (type,
16499 decl_const || TYPE_READONLY (type),
16500 decl_volatile || TYPE_VOLATILE (type),
16501 context_die);
16503 if (type_die != NULL)
16504 add_AT_die_ref (object_die, DW_AT_type, type_die);
16507 /* Given an object die, add the calling convention attribute for the
16508 function call type. */
16509 static void
16510 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
16512 enum dwarf_calling_convention value = DW_CC_normal;
16514 value = ((enum dwarf_calling_convention)
16515 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
16517 if (is_fortran ()
16518 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
16520 /* DWARF 2 doesn't provide a way to identify a program's source-level
16521 entry point. DW_AT_calling_convention attributes are only meant
16522 to describe functions' calling conventions. However, lacking a
16523 better way to signal the Fortran main program, we used this for
16524 a long time, following existing custom. Now, DWARF 4 has
16525 DW_AT_main_subprogram, which we add below, but some tools still
16526 rely on the old way, which we thus keep. */
16527 value = DW_CC_program;
16529 if (dwarf_version >= 4 || !dwarf_strict)
16530 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
16533 /* Only add the attribute if the backend requests it, and
16534 is not DW_CC_normal. */
16535 if (value && (value != DW_CC_normal))
16536 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
16539 /* Given a tree pointer to a struct, class, union, or enum type node, return
16540 a pointer to the (string) tag name for the given type, or zero if the type
16541 was declared without a tag. */
16543 static const char *
16544 type_tag (const_tree type)
16546 const char *name = 0;
16548 if (TYPE_NAME (type) != 0)
16550 tree t = 0;
16552 /* Find the IDENTIFIER_NODE for the type name. */
16553 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
16554 && !TYPE_NAMELESS (type))
16555 t = TYPE_NAME (type);
16557 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
16558 a TYPE_DECL node, regardless of whether or not a `typedef' was
16559 involved. */
16560 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
16561 && ! DECL_IGNORED_P (TYPE_NAME (type)))
16563 /* We want to be extra verbose. Don't call dwarf_name if
16564 DECL_NAME isn't set. The default hook for decl_printable_name
16565 doesn't like that, and in this context it's correct to return
16566 0, instead of "<anonymous>" or the like. */
16567 if (DECL_NAME (TYPE_NAME (type))
16568 && !DECL_NAMELESS (TYPE_NAME (type)))
16569 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
16572 /* Now get the name as a string, or invent one. */
16573 if (!name && t != 0)
16574 name = IDENTIFIER_POINTER (t);
16577 return (name == 0 || *name == '\0') ? 0 : name;
16580 /* Return the type associated with a data member, make a special check
16581 for bit field types. */
16583 static inline tree
16584 member_declared_type (const_tree member)
16586 return (DECL_BIT_FIELD_TYPE (member)
16587 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
16590 /* Get the decl's label, as described by its RTL. This may be different
16591 from the DECL_NAME name used in the source file. */
16593 #if 0
16594 static const char *
16595 decl_start_label (tree decl)
16597 rtx x;
16598 const char *fnname;
16600 x = DECL_RTL (decl);
16601 gcc_assert (MEM_P (x));
16603 x = XEXP (x, 0);
16604 gcc_assert (GET_CODE (x) == SYMBOL_REF);
16606 fnname = XSTR (x, 0);
16607 return fnname;
16609 #endif
16611 /* These routines generate the internal representation of the DIE's for
16612 the compilation unit. Debugging information is collected by walking
16613 the declaration trees passed in from dwarf2out_decl(). */
16615 static void
16616 gen_array_type_die (tree type, dw_die_ref context_die)
16618 dw_die_ref scope_die = scope_die_for (type, context_die);
16619 dw_die_ref array_die;
16621 /* GNU compilers represent multidimensional array types as sequences of one
16622 dimensional array types whose element types are themselves array types.
16623 We sometimes squish that down to a single array_type DIE with multiple
16624 subscripts in the Dwarf debugging info. The draft Dwarf specification
16625 say that we are allowed to do this kind of compression in C, because
16626 there is no difference between an array of arrays and a multidimensional
16627 array. We don't do this for Ada to remain as close as possible to the
16628 actual representation, which is especially important against the language
16629 flexibilty wrt arrays of variable size. */
16631 bool collapse_nested_arrays = !is_ada ();
16632 tree element_type;
16634 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
16635 DW_TAG_string_type doesn't have DW_AT_type attribute). */
16636 if (TYPE_STRING_FLAG (type)
16637 && TREE_CODE (type) == ARRAY_TYPE
16638 && is_fortran ()
16639 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
16641 HOST_WIDE_INT size;
16643 array_die = new_die (DW_TAG_string_type, scope_die, type);
16644 add_name_attribute (array_die, type_tag (type));
16645 equate_type_number_to_die (type, array_die);
16646 size = int_size_in_bytes (type);
16647 if (size >= 0)
16648 add_AT_unsigned (array_die, DW_AT_byte_size, size);
16649 else if (TYPE_DOMAIN (type) != NULL_TREE
16650 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
16651 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
16653 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
16654 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
16656 size = int_size_in_bytes (TREE_TYPE (szdecl));
16657 if (loc && size > 0)
16659 add_AT_location_description (array_die, DW_AT_string_length, loc);
16660 if (size != DWARF2_ADDR_SIZE)
16661 add_AT_unsigned (array_die, DW_AT_byte_size, size);
16664 return;
16667 array_die = new_die (DW_TAG_array_type, scope_die, type);
16668 add_name_attribute (array_die, type_tag (type));
16669 equate_type_number_to_die (type, array_die);
16671 if (TREE_CODE (type) == VECTOR_TYPE)
16672 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
16674 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
16675 if (is_fortran ()
16676 && TREE_CODE (type) == ARRAY_TYPE
16677 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
16678 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
16679 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16681 #if 0
16682 /* We default the array ordering. SDB will probably do
16683 the right things even if DW_AT_ordering is not present. It's not even
16684 an issue until we start to get into multidimensional arrays anyway. If
16685 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
16686 then we'll have to put the DW_AT_ordering attribute back in. (But if
16687 and when we find out that we need to put these in, we will only do so
16688 for multidimensional arrays. */
16689 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
16690 #endif
16692 if (TREE_CODE (type) == VECTOR_TYPE)
16694 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
16695 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
16696 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
16697 add_bound_info (subrange_die, DW_AT_upper_bound,
16698 size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
16700 else
16701 add_subscript_info (array_die, type, collapse_nested_arrays);
16703 /* Add representation of the type of the elements of this array type and
16704 emit the corresponding DIE if we haven't done it already. */
16705 element_type = TREE_TYPE (type);
16706 if (collapse_nested_arrays)
16707 while (TREE_CODE (element_type) == ARRAY_TYPE)
16709 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
16710 break;
16711 element_type = TREE_TYPE (element_type);
16714 add_type_attribute (array_die, element_type, 0, 0, context_die);
16716 add_gnat_descriptive_type_attribute (array_die, type, context_die);
16717 if (TYPE_ARTIFICIAL (type))
16718 add_AT_flag (array_die, DW_AT_artificial, 1);
16720 if (get_AT (array_die, DW_AT_name))
16721 add_pubtype (type, array_die);
16724 static dw_loc_descr_ref
16725 descr_info_loc (tree val, tree base_decl)
16727 HOST_WIDE_INT size;
16728 dw_loc_descr_ref loc, loc2;
16729 enum dwarf_location_atom op;
16731 if (val == base_decl)
16732 return new_loc_descr (DW_OP_push_object_address, 0, 0);
16734 switch (TREE_CODE (val))
16736 CASE_CONVERT:
16737 return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16738 case VAR_DECL:
16739 return loc_descriptor_from_tree (val, 0);
16740 case INTEGER_CST:
16741 if (host_integerp (val, 0))
16742 return int_loc_descriptor (tree_low_cst (val, 0));
16743 break;
16744 case INDIRECT_REF:
16745 size = int_size_in_bytes (TREE_TYPE (val));
16746 if (size < 0)
16747 break;
16748 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16749 if (!loc)
16750 break;
16751 if (size == DWARF2_ADDR_SIZE)
16752 add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
16753 else
16754 add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
16755 return loc;
16756 case POINTER_PLUS_EXPR:
16757 case PLUS_EXPR:
16758 if (host_integerp (TREE_OPERAND (val, 1), 1)
16759 && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
16760 < 16384)
16762 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16763 if (!loc)
16764 break;
16765 loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
16767 else
16769 op = DW_OP_plus;
16770 do_binop:
16771 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16772 if (!loc)
16773 break;
16774 loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
16775 if (!loc2)
16776 break;
16777 add_loc_descr (&loc, loc2);
16778 add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
16780 return loc;
16781 case MINUS_EXPR:
16782 op = DW_OP_minus;
16783 goto do_binop;
16784 case MULT_EXPR:
16785 op = DW_OP_mul;
16786 goto do_binop;
16787 case EQ_EXPR:
16788 op = DW_OP_eq;
16789 goto do_binop;
16790 case NE_EXPR:
16791 op = DW_OP_ne;
16792 goto do_binop;
16793 default:
16794 break;
16796 return NULL;
16799 static void
16800 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
16801 tree val, tree base_decl)
16803 dw_loc_descr_ref loc;
16805 if (host_integerp (val, 0))
16807 add_AT_unsigned (die, attr, tree_low_cst (val, 0));
16808 return;
16811 loc = descr_info_loc (val, base_decl);
16812 if (!loc)
16813 return;
16815 add_AT_loc (die, attr, loc);
16818 /* This routine generates DIE for array with hidden descriptor, details
16819 are filled into *info by a langhook. */
16821 static void
16822 gen_descr_array_type_die (tree type, struct array_descr_info *info,
16823 dw_die_ref context_die)
16825 dw_die_ref scope_die = scope_die_for (type, context_die);
16826 dw_die_ref array_die;
16827 int dim;
16829 array_die = new_die (DW_TAG_array_type, scope_die, type);
16830 add_name_attribute (array_die, type_tag (type));
16831 equate_type_number_to_die (type, array_die);
16833 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
16834 if (is_fortran ()
16835 && info->ndimensions >= 2)
16836 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16838 if (info->data_location)
16839 add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
16840 info->base_decl);
16841 if (info->associated)
16842 add_descr_info_field (array_die, DW_AT_associated, info->associated,
16843 info->base_decl);
16844 if (info->allocated)
16845 add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
16846 info->base_decl);
16848 for (dim = 0; dim < info->ndimensions; dim++)
16850 dw_die_ref subrange_die
16851 = new_die (DW_TAG_subrange_type, array_die, NULL);
16853 if (info->dimen[dim].lower_bound)
16855 /* If it is the default value, omit it. */
16856 int dflt;
16858 if (host_integerp (info->dimen[dim].lower_bound, 0)
16859 && (dflt = lower_bound_default ()) != -1
16860 && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
16862 else
16863 add_descr_info_field (subrange_die, DW_AT_lower_bound,
16864 info->dimen[dim].lower_bound,
16865 info->base_decl);
16867 if (info->dimen[dim].upper_bound)
16868 add_descr_info_field (subrange_die, DW_AT_upper_bound,
16869 info->dimen[dim].upper_bound,
16870 info->base_decl);
16871 if (info->dimen[dim].stride)
16872 add_descr_info_field (subrange_die, DW_AT_byte_stride,
16873 info->dimen[dim].stride,
16874 info->base_decl);
16877 gen_type_die (info->element_type, context_die);
16878 add_type_attribute (array_die, info->element_type, 0, 0, context_die);
16880 if (get_AT (array_die, DW_AT_name))
16881 add_pubtype (type, array_die);
16884 #if 0
16885 static void
16886 gen_entry_point_die (tree decl, dw_die_ref context_die)
16888 tree origin = decl_ultimate_origin (decl);
16889 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
16891 if (origin != NULL)
16892 add_abstract_origin_attribute (decl_die, origin);
16893 else
16895 add_name_and_src_coords_attributes (decl_die, decl);
16896 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
16897 0, 0, context_die);
16900 if (DECL_ABSTRACT (decl))
16901 equate_decl_number_to_die (decl, decl_die);
16902 else
16903 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
16905 #endif
16907 /* Walk through the list of incomplete types again, trying once more to
16908 emit full debugging info for them. */
16910 static void
16911 retry_incomplete_types (void)
16913 int i;
16915 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
16916 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
16917 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
16920 /* Determine what tag to use for a record type. */
16922 static enum dwarf_tag
16923 record_type_tag (tree type)
16925 if (! lang_hooks.types.classify_record)
16926 return DW_TAG_structure_type;
16928 switch (lang_hooks.types.classify_record (type))
16930 case RECORD_IS_STRUCT:
16931 return DW_TAG_structure_type;
16933 case RECORD_IS_CLASS:
16934 return DW_TAG_class_type;
16936 case RECORD_IS_INTERFACE:
16937 if (dwarf_version >= 3 || !dwarf_strict)
16938 return DW_TAG_interface_type;
16939 return DW_TAG_structure_type;
16941 default:
16942 gcc_unreachable ();
16946 /* Generate a DIE to represent an enumeration type. Note that these DIEs
16947 include all of the information about the enumeration values also. Each
16948 enumerated type name/value is listed as a child of the enumerated type
16949 DIE. */
16951 static dw_die_ref
16952 gen_enumeration_type_die (tree type, dw_die_ref context_die)
16954 dw_die_ref type_die = lookup_type_die (type);
16956 if (type_die == NULL)
16958 type_die = new_die (DW_TAG_enumeration_type,
16959 scope_die_for (type, context_die), type);
16960 equate_type_number_to_die (type, type_die);
16961 add_name_attribute (type_die, type_tag (type));
16962 if (dwarf_version >= 4 || !dwarf_strict)
16964 if (ENUM_IS_SCOPED (type))
16965 add_AT_flag (type_die, DW_AT_enum_class, 1);
16966 if (ENUM_IS_OPAQUE (type))
16967 add_AT_flag (type_die, DW_AT_declaration, 1);
16970 else if (! TYPE_SIZE (type))
16971 return type_die;
16972 else
16973 remove_AT (type_die, DW_AT_declaration);
16975 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
16976 given enum type is incomplete, do not generate the DW_AT_byte_size
16977 attribute or the DW_AT_element_list attribute. */
16978 if (TYPE_SIZE (type))
16980 tree link;
16982 TREE_ASM_WRITTEN (type) = 1;
16983 add_byte_size_attribute (type_die, type);
16984 if (TYPE_STUB_DECL (type) != NULL_TREE)
16986 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
16987 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
16990 /* If the first reference to this type was as the return type of an
16991 inline function, then it may not have a parent. Fix this now. */
16992 if (type_die->die_parent == NULL)
16993 add_child_die (scope_die_for (type, context_die), type_die);
16995 for (link = TYPE_VALUES (type);
16996 link != NULL; link = TREE_CHAIN (link))
16998 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
16999 tree value = TREE_VALUE (link);
17001 add_name_attribute (enum_die,
17002 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17004 if (TREE_CODE (value) == CONST_DECL)
17005 value = DECL_INITIAL (value);
17007 if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
17008 /* DWARF2 does not provide a way of indicating whether or
17009 not enumeration constants are signed or unsigned. GDB
17010 always assumes the values are signed, so we output all
17011 values as if they were signed. That means that
17012 enumeration constants with very large unsigned values
17013 will appear to have negative values in the debugger. */
17014 add_AT_int (enum_die, DW_AT_const_value,
17015 tree_low_cst (value, tree_int_cst_sgn (value) > 0));
17018 add_gnat_descriptive_type_attribute (type_die, type, context_die);
17019 if (TYPE_ARTIFICIAL (type))
17020 add_AT_flag (type_die, DW_AT_artificial, 1);
17022 else
17023 add_AT_flag (type_die, DW_AT_declaration, 1);
17025 add_pubtype (type, type_die);
17027 return type_die;
17030 /* Generate a DIE to represent either a real live formal parameter decl or to
17031 represent just the type of some formal parameter position in some function
17032 type.
17034 Note that this routine is a bit unusual because its argument may be a
17035 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17036 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17037 node. If it's the former then this function is being called to output a
17038 DIE to represent a formal parameter object (or some inlining thereof). If
17039 it's the latter, then this function is only being called to output a
17040 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17041 argument type of some subprogram type.
17042 If EMIT_NAME_P is true, name and source coordinate attributes
17043 are emitted. */
17045 static dw_die_ref
17046 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17047 dw_die_ref context_die)
17049 tree node_or_origin = node ? node : origin;
17050 tree ultimate_origin;
17051 dw_die_ref parm_die
17052 = new_die (DW_TAG_formal_parameter, context_die, node);
17054 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
17056 case tcc_declaration:
17057 ultimate_origin = decl_ultimate_origin (node_or_origin);
17058 if (node || ultimate_origin)
17059 origin = ultimate_origin;
17060 if (origin != NULL)
17061 add_abstract_origin_attribute (parm_die, origin);
17062 else if (emit_name_p)
17063 add_name_and_src_coords_attributes (parm_die, node);
17064 if (origin == NULL
17065 || (! DECL_ABSTRACT (node_or_origin)
17066 && variably_modified_type_p (TREE_TYPE (node_or_origin),
17067 decl_function_context
17068 (node_or_origin))))
17070 tree type = TREE_TYPE (node_or_origin);
17071 if (decl_by_reference_p (node_or_origin))
17072 add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
17073 context_die);
17074 else
17075 add_type_attribute (parm_die, type,
17076 TREE_READONLY (node_or_origin),
17077 TREE_THIS_VOLATILE (node_or_origin),
17078 context_die);
17080 if (origin == NULL && DECL_ARTIFICIAL (node))
17081 add_AT_flag (parm_die, DW_AT_artificial, 1);
17083 if (node && node != origin)
17084 equate_decl_number_to_die (node, parm_die);
17085 if (! DECL_ABSTRACT (node_or_origin))
17086 add_location_or_const_value_attribute (parm_die, node_or_origin,
17087 node == NULL, DW_AT_location);
17089 break;
17091 case tcc_type:
17092 /* We were called with some kind of a ..._TYPE node. */
17093 add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
17094 break;
17096 default:
17097 gcc_unreachable ();
17100 return parm_die;
17103 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
17104 children DW_TAG_formal_parameter DIEs representing the arguments of the
17105 parameter pack.
17107 PARM_PACK must be a function parameter pack.
17108 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
17109 must point to the subsequent arguments of the function PACK_ARG belongs to.
17110 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
17111 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
17112 following the last one for which a DIE was generated. */
17114 static dw_die_ref
17115 gen_formal_parameter_pack_die (tree parm_pack,
17116 tree pack_arg,
17117 dw_die_ref subr_die,
17118 tree *next_arg)
17120 tree arg;
17121 dw_die_ref parm_pack_die;
17123 gcc_assert (parm_pack
17124 && lang_hooks.function_parameter_pack_p (parm_pack)
17125 && subr_die);
17127 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
17128 add_src_coords_attributes (parm_pack_die, parm_pack);
17130 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
17132 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
17133 parm_pack))
17134 break;
17135 gen_formal_parameter_die (arg, NULL,
17136 false /* Don't emit name attribute. */,
17137 parm_pack_die);
17139 if (next_arg)
17140 *next_arg = arg;
17141 return parm_pack_die;
17144 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
17145 at the end of an (ANSI prototyped) formal parameters list. */
17147 static void
17148 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
17150 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
17153 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
17154 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
17155 parameters as specified in some function type specification (except for
17156 those which appear as part of a function *definition*). */
17158 static void
17159 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
17161 tree link;
17162 tree formal_type = NULL;
17163 tree first_parm_type;
17164 tree arg;
17166 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
17168 arg = DECL_ARGUMENTS (function_or_method_type);
17169 function_or_method_type = TREE_TYPE (function_or_method_type);
17171 else
17172 arg = NULL_TREE;
17174 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
17176 /* Make our first pass over the list of formal parameter types and output a
17177 DW_TAG_formal_parameter DIE for each one. */
17178 for (link = first_parm_type; link; )
17180 dw_die_ref parm_die;
17182 formal_type = TREE_VALUE (link);
17183 if (formal_type == void_type_node)
17184 break;
17186 /* Output a (nameless) DIE to represent the formal parameter itself. */
17187 parm_die = gen_formal_parameter_die (formal_type, NULL,
17188 true /* Emit name attribute. */,
17189 context_die);
17190 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
17191 && link == first_parm_type)
17193 add_AT_flag (parm_die, DW_AT_artificial, 1);
17194 if (dwarf_version >= 3 || !dwarf_strict)
17195 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
17197 else if (arg && DECL_ARTIFICIAL (arg))
17198 add_AT_flag (parm_die, DW_AT_artificial, 1);
17200 link = TREE_CHAIN (link);
17201 if (arg)
17202 arg = DECL_CHAIN (arg);
17205 /* If this function type has an ellipsis, add a
17206 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
17207 if (formal_type != void_type_node)
17208 gen_unspecified_parameters_die (function_or_method_type, context_die);
17210 /* Make our second (and final) pass over the list of formal parameter types
17211 and output DIEs to represent those types (as necessary). */
17212 for (link = TYPE_ARG_TYPES (function_or_method_type);
17213 link && TREE_VALUE (link);
17214 link = TREE_CHAIN (link))
17215 gen_type_die (TREE_VALUE (link), context_die);
17218 /* We want to generate the DIE for TYPE so that we can generate the
17219 die for MEMBER, which has been defined; we will need to refer back
17220 to the member declaration nested within TYPE. If we're trying to
17221 generate minimal debug info for TYPE, processing TYPE won't do the
17222 trick; we need to attach the member declaration by hand. */
17224 static void
17225 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
17227 gen_type_die (type, context_die);
17229 /* If we're trying to avoid duplicate debug info, we may not have
17230 emitted the member decl for this function. Emit it now. */
17231 if (TYPE_STUB_DECL (type)
17232 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
17233 && ! lookup_decl_die (member))
17235 dw_die_ref type_die;
17236 gcc_assert (!decl_ultimate_origin (member));
17238 push_decl_scope (type);
17239 type_die = lookup_type_die_strip_naming_typedef (type);
17240 if (TREE_CODE (member) == FUNCTION_DECL)
17241 gen_subprogram_die (member, type_die);
17242 else if (TREE_CODE (member) == FIELD_DECL)
17244 /* Ignore the nameless fields that are used to skip bits but handle
17245 C++ anonymous unions and structs. */
17246 if (DECL_NAME (member) != NULL_TREE
17247 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
17248 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
17250 gen_type_die (member_declared_type (member), type_die);
17251 gen_field_die (member, type_die);
17254 else
17255 gen_variable_die (member, NULL_TREE, type_die);
17257 pop_decl_scope ();
17261 /* Forward declare these functions, because they are mutually recursive
17262 with their set_block_* pairing functions. */
17263 static void set_decl_origin_self (tree);
17264 static void set_decl_abstract_flags (tree, int);
17266 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
17267 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
17268 that it points to the node itself, thus indicating that the node is its
17269 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
17270 the given node is NULL, recursively descend the decl/block tree which
17271 it is the root of, and for each other ..._DECL or BLOCK node contained
17272 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
17273 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
17274 values to point to themselves. */
17276 static void
17277 set_block_origin_self (tree stmt)
17279 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
17281 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
17284 tree local_decl;
17286 for (local_decl = BLOCK_VARS (stmt);
17287 local_decl != NULL_TREE;
17288 local_decl = DECL_CHAIN (local_decl))
17289 if (! DECL_EXTERNAL (local_decl))
17290 set_decl_origin_self (local_decl); /* Potential recursion. */
17294 tree subblock;
17296 for (subblock = BLOCK_SUBBLOCKS (stmt);
17297 subblock != NULL_TREE;
17298 subblock = BLOCK_CHAIN (subblock))
17299 set_block_origin_self (subblock); /* Recurse. */
17304 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
17305 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
17306 node to so that it points to the node itself, thus indicating that the
17307 node represents its own (abstract) origin. Additionally, if the
17308 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
17309 the decl/block tree of which the given node is the root of, and for
17310 each other ..._DECL or BLOCK node contained therein whose
17311 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
17312 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
17313 point to themselves. */
17315 static void
17316 set_decl_origin_self (tree decl)
17318 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
17320 DECL_ABSTRACT_ORIGIN (decl) = decl;
17321 if (TREE_CODE (decl) == FUNCTION_DECL)
17323 tree arg;
17325 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17326 DECL_ABSTRACT_ORIGIN (arg) = arg;
17327 if (DECL_INITIAL (decl) != NULL_TREE
17328 && DECL_INITIAL (decl) != error_mark_node)
17329 set_block_origin_self (DECL_INITIAL (decl));
17334 /* Given a pointer to some BLOCK node, and a boolean value to set the
17335 "abstract" flags to, set that value into the BLOCK_ABSTRACT flag for
17336 the given block, and for all local decls and all local sub-blocks
17337 (recursively) which are contained therein. */
17339 static void
17340 set_block_abstract_flags (tree stmt, int setting)
17342 tree local_decl;
17343 tree subblock;
17344 unsigned int i;
17346 BLOCK_ABSTRACT (stmt) = setting;
17348 for (local_decl = BLOCK_VARS (stmt);
17349 local_decl != NULL_TREE;
17350 local_decl = DECL_CHAIN (local_decl))
17351 if (! DECL_EXTERNAL (local_decl))
17352 set_decl_abstract_flags (local_decl, setting);
17354 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
17356 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
17357 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
17358 || TREE_CODE (local_decl) == PARM_DECL)
17359 set_decl_abstract_flags (local_decl, setting);
17362 for (subblock = BLOCK_SUBBLOCKS (stmt);
17363 subblock != NULL_TREE;
17364 subblock = BLOCK_CHAIN (subblock))
17365 set_block_abstract_flags (subblock, setting);
17368 /* Given a pointer to some ..._DECL node, and a boolean value to set the
17369 "abstract" flags to, set that value into the DECL_ABSTRACT flag for the
17370 given decl, and (in the case where the decl is a FUNCTION_DECL) also
17371 set the abstract flags for all of the parameters, local vars, local
17372 blocks and sub-blocks (recursively) to the same setting. */
17374 static void
17375 set_decl_abstract_flags (tree decl, int setting)
17377 DECL_ABSTRACT (decl) = setting;
17378 if (TREE_CODE (decl) == FUNCTION_DECL)
17380 tree arg;
17382 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17383 DECL_ABSTRACT (arg) = setting;
17384 if (DECL_INITIAL (decl) != NULL_TREE
17385 && DECL_INITIAL (decl) != error_mark_node)
17386 set_block_abstract_flags (DECL_INITIAL (decl), setting);
17390 /* Generate the DWARF2 info for the "abstract" instance of a function which we
17391 may later generate inlined and/or out-of-line instances of. */
17393 static void
17394 dwarf2out_abstract_function (tree decl)
17396 dw_die_ref old_die;
17397 tree save_fn;
17398 tree context;
17399 int was_abstract;
17400 htab_t old_decl_loc_table;
17401 htab_t old_cached_dw_loc_list_table;
17402 int old_call_site_count, old_tail_call_site_count;
17403 struct call_arg_loc_node *old_call_arg_locations;
17405 /* Make sure we have the actual abstract inline, not a clone. */
17406 decl = DECL_ORIGIN (decl);
17408 old_die = lookup_decl_die (decl);
17409 if (old_die && get_AT (old_die, DW_AT_inline))
17410 /* We've already generated the abstract instance. */
17411 return;
17413 /* We can be called while recursively when seeing block defining inlined subroutine
17414 DIE. Be sure to not clobber the outer location table nor use it or we would
17415 get locations in abstract instantces. */
17416 old_decl_loc_table = decl_loc_table;
17417 decl_loc_table = NULL;
17418 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
17419 cached_dw_loc_list_table = NULL;
17420 old_call_arg_locations = call_arg_locations;
17421 call_arg_locations = NULL;
17422 old_call_site_count = call_site_count;
17423 call_site_count = -1;
17424 old_tail_call_site_count = tail_call_site_count;
17425 tail_call_site_count = -1;
17427 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
17428 we don't get confused by DECL_ABSTRACT. */
17429 if (debug_info_level > DINFO_LEVEL_TERSE)
17431 context = decl_class_context (decl);
17432 if (context)
17433 gen_type_die_for_member
17434 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
17437 /* Pretend we've just finished compiling this function. */
17438 save_fn = current_function_decl;
17439 current_function_decl = decl;
17441 was_abstract = DECL_ABSTRACT (decl);
17442 set_decl_abstract_flags (decl, 1);
17443 dwarf2out_decl (decl);
17444 if (! was_abstract)
17445 set_decl_abstract_flags (decl, 0);
17447 current_function_decl = save_fn;
17448 decl_loc_table = old_decl_loc_table;
17449 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
17450 call_arg_locations = old_call_arg_locations;
17451 call_site_count = old_call_site_count;
17452 tail_call_site_count = old_tail_call_site_count;
17455 /* Helper function of premark_used_types() which gets called through
17456 htab_traverse.
17458 Marks the DIE of a given type in *SLOT as perennial, so it never gets
17459 marked as unused by prune_unused_types. */
17461 static int
17462 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
17464 tree type;
17465 dw_die_ref die;
17467 type = (tree) *slot;
17468 die = lookup_type_die (type);
17469 if (die != NULL)
17470 die->die_perennial_p = 1;
17471 return 1;
17474 /* Helper function of premark_types_used_by_global_vars which gets called
17475 through htab_traverse.
17477 Marks the DIE of a given type in *SLOT as perennial, so it never gets
17478 marked as unused by prune_unused_types. The DIE of the type is marked
17479 only if the global variable using the type will actually be emitted. */
17481 static int
17482 premark_types_used_by_global_vars_helper (void **slot,
17483 void *data ATTRIBUTE_UNUSED)
17485 struct types_used_by_vars_entry *entry;
17486 dw_die_ref die;
17488 entry = (struct types_used_by_vars_entry *) *slot;
17489 gcc_assert (entry->type != NULL
17490 && entry->var_decl != NULL);
17491 die = lookup_type_die (entry->type);
17492 if (die)
17494 /* Ask cgraph if the global variable really is to be emitted.
17495 If yes, then we'll keep the DIE of ENTRY->TYPE. */
17496 struct varpool_node *node = varpool_get_node (entry->var_decl);
17497 if (node && node->analyzed)
17499 die->die_perennial_p = 1;
17500 /* Keep the parent DIEs as well. */
17501 while ((die = die->die_parent) && die->die_perennial_p == 0)
17502 die->die_perennial_p = 1;
17505 return 1;
17508 /* Mark all members of used_types_hash as perennial. */
17510 static void
17511 premark_used_types (struct function *fun)
17513 if (fun && fun->used_types_hash)
17514 htab_traverse (fun->used_types_hash, premark_used_types_helper, NULL);
17517 /* Mark all members of types_used_by_vars_entry as perennial. */
17519 static void
17520 premark_types_used_by_global_vars (void)
17522 if (types_used_by_vars_hash)
17523 htab_traverse (types_used_by_vars_hash,
17524 premark_types_used_by_global_vars_helper, NULL);
17527 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
17528 for CA_LOC call arg loc node. */
17530 static dw_die_ref
17531 gen_call_site_die (tree decl, dw_die_ref subr_die,
17532 struct call_arg_loc_node *ca_loc)
17534 dw_die_ref stmt_die = NULL, die;
17535 tree block = ca_loc->block;
17537 while (block
17538 && block != DECL_INITIAL (decl)
17539 && TREE_CODE (block) == BLOCK)
17541 if (block_map.length () > BLOCK_NUMBER (block))
17542 stmt_die = block_map[BLOCK_NUMBER (block)];
17543 if (stmt_die)
17544 break;
17545 block = BLOCK_SUPERCONTEXT (block);
17547 if (stmt_die == NULL)
17548 stmt_die = subr_die;
17549 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
17550 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
17551 if (ca_loc->tail_call_p)
17552 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
17553 if (ca_loc->symbol_ref)
17555 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
17556 if (tdie)
17557 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
17558 else
17559 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
17561 return die;
17564 /* Generate a DIE to represent a declared function (either file-scope or
17565 block-local). */
17567 static void
17568 gen_subprogram_die (tree decl, dw_die_ref context_die)
17570 tree origin = decl_ultimate_origin (decl);
17571 dw_die_ref subr_die;
17572 tree outer_scope;
17573 dw_die_ref old_die = lookup_decl_die (decl);
17574 int declaration = (current_function_decl != decl
17575 || class_or_namespace_scope_p (context_die));
17577 premark_used_types (DECL_STRUCT_FUNCTION (decl));
17579 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
17580 started to generate the abstract instance of an inline, decided to output
17581 its containing class, and proceeded to emit the declaration of the inline
17582 from the member list for the class. If so, DECLARATION takes priority;
17583 we'll get back to the abstract instance when done with the class. */
17585 /* The class-scope declaration DIE must be the primary DIE. */
17586 if (origin && declaration && class_or_namespace_scope_p (context_die))
17588 origin = NULL;
17589 gcc_assert (!old_die);
17592 /* Now that the C++ front end lazily declares artificial member fns, we
17593 might need to retrofit the declaration into its class. */
17594 if (!declaration && !origin && !old_die
17595 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
17596 && !class_or_namespace_scope_p (context_die)
17597 && debug_info_level > DINFO_LEVEL_TERSE)
17598 old_die = force_decl_die (decl);
17600 if (origin != NULL)
17602 gcc_assert (!declaration || local_scope_p (context_die));
17604 /* Fixup die_parent for the abstract instance of a nested
17605 inline function. */
17606 if (old_die && old_die->die_parent == NULL)
17607 add_child_die (context_die, old_die);
17609 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17610 add_abstract_origin_attribute (subr_die, origin);
17611 /* This is where the actual code for a cloned function is.
17612 Let's emit linkage name attribute for it. This helps
17613 debuggers to e.g, set breakpoints into
17614 constructors/destructors when the user asks "break
17615 K::K". */
17616 add_linkage_name (subr_die, decl);
17618 else if (old_die)
17620 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17621 struct dwarf_file_data * file_index = lookup_filename (s.file);
17623 if (!get_AT_flag (old_die, DW_AT_declaration)
17624 /* We can have a normal definition following an inline one in the
17625 case of redefinition of GNU C extern inlines.
17626 It seems reasonable to use AT_specification in this case. */
17627 && !get_AT (old_die, DW_AT_inline))
17629 /* Detect and ignore this case, where we are trying to output
17630 something we have already output. */
17631 return;
17634 /* If the definition comes from the same place as the declaration,
17635 maybe use the old DIE. We always want the DIE for this function
17636 that has the *_pc attributes to be under comp_unit_die so the
17637 debugger can find it. We also need to do this for abstract
17638 instances of inlines, since the spec requires the out-of-line copy
17639 to have the same parent. For local class methods, this doesn't
17640 apply; we just use the old DIE. */
17641 if ((is_cu_die (old_die->die_parent) || context_die == NULL)
17642 && (DECL_ARTIFICIAL (decl)
17643 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
17644 && (get_AT_unsigned (old_die, DW_AT_decl_line)
17645 == (unsigned) s.line))))
17647 subr_die = old_die;
17649 /* Clear out the declaration attribute and the formal parameters.
17650 Do not remove all children, because it is possible that this
17651 declaration die was forced using force_decl_die(). In such
17652 cases die that forced declaration die (e.g. TAG_imported_module)
17653 is one of the children that we do not want to remove. */
17654 remove_AT (subr_die, DW_AT_declaration);
17655 remove_AT (subr_die, DW_AT_object_pointer);
17656 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
17658 else
17660 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17661 add_AT_specification (subr_die, old_die);
17662 add_pubname (decl, subr_die);
17663 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
17664 add_AT_file (subr_die, DW_AT_decl_file, file_index);
17665 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
17666 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
17669 else
17671 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17673 if (TREE_PUBLIC (decl))
17674 add_AT_flag (subr_die, DW_AT_external, 1);
17676 add_name_and_src_coords_attributes (subr_die, decl);
17677 add_pubname (decl, subr_die);
17678 if (debug_info_level > DINFO_LEVEL_TERSE)
17680 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
17681 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
17682 0, 0, context_die);
17685 add_pure_or_virtual_attribute (subr_die, decl);
17686 if (DECL_ARTIFICIAL (decl))
17687 add_AT_flag (subr_die, DW_AT_artificial, 1);
17689 add_accessibility_attribute (subr_die, decl);
17692 if (declaration)
17694 if (!old_die || !get_AT (old_die, DW_AT_inline))
17696 add_AT_flag (subr_die, DW_AT_declaration, 1);
17698 /* If this is an explicit function declaration then generate
17699 a DW_AT_explicit attribute. */
17700 if (lang_hooks.decls.function_decl_explicit_p (decl)
17701 && (dwarf_version >= 3 || !dwarf_strict))
17702 add_AT_flag (subr_die, DW_AT_explicit, 1);
17704 /* The first time we see a member function, it is in the context of
17705 the class to which it belongs. We make sure of this by emitting
17706 the class first. The next time is the definition, which is
17707 handled above. The two may come from the same source text.
17709 Note that force_decl_die() forces function declaration die. It is
17710 later reused to represent definition. */
17711 equate_decl_number_to_die (decl, subr_die);
17714 else if (DECL_ABSTRACT (decl))
17716 if (DECL_DECLARED_INLINE_P (decl))
17718 if (cgraph_function_possibly_inlined_p (decl))
17719 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
17720 else
17721 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
17723 else
17725 if (cgraph_function_possibly_inlined_p (decl))
17726 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
17727 else
17728 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
17731 if (DECL_DECLARED_INLINE_P (decl)
17732 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
17733 add_AT_flag (subr_die, DW_AT_artificial, 1);
17735 equate_decl_number_to_die (decl, subr_die);
17737 else if (!DECL_EXTERNAL (decl))
17739 HOST_WIDE_INT cfa_fb_offset;
17740 struct function *fun = DECL_STRUCT_FUNCTION (decl);
17742 if (!old_die || !get_AT (old_die, DW_AT_inline))
17743 equate_decl_number_to_die (decl, subr_die);
17745 gcc_checking_assert (fun);
17746 if (!flag_reorder_blocks_and_partition)
17748 dw_fde_ref fde = fun->fde;
17749 if (fde->dw_fde_begin)
17751 /* We have already generated the labels. */
17752 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
17753 fde->dw_fde_end, false);
17755 else
17757 /* Create start/end labels and add the range. */
17758 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
17759 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
17760 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
17761 current_function_funcdef_no);
17762 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
17763 current_function_funcdef_no);
17764 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
17765 false);
17768 #if VMS_DEBUGGING_INFO
17769 /* HP OpenVMS Industry Standard 64: DWARF Extensions
17770 Section 2.3 Prologue and Epilogue Attributes:
17771 When a breakpoint is set on entry to a function, it is generally
17772 desirable for execution to be suspended, not on the very first
17773 instruction of the function, but rather at a point after the
17774 function's frame has been set up, after any language defined local
17775 declaration processing has been completed, and before execution of
17776 the first statement of the function begins. Debuggers generally
17777 cannot properly determine where this point is. Similarly for a
17778 breakpoint set on exit from a function. The prologue and epilogue
17779 attributes allow a compiler to communicate the location(s) to use. */
17782 if (fde->dw_fde_vms_end_prologue)
17783 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
17784 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
17786 if (fde->dw_fde_vms_begin_epilogue)
17787 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
17788 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
17790 #endif
17793 else
17795 /* Generate pubnames entries for the split function code ranges. */
17796 dw_fde_ref fde = fun->fde;
17798 if (fde->dw_fde_second_begin)
17800 if (dwarf_version >= 3 || !dwarf_strict)
17802 /* We should use ranges for non-contiguous code section
17803 addresses. Use the actual code range for the initial
17804 section, since the HOT/COLD labels might precede an
17805 alignment offset. */
17806 bool range_list_added = false;
17807 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
17808 fde->dw_fde_end, &range_list_added,
17809 false);
17810 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
17811 fde->dw_fde_second_end,
17812 &range_list_added, false);
17813 if (range_list_added)
17814 add_ranges (NULL);
17816 else
17818 /* There is no real support in DW2 for this .. so we make
17819 a work-around. First, emit the pub name for the segment
17820 containing the function label. Then make and emit a
17821 simplified subprogram DIE for the second segment with the
17822 name pre-fixed by __hot/cold_sect_of_. We use the same
17823 linkage name for the second die so that gdb will find both
17824 sections when given "b foo". */
17825 const char *name = NULL;
17826 tree decl_name = DECL_NAME (decl);
17827 dw_die_ref seg_die;
17829 /* Do the 'primary' section. */
17830 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
17831 fde->dw_fde_end, false);
17833 /* Build a minimal DIE for the secondary section. */
17834 seg_die = new_die (DW_TAG_subprogram,
17835 subr_die->die_parent, decl);
17837 if (TREE_PUBLIC (decl))
17838 add_AT_flag (seg_die, DW_AT_external, 1);
17840 if (decl_name != NULL
17841 && IDENTIFIER_POINTER (decl_name) != NULL)
17843 name = dwarf2_name (decl, 1);
17844 if (! DECL_ARTIFICIAL (decl))
17845 add_src_coords_attributes (seg_die, decl);
17847 add_linkage_name (seg_die, decl);
17849 gcc_assert (name != NULL);
17850 add_pure_or_virtual_attribute (seg_die, decl);
17851 if (DECL_ARTIFICIAL (decl))
17852 add_AT_flag (seg_die, DW_AT_artificial, 1);
17854 name = concat ("__second_sect_of_", name, NULL);
17855 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
17856 fde->dw_fde_second_end, false);
17857 add_name_attribute (seg_die, name);
17858 if (want_pubnames ())
17859 add_pubname_string (name, seg_die);
17862 else
17863 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
17864 false);
17867 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
17869 /* We define the "frame base" as the function's CFA. This is more
17870 convenient for several reasons: (1) It's stable across the prologue
17871 and epilogue, which makes it better than just a frame pointer,
17872 (2) With dwarf3, there exists a one-byte encoding that allows us
17873 to reference the .debug_frame data by proxy, but failing that,
17874 (3) We can at least reuse the code inspection and interpretation
17875 code that determines the CFA position at various points in the
17876 function. */
17877 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
17879 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
17880 add_AT_loc (subr_die, DW_AT_frame_base, op);
17882 else
17884 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
17885 if (list->dw_loc_next)
17886 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
17887 else
17888 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
17891 /* Compute a displacement from the "steady-state frame pointer" to
17892 the CFA. The former is what all stack slots and argument slots
17893 will reference in the rtl; the latter is what we've told the
17894 debugger about. We'll need to adjust all frame_base references
17895 by this displacement. */
17896 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
17898 if (fun->static_chain_decl)
17899 add_AT_location_description (subr_die, DW_AT_static_link,
17900 loc_list_from_tree (fun->static_chain_decl, 2));
17903 /* Generate child dies for template paramaters. */
17904 if (debug_info_level > DINFO_LEVEL_TERSE)
17905 gen_generic_params_dies (decl);
17907 /* Now output descriptions of the arguments for this function. This gets
17908 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
17909 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
17910 `...' at the end of the formal parameter list. In order to find out if
17911 there was a trailing ellipsis or not, we must instead look at the type
17912 associated with the FUNCTION_DECL. This will be a node of type
17913 FUNCTION_TYPE. If the chain of type nodes hanging off of this
17914 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
17915 an ellipsis at the end. */
17917 /* In the case where we are describing a mere function declaration, all we
17918 need to do here (and all we *can* do here) is to describe the *types* of
17919 its formal parameters. */
17920 if (debug_info_level <= DINFO_LEVEL_TERSE)
17922 else if (declaration)
17923 gen_formal_types_die (decl, subr_die);
17924 else
17926 /* Generate DIEs to represent all known formal parameters. */
17927 tree parm = DECL_ARGUMENTS (decl);
17928 tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
17929 tree generic_decl_parm = generic_decl
17930 ? DECL_ARGUMENTS (generic_decl)
17931 : NULL;
17933 /* Now we want to walk the list of parameters of the function and
17934 emit their relevant DIEs.
17936 We consider the case of DECL being an instance of a generic function
17937 as well as it being a normal function.
17939 If DECL is an instance of a generic function we walk the
17940 parameters of the generic function declaration _and_ the parameters of
17941 DECL itself. This is useful because we want to emit specific DIEs for
17942 function parameter packs and those are declared as part of the
17943 generic function declaration. In that particular case,
17944 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
17945 That DIE has children DIEs representing the set of arguments
17946 of the pack. Note that the set of pack arguments can be empty.
17947 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
17948 children DIE.
17950 Otherwise, we just consider the parameters of DECL. */
17951 while (generic_decl_parm || parm)
17953 if (generic_decl_parm
17954 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
17955 gen_formal_parameter_pack_die (generic_decl_parm,
17956 parm, subr_die,
17957 &parm);
17958 else if (parm)
17960 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
17962 if (parm == DECL_ARGUMENTS (decl)
17963 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
17964 && parm_die
17965 && (dwarf_version >= 3 || !dwarf_strict))
17966 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
17968 parm = DECL_CHAIN (parm);
17971 if (generic_decl_parm)
17972 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
17975 /* Decide whether we need an unspecified_parameters DIE at the end.
17976 There are 2 more cases to do this for: 1) the ansi ... declaration -
17977 this is detectable when the end of the arg list is not a
17978 void_type_node 2) an unprototyped function declaration (not a
17979 definition). This just means that we have no info about the
17980 parameters at all. */
17981 if (prototype_p (TREE_TYPE (decl)))
17983 /* This is the prototyped case, check for.... */
17984 if (stdarg_p (TREE_TYPE (decl)))
17985 gen_unspecified_parameters_die (decl, subr_die);
17987 else if (DECL_INITIAL (decl) == NULL_TREE)
17988 gen_unspecified_parameters_die (decl, subr_die);
17991 /* Output Dwarf info for all of the stuff within the body of the function
17992 (if it has one - it may be just a declaration). */
17993 outer_scope = DECL_INITIAL (decl);
17995 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
17996 a function. This BLOCK actually represents the outermost binding contour
17997 for the function, i.e. the contour in which the function's formal
17998 parameters and labels get declared. Curiously, it appears that the front
17999 end doesn't actually put the PARM_DECL nodes for the current function onto
18000 the BLOCK_VARS list for this outer scope, but are strung off of the
18001 DECL_ARGUMENTS list for the function instead.
18003 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
18004 the LABEL_DECL nodes for the function however, and we output DWARF info
18005 for those in decls_for_scope. Just within the `outer_scope' there will be
18006 a BLOCK node representing the function's outermost pair of curly braces,
18007 and any blocks used for the base and member initializers of a C++
18008 constructor function. */
18009 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
18011 int call_site_note_count = 0;
18012 int tail_call_site_note_count = 0;
18014 /* Emit a DW_TAG_variable DIE for a named return value. */
18015 if (DECL_NAME (DECL_RESULT (decl)))
18016 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
18018 current_function_has_inlines = 0;
18019 decls_for_scope (outer_scope, subr_die, 0);
18021 if (call_arg_locations && !dwarf_strict)
18023 struct call_arg_loc_node *ca_loc;
18024 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
18026 dw_die_ref die = NULL;
18027 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
18028 rtx arg, next_arg;
18030 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
18031 arg; arg = next_arg)
18033 dw_loc_descr_ref reg, val;
18034 enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
18035 dw_die_ref cdie, tdie = NULL;
18037 next_arg = XEXP (arg, 1);
18038 if (REG_P (XEXP (XEXP (arg, 0), 0))
18039 && next_arg
18040 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
18041 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
18042 && REGNO (XEXP (XEXP (arg, 0), 0))
18043 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
18044 next_arg = XEXP (next_arg, 1);
18045 if (mode == VOIDmode)
18047 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
18048 if (mode == VOIDmode)
18049 mode = GET_MODE (XEXP (arg, 0));
18051 if (mode == VOIDmode || mode == BLKmode)
18052 continue;
18053 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
18055 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18056 tloc = XEXP (XEXP (arg, 0), 1);
18057 continue;
18059 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
18060 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
18062 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18063 tlocc = XEXP (XEXP (arg, 0), 1);
18064 continue;
18066 reg = NULL;
18067 if (REG_P (XEXP (XEXP (arg, 0), 0)))
18068 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
18069 VAR_INIT_STATUS_INITIALIZED);
18070 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
18072 rtx mem = XEXP (XEXP (arg, 0), 0);
18073 reg = mem_loc_descriptor (XEXP (mem, 0),
18074 get_address_mode (mem),
18075 GET_MODE (mem),
18076 VAR_INIT_STATUS_INITIALIZED);
18078 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
18079 == DEBUG_PARAMETER_REF)
18081 tree tdecl
18082 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
18083 tdie = lookup_decl_die (tdecl);
18084 if (tdie == NULL)
18085 continue;
18087 else
18088 continue;
18089 if (reg == NULL
18090 && GET_CODE (XEXP (XEXP (arg, 0), 0))
18091 != DEBUG_PARAMETER_REF)
18092 continue;
18093 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
18094 VOIDmode,
18095 VAR_INIT_STATUS_INITIALIZED);
18096 if (val == NULL)
18097 continue;
18098 if (die == NULL)
18099 die = gen_call_site_die (decl, subr_die, ca_loc);
18100 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
18101 NULL_TREE);
18102 if (reg != NULL)
18103 add_AT_loc (cdie, DW_AT_location, reg);
18104 else if (tdie != NULL)
18105 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
18106 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
18107 if (next_arg != XEXP (arg, 1))
18109 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
18110 if (mode == VOIDmode)
18111 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
18112 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
18113 0), 1),
18114 mode, VOIDmode,
18115 VAR_INIT_STATUS_INITIALIZED);
18116 if (val != NULL)
18117 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
18120 if (die == NULL
18121 && (ca_loc->symbol_ref || tloc))
18122 die = gen_call_site_die (decl, subr_die, ca_loc);
18123 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
18125 dw_loc_descr_ref tval = NULL;
18127 if (tloc != NULL_RTX)
18128 tval = mem_loc_descriptor (tloc,
18129 GET_MODE (tloc) == VOIDmode
18130 ? Pmode : GET_MODE (tloc),
18131 VOIDmode,
18132 VAR_INIT_STATUS_INITIALIZED);
18133 if (tval)
18134 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
18135 else if (tlocc != NULL_RTX)
18137 tval = mem_loc_descriptor (tlocc,
18138 GET_MODE (tlocc) == VOIDmode
18139 ? Pmode : GET_MODE (tlocc),
18140 VOIDmode,
18141 VAR_INIT_STATUS_INITIALIZED);
18142 if (tval)
18143 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
18144 tval);
18147 if (die != NULL)
18149 call_site_note_count++;
18150 if (ca_loc->tail_call_p)
18151 tail_call_site_note_count++;
18155 call_arg_locations = NULL;
18156 call_arg_loc_last = NULL;
18157 if (tail_call_site_count >= 0
18158 && tail_call_site_count == tail_call_site_note_count
18159 && !dwarf_strict)
18161 if (call_site_count >= 0
18162 && call_site_count == call_site_note_count)
18163 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
18164 else
18165 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
18167 call_site_count = -1;
18168 tail_call_site_count = -1;
18170 /* Add the calling convention attribute if requested. */
18171 add_calling_convention_attribute (subr_die, decl);
18175 /* Returns a hash value for X (which really is a die_struct). */
18177 static hashval_t
18178 common_block_die_table_hash (const void *x)
18180 const_dw_die_ref d = (const_dw_die_ref) x;
18181 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
18184 /* Return nonzero if decl_id and die_parent of die_struct X is the same
18185 as decl_id and die_parent of die_struct Y. */
18187 static int
18188 common_block_die_table_eq (const void *x, const void *y)
18190 const_dw_die_ref d = (const_dw_die_ref) x;
18191 const_dw_die_ref e = (const_dw_die_ref) y;
18192 return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
18195 /* Generate a DIE to represent a declared data object.
18196 Either DECL or ORIGIN must be non-null. */
18198 static void
18199 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
18201 HOST_WIDE_INT off = 0;
18202 tree com_decl;
18203 tree decl_or_origin = decl ? decl : origin;
18204 tree ultimate_origin;
18205 dw_die_ref var_die;
18206 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
18207 dw_die_ref origin_die;
18208 bool declaration = (DECL_EXTERNAL (decl_or_origin)
18209 || class_or_namespace_scope_p (context_die));
18210 bool specialization_p = false;
18212 ultimate_origin = decl_ultimate_origin (decl_or_origin);
18213 if (decl || ultimate_origin)
18214 origin = ultimate_origin;
18215 com_decl = fortran_common (decl_or_origin, &off);
18217 /* Symbol in common gets emitted as a child of the common block, in the form
18218 of a data member. */
18219 if (com_decl)
18221 dw_die_ref com_die;
18222 dw_loc_list_ref loc;
18223 die_node com_die_arg;
18225 var_die = lookup_decl_die (decl_or_origin);
18226 if (var_die)
18228 if (get_AT (var_die, DW_AT_location) == NULL)
18230 loc = loc_list_from_tree (com_decl, off ? 1 : 2);
18231 if (loc)
18233 if (off)
18235 /* Optimize the common case. */
18236 if (single_element_loc_list_p (loc)
18237 && loc->expr->dw_loc_opc == DW_OP_addr
18238 && loc->expr->dw_loc_next == NULL
18239 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
18240 == SYMBOL_REF)
18242 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18243 loc->expr->dw_loc_oprnd1.v.val_addr
18244 = plus_constant (GET_MODE (x), x , off);
18246 else
18247 loc_list_plus_const (loc, off);
18249 add_AT_location_description (var_die, DW_AT_location, loc);
18250 remove_AT (var_die, DW_AT_declaration);
18253 return;
18256 if (common_block_die_table == NULL)
18257 common_block_die_table
18258 = htab_create_ggc (10, common_block_die_table_hash,
18259 common_block_die_table_eq, NULL);
18261 com_die_arg.decl_id = DECL_UID (com_decl);
18262 com_die_arg.die_parent = context_die;
18263 com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
18264 loc = loc_list_from_tree (com_decl, 2);
18265 if (com_die == NULL)
18267 const char *cnam
18268 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
18269 void **slot;
18271 com_die = new_die (DW_TAG_common_block, context_die, decl);
18272 add_name_and_src_coords_attributes (com_die, com_decl);
18273 if (loc)
18275 add_AT_location_description (com_die, DW_AT_location, loc);
18276 /* Avoid sharing the same loc descriptor between
18277 DW_TAG_common_block and DW_TAG_variable. */
18278 loc = loc_list_from_tree (com_decl, 2);
18280 else if (DECL_EXTERNAL (decl))
18281 add_AT_flag (com_die, DW_AT_declaration, 1);
18282 if (want_pubnames ())
18283 add_pubname_string (cnam, com_die); /* ??? needed? */
18284 com_die->decl_id = DECL_UID (com_decl);
18285 slot = htab_find_slot (common_block_die_table, com_die, INSERT);
18286 *slot = (void *) com_die;
18288 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
18290 add_AT_location_description (com_die, DW_AT_location, loc);
18291 loc = loc_list_from_tree (com_decl, 2);
18292 remove_AT (com_die, DW_AT_declaration);
18294 var_die = new_die (DW_TAG_variable, com_die, decl);
18295 add_name_and_src_coords_attributes (var_die, decl);
18296 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
18297 TREE_THIS_VOLATILE (decl), context_die);
18298 add_AT_flag (var_die, DW_AT_external, 1);
18299 if (loc)
18301 if (off)
18303 /* Optimize the common case. */
18304 if (single_element_loc_list_p (loc)
18305 && loc->expr->dw_loc_opc == DW_OP_addr
18306 && loc->expr->dw_loc_next == NULL
18307 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
18309 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18310 loc->expr->dw_loc_oprnd1.v.val_addr
18311 = plus_constant (GET_MODE (x), x, off);
18313 else
18314 loc_list_plus_const (loc, off);
18316 add_AT_location_description (var_die, DW_AT_location, loc);
18318 else if (DECL_EXTERNAL (decl))
18319 add_AT_flag (var_die, DW_AT_declaration, 1);
18320 equate_decl_number_to_die (decl, var_die);
18321 return;
18324 /* If the compiler emitted a definition for the DECL declaration
18325 and if we already emitted a DIE for it, don't emit a second
18326 DIE for it again. Allow re-declarations of DECLs that are
18327 inside functions, though. */
18328 if (old_die && declaration && !local_scope_p (context_die))
18329 return;
18331 /* For static data members, the declaration in the class is supposed
18332 to have DW_TAG_member tag; the specification should still be
18333 DW_TAG_variable referencing the DW_TAG_member DIE. */
18334 if (declaration && class_scope_p (context_die))
18335 var_die = new_die (DW_TAG_member, context_die, decl);
18336 else
18337 var_die = new_die (DW_TAG_variable, context_die, decl);
18339 origin_die = NULL;
18340 if (origin != NULL)
18341 origin_die = add_abstract_origin_attribute (var_die, origin);
18343 /* Loop unrolling can create multiple blocks that refer to the same
18344 static variable, so we must test for the DW_AT_declaration flag.
18346 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
18347 copy decls and set the DECL_ABSTRACT flag on them instead of
18348 sharing them.
18350 ??? Duplicated blocks have been rewritten to use .debug_ranges.
18352 ??? The declare_in_namespace support causes us to get two DIEs for one
18353 variable, both of which are declarations. We want to avoid considering
18354 one to be a specification, so we must test that this DIE is not a
18355 declaration. */
18356 else if (old_die && TREE_STATIC (decl) && ! declaration
18357 && get_AT_flag (old_die, DW_AT_declaration) == 1)
18359 /* This is a definition of a C++ class level static. */
18360 add_AT_specification (var_die, old_die);
18361 specialization_p = true;
18362 if (DECL_NAME (decl))
18364 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18365 struct dwarf_file_data * file_index = lookup_filename (s.file);
18367 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18368 add_AT_file (var_die, DW_AT_decl_file, file_index);
18370 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18371 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
18373 if (old_die->die_tag == DW_TAG_member)
18374 add_linkage_name (var_die, decl);
18377 else
18378 add_name_and_src_coords_attributes (var_die, decl);
18380 if ((origin == NULL && !specialization_p)
18381 || (origin != NULL
18382 && !DECL_ABSTRACT (decl_or_origin)
18383 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
18384 decl_function_context
18385 (decl_or_origin))))
18387 tree type = TREE_TYPE (decl_or_origin);
18389 if (decl_by_reference_p (decl_or_origin))
18390 add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
18391 else
18392 add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
18393 TREE_THIS_VOLATILE (decl_or_origin), context_die);
18396 if (origin == NULL && !specialization_p)
18398 if (TREE_PUBLIC (decl))
18399 add_AT_flag (var_die, DW_AT_external, 1);
18401 if (DECL_ARTIFICIAL (decl))
18402 add_AT_flag (var_die, DW_AT_artificial, 1);
18404 add_accessibility_attribute (var_die, decl);
18407 if (declaration)
18408 add_AT_flag (var_die, DW_AT_declaration, 1);
18410 if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
18411 equate_decl_number_to_die (decl, var_die);
18413 if (! declaration
18414 && (! DECL_ABSTRACT (decl_or_origin)
18415 /* Local static vars are shared between all clones/inlines,
18416 so emit DW_AT_location on the abstract DIE if DECL_RTL is
18417 already set. */
18418 || (TREE_CODE (decl_or_origin) == VAR_DECL
18419 && TREE_STATIC (decl_or_origin)
18420 && DECL_RTL_SET_P (decl_or_origin)))
18421 /* When abstract origin already has DW_AT_location attribute, no need
18422 to add it again. */
18423 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
18425 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
18426 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
18427 defer_location (decl_or_origin, var_die);
18428 else
18429 add_location_or_const_value_attribute (var_die, decl_or_origin,
18430 decl == NULL, DW_AT_location);
18431 add_pubname (decl_or_origin, var_die);
18433 else
18434 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
18437 /* Generate a DIE to represent a named constant. */
18439 static void
18440 gen_const_die (tree decl, dw_die_ref context_die)
18442 dw_die_ref const_die;
18443 tree type = TREE_TYPE (decl);
18445 const_die = new_die (DW_TAG_constant, context_die, decl);
18446 add_name_and_src_coords_attributes (const_die, decl);
18447 add_type_attribute (const_die, type, 1, 0, context_die);
18448 if (TREE_PUBLIC (decl))
18449 add_AT_flag (const_die, DW_AT_external, 1);
18450 if (DECL_ARTIFICIAL (decl))
18451 add_AT_flag (const_die, DW_AT_artificial, 1);
18452 tree_add_const_value_attribute_for_decl (const_die, decl);
18455 /* Generate a DIE to represent a label identifier. */
18457 static void
18458 gen_label_die (tree decl, dw_die_ref context_die)
18460 tree origin = decl_ultimate_origin (decl);
18461 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
18462 rtx insn;
18463 char label[MAX_ARTIFICIAL_LABEL_BYTES];
18465 if (origin != NULL)
18466 add_abstract_origin_attribute (lbl_die, origin);
18467 else
18468 add_name_and_src_coords_attributes (lbl_die, decl);
18470 if (DECL_ABSTRACT (decl))
18471 equate_decl_number_to_die (decl, lbl_die);
18472 else
18474 insn = DECL_RTL_IF_SET (decl);
18476 /* Deleted labels are programmer specified labels which have been
18477 eliminated because of various optimizations. We still emit them
18478 here so that it is possible to put breakpoints on them. */
18479 if (insn
18480 && (LABEL_P (insn)
18481 || ((NOTE_P (insn)
18482 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
18484 /* When optimization is enabled (via -O) some parts of the compiler
18485 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
18486 represent source-level labels which were explicitly declared by
18487 the user. This really shouldn't be happening though, so catch
18488 it if it ever does happen. */
18489 gcc_assert (!INSN_DELETED_P (insn));
18491 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
18492 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18494 else if (insn
18495 && NOTE_P (insn)
18496 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
18497 && CODE_LABEL_NUMBER (insn) != -1)
18499 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
18500 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18505 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
18506 attributes to the DIE for a block STMT, to describe where the inlined
18507 function was called from. This is similar to add_src_coords_attributes. */
18509 static inline void
18510 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
18512 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
18514 if (dwarf_version >= 3 || !dwarf_strict)
18516 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
18517 add_AT_unsigned (die, DW_AT_call_line, s.line);
18522 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
18523 Add low_pc and high_pc attributes to the DIE for a block STMT. */
18525 static inline void
18526 add_high_low_attributes (tree stmt, dw_die_ref die)
18528 char label[MAX_ARTIFICIAL_LABEL_BYTES];
18530 if (BLOCK_FRAGMENT_CHAIN (stmt)
18531 && (dwarf_version >= 3 || !dwarf_strict))
18533 tree chain, superblock = NULL_TREE;
18534 dw_die_ref pdie;
18535 dw_attr_ref attr = NULL;
18537 if (inlined_function_outer_scope_p (stmt))
18539 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18540 BLOCK_NUMBER (stmt));
18541 add_AT_lbl_id (die, DW_AT_entry_pc, label);
18544 /* Optimize duplicate .debug_ranges lists or even tails of
18545 lists. If this BLOCK has same ranges as its supercontext,
18546 lookup DW_AT_ranges attribute in the supercontext (and
18547 recursively so), verify that the ranges_table contains the
18548 right values and use it instead of adding a new .debug_range. */
18549 for (chain = stmt, pdie = die;
18550 BLOCK_SAME_RANGE (chain);
18551 chain = BLOCK_SUPERCONTEXT (chain))
18553 dw_attr_ref new_attr;
18555 pdie = pdie->die_parent;
18556 if (pdie == NULL)
18557 break;
18558 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
18559 break;
18560 new_attr = get_AT (pdie, DW_AT_ranges);
18561 if (new_attr == NULL
18562 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
18563 break;
18564 attr = new_attr;
18565 superblock = BLOCK_SUPERCONTEXT (chain);
18567 if (attr != NULL
18568 && (ranges_table[attr->dw_attr_val.v.val_offset
18569 / 2 / DWARF2_ADDR_SIZE].num
18570 == BLOCK_NUMBER (superblock))
18571 && BLOCK_FRAGMENT_CHAIN (superblock))
18573 unsigned long off = attr->dw_attr_val.v.val_offset
18574 / 2 / DWARF2_ADDR_SIZE;
18575 unsigned long supercnt = 0, thiscnt = 0;
18576 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
18577 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
18579 ++supercnt;
18580 gcc_checking_assert (ranges_table[off + supercnt].num
18581 == BLOCK_NUMBER (chain));
18583 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
18584 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
18585 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
18586 ++thiscnt;
18587 gcc_assert (supercnt >= thiscnt);
18588 add_AT_range_list (die, DW_AT_ranges,
18589 ((off + supercnt - thiscnt)
18590 * 2 * DWARF2_ADDR_SIZE),
18591 false);
18592 return;
18595 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
18597 chain = BLOCK_FRAGMENT_CHAIN (stmt);
18600 add_ranges (chain);
18601 chain = BLOCK_FRAGMENT_CHAIN (chain);
18603 while (chain);
18604 add_ranges (NULL);
18606 else
18608 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
18609 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18610 BLOCK_NUMBER (stmt));
18611 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
18612 BLOCK_NUMBER (stmt));
18613 add_AT_low_high_pc (die, label, label_high, false);
18617 /* Generate a DIE for a lexical block. */
18619 static void
18620 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
18622 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
18624 if (call_arg_locations)
18626 if (block_map.length () <= BLOCK_NUMBER (stmt))
18627 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
18628 block_map[BLOCK_NUMBER (stmt)] = stmt_die;
18631 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
18632 add_high_low_attributes (stmt, stmt_die);
18634 decls_for_scope (stmt, stmt_die, depth);
18637 /* Generate a DIE for an inlined subprogram. */
18639 static void
18640 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
18642 tree decl;
18644 /* The instance of function that is effectively being inlined shall not
18645 be abstract. */
18646 gcc_assert (! BLOCK_ABSTRACT (stmt));
18648 decl = block_ultimate_origin (stmt);
18650 /* Emit info for the abstract instance first, if we haven't yet. We
18651 must emit this even if the block is abstract, otherwise when we
18652 emit the block below (or elsewhere), we may end up trying to emit
18653 a die whose origin die hasn't been emitted, and crashing. */
18654 dwarf2out_abstract_function (decl);
18656 if (! BLOCK_ABSTRACT (stmt))
18658 dw_die_ref subr_die
18659 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
18661 if (call_arg_locations)
18663 if (block_map.length () <= BLOCK_NUMBER (stmt))
18664 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
18665 block_map[BLOCK_NUMBER (stmt)] = subr_die;
18667 add_abstract_origin_attribute (subr_die, decl);
18668 if (TREE_ASM_WRITTEN (stmt))
18669 add_high_low_attributes (stmt, subr_die);
18670 add_call_src_coords_attributes (stmt, subr_die);
18672 decls_for_scope (stmt, subr_die, depth);
18673 current_function_has_inlines = 1;
18677 /* Generate a DIE for a field in a record, or structure. */
18679 static void
18680 gen_field_die (tree decl, dw_die_ref context_die)
18682 dw_die_ref decl_die;
18684 if (TREE_TYPE (decl) == error_mark_node)
18685 return;
18687 decl_die = new_die (DW_TAG_member, context_die, decl);
18688 add_name_and_src_coords_attributes (decl_die, decl);
18689 add_type_attribute (decl_die, member_declared_type (decl),
18690 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
18691 context_die);
18693 if (DECL_BIT_FIELD_TYPE (decl))
18695 add_byte_size_attribute (decl_die, decl);
18696 add_bit_size_attribute (decl_die, decl);
18697 add_bit_offset_attribute (decl_die, decl);
18700 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
18701 add_data_member_location_attribute (decl_die, decl);
18703 if (DECL_ARTIFICIAL (decl))
18704 add_AT_flag (decl_die, DW_AT_artificial, 1);
18706 add_accessibility_attribute (decl_die, decl);
18708 /* Equate decl number to die, so that we can look up this decl later on. */
18709 equate_decl_number_to_die (decl, decl_die);
18712 #if 0
18713 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18714 Use modified_type_die instead.
18715 We keep this code here just in case these types of DIEs may be needed to
18716 represent certain things in other languages (e.g. Pascal) someday. */
18718 static void
18719 gen_pointer_type_die (tree type, dw_die_ref context_die)
18721 dw_die_ref ptr_die
18722 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
18724 equate_type_number_to_die (type, ptr_die);
18725 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18726 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18729 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18730 Use modified_type_die instead.
18731 We keep this code here just in case these types of DIEs may be needed to
18732 represent certain things in other languages (e.g. Pascal) someday. */
18734 static void
18735 gen_reference_type_die (tree type, dw_die_ref context_die)
18737 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
18739 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
18740 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
18741 else
18742 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
18744 equate_type_number_to_die (type, ref_die);
18745 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
18746 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18748 #endif
18750 /* Generate a DIE for a pointer to a member type. */
18752 static void
18753 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
18755 dw_die_ref ptr_die
18756 = new_die (DW_TAG_ptr_to_member_type,
18757 scope_die_for (type, context_die), type);
18759 equate_type_number_to_die (type, ptr_die);
18760 add_AT_die_ref (ptr_die, DW_AT_containing_type,
18761 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
18762 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18765 typedef const char *dchar_p; /* For DEF_VEC_P. */
18767 static char *producer_string;
18769 /* Return a heap allocated producer string including command line options
18770 if -grecord-gcc-switches. */
18772 static char *
18773 gen_producer_string (void)
18775 size_t j;
18776 vec<dchar_p> switches = vNULL;
18777 const char *language_string = lang_hooks.name;
18778 char *producer, *tail;
18779 const char *p;
18780 size_t len = dwarf_record_gcc_switches ? 0 : 3;
18781 size_t plen = strlen (language_string) + 1 + strlen (version_string);
18783 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
18784 switch (save_decoded_options[j].opt_index)
18786 case OPT_o:
18787 case OPT_d:
18788 case OPT_dumpbase:
18789 case OPT_dumpdir:
18790 case OPT_auxbase:
18791 case OPT_auxbase_strip:
18792 case OPT_quiet:
18793 case OPT_version:
18794 case OPT_v:
18795 case OPT_w:
18796 case OPT_L:
18797 case OPT_D:
18798 case OPT_I:
18799 case OPT_U:
18800 case OPT_SPECIAL_unknown:
18801 case OPT_SPECIAL_ignore:
18802 case OPT_SPECIAL_program_name:
18803 case OPT_SPECIAL_input_file:
18804 case OPT_grecord_gcc_switches:
18805 case OPT_gno_record_gcc_switches:
18806 case OPT__output_pch_:
18807 case OPT_fdiagnostics_show_location_:
18808 case OPT_fdiagnostics_show_option:
18809 case OPT_fdiagnostics_show_caret:
18810 case OPT_fverbose_asm:
18811 case OPT____:
18812 case OPT__sysroot_:
18813 case OPT_nostdinc:
18814 case OPT_nostdinc__:
18815 /* Ignore these. */
18816 continue;
18817 default:
18818 if (cl_options[save_decoded_options[j].opt_index].flags
18819 & CL_NO_DWARF_RECORD)
18820 continue;
18821 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
18822 == '-');
18823 switch (save_decoded_options[j].canonical_option[0][1])
18825 case 'M':
18826 case 'i':
18827 case 'W':
18828 continue;
18829 case 'f':
18830 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
18831 "dump", 4) == 0)
18832 continue;
18833 break;
18834 default:
18835 break;
18837 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
18838 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
18839 break;
18842 producer = XNEWVEC (char, plen + 1 + len + 1);
18843 tail = producer;
18844 sprintf (tail, "%s %s", language_string, version_string);
18845 tail += plen;
18847 FOR_EACH_VEC_ELT (switches, j, p)
18849 len = strlen (p);
18850 *tail = ' ';
18851 memcpy (tail + 1, p, len);
18852 tail += len + 1;
18855 *tail = '\0';
18856 switches.release ();
18857 return producer;
18860 /* Generate the DIE for the compilation unit. */
18862 static dw_die_ref
18863 gen_compile_unit_die (const char *filename)
18865 dw_die_ref die;
18866 const char *language_string = lang_hooks.name;
18867 int language;
18869 die = new_die (DW_TAG_compile_unit, NULL, NULL);
18871 if (filename)
18873 add_name_attribute (die, filename);
18874 /* Don't add cwd for <built-in>. */
18875 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
18876 add_comp_dir_attribute (die);
18879 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
18881 /* If our producer is LTO try to figure out a common language to use
18882 from the global list of translation units. */
18883 if (strcmp (language_string, "GNU GIMPLE") == 0)
18885 unsigned i;
18886 tree t;
18887 const char *common_lang = NULL;
18889 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
18891 if (!TRANSLATION_UNIT_LANGUAGE (t))
18892 continue;
18893 if (!common_lang)
18894 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
18895 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
18897 else if (strncmp (common_lang, "GNU C", 5) == 0
18898 && strncmp(TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
18899 /* Mixing C and C++ is ok, use C++ in that case. */
18900 common_lang = "GNU C++";
18901 else
18903 /* Fall back to C. */
18904 common_lang = NULL;
18905 break;
18909 if (common_lang)
18910 language_string = common_lang;
18913 language = DW_LANG_C89;
18914 if (strcmp (language_string, "GNU C++") == 0)
18915 language = DW_LANG_C_plus_plus;
18916 else if (strcmp (language_string, "GNU F77") == 0)
18917 language = DW_LANG_Fortran77;
18918 else if (strcmp (language_string, "GNU Pascal") == 0)
18919 language = DW_LANG_Pascal83;
18920 else if (dwarf_version >= 3 || !dwarf_strict)
18922 if (strcmp (language_string, "GNU Ada") == 0)
18923 language = DW_LANG_Ada95;
18924 else if (strcmp (language_string, "GNU Fortran") == 0)
18925 language = DW_LANG_Fortran95;
18926 else if (strcmp (language_string, "GNU Java") == 0)
18927 language = DW_LANG_Java;
18928 else if (strcmp (language_string, "GNU Objective-C") == 0)
18929 language = DW_LANG_ObjC;
18930 else if (strcmp (language_string, "GNU Objective-C++") == 0)
18931 language = DW_LANG_ObjC_plus_plus;
18932 else if (dwarf_version >= 5 || !dwarf_strict)
18934 if (strcmp (language_string, "GNU Go") == 0)
18935 language = DW_LANG_Go;
18938 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
18939 else if (strcmp (language_string, "GNU Fortran") == 0)
18940 language = DW_LANG_Fortran90;
18942 add_AT_unsigned (die, DW_AT_language, language);
18944 switch (language)
18946 case DW_LANG_Fortran77:
18947 case DW_LANG_Fortran90:
18948 case DW_LANG_Fortran95:
18949 /* Fortran has case insensitive identifiers and the front-end
18950 lowercases everything. */
18951 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
18952 break;
18953 default:
18954 /* The default DW_ID_case_sensitive doesn't need to be specified. */
18955 break;
18957 return die;
18960 /* Generate the DIE for a base class. */
18962 static void
18963 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
18965 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
18967 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
18968 add_data_member_location_attribute (die, binfo);
18970 if (BINFO_VIRTUAL_P (binfo))
18971 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
18973 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
18974 children, otherwise the default is DW_ACCESS_public. In DWARF2
18975 the default has always been DW_ACCESS_private. */
18976 if (access == access_public_node)
18978 if (dwarf_version == 2
18979 || context_die->die_tag == DW_TAG_class_type)
18980 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
18982 else if (access == access_protected_node)
18983 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
18984 else if (dwarf_version > 2
18985 && context_die->die_tag != DW_TAG_class_type)
18986 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
18989 /* Generate a DIE for a class member. */
18991 static void
18992 gen_member_die (tree type, dw_die_ref context_die)
18994 tree member;
18995 tree binfo = TYPE_BINFO (type);
18996 dw_die_ref child;
18998 /* If this is not an incomplete type, output descriptions of each of its
18999 members. Note that as we output the DIEs necessary to represent the
19000 members of this record or union type, we will also be trying to output
19001 DIEs to represent the *types* of those members. However the `type'
19002 function (above) will specifically avoid generating type DIEs for member
19003 types *within* the list of member DIEs for this (containing) type except
19004 for those types (of members) which are explicitly marked as also being
19005 members of this (containing) type themselves. The g++ front- end can
19006 force any given type to be treated as a member of some other (containing)
19007 type by setting the TYPE_CONTEXT of the given (member) type to point to
19008 the TREE node representing the appropriate (containing) type. */
19010 /* First output info about the base classes. */
19011 if (binfo)
19013 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
19014 int i;
19015 tree base;
19017 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19018 gen_inheritance_die (base,
19019 (accesses ? (*accesses)[i] : access_public_node),
19020 context_die);
19023 /* Now output info about the data members and type members. */
19024 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19026 /* If we thought we were generating minimal debug info for TYPE
19027 and then changed our minds, some of the member declarations
19028 may have already been defined. Don't define them again, but
19029 do put them in the right order. */
19031 child = lookup_decl_die (member);
19032 if (child)
19033 splice_child_die (context_die, child);
19034 else
19035 gen_decl_die (member, NULL, context_die);
19038 /* Now output info about the function members (if any). */
19039 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19041 /* Don't include clones in the member list. */
19042 if (DECL_ABSTRACT_ORIGIN (member))
19043 continue;
19045 child = lookup_decl_die (member);
19046 if (child)
19047 splice_child_die (context_die, child);
19048 else
19049 gen_decl_die (member, NULL, context_die);
19053 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
19054 is set, we pretend that the type was never defined, so we only get the
19055 member DIEs needed by later specification DIEs. */
19057 static void
19058 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19059 enum debug_info_usage usage)
19061 dw_die_ref type_die = lookup_type_die (type);
19062 dw_die_ref scope_die = 0;
19063 int nested = 0;
19064 int complete = (TYPE_SIZE (type)
19065 && (! TYPE_STUB_DECL (type)
19066 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19067 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19068 complete = complete && should_emit_struct_debug (type, usage);
19070 if (type_die && ! complete)
19071 return;
19073 if (TYPE_CONTEXT (type) != NULL_TREE
19074 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19075 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19076 nested = 1;
19078 scope_die = scope_die_for (type, context_die);
19080 /* Generate child dies for template paramaters. */
19081 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
19082 schedule_generic_params_dies_gen (type);
19084 if (! type_die || (nested && is_cu_die (scope_die)))
19085 /* First occurrence of type or toplevel definition of nested class. */
19087 dw_die_ref old_die = type_die;
19089 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
19090 ? record_type_tag (type) : DW_TAG_union_type,
19091 scope_die, type);
19092 equate_type_number_to_die (type, type_die);
19093 if (old_die)
19094 add_AT_specification (type_die, old_die);
19095 else
19096 add_name_attribute (type_die, type_tag (type));
19098 else
19099 remove_AT (type_die, DW_AT_declaration);
19101 /* If this type has been completed, then give it a byte_size attribute and
19102 then give a list of members. */
19103 if (complete && !ns_decl)
19105 /* Prevent infinite recursion in cases where the type of some member of
19106 this type is expressed in terms of this type itself. */
19107 TREE_ASM_WRITTEN (type) = 1;
19108 add_byte_size_attribute (type_die, type);
19109 if (TYPE_STUB_DECL (type) != NULL_TREE)
19111 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19112 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19115 /* If the first reference to this type was as the return type of an
19116 inline function, then it may not have a parent. Fix this now. */
19117 if (type_die->die_parent == NULL)
19118 add_child_die (scope_die, type_die);
19120 push_decl_scope (type);
19121 gen_member_die (type, type_die);
19122 pop_decl_scope ();
19124 add_gnat_descriptive_type_attribute (type_die, type, context_die);
19125 if (TYPE_ARTIFICIAL (type))
19126 add_AT_flag (type_die, DW_AT_artificial, 1);
19128 /* GNU extension: Record what type our vtable lives in. */
19129 if (TYPE_VFIELD (type))
19131 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
19133 gen_type_die (vtype, context_die);
19134 add_AT_die_ref (type_die, DW_AT_containing_type,
19135 lookup_type_die (vtype));
19138 else
19140 add_AT_flag (type_die, DW_AT_declaration, 1);
19142 /* We don't need to do this for function-local types. */
19143 if (TYPE_STUB_DECL (type)
19144 && ! decl_function_context (TYPE_STUB_DECL (type)))
19145 vec_safe_push (incomplete_types, type);
19148 if (get_AT (type_die, DW_AT_name))
19149 add_pubtype (type, type_die);
19152 /* Generate a DIE for a subroutine _type_. */
19154 static void
19155 gen_subroutine_type_die (tree type, dw_die_ref context_die)
19157 tree return_type = TREE_TYPE (type);
19158 dw_die_ref subr_die
19159 = new_die (DW_TAG_subroutine_type,
19160 scope_die_for (type, context_die), type);
19162 equate_type_number_to_die (type, subr_die);
19163 add_prototyped_attribute (subr_die, type);
19164 add_type_attribute (subr_die, return_type, 0, 0, context_die);
19165 gen_formal_types_die (type, subr_die);
19167 if (get_AT (subr_die, DW_AT_name))
19168 add_pubtype (type, subr_die);
19171 /* Generate a DIE for a type definition. */
19173 static void
19174 gen_typedef_die (tree decl, dw_die_ref context_die)
19176 dw_die_ref type_die;
19177 tree origin;
19179 if (TREE_ASM_WRITTEN (decl))
19180 return;
19182 TREE_ASM_WRITTEN (decl) = 1;
19183 type_die = new_die (DW_TAG_typedef, context_die, decl);
19184 origin = decl_ultimate_origin (decl);
19185 if (origin != NULL)
19186 add_abstract_origin_attribute (type_die, origin);
19187 else
19189 tree type;
19191 add_name_and_src_coords_attributes (type_die, decl);
19192 if (DECL_ORIGINAL_TYPE (decl))
19194 type = DECL_ORIGINAL_TYPE (decl);
19196 gcc_assert (type != TREE_TYPE (decl));
19197 equate_type_number_to_die (TREE_TYPE (decl), type_die);
19199 else
19201 type = TREE_TYPE (decl);
19203 if (is_naming_typedef_decl (TYPE_NAME (type)))
19205 /* Here, we are in the case of decl being a typedef naming
19206 an anonymous type, e.g:
19207 typedef struct {...} foo;
19208 In that case TREE_TYPE (decl) is not a typedef variant
19209 type and TYPE_NAME of the anonymous type is set to the
19210 TYPE_DECL of the typedef. This construct is emitted by
19211 the C++ FE.
19213 TYPE is the anonymous struct named by the typedef
19214 DECL. As we need the DW_AT_type attribute of the
19215 DW_TAG_typedef to point to the DIE of TYPE, let's
19216 generate that DIE right away. add_type_attribute
19217 called below will then pick (via lookup_type_die) that
19218 anonymous struct DIE. */
19219 if (!TREE_ASM_WRITTEN (type))
19220 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
19222 /* This is a GNU Extension. We are adding a
19223 DW_AT_linkage_name attribute to the DIE of the
19224 anonymous struct TYPE. The value of that attribute
19225 is the name of the typedef decl naming the anonymous
19226 struct. This greatly eases the work of consumers of
19227 this debug info. */
19228 add_linkage_attr (lookup_type_die (type), decl);
19232 add_type_attribute (type_die, type, TREE_READONLY (decl),
19233 TREE_THIS_VOLATILE (decl), context_die);
19235 if (is_naming_typedef_decl (decl))
19236 /* We want that all subsequent calls to lookup_type_die with
19237 TYPE in argument yield the DW_TAG_typedef we have just
19238 created. */
19239 equate_type_number_to_die (type, type_die);
19241 add_accessibility_attribute (type_die, decl);
19244 if (DECL_ABSTRACT (decl))
19245 equate_decl_number_to_die (decl, type_die);
19247 if (get_AT (type_die, DW_AT_name))
19248 add_pubtype (decl, type_die);
19251 /* Generate a DIE for a struct, class, enum or union type. */
19253 static void
19254 gen_tagged_type_die (tree type,
19255 dw_die_ref context_die,
19256 enum debug_info_usage usage)
19258 int need_pop;
19260 if (type == NULL_TREE
19261 || !is_tagged_type (type))
19262 return;
19264 /* If this is a nested type whose containing class hasn't been written
19265 out yet, writing it out will cover this one, too. This does not apply
19266 to instantiations of member class templates; they need to be added to
19267 the containing class as they are generated. FIXME: This hurts the
19268 idea of combining type decls from multiple TUs, since we can't predict
19269 what set of template instantiations we'll get. */
19270 if (TYPE_CONTEXT (type)
19271 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19272 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
19274 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
19276 if (TREE_ASM_WRITTEN (type))
19277 return;
19279 /* If that failed, attach ourselves to the stub. */
19280 push_decl_scope (TYPE_CONTEXT (type));
19281 context_die = lookup_type_die (TYPE_CONTEXT (type));
19282 need_pop = 1;
19284 else if (TYPE_CONTEXT (type) != NULL_TREE
19285 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
19287 /* If this type is local to a function that hasn't been written
19288 out yet, use a NULL context for now; it will be fixed up in
19289 decls_for_scope. */
19290 context_die = lookup_decl_die (TYPE_CONTEXT (type));
19291 /* A declaration DIE doesn't count; nested types need to go in the
19292 specification. */
19293 if (context_die && is_declaration_die (context_die))
19294 context_die = NULL;
19295 need_pop = 0;
19297 else
19299 context_die = declare_in_namespace (type, context_die);
19300 need_pop = 0;
19303 if (TREE_CODE (type) == ENUMERAL_TYPE)
19305 /* This might have been written out by the call to
19306 declare_in_namespace. */
19307 if (!TREE_ASM_WRITTEN (type))
19308 gen_enumeration_type_die (type, context_die);
19310 else
19311 gen_struct_or_union_type_die (type, context_die, usage);
19313 if (need_pop)
19314 pop_decl_scope ();
19316 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
19317 it up if it is ever completed. gen_*_type_die will set it for us
19318 when appropriate. */
19321 /* Generate a type description DIE. */
19323 static void
19324 gen_type_die_with_usage (tree type, dw_die_ref context_die,
19325 enum debug_info_usage usage)
19327 struct array_descr_info info;
19329 if (type == NULL_TREE || type == error_mark_node)
19330 return;
19332 if (TYPE_NAME (type) != NULL_TREE
19333 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
19334 && is_redundant_typedef (TYPE_NAME (type))
19335 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
19336 /* The DECL of this type is a typedef we don't want to emit debug
19337 info for but we want debug info for its underlying typedef.
19338 This can happen for e.g, the injected-class-name of a C++
19339 type. */
19340 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
19342 /* If TYPE is a typedef type variant, let's generate debug info
19343 for the parent typedef which TYPE is a type of. */
19344 if (typedef_variant_p (type))
19346 if (TREE_ASM_WRITTEN (type))
19347 return;
19349 /* Prevent broken recursion; we can't hand off to the same type. */
19350 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
19352 /* Give typedefs the right scope. */
19353 context_die = scope_die_for (type, context_die);
19355 TREE_ASM_WRITTEN (type) = 1;
19357 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19358 return;
19361 /* If type is an anonymous tagged type named by a typedef, let's
19362 generate debug info for the typedef. */
19363 if (is_naming_typedef_decl (TYPE_NAME (type)))
19365 /* Use the DIE of the containing namespace as the parent DIE of
19366 the type description DIE we want to generate. */
19367 if (DECL_CONTEXT (TYPE_NAME (type))
19368 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
19369 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
19371 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19372 return;
19375 /* If this is an array type with hidden descriptor, handle it first. */
19376 if (!TREE_ASM_WRITTEN (type)
19377 && lang_hooks.types.get_array_descr_info
19378 && lang_hooks.types.get_array_descr_info (type, &info)
19379 && (dwarf_version >= 3 || !dwarf_strict))
19381 gen_descr_array_type_die (type, &info, context_die);
19382 TREE_ASM_WRITTEN (type) = 1;
19383 return;
19386 /* We are going to output a DIE to represent the unqualified version
19387 of this type (i.e. without any const or volatile qualifiers) so
19388 get the main variant (i.e. the unqualified version) of this type
19389 now. (Vectors are special because the debugging info is in the
19390 cloned type itself). */
19391 if (TREE_CODE (type) != VECTOR_TYPE)
19392 type = type_main_variant (type);
19394 if (TREE_ASM_WRITTEN (type))
19395 return;
19397 switch (TREE_CODE (type))
19399 case ERROR_MARK:
19400 break;
19402 case POINTER_TYPE:
19403 case REFERENCE_TYPE:
19404 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
19405 ensures that the gen_type_die recursion will terminate even if the
19406 type is recursive. Recursive types are possible in Ada. */
19407 /* ??? We could perhaps do this for all types before the switch
19408 statement. */
19409 TREE_ASM_WRITTEN (type) = 1;
19411 /* For these types, all that is required is that we output a DIE (or a
19412 set of DIEs) to represent the "basis" type. */
19413 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19414 DINFO_USAGE_IND_USE);
19415 break;
19417 case OFFSET_TYPE:
19418 /* This code is used for C++ pointer-to-data-member types.
19419 Output a description of the relevant class type. */
19420 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
19421 DINFO_USAGE_IND_USE);
19423 /* Output a description of the type of the object pointed to. */
19424 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19425 DINFO_USAGE_IND_USE);
19427 /* Now output a DIE to represent this pointer-to-data-member type
19428 itself. */
19429 gen_ptr_to_mbr_type_die (type, context_die);
19430 break;
19432 case FUNCTION_TYPE:
19433 /* Force out return type (in case it wasn't forced out already). */
19434 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19435 DINFO_USAGE_DIR_USE);
19436 gen_subroutine_type_die (type, context_die);
19437 break;
19439 case METHOD_TYPE:
19440 /* Force out return type (in case it wasn't forced out already). */
19441 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19442 DINFO_USAGE_DIR_USE);
19443 gen_subroutine_type_die (type, context_die);
19444 break;
19446 case ARRAY_TYPE:
19447 gen_array_type_die (type, context_die);
19448 break;
19450 case VECTOR_TYPE:
19451 gen_array_type_die (type, context_die);
19452 break;
19454 case ENUMERAL_TYPE:
19455 case RECORD_TYPE:
19456 case UNION_TYPE:
19457 case QUAL_UNION_TYPE:
19458 gen_tagged_type_die (type, context_die, usage);
19459 return;
19461 case VOID_TYPE:
19462 case INTEGER_TYPE:
19463 case REAL_TYPE:
19464 case FIXED_POINT_TYPE:
19465 case COMPLEX_TYPE:
19466 case BOOLEAN_TYPE:
19467 /* No DIEs needed for fundamental types. */
19468 break;
19470 case NULLPTR_TYPE:
19471 case LANG_TYPE:
19472 /* Just use DW_TAG_unspecified_type. */
19474 dw_die_ref type_die = lookup_type_die (type);
19475 if (type_die == NULL)
19477 tree name = TYPE_NAME (type);
19478 if (TREE_CODE (name) == TYPE_DECL)
19479 name = DECL_NAME (name);
19480 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
19481 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
19482 equate_type_number_to_die (type, type_die);
19485 break;
19487 default:
19488 gcc_unreachable ();
19491 TREE_ASM_WRITTEN (type) = 1;
19494 static void
19495 gen_type_die (tree type, dw_die_ref context_die)
19497 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
19500 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
19501 things which are local to the given block. */
19503 static void
19504 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
19506 int must_output_die = 0;
19507 bool inlined_func;
19509 /* Ignore blocks that are NULL. */
19510 if (stmt == NULL_TREE)
19511 return;
19513 inlined_func = inlined_function_outer_scope_p (stmt);
19515 /* If the block is one fragment of a non-contiguous block, do not
19516 process the variables, since they will have been done by the
19517 origin block. Do process subblocks. */
19518 if (BLOCK_FRAGMENT_ORIGIN (stmt))
19520 tree sub;
19522 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
19523 gen_block_die (sub, context_die, depth + 1);
19525 return;
19528 /* Determine if we need to output any Dwarf DIEs at all to represent this
19529 block. */
19530 if (inlined_func)
19531 /* The outer scopes for inlinings *must* always be represented. We
19532 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
19533 must_output_die = 1;
19534 else
19536 /* Determine if this block directly contains any "significant"
19537 local declarations which we will need to output DIEs for. */
19538 if (debug_info_level > DINFO_LEVEL_TERSE)
19539 /* We are not in terse mode so *any* local declaration counts
19540 as being a "significant" one. */
19541 must_output_die = ((BLOCK_VARS (stmt) != NULL
19542 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
19543 && (TREE_USED (stmt)
19544 || TREE_ASM_WRITTEN (stmt)
19545 || BLOCK_ABSTRACT (stmt)));
19546 else if ((TREE_USED (stmt)
19547 || TREE_ASM_WRITTEN (stmt)
19548 || BLOCK_ABSTRACT (stmt))
19549 && !dwarf2out_ignore_block (stmt))
19550 must_output_die = 1;
19553 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
19554 DIE for any block which contains no significant local declarations at
19555 all. Rather, in such cases we just call `decls_for_scope' so that any
19556 needed Dwarf info for any sub-blocks will get properly generated. Note
19557 that in terse mode, our definition of what constitutes a "significant"
19558 local declaration gets restricted to include only inlined function
19559 instances and local (nested) function definitions. */
19560 if (must_output_die)
19562 if (inlined_func)
19564 /* If STMT block is abstract, that means we have been called
19565 indirectly from dwarf2out_abstract_function.
19566 That function rightfully marks the descendent blocks (of
19567 the abstract function it is dealing with) as being abstract,
19568 precisely to prevent us from emitting any
19569 DW_TAG_inlined_subroutine DIE as a descendent
19570 of an abstract function instance. So in that case, we should
19571 not call gen_inlined_subroutine_die.
19573 Later though, when cgraph asks dwarf2out to emit info
19574 for the concrete instance of the function decl into which
19575 the concrete instance of STMT got inlined, the later will lead
19576 to the generation of a DW_TAG_inlined_subroutine DIE. */
19577 if (! BLOCK_ABSTRACT (stmt))
19578 gen_inlined_subroutine_die (stmt, context_die, depth);
19580 else
19581 gen_lexical_block_die (stmt, context_die, depth);
19583 else
19584 decls_for_scope (stmt, context_die, depth);
19587 /* Process variable DECL (or variable with origin ORIGIN) within
19588 block STMT and add it to CONTEXT_DIE. */
19589 static void
19590 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
19592 dw_die_ref die;
19593 tree decl_or_origin = decl ? decl : origin;
19595 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
19596 die = lookup_decl_die (decl_or_origin);
19597 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
19598 && TYPE_DECL_IS_STUB (decl_or_origin))
19599 die = lookup_type_die (TREE_TYPE (decl_or_origin));
19600 else
19601 die = NULL;
19603 if (die != NULL && die->die_parent == NULL)
19604 add_child_die (context_die, die);
19605 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
19606 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
19607 stmt, context_die);
19608 else
19609 gen_decl_die (decl, origin, context_die);
19612 /* Generate all of the decls declared within a given scope and (recursively)
19613 all of its sub-blocks. */
19615 static void
19616 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
19618 tree decl;
19619 unsigned int i;
19620 tree subblocks;
19622 /* Ignore NULL blocks. */
19623 if (stmt == NULL_TREE)
19624 return;
19626 /* Output the DIEs to represent all of the data objects and typedefs
19627 declared directly within this block but not within any nested
19628 sub-blocks. Also, nested function and tag DIEs have been
19629 generated with a parent of NULL; fix that up now. */
19630 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
19631 process_scope_var (stmt, decl, NULL_TREE, context_die);
19632 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
19633 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
19634 context_die);
19636 /* If we're at -g1, we're not interested in subblocks. */
19637 if (debug_info_level <= DINFO_LEVEL_TERSE)
19638 return;
19640 /* Output the DIEs to represent all sub-blocks (and the items declared
19641 therein) of this block. */
19642 for (subblocks = BLOCK_SUBBLOCKS (stmt);
19643 subblocks != NULL;
19644 subblocks = BLOCK_CHAIN (subblocks))
19645 gen_block_die (subblocks, context_die, depth + 1);
19648 /* Is this a typedef we can avoid emitting? */
19650 static inline int
19651 is_redundant_typedef (const_tree decl)
19653 if (TYPE_DECL_IS_STUB (decl))
19654 return 1;
19656 if (DECL_ARTIFICIAL (decl)
19657 && DECL_CONTEXT (decl)
19658 && is_tagged_type (DECL_CONTEXT (decl))
19659 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
19660 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
19661 /* Also ignore the artificial member typedef for the class name. */
19662 return 1;
19664 return 0;
19667 /* Return TRUE if TYPE is a typedef that names a type for linkage
19668 purposes. This kind of typedefs is produced by the C++ FE for
19669 constructs like:
19671 typedef struct {...} foo;
19673 In that case, there is no typedef variant type produced for foo.
19674 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
19675 struct type. */
19677 static bool
19678 is_naming_typedef_decl (const_tree decl)
19680 if (decl == NULL_TREE
19681 || TREE_CODE (decl) != TYPE_DECL
19682 || !is_tagged_type (TREE_TYPE (decl))
19683 || DECL_IS_BUILTIN (decl)
19684 || is_redundant_typedef (decl)
19685 /* It looks like Ada produces TYPE_DECLs that are very similar
19686 to C++ naming typedefs but that have different
19687 semantics. Let's be specific to c++ for now. */
19688 || !is_cxx ())
19689 return FALSE;
19691 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
19692 && TYPE_NAME (TREE_TYPE (decl)) == decl
19693 && (TYPE_STUB_DECL (TREE_TYPE (decl))
19694 != TYPE_NAME (TREE_TYPE (decl))));
19697 /* Returns the DIE for a context. */
19699 static inline dw_die_ref
19700 get_context_die (tree context)
19702 if (context)
19704 /* Find die that represents this context. */
19705 if (TYPE_P (context))
19707 context = TYPE_MAIN_VARIANT (context);
19708 return strip_naming_typedef (context, force_type_die (context));
19710 else
19711 return force_decl_die (context);
19713 return comp_unit_die ();
19716 /* Returns the DIE for decl. A DIE will always be returned. */
19718 static dw_die_ref
19719 force_decl_die (tree decl)
19721 dw_die_ref decl_die;
19722 unsigned saved_external_flag;
19723 tree save_fn = NULL_TREE;
19724 decl_die = lookup_decl_die (decl);
19725 if (!decl_die)
19727 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
19729 decl_die = lookup_decl_die (decl);
19730 if (decl_die)
19731 return decl_die;
19733 switch (TREE_CODE (decl))
19735 case FUNCTION_DECL:
19736 /* Clear current_function_decl, so that gen_subprogram_die thinks
19737 that this is a declaration. At this point, we just want to force
19738 declaration die. */
19739 save_fn = current_function_decl;
19740 current_function_decl = NULL_TREE;
19741 gen_subprogram_die (decl, context_die);
19742 current_function_decl = save_fn;
19743 break;
19745 case VAR_DECL:
19746 /* Set external flag to force declaration die. Restore it after
19747 gen_decl_die() call. */
19748 saved_external_flag = DECL_EXTERNAL (decl);
19749 DECL_EXTERNAL (decl) = 1;
19750 gen_decl_die (decl, NULL, context_die);
19751 DECL_EXTERNAL (decl) = saved_external_flag;
19752 break;
19754 case NAMESPACE_DECL:
19755 if (dwarf_version >= 3 || !dwarf_strict)
19756 dwarf2out_decl (decl);
19757 else
19758 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
19759 decl_die = comp_unit_die ();
19760 break;
19762 case TRANSLATION_UNIT_DECL:
19763 decl_die = comp_unit_die ();
19764 break;
19766 default:
19767 gcc_unreachable ();
19770 /* We should be able to find the DIE now. */
19771 if (!decl_die)
19772 decl_die = lookup_decl_die (decl);
19773 gcc_assert (decl_die);
19776 return decl_die;
19779 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
19780 always returned. */
19782 static dw_die_ref
19783 force_type_die (tree type)
19785 dw_die_ref type_die;
19787 type_die = lookup_type_die (type);
19788 if (!type_die)
19790 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
19792 type_die = modified_type_die (type, TYPE_READONLY (type),
19793 TYPE_VOLATILE (type), context_die);
19794 gcc_assert (type_die);
19796 return type_die;
19799 /* Force out any required namespaces to be able to output DECL,
19800 and return the new context_die for it, if it's changed. */
19802 static dw_die_ref
19803 setup_namespace_context (tree thing, dw_die_ref context_die)
19805 tree context = (DECL_P (thing)
19806 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
19807 if (context && TREE_CODE (context) == NAMESPACE_DECL)
19808 /* Force out the namespace. */
19809 context_die = force_decl_die (context);
19811 return context_die;
19814 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
19815 type) within its namespace, if appropriate.
19817 For compatibility with older debuggers, namespace DIEs only contain
19818 declarations; all definitions are emitted at CU scope. */
19820 static dw_die_ref
19821 declare_in_namespace (tree thing, dw_die_ref context_die)
19823 dw_die_ref ns_context;
19825 if (debug_info_level <= DINFO_LEVEL_TERSE)
19826 return context_die;
19828 /* If this decl is from an inlined function, then don't try to emit it in its
19829 namespace, as we will get confused. It would have already been emitted
19830 when the abstract instance of the inline function was emitted anyways. */
19831 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
19832 return context_die;
19834 ns_context = setup_namespace_context (thing, context_die);
19836 if (ns_context != context_die)
19838 if (is_fortran ())
19839 return ns_context;
19840 if (DECL_P (thing))
19841 gen_decl_die (thing, NULL, ns_context);
19842 else
19843 gen_type_die (thing, ns_context);
19845 return context_die;
19848 /* Generate a DIE for a namespace or namespace alias. */
19850 static void
19851 gen_namespace_die (tree decl, dw_die_ref context_die)
19853 dw_die_ref namespace_die;
19855 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
19856 they are an alias of. */
19857 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
19859 /* Output a real namespace or module. */
19860 context_die = setup_namespace_context (decl, comp_unit_die ());
19861 namespace_die = new_die (is_fortran ()
19862 ? DW_TAG_module : DW_TAG_namespace,
19863 context_die, decl);
19864 /* For Fortran modules defined in different CU don't add src coords. */
19865 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
19867 const char *name = dwarf2_name (decl, 0);
19868 if (name)
19869 add_name_attribute (namespace_die, name);
19871 else
19872 add_name_and_src_coords_attributes (namespace_die, decl);
19873 if (DECL_EXTERNAL (decl))
19874 add_AT_flag (namespace_die, DW_AT_declaration, 1);
19875 equate_decl_number_to_die (decl, namespace_die);
19877 else
19879 /* Output a namespace alias. */
19881 /* Force out the namespace we are an alias of, if necessary. */
19882 dw_die_ref origin_die
19883 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
19885 if (DECL_FILE_SCOPE_P (decl)
19886 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
19887 context_die = setup_namespace_context (decl, comp_unit_die ());
19888 /* Now create the namespace alias DIE. */
19889 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
19890 add_name_and_src_coords_attributes (namespace_die, decl);
19891 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
19892 equate_decl_number_to_die (decl, namespace_die);
19894 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
19895 if (want_pubnames ())
19896 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
19899 /* Generate Dwarf debug information for a decl described by DECL.
19900 The return value is currently only meaningful for PARM_DECLs,
19901 for all other decls it returns NULL. */
19903 static dw_die_ref
19904 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
19906 tree decl_or_origin = decl ? decl : origin;
19907 tree class_origin = NULL, ultimate_origin;
19909 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
19910 return NULL;
19912 switch (TREE_CODE (decl_or_origin))
19914 case ERROR_MARK:
19915 break;
19917 case CONST_DECL:
19918 if (!is_fortran () && !is_ada ())
19920 /* The individual enumerators of an enum type get output when we output
19921 the Dwarf representation of the relevant enum type itself. */
19922 break;
19925 /* Emit its type. */
19926 gen_type_die (TREE_TYPE (decl), context_die);
19928 /* And its containing namespace. */
19929 context_die = declare_in_namespace (decl, context_die);
19931 gen_const_die (decl, context_die);
19932 break;
19934 case FUNCTION_DECL:
19935 /* Don't output any DIEs to represent mere function declarations,
19936 unless they are class members or explicit block externs. */
19937 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
19938 && DECL_FILE_SCOPE_P (decl_or_origin)
19939 && (current_function_decl == NULL_TREE
19940 || DECL_ARTIFICIAL (decl_or_origin)))
19941 break;
19943 #if 0
19944 /* FIXME */
19945 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
19946 on local redeclarations of global functions. That seems broken. */
19947 if (current_function_decl != decl)
19948 /* This is only a declaration. */;
19949 #endif
19951 /* If we're emitting a clone, emit info for the abstract instance. */
19952 if (origin || DECL_ORIGIN (decl) != decl)
19953 dwarf2out_abstract_function (origin
19954 ? DECL_ORIGIN (origin)
19955 : DECL_ABSTRACT_ORIGIN (decl));
19957 /* If we're emitting an out-of-line copy of an inline function,
19958 emit info for the abstract instance and set up to refer to it. */
19959 else if (cgraph_function_possibly_inlined_p (decl)
19960 && ! DECL_ABSTRACT (decl)
19961 && ! class_or_namespace_scope_p (context_die)
19962 /* dwarf2out_abstract_function won't emit a die if this is just
19963 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
19964 that case, because that works only if we have a die. */
19965 && DECL_INITIAL (decl) != NULL_TREE)
19967 dwarf2out_abstract_function (decl);
19968 set_decl_origin_self (decl);
19971 /* Otherwise we're emitting the primary DIE for this decl. */
19972 else if (debug_info_level > DINFO_LEVEL_TERSE)
19974 /* Before we describe the FUNCTION_DECL itself, make sure that we
19975 have its containing type. */
19976 if (!origin)
19977 origin = decl_class_context (decl);
19978 if (origin != NULL_TREE)
19979 gen_type_die (origin, context_die);
19981 /* And its return type. */
19982 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
19984 /* And its virtual context. */
19985 if (DECL_VINDEX (decl) != NULL_TREE)
19986 gen_type_die (DECL_CONTEXT (decl), context_die);
19988 /* Make sure we have a member DIE for decl. */
19989 if (origin != NULL_TREE)
19990 gen_type_die_for_member (origin, decl, context_die);
19992 /* And its containing namespace. */
19993 context_die = declare_in_namespace (decl, context_die);
19996 /* Now output a DIE to represent the function itself. */
19997 if (decl)
19998 gen_subprogram_die (decl, context_die);
19999 break;
20001 case TYPE_DECL:
20002 /* If we are in terse mode, don't generate any DIEs to represent any
20003 actual typedefs. */
20004 if (debug_info_level <= DINFO_LEVEL_TERSE)
20005 break;
20007 /* In the special case of a TYPE_DECL node representing the declaration
20008 of some type tag, if the given TYPE_DECL is marked as having been
20009 instantiated from some other (original) TYPE_DECL node (e.g. one which
20010 was generated within the original definition of an inline function) we
20011 used to generate a special (abbreviated) DW_TAG_structure_type,
20012 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
20013 should be actually referencing those DIEs, as variable DIEs with that
20014 type would be emitted already in the abstract origin, so it was always
20015 removed during unused type prunning. Don't add anything in this
20016 case. */
20017 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20018 break;
20020 if (is_redundant_typedef (decl))
20021 gen_type_die (TREE_TYPE (decl), context_die);
20022 else
20023 /* Output a DIE to represent the typedef itself. */
20024 gen_typedef_die (decl, context_die);
20025 break;
20027 case LABEL_DECL:
20028 if (debug_info_level >= DINFO_LEVEL_NORMAL)
20029 gen_label_die (decl, context_die);
20030 break;
20032 case VAR_DECL:
20033 case RESULT_DECL:
20034 /* If we are in terse mode, don't generate any DIEs to represent any
20035 variable declarations or definitions. */
20036 if (debug_info_level <= DINFO_LEVEL_TERSE)
20037 break;
20039 /* Output any DIEs that are needed to specify the type of this data
20040 object. */
20041 if (decl_by_reference_p (decl_or_origin))
20042 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20043 else
20044 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20046 /* And its containing type. */
20047 class_origin = decl_class_context (decl_or_origin);
20048 if (class_origin != NULL_TREE)
20049 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
20051 /* And its containing namespace. */
20052 context_die = declare_in_namespace (decl_or_origin, context_die);
20054 /* Now output the DIE to represent the data object itself. This gets
20055 complicated because of the possibility that the VAR_DECL really
20056 represents an inlined instance of a formal parameter for an inline
20057 function. */
20058 ultimate_origin = decl_ultimate_origin (decl_or_origin);
20059 if (ultimate_origin != NULL_TREE
20060 && TREE_CODE (ultimate_origin) == PARM_DECL)
20061 gen_formal_parameter_die (decl, origin,
20062 true /* Emit name attribute. */,
20063 context_die);
20064 else
20065 gen_variable_die (decl, origin, context_die);
20066 break;
20068 case FIELD_DECL:
20069 /* Ignore the nameless fields that are used to skip bits but handle C++
20070 anonymous unions and structs. */
20071 if (DECL_NAME (decl) != NULL_TREE
20072 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
20073 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
20075 gen_type_die (member_declared_type (decl), context_die);
20076 gen_field_die (decl, context_die);
20078 break;
20080 case PARM_DECL:
20081 if (DECL_BY_REFERENCE (decl_or_origin))
20082 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20083 else
20084 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20085 return gen_formal_parameter_die (decl, origin,
20086 true /* Emit name attribute. */,
20087 context_die);
20089 case NAMESPACE_DECL:
20090 case IMPORTED_DECL:
20091 if (dwarf_version >= 3 || !dwarf_strict)
20092 gen_namespace_die (decl, context_die);
20093 break;
20095 default:
20096 /* Probably some frontend-internal decl. Assume we don't care. */
20097 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
20098 break;
20101 return NULL;
20104 /* Output debug information for global decl DECL. Called from toplev.c after
20105 compilation proper has finished. */
20107 static void
20108 dwarf2out_global_decl (tree decl)
20110 /* Output DWARF2 information for file-scope tentative data object
20111 declarations, file-scope (extern) function declarations (which
20112 had no corresponding body) and file-scope tagged type declarations
20113 and definitions which have not yet been forced out. */
20114 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
20115 dwarf2out_decl (decl);
20118 /* Output debug information for type decl DECL. Called from toplev.c
20119 and from language front ends (to record built-in types). */
20120 static void
20121 dwarf2out_type_decl (tree decl, int local)
20123 if (!local)
20124 dwarf2out_decl (decl);
20127 /* Output debug information for imported module or decl DECL.
20128 NAME is non-NULL name in the lexical block if the decl has been renamed.
20129 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
20130 that DECL belongs to.
20131 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
20132 static void
20133 dwarf2out_imported_module_or_decl_1 (tree decl,
20134 tree name,
20135 tree lexical_block,
20136 dw_die_ref lexical_block_die)
20138 expanded_location xloc;
20139 dw_die_ref imported_die = NULL;
20140 dw_die_ref at_import_die;
20142 if (TREE_CODE (decl) == IMPORTED_DECL)
20144 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
20145 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
20146 gcc_assert (decl);
20148 else
20149 xloc = expand_location (input_location);
20151 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
20153 at_import_die = force_type_die (TREE_TYPE (decl));
20154 /* For namespace N { typedef void T; } using N::T; base_type_die
20155 returns NULL, but DW_TAG_imported_declaration requires
20156 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
20157 if (!at_import_die)
20159 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
20160 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
20161 at_import_die = lookup_type_die (TREE_TYPE (decl));
20162 gcc_assert (at_import_die);
20165 else
20167 at_import_die = lookup_decl_die (decl);
20168 if (!at_import_die)
20170 /* If we're trying to avoid duplicate debug info, we may not have
20171 emitted the member decl for this field. Emit it now. */
20172 if (TREE_CODE (decl) == FIELD_DECL)
20174 tree type = DECL_CONTEXT (decl);
20176 if (TYPE_CONTEXT (type)
20177 && TYPE_P (TYPE_CONTEXT (type))
20178 && !should_emit_struct_debug (TYPE_CONTEXT (type),
20179 DINFO_USAGE_DIR_USE))
20180 return;
20181 gen_type_die_for_member (type, decl,
20182 get_context_die (TYPE_CONTEXT (type)));
20184 at_import_die = force_decl_die (decl);
20188 if (TREE_CODE (decl) == NAMESPACE_DECL)
20190 if (dwarf_version >= 3 || !dwarf_strict)
20191 imported_die = new_die (DW_TAG_imported_module,
20192 lexical_block_die,
20193 lexical_block);
20194 else
20195 return;
20197 else
20198 imported_die = new_die (DW_TAG_imported_declaration,
20199 lexical_block_die,
20200 lexical_block);
20202 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
20203 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
20204 if (name)
20205 add_AT_string (imported_die, DW_AT_name,
20206 IDENTIFIER_POINTER (name));
20207 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
20210 /* Output debug information for imported module or decl DECL.
20211 NAME is non-NULL name in context if the decl has been renamed.
20212 CHILD is true if decl is one of the renamed decls as part of
20213 importing whole module. */
20215 static void
20216 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
20217 bool child)
20219 /* dw_die_ref at_import_die; */
20220 dw_die_ref scope_die;
20222 if (debug_info_level <= DINFO_LEVEL_TERSE)
20223 return;
20225 gcc_assert (decl);
20227 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
20228 We need decl DIE for reference and scope die. First, get DIE for the decl
20229 itself. */
20231 /* Get the scope die for decl context. Use comp_unit_die for global module
20232 or decl. If die is not found for non globals, force new die. */
20233 if (context
20234 && TYPE_P (context)
20235 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
20236 return;
20238 if (!(dwarf_version >= 3 || !dwarf_strict))
20239 return;
20241 scope_die = get_context_die (context);
20243 if (child)
20245 gcc_assert (scope_die->die_child);
20246 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
20247 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
20248 scope_die = scope_die->die_child;
20251 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
20252 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
20256 /* Write the debugging output for DECL. */
20258 void
20259 dwarf2out_decl (tree decl)
20261 dw_die_ref context_die = comp_unit_die ();
20263 switch (TREE_CODE (decl))
20265 case ERROR_MARK:
20266 return;
20268 case FUNCTION_DECL:
20269 /* What we would really like to do here is to filter out all mere
20270 file-scope declarations of file-scope functions which are never
20271 referenced later within this translation unit (and keep all of ones
20272 that *are* referenced later on) but we aren't clairvoyant, so we have
20273 no idea which functions will be referenced in the future (i.e. later
20274 on within the current translation unit). So here we just ignore all
20275 file-scope function declarations which are not also definitions. If
20276 and when the debugger needs to know something about these functions,
20277 it will have to hunt around and find the DWARF information associated
20278 with the definition of the function.
20280 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
20281 nodes represent definitions and which ones represent mere
20282 declarations. We have to check DECL_INITIAL instead. That's because
20283 the C front-end supports some weird semantics for "extern inline"
20284 function definitions. These can get inlined within the current
20285 translation unit (and thus, we need to generate Dwarf info for their
20286 abstract instances so that the Dwarf info for the concrete inlined
20287 instances can have something to refer to) but the compiler never
20288 generates any out-of-lines instances of such things (despite the fact
20289 that they *are* definitions).
20291 The important point is that the C front-end marks these "extern
20292 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
20293 them anyway. Note that the C++ front-end also plays some similar games
20294 for inline function definitions appearing within include files which
20295 also contain `#pragma interface' pragmas.
20297 If we are called from dwarf2out_abstract_function output a DIE
20298 anyway. We can end up here this way with early inlining and LTO
20299 where the inlined function is output in a different LTRANS unit
20300 or not at all. */
20301 if (DECL_INITIAL (decl) == NULL_TREE
20302 && ! DECL_ABSTRACT (decl))
20303 return;
20305 /* If we're a nested function, initially use a parent of NULL; if we're
20306 a plain function, this will be fixed up in decls_for_scope. If
20307 we're a method, it will be ignored, since we already have a DIE. */
20308 if (decl_function_context (decl)
20309 /* But if we're in terse mode, we don't care about scope. */
20310 && debug_info_level > DINFO_LEVEL_TERSE)
20311 context_die = NULL;
20312 break;
20314 case VAR_DECL:
20315 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
20316 declaration and if the declaration was never even referenced from
20317 within this entire compilation unit. We suppress these DIEs in
20318 order to save space in the .debug section (by eliminating entries
20319 which are probably useless). Note that we must not suppress
20320 block-local extern declarations (whether used or not) because that
20321 would screw-up the debugger's name lookup mechanism and cause it to
20322 miss things which really ought to be in scope at a given point. */
20323 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
20324 return;
20326 /* For local statics lookup proper context die. */
20327 if (TREE_STATIC (decl)
20328 && DECL_CONTEXT (decl)
20329 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
20330 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20332 /* If we are in terse mode, don't generate any DIEs to represent any
20333 variable declarations or definitions. */
20334 if (debug_info_level <= DINFO_LEVEL_TERSE)
20335 return;
20336 break;
20338 case CONST_DECL:
20339 if (debug_info_level <= DINFO_LEVEL_TERSE)
20340 return;
20341 if (!is_fortran () && !is_ada ())
20342 return;
20343 if (TREE_STATIC (decl) && decl_function_context (decl))
20344 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20345 break;
20347 case NAMESPACE_DECL:
20348 case IMPORTED_DECL:
20349 if (debug_info_level <= DINFO_LEVEL_TERSE)
20350 return;
20351 if (lookup_decl_die (decl) != NULL)
20352 return;
20353 break;
20355 case TYPE_DECL:
20356 /* Don't emit stubs for types unless they are needed by other DIEs. */
20357 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
20358 return;
20360 /* Don't bother trying to generate any DIEs to represent any of the
20361 normal built-in types for the language we are compiling. */
20362 if (DECL_IS_BUILTIN (decl))
20363 return;
20365 /* If we are in terse mode, don't generate any DIEs for types. */
20366 if (debug_info_level <= DINFO_LEVEL_TERSE)
20367 return;
20369 /* If we're a function-scope tag, initially use a parent of NULL;
20370 this will be fixed up in decls_for_scope. */
20371 if (decl_function_context (decl))
20372 context_die = NULL;
20374 break;
20376 default:
20377 return;
20380 gen_decl_die (decl, NULL, context_die);
20383 /* Write the debugging output for DECL. */
20385 static void
20386 dwarf2out_function_decl (tree decl)
20388 dwarf2out_decl (decl);
20389 call_arg_locations = NULL;
20390 call_arg_loc_last = NULL;
20391 call_site_count = -1;
20392 tail_call_site_count = -1;
20393 block_map.release ();
20394 htab_empty (decl_loc_table);
20395 htab_empty (cached_dw_loc_list_table);
20398 /* Output a marker (i.e. a label) for the beginning of the generated code for
20399 a lexical block. */
20401 static void
20402 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
20403 unsigned int blocknum)
20405 switch_to_section (current_function_section ());
20406 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
20409 /* Output a marker (i.e. a label) for the end of the generated code for a
20410 lexical block. */
20412 static void
20413 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
20415 switch_to_section (current_function_section ());
20416 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
20419 /* Returns nonzero if it is appropriate not to emit any debugging
20420 information for BLOCK, because it doesn't contain any instructions.
20422 Don't allow this for blocks with nested functions or local classes
20423 as we would end up with orphans, and in the presence of scheduling
20424 we may end up calling them anyway. */
20426 static bool
20427 dwarf2out_ignore_block (const_tree block)
20429 tree decl;
20430 unsigned int i;
20432 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
20433 if (TREE_CODE (decl) == FUNCTION_DECL
20434 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20435 return 0;
20436 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
20438 decl = BLOCK_NONLOCALIZED_VAR (block, i);
20439 if (TREE_CODE (decl) == FUNCTION_DECL
20440 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20441 return 0;
20444 return 1;
20447 /* Hash table routines for file_hash. */
20449 static int
20450 file_table_eq (const void *p1_p, const void *p2_p)
20452 const struct dwarf_file_data *const p1 =
20453 (const struct dwarf_file_data *) p1_p;
20454 const char *const p2 = (const char *) p2_p;
20455 return filename_cmp (p1->filename, p2) == 0;
20458 static hashval_t
20459 file_table_hash (const void *p_p)
20461 const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
20462 return htab_hash_string (p->filename);
20465 /* Lookup FILE_NAME (in the list of filenames that we know about here in
20466 dwarf2out.c) and return its "index". The index of each (known) filename is
20467 just a unique number which is associated with only that one filename. We
20468 need such numbers for the sake of generating labels (in the .debug_sfnames
20469 section) and references to those files numbers (in the .debug_srcinfo
20470 and.debug_macinfo sections). If the filename given as an argument is not
20471 found in our current list, add it to the list and assign it the next
20472 available unique index number. In order to speed up searches, we remember
20473 the index of the filename was looked up last. This handles the majority of
20474 all searches. */
20476 static struct dwarf_file_data *
20477 lookup_filename (const char *file_name)
20479 void ** slot;
20480 struct dwarf_file_data * created;
20482 /* Check to see if the file name that was searched on the previous
20483 call matches this file name. If so, return the index. */
20484 if (file_table_last_lookup
20485 && (file_name == file_table_last_lookup->filename
20486 || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
20487 return file_table_last_lookup;
20489 /* Didn't match the previous lookup, search the table. */
20490 slot = htab_find_slot_with_hash (file_table, file_name,
20491 htab_hash_string (file_name), INSERT);
20492 if (*slot)
20493 return (struct dwarf_file_data *) *slot;
20495 created = ggc_alloc_dwarf_file_data ();
20496 created->filename = file_name;
20497 created->emitted_number = 0;
20498 *slot = created;
20499 return created;
20502 /* If the assembler will construct the file table, then translate the compiler
20503 internal file table number into the assembler file table number, and emit
20504 a .file directive if we haven't already emitted one yet. The file table
20505 numbers are different because we prune debug info for unused variables and
20506 types, which may include filenames. */
20508 static int
20509 maybe_emit_file (struct dwarf_file_data * fd)
20511 if (! fd->emitted_number)
20513 if (last_emitted_file)
20514 fd->emitted_number = last_emitted_file->emitted_number + 1;
20515 else
20516 fd->emitted_number = 1;
20517 last_emitted_file = fd;
20519 if (DWARF2_ASM_LINE_DEBUG_INFO)
20521 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
20522 output_quoted_string (asm_out_file,
20523 remap_debug_filename (fd->filename));
20524 fputc ('\n', asm_out_file);
20528 return fd->emitted_number;
20531 /* Schedule generation of a DW_AT_const_value attribute to DIE.
20532 That generation should happen after function debug info has been
20533 generated. The value of the attribute is the constant value of ARG. */
20535 static void
20536 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
20538 die_arg_entry entry;
20540 if (!die || !arg)
20541 return;
20543 if (!tmpl_value_parm_die_table)
20544 vec_alloc (tmpl_value_parm_die_table, 32);
20546 entry.die = die;
20547 entry.arg = arg;
20548 vec_safe_push (tmpl_value_parm_die_table, entry);
20551 /* Return TRUE if T is an instance of generic type, FALSE
20552 otherwise. */
20554 static bool
20555 generic_type_p (tree t)
20557 if (t == NULL_TREE || !TYPE_P (t))
20558 return false;
20559 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
20562 /* Schedule the generation of the generic parameter dies for the
20563 instance of generic type T. The proper generation itself is later
20564 done by gen_scheduled_generic_parms_dies. */
20566 static void
20567 schedule_generic_params_dies_gen (tree t)
20569 if (!generic_type_p (t))
20570 return;
20572 if (!generic_type_instances)
20573 vec_alloc (generic_type_instances, 256);
20575 vec_safe_push (generic_type_instances, t);
20578 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
20579 by append_entry_to_tmpl_value_parm_die_table. This function must
20580 be called after function DIEs have been generated. */
20582 static void
20583 gen_remaining_tmpl_value_param_die_attribute (void)
20585 if (tmpl_value_parm_die_table)
20587 unsigned i;
20588 die_arg_entry *e;
20590 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
20591 tree_add_const_value_attribute (e->die, e->arg);
20595 /* Generate generic parameters DIEs for instances of generic types
20596 that have been previously scheduled by
20597 schedule_generic_params_dies_gen. This function must be called
20598 after all the types of the CU have been laid out. */
20600 static void
20601 gen_scheduled_generic_parms_dies (void)
20603 unsigned i;
20604 tree t;
20606 if (!generic_type_instances)
20607 return;
20609 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
20610 if (COMPLETE_TYPE_P (t))
20611 gen_generic_params_dies (t);
20615 /* Replace DW_AT_name for the decl with name. */
20617 static void
20618 dwarf2out_set_name (tree decl, tree name)
20620 dw_die_ref die;
20621 dw_attr_ref attr;
20622 const char *dname;
20624 die = TYPE_SYMTAB_DIE (decl);
20625 if (!die)
20626 return;
20628 dname = dwarf2_name (name, 0);
20629 if (!dname)
20630 return;
20632 attr = get_AT (die, DW_AT_name);
20633 if (attr)
20635 struct indirect_string_node *node;
20637 node = find_AT_string (dname);
20638 /* replace the string. */
20639 attr->dw_attr_val.v.val_str = node;
20642 else
20643 add_name_attribute (die, dname);
20646 /* True if before or during processing of the first function being emitted. */
20647 static bool in_first_function_p = true;
20648 /* True if loc_note during dwarf2out_var_location call might still be
20649 before first real instruction at address equal to .Ltext0. */
20650 static bool maybe_at_text_label_p = true;
20651 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
20652 static unsigned int first_loclabel_num_not_at_text_label;
20654 /* Called by the final INSN scan whenever we see a var location. We
20655 use it to drop labels in the right places, and throw the location in
20656 our lookup table. */
20658 static void
20659 dwarf2out_var_location (rtx loc_note)
20661 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
20662 struct var_loc_node *newloc;
20663 rtx next_real, next_note;
20664 static const char *last_label;
20665 static const char *last_postcall_label;
20666 static bool last_in_cold_section_p;
20667 static rtx expected_next_loc_note;
20668 tree decl;
20669 bool var_loc_p;
20671 if (!NOTE_P (loc_note))
20673 if (CALL_P (loc_note))
20675 call_site_count++;
20676 if (SIBLING_CALL_P (loc_note))
20677 tail_call_site_count++;
20679 return;
20682 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
20683 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
20684 return;
20686 /* Optimize processing a large consecutive sequence of location
20687 notes so we don't spend too much time in next_real_insn. If the
20688 next insn is another location note, remember the next_real_insn
20689 calculation for next time. */
20690 next_real = cached_next_real_insn;
20691 if (next_real)
20693 if (expected_next_loc_note != loc_note)
20694 next_real = NULL_RTX;
20697 next_note = NEXT_INSN (loc_note);
20698 if (! next_note
20699 || INSN_DELETED_P (next_note)
20700 || GET_CODE (next_note) != NOTE
20701 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
20702 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
20703 next_note = NULL_RTX;
20705 if (! next_real)
20706 next_real = next_real_insn (loc_note);
20708 if (next_note)
20710 expected_next_loc_note = next_note;
20711 cached_next_real_insn = next_real;
20713 else
20714 cached_next_real_insn = NULL_RTX;
20716 /* If there are no instructions which would be affected by this note,
20717 don't do anything. */
20718 if (var_loc_p
20719 && next_real == NULL_RTX
20720 && !NOTE_DURING_CALL_P (loc_note))
20721 return;
20723 if (next_real == NULL_RTX)
20724 next_real = get_last_insn ();
20726 /* If there were any real insns between note we processed last time
20727 and this note (or if it is the first note), clear
20728 last_{,postcall_}label so that they are not reused this time. */
20729 if (last_var_location_insn == NULL_RTX
20730 || last_var_location_insn != next_real
20731 || last_in_cold_section_p != in_cold_section_p)
20733 last_label = NULL;
20734 last_postcall_label = NULL;
20737 if (var_loc_p)
20739 decl = NOTE_VAR_LOCATION_DECL (loc_note);
20740 newloc = add_var_loc_to_decl (decl, loc_note,
20741 NOTE_DURING_CALL_P (loc_note)
20742 ? last_postcall_label : last_label);
20743 if (newloc == NULL)
20744 return;
20746 else
20748 decl = NULL_TREE;
20749 newloc = NULL;
20752 /* If there were no real insns between note we processed last time
20753 and this note, use the label we emitted last time. Otherwise
20754 create a new label and emit it. */
20755 if (last_label == NULL)
20757 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
20758 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
20759 loclabel_num++;
20760 last_label = ggc_strdup (loclabel);
20761 /* See if loclabel might be equal to .Ltext0. If yes,
20762 bump first_loclabel_num_not_at_text_label. */
20763 if (!have_multiple_function_sections
20764 && in_first_function_p
20765 && maybe_at_text_label_p)
20767 static rtx last_start;
20768 rtx insn;
20769 for (insn = loc_note; insn; insn = previous_insn (insn))
20770 if (insn == last_start)
20771 break;
20772 else if (!NONDEBUG_INSN_P (insn))
20773 continue;
20774 else
20776 rtx body = PATTERN (insn);
20777 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
20778 continue;
20779 /* Inline asm could occupy zero bytes. */
20780 else if (GET_CODE (body) == ASM_INPUT
20781 || asm_noperands (body) >= 0)
20782 continue;
20783 #ifdef HAVE_attr_length
20784 else if (get_attr_min_length (insn) == 0)
20785 continue;
20786 #endif
20787 else
20789 /* Assume insn has non-zero length. */
20790 maybe_at_text_label_p = false;
20791 break;
20794 if (maybe_at_text_label_p)
20796 last_start = loc_note;
20797 first_loclabel_num_not_at_text_label = loclabel_num;
20802 if (!var_loc_p)
20804 struct call_arg_loc_node *ca_loc
20805 = ggc_alloc_cleared_call_arg_loc_node ();
20806 rtx prev = prev_real_insn (loc_note), x;
20807 ca_loc->call_arg_loc_note = loc_note;
20808 ca_loc->next = NULL;
20809 ca_loc->label = last_label;
20810 gcc_assert (prev
20811 && (CALL_P (prev)
20812 || (NONJUMP_INSN_P (prev)
20813 && GET_CODE (PATTERN (prev)) == SEQUENCE
20814 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
20815 if (!CALL_P (prev))
20816 prev = XVECEXP (PATTERN (prev), 0, 0);
20817 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
20818 x = get_call_rtx_from (PATTERN (prev));
20819 if (x)
20821 x = XEXP (XEXP (x, 0), 0);
20822 if (GET_CODE (x) == SYMBOL_REF
20823 && SYMBOL_REF_DECL (x)
20824 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
20825 ca_loc->symbol_ref = x;
20827 ca_loc->block = insn_scope (prev);
20828 if (call_arg_locations)
20829 call_arg_loc_last->next = ca_loc;
20830 else
20831 call_arg_locations = ca_loc;
20832 call_arg_loc_last = ca_loc;
20834 else if (!NOTE_DURING_CALL_P (loc_note))
20835 newloc->label = last_label;
20836 else
20838 if (!last_postcall_label)
20840 sprintf (loclabel, "%s-1", last_label);
20841 last_postcall_label = ggc_strdup (loclabel);
20843 newloc->label = last_postcall_label;
20846 last_var_location_insn = next_real;
20847 last_in_cold_section_p = in_cold_section_p;
20850 /* Note in one location list that text section has changed. */
20852 static int
20853 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
20855 var_loc_list *list = (var_loc_list *) *slot;
20856 if (list->first)
20857 list->last_before_switch
20858 = list->last->next ? list->last->next : list->last;
20859 return 1;
20862 /* Note in all location lists that text section has changed. */
20864 static void
20865 var_location_switch_text_section (void)
20867 if (decl_loc_table == NULL)
20868 return;
20870 htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
20873 /* Create a new line number table. */
20875 static dw_line_info_table *
20876 new_line_info_table (void)
20878 dw_line_info_table *table;
20880 table = ggc_alloc_cleared_dw_line_info_table_struct ();
20881 table->file_num = 1;
20882 table->line_num = 1;
20883 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
20885 return table;
20888 /* Lookup the "current" table into which we emit line info, so
20889 that we don't have to do it for every source line. */
20891 static void
20892 set_cur_line_info_table (section *sec)
20894 dw_line_info_table *table;
20896 if (sec == text_section)
20897 table = text_section_line_info;
20898 else if (sec == cold_text_section)
20900 table = cold_text_section_line_info;
20901 if (!table)
20903 cold_text_section_line_info = table = new_line_info_table ();
20904 table->end_label = cold_end_label;
20907 else
20909 const char *end_label;
20911 if (flag_reorder_blocks_and_partition)
20913 if (in_cold_section_p)
20914 end_label = crtl->subsections.cold_section_end_label;
20915 else
20916 end_label = crtl->subsections.hot_section_end_label;
20918 else
20920 char label[MAX_ARTIFICIAL_LABEL_BYTES];
20921 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
20922 current_function_funcdef_no);
20923 end_label = ggc_strdup (label);
20926 table = new_line_info_table ();
20927 table->end_label = end_label;
20929 vec_safe_push (separate_line_info, table);
20932 if (DWARF2_ASM_LINE_DEBUG_INFO)
20933 table->is_stmt = (cur_line_info_table
20934 ? cur_line_info_table->is_stmt
20935 : DWARF_LINE_DEFAULT_IS_STMT_START);
20936 cur_line_info_table = table;
20940 /* We need to reset the locations at the beginning of each
20941 function. We can't do this in the end_function hook, because the
20942 declarations that use the locations won't have been output when
20943 that hook is called. Also compute have_multiple_function_sections here. */
20945 static void
20946 dwarf2out_begin_function (tree fun)
20948 section *sec = function_section (fun);
20950 if (sec != text_section)
20951 have_multiple_function_sections = true;
20953 if (flag_reorder_blocks_and_partition && !cold_text_section)
20955 gcc_assert (current_function_decl == fun);
20956 cold_text_section = unlikely_text_section ();
20957 switch_to_section (cold_text_section);
20958 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
20959 switch_to_section (sec);
20962 dwarf2out_note_section_used ();
20963 call_site_count = 0;
20964 tail_call_site_count = 0;
20966 set_cur_line_info_table (sec);
20969 /* Helper function of dwarf2out_end_function, called only after emitting
20970 the very first function into assembly. Check if some .debug_loc range
20971 might end with a .LVL* label that could be equal to .Ltext0.
20972 In that case we must force using absolute addresses in .debug_loc ranges,
20973 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
20974 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
20975 list terminator.
20976 Set have_multiple_function_sections to true in that case and
20977 terminate htab traversal. */
20979 static int
20980 find_empty_loc_ranges_at_text_label (void **slot, void *)
20982 var_loc_list *entry;
20983 struct var_loc_node *node;
20985 entry = (var_loc_list *) *slot;
20986 node = entry->first;
20987 if (node && node->next && node->next->label)
20989 unsigned int i;
20990 const char *label = node->next->label;
20991 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
20993 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
20995 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
20996 if (strcmp (label, loclabel) == 0)
20998 have_multiple_function_sections = true;
20999 return 0;
21003 return 1;
21006 /* Hook called after emitting a function into assembly.
21007 This does something only for the very first function emitted. */
21009 static void
21010 dwarf2out_end_function (unsigned int)
21012 if (in_first_function_p
21013 && !have_multiple_function_sections
21014 && first_loclabel_num_not_at_text_label
21015 && decl_loc_table)
21016 htab_traverse (decl_loc_table, find_empty_loc_ranges_at_text_label,
21017 NULL);
21018 in_first_function_p = false;
21019 maybe_at_text_label_p = false;
21022 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
21024 static void
21025 push_dw_line_info_entry (dw_line_info_table *table,
21026 enum dw_line_info_opcode opcode, unsigned int val)
21028 dw_line_info_entry e;
21029 e.opcode = opcode;
21030 e.val = val;
21031 vec_safe_push (table->entries, e);
21034 /* Output a label to mark the beginning of a source code line entry
21035 and record information relating to this source line, in
21036 'line_info_table' for later output of the .debug_line section. */
21037 /* ??? The discriminator parameter ought to be unsigned. */
21039 static void
21040 dwarf2out_source_line (unsigned int line, const char *filename,
21041 int discriminator, bool is_stmt)
21043 unsigned int file_num;
21044 dw_line_info_table *table;
21046 if (debug_info_level < DINFO_LEVEL_NORMAL || line == 0)
21047 return;
21049 /* The discriminator column was added in dwarf4. Simplify the below
21050 by simply removing it if we're not supposed to output it. */
21051 if (dwarf_version < 4 && dwarf_strict)
21052 discriminator = 0;
21054 table = cur_line_info_table;
21055 file_num = maybe_emit_file (lookup_filename (filename));
21057 /* ??? TODO: Elide duplicate line number entries. Traditionally,
21058 the debugger has used the second (possibly duplicate) line number
21059 at the beginning of the function to mark the end of the prologue.
21060 We could eliminate any other duplicates within the function. For
21061 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
21062 that second line number entry. */
21063 /* Recall that this end-of-prologue indication is *not* the same thing
21064 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
21065 to which the hook corresponds, follows the last insn that was
21066 emitted by gen_prologue. What we need is to precede the first insn
21067 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
21068 insn that corresponds to something the user wrote. These may be
21069 very different locations once scheduling is enabled. */
21071 if (0 && file_num == table->file_num
21072 && line == table->line_num
21073 && discriminator == table->discrim_num
21074 && is_stmt == table->is_stmt)
21075 return;
21077 switch_to_section (current_function_section ());
21079 /* If requested, emit something human-readable. */
21080 if (flag_debug_asm)
21081 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
21083 if (DWARF2_ASM_LINE_DEBUG_INFO)
21085 /* Emit the .loc directive understood by GNU as. */
21086 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
21087 file_num, line, is_stmt, discriminator */
21088 fputs ("\t.loc ", asm_out_file);
21089 fprint_ul (asm_out_file, file_num);
21090 putc (' ', asm_out_file);
21091 fprint_ul (asm_out_file, line);
21092 putc (' ', asm_out_file);
21093 putc ('0', asm_out_file);
21095 if (is_stmt != table->is_stmt)
21097 fputs (" is_stmt ", asm_out_file);
21098 putc (is_stmt ? '1' : '0', asm_out_file);
21100 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
21102 gcc_assert (discriminator > 0);
21103 fputs (" discriminator ", asm_out_file);
21104 fprint_ul (asm_out_file, (unsigned long) discriminator);
21106 putc ('\n', asm_out_file);
21108 else
21110 unsigned int label_num = ++line_info_label_num;
21112 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
21114 push_dw_line_info_entry (table, LI_set_address, label_num);
21115 if (file_num != table->file_num)
21116 push_dw_line_info_entry (table, LI_set_file, file_num);
21117 if (discriminator != table->discrim_num)
21118 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
21119 if (is_stmt != table->is_stmt)
21120 push_dw_line_info_entry (table, LI_negate_stmt, 0);
21121 push_dw_line_info_entry (table, LI_set_line, line);
21124 table->file_num = file_num;
21125 table->line_num = line;
21126 table->discrim_num = discriminator;
21127 table->is_stmt = is_stmt;
21128 table->in_use = true;
21131 /* Record the beginning of a new source file. */
21133 static void
21134 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
21136 if (flag_eliminate_dwarf2_dups)
21138 /* Record the beginning of the file for break_out_includes. */
21139 dw_die_ref bincl_die;
21141 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
21142 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
21145 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21147 macinfo_entry e;
21148 e.code = DW_MACINFO_start_file;
21149 e.lineno = lineno;
21150 e.info = ggc_strdup (filename);
21151 vec_safe_push (macinfo_table, e);
21155 /* Record the end of a source file. */
21157 static void
21158 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
21160 if (flag_eliminate_dwarf2_dups)
21161 /* Record the end of the file for break_out_includes. */
21162 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
21164 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21166 macinfo_entry e;
21167 e.code = DW_MACINFO_end_file;
21168 e.lineno = lineno;
21169 e.info = NULL;
21170 vec_safe_push (macinfo_table, e);
21174 /* Called from debug_define in toplev.c. The `buffer' parameter contains
21175 the tail part of the directive line, i.e. the part which is past the
21176 initial whitespace, #, whitespace, directive-name, whitespace part. */
21178 static void
21179 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
21180 const char *buffer ATTRIBUTE_UNUSED)
21182 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21184 macinfo_entry e;
21185 /* Insert a dummy first entry to be able to optimize the whole
21186 predefined macro block using DW_MACRO_GNU_transparent_include. */
21187 if (macinfo_table->is_empty () && lineno <= 1)
21189 e.code = 0;
21190 e.lineno = 0;
21191 e.info = NULL;
21192 vec_safe_push (macinfo_table, e);
21194 e.code = DW_MACINFO_define;
21195 e.lineno = lineno;
21196 e.info = ggc_strdup (buffer);
21197 vec_safe_push (macinfo_table, e);
21201 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
21202 the tail part of the directive line, i.e. the part which is past the
21203 initial whitespace, #, whitespace, directive-name, whitespace part. */
21205 static void
21206 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
21207 const char *buffer ATTRIBUTE_UNUSED)
21209 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21211 macinfo_entry e;
21212 /* Insert a dummy first entry to be able to optimize the whole
21213 predefined macro block using DW_MACRO_GNU_transparent_include. */
21214 if (macinfo_table->is_empty () && lineno <= 1)
21216 e.code = 0;
21217 e.lineno = 0;
21218 e.info = NULL;
21219 vec_safe_push (macinfo_table, e);
21221 e.code = DW_MACINFO_undef;
21222 e.lineno = lineno;
21223 e.info = ggc_strdup (buffer);
21224 vec_safe_push (macinfo_table, e);
21228 /* Routines to manipulate hash table of CUs. */
21230 static hashval_t
21231 htab_macinfo_hash (const void *of)
21233 const macinfo_entry *const entry =
21234 (const macinfo_entry *) of;
21236 return htab_hash_string (entry->info);
21239 static int
21240 htab_macinfo_eq (const void *of1, const void *of2)
21242 const macinfo_entry *const entry1 = (const macinfo_entry *) of1;
21243 const macinfo_entry *const entry2 = (const macinfo_entry *) of2;
21245 return !strcmp (entry1->info, entry2->info);
21248 /* Output a single .debug_macinfo entry. */
21250 static void
21251 output_macinfo_op (macinfo_entry *ref)
21253 int file_num;
21254 size_t len;
21255 struct indirect_string_node *node;
21256 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21257 struct dwarf_file_data *fd;
21259 switch (ref->code)
21261 case DW_MACINFO_start_file:
21262 fd = lookup_filename (ref->info);
21263 file_num = maybe_emit_file (fd);
21264 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
21265 dw2_asm_output_data_uleb128 (ref->lineno,
21266 "Included from line number %lu",
21267 (unsigned long) ref->lineno);
21268 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
21269 break;
21270 case DW_MACINFO_end_file:
21271 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
21272 break;
21273 case DW_MACINFO_define:
21274 case DW_MACINFO_undef:
21275 len = strlen (ref->info) + 1;
21276 if (!dwarf_strict
21277 && len > DWARF_OFFSET_SIZE
21278 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21279 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21281 ref->code = ref->code == DW_MACINFO_define
21282 ? DW_MACRO_GNU_define_indirect
21283 : DW_MACRO_GNU_undef_indirect;
21284 output_macinfo_op (ref);
21285 return;
21287 dw2_asm_output_data (1, ref->code,
21288 ref->code == DW_MACINFO_define
21289 ? "Define macro" : "Undefine macro");
21290 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21291 (unsigned long) ref->lineno);
21292 dw2_asm_output_nstring (ref->info, -1, "The macro");
21293 break;
21294 case DW_MACRO_GNU_define_indirect:
21295 case DW_MACRO_GNU_undef_indirect:
21296 node = find_AT_string (ref->info);
21297 gcc_assert (node
21298 && ((node->form == DW_FORM_strp)
21299 || (node->form == DW_FORM_GNU_str_index)));
21300 dw2_asm_output_data (1, ref->code,
21301 ref->code == DW_MACRO_GNU_define_indirect
21302 ? "Define macro indirect"
21303 : "Undefine macro indirect");
21304 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21305 (unsigned long) ref->lineno);
21306 if (node->form == DW_FORM_strp)
21307 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
21308 debug_str_section, "The macro: \"%s\"",
21309 ref->info);
21310 else
21311 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
21312 ref->info);
21313 break;
21314 case DW_MACRO_GNU_transparent_include:
21315 dw2_asm_output_data (1, ref->code, "Transparent include");
21316 ASM_GENERATE_INTERNAL_LABEL (label,
21317 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
21318 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
21319 break;
21320 default:
21321 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
21322 ASM_COMMENT_START, (unsigned long) ref->code);
21323 break;
21327 /* Attempt to make a sequence of define/undef macinfo ops shareable with
21328 other compilation unit .debug_macinfo sections. IDX is the first
21329 index of a define/undef, return the number of ops that should be
21330 emitted in a comdat .debug_macinfo section and emit
21331 a DW_MACRO_GNU_transparent_include entry referencing it.
21332 If the define/undef entry should be emitted normally, return 0. */
21334 static unsigned
21335 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
21336 htab_t *macinfo_htab)
21338 macinfo_entry *first, *second, *cur, *inc;
21339 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
21340 unsigned char checksum[16];
21341 struct md5_ctx ctx;
21342 char *grp_name, *tail;
21343 const char *base;
21344 unsigned int i, count, encoded_filename_len, linebuf_len;
21345 void **slot;
21347 first = &(*macinfo_table)[idx];
21348 second = &(*macinfo_table)[idx + 1];
21350 /* Optimize only if there are at least two consecutive define/undef ops,
21351 and either all of them are before first DW_MACINFO_start_file
21352 with lineno {0,1} (i.e. predefined macro block), or all of them are
21353 in some included header file. */
21354 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
21355 return 0;
21356 if (vec_safe_is_empty (files))
21358 if (first->lineno > 1 || second->lineno > 1)
21359 return 0;
21361 else if (first->lineno == 0)
21362 return 0;
21364 /* Find the last define/undef entry that can be grouped together
21365 with first and at the same time compute md5 checksum of their
21366 codes, linenumbers and strings. */
21367 md5_init_ctx (&ctx);
21368 for (i = idx; macinfo_table->iterate (i, &cur); i++)
21369 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
21370 break;
21371 else if (vec_safe_is_empty (files) && cur->lineno > 1)
21372 break;
21373 else
21375 unsigned char code = cur->code;
21376 md5_process_bytes (&code, 1, &ctx);
21377 checksum_uleb128 (cur->lineno, &ctx);
21378 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
21380 md5_finish_ctx (&ctx, checksum);
21381 count = i - idx;
21383 /* From the containing include filename (if any) pick up just
21384 usable characters from its basename. */
21385 if (vec_safe_is_empty (files))
21386 base = "";
21387 else
21388 base = lbasename (files->last ().info);
21389 for (encoded_filename_len = 0, i = 0; base[i]; i++)
21390 if (ISIDNUM (base[i]) || base[i] == '.')
21391 encoded_filename_len++;
21392 /* Count . at the end. */
21393 if (encoded_filename_len)
21394 encoded_filename_len++;
21396 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
21397 linebuf_len = strlen (linebuf);
21399 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
21400 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
21401 + 16 * 2 + 1);
21402 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
21403 tail = grp_name + 4;
21404 if (encoded_filename_len)
21406 for (i = 0; base[i]; i++)
21407 if (ISIDNUM (base[i]) || base[i] == '.')
21408 *tail++ = base[i];
21409 *tail++ = '.';
21411 memcpy (tail, linebuf, linebuf_len);
21412 tail += linebuf_len;
21413 *tail++ = '.';
21414 for (i = 0; i < 16; i++)
21415 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
21417 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
21418 in the empty vector entry before the first define/undef. */
21419 inc = &(*macinfo_table)[idx - 1];
21420 inc->code = DW_MACRO_GNU_transparent_include;
21421 inc->lineno = 0;
21422 inc->info = ggc_strdup (grp_name);
21423 if (*macinfo_htab == NULL)
21424 *macinfo_htab = htab_create (10, htab_macinfo_hash, htab_macinfo_eq, NULL);
21425 /* Avoid emitting duplicates. */
21426 slot = htab_find_slot (*macinfo_htab, inc, INSERT);
21427 if (*slot != NULL)
21429 inc->code = 0;
21430 inc->info = NULL;
21431 /* If such an entry has been used before, just emit
21432 a DW_MACRO_GNU_transparent_include op. */
21433 inc = (macinfo_entry *) *slot;
21434 output_macinfo_op (inc);
21435 /* And clear all macinfo_entry in the range to avoid emitting them
21436 in the second pass. */
21437 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
21439 cur->code = 0;
21440 cur->info = NULL;
21443 else
21445 *slot = inc;
21446 inc->lineno = htab_elements (*macinfo_htab);
21447 output_macinfo_op (inc);
21449 return count;
21452 /* Save any strings needed by the macinfo table in the debug str
21453 table. All strings must be collected into the table by the time
21454 index_string is called. */
21456 static void
21457 save_macinfo_strings (void)
21459 unsigned len;
21460 unsigned i;
21461 macinfo_entry *ref;
21463 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
21465 switch (ref->code)
21467 /* Match the logic in output_macinfo_op to decide on
21468 indirect strings. */
21469 case DW_MACINFO_define:
21470 case DW_MACINFO_undef:
21471 len = strlen (ref->info) + 1;
21472 if (!dwarf_strict
21473 && len > DWARF_OFFSET_SIZE
21474 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21475 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21476 set_indirect_string (find_AT_string (ref->info));
21477 break;
21478 case DW_MACRO_GNU_define_indirect:
21479 case DW_MACRO_GNU_undef_indirect:
21480 set_indirect_string (find_AT_string (ref->info));
21481 break;
21482 default:
21483 break;
21488 /* Output macinfo section(s). */
21490 static void
21491 output_macinfo (void)
21493 unsigned i;
21494 unsigned long length = vec_safe_length (macinfo_table);
21495 macinfo_entry *ref;
21496 vec<macinfo_entry, va_gc> *files = NULL;
21497 htab_t macinfo_htab = NULL;
21499 if (! length)
21500 return;
21502 /* output_macinfo* uses these interchangeably. */
21503 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
21504 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
21505 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
21506 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
21508 /* For .debug_macro emit the section header. */
21509 if (!dwarf_strict)
21511 dw2_asm_output_data (2, 4, "DWARF macro version number");
21512 if (DWARF_OFFSET_SIZE == 8)
21513 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
21514 else
21515 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
21516 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
21517 (!dwarf_split_debug_info ? debug_line_section_label
21518 : debug_skeleton_line_section_label),
21519 debug_line_section, NULL);
21522 /* In the first loop, it emits the primary .debug_macinfo section
21523 and after each emitted op the macinfo_entry is cleared.
21524 If a longer range of define/undef ops can be optimized using
21525 DW_MACRO_GNU_transparent_include, the
21526 DW_MACRO_GNU_transparent_include op is emitted and kept in
21527 the vector before the first define/undef in the range and the
21528 whole range of define/undef ops is not emitted and kept. */
21529 for (i = 0; macinfo_table->iterate (i, &ref); i++)
21531 switch (ref->code)
21533 case DW_MACINFO_start_file:
21534 vec_safe_push (files, *ref);
21535 break;
21536 case DW_MACINFO_end_file:
21537 if (!vec_safe_is_empty (files))
21538 files->pop ();
21539 break;
21540 case DW_MACINFO_define:
21541 case DW_MACINFO_undef:
21542 if (!dwarf_strict
21543 && HAVE_COMDAT_GROUP
21544 && vec_safe_length (files) != 1
21545 && i > 0
21546 && i + 1 < length
21547 && (*macinfo_table)[i - 1].code == 0)
21549 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
21550 if (count)
21552 i += count - 1;
21553 continue;
21556 break;
21557 case 0:
21558 /* A dummy entry may be inserted at the beginning to be able
21559 to optimize the whole block of predefined macros. */
21560 if (i == 0)
21561 continue;
21562 default:
21563 break;
21565 output_macinfo_op (ref);
21566 ref->info = NULL;
21567 ref->code = 0;
21570 if (macinfo_htab == NULL)
21571 return;
21573 htab_delete (macinfo_htab);
21575 /* If any DW_MACRO_GNU_transparent_include were used, on those
21576 DW_MACRO_GNU_transparent_include entries terminate the
21577 current chain and switch to a new comdat .debug_macinfo
21578 section and emit the define/undef entries within it. */
21579 for (i = 0; macinfo_table->iterate (i, &ref); i++)
21580 switch (ref->code)
21582 case 0:
21583 continue;
21584 case DW_MACRO_GNU_transparent_include:
21586 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21587 tree comdat_key = get_identifier (ref->info);
21588 /* Terminate the previous .debug_macinfo section. */
21589 dw2_asm_output_data (1, 0, "End compilation unit");
21590 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
21591 SECTION_DEBUG
21592 | SECTION_LINKONCE,
21593 comdat_key);
21594 ASM_GENERATE_INTERNAL_LABEL (label,
21595 DEBUG_MACRO_SECTION_LABEL,
21596 ref->lineno);
21597 ASM_OUTPUT_LABEL (asm_out_file, label);
21598 ref->code = 0;
21599 ref->info = NULL;
21600 dw2_asm_output_data (2, 4, "DWARF macro version number");
21601 if (DWARF_OFFSET_SIZE == 8)
21602 dw2_asm_output_data (1, 1, "Flags: 64-bit");
21603 else
21604 dw2_asm_output_data (1, 0, "Flags: 32-bit");
21606 break;
21607 case DW_MACINFO_define:
21608 case DW_MACINFO_undef:
21609 output_macinfo_op (ref);
21610 ref->code = 0;
21611 ref->info = NULL;
21612 break;
21613 default:
21614 gcc_unreachable ();
21618 /* Set up for Dwarf output at the start of compilation. */
21620 static void
21621 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
21623 /* Allocate the file_table. */
21624 file_table = htab_create_ggc (50, file_table_hash,
21625 file_table_eq, NULL);
21627 /* Allocate the decl_die_table. */
21628 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
21629 decl_die_table_eq, NULL);
21631 /* Allocate the decl_loc_table. */
21632 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
21633 decl_loc_table_eq, NULL);
21635 /* Allocate the cached_dw_loc_list_table. */
21636 cached_dw_loc_list_table
21637 = htab_create_ggc (10, cached_dw_loc_list_table_hash,
21638 cached_dw_loc_list_table_eq, NULL);
21640 /* Allocate the initial hunk of the decl_scope_table. */
21641 vec_alloc (decl_scope_table, 256);
21643 /* Allocate the initial hunk of the abbrev_die_table. */
21644 abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
21645 (ABBREV_DIE_TABLE_INCREMENT);
21646 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
21647 /* Zero-th entry is allocated, but unused. */
21648 abbrev_die_table_in_use = 1;
21650 /* Allocate the pubtypes and pubnames vectors. */
21651 vec_alloc (pubname_table, 32);
21652 vec_alloc (pubtype_table, 32);
21654 vec_alloc (incomplete_types, 64);
21656 vec_alloc (used_rtx_array, 32);
21658 if (!dwarf_split_debug_info)
21660 debug_info_section = get_section (DEBUG_INFO_SECTION,
21661 SECTION_DEBUG, NULL);
21662 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
21663 SECTION_DEBUG, NULL);
21664 debug_loc_section = get_section (DEBUG_LOC_SECTION,
21665 SECTION_DEBUG, NULL);
21667 else
21669 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
21670 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
21671 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
21672 SECTION_DEBUG | SECTION_EXCLUDE,
21673 NULL);
21674 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
21675 SECTION_DEBUG, NULL);
21676 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
21677 SECTION_DEBUG, NULL);
21678 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
21679 SECTION_DEBUG, NULL);
21680 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
21681 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
21683 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
21684 the main .o, but the skeleton_line goes into the split off dwo. */
21685 debug_skeleton_line_section
21686 = get_section (DEBUG_DWO_LINE_SECTION,
21687 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
21688 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
21689 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
21690 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
21691 SECTION_DEBUG | SECTION_EXCLUDE,
21692 NULL);
21693 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
21694 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
21695 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
21696 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
21698 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
21699 SECTION_DEBUG, NULL);
21700 debug_macinfo_section = get_section (dwarf_strict
21701 ? DEBUG_MACINFO_SECTION
21702 : DEBUG_MACRO_SECTION,
21703 DEBUG_MACRO_SECTION_FLAGS, NULL);
21704 debug_line_section = get_section (DEBUG_LINE_SECTION,
21705 SECTION_DEBUG, NULL);
21706 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
21707 SECTION_DEBUG, NULL);
21708 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
21709 SECTION_DEBUG, NULL);
21710 debug_str_section = get_section (DEBUG_STR_SECTION,
21711 DEBUG_STR_SECTION_FLAGS, NULL);
21712 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
21713 SECTION_DEBUG, NULL);
21714 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
21715 SECTION_DEBUG, NULL);
21717 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
21718 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
21719 DEBUG_ABBREV_SECTION_LABEL, 0);
21720 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
21721 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
21722 COLD_TEXT_SECTION_LABEL, 0);
21723 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
21725 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
21726 DEBUG_INFO_SECTION_LABEL, 0);
21727 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
21728 DEBUG_LINE_SECTION_LABEL, 0);
21729 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
21730 DEBUG_RANGES_SECTION_LABEL, 0);
21731 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
21732 DEBUG_ADDR_SECTION_LABEL, 0);
21733 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
21734 dwarf_strict
21735 ? DEBUG_MACINFO_SECTION_LABEL
21736 : DEBUG_MACRO_SECTION_LABEL, 0);
21737 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
21739 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21740 vec_alloc (macinfo_table, 64);
21742 switch_to_section (text_section);
21743 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
21745 /* Make sure the line number table for .text always exists. */
21746 text_section_line_info = new_line_info_table ();
21747 text_section_line_info->end_label = text_end_label;
21750 /* Called before compile () starts outputtting functions, variables
21751 and toplevel asms into assembly. */
21753 static void
21754 dwarf2out_assembly_start (void)
21756 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
21757 && dwarf2out_do_cfi_asm ()
21758 && (!(flag_unwind_tables || flag_exceptions)
21759 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
21760 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
21763 /* A helper function for dwarf2out_finish called through
21764 htab_traverse. Assign a string its index. All strings must be
21765 collected into the table by the time index_string is called,
21766 because the indexing code relies on htab_traverse to traverse nodes
21767 in the same order for each run. */
21769 static int
21770 index_string (void **h, void *v)
21772 struct indirect_string_node *node = (struct indirect_string_node *) *h;
21773 unsigned int *index = (unsigned int *) v;
21775 find_string_form (node);
21776 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
21778 gcc_assert(node->index == NO_INDEX_ASSIGNED);
21779 node->index = *index;
21780 *index += 1;
21782 return 1;
21785 /* A helper function for output_indirect_strings called through
21786 htab_traverse. Output the offset to a string and update the
21787 current offset. */
21789 static int
21790 output_index_string_offset (void **h, void *v)
21792 struct indirect_string_node *node = (struct indirect_string_node *) *h;
21793 unsigned int *offset = (unsigned int *) v;
21795 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
21797 /* Assert that this node has been assigned an index. */
21798 gcc_assert (node->index != NO_INDEX_ASSIGNED
21799 && node->index != NOT_INDEXED);
21800 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
21801 "indexed string 0x%x: %s", node->index, node->str);
21802 *offset += strlen (node->str) + 1;
21804 return 1;
21807 /* A helper function for dwarf2out_finish called through
21808 htab_traverse. Output the indexed string. */
21810 static int
21811 output_index_string (void **h, void *v)
21813 struct indirect_string_node *node = (struct indirect_string_node *) *h;
21814 unsigned int *cur_idx = (unsigned int *) v;
21816 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
21818 /* Assert that the strings are output in the same order as their
21819 indexes were assigned. */
21820 gcc_assert (*cur_idx == node->index);
21821 ASM_OUTPUT_LABEL (asm_out_file, node->label);
21822 assemble_string (node->str, strlen (node->str) + 1);
21823 *cur_idx += 1;
21825 return 1;
21828 /* A helper function for dwarf2out_finish called through
21829 htab_traverse. Emit one queued .debug_str string. */
21831 static int
21832 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
21834 struct indirect_string_node *node = (struct indirect_string_node *) *h;
21836 if (node->form == DW_FORM_strp && node->refcount > 0)
21838 ASM_OUTPUT_LABEL (asm_out_file, node->label);
21839 assemble_string (node->str, strlen (node->str) + 1);
21842 return 1;
21845 /* Output the indexed string table. */
21847 static void
21848 output_indirect_strings (void)
21850 if (!dwarf_split_debug_info)
21852 switch_to_section (debug_str_section);
21853 htab_traverse (debug_str_hash, output_indirect_string, NULL);
21855 else
21857 unsigned int offset = 0;
21858 unsigned int cur_idx = 0;
21860 switch_to_section (debug_str_offsets_section);
21861 htab_traverse_noresize (debug_str_hash,
21862 output_index_string_offset,
21863 &offset);
21864 switch_to_section (debug_str_section);
21865 htab_traverse_noresize (debug_str_hash,
21866 output_index_string,
21867 &cur_idx);
21871 /* Callback for htab_traverse to assign an index to an entry in the
21872 table, and to write that entry to the .debug_addr section. */
21874 static int
21875 output_addr_table_entry (void **slot, void *data)
21877 addr_table_entry *entry = (addr_table_entry *) *slot;
21878 unsigned int *cur_index = (unsigned int *)data;
21880 if (entry->refcount == 0)
21882 gcc_assert (entry->index == NO_INDEX_ASSIGNED
21883 || entry->index == NOT_INDEXED);
21884 return 1;
21887 gcc_assert (entry->index == *cur_index);
21888 (*cur_index)++;
21890 switch (entry->kind)
21892 case ate_kind_rtx:
21893 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
21894 "0x%x", entry->index);
21895 break;
21896 case ate_kind_rtx_dtprel:
21897 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
21898 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
21899 DWARF2_ADDR_SIZE,
21900 entry->addr.rtl);
21901 fputc ('\n', asm_out_file);
21902 break;
21903 case ate_kind_label:
21904 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
21905 "0x%x", entry->index);
21906 break;
21907 default:
21908 gcc_unreachable ();
21910 return 1;
21913 /* Produce the .debug_addr section. */
21915 static void
21916 output_addr_table (void)
21918 unsigned int index = 0;
21919 if (addr_index_table == NULL || htab_size (addr_index_table) == 0)
21920 return;
21922 switch_to_section (debug_addr_section);
21923 htab_traverse_noresize (addr_index_table, output_addr_table_entry, &index);
21926 #if ENABLE_ASSERT_CHECKING
21927 /* Verify that all marks are clear. */
21929 static void
21930 verify_marks_clear (dw_die_ref die)
21932 dw_die_ref c;
21934 gcc_assert (! die->die_mark);
21935 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
21937 #endif /* ENABLE_ASSERT_CHECKING */
21939 /* Clear the marks for a die and its children.
21940 Be cool if the mark isn't set. */
21942 static void
21943 prune_unmark_dies (dw_die_ref die)
21945 dw_die_ref c;
21947 if (die->die_mark)
21948 die->die_mark = 0;
21949 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
21952 /* Given DIE that we're marking as used, find any other dies
21953 it references as attributes and mark them as used. */
21955 static void
21956 prune_unused_types_walk_attribs (dw_die_ref die)
21958 dw_attr_ref a;
21959 unsigned ix;
21961 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
21963 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
21965 /* A reference to another DIE.
21966 Make sure that it will get emitted.
21967 If it was broken out into a comdat group, don't follow it. */
21968 if (! AT_ref (a)->comdat_type_p
21969 || a->dw_attr == DW_AT_specification)
21970 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
21972 /* Set the string's refcount to 0 so that prune_unused_types_mark
21973 accounts properly for it. */
21974 if (AT_class (a) == dw_val_class_str)
21975 a->dw_attr_val.v.val_str->refcount = 0;
21979 /* Mark the generic parameters and arguments children DIEs of DIE. */
21981 static void
21982 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
21984 dw_die_ref c;
21986 if (die == NULL || die->die_child == NULL)
21987 return;
21988 c = die->die_child;
21991 switch (c->die_tag)
21993 case DW_TAG_template_type_param:
21994 case DW_TAG_template_value_param:
21995 case DW_TAG_GNU_template_template_param:
21996 case DW_TAG_GNU_template_parameter_pack:
21997 prune_unused_types_mark (c, 1);
21998 break;
21999 default:
22000 break;
22002 c = c->die_sib;
22003 } while (c && c != die->die_child);
22006 /* Mark DIE as being used. If DOKIDS is true, then walk down
22007 to DIE's children. */
22009 static void
22010 prune_unused_types_mark (dw_die_ref die, int dokids)
22012 dw_die_ref c;
22014 if (die->die_mark == 0)
22016 /* We haven't done this node yet. Mark it as used. */
22017 die->die_mark = 1;
22018 /* If this is the DIE of a generic type instantiation,
22019 mark the children DIEs that describe its generic parms and
22020 args. */
22021 prune_unused_types_mark_generic_parms_dies (die);
22023 /* We also have to mark its parents as used.
22024 (But we don't want to mark our parent's kids due to this,
22025 unless it is a class.) */
22026 if (die->die_parent)
22027 prune_unused_types_mark (die->die_parent,
22028 class_scope_p (die->die_parent));
22030 /* Mark any referenced nodes. */
22031 prune_unused_types_walk_attribs (die);
22033 /* If this node is a specification,
22034 also mark the definition, if it exists. */
22035 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
22036 prune_unused_types_mark (die->die_definition, 1);
22039 if (dokids && die->die_mark != 2)
22041 /* We need to walk the children, but haven't done so yet.
22042 Remember that we've walked the kids. */
22043 die->die_mark = 2;
22045 /* If this is an array type, we need to make sure our
22046 kids get marked, even if they're types. If we're
22047 breaking out types into comdat sections, do this
22048 for all type definitions. */
22049 if (die->die_tag == DW_TAG_array_type
22050 || (use_debug_types
22051 && is_type_die (die) && ! is_declaration_die (die)))
22052 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
22053 else
22054 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22058 /* For local classes, look if any static member functions were emitted
22059 and if so, mark them. */
22061 static void
22062 prune_unused_types_walk_local_classes (dw_die_ref die)
22064 dw_die_ref c;
22066 if (die->die_mark == 2)
22067 return;
22069 switch (die->die_tag)
22071 case DW_TAG_structure_type:
22072 case DW_TAG_union_type:
22073 case DW_TAG_class_type:
22074 break;
22076 case DW_TAG_subprogram:
22077 if (!get_AT_flag (die, DW_AT_declaration)
22078 || die->die_definition != NULL)
22079 prune_unused_types_mark (die, 1);
22080 return;
22082 default:
22083 return;
22086 /* Mark children. */
22087 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
22090 /* Walk the tree DIE and mark types that we actually use. */
22092 static void
22093 prune_unused_types_walk (dw_die_ref die)
22095 dw_die_ref c;
22097 /* Don't do anything if this node is already marked and
22098 children have been marked as well. */
22099 if (die->die_mark == 2)
22100 return;
22102 switch (die->die_tag)
22104 case DW_TAG_structure_type:
22105 case DW_TAG_union_type:
22106 case DW_TAG_class_type:
22107 if (die->die_perennial_p)
22108 break;
22110 for (c = die->die_parent; c; c = c->die_parent)
22111 if (c->die_tag == DW_TAG_subprogram)
22112 break;
22114 /* Finding used static member functions inside of classes
22115 is needed just for local classes, because for other classes
22116 static member function DIEs with DW_AT_specification
22117 are emitted outside of the DW_TAG_*_type. If we ever change
22118 it, we'd need to call this even for non-local classes. */
22119 if (c)
22120 prune_unused_types_walk_local_classes (die);
22122 /* It's a type node --- don't mark it. */
22123 return;
22125 case DW_TAG_const_type:
22126 case DW_TAG_packed_type:
22127 case DW_TAG_pointer_type:
22128 case DW_TAG_reference_type:
22129 case DW_TAG_rvalue_reference_type:
22130 case DW_TAG_volatile_type:
22131 case DW_TAG_typedef:
22132 case DW_TAG_array_type:
22133 case DW_TAG_interface_type:
22134 case DW_TAG_friend:
22135 case DW_TAG_variant_part:
22136 case DW_TAG_enumeration_type:
22137 case DW_TAG_subroutine_type:
22138 case DW_TAG_string_type:
22139 case DW_TAG_set_type:
22140 case DW_TAG_subrange_type:
22141 case DW_TAG_ptr_to_member_type:
22142 case DW_TAG_file_type:
22143 if (die->die_perennial_p)
22144 break;
22146 /* It's a type node --- don't mark it. */
22147 return;
22149 default:
22150 /* Mark everything else. */
22151 break;
22154 if (die->die_mark == 0)
22156 die->die_mark = 1;
22158 /* Now, mark any dies referenced from here. */
22159 prune_unused_types_walk_attribs (die);
22162 die->die_mark = 2;
22164 /* Mark children. */
22165 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22168 /* Increment the string counts on strings referred to from DIE's
22169 attributes. */
22171 static void
22172 prune_unused_types_update_strings (dw_die_ref die)
22174 dw_attr_ref a;
22175 unsigned ix;
22177 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22178 if (AT_class (a) == dw_val_class_str)
22180 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22181 s->refcount++;
22182 /* Avoid unnecessarily putting strings that are used less than
22183 twice in the hash table. */
22184 if (s->refcount
22185 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22187 void ** slot;
22188 slot = htab_find_slot_with_hash (debug_str_hash, s->str,
22189 htab_hash_string (s->str),
22190 INSERT);
22191 gcc_assert (*slot == NULL);
22192 *slot = s;
22197 /* Remove from the tree DIE any dies that aren't marked. */
22199 static void
22200 prune_unused_types_prune (dw_die_ref die)
22202 dw_die_ref c;
22204 gcc_assert (die->die_mark);
22205 prune_unused_types_update_strings (die);
22207 if (! die->die_child)
22208 return;
22210 c = die->die_child;
22211 do {
22212 dw_die_ref prev = c;
22213 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
22214 if (c == die->die_child)
22216 /* No marked children between 'prev' and the end of the list. */
22217 if (prev == c)
22218 /* No marked children at all. */
22219 die->die_child = NULL;
22220 else
22222 prev->die_sib = c->die_sib;
22223 die->die_child = prev;
22225 return;
22228 if (c != prev->die_sib)
22229 prev->die_sib = c;
22230 prune_unused_types_prune (c);
22231 } while (c != die->die_child);
22234 /* Remove dies representing declarations that we never use. */
22236 static void
22237 prune_unused_types (void)
22239 unsigned int i;
22240 limbo_die_node *node;
22241 comdat_type_node *ctnode;
22242 pubname_ref pub;
22243 dw_die_ref base_type;
22245 #if ENABLE_ASSERT_CHECKING
22246 /* All the marks should already be clear. */
22247 verify_marks_clear (comp_unit_die ());
22248 for (node = limbo_die_list; node; node = node->next)
22249 verify_marks_clear (node->die);
22250 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22251 verify_marks_clear (ctnode->root_die);
22252 #endif /* ENABLE_ASSERT_CHECKING */
22254 /* Mark types that are used in global variables. */
22255 premark_types_used_by_global_vars ();
22257 /* Set the mark on nodes that are actually used. */
22258 prune_unused_types_walk (comp_unit_die ());
22259 for (node = limbo_die_list; node; node = node->next)
22260 prune_unused_types_walk (node->die);
22261 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22263 prune_unused_types_walk (ctnode->root_die);
22264 prune_unused_types_mark (ctnode->type_die, 1);
22267 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
22268 are unusual in that they are pubnames that are the children of pubtypes.
22269 They should only be marked via their parent DW_TAG_enumeration_type die,
22270 not as roots in themselves. */
22271 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
22272 if (pub->die->die_tag != DW_TAG_enumerator)
22273 prune_unused_types_mark (pub->die, 1);
22274 for (i = 0; base_types.iterate (i, &base_type); i++)
22275 prune_unused_types_mark (base_type, 1);
22277 if (debug_str_hash)
22278 htab_empty (debug_str_hash);
22279 prune_unused_types_prune (comp_unit_die ());
22280 for (node = limbo_die_list; node; node = node->next)
22281 prune_unused_types_prune (node->die);
22282 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22283 prune_unused_types_prune (ctnode->root_die);
22285 /* Leave the marks clear. */
22286 prune_unmark_dies (comp_unit_die ());
22287 for (node = limbo_die_list; node; node = node->next)
22288 prune_unmark_dies (node->die);
22289 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22290 prune_unmark_dies (ctnode->root_die);
22293 /* Set the parameter to true if there are any relative pathnames in
22294 the file table. */
22295 static int
22296 file_table_relative_p (void ** slot, void *param)
22298 bool *p = (bool *) param;
22299 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
22300 if (!IS_ABSOLUTE_PATH (d->filename))
22302 *p = true;
22303 return 0;
22305 return 1;
22308 /* Routines to manipulate hash table of comdat type units. */
22310 static hashval_t
22311 htab_ct_hash (const void *of)
22313 hashval_t h;
22314 const comdat_type_node *const type_node = (const comdat_type_node *) of;
22316 memcpy (&h, type_node->signature, sizeof (h));
22317 return h;
22320 static int
22321 htab_ct_eq (const void *of1, const void *of2)
22323 const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
22324 const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
22326 return (! memcmp (type_node_1->signature, type_node_2->signature,
22327 DWARF_TYPE_SIGNATURE_SIZE));
22330 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
22331 to the location it would have been added, should we know its
22332 DECL_ASSEMBLER_NAME when we added other attributes. This will
22333 probably improve compactness of debug info, removing equivalent
22334 abbrevs, and hide any differences caused by deferring the
22335 computation of the assembler name, triggered by e.g. PCH. */
22337 static inline void
22338 move_linkage_attr (dw_die_ref die)
22340 unsigned ix = vec_safe_length (die->die_attr);
22341 dw_attr_node linkage = (*die->die_attr)[ix - 1];
22343 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
22344 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
22346 while (--ix > 0)
22348 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
22350 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
22351 break;
22354 if (ix != vec_safe_length (die->die_attr) - 1)
22356 die->die_attr->pop ();
22357 die->die_attr->quick_insert (ix, linkage);
22361 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
22362 referenced from typed stack ops and count how often they are used. */
22364 static void
22365 mark_base_types (dw_loc_descr_ref loc)
22367 dw_die_ref base_type = NULL;
22369 for (; loc; loc = loc->dw_loc_next)
22371 switch (loc->dw_loc_opc)
22373 case DW_OP_GNU_regval_type:
22374 case DW_OP_GNU_deref_type:
22375 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
22376 break;
22377 case DW_OP_GNU_convert:
22378 case DW_OP_GNU_reinterpret:
22379 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
22380 continue;
22381 /* FALLTHRU */
22382 case DW_OP_GNU_const_type:
22383 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
22384 break;
22385 case DW_OP_GNU_entry_value:
22386 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
22387 continue;
22388 default:
22389 continue;
22391 gcc_assert (base_type->die_parent == comp_unit_die ());
22392 if (base_type->die_mark)
22393 base_type->die_mark++;
22394 else
22396 base_types.safe_push (base_type);
22397 base_type->die_mark = 1;
22402 /* Comparison function for sorting marked base types. */
22404 static int
22405 base_type_cmp (const void *x, const void *y)
22407 dw_die_ref dx = *(const dw_die_ref *) x;
22408 dw_die_ref dy = *(const dw_die_ref *) y;
22409 unsigned int byte_size1, byte_size2;
22410 unsigned int encoding1, encoding2;
22411 if (dx->die_mark > dy->die_mark)
22412 return -1;
22413 if (dx->die_mark < dy->die_mark)
22414 return 1;
22415 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
22416 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
22417 if (byte_size1 < byte_size2)
22418 return 1;
22419 if (byte_size1 > byte_size2)
22420 return -1;
22421 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
22422 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
22423 if (encoding1 < encoding2)
22424 return 1;
22425 if (encoding1 > encoding2)
22426 return -1;
22427 return 0;
22430 /* Move base types marked by mark_base_types as early as possible
22431 in the CU, sorted by decreasing usage count both to make the
22432 uleb128 references as small as possible and to make sure they
22433 will have die_offset already computed by calc_die_sizes when
22434 sizes of typed stack loc ops is computed. */
22436 static void
22437 move_marked_base_types (void)
22439 unsigned int i;
22440 dw_die_ref base_type, die, c;
22442 if (base_types.is_empty ())
22443 return;
22445 /* Sort by decreasing usage count, they will be added again in that
22446 order later on. */
22447 base_types.qsort (base_type_cmp);
22448 die = comp_unit_die ();
22449 c = die->die_child;
22452 dw_die_ref prev = c;
22453 c = c->die_sib;
22454 while (c->die_mark)
22456 remove_child_with_prev (c, prev);
22457 /* As base types got marked, there must be at least
22458 one node other than DW_TAG_base_type. */
22459 gcc_assert (c != c->die_sib);
22460 c = c->die_sib;
22463 while (c != die->die_child);
22464 gcc_assert (die->die_child);
22465 c = die->die_child;
22466 for (i = 0; base_types.iterate (i, &base_type); i++)
22468 base_type->die_mark = 0;
22469 base_type->die_sib = c->die_sib;
22470 c->die_sib = base_type;
22471 c = base_type;
22475 /* Helper function for resolve_addr, attempt to resolve
22476 one CONST_STRING, return non-zero if not successful. Similarly verify that
22477 SYMBOL_REFs refer to variables emitted in the current CU. */
22479 static int
22480 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
22482 rtx rtl = *addr;
22484 if (GET_CODE (rtl) == CONST_STRING)
22486 size_t len = strlen (XSTR (rtl, 0)) + 1;
22487 tree t = build_string (len, XSTR (rtl, 0));
22488 tree tlen = size_int (len - 1);
22489 TREE_TYPE (t)
22490 = build_array_type (char_type_node, build_index_type (tlen));
22491 rtl = lookup_constant_def (t);
22492 if (!rtl || !MEM_P (rtl))
22493 return 1;
22494 rtl = XEXP (rtl, 0);
22495 vec_safe_push (used_rtx_array, rtl);
22496 *addr = rtl;
22497 return 0;
22500 if (GET_CODE (rtl) == SYMBOL_REF
22501 && SYMBOL_REF_DECL (rtl))
22503 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
22505 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
22506 return 1;
22508 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22509 return 1;
22512 if (GET_CODE (rtl) == CONST
22513 && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
22514 return 1;
22516 return 0;
22519 /* Helper function for resolve_addr, handle one location
22520 expression, return false if at least one CONST_STRING or SYMBOL_REF in
22521 the location list couldn't be resolved. */
22523 static bool
22524 resolve_addr_in_expr (dw_loc_descr_ref loc)
22526 dw_loc_descr_ref keep = NULL;
22527 for (; loc; loc = loc->dw_loc_next)
22528 switch (loc->dw_loc_opc)
22530 case DW_OP_addr:
22531 if (resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
22532 return false;
22533 break;
22534 case DW_OP_GNU_addr_index:
22535 case DW_OP_GNU_const_index:
22537 if ((loc->dw_loc_opc == DW_OP_GNU_addr_index
22538 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
22539 && resolve_one_addr (&loc->dw_loc_oprnd1.val_entry->addr.rtl,
22540 NULL))
22541 return false;
22543 break;
22544 case DW_OP_const4u:
22545 case DW_OP_const8u:
22546 if (loc->dtprel
22547 && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
22548 return false;
22549 break;
22550 case DW_OP_plus_uconst:
22551 if (size_of_loc_descr (loc)
22552 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
22554 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
22556 dw_loc_descr_ref repl
22557 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
22558 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
22559 add_loc_descr (&repl, loc->dw_loc_next);
22560 *loc = *repl;
22562 break;
22563 case DW_OP_implicit_value:
22564 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
22565 && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL))
22566 return false;
22567 break;
22568 case DW_OP_GNU_implicit_pointer:
22569 case DW_OP_GNU_parameter_ref:
22570 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
22572 dw_die_ref ref
22573 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
22574 if (ref == NULL)
22575 return false;
22576 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
22577 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
22578 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
22580 break;
22581 case DW_OP_GNU_const_type:
22582 case DW_OP_GNU_regval_type:
22583 case DW_OP_GNU_deref_type:
22584 case DW_OP_GNU_convert:
22585 case DW_OP_GNU_reinterpret:
22586 while (loc->dw_loc_next
22587 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
22589 dw_die_ref base1, base2;
22590 unsigned enc1, enc2, size1, size2;
22591 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
22592 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
22593 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
22594 else if (loc->dw_loc_oprnd1.val_class
22595 == dw_val_class_unsigned_const)
22596 break;
22597 else
22598 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
22599 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
22600 == dw_val_class_unsigned_const)
22601 break;
22602 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
22603 gcc_assert (base1->die_tag == DW_TAG_base_type
22604 && base2->die_tag == DW_TAG_base_type);
22605 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
22606 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
22607 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
22608 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
22609 if (size1 == size2
22610 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
22611 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
22612 && loc != keep)
22613 || enc1 == enc2))
22615 /* Optimize away next DW_OP_GNU_convert after
22616 adjusting LOC's base type die reference. */
22617 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
22618 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
22619 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
22620 else
22621 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
22622 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
22623 continue;
22625 /* Don't change integer DW_OP_GNU_convert after e.g. floating
22626 point typed stack entry. */
22627 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
22628 keep = loc->dw_loc_next;
22629 break;
22631 break;
22632 default:
22633 break;
22635 return true;
22638 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
22639 an address in .rodata section if the string literal is emitted there,
22640 or remove the containing location list or replace DW_AT_const_value
22641 with DW_AT_location and empty location expression, if it isn't found
22642 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
22643 to something that has been emitted in the current CU. */
22645 static void
22646 resolve_addr (dw_die_ref die)
22648 dw_die_ref c;
22649 dw_attr_ref a;
22650 dw_loc_list_ref *curr, *start, loc;
22651 unsigned ix;
22653 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22654 switch (AT_class (a))
22656 case dw_val_class_loc_list:
22657 start = curr = AT_loc_list_ptr (a);
22658 loc = *curr;
22659 gcc_assert (loc);
22660 /* The same list can be referenced more than once. See if we have
22661 already recorded the result from a previous pass. */
22662 if (loc->replaced)
22663 *curr = loc->dw_loc_next;
22664 else if (!loc->resolved_addr)
22666 /* As things stand, we do not expect or allow one die to
22667 reference a suffix of another die's location list chain.
22668 References must be identical or completely separate.
22669 There is therefore no need to cache the result of this
22670 pass on any list other than the first; doing so
22671 would lead to unnecessary writes. */
22672 while (*curr)
22674 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
22675 if (!resolve_addr_in_expr ((*curr)->expr))
22677 dw_loc_list_ref next = (*curr)->dw_loc_next;
22678 dw_loc_descr_ref l = (*curr)->expr;
22680 if (next && (*curr)->ll_symbol)
22682 gcc_assert (!next->ll_symbol);
22683 next->ll_symbol = (*curr)->ll_symbol;
22685 if (dwarf_split_debug_info)
22686 remove_loc_list_addr_table_entries (l);
22687 *curr = next;
22689 else
22691 mark_base_types ((*curr)->expr);
22692 curr = &(*curr)->dw_loc_next;
22695 if (loc == *start)
22696 loc->resolved_addr = 1;
22697 else
22699 loc->replaced = 1;
22700 loc->dw_loc_next = *start;
22703 if (!*start)
22705 remove_AT (die, a->dw_attr);
22706 ix--;
22708 break;
22709 case dw_val_class_loc:
22711 dw_loc_descr_ref l = AT_loc (a);
22712 /* For -gdwarf-2 don't attempt to optimize
22713 DW_AT_data_member_location containing
22714 DW_OP_plus_uconst - older consumers might
22715 rely on it being that op instead of a more complex,
22716 but shorter, location description. */
22717 if ((dwarf_version > 2
22718 || a->dw_attr != DW_AT_data_member_location
22719 || l == NULL
22720 || l->dw_loc_opc != DW_OP_plus_uconst
22721 || l->dw_loc_next != NULL)
22722 && !resolve_addr_in_expr (l))
22724 if (dwarf_split_debug_info)
22725 remove_loc_list_addr_table_entries (l);
22726 remove_AT (die, a->dw_attr);
22727 ix--;
22729 else
22730 mark_base_types (l);
22732 break;
22733 case dw_val_class_addr:
22734 if (a->dw_attr == DW_AT_const_value
22735 && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
22737 if (AT_index (a) != NOT_INDEXED)
22738 remove_addr_table_entry (a->dw_attr_val.val_entry);
22739 remove_AT (die, a->dw_attr);
22740 ix--;
22742 if (die->die_tag == DW_TAG_GNU_call_site
22743 && a->dw_attr == DW_AT_abstract_origin)
22745 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
22746 dw_die_ref tdie = lookup_decl_die (tdecl);
22747 if (tdie == NULL
22748 && DECL_EXTERNAL (tdecl)
22749 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
22751 force_decl_die (tdecl);
22752 tdie = lookup_decl_die (tdecl);
22754 if (tdie)
22756 a->dw_attr_val.val_class = dw_val_class_die_ref;
22757 a->dw_attr_val.v.val_die_ref.die = tdie;
22758 a->dw_attr_val.v.val_die_ref.external = 0;
22760 else
22762 if (AT_index (a) != NOT_INDEXED)
22763 remove_addr_table_entry (a->dw_attr_val.val_entry);
22764 remove_AT (die, a->dw_attr);
22765 ix--;
22768 break;
22769 default:
22770 break;
22773 FOR_EACH_CHILD (die, c, resolve_addr (c));
22776 /* Helper routines for optimize_location_lists.
22777 This pass tries to share identical local lists in .debug_loc
22778 section. */
22780 /* Iteratively hash operands of LOC opcode. */
22782 static inline hashval_t
22783 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
22785 dw_val_ref val1 = &loc->dw_loc_oprnd1;
22786 dw_val_ref val2 = &loc->dw_loc_oprnd2;
22788 switch (loc->dw_loc_opc)
22790 case DW_OP_const4u:
22791 case DW_OP_const8u:
22792 if (loc->dtprel)
22793 goto hash_addr;
22794 /* FALLTHRU */
22795 case DW_OP_const1u:
22796 case DW_OP_const1s:
22797 case DW_OP_const2u:
22798 case DW_OP_const2s:
22799 case DW_OP_const4s:
22800 case DW_OP_const8s:
22801 case DW_OP_constu:
22802 case DW_OP_consts:
22803 case DW_OP_pick:
22804 case DW_OP_plus_uconst:
22805 case DW_OP_breg0:
22806 case DW_OP_breg1:
22807 case DW_OP_breg2:
22808 case DW_OP_breg3:
22809 case DW_OP_breg4:
22810 case DW_OP_breg5:
22811 case DW_OP_breg6:
22812 case DW_OP_breg7:
22813 case DW_OP_breg8:
22814 case DW_OP_breg9:
22815 case DW_OP_breg10:
22816 case DW_OP_breg11:
22817 case DW_OP_breg12:
22818 case DW_OP_breg13:
22819 case DW_OP_breg14:
22820 case DW_OP_breg15:
22821 case DW_OP_breg16:
22822 case DW_OP_breg17:
22823 case DW_OP_breg18:
22824 case DW_OP_breg19:
22825 case DW_OP_breg20:
22826 case DW_OP_breg21:
22827 case DW_OP_breg22:
22828 case DW_OP_breg23:
22829 case DW_OP_breg24:
22830 case DW_OP_breg25:
22831 case DW_OP_breg26:
22832 case DW_OP_breg27:
22833 case DW_OP_breg28:
22834 case DW_OP_breg29:
22835 case DW_OP_breg30:
22836 case DW_OP_breg31:
22837 case DW_OP_regx:
22838 case DW_OP_fbreg:
22839 case DW_OP_piece:
22840 case DW_OP_deref_size:
22841 case DW_OP_xderef_size:
22842 hash = iterative_hash_object (val1->v.val_int, hash);
22843 break;
22844 case DW_OP_skip:
22845 case DW_OP_bra:
22847 int offset;
22849 gcc_assert (val1->val_class == dw_val_class_loc);
22850 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
22851 hash = iterative_hash_object (offset, hash);
22853 break;
22854 case DW_OP_implicit_value:
22855 hash = iterative_hash_object (val1->v.val_unsigned, hash);
22856 switch (val2->val_class)
22858 case dw_val_class_const:
22859 hash = iterative_hash_object (val2->v.val_int, hash);
22860 break;
22861 case dw_val_class_vec:
22863 unsigned int elt_size = val2->v.val_vec.elt_size;
22864 unsigned int len = val2->v.val_vec.length;
22866 hash = iterative_hash_object (elt_size, hash);
22867 hash = iterative_hash_object (len, hash);
22868 hash = iterative_hash (val2->v.val_vec.array,
22869 len * elt_size, hash);
22871 break;
22872 case dw_val_class_const_double:
22873 hash = iterative_hash_object (val2->v.val_double.low, hash);
22874 hash = iterative_hash_object (val2->v.val_double.high, hash);
22875 break;
22876 case dw_val_class_addr:
22877 hash = iterative_hash_rtx (val2->v.val_addr, hash);
22878 break;
22879 default:
22880 gcc_unreachable ();
22882 break;
22883 case DW_OP_bregx:
22884 case DW_OP_bit_piece:
22885 hash = iterative_hash_object (val1->v.val_int, hash);
22886 hash = iterative_hash_object (val2->v.val_int, hash);
22887 break;
22888 case DW_OP_addr:
22889 hash_addr:
22890 if (loc->dtprel)
22892 unsigned char dtprel = 0xd1;
22893 hash = iterative_hash_object (dtprel, hash);
22895 hash = iterative_hash_rtx (val1->v.val_addr, hash);
22896 break;
22897 case DW_OP_GNU_addr_index:
22898 case DW_OP_GNU_const_index:
22900 if (loc->dtprel)
22902 unsigned char dtprel = 0xd1;
22903 hash = iterative_hash_object (dtprel, hash);
22905 hash = iterative_hash_rtx (val1->val_entry->addr.rtl, hash);
22907 break;
22908 case DW_OP_GNU_implicit_pointer:
22909 hash = iterative_hash_object (val2->v.val_int, hash);
22910 break;
22911 case DW_OP_GNU_entry_value:
22912 hash = hash_loc_operands (val1->v.val_loc, hash);
22913 break;
22914 case DW_OP_GNU_regval_type:
22915 case DW_OP_GNU_deref_type:
22917 unsigned int byte_size
22918 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
22919 unsigned int encoding
22920 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
22921 hash = iterative_hash_object (val1->v.val_int, hash);
22922 hash = iterative_hash_object (byte_size, hash);
22923 hash = iterative_hash_object (encoding, hash);
22925 break;
22926 case DW_OP_GNU_convert:
22927 case DW_OP_GNU_reinterpret:
22928 if (val1->val_class == dw_val_class_unsigned_const)
22930 hash = iterative_hash_object (val1->v.val_unsigned, hash);
22931 break;
22933 /* FALLTHRU */
22934 case DW_OP_GNU_const_type:
22936 unsigned int byte_size
22937 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
22938 unsigned int encoding
22939 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
22940 hash = iterative_hash_object (byte_size, hash);
22941 hash = iterative_hash_object (encoding, hash);
22942 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
22943 break;
22944 hash = iterative_hash_object (val2->val_class, hash);
22945 switch (val2->val_class)
22947 case dw_val_class_const:
22948 hash = iterative_hash_object (val2->v.val_int, hash);
22949 break;
22950 case dw_val_class_vec:
22952 unsigned int elt_size = val2->v.val_vec.elt_size;
22953 unsigned int len = val2->v.val_vec.length;
22955 hash = iterative_hash_object (elt_size, hash);
22956 hash = iterative_hash_object (len, hash);
22957 hash = iterative_hash (val2->v.val_vec.array,
22958 len * elt_size, hash);
22960 break;
22961 case dw_val_class_const_double:
22962 hash = iterative_hash_object (val2->v.val_double.low, hash);
22963 hash = iterative_hash_object (val2->v.val_double.high, hash);
22964 break;
22965 default:
22966 gcc_unreachable ();
22969 break;
22971 default:
22972 /* Other codes have no operands. */
22973 break;
22975 return hash;
22978 /* Iteratively hash the whole DWARF location expression LOC. */
22980 static inline hashval_t
22981 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
22983 dw_loc_descr_ref l;
22984 bool sizes_computed = false;
22985 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
22986 size_of_locs (loc);
22988 for (l = loc; l != NULL; l = l->dw_loc_next)
22990 enum dwarf_location_atom opc = l->dw_loc_opc;
22991 hash = iterative_hash_object (opc, hash);
22992 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
22994 size_of_locs (loc);
22995 sizes_computed = true;
22997 hash = hash_loc_operands (l, hash);
22999 return hash;
23002 /* Compute hash of the whole location list LIST_HEAD. */
23004 static inline void
23005 hash_loc_list (dw_loc_list_ref list_head)
23007 dw_loc_list_ref curr = list_head;
23008 hashval_t hash = 0;
23010 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
23012 hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
23013 hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
23014 if (curr->section)
23015 hash = iterative_hash (curr->section, strlen (curr->section) + 1,
23016 hash);
23017 hash = hash_locs (curr->expr, hash);
23019 list_head->hash = hash;
23022 /* Return true if X and Y opcodes have the same operands. */
23024 static inline bool
23025 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
23027 dw_val_ref valx1 = &x->dw_loc_oprnd1;
23028 dw_val_ref valx2 = &x->dw_loc_oprnd2;
23029 dw_val_ref valy1 = &y->dw_loc_oprnd1;
23030 dw_val_ref valy2 = &y->dw_loc_oprnd2;
23032 switch (x->dw_loc_opc)
23034 case DW_OP_const4u:
23035 case DW_OP_const8u:
23036 if (x->dtprel)
23037 goto hash_addr;
23038 /* FALLTHRU */
23039 case DW_OP_const1u:
23040 case DW_OP_const1s:
23041 case DW_OP_const2u:
23042 case DW_OP_const2s:
23043 case DW_OP_const4s:
23044 case DW_OP_const8s:
23045 case DW_OP_constu:
23046 case DW_OP_consts:
23047 case DW_OP_pick:
23048 case DW_OP_plus_uconst:
23049 case DW_OP_breg0:
23050 case DW_OP_breg1:
23051 case DW_OP_breg2:
23052 case DW_OP_breg3:
23053 case DW_OP_breg4:
23054 case DW_OP_breg5:
23055 case DW_OP_breg6:
23056 case DW_OP_breg7:
23057 case DW_OP_breg8:
23058 case DW_OP_breg9:
23059 case DW_OP_breg10:
23060 case DW_OP_breg11:
23061 case DW_OP_breg12:
23062 case DW_OP_breg13:
23063 case DW_OP_breg14:
23064 case DW_OP_breg15:
23065 case DW_OP_breg16:
23066 case DW_OP_breg17:
23067 case DW_OP_breg18:
23068 case DW_OP_breg19:
23069 case DW_OP_breg20:
23070 case DW_OP_breg21:
23071 case DW_OP_breg22:
23072 case DW_OP_breg23:
23073 case DW_OP_breg24:
23074 case DW_OP_breg25:
23075 case DW_OP_breg26:
23076 case DW_OP_breg27:
23077 case DW_OP_breg28:
23078 case DW_OP_breg29:
23079 case DW_OP_breg30:
23080 case DW_OP_breg31:
23081 case DW_OP_regx:
23082 case DW_OP_fbreg:
23083 case DW_OP_piece:
23084 case DW_OP_deref_size:
23085 case DW_OP_xderef_size:
23086 return valx1->v.val_int == valy1->v.val_int;
23087 case DW_OP_skip:
23088 case DW_OP_bra:
23089 /* If splitting debug info, the use of DW_OP_GNU_addr_index
23090 can cause irrelevant differences in dw_loc_addr. */
23091 gcc_assert (valx1->val_class == dw_val_class_loc
23092 && valy1->val_class == dw_val_class_loc
23093 && (dwarf_split_debug_info
23094 || x->dw_loc_addr == y->dw_loc_addr));
23095 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
23096 case DW_OP_implicit_value:
23097 if (valx1->v.val_unsigned != valy1->v.val_unsigned
23098 || valx2->val_class != valy2->val_class)
23099 return false;
23100 switch (valx2->val_class)
23102 case dw_val_class_const:
23103 return valx2->v.val_int == valy2->v.val_int;
23104 case dw_val_class_vec:
23105 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23106 && valx2->v.val_vec.length == valy2->v.val_vec.length
23107 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23108 valx2->v.val_vec.elt_size
23109 * valx2->v.val_vec.length) == 0;
23110 case dw_val_class_const_double:
23111 return valx2->v.val_double.low == valy2->v.val_double.low
23112 && valx2->v.val_double.high == valy2->v.val_double.high;
23113 case dw_val_class_addr:
23114 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
23115 default:
23116 gcc_unreachable ();
23118 case DW_OP_bregx:
23119 case DW_OP_bit_piece:
23120 return valx1->v.val_int == valy1->v.val_int
23121 && valx2->v.val_int == valy2->v.val_int;
23122 case DW_OP_addr:
23123 hash_addr:
23124 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
23125 case DW_OP_GNU_addr_index:
23126 case DW_OP_GNU_const_index:
23128 rtx ax1 = valx1->val_entry->addr.rtl;
23129 rtx ay1 = valy1->val_entry->addr.rtl;
23130 return rtx_equal_p (ax1, ay1);
23132 case DW_OP_GNU_implicit_pointer:
23133 return valx1->val_class == dw_val_class_die_ref
23134 && valx1->val_class == valy1->val_class
23135 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
23136 && valx2->v.val_int == valy2->v.val_int;
23137 case DW_OP_GNU_entry_value:
23138 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
23139 case DW_OP_GNU_const_type:
23140 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
23141 || valx2->val_class != valy2->val_class)
23142 return false;
23143 switch (valx2->val_class)
23145 case dw_val_class_const:
23146 return valx2->v.val_int == valy2->v.val_int;
23147 case dw_val_class_vec:
23148 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23149 && valx2->v.val_vec.length == valy2->v.val_vec.length
23150 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23151 valx2->v.val_vec.elt_size
23152 * valx2->v.val_vec.length) == 0;
23153 case dw_val_class_const_double:
23154 return valx2->v.val_double.low == valy2->v.val_double.low
23155 && valx2->v.val_double.high == valy2->v.val_double.high;
23156 default:
23157 gcc_unreachable ();
23159 case DW_OP_GNU_regval_type:
23160 case DW_OP_GNU_deref_type:
23161 return valx1->v.val_int == valy1->v.val_int
23162 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
23163 case DW_OP_GNU_convert:
23164 case DW_OP_GNU_reinterpret:
23165 if (valx1->val_class != valy1->val_class)
23166 return false;
23167 if (valx1->val_class == dw_val_class_unsigned_const)
23168 return valx1->v.val_unsigned == valy1->v.val_unsigned;
23169 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
23170 case DW_OP_GNU_parameter_ref:
23171 return valx1->val_class == dw_val_class_die_ref
23172 && valx1->val_class == valy1->val_class
23173 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
23174 default:
23175 /* Other codes have no operands. */
23176 return true;
23180 /* Return true if DWARF location expressions X and Y are the same. */
23182 static inline bool
23183 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
23185 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
23186 if (x->dw_loc_opc != y->dw_loc_opc
23187 || x->dtprel != y->dtprel
23188 || !compare_loc_operands (x, y))
23189 break;
23190 return x == NULL && y == NULL;
23193 /* Return precomputed hash of location list X. */
23195 static hashval_t
23196 loc_list_hash (const void *x)
23198 return ((const struct dw_loc_list_struct *) x)->hash;
23201 /* Return 1 if location lists X and Y are the same. */
23203 static int
23204 loc_list_eq (const void *x, const void *y)
23206 const struct dw_loc_list_struct *a = (const struct dw_loc_list_struct *) x;
23207 const struct dw_loc_list_struct *b = (const struct dw_loc_list_struct *) y;
23208 if (a == b)
23209 return 1;
23210 if (a->hash != b->hash)
23211 return 0;
23212 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
23213 if (strcmp (a->begin, b->begin) != 0
23214 || strcmp (a->end, b->end) != 0
23215 || (a->section == NULL) != (b->section == NULL)
23216 || (a->section && strcmp (a->section, b->section) != 0)
23217 || !compare_locs (a->expr, b->expr))
23218 break;
23219 return a == NULL && b == NULL;
23222 /* Recursively optimize location lists referenced from DIE
23223 children and share them whenever possible. */
23225 static void
23226 optimize_location_lists_1 (dw_die_ref die, htab_t htab)
23228 dw_die_ref c;
23229 dw_attr_ref a;
23230 unsigned ix;
23231 void **slot;
23233 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23234 if (AT_class (a) == dw_val_class_loc_list)
23236 dw_loc_list_ref list = AT_loc_list (a);
23237 /* TODO: perform some optimizations here, before hashing
23238 it and storing into the hash table. */
23239 hash_loc_list (list);
23240 slot = htab_find_slot_with_hash (htab, list, list->hash,
23241 INSERT);
23242 if (*slot == NULL)
23243 *slot = (void *) list;
23244 else
23245 a->dw_attr_val.v.val_loc_list = (dw_loc_list_ref) *slot;
23248 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
23252 /* Recursively assign each location list a unique index into the debug_addr
23253 section. */
23255 static void
23256 index_location_lists (dw_die_ref die)
23258 dw_die_ref c;
23259 dw_attr_ref a;
23260 unsigned ix;
23262 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23263 if (AT_class (a) == dw_val_class_loc_list)
23265 dw_loc_list_ref list = AT_loc_list (a);
23266 dw_loc_list_ref curr;
23267 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
23269 /* Don't index an entry that has already been indexed
23270 or won't be output. */
23271 if (curr->begin_entry != NULL
23272 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
23273 continue;
23275 curr->begin_entry
23276 = add_addr_table_entry (xstrdup (curr->begin),
23277 ate_kind_label);
23281 FOR_EACH_CHILD (die, c, index_location_lists (c));
23284 /* Optimize location lists referenced from DIE
23285 children and share them whenever possible. */
23287 static void
23288 optimize_location_lists (dw_die_ref die)
23290 htab_t htab = htab_create (500, loc_list_hash, loc_list_eq, NULL);
23291 optimize_location_lists_1 (die, htab);
23292 htab_delete (htab);
23295 /* Output stuff that dwarf requires at the end of every file,
23296 and generate the DWARF-2 debugging info. */
23298 static void
23299 dwarf2out_finish (const char *filename)
23301 limbo_die_node *node, *next_node;
23302 comdat_type_node *ctnode;
23303 htab_t comdat_type_table;
23304 unsigned int i;
23305 dw_die_ref main_comp_unit_die;
23307 /* PCH might result in DW_AT_producer string being restored from the
23308 header compilation, so always fill it with empty string initially
23309 and overwrite only here. */
23310 dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
23311 producer_string = gen_producer_string ();
23312 producer->dw_attr_val.v.val_str->refcount--;
23313 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
23315 gen_scheduled_generic_parms_dies ();
23316 gen_remaining_tmpl_value_param_die_attribute ();
23318 /* Add the name for the main input file now. We delayed this from
23319 dwarf2out_init to avoid complications with PCH. */
23320 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
23321 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
23322 add_comp_dir_attribute (comp_unit_die ());
23323 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
23325 bool p = false;
23326 htab_traverse (file_table, file_table_relative_p, &p);
23327 if (p)
23328 add_comp_dir_attribute (comp_unit_die ());
23331 if (deferred_locations_list)
23332 for (i = 0; i < deferred_locations_list->length (); i++)
23334 add_location_or_const_value_attribute (
23335 (*deferred_locations_list)[i].die,
23336 (*deferred_locations_list)[i].variable,
23337 false,
23338 DW_AT_location);
23341 /* Traverse the limbo die list, and add parent/child links. The only
23342 dies without parents that should be here are concrete instances of
23343 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
23344 For concrete instances, we can get the parent die from the abstract
23345 instance. */
23346 for (node = limbo_die_list; node; node = next_node)
23348 dw_die_ref die = node->die;
23349 next_node = node->next;
23351 if (die->die_parent == NULL)
23353 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
23355 if (origin && origin->die_parent)
23356 add_child_die (origin->die_parent, die);
23357 else if (is_cu_die (die))
23359 else if (seen_error ())
23360 /* It's OK to be confused by errors in the input. */
23361 add_child_die (comp_unit_die (), die);
23362 else
23364 /* In certain situations, the lexical block containing a
23365 nested function can be optimized away, which results
23366 in the nested function die being orphaned. Likewise
23367 with the return type of that nested function. Force
23368 this to be a child of the containing function.
23370 It may happen that even the containing function got fully
23371 inlined and optimized out. In that case we are lost and
23372 assign the empty child. This should not be big issue as
23373 the function is likely unreachable too. */
23374 gcc_assert (node->created_for);
23376 if (DECL_P (node->created_for))
23377 origin = get_context_die (DECL_CONTEXT (node->created_for));
23378 else if (TYPE_P (node->created_for))
23379 origin = scope_die_for (node->created_for, comp_unit_die ());
23380 else
23381 origin = comp_unit_die ();
23383 add_child_die (origin, die);
23388 limbo_die_list = NULL;
23390 #if ENABLE_ASSERT_CHECKING
23392 dw_die_ref die = comp_unit_die (), c;
23393 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
23395 #endif
23396 resolve_addr (comp_unit_die ());
23397 move_marked_base_types ();
23399 for (node = deferred_asm_name; node; node = node->next)
23401 tree decl = node->created_for;
23402 /* When generating LTO bytecode we can not generate new assembler
23403 names at this point and all important decls got theirs via
23404 free-lang-data. */
23405 if ((!flag_generate_lto || DECL_ASSEMBLER_NAME_SET_P (decl))
23406 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
23408 add_linkage_attr (node->die, decl);
23409 move_linkage_attr (node->die);
23413 deferred_asm_name = NULL;
23415 /* Walk through the list of incomplete types again, trying once more to
23416 emit full debugging info for them. */
23417 retry_incomplete_types ();
23419 if (flag_eliminate_unused_debug_types)
23420 prune_unused_types ();
23422 /* Generate separate COMDAT sections for type DIEs. */
23423 if (use_debug_types)
23425 break_out_comdat_types (comp_unit_die ());
23427 /* Each new type_unit DIE was added to the limbo die list when created.
23428 Since these have all been added to comdat_type_list, clear the
23429 limbo die list. */
23430 limbo_die_list = NULL;
23432 /* For each new comdat type unit, copy declarations for incomplete
23433 types to make the new unit self-contained (i.e., no direct
23434 references to the main compile unit). */
23435 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23436 copy_decls_for_unworthy_types (ctnode->root_die);
23437 copy_decls_for_unworthy_types (comp_unit_die ());
23439 /* In the process of copying declarations from one unit to another,
23440 we may have left some declarations behind that are no longer
23441 referenced. Prune them. */
23442 prune_unused_types ();
23445 /* Generate separate CUs for each of the include files we've seen.
23446 They will go into limbo_die_list. */
23447 if (flag_eliminate_dwarf2_dups)
23448 break_out_includes (comp_unit_die ());
23450 /* Traverse the DIE's and add add sibling attributes to those DIE's
23451 that have children. */
23452 add_sibling_attributes (comp_unit_die ());
23453 for (node = limbo_die_list; node; node = node->next)
23454 add_sibling_attributes (node->die);
23455 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23456 add_sibling_attributes (ctnode->root_die);
23458 /* When splitting DWARF info, we put some attributes in the
23459 skeleton compile_unit DIE that remains in the .o, while
23460 most attributes go in the DWO compile_unit_die. */
23461 if (dwarf_split_debug_info)
23462 main_comp_unit_die = gen_compile_unit_die (NULL);
23463 else
23464 main_comp_unit_die = comp_unit_die ();
23466 /* Output a terminator label for the .text section. */
23467 switch_to_section (text_section);
23468 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
23469 if (cold_text_section)
23471 switch_to_section (cold_text_section);
23472 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
23475 /* We can only use the low/high_pc attributes if all of the code was
23476 in .text. */
23477 if (!have_multiple_function_sections
23478 || (dwarf_version < 3 && dwarf_strict))
23480 /* Don't add if the CU has no associated code. */
23481 if (text_section_used)
23482 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
23483 text_end_label, true);
23485 else
23487 unsigned fde_idx;
23488 dw_fde_ref fde;
23489 bool range_list_added = false;
23491 if (text_section_used)
23492 add_ranges_by_labels (main_comp_unit_die, text_section_label,
23493 text_end_label, &range_list_added, true);
23494 if (cold_text_section_used)
23495 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
23496 cold_end_label, &range_list_added, true);
23498 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
23500 if (DECL_IGNORED_P (fde->decl))
23501 continue;
23502 if (!fde->in_std_section)
23503 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
23504 fde->dw_fde_end, &range_list_added,
23505 true);
23506 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
23507 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
23508 fde->dw_fde_second_end, &range_list_added,
23509 true);
23512 if (range_list_added)
23514 /* We need to give .debug_loc and .debug_ranges an appropriate
23515 "base address". Use zero so that these addresses become
23516 absolute. Historically, we've emitted the unexpected
23517 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
23518 Emit both to give time for other tools to adapt. */
23519 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
23520 if (! dwarf_strict && dwarf_version < 4)
23521 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
23523 add_ranges (NULL);
23527 if (debug_info_level >= DINFO_LEVEL_NORMAL)
23528 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
23529 debug_line_section_label);
23531 if (have_macinfo)
23532 add_AT_macptr (comp_unit_die (),
23533 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
23534 macinfo_section_label);
23536 if (dwarf_split_debug_info && addr_index_table != NULL)
23538 /* optimize_location_lists calculates the size of the lists,
23539 so index them first, and assign indices to the entries.
23540 Although optimize_location_lists will remove entries from
23541 the table, it only does so for duplicates, and therefore
23542 only reduces ref_counts to 1. */
23543 unsigned int index = 0;
23544 index_location_lists (comp_unit_die ());
23545 htab_traverse_noresize (addr_index_table,
23546 index_addr_table_entry, &index);
23548 if (have_location_lists)
23549 optimize_location_lists (comp_unit_die ());
23551 save_macinfo_strings ();
23552 if (dwarf_split_debug_info)
23554 unsigned int index = 0;
23555 htab_traverse_noresize (debug_str_hash, index_string, &index);
23558 /* Output all of the compilation units. We put the main one last so that
23559 the offsets are available to output_pubnames. */
23560 for (node = limbo_die_list; node; node = node->next)
23561 output_comp_unit (node->die, 0);
23563 comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
23564 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23566 void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
23568 /* Don't output duplicate types. */
23569 if (*slot != HTAB_EMPTY_ENTRY)
23570 continue;
23572 /* Add a pointer to the line table for the main compilation unit
23573 so that the debugger can make sense of DW_AT_decl_file
23574 attributes. */
23575 if (debug_info_level >= DINFO_LEVEL_NORMAL)
23576 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
23577 (!dwarf_split_debug_info
23578 ? debug_line_section_label
23579 : debug_skeleton_line_section_label));
23581 output_comdat_type_unit (ctnode);
23582 *slot = ctnode;
23584 htab_delete (comdat_type_table);
23586 /* The AT_pubnames attribute needs to go in all skeleton dies, including
23587 both the main_cu and all skeleton TUs. Making this call unconditional
23588 would end up either adding a second copy of the AT_pubnames attribute, or
23589 requiring a special case in add_top_level_skeleton_die_attrs. */
23590 if (!dwarf_split_debug_info)
23591 add_AT_pubnames (comp_unit_die ());
23593 if (dwarf_split_debug_info)
23595 int mark;
23596 unsigned char checksum[16];
23597 struct md5_ctx ctx;
23599 /* Compute a checksum of the comp_unit to use as the dwo_id. */
23600 md5_init_ctx (&ctx);
23601 mark = 0;
23602 die_checksum (comp_unit_die (), &ctx, &mark);
23603 unmark_all_dies (comp_unit_die ());
23604 md5_finish_ctx (&ctx, checksum);
23606 /* Use the first 8 bytes of the checksum as the dwo_id,
23607 and add it to both comp-unit DIEs. */
23608 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
23609 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
23611 /* Add the base offset of the ranges table to the skeleton
23612 comp-unit DIE. */
23613 if (ranges_table_in_use)
23614 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
23615 ranges_section_label);
23617 switch_to_section (debug_addr_section);
23618 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
23619 output_addr_table ();
23622 /* Output the main compilation unit if non-empty or if .debug_macinfo
23623 or .debug_macro will be emitted. */
23624 output_comp_unit (comp_unit_die (), have_macinfo);
23626 if (dwarf_split_debug_info && info_section_emitted)
23627 output_skeleton_debug_sections (main_comp_unit_die);
23629 /* Output the abbreviation table. */
23630 if (abbrev_die_table_in_use != 1)
23632 switch_to_section (debug_abbrev_section);
23633 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
23634 output_abbrev_section ();
23637 /* Output location list section if necessary. */
23638 if (have_location_lists)
23640 /* Output the location lists info. */
23641 switch_to_section (debug_loc_section);
23642 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
23643 output_location_lists (comp_unit_die ());
23646 /* Output public names and types tables if necessary. */
23647 output_pubnames (pubname_table);
23648 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
23649 It shouldn't hurt to emit it always, since pure DWARF2 consumers
23650 simply won't look for the section. */
23651 output_pubnames (pubtype_table);
23653 /* Output the address range information if a CU (.debug_info section)
23654 was emitted. We output an empty table even if we had no functions
23655 to put in it. This because the consumer has no way to tell the
23656 difference between an empty table that we omitted and failure to
23657 generate a table that would have contained data. */
23658 if (info_section_emitted)
23660 unsigned long aranges_length = size_of_aranges ();
23662 switch_to_section (debug_aranges_section);
23663 output_aranges (aranges_length);
23666 /* Output ranges section if necessary. */
23667 if (ranges_table_in_use)
23669 switch_to_section (debug_ranges_section);
23670 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
23671 output_ranges ();
23674 /* Have to end the macro section. */
23675 if (have_macinfo)
23677 switch_to_section (debug_macinfo_section);
23678 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
23679 output_macinfo ();
23680 dw2_asm_output_data (1, 0, "End compilation unit");
23683 /* Output the source line correspondence table. We must do this
23684 even if there is no line information. Otherwise, on an empty
23685 translation unit, we will generate a present, but empty,
23686 .debug_info section. IRIX 6.5 `nm' will then complain when
23687 examining the file. This is done late so that any filenames
23688 used by the debug_info section are marked as 'used'. */
23689 switch_to_section (debug_line_section);
23690 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
23691 if (! DWARF2_ASM_LINE_DEBUG_INFO)
23692 output_line_info (false);
23694 if (dwarf_split_debug_info && info_section_emitted)
23696 switch_to_section (debug_skeleton_line_section);
23697 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
23698 output_line_info (true);
23701 /* If we emitted any indirect strings, output the string table too. */
23702 if (debug_str_hash)
23703 output_indirect_strings ();
23706 #include "gt-dwarf2out.h"