2013-01-12 Janus Weil <janus@gcc.gnu.org>
[official-gcc.git] / gcc / dwarf2out.c
blob5a03280ca73c42ca2f6a65494028feeba89640c1
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_set_name (tree, tree);
2356 /* The debug hooks structure. */
2358 const struct gcc_debug_hooks dwarf2_debug_hooks =
2360 dwarf2out_init,
2361 dwarf2out_finish,
2362 dwarf2out_assembly_start,
2363 dwarf2out_define,
2364 dwarf2out_undef,
2365 dwarf2out_start_source_file,
2366 dwarf2out_end_source_file,
2367 dwarf2out_begin_block,
2368 dwarf2out_end_block,
2369 dwarf2out_ignore_block,
2370 dwarf2out_source_line,
2371 dwarf2out_begin_prologue,
2372 #if VMS_DEBUGGING_INFO
2373 dwarf2out_vms_end_prologue,
2374 dwarf2out_vms_begin_epilogue,
2375 #else
2376 debug_nothing_int_charstar,
2377 debug_nothing_int_charstar,
2378 #endif
2379 dwarf2out_end_epilogue,
2380 dwarf2out_begin_function,
2381 debug_nothing_int, /* end_function */
2382 dwarf2out_function_decl, /* function_decl */
2383 dwarf2out_global_decl,
2384 dwarf2out_type_decl, /* type_decl */
2385 dwarf2out_imported_module_or_decl,
2386 debug_nothing_tree, /* deferred_inline_function */
2387 /* The DWARF 2 backend tries to reduce debugging bloat by not
2388 emitting the abstract description of inline functions until
2389 something tries to reference them. */
2390 dwarf2out_abstract_function, /* outlining_inline_function */
2391 debug_nothing_rtx, /* label */
2392 debug_nothing_int, /* handle_pch */
2393 dwarf2out_var_location,
2394 dwarf2out_switch_text_section,
2395 dwarf2out_set_name,
2396 1, /* start_end_main_source_file */
2397 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2400 /* NOTE: In the comments in this file, many references are made to
2401 "Debugging Information Entries". This term is abbreviated as `DIE'
2402 throughout the remainder of this file. */
2404 /* An internal representation of the DWARF output is built, and then
2405 walked to generate the DWARF debugging info. The walk of the internal
2406 representation is done after the entire program has been compiled.
2407 The types below are used to describe the internal representation. */
2409 /* Whether to put type DIEs into their own section .debug_types instead
2410 of making them part of the .debug_info section. Only supported for
2411 Dwarf V4 or higher and the user didn't disable them through
2412 -fno-debug-types-section. It is more efficient to put them in a
2413 separate comdat sections since the linker will then be able to
2414 remove duplicates. But not all tools support .debug_types sections
2415 yet. */
2417 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2419 /* Various DIE's use offsets relative to the beginning of the
2420 .debug_info section to refer to each other. */
2422 typedef long int dw_offset;
2424 /* Define typedefs here to avoid circular dependencies. */
2426 typedef struct dw_attr_struct *dw_attr_ref;
2427 typedef struct dw_line_info_struct *dw_line_info_ref;
2428 typedef struct pubname_struct *pubname_ref;
2429 typedef struct dw_ranges_struct *dw_ranges_ref;
2430 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
2431 typedef struct comdat_type_struct *comdat_type_node_ref;
2433 /* The entries in the line_info table more-or-less mirror the opcodes
2434 that are used in the real dwarf line table. Arrays of these entries
2435 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2436 supported. */
2438 enum dw_line_info_opcode {
2439 /* Emit DW_LNE_set_address; the operand is the label index. */
2440 LI_set_address,
2442 /* Emit a row to the matrix with the given line. This may be done
2443 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2444 special opcodes. */
2445 LI_set_line,
2447 /* Emit a DW_LNS_set_file. */
2448 LI_set_file,
2450 /* Emit a DW_LNS_set_column. */
2451 LI_set_column,
2453 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2454 LI_negate_stmt,
2456 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2457 LI_set_prologue_end,
2458 LI_set_epilogue_begin,
2460 /* Emit a DW_LNE_set_discriminator. */
2461 LI_set_discriminator
2464 typedef struct GTY(()) dw_line_info_struct {
2465 enum dw_line_info_opcode opcode;
2466 unsigned int val;
2467 } dw_line_info_entry;
2470 typedef struct GTY(()) dw_line_info_table_struct {
2471 /* The label that marks the end of this section. */
2472 const char *end_label;
2474 /* The values for the last row of the matrix, as collected in the table.
2475 These are used to minimize the changes to the next row. */
2476 unsigned int file_num;
2477 unsigned int line_num;
2478 unsigned int column_num;
2479 int discrim_num;
2480 bool is_stmt;
2481 bool in_use;
2483 vec<dw_line_info_entry, va_gc> *entries;
2484 } dw_line_info_table;
2486 typedef dw_line_info_table *dw_line_info_table_p;
2489 /* Each DIE attribute has a field specifying the attribute kind,
2490 a link to the next attribute in the chain, and an attribute value.
2491 Attributes are typically linked below the DIE they modify. */
2493 typedef struct GTY(()) dw_attr_struct {
2494 enum dwarf_attribute dw_attr;
2495 dw_val_node dw_attr_val;
2497 dw_attr_node;
2500 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2501 The children of each node form a circular list linked by
2502 die_sib. die_child points to the node *before* the "first" child node. */
2504 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
2505 union die_symbol_or_type_node
2507 const char * GTY ((tag ("0"))) die_symbol;
2508 comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
2510 GTY ((desc ("%0.comdat_type_p"))) die_id;
2511 vec<dw_attr_node, va_gc> *die_attr;
2512 dw_die_ref die_parent;
2513 dw_die_ref die_child;
2514 dw_die_ref die_sib;
2515 dw_die_ref die_definition; /* ref from a specification to its definition */
2516 dw_offset die_offset;
2517 unsigned long die_abbrev;
2518 int die_mark;
2519 unsigned int decl_id;
2520 enum dwarf_tag die_tag;
2521 /* Die is used and must not be pruned as unused. */
2522 BOOL_BITFIELD die_perennial_p : 1;
2523 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2524 /* Lots of spare bits. */
2526 die_node;
2528 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2529 #define FOR_EACH_CHILD(die, c, expr) do { \
2530 c = die->die_child; \
2531 if (c) do { \
2532 c = c->die_sib; \
2533 expr; \
2534 } while (c != die->die_child); \
2535 } while (0)
2537 /* The pubname structure */
2539 typedef struct GTY(()) pubname_struct {
2540 dw_die_ref die;
2541 const char *name;
2543 pubname_entry;
2546 struct GTY(()) dw_ranges_struct {
2547 /* If this is positive, it's a block number, otherwise it's a
2548 bitwise-negated index into dw_ranges_by_label. */
2549 int num;
2552 /* A structure to hold a macinfo entry. */
2554 typedef struct GTY(()) macinfo_struct {
2555 unsigned char code;
2556 unsigned HOST_WIDE_INT lineno;
2557 const char *info;
2559 macinfo_entry;
2562 struct GTY(()) dw_ranges_by_label_struct {
2563 const char *begin;
2564 const char *end;
2567 /* The comdat type node structure. */
2568 typedef struct GTY(()) comdat_type_struct
2570 dw_die_ref root_die;
2571 dw_die_ref type_die;
2572 dw_die_ref skeleton_die;
2573 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2574 struct comdat_type_struct *next;
2576 comdat_type_node;
2578 /* The limbo die list structure. */
2579 typedef struct GTY(()) limbo_die_struct {
2580 dw_die_ref die;
2581 tree created_for;
2582 struct limbo_die_struct *next;
2584 limbo_die_node;
2586 typedef struct skeleton_chain_struct
2588 dw_die_ref old_die;
2589 dw_die_ref new_die;
2590 struct skeleton_chain_struct *parent;
2592 skeleton_chain_node;
2594 /* Define a macro which returns nonzero for a TYPE_DECL which was
2595 implicitly generated for a type.
2597 Note that, unlike the C front-end (which generates a NULL named
2598 TYPE_DECL node for each complete tagged type, each array type,
2599 and each function type node created) the C++ front-end generates
2600 a _named_ TYPE_DECL node for each tagged type node created.
2601 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2602 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2603 front-end, but for each type, tagged or not. */
2605 #define TYPE_DECL_IS_STUB(decl) \
2606 (DECL_NAME (decl) == NULL_TREE \
2607 || (DECL_ARTIFICIAL (decl) \
2608 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
2609 /* This is necessary for stub decls that \
2610 appear in nested inline functions. */ \
2611 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2612 && (decl_ultimate_origin (decl) \
2613 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2615 /* Information concerning the compilation unit's programming
2616 language, and compiler version. */
2618 /* Fixed size portion of the DWARF compilation unit header. */
2619 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2620 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2622 /* Fixed size portion of the DWARF comdat type unit header. */
2623 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2624 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2625 + DWARF_OFFSET_SIZE)
2627 /* Fixed size portion of public names info. */
2628 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2630 /* Fixed size portion of the address range info. */
2631 #define DWARF_ARANGES_HEADER_SIZE \
2632 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2633 DWARF2_ADDR_SIZE * 2) \
2634 - DWARF_INITIAL_LENGTH_SIZE)
2636 /* Size of padding portion in the address range info. It must be
2637 aligned to twice the pointer size. */
2638 #define DWARF_ARANGES_PAD_SIZE \
2639 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2640 DWARF2_ADDR_SIZE * 2) \
2641 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2643 /* Use assembler line directives if available. */
2644 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2645 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2646 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2647 #else
2648 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2649 #endif
2650 #endif
2652 /* Minimum line offset in a special line info. opcode.
2653 This value was chosen to give a reasonable range of values. */
2654 #define DWARF_LINE_BASE -10
2656 /* First special line opcode - leave room for the standard opcodes. */
2657 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
2659 /* Range of line offsets in a special line info. opcode. */
2660 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
2662 /* Flag that indicates the initial value of the is_stmt_start flag.
2663 In the present implementation, we do not mark any lines as
2664 the beginning of a source statement, because that information
2665 is not made available by the GCC front-end. */
2666 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2668 /* Maximum number of operations per instruction bundle. */
2669 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2670 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2671 #endif
2673 /* This location is used by calc_die_sizes() to keep track
2674 the offset of each DIE within the .debug_info section. */
2675 static unsigned long next_die_offset;
2677 /* Record the root of the DIE's built for the current compilation unit. */
2678 static GTY(()) dw_die_ref single_comp_unit_die;
2680 /* A list of type DIEs that have been separated into comdat sections. */
2681 static GTY(()) comdat_type_node *comdat_type_list;
2683 /* A list of DIEs with a NULL parent waiting to be relocated. */
2684 static GTY(()) limbo_die_node *limbo_die_list;
2686 /* A list of DIEs for which we may have to generate
2687 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
2688 static GTY(()) limbo_die_node *deferred_asm_name;
2690 /* Filenames referenced by this compilation unit. */
2691 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
2693 /* A hash table of references to DIE's that describe declarations.
2694 The key is a DECL_UID() which is a unique number identifying each decl. */
2695 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
2697 /* A hash table of references to DIE's that describe COMMON blocks.
2698 The key is DECL_UID() ^ die_parent. */
2699 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
2701 typedef struct GTY(()) die_arg_entry_struct {
2702 dw_die_ref die;
2703 tree arg;
2704 } die_arg_entry;
2707 /* Node of the variable location list. */
2708 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2709 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2710 EXPR_LIST chain. For small bitsizes, bitsize is encoded
2711 in mode of the EXPR_LIST node and first EXPR_LIST operand
2712 is either NOTE_INSN_VAR_LOCATION for a piece with a known
2713 location or NULL for padding. For larger bitsizes,
2714 mode is 0 and first operand is a CONCAT with bitsize
2715 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2716 NULL as second operand. */
2717 rtx GTY (()) loc;
2718 const char * GTY (()) label;
2719 struct var_loc_node * GTY (()) next;
2722 /* Variable location list. */
2723 struct GTY (()) var_loc_list_def {
2724 struct var_loc_node * GTY (()) first;
2726 /* Pointer to the last but one or last element of the
2727 chained list. If the list is empty, both first and
2728 last are NULL, if the list contains just one node
2729 or the last node certainly is not redundant, it points
2730 to the last node, otherwise points to the last but one.
2731 Do not mark it for GC because it is marked through the chain. */
2732 struct var_loc_node * GTY ((skip ("%h"))) last;
2734 /* Pointer to the last element before section switch,
2735 if NULL, either sections weren't switched or first
2736 is after section switch. */
2737 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2739 /* DECL_UID of the variable decl. */
2740 unsigned int decl_id;
2742 typedef struct var_loc_list_def var_loc_list;
2744 /* Call argument location list. */
2745 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2746 rtx GTY (()) call_arg_loc_note;
2747 const char * GTY (()) label;
2748 tree GTY (()) block;
2749 bool tail_call_p;
2750 rtx GTY (()) symbol_ref;
2751 struct call_arg_loc_node * GTY (()) next;
2755 /* Table of decl location linked lists. */
2756 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
2758 /* Head and tail of call_arg_loc chain. */
2759 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2760 static struct call_arg_loc_node *call_arg_loc_last;
2762 /* Number of call sites in the current function. */
2763 static int call_site_count = -1;
2764 /* Number of tail call sites in the current function. */
2765 static int tail_call_site_count = -1;
2767 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
2768 DIEs. */
2769 static vec<dw_die_ref> block_map;
2771 /* A cached location list. */
2772 struct GTY (()) cached_dw_loc_list_def {
2773 /* The DECL_UID of the decl that this entry describes. */
2774 unsigned int decl_id;
2776 /* The cached location list. */
2777 dw_loc_list_ref loc_list;
2779 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2781 /* Table of cached location lists. */
2782 static GTY ((param_is (cached_dw_loc_list))) htab_t cached_dw_loc_list_table;
2784 /* A pointer to the base of a list of references to DIE's that
2785 are uniquely identified by their tag, presence/absence of
2786 children DIE's, and list of attribute/value pairs. */
2787 static GTY((length ("abbrev_die_table_allocated")))
2788 dw_die_ref *abbrev_die_table;
2790 /* Number of elements currently allocated for abbrev_die_table. */
2791 static GTY(()) unsigned abbrev_die_table_allocated;
2793 /* Number of elements in type_die_table currently in use. */
2794 static GTY(()) unsigned abbrev_die_table_in_use;
2796 /* Size (in elements) of increments by which we may expand the
2797 abbrev_die_table. */
2798 #define ABBREV_DIE_TABLE_INCREMENT 256
2800 /* A global counter for generating labels for line number data. */
2801 static unsigned int line_info_label_num;
2803 /* The current table to which we should emit line number information
2804 for the current function. This will be set up at the beginning of
2805 assembly for the function. */
2806 static dw_line_info_table *cur_line_info_table;
2808 /* The two default tables of line number info. */
2809 static GTY(()) dw_line_info_table *text_section_line_info;
2810 static GTY(()) dw_line_info_table *cold_text_section_line_info;
2812 /* The set of all non-default tables of line number info. */
2813 static GTY(()) vec<dw_line_info_table_p, va_gc> *separate_line_info;
2815 /* A flag to tell pubnames/types export if there is an info section to
2816 refer to. */
2817 static bool info_section_emitted;
2819 /* A pointer to the base of a table that contains a list of publicly
2820 accessible names. */
2821 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
2823 /* A pointer to the base of a table that contains a list of publicly
2824 accessible types. */
2825 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
2827 /* A pointer to the base of a table that contains a list of macro
2828 defines/undefines (and file start/end markers). */
2829 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
2831 /* True if .debug_macinfo or .debug_macros section is going to be
2832 emitted. */
2833 #define have_macinfo \
2834 (debug_info_level >= DINFO_LEVEL_VERBOSE \
2835 && !macinfo_table->is_empty ())
2837 /* Array of dies for which we should generate .debug_ranges info. */
2838 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
2840 /* Number of elements currently allocated for ranges_table. */
2841 static GTY(()) unsigned ranges_table_allocated;
2843 /* Number of elements in ranges_table currently in use. */
2844 static GTY(()) unsigned ranges_table_in_use;
2846 /* Array of pairs of labels referenced in ranges_table. */
2847 static GTY ((length ("ranges_by_label_allocated")))
2848 dw_ranges_by_label_ref ranges_by_label;
2850 /* Number of elements currently allocated for ranges_by_label. */
2851 static GTY(()) unsigned ranges_by_label_allocated;
2853 /* Number of elements in ranges_by_label currently in use. */
2854 static GTY(()) unsigned ranges_by_label_in_use;
2856 /* Size (in elements) of increments by which we may expand the
2857 ranges_table. */
2858 #define RANGES_TABLE_INCREMENT 64
2860 /* Whether we have location lists that need outputting */
2861 static GTY(()) bool have_location_lists;
2863 /* Unique label counter. */
2864 static GTY(()) unsigned int loclabel_num;
2866 /* Unique label counter for point-of-call tables. */
2867 static GTY(()) unsigned int poc_label_num;
2869 /* Record whether the function being analyzed contains inlined functions. */
2870 static int current_function_has_inlines;
2872 /* The last file entry emitted by maybe_emit_file(). */
2873 static GTY(()) struct dwarf_file_data * last_emitted_file;
2875 /* Number of internal labels generated by gen_internal_sym(). */
2876 static GTY(()) int label_num;
2878 /* Cached result of previous call to lookup_filename. */
2879 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
2881 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
2883 /* Instances of generic types for which we need to generate debug
2884 info that describe their generic parameters and arguments. That
2885 generation needs to happen once all types are properly laid out so
2886 we do it at the end of compilation. */
2887 static GTY(()) vec<tree, va_gc> *generic_type_instances;
2889 /* Offset from the "steady-state frame pointer" to the frame base,
2890 within the current function. */
2891 static HOST_WIDE_INT frame_pointer_fb_offset;
2892 static bool frame_pointer_fb_offset_valid;
2894 static vec<dw_die_ref> base_types;
2896 /* Forward declarations for functions defined in this file. */
2898 static int is_pseudo_reg (const_rtx);
2899 static tree type_main_variant (tree);
2900 static int is_tagged_type (const_tree);
2901 static const char *dwarf_tag_name (unsigned);
2902 static const char *dwarf_attr_name (unsigned);
2903 static const char *dwarf_form_name (unsigned);
2904 static tree decl_ultimate_origin (const_tree);
2905 static tree decl_class_context (tree);
2906 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
2907 static inline enum dw_val_class AT_class (dw_attr_ref);
2908 static inline unsigned int AT_index (dw_attr_ref);
2909 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
2910 static inline unsigned AT_flag (dw_attr_ref);
2911 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
2912 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
2913 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
2914 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
2915 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
2916 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2917 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
2918 unsigned int, unsigned char *);
2919 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
2920 static hashval_t debug_str_do_hash (const void *);
2921 static int debug_str_eq (const void *, const void *);
2922 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
2923 static inline const char *AT_string (dw_attr_ref);
2924 static enum dwarf_form AT_string_form (dw_attr_ref);
2925 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
2926 static void add_AT_specification (dw_die_ref, dw_die_ref);
2927 static inline dw_die_ref AT_ref (dw_attr_ref);
2928 static inline int AT_ref_external (dw_attr_ref);
2929 static inline void set_AT_ref_external (dw_attr_ref, int);
2930 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
2931 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
2932 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
2933 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
2934 dw_loc_list_ref);
2935 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
2936 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
2937 static void remove_addr_table_entry (addr_table_entry *);
2938 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
2939 static inline rtx AT_addr (dw_attr_ref);
2940 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
2941 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
2942 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
2943 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
2944 unsigned HOST_WIDE_INT);
2945 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
2946 unsigned long, bool);
2947 static inline const char *AT_lbl (dw_attr_ref);
2948 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
2949 static const char *get_AT_low_pc (dw_die_ref);
2950 static const char *get_AT_hi_pc (dw_die_ref);
2951 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
2952 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
2953 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
2954 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
2955 static bool is_cxx (void);
2956 static bool is_fortran (void);
2957 static bool is_ada (void);
2958 static void remove_AT (dw_die_ref, enum dwarf_attribute);
2959 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
2960 static void add_child_die (dw_die_ref, dw_die_ref);
2961 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
2962 static dw_die_ref lookup_type_die (tree);
2963 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
2964 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
2965 static void equate_type_number_to_die (tree, dw_die_ref);
2966 static hashval_t decl_die_table_hash (const void *);
2967 static int decl_die_table_eq (const void *, const void *);
2968 static dw_die_ref lookup_decl_die (tree);
2969 static hashval_t common_block_die_table_hash (const void *);
2970 static int common_block_die_table_eq (const void *, const void *);
2971 static hashval_t decl_loc_table_hash (const void *);
2972 static int decl_loc_table_eq (const void *, const void *);
2973 static var_loc_list *lookup_decl_loc (const_tree);
2974 static void equate_decl_number_to_die (tree, dw_die_ref);
2975 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
2976 static void print_spaces (FILE *);
2977 static void print_die (dw_die_ref, FILE *);
2978 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
2979 static dw_die_ref pop_compile_unit (dw_die_ref);
2980 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
2981 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
2982 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
2983 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
2984 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
2985 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
2986 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
2987 struct md5_ctx *, int *);
2988 struct checksum_attributes;
2989 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
2990 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
2991 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
2992 static void generate_type_signature (dw_die_ref, comdat_type_node *);
2993 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
2994 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
2995 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
2996 static int same_die_p (dw_die_ref, dw_die_ref, int *);
2997 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
2998 static void compute_section_prefix (dw_die_ref);
2999 static int is_type_die (dw_die_ref);
3000 static int is_comdat_die (dw_die_ref);
3001 static int is_symbol_die (dw_die_ref);
3002 static void assign_symbol_names (dw_die_ref);
3003 static void break_out_includes (dw_die_ref);
3004 static int is_declaration_die (dw_die_ref);
3005 static int should_move_die_to_comdat (dw_die_ref);
3006 static dw_die_ref clone_as_declaration (dw_die_ref);
3007 static dw_die_ref clone_die (dw_die_ref);
3008 static dw_die_ref clone_tree (dw_die_ref);
3009 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3010 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3011 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3012 static dw_die_ref generate_skeleton (dw_die_ref);
3013 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3014 dw_die_ref,
3015 dw_die_ref);
3016 static void break_out_comdat_types (dw_die_ref);
3017 static dw_die_ref copy_ancestor_tree (dw_die_ref, dw_die_ref, htab_t);
3018 static void copy_decls_walk (dw_die_ref, dw_die_ref, htab_t);
3019 static void copy_decls_for_unworthy_types (dw_die_ref);
3021 static hashval_t htab_cu_hash (const void *);
3022 static int htab_cu_eq (const void *, const void *);
3023 static void htab_cu_del (void *);
3024 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
3025 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
3026 static void add_sibling_attributes (dw_die_ref);
3027 static void build_abbrev_table (dw_die_ref, htab_t);
3028 static void output_location_lists (dw_die_ref);
3029 static int constant_size (unsigned HOST_WIDE_INT);
3030 static unsigned long size_of_die (dw_die_ref);
3031 static void calc_die_sizes (dw_die_ref);
3032 static void calc_base_type_die_sizes (void);
3033 static void mark_dies (dw_die_ref);
3034 static void unmark_dies (dw_die_ref);
3035 static void unmark_all_dies (dw_die_ref);
3036 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3037 static unsigned long size_of_aranges (void);
3038 static enum dwarf_form value_format (dw_attr_ref);
3039 static void output_value_format (dw_attr_ref);
3040 static void output_abbrev_section (void);
3041 static void output_die_abbrevs (unsigned long, dw_die_ref);
3042 static void output_die_symbol (dw_die_ref);
3043 static void output_die (dw_die_ref);
3044 static void output_compilation_unit_header (void);
3045 static void output_comp_unit (dw_die_ref, int);
3046 static void output_comdat_type_unit (comdat_type_node *);
3047 static const char *dwarf2_name (tree, int);
3048 static void add_pubname (tree, dw_die_ref);
3049 static void add_enumerator_pubname (const char *, dw_die_ref);
3050 static void add_pubname_string (const char *, dw_die_ref);
3051 static void add_pubtype (tree, dw_die_ref);
3052 static void output_pubnames (vec<pubname_entry, va_gc> *);
3053 static void output_aranges (unsigned long);
3054 static unsigned int add_ranges_num (int);
3055 static unsigned int add_ranges (const_tree);
3056 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3057 bool *, bool);
3058 static void output_ranges (void);
3059 static dw_line_info_table *new_line_info_table (void);
3060 static void output_line_info (bool);
3061 static void output_file_names (void);
3062 static dw_die_ref base_type_die (tree);
3063 static int is_base_type (tree);
3064 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3065 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
3066 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3067 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3068 static int type_is_enum (const_tree);
3069 static unsigned int dbx_reg_number (const_rtx);
3070 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3071 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3072 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3073 enum var_init_status);
3074 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3075 enum var_init_status);
3076 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3077 enum var_init_status);
3078 static int is_based_loc (const_rtx);
3079 static int resolve_one_addr (rtx *, void *);
3080 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3081 enum var_init_status);
3082 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
3083 enum var_init_status);
3084 static dw_loc_list_ref loc_list_from_tree (tree, int);
3085 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
3086 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3087 static tree field_type (const_tree);
3088 static unsigned int simple_type_align_in_bits (const_tree);
3089 static unsigned int simple_decl_align_in_bits (const_tree);
3090 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3091 static HOST_WIDE_INT field_byte_offset (const_tree);
3092 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3093 dw_loc_list_ref);
3094 static void add_data_member_location_attribute (dw_die_ref, tree);
3095 static bool add_const_value_attribute (dw_die_ref, rtx);
3096 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3097 static void insert_double (double_int, unsigned char *);
3098 static void insert_float (const_rtx, unsigned char *);
3099 static rtx rtl_for_decl_location (tree);
3100 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3101 enum dwarf_attribute);
3102 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3103 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3104 static void add_name_attribute (dw_die_ref, const char *);
3105 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3106 static void add_comp_dir_attribute (dw_die_ref);
3107 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
3108 static void add_subscript_info (dw_die_ref, tree, bool);
3109 static void add_byte_size_attribute (dw_die_ref, tree);
3110 static void add_bit_offset_attribute (dw_die_ref, tree);
3111 static void add_bit_size_attribute (dw_die_ref, tree);
3112 static void add_prototyped_attribute (dw_die_ref, tree);
3113 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3114 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3115 static void add_src_coords_attributes (dw_die_ref, tree);
3116 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3117 static void push_decl_scope (tree);
3118 static void pop_decl_scope (void);
3119 static dw_die_ref scope_die_for (tree, dw_die_ref);
3120 static inline int local_scope_p (dw_die_ref);
3121 static inline int class_scope_p (dw_die_ref);
3122 static inline int class_or_namespace_scope_p (dw_die_ref);
3123 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
3124 static void add_calling_convention_attribute (dw_die_ref, tree);
3125 static const char *type_tag (const_tree);
3126 static tree member_declared_type (const_tree);
3127 #if 0
3128 static const char *decl_start_label (tree);
3129 #endif
3130 static void gen_array_type_die (tree, dw_die_ref);
3131 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3132 #if 0
3133 static void gen_entry_point_die (tree, dw_die_ref);
3134 #endif
3135 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3136 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3137 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3138 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3139 static void gen_formal_types_die (tree, dw_die_ref);
3140 static void gen_subprogram_die (tree, dw_die_ref);
3141 static void gen_variable_die (tree, tree, dw_die_ref);
3142 static void gen_const_die (tree, dw_die_ref);
3143 static void gen_label_die (tree, dw_die_ref);
3144 static void gen_lexical_block_die (tree, dw_die_ref, int);
3145 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
3146 static void gen_field_die (tree, dw_die_ref);
3147 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3148 static dw_die_ref gen_compile_unit_die (const char *);
3149 static void gen_inheritance_die (tree, tree, dw_die_ref);
3150 static void gen_member_die (tree, dw_die_ref);
3151 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3152 enum debug_info_usage);
3153 static void gen_subroutine_type_die (tree, dw_die_ref);
3154 static void gen_typedef_die (tree, dw_die_ref);
3155 static void gen_type_die (tree, dw_die_ref);
3156 static void gen_block_die (tree, dw_die_ref, int);
3157 static void decls_for_scope (tree, dw_die_ref, int);
3158 static inline int is_redundant_typedef (const_tree);
3159 static bool is_naming_typedef_decl (const_tree);
3160 static inline dw_die_ref get_context_die (tree);
3161 static void gen_namespace_die (tree, dw_die_ref);
3162 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3163 static dw_die_ref force_decl_die (tree);
3164 static dw_die_ref force_type_die (tree);
3165 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3166 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3167 static struct dwarf_file_data * lookup_filename (const char *);
3168 static void retry_incomplete_types (void);
3169 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3170 static void gen_generic_params_dies (tree);
3171 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3172 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3173 static void splice_child_die (dw_die_ref, dw_die_ref);
3174 static int file_info_cmp (const void *, const void *);
3175 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3176 const char *, const char *);
3177 static void output_loc_list (dw_loc_list_ref);
3178 static char *gen_internal_sym (const char *);
3179 static bool want_pubnames (void);
3181 static void prune_unmark_dies (dw_die_ref);
3182 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3183 static void prune_unused_types_mark (dw_die_ref, int);
3184 static void prune_unused_types_walk (dw_die_ref);
3185 static void prune_unused_types_walk_attribs (dw_die_ref);
3186 static void prune_unused_types_prune (dw_die_ref);
3187 static void prune_unused_types (void);
3188 static int maybe_emit_file (struct dwarf_file_data *fd);
3189 static inline const char *AT_vms_delta1 (dw_attr_ref);
3190 static inline const char *AT_vms_delta2 (dw_attr_ref);
3191 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3192 const char *, const char *);
3193 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3194 static void gen_remaining_tmpl_value_param_die_attribute (void);
3195 static bool generic_type_p (tree);
3196 static void schedule_generic_params_dies_gen (tree t);
3197 static void gen_scheduled_generic_parms_dies (void);
3199 /* enum for tracking thread-local variables whose address is really an offset
3200 relative to the TLS pointer, which will need link-time relocation, but will
3201 not need relocation by the DWARF consumer. */
3203 enum dtprel_bool
3205 dtprel_false = 0,
3206 dtprel_true = 1
3209 /* Return the operator to use for an address of a variable. For dtprel_true, we
3210 use DW_OP_const*. For regular variables, which need both link-time
3211 relocation and consumer-level relocation (e.g., to account for shared objects
3212 loaded at a random address), we use DW_OP_addr*. */
3214 static inline enum dwarf_location_atom
3215 dw_addr_op (enum dtprel_bool dtprel)
3217 if (dtprel == dtprel_true)
3218 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3219 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3220 else
3221 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3224 /* Return a pointer to a newly allocated address location description. If
3225 dwarf_split_debug_info is true, then record the address with the appropriate
3226 relocation. */
3227 static inline dw_loc_descr_ref
3228 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3230 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3232 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3233 ref->dw_loc_oprnd1.v.val_addr = addr;
3234 ref->dtprel = dtprel;
3235 if (dwarf_split_debug_info)
3236 ref->dw_loc_oprnd1.val_entry
3237 = add_addr_table_entry (addr,
3238 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3239 else
3240 ref->dw_loc_oprnd1.val_entry = NULL;
3242 return ref;
3245 /* Section names used to hold DWARF debugging information. */
3247 #ifndef DEBUG_INFO_SECTION
3248 #define DEBUG_INFO_SECTION ".debug_info"
3249 #endif
3250 #ifndef DEBUG_DWO_INFO_SECTION
3251 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3252 #endif
3253 #ifndef DEBUG_ABBREV_SECTION
3254 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3255 #endif
3256 #ifndef DEBUG_DWO_ABBREV_SECTION
3257 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3258 #endif
3259 #ifndef DEBUG_ARANGES_SECTION
3260 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3261 #endif
3262 #ifndef DEBUG_ADDR_SECTION
3263 #define DEBUG_ADDR_SECTION ".debug_addr"
3264 #endif
3265 #ifndef DEBUG_NORM_MACINFO_SECTION
3266 #define DEBUG_NORM_MACINFO_SECTION ".debug_macinfo"
3267 #endif
3268 #ifndef DEBUG_DWO_MACINFO_SECTION
3269 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3270 #endif
3271 #ifndef DEBUG_MACINFO_SECTION
3272 #define DEBUG_MACINFO_SECTION \
3273 (!dwarf_split_debug_info \
3274 ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3275 #endif
3276 #ifndef DEBUG_NORM_MACRO_SECTION
3277 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3278 #endif
3279 #ifndef DEBUG_DWO_MACRO_SECTION
3280 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3281 #endif
3282 #ifndef DEBUG_MACRO_SECTION
3283 #define DEBUG_MACRO_SECTION \
3284 (!dwarf_split_debug_info \
3285 ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3286 #endif
3287 #ifndef DEBUG_LINE_SECTION
3288 #define DEBUG_LINE_SECTION ".debug_line"
3289 #endif
3290 #ifndef DEBUG_DWO_LINE_SECTION
3291 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3292 #endif
3293 #ifndef DEBUG_LOC_SECTION
3294 #define DEBUG_LOC_SECTION ".debug_loc"
3295 #endif
3296 #ifndef DEBUG_DWO_LOC_SECTION
3297 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3298 #endif
3299 #ifndef DEBUG_PUBNAMES_SECTION
3300 #define DEBUG_PUBNAMES_SECTION ".debug_pubnames"
3301 #endif
3302 #ifndef DEBUG_PUBTYPES_SECTION
3303 #define DEBUG_PUBTYPES_SECTION ".debug_pubtypes"
3304 #endif
3305 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3306 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3307 #ifndef DEBUG_STR_OFFSETS_SECTION
3308 #define DEBUG_STR_OFFSETS_SECTION \
3309 (!dwarf_split_debug_info \
3310 ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3311 #endif
3312 #define DEBUG_DWO_STR_SECTION ".debug_str.dwo"
3313 #define DEBUG_NORM_STR_SECTION ".debug_str"
3314 #ifndef DEBUG_STR_SECTION
3315 #define DEBUG_STR_SECTION \
3316 (!dwarf_split_debug_info ? (DEBUG_NORM_STR_SECTION) : (DEBUG_DWO_STR_SECTION))
3317 #endif
3318 #ifndef DEBUG_RANGES_SECTION
3319 #define DEBUG_RANGES_SECTION ".debug_ranges"
3320 #endif
3322 /* Standard ELF section names for compiled code and data. */
3323 #ifndef TEXT_SECTION_NAME
3324 #define TEXT_SECTION_NAME ".text"
3325 #endif
3327 /* Section flags for .debug_macinfo/.debug_macro section. */
3328 #define DEBUG_MACRO_SECTION_FLAGS \
3329 (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3331 /* Section flags for .debug_str section. */
3332 #define DEBUG_STR_SECTION_FLAGS \
3333 (dwarf_split_debug_info \
3334 ? SECTION_DEBUG | SECTION_EXCLUDE \
3335 : (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3336 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3337 : SECTION_DEBUG))
3339 /* Labels we insert at beginning sections we can reference instead of
3340 the section names themselves. */
3342 #ifndef TEXT_SECTION_LABEL
3343 #define TEXT_SECTION_LABEL "Ltext"
3344 #endif
3345 #ifndef COLD_TEXT_SECTION_LABEL
3346 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3347 #endif
3348 #ifndef DEBUG_LINE_SECTION_LABEL
3349 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3350 #endif
3351 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3352 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3353 #endif
3354 #ifndef DEBUG_INFO_SECTION_LABEL
3355 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3356 #endif
3357 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3358 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3359 #endif
3360 #ifndef DEBUG_ABBREV_SECTION_LABEL
3361 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3362 #endif
3363 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3364 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3365 #endif
3366 #ifndef DEBUG_ADDR_SECTION_LABEL
3367 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3368 #endif
3369 #ifndef DEBUG_LOC_SECTION_LABEL
3370 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3371 #endif
3372 #ifndef DEBUG_RANGES_SECTION_LABEL
3373 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3374 #endif
3375 #ifndef DEBUG_MACINFO_SECTION_LABEL
3376 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3377 #endif
3378 #ifndef DEBUG_MACRO_SECTION_LABEL
3379 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3380 #endif
3381 #define SKELETON_COMP_DIE_ABBREV 1
3382 #define SKELETON_TYPE_DIE_ABBREV 2
3384 /* Definitions of defaults for formats and names of various special
3385 (artificial) labels which may be generated within this file (when the -g
3386 options is used and DWARF2_DEBUGGING_INFO is in effect.
3387 If necessary, these may be overridden from within the tm.h file, but
3388 typically, overriding these defaults is unnecessary. */
3390 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3391 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3392 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3393 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3394 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3395 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3396 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3397 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3398 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3399 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3400 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3401 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3402 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3403 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3405 #ifndef TEXT_END_LABEL
3406 #define TEXT_END_LABEL "Letext"
3407 #endif
3408 #ifndef COLD_END_LABEL
3409 #define COLD_END_LABEL "Letext_cold"
3410 #endif
3411 #ifndef BLOCK_BEGIN_LABEL
3412 #define BLOCK_BEGIN_LABEL "LBB"
3413 #endif
3414 #ifndef BLOCK_END_LABEL
3415 #define BLOCK_END_LABEL "LBE"
3416 #endif
3417 #ifndef LINE_CODE_LABEL
3418 #define LINE_CODE_LABEL "LM"
3419 #endif
3422 /* Return the root of the DIE's built for the current compilation unit. */
3423 static dw_die_ref
3424 comp_unit_die (void)
3426 if (!single_comp_unit_die)
3427 single_comp_unit_die = gen_compile_unit_die (NULL);
3428 return single_comp_unit_die;
3431 /* We allow a language front-end to designate a function that is to be
3432 called to "demangle" any name before it is put into a DIE. */
3434 static const char *(*demangle_name_func) (const char *);
3436 void
3437 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3439 demangle_name_func = func;
3442 /* Test if rtl node points to a pseudo register. */
3444 static inline int
3445 is_pseudo_reg (const_rtx rtl)
3447 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3448 || (GET_CODE (rtl) == SUBREG
3449 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3452 /* Return a reference to a type, with its const and volatile qualifiers
3453 removed. */
3455 static inline tree
3456 type_main_variant (tree type)
3458 type = TYPE_MAIN_VARIANT (type);
3460 /* ??? There really should be only one main variant among any group of
3461 variants of a given type (and all of the MAIN_VARIANT values for all
3462 members of the group should point to that one type) but sometimes the C
3463 front-end messes this up for array types, so we work around that bug
3464 here. */
3465 if (TREE_CODE (type) == ARRAY_TYPE)
3466 while (type != TYPE_MAIN_VARIANT (type))
3467 type = TYPE_MAIN_VARIANT (type);
3469 return type;
3472 /* Return nonzero if the given type node represents a tagged type. */
3474 static inline int
3475 is_tagged_type (const_tree type)
3477 enum tree_code code = TREE_CODE (type);
3479 return (code == RECORD_TYPE || code == UNION_TYPE
3480 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3483 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3485 static void
3486 get_ref_die_offset_label (char *label, dw_die_ref ref)
3488 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3491 /* Return die_offset of a DIE reference to a base type. */
3493 static unsigned long int
3494 get_base_type_offset (dw_die_ref ref)
3496 if (ref->die_offset)
3497 return ref->die_offset;
3498 if (comp_unit_die ()->die_abbrev)
3500 calc_base_type_die_sizes ();
3501 gcc_assert (ref->die_offset);
3503 return ref->die_offset;
3506 /* Return die_offset of a DIE reference other than base type. */
3508 static unsigned long int
3509 get_ref_die_offset (dw_die_ref ref)
3511 gcc_assert (ref->die_offset);
3512 return ref->die_offset;
3515 /* Convert a DIE tag into its string name. */
3517 static const char *
3518 dwarf_tag_name (unsigned int tag)
3520 const char *name = get_DW_TAG_name (tag);
3522 if (name != NULL)
3523 return name;
3525 return "DW_TAG_<unknown>";
3528 /* Convert a DWARF attribute code into its string name. */
3530 static const char *
3531 dwarf_attr_name (unsigned int attr)
3533 const char *name;
3535 switch (attr)
3537 #if VMS_DEBUGGING_INFO
3538 case DW_AT_HP_prologue:
3539 return "DW_AT_HP_prologue";
3540 #else
3541 case DW_AT_MIPS_loop_unroll_factor:
3542 return "DW_AT_MIPS_loop_unroll_factor";
3543 #endif
3545 #if VMS_DEBUGGING_INFO
3546 case DW_AT_HP_epilogue:
3547 return "DW_AT_HP_epilogue";
3548 #else
3549 case DW_AT_MIPS_stride:
3550 return "DW_AT_MIPS_stride";
3551 #endif
3554 name = get_DW_AT_name (attr);
3556 if (name != NULL)
3557 return name;
3559 return "DW_AT_<unknown>";
3562 /* Convert a DWARF value form code into its string name. */
3564 static const char *
3565 dwarf_form_name (unsigned int form)
3567 const char *name = get_DW_FORM_name (form);
3569 if (name != NULL)
3570 return name;
3572 return "DW_FORM_<unknown>";
3575 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3576 instance of an inlined instance of a decl which is local to an inline
3577 function, so we have to trace all of the way back through the origin chain
3578 to find out what sort of node actually served as the original seed for the
3579 given block. */
3581 static tree
3582 decl_ultimate_origin (const_tree decl)
3584 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3585 return NULL_TREE;
3587 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
3588 nodes in the function to point to themselves; ignore that if
3589 we're trying to output the abstract instance of this function. */
3590 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3591 return NULL_TREE;
3593 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3594 most distant ancestor, this should never happen. */
3595 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3597 return DECL_ABSTRACT_ORIGIN (decl);
3600 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
3601 of a virtual function may refer to a base class, so we check the 'this'
3602 parameter. */
3604 static tree
3605 decl_class_context (tree decl)
3607 tree context = NULL_TREE;
3609 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3610 context = DECL_CONTEXT (decl);
3611 else
3612 context = TYPE_MAIN_VARIANT
3613 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3615 if (context && !TYPE_P (context))
3616 context = NULL_TREE;
3618 return context;
3621 /* Add an attribute/value pair to a DIE. */
3623 static inline void
3624 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
3626 /* Maybe this should be an assert? */
3627 if (die == NULL)
3628 return;
3630 vec_safe_reserve (die->die_attr, 1);
3631 vec_safe_push (die->die_attr, *attr);
3634 static inline enum dw_val_class
3635 AT_class (dw_attr_ref a)
3637 return a->dw_attr_val.val_class;
3640 /* Return the index for any attribute that will be referenced with a
3641 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
3642 are stored in dw_attr_val.v.val_str for reference counting
3643 pruning. */
3645 static inline unsigned int
3646 AT_index (dw_attr_ref a)
3648 if (AT_class (a) == dw_val_class_str)
3649 return a->dw_attr_val.v.val_str->index;
3650 else if (a->dw_attr_val.val_entry != NULL)
3651 return a->dw_attr_val.val_entry->index;
3652 return NOT_INDEXED;
3655 /* Add a flag value attribute to a DIE. */
3657 static inline void
3658 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3660 dw_attr_node attr;
3662 attr.dw_attr = attr_kind;
3663 attr.dw_attr_val.val_class = dw_val_class_flag;
3664 attr.dw_attr_val.val_entry = NULL;
3665 attr.dw_attr_val.v.val_flag = flag;
3666 add_dwarf_attr (die, &attr);
3669 static inline unsigned
3670 AT_flag (dw_attr_ref a)
3672 gcc_assert (a && AT_class (a) == dw_val_class_flag);
3673 return a->dw_attr_val.v.val_flag;
3676 /* Add a signed integer attribute value to a DIE. */
3678 static inline void
3679 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3681 dw_attr_node attr;
3683 attr.dw_attr = attr_kind;
3684 attr.dw_attr_val.val_class = dw_val_class_const;
3685 attr.dw_attr_val.val_entry = NULL;
3686 attr.dw_attr_val.v.val_int = int_val;
3687 add_dwarf_attr (die, &attr);
3690 static inline HOST_WIDE_INT
3691 AT_int (dw_attr_ref a)
3693 gcc_assert (a && AT_class (a) == dw_val_class_const);
3694 return a->dw_attr_val.v.val_int;
3697 /* Add an unsigned integer attribute value to a DIE. */
3699 static inline void
3700 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3701 unsigned HOST_WIDE_INT unsigned_val)
3703 dw_attr_node attr;
3705 attr.dw_attr = attr_kind;
3706 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3707 attr.dw_attr_val.val_entry = NULL;
3708 attr.dw_attr_val.v.val_unsigned = unsigned_val;
3709 add_dwarf_attr (die, &attr);
3712 static inline unsigned HOST_WIDE_INT
3713 AT_unsigned (dw_attr_ref a)
3715 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3716 return a->dw_attr_val.v.val_unsigned;
3719 /* Add an unsigned double integer attribute value to a DIE. */
3721 static inline void
3722 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3723 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3725 dw_attr_node attr;
3727 attr.dw_attr = attr_kind;
3728 attr.dw_attr_val.val_class = dw_val_class_const_double;
3729 attr.dw_attr_val.val_entry = NULL;
3730 attr.dw_attr_val.v.val_double.high = high;
3731 attr.dw_attr_val.v.val_double.low = low;
3732 add_dwarf_attr (die, &attr);
3735 /* Add a floating point attribute value to a DIE and return it. */
3737 static inline void
3738 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3739 unsigned int length, unsigned int elt_size, unsigned char *array)
3741 dw_attr_node attr;
3743 attr.dw_attr = attr_kind;
3744 attr.dw_attr_val.val_class = dw_val_class_vec;
3745 attr.dw_attr_val.val_entry = NULL;
3746 attr.dw_attr_val.v.val_vec.length = length;
3747 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3748 attr.dw_attr_val.v.val_vec.array = array;
3749 add_dwarf_attr (die, &attr);
3752 /* Add an 8-byte data attribute value to a DIE. */
3754 static inline void
3755 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3756 unsigned char data8[8])
3758 dw_attr_node attr;
3760 attr.dw_attr = attr_kind;
3761 attr.dw_attr_val.val_class = dw_val_class_data8;
3762 attr.dw_attr_val.val_entry = NULL;
3763 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3764 add_dwarf_attr (die, &attr);
3767 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
3768 dwarf_split_debug_info, address attributes in dies destined for the
3769 final executable have force_direct set to avoid using indexed
3770 references. */
3772 static inline void
3773 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
3774 bool force_direct)
3776 dw_attr_node attr;
3777 char * lbl_id;
3779 lbl_id = xstrdup (lbl_low);
3780 attr.dw_attr = DW_AT_low_pc;
3781 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3782 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3783 if (dwarf_split_debug_info && !force_direct)
3784 attr.dw_attr_val.val_entry
3785 = add_addr_table_entry (lbl_id, ate_kind_label);
3786 else
3787 attr.dw_attr_val.val_entry = NULL;
3788 add_dwarf_attr (die, &attr);
3790 attr.dw_attr = DW_AT_high_pc;
3791 if (dwarf_version < 4)
3792 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3793 else
3794 attr.dw_attr_val.val_class = dw_val_class_high_pc;
3795 lbl_id = xstrdup (lbl_high);
3796 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3797 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
3798 && dwarf_split_debug_info && !force_direct)
3799 attr.dw_attr_val.val_entry
3800 = add_addr_table_entry (lbl_id, ate_kind_label);
3801 else
3802 attr.dw_attr_val.val_entry = NULL;
3803 add_dwarf_attr (die, &attr);
3806 /* Hash and equality functions for debug_str_hash. */
3808 static hashval_t
3809 debug_str_do_hash (const void *x)
3811 return htab_hash_string (((const struct indirect_string_node *)x)->str);
3814 static int
3815 debug_str_eq (const void *x1, const void *x2)
3817 return strcmp ((((const struct indirect_string_node *)x1)->str),
3818 (const char *)x2) == 0;
3821 /* Add STR to the indirect string hash table. */
3823 static struct indirect_string_node *
3824 find_AT_string (const char *str)
3826 struct indirect_string_node *node;
3827 void **slot;
3829 if (! debug_str_hash)
3830 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
3831 debug_str_eq, NULL);
3833 slot = htab_find_slot_with_hash (debug_str_hash, str,
3834 htab_hash_string (str), INSERT);
3835 if (*slot == NULL)
3837 node = ggc_alloc_cleared_indirect_string_node ();
3838 node->str = ggc_strdup (str);
3839 *slot = node;
3841 else
3842 node = (struct indirect_string_node *) *slot;
3844 node->refcount++;
3845 return node;
3848 /* Add a string attribute value to a DIE. */
3850 static inline void
3851 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
3853 dw_attr_node attr;
3854 struct indirect_string_node *node;
3856 node = find_AT_string (str);
3858 attr.dw_attr = attr_kind;
3859 attr.dw_attr_val.val_class = dw_val_class_str;
3860 attr.dw_attr_val.val_entry = NULL;
3861 attr.dw_attr_val.v.val_str = node;
3862 add_dwarf_attr (die, &attr);
3865 static inline const char *
3866 AT_string (dw_attr_ref a)
3868 gcc_assert (a && AT_class (a) == dw_val_class_str);
3869 return a->dw_attr_val.v.val_str->str;
3872 /* Call this function directly to bypass AT_string_form's logic to put
3873 the string inline in the die. */
3875 static void
3876 set_indirect_string (struct indirect_string_node *node)
3878 char label[32];
3879 /* Already indirect is a no op. */
3880 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
3882 gcc_assert (node->label);
3883 return;
3885 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
3886 ++dw2_string_counter;
3887 node->label = xstrdup (label);
3889 if (!dwarf_split_debug_info)
3891 node->form = DW_FORM_strp;
3892 node->index = NOT_INDEXED;
3894 else
3896 node->form = DW_FORM_GNU_str_index;
3897 node->index = NO_INDEX_ASSIGNED;
3901 /* Find out whether a string should be output inline in DIE
3902 or out-of-line in .debug_str section. */
3904 static enum dwarf_form
3905 find_string_form (struct indirect_string_node *node)
3907 unsigned int len;
3909 if (node->form)
3910 return node->form;
3912 len = strlen (node->str) + 1;
3914 /* If the string is shorter or equal to the size of the reference, it is
3915 always better to put it inline. */
3916 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
3917 return node->form = DW_FORM_string;
3919 /* If we cannot expect the linker to merge strings in .debug_str
3920 section, only put it into .debug_str if it is worth even in this
3921 single module. */
3922 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
3923 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
3924 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
3925 return node->form = DW_FORM_string;
3927 set_indirect_string (node);
3929 return node->form;
3932 /* Find out whether the string referenced from the attribute should be
3933 output inline in DIE or out-of-line in .debug_str section. */
3935 static enum dwarf_form
3936 AT_string_form (dw_attr_ref a)
3938 gcc_assert (a && AT_class (a) == dw_val_class_str);
3939 return find_string_form (a->dw_attr_val.v.val_str);
3942 /* Add a DIE reference attribute value to a DIE. */
3944 static inline void
3945 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
3947 dw_attr_node attr;
3949 #ifdef ENABLE_CHECKING
3950 gcc_assert (targ_die != NULL);
3951 #else
3952 /* With LTO we can end up trying to reference something we didn't create
3953 a DIE for. Avoid crashing later on a NULL referenced DIE. */
3954 if (targ_die == NULL)
3955 return;
3956 #endif
3958 attr.dw_attr = attr_kind;
3959 attr.dw_attr_val.val_class = dw_val_class_die_ref;
3960 attr.dw_attr_val.val_entry = NULL;
3961 attr.dw_attr_val.v.val_die_ref.die = targ_die;
3962 attr.dw_attr_val.v.val_die_ref.external = 0;
3963 add_dwarf_attr (die, &attr);
3966 /* Change DIE reference REF to point to NEW_DIE instead. */
3968 static inline void
3969 change_AT_die_ref (dw_attr_ref ref, dw_die_ref new_die)
3971 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
3972 ref->dw_attr_val.v.val_die_ref.die = new_die;
3973 ref->dw_attr_val.v.val_die_ref.external = 0;
3976 /* Add an AT_specification attribute to a DIE, and also make the back
3977 pointer from the specification to the definition. */
3979 static inline void
3980 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
3982 add_AT_die_ref (die, DW_AT_specification, targ_die);
3983 gcc_assert (!targ_die->die_definition);
3984 targ_die->die_definition = die;
3987 static inline dw_die_ref
3988 AT_ref (dw_attr_ref a)
3990 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
3991 return a->dw_attr_val.v.val_die_ref.die;
3994 static inline int
3995 AT_ref_external (dw_attr_ref a)
3997 if (a && AT_class (a) == dw_val_class_die_ref)
3998 return a->dw_attr_val.v.val_die_ref.external;
4000 return 0;
4003 static inline void
4004 set_AT_ref_external (dw_attr_ref a, int i)
4006 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4007 a->dw_attr_val.v.val_die_ref.external = i;
4010 /* Add an FDE reference attribute value to a DIE. */
4012 static inline void
4013 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4015 dw_attr_node attr;
4017 attr.dw_attr = attr_kind;
4018 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4019 attr.dw_attr_val.val_entry = NULL;
4020 attr.dw_attr_val.v.val_fde_index = targ_fde;
4021 add_dwarf_attr (die, &attr);
4024 /* Add a location description attribute value to a DIE. */
4026 static inline void
4027 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4029 dw_attr_node attr;
4031 attr.dw_attr = attr_kind;
4032 attr.dw_attr_val.val_class = dw_val_class_loc;
4033 attr.dw_attr_val.val_entry = NULL;
4034 attr.dw_attr_val.v.val_loc = loc;
4035 add_dwarf_attr (die, &attr);
4038 static inline dw_loc_descr_ref
4039 AT_loc (dw_attr_ref a)
4041 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4042 return a->dw_attr_val.v.val_loc;
4045 static inline void
4046 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4048 dw_attr_node attr;
4050 attr.dw_attr = attr_kind;
4051 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4052 attr.dw_attr_val.val_entry = NULL;
4053 attr.dw_attr_val.v.val_loc_list = loc_list;
4054 add_dwarf_attr (die, &attr);
4055 have_location_lists = true;
4058 static inline dw_loc_list_ref
4059 AT_loc_list (dw_attr_ref a)
4061 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4062 return a->dw_attr_val.v.val_loc_list;
4065 static inline dw_loc_list_ref *
4066 AT_loc_list_ptr (dw_attr_ref a)
4068 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4069 return &a->dw_attr_val.v.val_loc_list;
4072 /* Table of entries into the .debug_addr section. */
4074 static GTY ((param_is (addr_table_entry))) htab_t addr_index_table;
4076 /* Hash an address_table_entry. */
4078 static hashval_t
4079 addr_table_entry_do_hash (const void *x)
4081 const addr_table_entry *a = (const addr_table_entry *) x;
4082 switch (a->kind)
4084 case ate_kind_rtx:
4085 return iterative_hash_rtx (a->addr.rtl, 0);
4086 case ate_kind_rtx_dtprel:
4087 return iterative_hash_rtx (a->addr.rtl, 1);
4088 case ate_kind_label:
4089 return htab_hash_string (a->addr.label);
4090 default:
4091 gcc_unreachable ();
4095 /* Determine equality for two address_table_entries. */
4097 static int
4098 addr_table_entry_eq (const void *x1, const void *x2)
4100 const addr_table_entry *a1 = (const addr_table_entry *) x1;
4101 const addr_table_entry *a2 = (const addr_table_entry *) x2;
4103 if (a1->kind != a2->kind)
4104 return 0;
4105 switch (a1->kind)
4107 case ate_kind_rtx:
4108 case ate_kind_rtx_dtprel:
4109 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4110 case ate_kind_label:
4111 return strcmp (a1->addr.label, a2->addr.label) == 0;
4112 default:
4113 gcc_unreachable ();
4117 /* Initialize an addr_table_entry. */
4119 void
4120 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4122 e->kind = kind;
4123 switch (kind)
4125 case ate_kind_rtx:
4126 case ate_kind_rtx_dtprel:
4127 e->addr.rtl = (rtx) addr;
4128 break;
4129 case ate_kind_label:
4130 e->addr.label = (char *) addr;
4131 break;
4133 e->refcount = 0;
4134 e->index = NO_INDEX_ASSIGNED;
4137 /* Add attr to the address table entry to the table. Defer setting an
4138 index until output time. */
4140 static addr_table_entry *
4141 add_addr_table_entry (void *addr, enum ate_kind kind)
4143 addr_table_entry *node;
4144 addr_table_entry finder;
4145 void **slot;
4147 gcc_assert (dwarf_split_debug_info);
4148 if (! addr_index_table)
4149 addr_index_table = htab_create_ggc (10, addr_table_entry_do_hash,
4150 addr_table_entry_eq, NULL);
4151 init_addr_table_entry (&finder, kind, addr);
4152 slot = htab_find_slot (addr_index_table, &finder, INSERT);
4154 if (*slot == HTAB_EMPTY_ENTRY)
4156 node = ggc_alloc_cleared_addr_table_entry ();
4157 init_addr_table_entry (node, kind, addr);
4158 *slot = node;
4160 else
4161 node = (addr_table_entry *) *slot;
4163 node->refcount++;
4164 return node;
4167 /* Remove an entry from the addr table by decrementing its refcount.
4168 Strictly, decrementing the refcount would be enough, but the
4169 assertion that the entry is actually in the table has found
4170 bugs. */
4172 static void
4173 remove_addr_table_entry (addr_table_entry *entry)
4175 addr_table_entry *node;
4177 gcc_assert (dwarf_split_debug_info && addr_index_table);
4178 node = (addr_table_entry *) htab_find (addr_index_table, entry);
4179 /* After an index is assigned, the table is frozen. */
4180 gcc_assert (node->refcount > 0 && node->index == NO_INDEX_ASSIGNED);
4181 node->refcount--;
4184 /* Given a location list, remove all addresses it refers to from the
4185 address_table. */
4187 static void
4188 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4190 for (; descr; descr = descr->dw_loc_next)
4191 if (descr->dw_loc_oprnd1.val_entry != NULL)
4193 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4194 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4198 /* A helper function for dwarf2out_finish called through
4199 htab_traverse. Assign an addr_table_entry its index. All entries
4200 must be collected into the table when this function is called,
4201 because the indexing code relies on htab_traverse to traverse nodes
4202 in the same order for each run. */
4204 static int
4205 index_addr_table_entry (void **h, void *v)
4207 addr_table_entry *node = (addr_table_entry *) *h;
4208 unsigned int *index = (unsigned int *) v;
4210 /* Don't index unreferenced nodes. */
4211 if (node->refcount == 0)
4212 return 1;
4214 gcc_assert(node->index == NO_INDEX_ASSIGNED);
4215 node->index = *index;
4216 *index += 1;
4218 return 1;
4221 /* Add an address constant attribute value to a DIE. When using
4222 dwarf_split_debug_info, address attributes in dies destined for the
4223 final executable should be direct references--setting the parameter
4224 force_direct ensures this behavior. */
4226 static inline void
4227 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4228 bool force_direct)
4230 dw_attr_node attr;
4232 attr.dw_attr = attr_kind;
4233 attr.dw_attr_val.val_class = dw_val_class_addr;
4234 attr.dw_attr_val.v.val_addr = addr;
4235 if (dwarf_split_debug_info && !force_direct)
4236 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4237 else
4238 attr.dw_attr_val.val_entry = NULL;
4239 add_dwarf_attr (die, &attr);
4242 /* Get the RTX from to an address DIE attribute. */
4244 static inline rtx
4245 AT_addr (dw_attr_ref a)
4247 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4248 return a->dw_attr_val.v.val_addr;
4251 /* Add a file attribute value to a DIE. */
4253 static inline void
4254 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4255 struct dwarf_file_data *fd)
4257 dw_attr_node attr;
4259 attr.dw_attr = attr_kind;
4260 attr.dw_attr_val.val_class = dw_val_class_file;
4261 attr.dw_attr_val.val_entry = NULL;
4262 attr.dw_attr_val.v.val_file = fd;
4263 add_dwarf_attr (die, &attr);
4266 /* Get the dwarf_file_data from a file DIE attribute. */
4268 static inline struct dwarf_file_data *
4269 AT_file (dw_attr_ref a)
4271 gcc_assert (a && AT_class (a) == dw_val_class_file);
4272 return a->dw_attr_val.v.val_file;
4275 /* Add a vms delta attribute value to a DIE. */
4277 static inline void
4278 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4279 const char *lbl1, const char *lbl2)
4281 dw_attr_node attr;
4283 attr.dw_attr = attr_kind;
4284 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4285 attr.dw_attr_val.val_entry = NULL;
4286 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4287 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4288 add_dwarf_attr (die, &attr);
4291 /* Add a label identifier attribute value to a DIE. */
4293 static inline void
4294 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4295 const char *lbl_id)
4297 dw_attr_node attr;
4299 attr.dw_attr = attr_kind;
4300 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4301 attr.dw_attr_val.val_entry = NULL;
4302 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4303 if (dwarf_split_debug_info)
4304 attr.dw_attr_val.val_entry
4305 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4306 ate_kind_label);
4307 add_dwarf_attr (die, &attr);
4310 /* Add a section offset attribute value to a DIE, an offset into the
4311 debug_line section. */
4313 static inline void
4314 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4315 const char *label)
4317 dw_attr_node attr;
4319 attr.dw_attr = attr_kind;
4320 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4321 attr.dw_attr_val.val_entry = NULL;
4322 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4323 add_dwarf_attr (die, &attr);
4326 /* Add a section offset attribute value to a DIE, an offset into the
4327 debug_macinfo section. */
4329 static inline void
4330 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4331 const char *label)
4333 dw_attr_node attr;
4335 attr.dw_attr = attr_kind;
4336 attr.dw_attr_val.val_class = dw_val_class_macptr;
4337 attr.dw_attr_val.val_entry = NULL;
4338 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4339 add_dwarf_attr (die, &attr);
4342 /* Add an offset attribute value to a DIE. */
4344 static inline void
4345 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4346 unsigned HOST_WIDE_INT offset)
4348 dw_attr_node attr;
4350 attr.dw_attr = attr_kind;
4351 attr.dw_attr_val.val_class = dw_val_class_offset;
4352 attr.dw_attr_val.val_entry = NULL;
4353 attr.dw_attr_val.v.val_offset = offset;
4354 add_dwarf_attr (die, &attr);
4357 /* Add a range_list attribute value to a DIE. When using
4358 dwarf_split_debug_info, address attributes in dies destined for the
4359 final executable should be direct references--setting the parameter
4360 force_direct ensures this behavior. */
4362 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4363 #define RELOCATED_OFFSET (NULL)
4365 static void
4366 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4367 long unsigned int offset, bool force_direct)
4369 dw_attr_node attr;
4371 attr.dw_attr = attr_kind;
4372 attr.dw_attr_val.val_class = dw_val_class_range_list;
4373 /* For the range_list attribute, use val_entry to store whether the
4374 offset should follow split-debug-info or normal semantics. This
4375 value is read in output_range_list_offset. */
4376 if (dwarf_split_debug_info && !force_direct)
4377 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4378 else
4379 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4380 attr.dw_attr_val.v.val_offset = offset;
4381 add_dwarf_attr (die, &attr);
4384 /* Return the start label of a delta attribute. */
4386 static inline const char *
4387 AT_vms_delta1 (dw_attr_ref a)
4389 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4390 return a->dw_attr_val.v.val_vms_delta.lbl1;
4393 /* Return the end label of a delta attribute. */
4395 static inline const char *
4396 AT_vms_delta2 (dw_attr_ref a)
4398 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4399 return a->dw_attr_val.v.val_vms_delta.lbl2;
4402 static inline const char *
4403 AT_lbl (dw_attr_ref a)
4405 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4406 || AT_class (a) == dw_val_class_lineptr
4407 || AT_class (a) == dw_val_class_macptr
4408 || AT_class (a) == dw_val_class_high_pc));
4409 return a->dw_attr_val.v.val_lbl_id;
4412 /* Get the attribute of type attr_kind. */
4414 static dw_attr_ref
4415 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4417 dw_attr_ref a;
4418 unsigned ix;
4419 dw_die_ref spec = NULL;
4421 if (! die)
4422 return NULL;
4424 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4425 if (a->dw_attr == attr_kind)
4426 return a;
4427 else if (a->dw_attr == DW_AT_specification
4428 || a->dw_attr == DW_AT_abstract_origin)
4429 spec = AT_ref (a);
4431 if (spec)
4432 return get_AT (spec, attr_kind);
4434 return NULL;
4437 /* Returns the parent of the declaration of DIE. */
4439 static dw_die_ref
4440 get_die_parent (dw_die_ref die)
4442 dw_die_ref t;
4444 if (!die)
4445 return NULL;
4447 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4448 || (t = get_AT_ref (die, DW_AT_specification)))
4449 die = t;
4451 return die->die_parent;
4454 /* Return the "low pc" attribute value, typically associated with a subprogram
4455 DIE. Return null if the "low pc" attribute is either not present, or if it
4456 cannot be represented as an assembler label identifier. */
4458 static inline const char *
4459 get_AT_low_pc (dw_die_ref die)
4461 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4463 return a ? AT_lbl (a) : NULL;
4466 /* Return the "high pc" attribute value, typically associated with a subprogram
4467 DIE. Return null if the "high pc" attribute is either not present, or if it
4468 cannot be represented as an assembler label identifier. */
4470 static inline const char *
4471 get_AT_hi_pc (dw_die_ref die)
4473 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4475 return a ? AT_lbl (a) : NULL;
4478 /* Return the value of the string attribute designated by ATTR_KIND, or
4479 NULL if it is not present. */
4481 static inline const char *
4482 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4484 dw_attr_ref a = get_AT (die, attr_kind);
4486 return a ? AT_string (a) : NULL;
4489 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4490 if it is not present. */
4492 static inline int
4493 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4495 dw_attr_ref a = get_AT (die, attr_kind);
4497 return a ? AT_flag (a) : 0;
4500 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4501 if it is not present. */
4503 static inline unsigned
4504 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4506 dw_attr_ref a = get_AT (die, attr_kind);
4508 return a ? AT_unsigned (a) : 0;
4511 static inline dw_die_ref
4512 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4514 dw_attr_ref a = get_AT (die, attr_kind);
4516 return a ? AT_ref (a) : NULL;
4519 static inline struct dwarf_file_data *
4520 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4522 dw_attr_ref a = get_AT (die, attr_kind);
4524 return a ? AT_file (a) : NULL;
4527 /* Return TRUE if the language is C++. */
4529 static inline bool
4530 is_cxx (void)
4532 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4534 return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
4537 /* Return TRUE if the language is Fortran. */
4539 static inline bool
4540 is_fortran (void)
4542 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4544 return (lang == DW_LANG_Fortran77
4545 || lang == DW_LANG_Fortran90
4546 || lang == DW_LANG_Fortran95);
4549 /* Return TRUE if the language is Ada. */
4551 static inline bool
4552 is_ada (void)
4554 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4556 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4559 /* Remove the specified attribute if present. */
4561 static void
4562 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4564 dw_attr_ref a;
4565 unsigned ix;
4567 if (! die)
4568 return;
4570 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4571 if (a->dw_attr == attr_kind)
4573 if (AT_class (a) == dw_val_class_str)
4574 if (a->dw_attr_val.v.val_str->refcount)
4575 a->dw_attr_val.v.val_str->refcount--;
4577 /* vec::ordered_remove should help reduce the number of abbrevs
4578 that are needed. */
4579 die->die_attr->ordered_remove (ix);
4580 return;
4584 /* Remove CHILD from its parent. PREV must have the property that
4585 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4587 static void
4588 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4590 gcc_assert (child->die_parent == prev->die_parent);
4591 gcc_assert (prev->die_sib == child);
4592 if (prev == child)
4594 gcc_assert (child->die_parent->die_child == child);
4595 prev = NULL;
4597 else
4598 prev->die_sib = child->die_sib;
4599 if (child->die_parent->die_child == child)
4600 child->die_parent->die_child = prev;
4603 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4604 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4606 static void
4607 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4609 dw_die_ref parent = old_child->die_parent;
4611 gcc_assert (parent == prev->die_parent);
4612 gcc_assert (prev->die_sib == old_child);
4614 new_child->die_parent = parent;
4615 if (prev == old_child)
4617 gcc_assert (parent->die_child == old_child);
4618 new_child->die_sib = new_child;
4620 else
4622 prev->die_sib = new_child;
4623 new_child->die_sib = old_child->die_sib;
4625 if (old_child->die_parent->die_child == old_child)
4626 old_child->die_parent->die_child = new_child;
4629 /* Move all children from OLD_PARENT to NEW_PARENT. */
4631 static void
4632 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4634 dw_die_ref c;
4635 new_parent->die_child = old_parent->die_child;
4636 old_parent->die_child = NULL;
4637 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4640 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4641 matches TAG. */
4643 static void
4644 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4646 dw_die_ref c;
4648 c = die->die_child;
4649 if (c) do {
4650 dw_die_ref prev = c;
4651 c = c->die_sib;
4652 while (c->die_tag == tag)
4654 remove_child_with_prev (c, prev);
4655 /* Might have removed every child. */
4656 if (c == c->die_sib)
4657 return;
4658 c = c->die_sib;
4660 } while (c != die->die_child);
4663 /* Add a CHILD_DIE as the last child of DIE. */
4665 static void
4666 add_child_die (dw_die_ref die, dw_die_ref child_die)
4668 /* FIXME this should probably be an assert. */
4669 if (! die || ! child_die)
4670 return;
4671 gcc_assert (die != child_die);
4673 child_die->die_parent = die;
4674 if (die->die_child)
4676 child_die->die_sib = die->die_child->die_sib;
4677 die->die_child->die_sib = child_die;
4679 else
4680 child_die->die_sib = child_die;
4681 die->die_child = child_die;
4684 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4685 is the specification, to the end of PARENT's list of children.
4686 This is done by removing and re-adding it. */
4688 static void
4689 splice_child_die (dw_die_ref parent, dw_die_ref child)
4691 dw_die_ref p;
4693 /* We want the declaration DIE from inside the class, not the
4694 specification DIE at toplevel. */
4695 if (child->die_parent != parent)
4697 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4699 if (tmp)
4700 child = tmp;
4703 gcc_assert (child->die_parent == parent
4704 || (child->die_parent
4705 == get_AT_ref (parent, DW_AT_specification)));
4707 for (p = child->die_parent->die_child; ; p = p->die_sib)
4708 if (p->die_sib == child)
4710 remove_child_with_prev (child, p);
4711 break;
4714 add_child_die (parent, child);
4717 /* Return a pointer to a newly created DIE node. */
4719 static inline dw_die_ref
4720 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4722 dw_die_ref die = ggc_alloc_cleared_die_node ();
4724 die->die_tag = tag_value;
4726 if (parent_die != NULL)
4727 add_child_die (parent_die, die);
4728 else
4730 limbo_die_node *limbo_node;
4732 limbo_node = ggc_alloc_cleared_limbo_die_node ();
4733 limbo_node->die = die;
4734 limbo_node->created_for = t;
4735 limbo_node->next = limbo_die_list;
4736 limbo_die_list = limbo_node;
4739 return die;
4742 /* Return the DIE associated with the given type specifier. */
4744 static inline dw_die_ref
4745 lookup_type_die (tree type)
4747 return TYPE_SYMTAB_DIE (type);
4750 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4751 anonymous type named by the typedef TYPE_DIE, return the DIE of the
4752 anonymous type instead the one of the naming typedef. */
4754 static inline dw_die_ref
4755 strip_naming_typedef (tree type, dw_die_ref type_die)
4757 if (type
4758 && TREE_CODE (type) == RECORD_TYPE
4759 && type_die
4760 && type_die->die_tag == DW_TAG_typedef
4761 && is_naming_typedef_decl (TYPE_NAME (type)))
4762 type_die = get_AT_ref (type_die, DW_AT_type);
4763 return type_die;
4766 /* Like lookup_type_die, but if type is an anonymous type named by a
4767 typedef[1], return the DIE of the anonymous type instead the one of
4768 the naming typedef. This is because in gen_typedef_die, we did
4769 equate the anonymous struct named by the typedef with the DIE of
4770 the naming typedef. So by default, lookup_type_die on an anonymous
4771 struct yields the DIE of the naming typedef.
4773 [1]: Read the comment of is_naming_typedef_decl to learn about what
4774 a naming typedef is. */
4776 static inline dw_die_ref
4777 lookup_type_die_strip_naming_typedef (tree type)
4779 dw_die_ref die = lookup_type_die (type);
4780 return strip_naming_typedef (type, die);
4783 /* Equate a DIE to a given type specifier. */
4785 static inline void
4786 equate_type_number_to_die (tree type, dw_die_ref type_die)
4788 TYPE_SYMTAB_DIE (type) = type_die;
4791 /* Returns a hash value for X (which really is a die_struct). */
4793 static hashval_t
4794 decl_die_table_hash (const void *x)
4796 return (hashval_t) ((const_dw_die_ref) x)->decl_id;
4799 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
4801 static int
4802 decl_die_table_eq (const void *x, const void *y)
4804 return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
4807 /* Return the DIE associated with a given declaration. */
4809 static inline dw_die_ref
4810 lookup_decl_die (tree decl)
4812 return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
4815 /* Returns a hash value for X (which really is a var_loc_list). */
4817 static hashval_t
4818 decl_loc_table_hash (const void *x)
4820 return (hashval_t) ((const var_loc_list *) x)->decl_id;
4823 /* Return nonzero if decl_id of var_loc_list X is the same as
4824 UID of decl *Y. */
4826 static int
4827 decl_loc_table_eq (const void *x, const void *y)
4829 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
4832 /* Return the var_loc list associated with a given declaration. */
4834 static inline var_loc_list *
4835 lookup_decl_loc (const_tree decl)
4837 if (!decl_loc_table)
4838 return NULL;
4839 return (var_loc_list *)
4840 htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
4843 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
4845 static hashval_t
4846 cached_dw_loc_list_table_hash (const void *x)
4848 return (hashval_t) ((const cached_dw_loc_list *) x)->decl_id;
4851 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
4852 UID of decl *Y. */
4854 static int
4855 cached_dw_loc_list_table_eq (const void *x, const void *y)
4857 return (((const cached_dw_loc_list *) x)->decl_id
4858 == DECL_UID ((const_tree) y));
4861 /* Equate a DIE to a particular declaration. */
4863 static void
4864 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
4866 unsigned int decl_id = DECL_UID (decl);
4867 void **slot;
4869 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
4870 *slot = decl_die;
4871 decl_die->decl_id = decl_id;
4874 /* Return how many bits covers PIECE EXPR_LIST. */
4876 static int
4877 decl_piece_bitsize (rtx piece)
4879 int ret = (int) GET_MODE (piece);
4880 if (ret)
4881 return ret;
4882 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
4883 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
4884 return INTVAL (XEXP (XEXP (piece, 0), 0));
4887 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
4889 static rtx *
4890 decl_piece_varloc_ptr (rtx piece)
4892 if ((int) GET_MODE (piece))
4893 return &XEXP (piece, 0);
4894 else
4895 return &XEXP (XEXP (piece, 0), 1);
4898 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
4899 Next is the chain of following piece nodes. */
4901 static rtx
4902 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
4904 if (bitsize <= (int) MAX_MACHINE_MODE)
4905 return alloc_EXPR_LIST (bitsize, loc_note, next);
4906 else
4907 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
4908 GEN_INT (bitsize),
4909 loc_note), next);
4912 /* Return rtx that should be stored into loc field for
4913 LOC_NOTE and BITPOS/BITSIZE. */
4915 static rtx
4916 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
4917 HOST_WIDE_INT bitsize)
4919 if (bitsize != -1)
4921 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
4922 if (bitpos != 0)
4923 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
4925 return loc_note;
4928 /* This function either modifies location piece list *DEST in
4929 place (if SRC and INNER is NULL), or copies location piece list
4930 *SRC to *DEST while modifying it. Location BITPOS is modified
4931 to contain LOC_NOTE, any pieces overlapping it are removed resp.
4932 not copied and if needed some padding around it is added.
4933 When modifying in place, DEST should point to EXPR_LIST where
4934 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
4935 to the start of the whole list and INNER points to the EXPR_LIST
4936 where earlier pieces cover PIECE_BITPOS bits. */
4938 static void
4939 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
4940 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
4941 HOST_WIDE_INT bitsize, rtx loc_note)
4943 int diff;
4944 bool copy = inner != NULL;
4946 if (copy)
4948 /* First copy all nodes preceding the current bitpos. */
4949 while (src != inner)
4951 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
4952 decl_piece_bitsize (*src), NULL_RTX);
4953 dest = &XEXP (*dest, 1);
4954 src = &XEXP (*src, 1);
4957 /* Add padding if needed. */
4958 if (bitpos != piece_bitpos)
4960 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
4961 copy ? NULL_RTX : *dest);
4962 dest = &XEXP (*dest, 1);
4964 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
4966 gcc_assert (!copy);
4967 /* A piece with correct bitpos and bitsize already exist,
4968 just update the location for it and return. */
4969 *decl_piece_varloc_ptr (*dest) = loc_note;
4970 return;
4972 /* Add the piece that changed. */
4973 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
4974 dest = &XEXP (*dest, 1);
4975 /* Skip over pieces that overlap it. */
4976 diff = bitpos - piece_bitpos + bitsize;
4977 if (!copy)
4978 src = dest;
4979 while (diff > 0 && *src)
4981 rtx piece = *src;
4982 diff -= decl_piece_bitsize (piece);
4983 if (copy)
4984 src = &XEXP (piece, 1);
4985 else
4987 *src = XEXP (piece, 1);
4988 free_EXPR_LIST_node (piece);
4991 /* Add padding if needed. */
4992 if (diff < 0 && *src)
4994 if (!copy)
4995 dest = src;
4996 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
4997 dest = &XEXP (*dest, 1);
4999 if (!copy)
5000 return;
5001 /* Finally copy all nodes following it. */
5002 while (*src)
5004 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5005 decl_piece_bitsize (*src), NULL_RTX);
5006 dest = &XEXP (*dest, 1);
5007 src = &XEXP (*src, 1);
5011 /* Add a variable location node to the linked list for DECL. */
5013 static struct var_loc_node *
5014 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5016 unsigned int decl_id;
5017 var_loc_list *temp;
5018 void **slot;
5019 struct var_loc_node *loc = NULL;
5020 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5022 if (DECL_DEBUG_EXPR_IS_FROM (decl))
5024 tree realdecl = DECL_DEBUG_EXPR (decl);
5025 if (realdecl
5026 && (handled_component_p (realdecl)
5027 || (TREE_CODE (realdecl) == MEM_REF
5028 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR)))
5030 HOST_WIDE_INT maxsize;
5031 tree innerdecl;
5032 innerdecl
5033 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5034 if (!DECL_P (innerdecl)
5035 || DECL_IGNORED_P (innerdecl)
5036 || TREE_STATIC (innerdecl)
5037 || bitsize <= 0
5038 || bitpos + bitsize > 256
5039 || bitsize != maxsize)
5040 return NULL;
5041 decl = innerdecl;
5045 decl_id = DECL_UID (decl);
5046 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5047 if (*slot == NULL)
5049 temp = ggc_alloc_cleared_var_loc_list ();
5050 temp->decl_id = decl_id;
5051 *slot = temp;
5053 else
5054 temp = (var_loc_list *) *slot;
5056 /* For PARM_DECLs try to keep around the original incoming value,
5057 even if that means we'll emit a zero-range .debug_loc entry. */
5058 if (temp->last
5059 && temp->first == temp->last
5060 && TREE_CODE (decl) == PARM_DECL
5061 && GET_CODE (temp->first->loc) == NOTE
5062 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5063 && DECL_INCOMING_RTL (decl)
5064 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5065 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5066 == GET_CODE (DECL_INCOMING_RTL (decl))
5067 && prev_real_insn (temp->first->loc) == NULL_RTX
5068 && (bitsize != -1
5069 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5070 NOTE_VAR_LOCATION_LOC (loc_note))
5071 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5072 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5074 loc = ggc_alloc_cleared_var_loc_node ();
5075 temp->first->next = loc;
5076 temp->last = loc;
5077 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5079 else if (temp->last)
5081 struct var_loc_node *last = temp->last, *unused = NULL;
5082 rtx *piece_loc = NULL, last_loc_note;
5083 int piece_bitpos = 0;
5084 if (last->next)
5086 last = last->next;
5087 gcc_assert (last->next == NULL);
5089 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5091 piece_loc = &last->loc;
5094 int cur_bitsize = decl_piece_bitsize (*piece_loc);
5095 if (piece_bitpos + cur_bitsize > bitpos)
5096 break;
5097 piece_bitpos += cur_bitsize;
5098 piece_loc = &XEXP (*piece_loc, 1);
5100 while (*piece_loc);
5102 /* TEMP->LAST here is either pointer to the last but one or
5103 last element in the chained list, LAST is pointer to the
5104 last element. */
5105 if (label && strcmp (last->label, label) == 0)
5107 /* For SRA optimized variables if there weren't any real
5108 insns since last note, just modify the last node. */
5109 if (piece_loc != NULL)
5111 adjust_piece_list (piece_loc, NULL, NULL,
5112 bitpos, piece_bitpos, bitsize, loc_note);
5113 return NULL;
5115 /* If the last note doesn't cover any instructions, remove it. */
5116 if (temp->last != last)
5118 temp->last->next = NULL;
5119 unused = last;
5120 last = temp->last;
5121 gcc_assert (strcmp (last->label, label) != 0);
5123 else
5125 gcc_assert (temp->first == temp->last
5126 || (temp->first->next == temp->last
5127 && TREE_CODE (decl) == PARM_DECL));
5128 memset (temp->last, '\0', sizeof (*temp->last));
5129 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5130 return temp->last;
5133 if (bitsize == -1 && NOTE_P (last->loc))
5134 last_loc_note = last->loc;
5135 else if (piece_loc != NULL
5136 && *piece_loc != NULL_RTX
5137 && piece_bitpos == bitpos
5138 && decl_piece_bitsize (*piece_loc) == bitsize)
5139 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5140 else
5141 last_loc_note = NULL_RTX;
5142 /* If the current location is the same as the end of the list,
5143 and either both or neither of the locations is uninitialized,
5144 we have nothing to do. */
5145 if (last_loc_note == NULL_RTX
5146 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5147 NOTE_VAR_LOCATION_LOC (loc_note)))
5148 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5149 != NOTE_VAR_LOCATION_STATUS (loc_note))
5150 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5151 == VAR_INIT_STATUS_UNINITIALIZED)
5152 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5153 == VAR_INIT_STATUS_UNINITIALIZED))))
5155 /* Add LOC to the end of list and update LAST. If the last
5156 element of the list has been removed above, reuse its
5157 memory for the new node, otherwise allocate a new one. */
5158 if (unused)
5160 loc = unused;
5161 memset (loc, '\0', sizeof (*loc));
5163 else
5164 loc = ggc_alloc_cleared_var_loc_node ();
5165 if (bitsize == -1 || piece_loc == NULL)
5166 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5167 else
5168 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5169 bitpos, piece_bitpos, bitsize, loc_note);
5170 last->next = loc;
5171 /* Ensure TEMP->LAST will point either to the new last but one
5172 element of the chain, or to the last element in it. */
5173 if (last != temp->last)
5174 temp->last = last;
5176 else if (unused)
5177 ggc_free (unused);
5179 else
5181 loc = ggc_alloc_cleared_var_loc_node ();
5182 temp->first = loc;
5183 temp->last = loc;
5184 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5186 return loc;
5189 /* Keep track of the number of spaces used to indent the
5190 output of the debugging routines that print the structure of
5191 the DIE internal representation. */
5192 static int print_indent;
5194 /* Indent the line the number of spaces given by print_indent. */
5196 static inline void
5197 print_spaces (FILE *outfile)
5199 fprintf (outfile, "%*s", print_indent, "");
5202 /* Print a type signature in hex. */
5204 static inline void
5205 print_signature (FILE *outfile, char *sig)
5207 int i;
5209 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5210 fprintf (outfile, "%02x", sig[i] & 0xff);
5213 /* Print the information associated with a given DIE, and its children.
5214 This routine is a debugging aid only. */
5216 static void
5217 print_die (dw_die_ref die, FILE *outfile)
5219 dw_attr_ref a;
5220 dw_die_ref c;
5221 unsigned ix;
5223 print_spaces (outfile);
5224 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5225 die->die_offset, dwarf_tag_name (die->die_tag),
5226 (void*) die);
5227 print_spaces (outfile);
5228 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5229 fprintf (outfile, " offset: %ld", die->die_offset);
5230 fprintf (outfile, " mark: %d\n", die->die_mark);
5232 if (die->comdat_type_p)
5234 print_spaces (outfile);
5235 fprintf (outfile, " signature: ");
5236 print_signature (outfile, die->die_id.die_type_node->signature);
5237 fprintf (outfile, "\n");
5240 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5242 print_spaces (outfile);
5243 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5245 switch (AT_class (a))
5247 case dw_val_class_addr:
5248 fprintf (outfile, "address");
5249 break;
5250 case dw_val_class_offset:
5251 fprintf (outfile, "offset");
5252 break;
5253 case dw_val_class_loc:
5254 fprintf (outfile, "location descriptor");
5255 break;
5256 case dw_val_class_loc_list:
5257 fprintf (outfile, "location list -> label:%s",
5258 AT_loc_list (a)->ll_symbol);
5259 break;
5260 case dw_val_class_range_list:
5261 fprintf (outfile, "range list");
5262 break;
5263 case dw_val_class_const:
5264 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5265 break;
5266 case dw_val_class_unsigned_const:
5267 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5268 break;
5269 case dw_val_class_const_double:
5270 fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
5271 HOST_WIDE_INT_PRINT_UNSIGNED")",
5272 a->dw_attr_val.v.val_double.high,
5273 a->dw_attr_val.v.val_double.low);
5274 break;
5275 case dw_val_class_vec:
5276 fprintf (outfile, "floating-point or vector constant");
5277 break;
5278 case dw_val_class_flag:
5279 fprintf (outfile, "%u", AT_flag (a));
5280 break;
5281 case dw_val_class_die_ref:
5282 if (AT_ref (a) != NULL)
5284 if (AT_ref (a)->comdat_type_p)
5286 fprintf (outfile, "die -> signature: ");
5287 print_signature (outfile,
5288 AT_ref (a)->die_id.die_type_node->signature);
5290 else if (AT_ref (a)->die_id.die_symbol)
5291 fprintf (outfile, "die -> label: %s",
5292 AT_ref (a)->die_id.die_symbol);
5293 else
5294 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5295 fprintf (outfile, " (%p)", (void *) AT_ref (a));
5297 else
5298 fprintf (outfile, "die -> <null>");
5299 break;
5300 case dw_val_class_vms_delta:
5301 fprintf (outfile, "delta: @slotcount(%s-%s)",
5302 AT_vms_delta2 (a), AT_vms_delta1 (a));
5303 break;
5304 case dw_val_class_lbl_id:
5305 case dw_val_class_lineptr:
5306 case dw_val_class_macptr:
5307 case dw_val_class_high_pc:
5308 fprintf (outfile, "label: %s", AT_lbl (a));
5309 break;
5310 case dw_val_class_str:
5311 if (AT_string (a) != NULL)
5312 fprintf (outfile, "\"%s\"", AT_string (a));
5313 else
5314 fprintf (outfile, "<null>");
5315 break;
5316 case dw_val_class_file:
5317 fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5318 AT_file (a)->emitted_number);
5319 break;
5320 case dw_val_class_data8:
5322 int i;
5324 for (i = 0; i < 8; i++)
5325 fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
5326 break;
5328 default:
5329 break;
5332 fprintf (outfile, "\n");
5335 if (die->die_child != NULL)
5337 print_indent += 4;
5338 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5339 print_indent -= 4;
5341 if (print_indent == 0)
5342 fprintf (outfile, "\n");
5345 /* Print the information collected for a given DIE. */
5347 DEBUG_FUNCTION void
5348 debug_dwarf_die (dw_die_ref die)
5350 print_die (die, stderr);
5353 /* Print all DWARF information collected for the compilation unit.
5354 This routine is a debugging aid only. */
5356 DEBUG_FUNCTION void
5357 debug_dwarf (void)
5359 print_indent = 0;
5360 print_die (comp_unit_die (), stderr);
5363 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5364 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5365 DIE that marks the start of the DIEs for this include file. */
5367 static dw_die_ref
5368 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5370 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5371 dw_die_ref new_unit = gen_compile_unit_die (filename);
5373 new_unit->die_sib = old_unit;
5374 return new_unit;
5377 /* Close an include-file CU and reopen the enclosing one. */
5379 static dw_die_ref
5380 pop_compile_unit (dw_die_ref old_unit)
5382 dw_die_ref new_unit = old_unit->die_sib;
5384 old_unit->die_sib = NULL;
5385 return new_unit;
5388 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5389 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5391 /* Calculate the checksum of a location expression. */
5393 static inline void
5394 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5396 int tem;
5398 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5399 CHECKSUM (tem);
5400 CHECKSUM (loc->dw_loc_oprnd1);
5401 CHECKSUM (loc->dw_loc_oprnd2);
5404 /* Calculate the checksum of an attribute. */
5406 static void
5407 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5409 dw_loc_descr_ref loc;
5410 rtx r;
5412 CHECKSUM (at->dw_attr);
5414 /* We don't care that this was compiled with a different compiler
5415 snapshot; if the output is the same, that's what matters. */
5416 if (at->dw_attr == DW_AT_producer)
5417 return;
5419 switch (AT_class (at))
5421 case dw_val_class_const:
5422 CHECKSUM (at->dw_attr_val.v.val_int);
5423 break;
5424 case dw_val_class_unsigned_const:
5425 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5426 break;
5427 case dw_val_class_const_double:
5428 CHECKSUM (at->dw_attr_val.v.val_double);
5429 break;
5430 case dw_val_class_vec:
5431 CHECKSUM (at->dw_attr_val.v.val_vec);
5432 break;
5433 case dw_val_class_flag:
5434 CHECKSUM (at->dw_attr_val.v.val_flag);
5435 break;
5436 case dw_val_class_str:
5437 CHECKSUM_STRING (AT_string (at));
5438 break;
5440 case dw_val_class_addr:
5441 r = AT_addr (at);
5442 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5443 CHECKSUM_STRING (XSTR (r, 0));
5444 break;
5446 case dw_val_class_offset:
5447 CHECKSUM (at->dw_attr_val.v.val_offset);
5448 break;
5450 case dw_val_class_loc:
5451 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5452 loc_checksum (loc, ctx);
5453 break;
5455 case dw_val_class_die_ref:
5456 die_checksum (AT_ref (at), ctx, mark);
5457 break;
5459 case dw_val_class_fde_ref:
5460 case dw_val_class_vms_delta:
5461 case dw_val_class_lbl_id:
5462 case dw_val_class_lineptr:
5463 case dw_val_class_macptr:
5464 case dw_val_class_high_pc:
5465 break;
5467 case dw_val_class_file:
5468 CHECKSUM_STRING (AT_file (at)->filename);
5469 break;
5471 case dw_val_class_data8:
5472 CHECKSUM (at->dw_attr_val.v.val_data8);
5473 break;
5475 default:
5476 break;
5480 /* Calculate the checksum of a DIE. */
5482 static void
5483 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5485 dw_die_ref c;
5486 dw_attr_ref a;
5487 unsigned ix;
5489 /* To avoid infinite recursion. */
5490 if (die->die_mark)
5492 CHECKSUM (die->die_mark);
5493 return;
5495 die->die_mark = ++(*mark);
5497 CHECKSUM (die->die_tag);
5499 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5500 attr_checksum (a, ctx, mark);
5502 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5505 #undef CHECKSUM
5506 #undef CHECKSUM_STRING
5508 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
5509 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5510 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5511 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5512 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5513 #define CHECKSUM_ATTR(FOO) \
5514 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5516 /* Calculate the checksum of a number in signed LEB128 format. */
5518 static void
5519 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5521 unsigned char byte;
5522 bool more;
5524 while (1)
5526 byte = (value & 0x7f);
5527 value >>= 7;
5528 more = !((value == 0 && (byte & 0x40) == 0)
5529 || (value == -1 && (byte & 0x40) != 0));
5530 if (more)
5531 byte |= 0x80;
5532 CHECKSUM (byte);
5533 if (!more)
5534 break;
5538 /* Calculate the checksum of a number in unsigned LEB128 format. */
5540 static void
5541 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5543 while (1)
5545 unsigned char byte = (value & 0x7f);
5546 value >>= 7;
5547 if (value != 0)
5548 /* More bytes to follow. */
5549 byte |= 0x80;
5550 CHECKSUM (byte);
5551 if (value == 0)
5552 break;
5556 /* Checksum the context of the DIE. This adds the names of any
5557 surrounding namespaces or structures to the checksum. */
5559 static void
5560 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5562 const char *name;
5563 dw_die_ref spec;
5564 int tag = die->die_tag;
5566 if (tag != DW_TAG_namespace
5567 && tag != DW_TAG_structure_type
5568 && tag != DW_TAG_class_type)
5569 return;
5571 name = get_AT_string (die, DW_AT_name);
5573 spec = get_AT_ref (die, DW_AT_specification);
5574 if (spec != NULL)
5575 die = spec;
5577 if (die->die_parent != NULL)
5578 checksum_die_context (die->die_parent, ctx);
5580 CHECKSUM_ULEB128 ('C');
5581 CHECKSUM_ULEB128 (tag);
5582 if (name != NULL)
5583 CHECKSUM_STRING (name);
5586 /* Calculate the checksum of a location expression. */
5588 static inline void
5589 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5591 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5592 were emitted as a DW_FORM_sdata instead of a location expression. */
5593 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5595 CHECKSUM_ULEB128 (DW_FORM_sdata);
5596 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5597 return;
5600 /* Otherwise, just checksum the raw location expression. */
5601 while (loc != NULL)
5603 CHECKSUM_ULEB128 (loc->dw_loc_opc);
5604 CHECKSUM (loc->dw_loc_oprnd1);
5605 CHECKSUM (loc->dw_loc_oprnd2);
5606 loc = loc->dw_loc_next;
5610 /* Calculate the checksum of an attribute. */
5612 static void
5613 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5614 struct md5_ctx *ctx, int *mark)
5616 dw_loc_descr_ref loc;
5617 rtx r;
5619 if (AT_class (at) == dw_val_class_die_ref)
5621 dw_die_ref target_die = AT_ref (at);
5623 /* For pointer and reference types, we checksum only the (qualified)
5624 name of the target type (if there is a name). For friend entries,
5625 we checksum only the (qualified) name of the target type or function.
5626 This allows the checksum to remain the same whether the target type
5627 is complete or not. */
5628 if ((at->dw_attr == DW_AT_type
5629 && (tag == DW_TAG_pointer_type
5630 || tag == DW_TAG_reference_type
5631 || tag == DW_TAG_rvalue_reference_type
5632 || tag == DW_TAG_ptr_to_member_type))
5633 || (at->dw_attr == DW_AT_friend
5634 && tag == DW_TAG_friend))
5636 dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5638 if (name_attr != NULL)
5640 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5642 if (decl == NULL)
5643 decl = target_die;
5644 CHECKSUM_ULEB128 ('N');
5645 CHECKSUM_ULEB128 (at->dw_attr);
5646 if (decl->die_parent != NULL)
5647 checksum_die_context (decl->die_parent, ctx);
5648 CHECKSUM_ULEB128 ('E');
5649 CHECKSUM_STRING (AT_string (name_attr));
5650 return;
5654 /* For all other references to another DIE, we check to see if the
5655 target DIE has already been visited. If it has, we emit a
5656 backward reference; if not, we descend recursively. */
5657 if (target_die->die_mark > 0)
5659 CHECKSUM_ULEB128 ('R');
5660 CHECKSUM_ULEB128 (at->dw_attr);
5661 CHECKSUM_ULEB128 (target_die->die_mark);
5663 else
5665 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5667 if (decl == NULL)
5668 decl = target_die;
5669 target_die->die_mark = ++(*mark);
5670 CHECKSUM_ULEB128 ('T');
5671 CHECKSUM_ULEB128 (at->dw_attr);
5672 if (decl->die_parent != NULL)
5673 checksum_die_context (decl->die_parent, ctx);
5674 die_checksum_ordered (target_die, ctx, mark);
5676 return;
5679 CHECKSUM_ULEB128 ('A');
5680 CHECKSUM_ULEB128 (at->dw_attr);
5682 switch (AT_class (at))
5684 case dw_val_class_const:
5685 CHECKSUM_ULEB128 (DW_FORM_sdata);
5686 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
5687 break;
5689 case dw_val_class_unsigned_const:
5690 CHECKSUM_ULEB128 (DW_FORM_sdata);
5691 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
5692 break;
5694 case dw_val_class_const_double:
5695 CHECKSUM_ULEB128 (DW_FORM_block);
5696 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
5697 CHECKSUM (at->dw_attr_val.v.val_double);
5698 break;
5700 case dw_val_class_vec:
5701 CHECKSUM_ULEB128 (DW_FORM_block);
5702 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
5703 CHECKSUM (at->dw_attr_val.v.val_vec);
5704 break;
5706 case dw_val_class_flag:
5707 CHECKSUM_ULEB128 (DW_FORM_flag);
5708 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
5709 break;
5711 case dw_val_class_str:
5712 CHECKSUM_ULEB128 (DW_FORM_string);
5713 CHECKSUM_STRING (AT_string (at));
5714 break;
5716 case dw_val_class_addr:
5717 r = AT_addr (at);
5718 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5719 CHECKSUM_ULEB128 (DW_FORM_string);
5720 CHECKSUM_STRING (XSTR (r, 0));
5721 break;
5723 case dw_val_class_offset:
5724 CHECKSUM_ULEB128 (DW_FORM_sdata);
5725 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
5726 break;
5728 case dw_val_class_loc:
5729 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5730 loc_checksum_ordered (loc, ctx);
5731 break;
5733 case dw_val_class_fde_ref:
5734 case dw_val_class_lbl_id:
5735 case dw_val_class_lineptr:
5736 case dw_val_class_macptr:
5737 case dw_val_class_high_pc:
5738 break;
5740 case dw_val_class_file:
5741 CHECKSUM_ULEB128 (DW_FORM_string);
5742 CHECKSUM_STRING (AT_file (at)->filename);
5743 break;
5745 case dw_val_class_data8:
5746 CHECKSUM (at->dw_attr_val.v.val_data8);
5747 break;
5749 default:
5750 break;
5754 struct checksum_attributes
5756 dw_attr_ref at_name;
5757 dw_attr_ref at_type;
5758 dw_attr_ref at_friend;
5759 dw_attr_ref at_accessibility;
5760 dw_attr_ref at_address_class;
5761 dw_attr_ref at_allocated;
5762 dw_attr_ref at_artificial;
5763 dw_attr_ref at_associated;
5764 dw_attr_ref at_binary_scale;
5765 dw_attr_ref at_bit_offset;
5766 dw_attr_ref at_bit_size;
5767 dw_attr_ref at_bit_stride;
5768 dw_attr_ref at_byte_size;
5769 dw_attr_ref at_byte_stride;
5770 dw_attr_ref at_const_value;
5771 dw_attr_ref at_containing_type;
5772 dw_attr_ref at_count;
5773 dw_attr_ref at_data_location;
5774 dw_attr_ref at_data_member_location;
5775 dw_attr_ref at_decimal_scale;
5776 dw_attr_ref at_decimal_sign;
5777 dw_attr_ref at_default_value;
5778 dw_attr_ref at_digit_count;
5779 dw_attr_ref at_discr;
5780 dw_attr_ref at_discr_list;
5781 dw_attr_ref at_discr_value;
5782 dw_attr_ref at_encoding;
5783 dw_attr_ref at_endianity;
5784 dw_attr_ref at_explicit;
5785 dw_attr_ref at_is_optional;
5786 dw_attr_ref at_location;
5787 dw_attr_ref at_lower_bound;
5788 dw_attr_ref at_mutable;
5789 dw_attr_ref at_ordering;
5790 dw_attr_ref at_picture_string;
5791 dw_attr_ref at_prototyped;
5792 dw_attr_ref at_small;
5793 dw_attr_ref at_segment;
5794 dw_attr_ref at_string_length;
5795 dw_attr_ref at_threads_scaled;
5796 dw_attr_ref at_upper_bound;
5797 dw_attr_ref at_use_location;
5798 dw_attr_ref at_use_UTF8;
5799 dw_attr_ref at_variable_parameter;
5800 dw_attr_ref at_virtuality;
5801 dw_attr_ref at_visibility;
5802 dw_attr_ref at_vtable_elem_location;
5805 /* Collect the attributes that we will want to use for the checksum. */
5807 static void
5808 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
5810 dw_attr_ref a;
5811 unsigned ix;
5813 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5815 switch (a->dw_attr)
5817 case DW_AT_name:
5818 attrs->at_name = a;
5819 break;
5820 case DW_AT_type:
5821 attrs->at_type = a;
5822 break;
5823 case DW_AT_friend:
5824 attrs->at_friend = a;
5825 break;
5826 case DW_AT_accessibility:
5827 attrs->at_accessibility = a;
5828 break;
5829 case DW_AT_address_class:
5830 attrs->at_address_class = a;
5831 break;
5832 case DW_AT_allocated:
5833 attrs->at_allocated = a;
5834 break;
5835 case DW_AT_artificial:
5836 attrs->at_artificial = a;
5837 break;
5838 case DW_AT_associated:
5839 attrs->at_associated = a;
5840 break;
5841 case DW_AT_binary_scale:
5842 attrs->at_binary_scale = a;
5843 break;
5844 case DW_AT_bit_offset:
5845 attrs->at_bit_offset = a;
5846 break;
5847 case DW_AT_bit_size:
5848 attrs->at_bit_size = a;
5849 break;
5850 case DW_AT_bit_stride:
5851 attrs->at_bit_stride = a;
5852 break;
5853 case DW_AT_byte_size:
5854 attrs->at_byte_size = a;
5855 break;
5856 case DW_AT_byte_stride:
5857 attrs->at_byte_stride = a;
5858 break;
5859 case DW_AT_const_value:
5860 attrs->at_const_value = a;
5861 break;
5862 case DW_AT_containing_type:
5863 attrs->at_containing_type = a;
5864 break;
5865 case DW_AT_count:
5866 attrs->at_count = a;
5867 break;
5868 case DW_AT_data_location:
5869 attrs->at_data_location = a;
5870 break;
5871 case DW_AT_data_member_location:
5872 attrs->at_data_member_location = a;
5873 break;
5874 case DW_AT_decimal_scale:
5875 attrs->at_decimal_scale = a;
5876 break;
5877 case DW_AT_decimal_sign:
5878 attrs->at_decimal_sign = a;
5879 break;
5880 case DW_AT_default_value:
5881 attrs->at_default_value = a;
5882 break;
5883 case DW_AT_digit_count:
5884 attrs->at_digit_count = a;
5885 break;
5886 case DW_AT_discr:
5887 attrs->at_discr = a;
5888 break;
5889 case DW_AT_discr_list:
5890 attrs->at_discr_list = a;
5891 break;
5892 case DW_AT_discr_value:
5893 attrs->at_discr_value = a;
5894 break;
5895 case DW_AT_encoding:
5896 attrs->at_encoding = a;
5897 break;
5898 case DW_AT_endianity:
5899 attrs->at_endianity = a;
5900 break;
5901 case DW_AT_explicit:
5902 attrs->at_explicit = a;
5903 break;
5904 case DW_AT_is_optional:
5905 attrs->at_is_optional = a;
5906 break;
5907 case DW_AT_location:
5908 attrs->at_location = a;
5909 break;
5910 case DW_AT_lower_bound:
5911 attrs->at_lower_bound = a;
5912 break;
5913 case DW_AT_mutable:
5914 attrs->at_mutable = a;
5915 break;
5916 case DW_AT_ordering:
5917 attrs->at_ordering = a;
5918 break;
5919 case DW_AT_picture_string:
5920 attrs->at_picture_string = a;
5921 break;
5922 case DW_AT_prototyped:
5923 attrs->at_prototyped = a;
5924 break;
5925 case DW_AT_small:
5926 attrs->at_small = a;
5927 break;
5928 case DW_AT_segment:
5929 attrs->at_segment = a;
5930 break;
5931 case DW_AT_string_length:
5932 attrs->at_string_length = a;
5933 break;
5934 case DW_AT_threads_scaled:
5935 attrs->at_threads_scaled = a;
5936 break;
5937 case DW_AT_upper_bound:
5938 attrs->at_upper_bound = a;
5939 break;
5940 case DW_AT_use_location:
5941 attrs->at_use_location = a;
5942 break;
5943 case DW_AT_use_UTF8:
5944 attrs->at_use_UTF8 = a;
5945 break;
5946 case DW_AT_variable_parameter:
5947 attrs->at_variable_parameter = a;
5948 break;
5949 case DW_AT_virtuality:
5950 attrs->at_virtuality = a;
5951 break;
5952 case DW_AT_visibility:
5953 attrs->at_visibility = a;
5954 break;
5955 case DW_AT_vtable_elem_location:
5956 attrs->at_vtable_elem_location = a;
5957 break;
5958 default:
5959 break;
5964 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
5966 static void
5967 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5969 dw_die_ref c;
5970 dw_die_ref decl;
5971 struct checksum_attributes attrs;
5973 CHECKSUM_ULEB128 ('D');
5974 CHECKSUM_ULEB128 (die->die_tag);
5976 memset (&attrs, 0, sizeof (attrs));
5978 decl = get_AT_ref (die, DW_AT_specification);
5979 if (decl != NULL)
5980 collect_checksum_attributes (&attrs, decl);
5981 collect_checksum_attributes (&attrs, die);
5983 CHECKSUM_ATTR (attrs.at_name);
5984 CHECKSUM_ATTR (attrs.at_accessibility);
5985 CHECKSUM_ATTR (attrs.at_address_class);
5986 CHECKSUM_ATTR (attrs.at_allocated);
5987 CHECKSUM_ATTR (attrs.at_artificial);
5988 CHECKSUM_ATTR (attrs.at_associated);
5989 CHECKSUM_ATTR (attrs.at_binary_scale);
5990 CHECKSUM_ATTR (attrs.at_bit_offset);
5991 CHECKSUM_ATTR (attrs.at_bit_size);
5992 CHECKSUM_ATTR (attrs.at_bit_stride);
5993 CHECKSUM_ATTR (attrs.at_byte_size);
5994 CHECKSUM_ATTR (attrs.at_byte_stride);
5995 CHECKSUM_ATTR (attrs.at_const_value);
5996 CHECKSUM_ATTR (attrs.at_containing_type);
5997 CHECKSUM_ATTR (attrs.at_count);
5998 CHECKSUM_ATTR (attrs.at_data_location);
5999 CHECKSUM_ATTR (attrs.at_data_member_location);
6000 CHECKSUM_ATTR (attrs.at_decimal_scale);
6001 CHECKSUM_ATTR (attrs.at_decimal_sign);
6002 CHECKSUM_ATTR (attrs.at_default_value);
6003 CHECKSUM_ATTR (attrs.at_digit_count);
6004 CHECKSUM_ATTR (attrs.at_discr);
6005 CHECKSUM_ATTR (attrs.at_discr_list);
6006 CHECKSUM_ATTR (attrs.at_discr_value);
6007 CHECKSUM_ATTR (attrs.at_encoding);
6008 CHECKSUM_ATTR (attrs.at_endianity);
6009 CHECKSUM_ATTR (attrs.at_explicit);
6010 CHECKSUM_ATTR (attrs.at_is_optional);
6011 CHECKSUM_ATTR (attrs.at_location);
6012 CHECKSUM_ATTR (attrs.at_lower_bound);
6013 CHECKSUM_ATTR (attrs.at_mutable);
6014 CHECKSUM_ATTR (attrs.at_ordering);
6015 CHECKSUM_ATTR (attrs.at_picture_string);
6016 CHECKSUM_ATTR (attrs.at_prototyped);
6017 CHECKSUM_ATTR (attrs.at_small);
6018 CHECKSUM_ATTR (attrs.at_segment);
6019 CHECKSUM_ATTR (attrs.at_string_length);
6020 CHECKSUM_ATTR (attrs.at_threads_scaled);
6021 CHECKSUM_ATTR (attrs.at_upper_bound);
6022 CHECKSUM_ATTR (attrs.at_use_location);
6023 CHECKSUM_ATTR (attrs.at_use_UTF8);
6024 CHECKSUM_ATTR (attrs.at_variable_parameter);
6025 CHECKSUM_ATTR (attrs.at_virtuality);
6026 CHECKSUM_ATTR (attrs.at_visibility);
6027 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6028 CHECKSUM_ATTR (attrs.at_type);
6029 CHECKSUM_ATTR (attrs.at_friend);
6031 /* Checksum the child DIEs, except for nested types and member functions. */
6032 c = die->die_child;
6033 if (c) do {
6034 dw_attr_ref name_attr;
6036 c = c->die_sib;
6037 name_attr = get_AT (c, DW_AT_name);
6038 if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
6039 && name_attr != NULL)
6041 CHECKSUM_ULEB128 ('S');
6042 CHECKSUM_ULEB128 (c->die_tag);
6043 CHECKSUM_STRING (AT_string (name_attr));
6045 else
6047 /* Mark this DIE so it gets processed when unmarking. */
6048 if (c->die_mark == 0)
6049 c->die_mark = -1;
6050 die_checksum_ordered (c, ctx, mark);
6052 } while (c != die->die_child);
6054 CHECKSUM_ULEB128 (0);
6057 #undef CHECKSUM
6058 #undef CHECKSUM_STRING
6059 #undef CHECKSUM_ATTR
6060 #undef CHECKSUM_LEB128
6061 #undef CHECKSUM_ULEB128
6063 /* Generate the type signature for DIE. This is computed by generating an
6064 MD5 checksum over the DIE's tag, its relevant attributes, and its
6065 children. Attributes that are references to other DIEs are processed
6066 by recursion, using the MARK field to prevent infinite recursion.
6067 If the DIE is nested inside a namespace or another type, we also
6068 need to include that context in the signature. The lower 64 bits
6069 of the resulting MD5 checksum comprise the signature. */
6071 static void
6072 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6074 int mark;
6075 const char *name;
6076 unsigned char checksum[16];
6077 struct md5_ctx ctx;
6078 dw_die_ref decl;
6079 dw_die_ref parent;
6081 name = get_AT_string (die, DW_AT_name);
6082 decl = get_AT_ref (die, DW_AT_specification);
6083 parent = get_die_parent (die);
6085 /* First, compute a signature for just the type name (and its surrounding
6086 context, if any. This is stored in the type unit DIE for link-time
6087 ODR (one-definition rule) checking. */
6089 if (is_cxx() && name != NULL)
6091 md5_init_ctx (&ctx);
6093 /* Checksum the names of surrounding namespaces and structures. */
6094 if (parent != NULL)
6095 checksum_die_context (parent, &ctx);
6097 md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
6098 md5_process_bytes (name, strlen (name) + 1, &ctx);
6099 md5_finish_ctx (&ctx, checksum);
6101 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6104 /* Next, compute the complete type signature. */
6106 md5_init_ctx (&ctx);
6107 mark = 1;
6108 die->die_mark = mark;
6110 /* Checksum the names of surrounding namespaces and structures. */
6111 if (parent != NULL)
6112 checksum_die_context (parent, &ctx);
6114 /* Checksum the DIE and its children. */
6115 die_checksum_ordered (die, &ctx, &mark);
6116 unmark_all_dies (die);
6117 md5_finish_ctx (&ctx, checksum);
6119 /* Store the signature in the type node and link the type DIE and the
6120 type node together. */
6121 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6122 DWARF_TYPE_SIGNATURE_SIZE);
6123 die->comdat_type_p = true;
6124 die->die_id.die_type_node = type_node;
6125 type_node->type_die = die;
6127 /* If the DIE is a specification, link its declaration to the type node
6128 as well. */
6129 if (decl != NULL)
6131 decl->comdat_type_p = true;
6132 decl->die_id.die_type_node = type_node;
6136 /* Do the location expressions look same? */
6137 static inline int
6138 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6140 return loc1->dw_loc_opc == loc2->dw_loc_opc
6141 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6142 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6145 /* Do the values look the same? */
6146 static int
6147 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6149 dw_loc_descr_ref loc1, loc2;
6150 rtx r1, r2;
6152 if (v1->val_class != v2->val_class)
6153 return 0;
6155 switch (v1->val_class)
6157 case dw_val_class_const:
6158 return v1->v.val_int == v2->v.val_int;
6159 case dw_val_class_unsigned_const:
6160 return v1->v.val_unsigned == v2->v.val_unsigned;
6161 case dw_val_class_const_double:
6162 return v1->v.val_double.high == v2->v.val_double.high
6163 && v1->v.val_double.low == v2->v.val_double.low;
6164 case dw_val_class_vec:
6165 if (v1->v.val_vec.length != v2->v.val_vec.length
6166 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6167 return 0;
6168 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6169 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6170 return 0;
6171 return 1;
6172 case dw_val_class_flag:
6173 return v1->v.val_flag == v2->v.val_flag;
6174 case dw_val_class_str:
6175 return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
6177 case dw_val_class_addr:
6178 r1 = v1->v.val_addr;
6179 r2 = v2->v.val_addr;
6180 if (GET_CODE (r1) != GET_CODE (r2))
6181 return 0;
6182 return !rtx_equal_p (r1, r2);
6184 case dw_val_class_offset:
6185 return v1->v.val_offset == v2->v.val_offset;
6187 case dw_val_class_loc:
6188 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6189 loc1 && loc2;
6190 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6191 if (!same_loc_p (loc1, loc2, mark))
6192 return 0;
6193 return !loc1 && !loc2;
6195 case dw_val_class_die_ref:
6196 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6198 case dw_val_class_fde_ref:
6199 case dw_val_class_vms_delta:
6200 case dw_val_class_lbl_id:
6201 case dw_val_class_lineptr:
6202 case dw_val_class_macptr:
6203 case dw_val_class_high_pc:
6204 return 1;
6206 case dw_val_class_file:
6207 return v1->v.val_file == v2->v.val_file;
6209 case dw_val_class_data8:
6210 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6212 default:
6213 return 1;
6217 /* Do the attributes look the same? */
6219 static int
6220 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6222 if (at1->dw_attr != at2->dw_attr)
6223 return 0;
6225 /* We don't care that this was compiled with a different compiler
6226 snapshot; if the output is the same, that's what matters. */
6227 if (at1->dw_attr == DW_AT_producer)
6228 return 1;
6230 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6233 /* Do the dies look the same? */
6235 static int
6236 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6238 dw_die_ref c1, c2;
6239 dw_attr_ref a1;
6240 unsigned ix;
6242 /* To avoid infinite recursion. */
6243 if (die1->die_mark)
6244 return die1->die_mark == die2->die_mark;
6245 die1->die_mark = die2->die_mark = ++(*mark);
6247 if (die1->die_tag != die2->die_tag)
6248 return 0;
6250 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6251 return 0;
6253 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6254 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6255 return 0;
6257 c1 = die1->die_child;
6258 c2 = die2->die_child;
6259 if (! c1)
6261 if (c2)
6262 return 0;
6264 else
6265 for (;;)
6267 if (!same_die_p (c1, c2, mark))
6268 return 0;
6269 c1 = c1->die_sib;
6270 c2 = c2->die_sib;
6271 if (c1 == die1->die_child)
6273 if (c2 == die2->die_child)
6274 break;
6275 else
6276 return 0;
6280 return 1;
6283 /* Do the dies look the same? Wrapper around same_die_p. */
6285 static int
6286 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6288 int mark = 0;
6289 int ret = same_die_p (die1, die2, &mark);
6291 unmark_all_dies (die1);
6292 unmark_all_dies (die2);
6294 return ret;
6297 /* The prefix to attach to symbols on DIEs in the current comdat debug
6298 info section. */
6299 static const char *comdat_symbol_id;
6301 /* The index of the current symbol within the current comdat CU. */
6302 static unsigned int comdat_symbol_number;
6304 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6305 children, and set comdat_symbol_id accordingly. */
6307 static void
6308 compute_section_prefix (dw_die_ref unit_die)
6310 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6311 const char *base = die_name ? lbasename (die_name) : "anonymous";
6312 char *name = XALLOCAVEC (char, strlen (base) + 64);
6313 char *p;
6314 int i, mark;
6315 unsigned char checksum[16];
6316 struct md5_ctx ctx;
6318 /* Compute the checksum of the DIE, then append part of it as hex digits to
6319 the name filename of the unit. */
6321 md5_init_ctx (&ctx);
6322 mark = 0;
6323 die_checksum (unit_die, &ctx, &mark);
6324 unmark_all_dies (unit_die);
6325 md5_finish_ctx (&ctx, checksum);
6327 sprintf (name, "%s.", base);
6328 clean_symbol_name (name);
6330 p = name + strlen (name);
6331 for (i = 0; i < 4; i++)
6333 sprintf (p, "%.2x", checksum[i]);
6334 p += 2;
6337 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6338 comdat_symbol_number = 0;
6341 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6343 static int
6344 is_type_die (dw_die_ref die)
6346 switch (die->die_tag)
6348 case DW_TAG_array_type:
6349 case DW_TAG_class_type:
6350 case DW_TAG_interface_type:
6351 case DW_TAG_enumeration_type:
6352 case DW_TAG_pointer_type:
6353 case DW_TAG_reference_type:
6354 case DW_TAG_rvalue_reference_type:
6355 case DW_TAG_string_type:
6356 case DW_TAG_structure_type:
6357 case DW_TAG_subroutine_type:
6358 case DW_TAG_union_type:
6359 case DW_TAG_ptr_to_member_type:
6360 case DW_TAG_set_type:
6361 case DW_TAG_subrange_type:
6362 case DW_TAG_base_type:
6363 case DW_TAG_const_type:
6364 case DW_TAG_file_type:
6365 case DW_TAG_packed_type:
6366 case DW_TAG_volatile_type:
6367 case DW_TAG_typedef:
6368 return 1;
6369 default:
6370 return 0;
6374 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6375 Basically, we want to choose the bits that are likely to be shared between
6376 compilations (types) and leave out the bits that are specific to individual
6377 compilations (functions). */
6379 static int
6380 is_comdat_die (dw_die_ref c)
6382 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6383 we do for stabs. The advantage is a greater likelihood of sharing between
6384 objects that don't include headers in the same order (and therefore would
6385 put the base types in a different comdat). jason 8/28/00 */
6387 if (c->die_tag == DW_TAG_base_type)
6388 return 0;
6390 if (c->die_tag == DW_TAG_pointer_type
6391 || c->die_tag == DW_TAG_reference_type
6392 || c->die_tag == DW_TAG_rvalue_reference_type
6393 || c->die_tag == DW_TAG_const_type
6394 || c->die_tag == DW_TAG_volatile_type)
6396 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6398 return t ? is_comdat_die (t) : 0;
6401 return is_type_die (c);
6404 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6405 compilation unit. */
6407 static int
6408 is_symbol_die (dw_die_ref c)
6410 return (is_type_die (c)
6411 || is_declaration_die (c)
6412 || c->die_tag == DW_TAG_namespace
6413 || c->die_tag == DW_TAG_module);
6416 /* Returns true iff C is a compile-unit DIE. */
6418 static inline bool
6419 is_cu_die (dw_die_ref c)
6421 return c && c->die_tag == DW_TAG_compile_unit;
6424 /* Returns true iff C is a unit DIE of some sort. */
6426 static inline bool
6427 is_unit_die (dw_die_ref c)
6429 return c && (c->die_tag == DW_TAG_compile_unit
6430 || c->die_tag == DW_TAG_partial_unit
6431 || c->die_tag == DW_TAG_type_unit);
6434 /* Returns true iff C is a namespace DIE. */
6436 static inline bool
6437 is_namespace_die (dw_die_ref c)
6439 return c && c->die_tag == DW_TAG_namespace;
6442 /* Returns true iff C is a class or structure DIE. */
6444 static inline bool
6445 is_class_die (dw_die_ref c)
6447 return c && (c->die_tag == DW_TAG_class_type
6448 || c->die_tag == DW_TAG_structure_type);
6451 static char *
6452 gen_internal_sym (const char *prefix)
6454 char buf[256];
6456 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6457 return xstrdup (buf);
6460 /* Assign symbols to all worthy DIEs under DIE. */
6462 static void
6463 assign_symbol_names (dw_die_ref die)
6465 dw_die_ref c;
6467 if (is_symbol_die (die) && !die->comdat_type_p)
6469 if (comdat_symbol_id)
6471 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6473 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6474 comdat_symbol_id, comdat_symbol_number++);
6475 die->die_id.die_symbol = xstrdup (p);
6477 else
6478 die->die_id.die_symbol = gen_internal_sym ("LDIE");
6481 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6484 struct cu_hash_table_entry
6486 dw_die_ref cu;
6487 unsigned min_comdat_num, max_comdat_num;
6488 struct cu_hash_table_entry *next;
6491 /* Routines to manipulate hash table of CUs. */
6492 static hashval_t
6493 htab_cu_hash (const void *of)
6495 const struct cu_hash_table_entry *const entry =
6496 (const struct cu_hash_table_entry *) of;
6498 return htab_hash_string (entry->cu->die_id.die_symbol);
6501 static int
6502 htab_cu_eq (const void *of1, const void *of2)
6504 const struct cu_hash_table_entry *const entry1 =
6505 (const struct cu_hash_table_entry *) of1;
6506 const struct die_struct *const entry2 = (const struct die_struct *) of2;
6508 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6511 static void
6512 htab_cu_del (void *what)
6514 struct cu_hash_table_entry *next,
6515 *entry = (struct cu_hash_table_entry *) what;
6517 while (entry)
6519 next = entry->next;
6520 free (entry);
6521 entry = next;
6525 /* Check whether we have already seen this CU and set up SYM_NUM
6526 accordingly. */
6527 static int
6528 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
6530 struct cu_hash_table_entry dummy;
6531 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6533 dummy.max_comdat_num = 0;
6535 slot = (struct cu_hash_table_entry **)
6536 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
6537 INSERT);
6538 entry = *slot;
6540 for (; entry; last = entry, entry = entry->next)
6542 if (same_die_p_wrap (cu, entry->cu))
6543 break;
6546 if (entry)
6548 *sym_num = entry->min_comdat_num;
6549 return 1;
6552 entry = XCNEW (struct cu_hash_table_entry);
6553 entry->cu = cu;
6554 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6555 entry->next = *slot;
6556 *slot = entry;
6558 return 0;
6561 /* Record SYM_NUM to record of CU in HTABLE. */
6562 static void
6563 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
6565 struct cu_hash_table_entry **slot, *entry;
6567 slot = (struct cu_hash_table_entry **)
6568 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
6569 NO_INSERT);
6570 entry = *slot;
6572 entry->max_comdat_num = sym_num;
6575 /* Traverse the DIE (which is always comp_unit_die), and set up
6576 additional compilation units for each of the include files we see
6577 bracketed by BINCL/EINCL. */
6579 static void
6580 break_out_includes (dw_die_ref die)
6582 dw_die_ref c;
6583 dw_die_ref unit = NULL;
6584 limbo_die_node *node, **pnode;
6585 htab_t cu_hash_table;
6587 c = die->die_child;
6588 if (c) do {
6589 dw_die_ref prev = c;
6590 c = c->die_sib;
6591 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6592 || (unit && is_comdat_die (c)))
6594 dw_die_ref next = c->die_sib;
6596 /* This DIE is for a secondary CU; remove it from the main one. */
6597 remove_child_with_prev (c, prev);
6599 if (c->die_tag == DW_TAG_GNU_BINCL)
6600 unit = push_new_compile_unit (unit, c);
6601 else if (c->die_tag == DW_TAG_GNU_EINCL)
6602 unit = pop_compile_unit (unit);
6603 else
6604 add_child_die (unit, c);
6605 c = next;
6606 if (c == die->die_child)
6607 break;
6609 } while (c != die->die_child);
6611 #if 0
6612 /* We can only use this in debugging, since the frontend doesn't check
6613 to make sure that we leave every include file we enter. */
6614 gcc_assert (!unit);
6615 #endif
6617 assign_symbol_names (die);
6618 cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6619 for (node = limbo_die_list, pnode = &limbo_die_list;
6620 node;
6621 node = node->next)
6623 int is_dupl;
6625 compute_section_prefix (node->die);
6626 is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6627 &comdat_symbol_number);
6628 assign_symbol_names (node->die);
6629 if (is_dupl)
6630 *pnode = node->next;
6631 else
6633 pnode = &node->next;
6634 record_comdat_symbol_number (node->die, cu_hash_table,
6635 comdat_symbol_number);
6638 htab_delete (cu_hash_table);
6641 /* Return non-zero if this DIE is a declaration. */
6643 static int
6644 is_declaration_die (dw_die_ref die)
6646 dw_attr_ref a;
6647 unsigned ix;
6649 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6650 if (a->dw_attr == DW_AT_declaration)
6651 return 1;
6653 return 0;
6656 /* Return non-zero if this DIE is nested inside a subprogram. */
6658 static int
6659 is_nested_in_subprogram (dw_die_ref die)
6661 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
6663 if (decl == NULL)
6664 decl = die;
6665 return local_scope_p (decl);
6668 /* Return non-zero if this DIE contains a defining declaration of a
6669 subprogram. */
6671 static int
6672 contains_subprogram_definition (dw_die_ref die)
6674 dw_die_ref c;
6676 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
6677 return 1;
6678 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition(c)) return 1);
6679 return 0;
6682 /* Return non-zero if this is a type DIE that should be moved to a
6683 COMDAT .debug_types section. */
6685 static int
6686 should_move_die_to_comdat (dw_die_ref die)
6688 switch (die->die_tag)
6690 case DW_TAG_class_type:
6691 case DW_TAG_structure_type:
6692 case DW_TAG_enumeration_type:
6693 case DW_TAG_union_type:
6694 /* Don't move declarations, inlined instances, or types nested in a
6695 subprogram. */
6696 if (is_declaration_die (die)
6697 || get_AT (die, DW_AT_abstract_origin)
6698 || is_nested_in_subprogram (die))
6699 return 0;
6700 /* A type definition should never contain a subprogram definition. */
6701 gcc_assert (!contains_subprogram_definition (die));
6702 return 1;
6703 case DW_TAG_array_type:
6704 case DW_TAG_interface_type:
6705 case DW_TAG_pointer_type:
6706 case DW_TAG_reference_type:
6707 case DW_TAG_rvalue_reference_type:
6708 case DW_TAG_string_type:
6709 case DW_TAG_subroutine_type:
6710 case DW_TAG_ptr_to_member_type:
6711 case DW_TAG_set_type:
6712 case DW_TAG_subrange_type:
6713 case DW_TAG_base_type:
6714 case DW_TAG_const_type:
6715 case DW_TAG_file_type:
6716 case DW_TAG_packed_type:
6717 case DW_TAG_volatile_type:
6718 case DW_TAG_typedef:
6719 default:
6720 return 0;
6724 /* Make a clone of DIE. */
6726 static dw_die_ref
6727 clone_die (dw_die_ref die)
6729 dw_die_ref clone;
6730 dw_attr_ref a;
6731 unsigned ix;
6733 clone = ggc_alloc_cleared_die_node ();
6734 clone->die_tag = die->die_tag;
6736 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6737 add_dwarf_attr (clone, a);
6739 return clone;
6742 /* Make a clone of the tree rooted at DIE. */
6744 static dw_die_ref
6745 clone_tree (dw_die_ref die)
6747 dw_die_ref c;
6748 dw_die_ref clone = clone_die (die);
6750 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
6752 return clone;
6755 /* Make a clone of DIE as a declaration. */
6757 static dw_die_ref
6758 clone_as_declaration (dw_die_ref die)
6760 dw_die_ref clone;
6761 dw_die_ref decl;
6762 dw_attr_ref a;
6763 unsigned ix;
6765 /* If the DIE is already a declaration, just clone it. */
6766 if (is_declaration_die (die))
6767 return clone_die (die);
6769 /* If the DIE is a specification, just clone its declaration DIE. */
6770 decl = get_AT_ref (die, DW_AT_specification);
6771 if (decl != NULL)
6773 clone = clone_die (decl);
6774 if (die->comdat_type_p)
6775 add_AT_die_ref (clone, DW_AT_signature, die);
6776 return clone;
6779 clone = ggc_alloc_cleared_die_node ();
6780 clone->die_tag = die->die_tag;
6782 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6784 /* We don't want to copy over all attributes.
6785 For example we don't want DW_AT_byte_size because otherwise we will no
6786 longer have a declaration and GDB will treat it as a definition. */
6788 switch (a->dw_attr)
6790 case DW_AT_artificial:
6791 case DW_AT_containing_type:
6792 case DW_AT_external:
6793 case DW_AT_name:
6794 case DW_AT_type:
6795 case DW_AT_virtuality:
6796 case DW_AT_linkage_name:
6797 case DW_AT_MIPS_linkage_name:
6798 add_dwarf_attr (clone, a);
6799 break;
6800 case DW_AT_byte_size:
6801 default:
6802 break;
6806 if (die->comdat_type_p)
6807 add_AT_die_ref (clone, DW_AT_signature, die);
6809 add_AT_flag (clone, DW_AT_declaration, 1);
6810 return clone;
6813 /* Copy the declaration context to the new type unit DIE. This includes
6814 any surrounding namespace or type declarations. If the DIE has an
6815 AT_specification attribute, it also includes attributes and children
6816 attached to the specification, and returns a pointer to the original
6817 parent of the declaration DIE. Returns NULL otherwise. */
6819 static dw_die_ref
6820 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
6822 dw_die_ref decl;
6823 dw_die_ref new_decl;
6824 dw_die_ref orig_parent = NULL;
6826 decl = get_AT_ref (die, DW_AT_specification);
6827 if (decl == NULL)
6828 decl = die;
6829 else
6831 unsigned ix;
6832 dw_die_ref c;
6833 dw_attr_ref a;
6835 /* The original DIE will be changed to a declaration, and must
6836 be moved to be a child of the original declaration DIE. */
6837 orig_parent = decl->die_parent;
6839 /* Copy the type node pointer from the new DIE to the original
6840 declaration DIE so we can forward references later. */
6841 decl->comdat_type_p = true;
6842 decl->die_id.die_type_node = die->die_id.die_type_node;
6844 remove_AT (die, DW_AT_specification);
6846 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
6848 if (a->dw_attr != DW_AT_name
6849 && a->dw_attr != DW_AT_declaration
6850 && a->dw_attr != DW_AT_external)
6851 add_dwarf_attr (die, a);
6854 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
6857 if (decl->die_parent != NULL
6858 && !is_unit_die (decl->die_parent))
6860 new_decl = copy_ancestor_tree (unit, decl, NULL);
6861 if (new_decl != NULL)
6863 remove_AT (new_decl, DW_AT_signature);
6864 add_AT_specification (die, new_decl);
6868 return orig_parent;
6871 /* Generate the skeleton ancestor tree for the given NODE, then clone
6872 the DIE and add the clone into the tree. */
6874 static void
6875 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
6877 if (node->new_die != NULL)
6878 return;
6880 node->new_die = clone_as_declaration (node->old_die);
6882 if (node->parent != NULL)
6884 generate_skeleton_ancestor_tree (node->parent);
6885 add_child_die (node->parent->new_die, node->new_die);
6889 /* Generate a skeleton tree of DIEs containing any declarations that are
6890 found in the original tree. We traverse the tree looking for declaration
6891 DIEs, and construct the skeleton from the bottom up whenever we find one. */
6893 static void
6894 generate_skeleton_bottom_up (skeleton_chain_node *parent)
6896 skeleton_chain_node node;
6897 dw_die_ref c;
6898 dw_die_ref first;
6899 dw_die_ref prev = NULL;
6900 dw_die_ref next = NULL;
6902 node.parent = parent;
6904 first = c = parent->old_die->die_child;
6905 if (c)
6906 next = c->die_sib;
6907 if (c) do {
6908 if (prev == NULL || prev->die_sib == c)
6909 prev = c;
6910 c = next;
6911 next = (c == first ? NULL : c->die_sib);
6912 node.old_die = c;
6913 node.new_die = NULL;
6914 if (is_declaration_die (c))
6916 /* Clone the existing DIE, move the original to the skeleton
6917 tree (which is in the main CU), and put the clone, with
6918 all the original's children, where the original came from. */
6919 dw_die_ref clone = clone_die (c);
6920 move_all_children (c, clone);
6922 replace_child (c, clone, prev);
6923 generate_skeleton_ancestor_tree (parent);
6924 add_child_die (parent->new_die, c);
6925 node.new_die = c;
6926 c = clone;
6928 generate_skeleton_bottom_up (&node);
6929 } while (next != NULL);
6932 /* Wrapper function for generate_skeleton_bottom_up. */
6934 static dw_die_ref
6935 generate_skeleton (dw_die_ref die)
6937 skeleton_chain_node node;
6939 node.old_die = die;
6940 node.new_die = NULL;
6941 node.parent = NULL;
6943 /* If this type definition is nested inside another type,
6944 always leave at least a declaration in its place. */
6945 if (die->die_parent != NULL && is_type_die (die->die_parent))
6946 node.new_die = clone_as_declaration (die);
6948 generate_skeleton_bottom_up (&node);
6949 return node.new_die;
6952 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
6953 declaration. The original DIE is moved to a new compile unit so that
6954 existing references to it follow it to the new location. If any of the
6955 original DIE's descendants is a declaration, we need to replace the
6956 original DIE with a skeleton tree and move the declarations back into the
6957 skeleton tree. */
6959 static dw_die_ref
6960 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
6961 dw_die_ref prev)
6963 dw_die_ref skeleton, orig_parent;
6965 /* Copy the declaration context to the type unit DIE. If the returned
6966 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
6967 that DIE. */
6968 orig_parent = copy_declaration_context (unit, child);
6970 skeleton = generate_skeleton (child);
6971 if (skeleton == NULL)
6972 remove_child_with_prev (child, prev);
6973 else
6975 skeleton->comdat_type_p = true;
6976 skeleton->die_id.die_type_node = child->die_id.die_type_node;
6978 /* If the original DIE was a specification, we need to put
6979 the skeleton under the parent DIE of the declaration.
6980 This leaves the original declaration in the tree, but
6981 it will be pruned later since there are no longer any
6982 references to it. */
6983 if (orig_parent != NULL)
6985 remove_child_with_prev (child, prev);
6986 add_child_die (orig_parent, skeleton);
6988 else
6989 replace_child (child, skeleton, prev);
6992 return skeleton;
6995 /* Traverse the DIE and set up additional .debug_types sections for each
6996 type worthy of being placed in a COMDAT section. */
6998 static void
6999 break_out_comdat_types (dw_die_ref die)
7001 dw_die_ref c;
7002 dw_die_ref first;
7003 dw_die_ref prev = NULL;
7004 dw_die_ref next = NULL;
7005 dw_die_ref unit = NULL;
7007 first = c = die->die_child;
7008 if (c)
7009 next = c->die_sib;
7010 if (c) do {
7011 if (prev == NULL || prev->die_sib == c)
7012 prev = c;
7013 c = next;
7014 next = (c == first ? NULL : c->die_sib);
7015 if (should_move_die_to_comdat (c))
7017 dw_die_ref replacement;
7018 comdat_type_node_ref type_node;
7020 /* Create a new type unit DIE as the root for the new tree, and
7021 add it to the list of comdat types. */
7022 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7023 add_AT_unsigned (unit, DW_AT_language,
7024 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7025 type_node = ggc_alloc_cleared_comdat_type_node ();
7026 type_node->root_die = unit;
7027 type_node->next = comdat_type_list;
7028 comdat_type_list = type_node;
7030 /* Generate the type signature. */
7031 generate_type_signature (c, type_node);
7033 /* Copy the declaration context, attributes, and children of the
7034 declaration into the new type unit DIE, then remove this DIE
7035 from the main CU (or replace it with a skeleton if necessary). */
7036 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7037 type_node->skeleton_die = replacement;
7039 /* Break out nested types into their own type units. */
7040 break_out_comdat_types (c);
7042 /* Add the DIE to the new compunit. */
7043 add_child_die (unit, c);
7045 if (replacement != NULL)
7046 c = replacement;
7048 else if (c->die_tag == DW_TAG_namespace
7049 || c->die_tag == DW_TAG_class_type
7050 || c->die_tag == DW_TAG_structure_type
7051 || c->die_tag == DW_TAG_union_type)
7053 /* Look for nested types that can be broken out. */
7054 break_out_comdat_types (c);
7056 } while (next != NULL);
7059 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7061 struct decl_table_entry
7063 dw_die_ref orig;
7064 dw_die_ref copy;
7067 /* Routines to manipulate hash table of copied declarations. */
7069 static hashval_t
7070 htab_decl_hash (const void *of)
7072 const struct decl_table_entry *const entry =
7073 (const struct decl_table_entry *) of;
7075 return htab_hash_pointer (entry->orig);
7078 static int
7079 htab_decl_eq (const void *of1, const void *of2)
7081 const struct decl_table_entry *const entry1 =
7082 (const struct decl_table_entry *) of1;
7083 const struct die_struct *const entry2 = (const struct die_struct *) of2;
7085 return entry1->orig == entry2;
7088 static void
7089 htab_decl_del (void *what)
7091 struct decl_table_entry *entry = (struct decl_table_entry *) what;
7093 free (entry);
7096 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7097 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7098 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7099 to check if the ancestor has already been copied into UNIT. */
7101 static dw_die_ref
7102 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
7104 dw_die_ref parent = die->die_parent;
7105 dw_die_ref new_parent = unit;
7106 dw_die_ref copy;
7107 void **slot = NULL;
7108 struct decl_table_entry *entry = NULL;
7110 if (decl_table)
7112 /* Check if the entry has already been copied to UNIT. */
7113 slot = htab_find_slot_with_hash (decl_table, die,
7114 htab_hash_pointer (die), INSERT);
7115 if (*slot != HTAB_EMPTY_ENTRY)
7117 entry = (struct decl_table_entry *) *slot;
7118 return entry->copy;
7121 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7122 entry = XCNEW (struct decl_table_entry);
7123 entry->orig = die;
7124 entry->copy = NULL;
7125 *slot = entry;
7128 if (parent != NULL)
7130 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7131 if (spec != NULL)
7132 parent = spec;
7133 if (!is_unit_die (parent))
7134 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7137 copy = clone_as_declaration (die);
7138 add_child_die (new_parent, copy);
7140 if (decl_table != NULL)
7142 /* Record the pointer to the copy. */
7143 entry->copy = copy;
7146 return copy;
7149 /* Like clone_tree, but additionally enter all the children into
7150 the hash table decl_table. */
7152 static dw_die_ref
7153 clone_tree_hash (dw_die_ref die, htab_t decl_table)
7155 dw_die_ref c;
7156 dw_die_ref clone = clone_die (die);
7157 struct decl_table_entry *entry;
7158 void **slot = htab_find_slot_with_hash (decl_table, die,
7159 htab_hash_pointer (die), INSERT);
7160 /* Assert that DIE isn't in the hash table yet. If it would be there
7161 before, the ancestors would be necessarily there as well, therefore
7162 clone_tree_hash wouldn't be called. */
7163 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7164 entry = XCNEW (struct decl_table_entry);
7165 entry->orig = die;
7166 entry->copy = clone;
7167 *slot = entry;
7169 FOR_EACH_CHILD (die, c,
7170 add_child_die (clone, clone_tree_hash (c, decl_table)));
7172 return clone;
7175 /* Walk the DIE and its children, looking for references to incomplete
7176 or trivial types that are unmarked (i.e., that are not in the current
7177 type_unit). */
7179 static void
7180 copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
7182 dw_die_ref c;
7183 dw_attr_ref a;
7184 unsigned ix;
7186 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7188 if (AT_class (a) == dw_val_class_die_ref)
7190 dw_die_ref targ = AT_ref (a);
7191 void **slot;
7192 struct decl_table_entry *entry;
7194 if (targ->die_mark != 0 || targ->comdat_type_p)
7195 continue;
7197 slot = htab_find_slot_with_hash (decl_table, targ,
7198 htab_hash_pointer (targ), INSERT);
7200 if (*slot != HTAB_EMPTY_ENTRY)
7202 /* TARG has already been copied, so we just need to
7203 modify the reference to point to the copy. */
7204 entry = (struct decl_table_entry *) *slot;
7205 a->dw_attr_val.v.val_die_ref.die = entry->copy;
7207 else
7209 dw_die_ref parent = unit;
7210 dw_die_ref copy = clone_die (targ);
7212 /* Record in DECL_TABLE that TARG has been copied.
7213 Need to do this now, before the recursive call,
7214 because DECL_TABLE may be expanded and SLOT
7215 would no longer be a valid pointer. */
7216 entry = XCNEW (struct decl_table_entry);
7217 entry->orig = targ;
7218 entry->copy = copy;
7219 *slot = entry;
7221 FOR_EACH_CHILD (targ, c,
7222 add_child_die (copy,
7223 clone_tree_hash (c, decl_table)));
7225 /* Make sure the cloned tree is marked as part of the
7226 type unit. */
7227 mark_dies (copy);
7229 /* If TARG has surrounding context, copy its ancestor tree
7230 into the new type unit. */
7231 if (targ->die_parent != NULL
7232 && !is_unit_die (targ->die_parent))
7233 parent = copy_ancestor_tree (unit, targ->die_parent,
7234 decl_table);
7236 add_child_die (parent, copy);
7237 a->dw_attr_val.v.val_die_ref.die = copy;
7239 /* Make sure the newly-copied DIE is walked. If it was
7240 installed in a previously-added context, it won't
7241 get visited otherwise. */
7242 if (parent != unit)
7244 /* Find the highest point of the newly-added tree,
7245 mark each node along the way, and walk from there. */
7246 parent->die_mark = 1;
7247 while (parent->die_parent
7248 && parent->die_parent->die_mark == 0)
7250 parent = parent->die_parent;
7251 parent->die_mark = 1;
7253 copy_decls_walk (unit, parent, decl_table);
7259 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7262 /* Copy declarations for "unworthy" types into the new comdat section.
7263 Incomplete types, modified types, and certain other types aren't broken
7264 out into comdat sections of their own, so they don't have a signature,
7265 and we need to copy the declaration into the same section so that we
7266 don't have an external reference. */
7268 static void
7269 copy_decls_for_unworthy_types (dw_die_ref unit)
7271 htab_t decl_table;
7273 mark_dies (unit);
7274 decl_table = htab_create (10, htab_decl_hash, htab_decl_eq, htab_decl_del);
7275 copy_decls_walk (unit, unit, decl_table);
7276 htab_delete (decl_table);
7277 unmark_dies (unit);
7280 /* Traverse the DIE and add a sibling attribute if it may have the
7281 effect of speeding up access to siblings. To save some space,
7282 avoid generating sibling attributes for DIE's without children. */
7284 static void
7285 add_sibling_attributes (dw_die_ref die)
7287 dw_die_ref c;
7289 if (! die->die_child)
7290 return;
7292 if (die->die_parent && die != die->die_parent->die_child)
7293 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7295 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7298 /* Output all location lists for the DIE and its children. */
7300 static void
7301 output_location_lists (dw_die_ref die)
7303 dw_die_ref c;
7304 dw_attr_ref a;
7305 unsigned ix;
7307 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7308 if (AT_class (a) == dw_val_class_loc_list)
7309 output_loc_list (AT_loc_list (a));
7311 FOR_EACH_CHILD (die, c, output_location_lists (c));
7314 /* We want to limit the number of external references, because they are
7315 larger than local references: a relocation takes multiple words, and
7316 even a sig8 reference is always eight bytes, whereas a local reference
7317 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7318 So if we encounter multiple external references to the same type DIE, we
7319 make a local typedef stub for it and redirect all references there.
7321 This is the element of the hash table for keeping track of these
7322 references. */
7324 struct external_ref
7326 dw_die_ref type;
7327 dw_die_ref stub;
7328 unsigned n_refs;
7331 /* Hash an external_ref. */
7333 static hashval_t
7334 hash_external_ref (const void *p)
7336 const struct external_ref *r = (const struct external_ref *)p;
7337 return htab_hash_pointer (r->type);
7340 /* Compare external_refs. */
7342 static int
7343 external_ref_eq (const void *p1, const void *p2)
7345 const struct external_ref *r1 = (const struct external_ref *)p1;
7346 const struct external_ref *r2 = (const struct external_ref *)p2;
7347 return r1->type == r2->type;
7350 /* Return a pointer to the external_ref for references to DIE. */
7352 static struct external_ref *
7353 lookup_external_ref (htab_t map, dw_die_ref die)
7355 struct external_ref ref, *ref_p;
7356 void ** slot;
7358 ref.type = die;
7359 slot = htab_find_slot (map, &ref, INSERT);
7360 if (*slot != HTAB_EMPTY_ENTRY)
7361 return (struct external_ref *) *slot;
7363 ref_p = XCNEW (struct external_ref);
7364 ref_p->type = die;
7365 *slot = ref_p;
7366 return ref_p;
7369 /* Subroutine of optimize_external_refs, below.
7371 If we see a type skeleton, record it as our stub. If we see external
7372 references, remember how many we've seen. */
7374 static void
7375 optimize_external_refs_1 (dw_die_ref die, htab_t map)
7377 dw_die_ref c;
7378 dw_attr_ref a;
7379 unsigned ix;
7380 struct external_ref *ref_p;
7382 if (is_type_die (die)
7383 && (c = get_AT_ref (die, DW_AT_signature)))
7385 /* This is a local skeleton; use it for local references. */
7386 ref_p = lookup_external_ref (map, c);
7387 ref_p->stub = die;
7390 /* Scan the DIE references, and remember any that refer to DIEs from
7391 other CUs (i.e. those which are not marked). */
7392 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7393 if (AT_class (a) == dw_val_class_die_ref
7394 && (c = AT_ref (a))->die_mark == 0
7395 && is_type_die (c))
7397 ref_p = lookup_external_ref (map, c);
7398 ref_p->n_refs++;
7401 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7404 /* htab_traverse callback function for optimize_external_refs, below. SLOT
7405 points to an external_ref, DATA is the CU we're processing. If we don't
7406 already have a local stub, and we have multiple refs, build a stub. */
7408 static int
7409 build_local_stub (void **slot, void *data)
7411 struct external_ref *ref_p = (struct external_ref *)*slot;
7413 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7415 /* We have multiple references to this type, so build a small stub.
7416 Both of these forms are a bit dodgy from the perspective of the
7417 DWARF standard, since technically they should have names. */
7418 dw_die_ref cu = (dw_die_ref) data;
7419 dw_die_ref type = ref_p->type;
7420 dw_die_ref stub = NULL;
7422 if (type->comdat_type_p)
7424 /* If we refer to this type via sig8, use AT_signature. */
7425 stub = new_die (type->die_tag, cu, NULL_TREE);
7426 add_AT_die_ref (stub, DW_AT_signature, type);
7428 else
7430 /* Otherwise, use a typedef with no name. */
7431 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7432 add_AT_die_ref (stub, DW_AT_type, type);
7435 stub->die_mark++;
7436 ref_p->stub = stub;
7438 return 1;
7441 /* DIE is a unit; look through all the DIE references to see if there are
7442 any external references to types, and if so, create local stubs for
7443 them which will be applied in build_abbrev_table. This is useful because
7444 references to local DIEs are smaller. */
7446 static htab_t
7447 optimize_external_refs (dw_die_ref die)
7449 htab_t map = htab_create (10, hash_external_ref, external_ref_eq, free);
7450 optimize_external_refs_1 (die, map);
7451 htab_traverse (map, build_local_stub, die);
7452 return map;
7455 /* The format of each DIE (and its attribute value pairs) is encoded in an
7456 abbreviation table. This routine builds the abbreviation table and assigns
7457 a unique abbreviation id for each abbreviation entry. The children of each
7458 die are visited recursively. */
7460 static void
7461 build_abbrev_table (dw_die_ref die, htab_t extern_map)
7463 unsigned long abbrev_id;
7464 unsigned int n_alloc;
7465 dw_die_ref c;
7466 dw_attr_ref a;
7467 unsigned ix;
7469 /* Scan the DIE references, and replace any that refer to
7470 DIEs from other CUs (i.e. those which are not marked) with
7471 the local stubs we built in optimize_external_refs. */
7472 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7473 if (AT_class (a) == dw_val_class_die_ref
7474 && (c = AT_ref (a))->die_mark == 0)
7476 struct external_ref *ref_p;
7477 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
7479 ref_p = lookup_external_ref (extern_map, c);
7480 if (ref_p->stub && ref_p->stub != die)
7481 change_AT_die_ref (a, ref_p->stub);
7482 else
7483 /* We aren't changing this reference, so mark it external. */
7484 set_AT_ref_external (a, 1);
7487 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7489 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7490 dw_attr_ref die_a, abbrev_a;
7491 unsigned ix;
7492 bool ok = true;
7494 if (abbrev->die_tag != die->die_tag)
7495 continue;
7496 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7497 continue;
7499 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
7500 continue;
7502 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
7504 abbrev_a = &(*abbrev->die_attr)[ix];
7505 if ((abbrev_a->dw_attr != die_a->dw_attr)
7506 || (value_format (abbrev_a) != value_format (die_a)))
7508 ok = false;
7509 break;
7512 if (ok)
7513 break;
7516 if (abbrev_id >= abbrev_die_table_in_use)
7518 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7520 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7521 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7522 n_alloc);
7524 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7525 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7526 abbrev_die_table_allocated = n_alloc;
7529 ++abbrev_die_table_in_use;
7530 abbrev_die_table[abbrev_id] = die;
7533 die->die_abbrev = abbrev_id;
7534 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
7537 /* Return the power-of-two number of bytes necessary to represent VALUE. */
7539 static int
7540 constant_size (unsigned HOST_WIDE_INT value)
7542 int log;
7544 if (value == 0)
7545 log = 0;
7546 else
7547 log = floor_log2 (value);
7549 log = log / 8;
7550 log = 1 << (floor_log2 (log) + 1);
7552 return log;
7555 /* Return the size of a DIE as it is represented in the
7556 .debug_info section. */
7558 static unsigned long
7559 size_of_die (dw_die_ref die)
7561 unsigned long size = 0;
7562 dw_attr_ref a;
7563 unsigned ix;
7564 enum dwarf_form form;
7566 size += size_of_uleb128 (die->die_abbrev);
7567 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7569 switch (AT_class (a))
7571 case dw_val_class_addr:
7572 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7574 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7575 size += size_of_uleb128 (AT_index (a));
7577 else
7578 size += DWARF2_ADDR_SIZE;
7579 break;
7580 case dw_val_class_offset:
7581 size += DWARF_OFFSET_SIZE;
7582 break;
7583 case dw_val_class_loc:
7585 unsigned long lsize = size_of_locs (AT_loc (a));
7587 /* Block length. */
7588 if (dwarf_version >= 4)
7589 size += size_of_uleb128 (lsize);
7590 else
7591 size += constant_size (lsize);
7592 size += lsize;
7594 break;
7595 case dw_val_class_loc_list:
7596 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7598 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7599 size += size_of_uleb128 (AT_index (a));
7601 else
7602 size += DWARF_OFFSET_SIZE;
7603 break;
7604 case dw_val_class_range_list:
7605 size += DWARF_OFFSET_SIZE;
7606 break;
7607 case dw_val_class_const:
7608 size += size_of_sleb128 (AT_int (a));
7609 break;
7610 case dw_val_class_unsigned_const:
7612 int csize = constant_size (AT_unsigned (a));
7613 if (dwarf_version == 3
7614 && a->dw_attr == DW_AT_data_member_location
7615 && csize >= 4)
7616 size += size_of_uleb128 (AT_unsigned (a));
7617 else
7618 size += csize;
7620 break;
7621 case dw_val_class_const_double:
7622 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
7623 if (HOST_BITS_PER_WIDE_INT >= 64)
7624 size++; /* block */
7625 break;
7626 case dw_val_class_vec:
7627 size += constant_size (a->dw_attr_val.v.val_vec.length
7628 * a->dw_attr_val.v.val_vec.elt_size)
7629 + a->dw_attr_val.v.val_vec.length
7630 * a->dw_attr_val.v.val_vec.elt_size; /* block */
7631 break;
7632 case dw_val_class_flag:
7633 if (dwarf_version >= 4)
7634 /* Currently all add_AT_flag calls pass in 1 as last argument,
7635 so DW_FORM_flag_present can be used. If that ever changes,
7636 we'll need to use DW_FORM_flag and have some optimization
7637 in build_abbrev_table that will change those to
7638 DW_FORM_flag_present if it is set to 1 in all DIEs using
7639 the same abbrev entry. */
7640 gcc_assert (a->dw_attr_val.v.val_flag == 1);
7641 else
7642 size += 1;
7643 break;
7644 case dw_val_class_die_ref:
7645 if (AT_ref_external (a))
7647 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
7648 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
7649 is sized by target address length, whereas in DWARF3
7650 it's always sized as an offset. */
7651 if (use_debug_types)
7652 size += DWARF_TYPE_SIGNATURE_SIZE;
7653 else if (dwarf_version == 2)
7654 size += DWARF2_ADDR_SIZE;
7655 else
7656 size += DWARF_OFFSET_SIZE;
7658 else
7659 size += DWARF_OFFSET_SIZE;
7660 break;
7661 case dw_val_class_fde_ref:
7662 size += DWARF_OFFSET_SIZE;
7663 break;
7664 case dw_val_class_lbl_id:
7665 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7667 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7668 size += size_of_uleb128 (AT_index (a));
7670 else
7671 size += DWARF2_ADDR_SIZE;
7672 break;
7673 case dw_val_class_lineptr:
7674 case dw_val_class_macptr:
7675 size += DWARF_OFFSET_SIZE;
7676 break;
7677 case dw_val_class_str:
7678 form = AT_string_form (a);
7679 if (form == DW_FORM_strp)
7680 size += DWARF_OFFSET_SIZE;
7681 else if (form == DW_FORM_GNU_str_index)
7682 size += size_of_uleb128 (AT_index (a));
7683 else
7684 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7685 break;
7686 case dw_val_class_file:
7687 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7688 break;
7689 case dw_val_class_data8:
7690 size += 8;
7691 break;
7692 case dw_val_class_vms_delta:
7693 size += DWARF_OFFSET_SIZE;
7694 break;
7695 case dw_val_class_high_pc:
7696 size += DWARF2_ADDR_SIZE;
7697 break;
7698 default:
7699 gcc_unreachable ();
7703 return size;
7706 /* Size the debugging information associated with a given DIE. Visits the
7707 DIE's children recursively. Updates the global variable next_die_offset, on
7708 each time through. Uses the current value of next_die_offset to update the
7709 die_offset field in each DIE. */
7711 static void
7712 calc_die_sizes (dw_die_ref die)
7714 dw_die_ref c;
7716 gcc_assert (die->die_offset == 0
7717 || (unsigned long int) die->die_offset == next_die_offset);
7718 die->die_offset = next_die_offset;
7719 next_die_offset += size_of_die (die);
7721 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
7723 if (die->die_child != NULL)
7724 /* Count the null byte used to terminate sibling lists. */
7725 next_die_offset += 1;
7728 /* Size just the base type children at the start of the CU.
7729 This is needed because build_abbrev needs to size locs
7730 and sizing of type based stack ops needs to know die_offset
7731 values for the base types. */
7733 static void
7734 calc_base_type_die_sizes (void)
7736 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7737 unsigned int i;
7738 dw_die_ref base_type;
7739 #if ENABLE_ASSERT_CHECKING
7740 dw_die_ref prev = comp_unit_die ()->die_child;
7741 #endif
7743 die_offset += size_of_die (comp_unit_die ());
7744 for (i = 0; base_types.iterate (i, &base_type); i++)
7746 #if ENABLE_ASSERT_CHECKING
7747 gcc_assert (base_type->die_offset == 0
7748 && prev->die_sib == base_type
7749 && base_type->die_child == NULL
7750 && base_type->die_abbrev);
7751 prev = base_type;
7752 #endif
7753 base_type->die_offset = die_offset;
7754 die_offset += size_of_die (base_type);
7758 /* Set the marks for a die and its children. We do this so
7759 that we know whether or not a reference needs to use FORM_ref_addr; only
7760 DIEs in the same CU will be marked. We used to clear out the offset
7761 and use that as the flag, but ran into ordering problems. */
7763 static void
7764 mark_dies (dw_die_ref die)
7766 dw_die_ref c;
7768 gcc_assert (!die->die_mark);
7770 die->die_mark = 1;
7771 FOR_EACH_CHILD (die, c, mark_dies (c));
7774 /* Clear the marks for a die and its children. */
7776 static void
7777 unmark_dies (dw_die_ref die)
7779 dw_die_ref c;
7781 if (! use_debug_types)
7782 gcc_assert (die->die_mark);
7784 die->die_mark = 0;
7785 FOR_EACH_CHILD (die, c, unmark_dies (c));
7788 /* Clear the marks for a die, its children and referred dies. */
7790 static void
7791 unmark_all_dies (dw_die_ref die)
7793 dw_die_ref c;
7794 dw_attr_ref a;
7795 unsigned ix;
7797 if (!die->die_mark)
7798 return;
7799 die->die_mark = 0;
7801 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
7803 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7804 if (AT_class (a) == dw_val_class_die_ref)
7805 unmark_all_dies (AT_ref (a));
7808 /* Return the size of the .debug_pubnames or .debug_pubtypes table
7809 generated for the compilation unit. */
7811 static unsigned long
7812 size_of_pubnames (vec<pubname_entry, va_gc> *names)
7814 unsigned long size;
7815 unsigned i;
7816 pubname_ref p;
7818 size = DWARF_PUBNAMES_HEADER_SIZE;
7819 FOR_EACH_VEC_ELT (*names, i, p)
7820 if (names != pubtype_table
7821 || p->die->die_offset != 0
7822 || !flag_eliminate_unused_debug_types)
7823 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
7825 size += DWARF_OFFSET_SIZE;
7826 return size;
7829 /* Return the size of the information in the .debug_aranges section. */
7831 static unsigned long
7832 size_of_aranges (void)
7834 unsigned long size;
7836 size = DWARF_ARANGES_HEADER_SIZE;
7838 /* Count the address/length pair for this compilation unit. */
7839 if (text_section_used)
7840 size += 2 * DWARF2_ADDR_SIZE;
7841 if (cold_text_section_used)
7842 size += 2 * DWARF2_ADDR_SIZE;
7843 if (have_multiple_function_sections)
7845 unsigned fde_idx;
7846 dw_fde_ref fde;
7848 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
7850 if (!fde->in_std_section)
7851 size += 2 * DWARF2_ADDR_SIZE;
7852 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
7853 size += 2 * DWARF2_ADDR_SIZE;
7857 /* Count the two zero words used to terminated the address range table. */
7858 size += 2 * DWARF2_ADDR_SIZE;
7859 return size;
7862 /* Select the encoding of an attribute value. */
7864 static enum dwarf_form
7865 value_format (dw_attr_ref a)
7867 switch (AT_class (a))
7869 case dw_val_class_addr:
7870 /* Only very few attributes allow DW_FORM_addr. */
7871 switch (a->dw_attr)
7873 case DW_AT_low_pc:
7874 case DW_AT_high_pc:
7875 case DW_AT_entry_pc:
7876 case DW_AT_trampoline:
7877 return (AT_index (a) == NOT_INDEXED
7878 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
7879 default:
7880 break;
7882 switch (DWARF2_ADDR_SIZE)
7884 case 1:
7885 return DW_FORM_data1;
7886 case 2:
7887 return DW_FORM_data2;
7888 case 4:
7889 return DW_FORM_data4;
7890 case 8:
7891 return DW_FORM_data8;
7892 default:
7893 gcc_unreachable ();
7895 case dw_val_class_range_list:
7896 case dw_val_class_loc_list:
7897 if (dwarf_version >= 4)
7898 return DW_FORM_sec_offset;
7899 /* FALLTHRU */
7900 case dw_val_class_vms_delta:
7901 case dw_val_class_offset:
7902 switch (DWARF_OFFSET_SIZE)
7904 case 4:
7905 return DW_FORM_data4;
7906 case 8:
7907 return DW_FORM_data8;
7908 default:
7909 gcc_unreachable ();
7911 case dw_val_class_loc:
7912 if (dwarf_version >= 4)
7913 return DW_FORM_exprloc;
7914 switch (constant_size (size_of_locs (AT_loc (a))))
7916 case 1:
7917 return DW_FORM_block1;
7918 case 2:
7919 return DW_FORM_block2;
7920 case 4:
7921 return DW_FORM_block4;
7922 default:
7923 gcc_unreachable ();
7925 case dw_val_class_const:
7926 return DW_FORM_sdata;
7927 case dw_val_class_unsigned_const:
7928 switch (constant_size (AT_unsigned (a)))
7930 case 1:
7931 return DW_FORM_data1;
7932 case 2:
7933 return DW_FORM_data2;
7934 case 4:
7935 /* In DWARF3 DW_AT_data_member_location with
7936 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
7937 constant, so we need to use DW_FORM_udata if we need
7938 a large constant. */
7939 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
7940 return DW_FORM_udata;
7941 return DW_FORM_data4;
7942 case 8:
7943 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
7944 return DW_FORM_udata;
7945 return DW_FORM_data8;
7946 default:
7947 gcc_unreachable ();
7949 case dw_val_class_const_double:
7950 switch (HOST_BITS_PER_WIDE_INT)
7952 case 8:
7953 return DW_FORM_data2;
7954 case 16:
7955 return DW_FORM_data4;
7956 case 32:
7957 return DW_FORM_data8;
7958 case 64:
7959 default:
7960 return DW_FORM_block1;
7962 case dw_val_class_vec:
7963 switch (constant_size (a->dw_attr_val.v.val_vec.length
7964 * a->dw_attr_val.v.val_vec.elt_size))
7966 case 1:
7967 return DW_FORM_block1;
7968 case 2:
7969 return DW_FORM_block2;
7970 case 4:
7971 return DW_FORM_block4;
7972 default:
7973 gcc_unreachable ();
7975 case dw_val_class_flag:
7976 if (dwarf_version >= 4)
7978 /* Currently all add_AT_flag calls pass in 1 as last argument,
7979 so DW_FORM_flag_present can be used. If that ever changes,
7980 we'll need to use DW_FORM_flag and have some optimization
7981 in build_abbrev_table that will change those to
7982 DW_FORM_flag_present if it is set to 1 in all DIEs using
7983 the same abbrev entry. */
7984 gcc_assert (a->dw_attr_val.v.val_flag == 1);
7985 return DW_FORM_flag_present;
7987 return DW_FORM_flag;
7988 case dw_val_class_die_ref:
7989 if (AT_ref_external (a))
7990 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
7991 else
7992 return DW_FORM_ref;
7993 case dw_val_class_fde_ref:
7994 return DW_FORM_data;
7995 case dw_val_class_lbl_id:
7996 return (AT_index (a) == NOT_INDEXED
7997 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
7998 case dw_val_class_lineptr:
7999 case dw_val_class_macptr:
8000 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8001 case dw_val_class_str:
8002 return AT_string_form (a);
8003 case dw_val_class_file:
8004 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8006 case 1:
8007 return DW_FORM_data1;
8008 case 2:
8009 return DW_FORM_data2;
8010 case 4:
8011 return DW_FORM_data4;
8012 default:
8013 gcc_unreachable ();
8016 case dw_val_class_data8:
8017 return DW_FORM_data8;
8019 case dw_val_class_high_pc:
8020 switch (DWARF2_ADDR_SIZE)
8022 case 1:
8023 return DW_FORM_data1;
8024 case 2:
8025 return DW_FORM_data2;
8026 case 4:
8027 return DW_FORM_data4;
8028 case 8:
8029 return DW_FORM_data8;
8030 default:
8031 gcc_unreachable ();
8034 default:
8035 gcc_unreachable ();
8039 /* Output the encoding of an attribute value. */
8041 static void
8042 output_value_format (dw_attr_ref a)
8044 enum dwarf_form form = value_format (a);
8046 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8049 /* Given a die and id, produce the appropriate abbreviations. */
8051 static void
8052 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8054 unsigned ix;
8055 dw_attr_ref a_attr;
8057 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8058 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8059 dwarf_tag_name (abbrev->die_tag));
8061 if (abbrev->die_child != NULL)
8062 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8063 else
8064 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8066 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8068 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8069 dwarf_attr_name (a_attr->dw_attr));
8070 output_value_format (a_attr);
8073 dw2_asm_output_data (1, 0, NULL);
8074 dw2_asm_output_data (1, 0, NULL);
8078 /* Output the .debug_abbrev section which defines the DIE abbreviation
8079 table. */
8081 static void
8082 output_abbrev_section (void)
8084 unsigned long abbrev_id;
8086 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8087 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8089 /* Terminate the table. */
8090 dw2_asm_output_data (1, 0, NULL);
8093 /* Output a symbol we can use to refer to this DIE from another CU. */
8095 static inline void
8096 output_die_symbol (dw_die_ref die)
8098 const char *sym = die->die_id.die_symbol;
8100 gcc_assert (!die->comdat_type_p);
8102 if (sym == 0)
8103 return;
8105 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8106 /* We make these global, not weak; if the target doesn't support
8107 .linkonce, it doesn't support combining the sections, so debugging
8108 will break. */
8109 targetm.asm_out.globalize_label (asm_out_file, sym);
8111 ASM_OUTPUT_LABEL (asm_out_file, sym);
8114 /* Return a new location list, given the begin and end range, and the
8115 expression. */
8117 static inline dw_loc_list_ref
8118 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8119 const char *section)
8121 dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
8123 retlist->begin = begin;
8124 retlist->begin_entry = NULL;
8125 retlist->end = end;
8126 retlist->expr = expr;
8127 retlist->section = section;
8129 return retlist;
8132 /* Generate a new internal symbol for this location list node, if it
8133 hasn't got one yet. */
8135 static inline void
8136 gen_llsym (dw_loc_list_ref list)
8138 gcc_assert (!list->ll_symbol);
8139 list->ll_symbol = gen_internal_sym ("LLST");
8142 /* Output the location list given to us. */
8144 static void
8145 output_loc_list (dw_loc_list_ref list_head)
8147 dw_loc_list_ref curr = list_head;
8149 if (list_head->emitted)
8150 return;
8151 list_head->emitted = true;
8153 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8155 /* Walk the location list, and output each range + expression. */
8156 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8158 unsigned long size;
8159 /* Don't output an entry that starts and ends at the same address. */
8160 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8161 continue;
8162 size = size_of_locs (curr->expr);
8163 /* If the expression is too large, drop it on the floor. We could
8164 perhaps put it into DW_TAG_dwarf_procedure and refer to that
8165 in the expression, but >= 64KB expressions for a single value
8166 in a single range are unlikely very useful. */
8167 if (size > 0xffff)
8168 continue;
8169 if (dwarf_split_debug_info)
8171 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8172 "Location list start/length entry (%s)",
8173 list_head->ll_symbol);
8174 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8175 "Location list range start index (%s)",
8176 curr->begin);
8177 /* The length field is 4 bytes. If we ever need to support
8178 an 8-byte length, we can add a new DW_LLE code or fall back
8179 to DW_LLE_GNU_start_end_entry. */
8180 dw2_asm_output_delta (4, curr->end, curr->begin,
8181 "Location list range length (%s)",
8182 list_head->ll_symbol);
8184 else if (!have_multiple_function_sections)
8186 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8187 "Location list begin address (%s)",
8188 list_head->ll_symbol);
8189 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8190 "Location list end address (%s)",
8191 list_head->ll_symbol);
8193 else
8195 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8196 "Location list begin address (%s)",
8197 list_head->ll_symbol);
8198 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8199 "Location list end address (%s)",
8200 list_head->ll_symbol);
8203 /* Output the block length for this list of location operations. */
8204 gcc_assert (size <= 0xffff);
8205 dw2_asm_output_data (2, size, "%s", "Location expression size");
8207 output_loc_sequence (curr->expr, -1);
8210 if (dwarf_split_debug_info)
8211 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8212 "Location list terminator (%s)",
8213 list_head->ll_symbol);
8214 else
8216 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8217 "Location list terminator begin (%s)",
8218 list_head->ll_symbol);
8219 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8220 "Location list terminator end (%s)",
8221 list_head->ll_symbol);
8225 /* Output a range_list offset into the debug_range section. Emit a
8226 relocated reference if val_entry is NULL, otherwise, emit an
8227 indirect reference. */
8229 static void
8230 output_range_list_offset (dw_attr_ref a)
8232 const char *name = dwarf_attr_name (a->dw_attr);
8234 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8236 char *p = strchr (ranges_section_label, '\0');
8237 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8238 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8239 debug_ranges_section, "%s", name);
8240 *p = '\0';
8242 else
8243 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8244 "%s (offset from %s)", name, ranges_section_label);
8247 /* Output the offset into the debug_loc section. */
8249 static void
8250 output_loc_list_offset (dw_attr_ref a)
8252 char *sym = AT_loc_list (a)->ll_symbol;
8254 gcc_assert (sym);
8255 if (dwarf_split_debug_info)
8256 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8257 "%s", dwarf_attr_name (a->dw_attr));
8258 else
8259 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8260 "%s", dwarf_attr_name (a->dw_attr));
8263 /* Output an attribute's index or value appropriately. */
8265 static void
8266 output_attr_index_or_value (dw_attr_ref a)
8268 const char *name = dwarf_attr_name (a->dw_attr);
8270 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8272 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8273 return;
8275 switch (AT_class (a))
8277 case dw_val_class_addr:
8278 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8279 break;
8280 case dw_val_class_high_pc:
8281 case dw_val_class_lbl_id:
8282 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8283 break;
8284 case dw_val_class_loc_list:
8285 output_loc_list_offset (a);
8286 break;
8287 default:
8288 gcc_unreachable ();
8292 /* Output a type signature. */
8294 static inline void
8295 output_signature (const char *sig, const char *name)
8297 int i;
8299 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8300 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8303 /* Output the DIE and its attributes. Called recursively to generate
8304 the definitions of each child DIE. */
8306 static void
8307 output_die (dw_die_ref die)
8309 dw_attr_ref a;
8310 dw_die_ref c;
8311 unsigned long size;
8312 unsigned ix;
8314 /* If someone in another CU might refer to us, set up a symbol for
8315 them to point to. */
8316 if (! die->comdat_type_p && die->die_id.die_symbol)
8317 output_die_symbol (die);
8319 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8320 (unsigned long)die->die_offset,
8321 dwarf_tag_name (die->die_tag));
8323 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8325 const char *name = dwarf_attr_name (a->dw_attr);
8327 switch (AT_class (a))
8329 case dw_val_class_addr:
8330 output_attr_index_or_value (a);
8331 break;
8333 case dw_val_class_offset:
8334 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8335 "%s", name);
8336 break;
8338 case dw_val_class_range_list:
8339 output_range_list_offset (a);
8340 break;
8342 case dw_val_class_loc:
8343 size = size_of_locs (AT_loc (a));
8345 /* Output the block length for this list of location operations. */
8346 if (dwarf_version >= 4)
8347 dw2_asm_output_data_uleb128 (size, "%s", name);
8348 else
8349 dw2_asm_output_data (constant_size (size), size, "%s", name);
8351 output_loc_sequence (AT_loc (a), -1);
8352 break;
8354 case dw_val_class_const:
8355 /* ??? It would be slightly more efficient to use a scheme like is
8356 used for unsigned constants below, but gdb 4.x does not sign
8357 extend. Gdb 5.x does sign extend. */
8358 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8359 break;
8361 case dw_val_class_unsigned_const:
8363 int csize = constant_size (AT_unsigned (a));
8364 if (dwarf_version == 3
8365 && a->dw_attr == DW_AT_data_member_location
8366 && csize >= 4)
8367 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8368 else
8369 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8371 break;
8373 case dw_val_class_const_double:
8375 unsigned HOST_WIDE_INT first, second;
8377 if (HOST_BITS_PER_WIDE_INT >= 64)
8378 dw2_asm_output_data (1,
8379 HOST_BITS_PER_DOUBLE_INT
8380 / HOST_BITS_PER_CHAR,
8381 NULL);
8383 if (WORDS_BIG_ENDIAN)
8385 first = a->dw_attr_val.v.val_double.high;
8386 second = a->dw_attr_val.v.val_double.low;
8388 else
8390 first = a->dw_attr_val.v.val_double.low;
8391 second = a->dw_attr_val.v.val_double.high;
8394 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8395 first, "%s", name);
8396 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8397 second, NULL);
8399 break;
8401 case dw_val_class_vec:
8403 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8404 unsigned int len = a->dw_attr_val.v.val_vec.length;
8405 unsigned int i;
8406 unsigned char *p;
8408 dw2_asm_output_data (constant_size (len * elt_size),
8409 len * elt_size, "%s", name);
8410 if (elt_size > sizeof (HOST_WIDE_INT))
8412 elt_size /= 2;
8413 len *= 2;
8415 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8416 i < len;
8417 i++, p += elt_size)
8418 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8419 "fp or vector constant word %u", i);
8420 break;
8423 case dw_val_class_flag:
8424 if (dwarf_version >= 4)
8426 /* Currently all add_AT_flag calls pass in 1 as last argument,
8427 so DW_FORM_flag_present can be used. If that ever changes,
8428 we'll need to use DW_FORM_flag and have some optimization
8429 in build_abbrev_table that will change those to
8430 DW_FORM_flag_present if it is set to 1 in all DIEs using
8431 the same abbrev entry. */
8432 gcc_assert (AT_flag (a) == 1);
8433 if (flag_debug_asm)
8434 fprintf (asm_out_file, "\t\t\t%s %s\n",
8435 ASM_COMMENT_START, name);
8436 break;
8438 dw2_asm_output_data (1, AT_flag (a), "%s", name);
8439 break;
8441 case dw_val_class_loc_list:
8442 output_attr_index_or_value (a);
8443 break;
8445 case dw_val_class_die_ref:
8446 if (AT_ref_external (a))
8448 if (AT_ref (a)->comdat_type_p)
8450 comdat_type_node_ref type_node =
8451 AT_ref (a)->die_id.die_type_node;
8453 gcc_assert (type_node);
8454 output_signature (type_node->signature, name);
8456 else
8458 const char *sym = AT_ref (a)->die_id.die_symbol;
8459 int size;
8461 gcc_assert (sym);
8462 /* In DWARF2, DW_FORM_ref_addr is sized by target address
8463 length, whereas in DWARF3 it's always sized as an
8464 offset. */
8465 if (dwarf_version == 2)
8466 size = DWARF2_ADDR_SIZE;
8467 else
8468 size = DWARF_OFFSET_SIZE;
8469 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8470 name);
8473 else
8475 gcc_assert (AT_ref (a)->die_offset);
8476 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8477 "%s", name);
8479 break;
8481 case dw_val_class_fde_ref:
8483 char l1[20];
8485 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8486 a->dw_attr_val.v.val_fde_index * 2);
8487 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8488 "%s", name);
8490 break;
8492 case dw_val_class_vms_delta:
8493 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
8494 AT_vms_delta2 (a), AT_vms_delta1 (a),
8495 "%s", name);
8496 break;
8498 case dw_val_class_lbl_id:
8499 output_attr_index_or_value (a);
8500 break;
8502 case dw_val_class_lineptr:
8503 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8504 debug_line_section, "%s", name);
8505 break;
8507 case dw_val_class_macptr:
8508 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8509 debug_macinfo_section, "%s", name);
8510 break;
8512 case dw_val_class_str:
8513 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
8514 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8515 a->dw_attr_val.v.val_str->label,
8516 debug_str_section,
8517 "%s: \"%s\"", name, AT_string (a));
8518 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
8519 dw2_asm_output_data_uleb128 (AT_index (a),
8520 "%s: \"%s\"", name, AT_string (a));
8521 else
8522 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8523 break;
8525 case dw_val_class_file:
8527 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8529 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8530 a->dw_attr_val.v.val_file->filename);
8531 break;
8534 case dw_val_class_data8:
8536 int i;
8538 for (i = 0; i < 8; i++)
8539 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
8540 i == 0 ? "%s" : NULL, name);
8541 break;
8544 case dw_val_class_high_pc:
8545 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
8546 get_AT_low_pc (die), "DW_AT_high_pc");
8547 break;
8549 default:
8550 gcc_unreachable ();
8554 FOR_EACH_CHILD (die, c, output_die (c));
8556 /* Add null byte to terminate sibling list. */
8557 if (die->die_child != NULL)
8558 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
8559 (unsigned long) die->die_offset);
8562 /* Output the compilation unit that appears at the beginning of the
8563 .debug_info section, and precedes the DIE descriptions. */
8565 static void
8566 output_compilation_unit_header (void)
8568 int ver = dwarf_version;
8570 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8571 dw2_asm_output_data (4, 0xffffffff,
8572 "Initial length escape value indicating 64-bit DWARF extension");
8573 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8574 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8575 "Length of Compilation Unit Info");
8576 dw2_asm_output_data (2, ver, "DWARF version number");
8577 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8578 debug_abbrev_section,
8579 "Offset Into Abbrev. Section");
8580 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8583 /* Output the compilation unit DIE and its children. */
8585 static void
8586 output_comp_unit (dw_die_ref die, int output_if_empty)
8588 const char *secname, *oldsym;
8589 char *tmp;
8590 htab_t extern_map;
8592 /* Unless we are outputting main CU, we may throw away empty ones. */
8593 if (!output_if_empty && die->die_child == NULL)
8594 return;
8596 /* Even if there are no children of this DIE, we must output the information
8597 about the compilation unit. Otherwise, on an empty translation unit, we
8598 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
8599 will then complain when examining the file. First mark all the DIEs in
8600 this CU so we know which get local refs. */
8601 mark_dies (die);
8603 extern_map = optimize_external_refs (die);
8605 build_abbrev_table (die, extern_map);
8607 htab_delete (extern_map);
8609 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8610 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8611 calc_die_sizes (die);
8613 oldsym = die->die_id.die_symbol;
8614 if (oldsym)
8616 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8618 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8619 secname = tmp;
8620 die->die_id.die_symbol = NULL;
8621 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8623 else
8625 switch_to_section (debug_info_section);
8626 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
8627 info_section_emitted = true;
8630 /* Output debugging information. */
8631 output_compilation_unit_header ();
8632 output_die (die);
8634 /* Leave the marks on the main CU, so we can check them in
8635 output_pubnames. */
8636 if (oldsym)
8638 unmark_dies (die);
8639 die->die_id.die_symbol = oldsym;
8643 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
8644 and .debug_pubtypes. This is configured per-target, but can be
8645 overridden by the -gpubnames or -gno-pubnames options. */
8647 static inline bool
8648 want_pubnames (void)
8650 return (debug_generate_pub_sections != -1
8651 ? debug_generate_pub_sections
8652 : targetm.want_debug_pub_sections);
8655 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
8657 static void
8658 add_AT_pubnames (dw_die_ref die)
8660 if (want_pubnames ())
8661 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
8664 /* Helper function to generate top-level dies for skeleton debug_info and
8665 debug_types. */
8667 static void
8668 add_top_level_skeleton_die_attrs (dw_die_ref die)
8670 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
8671 dw_attr_ref attr;
8673 add_comp_dir_attribute (die);
8674 add_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
8675 /* The specification suggests that these attributes be inline to avoid
8676 having a .debug_str section. We know that they exist in the die because
8677 we just added them. */
8678 attr = get_AT (die, DW_AT_GNU_dwo_name);
8679 attr->dw_attr_val.v.val_str->form = DW_FORM_string;
8680 attr = get_AT (die, DW_AT_comp_dir);
8681 attr->dw_attr_val.v.val_str->form = DW_FORM_string;
8683 add_AT_pubnames (die);
8684 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
8687 /* Return the single type-unit die for skeleton type units. */
8689 static dw_die_ref
8690 get_skeleton_type_unit (void)
8692 /* For dwarf_split_debug_sections with use_type info, all type units in the
8693 skeleton sections have identical dies (but different headers). This
8694 single die will be output many times. */
8696 static dw_die_ref skeleton_type_unit = NULL;
8698 if (skeleton_type_unit == NULL)
8700 skeleton_type_unit = new_die (DW_TAG_type_unit, NULL, NULL);
8701 add_top_level_skeleton_die_attrs (skeleton_type_unit);
8702 skeleton_type_unit->die_abbrev = SKELETON_TYPE_DIE_ABBREV;
8704 return skeleton_type_unit;
8707 /* Output skeleton debug sections that point to the dwo file. */
8709 static void
8710 output_skeleton_debug_sections (dw_die_ref comp_unit)
8712 /* These attributes will be found in the full debug_info section. */
8713 remove_AT (comp_unit, DW_AT_producer);
8714 remove_AT (comp_unit, DW_AT_language);
8716 /* Add attributes common to skeleton compile_units and type_units. */
8717 add_top_level_skeleton_die_attrs (comp_unit);
8719 switch_to_section (debug_skeleton_info_section);
8720 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
8722 /* Produce the skeleton compilation-unit header. This one differs enough from
8723 a normal CU header that it's better not to call output_compilation_unit
8724 header. */
8725 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8726 dw2_asm_output_data (4, 0xffffffff,
8727 "Initial length escape value indicating 64-bit DWARF extension");
8729 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8730 DWARF_COMPILE_UNIT_HEADER_SIZE
8731 - DWARF_INITIAL_LENGTH_SIZE
8732 + size_of_die (comp_unit),
8733 "Length of Compilation Unit Info");
8734 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
8735 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
8736 debug_abbrev_section,
8737 "Offset Into Abbrev. Section");
8738 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8740 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
8741 output_die (comp_unit);
8743 /* Build the skeleton debug_abbrev section. */
8744 switch_to_section (debug_skeleton_abbrev_section);
8745 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
8747 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
8748 if (use_debug_types)
8749 output_die_abbrevs (SKELETON_TYPE_DIE_ABBREV, get_skeleton_type_unit ());
8751 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
8754 /* Output a comdat type unit DIE and its children. */
8756 static void
8757 output_comdat_type_unit (comdat_type_node *node)
8759 const char *secname;
8760 char *tmp;
8761 int i;
8762 #if defined (OBJECT_FORMAT_ELF)
8763 tree comdat_key;
8764 #endif
8765 htab_t extern_map;
8767 /* First mark all the DIEs in this CU so we know which get local refs. */
8768 mark_dies (node->root_die);
8770 extern_map = optimize_external_refs (node->root_die);
8772 build_abbrev_table (node->root_die, extern_map);
8774 htab_delete (extern_map);
8776 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8777 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
8778 calc_die_sizes (node->root_die);
8780 #if defined (OBJECT_FORMAT_ELF)
8781 if (!dwarf_split_debug_info)
8782 secname = ".debug_types";
8783 else
8784 secname = ".debug_types.dwo";
8786 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
8787 sprintf (tmp, "wt.");
8788 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8789 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
8790 comdat_key = get_identifier (tmp);
8791 targetm.asm_out.named_section (secname,
8792 SECTION_DEBUG | SECTION_LINKONCE,
8793 comdat_key);
8794 #else
8795 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
8796 sprintf (tmp, ".gnu.linkonce.wt.");
8797 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8798 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
8799 secname = tmp;
8800 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8801 #endif
8803 /* Output debugging information. */
8804 output_compilation_unit_header ();
8805 output_signature (node->signature, "Type Signature");
8806 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
8807 "Offset to Type DIE");
8808 output_die (node->root_die);
8810 unmark_dies (node->root_die);
8812 #if defined (OBJECT_FORMAT_ELF)
8813 if (dwarf_split_debug_info)
8815 /* Produce the skeleton type-unit header. */
8816 const char *secname = ".debug_types";
8818 targetm.asm_out.named_section (secname,
8819 SECTION_DEBUG | SECTION_LINKONCE,
8820 comdat_key);
8821 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8822 dw2_asm_output_data (4, 0xffffffff,
8823 "Initial length escape value indicating 64-bit DWARF extension");
8825 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8826 DWARF_COMPILE_UNIT_HEADER_SIZE
8827 - DWARF_INITIAL_LENGTH_SIZE
8828 + size_of_die (get_skeleton_type_unit ())
8829 + DWARF_TYPE_SIGNATURE_SIZE + DWARF_OFFSET_SIZE,
8830 "Length of Type Unit Info");
8831 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
8832 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8833 debug_skeleton_abbrev_section_label,
8834 debug_abbrev_section,
8835 "Offset Into Abbrev. Section");
8836 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8837 output_signature (node->signature, "Type Signature");
8838 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, "Offset to Type DIE");
8840 output_die (get_skeleton_type_unit ());
8842 #endif
8845 /* Return the DWARF2/3 pubname associated with a decl. */
8847 static const char *
8848 dwarf2_name (tree decl, int scope)
8850 if (DECL_NAMELESS (decl))
8851 return NULL;
8852 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
8855 /* Add a new entry to .debug_pubnames if appropriate. */
8857 static void
8858 add_pubname_string (const char *str, dw_die_ref die)
8860 pubname_entry e;
8862 e.die = die;
8863 e.name = xstrdup (str);
8864 vec_safe_push (pubname_table, e);
8867 static void
8868 add_pubname (tree decl, dw_die_ref die)
8870 if (!want_pubnames ())
8871 return;
8873 /* Don't add items to the table when we expect that the consumer will have
8874 just read the enclosing die. For example, if the consumer is looking at a
8875 class_member, it will either be inside the class already, or will have just
8876 looked up the class to find the member. Either way, searching the class is
8877 faster than searching the index. */
8878 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
8879 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
8881 const char *name = dwarf2_name (decl, 1);
8883 if (name)
8884 add_pubname_string (name, die);
8888 /* Add an enumerator to the pubnames section. */
8890 static void
8891 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
8893 pubname_entry e;
8895 gcc_assert (scope_name);
8896 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
8897 e.die = die;
8898 vec_safe_push (pubname_table, e);
8901 /* Add a new entry to .debug_pubtypes if appropriate. */
8903 static void
8904 add_pubtype (tree decl, dw_die_ref die)
8906 pubname_entry e;
8908 if (!want_pubnames ())
8909 return;
8911 if ((TREE_PUBLIC (decl)
8912 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
8913 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
8915 tree scope = NULL;
8916 const char *scope_name = "";
8917 const char *sep = is_cxx () ? "::" : ".";
8918 const char *name;
8920 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
8921 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
8923 scope_name = lang_hooks.dwarf_name (scope, 1);
8924 if (scope_name != NULL && scope_name[0] != '\0')
8925 scope_name = concat (scope_name, sep, NULL);
8926 else
8927 scope_name = "";
8930 if (TYPE_P (decl))
8931 name = type_tag (decl);
8932 else
8933 name = lang_hooks.dwarf_name (decl, 1);
8935 /* If we don't have a name for the type, there's no point in adding
8936 it to the table. */
8937 if (name != NULL && name[0] != '\0')
8939 e.die = die;
8940 e.name = concat (scope_name, name, NULL);
8941 vec_safe_push (pubtype_table, e);
8944 /* Although it might be more consistent to add the pubinfo for the
8945 enumerators as their dies are created, they should only be added if the
8946 enum type meets the criteria above. So rather than re-check the parent
8947 enum type whenever an enumerator die is created, just output them all
8948 here. This isn't protected by the name conditional because anonymous
8949 enums don't have names. */
8950 if (die->die_tag == DW_TAG_enumeration_type)
8952 dw_die_ref c;
8954 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
8959 /* Output the public names table used to speed up access to externally
8960 visible names; or the public types table used to find type definitions. */
8962 static void
8963 output_pubnames (vec<pubname_entry, va_gc> *names)
8965 unsigned i;
8966 unsigned long pubnames_length = size_of_pubnames (names);
8967 pubname_ref pub;
8969 if (!want_pubnames () || !info_section_emitted)
8970 return;
8971 if (names == pubname_table)
8972 switch_to_section (debug_pubnames_section);
8973 else
8974 switch_to_section (debug_pubtypes_section);
8975 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8976 dw2_asm_output_data (4, 0xffffffff,
8977 "Initial length escape value indicating 64-bit DWARF extension");
8978 if (names == pubname_table)
8979 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8980 "Length of Public Names Info");
8981 else
8982 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8983 "Length of Public Type Names Info");
8984 /* Version number for pubnames/pubtypes is still 2, even in DWARF3. */
8985 dw2_asm_output_data (2, 2, "DWARF Version");
8986 if (dwarf_split_debug_info)
8987 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
8988 debug_skeleton_info_section,
8989 "Offset of Compilation Unit Info");
8990 else
8991 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8992 debug_info_section,
8993 "Offset of Compilation Unit Info");
8994 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
8995 "Compilation Unit Length");
8997 FOR_EACH_VEC_ELT (*names, i, pub)
8999 /* Enumerator names are part of the pubname table, but the parent
9000 DW_TAG_enumeration_type die may have been pruned. Don't output
9001 them if that is the case. */
9002 if (pub->die->die_tag == DW_TAG_enumerator && !pub->die->die_mark)
9003 continue;
9005 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9006 if (names == pubname_table)
9007 gcc_assert (pub->die->die_mark);
9009 if (names != pubtype_table
9010 || pub->die->die_offset != 0
9011 || !flag_eliminate_unused_debug_types)
9013 dw_offset die_offset = pub->die->die_offset;
9015 /* If we're putting types in their own .debug_types sections,
9016 the .debug_pubtypes table will still point to the compile
9017 unit (not the type unit), so we want to use the offset of
9018 the skeleton DIE (if there is one). */
9019 if (pub->die->comdat_type_p && names == pubtype_table)
9021 comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
9023 if (type_node != NULL)
9024 die_offset = (type_node->skeleton_die != NULL
9025 ? type_node->skeleton_die->die_offset
9026 : 0);
9029 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9031 dw2_asm_output_nstring (pub->name, -1, "external name");
9035 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9038 /* Output the information that goes into the .debug_aranges table.
9039 Namely, define the beginning and ending address range of the
9040 text section generated for this compilation unit. */
9042 static void
9043 output_aranges (unsigned long aranges_length)
9045 unsigned i;
9047 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9048 dw2_asm_output_data (4, 0xffffffff,
9049 "Initial length escape value indicating 64-bit DWARF extension");
9050 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9051 "Length of Address Ranges Info");
9052 /* Version number for aranges is still 2, even in DWARF3. */
9053 dw2_asm_output_data (2, 2, "DWARF Version");
9054 if (dwarf_split_debug_info)
9055 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9056 debug_skeleton_info_section,
9057 "Offset of Compilation Unit Info");
9058 else
9059 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9060 debug_info_section,
9061 "Offset of Compilation Unit Info");
9062 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9063 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9065 /* We need to align to twice the pointer size here. */
9066 if (DWARF_ARANGES_PAD_SIZE)
9068 /* Pad using a 2 byte words so that padding is correct for any
9069 pointer size. */
9070 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9071 2 * DWARF2_ADDR_SIZE);
9072 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9073 dw2_asm_output_data (2, 0, NULL);
9076 /* It is necessary not to output these entries if the sections were
9077 not used; if the sections were not used, the length will be 0 and
9078 the address may end up as 0 if the section is discarded by ld
9079 --gc-sections, leaving an invalid (0, 0) entry that can be
9080 confused with the terminator. */
9081 if (text_section_used)
9083 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9084 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9085 text_section_label, "Length");
9087 if (cold_text_section_used)
9089 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9090 "Address");
9091 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9092 cold_text_section_label, "Length");
9095 if (have_multiple_function_sections)
9097 unsigned fde_idx;
9098 dw_fde_ref fde;
9100 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9102 if (DECL_IGNORED_P (fde->decl))
9103 continue;
9104 if (!fde->in_std_section)
9106 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9107 "Address");
9108 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9109 fde->dw_fde_begin, "Length");
9111 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9113 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9114 "Address");
9115 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9116 fde->dw_fde_second_begin, "Length");
9121 /* Output the terminator words. */
9122 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9123 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9126 /* Add a new entry to .debug_ranges. Return the offset at which it
9127 was placed. */
9129 static unsigned int
9130 add_ranges_num (int num)
9132 unsigned int in_use = ranges_table_in_use;
9134 if (in_use == ranges_table_allocated)
9136 ranges_table_allocated += RANGES_TABLE_INCREMENT;
9137 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9138 ranges_table_allocated);
9139 memset (ranges_table + ranges_table_in_use, 0,
9140 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9143 ranges_table[in_use].num = num;
9144 ranges_table_in_use = in_use + 1;
9146 return in_use * 2 * DWARF2_ADDR_SIZE;
9149 /* Add a new entry to .debug_ranges corresponding to a block, or a
9150 range terminator if BLOCK is NULL. */
9152 static unsigned int
9153 add_ranges (const_tree block)
9155 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9158 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9159 When using dwarf_split_debug_info, address attributes in dies destined
9160 for the final executable should be direct references--setting the
9161 parameter force_direct ensures this behavior. */
9163 static void
9164 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9165 bool *added, bool force_direct)
9167 unsigned int in_use = ranges_by_label_in_use;
9168 unsigned int offset;
9170 if (in_use == ranges_by_label_allocated)
9172 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9173 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9174 ranges_by_label,
9175 ranges_by_label_allocated);
9176 memset (ranges_by_label + ranges_by_label_in_use, 0,
9177 RANGES_TABLE_INCREMENT
9178 * sizeof (struct dw_ranges_by_label_struct));
9181 ranges_by_label[in_use].begin = begin;
9182 ranges_by_label[in_use].end = end;
9183 ranges_by_label_in_use = in_use + 1;
9185 offset = add_ranges_num (-(int)in_use - 1);
9186 if (!*added)
9188 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9189 *added = true;
9193 static void
9194 output_ranges (void)
9196 unsigned i;
9197 static const char *const start_fmt = "Offset %#x";
9198 const char *fmt = start_fmt;
9200 for (i = 0; i < ranges_table_in_use; i++)
9202 int block_num = ranges_table[i].num;
9204 if (block_num > 0)
9206 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9207 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9209 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9210 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9212 /* If all code is in the text section, then the compilation
9213 unit base address defaults to DW_AT_low_pc, which is the
9214 base of the text section. */
9215 if (!have_multiple_function_sections)
9217 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9218 text_section_label,
9219 fmt, i * 2 * DWARF2_ADDR_SIZE);
9220 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9221 text_section_label, NULL);
9224 /* Otherwise, the compilation unit base address is zero,
9225 which allows us to use absolute addresses, and not worry
9226 about whether the target supports cross-section
9227 arithmetic. */
9228 else
9230 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9231 fmt, i * 2 * DWARF2_ADDR_SIZE);
9232 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9235 fmt = NULL;
9238 /* Negative block_num stands for an index into ranges_by_label. */
9239 else if (block_num < 0)
9241 int lab_idx = - block_num - 1;
9243 if (!have_multiple_function_sections)
9245 gcc_unreachable ();
9246 #if 0
9247 /* If we ever use add_ranges_by_labels () for a single
9248 function section, all we have to do is to take out
9249 the #if 0 above. */
9250 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9251 ranges_by_label[lab_idx].begin,
9252 text_section_label,
9253 fmt, i * 2 * DWARF2_ADDR_SIZE);
9254 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9255 ranges_by_label[lab_idx].end,
9256 text_section_label, NULL);
9257 #endif
9259 else
9261 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9262 ranges_by_label[lab_idx].begin,
9263 fmt, i * 2 * DWARF2_ADDR_SIZE);
9264 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9265 ranges_by_label[lab_idx].end,
9266 NULL);
9269 else
9271 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9272 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9273 fmt = start_fmt;
9278 /* Data structure containing information about input files. */
9279 struct file_info
9281 const char *path; /* Complete file name. */
9282 const char *fname; /* File name part. */
9283 int length; /* Length of entire string. */
9284 struct dwarf_file_data * file_idx; /* Index in input file table. */
9285 int dir_idx; /* Index in directory table. */
9288 /* Data structure containing information about directories with source
9289 files. */
9290 struct dir_info
9292 const char *path; /* Path including directory name. */
9293 int length; /* Path length. */
9294 int prefix; /* Index of directory entry which is a prefix. */
9295 int count; /* Number of files in this directory. */
9296 int dir_idx; /* Index of directory used as base. */
9299 /* Callback function for file_info comparison. We sort by looking at
9300 the directories in the path. */
9302 static int
9303 file_info_cmp (const void *p1, const void *p2)
9305 const struct file_info *const s1 = (const struct file_info *) p1;
9306 const struct file_info *const s2 = (const struct file_info *) p2;
9307 const unsigned char *cp1;
9308 const unsigned char *cp2;
9310 /* Take care of file names without directories. We need to make sure that
9311 we return consistent values to qsort since some will get confused if
9312 we return the same value when identical operands are passed in opposite
9313 orders. So if neither has a directory, return 0 and otherwise return
9314 1 or -1 depending on which one has the directory. */
9315 if ((s1->path == s1->fname || s2->path == s2->fname))
9316 return (s2->path == s2->fname) - (s1->path == s1->fname);
9318 cp1 = (const unsigned char *) s1->path;
9319 cp2 = (const unsigned char *) s2->path;
9321 while (1)
9323 ++cp1;
9324 ++cp2;
9325 /* Reached the end of the first path? If so, handle like above. */
9326 if ((cp1 == (const unsigned char *) s1->fname)
9327 || (cp2 == (const unsigned char *) s2->fname))
9328 return ((cp2 == (const unsigned char *) s2->fname)
9329 - (cp1 == (const unsigned char *) s1->fname));
9331 /* Character of current path component the same? */
9332 else if (*cp1 != *cp2)
9333 return *cp1 - *cp2;
9337 struct file_name_acquire_data
9339 struct file_info *files;
9340 int used_files;
9341 int max_files;
9344 /* Traversal function for the hash table. */
9346 static int
9347 file_name_acquire (void ** slot, void *data)
9349 struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
9350 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
9351 struct file_info *fi;
9352 const char *f;
9354 gcc_assert (fnad->max_files >= d->emitted_number);
9356 if (! d->emitted_number)
9357 return 1;
9359 gcc_assert (fnad->max_files != fnad->used_files);
9361 fi = fnad->files + fnad->used_files++;
9363 /* Skip all leading "./". */
9364 f = d->filename;
9365 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9366 f += 2;
9368 /* Create a new array entry. */
9369 fi->path = f;
9370 fi->length = strlen (f);
9371 fi->file_idx = d;
9373 /* Search for the file name part. */
9374 f = strrchr (f, DIR_SEPARATOR);
9375 #if defined (DIR_SEPARATOR_2)
9377 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9379 if (g != NULL)
9381 if (f == NULL || f < g)
9382 f = g;
9385 #endif
9387 fi->fname = f == NULL ? fi->path : f + 1;
9388 return 1;
9391 /* Output the directory table and the file name table. We try to minimize
9392 the total amount of memory needed. A heuristic is used to avoid large
9393 slowdowns with many input files. */
9395 static void
9396 output_file_names (void)
9398 struct file_name_acquire_data fnad;
9399 int numfiles;
9400 struct file_info *files;
9401 struct dir_info *dirs;
9402 int *saved;
9403 int *savehere;
9404 int *backmap;
9405 int ndirs;
9406 int idx_offset;
9407 int i;
9409 if (!last_emitted_file)
9411 dw2_asm_output_data (1, 0, "End directory table");
9412 dw2_asm_output_data (1, 0, "End file name table");
9413 return;
9416 numfiles = last_emitted_file->emitted_number;
9418 /* Allocate the various arrays we need. */
9419 files = XALLOCAVEC (struct file_info, numfiles);
9420 dirs = XALLOCAVEC (struct dir_info, numfiles);
9422 fnad.files = files;
9423 fnad.used_files = 0;
9424 fnad.max_files = numfiles;
9425 htab_traverse (file_table, file_name_acquire, &fnad);
9426 gcc_assert (fnad.used_files == fnad.max_files);
9428 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9430 /* Find all the different directories used. */
9431 dirs[0].path = files[0].path;
9432 dirs[0].length = files[0].fname - files[0].path;
9433 dirs[0].prefix = -1;
9434 dirs[0].count = 1;
9435 dirs[0].dir_idx = 0;
9436 files[0].dir_idx = 0;
9437 ndirs = 1;
9439 for (i = 1; i < numfiles; i++)
9440 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9441 && memcmp (dirs[ndirs - 1].path, files[i].path,
9442 dirs[ndirs - 1].length) == 0)
9444 /* Same directory as last entry. */
9445 files[i].dir_idx = ndirs - 1;
9446 ++dirs[ndirs - 1].count;
9448 else
9450 int j;
9452 /* This is a new directory. */
9453 dirs[ndirs].path = files[i].path;
9454 dirs[ndirs].length = files[i].fname - files[i].path;
9455 dirs[ndirs].count = 1;
9456 dirs[ndirs].dir_idx = ndirs;
9457 files[i].dir_idx = ndirs;
9459 /* Search for a prefix. */
9460 dirs[ndirs].prefix = -1;
9461 for (j = 0; j < ndirs; j++)
9462 if (dirs[j].length < dirs[ndirs].length
9463 && dirs[j].length > 1
9464 && (dirs[ndirs].prefix == -1
9465 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9466 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9467 dirs[ndirs].prefix = j;
9469 ++ndirs;
9472 /* Now to the actual work. We have to find a subset of the directories which
9473 allow expressing the file name using references to the directory table
9474 with the least amount of characters. We do not do an exhaustive search
9475 where we would have to check out every combination of every single
9476 possible prefix. Instead we use a heuristic which provides nearly optimal
9477 results in most cases and never is much off. */
9478 saved = XALLOCAVEC (int, ndirs);
9479 savehere = XALLOCAVEC (int, ndirs);
9481 memset (saved, '\0', ndirs * sizeof (saved[0]));
9482 for (i = 0; i < ndirs; i++)
9484 int j;
9485 int total;
9487 /* We can always save some space for the current directory. But this
9488 does not mean it will be enough to justify adding the directory. */
9489 savehere[i] = dirs[i].length;
9490 total = (savehere[i] - saved[i]) * dirs[i].count;
9492 for (j = i + 1; j < ndirs; j++)
9494 savehere[j] = 0;
9495 if (saved[j] < dirs[i].length)
9497 /* Determine whether the dirs[i] path is a prefix of the
9498 dirs[j] path. */
9499 int k;
9501 k = dirs[j].prefix;
9502 while (k != -1 && k != (int) i)
9503 k = dirs[k].prefix;
9505 if (k == (int) i)
9507 /* Yes it is. We can possibly save some memory by
9508 writing the filenames in dirs[j] relative to
9509 dirs[i]. */
9510 savehere[j] = dirs[i].length;
9511 total += (savehere[j] - saved[j]) * dirs[j].count;
9516 /* Check whether we can save enough to justify adding the dirs[i]
9517 directory. */
9518 if (total > dirs[i].length + 1)
9520 /* It's worthwhile adding. */
9521 for (j = i; j < ndirs; j++)
9522 if (savehere[j] > 0)
9524 /* Remember how much we saved for this directory so far. */
9525 saved[j] = savehere[j];
9527 /* Remember the prefix directory. */
9528 dirs[j].dir_idx = i;
9533 /* Emit the directory name table. */
9534 idx_offset = dirs[0].length > 0 ? 1 : 0;
9535 for (i = 1 - idx_offset; i < ndirs; i++)
9536 dw2_asm_output_nstring (dirs[i].path,
9537 dirs[i].length
9538 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9539 "Directory Entry: %#x", i + idx_offset);
9541 dw2_asm_output_data (1, 0, "End directory table");
9543 /* We have to emit them in the order of emitted_number since that's
9544 used in the debug info generation. To do this efficiently we
9545 generate a back-mapping of the indices first. */
9546 backmap = XALLOCAVEC (int, numfiles);
9547 for (i = 0; i < numfiles; i++)
9548 backmap[files[i].file_idx->emitted_number - 1] = i;
9550 /* Now write all the file names. */
9551 for (i = 0; i < numfiles; i++)
9553 int file_idx = backmap[i];
9554 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9556 #ifdef VMS_DEBUGGING_INFO
9557 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
9559 /* Setting these fields can lead to debugger miscomparisons,
9560 but VMS Debug requires them to be set correctly. */
9562 int ver;
9563 long long cdt;
9564 long siz;
9565 int maxfilelen = strlen (files[file_idx].path)
9566 + dirs[dir_idx].length
9567 + MAX_VMS_VERSION_LEN + 1;
9568 char *filebuf = XALLOCAVEC (char, maxfilelen);
9570 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
9571 snprintf (filebuf, maxfilelen, "%s;%d",
9572 files[file_idx].path + dirs[dir_idx].length, ver);
9574 dw2_asm_output_nstring
9575 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
9577 /* Include directory index. */
9578 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9580 /* Modification time. */
9581 dw2_asm_output_data_uleb128
9582 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
9583 ? cdt : 0,
9584 NULL);
9586 /* File length in bytes. */
9587 dw2_asm_output_data_uleb128
9588 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
9589 ? siz : 0,
9590 NULL);
9591 #else
9592 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
9593 "File Entry: %#x", (unsigned) i + 1);
9595 /* Include directory index. */
9596 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9598 /* Modification time. */
9599 dw2_asm_output_data_uleb128 (0, NULL);
9601 /* File length in bytes. */
9602 dw2_asm_output_data_uleb128 (0, NULL);
9603 #endif /* VMS_DEBUGGING_INFO */
9606 dw2_asm_output_data (1, 0, "End file name table");
9610 /* Output one line number table into the .debug_line section. */
9612 static void
9613 output_one_line_info_table (dw_line_info_table *table)
9615 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9616 unsigned int current_line = 1;
9617 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
9618 dw_line_info_entry *ent;
9619 size_t i;
9621 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
9623 switch (ent->opcode)
9625 case LI_set_address:
9626 /* ??? Unfortunately, we have little choice here currently, and
9627 must always use the most general form. GCC does not know the
9628 address delta itself, so we can't use DW_LNS_advance_pc. Many
9629 ports do have length attributes which will give an upper bound
9630 on the address range. We could perhaps use length attributes
9631 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
9632 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
9634 /* This can handle any delta. This takes
9635 4+DWARF2_ADDR_SIZE bytes. */
9636 dw2_asm_output_data (1, 0, "set address %s", line_label);
9637 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9638 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9639 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9640 break;
9642 case LI_set_line:
9643 if (ent->val == current_line)
9645 /* We still need to start a new row, so output a copy insn. */
9646 dw2_asm_output_data (1, DW_LNS_copy,
9647 "copy line %u", current_line);
9649 else
9651 int line_offset = ent->val - current_line;
9652 int line_delta = line_offset - DWARF_LINE_BASE;
9654 current_line = ent->val;
9655 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9657 /* This can handle deltas from -10 to 234, using the current
9658 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
9659 This takes 1 byte. */
9660 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9661 "line %u", current_line);
9663 else
9665 /* This can handle any delta. This takes at least 4 bytes,
9666 depending on the value being encoded. */
9667 dw2_asm_output_data (1, DW_LNS_advance_line,
9668 "advance to line %u", current_line);
9669 dw2_asm_output_data_sleb128 (line_offset, NULL);
9670 dw2_asm_output_data (1, DW_LNS_copy, NULL);
9673 break;
9675 case LI_set_file:
9676 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
9677 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9678 break;
9680 case LI_set_column:
9681 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
9682 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9683 break;
9685 case LI_negate_stmt:
9686 current_is_stmt = !current_is_stmt;
9687 dw2_asm_output_data (1, DW_LNS_negate_stmt,
9688 "is_stmt %d", current_is_stmt);
9689 break;
9691 case LI_set_prologue_end:
9692 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
9693 "set prologue end");
9694 break;
9696 case LI_set_epilogue_begin:
9697 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
9698 "set epilogue begin");
9699 break;
9701 case LI_set_discriminator:
9702 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
9703 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
9704 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
9705 dw2_asm_output_data_uleb128 (ent->val, NULL);
9706 break;
9710 /* Emit debug info for the address of the end of the table. */
9711 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
9712 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9713 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9714 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
9716 dw2_asm_output_data (1, 0, "end sequence");
9717 dw2_asm_output_data_uleb128 (1, NULL);
9718 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9721 /* Output the source line number correspondence information. This
9722 information goes into the .debug_line section. */
9724 static void
9725 output_line_info (bool prologue_only)
9727 char l1[20], l2[20], p1[20], p2[20];
9728 int ver = dwarf_version;
9729 bool saw_one = false;
9730 int opc;
9732 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
9733 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
9734 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
9735 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
9737 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9738 dw2_asm_output_data (4, 0xffffffff,
9739 "Initial length escape value indicating 64-bit DWARF extension");
9740 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
9741 "Length of Source Line Info");
9742 ASM_OUTPUT_LABEL (asm_out_file, l1);
9744 dw2_asm_output_data (2, ver, "DWARF Version");
9745 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
9746 ASM_OUTPUT_LABEL (asm_out_file, p1);
9748 /* Define the architecture-dependent minimum instruction length (in bytes).
9749 In this implementation of DWARF, this field is used for information
9750 purposes only. Since GCC generates assembly language, we have no
9751 a priori knowledge of how many instruction bytes are generated for each
9752 source line, and therefore can use only the DW_LNE_set_address and
9753 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
9754 this as '1', which is "correct enough" for all architectures,
9755 and don't let the target override. */
9756 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
9758 if (ver >= 4)
9759 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
9760 "Maximum Operations Per Instruction");
9761 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
9762 "Default is_stmt_start flag");
9763 dw2_asm_output_data (1, DWARF_LINE_BASE,
9764 "Line Base Value (Special Opcodes)");
9765 dw2_asm_output_data (1, DWARF_LINE_RANGE,
9766 "Line Range Value (Special Opcodes)");
9767 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
9768 "Special Opcode Base");
9770 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
9772 int n_op_args;
9773 switch (opc)
9775 case DW_LNS_advance_pc:
9776 case DW_LNS_advance_line:
9777 case DW_LNS_set_file:
9778 case DW_LNS_set_column:
9779 case DW_LNS_fixed_advance_pc:
9780 case DW_LNS_set_isa:
9781 n_op_args = 1;
9782 break;
9783 default:
9784 n_op_args = 0;
9785 break;
9788 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
9789 opc, n_op_args);
9792 /* Write out the information about the files we use. */
9793 output_file_names ();
9794 ASM_OUTPUT_LABEL (asm_out_file, p2);
9795 if (prologue_only)
9797 /* Output the marker for the end of the line number info. */
9798 ASM_OUTPUT_LABEL (asm_out_file, l2);
9799 return;
9802 if (separate_line_info)
9804 dw_line_info_table *table;
9805 size_t i;
9807 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
9808 if (table->in_use)
9810 output_one_line_info_table (table);
9811 saw_one = true;
9814 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
9816 output_one_line_info_table (cold_text_section_line_info);
9817 saw_one = true;
9820 /* ??? Some Darwin linkers crash on a .debug_line section with no
9821 sequences. Further, merely a DW_LNE_end_sequence entry is not
9822 sufficient -- the address column must also be initialized.
9823 Make sure to output at least one set_address/end_sequence pair,
9824 choosing .text since that section is always present. */
9825 if (text_section_line_info->in_use || !saw_one)
9826 output_one_line_info_table (text_section_line_info);
9828 /* Output the marker for the end of the line number info. */
9829 ASM_OUTPUT_LABEL (asm_out_file, l2);
9832 /* Given a pointer to a tree node for some base type, return a pointer to
9833 a DIE that describes the given type.
9835 This routine must only be called for GCC type nodes that correspond to
9836 Dwarf base (fundamental) types. */
9838 static dw_die_ref
9839 base_type_die (tree type)
9841 dw_die_ref base_type_result;
9842 enum dwarf_type encoding;
9844 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
9845 return 0;
9847 /* If this is a subtype that should not be emitted as a subrange type,
9848 use the base type. See subrange_type_for_debug_p. */
9849 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
9850 type = TREE_TYPE (type);
9852 switch (TREE_CODE (type))
9854 case INTEGER_TYPE:
9855 if ((dwarf_version >= 4 || !dwarf_strict)
9856 && TYPE_NAME (type)
9857 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
9858 && DECL_IS_BUILTIN (TYPE_NAME (type))
9859 && DECL_NAME (TYPE_NAME (type)))
9861 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
9862 if (strcmp (name, "char16_t") == 0
9863 || strcmp (name, "char32_t") == 0)
9865 encoding = DW_ATE_UTF;
9866 break;
9869 if (TYPE_STRING_FLAG (type))
9871 if (TYPE_UNSIGNED (type))
9872 encoding = DW_ATE_unsigned_char;
9873 else
9874 encoding = DW_ATE_signed_char;
9876 else if (TYPE_UNSIGNED (type))
9877 encoding = DW_ATE_unsigned;
9878 else
9879 encoding = DW_ATE_signed;
9880 break;
9882 case REAL_TYPE:
9883 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
9885 if (dwarf_version >= 3 || !dwarf_strict)
9886 encoding = DW_ATE_decimal_float;
9887 else
9888 encoding = DW_ATE_lo_user;
9890 else
9891 encoding = DW_ATE_float;
9892 break;
9894 case FIXED_POINT_TYPE:
9895 if (!(dwarf_version >= 3 || !dwarf_strict))
9896 encoding = DW_ATE_lo_user;
9897 else if (TYPE_UNSIGNED (type))
9898 encoding = DW_ATE_unsigned_fixed;
9899 else
9900 encoding = DW_ATE_signed_fixed;
9901 break;
9903 /* Dwarf2 doesn't know anything about complex ints, so use
9904 a user defined type for it. */
9905 case COMPLEX_TYPE:
9906 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
9907 encoding = DW_ATE_complex_float;
9908 else
9909 encoding = DW_ATE_lo_user;
9910 break;
9912 case BOOLEAN_TYPE:
9913 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
9914 encoding = DW_ATE_boolean;
9915 break;
9917 default:
9918 /* No other TREE_CODEs are Dwarf fundamental types. */
9919 gcc_unreachable ();
9922 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
9924 add_AT_unsigned (base_type_result, DW_AT_byte_size,
9925 int_size_in_bytes (type));
9926 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
9927 add_pubtype (type, base_type_result);
9929 return base_type_result;
9932 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
9933 given input type is a Dwarf "fundamental" type. Otherwise return null. */
9935 static inline int
9936 is_base_type (tree type)
9938 switch (TREE_CODE (type))
9940 case ERROR_MARK:
9941 case VOID_TYPE:
9942 case INTEGER_TYPE:
9943 case REAL_TYPE:
9944 case FIXED_POINT_TYPE:
9945 case COMPLEX_TYPE:
9946 case BOOLEAN_TYPE:
9947 return 1;
9949 case ARRAY_TYPE:
9950 case RECORD_TYPE:
9951 case UNION_TYPE:
9952 case QUAL_UNION_TYPE:
9953 case ENUMERAL_TYPE:
9954 case FUNCTION_TYPE:
9955 case METHOD_TYPE:
9956 case POINTER_TYPE:
9957 case REFERENCE_TYPE:
9958 case NULLPTR_TYPE:
9959 case OFFSET_TYPE:
9960 case LANG_TYPE:
9961 case VECTOR_TYPE:
9962 return 0;
9964 default:
9965 gcc_unreachable ();
9968 return 0;
9971 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
9972 node, return the size in bits for the type if it is a constant, or else
9973 return the alignment for the type if the type's size is not constant, or
9974 else return BITS_PER_WORD if the type actually turns out to be an
9975 ERROR_MARK node. */
9977 static inline unsigned HOST_WIDE_INT
9978 simple_type_size_in_bits (const_tree type)
9980 if (TREE_CODE (type) == ERROR_MARK)
9981 return BITS_PER_WORD;
9982 else if (TYPE_SIZE (type) == NULL_TREE)
9983 return 0;
9984 else if (host_integerp (TYPE_SIZE (type), 1))
9985 return tree_low_cst (TYPE_SIZE (type), 1);
9986 else
9987 return TYPE_ALIGN (type);
9990 /* Similarly, but return a double_int instead of UHWI. */
9992 static inline double_int
9993 double_int_type_size_in_bits (const_tree type)
9995 if (TREE_CODE (type) == ERROR_MARK)
9996 return double_int::from_uhwi (BITS_PER_WORD);
9997 else if (TYPE_SIZE (type) == NULL_TREE)
9998 return double_int_zero;
9999 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10000 return tree_to_double_int (TYPE_SIZE (type));
10001 else
10002 return double_int::from_uhwi (TYPE_ALIGN (type));
10005 /* Given a pointer to a tree node for a subrange type, return a pointer
10006 to a DIE that describes the given type. */
10008 static dw_die_ref
10009 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10011 dw_die_ref subrange_die;
10012 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10014 if (context_die == NULL)
10015 context_die = comp_unit_die ();
10017 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10019 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10021 /* The size of the subrange type and its base type do not match,
10022 so we need to generate a size attribute for the subrange type. */
10023 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10026 if (low)
10027 add_bound_info (subrange_die, DW_AT_lower_bound, low);
10028 if (high)
10029 add_bound_info (subrange_die, DW_AT_upper_bound, high);
10031 return subrange_die;
10034 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10035 entry that chains various modifiers in front of the given type. */
10037 static dw_die_ref
10038 modified_type_die (tree type, int is_const_type, int is_volatile_type,
10039 dw_die_ref context_die)
10041 enum tree_code code = TREE_CODE (type);
10042 dw_die_ref mod_type_die;
10043 dw_die_ref sub_die = NULL;
10044 tree item_type = NULL;
10045 tree qualified_type;
10046 tree name, low, high;
10047 dw_die_ref mod_scope;
10049 if (code == ERROR_MARK)
10050 return NULL;
10052 /* See if we already have the appropriately qualified variant of
10053 this type. */
10054 qualified_type
10055 = get_qualified_type (type,
10056 ((is_const_type ? TYPE_QUAL_CONST : 0)
10057 | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
10059 if (qualified_type == sizetype
10060 && TYPE_NAME (qualified_type)
10061 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10063 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10065 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10066 && TYPE_PRECISION (t)
10067 == TYPE_PRECISION (qualified_type)
10068 && TYPE_UNSIGNED (t)
10069 == TYPE_UNSIGNED (qualified_type));
10070 qualified_type = t;
10073 /* If we do, then we can just use its DIE, if it exists. */
10074 if (qualified_type)
10076 mod_type_die = lookup_type_die (qualified_type);
10077 if (mod_type_die)
10078 return mod_type_die;
10081 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10083 /* Handle C typedef types. */
10084 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10085 && !DECL_ARTIFICIAL (name))
10087 tree dtype = TREE_TYPE (name);
10089 if (qualified_type == dtype)
10091 /* For a named type, use the typedef. */
10092 gen_type_die (qualified_type, context_die);
10093 return lookup_type_die (qualified_type);
10095 else if (is_const_type < TYPE_READONLY (dtype)
10096 || is_volatile_type < TYPE_VOLATILE (dtype)
10097 || (is_const_type <= TYPE_READONLY (dtype)
10098 && is_volatile_type <= TYPE_VOLATILE (dtype)
10099 && DECL_ORIGINAL_TYPE (name) != type))
10100 /* cv-unqualified version of named type. Just use the unnamed
10101 type to which it refers. */
10102 return modified_type_die (DECL_ORIGINAL_TYPE (name),
10103 is_const_type, is_volatile_type,
10104 context_die);
10105 /* Else cv-qualified version of named type; fall through. */
10108 mod_scope = scope_die_for (type, context_die);
10110 if (is_const_type
10111 /* If both is_const_type and is_volatile_type, prefer the path
10112 which leads to a qualified type. */
10113 && (!is_volatile_type
10114 || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
10115 || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
10117 mod_type_die = new_die (DW_TAG_const_type, mod_scope, type);
10118 sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
10120 else if (is_volatile_type)
10122 mod_type_die = new_die (DW_TAG_volatile_type, mod_scope, type);
10123 sub_die = modified_type_die (type, is_const_type, 0, context_die);
10125 else if (code == POINTER_TYPE)
10127 mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10128 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10129 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10130 item_type = TREE_TYPE (type);
10131 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10132 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10133 TYPE_ADDR_SPACE (item_type));
10135 else if (code == REFERENCE_TYPE)
10137 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10138 mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10139 type);
10140 else
10141 mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10142 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10143 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10144 item_type = TREE_TYPE (type);
10145 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10146 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10147 TYPE_ADDR_SPACE (item_type));
10149 else if (code == INTEGER_TYPE
10150 && TREE_TYPE (type) != NULL_TREE
10151 && subrange_type_for_debug_p (type, &low, &high))
10153 mod_type_die = subrange_type_die (type, low, high, context_die);
10154 item_type = TREE_TYPE (type);
10156 else if (is_base_type (type))
10157 mod_type_die = base_type_die (type);
10158 else
10160 gen_type_die (type, context_die);
10162 /* We have to get the type_main_variant here (and pass that to the
10163 `lookup_type_die' routine) because the ..._TYPE node we have
10164 might simply be a *copy* of some original type node (where the
10165 copy was created to help us keep track of typedef names) and
10166 that copy might have a different TYPE_UID from the original
10167 ..._TYPE node. */
10168 if (TREE_CODE (type) != VECTOR_TYPE)
10169 return lookup_type_die (type_main_variant (type));
10170 else
10171 /* Vectors have the debugging information in the type,
10172 not the main variant. */
10173 return lookup_type_die (type);
10176 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
10177 don't output a DW_TAG_typedef, since there isn't one in the
10178 user's program; just attach a DW_AT_name to the type.
10179 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10180 if the base type already has the same name. */
10181 if (name
10182 && ((TREE_CODE (name) != TYPE_DECL
10183 && (qualified_type == TYPE_MAIN_VARIANT (type)
10184 || (!is_const_type && !is_volatile_type)))
10185 || (TREE_CODE (name) == TYPE_DECL
10186 && TREE_TYPE (name) == qualified_type
10187 && DECL_NAME (name))))
10189 if (TREE_CODE (name) == TYPE_DECL)
10190 /* Could just call add_name_and_src_coords_attributes here,
10191 but since this is a builtin type it doesn't have any
10192 useful source coordinates anyway. */
10193 name = DECL_NAME (name);
10194 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10196 /* This probably indicates a bug. */
10197 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10199 name = TYPE_NAME (type);
10200 if (name
10201 && TREE_CODE (name) == TYPE_DECL)
10202 name = DECL_NAME (name);
10203 add_name_attribute (mod_type_die,
10204 name ? IDENTIFIER_POINTER (name) : "__unknown__");
10207 if (qualified_type)
10208 equate_type_number_to_die (qualified_type, mod_type_die);
10210 if (item_type)
10211 /* We must do this after the equate_type_number_to_die call, in case
10212 this is a recursive type. This ensures that the modified_type_die
10213 recursion will terminate even if the type is recursive. Recursive
10214 types are possible in Ada. */
10215 sub_die = modified_type_die (item_type,
10216 TYPE_READONLY (item_type),
10217 TYPE_VOLATILE (item_type),
10218 context_die);
10220 if (sub_die != NULL)
10221 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10223 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10224 if (TYPE_ARTIFICIAL (type))
10225 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10227 return mod_type_die;
10230 /* Generate DIEs for the generic parameters of T.
10231 T must be either a generic type or a generic function.
10232 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
10234 static void
10235 gen_generic_params_dies (tree t)
10237 tree parms, args;
10238 int parms_num, i;
10239 dw_die_ref die = NULL;
10241 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10242 return;
10244 if (TYPE_P (t))
10245 die = lookup_type_die (t);
10246 else if (DECL_P (t))
10247 die = lookup_decl_die (t);
10249 gcc_assert (die);
10251 parms = lang_hooks.get_innermost_generic_parms (t);
10252 if (!parms)
10253 /* T has no generic parameter. It means T is neither a generic type
10254 or function. End of story. */
10255 return;
10257 parms_num = TREE_VEC_LENGTH (parms);
10258 args = lang_hooks.get_innermost_generic_args (t);
10259 for (i = 0; i < parms_num; i++)
10261 tree parm, arg, arg_pack_elems;
10263 parm = TREE_VEC_ELT (parms, i);
10264 arg = TREE_VEC_ELT (args, i);
10265 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
10266 gcc_assert (parm && TREE_VALUE (parm) && arg);
10268 if (parm && TREE_VALUE (parm) && arg)
10270 /* If PARM represents a template parameter pack,
10271 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
10272 by DW_TAG_template_*_parameter DIEs for the argument
10273 pack elements of ARG. Note that ARG would then be
10274 an argument pack. */
10275 if (arg_pack_elems)
10276 template_parameter_pack_die (TREE_VALUE (parm),
10277 arg_pack_elems,
10278 die);
10279 else
10280 generic_parameter_die (TREE_VALUE (parm), arg,
10281 true /* Emit DW_AT_name */, die);
10286 /* Create and return a DIE for PARM which should be
10287 the representation of a generic type parameter.
10288 For instance, in the C++ front end, PARM would be a template parameter.
10289 ARG is the argument to PARM.
10290 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
10291 name of the PARM.
10292 PARENT_DIE is the parent DIE which the new created DIE should be added to,
10293 as a child node. */
10295 static dw_die_ref
10296 generic_parameter_die (tree parm, tree arg,
10297 bool emit_name_p,
10298 dw_die_ref parent_die)
10300 dw_die_ref tmpl_die = NULL;
10301 const char *name = NULL;
10303 if (!parm || !DECL_NAME (parm) || !arg)
10304 return NULL;
10306 /* We support non-type generic parameters and arguments,
10307 type generic parameters and arguments, as well as
10308 generic generic parameters (a.k.a. template template parameters in C++)
10309 and arguments. */
10310 if (TREE_CODE (parm) == PARM_DECL)
10311 /* PARM is a nontype generic parameter */
10312 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10313 else if (TREE_CODE (parm) == TYPE_DECL)
10314 /* PARM is a type generic parameter. */
10315 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10316 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10317 /* PARM is a generic generic parameter.
10318 Its DIE is a GNU extension. It shall have a
10319 DW_AT_name attribute to represent the name of the template template
10320 parameter, and a DW_AT_GNU_template_name attribute to represent the
10321 name of the template template argument. */
10322 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10323 parent_die, parm);
10324 else
10325 gcc_unreachable ();
10327 if (tmpl_die)
10329 tree tmpl_type;
10331 /* If PARM is a generic parameter pack, it means we are
10332 emitting debug info for a template argument pack element.
10333 In other terms, ARG is a template argument pack element.
10334 In that case, we don't emit any DW_AT_name attribute for
10335 the die. */
10336 if (emit_name_p)
10338 name = IDENTIFIER_POINTER (DECL_NAME (parm));
10339 gcc_assert (name);
10340 add_AT_string (tmpl_die, DW_AT_name, name);
10343 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10345 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10346 TMPL_DIE should have a child DW_AT_type attribute that is set
10347 to the type of the argument to PARM, which is ARG.
10348 If PARM is a type generic parameter, TMPL_DIE should have a
10349 child DW_AT_type that is set to ARG. */
10350 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10351 add_type_attribute (tmpl_die, tmpl_type, 0,
10352 TREE_THIS_VOLATILE (tmpl_type),
10353 parent_die);
10355 else
10357 /* So TMPL_DIE is a DIE representing a
10358 a generic generic template parameter, a.k.a template template
10359 parameter in C++ and arg is a template. */
10361 /* The DW_AT_GNU_template_name attribute of the DIE must be set
10362 to the name of the argument. */
10363 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
10364 if (name)
10365 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
10368 if (TREE_CODE (parm) == PARM_DECL)
10369 /* So PARM is a non-type generic parameter.
10370 DWARF3 5.6.8 says we must set a DW_AT_const_value child
10371 attribute of TMPL_DIE which value represents the value
10372 of ARG.
10373 We must be careful here:
10374 The value of ARG might reference some function decls.
10375 We might currently be emitting debug info for a generic
10376 type and types are emitted before function decls, we don't
10377 know if the function decls referenced by ARG will actually be
10378 emitted after cgraph computations.
10379 So must defer the generation of the DW_AT_const_value to
10380 after cgraph is ready. */
10381 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
10384 return tmpl_die;
10387 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
10388 PARM_PACK must be a template parameter pack. The returned DIE
10389 will be child DIE of PARENT_DIE. */
10391 static dw_die_ref
10392 template_parameter_pack_die (tree parm_pack,
10393 tree parm_pack_args,
10394 dw_die_ref parent_die)
10396 dw_die_ref die;
10397 int j;
10399 gcc_assert (parent_die && parm_pack);
10401 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
10402 add_name_and_src_coords_attributes (die, parm_pack);
10403 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
10404 generic_parameter_die (parm_pack,
10405 TREE_VEC_ELT (parm_pack_args, j),
10406 false /* Don't emit DW_AT_name */,
10407 die);
10408 return die;
10411 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
10412 an enumerated type. */
10414 static inline int
10415 type_is_enum (const_tree type)
10417 return TREE_CODE (type) == ENUMERAL_TYPE;
10420 /* Return the DBX register number described by a given RTL node. */
10422 static unsigned int
10423 dbx_reg_number (const_rtx rtl)
10425 unsigned regno = REGNO (rtl);
10427 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10429 #ifdef LEAF_REG_REMAP
10430 if (crtl->uses_only_leaf_regs)
10432 int leaf_reg = LEAF_REG_REMAP (regno);
10433 if (leaf_reg != -1)
10434 regno = (unsigned) leaf_reg;
10436 #endif
10438 regno = DBX_REGISTER_NUMBER (regno);
10439 gcc_assert (regno != INVALID_REGNUM);
10440 return regno;
10443 /* Optionally add a DW_OP_piece term to a location description expression.
10444 DW_OP_piece is only added if the location description expression already
10445 doesn't end with DW_OP_piece. */
10447 static void
10448 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10450 dw_loc_descr_ref loc;
10452 if (*list_head != NULL)
10454 /* Find the end of the chain. */
10455 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10458 if (loc->dw_loc_opc != DW_OP_piece)
10459 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10463 /* Return a location descriptor that designates a machine register or
10464 zero if there is none. */
10466 static dw_loc_descr_ref
10467 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10469 rtx regs;
10471 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10472 return 0;
10474 /* We only use "frame base" when we're sure we're talking about the
10475 post-prologue local stack frame. We do this by *not* running
10476 register elimination until this point, and recognizing the special
10477 argument pointer and soft frame pointer rtx's.
10478 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
10479 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
10480 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
10482 dw_loc_descr_ref result = NULL;
10484 if (dwarf_version >= 4 || !dwarf_strict)
10486 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
10487 initialized);
10488 if (result)
10489 add_loc_descr (&result,
10490 new_loc_descr (DW_OP_stack_value, 0, 0));
10492 return result;
10495 regs = targetm.dwarf_register_span (rtl);
10497 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
10498 return multiple_reg_loc_descriptor (rtl, regs, initialized);
10499 else
10501 unsigned int dbx_regnum = dbx_reg_number (rtl);
10502 if (dbx_regnum == IGNORED_DWARF_REGNUM)
10503 return 0;
10504 return one_reg_loc_descriptor (dbx_regnum, initialized);
10508 /* Return a location descriptor that designates a machine register for
10509 a given hard register number. */
10511 static dw_loc_descr_ref
10512 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
10514 dw_loc_descr_ref reg_loc_descr;
10516 if (regno <= 31)
10517 reg_loc_descr
10518 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
10519 else
10520 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
10522 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10523 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10525 return reg_loc_descr;
10528 /* Given an RTL of a register, return a location descriptor that
10529 designates a value that spans more than one register. */
10531 static dw_loc_descr_ref
10532 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
10533 enum var_init_status initialized)
10535 int nregs, size, i;
10536 unsigned reg;
10537 dw_loc_descr_ref loc_result = NULL;
10539 reg = REGNO (rtl);
10540 #ifdef LEAF_REG_REMAP
10541 if (crtl->uses_only_leaf_regs)
10543 int leaf_reg = LEAF_REG_REMAP (reg);
10544 if (leaf_reg != -1)
10545 reg = (unsigned) leaf_reg;
10547 #endif
10548 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
10549 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
10551 /* Simple, contiguous registers. */
10552 if (regs == NULL_RTX)
10554 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
10556 loc_result = NULL;
10557 while (nregs--)
10559 dw_loc_descr_ref t;
10561 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
10562 VAR_INIT_STATUS_INITIALIZED);
10563 add_loc_descr (&loc_result, t);
10564 add_loc_descr_op_piece (&loc_result, size);
10565 ++reg;
10567 return loc_result;
10570 /* Now onto stupid register sets in non contiguous locations. */
10572 gcc_assert (GET_CODE (regs) == PARALLEL);
10574 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10575 loc_result = NULL;
10577 for (i = 0; i < XVECLEN (regs, 0); ++i)
10579 dw_loc_descr_ref t;
10581 t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
10582 VAR_INIT_STATUS_INITIALIZED);
10583 add_loc_descr (&loc_result, t);
10584 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10585 add_loc_descr_op_piece (&loc_result, size);
10588 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10589 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10590 return loc_result;
10593 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
10595 /* Return a location descriptor that designates a constant i,
10596 as a compound operation from constant (i >> shift), constant shift
10597 and DW_OP_shl. */
10599 static dw_loc_descr_ref
10600 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10602 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
10603 add_loc_descr (&ret, int_loc_descriptor (shift));
10604 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
10605 return ret;
10608 /* Return a location descriptor that designates a constant. */
10610 static dw_loc_descr_ref
10611 int_loc_descriptor (HOST_WIDE_INT i)
10613 enum dwarf_location_atom op;
10615 /* Pick the smallest representation of a constant, rather than just
10616 defaulting to the LEB encoding. */
10617 if (i >= 0)
10619 int clz = clz_hwi (i);
10620 int ctz = ctz_hwi (i);
10621 if (i <= 31)
10622 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
10623 else if (i <= 0xff)
10624 op = DW_OP_const1u;
10625 else if (i <= 0xffff)
10626 op = DW_OP_const2u;
10627 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10628 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10629 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
10630 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
10631 while DW_OP_const4u is 5 bytes. */
10632 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
10633 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10634 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10635 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
10636 while DW_OP_const4u is 5 bytes. */
10637 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10638 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10639 op = DW_OP_const4u;
10640 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10641 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10642 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
10643 while DW_OP_constu of constant >= 0x100000000 takes at least
10644 6 bytes. */
10645 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10646 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10647 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
10648 >= HOST_BITS_PER_WIDE_INT)
10649 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
10650 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
10651 while DW_OP_constu takes in this case at least 6 bytes. */
10652 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
10653 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10654 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10655 && size_of_uleb128 (i) > 6)
10656 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
10657 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
10658 else
10659 op = DW_OP_constu;
10661 else
10663 if (i >= -0x80)
10664 op = DW_OP_const1s;
10665 else if (i >= -0x8000)
10666 op = DW_OP_const2s;
10667 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10669 if (size_of_int_loc_descriptor (i) < 5)
10671 dw_loc_descr_ref ret = int_loc_descriptor (-i);
10672 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10673 return ret;
10675 op = DW_OP_const4s;
10677 else
10679 if (size_of_int_loc_descriptor (i)
10680 < (unsigned long) 1 + size_of_sleb128 (i))
10682 dw_loc_descr_ref ret = int_loc_descriptor (-i);
10683 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10684 return ret;
10686 op = DW_OP_consts;
10690 return new_loc_descr (op, i, 0);
10693 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
10694 without actually allocating it. */
10696 static unsigned long
10697 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10699 return size_of_int_loc_descriptor (i >> shift)
10700 + size_of_int_loc_descriptor (shift)
10701 + 1;
10704 /* Return size_of_locs (int_loc_descriptor (i)) without
10705 actually allocating it. */
10707 static unsigned long
10708 size_of_int_loc_descriptor (HOST_WIDE_INT i)
10710 unsigned long s;
10712 if (i >= 0)
10714 int clz, ctz;
10715 if (i <= 31)
10716 return 1;
10717 else if (i <= 0xff)
10718 return 2;
10719 else if (i <= 0xffff)
10720 return 3;
10721 clz = clz_hwi (i);
10722 ctz = ctz_hwi (i);
10723 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10724 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10725 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10726 - clz - 5);
10727 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10728 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10729 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10730 - clz - 8);
10731 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10732 return 5;
10733 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
10734 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10735 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10736 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10737 - clz - 8);
10738 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10739 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
10740 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10741 - clz - 16);
10742 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10743 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10744 && s > 6)
10745 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10746 - clz - 32);
10747 else
10748 return 1 + s;
10750 else
10752 if (i >= -0x80)
10753 return 2;
10754 else if (i >= -0x8000)
10755 return 3;
10756 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10758 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
10760 s = size_of_int_loc_descriptor (-i) + 1;
10761 if (s < 5)
10762 return s;
10764 return 5;
10766 else
10768 unsigned long r = 1 + size_of_sleb128 (i);
10769 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
10771 s = size_of_int_loc_descriptor (-i) + 1;
10772 if (s < r)
10773 return s;
10775 return r;
10780 /* Return loc description representing "address" of integer value.
10781 This can appear only as toplevel expression. */
10783 static dw_loc_descr_ref
10784 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
10786 int litsize;
10787 dw_loc_descr_ref loc_result = NULL;
10789 if (!(dwarf_version >= 4 || !dwarf_strict))
10790 return NULL;
10792 litsize = size_of_int_loc_descriptor (i);
10793 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
10794 is more compact. For DW_OP_stack_value we need:
10795 litsize + 1 (DW_OP_stack_value)
10796 and for DW_OP_implicit_value:
10797 1 (DW_OP_implicit_value) + 1 (length) + size. */
10798 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
10800 loc_result = int_loc_descriptor (i);
10801 add_loc_descr (&loc_result,
10802 new_loc_descr (DW_OP_stack_value, 0, 0));
10803 return loc_result;
10806 loc_result = new_loc_descr (DW_OP_implicit_value,
10807 size, 0);
10808 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
10809 loc_result->dw_loc_oprnd2.v.val_int = i;
10810 return loc_result;
10813 /* Return a location descriptor that designates a base+offset location. */
10815 static dw_loc_descr_ref
10816 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
10817 enum var_init_status initialized)
10819 unsigned int regno;
10820 dw_loc_descr_ref result;
10821 dw_fde_ref fde = cfun->fde;
10823 /* We only use "frame base" when we're sure we're talking about the
10824 post-prologue local stack frame. We do this by *not* running
10825 register elimination until this point, and recognizing the special
10826 argument pointer and soft frame pointer rtx's. */
10827 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
10829 rtx elim = (ira_use_lra_p
10830 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
10831 : eliminate_regs (reg, VOIDmode, NULL_RTX));
10833 if (elim != reg)
10835 if (GET_CODE (elim) == PLUS)
10837 offset += INTVAL (XEXP (elim, 1));
10838 elim = XEXP (elim, 0);
10840 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
10841 && (elim == hard_frame_pointer_rtx
10842 || elim == stack_pointer_rtx))
10843 || elim == (frame_pointer_needed
10844 ? hard_frame_pointer_rtx
10845 : stack_pointer_rtx));
10847 /* If drap register is used to align stack, use frame
10848 pointer + offset to access stack variables. If stack
10849 is aligned without drap, use stack pointer + offset to
10850 access stack variables. */
10851 if (crtl->stack_realign_tried
10852 && reg == frame_pointer_rtx)
10854 int base_reg
10855 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
10856 ? HARD_FRAME_POINTER_REGNUM
10857 : REGNO (elim));
10858 return new_reg_loc_descr (base_reg, offset);
10861 gcc_assert (frame_pointer_fb_offset_valid);
10862 offset += frame_pointer_fb_offset;
10863 return new_loc_descr (DW_OP_fbreg, offset, 0);
10867 regno = DWARF_FRAME_REGNUM (REGNO (reg));
10869 if (!optimize && fde
10870 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
10872 /* Use cfa+offset to represent the location of arguments passed
10873 on the stack when drap is used to align stack.
10874 Only do this when not optimizing, for optimized code var-tracking
10875 is supposed to track where the arguments live and the register
10876 used as vdrap or drap in some spot might be used for something
10877 else in other part of the routine. */
10878 return new_loc_descr (DW_OP_fbreg, offset, 0);
10881 if (regno <= 31)
10882 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
10883 offset, 0);
10884 else
10885 result = new_loc_descr (DW_OP_bregx, regno, offset);
10887 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10888 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10890 return result;
10893 /* Return true if this RTL expression describes a base+offset calculation. */
10895 static inline int
10896 is_based_loc (const_rtx rtl)
10898 return (GET_CODE (rtl) == PLUS
10899 && ((REG_P (XEXP (rtl, 0))
10900 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
10901 && CONST_INT_P (XEXP (rtl, 1)))));
10904 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
10905 failed. */
10907 static dw_loc_descr_ref
10908 tls_mem_loc_descriptor (rtx mem)
10910 tree base;
10911 dw_loc_descr_ref loc_result;
10913 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
10914 return NULL;
10916 base = get_base_address (MEM_EXPR (mem));
10917 if (base == NULL
10918 || TREE_CODE (base) != VAR_DECL
10919 || !DECL_THREAD_LOCAL_P (base))
10920 return NULL;
10922 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
10923 if (loc_result == NULL)
10924 return NULL;
10926 if (MEM_OFFSET (mem))
10927 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
10929 return loc_result;
10932 /* Output debug info about reason why we failed to expand expression as dwarf
10933 expression. */
10935 static void
10936 expansion_failed (tree expr, rtx rtl, char const *reason)
10938 if (dump_file && (dump_flags & TDF_DETAILS))
10940 fprintf (dump_file, "Failed to expand as dwarf: ");
10941 if (expr)
10942 print_generic_expr (dump_file, expr, dump_flags);
10943 if (rtl)
10945 fprintf (dump_file, "\n");
10946 print_rtl (dump_file, rtl);
10948 fprintf (dump_file, "\nReason: %s\n", reason);
10952 /* Helper function for const_ok_for_output, called either directly
10953 or via for_each_rtx. */
10955 static int
10956 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
10958 rtx rtl = *rtlp;
10960 if (GET_CODE (rtl) == UNSPEC)
10962 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
10963 we can't express it in the debug info. */
10964 #ifdef ENABLE_CHECKING
10965 /* Don't complain about TLS UNSPECs, those are just too hard to
10966 delegitimize. Note this could be a non-decl SYMBOL_REF such as
10967 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
10968 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
10969 if (XVECLEN (rtl, 0) == 0
10970 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
10971 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
10972 inform (current_function_decl
10973 ? DECL_SOURCE_LOCATION (current_function_decl)
10974 : UNKNOWN_LOCATION,
10975 #if NUM_UNSPEC_VALUES > 0
10976 "non-delegitimized UNSPEC %s (%d) found in variable location",
10977 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
10978 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
10979 XINT (rtl, 1));
10980 #else
10981 "non-delegitimized UNSPEC %d found in variable location",
10982 XINT (rtl, 1));
10983 #endif
10984 #endif
10985 expansion_failed (NULL_TREE, rtl,
10986 "UNSPEC hasn't been delegitimized.\n");
10987 return 1;
10990 if (targetm.const_not_ok_for_debug_p (rtl))
10992 expansion_failed (NULL_TREE, rtl,
10993 "Expression rejected for debug by the backend.\n");
10994 return 1;
10997 if (GET_CODE (rtl) != SYMBOL_REF)
10998 return 0;
11000 if (CONSTANT_POOL_ADDRESS_P (rtl))
11002 bool marked;
11003 get_pool_constant_mark (rtl, &marked);
11004 /* If all references to this pool constant were optimized away,
11005 it was not output and thus we can't represent it. */
11006 if (!marked)
11008 expansion_failed (NULL_TREE, rtl,
11009 "Constant was removed from constant pool.\n");
11010 return 1;
11014 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11015 return 1;
11017 /* Avoid references to external symbols in debug info, on several targets
11018 the linker might even refuse to link when linking a shared library,
11019 and in many other cases the relocations for .debug_info/.debug_loc are
11020 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
11021 to be defined within the same shared library or executable are fine. */
11022 if (SYMBOL_REF_EXTERNAL_P (rtl))
11024 tree decl = SYMBOL_REF_DECL (rtl);
11026 if (decl == NULL || !targetm.binds_local_p (decl))
11028 expansion_failed (NULL_TREE, rtl,
11029 "Symbol not defined in current TU.\n");
11030 return 1;
11034 return 0;
11037 /* Return true if constant RTL can be emitted in DW_OP_addr or
11038 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
11039 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
11041 static bool
11042 const_ok_for_output (rtx rtl)
11044 if (GET_CODE (rtl) == SYMBOL_REF)
11045 return const_ok_for_output_1 (&rtl, NULL) == 0;
11047 if (GET_CODE (rtl) == CONST)
11048 return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
11050 return true;
11053 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11054 if possible, NULL otherwise. */
11056 static dw_die_ref
11057 base_type_for_mode (enum machine_mode mode, bool unsignedp)
11059 dw_die_ref type_die;
11060 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11062 if (type == NULL)
11063 return NULL;
11064 switch (TREE_CODE (type))
11066 case INTEGER_TYPE:
11067 case REAL_TYPE:
11068 break;
11069 default:
11070 return NULL;
11072 type_die = lookup_type_die (type);
11073 if (!type_die)
11074 type_die = modified_type_die (type, false, false, comp_unit_die ());
11075 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11076 return NULL;
11077 return type_die;
11080 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11081 type matching MODE, or, if MODE is narrower than or as wide as
11082 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
11083 possible. */
11085 static dw_loc_descr_ref
11086 convert_descriptor_to_mode (enum machine_mode mode, dw_loc_descr_ref op)
11088 enum machine_mode outer_mode = mode;
11089 dw_die_ref type_die;
11090 dw_loc_descr_ref cvt;
11092 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11094 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11095 return op;
11097 type_die = base_type_for_mode (outer_mode, 1);
11098 if (type_die == NULL)
11099 return NULL;
11100 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11101 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11102 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11103 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11104 add_loc_descr (&op, cvt);
11105 return op;
11108 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
11110 static dw_loc_descr_ref
11111 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11112 dw_loc_descr_ref op1)
11114 dw_loc_descr_ref ret = op0;
11115 add_loc_descr (&ret, op1);
11116 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11117 if (STORE_FLAG_VALUE != 1)
11119 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11120 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11122 return ret;
11125 /* Return location descriptor for signed comparison OP RTL. */
11127 static dw_loc_descr_ref
11128 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11129 enum machine_mode mem_mode)
11131 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11132 dw_loc_descr_ref op0, op1;
11133 int shift;
11135 if (op_mode == VOIDmode)
11136 op_mode = GET_MODE (XEXP (rtl, 1));
11137 if (op_mode == VOIDmode)
11138 return NULL;
11140 if (dwarf_strict
11141 && (GET_MODE_CLASS (op_mode) != MODE_INT
11142 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11143 return NULL;
11145 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11146 VAR_INIT_STATUS_INITIALIZED);
11147 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11148 VAR_INIT_STATUS_INITIALIZED);
11150 if (op0 == NULL || op1 == NULL)
11151 return NULL;
11153 if (GET_MODE_CLASS (op_mode) != MODE_INT
11154 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11155 return compare_loc_descriptor (op, op0, op1);
11157 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11159 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11160 dw_loc_descr_ref cvt;
11162 if (type_die == NULL)
11163 return NULL;
11164 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11165 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11166 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11167 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11168 add_loc_descr (&op0, cvt);
11169 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11170 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11171 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11172 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11173 add_loc_descr (&op1, cvt);
11174 return compare_loc_descriptor (op, op0, op1);
11177 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11178 /* For eq/ne, if the operands are known to be zero-extended,
11179 there is no need to do the fancy shifting up. */
11180 if (op == DW_OP_eq || op == DW_OP_ne)
11182 dw_loc_descr_ref last0, last1;
11183 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11185 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11187 /* deref_size zero extends, and for constants we can check
11188 whether they are zero extended or not. */
11189 if (((last0->dw_loc_opc == DW_OP_deref_size
11190 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11191 || (CONST_INT_P (XEXP (rtl, 0))
11192 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11193 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11194 && ((last1->dw_loc_opc == DW_OP_deref_size
11195 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11196 || (CONST_INT_P (XEXP (rtl, 1))
11197 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11198 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11199 return compare_loc_descriptor (op, op0, op1);
11201 /* EQ/NE comparison against constant in narrower type than
11202 DWARF2_ADDR_SIZE can be performed either as
11203 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11204 DW_OP_{eq,ne}
11206 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11207 DW_OP_{eq,ne}. Pick whatever is shorter. */
11208 if (CONST_INT_P (XEXP (rtl, 1))
11209 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11210 && (size_of_int_loc_descriptor (shift) + 1
11211 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11212 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11213 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11214 & GET_MODE_MASK (op_mode))))
11216 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
11217 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11218 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11219 & GET_MODE_MASK (op_mode));
11220 return compare_loc_descriptor (op, op0, op1);
11223 add_loc_descr (&op0, int_loc_descriptor (shift));
11224 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11225 if (CONST_INT_P (XEXP (rtl, 1)))
11226 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11227 else
11229 add_loc_descr (&op1, int_loc_descriptor (shift));
11230 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11232 return compare_loc_descriptor (op, op0, op1);
11235 /* Return location descriptor for unsigned comparison OP RTL. */
11237 static dw_loc_descr_ref
11238 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11239 enum machine_mode mem_mode)
11241 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11242 dw_loc_descr_ref op0, op1;
11244 if (op_mode == VOIDmode)
11245 op_mode = GET_MODE (XEXP (rtl, 1));
11246 if (op_mode == VOIDmode)
11247 return NULL;
11248 if (GET_MODE_CLASS (op_mode) != MODE_INT)
11249 return NULL;
11251 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11252 return NULL;
11254 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11255 VAR_INIT_STATUS_INITIALIZED);
11256 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11257 VAR_INIT_STATUS_INITIALIZED);
11259 if (op0 == NULL || op1 == NULL)
11260 return NULL;
11262 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
11264 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
11265 dw_loc_descr_ref last0, last1;
11266 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11268 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11270 if (CONST_INT_P (XEXP (rtl, 0)))
11271 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
11272 /* deref_size zero extends, so no need to mask it again. */
11273 else if (last0->dw_loc_opc != DW_OP_deref_size
11274 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11276 add_loc_descr (&op0, int_loc_descriptor (mask));
11277 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11279 if (CONST_INT_P (XEXP (rtl, 1)))
11280 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11281 /* deref_size zero extends, so no need to mask it again. */
11282 else if (last1->dw_loc_opc != DW_OP_deref_size
11283 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11285 add_loc_descr (&op1, int_loc_descriptor (mask));
11286 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11289 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11291 HOST_WIDE_INT bias = 1;
11292 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11293 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11294 if (CONST_INT_P (XEXP (rtl, 1)))
11295 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
11296 + INTVAL (XEXP (rtl, 1)));
11297 else
11298 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
11299 bias, 0));
11301 return compare_loc_descriptor (op, op0, op1);
11304 /* Return location descriptor for {U,S}{MIN,MAX}. */
11306 static dw_loc_descr_ref
11307 minmax_loc_descriptor (rtx rtl, enum machine_mode mode,
11308 enum machine_mode mem_mode)
11310 enum dwarf_location_atom op;
11311 dw_loc_descr_ref op0, op1, ret;
11312 dw_loc_descr_ref bra_node, drop_node;
11314 if (dwarf_strict
11315 && (GET_MODE_CLASS (mode) != MODE_INT
11316 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
11317 return NULL;
11319 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11320 VAR_INIT_STATUS_INITIALIZED);
11321 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11322 VAR_INIT_STATUS_INITIALIZED);
11324 if (op0 == NULL || op1 == NULL)
11325 return NULL;
11327 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
11328 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
11329 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
11330 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
11332 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11334 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11335 add_loc_descr (&op0, int_loc_descriptor (mask));
11336 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11337 add_loc_descr (&op1, int_loc_descriptor (mask));
11338 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11340 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11342 HOST_WIDE_INT bias = 1;
11343 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11344 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11345 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11348 else if (GET_MODE_CLASS (mode) == MODE_INT
11349 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11351 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
11352 add_loc_descr (&op0, int_loc_descriptor (shift));
11353 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11354 add_loc_descr (&op1, int_loc_descriptor (shift));
11355 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11357 else if (GET_MODE_CLASS (mode) == MODE_INT
11358 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11360 dw_die_ref type_die = base_type_for_mode (mode, 0);
11361 dw_loc_descr_ref cvt;
11362 if (type_die == NULL)
11363 return NULL;
11364 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11365 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11366 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11367 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11368 add_loc_descr (&op0, cvt);
11369 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11370 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11371 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11372 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11373 add_loc_descr (&op1, cvt);
11376 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
11377 op = DW_OP_lt;
11378 else
11379 op = DW_OP_gt;
11380 ret = op0;
11381 add_loc_descr (&ret, op1);
11382 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11383 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11384 add_loc_descr (&ret, bra_node);
11385 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11386 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11387 add_loc_descr (&ret, drop_node);
11388 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11389 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11390 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
11391 && GET_MODE_CLASS (mode) == MODE_INT
11392 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11393 ret = convert_descriptor_to_mode (mode, ret);
11394 return ret;
11397 /* Helper function for mem_loc_descriptor. Perform OP binary op,
11398 but after converting arguments to type_die, afterwards
11399 convert back to unsigned. */
11401 static dw_loc_descr_ref
11402 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
11403 enum machine_mode mode, enum machine_mode mem_mode)
11405 dw_loc_descr_ref cvt, op0, op1;
11407 if (type_die == NULL)
11408 return NULL;
11409 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11410 VAR_INIT_STATUS_INITIALIZED);
11411 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11412 VAR_INIT_STATUS_INITIALIZED);
11413 if (op0 == NULL || op1 == NULL)
11414 return NULL;
11415 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11416 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11417 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11418 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11419 add_loc_descr (&op0, cvt);
11420 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11421 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11422 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11423 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11424 add_loc_descr (&op1, cvt);
11425 add_loc_descr (&op0, op1);
11426 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
11427 return convert_descriptor_to_mode (mode, op0);
11430 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
11431 const0 is DW_OP_lit0 or corresponding typed constant,
11432 const1 is DW_OP_lit1 or corresponding typed constant
11433 and constMSB is constant with just the MSB bit set
11434 for the mode):
11435 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11436 L1: const0 DW_OP_swap
11437 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
11438 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11439 L3: DW_OP_drop
11440 L4: DW_OP_nop
11442 CTZ is similar:
11443 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11444 L1: const0 DW_OP_swap
11445 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11446 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11447 L3: DW_OP_drop
11448 L4: DW_OP_nop
11450 FFS is similar:
11451 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
11452 L1: const1 DW_OP_swap
11453 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11454 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11455 L3: DW_OP_drop
11456 L4: DW_OP_nop */
11458 static dw_loc_descr_ref
11459 clz_loc_descriptor (rtx rtl, enum machine_mode mode,
11460 enum machine_mode mem_mode)
11462 dw_loc_descr_ref op0, ret, tmp;
11463 HOST_WIDE_INT valv;
11464 dw_loc_descr_ref l1jump, l1label;
11465 dw_loc_descr_ref l2jump, l2label;
11466 dw_loc_descr_ref l3jump, l3label;
11467 dw_loc_descr_ref l4jump, l4label;
11468 rtx msb;
11470 if (GET_MODE_CLASS (mode) != MODE_INT
11471 || GET_MODE (XEXP (rtl, 0)) != mode
11472 || (GET_CODE (rtl) == CLZ
11473 && GET_MODE_BITSIZE (mode) > HOST_BITS_PER_DOUBLE_INT))
11474 return NULL;
11476 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11477 VAR_INIT_STATUS_INITIALIZED);
11478 if (op0 == NULL)
11479 return NULL;
11480 ret = op0;
11481 if (GET_CODE (rtl) == CLZ)
11483 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11484 valv = GET_MODE_BITSIZE (mode);
11486 else if (GET_CODE (rtl) == FFS)
11487 valv = 0;
11488 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11489 valv = GET_MODE_BITSIZE (mode);
11490 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11491 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
11492 add_loc_descr (&ret, l1jump);
11493 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11494 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
11495 VAR_INIT_STATUS_INITIALIZED);
11496 if (tmp == NULL)
11497 return NULL;
11498 add_loc_descr (&ret, tmp);
11499 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
11500 add_loc_descr (&ret, l4jump);
11501 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
11502 ? const1_rtx : const0_rtx,
11503 mode, mem_mode,
11504 VAR_INIT_STATUS_INITIALIZED);
11505 if (l1label == NULL)
11506 return NULL;
11507 add_loc_descr (&ret, l1label);
11508 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11509 l2label = new_loc_descr (DW_OP_dup, 0, 0);
11510 add_loc_descr (&ret, l2label);
11511 if (GET_CODE (rtl) != CLZ)
11512 msb = const1_rtx;
11513 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11514 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
11515 << (GET_MODE_BITSIZE (mode) - 1));
11516 else
11517 msb = immed_double_const (0, (unsigned HOST_WIDE_INT) 1
11518 << (GET_MODE_BITSIZE (mode)
11519 - HOST_BITS_PER_WIDE_INT - 1), mode);
11520 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
11521 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11522 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
11523 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
11524 else
11525 tmp = mem_loc_descriptor (msb, mode, mem_mode,
11526 VAR_INIT_STATUS_INITIALIZED);
11527 if (tmp == NULL)
11528 return NULL;
11529 add_loc_descr (&ret, tmp);
11530 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11531 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
11532 add_loc_descr (&ret, l3jump);
11533 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11534 VAR_INIT_STATUS_INITIALIZED);
11535 if (tmp == NULL)
11536 return NULL;
11537 add_loc_descr (&ret, tmp);
11538 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
11539 ? DW_OP_shl : DW_OP_shr, 0, 0));
11540 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11541 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
11542 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11543 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
11544 add_loc_descr (&ret, l2jump);
11545 l3label = new_loc_descr (DW_OP_drop, 0, 0);
11546 add_loc_descr (&ret, l3label);
11547 l4label = new_loc_descr (DW_OP_nop, 0, 0);
11548 add_loc_descr (&ret, l4label);
11549 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11550 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11551 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11552 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11553 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11554 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
11555 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11556 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
11557 return ret;
11560 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
11561 const1 is DW_OP_lit1 or corresponding typed constant):
11562 const0 DW_OP_swap
11563 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11564 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11565 L2: DW_OP_drop
11567 PARITY is similar:
11568 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11569 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11570 L2: DW_OP_drop */
11572 static dw_loc_descr_ref
11573 popcount_loc_descriptor (rtx rtl, enum machine_mode mode,
11574 enum machine_mode mem_mode)
11576 dw_loc_descr_ref op0, ret, tmp;
11577 dw_loc_descr_ref l1jump, l1label;
11578 dw_loc_descr_ref l2jump, l2label;
11580 if (GET_MODE_CLASS (mode) != MODE_INT
11581 || GET_MODE (XEXP (rtl, 0)) != mode)
11582 return NULL;
11584 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11585 VAR_INIT_STATUS_INITIALIZED);
11586 if (op0 == NULL)
11587 return NULL;
11588 ret = op0;
11589 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11590 VAR_INIT_STATUS_INITIALIZED);
11591 if (tmp == NULL)
11592 return NULL;
11593 add_loc_descr (&ret, tmp);
11594 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11595 l1label = new_loc_descr (DW_OP_dup, 0, 0);
11596 add_loc_descr (&ret, l1label);
11597 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11598 add_loc_descr (&ret, l2jump);
11599 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11600 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11601 tmp = mem_loc_descriptor (const1_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_and, 0, 0));
11607 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
11608 ? DW_OP_plus : DW_OP_xor, 0, 0));
11609 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11610 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11611 VAR_INIT_STATUS_INITIALIZED);
11612 add_loc_descr (&ret, tmp);
11613 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11614 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11615 add_loc_descr (&ret, l1jump);
11616 l2label = new_loc_descr (DW_OP_drop, 0, 0);
11617 add_loc_descr (&ret, l2label);
11618 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11619 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11620 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11621 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11622 return ret;
11625 /* BSWAP (constS is initial shift count, either 56 or 24):
11626 constS const0
11627 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
11628 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
11629 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
11630 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
11631 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
11633 static dw_loc_descr_ref
11634 bswap_loc_descriptor (rtx rtl, enum machine_mode mode,
11635 enum machine_mode mem_mode)
11637 dw_loc_descr_ref op0, ret, tmp;
11638 dw_loc_descr_ref l1jump, l1label;
11639 dw_loc_descr_ref l2jump, l2label;
11641 if (GET_MODE_CLASS (mode) != MODE_INT
11642 || BITS_PER_UNIT != 8
11643 || (GET_MODE_BITSIZE (mode) != 32
11644 && GET_MODE_BITSIZE (mode) != 64))
11645 return NULL;
11647 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11648 VAR_INIT_STATUS_INITIALIZED);
11649 if (op0 == NULL)
11650 return NULL;
11652 ret = op0;
11653 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11654 mode, mem_mode,
11655 VAR_INIT_STATUS_INITIALIZED);
11656 if (tmp == NULL)
11657 return NULL;
11658 add_loc_descr (&ret, tmp);
11659 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11660 VAR_INIT_STATUS_INITIALIZED);
11661 if (tmp == NULL)
11662 return NULL;
11663 add_loc_descr (&ret, tmp);
11664 l1label = new_loc_descr (DW_OP_pick, 2, 0);
11665 add_loc_descr (&ret, l1label);
11666 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11667 mode, mem_mode,
11668 VAR_INIT_STATUS_INITIALIZED);
11669 add_loc_descr (&ret, tmp);
11670 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
11671 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11672 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11673 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
11674 VAR_INIT_STATUS_INITIALIZED);
11675 if (tmp == NULL)
11676 return NULL;
11677 add_loc_descr (&ret, tmp);
11678 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11679 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
11680 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11681 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11682 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11683 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11684 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11685 VAR_INIT_STATUS_INITIALIZED);
11686 add_loc_descr (&ret, tmp);
11687 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
11688 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11689 add_loc_descr (&ret, l2jump);
11690 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
11691 VAR_INIT_STATUS_INITIALIZED);
11692 add_loc_descr (&ret, tmp);
11693 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11694 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11695 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11696 add_loc_descr (&ret, l1jump);
11697 l2label = new_loc_descr (DW_OP_drop, 0, 0);
11698 add_loc_descr (&ret, l2label);
11699 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11700 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11701 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11702 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11703 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11704 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11705 return ret;
11708 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
11709 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11710 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
11711 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
11713 ROTATERT is similar:
11714 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
11715 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11716 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
11718 static dw_loc_descr_ref
11719 rotate_loc_descriptor (rtx rtl, enum machine_mode mode,
11720 enum machine_mode mem_mode)
11722 rtx rtlop1 = XEXP (rtl, 1);
11723 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
11724 int i;
11726 if (GET_MODE_CLASS (mode) != MODE_INT)
11727 return NULL;
11729 if (GET_MODE (rtlop1) != VOIDmode
11730 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
11731 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
11732 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11733 VAR_INIT_STATUS_INITIALIZED);
11734 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
11735 VAR_INIT_STATUS_INITIALIZED);
11736 if (op0 == NULL || op1 == NULL)
11737 return NULL;
11738 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11739 for (i = 0; i < 2; i++)
11741 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
11742 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
11743 mode, mem_mode,
11744 VAR_INIT_STATUS_INITIALIZED);
11745 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
11746 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11747 ? DW_OP_const4u
11748 : HOST_BITS_PER_WIDE_INT == 64
11749 ? DW_OP_const8u : DW_OP_constu,
11750 GET_MODE_MASK (mode), 0);
11751 else
11752 mask[i] = NULL;
11753 if (mask[i] == NULL)
11754 return NULL;
11755 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
11757 ret = op0;
11758 add_loc_descr (&ret, op1);
11759 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11760 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11761 if (GET_CODE (rtl) == ROTATERT)
11763 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11764 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11765 GET_MODE_BITSIZE (mode), 0));
11767 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11768 if (mask[0] != NULL)
11769 add_loc_descr (&ret, mask[0]);
11770 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11771 if (mask[1] != NULL)
11773 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11774 add_loc_descr (&ret, mask[1]);
11775 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11777 if (GET_CODE (rtl) == ROTATE)
11779 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11780 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11781 GET_MODE_BITSIZE (mode), 0));
11783 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11784 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11785 return ret;
11788 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
11789 for DEBUG_PARAMETER_REF RTL. */
11791 static dw_loc_descr_ref
11792 parameter_ref_descriptor (rtx rtl)
11794 dw_loc_descr_ref ret;
11795 dw_die_ref ref;
11797 if (dwarf_strict)
11798 return NULL;
11799 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
11800 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
11801 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
11802 if (ref)
11804 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11805 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
11806 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
11808 else
11810 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
11811 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
11813 return ret;
11816 /* The following routine converts the RTL for a variable or parameter
11817 (resident in memory) into an equivalent Dwarf representation of a
11818 mechanism for getting the address of that same variable onto the top of a
11819 hypothetical "address evaluation" stack.
11821 When creating memory location descriptors, we are effectively transforming
11822 the RTL for a memory-resident object into its Dwarf postfix expression
11823 equivalent. This routine recursively descends an RTL tree, turning
11824 it into Dwarf postfix code as it goes.
11826 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
11828 MEM_MODE is the mode of the memory reference, needed to handle some
11829 autoincrement addressing modes.
11831 Return 0 if we can't represent the location. */
11833 dw_loc_descr_ref
11834 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
11835 enum machine_mode mem_mode,
11836 enum var_init_status initialized)
11838 dw_loc_descr_ref mem_loc_result = NULL;
11839 enum dwarf_location_atom op;
11840 dw_loc_descr_ref op0, op1;
11841 rtx inner = NULL_RTX;
11843 if (mode == VOIDmode)
11844 mode = GET_MODE (rtl);
11846 /* Note that for a dynamically sized array, the location we will generate a
11847 description of here will be the lowest numbered location which is
11848 actually within the array. That's *not* necessarily the same as the
11849 zeroth element of the array. */
11851 rtl = targetm.delegitimize_address (rtl);
11853 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
11854 return NULL;
11856 switch (GET_CODE (rtl))
11858 case POST_INC:
11859 case POST_DEC:
11860 case POST_MODIFY:
11861 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
11863 case SUBREG:
11864 /* The case of a subreg may arise when we have a local (register)
11865 variable or a formal (register) parameter which doesn't quite fill
11866 up an entire register. For now, just assume that it is
11867 legitimate to make the Dwarf info refer to the whole register which
11868 contains the given subreg. */
11869 if (!subreg_lowpart_p (rtl))
11870 break;
11871 inner = SUBREG_REG (rtl);
11872 case TRUNCATE:
11873 if (inner == NULL_RTX)
11874 inner = XEXP (rtl, 0);
11875 if (GET_MODE_CLASS (mode) == MODE_INT
11876 && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
11877 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11878 #ifdef POINTERS_EXTEND_UNSIGNED
11879 || (mode == Pmode && mem_mode != VOIDmode)
11880 #endif
11882 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
11884 mem_loc_result = mem_loc_descriptor (inner,
11885 GET_MODE (inner),
11886 mem_mode, initialized);
11887 break;
11889 if (dwarf_strict)
11890 break;
11891 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
11892 break;
11893 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
11894 && (GET_MODE_CLASS (mode) != MODE_INT
11895 || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
11896 break;
11897 else
11899 dw_die_ref type_die;
11900 dw_loc_descr_ref cvt;
11902 mem_loc_result = mem_loc_descriptor (inner,
11903 GET_MODE (inner),
11904 mem_mode, initialized);
11905 if (mem_loc_result == NULL)
11906 break;
11907 type_die = base_type_for_mode (mode,
11908 GET_MODE_CLASS (mode) == MODE_INT);
11909 if (type_die == NULL)
11911 mem_loc_result = NULL;
11912 break;
11914 if (GET_MODE_SIZE (mode)
11915 != GET_MODE_SIZE (GET_MODE (inner)))
11916 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11917 else
11918 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
11919 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11920 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11921 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11922 add_loc_descr (&mem_loc_result, cvt);
11924 break;
11926 case REG:
11927 if (GET_MODE_CLASS (mode) != MODE_INT
11928 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
11929 && rtl != arg_pointer_rtx
11930 && rtl != frame_pointer_rtx
11931 #ifdef POINTERS_EXTEND_UNSIGNED
11932 && (mode != Pmode || mem_mode == VOIDmode)
11933 #endif
11936 dw_die_ref type_die;
11937 unsigned int dbx_regnum;
11939 if (dwarf_strict)
11940 break;
11941 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
11942 break;
11943 type_die = base_type_for_mode (mode,
11944 GET_MODE_CLASS (mode) == MODE_INT);
11945 if (type_die == NULL)
11946 break;
11948 dbx_regnum = dbx_reg_number (rtl);
11949 if (dbx_regnum == IGNORED_DWARF_REGNUM)
11950 break;
11951 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
11952 dbx_regnum, 0);
11953 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
11954 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
11955 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
11956 break;
11958 /* Whenever a register number forms a part of the description of the
11959 method for calculating the (dynamic) address of a memory resident
11960 object, DWARF rules require the register number be referred to as
11961 a "base register". This distinction is not based in any way upon
11962 what category of register the hardware believes the given register
11963 belongs to. This is strictly DWARF terminology we're dealing with
11964 here. Note that in cases where the location of a memory-resident
11965 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
11966 OP_CONST (0)) the actual DWARF location descriptor that we generate
11967 may just be OP_BASEREG (basereg). This may look deceptively like
11968 the object in question was allocated to a register (rather than in
11969 memory) so DWARF consumers need to be aware of the subtle
11970 distinction between OP_REG and OP_BASEREG. */
11971 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
11972 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
11973 else if (stack_realign_drap
11974 && crtl->drap_reg
11975 && crtl->args.internal_arg_pointer == rtl
11976 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
11978 /* If RTL is internal_arg_pointer, which has been optimized
11979 out, use DRAP instead. */
11980 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
11981 VAR_INIT_STATUS_INITIALIZED);
11983 break;
11985 case SIGN_EXTEND:
11986 case ZERO_EXTEND:
11987 if (GET_MODE_CLASS (mode) != MODE_INT)
11988 break;
11989 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
11990 mem_mode, VAR_INIT_STATUS_INITIALIZED);
11991 if (op0 == 0)
11992 break;
11993 else if (GET_CODE (rtl) == ZERO_EXTEND
11994 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11995 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
11996 < HOST_BITS_PER_WIDE_INT
11997 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
11998 to expand zero extend as two shifts instead of
11999 masking. */
12000 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12002 enum machine_mode imode = GET_MODE (XEXP (rtl, 0));
12003 mem_loc_result = op0;
12004 add_loc_descr (&mem_loc_result,
12005 int_loc_descriptor (GET_MODE_MASK (imode)));
12006 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12008 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12010 int shift = DWARF2_ADDR_SIZE
12011 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12012 shift *= BITS_PER_UNIT;
12013 if (GET_CODE (rtl) == SIGN_EXTEND)
12014 op = DW_OP_shra;
12015 else
12016 op = DW_OP_shr;
12017 mem_loc_result = op0;
12018 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12019 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12020 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12021 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12023 else if (!dwarf_strict)
12025 dw_die_ref type_die1, type_die2;
12026 dw_loc_descr_ref cvt;
12028 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12029 GET_CODE (rtl) == ZERO_EXTEND);
12030 if (type_die1 == NULL)
12031 break;
12032 type_die2 = base_type_for_mode (mode, 1);
12033 if (type_die2 == NULL)
12034 break;
12035 mem_loc_result = op0;
12036 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12037 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12038 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12039 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12040 add_loc_descr (&mem_loc_result, cvt);
12041 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12042 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12043 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12044 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12045 add_loc_descr (&mem_loc_result, cvt);
12047 break;
12049 case MEM:
12051 rtx new_rtl = avoid_constant_pool_reference (rtl);
12052 if (new_rtl != rtl)
12054 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12055 initialized);
12056 if (mem_loc_result != NULL)
12057 return mem_loc_result;
12060 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12061 get_address_mode (rtl), mode,
12062 VAR_INIT_STATUS_INITIALIZED);
12063 if (mem_loc_result == NULL)
12064 mem_loc_result = tls_mem_loc_descriptor (rtl);
12065 if (mem_loc_result != NULL)
12067 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12068 || GET_MODE_CLASS (mode) != MODE_INT)
12070 dw_die_ref type_die;
12071 dw_loc_descr_ref deref;
12073 if (dwarf_strict)
12074 return NULL;
12075 type_die
12076 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12077 if (type_die == NULL)
12078 return NULL;
12079 deref = new_loc_descr (DW_OP_GNU_deref_type,
12080 GET_MODE_SIZE (mode), 0);
12081 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12082 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12083 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12084 add_loc_descr (&mem_loc_result, deref);
12086 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12087 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12088 else
12089 add_loc_descr (&mem_loc_result,
12090 new_loc_descr (DW_OP_deref_size,
12091 GET_MODE_SIZE (mode), 0));
12093 break;
12095 case LO_SUM:
12096 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12098 case LABEL_REF:
12099 /* Some ports can transform a symbol ref into a label ref, because
12100 the symbol ref is too far away and has to be dumped into a constant
12101 pool. */
12102 case CONST:
12103 case SYMBOL_REF:
12104 if (GET_MODE_CLASS (mode) != MODE_INT
12105 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12106 #ifdef POINTERS_EXTEND_UNSIGNED
12107 && (mode != Pmode || mem_mode == VOIDmode)
12108 #endif
12110 break;
12111 if (GET_CODE (rtl) == SYMBOL_REF
12112 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12114 dw_loc_descr_ref temp;
12116 /* If this is not defined, we have no way to emit the data. */
12117 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12118 break;
12120 temp = new_addr_loc_descr (rtl, dtprel_true);
12122 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12123 add_loc_descr (&mem_loc_result, temp);
12125 break;
12128 if (!const_ok_for_output (rtl))
12129 break;
12131 symref:
12132 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12133 vec_safe_push (used_rtx_array, rtl);
12134 break;
12136 case CONCAT:
12137 case CONCATN:
12138 case VAR_LOCATION:
12139 case DEBUG_IMPLICIT_PTR:
12140 expansion_failed (NULL_TREE, rtl,
12141 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12142 return 0;
12144 case ENTRY_VALUE:
12145 if (dwarf_strict)
12146 return NULL;
12147 if (REG_P (ENTRY_VALUE_EXP (rtl)))
12149 if (GET_MODE_CLASS (mode) != MODE_INT
12150 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12151 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12152 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12153 else
12155 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12156 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12157 return NULL;
12158 op0 = one_reg_loc_descriptor (dbx_regnum,
12159 VAR_INIT_STATUS_INITIALIZED);
12162 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12163 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12165 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12166 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12167 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12168 return NULL;
12170 else
12171 gcc_unreachable ();
12172 if (op0 == NULL)
12173 return NULL;
12174 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12175 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12176 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12177 break;
12179 case DEBUG_PARAMETER_REF:
12180 mem_loc_result = parameter_ref_descriptor (rtl);
12181 break;
12183 case PRE_MODIFY:
12184 /* Extract the PLUS expression nested inside and fall into
12185 PLUS code below. */
12186 rtl = XEXP (rtl, 1);
12187 goto plus;
12189 case PRE_INC:
12190 case PRE_DEC:
12191 /* Turn these into a PLUS expression and fall into the PLUS code
12192 below. */
12193 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12194 GEN_INT (GET_CODE (rtl) == PRE_INC
12195 ? GET_MODE_UNIT_SIZE (mem_mode)
12196 : -GET_MODE_UNIT_SIZE (mem_mode)));
12198 /* ... fall through ... */
12200 case PLUS:
12201 plus:
12202 if (is_based_loc (rtl)
12203 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12204 || XEXP (rtl, 0) == arg_pointer_rtx
12205 || XEXP (rtl, 0) == frame_pointer_rtx)
12206 && GET_MODE_CLASS (mode) == MODE_INT)
12207 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12208 INTVAL (XEXP (rtl, 1)),
12209 VAR_INIT_STATUS_INITIALIZED);
12210 else
12212 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12213 VAR_INIT_STATUS_INITIALIZED);
12214 if (mem_loc_result == 0)
12215 break;
12217 if (CONST_INT_P (XEXP (rtl, 1))
12218 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12219 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12220 else
12222 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12223 VAR_INIT_STATUS_INITIALIZED);
12224 if (op1 == 0)
12225 break;
12226 add_loc_descr (&mem_loc_result, op1);
12227 add_loc_descr (&mem_loc_result,
12228 new_loc_descr (DW_OP_plus, 0, 0));
12231 break;
12233 /* If a pseudo-reg is optimized away, it is possible for it to
12234 be replaced with a MEM containing a multiply or shift. */
12235 case MINUS:
12236 op = DW_OP_minus;
12237 goto do_binop;
12239 case MULT:
12240 op = DW_OP_mul;
12241 goto do_binop;
12243 case DIV:
12244 if (!dwarf_strict
12245 && GET_MODE_CLASS (mode) == MODE_INT
12246 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12248 mem_loc_result = typed_binop (DW_OP_div, rtl,
12249 base_type_for_mode (mode, 0),
12250 mode, mem_mode);
12251 break;
12253 op = DW_OP_div;
12254 goto do_binop;
12256 case UMOD:
12257 op = DW_OP_mod;
12258 goto do_binop;
12260 case ASHIFT:
12261 op = DW_OP_shl;
12262 goto do_shift;
12264 case ASHIFTRT:
12265 op = DW_OP_shra;
12266 goto do_shift;
12268 case LSHIFTRT:
12269 op = DW_OP_shr;
12270 goto do_shift;
12272 do_shift:
12273 if (GET_MODE_CLASS (mode) != MODE_INT)
12274 break;
12275 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12276 VAR_INIT_STATUS_INITIALIZED);
12278 rtx rtlop1 = XEXP (rtl, 1);
12279 if (GET_MODE (rtlop1) != VOIDmode
12280 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
12281 < GET_MODE_BITSIZE (mode))
12282 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12283 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12284 VAR_INIT_STATUS_INITIALIZED);
12287 if (op0 == 0 || op1 == 0)
12288 break;
12290 mem_loc_result = op0;
12291 add_loc_descr (&mem_loc_result, op1);
12292 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12293 break;
12295 case AND:
12296 op = DW_OP_and;
12297 goto do_binop;
12299 case IOR:
12300 op = DW_OP_or;
12301 goto do_binop;
12303 case XOR:
12304 op = DW_OP_xor;
12305 goto do_binop;
12307 do_binop:
12308 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12309 VAR_INIT_STATUS_INITIALIZED);
12310 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12311 VAR_INIT_STATUS_INITIALIZED);
12313 if (op0 == 0 || op1 == 0)
12314 break;
12316 mem_loc_result = op0;
12317 add_loc_descr (&mem_loc_result, op1);
12318 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12319 break;
12321 case MOD:
12322 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
12324 mem_loc_result = typed_binop (DW_OP_mod, rtl,
12325 base_type_for_mode (mode, 0),
12326 mode, mem_mode);
12327 break;
12330 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12331 VAR_INIT_STATUS_INITIALIZED);
12332 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12333 VAR_INIT_STATUS_INITIALIZED);
12335 if (op0 == 0 || op1 == 0)
12336 break;
12338 mem_loc_result = op0;
12339 add_loc_descr (&mem_loc_result, op1);
12340 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12341 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12342 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
12343 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
12344 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
12345 break;
12347 case UDIV:
12348 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
12350 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
12352 op = DW_OP_div;
12353 goto do_binop;
12355 mem_loc_result = typed_binop (DW_OP_div, rtl,
12356 base_type_for_mode (mode, 1),
12357 mode, mem_mode);
12359 break;
12361 case NOT:
12362 op = DW_OP_not;
12363 goto do_unop;
12365 case ABS:
12366 op = DW_OP_abs;
12367 goto do_unop;
12369 case NEG:
12370 op = DW_OP_neg;
12371 goto do_unop;
12373 do_unop:
12374 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12375 VAR_INIT_STATUS_INITIALIZED);
12377 if (op0 == 0)
12378 break;
12380 mem_loc_result = op0;
12381 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12382 break;
12384 case CONST_INT:
12385 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12386 #ifdef POINTERS_EXTEND_UNSIGNED
12387 || (mode == Pmode
12388 && mem_mode != VOIDmode
12389 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
12390 #endif
12393 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12394 break;
12396 if (!dwarf_strict
12397 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
12398 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
12400 dw_die_ref type_die = base_type_for_mode (mode, 1);
12401 enum machine_mode amode;
12402 if (type_die == NULL)
12403 return NULL;
12404 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
12405 MODE_INT, 0);
12406 if (INTVAL (rtl) >= 0
12407 && amode != BLKmode
12408 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
12409 /* const DW_OP_GNU_convert <XXX> vs.
12410 DW_OP_GNU_const_type <XXX, 1, const>. */
12411 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
12412 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
12414 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12415 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12416 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12417 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12418 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
12419 add_loc_descr (&mem_loc_result, op0);
12420 return mem_loc_result;
12422 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
12423 INTVAL (rtl));
12424 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12425 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12426 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12427 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12428 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12429 else
12431 mem_loc_result->dw_loc_oprnd2.val_class
12432 = dw_val_class_const_double;
12433 mem_loc_result->dw_loc_oprnd2.v.val_double
12434 = double_int::from_shwi (INTVAL (rtl));
12437 break;
12439 case CONST_DOUBLE:
12440 if (!dwarf_strict)
12442 dw_die_ref type_die;
12444 /* Note that a CONST_DOUBLE rtx could represent either an integer
12445 or a floating-point constant. A CONST_DOUBLE is used whenever
12446 the constant requires more than one word in order to be
12447 adequately represented. We output CONST_DOUBLEs as blocks. */
12448 if (mode == VOIDmode
12449 || (GET_MODE (rtl) == VOIDmode
12450 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
12451 break;
12452 type_die = base_type_for_mode (mode,
12453 GET_MODE_CLASS (mode) == MODE_INT);
12454 if (type_die == NULL)
12455 return NULL;
12456 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
12457 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12458 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12459 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12460 if (SCALAR_FLOAT_MODE_P (mode))
12462 unsigned int length = GET_MODE_SIZE (mode);
12463 unsigned char *array
12464 = (unsigned char*) ggc_alloc_atomic (length);
12466 insert_float (rtl, array);
12467 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12468 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12469 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12470 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12472 else
12474 mem_loc_result->dw_loc_oprnd2.val_class
12475 = dw_val_class_const_double;
12476 mem_loc_result->dw_loc_oprnd2.v.val_double
12477 = rtx_to_double_int (rtl);
12480 break;
12482 case EQ:
12483 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
12484 break;
12486 case GE:
12487 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12488 break;
12490 case GT:
12491 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12492 break;
12494 case LE:
12495 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12496 break;
12498 case LT:
12499 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12500 break;
12502 case NE:
12503 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
12504 break;
12506 case GEU:
12507 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12508 break;
12510 case GTU:
12511 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12512 break;
12514 case LEU:
12515 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12516 break;
12518 case LTU:
12519 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12520 break;
12522 case UMIN:
12523 case UMAX:
12524 if (GET_MODE_CLASS (mode) != MODE_INT)
12525 break;
12526 /* FALLTHRU */
12527 case SMIN:
12528 case SMAX:
12529 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
12530 break;
12532 case ZERO_EXTRACT:
12533 case SIGN_EXTRACT:
12534 if (CONST_INT_P (XEXP (rtl, 1))
12535 && CONST_INT_P (XEXP (rtl, 2))
12536 && ((unsigned) INTVAL (XEXP (rtl, 1))
12537 + (unsigned) INTVAL (XEXP (rtl, 2))
12538 <= GET_MODE_BITSIZE (mode))
12539 && GET_MODE_CLASS (mode) == MODE_INT
12540 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12541 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
12543 int shift, size;
12544 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12545 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12546 if (op0 == 0)
12547 break;
12548 if (GET_CODE (rtl) == SIGN_EXTRACT)
12549 op = DW_OP_shra;
12550 else
12551 op = DW_OP_shr;
12552 mem_loc_result = op0;
12553 size = INTVAL (XEXP (rtl, 1));
12554 shift = INTVAL (XEXP (rtl, 2));
12555 if (BITS_BIG_ENDIAN)
12556 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12557 - shift - size;
12558 if (shift + size != (int) DWARF2_ADDR_SIZE)
12560 add_loc_descr (&mem_loc_result,
12561 int_loc_descriptor (DWARF2_ADDR_SIZE
12562 - shift - size));
12563 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12565 if (size != (int) DWARF2_ADDR_SIZE)
12567 add_loc_descr (&mem_loc_result,
12568 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
12569 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12572 break;
12574 case IF_THEN_ELSE:
12576 dw_loc_descr_ref op2, bra_node, drop_node;
12577 op0 = mem_loc_descriptor (XEXP (rtl, 0),
12578 GET_MODE (XEXP (rtl, 0)) == VOIDmode
12579 ? word_mode : GET_MODE (XEXP (rtl, 0)),
12580 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12581 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12582 VAR_INIT_STATUS_INITIALIZED);
12583 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
12584 VAR_INIT_STATUS_INITIALIZED);
12585 if (op0 == NULL || op1 == NULL || op2 == NULL)
12586 break;
12588 mem_loc_result = op1;
12589 add_loc_descr (&mem_loc_result, op2);
12590 add_loc_descr (&mem_loc_result, op0);
12591 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12592 add_loc_descr (&mem_loc_result, bra_node);
12593 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
12594 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12595 add_loc_descr (&mem_loc_result, drop_node);
12596 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12597 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12599 break;
12601 case FLOAT_EXTEND:
12602 case FLOAT_TRUNCATE:
12603 case FLOAT:
12604 case UNSIGNED_FLOAT:
12605 case FIX:
12606 case UNSIGNED_FIX:
12607 if (!dwarf_strict)
12609 dw_die_ref type_die;
12610 dw_loc_descr_ref cvt;
12612 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12613 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12614 if (op0 == NULL)
12615 break;
12616 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
12617 && (GET_CODE (rtl) == FLOAT
12618 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
12619 <= DWARF2_ADDR_SIZE))
12621 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12622 GET_CODE (rtl) == UNSIGNED_FLOAT);
12623 if (type_die == NULL)
12624 break;
12625 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12626 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12627 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12628 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12629 add_loc_descr (&op0, cvt);
12631 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
12632 if (type_die == NULL)
12633 break;
12634 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12635 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12636 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12637 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12638 add_loc_descr (&op0, cvt);
12639 if (GET_MODE_CLASS (mode) == MODE_INT
12640 && (GET_CODE (rtl) == FIX
12641 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
12643 op0 = convert_descriptor_to_mode (mode, op0);
12644 if (op0 == NULL)
12645 break;
12647 mem_loc_result = op0;
12649 break;
12651 case CLZ:
12652 case CTZ:
12653 case FFS:
12654 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
12655 break;
12657 case POPCOUNT:
12658 case PARITY:
12659 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
12660 break;
12662 case BSWAP:
12663 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
12664 break;
12666 case ROTATE:
12667 case ROTATERT:
12668 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
12669 break;
12671 case COMPARE:
12672 /* In theory, we could implement the above. */
12673 /* DWARF cannot represent the unsigned compare operations
12674 natively. */
12675 case SS_MULT:
12676 case US_MULT:
12677 case SS_DIV:
12678 case US_DIV:
12679 case SS_PLUS:
12680 case US_PLUS:
12681 case SS_MINUS:
12682 case US_MINUS:
12683 case SS_NEG:
12684 case US_NEG:
12685 case SS_ABS:
12686 case SS_ASHIFT:
12687 case US_ASHIFT:
12688 case SS_TRUNCATE:
12689 case US_TRUNCATE:
12690 case UNORDERED:
12691 case ORDERED:
12692 case UNEQ:
12693 case UNGE:
12694 case UNGT:
12695 case UNLE:
12696 case UNLT:
12697 case LTGT:
12698 case FRACT_CONVERT:
12699 case UNSIGNED_FRACT_CONVERT:
12700 case SAT_FRACT:
12701 case UNSIGNED_SAT_FRACT:
12702 case SQRT:
12703 case ASM_OPERANDS:
12704 case VEC_MERGE:
12705 case VEC_SELECT:
12706 case VEC_CONCAT:
12707 case VEC_DUPLICATE:
12708 case UNSPEC:
12709 case HIGH:
12710 case FMA:
12711 case STRICT_LOW_PART:
12712 case CONST_VECTOR:
12713 case CONST_FIXED:
12714 case CLRSB:
12715 case CLOBBER:
12716 /* If delegitimize_address couldn't do anything with the UNSPEC, we
12717 can't express it in the debug info. This can happen e.g. with some
12718 TLS UNSPECs. */
12719 break;
12721 case CONST_STRING:
12722 resolve_one_addr (&rtl, NULL);
12723 goto symref;
12725 default:
12726 #ifdef ENABLE_CHECKING
12727 print_rtl (stderr, rtl);
12728 gcc_unreachable ();
12729 #else
12730 break;
12731 #endif
12734 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12735 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12737 return mem_loc_result;
12740 /* Return a descriptor that describes the concatenation of two locations.
12741 This is typically a complex variable. */
12743 static dw_loc_descr_ref
12744 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
12746 dw_loc_descr_ref cc_loc_result = NULL;
12747 dw_loc_descr_ref x0_ref
12748 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12749 dw_loc_descr_ref x1_ref
12750 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12752 if (x0_ref == 0 || x1_ref == 0)
12753 return 0;
12755 cc_loc_result = x0_ref;
12756 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
12758 add_loc_descr (&cc_loc_result, x1_ref);
12759 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
12761 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
12762 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12764 return cc_loc_result;
12767 /* Return a descriptor that describes the concatenation of N
12768 locations. */
12770 static dw_loc_descr_ref
12771 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
12773 unsigned int i;
12774 dw_loc_descr_ref cc_loc_result = NULL;
12775 unsigned int n = XVECLEN (concatn, 0);
12777 for (i = 0; i < n; ++i)
12779 dw_loc_descr_ref ref;
12780 rtx x = XVECEXP (concatn, 0, i);
12782 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12783 if (ref == NULL)
12784 return NULL;
12786 add_loc_descr (&cc_loc_result, ref);
12787 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
12790 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12791 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12793 return cc_loc_result;
12796 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
12797 for DEBUG_IMPLICIT_PTR RTL. */
12799 static dw_loc_descr_ref
12800 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
12802 dw_loc_descr_ref ret;
12803 dw_die_ref ref;
12805 if (dwarf_strict)
12806 return NULL;
12807 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
12808 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
12809 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
12810 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
12811 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
12812 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
12813 if (ref)
12815 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12816 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12817 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12819 else
12821 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12822 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
12824 return ret;
12827 /* Output a proper Dwarf location descriptor for a variable or parameter
12828 which is either allocated in a register or in a memory location. For a
12829 register, we just generate an OP_REG and the register number. For a
12830 memory location we provide a Dwarf postfix expression describing how to
12831 generate the (dynamic) address of the object onto the address stack.
12833 MODE is mode of the decl if this loc_descriptor is going to be used in
12834 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
12835 allowed, VOIDmode otherwise.
12837 If we don't know how to describe it, return 0. */
12839 static dw_loc_descr_ref
12840 loc_descriptor (rtx rtl, enum machine_mode mode,
12841 enum var_init_status initialized)
12843 dw_loc_descr_ref loc_result = NULL;
12845 switch (GET_CODE (rtl))
12847 case SUBREG:
12848 /* The case of a subreg may arise when we have a local (register)
12849 variable or a formal (register) parameter which doesn't quite fill
12850 up an entire register. For now, just assume that it is
12851 legitimate to make the Dwarf info refer to the whole register which
12852 contains the given subreg. */
12853 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
12854 loc_result = loc_descriptor (SUBREG_REG (rtl),
12855 GET_MODE (SUBREG_REG (rtl)), initialized);
12856 else
12857 goto do_default;
12858 break;
12860 case REG:
12861 loc_result = reg_loc_descriptor (rtl, initialized);
12862 break;
12864 case MEM:
12865 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
12866 GET_MODE (rtl), initialized);
12867 if (loc_result == NULL)
12868 loc_result = tls_mem_loc_descriptor (rtl);
12869 if (loc_result == NULL)
12871 rtx new_rtl = avoid_constant_pool_reference (rtl);
12872 if (new_rtl != rtl)
12873 loc_result = loc_descriptor (new_rtl, mode, initialized);
12875 break;
12877 case CONCAT:
12878 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
12879 initialized);
12880 break;
12882 case CONCATN:
12883 loc_result = concatn_loc_descriptor (rtl, initialized);
12884 break;
12886 case VAR_LOCATION:
12887 /* Single part. */
12888 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
12890 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
12891 if (GET_CODE (loc) == EXPR_LIST)
12892 loc = XEXP (loc, 0);
12893 loc_result = loc_descriptor (loc, mode, initialized);
12894 break;
12897 rtl = XEXP (rtl, 1);
12898 /* FALLTHRU */
12900 case PARALLEL:
12902 rtvec par_elems = XVEC (rtl, 0);
12903 int num_elem = GET_NUM_ELEM (par_elems);
12904 enum machine_mode mode;
12905 int i;
12907 /* Create the first one, so we have something to add to. */
12908 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
12909 VOIDmode, initialized);
12910 if (loc_result == NULL)
12911 return NULL;
12912 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
12913 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
12914 for (i = 1; i < num_elem; i++)
12916 dw_loc_descr_ref temp;
12918 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
12919 VOIDmode, initialized);
12920 if (temp == NULL)
12921 return NULL;
12922 add_loc_descr (&loc_result, temp);
12923 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
12924 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
12927 break;
12929 case CONST_INT:
12930 if (mode != VOIDmode && mode != BLKmode)
12931 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
12932 INTVAL (rtl));
12933 break;
12935 case CONST_DOUBLE:
12936 if (mode == VOIDmode)
12937 mode = GET_MODE (rtl);
12939 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
12941 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
12943 /* Note that a CONST_DOUBLE rtx could represent either an integer
12944 or a floating-point constant. A CONST_DOUBLE is used whenever
12945 the constant requires more than one word in order to be
12946 adequately represented. We output CONST_DOUBLEs as blocks. */
12947 loc_result = new_loc_descr (DW_OP_implicit_value,
12948 GET_MODE_SIZE (mode), 0);
12949 if (SCALAR_FLOAT_MODE_P (mode))
12951 unsigned int length = GET_MODE_SIZE (mode);
12952 unsigned char *array
12953 = (unsigned char*) ggc_alloc_atomic (length);
12955 insert_float (rtl, array);
12956 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12957 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12958 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12959 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12961 else
12963 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
12964 loc_result->dw_loc_oprnd2.v.val_double
12965 = rtx_to_double_int (rtl);
12968 break;
12970 case CONST_VECTOR:
12971 if (mode == VOIDmode)
12972 mode = GET_MODE (rtl);
12974 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
12976 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
12977 unsigned int length = CONST_VECTOR_NUNITS (rtl);
12978 unsigned char *array = (unsigned char *)
12979 ggc_alloc_atomic (length * elt_size);
12980 unsigned int i;
12981 unsigned char *p;
12983 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
12984 switch (GET_MODE_CLASS (mode))
12986 case MODE_VECTOR_INT:
12987 for (i = 0, p = array; i < length; i++, p += elt_size)
12989 rtx elt = CONST_VECTOR_ELT (rtl, i);
12990 double_int val = rtx_to_double_int (elt);
12992 if (elt_size <= sizeof (HOST_WIDE_INT))
12993 insert_int (val.to_shwi (), elt_size, p);
12994 else
12996 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
12997 insert_double (val, p);
13000 break;
13002 case MODE_VECTOR_FLOAT:
13003 for (i = 0, p = array; i < length; i++, p += elt_size)
13005 rtx elt = CONST_VECTOR_ELT (rtl, i);
13006 insert_float (elt, p);
13008 break;
13010 default:
13011 gcc_unreachable ();
13014 loc_result = new_loc_descr (DW_OP_implicit_value,
13015 length * elt_size, 0);
13016 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13017 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13018 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13019 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13021 break;
13023 case CONST:
13024 if (mode == VOIDmode
13025 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13026 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13027 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13029 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13030 break;
13032 /* FALLTHROUGH */
13033 case SYMBOL_REF:
13034 if (!const_ok_for_output (rtl))
13035 break;
13036 case LABEL_REF:
13037 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13038 && (dwarf_version >= 4 || !dwarf_strict))
13040 loc_result = new_addr_loc_descr (rtl, dtprel_false);
13041 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13042 vec_safe_push (used_rtx_array, rtl);
13044 break;
13046 case DEBUG_IMPLICIT_PTR:
13047 loc_result = implicit_ptr_descriptor (rtl, 0);
13048 break;
13050 case PLUS:
13051 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13052 && CONST_INT_P (XEXP (rtl, 1)))
13054 loc_result
13055 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13056 break;
13058 /* FALLTHRU */
13059 do_default:
13060 default:
13061 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13062 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13063 && dwarf_version >= 4)
13064 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13066 /* Value expression. */
13067 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13068 if (loc_result)
13069 add_loc_descr (&loc_result,
13070 new_loc_descr (DW_OP_stack_value, 0, 0));
13072 break;
13075 return loc_result;
13078 /* We need to figure out what section we should use as the base for the
13079 address ranges where a given location is valid.
13080 1. If this particular DECL has a section associated with it, use that.
13081 2. If this function has a section associated with it, use that.
13082 3. Otherwise, use the text section.
13083 XXX: If you split a variable across multiple sections, we won't notice. */
13085 static const char *
13086 secname_for_decl (const_tree decl)
13088 const char *secname;
13090 if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
13092 tree sectree = DECL_SECTION_NAME (decl);
13093 secname = TREE_STRING_POINTER (sectree);
13095 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13097 tree sectree = DECL_SECTION_NAME (current_function_decl);
13098 secname = TREE_STRING_POINTER (sectree);
13100 else if (cfun && in_cold_section_p)
13101 secname = crtl->subsections.cold_section_label;
13102 else
13103 secname = text_section_label;
13105 return secname;
13108 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
13110 static bool
13111 decl_by_reference_p (tree decl)
13113 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13114 || TREE_CODE (decl) == VAR_DECL)
13115 && DECL_BY_REFERENCE (decl));
13118 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13119 for VARLOC. */
13121 static dw_loc_descr_ref
13122 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13123 enum var_init_status initialized)
13125 int have_address = 0;
13126 dw_loc_descr_ref descr;
13127 enum machine_mode mode;
13129 if (want_address != 2)
13131 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13132 /* Single part. */
13133 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13135 varloc = PAT_VAR_LOCATION_LOC (varloc);
13136 if (GET_CODE (varloc) == EXPR_LIST)
13137 varloc = XEXP (varloc, 0);
13138 mode = GET_MODE (varloc);
13139 if (MEM_P (varloc))
13141 rtx addr = XEXP (varloc, 0);
13142 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13143 mode, initialized);
13144 if (descr)
13145 have_address = 1;
13146 else
13148 rtx x = avoid_constant_pool_reference (varloc);
13149 if (x != varloc)
13150 descr = mem_loc_descriptor (x, mode, VOIDmode,
13151 initialized);
13154 else
13155 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13157 else
13158 return 0;
13160 else
13162 if (GET_CODE (varloc) == VAR_LOCATION)
13163 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13164 else
13165 mode = DECL_MODE (loc);
13166 descr = loc_descriptor (varloc, mode, initialized);
13167 have_address = 1;
13170 if (!descr)
13171 return 0;
13173 if (want_address == 2 && !have_address
13174 && (dwarf_version >= 4 || !dwarf_strict))
13176 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13178 expansion_failed (loc, NULL_RTX,
13179 "DWARF address size mismatch");
13180 return 0;
13182 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13183 have_address = 1;
13185 /* Show if we can't fill the request for an address. */
13186 if (want_address && !have_address)
13188 expansion_failed (loc, NULL_RTX,
13189 "Want address and only have value");
13190 return 0;
13193 /* If we've got an address and don't want one, dereference. */
13194 if (!want_address && have_address)
13196 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13197 enum dwarf_location_atom op;
13199 if (size > DWARF2_ADDR_SIZE || size == -1)
13201 expansion_failed (loc, NULL_RTX,
13202 "DWARF address size mismatch");
13203 return 0;
13205 else if (size == DWARF2_ADDR_SIZE)
13206 op = DW_OP_deref;
13207 else
13208 op = DW_OP_deref_size;
13210 add_loc_descr (&descr, new_loc_descr (op, size, 0));
13213 return descr;
13216 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
13217 if it is not possible. */
13219 static dw_loc_descr_ref
13220 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
13222 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
13223 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
13224 else if (dwarf_version >= 3 || !dwarf_strict)
13225 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
13226 else
13227 return NULL;
13230 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13231 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
13233 static dw_loc_descr_ref
13234 dw_sra_loc_expr (tree decl, rtx loc)
13236 rtx p;
13237 unsigned int padsize = 0;
13238 dw_loc_descr_ref descr, *descr_tail;
13239 unsigned HOST_WIDE_INT decl_size;
13240 rtx varloc;
13241 enum var_init_status initialized;
13243 if (DECL_SIZE (decl) == NULL
13244 || !host_integerp (DECL_SIZE (decl), 1))
13245 return NULL;
13247 decl_size = tree_low_cst (DECL_SIZE (decl), 1);
13248 descr = NULL;
13249 descr_tail = &descr;
13251 for (p = loc; p; p = XEXP (p, 1))
13253 unsigned int bitsize = decl_piece_bitsize (p);
13254 rtx loc_note = *decl_piece_varloc_ptr (p);
13255 dw_loc_descr_ref cur_descr;
13256 dw_loc_descr_ref *tail, last = NULL;
13257 unsigned int opsize = 0;
13259 if (loc_note == NULL_RTX
13260 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
13262 padsize += bitsize;
13263 continue;
13265 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
13266 varloc = NOTE_VAR_LOCATION (loc_note);
13267 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
13268 if (cur_descr == NULL)
13270 padsize += bitsize;
13271 continue;
13274 /* Check that cur_descr either doesn't use
13275 DW_OP_*piece operations, or their sum is equal
13276 to bitsize. Otherwise we can't embed it. */
13277 for (tail = &cur_descr; *tail != NULL;
13278 tail = &(*tail)->dw_loc_next)
13279 if ((*tail)->dw_loc_opc == DW_OP_piece)
13281 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
13282 * BITS_PER_UNIT;
13283 last = *tail;
13285 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
13287 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
13288 last = *tail;
13291 if (last != NULL && opsize != bitsize)
13293 padsize += bitsize;
13294 continue;
13297 /* If there is a hole, add DW_OP_*piece after empty DWARF
13298 expression, which means that those bits are optimized out. */
13299 if (padsize)
13301 if (padsize > decl_size)
13302 return NULL;
13303 decl_size -= padsize;
13304 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
13305 if (*descr_tail == NULL)
13306 return NULL;
13307 descr_tail = &(*descr_tail)->dw_loc_next;
13308 padsize = 0;
13310 *descr_tail = cur_descr;
13311 descr_tail = tail;
13312 if (bitsize > decl_size)
13313 return NULL;
13314 decl_size -= bitsize;
13315 if (last == NULL)
13317 HOST_WIDE_INT offset = 0;
13318 if (GET_CODE (varloc) == VAR_LOCATION
13319 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13321 varloc = PAT_VAR_LOCATION_LOC (varloc);
13322 if (GET_CODE (varloc) == EXPR_LIST)
13323 varloc = XEXP (varloc, 0);
13327 if (GET_CODE (varloc) == CONST
13328 || GET_CODE (varloc) == SIGN_EXTEND
13329 || GET_CODE (varloc) == ZERO_EXTEND)
13330 varloc = XEXP (varloc, 0);
13331 else if (GET_CODE (varloc) == SUBREG)
13332 varloc = SUBREG_REG (varloc);
13333 else
13334 break;
13336 while (1);
13337 /* DW_OP_bit_size offset should be zero for register
13338 or implicit location descriptions and empty location
13339 descriptions, but for memory addresses needs big endian
13340 adjustment. */
13341 if (MEM_P (varloc))
13343 unsigned HOST_WIDE_INT memsize
13344 = MEM_SIZE (varloc) * BITS_PER_UNIT;
13345 if (memsize != bitsize)
13347 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
13348 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
13349 return NULL;
13350 if (memsize < bitsize)
13351 return NULL;
13352 if (BITS_BIG_ENDIAN)
13353 offset = memsize - bitsize;
13357 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
13358 if (*descr_tail == NULL)
13359 return NULL;
13360 descr_tail = &(*descr_tail)->dw_loc_next;
13364 /* If there were any non-empty expressions, add padding till the end of
13365 the decl. */
13366 if (descr != NULL && decl_size != 0)
13368 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
13369 if (*descr_tail == NULL)
13370 return NULL;
13372 return descr;
13375 /* Return the dwarf representation of the location list LOC_LIST of
13376 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
13377 function. */
13379 static dw_loc_list_ref
13380 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
13382 const char *endname, *secname;
13383 rtx varloc;
13384 enum var_init_status initialized;
13385 struct var_loc_node *node;
13386 dw_loc_descr_ref descr;
13387 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13388 dw_loc_list_ref list = NULL;
13389 dw_loc_list_ref *listp = &list;
13391 /* Now that we know what section we are using for a base,
13392 actually construct the list of locations.
13393 The first location information is what is passed to the
13394 function that creates the location list, and the remaining
13395 locations just get added on to that list.
13396 Note that we only know the start address for a location
13397 (IE location changes), so to build the range, we use
13398 the range [current location start, next location start].
13399 This means we have to special case the last node, and generate
13400 a range of [last location start, end of function label]. */
13402 secname = secname_for_decl (decl);
13404 for (node = loc_list->first; node; node = node->next)
13405 if (GET_CODE (node->loc) == EXPR_LIST
13406 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
13408 if (GET_CODE (node->loc) == EXPR_LIST)
13410 /* This requires DW_OP_{,bit_}piece, which is not usable
13411 inside DWARF expressions. */
13412 if (want_address != 2)
13413 continue;
13414 descr = dw_sra_loc_expr (decl, node->loc);
13415 if (descr == NULL)
13416 continue;
13418 else
13420 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13421 varloc = NOTE_VAR_LOCATION (node->loc);
13422 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
13424 if (descr)
13426 bool range_across_switch = false;
13427 /* If section switch happens in between node->label
13428 and node->next->label (or end of function) and
13429 we can't emit it as a single entry list,
13430 emit two ranges, first one ending at the end
13431 of first partition and second one starting at the
13432 beginning of second partition. */
13433 if (node == loc_list->last_before_switch
13434 && (node != loc_list->first || loc_list->first->next)
13435 && current_function_decl)
13437 endname = cfun->fde->dw_fde_end;
13438 range_across_switch = true;
13440 /* The variable has a location between NODE->LABEL and
13441 NODE->NEXT->LABEL. */
13442 else if (node->next)
13443 endname = node->next->label;
13444 /* If the variable has a location at the last label
13445 it keeps its location until the end of function. */
13446 else if (!current_function_decl)
13447 endname = text_end_label;
13448 else
13450 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13451 current_function_funcdef_no);
13452 endname = ggc_strdup (label_id);
13455 *listp = new_loc_list (descr, node->label, endname, secname);
13456 if (TREE_CODE (decl) == PARM_DECL
13457 && node == loc_list->first
13458 && GET_CODE (node->loc) == NOTE
13459 && strcmp (node->label, endname) == 0)
13460 (*listp)->force = true;
13461 listp = &(*listp)->dw_loc_next;
13463 if (range_across_switch)
13465 if (GET_CODE (node->loc) == EXPR_LIST)
13466 descr = dw_sra_loc_expr (decl, node->loc);
13467 else
13469 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13470 varloc = NOTE_VAR_LOCATION (node->loc);
13471 descr = dw_loc_list_1 (decl, varloc, want_address,
13472 initialized);
13474 gcc_assert (descr);
13475 /* The variable has a location between NODE->LABEL and
13476 NODE->NEXT->LABEL. */
13477 if (node->next)
13478 endname = node->next->label;
13479 else
13480 endname = cfun->fde->dw_fde_second_end;
13481 *listp = new_loc_list (descr,
13482 cfun->fde->dw_fde_second_begin,
13483 endname, secname);
13484 listp = &(*listp)->dw_loc_next;
13489 /* Try to avoid the overhead of a location list emitting a location
13490 expression instead, but only if we didn't have more than one
13491 location entry in the first place. If some entries were not
13492 representable, we don't want to pretend a single entry that was
13493 applies to the entire scope in which the variable is
13494 available. */
13495 if (list && loc_list->first->next)
13496 gen_llsym (list);
13498 return list;
13501 /* Return if the loc_list has only single element and thus can be represented
13502 as location description. */
13504 static bool
13505 single_element_loc_list_p (dw_loc_list_ref list)
13507 gcc_assert (!list->dw_loc_next || list->ll_symbol);
13508 return !list->ll_symbol;
13511 /* To each location in list LIST add loc descr REF. */
13513 static void
13514 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
13516 dw_loc_descr_ref copy;
13517 add_loc_descr (&list->expr, ref);
13518 list = list->dw_loc_next;
13519 while (list)
13521 copy = ggc_alloc_dw_loc_descr_node ();
13522 memcpy (copy, ref, sizeof (dw_loc_descr_node));
13523 add_loc_descr (&list->expr, copy);
13524 while (copy->dw_loc_next)
13526 dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
13527 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
13528 copy->dw_loc_next = new_copy;
13529 copy = new_copy;
13531 list = list->dw_loc_next;
13535 /* Given two lists RET and LIST
13536 produce location list that is result of adding expression in LIST
13537 to expression in RET on each position in program.
13538 Might be destructive on both RET and LIST.
13540 TODO: We handle only simple cases of RET or LIST having at most one
13541 element. General case would inolve sorting the lists in program order
13542 and merging them that will need some additional work.
13543 Adding that will improve quality of debug info especially for SRA-ed
13544 structures. */
13546 static void
13547 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
13549 if (!list)
13550 return;
13551 if (!*ret)
13553 *ret = list;
13554 return;
13556 if (!list->dw_loc_next)
13558 add_loc_descr_to_each (*ret, list->expr);
13559 return;
13561 if (!(*ret)->dw_loc_next)
13563 add_loc_descr_to_each (list, (*ret)->expr);
13564 *ret = list;
13565 return;
13567 expansion_failed (NULL_TREE, NULL_RTX,
13568 "Don't know how to merge two non-trivial"
13569 " location lists.\n");
13570 *ret = NULL;
13571 return;
13574 /* LOC is constant expression. Try a luck, look it up in constant
13575 pool and return its loc_descr of its address. */
13577 static dw_loc_descr_ref
13578 cst_pool_loc_descr (tree loc)
13580 /* Get an RTL for this, if something has been emitted. */
13581 rtx rtl = lookup_constant_def (loc);
13583 if (!rtl || !MEM_P (rtl))
13585 gcc_assert (!rtl);
13586 return 0;
13588 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
13590 /* TODO: We might get more coverage if we was actually delaying expansion
13591 of all expressions till end of compilation when constant pools are fully
13592 populated. */
13593 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
13595 expansion_failed (loc, NULL_RTX,
13596 "CST value in contant pool but not marked.");
13597 return 0;
13599 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13600 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
13603 /* Return dw_loc_list representing address of addr_expr LOC
13604 by looking for inner INDIRECT_REF expression and turning
13605 it into simple arithmetics. */
13607 static dw_loc_list_ref
13608 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
13610 tree obj, offset;
13611 HOST_WIDE_INT bitsize, bitpos, bytepos;
13612 enum machine_mode mode;
13613 int unsignedp, volatilep = 0;
13614 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13616 obj = get_inner_reference (TREE_OPERAND (loc, 0),
13617 &bitsize, &bitpos, &offset, &mode,
13618 &unsignedp, &volatilep, false);
13619 STRIP_NOPS (obj);
13620 if (bitpos % BITS_PER_UNIT)
13622 expansion_failed (loc, NULL_RTX, "bitfield access");
13623 return 0;
13625 if (!INDIRECT_REF_P (obj))
13627 expansion_failed (obj,
13628 NULL_RTX, "no indirect ref in inner refrence");
13629 return 0;
13631 if (!offset && !bitpos)
13632 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
13633 else if (toplev
13634 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
13635 && (dwarf_version >= 4 || !dwarf_strict))
13637 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
13638 if (!list_ret)
13639 return 0;
13640 if (offset)
13642 /* Variable offset. */
13643 list_ret1 = loc_list_from_tree (offset, 0);
13644 if (list_ret1 == 0)
13645 return 0;
13646 add_loc_list (&list_ret, list_ret1);
13647 if (!list_ret)
13648 return 0;
13649 add_loc_descr_to_each (list_ret,
13650 new_loc_descr (DW_OP_plus, 0, 0));
13652 bytepos = bitpos / BITS_PER_UNIT;
13653 if (bytepos > 0)
13654 add_loc_descr_to_each (list_ret,
13655 new_loc_descr (DW_OP_plus_uconst,
13656 bytepos, 0));
13657 else if (bytepos < 0)
13658 loc_list_plus_const (list_ret, bytepos);
13659 add_loc_descr_to_each (list_ret,
13660 new_loc_descr (DW_OP_stack_value, 0, 0));
13662 return list_ret;
13666 /* Generate Dwarf location list representing LOC.
13667 If WANT_ADDRESS is false, expression computing LOC will be computed
13668 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
13669 if WANT_ADDRESS is 2, expression computing address useable in location
13670 will be returned (i.e. DW_OP_reg can be used
13671 to refer to register values). */
13673 static dw_loc_list_ref
13674 loc_list_from_tree (tree loc, int want_address)
13676 dw_loc_descr_ref ret = NULL, ret1 = NULL;
13677 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13678 int have_address = 0;
13679 enum dwarf_location_atom op;
13681 /* ??? Most of the time we do not take proper care for sign/zero
13682 extending the values properly. Hopefully this won't be a real
13683 problem... */
13685 switch (TREE_CODE (loc))
13687 case ERROR_MARK:
13688 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
13689 return 0;
13691 case PLACEHOLDER_EXPR:
13692 /* This case involves extracting fields from an object to determine the
13693 position of other fields. We don't try to encode this here. The
13694 only user of this is Ada, which encodes the needed information using
13695 the names of types. */
13696 expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
13697 return 0;
13699 case CALL_EXPR:
13700 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
13701 /* There are no opcodes for these operations. */
13702 return 0;
13704 case PREINCREMENT_EXPR:
13705 case PREDECREMENT_EXPR:
13706 case POSTINCREMENT_EXPR:
13707 case POSTDECREMENT_EXPR:
13708 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
13709 /* There are no opcodes for these operations. */
13710 return 0;
13712 case ADDR_EXPR:
13713 /* If we already want an address, see if there is INDIRECT_REF inside
13714 e.g. for &this->field. */
13715 if (want_address)
13717 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
13718 (loc, want_address == 2);
13719 if (list_ret)
13720 have_address = 1;
13721 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
13722 && (ret = cst_pool_loc_descr (loc)))
13723 have_address = 1;
13725 /* Otherwise, process the argument and look for the address. */
13726 if (!list_ret && !ret)
13727 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
13728 else
13730 if (want_address)
13731 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
13732 return NULL;
13734 break;
13736 case VAR_DECL:
13737 if (DECL_THREAD_LOCAL_P (loc))
13739 rtx rtl;
13740 enum dwarf_location_atom tls_op;
13741 enum dtprel_bool dtprel = dtprel_false;
13743 if (targetm.have_tls)
13745 /* If this is not defined, we have no way to emit the
13746 data. */
13747 if (!targetm.asm_out.output_dwarf_dtprel)
13748 return 0;
13750 /* The way DW_OP_GNU_push_tls_address is specified, we
13751 can only look up addresses of objects in the current
13752 module. We used DW_OP_addr as first op, but that's
13753 wrong, because DW_OP_addr is relocated by the debug
13754 info consumer, while DW_OP_GNU_push_tls_address
13755 operand shouldn't be. */
13756 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
13757 return 0;
13758 dtprel = dtprel_true;
13759 tls_op = DW_OP_GNU_push_tls_address;
13761 else
13763 if (!targetm.emutls.debug_form_tls_address
13764 || !(dwarf_version >= 3 || !dwarf_strict))
13765 return 0;
13766 /* We stuffed the control variable into the DECL_VALUE_EXPR
13767 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
13768 no longer appear in gimple code. We used the control
13769 variable in specific so that we could pick it up here. */
13770 loc = DECL_VALUE_EXPR (loc);
13771 tls_op = DW_OP_form_tls_address;
13774 rtl = rtl_for_decl_location (loc);
13775 if (rtl == NULL_RTX)
13776 return 0;
13778 if (!MEM_P (rtl))
13779 return 0;
13780 rtl = XEXP (rtl, 0);
13781 if (! CONSTANT_P (rtl))
13782 return 0;
13784 ret = new_addr_loc_descr (rtl, dtprel);
13785 ret1 = new_loc_descr (tls_op, 0, 0);
13786 add_loc_descr (&ret, ret1);
13788 have_address = 1;
13789 break;
13791 /* FALLTHRU */
13793 case PARM_DECL:
13794 case RESULT_DECL:
13795 if (DECL_HAS_VALUE_EXPR_P (loc))
13796 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
13797 want_address);
13798 /* FALLTHRU */
13800 case FUNCTION_DECL:
13802 rtx rtl;
13803 var_loc_list *loc_list = lookup_decl_loc (loc);
13805 if (loc_list && loc_list->first)
13807 list_ret = dw_loc_list (loc_list, loc, want_address);
13808 have_address = want_address != 0;
13809 break;
13811 rtl = rtl_for_decl_location (loc);
13812 if (rtl == NULL_RTX)
13814 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
13815 return 0;
13817 else if (CONST_INT_P (rtl))
13819 HOST_WIDE_INT val = INTVAL (rtl);
13820 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13821 val &= GET_MODE_MASK (DECL_MODE (loc));
13822 ret = int_loc_descriptor (val);
13824 else if (GET_CODE (rtl) == CONST_STRING)
13826 expansion_failed (loc, NULL_RTX, "CONST_STRING");
13827 return 0;
13829 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
13830 ret = new_addr_loc_descr (rtl, dtprel_false);
13831 else
13833 enum machine_mode mode, mem_mode;
13835 /* Certain constructs can only be represented at top-level. */
13836 if (want_address == 2)
13838 ret = loc_descriptor (rtl, VOIDmode,
13839 VAR_INIT_STATUS_INITIALIZED);
13840 have_address = 1;
13842 else
13844 mode = GET_MODE (rtl);
13845 mem_mode = VOIDmode;
13846 if (MEM_P (rtl))
13848 mem_mode = mode;
13849 mode = get_address_mode (rtl);
13850 rtl = XEXP (rtl, 0);
13851 have_address = 1;
13853 ret = mem_loc_descriptor (rtl, mode, mem_mode,
13854 VAR_INIT_STATUS_INITIALIZED);
13856 if (!ret)
13857 expansion_failed (loc, rtl,
13858 "failed to produce loc descriptor for rtl");
13861 break;
13863 case MEM_REF:
13864 /* ??? FIXME. */
13865 if (!integer_zerop (TREE_OPERAND (loc, 1)))
13866 return 0;
13867 /* Fallthru. */
13868 case INDIRECT_REF:
13869 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13870 have_address = 1;
13871 break;
13873 case COMPOUND_EXPR:
13874 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
13876 CASE_CONVERT:
13877 case VIEW_CONVERT_EXPR:
13878 case SAVE_EXPR:
13879 case MODIFY_EXPR:
13880 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
13882 case COMPONENT_REF:
13883 case BIT_FIELD_REF:
13884 case ARRAY_REF:
13885 case ARRAY_RANGE_REF:
13886 case REALPART_EXPR:
13887 case IMAGPART_EXPR:
13889 tree obj, offset;
13890 HOST_WIDE_INT bitsize, bitpos, bytepos;
13891 enum machine_mode mode;
13892 int unsignedp, volatilep = 0;
13894 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
13895 &unsignedp, &volatilep, false);
13897 gcc_assert (obj != loc);
13899 list_ret = loc_list_from_tree (obj,
13900 want_address == 2
13901 && !bitpos && !offset ? 2 : 1);
13902 /* TODO: We can extract value of the small expression via shifting even
13903 for nonzero bitpos. */
13904 if (list_ret == 0)
13905 return 0;
13906 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
13908 expansion_failed (loc, NULL_RTX,
13909 "bitfield access");
13910 return 0;
13913 if (offset != NULL_TREE)
13915 /* Variable offset. */
13916 list_ret1 = loc_list_from_tree (offset, 0);
13917 if (list_ret1 == 0)
13918 return 0;
13919 add_loc_list (&list_ret, list_ret1);
13920 if (!list_ret)
13921 return 0;
13922 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
13925 bytepos = bitpos / BITS_PER_UNIT;
13926 if (bytepos > 0)
13927 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
13928 else if (bytepos < 0)
13929 loc_list_plus_const (list_ret, bytepos);
13931 have_address = 1;
13932 break;
13935 case INTEGER_CST:
13936 if ((want_address || !host_integerp (loc, 0))
13937 && (ret = cst_pool_loc_descr (loc)))
13938 have_address = 1;
13939 else if (want_address == 2
13940 && host_integerp (loc, 0)
13941 && (ret = address_of_int_loc_descriptor
13942 (int_size_in_bytes (TREE_TYPE (loc)),
13943 tree_low_cst (loc, 0))))
13944 have_address = 1;
13945 else if (host_integerp (loc, 0))
13946 ret = int_loc_descriptor (tree_low_cst (loc, 0));
13947 else
13949 expansion_failed (loc, NULL_RTX,
13950 "Integer operand is not host integer");
13951 return 0;
13953 break;
13955 case CONSTRUCTOR:
13956 case REAL_CST:
13957 case STRING_CST:
13958 case COMPLEX_CST:
13959 if ((ret = cst_pool_loc_descr (loc)))
13960 have_address = 1;
13961 else
13962 /* We can construct small constants here using int_loc_descriptor. */
13963 expansion_failed (loc, NULL_RTX,
13964 "constructor or constant not in constant pool");
13965 break;
13967 case TRUTH_AND_EXPR:
13968 case TRUTH_ANDIF_EXPR:
13969 case BIT_AND_EXPR:
13970 op = DW_OP_and;
13971 goto do_binop;
13973 case TRUTH_XOR_EXPR:
13974 case BIT_XOR_EXPR:
13975 op = DW_OP_xor;
13976 goto do_binop;
13978 case TRUTH_OR_EXPR:
13979 case TRUTH_ORIF_EXPR:
13980 case BIT_IOR_EXPR:
13981 op = DW_OP_or;
13982 goto do_binop;
13984 case FLOOR_DIV_EXPR:
13985 case CEIL_DIV_EXPR:
13986 case ROUND_DIV_EXPR:
13987 case TRUNC_DIV_EXPR:
13988 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13989 return 0;
13990 op = DW_OP_div;
13991 goto do_binop;
13993 case MINUS_EXPR:
13994 op = DW_OP_minus;
13995 goto do_binop;
13997 case FLOOR_MOD_EXPR:
13998 case CEIL_MOD_EXPR:
13999 case ROUND_MOD_EXPR:
14000 case TRUNC_MOD_EXPR:
14001 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14003 op = DW_OP_mod;
14004 goto do_binop;
14006 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14007 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14008 if (list_ret == 0 || list_ret1 == 0)
14009 return 0;
14011 add_loc_list (&list_ret, list_ret1);
14012 if (list_ret == 0)
14013 return 0;
14014 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14015 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14016 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14017 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14018 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14019 break;
14021 case MULT_EXPR:
14022 op = DW_OP_mul;
14023 goto do_binop;
14025 case LSHIFT_EXPR:
14026 op = DW_OP_shl;
14027 goto do_binop;
14029 case RSHIFT_EXPR:
14030 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14031 goto do_binop;
14033 case POINTER_PLUS_EXPR:
14034 case PLUS_EXPR:
14035 if (host_integerp (TREE_OPERAND (loc, 1), 0))
14037 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14038 if (list_ret == 0)
14039 return 0;
14041 loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
14042 break;
14045 op = DW_OP_plus;
14046 goto do_binop;
14048 case LE_EXPR:
14049 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14050 return 0;
14052 op = DW_OP_le;
14053 goto do_binop;
14055 case GE_EXPR:
14056 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14057 return 0;
14059 op = DW_OP_ge;
14060 goto do_binop;
14062 case LT_EXPR:
14063 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14064 return 0;
14066 op = DW_OP_lt;
14067 goto do_binop;
14069 case GT_EXPR:
14070 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14071 return 0;
14073 op = DW_OP_gt;
14074 goto do_binop;
14076 case EQ_EXPR:
14077 op = DW_OP_eq;
14078 goto do_binop;
14080 case NE_EXPR:
14081 op = DW_OP_ne;
14082 goto do_binop;
14084 do_binop:
14085 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14086 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14087 if (list_ret == 0 || list_ret1 == 0)
14088 return 0;
14090 add_loc_list (&list_ret, list_ret1);
14091 if (list_ret == 0)
14092 return 0;
14093 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14094 break;
14096 case TRUTH_NOT_EXPR:
14097 case BIT_NOT_EXPR:
14098 op = DW_OP_not;
14099 goto do_unop;
14101 case ABS_EXPR:
14102 op = DW_OP_abs;
14103 goto do_unop;
14105 case NEGATE_EXPR:
14106 op = DW_OP_neg;
14107 goto do_unop;
14109 do_unop:
14110 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14111 if (list_ret == 0)
14112 return 0;
14114 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14115 break;
14117 case MIN_EXPR:
14118 case MAX_EXPR:
14120 const enum tree_code code =
14121 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14123 loc = build3 (COND_EXPR, TREE_TYPE (loc),
14124 build2 (code, integer_type_node,
14125 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14126 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
14129 /* ... fall through ... */
14131 case COND_EXPR:
14133 dw_loc_descr_ref lhs
14134 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
14135 dw_loc_list_ref rhs
14136 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
14137 dw_loc_descr_ref bra_node, jump_node, tmp;
14139 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14140 if (list_ret == 0 || lhs == 0 || rhs == 0)
14141 return 0;
14143 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14144 add_loc_descr_to_each (list_ret, bra_node);
14146 add_loc_list (&list_ret, rhs);
14147 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
14148 add_loc_descr_to_each (list_ret, jump_node);
14150 add_loc_descr_to_each (list_ret, lhs);
14151 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14152 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
14154 /* ??? Need a node to point the skip at. Use a nop. */
14155 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14156 add_loc_descr_to_each (list_ret, tmp);
14157 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14158 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
14160 break;
14162 case FIX_TRUNC_EXPR:
14163 return 0;
14165 default:
14166 /* Leave front-end specific codes as simply unknown. This comes
14167 up, for instance, with the C STMT_EXPR. */
14168 if ((unsigned int) TREE_CODE (loc)
14169 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
14171 expansion_failed (loc, NULL_RTX,
14172 "language specific tree node");
14173 return 0;
14176 #ifdef ENABLE_CHECKING
14177 /* Otherwise this is a generic code; we should just lists all of
14178 these explicitly. We forgot one. */
14179 gcc_unreachable ();
14180 #else
14181 /* In a release build, we want to degrade gracefully: better to
14182 generate incomplete debugging information than to crash. */
14183 return NULL;
14184 #endif
14187 if (!ret && !list_ret)
14188 return 0;
14190 if (want_address == 2 && !have_address
14191 && (dwarf_version >= 4 || !dwarf_strict))
14193 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14195 expansion_failed (loc, NULL_RTX,
14196 "DWARF address size mismatch");
14197 return 0;
14199 if (ret)
14200 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
14201 else
14202 add_loc_descr_to_each (list_ret,
14203 new_loc_descr (DW_OP_stack_value, 0, 0));
14204 have_address = 1;
14206 /* Show if we can't fill the request for an address. */
14207 if (want_address && !have_address)
14209 expansion_failed (loc, NULL_RTX,
14210 "Want address and only have value");
14211 return 0;
14214 gcc_assert (!ret || !list_ret);
14216 /* If we've got an address and don't want one, dereference. */
14217 if (!want_address && have_address)
14219 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14221 if (size > DWARF2_ADDR_SIZE || size == -1)
14223 expansion_failed (loc, NULL_RTX,
14224 "DWARF address size mismatch");
14225 return 0;
14227 else if (size == DWARF2_ADDR_SIZE)
14228 op = DW_OP_deref;
14229 else
14230 op = DW_OP_deref_size;
14232 if (ret)
14233 add_loc_descr (&ret, new_loc_descr (op, size, 0));
14234 else
14235 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
14237 if (ret)
14238 list_ret = new_loc_list (ret, NULL, NULL, NULL);
14240 return list_ret;
14243 /* Same as above but return only single location expression. */
14244 static dw_loc_descr_ref
14245 loc_descriptor_from_tree (tree loc, int want_address)
14247 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
14248 if (!ret)
14249 return NULL;
14250 if (ret->dw_loc_next)
14252 expansion_failed (loc, NULL_RTX,
14253 "Location list where only loc descriptor needed");
14254 return NULL;
14256 return ret->expr;
14259 /* Given a value, round it up to the lowest multiple of `boundary'
14260 which is not less than the value itself. */
14262 static inline HOST_WIDE_INT
14263 ceiling (HOST_WIDE_INT value, unsigned int boundary)
14265 return (((value + boundary - 1) / boundary) * boundary);
14268 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
14269 pointer to the declared type for the relevant field variable, or return
14270 `integer_type_node' if the given node turns out to be an
14271 ERROR_MARK node. */
14273 static inline tree
14274 field_type (const_tree decl)
14276 tree type;
14278 if (TREE_CODE (decl) == ERROR_MARK)
14279 return integer_type_node;
14281 type = DECL_BIT_FIELD_TYPE (decl);
14282 if (type == NULL_TREE)
14283 type = TREE_TYPE (decl);
14285 return type;
14288 /* Given a pointer to a tree node, return the alignment in bits for
14289 it, or else return BITS_PER_WORD if the node actually turns out to
14290 be an ERROR_MARK node. */
14292 static inline unsigned
14293 simple_type_align_in_bits (const_tree type)
14295 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
14298 static inline unsigned
14299 simple_decl_align_in_bits (const_tree decl)
14301 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
14304 /* Return the result of rounding T up to ALIGN. */
14306 static inline double_int
14307 round_up_to_align (double_int t, unsigned int align)
14309 double_int alignd = double_int::from_uhwi (align);
14310 t += alignd;
14311 t += double_int_minus_one;
14312 t = t.div (alignd, true, TRUNC_DIV_EXPR);
14313 t *= alignd;
14314 return t;
14317 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
14318 lowest addressed byte of the "containing object" for the given FIELD_DECL,
14319 or return 0 if we are unable to determine what that offset is, either
14320 because the argument turns out to be a pointer to an ERROR_MARK node, or
14321 because the offset is actually variable. (We can't handle the latter case
14322 just yet). */
14324 static HOST_WIDE_INT
14325 field_byte_offset (const_tree decl)
14327 double_int object_offset_in_bits;
14328 double_int object_offset_in_bytes;
14329 double_int bitpos_int;
14331 if (TREE_CODE (decl) == ERROR_MARK)
14332 return 0;
14334 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
14336 /* We cannot yet cope with fields whose positions are variable, so
14337 for now, when we see such things, we simply return 0. Someday, we may
14338 be able to handle such cases, but it will be damn difficult. */
14339 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
14340 return 0;
14342 bitpos_int = tree_to_double_int (bit_position (decl));
14344 #ifdef PCC_BITFIELD_TYPE_MATTERS
14345 if (PCC_BITFIELD_TYPE_MATTERS)
14347 tree type;
14348 tree field_size_tree;
14349 double_int deepest_bitpos;
14350 double_int field_size_in_bits;
14351 unsigned int type_align_in_bits;
14352 unsigned int decl_align_in_bits;
14353 double_int type_size_in_bits;
14355 type = field_type (decl);
14356 type_size_in_bits = double_int_type_size_in_bits (type);
14357 type_align_in_bits = simple_type_align_in_bits (type);
14359 field_size_tree = DECL_SIZE (decl);
14361 /* The size could be unspecified if there was an error, or for
14362 a flexible array member. */
14363 if (!field_size_tree)
14364 field_size_tree = bitsize_zero_node;
14366 /* If the size of the field is not constant, use the type size. */
14367 if (TREE_CODE (field_size_tree) == INTEGER_CST)
14368 field_size_in_bits = tree_to_double_int (field_size_tree);
14369 else
14370 field_size_in_bits = type_size_in_bits;
14372 decl_align_in_bits = simple_decl_align_in_bits (decl);
14374 /* The GCC front-end doesn't make any attempt to keep track of the
14375 starting bit offset (relative to the start of the containing
14376 structure type) of the hypothetical "containing object" for a
14377 bit-field. Thus, when computing the byte offset value for the
14378 start of the "containing object" of a bit-field, we must deduce
14379 this information on our own. This can be rather tricky to do in
14380 some cases. For example, handling the following structure type
14381 definition when compiling for an i386/i486 target (which only
14382 aligns long long's to 32-bit boundaries) can be very tricky:
14384 struct S { int field1; long long field2:31; };
14386 Fortunately, there is a simple rule-of-thumb which can be used
14387 in such cases. When compiling for an i386/i486, GCC will
14388 allocate 8 bytes for the structure shown above. It decides to
14389 do this based upon one simple rule for bit-field allocation.
14390 GCC allocates each "containing object" for each bit-field at
14391 the first (i.e. lowest addressed) legitimate alignment boundary
14392 (based upon the required minimum alignment for the declared
14393 type of the field) which it can possibly use, subject to the
14394 condition that there is still enough available space remaining
14395 in the containing object (when allocated at the selected point)
14396 to fully accommodate all of the bits of the bit-field itself.
14398 This simple rule makes it obvious why GCC allocates 8 bytes for
14399 each object of the structure type shown above. When looking
14400 for a place to allocate the "containing object" for `field2',
14401 the compiler simply tries to allocate a 64-bit "containing
14402 object" at each successive 32-bit boundary (starting at zero)
14403 until it finds a place to allocate that 64- bit field such that
14404 at least 31 contiguous (and previously unallocated) bits remain
14405 within that selected 64 bit field. (As it turns out, for the
14406 example above, the compiler finds it is OK to allocate the
14407 "containing object" 64-bit field at bit-offset zero within the
14408 structure type.)
14410 Here we attempt to work backwards from the limited set of facts
14411 we're given, and we try to deduce from those facts, where GCC
14412 must have believed that the containing object started (within
14413 the structure type). The value we deduce is then used (by the
14414 callers of this routine) to generate DW_AT_location and
14415 DW_AT_bit_offset attributes for fields (both bit-fields and, in
14416 the case of DW_AT_location, regular fields as well). */
14418 /* Figure out the bit-distance from the start of the structure to
14419 the "deepest" bit of the bit-field. */
14420 deepest_bitpos = bitpos_int + field_size_in_bits;
14422 /* This is the tricky part. Use some fancy footwork to deduce
14423 where the lowest addressed bit of the containing object must
14424 be. */
14425 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14427 /* Round up to type_align by default. This works best for
14428 bitfields. */
14429 object_offset_in_bits
14430 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
14432 if (object_offset_in_bits.ugt (bitpos_int))
14434 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14436 /* Round up to decl_align instead. */
14437 object_offset_in_bits
14438 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
14441 else
14442 #endif /* PCC_BITFIELD_TYPE_MATTERS */
14443 object_offset_in_bits = bitpos_int;
14445 object_offset_in_bytes
14446 = object_offset_in_bits.div (double_int::from_uhwi (BITS_PER_UNIT),
14447 true, TRUNC_DIV_EXPR);
14448 return object_offset_in_bytes.to_shwi ();
14451 /* The following routines define various Dwarf attributes and any data
14452 associated with them. */
14454 /* Add a location description attribute value to a DIE.
14456 This emits location attributes suitable for whole variables and
14457 whole parameters. Note that the location attributes for struct fields are
14458 generated by the routine `data_member_location_attribute' below. */
14460 static inline void
14461 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
14462 dw_loc_list_ref descr)
14464 if (descr == 0)
14465 return;
14466 if (single_element_loc_list_p (descr))
14467 add_AT_loc (die, attr_kind, descr->expr);
14468 else
14469 add_AT_loc_list (die, attr_kind, descr);
14472 /* Add DW_AT_accessibility attribute to DIE if needed. */
14474 static void
14475 add_accessibility_attribute (dw_die_ref die, tree decl)
14477 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
14478 children, otherwise the default is DW_ACCESS_public. In DWARF2
14479 the default has always been DW_ACCESS_public. */
14480 if (TREE_PROTECTED (decl))
14481 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
14482 else if (TREE_PRIVATE (decl))
14484 if (dwarf_version == 2
14485 || die->die_parent == NULL
14486 || die->die_parent->die_tag != DW_TAG_class_type)
14487 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
14489 else if (dwarf_version > 2
14490 && die->die_parent
14491 && die->die_parent->die_tag == DW_TAG_class_type)
14492 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
14495 /* Attach the specialized form of location attribute used for data members of
14496 struct and union types. In the special case of a FIELD_DECL node which
14497 represents a bit-field, the "offset" part of this special location
14498 descriptor must indicate the distance in bytes from the lowest-addressed
14499 byte of the containing struct or union type to the lowest-addressed byte of
14500 the "containing object" for the bit-field. (See the `field_byte_offset'
14501 function above).
14503 For any given bit-field, the "containing object" is a hypothetical object
14504 (of some integral or enum type) within which the given bit-field lives. The
14505 type of this hypothetical "containing object" is always the same as the
14506 declared type of the individual bit-field itself (for GCC anyway... the
14507 DWARF spec doesn't actually mandate this). Note that it is the size (in
14508 bytes) of the hypothetical "containing object" which will be given in the
14509 DW_AT_byte_size attribute for this bit-field. (See the
14510 `byte_size_attribute' function below.) It is also used when calculating the
14511 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
14512 function below.) */
14514 static void
14515 add_data_member_location_attribute (dw_die_ref die, tree decl)
14517 HOST_WIDE_INT offset;
14518 dw_loc_descr_ref loc_descr = 0;
14520 if (TREE_CODE (decl) == TREE_BINFO)
14522 /* We're working on the TAG_inheritance for a base class. */
14523 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
14525 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
14526 aren't at a fixed offset from all (sub)objects of the same
14527 type. We need to extract the appropriate offset from our
14528 vtable. The following dwarf expression means
14530 BaseAddr = ObAddr + *((*ObAddr) - Offset)
14532 This is specific to the V3 ABI, of course. */
14534 dw_loc_descr_ref tmp;
14536 /* Make a copy of the object address. */
14537 tmp = new_loc_descr (DW_OP_dup, 0, 0);
14538 add_loc_descr (&loc_descr, tmp);
14540 /* Extract the vtable address. */
14541 tmp = new_loc_descr (DW_OP_deref, 0, 0);
14542 add_loc_descr (&loc_descr, tmp);
14544 /* Calculate the address of the offset. */
14545 offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
14546 gcc_assert (offset < 0);
14548 tmp = int_loc_descriptor (-offset);
14549 add_loc_descr (&loc_descr, tmp);
14550 tmp = new_loc_descr (DW_OP_minus, 0, 0);
14551 add_loc_descr (&loc_descr, tmp);
14553 /* Extract the offset. */
14554 tmp = new_loc_descr (DW_OP_deref, 0, 0);
14555 add_loc_descr (&loc_descr, tmp);
14557 /* Add it to the object address. */
14558 tmp = new_loc_descr (DW_OP_plus, 0, 0);
14559 add_loc_descr (&loc_descr, tmp);
14561 else
14562 offset = tree_low_cst (BINFO_OFFSET (decl), 0);
14564 else
14565 offset = field_byte_offset (decl);
14567 if (! loc_descr)
14569 if (dwarf_version > 2)
14571 /* Don't need to output a location expression, just the constant. */
14572 if (offset < 0)
14573 add_AT_int (die, DW_AT_data_member_location, offset);
14574 else
14575 add_AT_unsigned (die, DW_AT_data_member_location, offset);
14576 return;
14578 else
14580 enum dwarf_location_atom op;
14582 /* The DWARF2 standard says that we should assume that the structure
14583 address is already on the stack, so we can specify a structure
14584 field address by using DW_OP_plus_uconst. */
14585 op = DW_OP_plus_uconst;
14586 loc_descr = new_loc_descr (op, offset, 0);
14590 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
14593 /* Writes integer values to dw_vec_const array. */
14595 static void
14596 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
14598 while (size != 0)
14600 *dest++ = val & 0xff;
14601 val >>= 8;
14602 --size;
14606 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
14608 static HOST_WIDE_INT
14609 extract_int (const unsigned char *src, unsigned int size)
14611 HOST_WIDE_INT val = 0;
14613 src += size;
14614 while (size != 0)
14616 val <<= 8;
14617 val |= *--src & 0xff;
14618 --size;
14620 return val;
14623 /* Writes double_int values to dw_vec_const array. */
14625 static void
14626 insert_double (double_int val, unsigned char *dest)
14628 unsigned char *p0 = dest;
14629 unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
14631 if (WORDS_BIG_ENDIAN)
14633 p0 = p1;
14634 p1 = dest;
14637 insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
14638 insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
14641 /* Writes floating point values to dw_vec_const array. */
14643 static void
14644 insert_float (const_rtx rtl, unsigned char *array)
14646 REAL_VALUE_TYPE rv;
14647 long val[4];
14648 int i;
14650 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
14651 real_to_target (val, &rv, GET_MODE (rtl));
14653 /* real_to_target puts 32-bit pieces in each long. Pack them. */
14654 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
14656 insert_int (val[i], 4, array);
14657 array += 4;
14661 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
14662 does not have a "location" either in memory or in a register. These
14663 things can arise in GNU C when a constant is passed as an actual parameter
14664 to an inlined function. They can also arise in C++ where declared
14665 constants do not necessarily get memory "homes". */
14667 static bool
14668 add_const_value_attribute (dw_die_ref die, rtx rtl)
14670 switch (GET_CODE (rtl))
14672 case CONST_INT:
14674 HOST_WIDE_INT val = INTVAL (rtl);
14676 if (val < 0)
14677 add_AT_int (die, DW_AT_const_value, val);
14678 else
14679 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
14681 return true;
14683 case CONST_DOUBLE:
14684 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
14685 floating-point constant. A CONST_DOUBLE is used whenever the
14686 constant requires more than one word in order to be adequately
14687 represented. */
14689 enum machine_mode mode = GET_MODE (rtl);
14691 if (SCALAR_FLOAT_MODE_P (mode))
14693 unsigned int length = GET_MODE_SIZE (mode);
14694 unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
14696 insert_float (rtl, array);
14697 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
14699 else
14700 add_AT_double (die, DW_AT_const_value,
14701 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
14703 return true;
14705 case CONST_VECTOR:
14707 enum machine_mode mode = GET_MODE (rtl);
14708 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
14709 unsigned int length = CONST_VECTOR_NUNITS (rtl);
14710 unsigned char *array = (unsigned char *) ggc_alloc_atomic
14711 (length * elt_size);
14712 unsigned int i;
14713 unsigned char *p;
14715 switch (GET_MODE_CLASS (mode))
14717 case MODE_VECTOR_INT:
14718 for (i = 0, p = array; i < length; i++, p += elt_size)
14720 rtx elt = CONST_VECTOR_ELT (rtl, i);
14721 double_int val = rtx_to_double_int (elt);
14723 if (elt_size <= sizeof (HOST_WIDE_INT))
14724 insert_int (val.to_shwi (), elt_size, p);
14725 else
14727 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
14728 insert_double (val, p);
14731 break;
14733 case MODE_VECTOR_FLOAT:
14734 for (i = 0, p = array; i < length; i++, p += elt_size)
14736 rtx elt = CONST_VECTOR_ELT (rtl, i);
14737 insert_float (elt, p);
14739 break;
14741 default:
14742 gcc_unreachable ();
14745 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
14747 return true;
14749 case CONST_STRING:
14750 if (dwarf_version >= 4 || !dwarf_strict)
14752 dw_loc_descr_ref loc_result;
14753 resolve_one_addr (&rtl, NULL);
14754 rtl_addr:
14755 loc_result = new_addr_loc_descr (rtl, dtprel_false);
14756 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14757 add_AT_loc (die, DW_AT_location, loc_result);
14758 vec_safe_push (used_rtx_array, rtl);
14759 return true;
14761 return false;
14763 case CONST:
14764 if (CONSTANT_P (XEXP (rtl, 0)))
14765 return add_const_value_attribute (die, XEXP (rtl, 0));
14766 /* FALLTHROUGH */
14767 case SYMBOL_REF:
14768 if (!const_ok_for_output (rtl))
14769 return false;
14770 case LABEL_REF:
14771 if (dwarf_version >= 4 || !dwarf_strict)
14772 goto rtl_addr;
14773 return false;
14775 case PLUS:
14776 /* In cases where an inlined instance of an inline function is passed
14777 the address of an `auto' variable (which is local to the caller) we
14778 can get a situation where the DECL_RTL of the artificial local
14779 variable (for the inlining) which acts as a stand-in for the
14780 corresponding formal parameter (of the inline function) will look
14781 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
14782 exactly a compile-time constant expression, but it isn't the address
14783 of the (artificial) local variable either. Rather, it represents the
14784 *value* which the artificial local variable always has during its
14785 lifetime. We currently have no way to represent such quasi-constant
14786 values in Dwarf, so for now we just punt and generate nothing. */
14787 return false;
14789 case HIGH:
14790 case CONST_FIXED:
14791 return false;
14793 case MEM:
14794 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
14795 && MEM_READONLY_P (rtl)
14796 && GET_MODE (rtl) == BLKmode)
14798 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
14799 return true;
14801 return false;
14803 default:
14804 /* No other kinds of rtx should be possible here. */
14805 gcc_unreachable ();
14807 return false;
14810 /* Determine whether the evaluation of EXPR references any variables
14811 or functions which aren't otherwise used (and therefore may not be
14812 output). */
14813 static tree
14814 reference_to_unused (tree * tp, int * walk_subtrees,
14815 void * data ATTRIBUTE_UNUSED)
14817 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
14818 *walk_subtrees = 0;
14820 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
14821 && ! TREE_ASM_WRITTEN (*tp))
14822 return *tp;
14823 /* ??? The C++ FE emits debug information for using decls, so
14824 putting gcc_unreachable here falls over. See PR31899. For now
14825 be conservative. */
14826 else if (!cgraph_global_info_ready
14827 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
14828 return *tp;
14829 else if (TREE_CODE (*tp) == VAR_DECL)
14831 struct varpool_node *node = varpool_get_node (*tp);
14832 if (!node || !node->analyzed)
14833 return *tp;
14835 else if (TREE_CODE (*tp) == FUNCTION_DECL
14836 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
14838 /* The call graph machinery must have finished analyzing,
14839 optimizing and gimplifying the CU by now.
14840 So if *TP has no call graph node associated
14841 to it, it means *TP will not be emitted. */
14842 if (!cgraph_get_node (*tp))
14843 return *tp;
14845 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
14846 return *tp;
14848 return NULL_TREE;
14851 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
14852 for use in a later add_const_value_attribute call. */
14854 static rtx
14855 rtl_for_decl_init (tree init, tree type)
14857 rtx rtl = NULL_RTX;
14859 STRIP_NOPS (init);
14861 /* If a variable is initialized with a string constant without embedded
14862 zeros, build CONST_STRING. */
14863 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
14865 tree enttype = TREE_TYPE (type);
14866 tree domain = TYPE_DOMAIN (type);
14867 enum machine_mode mode = TYPE_MODE (enttype);
14869 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
14870 && domain
14871 && integer_zerop (TYPE_MIN_VALUE (domain))
14872 && compare_tree_int (TYPE_MAX_VALUE (domain),
14873 TREE_STRING_LENGTH (init) - 1) == 0
14874 && ((size_t) TREE_STRING_LENGTH (init)
14875 == strlen (TREE_STRING_POINTER (init)) + 1))
14877 rtl = gen_rtx_CONST_STRING (VOIDmode,
14878 ggc_strdup (TREE_STRING_POINTER (init)));
14879 rtl = gen_rtx_MEM (BLKmode, rtl);
14880 MEM_READONLY_P (rtl) = 1;
14883 /* Other aggregates, and complex values, could be represented using
14884 CONCAT: FIXME! */
14885 else if (AGGREGATE_TYPE_P (type)
14886 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
14887 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
14888 || TREE_CODE (type) == COMPLEX_TYPE)
14890 /* Vectors only work if their mode is supported by the target.
14891 FIXME: generic vectors ought to work too. */
14892 else if (TREE_CODE (type) == VECTOR_TYPE
14893 && !VECTOR_MODE_P (TYPE_MODE (type)))
14895 /* If the initializer is something that we know will expand into an
14896 immediate RTL constant, expand it now. We must be careful not to
14897 reference variables which won't be output. */
14898 else if (initializer_constant_valid_p (init, type)
14899 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
14901 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
14902 possible. */
14903 if (TREE_CODE (type) == VECTOR_TYPE)
14904 switch (TREE_CODE (init))
14906 case VECTOR_CST:
14907 break;
14908 case CONSTRUCTOR:
14909 if (TREE_CONSTANT (init))
14911 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
14912 bool constant_p = true;
14913 tree value;
14914 unsigned HOST_WIDE_INT ix;
14916 /* Even when ctor is constant, it might contain non-*_CST
14917 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
14918 belong into VECTOR_CST nodes. */
14919 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
14920 if (!CONSTANT_CLASS_P (value))
14922 constant_p = false;
14923 break;
14926 if (constant_p)
14928 init = build_vector_from_ctor (type, elts);
14929 break;
14932 /* FALLTHRU */
14934 default:
14935 return NULL;
14938 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
14940 /* If expand_expr returns a MEM, it wasn't immediate. */
14941 gcc_assert (!rtl || !MEM_P (rtl));
14944 return rtl;
14947 /* Generate RTL for the variable DECL to represent its location. */
14949 static rtx
14950 rtl_for_decl_location (tree decl)
14952 rtx rtl;
14954 /* Here we have to decide where we are going to say the parameter "lives"
14955 (as far as the debugger is concerned). We only have a couple of
14956 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
14958 DECL_RTL normally indicates where the parameter lives during most of the
14959 activation of the function. If optimization is enabled however, this
14960 could be either NULL or else a pseudo-reg. Both of those cases indicate
14961 that the parameter doesn't really live anywhere (as far as the code
14962 generation parts of GCC are concerned) during most of the function's
14963 activation. That will happen (for example) if the parameter is never
14964 referenced within the function.
14966 We could just generate a location descriptor here for all non-NULL
14967 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
14968 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
14969 where DECL_RTL is NULL or is a pseudo-reg.
14971 Note however that we can only get away with using DECL_INCOMING_RTL as
14972 a backup substitute for DECL_RTL in certain limited cases. In cases
14973 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
14974 we can be sure that the parameter was passed using the same type as it is
14975 declared to have within the function, and that its DECL_INCOMING_RTL
14976 points us to a place where a value of that type is passed.
14978 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
14979 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
14980 because in these cases DECL_INCOMING_RTL points us to a value of some
14981 type which is *different* from the type of the parameter itself. Thus,
14982 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
14983 such cases, the debugger would end up (for example) trying to fetch a
14984 `float' from a place which actually contains the first part of a
14985 `double'. That would lead to really incorrect and confusing
14986 output at debug-time.
14988 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
14989 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
14990 are a couple of exceptions however. On little-endian machines we can
14991 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
14992 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
14993 an integral type that is smaller than TREE_TYPE (decl). These cases arise
14994 when (on a little-endian machine) a non-prototyped function has a
14995 parameter declared to be of type `short' or `char'. In such cases,
14996 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
14997 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
14998 passed `int' value. If the debugger then uses that address to fetch
14999 a `short' or a `char' (on a little-endian machine) the result will be
15000 the correct data, so we allow for such exceptional cases below.
15002 Note that our goal here is to describe the place where the given formal
15003 parameter lives during most of the function's activation (i.e. between the
15004 end of the prologue and the start of the epilogue). We'll do that as best
15005 as we can. Note however that if the given formal parameter is modified
15006 sometime during the execution of the function, then a stack backtrace (at
15007 debug-time) will show the function as having been called with the *new*
15008 value rather than the value which was originally passed in. This happens
15009 rarely enough that it is not a major problem, but it *is* a problem, and
15010 I'd like to fix it.
15012 A future version of dwarf2out.c may generate two additional attributes for
15013 any given DW_TAG_formal_parameter DIE which will describe the "passed
15014 type" and the "passed location" for the given formal parameter in addition
15015 to the attributes we now generate to indicate the "declared type" and the
15016 "active location" for each parameter. This additional set of attributes
15017 could be used by debuggers for stack backtraces. Separately, note that
15018 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15019 This happens (for example) for inlined-instances of inline function formal
15020 parameters which are never referenced. This really shouldn't be
15021 happening. All PARM_DECL nodes should get valid non-NULL
15022 DECL_INCOMING_RTL values. FIXME. */
15024 /* Use DECL_RTL as the "location" unless we find something better. */
15025 rtl = DECL_RTL_IF_SET (decl);
15027 /* When generating abstract instances, ignore everything except
15028 constants, symbols living in memory, and symbols living in
15029 fixed registers. */
15030 if (! reload_completed)
15032 if (rtl
15033 && (CONSTANT_P (rtl)
15034 || (MEM_P (rtl)
15035 && CONSTANT_P (XEXP (rtl, 0)))
15036 || (REG_P (rtl)
15037 && TREE_CODE (decl) == VAR_DECL
15038 && TREE_STATIC (decl))))
15040 rtl = targetm.delegitimize_address (rtl);
15041 return rtl;
15043 rtl = NULL_RTX;
15045 else if (TREE_CODE (decl) == PARM_DECL)
15047 if (rtl == NULL_RTX
15048 || is_pseudo_reg (rtl)
15049 || (MEM_P (rtl)
15050 && is_pseudo_reg (XEXP (rtl, 0))
15051 && DECL_INCOMING_RTL (decl)
15052 && MEM_P (DECL_INCOMING_RTL (decl))
15053 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15055 tree declared_type = TREE_TYPE (decl);
15056 tree passed_type = DECL_ARG_TYPE (decl);
15057 enum machine_mode dmode = TYPE_MODE (declared_type);
15058 enum machine_mode pmode = TYPE_MODE (passed_type);
15060 /* This decl represents a formal parameter which was optimized out.
15061 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15062 all cases where (rtl == NULL_RTX) just below. */
15063 if (dmode == pmode)
15064 rtl = DECL_INCOMING_RTL (decl);
15065 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15066 && SCALAR_INT_MODE_P (dmode)
15067 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15068 && DECL_INCOMING_RTL (decl))
15070 rtx inc = DECL_INCOMING_RTL (decl);
15071 if (REG_P (inc))
15072 rtl = inc;
15073 else if (MEM_P (inc))
15075 if (BYTES_BIG_ENDIAN)
15076 rtl = adjust_address_nv (inc, dmode,
15077 GET_MODE_SIZE (pmode)
15078 - GET_MODE_SIZE (dmode));
15079 else
15080 rtl = inc;
15085 /* If the parm was passed in registers, but lives on the stack, then
15086 make a big endian correction if the mode of the type of the
15087 parameter is not the same as the mode of the rtl. */
15088 /* ??? This is the same series of checks that are made in dbxout.c before
15089 we reach the big endian correction code there. It isn't clear if all
15090 of these checks are necessary here, but keeping them all is the safe
15091 thing to do. */
15092 else if (MEM_P (rtl)
15093 && XEXP (rtl, 0) != const0_rtx
15094 && ! CONSTANT_P (XEXP (rtl, 0))
15095 /* Not passed in memory. */
15096 && !MEM_P (DECL_INCOMING_RTL (decl))
15097 /* Not passed by invisible reference. */
15098 && (!REG_P (XEXP (rtl, 0))
15099 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15100 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15101 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15102 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15103 #endif
15105 /* Big endian correction check. */
15106 && BYTES_BIG_ENDIAN
15107 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15108 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15109 < UNITS_PER_WORD))
15111 enum machine_mode addr_mode = get_address_mode (rtl);
15112 int offset = (UNITS_PER_WORD
15113 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15115 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15116 plus_constant (addr_mode, XEXP (rtl, 0), offset));
15119 else if (TREE_CODE (decl) == VAR_DECL
15120 && rtl
15121 && MEM_P (rtl)
15122 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
15123 && BYTES_BIG_ENDIAN)
15125 enum machine_mode addr_mode = get_address_mode (rtl);
15126 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
15127 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
15129 /* If a variable is declared "register" yet is smaller than
15130 a register, then if we store the variable to memory, it
15131 looks like we're storing a register-sized value, when in
15132 fact we are not. We need to adjust the offset of the
15133 storage location to reflect the actual value's bytes,
15134 else gdb will not be able to display it. */
15135 if (rsize > dsize)
15136 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15137 plus_constant (addr_mode, XEXP (rtl, 0),
15138 rsize - dsize));
15141 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
15142 and will have been substituted directly into all expressions that use it.
15143 C does not have such a concept, but C++ and other languages do. */
15144 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
15145 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
15147 if (rtl)
15148 rtl = targetm.delegitimize_address (rtl);
15150 /* If we don't look past the constant pool, we risk emitting a
15151 reference to a constant pool entry that isn't referenced from
15152 code, and thus is not emitted. */
15153 if (rtl)
15154 rtl = avoid_constant_pool_reference (rtl);
15156 /* Try harder to get a rtl. If this symbol ends up not being emitted
15157 in the current CU, resolve_addr will remove the expression referencing
15158 it. */
15159 if (rtl == NULL_RTX
15160 && TREE_CODE (decl) == VAR_DECL
15161 && !DECL_EXTERNAL (decl)
15162 && TREE_STATIC (decl)
15163 && DECL_NAME (decl)
15164 && !DECL_HARD_REGISTER (decl)
15165 && DECL_MODE (decl) != VOIDmode)
15167 rtl = make_decl_rtl_for_debug (decl);
15168 if (!MEM_P (rtl)
15169 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
15170 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
15171 rtl = NULL_RTX;
15174 return rtl;
15177 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
15178 returned. If so, the decl for the COMMON block is returned, and the
15179 value is the offset into the common block for the symbol. */
15181 static tree
15182 fortran_common (tree decl, HOST_WIDE_INT *value)
15184 tree val_expr, cvar;
15185 enum machine_mode mode;
15186 HOST_WIDE_INT bitsize, bitpos;
15187 tree offset;
15188 int unsignedp, volatilep = 0;
15190 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
15191 it does not have a value (the offset into the common area), or if it
15192 is thread local (as opposed to global) then it isn't common, and shouldn't
15193 be handled as such. */
15194 if (TREE_CODE (decl) != VAR_DECL
15195 || !TREE_STATIC (decl)
15196 || !DECL_HAS_VALUE_EXPR_P (decl)
15197 || !is_fortran ())
15198 return NULL_TREE;
15200 val_expr = DECL_VALUE_EXPR (decl);
15201 if (TREE_CODE (val_expr) != COMPONENT_REF)
15202 return NULL_TREE;
15204 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
15205 &mode, &unsignedp, &volatilep, true);
15207 if (cvar == NULL_TREE
15208 || TREE_CODE (cvar) != VAR_DECL
15209 || DECL_ARTIFICIAL (cvar)
15210 || !TREE_PUBLIC (cvar))
15211 return NULL_TREE;
15213 *value = 0;
15214 if (offset != NULL)
15216 if (!host_integerp (offset, 0))
15217 return NULL_TREE;
15218 *value = tree_low_cst (offset, 0);
15220 if (bitpos != 0)
15221 *value += bitpos / BITS_PER_UNIT;
15223 return cvar;
15226 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
15227 data attribute for a variable or a parameter. We generate the
15228 DW_AT_const_value attribute only in those cases where the given variable
15229 or parameter does not have a true "location" either in memory or in a
15230 register. This can happen (for example) when a constant is passed as an
15231 actual argument in a call to an inline function. (It's possible that
15232 these things can crop up in other ways also.) Note that one type of
15233 constant value which can be passed into an inlined function is a constant
15234 pointer. This can happen for example if an actual argument in an inlined
15235 function call evaluates to a compile-time constant address.
15237 CACHE_P is true if it is worth caching the location list for DECL,
15238 so that future calls can reuse it rather than regenerate it from scratch.
15239 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
15240 since we will need to refer to them each time the function is inlined. */
15242 static bool
15243 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
15244 enum dwarf_attribute attr)
15246 rtx rtl;
15247 dw_loc_list_ref list;
15248 var_loc_list *loc_list;
15249 cached_dw_loc_list *cache;
15250 void **slot;
15252 if (TREE_CODE (decl) == ERROR_MARK)
15253 return false;
15255 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
15256 || TREE_CODE (decl) == RESULT_DECL);
15258 /* Try to get some constant RTL for this decl, and use that as the value of
15259 the location. */
15261 rtl = rtl_for_decl_location (decl);
15262 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15263 && add_const_value_attribute (die, rtl))
15264 return true;
15266 /* See if we have single element location list that is equivalent to
15267 a constant value. That way we are better to use add_const_value_attribute
15268 rather than expanding constant value equivalent. */
15269 loc_list = lookup_decl_loc (decl);
15270 if (loc_list
15271 && loc_list->first
15272 && loc_list->first->next == NULL
15273 && NOTE_P (loc_list->first->loc)
15274 && NOTE_VAR_LOCATION (loc_list->first->loc)
15275 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
15277 struct var_loc_node *node;
15279 node = loc_list->first;
15280 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
15281 if (GET_CODE (rtl) == EXPR_LIST)
15282 rtl = XEXP (rtl, 0);
15283 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15284 && add_const_value_attribute (die, rtl))
15285 return true;
15287 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
15288 list several times. See if we've already cached the contents. */
15289 list = NULL;
15290 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
15291 cache_p = false;
15292 if (cache_p)
15294 cache = (cached_dw_loc_list *)
15295 htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
15296 if (cache)
15297 list = cache->loc_list;
15299 if (list == NULL)
15301 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
15302 /* It is usually worth caching this result if the decl is from
15303 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
15304 if (cache_p && list && list->dw_loc_next)
15306 slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
15307 DECL_UID (decl), INSERT);
15308 cache = ggc_alloc_cleared_cached_dw_loc_list ();
15309 cache->decl_id = DECL_UID (decl);
15310 cache->loc_list = list;
15311 *slot = cache;
15314 if (list)
15316 add_AT_location_description (die, attr, list);
15317 return true;
15319 /* None of that worked, so it must not really have a location;
15320 try adding a constant value attribute from the DECL_INITIAL. */
15321 return tree_add_const_value_attribute_for_decl (die, decl);
15324 /* Add VARIABLE and DIE into deferred locations list. */
15326 static void
15327 defer_location (tree variable, dw_die_ref die)
15329 deferred_locations entry;
15330 entry.variable = variable;
15331 entry.die = die;
15332 vec_safe_push (deferred_locations_list, entry);
15335 /* Helper function for tree_add_const_value_attribute. Natively encode
15336 initializer INIT into an array. Return true if successful. */
15338 static bool
15339 native_encode_initializer (tree init, unsigned char *array, int size)
15341 tree type;
15343 if (init == NULL_TREE)
15344 return false;
15346 STRIP_NOPS (init);
15347 switch (TREE_CODE (init))
15349 case STRING_CST:
15350 type = TREE_TYPE (init);
15351 if (TREE_CODE (type) == ARRAY_TYPE)
15353 tree enttype = TREE_TYPE (type);
15354 enum machine_mode mode = TYPE_MODE (enttype);
15356 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
15357 return false;
15358 if (int_size_in_bytes (type) != size)
15359 return false;
15360 if (size > TREE_STRING_LENGTH (init))
15362 memcpy (array, TREE_STRING_POINTER (init),
15363 TREE_STRING_LENGTH (init));
15364 memset (array + TREE_STRING_LENGTH (init),
15365 '\0', size - TREE_STRING_LENGTH (init));
15367 else
15368 memcpy (array, TREE_STRING_POINTER (init), size);
15369 return true;
15371 return false;
15372 case CONSTRUCTOR:
15373 type = TREE_TYPE (init);
15374 if (int_size_in_bytes (type) != size)
15375 return false;
15376 if (TREE_CODE (type) == ARRAY_TYPE)
15378 HOST_WIDE_INT min_index;
15379 unsigned HOST_WIDE_INT cnt;
15380 int curpos = 0, fieldsize;
15381 constructor_elt *ce;
15383 if (TYPE_DOMAIN (type) == NULL_TREE
15384 || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
15385 return false;
15387 fieldsize = int_size_in_bytes (TREE_TYPE (type));
15388 if (fieldsize <= 0)
15389 return false;
15391 min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
15392 memset (array, '\0', size);
15393 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15395 tree val = ce->value;
15396 tree index = ce->index;
15397 int pos = curpos;
15398 if (index && TREE_CODE (index) == RANGE_EXPR)
15399 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
15400 * fieldsize;
15401 else if (index)
15402 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
15404 if (val)
15406 STRIP_NOPS (val);
15407 if (!native_encode_initializer (val, array + pos, fieldsize))
15408 return false;
15410 curpos = pos + fieldsize;
15411 if (index && TREE_CODE (index) == RANGE_EXPR)
15413 int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
15414 - tree_low_cst (TREE_OPERAND (index, 0), 0);
15415 while (count-- > 0)
15417 if (val)
15418 memcpy (array + curpos, array + pos, fieldsize);
15419 curpos += fieldsize;
15422 gcc_assert (curpos <= size);
15424 return true;
15426 else if (TREE_CODE (type) == RECORD_TYPE
15427 || TREE_CODE (type) == UNION_TYPE)
15429 tree field = NULL_TREE;
15430 unsigned HOST_WIDE_INT cnt;
15431 constructor_elt *ce;
15433 if (int_size_in_bytes (type) != size)
15434 return false;
15436 if (TREE_CODE (type) == RECORD_TYPE)
15437 field = TYPE_FIELDS (type);
15439 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15441 tree val = ce->value;
15442 int pos, fieldsize;
15444 if (ce->index != 0)
15445 field = ce->index;
15447 if (val)
15448 STRIP_NOPS (val);
15450 if (field == NULL_TREE || DECL_BIT_FIELD (field))
15451 return false;
15453 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
15454 && TYPE_DOMAIN (TREE_TYPE (field))
15455 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
15456 return false;
15457 else if (DECL_SIZE_UNIT (field) == NULL_TREE
15458 || !host_integerp (DECL_SIZE_UNIT (field), 0))
15459 return false;
15460 fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
15461 pos = int_byte_position (field);
15462 gcc_assert (pos + fieldsize <= size);
15463 if (val
15464 && !native_encode_initializer (val, array + pos, fieldsize))
15465 return false;
15467 return true;
15469 return false;
15470 case VIEW_CONVERT_EXPR:
15471 case NON_LVALUE_EXPR:
15472 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
15473 default:
15474 return native_encode_expr (init, array, size) == size;
15478 /* Attach a DW_AT_const_value attribute to DIE. The value of the
15479 attribute is the const value T. */
15481 static bool
15482 tree_add_const_value_attribute (dw_die_ref die, tree t)
15484 tree init;
15485 tree type = TREE_TYPE (t);
15486 rtx rtl;
15488 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
15489 return false;
15491 init = t;
15492 gcc_assert (!DECL_P (init));
15494 rtl = rtl_for_decl_init (init, type);
15495 if (rtl)
15496 return add_const_value_attribute (die, rtl);
15497 /* If the host and target are sane, try harder. */
15498 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
15499 && initializer_constant_valid_p (init, type))
15501 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
15502 if (size > 0 && (int) size == size)
15504 unsigned char *array = (unsigned char *)
15505 ggc_alloc_cleared_atomic (size);
15507 if (native_encode_initializer (init, array, size))
15509 add_AT_vec (die, DW_AT_const_value, size, 1, array);
15510 return true;
15514 return false;
15517 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
15518 attribute is the const value of T, where T is an integral constant
15519 variable with static storage duration
15520 (so it can't be a PARM_DECL or a RESULT_DECL). */
15522 static bool
15523 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
15526 if (!decl
15527 || (TREE_CODE (decl) != VAR_DECL
15528 && TREE_CODE (decl) != CONST_DECL)
15529 || (TREE_CODE (decl) == VAR_DECL
15530 && !TREE_STATIC (decl)))
15531 return false;
15533 if (TREE_READONLY (decl)
15534 && ! TREE_THIS_VOLATILE (decl)
15535 && DECL_INITIAL (decl))
15536 /* OK */;
15537 else
15538 return false;
15540 /* Don't add DW_AT_const_value if abstract origin already has one. */
15541 if (get_AT (var_die, DW_AT_const_value))
15542 return false;
15544 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
15547 /* Convert the CFI instructions for the current function into a
15548 location list. This is used for DW_AT_frame_base when we targeting
15549 a dwarf2 consumer that does not support the dwarf3
15550 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
15551 expressions. */
15553 static dw_loc_list_ref
15554 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
15556 int ix;
15557 dw_fde_ref fde;
15558 dw_loc_list_ref list, *list_tail;
15559 dw_cfi_ref cfi;
15560 dw_cfa_location last_cfa, next_cfa;
15561 const char *start_label, *last_label, *section;
15562 dw_cfa_location remember;
15564 fde = cfun->fde;
15565 gcc_assert (fde != NULL);
15567 section = secname_for_decl (current_function_decl);
15568 list_tail = &list;
15569 list = NULL;
15571 memset (&next_cfa, 0, sizeof (next_cfa));
15572 next_cfa.reg = INVALID_REGNUM;
15573 remember = next_cfa;
15575 start_label = fde->dw_fde_begin;
15577 /* ??? Bald assumption that the CIE opcode list does not contain
15578 advance opcodes. */
15579 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
15580 lookup_cfa_1 (cfi, &next_cfa, &remember);
15582 last_cfa = next_cfa;
15583 last_label = start_label;
15585 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
15587 /* If the first partition contained no CFI adjustments, the
15588 CIE opcodes apply to the whole first partition. */
15589 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15590 fde->dw_fde_begin, fde->dw_fde_end, section);
15591 list_tail =&(*list_tail)->dw_loc_next;
15592 start_label = last_label = fde->dw_fde_second_begin;
15595 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
15597 switch (cfi->dw_cfi_opc)
15599 case DW_CFA_set_loc:
15600 case DW_CFA_advance_loc1:
15601 case DW_CFA_advance_loc2:
15602 case DW_CFA_advance_loc4:
15603 if (!cfa_equal_p (&last_cfa, &next_cfa))
15605 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15606 start_label, last_label, section);
15608 list_tail = &(*list_tail)->dw_loc_next;
15609 last_cfa = next_cfa;
15610 start_label = last_label;
15612 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
15613 break;
15615 case DW_CFA_advance_loc:
15616 /* The encoding is complex enough that we should never emit this. */
15617 gcc_unreachable ();
15619 default:
15620 lookup_cfa_1 (cfi, &next_cfa, &remember);
15621 break;
15623 if (ix + 1 == fde->dw_fde_switch_cfi_index)
15625 if (!cfa_equal_p (&last_cfa, &next_cfa))
15627 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15628 start_label, last_label, section);
15630 list_tail = &(*list_tail)->dw_loc_next;
15631 last_cfa = next_cfa;
15632 start_label = last_label;
15634 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15635 start_label, fde->dw_fde_end, section);
15636 list_tail = &(*list_tail)->dw_loc_next;
15637 start_label = last_label = fde->dw_fde_second_begin;
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);
15645 list_tail = &(*list_tail)->dw_loc_next;
15646 start_label = last_label;
15649 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
15650 start_label,
15651 fde->dw_fde_second_begin
15652 ? fde->dw_fde_second_end : fde->dw_fde_end,
15653 section);
15655 if (list && list->dw_loc_next)
15656 gen_llsym (list);
15658 return list;
15661 /* Compute a displacement from the "steady-state frame pointer" to the
15662 frame base (often the same as the CFA), and store it in
15663 frame_pointer_fb_offset. OFFSET is added to the displacement
15664 before the latter is negated. */
15666 static void
15667 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
15669 rtx reg, elim;
15671 #ifdef FRAME_POINTER_CFA_OFFSET
15672 reg = frame_pointer_rtx;
15673 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
15674 #else
15675 reg = arg_pointer_rtx;
15676 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
15677 #endif
15679 elim = (ira_use_lra_p
15680 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
15681 : eliminate_regs (reg, VOIDmode, NULL_RTX));
15682 if (GET_CODE (elim) == PLUS)
15684 offset += INTVAL (XEXP (elim, 1));
15685 elim = XEXP (elim, 0);
15688 frame_pointer_fb_offset = -offset;
15690 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
15691 in which to eliminate. This is because it's stack pointer isn't
15692 directly accessible as a register within the ISA. To work around
15693 this, assume that while we cannot provide a proper value for
15694 frame_pointer_fb_offset, we won't need one either. */
15695 frame_pointer_fb_offset_valid
15696 = ((SUPPORTS_STACK_ALIGNMENT
15697 && (elim == hard_frame_pointer_rtx
15698 || elim == stack_pointer_rtx))
15699 || elim == (frame_pointer_needed
15700 ? hard_frame_pointer_rtx
15701 : stack_pointer_rtx));
15704 /* Generate a DW_AT_name attribute given some string value to be included as
15705 the value of the attribute. */
15707 static void
15708 add_name_attribute (dw_die_ref die, const char *name_string)
15710 if (name_string != NULL && *name_string != 0)
15712 if (demangle_name_func)
15713 name_string = (*demangle_name_func) (name_string);
15715 add_AT_string (die, DW_AT_name, name_string);
15719 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
15720 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
15721 of TYPE accordingly.
15723 ??? This is a temporary measure until after we're able to generate
15724 regular DWARF for the complex Ada type system. */
15726 static void
15727 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
15728 dw_die_ref context_die)
15730 tree dtype;
15731 dw_die_ref dtype_die;
15733 if (!lang_hooks.types.descriptive_type)
15734 return;
15736 dtype = lang_hooks.types.descriptive_type (type);
15737 if (!dtype)
15738 return;
15740 dtype_die = lookup_type_die (dtype);
15741 if (!dtype_die)
15743 gen_type_die (dtype, context_die);
15744 dtype_die = lookup_type_die (dtype);
15745 gcc_assert (dtype_die);
15748 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
15751 /* Generate a DW_AT_comp_dir attribute for DIE. */
15753 static void
15754 add_comp_dir_attribute (dw_die_ref die)
15756 const char *wd = get_src_pwd ();
15757 char *wd1;
15759 if (wd == NULL)
15760 return;
15762 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
15764 int wdlen;
15766 wdlen = strlen (wd);
15767 wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
15768 strcpy (wd1, wd);
15769 wd1 [wdlen] = DIR_SEPARATOR;
15770 wd1 [wdlen + 1] = 0;
15771 wd = wd1;
15774 add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
15777 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
15778 default. */
15780 static int
15781 lower_bound_default (void)
15783 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
15785 case DW_LANG_C:
15786 case DW_LANG_C89:
15787 case DW_LANG_C99:
15788 case DW_LANG_C_plus_plus:
15789 case DW_LANG_ObjC:
15790 case DW_LANG_ObjC_plus_plus:
15791 case DW_LANG_Java:
15792 return 0;
15793 case DW_LANG_Fortran77:
15794 case DW_LANG_Fortran90:
15795 case DW_LANG_Fortran95:
15796 return 1;
15797 case DW_LANG_UPC:
15798 case DW_LANG_D:
15799 case DW_LANG_Python:
15800 return dwarf_version >= 4 ? 0 : -1;
15801 case DW_LANG_Ada95:
15802 case DW_LANG_Ada83:
15803 case DW_LANG_Cobol74:
15804 case DW_LANG_Cobol85:
15805 case DW_LANG_Pascal83:
15806 case DW_LANG_Modula2:
15807 case DW_LANG_PLI:
15808 return dwarf_version >= 4 ? 1 : -1;
15809 default:
15810 return -1;
15814 /* Given a tree node describing an array bound (either lower or upper) output
15815 a representation for that bound. */
15817 static void
15818 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
15820 switch (TREE_CODE (bound))
15822 case ERROR_MARK:
15823 return;
15825 /* All fixed-bounds are represented by INTEGER_CST nodes. */
15826 case INTEGER_CST:
15828 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
15829 int dflt;
15831 /* Use the default if possible. */
15832 if (bound_attr == DW_AT_lower_bound
15833 && host_integerp (bound, 0)
15834 && (dflt = lower_bound_default ()) != -1
15835 && tree_low_cst (bound, 0) == dflt)
15838 /* Otherwise represent the bound as an unsigned value with the
15839 precision of its type. The precision and signedness of the
15840 type will be necessary to re-interpret it unambiguously. */
15841 else if (prec < HOST_BITS_PER_WIDE_INT)
15843 unsigned HOST_WIDE_INT mask
15844 = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
15845 add_AT_unsigned (subrange_die, bound_attr,
15846 TREE_INT_CST_LOW (bound) & mask);
15848 else if (prec == HOST_BITS_PER_WIDE_INT
15849 || TREE_INT_CST_HIGH (bound) == 0)
15850 add_AT_unsigned (subrange_die, bound_attr,
15851 TREE_INT_CST_LOW (bound));
15852 else
15853 add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
15854 TREE_INT_CST_LOW (bound));
15856 break;
15858 CASE_CONVERT:
15859 case VIEW_CONVERT_EXPR:
15860 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
15861 break;
15863 case SAVE_EXPR:
15864 break;
15866 case VAR_DECL:
15867 case PARM_DECL:
15868 case RESULT_DECL:
15870 dw_die_ref decl_die = lookup_decl_die (bound);
15872 /* ??? Can this happen, or should the variable have been bound
15873 first? Probably it can, since I imagine that we try to create
15874 the types of parameters in the order in which they exist in
15875 the list, and won't have created a forward reference to a
15876 later parameter. */
15877 if (decl_die != NULL)
15879 add_AT_die_ref (subrange_die, bound_attr, decl_die);
15880 break;
15883 /* FALLTHRU */
15885 default:
15887 /* Otherwise try to create a stack operation procedure to
15888 evaluate the value of the array bound. */
15890 dw_die_ref ctx, decl_die;
15891 dw_loc_list_ref list;
15893 list = loc_list_from_tree (bound, 2);
15894 if (list == NULL || single_element_loc_list_p (list))
15896 /* If DW_AT_*bound is not a reference nor constant, it is
15897 a DWARF expression rather than location description.
15898 For that loc_list_from_tree (bound, 0) is needed.
15899 If that fails to give a single element list,
15900 fall back to outputting this as a reference anyway. */
15901 dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
15902 if (list2 && single_element_loc_list_p (list2))
15904 add_AT_loc (subrange_die, bound_attr, list2->expr);
15905 break;
15908 if (list == NULL)
15909 break;
15911 if (current_function_decl == 0)
15912 ctx = comp_unit_die ();
15913 else
15914 ctx = lookup_decl_die (current_function_decl);
15916 decl_die = new_die (DW_TAG_variable, ctx, bound);
15917 add_AT_flag (decl_die, DW_AT_artificial, 1);
15918 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
15919 add_AT_location_description (decl_die, DW_AT_location, list);
15920 add_AT_die_ref (subrange_die, bound_attr, decl_die);
15921 break;
15926 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
15927 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
15928 Note that the block of subscript information for an array type also
15929 includes information about the element type of the given array type. */
15931 static void
15932 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
15934 unsigned dimension_number;
15935 tree lower, upper;
15936 dw_die_ref subrange_die;
15938 for (dimension_number = 0;
15939 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
15940 type = TREE_TYPE (type), dimension_number++)
15942 tree domain = TYPE_DOMAIN (type);
15944 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
15945 break;
15947 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
15948 and (in GNU C only) variable bounds. Handle all three forms
15949 here. */
15950 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
15951 if (domain)
15953 /* We have an array type with specified bounds. */
15954 lower = TYPE_MIN_VALUE (domain);
15955 upper = TYPE_MAX_VALUE (domain);
15957 /* Define the index type. */
15958 if (TREE_TYPE (domain))
15960 /* ??? This is probably an Ada unnamed subrange type. Ignore the
15961 TREE_TYPE field. We can't emit debug info for this
15962 because it is an unnamed integral type. */
15963 if (TREE_CODE (domain) == INTEGER_TYPE
15964 && TYPE_NAME (domain) == NULL_TREE
15965 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
15966 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
15968 else
15969 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
15970 type_die);
15973 /* ??? If upper is NULL, the array has unspecified length,
15974 but it does have a lower bound. This happens with Fortran
15975 dimension arr(N:*)
15976 Since the debugger is definitely going to need to know N
15977 to produce useful results, go ahead and output the lower
15978 bound solo, and hope the debugger can cope. */
15980 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
15981 if (upper)
15982 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
15985 /* Otherwise we have an array type with an unspecified length. The
15986 DWARF-2 spec does not say how to handle this; let's just leave out the
15987 bounds. */
15991 static void
15992 add_byte_size_attribute (dw_die_ref die, tree tree_node)
15994 dw_die_ref decl_die;
15995 unsigned size;
15997 switch (TREE_CODE (tree_node))
15999 case ERROR_MARK:
16000 size = 0;
16001 break;
16002 case ENUMERAL_TYPE:
16003 case RECORD_TYPE:
16004 case UNION_TYPE:
16005 case QUAL_UNION_TYPE:
16006 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
16007 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
16009 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
16010 return;
16012 size = int_size_in_bytes (tree_node);
16013 break;
16014 case FIELD_DECL:
16015 /* For a data member of a struct or union, the DW_AT_byte_size is
16016 generally given as the number of bytes normally allocated for an
16017 object of the *declared* type of the member itself. This is true
16018 even for bit-fields. */
16019 size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
16020 break;
16021 default:
16022 gcc_unreachable ();
16025 /* Note that `size' might be -1 when we get to this point. If it is, that
16026 indicates that the byte size of the entity in question is variable. We
16027 have no good way of expressing this fact in Dwarf at the present time,
16028 so just let the -1 pass on through. */
16029 add_AT_unsigned (die, DW_AT_byte_size, size);
16032 /* For a FIELD_DECL node which represents a bit-field, output an attribute
16033 which specifies the distance in bits from the highest order bit of the
16034 "containing object" for the bit-field to the highest order bit of the
16035 bit-field itself.
16037 For any given bit-field, the "containing object" is a hypothetical object
16038 (of some integral or enum type) within which the given bit-field lives. The
16039 type of this hypothetical "containing object" is always the same as the
16040 declared type of the individual bit-field itself. The determination of the
16041 exact location of the "containing object" for a bit-field is rather
16042 complicated. It's handled by the `field_byte_offset' function (above).
16044 Note that it is the size (in bytes) of the hypothetical "containing object"
16045 which will be given in the DW_AT_byte_size attribute for this bit-field.
16046 (See `byte_size_attribute' above). */
16048 static inline void
16049 add_bit_offset_attribute (dw_die_ref die, tree decl)
16051 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
16052 tree type = DECL_BIT_FIELD_TYPE (decl);
16053 HOST_WIDE_INT bitpos_int;
16054 HOST_WIDE_INT highest_order_object_bit_offset;
16055 HOST_WIDE_INT highest_order_field_bit_offset;
16056 HOST_WIDE_INT bit_offset;
16058 /* Must be a field and a bit field. */
16059 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
16061 /* We can't yet handle bit-fields whose offsets are variable, so if we
16062 encounter such things, just return without generating any attribute
16063 whatsoever. Likewise for variable or too large size. */
16064 if (! host_integerp (bit_position (decl), 0)
16065 || ! host_integerp (DECL_SIZE (decl), 1))
16066 return;
16068 bitpos_int = int_bit_position (decl);
16070 /* Note that the bit offset is always the distance (in bits) from the
16071 highest-order bit of the "containing object" to the highest-order bit of
16072 the bit-field itself. Since the "high-order end" of any object or field
16073 is different on big-endian and little-endian machines, the computation
16074 below must take account of these differences. */
16075 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
16076 highest_order_field_bit_offset = bitpos_int;
16078 if (! BYTES_BIG_ENDIAN)
16080 highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
16081 highest_order_object_bit_offset += simple_type_size_in_bits (type);
16084 bit_offset
16085 = (! BYTES_BIG_ENDIAN
16086 ? highest_order_object_bit_offset - highest_order_field_bit_offset
16087 : highest_order_field_bit_offset - highest_order_object_bit_offset);
16089 if (bit_offset < 0)
16090 add_AT_int (die, DW_AT_bit_offset, bit_offset);
16091 else
16092 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
16095 /* For a FIELD_DECL node which represents a bit field, output an attribute
16096 which specifies the length in bits of the given field. */
16098 static inline void
16099 add_bit_size_attribute (dw_die_ref die, tree decl)
16101 /* Must be a field and a bit field. */
16102 gcc_assert (TREE_CODE (decl) == FIELD_DECL
16103 && DECL_BIT_FIELD_TYPE (decl));
16105 if (host_integerp (DECL_SIZE (decl), 1))
16106 add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
16109 /* If the compiled language is ANSI C, then add a 'prototyped'
16110 attribute, if arg types are given for the parameters of a function. */
16112 static inline void
16113 add_prototyped_attribute (dw_die_ref die, tree func_type)
16115 if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
16116 && prototype_p (func_type))
16117 add_AT_flag (die, DW_AT_prototyped, 1);
16120 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
16121 by looking in either the type declaration or object declaration
16122 equate table. */
16124 static inline dw_die_ref
16125 add_abstract_origin_attribute (dw_die_ref die, tree origin)
16127 dw_die_ref origin_die = NULL;
16129 if (TREE_CODE (origin) != FUNCTION_DECL)
16131 /* We may have gotten separated from the block for the inlined
16132 function, if we're in an exception handler or some such; make
16133 sure that the abstract function has been written out.
16135 Doing this for nested functions is wrong, however; functions are
16136 distinct units, and our context might not even be inline. */
16137 tree fn = origin;
16139 if (TYPE_P (fn))
16140 fn = TYPE_STUB_DECL (fn);
16142 fn = decl_function_context (fn);
16143 if (fn)
16144 dwarf2out_abstract_function (fn);
16147 if (DECL_P (origin))
16148 origin_die = lookup_decl_die (origin);
16149 else if (TYPE_P (origin))
16150 origin_die = lookup_type_die (origin);
16152 /* XXX: Functions that are never lowered don't always have correct block
16153 trees (in the case of java, they simply have no block tree, in some other
16154 languages). For these functions, there is nothing we can really do to
16155 output correct debug info for inlined functions in all cases. Rather
16156 than die, we'll just produce deficient debug info now, in that we will
16157 have variables without a proper abstract origin. In the future, when all
16158 functions are lowered, we should re-add a gcc_assert (origin_die)
16159 here. */
16161 if (origin_die)
16162 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
16163 return origin_die;
16166 /* We do not currently support the pure_virtual attribute. */
16168 static inline void
16169 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
16171 if (DECL_VINDEX (func_decl))
16173 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
16175 if (host_integerp (DECL_VINDEX (func_decl), 0))
16176 add_AT_loc (die, DW_AT_vtable_elem_location,
16177 new_loc_descr (DW_OP_constu,
16178 tree_low_cst (DECL_VINDEX (func_decl), 0),
16179 0));
16181 /* GNU extension: Record what type this method came from originally. */
16182 if (debug_info_level > DINFO_LEVEL_TERSE
16183 && DECL_CONTEXT (func_decl))
16184 add_AT_die_ref (die, DW_AT_containing_type,
16185 lookup_type_die (DECL_CONTEXT (func_decl)));
16189 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
16190 given decl. This used to be a vendor extension until after DWARF 4
16191 standardized it. */
16193 static void
16194 add_linkage_attr (dw_die_ref die, tree decl)
16196 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
16198 /* Mimic what assemble_name_raw does with a leading '*'. */
16199 if (name[0] == '*')
16200 name = &name[1];
16202 if (dwarf_version >= 4)
16203 add_AT_string (die, DW_AT_linkage_name, name);
16204 else
16205 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
16208 /* Add source coordinate attributes for the given decl. */
16210 static void
16211 add_src_coords_attributes (dw_die_ref die, tree decl)
16213 expanded_location s;
16215 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
16216 return;
16217 s = expand_location (DECL_SOURCE_LOCATION (decl));
16218 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
16219 add_AT_unsigned (die, DW_AT_decl_line, s.line);
16222 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
16224 static void
16225 add_linkage_name (dw_die_ref die, tree decl)
16227 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
16228 && TREE_PUBLIC (decl)
16229 && !DECL_ABSTRACT (decl)
16230 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
16231 && die->die_tag != DW_TAG_member)
16233 /* Defer until we have an assembler name set. */
16234 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
16236 limbo_die_node *asm_name;
16238 asm_name = ggc_alloc_cleared_limbo_die_node ();
16239 asm_name->die = die;
16240 asm_name->created_for = decl;
16241 asm_name->next = deferred_asm_name;
16242 deferred_asm_name = asm_name;
16244 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
16245 add_linkage_attr (die, decl);
16249 /* Add a DW_AT_name attribute and source coordinate attribute for the
16250 given decl, but only if it actually has a name. */
16252 static void
16253 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
16255 tree decl_name;
16257 decl_name = DECL_NAME (decl);
16258 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
16260 const char *name = dwarf2_name (decl, 0);
16261 if (name)
16262 add_name_attribute (die, name);
16263 if (! DECL_ARTIFICIAL (decl))
16264 add_src_coords_attributes (die, decl);
16266 add_linkage_name (die, decl);
16269 #ifdef VMS_DEBUGGING_INFO
16270 /* Get the function's name, as described by its RTL. This may be different
16271 from the DECL_NAME name used in the source file. */
16272 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
16274 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
16275 XEXP (DECL_RTL (decl), 0), false);
16276 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
16278 #endif /* VMS_DEBUGGING_INFO */
16281 #ifdef VMS_DEBUGGING_INFO
16282 /* Output the debug main pointer die for VMS */
16284 void
16285 dwarf2out_vms_debug_main_pointer (void)
16287 char label[MAX_ARTIFICIAL_LABEL_BYTES];
16288 dw_die_ref die;
16290 /* Allocate the VMS debug main subprogram die. */
16291 die = ggc_alloc_cleared_die_node ();
16292 die->die_tag = DW_TAG_subprogram;
16293 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
16294 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
16295 current_function_funcdef_no);
16296 add_AT_lbl_id (die, DW_AT_entry_pc, label);
16298 /* Make it the first child of comp_unit_die (). */
16299 die->die_parent = comp_unit_die ();
16300 if (comp_unit_die ()->die_child)
16302 die->die_sib = comp_unit_die ()->die_child->die_sib;
16303 comp_unit_die ()->die_child->die_sib = die;
16305 else
16307 die->die_sib = die;
16308 comp_unit_die ()->die_child = die;
16311 #endif /* VMS_DEBUGGING_INFO */
16313 /* Push a new declaration scope. */
16315 static void
16316 push_decl_scope (tree scope)
16318 vec_safe_push (decl_scope_table, scope);
16321 /* Pop a declaration scope. */
16323 static inline void
16324 pop_decl_scope (void)
16326 decl_scope_table->pop ();
16329 /* walk_tree helper function for uses_local_type, below. */
16331 static tree
16332 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
16334 if (!TYPE_P (*tp))
16335 *walk_subtrees = 0;
16336 else
16338 tree name = TYPE_NAME (*tp);
16339 if (name && DECL_P (name) && decl_function_context (name))
16340 return *tp;
16342 return NULL_TREE;
16345 /* If TYPE involves a function-local type (including a local typedef to a
16346 non-local type), returns that type; otherwise returns NULL_TREE. */
16348 static tree
16349 uses_local_type (tree type)
16351 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
16352 return used;
16355 /* Return the DIE for the scope that immediately contains this type.
16356 Non-named types that do not involve a function-local type get global
16357 scope. Named types nested in namespaces or other types get their
16358 containing scope. All other types (i.e. function-local named types) get
16359 the current active scope. */
16361 static dw_die_ref
16362 scope_die_for (tree t, dw_die_ref context_die)
16364 dw_die_ref scope_die = NULL;
16365 tree containing_scope;
16367 /* Non-types always go in the current scope. */
16368 gcc_assert (TYPE_P (t));
16370 /* Use the scope of the typedef, rather than the scope of the type
16371 it refers to. */
16372 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
16373 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
16374 else
16375 containing_scope = TYPE_CONTEXT (t);
16377 /* Use the containing namespace if there is one. */
16378 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
16380 if (context_die == lookup_decl_die (containing_scope))
16381 /* OK */;
16382 else if (debug_info_level > DINFO_LEVEL_TERSE)
16383 context_die = get_context_die (containing_scope);
16384 else
16385 containing_scope = NULL_TREE;
16388 /* Ignore function type "scopes" from the C frontend. They mean that
16389 a tagged type is local to a parmlist of a function declarator, but
16390 that isn't useful to DWARF. */
16391 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
16392 containing_scope = NULL_TREE;
16394 if (SCOPE_FILE_SCOPE_P (containing_scope))
16396 /* If T uses a local type keep it local as well, to avoid references
16397 to function-local DIEs from outside the function. */
16398 if (current_function_decl && uses_local_type (t))
16399 scope_die = context_die;
16400 else
16401 scope_die = comp_unit_die ();
16403 else if (TYPE_P (containing_scope))
16405 /* For types, we can just look up the appropriate DIE. */
16406 if (debug_info_level > DINFO_LEVEL_TERSE)
16407 scope_die = get_context_die (containing_scope);
16408 else
16410 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
16411 if (scope_die == NULL)
16412 scope_die = comp_unit_die ();
16415 else
16416 scope_die = context_die;
16418 return scope_die;
16421 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
16423 static inline int
16424 local_scope_p (dw_die_ref context_die)
16426 for (; context_die; context_die = context_die->die_parent)
16427 if (context_die->die_tag == DW_TAG_inlined_subroutine
16428 || context_die->die_tag == DW_TAG_subprogram)
16429 return 1;
16431 return 0;
16434 /* Returns nonzero if CONTEXT_DIE is a class. */
16436 static inline int
16437 class_scope_p (dw_die_ref context_die)
16439 return (context_die
16440 && (context_die->die_tag == DW_TAG_structure_type
16441 || context_die->die_tag == DW_TAG_class_type
16442 || context_die->die_tag == DW_TAG_interface_type
16443 || context_die->die_tag == DW_TAG_union_type));
16446 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
16447 whether or not to treat a DIE in this context as a declaration. */
16449 static inline int
16450 class_or_namespace_scope_p (dw_die_ref context_die)
16452 return (class_scope_p (context_die)
16453 || (context_die && context_die->die_tag == DW_TAG_namespace));
16456 /* Many forms of DIEs require a "type description" attribute. This
16457 routine locates the proper "type descriptor" die for the type given
16458 by 'type', and adds a DW_AT_type attribute below the given die. */
16460 static void
16461 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
16462 int decl_volatile, dw_die_ref context_die)
16464 enum tree_code code = TREE_CODE (type);
16465 dw_die_ref type_die = NULL;
16467 /* ??? If this type is an unnamed subrange type of an integral, floating-point
16468 or fixed-point type, use the inner type. This is because we have no
16469 support for unnamed types in base_type_die. This can happen if this is
16470 an Ada subrange type. Correct solution is emit a subrange type die. */
16471 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
16472 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
16473 type = TREE_TYPE (type), code = TREE_CODE (type);
16475 if (code == ERROR_MARK
16476 /* Handle a special case. For functions whose return type is void, we
16477 generate *no* type attribute. (Note that no object may have type
16478 `void', so this only applies to function return types). */
16479 || code == VOID_TYPE)
16480 return;
16482 type_die = modified_type_die (type,
16483 decl_const || TYPE_READONLY (type),
16484 decl_volatile || TYPE_VOLATILE (type),
16485 context_die);
16487 if (type_die != NULL)
16488 add_AT_die_ref (object_die, DW_AT_type, type_die);
16491 /* Given an object die, add the calling convention attribute for the
16492 function call type. */
16493 static void
16494 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
16496 enum dwarf_calling_convention value = DW_CC_normal;
16498 value = ((enum dwarf_calling_convention)
16499 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
16501 if (is_fortran ()
16502 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
16504 /* DWARF 2 doesn't provide a way to identify a program's source-level
16505 entry point. DW_AT_calling_convention attributes are only meant
16506 to describe functions' calling conventions. However, lacking a
16507 better way to signal the Fortran main program, we used this for
16508 a long time, following existing custom. Now, DWARF 4 has
16509 DW_AT_main_subprogram, which we add below, but some tools still
16510 rely on the old way, which we thus keep. */
16511 value = DW_CC_program;
16513 if (dwarf_version >= 4 || !dwarf_strict)
16514 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
16517 /* Only add the attribute if the backend requests it, and
16518 is not DW_CC_normal. */
16519 if (value && (value != DW_CC_normal))
16520 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
16523 /* Given a tree pointer to a struct, class, union, or enum type node, return
16524 a pointer to the (string) tag name for the given type, or zero if the type
16525 was declared without a tag. */
16527 static const char *
16528 type_tag (const_tree type)
16530 const char *name = 0;
16532 if (TYPE_NAME (type) != 0)
16534 tree t = 0;
16536 /* Find the IDENTIFIER_NODE for the type name. */
16537 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
16538 && !TYPE_NAMELESS (type))
16539 t = TYPE_NAME (type);
16541 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
16542 a TYPE_DECL node, regardless of whether or not a `typedef' was
16543 involved. */
16544 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
16545 && ! DECL_IGNORED_P (TYPE_NAME (type)))
16547 /* We want to be extra verbose. Don't call dwarf_name if
16548 DECL_NAME isn't set. The default hook for decl_printable_name
16549 doesn't like that, and in this context it's correct to return
16550 0, instead of "<anonymous>" or the like. */
16551 if (DECL_NAME (TYPE_NAME (type))
16552 && !DECL_NAMELESS (TYPE_NAME (type)))
16553 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
16556 /* Now get the name as a string, or invent one. */
16557 if (!name && t != 0)
16558 name = IDENTIFIER_POINTER (t);
16561 return (name == 0 || *name == '\0') ? 0 : name;
16564 /* Return the type associated with a data member, make a special check
16565 for bit field types. */
16567 static inline tree
16568 member_declared_type (const_tree member)
16570 return (DECL_BIT_FIELD_TYPE (member)
16571 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
16574 /* Get the decl's label, as described by its RTL. This may be different
16575 from the DECL_NAME name used in the source file. */
16577 #if 0
16578 static const char *
16579 decl_start_label (tree decl)
16581 rtx x;
16582 const char *fnname;
16584 x = DECL_RTL (decl);
16585 gcc_assert (MEM_P (x));
16587 x = XEXP (x, 0);
16588 gcc_assert (GET_CODE (x) == SYMBOL_REF);
16590 fnname = XSTR (x, 0);
16591 return fnname;
16593 #endif
16595 /* These routines generate the internal representation of the DIE's for
16596 the compilation unit. Debugging information is collected by walking
16597 the declaration trees passed in from dwarf2out_decl(). */
16599 static void
16600 gen_array_type_die (tree type, dw_die_ref context_die)
16602 dw_die_ref scope_die = scope_die_for (type, context_die);
16603 dw_die_ref array_die;
16605 /* GNU compilers represent multidimensional array types as sequences of one
16606 dimensional array types whose element types are themselves array types.
16607 We sometimes squish that down to a single array_type DIE with multiple
16608 subscripts in the Dwarf debugging info. The draft Dwarf specification
16609 say that we are allowed to do this kind of compression in C, because
16610 there is no difference between an array of arrays and a multidimensional
16611 array. We don't do this for Ada to remain as close as possible to the
16612 actual representation, which is especially important against the language
16613 flexibilty wrt arrays of variable size. */
16615 bool collapse_nested_arrays = !is_ada ();
16616 tree element_type;
16618 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
16619 DW_TAG_string_type doesn't have DW_AT_type attribute). */
16620 if (TYPE_STRING_FLAG (type)
16621 && TREE_CODE (type) == ARRAY_TYPE
16622 && is_fortran ()
16623 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
16625 HOST_WIDE_INT size;
16627 array_die = new_die (DW_TAG_string_type, scope_die, type);
16628 add_name_attribute (array_die, type_tag (type));
16629 equate_type_number_to_die (type, array_die);
16630 size = int_size_in_bytes (type);
16631 if (size >= 0)
16632 add_AT_unsigned (array_die, DW_AT_byte_size, size);
16633 else if (TYPE_DOMAIN (type) != NULL_TREE
16634 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
16635 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
16637 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
16638 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
16640 size = int_size_in_bytes (TREE_TYPE (szdecl));
16641 if (loc && size > 0)
16643 add_AT_location_description (array_die, DW_AT_string_length, loc);
16644 if (size != DWARF2_ADDR_SIZE)
16645 add_AT_unsigned (array_die, DW_AT_byte_size, size);
16648 return;
16651 array_die = new_die (DW_TAG_array_type, scope_die, type);
16652 add_name_attribute (array_die, type_tag (type));
16653 equate_type_number_to_die (type, array_die);
16655 if (TREE_CODE (type) == VECTOR_TYPE)
16656 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
16658 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
16659 if (is_fortran ()
16660 && TREE_CODE (type) == ARRAY_TYPE
16661 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
16662 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
16663 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16665 #if 0
16666 /* We default the array ordering. SDB will probably do
16667 the right things even if DW_AT_ordering is not present. It's not even
16668 an issue until we start to get into multidimensional arrays anyway. If
16669 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
16670 then we'll have to put the DW_AT_ordering attribute back in. (But if
16671 and when we find out that we need to put these in, we will only do so
16672 for multidimensional arrays. */
16673 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
16674 #endif
16676 if (TREE_CODE (type) == VECTOR_TYPE)
16678 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
16679 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
16680 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
16681 add_bound_info (subrange_die, DW_AT_upper_bound,
16682 size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
16684 else
16685 add_subscript_info (array_die, type, collapse_nested_arrays);
16687 /* Add representation of the type of the elements of this array type and
16688 emit the corresponding DIE if we haven't done it already. */
16689 element_type = TREE_TYPE (type);
16690 if (collapse_nested_arrays)
16691 while (TREE_CODE (element_type) == ARRAY_TYPE)
16693 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
16694 break;
16695 element_type = TREE_TYPE (element_type);
16698 add_type_attribute (array_die, element_type, 0, 0, context_die);
16700 add_gnat_descriptive_type_attribute (array_die, type, context_die);
16701 if (TYPE_ARTIFICIAL (type))
16702 add_AT_flag (array_die, DW_AT_artificial, 1);
16704 if (get_AT (array_die, DW_AT_name))
16705 add_pubtype (type, array_die);
16708 static dw_loc_descr_ref
16709 descr_info_loc (tree val, tree base_decl)
16711 HOST_WIDE_INT size;
16712 dw_loc_descr_ref loc, loc2;
16713 enum dwarf_location_atom op;
16715 if (val == base_decl)
16716 return new_loc_descr (DW_OP_push_object_address, 0, 0);
16718 switch (TREE_CODE (val))
16720 CASE_CONVERT:
16721 return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16722 case VAR_DECL:
16723 return loc_descriptor_from_tree (val, 0);
16724 case INTEGER_CST:
16725 if (host_integerp (val, 0))
16726 return int_loc_descriptor (tree_low_cst (val, 0));
16727 break;
16728 case INDIRECT_REF:
16729 size = int_size_in_bytes (TREE_TYPE (val));
16730 if (size < 0)
16731 break;
16732 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16733 if (!loc)
16734 break;
16735 if (size == DWARF2_ADDR_SIZE)
16736 add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
16737 else
16738 add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
16739 return loc;
16740 case POINTER_PLUS_EXPR:
16741 case PLUS_EXPR:
16742 if (host_integerp (TREE_OPERAND (val, 1), 1)
16743 && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
16744 < 16384)
16746 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16747 if (!loc)
16748 break;
16749 loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
16751 else
16753 op = DW_OP_plus;
16754 do_binop:
16755 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16756 if (!loc)
16757 break;
16758 loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
16759 if (!loc2)
16760 break;
16761 add_loc_descr (&loc, loc2);
16762 add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
16764 return loc;
16765 case MINUS_EXPR:
16766 op = DW_OP_minus;
16767 goto do_binop;
16768 case MULT_EXPR:
16769 op = DW_OP_mul;
16770 goto do_binop;
16771 case EQ_EXPR:
16772 op = DW_OP_eq;
16773 goto do_binop;
16774 case NE_EXPR:
16775 op = DW_OP_ne;
16776 goto do_binop;
16777 default:
16778 break;
16780 return NULL;
16783 static void
16784 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
16785 tree val, tree base_decl)
16787 dw_loc_descr_ref loc;
16789 if (host_integerp (val, 0))
16791 add_AT_unsigned (die, attr, tree_low_cst (val, 0));
16792 return;
16795 loc = descr_info_loc (val, base_decl);
16796 if (!loc)
16797 return;
16799 add_AT_loc (die, attr, loc);
16802 /* This routine generates DIE for array with hidden descriptor, details
16803 are filled into *info by a langhook. */
16805 static void
16806 gen_descr_array_type_die (tree type, struct array_descr_info *info,
16807 dw_die_ref context_die)
16809 dw_die_ref scope_die = scope_die_for (type, context_die);
16810 dw_die_ref array_die;
16811 int dim;
16813 array_die = new_die (DW_TAG_array_type, scope_die, type);
16814 add_name_attribute (array_die, type_tag (type));
16815 equate_type_number_to_die (type, array_die);
16817 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
16818 if (is_fortran ()
16819 && info->ndimensions >= 2)
16820 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16822 if (info->data_location)
16823 add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
16824 info->base_decl);
16825 if (info->associated)
16826 add_descr_info_field (array_die, DW_AT_associated, info->associated,
16827 info->base_decl);
16828 if (info->allocated)
16829 add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
16830 info->base_decl);
16832 for (dim = 0; dim < info->ndimensions; dim++)
16834 dw_die_ref subrange_die
16835 = new_die (DW_TAG_subrange_type, array_die, NULL);
16837 if (info->dimen[dim].lower_bound)
16839 /* If it is the default value, omit it. */
16840 int dflt;
16842 if (host_integerp (info->dimen[dim].lower_bound, 0)
16843 && (dflt = lower_bound_default ()) != -1
16844 && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
16846 else
16847 add_descr_info_field (subrange_die, DW_AT_lower_bound,
16848 info->dimen[dim].lower_bound,
16849 info->base_decl);
16851 if (info->dimen[dim].upper_bound)
16852 add_descr_info_field (subrange_die, DW_AT_upper_bound,
16853 info->dimen[dim].upper_bound,
16854 info->base_decl);
16855 if (info->dimen[dim].stride)
16856 add_descr_info_field (subrange_die, DW_AT_byte_stride,
16857 info->dimen[dim].stride,
16858 info->base_decl);
16861 gen_type_die (info->element_type, context_die);
16862 add_type_attribute (array_die, info->element_type, 0, 0, context_die);
16864 if (get_AT (array_die, DW_AT_name))
16865 add_pubtype (type, array_die);
16868 #if 0
16869 static void
16870 gen_entry_point_die (tree decl, dw_die_ref context_die)
16872 tree origin = decl_ultimate_origin (decl);
16873 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
16875 if (origin != NULL)
16876 add_abstract_origin_attribute (decl_die, origin);
16877 else
16879 add_name_and_src_coords_attributes (decl_die, decl);
16880 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
16881 0, 0, context_die);
16884 if (DECL_ABSTRACT (decl))
16885 equate_decl_number_to_die (decl, decl_die);
16886 else
16887 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
16889 #endif
16891 /* Walk through the list of incomplete types again, trying once more to
16892 emit full debugging info for them. */
16894 static void
16895 retry_incomplete_types (void)
16897 int i;
16899 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
16900 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
16901 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
16904 /* Determine what tag to use for a record type. */
16906 static enum dwarf_tag
16907 record_type_tag (tree type)
16909 if (! lang_hooks.types.classify_record)
16910 return DW_TAG_structure_type;
16912 switch (lang_hooks.types.classify_record (type))
16914 case RECORD_IS_STRUCT:
16915 return DW_TAG_structure_type;
16917 case RECORD_IS_CLASS:
16918 return DW_TAG_class_type;
16920 case RECORD_IS_INTERFACE:
16921 if (dwarf_version >= 3 || !dwarf_strict)
16922 return DW_TAG_interface_type;
16923 return DW_TAG_structure_type;
16925 default:
16926 gcc_unreachable ();
16930 /* Generate a DIE to represent an enumeration type. Note that these DIEs
16931 include all of the information about the enumeration values also. Each
16932 enumerated type name/value is listed as a child of the enumerated type
16933 DIE. */
16935 static dw_die_ref
16936 gen_enumeration_type_die (tree type, dw_die_ref context_die)
16938 dw_die_ref type_die = lookup_type_die (type);
16940 if (type_die == NULL)
16942 type_die = new_die (DW_TAG_enumeration_type,
16943 scope_die_for (type, context_die), type);
16944 equate_type_number_to_die (type, type_die);
16945 add_name_attribute (type_die, type_tag (type));
16946 if (dwarf_version >= 4 || !dwarf_strict)
16948 if (ENUM_IS_SCOPED (type))
16949 add_AT_flag (type_die, DW_AT_enum_class, 1);
16950 if (ENUM_IS_OPAQUE (type))
16951 add_AT_flag (type_die, DW_AT_declaration, 1);
16954 else if (! TYPE_SIZE (type))
16955 return type_die;
16956 else
16957 remove_AT (type_die, DW_AT_declaration);
16959 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
16960 given enum type is incomplete, do not generate the DW_AT_byte_size
16961 attribute or the DW_AT_element_list attribute. */
16962 if (TYPE_SIZE (type))
16964 tree link;
16966 TREE_ASM_WRITTEN (type) = 1;
16967 add_byte_size_attribute (type_die, type);
16968 if (TYPE_STUB_DECL (type) != NULL_TREE)
16970 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
16971 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
16974 /* If the first reference to this type was as the return type of an
16975 inline function, then it may not have a parent. Fix this now. */
16976 if (type_die->die_parent == NULL)
16977 add_child_die (scope_die_for (type, context_die), type_die);
16979 for (link = TYPE_VALUES (type);
16980 link != NULL; link = TREE_CHAIN (link))
16982 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
16983 tree value = TREE_VALUE (link);
16985 add_name_attribute (enum_die,
16986 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
16988 if (TREE_CODE (value) == CONST_DECL)
16989 value = DECL_INITIAL (value);
16991 if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
16992 /* DWARF2 does not provide a way of indicating whether or
16993 not enumeration constants are signed or unsigned. GDB
16994 always assumes the values are signed, so we output all
16995 values as if they were signed. That means that
16996 enumeration constants with very large unsigned values
16997 will appear to have negative values in the debugger. */
16998 add_AT_int (enum_die, DW_AT_const_value,
16999 tree_low_cst (value, tree_int_cst_sgn (value) > 0));
17002 add_gnat_descriptive_type_attribute (type_die, type, context_die);
17003 if (TYPE_ARTIFICIAL (type))
17004 add_AT_flag (type_die, DW_AT_artificial, 1);
17006 else
17007 add_AT_flag (type_die, DW_AT_declaration, 1);
17009 add_pubtype (type, type_die);
17011 return type_die;
17014 /* Generate a DIE to represent either a real live formal parameter decl or to
17015 represent just the type of some formal parameter position in some function
17016 type.
17018 Note that this routine is a bit unusual because its argument may be a
17019 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17020 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17021 node. If it's the former then this function is being called to output a
17022 DIE to represent a formal parameter object (or some inlining thereof). If
17023 it's the latter, then this function is only being called to output a
17024 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17025 argument type of some subprogram type.
17026 If EMIT_NAME_P is true, name and source coordinate attributes
17027 are emitted. */
17029 static dw_die_ref
17030 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17031 dw_die_ref context_die)
17033 tree node_or_origin = node ? node : origin;
17034 tree ultimate_origin;
17035 dw_die_ref parm_die
17036 = new_die (DW_TAG_formal_parameter, context_die, node);
17038 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
17040 case tcc_declaration:
17041 ultimate_origin = decl_ultimate_origin (node_or_origin);
17042 if (node || ultimate_origin)
17043 origin = ultimate_origin;
17044 if (origin != NULL)
17045 add_abstract_origin_attribute (parm_die, origin);
17046 else if (emit_name_p)
17047 add_name_and_src_coords_attributes (parm_die, node);
17048 if (origin == NULL
17049 || (! DECL_ABSTRACT (node_or_origin)
17050 && variably_modified_type_p (TREE_TYPE (node_or_origin),
17051 decl_function_context
17052 (node_or_origin))))
17054 tree type = TREE_TYPE (node_or_origin);
17055 if (decl_by_reference_p (node_or_origin))
17056 add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
17057 context_die);
17058 else
17059 add_type_attribute (parm_die, type,
17060 TREE_READONLY (node_or_origin),
17061 TREE_THIS_VOLATILE (node_or_origin),
17062 context_die);
17064 if (origin == NULL && DECL_ARTIFICIAL (node))
17065 add_AT_flag (parm_die, DW_AT_artificial, 1);
17067 if (node && node != origin)
17068 equate_decl_number_to_die (node, parm_die);
17069 if (! DECL_ABSTRACT (node_or_origin))
17070 add_location_or_const_value_attribute (parm_die, node_or_origin,
17071 node == NULL, DW_AT_location);
17073 break;
17075 case tcc_type:
17076 /* We were called with some kind of a ..._TYPE node. */
17077 add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
17078 break;
17080 default:
17081 gcc_unreachable ();
17084 return parm_die;
17087 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
17088 children DW_TAG_formal_parameter DIEs representing the arguments of the
17089 parameter pack.
17091 PARM_PACK must be a function parameter pack.
17092 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
17093 must point to the subsequent arguments of the function PACK_ARG belongs to.
17094 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
17095 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
17096 following the last one for which a DIE was generated. */
17098 static dw_die_ref
17099 gen_formal_parameter_pack_die (tree parm_pack,
17100 tree pack_arg,
17101 dw_die_ref subr_die,
17102 tree *next_arg)
17104 tree arg;
17105 dw_die_ref parm_pack_die;
17107 gcc_assert (parm_pack
17108 && lang_hooks.function_parameter_pack_p (parm_pack)
17109 && subr_die);
17111 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
17112 add_src_coords_attributes (parm_pack_die, parm_pack);
17114 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
17116 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
17117 parm_pack))
17118 break;
17119 gen_formal_parameter_die (arg, NULL,
17120 false /* Don't emit name attribute. */,
17121 parm_pack_die);
17123 if (next_arg)
17124 *next_arg = arg;
17125 return parm_pack_die;
17128 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
17129 at the end of an (ANSI prototyped) formal parameters list. */
17131 static void
17132 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
17134 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
17137 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
17138 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
17139 parameters as specified in some function type specification (except for
17140 those which appear as part of a function *definition*). */
17142 static void
17143 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
17145 tree link;
17146 tree formal_type = NULL;
17147 tree first_parm_type;
17148 tree arg;
17150 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
17152 arg = DECL_ARGUMENTS (function_or_method_type);
17153 function_or_method_type = TREE_TYPE (function_or_method_type);
17155 else
17156 arg = NULL_TREE;
17158 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
17160 /* Make our first pass over the list of formal parameter types and output a
17161 DW_TAG_formal_parameter DIE for each one. */
17162 for (link = first_parm_type; link; )
17164 dw_die_ref parm_die;
17166 formal_type = TREE_VALUE (link);
17167 if (formal_type == void_type_node)
17168 break;
17170 /* Output a (nameless) DIE to represent the formal parameter itself. */
17171 parm_die = gen_formal_parameter_die (formal_type, NULL,
17172 true /* Emit name attribute. */,
17173 context_die);
17174 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
17175 && link == first_parm_type)
17177 add_AT_flag (parm_die, DW_AT_artificial, 1);
17178 if (dwarf_version >= 3 || !dwarf_strict)
17179 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
17181 else if (arg && DECL_ARTIFICIAL (arg))
17182 add_AT_flag (parm_die, DW_AT_artificial, 1);
17184 link = TREE_CHAIN (link);
17185 if (arg)
17186 arg = DECL_CHAIN (arg);
17189 /* If this function type has an ellipsis, add a
17190 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
17191 if (formal_type != void_type_node)
17192 gen_unspecified_parameters_die (function_or_method_type, context_die);
17194 /* Make our second (and final) pass over the list of formal parameter types
17195 and output DIEs to represent those types (as necessary). */
17196 for (link = TYPE_ARG_TYPES (function_or_method_type);
17197 link && TREE_VALUE (link);
17198 link = TREE_CHAIN (link))
17199 gen_type_die (TREE_VALUE (link), context_die);
17202 /* We want to generate the DIE for TYPE so that we can generate the
17203 die for MEMBER, which has been defined; we will need to refer back
17204 to the member declaration nested within TYPE. If we're trying to
17205 generate minimal debug info for TYPE, processing TYPE won't do the
17206 trick; we need to attach the member declaration by hand. */
17208 static void
17209 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
17211 gen_type_die (type, context_die);
17213 /* If we're trying to avoid duplicate debug info, we may not have
17214 emitted the member decl for this function. Emit it now. */
17215 if (TYPE_STUB_DECL (type)
17216 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
17217 && ! lookup_decl_die (member))
17219 dw_die_ref type_die;
17220 gcc_assert (!decl_ultimate_origin (member));
17222 push_decl_scope (type);
17223 type_die = lookup_type_die_strip_naming_typedef (type);
17224 if (TREE_CODE (member) == FUNCTION_DECL)
17225 gen_subprogram_die (member, type_die);
17226 else if (TREE_CODE (member) == FIELD_DECL)
17228 /* Ignore the nameless fields that are used to skip bits but handle
17229 C++ anonymous unions and structs. */
17230 if (DECL_NAME (member) != NULL_TREE
17231 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
17232 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
17234 gen_type_die (member_declared_type (member), type_die);
17235 gen_field_die (member, type_die);
17238 else
17239 gen_variable_die (member, NULL_TREE, type_die);
17241 pop_decl_scope ();
17245 /* Forward declare these functions, because they are mutually recursive
17246 with their set_block_* pairing functions. */
17247 static void set_decl_origin_self (tree);
17248 static void set_decl_abstract_flags (tree, int);
17250 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
17251 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
17252 that it points to the node itself, thus indicating that the node is its
17253 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
17254 the given node is NULL, recursively descend the decl/block tree which
17255 it is the root of, and for each other ..._DECL or BLOCK node contained
17256 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
17257 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
17258 values to point to themselves. */
17260 static void
17261 set_block_origin_self (tree stmt)
17263 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
17265 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
17268 tree local_decl;
17270 for (local_decl = BLOCK_VARS (stmt);
17271 local_decl != NULL_TREE;
17272 local_decl = DECL_CHAIN (local_decl))
17273 if (! DECL_EXTERNAL (local_decl))
17274 set_decl_origin_self (local_decl); /* Potential recursion. */
17278 tree subblock;
17280 for (subblock = BLOCK_SUBBLOCKS (stmt);
17281 subblock != NULL_TREE;
17282 subblock = BLOCK_CHAIN (subblock))
17283 set_block_origin_self (subblock); /* Recurse. */
17288 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
17289 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
17290 node to so that it points to the node itself, thus indicating that the
17291 node represents its own (abstract) origin. Additionally, if the
17292 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
17293 the decl/block tree of which the given node is the root of, and for
17294 each other ..._DECL or BLOCK node contained therein whose
17295 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
17296 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
17297 point to themselves. */
17299 static void
17300 set_decl_origin_self (tree decl)
17302 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
17304 DECL_ABSTRACT_ORIGIN (decl) = decl;
17305 if (TREE_CODE (decl) == FUNCTION_DECL)
17307 tree arg;
17309 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17310 DECL_ABSTRACT_ORIGIN (arg) = arg;
17311 if (DECL_INITIAL (decl) != NULL_TREE
17312 && DECL_INITIAL (decl) != error_mark_node)
17313 set_block_origin_self (DECL_INITIAL (decl));
17318 /* Given a pointer to some BLOCK node, and a boolean value to set the
17319 "abstract" flags to, set that value into the BLOCK_ABSTRACT flag for
17320 the given block, and for all local decls and all local sub-blocks
17321 (recursively) which are contained therein. */
17323 static void
17324 set_block_abstract_flags (tree stmt, int setting)
17326 tree local_decl;
17327 tree subblock;
17328 unsigned int i;
17330 BLOCK_ABSTRACT (stmt) = setting;
17332 for (local_decl = BLOCK_VARS (stmt);
17333 local_decl != NULL_TREE;
17334 local_decl = DECL_CHAIN (local_decl))
17335 if (! DECL_EXTERNAL (local_decl))
17336 set_decl_abstract_flags (local_decl, setting);
17338 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
17340 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
17341 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
17342 || TREE_CODE (local_decl) == PARM_DECL)
17343 set_decl_abstract_flags (local_decl, setting);
17346 for (subblock = BLOCK_SUBBLOCKS (stmt);
17347 subblock != NULL_TREE;
17348 subblock = BLOCK_CHAIN (subblock))
17349 set_block_abstract_flags (subblock, setting);
17352 /* Given a pointer to some ..._DECL node, and a boolean value to set the
17353 "abstract" flags to, set that value into the DECL_ABSTRACT flag for the
17354 given decl, and (in the case where the decl is a FUNCTION_DECL) also
17355 set the abstract flags for all of the parameters, local vars, local
17356 blocks and sub-blocks (recursively) to the same setting. */
17358 static void
17359 set_decl_abstract_flags (tree decl, int setting)
17361 DECL_ABSTRACT (decl) = setting;
17362 if (TREE_CODE (decl) == FUNCTION_DECL)
17364 tree arg;
17366 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17367 DECL_ABSTRACT (arg) = setting;
17368 if (DECL_INITIAL (decl) != NULL_TREE
17369 && DECL_INITIAL (decl) != error_mark_node)
17370 set_block_abstract_flags (DECL_INITIAL (decl), setting);
17374 /* Generate the DWARF2 info for the "abstract" instance of a function which we
17375 may later generate inlined and/or out-of-line instances of. */
17377 static void
17378 dwarf2out_abstract_function (tree decl)
17380 dw_die_ref old_die;
17381 tree save_fn;
17382 tree context;
17383 int was_abstract;
17384 htab_t old_decl_loc_table;
17385 htab_t old_cached_dw_loc_list_table;
17386 int old_call_site_count, old_tail_call_site_count;
17387 struct call_arg_loc_node *old_call_arg_locations;
17389 /* Make sure we have the actual abstract inline, not a clone. */
17390 decl = DECL_ORIGIN (decl);
17392 old_die = lookup_decl_die (decl);
17393 if (old_die && get_AT (old_die, DW_AT_inline))
17394 /* We've already generated the abstract instance. */
17395 return;
17397 /* We can be called while recursively when seeing block defining inlined subroutine
17398 DIE. Be sure to not clobber the outer location table nor use it or we would
17399 get locations in abstract instantces. */
17400 old_decl_loc_table = decl_loc_table;
17401 decl_loc_table = NULL;
17402 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
17403 cached_dw_loc_list_table = NULL;
17404 old_call_arg_locations = call_arg_locations;
17405 call_arg_locations = NULL;
17406 old_call_site_count = call_site_count;
17407 call_site_count = -1;
17408 old_tail_call_site_count = tail_call_site_count;
17409 tail_call_site_count = -1;
17411 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
17412 we don't get confused by DECL_ABSTRACT. */
17413 if (debug_info_level > DINFO_LEVEL_TERSE)
17415 context = decl_class_context (decl);
17416 if (context)
17417 gen_type_die_for_member
17418 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
17421 /* Pretend we've just finished compiling this function. */
17422 save_fn = current_function_decl;
17423 current_function_decl = decl;
17425 was_abstract = DECL_ABSTRACT (decl);
17426 set_decl_abstract_flags (decl, 1);
17427 dwarf2out_decl (decl);
17428 if (! was_abstract)
17429 set_decl_abstract_flags (decl, 0);
17431 current_function_decl = save_fn;
17432 decl_loc_table = old_decl_loc_table;
17433 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
17434 call_arg_locations = old_call_arg_locations;
17435 call_site_count = old_call_site_count;
17436 tail_call_site_count = old_tail_call_site_count;
17439 /* Helper function of premark_used_types() which gets called through
17440 htab_traverse.
17442 Marks the DIE of a given type in *SLOT as perennial, so it never gets
17443 marked as unused by prune_unused_types. */
17445 static int
17446 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
17448 tree type;
17449 dw_die_ref die;
17451 type = (tree) *slot;
17452 die = lookup_type_die (type);
17453 if (die != NULL)
17454 die->die_perennial_p = 1;
17455 return 1;
17458 /* Helper function of premark_types_used_by_global_vars which gets called
17459 through htab_traverse.
17461 Marks the DIE of a given type in *SLOT as perennial, so it never gets
17462 marked as unused by prune_unused_types. The DIE of the type is marked
17463 only if the global variable using the type will actually be emitted. */
17465 static int
17466 premark_types_used_by_global_vars_helper (void **slot,
17467 void *data ATTRIBUTE_UNUSED)
17469 struct types_used_by_vars_entry *entry;
17470 dw_die_ref die;
17472 entry = (struct types_used_by_vars_entry *) *slot;
17473 gcc_assert (entry->type != NULL
17474 && entry->var_decl != NULL);
17475 die = lookup_type_die (entry->type);
17476 if (die)
17478 /* Ask cgraph if the global variable really is to be emitted.
17479 If yes, then we'll keep the DIE of ENTRY->TYPE. */
17480 struct varpool_node *node = varpool_get_node (entry->var_decl);
17481 if (node && node->analyzed)
17483 die->die_perennial_p = 1;
17484 /* Keep the parent DIEs as well. */
17485 while ((die = die->die_parent) && die->die_perennial_p == 0)
17486 die->die_perennial_p = 1;
17489 return 1;
17492 /* Mark all members of used_types_hash as perennial. */
17494 static void
17495 premark_used_types (struct function *fun)
17497 if (fun && fun->used_types_hash)
17498 htab_traverse (fun->used_types_hash, premark_used_types_helper, NULL);
17501 /* Mark all members of types_used_by_vars_entry as perennial. */
17503 static void
17504 premark_types_used_by_global_vars (void)
17506 if (types_used_by_vars_hash)
17507 htab_traverse (types_used_by_vars_hash,
17508 premark_types_used_by_global_vars_helper, NULL);
17511 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
17512 for CA_LOC call arg loc node. */
17514 static dw_die_ref
17515 gen_call_site_die (tree decl, dw_die_ref subr_die,
17516 struct call_arg_loc_node *ca_loc)
17518 dw_die_ref stmt_die = NULL, die;
17519 tree block = ca_loc->block;
17521 while (block
17522 && block != DECL_INITIAL (decl)
17523 && TREE_CODE (block) == BLOCK)
17525 if (block_map.length () > BLOCK_NUMBER (block))
17526 stmt_die = block_map[BLOCK_NUMBER (block)];
17527 if (stmt_die)
17528 break;
17529 block = BLOCK_SUPERCONTEXT (block);
17531 if (stmt_die == NULL)
17532 stmt_die = subr_die;
17533 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
17534 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
17535 if (ca_loc->tail_call_p)
17536 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
17537 if (ca_loc->symbol_ref)
17539 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
17540 if (tdie)
17541 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
17542 else
17543 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
17545 return die;
17548 /* Generate a DIE to represent a declared function (either file-scope or
17549 block-local). */
17551 static void
17552 gen_subprogram_die (tree decl, dw_die_ref context_die)
17554 tree origin = decl_ultimate_origin (decl);
17555 dw_die_ref subr_die;
17556 tree outer_scope;
17557 dw_die_ref old_die = lookup_decl_die (decl);
17558 int declaration = (current_function_decl != decl
17559 || class_or_namespace_scope_p (context_die));
17561 premark_used_types (DECL_STRUCT_FUNCTION (decl));
17563 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
17564 started to generate the abstract instance of an inline, decided to output
17565 its containing class, and proceeded to emit the declaration of the inline
17566 from the member list for the class. If so, DECLARATION takes priority;
17567 we'll get back to the abstract instance when done with the class. */
17569 /* The class-scope declaration DIE must be the primary DIE. */
17570 if (origin && declaration && class_or_namespace_scope_p (context_die))
17572 origin = NULL;
17573 gcc_assert (!old_die);
17576 /* Now that the C++ front end lazily declares artificial member fns, we
17577 might need to retrofit the declaration into its class. */
17578 if (!declaration && !origin && !old_die
17579 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
17580 && !class_or_namespace_scope_p (context_die)
17581 && debug_info_level > DINFO_LEVEL_TERSE)
17582 old_die = force_decl_die (decl);
17584 if (origin != NULL)
17586 gcc_assert (!declaration || local_scope_p (context_die));
17588 /* Fixup die_parent for the abstract instance of a nested
17589 inline function. */
17590 if (old_die && old_die->die_parent == NULL)
17591 add_child_die (context_die, old_die);
17593 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17594 add_abstract_origin_attribute (subr_die, origin);
17595 /* This is where the actual code for a cloned function is.
17596 Let's emit linkage name attribute for it. This helps
17597 debuggers to e.g, set breakpoints into
17598 constructors/destructors when the user asks "break
17599 K::K". */
17600 add_linkage_name (subr_die, decl);
17602 else if (old_die)
17604 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17605 struct dwarf_file_data * file_index = lookup_filename (s.file);
17607 if (!get_AT_flag (old_die, DW_AT_declaration)
17608 /* We can have a normal definition following an inline one in the
17609 case of redefinition of GNU C extern inlines.
17610 It seems reasonable to use AT_specification in this case. */
17611 && !get_AT (old_die, DW_AT_inline))
17613 /* Detect and ignore this case, where we are trying to output
17614 something we have already output. */
17615 return;
17618 /* If the definition comes from the same place as the declaration,
17619 maybe use the old DIE. We always want the DIE for this function
17620 that has the *_pc attributes to be under comp_unit_die so the
17621 debugger can find it. We also need to do this for abstract
17622 instances of inlines, since the spec requires the out-of-line copy
17623 to have the same parent. For local class methods, this doesn't
17624 apply; we just use the old DIE. */
17625 if ((is_cu_die (old_die->die_parent) || context_die == NULL)
17626 && (DECL_ARTIFICIAL (decl)
17627 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
17628 && (get_AT_unsigned (old_die, DW_AT_decl_line)
17629 == (unsigned) s.line))))
17631 subr_die = old_die;
17633 /* Clear out the declaration attribute and the formal parameters.
17634 Do not remove all children, because it is possible that this
17635 declaration die was forced using force_decl_die(). In such
17636 cases die that forced declaration die (e.g. TAG_imported_module)
17637 is one of the children that we do not want to remove. */
17638 remove_AT (subr_die, DW_AT_declaration);
17639 remove_AT (subr_die, DW_AT_object_pointer);
17640 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
17642 else
17644 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17645 add_AT_specification (subr_die, old_die);
17646 add_pubname (decl, subr_die);
17647 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
17648 add_AT_file (subr_die, DW_AT_decl_file, file_index);
17649 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
17650 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
17653 else
17655 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17657 if (TREE_PUBLIC (decl))
17658 add_AT_flag (subr_die, DW_AT_external, 1);
17660 add_name_and_src_coords_attributes (subr_die, decl);
17661 add_pubname (decl, subr_die);
17662 if (debug_info_level > DINFO_LEVEL_TERSE)
17664 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
17665 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
17666 0, 0, context_die);
17669 add_pure_or_virtual_attribute (subr_die, decl);
17670 if (DECL_ARTIFICIAL (decl))
17671 add_AT_flag (subr_die, DW_AT_artificial, 1);
17673 add_accessibility_attribute (subr_die, decl);
17676 if (declaration)
17678 if (!old_die || !get_AT (old_die, DW_AT_inline))
17680 add_AT_flag (subr_die, DW_AT_declaration, 1);
17682 /* If this is an explicit function declaration then generate
17683 a DW_AT_explicit attribute. */
17684 if (lang_hooks.decls.function_decl_explicit_p (decl)
17685 && (dwarf_version >= 3 || !dwarf_strict))
17686 add_AT_flag (subr_die, DW_AT_explicit, 1);
17688 /* The first time we see a member function, it is in the context of
17689 the class to which it belongs. We make sure of this by emitting
17690 the class first. The next time is the definition, which is
17691 handled above. The two may come from the same source text.
17693 Note that force_decl_die() forces function declaration die. It is
17694 later reused to represent definition. */
17695 equate_decl_number_to_die (decl, subr_die);
17698 else if (DECL_ABSTRACT (decl))
17700 if (DECL_DECLARED_INLINE_P (decl))
17702 if (cgraph_function_possibly_inlined_p (decl))
17703 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
17704 else
17705 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
17707 else
17709 if (cgraph_function_possibly_inlined_p (decl))
17710 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
17711 else
17712 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
17715 if (DECL_DECLARED_INLINE_P (decl)
17716 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
17717 add_AT_flag (subr_die, DW_AT_artificial, 1);
17719 equate_decl_number_to_die (decl, subr_die);
17721 else if (!DECL_EXTERNAL (decl))
17723 HOST_WIDE_INT cfa_fb_offset;
17724 struct function *fun = DECL_STRUCT_FUNCTION (decl);
17726 if (!old_die || !get_AT (old_die, DW_AT_inline))
17727 equate_decl_number_to_die (decl, subr_die);
17729 gcc_checking_assert (fun);
17730 if (!flag_reorder_blocks_and_partition)
17732 dw_fde_ref fde = fun->fde;
17733 if (fde->dw_fde_begin)
17735 /* We have already generated the labels. */
17736 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
17737 fde->dw_fde_end, false);
17739 else
17741 /* Create start/end labels and add the range. */
17742 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
17743 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
17744 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
17745 current_function_funcdef_no);
17746 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
17747 current_function_funcdef_no);
17748 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
17749 false);
17752 #if VMS_DEBUGGING_INFO
17753 /* HP OpenVMS Industry Standard 64: DWARF Extensions
17754 Section 2.3 Prologue and Epilogue Attributes:
17755 When a breakpoint is set on entry to a function, it is generally
17756 desirable for execution to be suspended, not on the very first
17757 instruction of the function, but rather at a point after the
17758 function's frame has been set up, after any language defined local
17759 declaration processing has been completed, and before execution of
17760 the first statement of the function begins. Debuggers generally
17761 cannot properly determine where this point is. Similarly for a
17762 breakpoint set on exit from a function. The prologue and epilogue
17763 attributes allow a compiler to communicate the location(s) to use. */
17766 if (fde->dw_fde_vms_end_prologue)
17767 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
17768 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
17770 if (fde->dw_fde_vms_begin_epilogue)
17771 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
17772 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
17774 #endif
17777 else
17779 /* Generate pubnames entries for the split function code ranges. */
17780 dw_fde_ref fde = fun->fde;
17782 if (fde->dw_fde_second_begin)
17784 if (dwarf_version >= 3 || !dwarf_strict)
17786 /* We should use ranges for non-contiguous code section
17787 addresses. Use the actual code range for the initial
17788 section, since the HOT/COLD labels might precede an
17789 alignment offset. */
17790 bool range_list_added = false;
17791 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
17792 fde->dw_fde_end, &range_list_added,
17793 false);
17794 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
17795 fde->dw_fde_second_end,
17796 &range_list_added, false);
17797 if (range_list_added)
17798 add_ranges (NULL);
17800 else
17802 /* There is no real support in DW2 for this .. so we make
17803 a work-around. First, emit the pub name for the segment
17804 containing the function label. Then make and emit a
17805 simplified subprogram DIE for the second segment with the
17806 name pre-fixed by __hot/cold_sect_of_. We use the same
17807 linkage name for the second die so that gdb will find both
17808 sections when given "b foo". */
17809 const char *name = NULL;
17810 tree decl_name = DECL_NAME (decl);
17811 dw_die_ref seg_die;
17813 /* Do the 'primary' section. */
17814 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
17815 fde->dw_fde_end, false);
17817 /* Build a minimal DIE for the secondary section. */
17818 seg_die = new_die (DW_TAG_subprogram,
17819 subr_die->die_parent, decl);
17821 if (TREE_PUBLIC (decl))
17822 add_AT_flag (seg_die, DW_AT_external, 1);
17824 if (decl_name != NULL
17825 && IDENTIFIER_POINTER (decl_name) != NULL)
17827 name = dwarf2_name (decl, 1);
17828 if (! DECL_ARTIFICIAL (decl))
17829 add_src_coords_attributes (seg_die, decl);
17831 add_linkage_name (seg_die, decl);
17833 gcc_assert (name != NULL);
17834 add_pure_or_virtual_attribute (seg_die, decl);
17835 if (DECL_ARTIFICIAL (decl))
17836 add_AT_flag (seg_die, DW_AT_artificial, 1);
17838 name = concat ("__second_sect_of_", name, NULL);
17839 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
17840 fde->dw_fde_second_end, false);
17841 add_name_attribute (seg_die, name);
17842 if (want_pubnames ())
17843 add_pubname_string (name, seg_die);
17846 else
17847 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
17848 false);
17851 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
17853 /* We define the "frame base" as the function's CFA. This is more
17854 convenient for several reasons: (1) It's stable across the prologue
17855 and epilogue, which makes it better than just a frame pointer,
17856 (2) With dwarf3, there exists a one-byte encoding that allows us
17857 to reference the .debug_frame data by proxy, but failing that,
17858 (3) We can at least reuse the code inspection and interpretation
17859 code that determines the CFA position at various points in the
17860 function. */
17861 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
17863 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
17864 add_AT_loc (subr_die, DW_AT_frame_base, op);
17866 else
17868 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
17869 if (list->dw_loc_next)
17870 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
17871 else
17872 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
17875 /* Compute a displacement from the "steady-state frame pointer" to
17876 the CFA. The former is what all stack slots and argument slots
17877 will reference in the rtl; the later is what we've told the
17878 debugger about. We'll need to adjust all frame_base references
17879 by this displacement. */
17880 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
17882 if (fun->static_chain_decl)
17883 add_AT_location_description (subr_die, DW_AT_static_link,
17884 loc_list_from_tree (fun->static_chain_decl, 2));
17887 /* Generate child dies for template paramaters. */
17888 if (debug_info_level > DINFO_LEVEL_TERSE)
17889 gen_generic_params_dies (decl);
17891 /* Now output descriptions of the arguments for this function. This gets
17892 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
17893 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
17894 `...' at the end of the formal parameter list. In order to find out if
17895 there was a trailing ellipsis or not, we must instead look at the type
17896 associated with the FUNCTION_DECL. This will be a node of type
17897 FUNCTION_TYPE. If the chain of type nodes hanging off of this
17898 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
17899 an ellipsis at the end. */
17901 /* In the case where we are describing a mere function declaration, all we
17902 need to do here (and all we *can* do here) is to describe the *types* of
17903 its formal parameters. */
17904 if (debug_info_level <= DINFO_LEVEL_TERSE)
17906 else if (declaration)
17907 gen_formal_types_die (decl, subr_die);
17908 else
17910 /* Generate DIEs to represent all known formal parameters. */
17911 tree parm = DECL_ARGUMENTS (decl);
17912 tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
17913 tree generic_decl_parm = generic_decl
17914 ? DECL_ARGUMENTS (generic_decl)
17915 : NULL;
17917 /* Now we want to walk the list of parameters of the function and
17918 emit their relevant DIEs.
17920 We consider the case of DECL being an instance of a generic function
17921 as well as it being a normal function.
17923 If DECL is an instance of a generic function we walk the
17924 parameters of the generic function declaration _and_ the parameters of
17925 DECL itself. This is useful because we want to emit specific DIEs for
17926 function parameter packs and those are declared as part of the
17927 generic function declaration. In that particular case,
17928 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
17929 That DIE has children DIEs representing the set of arguments
17930 of the pack. Note that the set of pack arguments can be empty.
17931 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
17932 children DIE.
17934 Otherwise, we just consider the parameters of DECL. */
17935 while (generic_decl_parm || parm)
17937 if (generic_decl_parm
17938 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
17939 gen_formal_parameter_pack_die (generic_decl_parm,
17940 parm, subr_die,
17941 &parm);
17942 else if (parm)
17944 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
17946 if (parm == DECL_ARGUMENTS (decl)
17947 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
17948 && parm_die
17949 && (dwarf_version >= 3 || !dwarf_strict))
17950 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
17952 parm = DECL_CHAIN (parm);
17955 if (generic_decl_parm)
17956 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
17959 /* Decide whether we need an unspecified_parameters DIE at the end.
17960 There are 2 more cases to do this for: 1) the ansi ... declaration -
17961 this is detectable when the end of the arg list is not a
17962 void_type_node 2) an unprototyped function declaration (not a
17963 definition). This just means that we have no info about the
17964 parameters at all. */
17965 if (prototype_p (TREE_TYPE (decl)))
17967 /* This is the prototyped case, check for.... */
17968 if (stdarg_p (TREE_TYPE (decl)))
17969 gen_unspecified_parameters_die (decl, subr_die);
17971 else if (DECL_INITIAL (decl) == NULL_TREE)
17972 gen_unspecified_parameters_die (decl, subr_die);
17975 /* Output Dwarf info for all of the stuff within the body of the function
17976 (if it has one - it may be just a declaration). */
17977 outer_scope = DECL_INITIAL (decl);
17979 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
17980 a function. This BLOCK actually represents the outermost binding contour
17981 for the function, i.e. the contour in which the function's formal
17982 parameters and labels get declared. Curiously, it appears that the front
17983 end doesn't actually put the PARM_DECL nodes for the current function onto
17984 the BLOCK_VARS list for this outer scope, but are strung off of the
17985 DECL_ARGUMENTS list for the function instead.
17987 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
17988 the LABEL_DECL nodes for the function however, and we output DWARF info
17989 for those in decls_for_scope. Just within the `outer_scope' there will be
17990 a BLOCK node representing the function's outermost pair of curly braces,
17991 and any blocks used for the base and member initializers of a C++
17992 constructor function. */
17993 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
17995 int call_site_note_count = 0;
17996 int tail_call_site_note_count = 0;
17998 /* Emit a DW_TAG_variable DIE for a named return value. */
17999 if (DECL_NAME (DECL_RESULT (decl)))
18000 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
18002 current_function_has_inlines = 0;
18003 decls_for_scope (outer_scope, subr_die, 0);
18005 if (call_arg_locations && !dwarf_strict)
18007 struct call_arg_loc_node *ca_loc;
18008 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
18010 dw_die_ref die = NULL;
18011 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
18012 rtx arg, next_arg;
18014 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
18015 arg; arg = next_arg)
18017 dw_loc_descr_ref reg, val;
18018 enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
18019 dw_die_ref cdie, tdie = NULL;
18021 next_arg = XEXP (arg, 1);
18022 if (REG_P (XEXP (XEXP (arg, 0), 0))
18023 && next_arg
18024 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
18025 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
18026 && REGNO (XEXP (XEXP (arg, 0), 0))
18027 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
18028 next_arg = XEXP (next_arg, 1);
18029 if (mode == VOIDmode)
18031 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
18032 if (mode == VOIDmode)
18033 mode = GET_MODE (XEXP (arg, 0));
18035 if (mode == VOIDmode || mode == BLKmode)
18036 continue;
18037 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
18039 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18040 tloc = XEXP (XEXP (arg, 0), 1);
18041 continue;
18043 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
18044 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
18046 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18047 tlocc = XEXP (XEXP (arg, 0), 1);
18048 continue;
18050 reg = NULL;
18051 if (REG_P (XEXP (XEXP (arg, 0), 0)))
18052 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
18053 VAR_INIT_STATUS_INITIALIZED);
18054 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
18056 rtx mem = XEXP (XEXP (arg, 0), 0);
18057 reg = mem_loc_descriptor (XEXP (mem, 0),
18058 get_address_mode (mem),
18059 GET_MODE (mem),
18060 VAR_INIT_STATUS_INITIALIZED);
18062 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
18063 == DEBUG_PARAMETER_REF)
18065 tree tdecl
18066 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
18067 tdie = lookup_decl_die (tdecl);
18068 if (tdie == NULL)
18069 continue;
18071 else
18072 continue;
18073 if (reg == NULL
18074 && GET_CODE (XEXP (XEXP (arg, 0), 0))
18075 != DEBUG_PARAMETER_REF)
18076 continue;
18077 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
18078 VOIDmode,
18079 VAR_INIT_STATUS_INITIALIZED);
18080 if (val == NULL)
18081 continue;
18082 if (die == NULL)
18083 die = gen_call_site_die (decl, subr_die, ca_loc);
18084 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
18085 NULL_TREE);
18086 if (reg != NULL)
18087 add_AT_loc (cdie, DW_AT_location, reg);
18088 else if (tdie != NULL)
18089 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
18090 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
18091 if (next_arg != XEXP (arg, 1))
18093 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
18094 if (mode == VOIDmode)
18095 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
18096 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
18097 0), 1),
18098 mode, VOIDmode,
18099 VAR_INIT_STATUS_INITIALIZED);
18100 if (val != NULL)
18101 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
18104 if (die == NULL
18105 && (ca_loc->symbol_ref || tloc))
18106 die = gen_call_site_die (decl, subr_die, ca_loc);
18107 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
18109 dw_loc_descr_ref tval = NULL;
18111 if (tloc != NULL_RTX)
18112 tval = mem_loc_descriptor (tloc,
18113 GET_MODE (tloc) == VOIDmode
18114 ? Pmode : GET_MODE (tloc),
18115 VOIDmode,
18116 VAR_INIT_STATUS_INITIALIZED);
18117 if (tval)
18118 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
18119 else if (tlocc != NULL_RTX)
18121 tval = mem_loc_descriptor (tlocc,
18122 GET_MODE (tlocc) == VOIDmode
18123 ? Pmode : GET_MODE (tlocc),
18124 VOIDmode,
18125 VAR_INIT_STATUS_INITIALIZED);
18126 if (tval)
18127 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
18128 tval);
18131 if (die != NULL)
18133 call_site_note_count++;
18134 if (ca_loc->tail_call_p)
18135 tail_call_site_note_count++;
18139 call_arg_locations = NULL;
18140 call_arg_loc_last = NULL;
18141 if (tail_call_site_count >= 0
18142 && tail_call_site_count == tail_call_site_note_count
18143 && !dwarf_strict)
18145 if (call_site_count >= 0
18146 && call_site_count == call_site_note_count)
18147 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
18148 else
18149 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
18151 call_site_count = -1;
18152 tail_call_site_count = -1;
18154 /* Add the calling convention attribute if requested. */
18155 add_calling_convention_attribute (subr_die, decl);
18159 /* Returns a hash value for X (which really is a die_struct). */
18161 static hashval_t
18162 common_block_die_table_hash (const void *x)
18164 const_dw_die_ref d = (const_dw_die_ref) x;
18165 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
18168 /* Return nonzero if decl_id and die_parent of die_struct X is the same
18169 as decl_id and die_parent of die_struct Y. */
18171 static int
18172 common_block_die_table_eq (const void *x, const void *y)
18174 const_dw_die_ref d = (const_dw_die_ref) x;
18175 const_dw_die_ref e = (const_dw_die_ref) y;
18176 return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
18179 /* Generate a DIE to represent a declared data object.
18180 Either DECL or ORIGIN must be non-null. */
18182 static void
18183 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
18185 HOST_WIDE_INT off = 0;
18186 tree com_decl;
18187 tree decl_or_origin = decl ? decl : origin;
18188 tree ultimate_origin;
18189 dw_die_ref var_die;
18190 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
18191 dw_die_ref origin_die;
18192 bool declaration = (DECL_EXTERNAL (decl_or_origin)
18193 || class_or_namespace_scope_p (context_die));
18194 bool specialization_p = false;
18196 ultimate_origin = decl_ultimate_origin (decl_or_origin);
18197 if (decl || ultimate_origin)
18198 origin = ultimate_origin;
18199 com_decl = fortran_common (decl_or_origin, &off);
18201 /* Symbol in common gets emitted as a child of the common block, in the form
18202 of a data member. */
18203 if (com_decl)
18205 dw_die_ref com_die;
18206 dw_loc_list_ref loc;
18207 die_node com_die_arg;
18209 var_die = lookup_decl_die (decl_or_origin);
18210 if (var_die)
18212 if (get_AT (var_die, DW_AT_location) == NULL)
18214 loc = loc_list_from_tree (com_decl, off ? 1 : 2);
18215 if (loc)
18217 if (off)
18219 /* Optimize the common case. */
18220 if (single_element_loc_list_p (loc)
18221 && loc->expr->dw_loc_opc == DW_OP_addr
18222 && loc->expr->dw_loc_next == NULL
18223 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
18224 == SYMBOL_REF)
18226 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18227 loc->expr->dw_loc_oprnd1.v.val_addr
18228 = plus_constant (GET_MODE (x), x , off);
18230 else
18231 loc_list_plus_const (loc, off);
18233 add_AT_location_description (var_die, DW_AT_location, loc);
18234 remove_AT (var_die, DW_AT_declaration);
18237 return;
18240 if (common_block_die_table == NULL)
18241 common_block_die_table
18242 = htab_create_ggc (10, common_block_die_table_hash,
18243 common_block_die_table_eq, NULL);
18245 com_die_arg.decl_id = DECL_UID (com_decl);
18246 com_die_arg.die_parent = context_die;
18247 com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
18248 loc = loc_list_from_tree (com_decl, 2);
18249 if (com_die == NULL)
18251 const char *cnam
18252 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
18253 void **slot;
18255 com_die = new_die (DW_TAG_common_block, context_die, decl);
18256 add_name_and_src_coords_attributes (com_die, com_decl);
18257 if (loc)
18259 add_AT_location_description (com_die, DW_AT_location, loc);
18260 /* Avoid sharing the same loc descriptor between
18261 DW_TAG_common_block and DW_TAG_variable. */
18262 loc = loc_list_from_tree (com_decl, 2);
18264 else if (DECL_EXTERNAL (decl))
18265 add_AT_flag (com_die, DW_AT_declaration, 1);
18266 if (want_pubnames ())
18267 add_pubname_string (cnam, com_die); /* ??? needed? */
18268 com_die->decl_id = DECL_UID (com_decl);
18269 slot = htab_find_slot (common_block_die_table, com_die, INSERT);
18270 *slot = (void *) com_die;
18272 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
18274 add_AT_location_description (com_die, DW_AT_location, loc);
18275 loc = loc_list_from_tree (com_decl, 2);
18276 remove_AT (com_die, DW_AT_declaration);
18278 var_die = new_die (DW_TAG_variable, com_die, decl);
18279 add_name_and_src_coords_attributes (var_die, decl);
18280 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
18281 TREE_THIS_VOLATILE (decl), context_die);
18282 add_AT_flag (var_die, DW_AT_external, 1);
18283 if (loc)
18285 if (off)
18287 /* Optimize the common case. */
18288 if (single_element_loc_list_p (loc)
18289 && loc->expr->dw_loc_opc == DW_OP_addr
18290 && loc->expr->dw_loc_next == NULL
18291 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
18293 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18294 loc->expr->dw_loc_oprnd1.v.val_addr
18295 = plus_constant (GET_MODE (x), x, off);
18297 else
18298 loc_list_plus_const (loc, off);
18300 add_AT_location_description (var_die, DW_AT_location, loc);
18302 else if (DECL_EXTERNAL (decl))
18303 add_AT_flag (var_die, DW_AT_declaration, 1);
18304 equate_decl_number_to_die (decl, var_die);
18305 return;
18308 /* If the compiler emitted a definition for the DECL declaration
18309 and if we already emitted a DIE for it, don't emit a second
18310 DIE for it again. Allow re-declarations of DECLs that are
18311 inside functions, though. */
18312 if (old_die && declaration && !local_scope_p (context_die))
18313 return;
18315 /* For static data members, the declaration in the class is supposed
18316 to have DW_TAG_member tag; the specification should still be
18317 DW_TAG_variable referencing the DW_TAG_member DIE. */
18318 if (declaration && class_scope_p (context_die))
18319 var_die = new_die (DW_TAG_member, context_die, decl);
18320 else
18321 var_die = new_die (DW_TAG_variable, context_die, decl);
18323 origin_die = NULL;
18324 if (origin != NULL)
18325 origin_die = add_abstract_origin_attribute (var_die, origin);
18327 /* Loop unrolling can create multiple blocks that refer to the same
18328 static variable, so we must test for the DW_AT_declaration flag.
18330 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
18331 copy decls and set the DECL_ABSTRACT flag on them instead of
18332 sharing them.
18334 ??? Duplicated blocks have been rewritten to use .debug_ranges.
18336 ??? The declare_in_namespace support causes us to get two DIEs for one
18337 variable, both of which are declarations. We want to avoid considering
18338 one to be a specification, so we must test that this DIE is not a
18339 declaration. */
18340 else if (old_die && TREE_STATIC (decl) && ! declaration
18341 && get_AT_flag (old_die, DW_AT_declaration) == 1)
18343 /* This is a definition of a C++ class level static. */
18344 add_AT_specification (var_die, old_die);
18345 specialization_p = true;
18346 if (DECL_NAME (decl))
18348 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18349 struct dwarf_file_data * file_index = lookup_filename (s.file);
18351 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18352 add_AT_file (var_die, DW_AT_decl_file, file_index);
18354 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18355 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
18357 if (old_die->die_tag == DW_TAG_member)
18358 add_linkage_name (var_die, decl);
18361 else
18362 add_name_and_src_coords_attributes (var_die, decl);
18364 if ((origin == NULL && !specialization_p)
18365 || (origin != NULL
18366 && !DECL_ABSTRACT (decl_or_origin)
18367 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
18368 decl_function_context
18369 (decl_or_origin))))
18371 tree type = TREE_TYPE (decl_or_origin);
18373 if (decl_by_reference_p (decl_or_origin))
18374 add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
18375 else
18376 add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
18377 TREE_THIS_VOLATILE (decl_or_origin), context_die);
18380 if (origin == NULL && !specialization_p)
18382 if (TREE_PUBLIC (decl))
18383 add_AT_flag (var_die, DW_AT_external, 1);
18385 if (DECL_ARTIFICIAL (decl))
18386 add_AT_flag (var_die, DW_AT_artificial, 1);
18388 add_accessibility_attribute (var_die, decl);
18391 if (declaration)
18392 add_AT_flag (var_die, DW_AT_declaration, 1);
18394 if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
18395 equate_decl_number_to_die (decl, var_die);
18397 if (! declaration
18398 && (! DECL_ABSTRACT (decl_or_origin)
18399 /* Local static vars are shared between all clones/inlines,
18400 so emit DW_AT_location on the abstract DIE if DECL_RTL is
18401 already set. */
18402 || (TREE_CODE (decl_or_origin) == VAR_DECL
18403 && TREE_STATIC (decl_or_origin)
18404 && DECL_RTL_SET_P (decl_or_origin)))
18405 /* When abstract origin already has DW_AT_location attribute, no need
18406 to add it again. */
18407 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
18409 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
18410 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
18411 defer_location (decl_or_origin, var_die);
18412 else
18413 add_location_or_const_value_attribute (var_die, decl_or_origin,
18414 decl == NULL, DW_AT_location);
18415 add_pubname (decl_or_origin, var_die);
18417 else
18418 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
18421 /* Generate a DIE to represent a named constant. */
18423 static void
18424 gen_const_die (tree decl, dw_die_ref context_die)
18426 dw_die_ref const_die;
18427 tree type = TREE_TYPE (decl);
18429 const_die = new_die (DW_TAG_constant, context_die, decl);
18430 add_name_and_src_coords_attributes (const_die, decl);
18431 add_type_attribute (const_die, type, 1, 0, context_die);
18432 if (TREE_PUBLIC (decl))
18433 add_AT_flag (const_die, DW_AT_external, 1);
18434 if (DECL_ARTIFICIAL (decl))
18435 add_AT_flag (const_die, DW_AT_artificial, 1);
18436 tree_add_const_value_attribute_for_decl (const_die, decl);
18439 /* Generate a DIE to represent a label identifier. */
18441 static void
18442 gen_label_die (tree decl, dw_die_ref context_die)
18444 tree origin = decl_ultimate_origin (decl);
18445 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
18446 rtx insn;
18447 char label[MAX_ARTIFICIAL_LABEL_BYTES];
18449 if (origin != NULL)
18450 add_abstract_origin_attribute (lbl_die, origin);
18451 else
18452 add_name_and_src_coords_attributes (lbl_die, decl);
18454 if (DECL_ABSTRACT (decl))
18455 equate_decl_number_to_die (decl, lbl_die);
18456 else
18458 insn = DECL_RTL_IF_SET (decl);
18460 /* Deleted labels are programmer specified labels which have been
18461 eliminated because of various optimizations. We still emit them
18462 here so that it is possible to put breakpoints on them. */
18463 if (insn
18464 && (LABEL_P (insn)
18465 || ((NOTE_P (insn)
18466 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
18468 /* When optimization is enabled (via -O) some parts of the compiler
18469 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
18470 represent source-level labels which were explicitly declared by
18471 the user. This really shouldn't be happening though, so catch
18472 it if it ever does happen. */
18473 gcc_assert (!INSN_DELETED_P (insn));
18475 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
18476 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18478 else if (insn
18479 && NOTE_P (insn)
18480 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
18481 && CODE_LABEL_NUMBER (insn) != -1)
18483 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
18484 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18489 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
18490 attributes to the DIE for a block STMT, to describe where the inlined
18491 function was called from. This is similar to add_src_coords_attributes. */
18493 static inline void
18494 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
18496 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
18498 if (dwarf_version >= 3 || !dwarf_strict)
18500 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
18501 add_AT_unsigned (die, DW_AT_call_line, s.line);
18506 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
18507 Add low_pc and high_pc attributes to the DIE for a block STMT. */
18509 static inline void
18510 add_high_low_attributes (tree stmt, dw_die_ref die)
18512 char label[MAX_ARTIFICIAL_LABEL_BYTES];
18514 if (BLOCK_FRAGMENT_CHAIN (stmt)
18515 && (dwarf_version >= 3 || !dwarf_strict))
18517 tree chain, superblock = NULL_TREE;
18518 dw_die_ref pdie;
18519 dw_attr_ref attr = NULL;
18521 if (inlined_function_outer_scope_p (stmt))
18523 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18524 BLOCK_NUMBER (stmt));
18525 add_AT_lbl_id (die, DW_AT_entry_pc, label);
18528 /* Optimize duplicate .debug_ranges lists or even tails of
18529 lists. If this BLOCK has same ranges as its supercontext,
18530 lookup DW_AT_ranges attribute in the supercontext (and
18531 recursively so), verify that the ranges_table contains the
18532 right values and use it instead of adding a new .debug_range. */
18533 for (chain = stmt, pdie = die;
18534 BLOCK_SAME_RANGE (chain);
18535 chain = BLOCK_SUPERCONTEXT (chain))
18537 dw_attr_ref new_attr;
18539 pdie = pdie->die_parent;
18540 if (pdie == NULL)
18541 break;
18542 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
18543 break;
18544 new_attr = get_AT (pdie, DW_AT_ranges);
18545 if (new_attr == NULL
18546 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
18547 break;
18548 attr = new_attr;
18549 superblock = BLOCK_SUPERCONTEXT (chain);
18551 if (attr != NULL
18552 && (ranges_table[attr->dw_attr_val.v.val_offset
18553 / 2 / DWARF2_ADDR_SIZE].num
18554 == BLOCK_NUMBER (superblock))
18555 && BLOCK_FRAGMENT_CHAIN (superblock))
18557 unsigned long off = attr->dw_attr_val.v.val_offset
18558 / 2 / DWARF2_ADDR_SIZE;
18559 unsigned long supercnt = 0, thiscnt = 0;
18560 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
18561 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
18563 ++supercnt;
18564 gcc_checking_assert (ranges_table[off + supercnt].num
18565 == BLOCK_NUMBER (chain));
18567 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
18568 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
18569 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
18570 ++thiscnt;
18571 gcc_assert (supercnt >= thiscnt);
18572 add_AT_range_list (die, DW_AT_ranges,
18573 ((off + supercnt - thiscnt)
18574 * 2 * DWARF2_ADDR_SIZE),
18575 false);
18576 return;
18579 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
18581 chain = BLOCK_FRAGMENT_CHAIN (stmt);
18584 add_ranges (chain);
18585 chain = BLOCK_FRAGMENT_CHAIN (chain);
18587 while (chain);
18588 add_ranges (NULL);
18590 else
18592 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
18593 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18594 BLOCK_NUMBER (stmt));
18595 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
18596 BLOCK_NUMBER (stmt));
18597 add_AT_low_high_pc (die, label, label_high, false);
18601 /* Generate a DIE for a lexical block. */
18603 static void
18604 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
18606 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
18608 if (call_arg_locations)
18610 if (block_map.length () <= BLOCK_NUMBER (stmt))
18611 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
18612 block_map[BLOCK_NUMBER (stmt)] = stmt_die;
18615 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
18616 add_high_low_attributes (stmt, stmt_die);
18618 decls_for_scope (stmt, stmt_die, depth);
18621 /* Generate a DIE for an inlined subprogram. */
18623 static void
18624 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
18626 tree decl;
18628 /* The instance of function that is effectively being inlined shall not
18629 be abstract. */
18630 gcc_assert (! BLOCK_ABSTRACT (stmt));
18632 decl = block_ultimate_origin (stmt);
18634 /* Emit info for the abstract instance first, if we haven't yet. We
18635 must emit this even if the block is abstract, otherwise when we
18636 emit the block below (or elsewhere), we may end up trying to emit
18637 a die whose origin die hasn't been emitted, and crashing. */
18638 dwarf2out_abstract_function (decl);
18640 if (! BLOCK_ABSTRACT (stmt))
18642 dw_die_ref subr_die
18643 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
18645 if (call_arg_locations)
18647 if (block_map.length () <= BLOCK_NUMBER (stmt))
18648 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
18649 block_map[BLOCK_NUMBER (stmt)] = subr_die;
18651 add_abstract_origin_attribute (subr_die, decl);
18652 if (TREE_ASM_WRITTEN (stmt))
18653 add_high_low_attributes (stmt, subr_die);
18654 add_call_src_coords_attributes (stmt, subr_die);
18656 decls_for_scope (stmt, subr_die, depth);
18657 current_function_has_inlines = 1;
18661 /* Generate a DIE for a field in a record, or structure. */
18663 static void
18664 gen_field_die (tree decl, dw_die_ref context_die)
18666 dw_die_ref decl_die;
18668 if (TREE_TYPE (decl) == error_mark_node)
18669 return;
18671 decl_die = new_die (DW_TAG_member, context_die, decl);
18672 add_name_and_src_coords_attributes (decl_die, decl);
18673 add_type_attribute (decl_die, member_declared_type (decl),
18674 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
18675 context_die);
18677 if (DECL_BIT_FIELD_TYPE (decl))
18679 add_byte_size_attribute (decl_die, decl);
18680 add_bit_size_attribute (decl_die, decl);
18681 add_bit_offset_attribute (decl_die, decl);
18684 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
18685 add_data_member_location_attribute (decl_die, decl);
18687 if (DECL_ARTIFICIAL (decl))
18688 add_AT_flag (decl_die, DW_AT_artificial, 1);
18690 add_accessibility_attribute (decl_die, decl);
18692 /* Equate decl number to die, so that we can look up this decl later on. */
18693 equate_decl_number_to_die (decl, decl_die);
18696 #if 0
18697 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18698 Use modified_type_die instead.
18699 We keep this code here just in case these types of DIEs may be needed to
18700 represent certain things in other languages (e.g. Pascal) someday. */
18702 static void
18703 gen_pointer_type_die (tree type, dw_die_ref context_die)
18705 dw_die_ref ptr_die
18706 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
18708 equate_type_number_to_die (type, ptr_die);
18709 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18710 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
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_reference_type_die (tree type, dw_die_ref context_die)
18721 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
18723 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
18724 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
18725 else
18726 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
18728 equate_type_number_to_die (type, ref_die);
18729 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
18730 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18732 #endif
18734 /* Generate a DIE for a pointer to a member type. */
18736 static void
18737 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
18739 dw_die_ref ptr_die
18740 = new_die (DW_TAG_ptr_to_member_type,
18741 scope_die_for (type, context_die), type);
18743 equate_type_number_to_die (type, ptr_die);
18744 add_AT_die_ref (ptr_die, DW_AT_containing_type,
18745 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
18746 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18749 typedef const char *dchar_p; /* For DEF_VEC_P. */
18751 static char *producer_string;
18753 /* Return a heap allocated producer string including command line options
18754 if -grecord-gcc-switches. */
18756 static char *
18757 gen_producer_string (void)
18759 size_t j;
18760 vec<dchar_p> switches = vNULL;
18761 const char *language_string = lang_hooks.name;
18762 char *producer, *tail;
18763 const char *p;
18764 size_t len = dwarf_record_gcc_switches ? 0 : 3;
18765 size_t plen = strlen (language_string) + 1 + strlen (version_string);
18767 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
18768 switch (save_decoded_options[j].opt_index)
18770 case OPT_o:
18771 case OPT_d:
18772 case OPT_dumpbase:
18773 case OPT_dumpdir:
18774 case OPT_auxbase:
18775 case OPT_auxbase_strip:
18776 case OPT_quiet:
18777 case OPT_version:
18778 case OPT_v:
18779 case OPT_w:
18780 case OPT_L:
18781 case OPT_D:
18782 case OPT_I:
18783 case OPT_U:
18784 case OPT_SPECIAL_unknown:
18785 case OPT_SPECIAL_ignore:
18786 case OPT_SPECIAL_program_name:
18787 case OPT_SPECIAL_input_file:
18788 case OPT_grecord_gcc_switches:
18789 case OPT_gno_record_gcc_switches:
18790 case OPT__output_pch_:
18791 case OPT_fdiagnostics_show_location_:
18792 case OPT_fdiagnostics_show_option:
18793 case OPT_fdiagnostics_show_caret:
18794 case OPT_fverbose_asm:
18795 case OPT____:
18796 case OPT__sysroot_:
18797 case OPT_nostdinc:
18798 case OPT_nostdinc__:
18799 /* Ignore these. */
18800 continue;
18801 default:
18802 if (cl_options[save_decoded_options[j].opt_index].flags
18803 & CL_NO_DWARF_RECORD)
18804 continue;
18805 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
18806 == '-');
18807 switch (save_decoded_options[j].canonical_option[0][1])
18809 case 'M':
18810 case 'i':
18811 case 'W':
18812 continue;
18813 case 'f':
18814 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
18815 "dump", 4) == 0)
18816 continue;
18817 break;
18818 default:
18819 break;
18821 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
18822 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
18823 break;
18826 producer = XNEWVEC (char, plen + 1 + len + 1);
18827 tail = producer;
18828 sprintf (tail, "%s %s", language_string, version_string);
18829 tail += plen;
18831 FOR_EACH_VEC_ELT (switches, j, p)
18833 len = strlen (p);
18834 *tail = ' ';
18835 memcpy (tail + 1, p, len);
18836 tail += len + 1;
18839 *tail = '\0';
18840 switches.release ();
18841 return producer;
18844 /* Generate the DIE for the compilation unit. */
18846 static dw_die_ref
18847 gen_compile_unit_die (const char *filename)
18849 dw_die_ref die;
18850 const char *language_string = lang_hooks.name;
18851 int language;
18853 die = new_die (DW_TAG_compile_unit, NULL, NULL);
18855 if (filename)
18857 add_name_attribute (die, filename);
18858 /* Don't add cwd for <built-in>. */
18859 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
18860 add_comp_dir_attribute (die);
18863 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
18865 /* If our producer is LTO try to figure out a common language to use
18866 from the global list of translation units. */
18867 if (strcmp (language_string, "GNU GIMPLE") == 0)
18869 unsigned i;
18870 tree t;
18871 const char *common_lang = NULL;
18873 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
18875 if (!TRANSLATION_UNIT_LANGUAGE (t))
18876 continue;
18877 if (!common_lang)
18878 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
18879 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
18881 else if (strncmp (common_lang, "GNU C", 5) == 0
18882 && strncmp(TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
18883 /* Mixing C and C++ is ok, use C++ in that case. */
18884 common_lang = "GNU C++";
18885 else
18887 /* Fall back to C. */
18888 common_lang = NULL;
18889 break;
18893 if (common_lang)
18894 language_string = common_lang;
18897 language = DW_LANG_C89;
18898 if (strcmp (language_string, "GNU C++") == 0)
18899 language = DW_LANG_C_plus_plus;
18900 else if (strcmp (language_string, "GNU F77") == 0)
18901 language = DW_LANG_Fortran77;
18902 else if (strcmp (language_string, "GNU Pascal") == 0)
18903 language = DW_LANG_Pascal83;
18904 else if (dwarf_version >= 3 || !dwarf_strict)
18906 if (strcmp (language_string, "GNU Ada") == 0)
18907 language = DW_LANG_Ada95;
18908 else if (strcmp (language_string, "GNU Fortran") == 0)
18909 language = DW_LANG_Fortran95;
18910 else if (strcmp (language_string, "GNU Java") == 0)
18911 language = DW_LANG_Java;
18912 else if (strcmp (language_string, "GNU Objective-C") == 0)
18913 language = DW_LANG_ObjC;
18914 else if (strcmp (language_string, "GNU Objective-C++") == 0)
18915 language = DW_LANG_ObjC_plus_plus;
18916 else if (dwarf_version >= 5 || !dwarf_strict)
18918 if (strcmp (language_string, "GNU Go") == 0)
18919 language = DW_LANG_Go;
18922 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
18923 else if (strcmp (language_string, "GNU Fortran") == 0)
18924 language = DW_LANG_Fortran90;
18926 add_AT_unsigned (die, DW_AT_language, language);
18928 switch (language)
18930 case DW_LANG_Fortran77:
18931 case DW_LANG_Fortran90:
18932 case DW_LANG_Fortran95:
18933 /* Fortran has case insensitive identifiers and the front-end
18934 lowercases everything. */
18935 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
18936 break;
18937 default:
18938 /* The default DW_ID_case_sensitive doesn't need to be specified. */
18939 break;
18941 return die;
18944 /* Generate the DIE for a base class. */
18946 static void
18947 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
18949 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
18951 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
18952 add_data_member_location_attribute (die, binfo);
18954 if (BINFO_VIRTUAL_P (binfo))
18955 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
18957 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
18958 children, otherwise the default is DW_ACCESS_public. In DWARF2
18959 the default has always been DW_ACCESS_private. */
18960 if (access == access_public_node)
18962 if (dwarf_version == 2
18963 || context_die->die_tag == DW_TAG_class_type)
18964 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
18966 else if (access == access_protected_node)
18967 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
18968 else if (dwarf_version > 2
18969 && context_die->die_tag != DW_TAG_class_type)
18970 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
18973 /* Generate a DIE for a class member. */
18975 static void
18976 gen_member_die (tree type, dw_die_ref context_die)
18978 tree member;
18979 tree binfo = TYPE_BINFO (type);
18980 dw_die_ref child;
18982 /* If this is not an incomplete type, output descriptions of each of its
18983 members. Note that as we output the DIEs necessary to represent the
18984 members of this record or union type, we will also be trying to output
18985 DIEs to represent the *types* of those members. However the `type'
18986 function (above) will specifically avoid generating type DIEs for member
18987 types *within* the list of member DIEs for this (containing) type except
18988 for those types (of members) which are explicitly marked as also being
18989 members of this (containing) type themselves. The g++ front- end can
18990 force any given type to be treated as a member of some other (containing)
18991 type by setting the TYPE_CONTEXT of the given (member) type to point to
18992 the TREE node representing the appropriate (containing) type. */
18994 /* First output info about the base classes. */
18995 if (binfo)
18997 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
18998 int i;
18999 tree base;
19001 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19002 gen_inheritance_die (base,
19003 (accesses ? (*accesses)[i] : access_public_node),
19004 context_die);
19007 /* Now output info about the data members and type members. */
19008 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19010 /* If we thought we were generating minimal debug info for TYPE
19011 and then changed our minds, some of the member declarations
19012 may have already been defined. Don't define them again, but
19013 do put them in the right order. */
19015 child = lookup_decl_die (member);
19016 if (child)
19017 splice_child_die (context_die, child);
19018 else
19019 gen_decl_die (member, NULL, context_die);
19022 /* Now output info about the function members (if any). */
19023 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19025 /* Don't include clones in the member list. */
19026 if (DECL_ABSTRACT_ORIGIN (member))
19027 continue;
19029 child = lookup_decl_die (member);
19030 if (child)
19031 splice_child_die (context_die, child);
19032 else
19033 gen_decl_die (member, NULL, context_die);
19037 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
19038 is set, we pretend that the type was never defined, so we only get the
19039 member DIEs needed by later specification DIEs. */
19041 static void
19042 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19043 enum debug_info_usage usage)
19045 dw_die_ref type_die = lookup_type_die (type);
19046 dw_die_ref scope_die = 0;
19047 int nested = 0;
19048 int complete = (TYPE_SIZE (type)
19049 && (! TYPE_STUB_DECL (type)
19050 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19051 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19052 complete = complete && should_emit_struct_debug (type, usage);
19054 if (type_die && ! complete)
19055 return;
19057 if (TYPE_CONTEXT (type) != NULL_TREE
19058 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19059 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19060 nested = 1;
19062 scope_die = scope_die_for (type, context_die);
19064 if (! type_die || (nested && is_cu_die (scope_die)))
19065 /* First occurrence of type or toplevel definition of nested class. */
19067 dw_die_ref old_die = type_die;
19069 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
19070 ? record_type_tag (type) : DW_TAG_union_type,
19071 scope_die, type);
19072 equate_type_number_to_die (type, type_die);
19073 if (old_die)
19074 add_AT_specification (type_die, old_die);
19075 else
19076 add_name_attribute (type_die, type_tag (type));
19078 else
19079 remove_AT (type_die, DW_AT_declaration);
19081 /* Generate child dies for template paramaters. */
19082 if (debug_info_level > DINFO_LEVEL_TERSE
19083 && COMPLETE_TYPE_P (type))
19084 schedule_generic_params_dies_gen (type);
19086 /* If this type has been completed, then give it a byte_size attribute and
19087 then give a list of members. */
19088 if (complete && !ns_decl)
19090 /* Prevent infinite recursion in cases where the type of some member of
19091 this type is expressed in terms of this type itself. */
19092 TREE_ASM_WRITTEN (type) = 1;
19093 add_byte_size_attribute (type_die, type);
19094 if (TYPE_STUB_DECL (type) != NULL_TREE)
19096 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19097 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19100 /* If the first reference to this type was as the return type of an
19101 inline function, then it may not have a parent. Fix this now. */
19102 if (type_die->die_parent == NULL)
19103 add_child_die (scope_die, type_die);
19105 push_decl_scope (type);
19106 gen_member_die (type, type_die);
19107 pop_decl_scope ();
19109 add_gnat_descriptive_type_attribute (type_die, type, context_die);
19110 if (TYPE_ARTIFICIAL (type))
19111 add_AT_flag (type_die, DW_AT_artificial, 1);
19113 /* GNU extension: Record what type our vtable lives in. */
19114 if (TYPE_VFIELD (type))
19116 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
19118 gen_type_die (vtype, context_die);
19119 add_AT_die_ref (type_die, DW_AT_containing_type,
19120 lookup_type_die (vtype));
19123 else
19125 add_AT_flag (type_die, DW_AT_declaration, 1);
19127 /* We don't need to do this for function-local types. */
19128 if (TYPE_STUB_DECL (type)
19129 && ! decl_function_context (TYPE_STUB_DECL (type)))
19130 vec_safe_push (incomplete_types, type);
19133 if (get_AT (type_die, DW_AT_name))
19134 add_pubtype (type, type_die);
19137 /* Generate a DIE for a subroutine _type_. */
19139 static void
19140 gen_subroutine_type_die (tree type, dw_die_ref context_die)
19142 tree return_type = TREE_TYPE (type);
19143 dw_die_ref subr_die
19144 = new_die (DW_TAG_subroutine_type,
19145 scope_die_for (type, context_die), type);
19147 equate_type_number_to_die (type, subr_die);
19148 add_prototyped_attribute (subr_die, type);
19149 add_type_attribute (subr_die, return_type, 0, 0, context_die);
19150 gen_formal_types_die (type, subr_die);
19152 if (get_AT (subr_die, DW_AT_name))
19153 add_pubtype (type, subr_die);
19156 /* Generate a DIE for a type definition. */
19158 static void
19159 gen_typedef_die (tree decl, dw_die_ref context_die)
19161 dw_die_ref type_die;
19162 tree origin;
19164 if (TREE_ASM_WRITTEN (decl))
19165 return;
19167 TREE_ASM_WRITTEN (decl) = 1;
19168 type_die = new_die (DW_TAG_typedef, context_die, decl);
19169 origin = decl_ultimate_origin (decl);
19170 if (origin != NULL)
19171 add_abstract_origin_attribute (type_die, origin);
19172 else
19174 tree type;
19176 add_name_and_src_coords_attributes (type_die, decl);
19177 if (DECL_ORIGINAL_TYPE (decl))
19179 type = DECL_ORIGINAL_TYPE (decl);
19181 gcc_assert (type != TREE_TYPE (decl));
19182 equate_type_number_to_die (TREE_TYPE (decl), type_die);
19184 else
19186 type = TREE_TYPE (decl);
19188 if (is_naming_typedef_decl (TYPE_NAME (type)))
19190 /* Here, we are in the case of decl being a typedef naming
19191 an anonymous type, e.g:
19192 typedef struct {...} foo;
19193 In that case TREE_TYPE (decl) is not a typedef variant
19194 type and TYPE_NAME of the anonymous type is set to the
19195 TYPE_DECL of the typedef. This construct is emitted by
19196 the C++ FE.
19198 TYPE is the anonymous struct named by the typedef
19199 DECL. As we need the DW_AT_type attribute of the
19200 DW_TAG_typedef to point to the DIE of TYPE, let's
19201 generate that DIE right away. add_type_attribute
19202 called below will then pick (via lookup_type_die) that
19203 anonymous struct DIE. */
19204 if (!TREE_ASM_WRITTEN (type))
19205 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
19207 /* This is a GNU Extension. We are adding a
19208 DW_AT_linkage_name attribute to the DIE of the
19209 anonymous struct TYPE. The value of that attribute
19210 is the name of the typedef decl naming the anonymous
19211 struct. This greatly eases the work of consumers of
19212 this debug info. */
19213 add_linkage_attr (lookup_type_die (type), decl);
19217 add_type_attribute (type_die, type, TREE_READONLY (decl),
19218 TREE_THIS_VOLATILE (decl), context_die);
19220 if (is_naming_typedef_decl (decl))
19221 /* We want that all subsequent calls to lookup_type_die with
19222 TYPE in argument yield the DW_TAG_typedef we have just
19223 created. */
19224 equate_type_number_to_die (type, type_die);
19226 add_accessibility_attribute (type_die, decl);
19229 if (DECL_ABSTRACT (decl))
19230 equate_decl_number_to_die (decl, type_die);
19232 if (get_AT (type_die, DW_AT_name))
19233 add_pubtype (decl, type_die);
19236 /* Generate a DIE for a struct, class, enum or union type. */
19238 static void
19239 gen_tagged_type_die (tree type,
19240 dw_die_ref context_die,
19241 enum debug_info_usage usage)
19243 int need_pop;
19245 if (type == NULL_TREE
19246 || !is_tagged_type (type))
19247 return;
19249 /* If this is a nested type whose containing class hasn't been written
19250 out yet, writing it out will cover this one, too. This does not apply
19251 to instantiations of member class templates; they need to be added to
19252 the containing class as they are generated. FIXME: This hurts the
19253 idea of combining type decls from multiple TUs, since we can't predict
19254 what set of template instantiations we'll get. */
19255 if (TYPE_CONTEXT (type)
19256 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19257 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
19259 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
19261 if (TREE_ASM_WRITTEN (type))
19262 return;
19264 /* If that failed, attach ourselves to the stub. */
19265 push_decl_scope (TYPE_CONTEXT (type));
19266 context_die = lookup_type_die (TYPE_CONTEXT (type));
19267 need_pop = 1;
19269 else if (TYPE_CONTEXT (type) != NULL_TREE
19270 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
19272 /* If this type is local to a function that hasn't been written
19273 out yet, use a NULL context for now; it will be fixed up in
19274 decls_for_scope. */
19275 context_die = lookup_decl_die (TYPE_CONTEXT (type));
19276 /* A declaration DIE doesn't count; nested types need to go in the
19277 specification. */
19278 if (context_die && is_declaration_die (context_die))
19279 context_die = NULL;
19280 need_pop = 0;
19282 else
19284 context_die = declare_in_namespace (type, context_die);
19285 need_pop = 0;
19288 if (TREE_CODE (type) == ENUMERAL_TYPE)
19290 /* This might have been written out by the call to
19291 declare_in_namespace. */
19292 if (!TREE_ASM_WRITTEN (type))
19293 gen_enumeration_type_die (type, context_die);
19295 else
19296 gen_struct_or_union_type_die (type, context_die, usage);
19298 if (need_pop)
19299 pop_decl_scope ();
19301 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
19302 it up if it is ever completed. gen_*_type_die will set it for us
19303 when appropriate. */
19306 /* Generate a type description DIE. */
19308 static void
19309 gen_type_die_with_usage (tree type, dw_die_ref context_die,
19310 enum debug_info_usage usage)
19312 struct array_descr_info info;
19314 if (type == NULL_TREE || type == error_mark_node)
19315 return;
19317 if (TYPE_NAME (type) != NULL_TREE
19318 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
19319 && is_redundant_typedef (TYPE_NAME (type))
19320 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
19321 /* The DECL of this type is a typedef we don't want to emit debug
19322 info for but we want debug info for its underlying typedef.
19323 This can happen for e.g, the injected-class-name of a C++
19324 type. */
19325 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
19327 /* If TYPE is a typedef type variant, let's generate debug info
19328 for the parent typedef which TYPE is a type of. */
19329 if (typedef_variant_p (type))
19331 if (TREE_ASM_WRITTEN (type))
19332 return;
19334 /* Prevent broken recursion; we can't hand off to the same type. */
19335 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
19337 /* Give typedefs the right scope. */
19338 context_die = scope_die_for (type, context_die);
19340 TREE_ASM_WRITTEN (type) = 1;
19342 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19343 return;
19346 /* If type is an anonymous tagged type named by a typedef, let's
19347 generate debug info for the typedef. */
19348 if (is_naming_typedef_decl (TYPE_NAME (type)))
19350 /* Use the DIE of the containing namespace as the parent DIE of
19351 the type description DIE we want to generate. */
19352 if (DECL_CONTEXT (TYPE_NAME (type))
19353 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
19354 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
19356 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19357 return;
19360 /* If this is an array type with hidden descriptor, handle it first. */
19361 if (!TREE_ASM_WRITTEN (type)
19362 && lang_hooks.types.get_array_descr_info
19363 && lang_hooks.types.get_array_descr_info (type, &info)
19364 && (dwarf_version >= 3 || !dwarf_strict))
19366 gen_descr_array_type_die (type, &info, context_die);
19367 TREE_ASM_WRITTEN (type) = 1;
19368 return;
19371 /* We are going to output a DIE to represent the unqualified version
19372 of this type (i.e. without any const or volatile qualifiers) so
19373 get the main variant (i.e. the unqualified version) of this type
19374 now. (Vectors are special because the debugging info is in the
19375 cloned type itself). */
19376 if (TREE_CODE (type) != VECTOR_TYPE)
19377 type = type_main_variant (type);
19379 if (TREE_ASM_WRITTEN (type))
19380 return;
19382 switch (TREE_CODE (type))
19384 case ERROR_MARK:
19385 break;
19387 case POINTER_TYPE:
19388 case REFERENCE_TYPE:
19389 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
19390 ensures that the gen_type_die recursion will terminate even if the
19391 type is recursive. Recursive types are possible in Ada. */
19392 /* ??? We could perhaps do this for all types before the switch
19393 statement. */
19394 TREE_ASM_WRITTEN (type) = 1;
19396 /* For these types, all that is required is that we output a DIE (or a
19397 set of DIEs) to represent the "basis" type. */
19398 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19399 DINFO_USAGE_IND_USE);
19400 break;
19402 case OFFSET_TYPE:
19403 /* This code is used for C++ pointer-to-data-member types.
19404 Output a description of the relevant class type. */
19405 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
19406 DINFO_USAGE_IND_USE);
19408 /* Output a description of the type of the object pointed to. */
19409 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19410 DINFO_USAGE_IND_USE);
19412 /* Now output a DIE to represent this pointer-to-data-member type
19413 itself. */
19414 gen_ptr_to_mbr_type_die (type, context_die);
19415 break;
19417 case FUNCTION_TYPE:
19418 /* Force out return type (in case it wasn't forced out already). */
19419 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19420 DINFO_USAGE_DIR_USE);
19421 gen_subroutine_type_die (type, context_die);
19422 break;
19424 case METHOD_TYPE:
19425 /* Force out return type (in case it wasn't forced out already). */
19426 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19427 DINFO_USAGE_DIR_USE);
19428 gen_subroutine_type_die (type, context_die);
19429 break;
19431 case ARRAY_TYPE:
19432 gen_array_type_die (type, context_die);
19433 break;
19435 case VECTOR_TYPE:
19436 gen_array_type_die (type, context_die);
19437 break;
19439 case ENUMERAL_TYPE:
19440 case RECORD_TYPE:
19441 case UNION_TYPE:
19442 case QUAL_UNION_TYPE:
19443 gen_tagged_type_die (type, context_die, usage);
19444 return;
19446 case VOID_TYPE:
19447 case INTEGER_TYPE:
19448 case REAL_TYPE:
19449 case FIXED_POINT_TYPE:
19450 case COMPLEX_TYPE:
19451 case BOOLEAN_TYPE:
19452 /* No DIEs needed for fundamental types. */
19453 break;
19455 case NULLPTR_TYPE:
19456 case LANG_TYPE:
19457 /* Just use DW_TAG_unspecified_type. */
19459 dw_die_ref type_die = lookup_type_die (type);
19460 if (type_die == NULL)
19462 tree name = TYPE_NAME (type);
19463 if (TREE_CODE (name) == TYPE_DECL)
19464 name = DECL_NAME (name);
19465 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
19466 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
19467 equate_type_number_to_die (type, type_die);
19470 break;
19472 default:
19473 gcc_unreachable ();
19476 TREE_ASM_WRITTEN (type) = 1;
19479 static void
19480 gen_type_die (tree type, dw_die_ref context_die)
19482 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
19485 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
19486 things which are local to the given block. */
19488 static void
19489 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
19491 int must_output_die = 0;
19492 bool inlined_func;
19494 /* Ignore blocks that are NULL. */
19495 if (stmt == NULL_TREE)
19496 return;
19498 inlined_func = inlined_function_outer_scope_p (stmt);
19500 /* If the block is one fragment of a non-contiguous block, do not
19501 process the variables, since they will have been done by the
19502 origin block. Do process subblocks. */
19503 if (BLOCK_FRAGMENT_ORIGIN (stmt))
19505 tree sub;
19507 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
19508 gen_block_die (sub, context_die, depth + 1);
19510 return;
19513 /* Determine if we need to output any Dwarf DIEs at all to represent this
19514 block. */
19515 if (inlined_func)
19516 /* The outer scopes for inlinings *must* always be represented. We
19517 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
19518 must_output_die = 1;
19519 else
19521 /* Determine if this block directly contains any "significant"
19522 local declarations which we will need to output DIEs for. */
19523 if (debug_info_level > DINFO_LEVEL_TERSE)
19524 /* We are not in terse mode so *any* local declaration counts
19525 as being a "significant" one. */
19526 must_output_die = ((BLOCK_VARS (stmt) != NULL
19527 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
19528 && (TREE_USED (stmt)
19529 || TREE_ASM_WRITTEN (stmt)
19530 || BLOCK_ABSTRACT (stmt)));
19531 else if ((TREE_USED (stmt)
19532 || TREE_ASM_WRITTEN (stmt)
19533 || BLOCK_ABSTRACT (stmt))
19534 && !dwarf2out_ignore_block (stmt))
19535 must_output_die = 1;
19538 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
19539 DIE for any block which contains no significant local declarations at
19540 all. Rather, in such cases we just call `decls_for_scope' so that any
19541 needed Dwarf info for any sub-blocks will get properly generated. Note
19542 that in terse mode, our definition of what constitutes a "significant"
19543 local declaration gets restricted to include only inlined function
19544 instances and local (nested) function definitions. */
19545 if (must_output_die)
19547 if (inlined_func)
19549 /* If STMT block is abstract, that means we have been called
19550 indirectly from dwarf2out_abstract_function.
19551 That function rightfully marks the descendent blocks (of
19552 the abstract function it is dealing with) as being abstract,
19553 precisely to prevent us from emitting any
19554 DW_TAG_inlined_subroutine DIE as a descendent
19555 of an abstract function instance. So in that case, we should
19556 not call gen_inlined_subroutine_die.
19558 Later though, when cgraph asks dwarf2out to emit info
19559 for the concrete instance of the function decl into which
19560 the concrete instance of STMT got inlined, the later will lead
19561 to the generation of a DW_TAG_inlined_subroutine DIE. */
19562 if (! BLOCK_ABSTRACT (stmt))
19563 gen_inlined_subroutine_die (stmt, context_die, depth);
19565 else
19566 gen_lexical_block_die (stmt, context_die, depth);
19568 else
19569 decls_for_scope (stmt, context_die, depth);
19572 /* Process variable DECL (or variable with origin ORIGIN) within
19573 block STMT and add it to CONTEXT_DIE. */
19574 static void
19575 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
19577 dw_die_ref die;
19578 tree decl_or_origin = decl ? decl : origin;
19580 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
19581 die = lookup_decl_die (decl_or_origin);
19582 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
19583 && TYPE_DECL_IS_STUB (decl_or_origin))
19584 die = lookup_type_die (TREE_TYPE (decl_or_origin));
19585 else
19586 die = NULL;
19588 if (die != NULL && die->die_parent == NULL)
19589 add_child_die (context_die, die);
19590 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
19591 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
19592 stmt, context_die);
19593 else
19594 gen_decl_die (decl, origin, context_die);
19597 /* Generate all of the decls declared within a given scope and (recursively)
19598 all of its sub-blocks. */
19600 static void
19601 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
19603 tree decl;
19604 unsigned int i;
19605 tree subblocks;
19607 /* Ignore NULL blocks. */
19608 if (stmt == NULL_TREE)
19609 return;
19611 /* Output the DIEs to represent all of the data objects and typedefs
19612 declared directly within this block but not within any nested
19613 sub-blocks. Also, nested function and tag DIEs have been
19614 generated with a parent of NULL; fix that up now. */
19615 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
19616 process_scope_var (stmt, decl, NULL_TREE, context_die);
19617 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
19618 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
19619 context_die);
19621 /* If we're at -g1, we're not interested in subblocks. */
19622 if (debug_info_level <= DINFO_LEVEL_TERSE)
19623 return;
19625 /* Output the DIEs to represent all sub-blocks (and the items declared
19626 therein) of this block. */
19627 for (subblocks = BLOCK_SUBBLOCKS (stmt);
19628 subblocks != NULL;
19629 subblocks = BLOCK_CHAIN (subblocks))
19630 gen_block_die (subblocks, context_die, depth + 1);
19633 /* Is this a typedef we can avoid emitting? */
19635 static inline int
19636 is_redundant_typedef (const_tree decl)
19638 if (TYPE_DECL_IS_STUB (decl))
19639 return 1;
19641 if (DECL_ARTIFICIAL (decl)
19642 && DECL_CONTEXT (decl)
19643 && is_tagged_type (DECL_CONTEXT (decl))
19644 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
19645 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
19646 /* Also ignore the artificial member typedef for the class name. */
19647 return 1;
19649 return 0;
19652 /* Return TRUE if TYPE is a typedef that names a type for linkage
19653 purposes. This kind of typedefs is produced by the C++ FE for
19654 constructs like:
19656 typedef struct {...} foo;
19658 In that case, there is no typedef variant type produced for foo.
19659 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
19660 struct type. */
19662 static bool
19663 is_naming_typedef_decl (const_tree decl)
19665 if (decl == NULL_TREE
19666 || TREE_CODE (decl) != TYPE_DECL
19667 || !is_tagged_type (TREE_TYPE (decl))
19668 || DECL_IS_BUILTIN (decl)
19669 || is_redundant_typedef (decl)
19670 /* It looks like Ada produces TYPE_DECLs that are very similar
19671 to C++ naming typedefs but that have different
19672 semantics. Let's be specific to c++ for now. */
19673 || !is_cxx ())
19674 return FALSE;
19676 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
19677 && TYPE_NAME (TREE_TYPE (decl)) == decl
19678 && (TYPE_STUB_DECL (TREE_TYPE (decl))
19679 != TYPE_NAME (TREE_TYPE (decl))));
19682 /* Returns the DIE for a context. */
19684 static inline dw_die_ref
19685 get_context_die (tree context)
19687 if (context)
19689 /* Find die that represents this context. */
19690 if (TYPE_P (context))
19692 context = TYPE_MAIN_VARIANT (context);
19693 return strip_naming_typedef (context, force_type_die (context));
19695 else
19696 return force_decl_die (context);
19698 return comp_unit_die ();
19701 /* Returns the DIE for decl. A DIE will always be returned. */
19703 static dw_die_ref
19704 force_decl_die (tree decl)
19706 dw_die_ref decl_die;
19707 unsigned saved_external_flag;
19708 tree save_fn = NULL_TREE;
19709 decl_die = lookup_decl_die (decl);
19710 if (!decl_die)
19712 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
19714 decl_die = lookup_decl_die (decl);
19715 if (decl_die)
19716 return decl_die;
19718 switch (TREE_CODE (decl))
19720 case FUNCTION_DECL:
19721 /* Clear current_function_decl, so that gen_subprogram_die thinks
19722 that this is a declaration. At this point, we just want to force
19723 declaration die. */
19724 save_fn = current_function_decl;
19725 current_function_decl = NULL_TREE;
19726 gen_subprogram_die (decl, context_die);
19727 current_function_decl = save_fn;
19728 break;
19730 case VAR_DECL:
19731 /* Set external flag to force declaration die. Restore it after
19732 gen_decl_die() call. */
19733 saved_external_flag = DECL_EXTERNAL (decl);
19734 DECL_EXTERNAL (decl) = 1;
19735 gen_decl_die (decl, NULL, context_die);
19736 DECL_EXTERNAL (decl) = saved_external_flag;
19737 break;
19739 case NAMESPACE_DECL:
19740 if (dwarf_version >= 3 || !dwarf_strict)
19741 dwarf2out_decl (decl);
19742 else
19743 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
19744 decl_die = comp_unit_die ();
19745 break;
19747 case TRANSLATION_UNIT_DECL:
19748 decl_die = comp_unit_die ();
19749 break;
19751 default:
19752 gcc_unreachable ();
19755 /* We should be able to find the DIE now. */
19756 if (!decl_die)
19757 decl_die = lookup_decl_die (decl);
19758 gcc_assert (decl_die);
19761 return decl_die;
19764 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
19765 always returned. */
19767 static dw_die_ref
19768 force_type_die (tree type)
19770 dw_die_ref type_die;
19772 type_die = lookup_type_die (type);
19773 if (!type_die)
19775 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
19777 type_die = modified_type_die (type, TYPE_READONLY (type),
19778 TYPE_VOLATILE (type), context_die);
19779 gcc_assert (type_die);
19781 return type_die;
19784 /* Force out any required namespaces to be able to output DECL,
19785 and return the new context_die for it, if it's changed. */
19787 static dw_die_ref
19788 setup_namespace_context (tree thing, dw_die_ref context_die)
19790 tree context = (DECL_P (thing)
19791 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
19792 if (context && TREE_CODE (context) == NAMESPACE_DECL)
19793 /* Force out the namespace. */
19794 context_die = force_decl_die (context);
19796 return context_die;
19799 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
19800 type) within its namespace, if appropriate.
19802 For compatibility with older debuggers, namespace DIEs only contain
19803 declarations; all definitions are emitted at CU scope. */
19805 static dw_die_ref
19806 declare_in_namespace (tree thing, dw_die_ref context_die)
19808 dw_die_ref ns_context;
19810 if (debug_info_level <= DINFO_LEVEL_TERSE)
19811 return context_die;
19813 /* If this decl is from an inlined function, then don't try to emit it in its
19814 namespace, as we will get confused. It would have already been emitted
19815 when the abstract instance of the inline function was emitted anyways. */
19816 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
19817 return context_die;
19819 ns_context = setup_namespace_context (thing, context_die);
19821 if (ns_context != context_die)
19823 if (is_fortran ())
19824 return ns_context;
19825 if (DECL_P (thing))
19826 gen_decl_die (thing, NULL, ns_context);
19827 else
19828 gen_type_die (thing, ns_context);
19830 return context_die;
19833 /* Generate a DIE for a namespace or namespace alias. */
19835 static void
19836 gen_namespace_die (tree decl, dw_die_ref context_die)
19838 dw_die_ref namespace_die;
19840 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
19841 they are an alias of. */
19842 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
19844 /* Output a real namespace or module. */
19845 context_die = setup_namespace_context (decl, comp_unit_die ());
19846 namespace_die = new_die (is_fortran ()
19847 ? DW_TAG_module : DW_TAG_namespace,
19848 context_die, decl);
19849 /* For Fortran modules defined in different CU don't add src coords. */
19850 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
19852 const char *name = dwarf2_name (decl, 0);
19853 if (name)
19854 add_name_attribute (namespace_die, name);
19856 else
19857 add_name_and_src_coords_attributes (namespace_die, decl);
19858 if (DECL_EXTERNAL (decl))
19859 add_AT_flag (namespace_die, DW_AT_declaration, 1);
19860 equate_decl_number_to_die (decl, namespace_die);
19862 else
19864 /* Output a namespace alias. */
19866 /* Force out the namespace we are an alias of, if necessary. */
19867 dw_die_ref origin_die
19868 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
19870 if (DECL_FILE_SCOPE_P (decl)
19871 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
19872 context_die = setup_namespace_context (decl, comp_unit_die ());
19873 /* Now create the namespace alias DIE. */
19874 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
19875 add_name_and_src_coords_attributes (namespace_die, decl);
19876 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
19877 equate_decl_number_to_die (decl, namespace_die);
19879 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
19880 if (want_pubnames ())
19881 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
19884 /* Generate Dwarf debug information for a decl described by DECL.
19885 The return value is currently only meaningful for PARM_DECLs,
19886 for all other decls it returns NULL. */
19888 static dw_die_ref
19889 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
19891 tree decl_or_origin = decl ? decl : origin;
19892 tree class_origin = NULL, ultimate_origin;
19894 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
19895 return NULL;
19897 switch (TREE_CODE (decl_or_origin))
19899 case ERROR_MARK:
19900 break;
19902 case CONST_DECL:
19903 if (!is_fortran () && !is_ada ())
19905 /* The individual enumerators of an enum type get output when we output
19906 the Dwarf representation of the relevant enum type itself. */
19907 break;
19910 /* Emit its type. */
19911 gen_type_die (TREE_TYPE (decl), context_die);
19913 /* And its containing namespace. */
19914 context_die = declare_in_namespace (decl, context_die);
19916 gen_const_die (decl, context_die);
19917 break;
19919 case FUNCTION_DECL:
19920 /* Don't output any DIEs to represent mere function declarations,
19921 unless they are class members or explicit block externs. */
19922 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
19923 && DECL_FILE_SCOPE_P (decl_or_origin)
19924 && (current_function_decl == NULL_TREE
19925 || DECL_ARTIFICIAL (decl_or_origin)))
19926 break;
19928 #if 0
19929 /* FIXME */
19930 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
19931 on local redeclarations of global functions. That seems broken. */
19932 if (current_function_decl != decl)
19933 /* This is only a declaration. */;
19934 #endif
19936 /* If we're emitting a clone, emit info for the abstract instance. */
19937 if (origin || DECL_ORIGIN (decl) != decl)
19938 dwarf2out_abstract_function (origin
19939 ? DECL_ORIGIN (origin)
19940 : DECL_ABSTRACT_ORIGIN (decl));
19942 /* If we're emitting an out-of-line copy of an inline function,
19943 emit info for the abstract instance and set up to refer to it. */
19944 else if (cgraph_function_possibly_inlined_p (decl)
19945 && ! DECL_ABSTRACT (decl)
19946 && ! class_or_namespace_scope_p (context_die)
19947 /* dwarf2out_abstract_function won't emit a die if this is just
19948 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
19949 that case, because that works only if we have a die. */
19950 && DECL_INITIAL (decl) != NULL_TREE)
19952 dwarf2out_abstract_function (decl);
19953 set_decl_origin_self (decl);
19956 /* Otherwise we're emitting the primary DIE for this decl. */
19957 else if (debug_info_level > DINFO_LEVEL_TERSE)
19959 /* Before we describe the FUNCTION_DECL itself, make sure that we
19960 have its containing type. */
19961 if (!origin)
19962 origin = decl_class_context (decl);
19963 if (origin != NULL_TREE)
19964 gen_type_die (origin, context_die);
19966 /* And its return type. */
19967 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
19969 /* And its virtual context. */
19970 if (DECL_VINDEX (decl) != NULL_TREE)
19971 gen_type_die (DECL_CONTEXT (decl), context_die);
19973 /* Make sure we have a member DIE for decl. */
19974 if (origin != NULL_TREE)
19975 gen_type_die_for_member (origin, decl, context_die);
19977 /* And its containing namespace. */
19978 context_die = declare_in_namespace (decl, context_die);
19981 /* Now output a DIE to represent the function itself. */
19982 if (decl)
19983 gen_subprogram_die (decl, context_die);
19984 break;
19986 case TYPE_DECL:
19987 /* If we are in terse mode, don't generate any DIEs to represent any
19988 actual typedefs. */
19989 if (debug_info_level <= DINFO_LEVEL_TERSE)
19990 break;
19992 /* In the special case of a TYPE_DECL node representing the declaration
19993 of some type tag, if the given TYPE_DECL is marked as having been
19994 instantiated from some other (original) TYPE_DECL node (e.g. one which
19995 was generated within the original definition of an inline function) we
19996 used to generate a special (abbreviated) DW_TAG_structure_type,
19997 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
19998 should be actually referencing those DIEs, as variable DIEs with that
19999 type would be emitted already in the abstract origin, so it was always
20000 removed during unused type prunning. Don't add anything in this
20001 case. */
20002 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20003 break;
20005 if (is_redundant_typedef (decl))
20006 gen_type_die (TREE_TYPE (decl), context_die);
20007 else
20008 /* Output a DIE to represent the typedef itself. */
20009 gen_typedef_die (decl, context_die);
20010 break;
20012 case LABEL_DECL:
20013 if (debug_info_level >= DINFO_LEVEL_NORMAL)
20014 gen_label_die (decl, context_die);
20015 break;
20017 case VAR_DECL:
20018 case RESULT_DECL:
20019 /* If we are in terse mode, don't generate any DIEs to represent any
20020 variable declarations or definitions. */
20021 if (debug_info_level <= DINFO_LEVEL_TERSE)
20022 break;
20024 /* Output any DIEs that are needed to specify the type of this data
20025 object. */
20026 if (decl_by_reference_p (decl_or_origin))
20027 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20028 else
20029 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20031 /* And its containing type. */
20032 class_origin = decl_class_context (decl_or_origin);
20033 if (class_origin != NULL_TREE)
20034 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
20036 /* And its containing namespace. */
20037 context_die = declare_in_namespace (decl_or_origin, context_die);
20039 /* Now output the DIE to represent the data object itself. This gets
20040 complicated because of the possibility that the VAR_DECL really
20041 represents an inlined instance of a formal parameter for an inline
20042 function. */
20043 ultimate_origin = decl_ultimate_origin (decl_or_origin);
20044 if (ultimate_origin != NULL_TREE
20045 && TREE_CODE (ultimate_origin) == PARM_DECL)
20046 gen_formal_parameter_die (decl, origin,
20047 true /* Emit name attribute. */,
20048 context_die);
20049 else
20050 gen_variable_die (decl, origin, context_die);
20051 break;
20053 case FIELD_DECL:
20054 /* Ignore the nameless fields that are used to skip bits but handle C++
20055 anonymous unions and structs. */
20056 if (DECL_NAME (decl) != NULL_TREE
20057 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
20058 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
20060 gen_type_die (member_declared_type (decl), context_die);
20061 gen_field_die (decl, context_die);
20063 break;
20065 case PARM_DECL:
20066 if (DECL_BY_REFERENCE (decl_or_origin))
20067 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20068 else
20069 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20070 return gen_formal_parameter_die (decl, origin,
20071 true /* Emit name attribute. */,
20072 context_die);
20074 case NAMESPACE_DECL:
20075 case IMPORTED_DECL:
20076 if (dwarf_version >= 3 || !dwarf_strict)
20077 gen_namespace_die (decl, context_die);
20078 break;
20080 default:
20081 /* Probably some frontend-internal decl. Assume we don't care. */
20082 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
20083 break;
20086 return NULL;
20089 /* Output debug information for global decl DECL. Called from toplev.c after
20090 compilation proper has finished. */
20092 static void
20093 dwarf2out_global_decl (tree decl)
20095 /* Output DWARF2 information for file-scope tentative data object
20096 declarations, file-scope (extern) function declarations (which
20097 had no corresponding body) and file-scope tagged type declarations
20098 and definitions which have not yet been forced out. */
20099 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
20100 dwarf2out_decl (decl);
20103 /* Output debug information for type decl DECL. Called from toplev.c
20104 and from language front ends (to record built-in types). */
20105 static void
20106 dwarf2out_type_decl (tree decl, int local)
20108 if (!local)
20109 dwarf2out_decl (decl);
20112 /* Output debug information for imported module or decl DECL.
20113 NAME is non-NULL name in the lexical block if the decl has been renamed.
20114 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
20115 that DECL belongs to.
20116 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
20117 static void
20118 dwarf2out_imported_module_or_decl_1 (tree decl,
20119 tree name,
20120 tree lexical_block,
20121 dw_die_ref lexical_block_die)
20123 expanded_location xloc;
20124 dw_die_ref imported_die = NULL;
20125 dw_die_ref at_import_die;
20127 if (TREE_CODE (decl) == IMPORTED_DECL)
20129 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
20130 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
20131 gcc_assert (decl);
20133 else
20134 xloc = expand_location (input_location);
20136 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
20138 at_import_die = force_type_die (TREE_TYPE (decl));
20139 /* For namespace N { typedef void T; } using N::T; base_type_die
20140 returns NULL, but DW_TAG_imported_declaration requires
20141 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
20142 if (!at_import_die)
20144 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
20145 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
20146 at_import_die = lookup_type_die (TREE_TYPE (decl));
20147 gcc_assert (at_import_die);
20150 else
20152 at_import_die = lookup_decl_die (decl);
20153 if (!at_import_die)
20155 /* If we're trying to avoid duplicate debug info, we may not have
20156 emitted the member decl for this field. Emit it now. */
20157 if (TREE_CODE (decl) == FIELD_DECL)
20159 tree type = DECL_CONTEXT (decl);
20161 if (TYPE_CONTEXT (type)
20162 && TYPE_P (TYPE_CONTEXT (type))
20163 && !should_emit_struct_debug (TYPE_CONTEXT (type),
20164 DINFO_USAGE_DIR_USE))
20165 return;
20166 gen_type_die_for_member (type, decl,
20167 get_context_die (TYPE_CONTEXT (type)));
20169 at_import_die = force_decl_die (decl);
20173 if (TREE_CODE (decl) == NAMESPACE_DECL)
20175 if (dwarf_version >= 3 || !dwarf_strict)
20176 imported_die = new_die (DW_TAG_imported_module,
20177 lexical_block_die,
20178 lexical_block);
20179 else
20180 return;
20182 else
20183 imported_die = new_die (DW_TAG_imported_declaration,
20184 lexical_block_die,
20185 lexical_block);
20187 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
20188 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
20189 if (name)
20190 add_AT_string (imported_die, DW_AT_name,
20191 IDENTIFIER_POINTER (name));
20192 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
20195 /* Output debug information for imported module or decl DECL.
20196 NAME is non-NULL name in context if the decl has been renamed.
20197 CHILD is true if decl is one of the renamed decls as part of
20198 importing whole module. */
20200 static void
20201 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
20202 bool child)
20204 /* dw_die_ref at_import_die; */
20205 dw_die_ref scope_die;
20207 if (debug_info_level <= DINFO_LEVEL_TERSE)
20208 return;
20210 gcc_assert (decl);
20212 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
20213 We need decl DIE for reference and scope die. First, get DIE for the decl
20214 itself. */
20216 /* Get the scope die for decl context. Use comp_unit_die for global module
20217 or decl. If die is not found for non globals, force new die. */
20218 if (context
20219 && TYPE_P (context)
20220 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
20221 return;
20223 if (!(dwarf_version >= 3 || !dwarf_strict))
20224 return;
20226 scope_die = get_context_die (context);
20228 if (child)
20230 gcc_assert (scope_die->die_child);
20231 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
20232 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
20233 scope_die = scope_die->die_child;
20236 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
20237 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
20241 /* Write the debugging output for DECL. */
20243 void
20244 dwarf2out_decl (tree decl)
20246 dw_die_ref context_die = comp_unit_die ();
20248 switch (TREE_CODE (decl))
20250 case ERROR_MARK:
20251 return;
20253 case FUNCTION_DECL:
20254 /* What we would really like to do here is to filter out all mere
20255 file-scope declarations of file-scope functions which are never
20256 referenced later within this translation unit (and keep all of ones
20257 that *are* referenced later on) but we aren't clairvoyant, so we have
20258 no idea which functions will be referenced in the future (i.e. later
20259 on within the current translation unit). So here we just ignore all
20260 file-scope function declarations which are not also definitions. If
20261 and when the debugger needs to know something about these functions,
20262 it will have to hunt around and find the DWARF information associated
20263 with the definition of the function.
20265 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
20266 nodes represent definitions and which ones represent mere
20267 declarations. We have to check DECL_INITIAL instead. That's because
20268 the C front-end supports some weird semantics for "extern inline"
20269 function definitions. These can get inlined within the current
20270 translation unit (and thus, we need to generate Dwarf info for their
20271 abstract instances so that the Dwarf info for the concrete inlined
20272 instances can have something to refer to) but the compiler never
20273 generates any out-of-lines instances of such things (despite the fact
20274 that they *are* definitions).
20276 The important point is that the C front-end marks these "extern
20277 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
20278 them anyway. Note that the C++ front-end also plays some similar games
20279 for inline function definitions appearing within include files which
20280 also contain `#pragma interface' pragmas.
20282 If we are called from dwarf2out_abstract_function output a DIE
20283 anyway. We can end up here this way with early inlining and LTO
20284 where the inlined function is output in a different LTRANS unit
20285 or not at all. */
20286 if (DECL_INITIAL (decl) == NULL_TREE
20287 && ! DECL_ABSTRACT (decl))
20288 return;
20290 /* If we're a nested function, initially use a parent of NULL; if we're
20291 a plain function, this will be fixed up in decls_for_scope. If
20292 we're a method, it will be ignored, since we already have a DIE. */
20293 if (decl_function_context (decl)
20294 /* But if we're in terse mode, we don't care about scope. */
20295 && debug_info_level > DINFO_LEVEL_TERSE)
20296 context_die = NULL;
20297 break;
20299 case VAR_DECL:
20300 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
20301 declaration and if the declaration was never even referenced from
20302 within this entire compilation unit. We suppress these DIEs in
20303 order to save space in the .debug section (by eliminating entries
20304 which are probably useless). Note that we must not suppress
20305 block-local extern declarations (whether used or not) because that
20306 would screw-up the debugger's name lookup mechanism and cause it to
20307 miss things which really ought to be in scope at a given point. */
20308 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
20309 return;
20311 /* For local statics lookup proper context die. */
20312 if (TREE_STATIC (decl)
20313 && DECL_CONTEXT (decl)
20314 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
20315 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20317 /* If we are in terse mode, don't generate any DIEs to represent any
20318 variable declarations or definitions. */
20319 if (debug_info_level <= DINFO_LEVEL_TERSE)
20320 return;
20321 break;
20323 case CONST_DECL:
20324 if (debug_info_level <= DINFO_LEVEL_TERSE)
20325 return;
20326 if (!is_fortran () && !is_ada ())
20327 return;
20328 if (TREE_STATIC (decl) && decl_function_context (decl))
20329 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20330 break;
20332 case NAMESPACE_DECL:
20333 case IMPORTED_DECL:
20334 if (debug_info_level <= DINFO_LEVEL_TERSE)
20335 return;
20336 if (lookup_decl_die (decl) != NULL)
20337 return;
20338 break;
20340 case TYPE_DECL:
20341 /* Don't emit stubs for types unless they are needed by other DIEs. */
20342 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
20343 return;
20345 /* Don't bother trying to generate any DIEs to represent any of the
20346 normal built-in types for the language we are compiling. */
20347 if (DECL_IS_BUILTIN (decl))
20348 return;
20350 /* If we are in terse mode, don't generate any DIEs for types. */
20351 if (debug_info_level <= DINFO_LEVEL_TERSE)
20352 return;
20354 /* If we're a function-scope tag, initially use a parent of NULL;
20355 this will be fixed up in decls_for_scope. */
20356 if (decl_function_context (decl))
20357 context_die = NULL;
20359 break;
20361 default:
20362 return;
20365 gen_decl_die (decl, NULL, context_die);
20368 /* Write the debugging output for DECL. */
20370 static void
20371 dwarf2out_function_decl (tree decl)
20373 dwarf2out_decl (decl);
20374 call_arg_locations = NULL;
20375 call_arg_loc_last = NULL;
20376 call_site_count = -1;
20377 tail_call_site_count = -1;
20378 block_map.release ();
20379 htab_empty (decl_loc_table);
20380 htab_empty (cached_dw_loc_list_table);
20383 /* Output a marker (i.e. a label) for the beginning of the generated code for
20384 a lexical block. */
20386 static void
20387 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
20388 unsigned int blocknum)
20390 switch_to_section (current_function_section ());
20391 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
20394 /* Output a marker (i.e. a label) for the end of the generated code for a
20395 lexical block. */
20397 static void
20398 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
20400 switch_to_section (current_function_section ());
20401 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
20404 /* Returns nonzero if it is appropriate not to emit any debugging
20405 information for BLOCK, because it doesn't contain any instructions.
20407 Don't allow this for blocks with nested functions or local classes
20408 as we would end up with orphans, and in the presence of scheduling
20409 we may end up calling them anyway. */
20411 static bool
20412 dwarf2out_ignore_block (const_tree block)
20414 tree decl;
20415 unsigned int i;
20417 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
20418 if (TREE_CODE (decl) == FUNCTION_DECL
20419 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20420 return 0;
20421 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
20423 decl = BLOCK_NONLOCALIZED_VAR (block, i);
20424 if (TREE_CODE (decl) == FUNCTION_DECL
20425 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20426 return 0;
20429 return 1;
20432 /* Hash table routines for file_hash. */
20434 static int
20435 file_table_eq (const void *p1_p, const void *p2_p)
20437 const struct dwarf_file_data *const p1 =
20438 (const struct dwarf_file_data *) p1_p;
20439 const char *const p2 = (const char *) p2_p;
20440 return filename_cmp (p1->filename, p2) == 0;
20443 static hashval_t
20444 file_table_hash (const void *p_p)
20446 const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
20447 return htab_hash_string (p->filename);
20450 /* Lookup FILE_NAME (in the list of filenames that we know about here in
20451 dwarf2out.c) and return its "index". The index of each (known) filename is
20452 just a unique number which is associated with only that one filename. We
20453 need such numbers for the sake of generating labels (in the .debug_sfnames
20454 section) and references to those files numbers (in the .debug_srcinfo
20455 and.debug_macinfo sections). If the filename given as an argument is not
20456 found in our current list, add it to the list and assign it the next
20457 available unique index number. In order to speed up searches, we remember
20458 the index of the filename was looked up last. This handles the majority of
20459 all searches. */
20461 static struct dwarf_file_data *
20462 lookup_filename (const char *file_name)
20464 void ** slot;
20465 struct dwarf_file_data * created;
20467 /* Check to see if the file name that was searched on the previous
20468 call matches this file name. If so, return the index. */
20469 if (file_table_last_lookup
20470 && (file_name == file_table_last_lookup->filename
20471 || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
20472 return file_table_last_lookup;
20474 /* Didn't match the previous lookup, search the table. */
20475 slot = htab_find_slot_with_hash (file_table, file_name,
20476 htab_hash_string (file_name), INSERT);
20477 if (*slot)
20478 return (struct dwarf_file_data *) *slot;
20480 created = ggc_alloc_dwarf_file_data ();
20481 created->filename = file_name;
20482 created->emitted_number = 0;
20483 *slot = created;
20484 return created;
20487 /* If the assembler will construct the file table, then translate the compiler
20488 internal file table number into the assembler file table number, and emit
20489 a .file directive if we haven't already emitted one yet. The file table
20490 numbers are different because we prune debug info for unused variables and
20491 types, which may include filenames. */
20493 static int
20494 maybe_emit_file (struct dwarf_file_data * fd)
20496 if (! fd->emitted_number)
20498 if (last_emitted_file)
20499 fd->emitted_number = last_emitted_file->emitted_number + 1;
20500 else
20501 fd->emitted_number = 1;
20502 last_emitted_file = fd;
20504 if (DWARF2_ASM_LINE_DEBUG_INFO)
20506 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
20507 output_quoted_string (asm_out_file,
20508 remap_debug_filename (fd->filename));
20509 fputc ('\n', asm_out_file);
20513 return fd->emitted_number;
20516 /* Schedule generation of a DW_AT_const_value attribute to DIE.
20517 That generation should happen after function debug info has been
20518 generated. The value of the attribute is the constant value of ARG. */
20520 static void
20521 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
20523 die_arg_entry entry;
20525 if (!die || !arg)
20526 return;
20528 if (!tmpl_value_parm_die_table)
20529 vec_alloc (tmpl_value_parm_die_table, 32);
20531 entry.die = die;
20532 entry.arg = arg;
20533 vec_safe_push (tmpl_value_parm_die_table, entry);
20536 /* Return TRUE if T is an instance of generic type, FALSE
20537 otherwise. */
20539 static bool
20540 generic_type_p (tree t)
20542 if (t == NULL_TREE || !TYPE_P (t))
20543 return false;
20544 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
20547 /* Schedule the generation of the generic parameter dies for the
20548 instance of generic type T. The proper generation itself is later
20549 done by gen_scheduled_generic_parms_dies. */
20551 static void
20552 schedule_generic_params_dies_gen (tree t)
20554 if (!generic_type_p (t))
20555 return;
20557 if (!generic_type_instances)
20558 vec_alloc (generic_type_instances, 256);
20560 vec_safe_push (generic_type_instances, t);
20563 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
20564 by append_entry_to_tmpl_value_parm_die_table. This function must
20565 be called after function DIEs have been generated. */
20567 static void
20568 gen_remaining_tmpl_value_param_die_attribute (void)
20570 if (tmpl_value_parm_die_table)
20572 unsigned i;
20573 die_arg_entry *e;
20575 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
20576 tree_add_const_value_attribute (e->die, e->arg);
20580 /* Generate generic parameters DIEs for instances of generic types
20581 that have been previously scheduled by
20582 schedule_generic_params_dies_gen. This function must be called
20583 after all the types of the CU have been laid out. */
20585 static void
20586 gen_scheduled_generic_parms_dies (void)
20588 unsigned i;
20589 tree t;
20591 if (!generic_type_instances)
20592 return;
20594 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
20595 gen_generic_params_dies (t);
20599 /* Replace DW_AT_name for the decl with name. */
20601 static void
20602 dwarf2out_set_name (tree decl, tree name)
20604 dw_die_ref die;
20605 dw_attr_ref attr;
20606 const char *dname;
20608 die = TYPE_SYMTAB_DIE (decl);
20609 if (!die)
20610 return;
20612 dname = dwarf2_name (name, 0);
20613 if (!dname)
20614 return;
20616 attr = get_AT (die, DW_AT_name);
20617 if (attr)
20619 struct indirect_string_node *node;
20621 node = find_AT_string (dname);
20622 /* replace the string. */
20623 attr->dw_attr_val.v.val_str = node;
20626 else
20627 add_name_attribute (die, dname);
20630 /* Called by the final INSN scan whenever we see a var location. We
20631 use it to drop labels in the right places, and throw the location in
20632 our lookup table. */
20634 static void
20635 dwarf2out_var_location (rtx loc_note)
20637 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
20638 struct var_loc_node *newloc;
20639 rtx next_real, next_note;
20640 static const char *last_label;
20641 static const char *last_postcall_label;
20642 static bool last_in_cold_section_p;
20643 static rtx expected_next_loc_note;
20644 tree decl;
20645 bool var_loc_p;
20647 if (!NOTE_P (loc_note))
20649 if (CALL_P (loc_note))
20651 call_site_count++;
20652 if (SIBLING_CALL_P (loc_note))
20653 tail_call_site_count++;
20655 return;
20658 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
20659 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
20660 return;
20662 /* Optimize processing a large consecutive sequence of location
20663 notes so we don't spend too much time in next_real_insn. If the
20664 next insn is another location note, remember the next_real_insn
20665 calculation for next time. */
20666 next_real = cached_next_real_insn;
20667 if (next_real)
20669 if (expected_next_loc_note != loc_note)
20670 next_real = NULL_RTX;
20673 next_note = NEXT_INSN (loc_note);
20674 if (! next_note
20675 || INSN_DELETED_P (next_note)
20676 || GET_CODE (next_note) != NOTE
20677 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
20678 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
20679 next_note = NULL_RTX;
20681 if (! next_real)
20682 next_real = next_real_insn (loc_note);
20684 if (next_note)
20686 expected_next_loc_note = next_note;
20687 cached_next_real_insn = next_real;
20689 else
20690 cached_next_real_insn = NULL_RTX;
20692 /* If there are no instructions which would be affected by this note,
20693 don't do anything. */
20694 if (var_loc_p
20695 && next_real == NULL_RTX
20696 && !NOTE_DURING_CALL_P (loc_note))
20697 return;
20699 if (next_real == NULL_RTX)
20700 next_real = get_last_insn ();
20702 /* If there were any real insns between note we processed last time
20703 and this note (or if it is the first note), clear
20704 last_{,postcall_}label so that they are not reused this time. */
20705 if (last_var_location_insn == NULL_RTX
20706 || last_var_location_insn != next_real
20707 || last_in_cold_section_p != in_cold_section_p)
20709 last_label = NULL;
20710 last_postcall_label = NULL;
20713 if (var_loc_p)
20715 decl = NOTE_VAR_LOCATION_DECL (loc_note);
20716 newloc = add_var_loc_to_decl (decl, loc_note,
20717 NOTE_DURING_CALL_P (loc_note)
20718 ? last_postcall_label : last_label);
20719 if (newloc == NULL)
20720 return;
20722 else
20724 decl = NULL_TREE;
20725 newloc = NULL;
20728 /* If there were no real insns between note we processed last time
20729 and this note, use the label we emitted last time. Otherwise
20730 create a new label and emit it. */
20731 if (last_label == NULL)
20733 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
20734 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
20735 loclabel_num++;
20736 last_label = ggc_strdup (loclabel);
20739 if (!var_loc_p)
20741 struct call_arg_loc_node *ca_loc
20742 = ggc_alloc_cleared_call_arg_loc_node ();
20743 rtx prev = prev_real_insn (loc_note), x;
20744 ca_loc->call_arg_loc_note = loc_note;
20745 ca_loc->next = NULL;
20746 ca_loc->label = last_label;
20747 gcc_assert (prev
20748 && (CALL_P (prev)
20749 || (NONJUMP_INSN_P (prev)
20750 && GET_CODE (PATTERN (prev)) == SEQUENCE
20751 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
20752 if (!CALL_P (prev))
20753 prev = XVECEXP (PATTERN (prev), 0, 0);
20754 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
20755 x = get_call_rtx_from (PATTERN (prev));
20756 if (x)
20758 x = XEXP (XEXP (x, 0), 0);
20759 if (GET_CODE (x) == SYMBOL_REF
20760 && SYMBOL_REF_DECL (x)
20761 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
20762 ca_loc->symbol_ref = x;
20764 ca_loc->block = insn_scope (prev);
20765 if (call_arg_locations)
20766 call_arg_loc_last->next = ca_loc;
20767 else
20768 call_arg_locations = ca_loc;
20769 call_arg_loc_last = ca_loc;
20771 else if (!NOTE_DURING_CALL_P (loc_note))
20772 newloc->label = last_label;
20773 else
20775 if (!last_postcall_label)
20777 sprintf (loclabel, "%s-1", last_label);
20778 last_postcall_label = ggc_strdup (loclabel);
20780 newloc->label = last_postcall_label;
20783 last_var_location_insn = next_real;
20784 last_in_cold_section_p = in_cold_section_p;
20787 /* Note in one location list that text section has changed. */
20789 static int
20790 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
20792 var_loc_list *list = (var_loc_list *) *slot;
20793 if (list->first)
20794 list->last_before_switch
20795 = list->last->next ? list->last->next : list->last;
20796 return 1;
20799 /* Note in all location lists that text section has changed. */
20801 static void
20802 var_location_switch_text_section (void)
20804 if (decl_loc_table == NULL)
20805 return;
20807 htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
20810 /* Create a new line number table. */
20812 static dw_line_info_table *
20813 new_line_info_table (void)
20815 dw_line_info_table *table;
20817 table = ggc_alloc_cleared_dw_line_info_table_struct ();
20818 table->file_num = 1;
20819 table->line_num = 1;
20820 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
20822 return table;
20825 /* Lookup the "current" table into which we emit line info, so
20826 that we don't have to do it for every source line. */
20828 static void
20829 set_cur_line_info_table (section *sec)
20831 dw_line_info_table *table;
20833 if (sec == text_section)
20834 table = text_section_line_info;
20835 else if (sec == cold_text_section)
20837 table = cold_text_section_line_info;
20838 if (!table)
20840 cold_text_section_line_info = table = new_line_info_table ();
20841 table->end_label = cold_end_label;
20844 else
20846 const char *end_label;
20848 if (flag_reorder_blocks_and_partition)
20850 if (in_cold_section_p)
20851 end_label = crtl->subsections.cold_section_end_label;
20852 else
20853 end_label = crtl->subsections.hot_section_end_label;
20855 else
20857 char label[MAX_ARTIFICIAL_LABEL_BYTES];
20858 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
20859 current_function_funcdef_no);
20860 end_label = ggc_strdup (label);
20863 table = new_line_info_table ();
20864 table->end_label = end_label;
20866 vec_safe_push (separate_line_info, table);
20869 if (DWARF2_ASM_LINE_DEBUG_INFO)
20870 table->is_stmt = (cur_line_info_table
20871 ? cur_line_info_table->is_stmt
20872 : DWARF_LINE_DEFAULT_IS_STMT_START);
20873 cur_line_info_table = table;
20877 /* We need to reset the locations at the beginning of each
20878 function. We can't do this in the end_function hook, because the
20879 declarations that use the locations won't have been output when
20880 that hook is called. Also compute have_multiple_function_sections here. */
20882 static void
20883 dwarf2out_begin_function (tree fun)
20885 section *sec = function_section (fun);
20887 if (sec != text_section)
20888 have_multiple_function_sections = true;
20890 if (flag_reorder_blocks_and_partition && !cold_text_section)
20892 gcc_assert (current_function_decl == fun);
20893 cold_text_section = unlikely_text_section ();
20894 switch_to_section (cold_text_section);
20895 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
20896 switch_to_section (sec);
20899 dwarf2out_note_section_used ();
20900 call_site_count = 0;
20901 tail_call_site_count = 0;
20903 set_cur_line_info_table (sec);
20906 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
20908 static void
20909 push_dw_line_info_entry (dw_line_info_table *table,
20910 enum dw_line_info_opcode opcode, unsigned int val)
20912 dw_line_info_entry e;
20913 e.opcode = opcode;
20914 e.val = val;
20915 vec_safe_push (table->entries, e);
20918 /* Output a label to mark the beginning of a source code line entry
20919 and record information relating to this source line, in
20920 'line_info_table' for later output of the .debug_line section. */
20921 /* ??? The discriminator parameter ought to be unsigned. */
20923 static void
20924 dwarf2out_source_line (unsigned int line, const char *filename,
20925 int discriminator, bool is_stmt)
20927 unsigned int file_num;
20928 dw_line_info_table *table;
20930 if (debug_info_level < DINFO_LEVEL_NORMAL || line == 0)
20931 return;
20933 /* The discriminator column was added in dwarf4. Simplify the below
20934 by simply removing it if we're not supposed to output it. */
20935 if (dwarf_version < 4 && dwarf_strict)
20936 discriminator = 0;
20938 table = cur_line_info_table;
20939 file_num = maybe_emit_file (lookup_filename (filename));
20941 /* ??? TODO: Elide duplicate line number entries. Traditionally,
20942 the debugger has used the second (possibly duplicate) line number
20943 at the beginning of the function to mark the end of the prologue.
20944 We could eliminate any other duplicates within the function. For
20945 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
20946 that second line number entry. */
20947 /* Recall that this end-of-prologue indication is *not* the same thing
20948 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
20949 to which the hook corresponds, follows the last insn that was
20950 emitted by gen_prologue. What we need is to precede the first insn
20951 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
20952 insn that corresponds to something the user wrote. These may be
20953 very different locations once scheduling is enabled. */
20955 if (0 && file_num == table->file_num
20956 && line == table->line_num
20957 && discriminator == table->discrim_num
20958 && is_stmt == table->is_stmt)
20959 return;
20961 switch_to_section (current_function_section ());
20963 /* If requested, emit something human-readable. */
20964 if (flag_debug_asm)
20965 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
20967 if (DWARF2_ASM_LINE_DEBUG_INFO)
20969 /* Emit the .loc directive understood by GNU as. */
20970 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
20971 file_num, line, is_stmt, discriminator */
20972 fputs ("\t.loc ", asm_out_file);
20973 fprint_ul (asm_out_file, file_num);
20974 putc (' ', asm_out_file);
20975 fprint_ul (asm_out_file, line);
20976 putc (' ', asm_out_file);
20977 putc ('0', asm_out_file);
20979 if (is_stmt != table->is_stmt)
20981 fputs (" is_stmt ", asm_out_file);
20982 putc (is_stmt ? '1' : '0', asm_out_file);
20984 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
20986 gcc_assert (discriminator > 0);
20987 fputs (" discriminator ", asm_out_file);
20988 fprint_ul (asm_out_file, (unsigned long) discriminator);
20990 putc ('\n', asm_out_file);
20992 else
20994 unsigned int label_num = ++line_info_label_num;
20996 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
20998 push_dw_line_info_entry (table, LI_set_address, label_num);
20999 if (file_num != table->file_num)
21000 push_dw_line_info_entry (table, LI_set_file, file_num);
21001 if (discriminator != table->discrim_num)
21002 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
21003 if (is_stmt != table->is_stmt)
21004 push_dw_line_info_entry (table, LI_negate_stmt, 0);
21005 push_dw_line_info_entry (table, LI_set_line, line);
21008 table->file_num = file_num;
21009 table->line_num = line;
21010 table->discrim_num = discriminator;
21011 table->is_stmt = is_stmt;
21012 table->in_use = true;
21015 /* Record the beginning of a new source file. */
21017 static void
21018 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
21020 if (flag_eliminate_dwarf2_dups)
21022 /* Record the beginning of the file for break_out_includes. */
21023 dw_die_ref bincl_die;
21025 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
21026 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
21029 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21031 macinfo_entry e;
21032 e.code = DW_MACINFO_start_file;
21033 e.lineno = lineno;
21034 e.info = ggc_strdup (filename);
21035 vec_safe_push (macinfo_table, e);
21039 /* Record the end of a source file. */
21041 static void
21042 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
21044 if (flag_eliminate_dwarf2_dups)
21045 /* Record the end of the file for break_out_includes. */
21046 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
21048 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21050 macinfo_entry e;
21051 e.code = DW_MACINFO_end_file;
21052 e.lineno = lineno;
21053 e.info = NULL;
21054 vec_safe_push (macinfo_table, e);
21058 /* Called from debug_define in toplev.c. The `buffer' parameter contains
21059 the tail part of the directive line, i.e. the part which is past the
21060 initial whitespace, #, whitespace, directive-name, whitespace part. */
21062 static void
21063 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
21064 const char *buffer ATTRIBUTE_UNUSED)
21066 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21068 macinfo_entry e;
21069 /* Insert a dummy first entry to be able to optimize the whole
21070 predefined macro block using DW_MACRO_GNU_transparent_include. */
21071 if (macinfo_table->is_empty () && lineno <= 1)
21073 e.code = 0;
21074 e.lineno = 0;
21075 e.info = NULL;
21076 vec_safe_push (macinfo_table, e);
21078 e.code = DW_MACINFO_define;
21079 e.lineno = lineno;
21080 e.info = ggc_strdup (buffer);
21081 vec_safe_push (macinfo_table, e);
21085 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
21086 the tail part of the directive line, i.e. the part which is past the
21087 initial whitespace, #, whitespace, directive-name, whitespace part. */
21089 static void
21090 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
21091 const char *buffer ATTRIBUTE_UNUSED)
21093 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21095 macinfo_entry e;
21096 /* Insert a dummy first entry to be able to optimize the whole
21097 predefined macro block using DW_MACRO_GNU_transparent_include. */
21098 if (macinfo_table->is_empty () && lineno <= 1)
21100 e.code = 0;
21101 e.lineno = 0;
21102 e.info = NULL;
21103 vec_safe_push (macinfo_table, e);
21105 e.code = DW_MACINFO_undef;
21106 e.lineno = lineno;
21107 e.info = ggc_strdup (buffer);
21108 vec_safe_push (macinfo_table, e);
21112 /* Routines to manipulate hash table of CUs. */
21114 static hashval_t
21115 htab_macinfo_hash (const void *of)
21117 const macinfo_entry *const entry =
21118 (const macinfo_entry *) of;
21120 return htab_hash_string (entry->info);
21123 static int
21124 htab_macinfo_eq (const void *of1, const void *of2)
21126 const macinfo_entry *const entry1 = (const macinfo_entry *) of1;
21127 const macinfo_entry *const entry2 = (const macinfo_entry *) of2;
21129 return !strcmp (entry1->info, entry2->info);
21132 /* Output a single .debug_macinfo entry. */
21134 static void
21135 output_macinfo_op (macinfo_entry *ref)
21137 int file_num;
21138 size_t len;
21139 struct indirect_string_node *node;
21140 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21141 struct dwarf_file_data *fd;
21143 switch (ref->code)
21145 case DW_MACINFO_start_file:
21146 fd = lookup_filename (ref->info);
21147 file_num = maybe_emit_file (fd);
21148 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
21149 dw2_asm_output_data_uleb128 (ref->lineno,
21150 "Included from line number %lu",
21151 (unsigned long) ref->lineno);
21152 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
21153 break;
21154 case DW_MACINFO_end_file:
21155 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
21156 break;
21157 case DW_MACINFO_define:
21158 case DW_MACINFO_undef:
21159 len = strlen (ref->info) + 1;
21160 if (!dwarf_strict
21161 && len > DWARF_OFFSET_SIZE
21162 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21163 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21165 ref->code = ref->code == DW_MACINFO_define
21166 ? DW_MACRO_GNU_define_indirect
21167 : DW_MACRO_GNU_undef_indirect;
21168 output_macinfo_op (ref);
21169 return;
21171 dw2_asm_output_data (1, ref->code,
21172 ref->code == DW_MACINFO_define
21173 ? "Define macro" : "Undefine macro");
21174 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21175 (unsigned long) ref->lineno);
21176 dw2_asm_output_nstring (ref->info, -1, "The macro");
21177 break;
21178 case DW_MACRO_GNU_define_indirect:
21179 case DW_MACRO_GNU_undef_indirect:
21180 node = find_AT_string (ref->info);
21181 gcc_assert (node
21182 && ((node->form == DW_FORM_strp)
21183 || (node->form == DW_FORM_GNU_str_index)));
21184 dw2_asm_output_data (1, ref->code,
21185 ref->code == DW_MACRO_GNU_define_indirect
21186 ? "Define macro indirect"
21187 : "Undefine macro indirect");
21188 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21189 (unsigned long) ref->lineno);
21190 if (node->form == DW_FORM_strp)
21191 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
21192 debug_str_section, "The macro: \"%s\"",
21193 ref->info);
21194 else
21195 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
21196 ref->info);
21197 break;
21198 case DW_MACRO_GNU_transparent_include:
21199 dw2_asm_output_data (1, ref->code, "Transparent include");
21200 ASM_GENERATE_INTERNAL_LABEL (label,
21201 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
21202 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
21203 break;
21204 default:
21205 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
21206 ASM_COMMENT_START, (unsigned long) ref->code);
21207 break;
21211 /* Attempt to make a sequence of define/undef macinfo ops shareable with
21212 other compilation unit .debug_macinfo sections. IDX is the first
21213 index of a define/undef, return the number of ops that should be
21214 emitted in a comdat .debug_macinfo section and emit
21215 a DW_MACRO_GNU_transparent_include entry referencing it.
21216 If the define/undef entry should be emitted normally, return 0. */
21218 static unsigned
21219 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
21220 htab_t *macinfo_htab)
21222 macinfo_entry *first, *second, *cur, *inc;
21223 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
21224 unsigned char checksum[16];
21225 struct md5_ctx ctx;
21226 char *grp_name, *tail;
21227 const char *base;
21228 unsigned int i, count, encoded_filename_len, linebuf_len;
21229 void **slot;
21231 first = &(*macinfo_table)[idx];
21232 second = &(*macinfo_table)[idx + 1];
21234 /* Optimize only if there are at least two consecutive define/undef ops,
21235 and either all of them are before first DW_MACINFO_start_file
21236 with lineno {0,1} (i.e. predefined macro block), or all of them are
21237 in some included header file. */
21238 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
21239 return 0;
21240 if (vec_safe_is_empty (files))
21242 if (first->lineno > 1 || second->lineno > 1)
21243 return 0;
21245 else if (first->lineno == 0)
21246 return 0;
21248 /* Find the last define/undef entry that can be grouped together
21249 with first and at the same time compute md5 checksum of their
21250 codes, linenumbers and strings. */
21251 md5_init_ctx (&ctx);
21252 for (i = idx; macinfo_table->iterate (i, &cur); i++)
21253 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
21254 break;
21255 else if (vec_safe_is_empty (files) && cur->lineno > 1)
21256 break;
21257 else
21259 unsigned char code = cur->code;
21260 md5_process_bytes (&code, 1, &ctx);
21261 checksum_uleb128 (cur->lineno, &ctx);
21262 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
21264 md5_finish_ctx (&ctx, checksum);
21265 count = i - idx;
21267 /* From the containing include filename (if any) pick up just
21268 usable characters from its basename. */
21269 if (vec_safe_is_empty (files))
21270 base = "";
21271 else
21272 base = lbasename (files->last ().info);
21273 for (encoded_filename_len = 0, i = 0; base[i]; i++)
21274 if (ISIDNUM (base[i]) || base[i] == '.')
21275 encoded_filename_len++;
21276 /* Count . at the end. */
21277 if (encoded_filename_len)
21278 encoded_filename_len++;
21280 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
21281 linebuf_len = strlen (linebuf);
21283 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
21284 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
21285 + 16 * 2 + 1);
21286 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
21287 tail = grp_name + 4;
21288 if (encoded_filename_len)
21290 for (i = 0; base[i]; i++)
21291 if (ISIDNUM (base[i]) || base[i] == '.')
21292 *tail++ = base[i];
21293 *tail++ = '.';
21295 memcpy (tail, linebuf, linebuf_len);
21296 tail += linebuf_len;
21297 *tail++ = '.';
21298 for (i = 0; i < 16; i++)
21299 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
21301 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
21302 in the empty vector entry before the first define/undef. */
21303 inc = &(*macinfo_table)[idx - 1];
21304 inc->code = DW_MACRO_GNU_transparent_include;
21305 inc->lineno = 0;
21306 inc->info = ggc_strdup (grp_name);
21307 if (*macinfo_htab == NULL)
21308 *macinfo_htab = htab_create (10, htab_macinfo_hash, htab_macinfo_eq, NULL);
21309 /* Avoid emitting duplicates. */
21310 slot = htab_find_slot (*macinfo_htab, inc, INSERT);
21311 if (*slot != NULL)
21313 inc->code = 0;
21314 inc->info = NULL;
21315 /* If such an entry has been used before, just emit
21316 a DW_MACRO_GNU_transparent_include op. */
21317 inc = (macinfo_entry *) *slot;
21318 output_macinfo_op (inc);
21319 /* And clear all macinfo_entry in the range to avoid emitting them
21320 in the second pass. */
21321 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
21323 cur->code = 0;
21324 cur->info = NULL;
21327 else
21329 *slot = inc;
21330 inc->lineno = htab_elements (*macinfo_htab);
21331 output_macinfo_op (inc);
21333 return count;
21336 /* Save any strings needed by the macinfo table in the debug str
21337 table. All strings must be collected into the table by the time
21338 index_string is called. */
21340 static void
21341 save_macinfo_strings (void)
21343 unsigned len;
21344 unsigned i;
21345 macinfo_entry *ref;
21347 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
21349 switch (ref->code)
21351 /* Match the logic in output_macinfo_op to decide on
21352 indirect strings. */
21353 case DW_MACINFO_define:
21354 case DW_MACINFO_undef:
21355 len = strlen (ref->info) + 1;
21356 if (!dwarf_strict
21357 && len > DWARF_OFFSET_SIZE
21358 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21359 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21360 set_indirect_string (find_AT_string (ref->info));
21361 break;
21362 case DW_MACRO_GNU_define_indirect:
21363 case DW_MACRO_GNU_undef_indirect:
21364 set_indirect_string (find_AT_string (ref->info));
21365 break;
21366 default:
21367 break;
21372 /* Output macinfo section(s). */
21374 static void
21375 output_macinfo (void)
21377 unsigned i;
21378 unsigned long length = vec_safe_length (macinfo_table);
21379 macinfo_entry *ref;
21380 vec<macinfo_entry, va_gc> *files = NULL;
21381 htab_t macinfo_htab = NULL;
21383 if (! length)
21384 return;
21386 /* output_macinfo* uses these interchangeably. */
21387 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
21388 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
21389 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
21390 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
21392 /* For .debug_macro emit the section header. */
21393 if (!dwarf_strict)
21395 dw2_asm_output_data (2, 4, "DWARF macro version number");
21396 if (DWARF_OFFSET_SIZE == 8)
21397 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
21398 else
21399 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
21400 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
21401 (!dwarf_split_debug_info ? debug_line_section_label
21402 : debug_skeleton_line_section_label),
21403 debug_line_section, NULL);
21406 /* In the first loop, it emits the primary .debug_macinfo section
21407 and after each emitted op the macinfo_entry is cleared.
21408 If a longer range of define/undef ops can be optimized using
21409 DW_MACRO_GNU_transparent_include, the
21410 DW_MACRO_GNU_transparent_include op is emitted and kept in
21411 the vector before the first define/undef in the range and the
21412 whole range of define/undef ops is not emitted and kept. */
21413 for (i = 0; macinfo_table->iterate (i, &ref); i++)
21415 switch (ref->code)
21417 case DW_MACINFO_start_file:
21418 vec_safe_push (files, *ref);
21419 break;
21420 case DW_MACINFO_end_file:
21421 if (!vec_safe_is_empty (files))
21422 files->pop ();
21423 break;
21424 case DW_MACINFO_define:
21425 case DW_MACINFO_undef:
21426 if (!dwarf_strict
21427 && HAVE_COMDAT_GROUP
21428 && vec_safe_length (files) != 1
21429 && i > 0
21430 && i + 1 < length
21431 && (*macinfo_table)[i - 1].code == 0)
21433 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
21434 if (count)
21436 i += count - 1;
21437 continue;
21440 break;
21441 case 0:
21442 /* A dummy entry may be inserted at the beginning to be able
21443 to optimize the whole block of predefined macros. */
21444 if (i == 0)
21445 continue;
21446 default:
21447 break;
21449 output_macinfo_op (ref);
21450 ref->info = NULL;
21451 ref->code = 0;
21454 if (macinfo_htab == NULL)
21455 return;
21457 htab_delete (macinfo_htab);
21459 /* If any DW_MACRO_GNU_transparent_include were used, on those
21460 DW_MACRO_GNU_transparent_include entries terminate the
21461 current chain and switch to a new comdat .debug_macinfo
21462 section and emit the define/undef entries within it. */
21463 for (i = 0; macinfo_table->iterate (i, &ref); i++)
21464 switch (ref->code)
21466 case 0:
21467 continue;
21468 case DW_MACRO_GNU_transparent_include:
21470 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21471 tree comdat_key = get_identifier (ref->info);
21472 /* Terminate the previous .debug_macinfo section. */
21473 dw2_asm_output_data (1, 0, "End compilation unit");
21474 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
21475 SECTION_DEBUG
21476 | SECTION_LINKONCE,
21477 comdat_key);
21478 ASM_GENERATE_INTERNAL_LABEL (label,
21479 DEBUG_MACRO_SECTION_LABEL,
21480 ref->lineno);
21481 ASM_OUTPUT_LABEL (asm_out_file, label);
21482 ref->code = 0;
21483 ref->info = NULL;
21484 dw2_asm_output_data (2, 4, "DWARF macro version number");
21485 if (DWARF_OFFSET_SIZE == 8)
21486 dw2_asm_output_data (1, 1, "Flags: 64-bit");
21487 else
21488 dw2_asm_output_data (1, 0, "Flags: 32-bit");
21490 break;
21491 case DW_MACINFO_define:
21492 case DW_MACINFO_undef:
21493 output_macinfo_op (ref);
21494 ref->code = 0;
21495 ref->info = NULL;
21496 break;
21497 default:
21498 gcc_unreachable ();
21502 /* Set up for Dwarf output at the start of compilation. */
21504 static void
21505 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
21507 /* Allocate the file_table. */
21508 file_table = htab_create_ggc (50, file_table_hash,
21509 file_table_eq, NULL);
21511 /* Allocate the decl_die_table. */
21512 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
21513 decl_die_table_eq, NULL);
21515 /* Allocate the decl_loc_table. */
21516 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
21517 decl_loc_table_eq, NULL);
21519 /* Allocate the cached_dw_loc_list_table. */
21520 cached_dw_loc_list_table
21521 = htab_create_ggc (10, cached_dw_loc_list_table_hash,
21522 cached_dw_loc_list_table_eq, NULL);
21524 /* Allocate the initial hunk of the decl_scope_table. */
21525 vec_alloc (decl_scope_table, 256);
21527 /* Allocate the initial hunk of the abbrev_die_table. */
21528 abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
21529 (ABBREV_DIE_TABLE_INCREMENT);
21530 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
21531 /* Zero-th entry is allocated, but unused. */
21532 abbrev_die_table_in_use = 1;
21534 /* Allocate the pubtypes and pubnames vectors. */
21535 vec_alloc (pubname_table, 32);
21536 vec_alloc (pubtype_table, 32);
21538 vec_alloc (incomplete_types, 64);
21540 vec_alloc (used_rtx_array, 32);
21542 if (!dwarf_split_debug_info)
21544 debug_info_section = get_section (DEBUG_INFO_SECTION,
21545 SECTION_DEBUG, NULL);
21546 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
21547 SECTION_DEBUG, NULL);
21548 debug_loc_section = get_section (DEBUG_LOC_SECTION,
21549 SECTION_DEBUG, NULL);
21551 else
21553 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
21554 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
21555 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
21556 SECTION_DEBUG | SECTION_EXCLUDE,
21557 NULL);
21558 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
21559 SECTION_DEBUG, NULL);
21560 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
21561 SECTION_DEBUG, NULL);
21562 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
21563 SECTION_DEBUG, NULL);
21564 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
21565 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
21567 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
21568 the main .o, but the skeleton_line goes into the split off dwo. */
21569 debug_skeleton_line_section
21570 = get_section (DEBUG_DWO_LINE_SECTION,
21571 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
21572 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
21573 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
21574 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
21575 SECTION_DEBUG | SECTION_EXCLUDE,
21576 NULL);
21577 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
21578 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
21579 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
21580 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
21582 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
21583 SECTION_DEBUG, NULL);
21584 debug_macinfo_section = get_section (dwarf_strict
21585 ? DEBUG_MACINFO_SECTION
21586 : DEBUG_MACRO_SECTION,
21587 DEBUG_MACRO_SECTION_FLAGS, NULL);
21588 debug_line_section = get_section (DEBUG_LINE_SECTION,
21589 SECTION_DEBUG, NULL);
21590 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
21591 SECTION_DEBUG, NULL);
21592 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
21593 SECTION_DEBUG, NULL);
21594 debug_str_section = get_section (DEBUG_STR_SECTION,
21595 DEBUG_STR_SECTION_FLAGS, NULL);
21596 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
21597 SECTION_DEBUG, NULL);
21598 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
21599 SECTION_DEBUG, NULL);
21601 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
21602 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
21603 DEBUG_ABBREV_SECTION_LABEL, 0);
21604 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
21605 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
21606 COLD_TEXT_SECTION_LABEL, 0);
21607 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
21609 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
21610 DEBUG_INFO_SECTION_LABEL, 0);
21611 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
21612 DEBUG_LINE_SECTION_LABEL, 0);
21613 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
21614 DEBUG_RANGES_SECTION_LABEL, 0);
21615 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
21616 DEBUG_ADDR_SECTION_LABEL, 0);
21617 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
21618 dwarf_strict
21619 ? DEBUG_MACINFO_SECTION_LABEL
21620 : DEBUG_MACRO_SECTION_LABEL, 0);
21621 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
21623 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21624 vec_alloc (macinfo_table, 64);
21626 switch_to_section (text_section);
21627 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
21629 /* Make sure the line number table for .text always exists. */
21630 text_section_line_info = new_line_info_table ();
21631 text_section_line_info->end_label = text_end_label;
21634 /* Called before compile () starts outputtting functions, variables
21635 and toplevel asms into assembly. */
21637 static void
21638 dwarf2out_assembly_start (void)
21640 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
21641 && dwarf2out_do_cfi_asm ()
21642 && (!(flag_unwind_tables || flag_exceptions)
21643 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
21644 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
21647 /* A helper function for dwarf2out_finish called through
21648 htab_traverse. Assign a string its index. All strings must be
21649 collected into the table by the time index_string is called,
21650 because the indexing code relies on htab_traverse to traverse nodes
21651 in the same order for each run. */
21653 static int
21654 index_string (void **h, void *v)
21656 struct indirect_string_node *node = (struct indirect_string_node *) *h;
21657 unsigned int *index = (unsigned int *) v;
21659 find_string_form (node);
21660 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
21662 gcc_assert(node->index == NO_INDEX_ASSIGNED);
21663 node->index = *index;
21664 *index += 1;
21666 return 1;
21669 /* A helper function for output_indirect_strings called through
21670 htab_traverse. Output the offset to a string and update the
21671 current offset. */
21673 static int
21674 output_index_string_offset (void **h, void *v)
21676 struct indirect_string_node *node = (struct indirect_string_node *) *h;
21677 unsigned int *offset = (unsigned int *) v;
21679 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
21681 /* Assert that this node has been assigned an index. */
21682 gcc_assert (node->index != NO_INDEX_ASSIGNED
21683 && node->index != NOT_INDEXED);
21684 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
21685 "indexed string 0x%x: %s", node->index, node->str);
21686 *offset += strlen (node->str) + 1;
21688 return 1;
21691 /* A helper function for dwarf2out_finish called through
21692 htab_traverse. Output the indexed string. */
21694 static int
21695 output_index_string (void **h, void *v)
21697 struct indirect_string_node *node = (struct indirect_string_node *) *h;
21698 unsigned int *cur_idx = (unsigned int *) v;
21700 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
21702 /* Assert that the strings are output in the same order as their
21703 indexes were assigned. */
21704 gcc_assert (*cur_idx == node->index);
21705 ASM_OUTPUT_LABEL (asm_out_file, node->label);
21706 assemble_string (node->str, strlen (node->str) + 1);
21707 *cur_idx += 1;
21709 return 1;
21712 /* A helper function for dwarf2out_finish called through
21713 htab_traverse. Emit one queued .debug_str string. */
21715 static int
21716 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
21718 struct indirect_string_node *node = (struct indirect_string_node *) *h;
21720 if (node->form == DW_FORM_strp && node->refcount > 0)
21722 ASM_OUTPUT_LABEL (asm_out_file, node->label);
21723 assemble_string (node->str, strlen (node->str) + 1);
21726 return 1;
21729 /* Output the indexed string table. */
21731 static void
21732 output_indirect_strings (void)
21734 if (!dwarf_split_debug_info)
21736 switch_to_section (debug_str_section);
21737 htab_traverse (debug_str_hash, output_indirect_string, NULL);
21739 else
21741 unsigned int offset = 0;
21742 unsigned int cur_idx = 0;
21744 switch_to_section (debug_str_offsets_section);
21745 htab_traverse_noresize (debug_str_hash,
21746 output_index_string_offset,
21747 &offset);
21748 switch_to_section (debug_str_section);
21749 htab_traverse_noresize (debug_str_hash,
21750 output_index_string,
21751 &cur_idx);
21755 /* Callback for htab_traverse to assign an index to an entry in the
21756 table, and to write that entry to the .debug_addr section. */
21758 static int
21759 output_addr_table_entry (void **slot, void *data)
21761 addr_table_entry *entry = (addr_table_entry *) *slot;
21762 unsigned int *cur_index = (unsigned int *)data;
21764 if (entry->refcount == 0)
21766 gcc_assert (entry->index == NO_INDEX_ASSIGNED
21767 || entry->index == NOT_INDEXED);
21768 return 1;
21771 gcc_assert (entry->index == *cur_index);
21772 (*cur_index)++;
21774 switch (entry->kind)
21776 case ate_kind_rtx:
21777 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
21778 "0x%x", entry->index);
21779 break;
21780 case ate_kind_rtx_dtprel:
21781 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
21782 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
21783 DWARF2_ADDR_SIZE,
21784 entry->addr.rtl);
21785 fputc ('\n', asm_out_file);
21786 break;
21787 case ate_kind_label:
21788 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
21789 "0x%x", entry->index);
21790 break;
21791 default:
21792 gcc_unreachable ();
21794 return 1;
21797 /* Produce the .debug_addr section. */
21799 static void
21800 output_addr_table (void)
21802 unsigned int index = 0;
21803 if (addr_index_table == NULL || htab_size (addr_index_table) == 0)
21804 return;
21806 switch_to_section (debug_addr_section);
21807 htab_traverse_noresize (addr_index_table, output_addr_table_entry, &index);
21810 #if ENABLE_ASSERT_CHECKING
21811 /* Verify that all marks are clear. */
21813 static void
21814 verify_marks_clear (dw_die_ref die)
21816 dw_die_ref c;
21818 gcc_assert (! die->die_mark);
21819 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
21821 #endif /* ENABLE_ASSERT_CHECKING */
21823 /* Clear the marks for a die and its children.
21824 Be cool if the mark isn't set. */
21826 static void
21827 prune_unmark_dies (dw_die_ref die)
21829 dw_die_ref c;
21831 if (die->die_mark)
21832 die->die_mark = 0;
21833 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
21836 /* Given DIE that we're marking as used, find any other dies
21837 it references as attributes and mark them as used. */
21839 static void
21840 prune_unused_types_walk_attribs (dw_die_ref die)
21842 dw_attr_ref a;
21843 unsigned ix;
21845 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
21847 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
21849 /* A reference to another DIE.
21850 Make sure that it will get emitted.
21851 If it was broken out into a comdat group, don't follow it. */
21852 if (! AT_ref (a)->comdat_type_p
21853 || a->dw_attr == DW_AT_specification)
21854 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
21856 /* Set the string's refcount to 0 so that prune_unused_types_mark
21857 accounts properly for it. */
21858 if (AT_class (a) == dw_val_class_str)
21859 a->dw_attr_val.v.val_str->refcount = 0;
21863 /* Mark the generic parameters and arguments children DIEs of DIE. */
21865 static void
21866 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
21868 dw_die_ref c;
21870 if (die == NULL || die->die_child == NULL)
21871 return;
21872 c = die->die_child;
21875 switch (c->die_tag)
21877 case DW_TAG_template_type_param:
21878 case DW_TAG_template_value_param:
21879 case DW_TAG_GNU_template_template_param:
21880 case DW_TAG_GNU_template_parameter_pack:
21881 prune_unused_types_mark (c, 1);
21882 break;
21883 default:
21884 break;
21886 c = c->die_sib;
21887 } while (c && c != die->die_child);
21890 /* Mark DIE as being used. If DOKIDS is true, then walk down
21891 to DIE's children. */
21893 static void
21894 prune_unused_types_mark (dw_die_ref die, int dokids)
21896 dw_die_ref c;
21898 if (die->die_mark == 0)
21900 /* We haven't done this node yet. Mark it as used. */
21901 die->die_mark = 1;
21902 /* If this is the DIE of a generic type instantiation,
21903 mark the children DIEs that describe its generic parms and
21904 args. */
21905 prune_unused_types_mark_generic_parms_dies (die);
21907 /* We also have to mark its parents as used.
21908 (But we don't want to mark our parents' kids due to this.) */
21909 if (die->die_parent)
21910 prune_unused_types_mark (die->die_parent, 0);
21912 /* Mark any referenced nodes. */
21913 prune_unused_types_walk_attribs (die);
21915 /* If this node is a specification,
21916 also mark the definition, if it exists. */
21917 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
21918 prune_unused_types_mark (die->die_definition, 1);
21921 if (dokids && die->die_mark != 2)
21923 /* We need to walk the children, but haven't done so yet.
21924 Remember that we've walked the kids. */
21925 die->die_mark = 2;
21927 /* If this is an array type, we need to make sure our
21928 kids get marked, even if they're types. If we're
21929 breaking out types into comdat sections, do this
21930 for all type definitions. */
21931 if (die->die_tag == DW_TAG_array_type
21932 || (use_debug_types
21933 && is_type_die (die) && ! is_declaration_die (die)))
21934 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
21935 else
21936 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
21940 /* For local classes, look if any static member functions were emitted
21941 and if so, mark them. */
21943 static void
21944 prune_unused_types_walk_local_classes (dw_die_ref die)
21946 dw_die_ref c;
21948 if (die->die_mark == 2)
21949 return;
21951 switch (die->die_tag)
21953 case DW_TAG_structure_type:
21954 case DW_TAG_union_type:
21955 case DW_TAG_class_type:
21956 break;
21958 case DW_TAG_subprogram:
21959 if (!get_AT_flag (die, DW_AT_declaration)
21960 || die->die_definition != NULL)
21961 prune_unused_types_mark (die, 1);
21962 return;
21964 default:
21965 return;
21968 /* Mark children. */
21969 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
21972 /* Walk the tree DIE and mark types that we actually use. */
21974 static void
21975 prune_unused_types_walk (dw_die_ref die)
21977 dw_die_ref c;
21979 /* Don't do anything if this node is already marked and
21980 children have been marked as well. */
21981 if (die->die_mark == 2)
21982 return;
21984 switch (die->die_tag)
21986 case DW_TAG_structure_type:
21987 case DW_TAG_union_type:
21988 case DW_TAG_class_type:
21989 if (die->die_perennial_p)
21990 break;
21992 for (c = die->die_parent; c; c = c->die_parent)
21993 if (c->die_tag == DW_TAG_subprogram)
21994 break;
21996 /* Finding used static member functions inside of classes
21997 is needed just for local classes, because for other classes
21998 static member function DIEs with DW_AT_specification
21999 are emitted outside of the DW_TAG_*_type. If we ever change
22000 it, we'd need to call this even for non-local classes. */
22001 if (c)
22002 prune_unused_types_walk_local_classes (die);
22004 /* It's a type node --- don't mark it. */
22005 return;
22007 case DW_TAG_const_type:
22008 case DW_TAG_packed_type:
22009 case DW_TAG_pointer_type:
22010 case DW_TAG_reference_type:
22011 case DW_TAG_rvalue_reference_type:
22012 case DW_TAG_volatile_type:
22013 case DW_TAG_typedef:
22014 case DW_TAG_array_type:
22015 case DW_TAG_interface_type:
22016 case DW_TAG_friend:
22017 case DW_TAG_variant_part:
22018 case DW_TAG_enumeration_type:
22019 case DW_TAG_subroutine_type:
22020 case DW_TAG_string_type:
22021 case DW_TAG_set_type:
22022 case DW_TAG_subrange_type:
22023 case DW_TAG_ptr_to_member_type:
22024 case DW_TAG_file_type:
22025 if (die->die_perennial_p)
22026 break;
22028 /* It's a type node --- don't mark it. */
22029 return;
22031 default:
22032 /* Mark everything else. */
22033 break;
22036 if (die->die_mark == 0)
22038 die->die_mark = 1;
22040 /* Now, mark any dies referenced from here. */
22041 prune_unused_types_walk_attribs (die);
22044 die->die_mark = 2;
22046 /* Mark children. */
22047 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22050 /* Increment the string counts on strings referred to from DIE's
22051 attributes. */
22053 static void
22054 prune_unused_types_update_strings (dw_die_ref die)
22056 dw_attr_ref a;
22057 unsigned ix;
22059 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22060 if (AT_class (a) == dw_val_class_str)
22062 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22063 s->refcount++;
22064 /* Avoid unnecessarily putting strings that are used less than
22065 twice in the hash table. */
22066 if (s->refcount
22067 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22069 void ** slot;
22070 slot = htab_find_slot_with_hash (debug_str_hash, s->str,
22071 htab_hash_string (s->str),
22072 INSERT);
22073 gcc_assert (*slot == NULL);
22074 *slot = s;
22079 /* Remove from the tree DIE any dies that aren't marked. */
22081 static void
22082 prune_unused_types_prune (dw_die_ref die)
22084 dw_die_ref c;
22085 int pruned = 0;
22087 gcc_assert (die->die_mark);
22088 prune_unused_types_update_strings (die);
22090 if (! die->die_child)
22091 return;
22093 c = die->die_child;
22094 do {
22095 dw_die_ref prev = c;
22096 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
22097 if (c == die->die_child)
22099 /* No marked children between 'prev' and the end of the list. */
22100 if (prev == c)
22101 /* No marked children at all. */
22102 die->die_child = NULL;
22103 else
22105 prev->die_sib = c->die_sib;
22106 die->die_child = prev;
22108 pruned = 1;
22109 goto finished;
22112 if (c != prev->die_sib)
22114 prev->die_sib = c;
22115 pruned = 1;
22117 prune_unused_types_prune (c);
22118 } while (c != die->die_child);
22120 finished:
22121 /* If we pruned children, and this is a class, mark it as a
22122 declaration to inform debuggers that this is not a complete
22123 class definition. */
22124 if (pruned && die->die_mark == 1 && class_scope_p (die)
22125 && ! is_declaration_die (die))
22126 add_AT_flag (die, DW_AT_declaration, 1);
22129 /* Remove dies representing declarations that we never use. */
22131 static void
22132 prune_unused_types (void)
22134 unsigned int i;
22135 limbo_die_node *node;
22136 comdat_type_node *ctnode;
22137 pubname_ref pub;
22138 dw_die_ref base_type;
22140 #if ENABLE_ASSERT_CHECKING
22141 /* All the marks should already be clear. */
22142 verify_marks_clear (comp_unit_die ());
22143 for (node = limbo_die_list; node; node = node->next)
22144 verify_marks_clear (node->die);
22145 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22146 verify_marks_clear (ctnode->root_die);
22147 #endif /* ENABLE_ASSERT_CHECKING */
22149 /* Mark types that are used in global variables. */
22150 premark_types_used_by_global_vars ();
22152 /* Set the mark on nodes that are actually used. */
22153 prune_unused_types_walk (comp_unit_die ());
22154 for (node = limbo_die_list; node; node = node->next)
22155 prune_unused_types_walk (node->die);
22156 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22158 prune_unused_types_walk (ctnode->root_die);
22159 prune_unused_types_mark (ctnode->type_die, 1);
22162 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
22163 are unusual in that they are pubnames that are the children of pubtypes.
22164 They should only be marked via their parent DW_TAG_enumeration_type die,
22165 not as roots in themselves. */
22166 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
22167 if (pub->die->die_tag != DW_TAG_enumerator)
22168 prune_unused_types_mark (pub->die, 1);
22169 for (i = 0; base_types.iterate (i, &base_type); i++)
22170 prune_unused_types_mark (base_type, 1);
22172 if (debug_str_hash)
22173 htab_empty (debug_str_hash);
22174 prune_unused_types_prune (comp_unit_die ());
22175 for (node = limbo_die_list; node; node = node->next)
22176 prune_unused_types_prune (node->die);
22177 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22178 prune_unused_types_prune (ctnode->root_die);
22180 /* Leave the marks clear. */
22181 prune_unmark_dies (comp_unit_die ());
22182 for (node = limbo_die_list; node; node = node->next)
22183 prune_unmark_dies (node->die);
22184 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22185 prune_unmark_dies (ctnode->root_die);
22188 /* Set the parameter to true if there are any relative pathnames in
22189 the file table. */
22190 static int
22191 file_table_relative_p (void ** slot, void *param)
22193 bool *p = (bool *) param;
22194 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
22195 if (!IS_ABSOLUTE_PATH (d->filename))
22197 *p = true;
22198 return 0;
22200 return 1;
22203 /* Routines to manipulate hash table of comdat type units. */
22205 static hashval_t
22206 htab_ct_hash (const void *of)
22208 hashval_t h;
22209 const comdat_type_node *const type_node = (const comdat_type_node *) of;
22211 memcpy (&h, type_node->signature, sizeof (h));
22212 return h;
22215 static int
22216 htab_ct_eq (const void *of1, const void *of2)
22218 const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
22219 const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
22221 return (! memcmp (type_node_1->signature, type_node_2->signature,
22222 DWARF_TYPE_SIGNATURE_SIZE));
22225 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
22226 to the location it would have been added, should we know its
22227 DECL_ASSEMBLER_NAME when we added other attributes. This will
22228 probably improve compactness of debug info, removing equivalent
22229 abbrevs, and hide any differences caused by deferring the
22230 computation of the assembler name, triggered by e.g. PCH. */
22232 static inline void
22233 move_linkage_attr (dw_die_ref die)
22235 unsigned ix = vec_safe_length (die->die_attr);
22236 dw_attr_node linkage = (*die->die_attr)[ix - 1];
22238 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
22239 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
22241 while (--ix > 0)
22243 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
22245 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
22246 break;
22249 if (ix != vec_safe_length (die->die_attr) - 1)
22251 die->die_attr->pop ();
22252 die->die_attr->quick_insert (ix, linkage);
22256 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
22257 referenced from typed stack ops and count how often they are used. */
22259 static void
22260 mark_base_types (dw_loc_descr_ref loc)
22262 dw_die_ref base_type = NULL;
22264 for (; loc; loc = loc->dw_loc_next)
22266 switch (loc->dw_loc_opc)
22268 case DW_OP_GNU_regval_type:
22269 case DW_OP_GNU_deref_type:
22270 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
22271 break;
22272 case DW_OP_GNU_convert:
22273 case DW_OP_GNU_reinterpret:
22274 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
22275 continue;
22276 /* FALLTHRU */
22277 case DW_OP_GNU_const_type:
22278 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
22279 break;
22280 case DW_OP_GNU_entry_value:
22281 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
22282 continue;
22283 default:
22284 continue;
22286 gcc_assert (base_type->die_parent == comp_unit_die ());
22287 if (base_type->die_mark)
22288 base_type->die_mark++;
22289 else
22291 base_types.safe_push (base_type);
22292 base_type->die_mark = 1;
22297 /* Comparison function for sorting marked base types. */
22299 static int
22300 base_type_cmp (const void *x, const void *y)
22302 dw_die_ref dx = *(const dw_die_ref *) x;
22303 dw_die_ref dy = *(const dw_die_ref *) y;
22304 unsigned int byte_size1, byte_size2;
22305 unsigned int encoding1, encoding2;
22306 if (dx->die_mark > dy->die_mark)
22307 return -1;
22308 if (dx->die_mark < dy->die_mark)
22309 return 1;
22310 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
22311 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
22312 if (byte_size1 < byte_size2)
22313 return 1;
22314 if (byte_size1 > byte_size2)
22315 return -1;
22316 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
22317 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
22318 if (encoding1 < encoding2)
22319 return 1;
22320 if (encoding1 > encoding2)
22321 return -1;
22322 return 0;
22325 /* Move base types marked by mark_base_types as early as possible
22326 in the CU, sorted by decreasing usage count both to make the
22327 uleb128 references as small as possible and to make sure they
22328 will have die_offset already computed by calc_die_sizes when
22329 sizes of typed stack loc ops is computed. */
22331 static void
22332 move_marked_base_types (void)
22334 unsigned int i;
22335 dw_die_ref base_type, die, c;
22337 if (base_types.is_empty ())
22338 return;
22340 /* Sort by decreasing usage count, they will be added again in that
22341 order later on. */
22342 base_types.qsort (base_type_cmp);
22343 die = comp_unit_die ();
22344 c = die->die_child;
22347 dw_die_ref prev = c;
22348 c = c->die_sib;
22349 while (c->die_mark)
22351 remove_child_with_prev (c, prev);
22352 /* As base types got marked, there must be at least
22353 one node other than DW_TAG_base_type. */
22354 gcc_assert (c != c->die_sib);
22355 c = c->die_sib;
22358 while (c != die->die_child);
22359 gcc_assert (die->die_child);
22360 c = die->die_child;
22361 for (i = 0; base_types.iterate (i, &base_type); i++)
22363 base_type->die_mark = 0;
22364 base_type->die_sib = c->die_sib;
22365 c->die_sib = base_type;
22366 c = base_type;
22370 /* Helper function for resolve_addr, attempt to resolve
22371 one CONST_STRING, return non-zero if not successful. Similarly verify that
22372 SYMBOL_REFs refer to variables emitted in the current CU. */
22374 static int
22375 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
22377 rtx rtl = *addr;
22379 if (GET_CODE (rtl) == CONST_STRING)
22381 size_t len = strlen (XSTR (rtl, 0)) + 1;
22382 tree t = build_string (len, XSTR (rtl, 0));
22383 tree tlen = size_int (len - 1);
22384 TREE_TYPE (t)
22385 = build_array_type (char_type_node, build_index_type (tlen));
22386 rtl = lookup_constant_def (t);
22387 if (!rtl || !MEM_P (rtl))
22388 return 1;
22389 rtl = XEXP (rtl, 0);
22390 vec_safe_push (used_rtx_array, rtl);
22391 *addr = rtl;
22392 return 0;
22395 if (GET_CODE (rtl) == SYMBOL_REF
22396 && SYMBOL_REF_DECL (rtl))
22398 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
22400 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
22401 return 1;
22403 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22404 return 1;
22407 if (GET_CODE (rtl) == CONST
22408 && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
22409 return 1;
22411 return 0;
22414 /* Helper function for resolve_addr, handle one location
22415 expression, return false if at least one CONST_STRING or SYMBOL_REF in
22416 the location list couldn't be resolved. */
22418 static bool
22419 resolve_addr_in_expr (dw_loc_descr_ref loc)
22421 dw_loc_descr_ref keep = NULL;
22422 for (; loc; loc = loc->dw_loc_next)
22423 switch (loc->dw_loc_opc)
22425 case DW_OP_addr:
22426 if (resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
22427 return false;
22428 break;
22429 case DW_OP_GNU_addr_index:
22430 case DW_OP_GNU_const_index:
22432 if ((loc->dw_loc_opc == DW_OP_GNU_addr_index
22433 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
22434 && resolve_one_addr (&loc->dw_loc_oprnd1.val_entry->addr.rtl,
22435 NULL))
22436 return false;
22438 break;
22439 case DW_OP_const4u:
22440 case DW_OP_const8u:
22441 if (loc->dtprel
22442 && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
22443 return false;
22444 break;
22445 case DW_OP_plus_uconst:
22446 if (size_of_loc_descr (loc)
22447 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
22449 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
22451 dw_loc_descr_ref repl
22452 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
22453 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
22454 add_loc_descr (&repl, loc->dw_loc_next);
22455 *loc = *repl;
22457 break;
22458 case DW_OP_implicit_value:
22459 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
22460 && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL))
22461 return false;
22462 break;
22463 case DW_OP_GNU_implicit_pointer:
22464 case DW_OP_GNU_parameter_ref:
22465 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
22467 dw_die_ref ref
22468 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
22469 if (ref == NULL)
22470 return false;
22471 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
22472 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
22473 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
22475 break;
22476 case DW_OP_GNU_const_type:
22477 case DW_OP_GNU_regval_type:
22478 case DW_OP_GNU_deref_type:
22479 case DW_OP_GNU_convert:
22480 case DW_OP_GNU_reinterpret:
22481 while (loc->dw_loc_next
22482 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
22484 dw_die_ref base1, base2;
22485 unsigned enc1, enc2, size1, size2;
22486 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
22487 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
22488 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
22489 else if (loc->dw_loc_oprnd1.val_class
22490 == dw_val_class_unsigned_const)
22491 break;
22492 else
22493 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
22494 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
22495 == dw_val_class_unsigned_const)
22496 break;
22497 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
22498 gcc_assert (base1->die_tag == DW_TAG_base_type
22499 && base2->die_tag == DW_TAG_base_type);
22500 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
22501 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
22502 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
22503 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
22504 if (size1 == size2
22505 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
22506 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
22507 && loc != keep)
22508 || enc1 == enc2))
22510 /* Optimize away next DW_OP_GNU_convert after
22511 adjusting LOC's base type die reference. */
22512 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
22513 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
22514 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
22515 else
22516 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
22517 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
22518 continue;
22520 /* Don't change integer DW_OP_GNU_convert after e.g. floating
22521 point typed stack entry. */
22522 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
22523 keep = loc->dw_loc_next;
22524 break;
22526 break;
22527 default:
22528 break;
22530 return true;
22533 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
22534 an address in .rodata section if the string literal is emitted there,
22535 or remove the containing location list or replace DW_AT_const_value
22536 with DW_AT_location and empty location expression, if it isn't found
22537 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
22538 to something that has been emitted in the current CU. */
22540 static void
22541 resolve_addr (dw_die_ref die)
22543 dw_die_ref c;
22544 dw_attr_ref a;
22545 dw_loc_list_ref *curr, *start, loc;
22546 unsigned ix;
22548 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22549 switch (AT_class (a))
22551 case dw_val_class_loc_list:
22552 start = curr = AT_loc_list_ptr (a);
22553 loc = *curr;
22554 gcc_assert (loc);
22555 /* The same list can be referenced more than once. See if we have
22556 already recorded the result from a previous pass. */
22557 if (loc->replaced)
22558 *curr = loc->dw_loc_next;
22559 else if (!loc->resolved_addr)
22561 /* As things stand, we do not expect or allow one die to
22562 reference a suffix of another die's location list chain.
22563 References must be identical or completely separate.
22564 There is therefore no need to cache the result of this
22565 pass on any list other than the first; doing so
22566 would lead to unnecessary writes. */
22567 while (*curr)
22569 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
22570 if (!resolve_addr_in_expr ((*curr)->expr))
22572 dw_loc_list_ref next = (*curr)->dw_loc_next;
22573 dw_loc_descr_ref l = (*curr)->expr;
22575 if (next && (*curr)->ll_symbol)
22577 gcc_assert (!next->ll_symbol);
22578 next->ll_symbol = (*curr)->ll_symbol;
22580 if (dwarf_split_debug_info)
22581 remove_loc_list_addr_table_entries (l);
22582 *curr = next;
22584 else
22586 mark_base_types ((*curr)->expr);
22587 curr = &(*curr)->dw_loc_next;
22590 if (loc == *start)
22591 loc->resolved_addr = 1;
22592 else
22594 loc->replaced = 1;
22595 if (dwarf_split_debug_info)
22596 remove_loc_list_addr_table_entries (loc->expr);
22597 loc->dw_loc_next = *start;
22600 if (!*start)
22602 remove_AT (die, a->dw_attr);
22603 ix--;
22605 break;
22606 case dw_val_class_loc:
22608 dw_loc_descr_ref l = AT_loc (a);
22609 /* For -gdwarf-2 don't attempt to optimize
22610 DW_AT_data_member_location containing
22611 DW_OP_plus_uconst - older consumers might
22612 rely on it being that op instead of a more complex,
22613 but shorter, location description. */
22614 if ((dwarf_version > 2
22615 || a->dw_attr != DW_AT_data_member_location
22616 || l == NULL
22617 || l->dw_loc_opc != DW_OP_plus_uconst
22618 || l->dw_loc_next != NULL)
22619 && !resolve_addr_in_expr (l))
22621 if (dwarf_split_debug_info)
22622 remove_loc_list_addr_table_entries (l);
22623 remove_AT (die, a->dw_attr);
22624 ix--;
22626 else
22627 mark_base_types (l);
22629 break;
22630 case dw_val_class_addr:
22631 if (a->dw_attr == DW_AT_const_value
22632 && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
22634 if (AT_index (a) != NOT_INDEXED)
22635 remove_addr_table_entry (a->dw_attr_val.val_entry);
22636 remove_AT (die, a->dw_attr);
22637 ix--;
22639 if (die->die_tag == DW_TAG_GNU_call_site
22640 && a->dw_attr == DW_AT_abstract_origin)
22642 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
22643 dw_die_ref tdie = lookup_decl_die (tdecl);
22644 if (tdie == NULL
22645 && DECL_EXTERNAL (tdecl)
22646 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
22648 force_decl_die (tdecl);
22649 tdie = lookup_decl_die (tdecl);
22651 if (tdie)
22653 a->dw_attr_val.val_class = dw_val_class_die_ref;
22654 a->dw_attr_val.v.val_die_ref.die = tdie;
22655 a->dw_attr_val.v.val_die_ref.external = 0;
22657 else
22659 if (AT_index (a) != NOT_INDEXED)
22660 remove_addr_table_entry (a->dw_attr_val.val_entry);
22661 remove_AT (die, a->dw_attr);
22662 ix--;
22665 break;
22666 default:
22667 break;
22670 FOR_EACH_CHILD (die, c, resolve_addr (c));
22673 /* Helper routines for optimize_location_lists.
22674 This pass tries to share identical local lists in .debug_loc
22675 section. */
22677 /* Iteratively hash operands of LOC opcode. */
22679 static inline hashval_t
22680 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
22682 dw_val_ref val1 = &loc->dw_loc_oprnd1;
22683 dw_val_ref val2 = &loc->dw_loc_oprnd2;
22685 switch (loc->dw_loc_opc)
22687 case DW_OP_const4u:
22688 case DW_OP_const8u:
22689 if (loc->dtprel)
22690 goto hash_addr;
22691 /* FALLTHRU */
22692 case DW_OP_const1u:
22693 case DW_OP_const1s:
22694 case DW_OP_const2u:
22695 case DW_OP_const2s:
22696 case DW_OP_const4s:
22697 case DW_OP_const8s:
22698 case DW_OP_constu:
22699 case DW_OP_consts:
22700 case DW_OP_pick:
22701 case DW_OP_plus_uconst:
22702 case DW_OP_breg0:
22703 case DW_OP_breg1:
22704 case DW_OP_breg2:
22705 case DW_OP_breg3:
22706 case DW_OP_breg4:
22707 case DW_OP_breg5:
22708 case DW_OP_breg6:
22709 case DW_OP_breg7:
22710 case DW_OP_breg8:
22711 case DW_OP_breg9:
22712 case DW_OP_breg10:
22713 case DW_OP_breg11:
22714 case DW_OP_breg12:
22715 case DW_OP_breg13:
22716 case DW_OP_breg14:
22717 case DW_OP_breg15:
22718 case DW_OP_breg16:
22719 case DW_OP_breg17:
22720 case DW_OP_breg18:
22721 case DW_OP_breg19:
22722 case DW_OP_breg20:
22723 case DW_OP_breg21:
22724 case DW_OP_breg22:
22725 case DW_OP_breg23:
22726 case DW_OP_breg24:
22727 case DW_OP_breg25:
22728 case DW_OP_breg26:
22729 case DW_OP_breg27:
22730 case DW_OP_breg28:
22731 case DW_OP_breg29:
22732 case DW_OP_breg30:
22733 case DW_OP_breg31:
22734 case DW_OP_regx:
22735 case DW_OP_fbreg:
22736 case DW_OP_piece:
22737 case DW_OP_deref_size:
22738 case DW_OP_xderef_size:
22739 hash = iterative_hash_object (val1->v.val_int, hash);
22740 break;
22741 case DW_OP_skip:
22742 case DW_OP_bra:
22744 int offset;
22746 gcc_assert (val1->val_class == dw_val_class_loc);
22747 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
22748 hash = iterative_hash_object (offset, hash);
22750 break;
22751 case DW_OP_implicit_value:
22752 hash = iterative_hash_object (val1->v.val_unsigned, hash);
22753 switch (val2->val_class)
22755 case dw_val_class_const:
22756 hash = iterative_hash_object (val2->v.val_int, hash);
22757 break;
22758 case dw_val_class_vec:
22760 unsigned int elt_size = val2->v.val_vec.elt_size;
22761 unsigned int len = val2->v.val_vec.length;
22763 hash = iterative_hash_object (elt_size, hash);
22764 hash = iterative_hash_object (len, hash);
22765 hash = iterative_hash (val2->v.val_vec.array,
22766 len * elt_size, hash);
22768 break;
22769 case dw_val_class_const_double:
22770 hash = iterative_hash_object (val2->v.val_double.low, hash);
22771 hash = iterative_hash_object (val2->v.val_double.high, hash);
22772 break;
22773 case dw_val_class_addr:
22774 hash = iterative_hash_rtx (val2->v.val_addr, hash);
22775 break;
22776 default:
22777 gcc_unreachable ();
22779 break;
22780 case DW_OP_bregx:
22781 case DW_OP_bit_piece:
22782 hash = iterative_hash_object (val1->v.val_int, hash);
22783 hash = iterative_hash_object (val2->v.val_int, hash);
22784 break;
22785 case DW_OP_addr:
22786 hash_addr:
22787 if (loc->dtprel)
22789 unsigned char dtprel = 0xd1;
22790 hash = iterative_hash_object (dtprel, hash);
22792 hash = iterative_hash_rtx (val1->v.val_addr, hash);
22793 break;
22794 case DW_OP_GNU_addr_index:
22795 case DW_OP_GNU_const_index:
22797 if (loc->dtprel)
22799 unsigned char dtprel = 0xd1;
22800 hash = iterative_hash_object (dtprel, hash);
22802 hash = iterative_hash_rtx (val1->val_entry->addr.rtl, hash);
22804 break;
22805 case DW_OP_GNU_implicit_pointer:
22806 hash = iterative_hash_object (val2->v.val_int, hash);
22807 break;
22808 case DW_OP_GNU_entry_value:
22809 hash = hash_loc_operands (val1->v.val_loc, hash);
22810 break;
22811 case DW_OP_GNU_regval_type:
22812 case DW_OP_GNU_deref_type:
22814 unsigned int byte_size
22815 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
22816 unsigned int encoding
22817 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
22818 hash = iterative_hash_object (val1->v.val_int, hash);
22819 hash = iterative_hash_object (byte_size, hash);
22820 hash = iterative_hash_object (encoding, hash);
22822 break;
22823 case DW_OP_GNU_convert:
22824 case DW_OP_GNU_reinterpret:
22825 if (val1->val_class == dw_val_class_unsigned_const)
22827 hash = iterative_hash_object (val1->v.val_unsigned, hash);
22828 break;
22830 /* FALLTHRU */
22831 case DW_OP_GNU_const_type:
22833 unsigned int byte_size
22834 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
22835 unsigned int encoding
22836 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
22837 hash = iterative_hash_object (byte_size, hash);
22838 hash = iterative_hash_object (encoding, hash);
22839 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
22840 break;
22841 hash = iterative_hash_object (val2->val_class, hash);
22842 switch (val2->val_class)
22844 case dw_val_class_const:
22845 hash = iterative_hash_object (val2->v.val_int, hash);
22846 break;
22847 case dw_val_class_vec:
22849 unsigned int elt_size = val2->v.val_vec.elt_size;
22850 unsigned int len = val2->v.val_vec.length;
22852 hash = iterative_hash_object (elt_size, hash);
22853 hash = iterative_hash_object (len, hash);
22854 hash = iterative_hash (val2->v.val_vec.array,
22855 len * elt_size, hash);
22857 break;
22858 case dw_val_class_const_double:
22859 hash = iterative_hash_object (val2->v.val_double.low, hash);
22860 hash = iterative_hash_object (val2->v.val_double.high, hash);
22861 break;
22862 default:
22863 gcc_unreachable ();
22866 break;
22868 default:
22869 /* Other codes have no operands. */
22870 break;
22872 return hash;
22875 /* Iteratively hash the whole DWARF location expression LOC. */
22877 static inline hashval_t
22878 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
22880 dw_loc_descr_ref l;
22881 bool sizes_computed = false;
22882 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
22883 size_of_locs (loc);
22885 for (l = loc; l != NULL; l = l->dw_loc_next)
22887 enum dwarf_location_atom opc = l->dw_loc_opc;
22888 hash = iterative_hash_object (opc, hash);
22889 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
22891 size_of_locs (loc);
22892 sizes_computed = true;
22894 hash = hash_loc_operands (l, hash);
22896 return hash;
22899 /* Compute hash of the whole location list LIST_HEAD. */
22901 static inline void
22902 hash_loc_list (dw_loc_list_ref list_head)
22904 dw_loc_list_ref curr = list_head;
22905 hashval_t hash = 0;
22907 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
22909 hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
22910 hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
22911 if (curr->section)
22912 hash = iterative_hash (curr->section, strlen (curr->section) + 1,
22913 hash);
22914 hash = hash_locs (curr->expr, hash);
22916 list_head->hash = hash;
22919 /* Return true if X and Y opcodes have the same operands. */
22921 static inline bool
22922 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
22924 dw_val_ref valx1 = &x->dw_loc_oprnd1;
22925 dw_val_ref valx2 = &x->dw_loc_oprnd2;
22926 dw_val_ref valy1 = &y->dw_loc_oprnd1;
22927 dw_val_ref valy2 = &y->dw_loc_oprnd2;
22929 switch (x->dw_loc_opc)
22931 case DW_OP_const4u:
22932 case DW_OP_const8u:
22933 if (x->dtprel)
22934 goto hash_addr;
22935 /* FALLTHRU */
22936 case DW_OP_const1u:
22937 case DW_OP_const1s:
22938 case DW_OP_const2u:
22939 case DW_OP_const2s:
22940 case DW_OP_const4s:
22941 case DW_OP_const8s:
22942 case DW_OP_constu:
22943 case DW_OP_consts:
22944 case DW_OP_pick:
22945 case DW_OP_plus_uconst:
22946 case DW_OP_breg0:
22947 case DW_OP_breg1:
22948 case DW_OP_breg2:
22949 case DW_OP_breg3:
22950 case DW_OP_breg4:
22951 case DW_OP_breg5:
22952 case DW_OP_breg6:
22953 case DW_OP_breg7:
22954 case DW_OP_breg8:
22955 case DW_OP_breg9:
22956 case DW_OP_breg10:
22957 case DW_OP_breg11:
22958 case DW_OP_breg12:
22959 case DW_OP_breg13:
22960 case DW_OP_breg14:
22961 case DW_OP_breg15:
22962 case DW_OP_breg16:
22963 case DW_OP_breg17:
22964 case DW_OP_breg18:
22965 case DW_OP_breg19:
22966 case DW_OP_breg20:
22967 case DW_OP_breg21:
22968 case DW_OP_breg22:
22969 case DW_OP_breg23:
22970 case DW_OP_breg24:
22971 case DW_OP_breg25:
22972 case DW_OP_breg26:
22973 case DW_OP_breg27:
22974 case DW_OP_breg28:
22975 case DW_OP_breg29:
22976 case DW_OP_breg30:
22977 case DW_OP_breg31:
22978 case DW_OP_regx:
22979 case DW_OP_fbreg:
22980 case DW_OP_piece:
22981 case DW_OP_deref_size:
22982 case DW_OP_xderef_size:
22983 return valx1->v.val_int == valy1->v.val_int;
22984 case DW_OP_skip:
22985 case DW_OP_bra:
22986 /* If splitting debug info, the use of DW_OP_GNU_addr_index
22987 can cause irrelevant differences in dw_loc_addr. */
22988 gcc_assert (valx1->val_class == dw_val_class_loc
22989 && valy1->val_class == dw_val_class_loc
22990 && (dwarf_split_debug_info
22991 || x->dw_loc_addr == y->dw_loc_addr));
22992 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
22993 case DW_OP_implicit_value:
22994 if (valx1->v.val_unsigned != valy1->v.val_unsigned
22995 || valx2->val_class != valy2->val_class)
22996 return false;
22997 switch (valx2->val_class)
22999 case dw_val_class_const:
23000 return valx2->v.val_int == valy2->v.val_int;
23001 case dw_val_class_vec:
23002 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23003 && valx2->v.val_vec.length == valy2->v.val_vec.length
23004 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23005 valx2->v.val_vec.elt_size
23006 * valx2->v.val_vec.length) == 0;
23007 case dw_val_class_const_double:
23008 return valx2->v.val_double.low == valy2->v.val_double.low
23009 && valx2->v.val_double.high == valy2->v.val_double.high;
23010 case dw_val_class_addr:
23011 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
23012 default:
23013 gcc_unreachable ();
23015 case DW_OP_bregx:
23016 case DW_OP_bit_piece:
23017 return valx1->v.val_int == valy1->v.val_int
23018 && valx2->v.val_int == valy2->v.val_int;
23019 case DW_OP_addr:
23020 hash_addr:
23021 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
23022 case DW_OP_GNU_addr_index:
23023 case DW_OP_GNU_const_index:
23025 rtx ax1 = valx1->val_entry->addr.rtl;
23026 rtx ay1 = valy1->val_entry->addr.rtl;
23027 return rtx_equal_p (ax1, ay1);
23029 case DW_OP_GNU_implicit_pointer:
23030 return valx1->val_class == dw_val_class_die_ref
23031 && valx1->val_class == valy1->val_class
23032 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
23033 && valx2->v.val_int == valy2->v.val_int;
23034 case DW_OP_GNU_entry_value:
23035 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
23036 case DW_OP_GNU_const_type:
23037 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
23038 || valx2->val_class != valy2->val_class)
23039 return false;
23040 switch (valx2->val_class)
23042 case dw_val_class_const:
23043 return valx2->v.val_int == valy2->v.val_int;
23044 case dw_val_class_vec:
23045 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23046 && valx2->v.val_vec.length == valy2->v.val_vec.length
23047 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23048 valx2->v.val_vec.elt_size
23049 * valx2->v.val_vec.length) == 0;
23050 case dw_val_class_const_double:
23051 return valx2->v.val_double.low == valy2->v.val_double.low
23052 && valx2->v.val_double.high == valy2->v.val_double.high;
23053 default:
23054 gcc_unreachable ();
23056 case DW_OP_GNU_regval_type:
23057 case DW_OP_GNU_deref_type:
23058 return valx1->v.val_int == valy1->v.val_int
23059 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
23060 case DW_OP_GNU_convert:
23061 case DW_OP_GNU_reinterpret:
23062 if (valx1->val_class != valy1->val_class)
23063 return false;
23064 if (valx1->val_class == dw_val_class_unsigned_const)
23065 return valx1->v.val_unsigned == valy1->v.val_unsigned;
23066 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
23067 case DW_OP_GNU_parameter_ref:
23068 return valx1->val_class == dw_val_class_die_ref
23069 && valx1->val_class == valy1->val_class
23070 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
23071 default:
23072 /* Other codes have no operands. */
23073 return true;
23077 /* Return true if DWARF location expressions X and Y are the same. */
23079 static inline bool
23080 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
23082 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
23083 if (x->dw_loc_opc != y->dw_loc_opc
23084 || x->dtprel != y->dtprel
23085 || !compare_loc_operands (x, y))
23086 break;
23087 return x == NULL && y == NULL;
23090 /* Return precomputed hash of location list X. */
23092 static hashval_t
23093 loc_list_hash (const void *x)
23095 return ((const struct dw_loc_list_struct *) x)->hash;
23098 /* Return 1 if location lists X and Y are the same. */
23100 static int
23101 loc_list_eq (const void *x, const void *y)
23103 const struct dw_loc_list_struct *a = (const struct dw_loc_list_struct *) x;
23104 const struct dw_loc_list_struct *b = (const struct dw_loc_list_struct *) y;
23105 if (a == b)
23106 return 1;
23107 if (a->hash != b->hash)
23108 return 0;
23109 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
23110 if (strcmp (a->begin, b->begin) != 0
23111 || strcmp (a->end, b->end) != 0
23112 || (a->section == NULL) != (b->section == NULL)
23113 || (a->section && strcmp (a->section, b->section) != 0)
23114 || !compare_locs (a->expr, b->expr))
23115 break;
23116 return a == NULL && b == NULL;
23119 /* Recursively optimize location lists referenced from DIE
23120 children and share them whenever possible. */
23122 static void
23123 optimize_location_lists_1 (dw_die_ref die, htab_t htab)
23125 dw_die_ref c;
23126 dw_attr_ref a;
23127 unsigned ix;
23128 void **slot;
23130 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23131 if (AT_class (a) == dw_val_class_loc_list)
23133 dw_loc_list_ref list = AT_loc_list (a);
23134 /* TODO: perform some optimizations here, before hashing
23135 it and storing into the hash table. */
23136 hash_loc_list (list);
23137 slot = htab_find_slot_with_hash (htab, list, list->hash,
23138 INSERT);
23139 if (*slot == NULL)
23140 *slot = (void *) list;
23141 else
23142 a->dw_attr_val.v.val_loc_list = (dw_loc_list_ref) *slot;
23145 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
23149 /* Recursively assign each location list a unique index into the debug_addr
23150 section. */
23152 static void
23153 index_location_lists (dw_die_ref die)
23155 dw_die_ref c;
23156 dw_attr_ref a;
23157 unsigned ix;
23159 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23160 if (AT_class (a) == dw_val_class_loc_list)
23162 dw_loc_list_ref list = AT_loc_list (a);
23163 dw_loc_list_ref curr;
23164 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
23166 /* Don't index an entry that has already been indexed
23167 or won't be output. */
23168 if (curr->begin_entry != NULL
23169 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
23170 continue;
23172 curr->begin_entry
23173 = add_addr_table_entry (xstrdup (curr->begin),
23174 ate_kind_label);
23178 FOR_EACH_CHILD (die, c, index_location_lists (c));
23181 /* Optimize location lists referenced from DIE
23182 children and share them whenever possible. */
23184 static void
23185 optimize_location_lists (dw_die_ref die)
23187 htab_t htab = htab_create (500, loc_list_hash, loc_list_eq, NULL);
23188 optimize_location_lists_1 (die, htab);
23189 htab_delete (htab);
23192 /* Output stuff that dwarf requires at the end of every file,
23193 and generate the DWARF-2 debugging info. */
23195 static void
23196 dwarf2out_finish (const char *filename)
23198 limbo_die_node *node, *next_node;
23199 comdat_type_node *ctnode;
23200 htab_t comdat_type_table;
23201 unsigned int i;
23202 dw_die_ref main_comp_unit_die;
23204 /* PCH might result in DW_AT_producer string being restored from the
23205 header compilation, so always fill it with empty string initially
23206 and overwrite only here. */
23207 dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
23208 producer_string = gen_producer_string ();
23209 producer->dw_attr_val.v.val_str->refcount--;
23210 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
23212 gen_scheduled_generic_parms_dies ();
23213 gen_remaining_tmpl_value_param_die_attribute ();
23215 /* Add the name for the main input file now. We delayed this from
23216 dwarf2out_init to avoid complications with PCH. */
23217 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
23218 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
23219 add_comp_dir_attribute (comp_unit_die ());
23220 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
23222 bool p = false;
23223 htab_traverse (file_table, file_table_relative_p, &p);
23224 if (p)
23225 add_comp_dir_attribute (comp_unit_die ());
23228 if (deferred_locations_list)
23229 for (i = 0; i < deferred_locations_list->length (); i++)
23231 add_location_or_const_value_attribute (
23232 (*deferred_locations_list)[i].die,
23233 (*deferred_locations_list)[i].variable,
23234 false,
23235 DW_AT_location);
23238 /* Traverse the limbo die list, and add parent/child links. The only
23239 dies without parents that should be here are concrete instances of
23240 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
23241 For concrete instances, we can get the parent die from the abstract
23242 instance. */
23243 for (node = limbo_die_list; node; node = next_node)
23245 dw_die_ref die = node->die;
23246 next_node = node->next;
23248 if (die->die_parent == NULL)
23250 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
23252 if (origin && origin->die_parent)
23253 add_child_die (origin->die_parent, die);
23254 else if (is_cu_die (die))
23256 else if (seen_error ())
23257 /* It's OK to be confused by errors in the input. */
23258 add_child_die (comp_unit_die (), die);
23259 else
23261 /* In certain situations, the lexical block containing a
23262 nested function can be optimized away, which results
23263 in the nested function die being orphaned. Likewise
23264 with the return type of that nested function. Force
23265 this to be a child of the containing function.
23267 It may happen that even the containing function got fully
23268 inlined and optimized out. In that case we are lost and
23269 assign the empty child. This should not be big issue as
23270 the function is likely unreachable too. */
23271 gcc_assert (node->created_for);
23273 if (DECL_P (node->created_for))
23274 origin = get_context_die (DECL_CONTEXT (node->created_for));
23275 else if (TYPE_P (node->created_for))
23276 origin = scope_die_for (node->created_for, comp_unit_die ());
23277 else
23278 origin = comp_unit_die ();
23280 add_child_die (origin, die);
23285 limbo_die_list = NULL;
23287 #if ENABLE_ASSERT_CHECKING
23289 dw_die_ref die = comp_unit_die (), c;
23290 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
23292 #endif
23293 resolve_addr (comp_unit_die ());
23294 move_marked_base_types ();
23296 for (node = deferred_asm_name; node; node = node->next)
23298 tree decl = node->created_for;
23299 /* When generating LTO bytecode we can not generate new assembler
23300 names at this point and all important decls got theirs via
23301 free-lang-data. */
23302 if ((!flag_generate_lto || DECL_ASSEMBLER_NAME_SET_P (decl))
23303 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
23305 add_linkage_attr (node->die, decl);
23306 move_linkage_attr (node->die);
23310 deferred_asm_name = NULL;
23312 /* Walk through the list of incomplete types again, trying once more to
23313 emit full debugging info for them. */
23314 retry_incomplete_types ();
23316 if (flag_eliminate_unused_debug_types)
23317 prune_unused_types ();
23319 /* Generate separate COMDAT sections for type DIEs. */
23320 if (use_debug_types)
23322 break_out_comdat_types (comp_unit_die ());
23324 /* Each new type_unit DIE was added to the limbo die list when created.
23325 Since these have all been added to comdat_type_list, clear the
23326 limbo die list. */
23327 limbo_die_list = NULL;
23329 /* For each new comdat type unit, copy declarations for incomplete
23330 types to make the new unit self-contained (i.e., no direct
23331 references to the main compile unit). */
23332 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23333 copy_decls_for_unworthy_types (ctnode->root_die);
23334 copy_decls_for_unworthy_types (comp_unit_die ());
23336 /* In the process of copying declarations from one unit to another,
23337 we may have left some declarations behind that are no longer
23338 referenced. Prune them. */
23339 prune_unused_types ();
23342 /* Generate separate CUs for each of the include files we've seen.
23343 They will go into limbo_die_list. */
23344 if (flag_eliminate_dwarf2_dups)
23345 break_out_includes (comp_unit_die ());
23347 /* Traverse the DIE's and add add sibling attributes to those DIE's
23348 that have children. */
23349 add_sibling_attributes (comp_unit_die ());
23350 for (node = limbo_die_list; node; node = node->next)
23351 add_sibling_attributes (node->die);
23352 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23353 add_sibling_attributes (ctnode->root_die);
23355 /* When splitting DWARF info, we put some attributes in the
23356 skeleton compile_unit DIE that remains in the .o, while
23357 most attributes go in the DWO compile_unit_die. */
23358 if (dwarf_split_debug_info)
23359 main_comp_unit_die = gen_compile_unit_die (NULL);
23360 else
23361 main_comp_unit_die = comp_unit_die ();
23363 /* Output a terminator label for the .text section. */
23364 switch_to_section (text_section);
23365 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
23366 if (cold_text_section)
23368 switch_to_section (cold_text_section);
23369 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
23372 /* We can only use the low/high_pc attributes if all of the code was
23373 in .text. */
23374 if (!have_multiple_function_sections
23375 || (dwarf_version < 3 && dwarf_strict))
23377 /* Don't add if the CU has no associated code. */
23378 if (text_section_used)
23379 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
23380 text_end_label, true);
23382 else
23384 unsigned fde_idx;
23385 dw_fde_ref fde;
23386 bool range_list_added = false;
23388 if (text_section_used)
23389 add_ranges_by_labels (main_comp_unit_die, text_section_label,
23390 text_end_label, &range_list_added, true);
23391 if (cold_text_section_used)
23392 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
23393 cold_end_label, &range_list_added, true);
23395 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
23397 if (DECL_IGNORED_P (fde->decl))
23398 continue;
23399 if (!fde->in_std_section)
23400 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
23401 fde->dw_fde_end, &range_list_added,
23402 true);
23403 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
23404 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
23405 fde->dw_fde_second_end, &range_list_added,
23406 true);
23409 if (range_list_added)
23411 /* We need to give .debug_loc and .debug_ranges an appropriate
23412 "base address". Use zero so that these addresses become
23413 absolute. Historically, we've emitted the unexpected
23414 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
23415 Emit both to give time for other tools to adapt. */
23416 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
23417 if (! dwarf_strict && dwarf_version < 4)
23418 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
23420 add_ranges (NULL);
23424 if (debug_info_level >= DINFO_LEVEL_NORMAL)
23425 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
23426 debug_line_section_label);
23428 if (have_macinfo)
23429 add_AT_macptr (comp_unit_die (),
23430 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
23431 macinfo_section_label);
23433 if (dwarf_split_debug_info && addr_index_table != NULL)
23435 /* optimize_location_lists calculates the size of the lists,
23436 so index them first, and assign indices to the entries.
23437 Although optimize_location_lists will remove entries from
23438 the table, it only does so for duplicates, and therefore
23439 only reduces ref_counts to 1. */
23440 unsigned int index = 0;
23441 index_location_lists (comp_unit_die ());
23442 htab_traverse_noresize (addr_index_table,
23443 index_addr_table_entry, &index);
23445 if (have_location_lists)
23446 optimize_location_lists (comp_unit_die ());
23448 save_macinfo_strings ();
23449 if (dwarf_split_debug_info)
23451 unsigned int index = 0;
23452 htab_traverse_noresize (debug_str_hash, index_string, &index);
23455 /* Output all of the compilation units. We put the main one last so that
23456 the offsets are available to output_pubnames. */
23457 for (node = limbo_die_list; node; node = node->next)
23458 output_comp_unit (node->die, 0);
23460 comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
23461 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23463 void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
23465 /* Don't output duplicate types. */
23466 if (*slot != HTAB_EMPTY_ENTRY)
23467 continue;
23469 /* Add a pointer to the line table for the main compilation unit
23470 so that the debugger can make sense of DW_AT_decl_file
23471 attributes. */
23472 if (debug_info_level >= DINFO_LEVEL_NORMAL)
23473 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
23474 (!dwarf_split_debug_info
23475 ? debug_line_section_label
23476 : debug_skeleton_line_section_label));
23478 output_comdat_type_unit (ctnode);
23479 *slot = ctnode;
23481 htab_delete (comdat_type_table);
23483 /* The AT_pubnames attribute needs to go in all skeleton dies, including
23484 both the main_cu and all skeleton TUs. Making this call unconditional
23485 would end up either adding a second copy of the AT_pubnames attribute, or
23486 requiring a special case in add_top_level_skeleton_die_attrs. */
23487 if (!dwarf_split_debug_info)
23488 add_AT_pubnames (comp_unit_die ());
23490 if (dwarf_split_debug_info)
23492 int mark;
23493 unsigned char checksum[16];
23494 struct md5_ctx ctx;
23496 /* Compute a checksum of the comp_unit to use as the dwo_id. */
23497 md5_init_ctx (&ctx);
23498 mark = 0;
23499 die_checksum (comp_unit_die (), &ctx, &mark);
23500 unmark_all_dies (comp_unit_die ());
23501 md5_finish_ctx (&ctx, checksum);
23503 /* Use the first 8 bytes of the checksum as the dwo_id,
23504 and add it to both comp-unit DIEs. */
23505 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
23506 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
23508 /* Add the base offset of the ranges table to the skeleton
23509 comp-unit DIE. */
23510 if (ranges_table_in_use)
23511 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
23512 ranges_section_label);
23514 switch_to_section (debug_addr_section);
23515 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
23516 output_addr_table ();
23519 /* Output the main compilation unit if non-empty or if .debug_macinfo
23520 or .debug_macro will be emitted. */
23521 output_comp_unit (comp_unit_die (), have_macinfo);
23523 if (dwarf_split_debug_info && info_section_emitted)
23524 output_skeleton_debug_sections (main_comp_unit_die);
23526 /* Output the abbreviation table. */
23527 if (abbrev_die_table_in_use != 1)
23529 switch_to_section (debug_abbrev_section);
23530 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
23531 output_abbrev_section ();
23534 /* Output location list section if necessary. */
23535 if (have_location_lists)
23537 /* Output the location lists info. */
23538 switch_to_section (debug_loc_section);
23539 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
23540 output_location_lists (comp_unit_die ());
23543 /* Output public names and types tables if necessary. */
23544 output_pubnames (pubname_table);
23545 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
23546 It shouldn't hurt to emit it always, since pure DWARF2 consumers
23547 simply won't look for the section. */
23548 output_pubnames (pubtype_table);
23550 /* Output the address range information if a CU (.debug_info section)
23551 was emitted. We output an empty table even if we had no functions
23552 to put in it. This because the consumer has no way to tell the
23553 difference between an empty table that we omitted and failure to
23554 generate a table that would have contained data. */
23555 if (info_section_emitted)
23557 unsigned long aranges_length = size_of_aranges ();
23559 switch_to_section (debug_aranges_section);
23560 output_aranges (aranges_length);
23563 /* Output ranges section if necessary. */
23564 if (ranges_table_in_use)
23566 switch_to_section (debug_ranges_section);
23567 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
23568 output_ranges ();
23571 /* Have to end the macro section. */
23572 if (have_macinfo)
23574 switch_to_section (debug_macinfo_section);
23575 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
23576 output_macinfo ();
23577 dw2_asm_output_data (1, 0, "End compilation unit");
23580 /* Output the source line correspondence table. We must do this
23581 even if there is no line information. Otherwise, on an empty
23582 translation unit, we will generate a present, but empty,
23583 .debug_info section. IRIX 6.5 `nm' will then complain when
23584 examining the file. This is done late so that any filenames
23585 used by the debug_info section are marked as 'used'. */
23586 switch_to_section (debug_line_section);
23587 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
23588 if (! DWARF2_ASM_LINE_DEBUG_INFO)
23589 output_line_info (false);
23591 if (dwarf_split_debug_info && info_section_emitted)
23593 switch_to_section (debug_skeleton_line_section);
23594 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
23595 output_line_info (true);
23598 /* If we emitted any indirect strings, output the string table too. */
23599 if (debug_str_hash)
23600 output_indirect_strings ();
23603 #include "gt-dwarf2out.h"