Merge trunk version 195164 into gupc branch.
[official-gcc.git] / gcc / dwarf2out.c
blob22c8733061ccffaa4ca44d20bec7a673f366bab4
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 int is_tagged_type (const_tree);
2900 static const char *dwarf_tag_name (unsigned);
2901 static const char *dwarf_attr_name (unsigned);
2902 static const char *dwarf_form_name (unsigned);
2903 static tree decl_ultimate_origin (const_tree);
2904 static tree decl_class_context (tree);
2905 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
2906 static inline enum dw_val_class AT_class (dw_attr_ref);
2907 static inline unsigned int AT_index (dw_attr_ref);
2908 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
2909 static inline unsigned AT_flag (dw_attr_ref);
2910 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
2911 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
2912 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
2913 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
2914 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
2915 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2916 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
2917 unsigned int, unsigned char *);
2918 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
2919 static hashval_t debug_str_do_hash (const void *);
2920 static int debug_str_eq (const void *, const void *);
2921 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
2922 static inline const char *AT_string (dw_attr_ref);
2923 static enum dwarf_form AT_string_form (dw_attr_ref);
2924 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
2925 static void add_AT_specification (dw_die_ref, dw_die_ref);
2926 static inline dw_die_ref AT_ref (dw_attr_ref);
2927 static inline int AT_ref_external (dw_attr_ref);
2928 static inline void set_AT_ref_external (dw_attr_ref, int);
2929 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
2930 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
2931 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
2932 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
2933 dw_loc_list_ref);
2934 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
2935 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
2936 static void remove_addr_table_entry (addr_table_entry *);
2937 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
2938 static inline rtx AT_addr (dw_attr_ref);
2939 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
2940 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
2941 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
2942 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
2943 unsigned HOST_WIDE_INT);
2944 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
2945 unsigned long, bool);
2946 static inline const char *AT_lbl (dw_attr_ref);
2947 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
2948 static const char *get_AT_low_pc (dw_die_ref);
2949 static const char *get_AT_hi_pc (dw_die_ref);
2950 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
2951 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
2952 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
2953 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
2954 static bool is_cxx (void);
2955 static bool is_fortran (void);
2956 static bool is_ada (void);
2957 static void remove_AT (dw_die_ref, enum dwarf_attribute);
2958 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
2959 static void add_child_die (dw_die_ref, dw_die_ref);
2960 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
2961 static dw_die_ref lookup_type_die (tree);
2962 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
2963 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
2964 static void equate_type_number_to_die (tree, dw_die_ref);
2965 static hashval_t decl_die_table_hash (const void *);
2966 static int decl_die_table_eq (const void *, const void *);
2967 static dw_die_ref lookup_decl_die (tree);
2968 static hashval_t common_block_die_table_hash (const void *);
2969 static int common_block_die_table_eq (const void *, const void *);
2970 static hashval_t decl_loc_table_hash (const void *);
2971 static int decl_loc_table_eq (const void *, const void *);
2972 static var_loc_list *lookup_decl_loc (const_tree);
2973 static void equate_decl_number_to_die (tree, dw_die_ref);
2974 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
2975 static void print_spaces (FILE *);
2976 static void print_die (dw_die_ref, FILE *);
2977 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
2978 static dw_die_ref pop_compile_unit (dw_die_ref);
2979 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
2980 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
2981 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
2982 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
2983 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
2984 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
2985 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
2986 struct md5_ctx *, int *);
2987 struct checksum_attributes;
2988 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
2989 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
2990 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
2991 static void generate_type_signature (dw_die_ref, comdat_type_node *);
2992 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
2993 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
2994 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
2995 static int same_die_p (dw_die_ref, dw_die_ref, int *);
2996 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
2997 static void compute_section_prefix (dw_die_ref);
2998 static int is_type_die (dw_die_ref);
2999 static int is_comdat_die (dw_die_ref);
3000 static int is_symbol_die (dw_die_ref);
3001 static void assign_symbol_names (dw_die_ref);
3002 static void break_out_includes (dw_die_ref);
3003 static int is_declaration_die (dw_die_ref);
3004 static int should_move_die_to_comdat (dw_die_ref);
3005 static dw_die_ref clone_as_declaration (dw_die_ref);
3006 static dw_die_ref clone_die (dw_die_ref);
3007 static dw_die_ref clone_tree (dw_die_ref);
3008 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3009 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3010 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3011 static dw_die_ref generate_skeleton (dw_die_ref);
3012 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3013 dw_die_ref,
3014 dw_die_ref);
3015 static void break_out_comdat_types (dw_die_ref);
3016 static dw_die_ref copy_ancestor_tree (dw_die_ref, dw_die_ref, htab_t);
3017 static void copy_decls_walk (dw_die_ref, dw_die_ref, htab_t);
3018 static void copy_decls_for_unworthy_types (dw_die_ref);
3020 static hashval_t htab_cu_hash (const void *);
3021 static int htab_cu_eq (const void *, const void *);
3022 static void htab_cu_del (void *);
3023 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
3024 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
3025 static void add_sibling_attributes (dw_die_ref);
3026 static void build_abbrev_table (dw_die_ref, htab_t);
3027 static void output_location_lists (dw_die_ref);
3028 static int constant_size (unsigned HOST_WIDE_INT);
3029 static unsigned long size_of_die (dw_die_ref);
3030 static void calc_die_sizes (dw_die_ref);
3031 static void calc_base_type_die_sizes (void);
3032 static void mark_dies (dw_die_ref);
3033 static void unmark_dies (dw_die_ref);
3034 static void unmark_all_dies (dw_die_ref);
3035 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3036 static unsigned long size_of_aranges (void);
3037 static enum dwarf_form value_format (dw_attr_ref);
3038 static void output_value_format (dw_attr_ref);
3039 static void output_abbrev_section (void);
3040 static void output_die_abbrevs (unsigned long, dw_die_ref);
3041 static void output_die_symbol (dw_die_ref);
3042 static void output_die (dw_die_ref);
3043 static void output_compilation_unit_header (void);
3044 static void output_comp_unit (dw_die_ref, int);
3045 static void output_comdat_type_unit (comdat_type_node *);
3046 static const char *dwarf2_name (tree, int);
3047 static void add_pubname (tree, dw_die_ref);
3048 static void add_enumerator_pubname (const char *, dw_die_ref);
3049 static void add_pubname_string (const char *, dw_die_ref);
3050 static void add_pubtype (tree, dw_die_ref);
3051 static void output_pubnames (vec<pubname_entry, va_gc> *);
3052 static void output_aranges (unsigned long);
3053 static unsigned int add_ranges_num (int);
3054 static unsigned int add_ranges (const_tree);
3055 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3056 bool *, bool);
3057 static void output_ranges (void);
3058 static dw_line_info_table *new_line_info_table (void);
3059 static void output_line_info (bool);
3060 static void output_file_names (void);
3061 static dw_die_ref base_type_die (tree);
3062 static int is_base_type (tree);
3063 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3064 static dw_die_ref modified_type_die (tree, int, dw_die_ref);
3065 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3066 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3067 static int type_is_enum (const_tree);
3068 static unsigned int dbx_reg_number (const_rtx);
3069 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3070 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3071 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3072 enum var_init_status);
3073 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3074 enum var_init_status);
3075 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3076 enum var_init_status);
3077 static int is_based_loc (const_rtx);
3078 static int resolve_one_addr (rtx *, void *);
3079 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3080 enum var_init_status);
3081 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
3082 enum var_init_status);
3083 static dw_loc_list_ref loc_list_from_tree (tree, int);
3084 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
3085 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3086 static tree field_type (const_tree);
3087 static unsigned int simple_type_align_in_bits (const_tree);
3088 static unsigned int simple_decl_align_in_bits (const_tree);
3089 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3090 static HOST_WIDE_INT field_byte_offset (const_tree);
3091 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3092 dw_loc_list_ref);
3093 static void add_data_member_location_attribute (dw_die_ref, tree);
3094 static bool add_const_value_attribute (dw_die_ref, rtx);
3095 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3096 static void insert_double (double_int, unsigned char *);
3097 static void insert_float (const_rtx, unsigned char *);
3098 static rtx rtl_for_decl_location (tree);
3099 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3100 enum dwarf_attribute);
3101 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3102 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3103 static void add_name_attribute (dw_die_ref, const char *);
3104 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3105 static void add_comp_dir_attribute (dw_die_ref);
3106 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
3107 static void add_subscript_info (dw_die_ref, tree, bool);
3108 static void add_byte_size_attribute (dw_die_ref, tree);
3109 static void add_bit_offset_attribute (dw_die_ref, tree);
3110 static void add_bit_size_attribute (dw_die_ref, tree);
3111 static void add_prototyped_attribute (dw_die_ref, tree);
3112 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3113 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3114 static void add_src_coords_attributes (dw_die_ref, tree);
3115 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3116 static void push_decl_scope (tree);
3117 static void pop_decl_scope (void);
3118 static dw_die_ref scope_die_for (tree, dw_die_ref);
3119 static inline int local_scope_p (dw_die_ref);
3120 static inline int class_scope_p (dw_die_ref);
3121 static inline int class_or_namespace_scope_p (dw_die_ref);
3122 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
3123 static void add_calling_convention_attribute (dw_die_ref, tree);
3124 static const char *type_tag (const_tree);
3125 static tree member_declared_type (const_tree);
3126 #if 0
3127 static const char *decl_start_label (tree);
3128 #endif
3129 static void gen_array_type_die (tree, dw_die_ref);
3130 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3131 #if 0
3132 static void gen_entry_point_die (tree, dw_die_ref);
3133 #endif
3134 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3135 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3136 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3137 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3138 static void gen_formal_types_die (tree, dw_die_ref);
3139 static void gen_subprogram_die (tree, dw_die_ref);
3140 static void gen_variable_die (tree, tree, dw_die_ref);
3141 static void gen_const_die (tree, dw_die_ref);
3142 static void gen_label_die (tree, dw_die_ref);
3143 static void gen_lexical_block_die (tree, dw_die_ref, int);
3144 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
3145 static void gen_field_die (tree, dw_die_ref);
3146 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3147 static dw_die_ref gen_compile_unit_die (const char *);
3148 static void gen_inheritance_die (tree, tree, dw_die_ref);
3149 static void gen_member_die (tree, dw_die_ref);
3150 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3151 enum debug_info_usage);
3152 static void gen_subroutine_type_die (tree, dw_die_ref);
3153 static void gen_typedef_die (tree, dw_die_ref);
3154 static void gen_type_die (tree, dw_die_ref);
3155 static void gen_block_die (tree, dw_die_ref, int);
3156 static void decls_for_scope (tree, dw_die_ref, int);
3157 static inline int is_redundant_typedef (const_tree);
3158 static bool is_naming_typedef_decl (const_tree);
3159 static inline dw_die_ref get_context_die (tree);
3160 static void gen_namespace_die (tree, dw_die_ref);
3161 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3162 static dw_die_ref force_decl_die (tree);
3163 static dw_die_ref force_type_die (tree);
3164 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3165 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3166 static struct dwarf_file_data * lookup_filename (const char *);
3167 static void retry_incomplete_types (void);
3168 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3169 static void gen_generic_params_dies (tree);
3170 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3171 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3172 static void splice_child_die (dw_die_ref, dw_die_ref);
3173 static int file_info_cmp (const void *, const void *);
3174 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3175 const char *, const char *);
3176 static void output_loc_list (dw_loc_list_ref);
3177 static char *gen_internal_sym (const char *);
3178 static bool want_pubnames (void);
3180 static void prune_unmark_dies (dw_die_ref);
3181 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3182 static void prune_unused_types_mark (dw_die_ref, int);
3183 static void prune_unused_types_walk (dw_die_ref);
3184 static void prune_unused_types_walk_attribs (dw_die_ref);
3185 static void prune_unused_types_prune (dw_die_ref);
3186 static void prune_unused_types (void);
3187 static int maybe_emit_file (struct dwarf_file_data *fd);
3188 static inline const char *AT_vms_delta1 (dw_attr_ref);
3189 static inline const char *AT_vms_delta2 (dw_attr_ref);
3190 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3191 const char *, const char *);
3192 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3193 static void gen_remaining_tmpl_value_param_die_attribute (void);
3194 static bool generic_type_p (tree);
3195 static void schedule_generic_params_dies_gen (tree t);
3196 static void gen_scheduled_generic_parms_dies (void);
3198 /* enum for tracking thread-local variables whose address is really an offset
3199 relative to the TLS pointer, which will need link-time relocation, but will
3200 not need relocation by the DWARF consumer. */
3202 enum dtprel_bool
3204 dtprel_false = 0,
3205 dtprel_true = 1
3208 /* Return the operator to use for an address of a variable. For dtprel_true, we
3209 use DW_OP_const*. For regular variables, which need both link-time
3210 relocation and consumer-level relocation (e.g., to account for shared objects
3211 loaded at a random address), we use DW_OP_addr*. */
3213 static inline enum dwarf_location_atom
3214 dw_addr_op (enum dtprel_bool dtprel)
3216 if (dtprel == dtprel_true)
3217 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3218 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3219 else
3220 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3223 /* Return a pointer to a newly allocated address location description. If
3224 dwarf_split_debug_info is true, then record the address with the appropriate
3225 relocation. */
3226 static inline dw_loc_descr_ref
3227 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3229 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3231 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3232 ref->dw_loc_oprnd1.v.val_addr = addr;
3233 ref->dtprel = dtprel;
3234 if (dwarf_split_debug_info)
3235 ref->dw_loc_oprnd1.val_entry
3236 = add_addr_table_entry (addr,
3237 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3238 else
3239 ref->dw_loc_oprnd1.val_entry = NULL;
3241 return ref;
3244 /* Section names used to hold DWARF debugging information. */
3246 #ifndef DEBUG_INFO_SECTION
3247 #define DEBUG_INFO_SECTION ".debug_info"
3248 #endif
3249 #ifndef DEBUG_DWO_INFO_SECTION
3250 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3251 #endif
3252 #ifndef DEBUG_ABBREV_SECTION
3253 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3254 #endif
3255 #ifndef DEBUG_DWO_ABBREV_SECTION
3256 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3257 #endif
3258 #ifndef DEBUG_ARANGES_SECTION
3259 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3260 #endif
3261 #ifndef DEBUG_ADDR_SECTION
3262 #define DEBUG_ADDR_SECTION ".debug_addr"
3263 #endif
3264 #ifndef DEBUG_NORM_MACINFO_SECTION
3265 #define DEBUG_NORM_MACINFO_SECTION ".debug_macinfo"
3266 #endif
3267 #ifndef DEBUG_DWO_MACINFO_SECTION
3268 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3269 #endif
3270 #ifndef DEBUG_MACINFO_SECTION
3271 #define DEBUG_MACINFO_SECTION \
3272 (!dwarf_split_debug_info \
3273 ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3274 #endif
3275 #ifndef DEBUG_NORM_MACRO_SECTION
3276 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3277 #endif
3278 #ifndef DEBUG_DWO_MACRO_SECTION
3279 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3280 #endif
3281 #ifndef DEBUG_MACRO_SECTION
3282 #define DEBUG_MACRO_SECTION \
3283 (!dwarf_split_debug_info \
3284 ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3285 #endif
3286 #ifndef DEBUG_LINE_SECTION
3287 #define DEBUG_LINE_SECTION ".debug_line"
3288 #endif
3289 #ifndef DEBUG_DWO_LINE_SECTION
3290 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3291 #endif
3292 #ifndef DEBUG_LOC_SECTION
3293 #define DEBUG_LOC_SECTION ".debug_loc"
3294 #endif
3295 #ifndef DEBUG_DWO_LOC_SECTION
3296 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3297 #endif
3298 #ifndef DEBUG_PUBNAMES_SECTION
3299 #define DEBUG_PUBNAMES_SECTION ".debug_pubnames"
3300 #endif
3301 #ifndef DEBUG_PUBTYPES_SECTION
3302 #define DEBUG_PUBTYPES_SECTION ".debug_pubtypes"
3303 #endif
3304 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3305 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3306 #ifndef DEBUG_STR_OFFSETS_SECTION
3307 #define DEBUG_STR_OFFSETS_SECTION \
3308 (!dwarf_split_debug_info \
3309 ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3310 #endif
3311 #define DEBUG_DWO_STR_SECTION ".debug_str.dwo"
3312 #define DEBUG_NORM_STR_SECTION ".debug_str"
3313 #ifndef DEBUG_STR_SECTION
3314 #define DEBUG_STR_SECTION \
3315 (!dwarf_split_debug_info ? (DEBUG_NORM_STR_SECTION) : (DEBUG_DWO_STR_SECTION))
3316 #endif
3317 #ifndef DEBUG_RANGES_SECTION
3318 #define DEBUG_RANGES_SECTION ".debug_ranges"
3319 #endif
3321 /* Standard ELF section names for compiled code and data. */
3322 #ifndef TEXT_SECTION_NAME
3323 #define TEXT_SECTION_NAME ".text"
3324 #endif
3326 /* Section flags for .debug_macinfo/.debug_macro section. */
3327 #define DEBUG_MACRO_SECTION_FLAGS \
3328 (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3330 /* Section flags for .debug_str section. */
3331 #define DEBUG_STR_SECTION_FLAGS \
3332 (dwarf_split_debug_info \
3333 ? SECTION_DEBUG | SECTION_EXCLUDE \
3334 : (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3335 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3336 : SECTION_DEBUG))
3338 /* Labels we insert at beginning sections we can reference instead of
3339 the section names themselves. */
3341 #ifndef TEXT_SECTION_LABEL
3342 #define TEXT_SECTION_LABEL "Ltext"
3343 #endif
3344 #ifndef COLD_TEXT_SECTION_LABEL
3345 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3346 #endif
3347 #ifndef DEBUG_LINE_SECTION_LABEL
3348 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3349 #endif
3350 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3351 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3352 #endif
3353 #ifndef DEBUG_INFO_SECTION_LABEL
3354 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3355 #endif
3356 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3357 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3358 #endif
3359 #ifndef DEBUG_ABBREV_SECTION_LABEL
3360 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3361 #endif
3362 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3363 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3364 #endif
3365 #ifndef DEBUG_ADDR_SECTION_LABEL
3366 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3367 #endif
3368 #ifndef DEBUG_LOC_SECTION_LABEL
3369 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3370 #endif
3371 #ifndef DEBUG_RANGES_SECTION_LABEL
3372 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3373 #endif
3374 #ifndef DEBUG_MACINFO_SECTION_LABEL
3375 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3376 #endif
3377 #ifndef DEBUG_MACRO_SECTION_LABEL
3378 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3379 #endif
3380 #define SKELETON_COMP_DIE_ABBREV 1
3381 #define SKELETON_TYPE_DIE_ABBREV 2
3383 /* Definitions of defaults for formats and names of various special
3384 (artificial) labels which may be generated within this file (when the -g
3385 options is used and DWARF2_DEBUGGING_INFO is in effect.
3386 If necessary, these may be overridden from within the tm.h file, but
3387 typically, overriding these defaults is unnecessary. */
3389 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3390 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3391 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3392 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3393 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3394 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3395 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3396 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3397 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3398 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3399 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3400 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3401 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3402 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3404 #ifndef TEXT_END_LABEL
3405 #define TEXT_END_LABEL "Letext"
3406 #endif
3407 #ifndef COLD_END_LABEL
3408 #define COLD_END_LABEL "Letext_cold"
3409 #endif
3410 #ifndef BLOCK_BEGIN_LABEL
3411 #define BLOCK_BEGIN_LABEL "LBB"
3412 #endif
3413 #ifndef BLOCK_END_LABEL
3414 #define BLOCK_END_LABEL "LBE"
3415 #endif
3416 #ifndef LINE_CODE_LABEL
3417 #define LINE_CODE_LABEL "LM"
3418 #endif
3421 /* Return the root of the DIE's built for the current compilation unit. */
3422 static dw_die_ref
3423 comp_unit_die (void)
3425 if (!single_comp_unit_die)
3426 single_comp_unit_die = gen_compile_unit_die (NULL);
3427 return single_comp_unit_die;
3430 /* We allow a language front-end to designate a function that is to be
3431 called to "demangle" any name before it is put into a DIE. */
3433 static const char *(*demangle_name_func) (const char *);
3435 void
3436 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3438 demangle_name_func = func;
3441 /* Test if rtl node points to a pseudo register. */
3443 static inline int
3444 is_pseudo_reg (const_rtx rtl)
3446 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3447 || (GET_CODE (rtl) == SUBREG
3448 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3451 /* Return a reference to a type, with its const and volatile qualifiers
3452 removed. */
3454 static inline tree
3455 type_main_variant (tree type)
3457 type = TYPE_MAIN_VARIANT (type);
3459 /* ??? There really should be only one main variant among any group of
3460 variants of a given type (and all of the MAIN_VARIANT values for all
3461 members of the group should point to that one type) but sometimes the C
3462 front-end messes this up for array types, so we work around that bug
3463 here. */
3464 if (TREE_CODE (type) == ARRAY_TYPE)
3465 while (type != TYPE_MAIN_VARIANT (type))
3466 type = TYPE_MAIN_VARIANT (type);
3468 return type;
3471 /* Return nonzero if the given type node represents a tagged type. */
3473 static inline int
3474 is_tagged_type (const_tree type)
3476 enum tree_code code = TREE_CODE (type);
3478 return (code == RECORD_TYPE || code == UNION_TYPE
3479 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3482 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3484 static void
3485 get_ref_die_offset_label (char *label, dw_die_ref ref)
3487 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3490 /* Return die_offset of a DIE reference to a base type. */
3492 static unsigned long int
3493 get_base_type_offset (dw_die_ref ref)
3495 if (ref->die_offset)
3496 return ref->die_offset;
3497 if (comp_unit_die ()->die_abbrev)
3499 calc_base_type_die_sizes ();
3500 gcc_assert (ref->die_offset);
3502 return ref->die_offset;
3505 /* Return die_offset of a DIE reference other than base type. */
3507 static unsigned long int
3508 get_ref_die_offset (dw_die_ref ref)
3510 gcc_assert (ref->die_offset);
3511 return ref->die_offset;
3514 /* Convert a DIE tag into its string name. */
3516 static const char *
3517 dwarf_tag_name (unsigned int tag)
3519 const char *name = get_DW_TAG_name (tag);
3521 if (name != NULL)
3522 return name;
3524 return "DW_TAG_<unknown>";
3527 /* Convert a DWARF attribute code into its string name. */
3529 static const char *
3530 dwarf_attr_name (unsigned int attr)
3532 const char *name;
3534 switch (attr)
3536 #if VMS_DEBUGGING_INFO
3537 case DW_AT_HP_prologue:
3538 return "DW_AT_HP_prologue";
3539 #else
3540 case DW_AT_MIPS_loop_unroll_factor:
3541 return "DW_AT_MIPS_loop_unroll_factor";
3542 #endif
3544 #if VMS_DEBUGGING_INFO
3545 case DW_AT_HP_epilogue:
3546 return "DW_AT_HP_epilogue";
3547 #else
3548 case DW_AT_MIPS_stride:
3549 return "DW_AT_MIPS_stride";
3550 #endif
3553 name = get_DW_AT_name (attr);
3555 if (name != NULL)
3556 return name;
3558 return "DW_AT_<unknown>";
3561 /* Convert a DWARF value form code into its string name. */
3563 static const char *
3564 dwarf_form_name (unsigned int form)
3566 const char *name = get_DW_FORM_name (form);
3568 if (name != NULL)
3569 return name;
3571 return "DW_FORM_<unknown>";
3574 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3575 instance of an inlined instance of a decl which is local to an inline
3576 function, so we have to trace all of the way back through the origin chain
3577 to find out what sort of node actually served as the original seed for the
3578 given block. */
3580 static tree
3581 decl_ultimate_origin (const_tree decl)
3583 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3584 return NULL_TREE;
3586 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
3587 nodes in the function to point to themselves; ignore that if
3588 we're trying to output the abstract instance of this function. */
3589 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3590 return NULL_TREE;
3592 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3593 most distant ancestor, this should never happen. */
3594 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3596 return DECL_ABSTRACT_ORIGIN (decl);
3599 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
3600 of a virtual function may refer to a base class, so we check the 'this'
3601 parameter. */
3603 static tree
3604 decl_class_context (tree decl)
3606 tree context = NULL_TREE;
3608 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3609 context = DECL_CONTEXT (decl);
3610 else
3611 context = TYPE_MAIN_VARIANT
3612 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3614 if (context && !TYPE_P (context))
3615 context = NULL_TREE;
3617 return context;
3620 /* Add an attribute/value pair to a DIE. */
3622 static inline void
3623 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
3625 /* Maybe this should be an assert? */
3626 if (die == NULL)
3627 return;
3629 vec_safe_reserve (die->die_attr, 1);
3630 vec_safe_push (die->die_attr, *attr);
3633 static inline enum dw_val_class
3634 AT_class (dw_attr_ref a)
3636 return a->dw_attr_val.val_class;
3639 /* Return the index for any attribute that will be referenced with a
3640 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
3641 are stored in dw_attr_val.v.val_str for reference counting
3642 pruning. */
3644 static inline unsigned int
3645 AT_index (dw_attr_ref a)
3647 if (AT_class (a) == dw_val_class_str)
3648 return a->dw_attr_val.v.val_str->index;
3649 else if (a->dw_attr_val.val_entry != NULL)
3650 return a->dw_attr_val.val_entry->index;
3651 return NOT_INDEXED;
3654 /* Add a flag value attribute to a DIE. */
3656 static inline void
3657 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3659 dw_attr_node attr;
3661 attr.dw_attr = attr_kind;
3662 attr.dw_attr_val.val_class = dw_val_class_flag;
3663 attr.dw_attr_val.val_entry = NULL;
3664 attr.dw_attr_val.v.val_flag = flag;
3665 add_dwarf_attr (die, &attr);
3668 static inline unsigned
3669 AT_flag (dw_attr_ref a)
3671 gcc_assert (a && AT_class (a) == dw_val_class_flag);
3672 return a->dw_attr_val.v.val_flag;
3675 /* Add a signed integer attribute value to a DIE. */
3677 static inline void
3678 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3680 dw_attr_node attr;
3682 attr.dw_attr = attr_kind;
3683 attr.dw_attr_val.val_class = dw_val_class_const;
3684 attr.dw_attr_val.val_entry = NULL;
3685 attr.dw_attr_val.v.val_int = int_val;
3686 add_dwarf_attr (die, &attr);
3689 static inline HOST_WIDE_INT
3690 AT_int (dw_attr_ref a)
3692 gcc_assert (a && AT_class (a) == dw_val_class_const);
3693 return a->dw_attr_val.v.val_int;
3696 /* Add an unsigned integer attribute value to a DIE. */
3698 static inline void
3699 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3700 unsigned HOST_WIDE_INT unsigned_val)
3702 dw_attr_node attr;
3704 attr.dw_attr = attr_kind;
3705 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3706 attr.dw_attr_val.val_entry = NULL;
3707 attr.dw_attr_val.v.val_unsigned = unsigned_val;
3708 add_dwarf_attr (die, &attr);
3711 static inline unsigned HOST_WIDE_INT
3712 AT_unsigned (dw_attr_ref a)
3714 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3715 return a->dw_attr_val.v.val_unsigned;
3718 /* Add an unsigned double integer attribute value to a DIE. */
3720 static inline void
3721 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3722 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3724 dw_attr_node attr;
3726 attr.dw_attr = attr_kind;
3727 attr.dw_attr_val.val_class = dw_val_class_const_double;
3728 attr.dw_attr_val.val_entry = NULL;
3729 attr.dw_attr_val.v.val_double.high = high;
3730 attr.dw_attr_val.v.val_double.low = low;
3731 add_dwarf_attr (die, &attr);
3734 /* Add a floating point attribute value to a DIE and return it. */
3736 static inline void
3737 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3738 unsigned int length, unsigned int elt_size, unsigned char *array)
3740 dw_attr_node attr;
3742 attr.dw_attr = attr_kind;
3743 attr.dw_attr_val.val_class = dw_val_class_vec;
3744 attr.dw_attr_val.val_entry = NULL;
3745 attr.dw_attr_val.v.val_vec.length = length;
3746 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3747 attr.dw_attr_val.v.val_vec.array = array;
3748 add_dwarf_attr (die, &attr);
3751 /* Add an 8-byte data attribute value to a DIE. */
3753 static inline void
3754 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3755 unsigned char data8[8])
3757 dw_attr_node attr;
3759 attr.dw_attr = attr_kind;
3760 attr.dw_attr_val.val_class = dw_val_class_data8;
3761 attr.dw_attr_val.val_entry = NULL;
3762 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3763 add_dwarf_attr (die, &attr);
3766 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
3767 dwarf_split_debug_info, address attributes in dies destined for the
3768 final executable have force_direct set to avoid using indexed
3769 references. */
3771 static inline void
3772 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
3773 bool force_direct)
3775 dw_attr_node attr;
3776 char * lbl_id;
3778 lbl_id = xstrdup (lbl_low);
3779 attr.dw_attr = DW_AT_low_pc;
3780 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3781 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3782 if (dwarf_split_debug_info && !force_direct)
3783 attr.dw_attr_val.val_entry
3784 = add_addr_table_entry (lbl_id, ate_kind_label);
3785 else
3786 attr.dw_attr_val.val_entry = NULL;
3787 add_dwarf_attr (die, &attr);
3789 attr.dw_attr = DW_AT_high_pc;
3790 if (dwarf_version < 4)
3791 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3792 else
3793 attr.dw_attr_val.val_class = dw_val_class_high_pc;
3794 lbl_id = xstrdup (lbl_high);
3795 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3796 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
3797 && dwarf_split_debug_info && !force_direct)
3798 attr.dw_attr_val.val_entry
3799 = add_addr_table_entry (lbl_id, ate_kind_label);
3800 else
3801 attr.dw_attr_val.val_entry = NULL;
3802 add_dwarf_attr (die, &attr);
3805 /* Hash and equality functions for debug_str_hash. */
3807 static hashval_t
3808 debug_str_do_hash (const void *x)
3810 return htab_hash_string (((const struct indirect_string_node *)x)->str);
3813 static int
3814 debug_str_eq (const void *x1, const void *x2)
3816 return strcmp ((((const struct indirect_string_node *)x1)->str),
3817 (const char *)x2) == 0;
3820 /* Add STR to the indirect string hash table. */
3822 static struct indirect_string_node *
3823 find_AT_string (const char *str)
3825 struct indirect_string_node *node;
3826 void **slot;
3828 if (! debug_str_hash)
3829 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
3830 debug_str_eq, NULL);
3832 slot = htab_find_slot_with_hash (debug_str_hash, str,
3833 htab_hash_string (str), INSERT);
3834 if (*slot == NULL)
3836 node = ggc_alloc_cleared_indirect_string_node ();
3837 node->str = ggc_strdup (str);
3838 *slot = node;
3840 else
3841 node = (struct indirect_string_node *) *slot;
3843 node->refcount++;
3844 return node;
3847 /* Add a string attribute value to a DIE. */
3849 static inline void
3850 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
3852 dw_attr_node attr;
3853 struct indirect_string_node *node;
3855 node = find_AT_string (str);
3857 attr.dw_attr = attr_kind;
3858 attr.dw_attr_val.val_class = dw_val_class_str;
3859 attr.dw_attr_val.val_entry = NULL;
3860 attr.dw_attr_val.v.val_str = node;
3861 add_dwarf_attr (die, &attr);
3864 static inline const char *
3865 AT_string (dw_attr_ref a)
3867 gcc_assert (a && AT_class (a) == dw_val_class_str);
3868 return a->dw_attr_val.v.val_str->str;
3871 /* Call this function directly to bypass AT_string_form's logic to put
3872 the string inline in the die. */
3874 static void
3875 set_indirect_string (struct indirect_string_node *node)
3877 char label[32];
3878 /* Already indirect is a no op. */
3879 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
3881 gcc_assert (node->label);
3882 return;
3884 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
3885 ++dw2_string_counter;
3886 node->label = xstrdup (label);
3888 if (!dwarf_split_debug_info)
3890 node->form = DW_FORM_strp;
3891 node->index = NOT_INDEXED;
3893 else
3895 node->form = DW_FORM_GNU_str_index;
3896 node->index = NO_INDEX_ASSIGNED;
3900 /* Find out whether a string should be output inline in DIE
3901 or out-of-line in .debug_str section. */
3903 static enum dwarf_form
3904 find_string_form (struct indirect_string_node *node)
3906 unsigned int len;
3908 if (node->form)
3909 return node->form;
3911 len = strlen (node->str) + 1;
3913 /* If the string is shorter or equal to the size of the reference, it is
3914 always better to put it inline. */
3915 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
3916 return node->form = DW_FORM_string;
3918 /* If we cannot expect the linker to merge strings in .debug_str
3919 section, only put it into .debug_str if it is worth even in this
3920 single module. */
3921 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
3922 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
3923 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
3924 return node->form = DW_FORM_string;
3926 set_indirect_string (node);
3928 return node->form;
3931 /* Find out whether the string referenced from the attribute should be
3932 output inline in DIE or out-of-line in .debug_str section. */
3934 static enum dwarf_form
3935 AT_string_form (dw_attr_ref a)
3937 gcc_assert (a && AT_class (a) == dw_val_class_str);
3938 return find_string_form (a->dw_attr_val.v.val_str);
3941 /* Add a DIE reference attribute value to a DIE. */
3943 static inline void
3944 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
3946 dw_attr_node attr;
3948 #ifdef ENABLE_CHECKING
3949 gcc_assert (targ_die != NULL);
3950 #else
3951 /* With LTO we can end up trying to reference something we didn't create
3952 a DIE for. Avoid crashing later on a NULL referenced DIE. */
3953 if (targ_die == NULL)
3954 return;
3955 #endif
3957 attr.dw_attr = attr_kind;
3958 attr.dw_attr_val.val_class = dw_val_class_die_ref;
3959 attr.dw_attr_val.val_entry = NULL;
3960 attr.dw_attr_val.v.val_die_ref.die = targ_die;
3961 attr.dw_attr_val.v.val_die_ref.external = 0;
3962 add_dwarf_attr (die, &attr);
3965 /* Change DIE reference REF to point to NEW_DIE instead. */
3967 static inline void
3968 change_AT_die_ref (dw_attr_ref ref, dw_die_ref new_die)
3970 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
3971 ref->dw_attr_val.v.val_die_ref.die = new_die;
3972 ref->dw_attr_val.v.val_die_ref.external = 0;
3975 /* Add an AT_specification attribute to a DIE, and also make the back
3976 pointer from the specification to the definition. */
3978 static inline void
3979 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
3981 add_AT_die_ref (die, DW_AT_specification, targ_die);
3982 gcc_assert (!targ_die->die_definition);
3983 targ_die->die_definition = die;
3986 static inline dw_die_ref
3987 AT_ref (dw_attr_ref a)
3989 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
3990 return a->dw_attr_val.v.val_die_ref.die;
3993 static inline int
3994 AT_ref_external (dw_attr_ref a)
3996 if (a && AT_class (a) == dw_val_class_die_ref)
3997 return a->dw_attr_val.v.val_die_ref.external;
3999 return 0;
4002 static inline void
4003 set_AT_ref_external (dw_attr_ref a, int i)
4005 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4006 a->dw_attr_val.v.val_die_ref.external = i;
4009 /* Add an FDE reference attribute value to a DIE. */
4011 static inline void
4012 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4014 dw_attr_node attr;
4016 attr.dw_attr = attr_kind;
4017 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4018 attr.dw_attr_val.val_entry = NULL;
4019 attr.dw_attr_val.v.val_fde_index = targ_fde;
4020 add_dwarf_attr (die, &attr);
4023 /* Add a location description attribute value to a DIE. */
4025 static inline void
4026 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4028 dw_attr_node attr;
4030 attr.dw_attr = attr_kind;
4031 attr.dw_attr_val.val_class = dw_val_class_loc;
4032 attr.dw_attr_val.val_entry = NULL;
4033 attr.dw_attr_val.v.val_loc = loc;
4034 add_dwarf_attr (die, &attr);
4037 static inline dw_loc_descr_ref
4038 AT_loc (dw_attr_ref a)
4040 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4041 return a->dw_attr_val.v.val_loc;
4044 static inline void
4045 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4047 dw_attr_node attr;
4049 attr.dw_attr = attr_kind;
4050 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4051 attr.dw_attr_val.val_entry = NULL;
4052 attr.dw_attr_val.v.val_loc_list = loc_list;
4053 add_dwarf_attr (die, &attr);
4054 have_location_lists = true;
4057 static inline dw_loc_list_ref
4058 AT_loc_list (dw_attr_ref a)
4060 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4061 return a->dw_attr_val.v.val_loc_list;
4064 static inline dw_loc_list_ref *
4065 AT_loc_list_ptr (dw_attr_ref a)
4067 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4068 return &a->dw_attr_val.v.val_loc_list;
4071 /* Table of entries into the .debug_addr section. */
4073 static GTY ((param_is (addr_table_entry))) htab_t addr_index_table;
4075 /* Hash an address_table_entry. */
4077 static hashval_t
4078 addr_table_entry_do_hash (const void *x)
4080 const addr_table_entry *a = (const addr_table_entry *) x;
4081 switch (a->kind)
4083 case ate_kind_rtx:
4084 return iterative_hash_rtx (a->addr.rtl, 0);
4085 case ate_kind_rtx_dtprel:
4086 return iterative_hash_rtx (a->addr.rtl, 1);
4087 case ate_kind_label:
4088 return htab_hash_string (a->addr.label);
4089 default:
4090 gcc_unreachable ();
4094 /* Determine equality for two address_table_entries. */
4096 static int
4097 addr_table_entry_eq (const void *x1, const void *x2)
4099 const addr_table_entry *a1 = (const addr_table_entry *) x1;
4100 const addr_table_entry *a2 = (const addr_table_entry *) x2;
4102 if (a1->kind != a2->kind)
4103 return 0;
4104 switch (a1->kind)
4106 case ate_kind_rtx:
4107 case ate_kind_rtx_dtprel:
4108 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4109 case ate_kind_label:
4110 return strcmp (a1->addr.label, a2->addr.label) == 0;
4111 default:
4112 gcc_unreachable ();
4116 /* Initialize an addr_table_entry. */
4118 void
4119 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4121 e->kind = kind;
4122 switch (kind)
4124 case ate_kind_rtx:
4125 case ate_kind_rtx_dtprel:
4126 e->addr.rtl = (rtx) addr;
4127 break;
4128 case ate_kind_label:
4129 e->addr.label = (char *) addr;
4130 break;
4132 e->refcount = 0;
4133 e->index = NO_INDEX_ASSIGNED;
4136 /* Add attr to the address table entry to the table. Defer setting an
4137 index until output time. */
4139 static addr_table_entry *
4140 add_addr_table_entry (void *addr, enum ate_kind kind)
4142 addr_table_entry *node;
4143 addr_table_entry finder;
4144 void **slot;
4146 gcc_assert (dwarf_split_debug_info);
4147 if (! addr_index_table)
4148 addr_index_table = htab_create_ggc (10, addr_table_entry_do_hash,
4149 addr_table_entry_eq, NULL);
4150 init_addr_table_entry (&finder, kind, addr);
4151 slot = htab_find_slot (addr_index_table, &finder, INSERT);
4153 if (*slot == HTAB_EMPTY_ENTRY)
4155 node = ggc_alloc_cleared_addr_table_entry ();
4156 init_addr_table_entry (node, kind, addr);
4157 *slot = node;
4159 else
4160 node = (addr_table_entry *) *slot;
4162 node->refcount++;
4163 return node;
4166 /* Remove an entry from the addr table by decrementing its refcount.
4167 Strictly, decrementing the refcount would be enough, but the
4168 assertion that the entry is actually in the table has found
4169 bugs. */
4171 static void
4172 remove_addr_table_entry (addr_table_entry *entry)
4174 addr_table_entry *node;
4176 gcc_assert (dwarf_split_debug_info && addr_index_table);
4177 node = (addr_table_entry *) htab_find (addr_index_table, entry);
4178 /* After an index is assigned, the table is frozen. */
4179 gcc_assert (node->refcount > 0 && node->index == NO_INDEX_ASSIGNED);
4180 node->refcount--;
4183 /* Given a location list, remove all addresses it refers to from the
4184 address_table. */
4186 static void
4187 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4189 for (; descr; descr = descr->dw_loc_next)
4190 if (descr->dw_loc_oprnd1.val_entry != NULL)
4192 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4193 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4197 /* A helper function for dwarf2out_finish called through
4198 htab_traverse. Assign an addr_table_entry its index. All entries
4199 must be collected into the table when this function is called,
4200 because the indexing code relies on htab_traverse to traverse nodes
4201 in the same order for each run. */
4203 static int
4204 index_addr_table_entry (void **h, void *v)
4206 addr_table_entry *node = (addr_table_entry *) *h;
4207 unsigned int *index = (unsigned int *) v;
4209 /* Don't index unreferenced nodes. */
4210 if (node->refcount == 0)
4211 return 1;
4213 gcc_assert(node->index == NO_INDEX_ASSIGNED);
4214 node->index = *index;
4215 *index += 1;
4217 return 1;
4220 /* Add an address constant attribute value to a DIE. When using
4221 dwarf_split_debug_info, address attributes in dies destined for the
4222 final executable should be direct references--setting the parameter
4223 force_direct ensures this behavior. */
4225 static inline void
4226 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4227 bool force_direct)
4229 dw_attr_node attr;
4231 attr.dw_attr = attr_kind;
4232 attr.dw_attr_val.val_class = dw_val_class_addr;
4233 attr.dw_attr_val.v.val_addr = addr;
4234 if (dwarf_split_debug_info && !force_direct)
4235 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4236 else
4237 attr.dw_attr_val.val_entry = NULL;
4238 add_dwarf_attr (die, &attr);
4241 /* Get the RTX from to an address DIE attribute. */
4243 static inline rtx
4244 AT_addr (dw_attr_ref a)
4246 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4247 return a->dw_attr_val.v.val_addr;
4250 /* Add a file attribute value to a DIE. */
4252 static inline void
4253 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4254 struct dwarf_file_data *fd)
4256 dw_attr_node attr;
4258 attr.dw_attr = attr_kind;
4259 attr.dw_attr_val.val_class = dw_val_class_file;
4260 attr.dw_attr_val.val_entry = NULL;
4261 attr.dw_attr_val.v.val_file = fd;
4262 add_dwarf_attr (die, &attr);
4265 /* Get the dwarf_file_data from a file DIE attribute. */
4267 static inline struct dwarf_file_data *
4268 AT_file (dw_attr_ref a)
4270 gcc_assert (a && AT_class (a) == dw_val_class_file);
4271 return a->dw_attr_val.v.val_file;
4274 /* Add a vms delta attribute value to a DIE. */
4276 static inline void
4277 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4278 const char *lbl1, const char *lbl2)
4280 dw_attr_node attr;
4282 attr.dw_attr = attr_kind;
4283 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4284 attr.dw_attr_val.val_entry = NULL;
4285 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4286 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4287 add_dwarf_attr (die, &attr);
4290 /* Add a label identifier attribute value to a DIE. */
4292 static inline void
4293 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4294 const char *lbl_id)
4296 dw_attr_node attr;
4298 attr.dw_attr = attr_kind;
4299 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4300 attr.dw_attr_val.val_entry = NULL;
4301 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4302 if (dwarf_split_debug_info)
4303 attr.dw_attr_val.val_entry
4304 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4305 ate_kind_label);
4306 add_dwarf_attr (die, &attr);
4309 /* Add a section offset attribute value to a DIE, an offset into the
4310 debug_line section. */
4312 static inline void
4313 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4314 const char *label)
4316 dw_attr_node attr;
4318 attr.dw_attr = attr_kind;
4319 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4320 attr.dw_attr_val.val_entry = NULL;
4321 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4322 add_dwarf_attr (die, &attr);
4325 /* Add a section offset attribute value to a DIE, an offset into the
4326 debug_macinfo section. */
4328 static inline void
4329 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4330 const char *label)
4332 dw_attr_node attr;
4334 attr.dw_attr = attr_kind;
4335 attr.dw_attr_val.val_class = dw_val_class_macptr;
4336 attr.dw_attr_val.val_entry = NULL;
4337 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4338 add_dwarf_attr (die, &attr);
4341 /* Add an offset attribute value to a DIE. */
4343 static inline void
4344 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4345 unsigned HOST_WIDE_INT offset)
4347 dw_attr_node attr;
4349 attr.dw_attr = attr_kind;
4350 attr.dw_attr_val.val_class = dw_val_class_offset;
4351 attr.dw_attr_val.val_entry = NULL;
4352 attr.dw_attr_val.v.val_offset = offset;
4353 add_dwarf_attr (die, &attr);
4356 /* Add a range_list attribute value to a DIE. When using
4357 dwarf_split_debug_info, address attributes in dies destined for the
4358 final executable should be direct references--setting the parameter
4359 force_direct ensures this behavior. */
4361 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4362 #define RELOCATED_OFFSET (NULL)
4364 static void
4365 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4366 long unsigned int offset, bool force_direct)
4368 dw_attr_node attr;
4370 attr.dw_attr = attr_kind;
4371 attr.dw_attr_val.val_class = dw_val_class_range_list;
4372 /* For the range_list attribute, use val_entry to store whether the
4373 offset should follow split-debug-info or normal semantics. This
4374 value is read in output_range_list_offset. */
4375 if (dwarf_split_debug_info && !force_direct)
4376 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4377 else
4378 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4379 attr.dw_attr_val.v.val_offset = offset;
4380 add_dwarf_attr (die, &attr);
4383 /* Return the start label of a delta attribute. */
4385 static inline const char *
4386 AT_vms_delta1 (dw_attr_ref a)
4388 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4389 return a->dw_attr_val.v.val_vms_delta.lbl1;
4392 /* Return the end label of a delta attribute. */
4394 static inline const char *
4395 AT_vms_delta2 (dw_attr_ref a)
4397 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4398 return a->dw_attr_val.v.val_vms_delta.lbl2;
4401 static inline const char *
4402 AT_lbl (dw_attr_ref a)
4404 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4405 || AT_class (a) == dw_val_class_lineptr
4406 || AT_class (a) == dw_val_class_macptr
4407 || AT_class (a) == dw_val_class_high_pc));
4408 return a->dw_attr_val.v.val_lbl_id;
4411 /* Get the attribute of type attr_kind. */
4413 static dw_attr_ref
4414 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4416 dw_attr_ref a;
4417 unsigned ix;
4418 dw_die_ref spec = NULL;
4420 if (! die)
4421 return NULL;
4423 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4424 if (a->dw_attr == attr_kind)
4425 return a;
4426 else if (a->dw_attr == DW_AT_specification
4427 || a->dw_attr == DW_AT_abstract_origin)
4428 spec = AT_ref (a);
4430 if (spec)
4431 return get_AT (spec, attr_kind);
4433 return NULL;
4436 /* Returns the parent of the declaration of DIE. */
4438 static dw_die_ref
4439 get_die_parent (dw_die_ref die)
4441 dw_die_ref t;
4443 if (!die)
4444 return NULL;
4446 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4447 || (t = get_AT_ref (die, DW_AT_specification)))
4448 die = t;
4450 return die->die_parent;
4453 /* Return the "low pc" attribute value, typically associated with a subprogram
4454 DIE. Return null if the "low pc" attribute is either not present, or if it
4455 cannot be represented as an assembler label identifier. */
4457 static inline const char *
4458 get_AT_low_pc (dw_die_ref die)
4460 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4462 return a ? AT_lbl (a) : NULL;
4465 /* Return the "high pc" attribute value, typically associated with a subprogram
4466 DIE. Return null if the "high pc" attribute is either not present, or if it
4467 cannot be represented as an assembler label identifier. */
4469 static inline const char *
4470 get_AT_hi_pc (dw_die_ref die)
4472 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4474 return a ? AT_lbl (a) : NULL;
4477 /* Return the value of the string attribute designated by ATTR_KIND, or
4478 NULL if it is not present. */
4480 static inline const char *
4481 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4483 dw_attr_ref a = get_AT (die, attr_kind);
4485 return a ? AT_string (a) : NULL;
4488 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4489 if it is not present. */
4491 static inline int
4492 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4494 dw_attr_ref a = get_AT (die, attr_kind);
4496 return a ? AT_flag (a) : 0;
4499 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4500 if it is not present. */
4502 static inline unsigned
4503 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4505 dw_attr_ref a = get_AT (die, attr_kind);
4507 return a ? AT_unsigned (a) : 0;
4510 static inline dw_die_ref
4511 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4513 dw_attr_ref a = get_AT (die, attr_kind);
4515 return a ? AT_ref (a) : NULL;
4518 static inline struct dwarf_file_data *
4519 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4521 dw_attr_ref a = get_AT (die, attr_kind);
4523 return a ? AT_file (a) : NULL;
4526 /* Return TRUE if the language is C++. */
4528 static inline bool
4529 is_cxx (void)
4531 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4533 return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
4536 /* Return TRUE if the language is Fortran. */
4538 static inline bool
4539 is_fortran (void)
4541 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4543 return (lang == DW_LANG_Fortran77
4544 || lang == DW_LANG_Fortran90
4545 || lang == DW_LANG_Fortran95);
4548 /* Return TRUE if the language is Ada. */
4550 static inline bool
4551 is_ada (void)
4553 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4555 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4558 /* Remove the specified attribute if present. */
4560 static void
4561 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4563 dw_attr_ref a;
4564 unsigned ix;
4566 if (! die)
4567 return;
4569 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4570 if (a->dw_attr == attr_kind)
4572 if (AT_class (a) == dw_val_class_str)
4573 if (a->dw_attr_val.v.val_str->refcount)
4574 a->dw_attr_val.v.val_str->refcount--;
4576 /* vec::ordered_remove should help reduce the number of abbrevs
4577 that are needed. */
4578 die->die_attr->ordered_remove (ix);
4579 return;
4583 /* Remove CHILD from its parent. PREV must have the property that
4584 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4586 static void
4587 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4589 gcc_assert (child->die_parent == prev->die_parent);
4590 gcc_assert (prev->die_sib == child);
4591 if (prev == child)
4593 gcc_assert (child->die_parent->die_child == child);
4594 prev = NULL;
4596 else
4597 prev->die_sib = child->die_sib;
4598 if (child->die_parent->die_child == child)
4599 child->die_parent->die_child = prev;
4602 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4603 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4605 static void
4606 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4608 dw_die_ref parent = old_child->die_parent;
4610 gcc_assert (parent == prev->die_parent);
4611 gcc_assert (prev->die_sib == old_child);
4613 new_child->die_parent = parent;
4614 if (prev == old_child)
4616 gcc_assert (parent->die_child == old_child);
4617 new_child->die_sib = new_child;
4619 else
4621 prev->die_sib = new_child;
4622 new_child->die_sib = old_child->die_sib;
4624 if (old_child->die_parent->die_child == old_child)
4625 old_child->die_parent->die_child = new_child;
4628 /* Move all children from OLD_PARENT to NEW_PARENT. */
4630 static void
4631 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4633 dw_die_ref c;
4634 new_parent->die_child = old_parent->die_child;
4635 old_parent->die_child = NULL;
4636 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4639 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4640 matches TAG. */
4642 static void
4643 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4645 dw_die_ref c;
4647 c = die->die_child;
4648 if (c) do {
4649 dw_die_ref prev = c;
4650 c = c->die_sib;
4651 while (c->die_tag == tag)
4653 remove_child_with_prev (c, prev);
4654 /* Might have removed every child. */
4655 if (c == c->die_sib)
4656 return;
4657 c = c->die_sib;
4659 } while (c != die->die_child);
4662 /* Add a CHILD_DIE as the last child of DIE. */
4664 static void
4665 add_child_die (dw_die_ref die, dw_die_ref child_die)
4667 /* FIXME this should probably be an assert. */
4668 if (! die || ! child_die)
4669 return;
4670 gcc_assert (die != child_die);
4672 child_die->die_parent = die;
4673 if (die->die_child)
4675 child_die->die_sib = die->die_child->die_sib;
4676 die->die_child->die_sib = child_die;
4678 else
4679 child_die->die_sib = child_die;
4680 die->die_child = child_die;
4683 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4684 is the specification, to the end of PARENT's list of children.
4685 This is done by removing and re-adding it. */
4687 static void
4688 splice_child_die (dw_die_ref parent, dw_die_ref child)
4690 dw_die_ref p;
4692 /* We want the declaration DIE from inside the class, not the
4693 specification DIE at toplevel. */
4694 if (child->die_parent != parent)
4696 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4698 if (tmp)
4699 child = tmp;
4702 gcc_assert (child->die_parent == parent
4703 || (child->die_parent
4704 == get_AT_ref (parent, DW_AT_specification)));
4706 for (p = child->die_parent->die_child; ; p = p->die_sib)
4707 if (p->die_sib == child)
4709 remove_child_with_prev (child, p);
4710 break;
4713 add_child_die (parent, child);
4716 /* Return a pointer to a newly created DIE node. */
4718 static inline dw_die_ref
4719 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4721 dw_die_ref die = ggc_alloc_cleared_die_node ();
4723 die->die_tag = tag_value;
4725 if (parent_die != NULL)
4726 add_child_die (parent_die, die);
4727 else
4729 limbo_die_node *limbo_node;
4731 limbo_node = ggc_alloc_cleared_limbo_die_node ();
4732 limbo_node->die = die;
4733 limbo_node->created_for = t;
4734 limbo_node->next = limbo_die_list;
4735 limbo_die_list = limbo_node;
4738 return die;
4741 /* Return the DIE associated with the given type specifier. */
4743 static inline dw_die_ref
4744 lookup_type_die (tree type)
4746 return TYPE_SYMTAB_DIE (type);
4749 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4750 anonymous type named by the typedef TYPE_DIE, return the DIE of the
4751 anonymous type instead the one of the naming typedef. */
4753 static inline dw_die_ref
4754 strip_naming_typedef (tree type, dw_die_ref type_die)
4756 if (type
4757 && TREE_CODE (type) == RECORD_TYPE
4758 && type_die
4759 && type_die->die_tag == DW_TAG_typedef
4760 && is_naming_typedef_decl (TYPE_NAME (type)))
4761 type_die = get_AT_ref (type_die, DW_AT_type);
4762 return type_die;
4765 /* Like lookup_type_die, but if type is an anonymous type named by a
4766 typedef[1], return the DIE of the anonymous type instead the one of
4767 the naming typedef. This is because in gen_typedef_die, we did
4768 equate the anonymous struct named by the typedef with the DIE of
4769 the naming typedef. So by default, lookup_type_die on an anonymous
4770 struct yields the DIE of the naming typedef.
4772 [1]: Read the comment of is_naming_typedef_decl to learn about what
4773 a naming typedef is. */
4775 static inline dw_die_ref
4776 lookup_type_die_strip_naming_typedef (tree type)
4778 dw_die_ref die = lookup_type_die (type);
4779 return strip_naming_typedef (type, die);
4782 /* Equate a DIE to a given type specifier. */
4784 static inline void
4785 equate_type_number_to_die (tree type, dw_die_ref type_die)
4787 TYPE_SYMTAB_DIE (type) = type_die;
4790 /* Returns a hash value for X (which really is a die_struct). */
4792 static hashval_t
4793 decl_die_table_hash (const void *x)
4795 return (hashval_t) ((const_dw_die_ref) x)->decl_id;
4798 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
4800 static int
4801 decl_die_table_eq (const void *x, const void *y)
4803 return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
4806 /* Return the DIE associated with a given declaration. */
4808 static inline dw_die_ref
4809 lookup_decl_die (tree decl)
4811 return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
4814 /* Returns a hash value for X (which really is a var_loc_list). */
4816 static hashval_t
4817 decl_loc_table_hash (const void *x)
4819 return (hashval_t) ((const var_loc_list *) x)->decl_id;
4822 /* Return nonzero if decl_id of var_loc_list X is the same as
4823 UID of decl *Y. */
4825 static int
4826 decl_loc_table_eq (const void *x, const void *y)
4828 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
4831 /* Return the var_loc list associated with a given declaration. */
4833 static inline var_loc_list *
4834 lookup_decl_loc (const_tree decl)
4836 if (!decl_loc_table)
4837 return NULL;
4838 return (var_loc_list *)
4839 htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
4842 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
4844 static hashval_t
4845 cached_dw_loc_list_table_hash (const void *x)
4847 return (hashval_t) ((const cached_dw_loc_list *) x)->decl_id;
4850 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
4851 UID of decl *Y. */
4853 static int
4854 cached_dw_loc_list_table_eq (const void *x, const void *y)
4856 return (((const cached_dw_loc_list *) x)->decl_id
4857 == DECL_UID ((const_tree) y));
4860 /* Equate a DIE to a particular declaration. */
4862 static void
4863 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
4865 unsigned int decl_id = DECL_UID (decl);
4866 void **slot;
4868 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
4869 *slot = decl_die;
4870 decl_die->decl_id = decl_id;
4873 /* Return how many bits covers PIECE EXPR_LIST. */
4875 static int
4876 decl_piece_bitsize (rtx piece)
4878 int ret = (int) GET_MODE (piece);
4879 if (ret)
4880 return ret;
4881 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
4882 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
4883 return INTVAL (XEXP (XEXP (piece, 0), 0));
4886 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
4888 static rtx *
4889 decl_piece_varloc_ptr (rtx piece)
4891 if ((int) GET_MODE (piece))
4892 return &XEXP (piece, 0);
4893 else
4894 return &XEXP (XEXP (piece, 0), 1);
4897 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
4898 Next is the chain of following piece nodes. */
4900 static rtx
4901 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
4903 if (bitsize <= (int) MAX_MACHINE_MODE)
4904 return alloc_EXPR_LIST (bitsize, loc_note, next);
4905 else
4906 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
4907 GEN_INT (bitsize),
4908 loc_note), next);
4911 /* Return rtx that should be stored into loc field for
4912 LOC_NOTE and BITPOS/BITSIZE. */
4914 static rtx
4915 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
4916 HOST_WIDE_INT bitsize)
4918 if (bitsize != -1)
4920 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
4921 if (bitpos != 0)
4922 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
4924 return loc_note;
4927 /* This function either modifies location piece list *DEST in
4928 place (if SRC and INNER is NULL), or copies location piece list
4929 *SRC to *DEST while modifying it. Location BITPOS is modified
4930 to contain LOC_NOTE, any pieces overlapping it are removed resp.
4931 not copied and if needed some padding around it is added.
4932 When modifying in place, DEST should point to EXPR_LIST where
4933 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
4934 to the start of the whole list and INNER points to the EXPR_LIST
4935 where earlier pieces cover PIECE_BITPOS bits. */
4937 static void
4938 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
4939 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
4940 HOST_WIDE_INT bitsize, rtx loc_note)
4942 int diff;
4943 bool copy = inner != NULL;
4945 if (copy)
4947 /* First copy all nodes preceding the current bitpos. */
4948 while (src != inner)
4950 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
4951 decl_piece_bitsize (*src), NULL_RTX);
4952 dest = &XEXP (*dest, 1);
4953 src = &XEXP (*src, 1);
4956 /* Add padding if needed. */
4957 if (bitpos != piece_bitpos)
4959 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
4960 copy ? NULL_RTX : *dest);
4961 dest = &XEXP (*dest, 1);
4963 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
4965 gcc_assert (!copy);
4966 /* A piece with correct bitpos and bitsize already exist,
4967 just update the location for it and return. */
4968 *decl_piece_varloc_ptr (*dest) = loc_note;
4969 return;
4971 /* Add the piece that changed. */
4972 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
4973 dest = &XEXP (*dest, 1);
4974 /* Skip over pieces that overlap it. */
4975 diff = bitpos - piece_bitpos + bitsize;
4976 if (!copy)
4977 src = dest;
4978 while (diff > 0 && *src)
4980 rtx piece = *src;
4981 diff -= decl_piece_bitsize (piece);
4982 if (copy)
4983 src = &XEXP (piece, 1);
4984 else
4986 *src = XEXP (piece, 1);
4987 free_EXPR_LIST_node (piece);
4990 /* Add padding if needed. */
4991 if (diff < 0 && *src)
4993 if (!copy)
4994 dest = src;
4995 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
4996 dest = &XEXP (*dest, 1);
4998 if (!copy)
4999 return;
5000 /* Finally copy all nodes following it. */
5001 while (*src)
5003 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5004 decl_piece_bitsize (*src), NULL_RTX);
5005 dest = &XEXP (*dest, 1);
5006 src = &XEXP (*src, 1);
5010 /* Add a variable location node to the linked list for DECL. */
5012 static struct var_loc_node *
5013 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5015 unsigned int decl_id;
5016 var_loc_list *temp;
5017 void **slot;
5018 struct var_loc_node *loc = NULL;
5019 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5021 if (DECL_DEBUG_EXPR_IS_FROM (decl))
5023 tree realdecl = DECL_DEBUG_EXPR (decl);
5024 if (realdecl
5025 && (handled_component_p (realdecl)
5026 || (TREE_CODE (realdecl) == MEM_REF
5027 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR)))
5029 HOST_WIDE_INT maxsize;
5030 tree innerdecl;
5031 innerdecl
5032 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5033 if (!DECL_P (innerdecl)
5034 || DECL_IGNORED_P (innerdecl)
5035 || TREE_STATIC (innerdecl)
5036 || bitsize <= 0
5037 || bitpos + bitsize > 256
5038 || bitsize != maxsize)
5039 return NULL;
5040 decl = innerdecl;
5044 decl_id = DECL_UID (decl);
5045 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5046 if (*slot == NULL)
5048 temp = ggc_alloc_cleared_var_loc_list ();
5049 temp->decl_id = decl_id;
5050 *slot = temp;
5052 else
5053 temp = (var_loc_list *) *slot;
5055 /* For PARM_DECLs try to keep around the original incoming value,
5056 even if that means we'll emit a zero-range .debug_loc entry. */
5057 if (temp->last
5058 && temp->first == temp->last
5059 && TREE_CODE (decl) == PARM_DECL
5060 && GET_CODE (temp->first->loc) == NOTE
5061 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5062 && DECL_INCOMING_RTL (decl)
5063 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5064 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5065 == GET_CODE (DECL_INCOMING_RTL (decl))
5066 && prev_real_insn (temp->first->loc) == NULL_RTX
5067 && (bitsize != -1
5068 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5069 NOTE_VAR_LOCATION_LOC (loc_note))
5070 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5071 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5073 loc = ggc_alloc_cleared_var_loc_node ();
5074 temp->first->next = loc;
5075 temp->last = loc;
5076 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5078 else if (temp->last)
5080 struct var_loc_node *last = temp->last, *unused = NULL;
5081 rtx *piece_loc = NULL, last_loc_note;
5082 int piece_bitpos = 0;
5083 if (last->next)
5085 last = last->next;
5086 gcc_assert (last->next == NULL);
5088 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5090 piece_loc = &last->loc;
5093 int cur_bitsize = decl_piece_bitsize (*piece_loc);
5094 if (piece_bitpos + cur_bitsize > bitpos)
5095 break;
5096 piece_bitpos += cur_bitsize;
5097 piece_loc = &XEXP (*piece_loc, 1);
5099 while (*piece_loc);
5101 /* TEMP->LAST here is either pointer to the last but one or
5102 last element in the chained list, LAST is pointer to the
5103 last element. */
5104 if (label && strcmp (last->label, label) == 0)
5106 /* For SRA optimized variables if there weren't any real
5107 insns since last note, just modify the last node. */
5108 if (piece_loc != NULL)
5110 adjust_piece_list (piece_loc, NULL, NULL,
5111 bitpos, piece_bitpos, bitsize, loc_note);
5112 return NULL;
5114 /* If the last note doesn't cover any instructions, remove it. */
5115 if (temp->last != last)
5117 temp->last->next = NULL;
5118 unused = last;
5119 last = temp->last;
5120 gcc_assert (strcmp (last->label, label) != 0);
5122 else
5124 gcc_assert (temp->first == temp->last
5125 || (temp->first->next == temp->last
5126 && TREE_CODE (decl) == PARM_DECL));
5127 memset (temp->last, '\0', sizeof (*temp->last));
5128 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5129 return temp->last;
5132 if (bitsize == -1 && NOTE_P (last->loc))
5133 last_loc_note = last->loc;
5134 else if (piece_loc != NULL
5135 && *piece_loc != NULL_RTX
5136 && piece_bitpos == bitpos
5137 && decl_piece_bitsize (*piece_loc) == bitsize)
5138 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5139 else
5140 last_loc_note = NULL_RTX;
5141 /* If the current location is the same as the end of the list,
5142 and either both or neither of the locations is uninitialized,
5143 we have nothing to do. */
5144 if (last_loc_note == NULL_RTX
5145 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5146 NOTE_VAR_LOCATION_LOC (loc_note)))
5147 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5148 != NOTE_VAR_LOCATION_STATUS (loc_note))
5149 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5150 == VAR_INIT_STATUS_UNINITIALIZED)
5151 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5152 == VAR_INIT_STATUS_UNINITIALIZED))))
5154 /* Add LOC to the end of list and update LAST. If the last
5155 element of the list has been removed above, reuse its
5156 memory for the new node, otherwise allocate a new one. */
5157 if (unused)
5159 loc = unused;
5160 memset (loc, '\0', sizeof (*loc));
5162 else
5163 loc = ggc_alloc_cleared_var_loc_node ();
5164 if (bitsize == -1 || piece_loc == NULL)
5165 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5166 else
5167 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5168 bitpos, piece_bitpos, bitsize, loc_note);
5169 last->next = loc;
5170 /* Ensure TEMP->LAST will point either to the new last but one
5171 element of the chain, or to the last element in it. */
5172 if (last != temp->last)
5173 temp->last = last;
5175 else if (unused)
5176 ggc_free (unused);
5178 else
5180 loc = ggc_alloc_cleared_var_loc_node ();
5181 temp->first = loc;
5182 temp->last = loc;
5183 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5185 return loc;
5188 /* Keep track of the number of spaces used to indent the
5189 output of the debugging routines that print the structure of
5190 the DIE internal representation. */
5191 static int print_indent;
5193 /* Indent the line the number of spaces given by print_indent. */
5195 static inline void
5196 print_spaces (FILE *outfile)
5198 fprintf (outfile, "%*s", print_indent, "");
5201 /* Print a type signature in hex. */
5203 static inline void
5204 print_signature (FILE *outfile, char *sig)
5206 int i;
5208 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5209 fprintf (outfile, "%02x", sig[i] & 0xff);
5212 /* Print the information associated with a given DIE, and its children.
5213 This routine is a debugging aid only. */
5215 static void
5216 print_die (dw_die_ref die, FILE *outfile)
5218 dw_attr_ref a;
5219 dw_die_ref c;
5220 unsigned ix;
5222 print_spaces (outfile);
5223 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5224 die->die_offset, dwarf_tag_name (die->die_tag),
5225 (void*) die);
5226 print_spaces (outfile);
5227 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5228 fprintf (outfile, " offset: %ld", die->die_offset);
5229 fprintf (outfile, " mark: %d\n", die->die_mark);
5231 if (die->comdat_type_p)
5233 print_spaces (outfile);
5234 fprintf (outfile, " signature: ");
5235 print_signature (outfile, die->die_id.die_type_node->signature);
5236 fprintf (outfile, "\n");
5239 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5241 print_spaces (outfile);
5242 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5244 switch (AT_class (a))
5246 case dw_val_class_addr:
5247 fprintf (outfile, "address");
5248 break;
5249 case dw_val_class_offset:
5250 fprintf (outfile, "offset");
5251 break;
5252 case dw_val_class_loc:
5253 fprintf (outfile, "location descriptor");
5254 break;
5255 case dw_val_class_loc_list:
5256 fprintf (outfile, "location list -> label:%s",
5257 AT_loc_list (a)->ll_symbol);
5258 break;
5259 case dw_val_class_range_list:
5260 fprintf (outfile, "range list");
5261 break;
5262 case dw_val_class_const:
5263 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5264 break;
5265 case dw_val_class_unsigned_const:
5266 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5267 break;
5268 case dw_val_class_const_double:
5269 fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
5270 HOST_WIDE_INT_PRINT_UNSIGNED")",
5271 a->dw_attr_val.v.val_double.high,
5272 a->dw_attr_val.v.val_double.low);
5273 break;
5274 case dw_val_class_vec:
5275 fprintf (outfile, "floating-point or vector constant");
5276 break;
5277 case dw_val_class_flag:
5278 fprintf (outfile, "%u", AT_flag (a));
5279 break;
5280 case dw_val_class_die_ref:
5281 if (AT_ref (a) != NULL)
5283 if (AT_ref (a)->comdat_type_p)
5285 fprintf (outfile, "die -> signature: ");
5286 print_signature (outfile,
5287 AT_ref (a)->die_id.die_type_node->signature);
5289 else if (AT_ref (a)->die_id.die_symbol)
5290 fprintf (outfile, "die -> label: %s",
5291 AT_ref (a)->die_id.die_symbol);
5292 else
5293 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5294 fprintf (outfile, " (%p)", (void *) AT_ref (a));
5296 else
5297 fprintf (outfile, "die -> <null>");
5298 break;
5299 case dw_val_class_vms_delta:
5300 fprintf (outfile, "delta: @slotcount(%s-%s)",
5301 AT_vms_delta2 (a), AT_vms_delta1 (a));
5302 break;
5303 case dw_val_class_lbl_id:
5304 case dw_val_class_lineptr:
5305 case dw_val_class_macptr:
5306 case dw_val_class_high_pc:
5307 fprintf (outfile, "label: %s", AT_lbl (a));
5308 break;
5309 case dw_val_class_str:
5310 if (AT_string (a) != NULL)
5311 fprintf (outfile, "\"%s\"", AT_string (a));
5312 else
5313 fprintf (outfile, "<null>");
5314 break;
5315 case dw_val_class_file:
5316 fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5317 AT_file (a)->emitted_number);
5318 break;
5319 case dw_val_class_data8:
5321 int i;
5323 for (i = 0; i < 8; i++)
5324 fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
5325 break;
5327 default:
5328 break;
5331 fprintf (outfile, "\n");
5334 if (die->die_child != NULL)
5336 print_indent += 4;
5337 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5338 print_indent -= 4;
5340 if (print_indent == 0)
5341 fprintf (outfile, "\n");
5344 /* Print the information collected for a given DIE. */
5346 DEBUG_FUNCTION void
5347 debug_dwarf_die (dw_die_ref die)
5349 print_die (die, stderr);
5352 /* Print all DWARF information collected for the compilation unit.
5353 This routine is a debugging aid only. */
5355 DEBUG_FUNCTION void
5356 debug_dwarf (void)
5358 print_indent = 0;
5359 print_die (comp_unit_die (), stderr);
5362 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5363 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5364 DIE that marks the start of the DIEs for this include file. */
5366 static dw_die_ref
5367 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5369 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5370 dw_die_ref new_unit = gen_compile_unit_die (filename);
5372 new_unit->die_sib = old_unit;
5373 return new_unit;
5376 /* Close an include-file CU and reopen the enclosing one. */
5378 static dw_die_ref
5379 pop_compile_unit (dw_die_ref old_unit)
5381 dw_die_ref new_unit = old_unit->die_sib;
5383 old_unit->die_sib = NULL;
5384 return new_unit;
5387 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5388 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5390 /* Calculate the checksum of a location expression. */
5392 static inline void
5393 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5395 int tem;
5397 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5398 CHECKSUM (tem);
5399 CHECKSUM (loc->dw_loc_oprnd1);
5400 CHECKSUM (loc->dw_loc_oprnd2);
5403 /* Calculate the checksum of an attribute. */
5405 static void
5406 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5408 dw_loc_descr_ref loc;
5409 rtx r;
5411 CHECKSUM (at->dw_attr);
5413 /* We don't care that this was compiled with a different compiler
5414 snapshot; if the output is the same, that's what matters. */
5415 if (at->dw_attr == DW_AT_producer)
5416 return;
5418 switch (AT_class (at))
5420 case dw_val_class_const:
5421 CHECKSUM (at->dw_attr_val.v.val_int);
5422 break;
5423 case dw_val_class_unsigned_const:
5424 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5425 break;
5426 case dw_val_class_const_double:
5427 CHECKSUM (at->dw_attr_val.v.val_double);
5428 break;
5429 case dw_val_class_vec:
5430 CHECKSUM (at->dw_attr_val.v.val_vec);
5431 break;
5432 case dw_val_class_flag:
5433 CHECKSUM (at->dw_attr_val.v.val_flag);
5434 break;
5435 case dw_val_class_str:
5436 CHECKSUM_STRING (AT_string (at));
5437 break;
5439 case dw_val_class_addr:
5440 r = AT_addr (at);
5441 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5442 CHECKSUM_STRING (XSTR (r, 0));
5443 break;
5445 case dw_val_class_offset:
5446 CHECKSUM (at->dw_attr_val.v.val_offset);
5447 break;
5449 case dw_val_class_loc:
5450 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5451 loc_checksum (loc, ctx);
5452 break;
5454 case dw_val_class_die_ref:
5455 die_checksum (AT_ref (at), ctx, mark);
5456 break;
5458 case dw_val_class_fde_ref:
5459 case dw_val_class_vms_delta:
5460 case dw_val_class_lbl_id:
5461 case dw_val_class_lineptr:
5462 case dw_val_class_macptr:
5463 case dw_val_class_high_pc:
5464 break;
5466 case dw_val_class_file:
5467 CHECKSUM_STRING (AT_file (at)->filename);
5468 break;
5470 case dw_val_class_data8:
5471 CHECKSUM (at->dw_attr_val.v.val_data8);
5472 break;
5474 default:
5475 break;
5479 /* Calculate the checksum of a DIE. */
5481 static void
5482 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5484 dw_die_ref c;
5485 dw_attr_ref a;
5486 unsigned ix;
5488 /* To avoid infinite recursion. */
5489 if (die->die_mark)
5491 CHECKSUM (die->die_mark);
5492 return;
5494 die->die_mark = ++(*mark);
5496 CHECKSUM (die->die_tag);
5498 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5499 attr_checksum (a, ctx, mark);
5501 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5504 #undef CHECKSUM
5505 #undef CHECKSUM_STRING
5507 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
5508 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5509 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5510 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5511 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5512 #define CHECKSUM_ATTR(FOO) \
5513 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5515 /* Calculate the checksum of a number in signed LEB128 format. */
5517 static void
5518 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5520 unsigned char byte;
5521 bool more;
5523 while (1)
5525 byte = (value & 0x7f);
5526 value >>= 7;
5527 more = !((value == 0 && (byte & 0x40) == 0)
5528 || (value == -1 && (byte & 0x40) != 0));
5529 if (more)
5530 byte |= 0x80;
5531 CHECKSUM (byte);
5532 if (!more)
5533 break;
5537 /* Calculate the checksum of a number in unsigned LEB128 format. */
5539 static void
5540 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5542 while (1)
5544 unsigned char byte = (value & 0x7f);
5545 value >>= 7;
5546 if (value != 0)
5547 /* More bytes to follow. */
5548 byte |= 0x80;
5549 CHECKSUM (byte);
5550 if (value == 0)
5551 break;
5555 /* Checksum the context of the DIE. This adds the names of any
5556 surrounding namespaces or structures to the checksum. */
5558 static void
5559 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5561 const char *name;
5562 dw_die_ref spec;
5563 int tag = die->die_tag;
5565 if (tag != DW_TAG_namespace
5566 && tag != DW_TAG_structure_type
5567 && tag != DW_TAG_class_type)
5568 return;
5570 name = get_AT_string (die, DW_AT_name);
5572 spec = get_AT_ref (die, DW_AT_specification);
5573 if (spec != NULL)
5574 die = spec;
5576 if (die->die_parent != NULL)
5577 checksum_die_context (die->die_parent, ctx);
5579 CHECKSUM_ULEB128 ('C');
5580 CHECKSUM_ULEB128 (tag);
5581 if (name != NULL)
5582 CHECKSUM_STRING (name);
5585 /* Calculate the checksum of a location expression. */
5587 static inline void
5588 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5590 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5591 were emitted as a DW_FORM_sdata instead of a location expression. */
5592 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5594 CHECKSUM_ULEB128 (DW_FORM_sdata);
5595 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5596 return;
5599 /* Otherwise, just checksum the raw location expression. */
5600 while (loc != NULL)
5602 CHECKSUM_ULEB128 (loc->dw_loc_opc);
5603 CHECKSUM (loc->dw_loc_oprnd1);
5604 CHECKSUM (loc->dw_loc_oprnd2);
5605 loc = loc->dw_loc_next;
5609 /* Calculate the checksum of an attribute. */
5611 static void
5612 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5613 struct md5_ctx *ctx, int *mark)
5615 dw_loc_descr_ref loc;
5616 rtx r;
5618 if (AT_class (at) == dw_val_class_die_ref)
5620 dw_die_ref target_die = AT_ref (at);
5622 /* For pointer and reference types, we checksum only the (qualified)
5623 name of the target type (if there is a name). For friend entries,
5624 we checksum only the (qualified) name of the target type or function.
5625 This allows the checksum to remain the same whether the target type
5626 is complete or not. */
5627 if ((at->dw_attr == DW_AT_type
5628 && (tag == DW_TAG_pointer_type
5629 || tag == DW_TAG_reference_type
5630 || tag == DW_TAG_rvalue_reference_type
5631 || tag == DW_TAG_ptr_to_member_type))
5632 || (at->dw_attr == DW_AT_friend
5633 && tag == DW_TAG_friend))
5635 dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5637 if (name_attr != NULL)
5639 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5641 if (decl == NULL)
5642 decl = target_die;
5643 CHECKSUM_ULEB128 ('N');
5644 CHECKSUM_ULEB128 (at->dw_attr);
5645 if (decl->die_parent != NULL)
5646 checksum_die_context (decl->die_parent, ctx);
5647 CHECKSUM_ULEB128 ('E');
5648 CHECKSUM_STRING (AT_string (name_attr));
5649 return;
5653 /* For all other references to another DIE, we check to see if the
5654 target DIE has already been visited. If it has, we emit a
5655 backward reference; if not, we descend recursively. */
5656 if (target_die->die_mark > 0)
5658 CHECKSUM_ULEB128 ('R');
5659 CHECKSUM_ULEB128 (at->dw_attr);
5660 CHECKSUM_ULEB128 (target_die->die_mark);
5662 else
5664 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5666 if (decl == NULL)
5667 decl = target_die;
5668 target_die->die_mark = ++(*mark);
5669 CHECKSUM_ULEB128 ('T');
5670 CHECKSUM_ULEB128 (at->dw_attr);
5671 if (decl->die_parent != NULL)
5672 checksum_die_context (decl->die_parent, ctx);
5673 die_checksum_ordered (target_die, ctx, mark);
5675 return;
5678 CHECKSUM_ULEB128 ('A');
5679 CHECKSUM_ULEB128 (at->dw_attr);
5681 switch (AT_class (at))
5683 case dw_val_class_const:
5684 CHECKSUM_ULEB128 (DW_FORM_sdata);
5685 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
5686 break;
5688 case dw_val_class_unsigned_const:
5689 CHECKSUM_ULEB128 (DW_FORM_sdata);
5690 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
5691 break;
5693 case dw_val_class_const_double:
5694 CHECKSUM_ULEB128 (DW_FORM_block);
5695 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
5696 CHECKSUM (at->dw_attr_val.v.val_double);
5697 break;
5699 case dw_val_class_vec:
5700 CHECKSUM_ULEB128 (DW_FORM_block);
5701 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
5702 CHECKSUM (at->dw_attr_val.v.val_vec);
5703 break;
5705 case dw_val_class_flag:
5706 CHECKSUM_ULEB128 (DW_FORM_flag);
5707 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
5708 break;
5710 case dw_val_class_str:
5711 CHECKSUM_ULEB128 (DW_FORM_string);
5712 CHECKSUM_STRING (AT_string (at));
5713 break;
5715 case dw_val_class_addr:
5716 r = AT_addr (at);
5717 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5718 CHECKSUM_ULEB128 (DW_FORM_string);
5719 CHECKSUM_STRING (XSTR (r, 0));
5720 break;
5722 case dw_val_class_offset:
5723 CHECKSUM_ULEB128 (DW_FORM_sdata);
5724 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
5725 break;
5727 case dw_val_class_loc:
5728 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5729 loc_checksum_ordered (loc, ctx);
5730 break;
5732 case dw_val_class_fde_ref:
5733 case dw_val_class_lbl_id:
5734 case dw_val_class_lineptr:
5735 case dw_val_class_macptr:
5736 case dw_val_class_high_pc:
5737 break;
5739 case dw_val_class_file:
5740 CHECKSUM_ULEB128 (DW_FORM_string);
5741 CHECKSUM_STRING (AT_file (at)->filename);
5742 break;
5744 case dw_val_class_data8:
5745 CHECKSUM (at->dw_attr_val.v.val_data8);
5746 break;
5748 default:
5749 break;
5753 struct checksum_attributes
5755 dw_attr_ref at_name;
5756 dw_attr_ref at_type;
5757 dw_attr_ref at_friend;
5758 dw_attr_ref at_accessibility;
5759 dw_attr_ref at_address_class;
5760 dw_attr_ref at_allocated;
5761 dw_attr_ref at_artificial;
5762 dw_attr_ref at_associated;
5763 dw_attr_ref at_binary_scale;
5764 dw_attr_ref at_bit_offset;
5765 dw_attr_ref at_bit_size;
5766 dw_attr_ref at_bit_stride;
5767 dw_attr_ref at_byte_size;
5768 dw_attr_ref at_byte_stride;
5769 dw_attr_ref at_const_value;
5770 dw_attr_ref at_containing_type;
5771 dw_attr_ref at_count;
5772 dw_attr_ref at_data_location;
5773 dw_attr_ref at_data_member_location;
5774 dw_attr_ref at_decimal_scale;
5775 dw_attr_ref at_decimal_sign;
5776 dw_attr_ref at_default_value;
5777 dw_attr_ref at_digit_count;
5778 dw_attr_ref at_discr;
5779 dw_attr_ref at_discr_list;
5780 dw_attr_ref at_discr_value;
5781 dw_attr_ref at_encoding;
5782 dw_attr_ref at_endianity;
5783 dw_attr_ref at_explicit;
5784 dw_attr_ref at_is_optional;
5785 dw_attr_ref at_location;
5786 dw_attr_ref at_lower_bound;
5787 dw_attr_ref at_mutable;
5788 dw_attr_ref at_ordering;
5789 dw_attr_ref at_picture_string;
5790 dw_attr_ref at_prototyped;
5791 dw_attr_ref at_small;
5792 dw_attr_ref at_segment;
5793 dw_attr_ref at_string_length;
5794 dw_attr_ref at_threads_scaled;
5795 dw_attr_ref at_upper_bound;
5796 dw_attr_ref at_use_location;
5797 dw_attr_ref at_use_UTF8;
5798 dw_attr_ref at_variable_parameter;
5799 dw_attr_ref at_virtuality;
5800 dw_attr_ref at_visibility;
5801 dw_attr_ref at_vtable_elem_location;
5804 /* Collect the attributes that we will want to use for the checksum. */
5806 static void
5807 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
5809 dw_attr_ref a;
5810 unsigned ix;
5812 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5814 switch (a->dw_attr)
5816 case DW_AT_name:
5817 attrs->at_name = a;
5818 break;
5819 case DW_AT_type:
5820 attrs->at_type = a;
5821 break;
5822 case DW_AT_friend:
5823 attrs->at_friend = a;
5824 break;
5825 case DW_AT_accessibility:
5826 attrs->at_accessibility = a;
5827 break;
5828 case DW_AT_address_class:
5829 attrs->at_address_class = a;
5830 break;
5831 case DW_AT_allocated:
5832 attrs->at_allocated = a;
5833 break;
5834 case DW_AT_artificial:
5835 attrs->at_artificial = a;
5836 break;
5837 case DW_AT_associated:
5838 attrs->at_associated = a;
5839 break;
5840 case DW_AT_binary_scale:
5841 attrs->at_binary_scale = a;
5842 break;
5843 case DW_AT_bit_offset:
5844 attrs->at_bit_offset = a;
5845 break;
5846 case DW_AT_bit_size:
5847 attrs->at_bit_size = a;
5848 break;
5849 case DW_AT_bit_stride:
5850 attrs->at_bit_stride = a;
5851 break;
5852 case DW_AT_byte_size:
5853 attrs->at_byte_size = a;
5854 break;
5855 case DW_AT_byte_stride:
5856 attrs->at_byte_stride = a;
5857 break;
5858 case DW_AT_const_value:
5859 attrs->at_const_value = a;
5860 break;
5861 case DW_AT_containing_type:
5862 attrs->at_containing_type = a;
5863 break;
5864 case DW_AT_count:
5865 attrs->at_count = a;
5866 break;
5867 case DW_AT_data_location:
5868 attrs->at_data_location = a;
5869 break;
5870 case DW_AT_data_member_location:
5871 attrs->at_data_member_location = a;
5872 break;
5873 case DW_AT_decimal_scale:
5874 attrs->at_decimal_scale = a;
5875 break;
5876 case DW_AT_decimal_sign:
5877 attrs->at_decimal_sign = a;
5878 break;
5879 case DW_AT_default_value:
5880 attrs->at_default_value = a;
5881 break;
5882 case DW_AT_digit_count:
5883 attrs->at_digit_count = a;
5884 break;
5885 case DW_AT_discr:
5886 attrs->at_discr = a;
5887 break;
5888 case DW_AT_discr_list:
5889 attrs->at_discr_list = a;
5890 break;
5891 case DW_AT_discr_value:
5892 attrs->at_discr_value = a;
5893 break;
5894 case DW_AT_encoding:
5895 attrs->at_encoding = a;
5896 break;
5897 case DW_AT_endianity:
5898 attrs->at_endianity = a;
5899 break;
5900 case DW_AT_explicit:
5901 attrs->at_explicit = a;
5902 break;
5903 case DW_AT_is_optional:
5904 attrs->at_is_optional = a;
5905 break;
5906 case DW_AT_location:
5907 attrs->at_location = a;
5908 break;
5909 case DW_AT_lower_bound:
5910 attrs->at_lower_bound = a;
5911 break;
5912 case DW_AT_mutable:
5913 attrs->at_mutable = a;
5914 break;
5915 case DW_AT_ordering:
5916 attrs->at_ordering = a;
5917 break;
5918 case DW_AT_picture_string:
5919 attrs->at_picture_string = a;
5920 break;
5921 case DW_AT_prototyped:
5922 attrs->at_prototyped = a;
5923 break;
5924 case DW_AT_small:
5925 attrs->at_small = a;
5926 break;
5927 case DW_AT_segment:
5928 attrs->at_segment = a;
5929 break;
5930 case DW_AT_string_length:
5931 attrs->at_string_length = a;
5932 break;
5933 case DW_AT_threads_scaled:
5934 attrs->at_threads_scaled = a;
5935 break;
5936 case DW_AT_upper_bound:
5937 attrs->at_upper_bound = a;
5938 break;
5939 case DW_AT_use_location:
5940 attrs->at_use_location = a;
5941 break;
5942 case DW_AT_use_UTF8:
5943 attrs->at_use_UTF8 = a;
5944 break;
5945 case DW_AT_variable_parameter:
5946 attrs->at_variable_parameter = a;
5947 break;
5948 case DW_AT_virtuality:
5949 attrs->at_virtuality = a;
5950 break;
5951 case DW_AT_visibility:
5952 attrs->at_visibility = a;
5953 break;
5954 case DW_AT_vtable_elem_location:
5955 attrs->at_vtable_elem_location = a;
5956 break;
5957 default:
5958 break;
5963 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
5965 static void
5966 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5968 dw_die_ref c;
5969 dw_die_ref decl;
5970 struct checksum_attributes attrs;
5972 CHECKSUM_ULEB128 ('D');
5973 CHECKSUM_ULEB128 (die->die_tag);
5975 memset (&attrs, 0, sizeof (attrs));
5977 decl = get_AT_ref (die, DW_AT_specification);
5978 if (decl != NULL)
5979 collect_checksum_attributes (&attrs, decl);
5980 collect_checksum_attributes (&attrs, die);
5982 CHECKSUM_ATTR (attrs.at_name);
5983 CHECKSUM_ATTR (attrs.at_accessibility);
5984 CHECKSUM_ATTR (attrs.at_address_class);
5985 CHECKSUM_ATTR (attrs.at_allocated);
5986 CHECKSUM_ATTR (attrs.at_artificial);
5987 CHECKSUM_ATTR (attrs.at_associated);
5988 CHECKSUM_ATTR (attrs.at_binary_scale);
5989 CHECKSUM_ATTR (attrs.at_bit_offset);
5990 CHECKSUM_ATTR (attrs.at_bit_size);
5991 CHECKSUM_ATTR (attrs.at_bit_stride);
5992 CHECKSUM_ATTR (attrs.at_byte_size);
5993 CHECKSUM_ATTR (attrs.at_byte_stride);
5994 CHECKSUM_ATTR (attrs.at_const_value);
5995 CHECKSUM_ATTR (attrs.at_containing_type);
5996 CHECKSUM_ATTR (attrs.at_count);
5997 CHECKSUM_ATTR (attrs.at_data_location);
5998 CHECKSUM_ATTR (attrs.at_data_member_location);
5999 CHECKSUM_ATTR (attrs.at_decimal_scale);
6000 CHECKSUM_ATTR (attrs.at_decimal_sign);
6001 CHECKSUM_ATTR (attrs.at_default_value);
6002 CHECKSUM_ATTR (attrs.at_digit_count);
6003 CHECKSUM_ATTR (attrs.at_discr);
6004 CHECKSUM_ATTR (attrs.at_discr_list);
6005 CHECKSUM_ATTR (attrs.at_discr_value);
6006 CHECKSUM_ATTR (attrs.at_encoding);
6007 CHECKSUM_ATTR (attrs.at_endianity);
6008 CHECKSUM_ATTR (attrs.at_explicit);
6009 CHECKSUM_ATTR (attrs.at_is_optional);
6010 CHECKSUM_ATTR (attrs.at_location);
6011 CHECKSUM_ATTR (attrs.at_lower_bound);
6012 CHECKSUM_ATTR (attrs.at_mutable);
6013 CHECKSUM_ATTR (attrs.at_ordering);
6014 CHECKSUM_ATTR (attrs.at_picture_string);
6015 CHECKSUM_ATTR (attrs.at_prototyped);
6016 CHECKSUM_ATTR (attrs.at_small);
6017 CHECKSUM_ATTR (attrs.at_segment);
6018 CHECKSUM_ATTR (attrs.at_string_length);
6019 CHECKSUM_ATTR (attrs.at_threads_scaled);
6020 CHECKSUM_ATTR (attrs.at_upper_bound);
6021 CHECKSUM_ATTR (attrs.at_use_location);
6022 CHECKSUM_ATTR (attrs.at_use_UTF8);
6023 CHECKSUM_ATTR (attrs.at_variable_parameter);
6024 CHECKSUM_ATTR (attrs.at_virtuality);
6025 CHECKSUM_ATTR (attrs.at_visibility);
6026 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6027 CHECKSUM_ATTR (attrs.at_type);
6028 CHECKSUM_ATTR (attrs.at_friend);
6030 /* Checksum the child DIEs, except for nested types and member functions. */
6031 c = die->die_child;
6032 if (c) do {
6033 dw_attr_ref name_attr;
6035 c = c->die_sib;
6036 name_attr = get_AT (c, DW_AT_name);
6037 if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
6038 && name_attr != NULL)
6040 CHECKSUM_ULEB128 ('S');
6041 CHECKSUM_ULEB128 (c->die_tag);
6042 CHECKSUM_STRING (AT_string (name_attr));
6044 else
6046 /* Mark this DIE so it gets processed when unmarking. */
6047 if (c->die_mark == 0)
6048 c->die_mark = -1;
6049 die_checksum_ordered (c, ctx, mark);
6051 } while (c != die->die_child);
6053 CHECKSUM_ULEB128 (0);
6056 #undef CHECKSUM
6057 #undef CHECKSUM_STRING
6058 #undef CHECKSUM_ATTR
6059 #undef CHECKSUM_LEB128
6060 #undef CHECKSUM_ULEB128
6062 /* Generate the type signature for DIE. This is computed by generating an
6063 MD5 checksum over the DIE's tag, its relevant attributes, and its
6064 children. Attributes that are references to other DIEs are processed
6065 by recursion, using the MARK field to prevent infinite recursion.
6066 If the DIE is nested inside a namespace or another type, we also
6067 need to include that context in the signature. The lower 64 bits
6068 of the resulting MD5 checksum comprise the signature. */
6070 static void
6071 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6073 int mark;
6074 const char *name;
6075 unsigned char checksum[16];
6076 struct md5_ctx ctx;
6077 dw_die_ref decl;
6078 dw_die_ref parent;
6080 name = get_AT_string (die, DW_AT_name);
6081 decl = get_AT_ref (die, DW_AT_specification);
6082 parent = get_die_parent (die);
6084 /* First, compute a signature for just the type name (and its surrounding
6085 context, if any. This is stored in the type unit DIE for link-time
6086 ODR (one-definition rule) checking. */
6088 if (is_cxx() && name != NULL)
6090 md5_init_ctx (&ctx);
6092 /* Checksum the names of surrounding namespaces and structures. */
6093 if (parent != NULL)
6094 checksum_die_context (parent, &ctx);
6096 md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
6097 md5_process_bytes (name, strlen (name) + 1, &ctx);
6098 md5_finish_ctx (&ctx, checksum);
6100 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6103 /* Next, compute the complete type signature. */
6105 md5_init_ctx (&ctx);
6106 mark = 1;
6107 die->die_mark = mark;
6109 /* Checksum the names of surrounding namespaces and structures. */
6110 if (parent != NULL)
6111 checksum_die_context (parent, &ctx);
6113 /* Checksum the DIE and its children. */
6114 die_checksum_ordered (die, &ctx, &mark);
6115 unmark_all_dies (die);
6116 md5_finish_ctx (&ctx, checksum);
6118 /* Store the signature in the type node and link the type DIE and the
6119 type node together. */
6120 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6121 DWARF_TYPE_SIGNATURE_SIZE);
6122 die->comdat_type_p = true;
6123 die->die_id.die_type_node = type_node;
6124 type_node->type_die = die;
6126 /* If the DIE is a specification, link its declaration to the type node
6127 as well. */
6128 if (decl != NULL)
6130 decl->comdat_type_p = true;
6131 decl->die_id.die_type_node = type_node;
6135 /* Do the location expressions look same? */
6136 static inline int
6137 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6139 return loc1->dw_loc_opc == loc2->dw_loc_opc
6140 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6141 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6144 /* Do the values look the same? */
6145 static int
6146 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6148 dw_loc_descr_ref loc1, loc2;
6149 rtx r1, r2;
6151 if (v1->val_class != v2->val_class)
6152 return 0;
6154 switch (v1->val_class)
6156 case dw_val_class_const:
6157 return v1->v.val_int == v2->v.val_int;
6158 case dw_val_class_unsigned_const:
6159 return v1->v.val_unsigned == v2->v.val_unsigned;
6160 case dw_val_class_const_double:
6161 return v1->v.val_double.high == v2->v.val_double.high
6162 && v1->v.val_double.low == v2->v.val_double.low;
6163 case dw_val_class_vec:
6164 if (v1->v.val_vec.length != v2->v.val_vec.length
6165 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6166 return 0;
6167 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6168 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6169 return 0;
6170 return 1;
6171 case dw_val_class_flag:
6172 return v1->v.val_flag == v2->v.val_flag;
6173 case dw_val_class_str:
6174 return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
6176 case dw_val_class_addr:
6177 r1 = v1->v.val_addr;
6178 r2 = v2->v.val_addr;
6179 if (GET_CODE (r1) != GET_CODE (r2))
6180 return 0;
6181 return !rtx_equal_p (r1, r2);
6183 case dw_val_class_offset:
6184 return v1->v.val_offset == v2->v.val_offset;
6186 case dw_val_class_loc:
6187 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6188 loc1 && loc2;
6189 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6190 if (!same_loc_p (loc1, loc2, mark))
6191 return 0;
6192 return !loc1 && !loc2;
6194 case dw_val_class_die_ref:
6195 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6197 case dw_val_class_fde_ref:
6198 case dw_val_class_vms_delta:
6199 case dw_val_class_lbl_id:
6200 case dw_val_class_lineptr:
6201 case dw_val_class_macptr:
6202 case dw_val_class_high_pc:
6203 return 1;
6205 case dw_val_class_file:
6206 return v1->v.val_file == v2->v.val_file;
6208 case dw_val_class_data8:
6209 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6211 default:
6212 return 1;
6216 /* Do the attributes look the same? */
6218 static int
6219 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6221 if (at1->dw_attr != at2->dw_attr)
6222 return 0;
6224 /* We don't care that this was compiled with a different compiler
6225 snapshot; if the output is the same, that's what matters. */
6226 if (at1->dw_attr == DW_AT_producer)
6227 return 1;
6229 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6232 /* Do the dies look the same? */
6234 static int
6235 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6237 dw_die_ref c1, c2;
6238 dw_attr_ref a1;
6239 unsigned ix;
6241 /* To avoid infinite recursion. */
6242 if (die1->die_mark)
6243 return die1->die_mark == die2->die_mark;
6244 die1->die_mark = die2->die_mark = ++(*mark);
6246 if (die1->die_tag != die2->die_tag)
6247 return 0;
6249 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6250 return 0;
6252 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6253 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6254 return 0;
6256 c1 = die1->die_child;
6257 c2 = die2->die_child;
6258 if (! c1)
6260 if (c2)
6261 return 0;
6263 else
6264 for (;;)
6266 if (!same_die_p (c1, c2, mark))
6267 return 0;
6268 c1 = c1->die_sib;
6269 c2 = c2->die_sib;
6270 if (c1 == die1->die_child)
6272 if (c2 == die2->die_child)
6273 break;
6274 else
6275 return 0;
6279 return 1;
6282 /* Do the dies look the same? Wrapper around same_die_p. */
6284 static int
6285 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6287 int mark = 0;
6288 int ret = same_die_p (die1, die2, &mark);
6290 unmark_all_dies (die1);
6291 unmark_all_dies (die2);
6293 return ret;
6296 /* The prefix to attach to symbols on DIEs in the current comdat debug
6297 info section. */
6298 static const char *comdat_symbol_id;
6300 /* The index of the current symbol within the current comdat CU. */
6301 static unsigned int comdat_symbol_number;
6303 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6304 children, and set comdat_symbol_id accordingly. */
6306 static void
6307 compute_section_prefix (dw_die_ref unit_die)
6309 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6310 const char *base = die_name ? lbasename (die_name) : "anonymous";
6311 char *name = XALLOCAVEC (char, strlen (base) + 64);
6312 char *p;
6313 int i, mark;
6314 unsigned char checksum[16];
6315 struct md5_ctx ctx;
6317 /* Compute the checksum of the DIE, then append part of it as hex digits to
6318 the name filename of the unit. */
6320 md5_init_ctx (&ctx);
6321 mark = 0;
6322 die_checksum (unit_die, &ctx, &mark);
6323 unmark_all_dies (unit_die);
6324 md5_finish_ctx (&ctx, checksum);
6326 sprintf (name, "%s.", base);
6327 clean_symbol_name (name);
6329 p = name + strlen (name);
6330 for (i = 0; i < 4; i++)
6332 sprintf (p, "%.2x", checksum[i]);
6333 p += 2;
6336 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6337 comdat_symbol_number = 0;
6340 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6342 static int
6343 is_type_die (dw_die_ref die)
6345 switch (die->die_tag)
6347 case DW_TAG_array_type:
6348 case DW_TAG_class_type:
6349 case DW_TAG_interface_type:
6350 case DW_TAG_enumeration_type:
6351 case DW_TAG_pointer_type:
6352 case DW_TAG_reference_type:
6353 case DW_TAG_rvalue_reference_type:
6354 case DW_TAG_string_type:
6355 case DW_TAG_structure_type:
6356 case DW_TAG_subroutine_type:
6357 case DW_TAG_union_type:
6358 case DW_TAG_ptr_to_member_type:
6359 case DW_TAG_set_type:
6360 case DW_TAG_subrange_type:
6361 case DW_TAG_base_type:
6362 case DW_TAG_const_type:
6363 case DW_TAG_file_type:
6364 case DW_TAG_packed_type:
6365 case DW_TAG_volatile_type:
6366 case DW_TAG_typedef:
6367 return 1;
6368 default:
6369 return 0;
6373 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6374 Basically, we want to choose the bits that are likely to be shared between
6375 compilations (types) and leave out the bits that are specific to individual
6376 compilations (functions). */
6378 static int
6379 is_comdat_die (dw_die_ref c)
6381 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6382 we do for stabs. The advantage is a greater likelihood of sharing between
6383 objects that don't include headers in the same order (and therefore would
6384 put the base types in a different comdat). jason 8/28/00 */
6386 if (c->die_tag == DW_TAG_base_type)
6387 return 0;
6389 if (c->die_tag == DW_TAG_pointer_type
6390 || c->die_tag == DW_TAG_reference_type
6391 || c->die_tag == DW_TAG_rvalue_reference_type
6392 || c->die_tag == DW_TAG_const_type
6393 || c->die_tag == DW_TAG_volatile_type)
6395 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6397 return t ? is_comdat_die (t) : 0;
6400 return is_type_die (c);
6403 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6404 compilation unit. */
6406 static int
6407 is_symbol_die (dw_die_ref c)
6409 return (is_type_die (c)
6410 || is_declaration_die (c)
6411 || c->die_tag == DW_TAG_namespace
6412 || c->die_tag == DW_TAG_module);
6415 /* Returns true iff C is a compile-unit DIE. */
6417 static inline bool
6418 is_cu_die (dw_die_ref c)
6420 return c && c->die_tag == DW_TAG_compile_unit;
6423 /* Returns true iff C is a unit DIE of some sort. */
6425 static inline bool
6426 is_unit_die (dw_die_ref c)
6428 return c && (c->die_tag == DW_TAG_compile_unit
6429 || c->die_tag == DW_TAG_partial_unit
6430 || c->die_tag == DW_TAG_type_unit);
6433 /* Returns true iff C is a namespace DIE. */
6435 static inline bool
6436 is_namespace_die (dw_die_ref c)
6438 return c && c->die_tag == DW_TAG_namespace;
6441 /* Returns true iff C is a class or structure DIE. */
6443 static inline bool
6444 is_class_die (dw_die_ref c)
6446 return c && (c->die_tag == DW_TAG_class_type
6447 || c->die_tag == DW_TAG_structure_type);
6450 static char *
6451 gen_internal_sym (const char *prefix)
6453 char buf[256];
6455 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6456 return xstrdup (buf);
6459 /* Assign symbols to all worthy DIEs under DIE. */
6461 static void
6462 assign_symbol_names (dw_die_ref die)
6464 dw_die_ref c;
6466 if (is_symbol_die (die) && !die->comdat_type_p)
6468 if (comdat_symbol_id)
6470 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6472 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6473 comdat_symbol_id, comdat_symbol_number++);
6474 die->die_id.die_symbol = xstrdup (p);
6476 else
6477 die->die_id.die_symbol = gen_internal_sym ("LDIE");
6480 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6483 struct cu_hash_table_entry
6485 dw_die_ref cu;
6486 unsigned min_comdat_num, max_comdat_num;
6487 struct cu_hash_table_entry *next;
6490 /* Routines to manipulate hash table of CUs. */
6491 static hashval_t
6492 htab_cu_hash (const void *of)
6494 const struct cu_hash_table_entry *const entry =
6495 (const struct cu_hash_table_entry *) of;
6497 return htab_hash_string (entry->cu->die_id.die_symbol);
6500 static int
6501 htab_cu_eq (const void *of1, const void *of2)
6503 const struct cu_hash_table_entry *const entry1 =
6504 (const struct cu_hash_table_entry *) of1;
6505 const struct die_struct *const entry2 = (const struct die_struct *) of2;
6507 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6510 static void
6511 htab_cu_del (void *what)
6513 struct cu_hash_table_entry *next,
6514 *entry = (struct cu_hash_table_entry *) what;
6516 while (entry)
6518 next = entry->next;
6519 free (entry);
6520 entry = next;
6524 /* Check whether we have already seen this CU and set up SYM_NUM
6525 accordingly. */
6526 static int
6527 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
6529 struct cu_hash_table_entry dummy;
6530 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6532 dummy.max_comdat_num = 0;
6534 slot = (struct cu_hash_table_entry **)
6535 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
6536 INSERT);
6537 entry = *slot;
6539 for (; entry; last = entry, entry = entry->next)
6541 if (same_die_p_wrap (cu, entry->cu))
6542 break;
6545 if (entry)
6547 *sym_num = entry->min_comdat_num;
6548 return 1;
6551 entry = XCNEW (struct cu_hash_table_entry);
6552 entry->cu = cu;
6553 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6554 entry->next = *slot;
6555 *slot = entry;
6557 return 0;
6560 /* Record SYM_NUM to record of CU in HTABLE. */
6561 static void
6562 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
6564 struct cu_hash_table_entry **slot, *entry;
6566 slot = (struct cu_hash_table_entry **)
6567 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
6568 NO_INSERT);
6569 entry = *slot;
6571 entry->max_comdat_num = sym_num;
6574 /* Traverse the DIE (which is always comp_unit_die), and set up
6575 additional compilation units for each of the include files we see
6576 bracketed by BINCL/EINCL. */
6578 static void
6579 break_out_includes (dw_die_ref die)
6581 dw_die_ref c;
6582 dw_die_ref unit = NULL;
6583 limbo_die_node *node, **pnode;
6584 htab_t cu_hash_table;
6586 c = die->die_child;
6587 if (c) do {
6588 dw_die_ref prev = c;
6589 c = c->die_sib;
6590 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6591 || (unit && is_comdat_die (c)))
6593 dw_die_ref next = c->die_sib;
6595 /* This DIE is for a secondary CU; remove it from the main one. */
6596 remove_child_with_prev (c, prev);
6598 if (c->die_tag == DW_TAG_GNU_BINCL)
6599 unit = push_new_compile_unit (unit, c);
6600 else if (c->die_tag == DW_TAG_GNU_EINCL)
6601 unit = pop_compile_unit (unit);
6602 else
6603 add_child_die (unit, c);
6604 c = next;
6605 if (c == die->die_child)
6606 break;
6608 } while (c != die->die_child);
6610 #if 0
6611 /* We can only use this in debugging, since the frontend doesn't check
6612 to make sure that we leave every include file we enter. */
6613 gcc_assert (!unit);
6614 #endif
6616 assign_symbol_names (die);
6617 cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6618 for (node = limbo_die_list, pnode = &limbo_die_list;
6619 node;
6620 node = node->next)
6622 int is_dupl;
6624 compute_section_prefix (node->die);
6625 is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6626 &comdat_symbol_number);
6627 assign_symbol_names (node->die);
6628 if (is_dupl)
6629 *pnode = node->next;
6630 else
6632 pnode = &node->next;
6633 record_comdat_symbol_number (node->die, cu_hash_table,
6634 comdat_symbol_number);
6637 htab_delete (cu_hash_table);
6640 /* Return non-zero if this DIE is a declaration. */
6642 static int
6643 is_declaration_die (dw_die_ref die)
6645 dw_attr_ref a;
6646 unsigned ix;
6648 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6649 if (a->dw_attr == DW_AT_declaration)
6650 return 1;
6652 return 0;
6655 /* Return non-zero if this DIE is nested inside a subprogram. */
6657 static int
6658 is_nested_in_subprogram (dw_die_ref die)
6660 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
6662 if (decl == NULL)
6663 decl = die;
6664 return local_scope_p (decl);
6667 /* Return non-zero if this DIE contains a defining declaration of a
6668 subprogram. */
6670 static int
6671 contains_subprogram_definition (dw_die_ref die)
6673 dw_die_ref c;
6675 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
6676 return 1;
6677 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition(c)) return 1);
6678 return 0;
6681 /* Return non-zero if this is a type DIE that should be moved to a
6682 COMDAT .debug_types section. */
6684 static int
6685 should_move_die_to_comdat (dw_die_ref die)
6687 switch (die->die_tag)
6689 case DW_TAG_class_type:
6690 case DW_TAG_structure_type:
6691 case DW_TAG_enumeration_type:
6692 case DW_TAG_union_type:
6693 /* Don't move declarations, inlined instances, or types nested in a
6694 subprogram. */
6695 if (is_declaration_die (die)
6696 || get_AT (die, DW_AT_abstract_origin)
6697 || is_nested_in_subprogram (die))
6698 return 0;
6699 /* A type definition should never contain a subprogram definition. */
6700 gcc_assert (!contains_subprogram_definition (die));
6701 return 1;
6702 case DW_TAG_array_type:
6703 case DW_TAG_interface_type:
6704 case DW_TAG_pointer_type:
6705 case DW_TAG_reference_type:
6706 case DW_TAG_rvalue_reference_type:
6707 case DW_TAG_string_type:
6708 case DW_TAG_subroutine_type:
6709 case DW_TAG_ptr_to_member_type:
6710 case DW_TAG_set_type:
6711 case DW_TAG_subrange_type:
6712 case DW_TAG_base_type:
6713 case DW_TAG_const_type:
6714 case DW_TAG_file_type:
6715 case DW_TAG_packed_type:
6716 case DW_TAG_volatile_type:
6717 case DW_TAG_typedef:
6718 default:
6719 return 0;
6723 /* Make a clone of DIE. */
6725 static dw_die_ref
6726 clone_die (dw_die_ref die)
6728 dw_die_ref clone;
6729 dw_attr_ref a;
6730 unsigned ix;
6732 clone = ggc_alloc_cleared_die_node ();
6733 clone->die_tag = die->die_tag;
6735 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6736 add_dwarf_attr (clone, a);
6738 return clone;
6741 /* Make a clone of the tree rooted at DIE. */
6743 static dw_die_ref
6744 clone_tree (dw_die_ref die)
6746 dw_die_ref c;
6747 dw_die_ref clone = clone_die (die);
6749 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
6751 return clone;
6754 /* Make a clone of DIE as a declaration. */
6756 static dw_die_ref
6757 clone_as_declaration (dw_die_ref die)
6759 dw_die_ref clone;
6760 dw_die_ref decl;
6761 dw_attr_ref a;
6762 unsigned ix;
6764 /* If the DIE is already a declaration, just clone it. */
6765 if (is_declaration_die (die))
6766 return clone_die (die);
6768 /* If the DIE is a specification, just clone its declaration DIE. */
6769 decl = get_AT_ref (die, DW_AT_specification);
6770 if (decl != NULL)
6772 clone = clone_die (decl);
6773 if (die->comdat_type_p)
6774 add_AT_die_ref (clone, DW_AT_signature, die);
6775 return clone;
6778 clone = ggc_alloc_cleared_die_node ();
6779 clone->die_tag = die->die_tag;
6781 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6783 /* We don't want to copy over all attributes.
6784 For example we don't want DW_AT_byte_size because otherwise we will no
6785 longer have a declaration and GDB will treat it as a definition. */
6787 switch (a->dw_attr)
6789 case DW_AT_artificial:
6790 case DW_AT_containing_type:
6791 case DW_AT_external:
6792 case DW_AT_name:
6793 case DW_AT_type:
6794 case DW_AT_virtuality:
6795 case DW_AT_linkage_name:
6796 case DW_AT_MIPS_linkage_name:
6797 add_dwarf_attr (clone, a);
6798 break;
6799 case DW_AT_byte_size:
6800 default:
6801 break;
6805 if (die->comdat_type_p)
6806 add_AT_die_ref (clone, DW_AT_signature, die);
6808 add_AT_flag (clone, DW_AT_declaration, 1);
6809 return clone;
6812 /* Copy the declaration context to the new type unit DIE. This includes
6813 any surrounding namespace or type declarations. If the DIE has an
6814 AT_specification attribute, it also includes attributes and children
6815 attached to the specification, and returns a pointer to the original
6816 parent of the declaration DIE. Returns NULL otherwise. */
6818 static dw_die_ref
6819 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
6821 dw_die_ref decl;
6822 dw_die_ref new_decl;
6823 dw_die_ref orig_parent = NULL;
6825 decl = get_AT_ref (die, DW_AT_specification);
6826 if (decl == NULL)
6827 decl = die;
6828 else
6830 unsigned ix;
6831 dw_die_ref c;
6832 dw_attr_ref a;
6834 /* The original DIE will be changed to a declaration, and must
6835 be moved to be a child of the original declaration DIE. */
6836 orig_parent = decl->die_parent;
6838 /* Copy the type node pointer from the new DIE to the original
6839 declaration DIE so we can forward references later. */
6840 decl->comdat_type_p = true;
6841 decl->die_id.die_type_node = die->die_id.die_type_node;
6843 remove_AT (die, DW_AT_specification);
6845 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
6847 if (a->dw_attr != DW_AT_name
6848 && a->dw_attr != DW_AT_declaration
6849 && a->dw_attr != DW_AT_external)
6850 add_dwarf_attr (die, a);
6853 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
6856 if (decl->die_parent != NULL
6857 && !is_unit_die (decl->die_parent))
6859 new_decl = copy_ancestor_tree (unit, decl, NULL);
6860 if (new_decl != NULL)
6862 remove_AT (new_decl, DW_AT_signature);
6863 add_AT_specification (die, new_decl);
6867 return orig_parent;
6870 /* Generate the skeleton ancestor tree for the given NODE, then clone
6871 the DIE and add the clone into the tree. */
6873 static void
6874 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
6876 if (node->new_die != NULL)
6877 return;
6879 node->new_die = clone_as_declaration (node->old_die);
6881 if (node->parent != NULL)
6883 generate_skeleton_ancestor_tree (node->parent);
6884 add_child_die (node->parent->new_die, node->new_die);
6888 /* Generate a skeleton tree of DIEs containing any declarations that are
6889 found in the original tree. We traverse the tree looking for declaration
6890 DIEs, and construct the skeleton from the bottom up whenever we find one. */
6892 static void
6893 generate_skeleton_bottom_up (skeleton_chain_node *parent)
6895 skeleton_chain_node node;
6896 dw_die_ref c;
6897 dw_die_ref first;
6898 dw_die_ref prev = NULL;
6899 dw_die_ref next = NULL;
6901 node.parent = parent;
6903 first = c = parent->old_die->die_child;
6904 if (c)
6905 next = c->die_sib;
6906 if (c) do {
6907 if (prev == NULL || prev->die_sib == c)
6908 prev = c;
6909 c = next;
6910 next = (c == first ? NULL : c->die_sib);
6911 node.old_die = c;
6912 node.new_die = NULL;
6913 if (is_declaration_die (c))
6915 /* Clone the existing DIE, move the original to the skeleton
6916 tree (which is in the main CU), and put the clone, with
6917 all the original's children, where the original came from. */
6918 dw_die_ref clone = clone_die (c);
6919 move_all_children (c, clone);
6921 replace_child (c, clone, prev);
6922 generate_skeleton_ancestor_tree (parent);
6923 add_child_die (parent->new_die, c);
6924 node.new_die = c;
6925 c = clone;
6927 generate_skeleton_bottom_up (&node);
6928 } while (next != NULL);
6931 /* Wrapper function for generate_skeleton_bottom_up. */
6933 static dw_die_ref
6934 generate_skeleton (dw_die_ref die)
6936 skeleton_chain_node node;
6938 node.old_die = die;
6939 node.new_die = NULL;
6940 node.parent = NULL;
6942 /* If this type definition is nested inside another type,
6943 always leave at least a declaration in its place. */
6944 if (die->die_parent != NULL && is_type_die (die->die_parent))
6945 node.new_die = clone_as_declaration (die);
6947 generate_skeleton_bottom_up (&node);
6948 return node.new_die;
6951 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
6952 declaration. The original DIE is moved to a new compile unit so that
6953 existing references to it follow it to the new location. If any of the
6954 original DIE's descendants is a declaration, we need to replace the
6955 original DIE with a skeleton tree and move the declarations back into the
6956 skeleton tree. */
6958 static dw_die_ref
6959 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
6960 dw_die_ref prev)
6962 dw_die_ref skeleton, orig_parent;
6964 /* Copy the declaration context to the type unit DIE. If the returned
6965 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
6966 that DIE. */
6967 orig_parent = copy_declaration_context (unit, child);
6969 skeleton = generate_skeleton (child);
6970 if (skeleton == NULL)
6971 remove_child_with_prev (child, prev);
6972 else
6974 skeleton->comdat_type_p = true;
6975 skeleton->die_id.die_type_node = child->die_id.die_type_node;
6977 /* If the original DIE was a specification, we need to put
6978 the skeleton under the parent DIE of the declaration.
6979 This leaves the original declaration in the tree, but
6980 it will be pruned later since there are no longer any
6981 references to it. */
6982 if (orig_parent != NULL)
6984 remove_child_with_prev (child, prev);
6985 add_child_die (orig_parent, skeleton);
6987 else
6988 replace_child (child, skeleton, prev);
6991 return skeleton;
6994 /* Traverse the DIE and set up additional .debug_types sections for each
6995 type worthy of being placed in a COMDAT section. */
6997 static void
6998 break_out_comdat_types (dw_die_ref die)
7000 dw_die_ref c;
7001 dw_die_ref first;
7002 dw_die_ref prev = NULL;
7003 dw_die_ref next = NULL;
7004 dw_die_ref unit = NULL;
7006 first = c = die->die_child;
7007 if (c)
7008 next = c->die_sib;
7009 if (c) do {
7010 if (prev == NULL || prev->die_sib == c)
7011 prev = c;
7012 c = next;
7013 next = (c == first ? NULL : c->die_sib);
7014 if (should_move_die_to_comdat (c))
7016 dw_die_ref replacement;
7017 comdat_type_node_ref type_node;
7019 /* Create a new type unit DIE as the root for the new tree, and
7020 add it to the list of comdat types. */
7021 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7022 add_AT_unsigned (unit, DW_AT_language,
7023 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7024 type_node = ggc_alloc_cleared_comdat_type_node ();
7025 type_node->root_die = unit;
7026 type_node->next = comdat_type_list;
7027 comdat_type_list = type_node;
7029 /* Generate the type signature. */
7030 generate_type_signature (c, type_node);
7032 /* Copy the declaration context, attributes, and children of the
7033 declaration into the new type unit DIE, then remove this DIE
7034 from the main CU (or replace it with a skeleton if necessary). */
7035 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7036 type_node->skeleton_die = replacement;
7038 /* Break out nested types into their own type units. */
7039 break_out_comdat_types (c);
7041 /* Add the DIE to the new compunit. */
7042 add_child_die (unit, c);
7044 if (replacement != NULL)
7045 c = replacement;
7047 else if (c->die_tag == DW_TAG_namespace
7048 || c->die_tag == DW_TAG_class_type
7049 || c->die_tag == DW_TAG_structure_type
7050 || c->die_tag == DW_TAG_union_type)
7052 /* Look for nested types that can be broken out. */
7053 break_out_comdat_types (c);
7055 } while (next != NULL);
7058 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7060 struct decl_table_entry
7062 dw_die_ref orig;
7063 dw_die_ref copy;
7066 /* Routines to manipulate hash table of copied declarations. */
7068 static hashval_t
7069 htab_decl_hash (const void *of)
7071 const struct decl_table_entry *const entry =
7072 (const struct decl_table_entry *) of;
7074 return htab_hash_pointer (entry->orig);
7077 static int
7078 htab_decl_eq (const void *of1, const void *of2)
7080 const struct decl_table_entry *const entry1 =
7081 (const struct decl_table_entry *) of1;
7082 const struct die_struct *const entry2 = (const struct die_struct *) of2;
7084 return entry1->orig == entry2;
7087 static void
7088 htab_decl_del (void *what)
7090 struct decl_table_entry *entry = (struct decl_table_entry *) what;
7092 free (entry);
7095 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7096 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7097 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7098 to check if the ancestor has already been copied into UNIT. */
7100 static dw_die_ref
7101 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
7103 dw_die_ref parent = die->die_parent;
7104 dw_die_ref new_parent = unit;
7105 dw_die_ref copy;
7106 void **slot = NULL;
7107 struct decl_table_entry *entry = NULL;
7109 if (decl_table)
7111 /* Check if the entry has already been copied to UNIT. */
7112 slot = htab_find_slot_with_hash (decl_table, die,
7113 htab_hash_pointer (die), INSERT);
7114 if (*slot != HTAB_EMPTY_ENTRY)
7116 entry = (struct decl_table_entry *) *slot;
7117 return entry->copy;
7120 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7121 entry = XCNEW (struct decl_table_entry);
7122 entry->orig = die;
7123 entry->copy = NULL;
7124 *slot = entry;
7127 if (parent != NULL)
7129 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7130 if (spec != NULL)
7131 parent = spec;
7132 if (!is_unit_die (parent))
7133 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7136 copy = clone_as_declaration (die);
7137 add_child_die (new_parent, copy);
7139 if (decl_table != NULL)
7141 /* Record the pointer to the copy. */
7142 entry->copy = copy;
7145 return copy;
7148 /* Like clone_tree, but additionally enter all the children into
7149 the hash table decl_table. */
7151 static dw_die_ref
7152 clone_tree_hash (dw_die_ref die, htab_t decl_table)
7154 dw_die_ref c;
7155 dw_die_ref clone = clone_die (die);
7156 struct decl_table_entry *entry;
7157 void **slot = htab_find_slot_with_hash (decl_table, die,
7158 htab_hash_pointer (die), INSERT);
7159 /* Assert that DIE isn't in the hash table yet. If it would be there
7160 before, the ancestors would be necessarily there as well, therefore
7161 clone_tree_hash wouldn't be called. */
7162 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7163 entry = XCNEW (struct decl_table_entry);
7164 entry->orig = die;
7165 entry->copy = clone;
7166 *slot = entry;
7168 FOR_EACH_CHILD (die, c,
7169 add_child_die (clone, clone_tree_hash (c, decl_table)));
7171 return clone;
7174 /* Walk the DIE and its children, looking for references to incomplete
7175 or trivial types that are unmarked (i.e., that are not in the current
7176 type_unit). */
7178 static void
7179 copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
7181 dw_die_ref c;
7182 dw_attr_ref a;
7183 unsigned ix;
7185 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7187 if (AT_class (a) == dw_val_class_die_ref)
7189 dw_die_ref targ = AT_ref (a);
7190 void **slot;
7191 struct decl_table_entry *entry;
7193 if (targ->die_mark != 0 || targ->comdat_type_p)
7194 continue;
7196 slot = htab_find_slot_with_hash (decl_table, targ,
7197 htab_hash_pointer (targ), INSERT);
7199 if (*slot != HTAB_EMPTY_ENTRY)
7201 /* TARG has already been copied, so we just need to
7202 modify the reference to point to the copy. */
7203 entry = (struct decl_table_entry *) *slot;
7204 a->dw_attr_val.v.val_die_ref.die = entry->copy;
7206 else
7208 dw_die_ref parent = unit;
7209 dw_die_ref copy = clone_die (targ);
7211 /* Record in DECL_TABLE that TARG has been copied.
7212 Need to do this now, before the recursive call,
7213 because DECL_TABLE may be expanded and SLOT
7214 would no longer be a valid pointer. */
7215 entry = XCNEW (struct decl_table_entry);
7216 entry->orig = targ;
7217 entry->copy = copy;
7218 *slot = entry;
7220 FOR_EACH_CHILD (targ, c,
7221 add_child_die (copy,
7222 clone_tree_hash (c, decl_table)));
7224 /* Make sure the cloned tree is marked as part of the
7225 type unit. */
7226 mark_dies (copy);
7228 /* If TARG has surrounding context, copy its ancestor tree
7229 into the new type unit. */
7230 if (targ->die_parent != NULL
7231 && !is_unit_die (targ->die_parent))
7232 parent = copy_ancestor_tree (unit, targ->die_parent,
7233 decl_table);
7235 add_child_die (parent, copy);
7236 a->dw_attr_val.v.val_die_ref.die = copy;
7238 /* Make sure the newly-copied DIE is walked. If it was
7239 installed in a previously-added context, it won't
7240 get visited otherwise. */
7241 if (parent != unit)
7243 /* Find the highest point of the newly-added tree,
7244 mark each node along the way, and walk from there. */
7245 parent->die_mark = 1;
7246 while (parent->die_parent
7247 && parent->die_parent->die_mark == 0)
7249 parent = parent->die_parent;
7250 parent->die_mark = 1;
7252 copy_decls_walk (unit, parent, decl_table);
7258 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7261 /* Copy declarations for "unworthy" types into the new comdat section.
7262 Incomplete types, modified types, and certain other types aren't broken
7263 out into comdat sections of their own, so they don't have a signature,
7264 and we need to copy the declaration into the same section so that we
7265 don't have an external reference. */
7267 static void
7268 copy_decls_for_unworthy_types (dw_die_ref unit)
7270 htab_t decl_table;
7272 mark_dies (unit);
7273 decl_table = htab_create (10, htab_decl_hash, htab_decl_eq, htab_decl_del);
7274 copy_decls_walk (unit, unit, decl_table);
7275 htab_delete (decl_table);
7276 unmark_dies (unit);
7279 /* Traverse the DIE and add a sibling attribute if it may have the
7280 effect of speeding up access to siblings. To save some space,
7281 avoid generating sibling attributes for DIE's without children. */
7283 static void
7284 add_sibling_attributes (dw_die_ref die)
7286 dw_die_ref c;
7288 if (! die->die_child)
7289 return;
7291 if (die->die_parent && die != die->die_parent->die_child)
7292 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7294 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7297 /* Output all location lists for the DIE and its children. */
7299 static void
7300 output_location_lists (dw_die_ref die)
7302 dw_die_ref c;
7303 dw_attr_ref a;
7304 unsigned ix;
7306 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7307 if (AT_class (a) == dw_val_class_loc_list)
7308 output_loc_list (AT_loc_list (a));
7310 FOR_EACH_CHILD (die, c, output_location_lists (c));
7313 /* We want to limit the number of external references, because they are
7314 larger than local references: a relocation takes multiple words, and
7315 even a sig8 reference is always eight bytes, whereas a local reference
7316 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7317 So if we encounter multiple external references to the same type DIE, we
7318 make a local typedef stub for it and redirect all references there.
7320 This is the element of the hash table for keeping track of these
7321 references. */
7323 struct external_ref
7325 dw_die_ref type;
7326 dw_die_ref stub;
7327 unsigned n_refs;
7330 /* Hash an external_ref. */
7332 static hashval_t
7333 hash_external_ref (const void *p)
7335 const struct external_ref *r = (const struct external_ref *)p;
7336 return htab_hash_pointer (r->type);
7339 /* Compare external_refs. */
7341 static int
7342 external_ref_eq (const void *p1, const void *p2)
7344 const struct external_ref *r1 = (const struct external_ref *)p1;
7345 const struct external_ref *r2 = (const struct external_ref *)p2;
7346 return r1->type == r2->type;
7349 /* Return a pointer to the external_ref for references to DIE. */
7351 static struct external_ref *
7352 lookup_external_ref (htab_t map, dw_die_ref die)
7354 struct external_ref ref, *ref_p;
7355 void ** slot;
7357 ref.type = die;
7358 slot = htab_find_slot (map, &ref, INSERT);
7359 if (*slot != HTAB_EMPTY_ENTRY)
7360 return (struct external_ref *) *slot;
7362 ref_p = XCNEW (struct external_ref);
7363 ref_p->type = die;
7364 *slot = ref_p;
7365 return ref_p;
7368 /* Subroutine of optimize_external_refs, below.
7370 If we see a type skeleton, record it as our stub. If we see external
7371 references, remember how many we've seen. */
7373 static void
7374 optimize_external_refs_1 (dw_die_ref die, htab_t map)
7376 dw_die_ref c;
7377 dw_attr_ref a;
7378 unsigned ix;
7379 struct external_ref *ref_p;
7381 if (is_type_die (die)
7382 && (c = get_AT_ref (die, DW_AT_signature)))
7384 /* This is a local skeleton; use it for local references. */
7385 ref_p = lookup_external_ref (map, c);
7386 ref_p->stub = die;
7389 /* Scan the DIE references, and remember any that refer to DIEs from
7390 other CUs (i.e. those which are not marked). */
7391 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7392 if (AT_class (a) == dw_val_class_die_ref
7393 && (c = AT_ref (a))->die_mark == 0
7394 && is_type_die (c))
7396 ref_p = lookup_external_ref (map, c);
7397 ref_p->n_refs++;
7400 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7403 /* htab_traverse callback function for optimize_external_refs, below. SLOT
7404 points to an external_ref, DATA is the CU we're processing. If we don't
7405 already have a local stub, and we have multiple refs, build a stub. */
7407 static int
7408 build_local_stub (void **slot, void *data)
7410 struct external_ref *ref_p = (struct external_ref *)*slot;
7412 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7414 /* We have multiple references to this type, so build a small stub.
7415 Both of these forms are a bit dodgy from the perspective of the
7416 DWARF standard, since technically they should have names. */
7417 dw_die_ref cu = (dw_die_ref) data;
7418 dw_die_ref type = ref_p->type;
7419 dw_die_ref stub = NULL;
7421 if (type->comdat_type_p)
7423 /* If we refer to this type via sig8, use AT_signature. */
7424 stub = new_die (type->die_tag, cu, NULL_TREE);
7425 add_AT_die_ref (stub, DW_AT_signature, type);
7427 else
7429 /* Otherwise, use a typedef with no name. */
7430 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7431 add_AT_die_ref (stub, DW_AT_type, type);
7434 stub->die_mark++;
7435 ref_p->stub = stub;
7437 return 1;
7440 /* DIE is a unit; look through all the DIE references to see if there are
7441 any external references to types, and if so, create local stubs for
7442 them which will be applied in build_abbrev_table. This is useful because
7443 references to local DIEs are smaller. */
7445 static htab_t
7446 optimize_external_refs (dw_die_ref die)
7448 htab_t map = htab_create (10, hash_external_ref, external_ref_eq, free);
7449 optimize_external_refs_1 (die, map);
7450 htab_traverse (map, build_local_stub, die);
7451 return map;
7454 /* The format of each DIE (and its attribute value pairs) is encoded in an
7455 abbreviation table. This routine builds the abbreviation table and assigns
7456 a unique abbreviation id for each abbreviation entry. The children of each
7457 die are visited recursively. */
7459 static void
7460 build_abbrev_table (dw_die_ref die, htab_t extern_map)
7462 unsigned long abbrev_id;
7463 unsigned int n_alloc;
7464 dw_die_ref c;
7465 dw_attr_ref a;
7466 unsigned ix;
7468 /* Scan the DIE references, and replace any that refer to
7469 DIEs from other CUs (i.e. those which are not marked) with
7470 the local stubs we built in optimize_external_refs. */
7471 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7472 if (AT_class (a) == dw_val_class_die_ref
7473 && (c = AT_ref (a))->die_mark == 0)
7475 struct external_ref *ref_p;
7476 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
7478 ref_p = lookup_external_ref (extern_map, c);
7479 if (ref_p->stub && ref_p->stub != die)
7480 change_AT_die_ref (a, ref_p->stub);
7481 else
7482 /* We aren't changing this reference, so mark it external. */
7483 set_AT_ref_external (a, 1);
7486 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7488 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7489 dw_attr_ref die_a, abbrev_a;
7490 unsigned ix;
7491 bool ok = true;
7493 if (abbrev->die_tag != die->die_tag)
7494 continue;
7495 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7496 continue;
7498 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
7499 continue;
7501 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
7503 abbrev_a = &(*abbrev->die_attr)[ix];
7504 if ((abbrev_a->dw_attr != die_a->dw_attr)
7505 || (value_format (abbrev_a) != value_format (die_a)))
7507 ok = false;
7508 break;
7511 if (ok)
7512 break;
7515 if (abbrev_id >= abbrev_die_table_in_use)
7517 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7519 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7520 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7521 n_alloc);
7523 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7524 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7525 abbrev_die_table_allocated = n_alloc;
7528 ++abbrev_die_table_in_use;
7529 abbrev_die_table[abbrev_id] = die;
7532 die->die_abbrev = abbrev_id;
7533 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
7536 /* Return the power-of-two number of bytes necessary to represent VALUE. */
7538 static int
7539 constant_size (unsigned HOST_WIDE_INT value)
7541 int log;
7543 if (value == 0)
7544 log = 0;
7545 else
7546 log = floor_log2 (value);
7548 log = log / 8;
7549 log = 1 << (floor_log2 (log) + 1);
7551 return log;
7554 /* Return the size of a DIE as it is represented in the
7555 .debug_info section. */
7557 static unsigned long
7558 size_of_die (dw_die_ref die)
7560 unsigned long size = 0;
7561 dw_attr_ref a;
7562 unsigned ix;
7563 enum dwarf_form form;
7565 size += size_of_uleb128 (die->die_abbrev);
7566 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7568 switch (AT_class (a))
7570 case dw_val_class_addr:
7571 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7573 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7574 size += size_of_uleb128 (AT_index (a));
7576 else
7577 size += DWARF2_ADDR_SIZE;
7578 break;
7579 case dw_val_class_offset:
7580 size += DWARF_OFFSET_SIZE;
7581 break;
7582 case dw_val_class_loc:
7584 unsigned long lsize = size_of_locs (AT_loc (a));
7586 /* Block length. */
7587 if (dwarf_version >= 4)
7588 size += size_of_uleb128 (lsize);
7589 else
7590 size += constant_size (lsize);
7591 size += lsize;
7593 break;
7594 case dw_val_class_loc_list:
7595 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7597 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7598 size += size_of_uleb128 (AT_index (a));
7600 else
7601 size += DWARF_OFFSET_SIZE;
7602 break;
7603 case dw_val_class_range_list:
7604 size += DWARF_OFFSET_SIZE;
7605 break;
7606 case dw_val_class_const:
7607 size += size_of_sleb128 (AT_int (a));
7608 break;
7609 case dw_val_class_unsigned_const:
7611 int csize = constant_size (AT_unsigned (a));
7612 if (dwarf_version == 3
7613 && a->dw_attr == DW_AT_data_member_location
7614 && csize >= 4)
7615 size += size_of_uleb128 (AT_unsigned (a));
7616 else
7617 size += csize;
7619 break;
7620 case dw_val_class_const_double:
7621 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
7622 if (HOST_BITS_PER_WIDE_INT >= 64)
7623 size++; /* block */
7624 break;
7625 case dw_val_class_vec:
7626 size += constant_size (a->dw_attr_val.v.val_vec.length
7627 * a->dw_attr_val.v.val_vec.elt_size)
7628 + a->dw_attr_val.v.val_vec.length
7629 * a->dw_attr_val.v.val_vec.elt_size; /* block */
7630 break;
7631 case dw_val_class_flag:
7632 if (dwarf_version >= 4)
7633 /* Currently all add_AT_flag calls pass in 1 as last argument,
7634 so DW_FORM_flag_present can be used. If that ever changes,
7635 we'll need to use DW_FORM_flag and have some optimization
7636 in build_abbrev_table that will change those to
7637 DW_FORM_flag_present if it is set to 1 in all DIEs using
7638 the same abbrev entry. */
7639 gcc_assert (a->dw_attr_val.v.val_flag == 1);
7640 else
7641 size += 1;
7642 break;
7643 case dw_val_class_die_ref:
7644 if (AT_ref_external (a))
7646 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
7647 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
7648 is sized by target address length, whereas in DWARF3
7649 it's always sized as an offset. */
7650 if (use_debug_types)
7651 size += DWARF_TYPE_SIGNATURE_SIZE;
7652 else if (dwarf_version == 2)
7653 size += DWARF2_ADDR_SIZE;
7654 else
7655 size += DWARF_OFFSET_SIZE;
7657 else
7658 size += DWARF_OFFSET_SIZE;
7659 break;
7660 case dw_val_class_fde_ref:
7661 size += DWARF_OFFSET_SIZE;
7662 break;
7663 case dw_val_class_lbl_id:
7664 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7666 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7667 size += size_of_uleb128 (AT_index (a));
7669 else
7670 size += DWARF2_ADDR_SIZE;
7671 break;
7672 case dw_val_class_lineptr:
7673 case dw_val_class_macptr:
7674 size += DWARF_OFFSET_SIZE;
7675 break;
7676 case dw_val_class_str:
7677 form = AT_string_form (a);
7678 if (form == DW_FORM_strp)
7679 size += DWARF_OFFSET_SIZE;
7680 else if (form == DW_FORM_GNU_str_index)
7681 size += size_of_uleb128 (AT_index (a));
7682 else
7683 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7684 break;
7685 case dw_val_class_file:
7686 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7687 break;
7688 case dw_val_class_data8:
7689 size += 8;
7690 break;
7691 case dw_val_class_vms_delta:
7692 size += DWARF_OFFSET_SIZE;
7693 break;
7694 case dw_val_class_high_pc:
7695 size += DWARF2_ADDR_SIZE;
7696 break;
7697 default:
7698 gcc_unreachable ();
7702 return size;
7705 /* Size the debugging information associated with a given DIE. Visits the
7706 DIE's children recursively. Updates the global variable next_die_offset, on
7707 each time through. Uses the current value of next_die_offset to update the
7708 die_offset field in each DIE. */
7710 static void
7711 calc_die_sizes (dw_die_ref die)
7713 dw_die_ref c;
7715 gcc_assert (die->die_offset == 0
7716 || (unsigned long int) die->die_offset == next_die_offset);
7717 die->die_offset = next_die_offset;
7718 next_die_offset += size_of_die (die);
7720 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
7722 if (die->die_child != NULL)
7723 /* Count the null byte used to terminate sibling lists. */
7724 next_die_offset += 1;
7727 /* Size just the base type children at the start of the CU.
7728 This is needed because build_abbrev needs to size locs
7729 and sizing of type based stack ops needs to know die_offset
7730 values for the base types. */
7732 static void
7733 calc_base_type_die_sizes (void)
7735 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7736 unsigned int i;
7737 dw_die_ref base_type;
7738 #if ENABLE_ASSERT_CHECKING
7739 dw_die_ref prev = comp_unit_die ()->die_child;
7740 #endif
7742 die_offset += size_of_die (comp_unit_die ());
7743 for (i = 0; base_types.iterate (i, &base_type); i++)
7745 #if ENABLE_ASSERT_CHECKING
7746 gcc_assert (base_type->die_offset == 0
7747 && prev->die_sib == base_type
7748 && base_type->die_child == NULL
7749 && base_type->die_abbrev);
7750 prev = base_type;
7751 #endif
7752 base_type->die_offset = die_offset;
7753 die_offset += size_of_die (base_type);
7757 /* Set the marks for a die and its children. We do this so
7758 that we know whether or not a reference needs to use FORM_ref_addr; only
7759 DIEs in the same CU will be marked. We used to clear out the offset
7760 and use that as the flag, but ran into ordering problems. */
7762 static void
7763 mark_dies (dw_die_ref die)
7765 dw_die_ref c;
7767 gcc_assert (!die->die_mark);
7769 die->die_mark = 1;
7770 FOR_EACH_CHILD (die, c, mark_dies (c));
7773 /* Clear the marks for a die and its children. */
7775 static void
7776 unmark_dies (dw_die_ref die)
7778 dw_die_ref c;
7780 if (! use_debug_types)
7781 gcc_assert (die->die_mark);
7783 die->die_mark = 0;
7784 FOR_EACH_CHILD (die, c, unmark_dies (c));
7787 /* Clear the marks for a die, its children and referred dies. */
7789 static void
7790 unmark_all_dies (dw_die_ref die)
7792 dw_die_ref c;
7793 dw_attr_ref a;
7794 unsigned ix;
7796 if (!die->die_mark)
7797 return;
7798 die->die_mark = 0;
7800 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
7802 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7803 if (AT_class (a) == dw_val_class_die_ref)
7804 unmark_all_dies (AT_ref (a));
7807 /* Return the size of the .debug_pubnames or .debug_pubtypes table
7808 generated for the compilation unit. */
7810 static unsigned long
7811 size_of_pubnames (vec<pubname_entry, va_gc> *names)
7813 unsigned long size;
7814 unsigned i;
7815 pubname_ref p;
7817 size = DWARF_PUBNAMES_HEADER_SIZE;
7818 FOR_EACH_VEC_ELT (*names, i, p)
7819 if (names != pubtype_table
7820 || p->die->die_offset != 0
7821 || !flag_eliminate_unused_debug_types)
7822 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
7824 size += DWARF_OFFSET_SIZE;
7825 return size;
7828 /* Return the size of the information in the .debug_aranges section. */
7830 static unsigned long
7831 size_of_aranges (void)
7833 unsigned long size;
7835 size = DWARF_ARANGES_HEADER_SIZE;
7837 /* Count the address/length pair for this compilation unit. */
7838 if (text_section_used)
7839 size += 2 * DWARF2_ADDR_SIZE;
7840 if (cold_text_section_used)
7841 size += 2 * DWARF2_ADDR_SIZE;
7842 if (have_multiple_function_sections)
7844 unsigned fde_idx;
7845 dw_fde_ref fde;
7847 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
7849 if (!fde->in_std_section)
7850 size += 2 * DWARF2_ADDR_SIZE;
7851 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
7852 size += 2 * DWARF2_ADDR_SIZE;
7856 /* Count the two zero words used to terminated the address range table. */
7857 size += 2 * DWARF2_ADDR_SIZE;
7858 return size;
7861 /* Select the encoding of an attribute value. */
7863 static enum dwarf_form
7864 value_format (dw_attr_ref a)
7866 switch (AT_class (a))
7868 case dw_val_class_addr:
7869 /* Only very few attributes allow DW_FORM_addr. */
7870 switch (a->dw_attr)
7872 case DW_AT_low_pc:
7873 case DW_AT_high_pc:
7874 case DW_AT_entry_pc:
7875 case DW_AT_trampoline:
7876 return (AT_index (a) == NOT_INDEXED
7877 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
7878 default:
7879 break;
7881 switch (DWARF2_ADDR_SIZE)
7883 case 1:
7884 return DW_FORM_data1;
7885 case 2:
7886 return DW_FORM_data2;
7887 case 4:
7888 return DW_FORM_data4;
7889 case 8:
7890 return DW_FORM_data8;
7891 default:
7892 gcc_unreachable ();
7894 case dw_val_class_range_list:
7895 case dw_val_class_loc_list:
7896 if (dwarf_version >= 4)
7897 return DW_FORM_sec_offset;
7898 /* FALLTHRU */
7899 case dw_val_class_vms_delta:
7900 case dw_val_class_offset:
7901 switch (DWARF_OFFSET_SIZE)
7903 case 4:
7904 return DW_FORM_data4;
7905 case 8:
7906 return DW_FORM_data8;
7907 default:
7908 gcc_unreachable ();
7910 case dw_val_class_loc:
7911 if (dwarf_version >= 4)
7912 return DW_FORM_exprloc;
7913 switch (constant_size (size_of_locs (AT_loc (a))))
7915 case 1:
7916 return DW_FORM_block1;
7917 case 2:
7918 return DW_FORM_block2;
7919 case 4:
7920 return DW_FORM_block4;
7921 default:
7922 gcc_unreachable ();
7924 case dw_val_class_const:
7925 return DW_FORM_sdata;
7926 case dw_val_class_unsigned_const:
7927 switch (constant_size (AT_unsigned (a)))
7929 case 1:
7930 return DW_FORM_data1;
7931 case 2:
7932 return DW_FORM_data2;
7933 case 4:
7934 /* In DWARF3 DW_AT_data_member_location with
7935 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
7936 constant, so we need to use DW_FORM_udata if we need
7937 a large constant. */
7938 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
7939 return DW_FORM_udata;
7940 return DW_FORM_data4;
7941 case 8:
7942 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
7943 return DW_FORM_udata;
7944 return DW_FORM_data8;
7945 default:
7946 gcc_unreachable ();
7948 case dw_val_class_const_double:
7949 switch (HOST_BITS_PER_WIDE_INT)
7951 case 8:
7952 return DW_FORM_data2;
7953 case 16:
7954 return DW_FORM_data4;
7955 case 32:
7956 return DW_FORM_data8;
7957 case 64:
7958 default:
7959 return DW_FORM_block1;
7961 case dw_val_class_vec:
7962 switch (constant_size (a->dw_attr_val.v.val_vec.length
7963 * a->dw_attr_val.v.val_vec.elt_size))
7965 case 1:
7966 return DW_FORM_block1;
7967 case 2:
7968 return DW_FORM_block2;
7969 case 4:
7970 return DW_FORM_block4;
7971 default:
7972 gcc_unreachable ();
7974 case dw_val_class_flag:
7975 if (dwarf_version >= 4)
7977 /* Currently all add_AT_flag calls pass in 1 as last argument,
7978 so DW_FORM_flag_present can be used. If that ever changes,
7979 we'll need to use DW_FORM_flag and have some optimization
7980 in build_abbrev_table that will change those to
7981 DW_FORM_flag_present if it is set to 1 in all DIEs using
7982 the same abbrev entry. */
7983 gcc_assert (a->dw_attr_val.v.val_flag == 1);
7984 return DW_FORM_flag_present;
7986 return DW_FORM_flag;
7987 case dw_val_class_die_ref:
7988 if (AT_ref_external (a))
7989 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
7990 else
7991 return DW_FORM_ref;
7992 case dw_val_class_fde_ref:
7993 return DW_FORM_data;
7994 case dw_val_class_lbl_id:
7995 return (AT_index (a) == NOT_INDEXED
7996 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
7997 case dw_val_class_lineptr:
7998 case dw_val_class_macptr:
7999 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8000 case dw_val_class_str:
8001 return AT_string_form (a);
8002 case dw_val_class_file:
8003 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8005 case 1:
8006 return DW_FORM_data1;
8007 case 2:
8008 return DW_FORM_data2;
8009 case 4:
8010 return DW_FORM_data4;
8011 default:
8012 gcc_unreachable ();
8015 case dw_val_class_data8:
8016 return DW_FORM_data8;
8018 case dw_val_class_high_pc:
8019 switch (DWARF2_ADDR_SIZE)
8021 case 1:
8022 return DW_FORM_data1;
8023 case 2:
8024 return DW_FORM_data2;
8025 case 4:
8026 return DW_FORM_data4;
8027 case 8:
8028 return DW_FORM_data8;
8029 default:
8030 gcc_unreachable ();
8033 default:
8034 gcc_unreachable ();
8038 /* Output the encoding of an attribute value. */
8040 static void
8041 output_value_format (dw_attr_ref a)
8043 enum dwarf_form form = value_format (a);
8045 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8048 /* Given a die and id, produce the appropriate abbreviations. */
8050 static void
8051 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8053 unsigned ix;
8054 dw_attr_ref a_attr;
8056 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8057 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8058 dwarf_tag_name (abbrev->die_tag));
8060 if (abbrev->die_child != NULL)
8061 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8062 else
8063 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8065 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8067 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8068 dwarf_attr_name (a_attr->dw_attr));
8069 output_value_format (a_attr);
8072 dw2_asm_output_data (1, 0, NULL);
8073 dw2_asm_output_data (1, 0, NULL);
8077 /* Output the .debug_abbrev section which defines the DIE abbreviation
8078 table. */
8080 static void
8081 output_abbrev_section (void)
8083 unsigned long abbrev_id;
8085 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8086 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8088 /* Terminate the table. */
8089 dw2_asm_output_data (1, 0, NULL);
8092 /* Output a symbol we can use to refer to this DIE from another CU. */
8094 static inline void
8095 output_die_symbol (dw_die_ref die)
8097 const char *sym = die->die_id.die_symbol;
8099 gcc_assert (!die->comdat_type_p);
8101 if (sym == 0)
8102 return;
8104 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8105 /* We make these global, not weak; if the target doesn't support
8106 .linkonce, it doesn't support combining the sections, so debugging
8107 will break. */
8108 targetm.asm_out.globalize_label (asm_out_file, sym);
8110 ASM_OUTPUT_LABEL (asm_out_file, sym);
8113 /* Return a new location list, given the begin and end range, and the
8114 expression. */
8116 static inline dw_loc_list_ref
8117 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8118 const char *section)
8120 dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
8122 retlist->begin = begin;
8123 retlist->begin_entry = NULL;
8124 retlist->end = end;
8125 retlist->expr = expr;
8126 retlist->section = section;
8128 return retlist;
8131 /* Generate a new internal symbol for this location list node, if it
8132 hasn't got one yet. */
8134 static inline void
8135 gen_llsym (dw_loc_list_ref list)
8137 gcc_assert (!list->ll_symbol);
8138 list->ll_symbol = gen_internal_sym ("LLST");
8141 /* Output the location list given to us. */
8143 static void
8144 output_loc_list (dw_loc_list_ref list_head)
8146 dw_loc_list_ref curr = list_head;
8148 if (list_head->emitted)
8149 return;
8150 list_head->emitted = true;
8152 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8154 /* Walk the location list, and output each range + expression. */
8155 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8157 unsigned long size;
8158 /* Don't output an entry that starts and ends at the same address. */
8159 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8160 continue;
8161 size = size_of_locs (curr->expr);
8162 /* If the expression is too large, drop it on the floor. We could
8163 perhaps put it into DW_TAG_dwarf_procedure and refer to that
8164 in the expression, but >= 64KB expressions for a single value
8165 in a single range are unlikely very useful. */
8166 if (size > 0xffff)
8167 continue;
8168 if (dwarf_split_debug_info)
8170 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8171 "Location list start/length entry (%s)",
8172 list_head->ll_symbol);
8173 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8174 "Location list range start index (%s)",
8175 curr->begin);
8176 /* The length field is 4 bytes. If we ever need to support
8177 an 8-byte length, we can add a new DW_LLE code or fall back
8178 to DW_LLE_GNU_start_end_entry. */
8179 dw2_asm_output_delta (4, curr->end, curr->begin,
8180 "Location list range length (%s)",
8181 list_head->ll_symbol);
8183 else if (!have_multiple_function_sections)
8185 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8186 "Location list begin address (%s)",
8187 list_head->ll_symbol);
8188 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8189 "Location list end address (%s)",
8190 list_head->ll_symbol);
8192 else
8194 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8195 "Location list begin address (%s)",
8196 list_head->ll_symbol);
8197 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8198 "Location list end address (%s)",
8199 list_head->ll_symbol);
8202 /* Output the block length for this list of location operations. */
8203 gcc_assert (size <= 0xffff);
8204 dw2_asm_output_data (2, size, "%s", "Location expression size");
8206 output_loc_sequence (curr->expr, -1);
8209 if (dwarf_split_debug_info)
8210 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8211 "Location list terminator (%s)",
8212 list_head->ll_symbol);
8213 else
8215 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8216 "Location list terminator begin (%s)",
8217 list_head->ll_symbol);
8218 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8219 "Location list terminator end (%s)",
8220 list_head->ll_symbol);
8224 /* Output a range_list offset into the debug_range section. Emit a
8225 relocated reference if val_entry is NULL, otherwise, emit an
8226 indirect reference. */
8228 static void
8229 output_range_list_offset (dw_attr_ref a)
8231 const char *name = dwarf_attr_name (a->dw_attr);
8233 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8235 char *p = strchr (ranges_section_label, '\0');
8236 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8237 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8238 debug_ranges_section, "%s", name);
8239 *p = '\0';
8241 else
8242 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8243 "%s (offset from %s)", name, ranges_section_label);
8246 /* Output the offset into the debug_loc section. */
8248 static void
8249 output_loc_list_offset (dw_attr_ref a)
8251 char *sym = AT_loc_list (a)->ll_symbol;
8253 gcc_assert (sym);
8254 if (dwarf_split_debug_info)
8255 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8256 "%s", dwarf_attr_name (a->dw_attr));
8257 else
8258 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8259 "%s", dwarf_attr_name (a->dw_attr));
8262 /* Output an attribute's index or value appropriately. */
8264 static void
8265 output_attr_index_or_value (dw_attr_ref a)
8267 const char *name = dwarf_attr_name (a->dw_attr);
8269 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8271 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8272 return;
8274 switch (AT_class (a))
8276 case dw_val_class_addr:
8277 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8278 break;
8279 case dw_val_class_high_pc:
8280 case dw_val_class_lbl_id:
8281 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8282 break;
8283 case dw_val_class_loc_list:
8284 output_loc_list_offset (a);
8285 break;
8286 default:
8287 gcc_unreachable ();
8291 /* Output a type signature. */
8293 static inline void
8294 output_signature (const char *sig, const char *name)
8296 int i;
8298 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8299 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8302 /* Output the DIE and its attributes. Called recursively to generate
8303 the definitions of each child DIE. */
8305 static void
8306 output_die (dw_die_ref die)
8308 dw_attr_ref a;
8309 dw_die_ref c;
8310 unsigned long size;
8311 unsigned ix;
8313 /* If someone in another CU might refer to us, set up a symbol for
8314 them to point to. */
8315 if (! die->comdat_type_p && die->die_id.die_symbol)
8316 output_die_symbol (die);
8318 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8319 (unsigned long)die->die_offset,
8320 dwarf_tag_name (die->die_tag));
8322 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8324 const char *name = dwarf_attr_name (a->dw_attr);
8326 switch (AT_class (a))
8328 case dw_val_class_addr:
8329 output_attr_index_or_value (a);
8330 break;
8332 case dw_val_class_offset:
8333 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8334 "%s", name);
8335 break;
8337 case dw_val_class_range_list:
8338 output_range_list_offset (a);
8339 break;
8341 case dw_val_class_loc:
8342 size = size_of_locs (AT_loc (a));
8344 /* Output the block length for this list of location operations. */
8345 if (dwarf_version >= 4)
8346 dw2_asm_output_data_uleb128 (size, "%s", name);
8347 else
8348 dw2_asm_output_data (constant_size (size), size, "%s", name);
8350 output_loc_sequence (AT_loc (a), -1);
8351 break;
8353 case dw_val_class_const:
8354 /* ??? It would be slightly more efficient to use a scheme like is
8355 used for unsigned constants below, but gdb 4.x does not sign
8356 extend. Gdb 5.x does sign extend. */
8357 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8358 break;
8360 case dw_val_class_unsigned_const:
8362 int csize = constant_size (AT_unsigned (a));
8363 if (dwarf_version == 3
8364 && a->dw_attr == DW_AT_data_member_location
8365 && csize >= 4)
8366 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8367 else
8368 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8370 break;
8372 case dw_val_class_const_double:
8374 unsigned HOST_WIDE_INT first, second;
8376 if (HOST_BITS_PER_WIDE_INT >= 64)
8377 dw2_asm_output_data (1,
8378 HOST_BITS_PER_DOUBLE_INT
8379 / HOST_BITS_PER_CHAR,
8380 NULL);
8382 if (WORDS_BIG_ENDIAN)
8384 first = a->dw_attr_val.v.val_double.high;
8385 second = a->dw_attr_val.v.val_double.low;
8387 else
8389 first = a->dw_attr_val.v.val_double.low;
8390 second = a->dw_attr_val.v.val_double.high;
8393 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8394 first, "%s", name);
8395 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8396 second, NULL);
8398 break;
8400 case dw_val_class_vec:
8402 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8403 unsigned int len = a->dw_attr_val.v.val_vec.length;
8404 unsigned int i;
8405 unsigned char *p;
8407 dw2_asm_output_data (constant_size (len * elt_size),
8408 len * elt_size, "%s", name);
8409 if (elt_size > sizeof (HOST_WIDE_INT))
8411 elt_size /= 2;
8412 len *= 2;
8414 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8415 i < len;
8416 i++, p += elt_size)
8417 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8418 "fp or vector constant word %u", i);
8419 break;
8422 case dw_val_class_flag:
8423 if (dwarf_version >= 4)
8425 /* Currently all add_AT_flag calls pass in 1 as last argument,
8426 so DW_FORM_flag_present can be used. If that ever changes,
8427 we'll need to use DW_FORM_flag and have some optimization
8428 in build_abbrev_table that will change those to
8429 DW_FORM_flag_present if it is set to 1 in all DIEs using
8430 the same abbrev entry. */
8431 gcc_assert (AT_flag (a) == 1);
8432 if (flag_debug_asm)
8433 fprintf (asm_out_file, "\t\t\t%s %s\n",
8434 ASM_COMMENT_START, name);
8435 break;
8437 dw2_asm_output_data (1, AT_flag (a), "%s", name);
8438 break;
8440 case dw_val_class_loc_list:
8441 output_attr_index_or_value (a);
8442 break;
8444 case dw_val_class_die_ref:
8445 if (AT_ref_external (a))
8447 if (AT_ref (a)->comdat_type_p)
8449 comdat_type_node_ref type_node =
8450 AT_ref (a)->die_id.die_type_node;
8452 gcc_assert (type_node);
8453 output_signature (type_node->signature, name);
8455 else
8457 const char *sym = AT_ref (a)->die_id.die_symbol;
8458 int size;
8460 gcc_assert (sym);
8461 /* In DWARF2, DW_FORM_ref_addr is sized by target address
8462 length, whereas in DWARF3 it's always sized as an
8463 offset. */
8464 if (dwarf_version == 2)
8465 size = DWARF2_ADDR_SIZE;
8466 else
8467 size = DWARF_OFFSET_SIZE;
8468 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8469 name);
8472 else
8474 gcc_assert (AT_ref (a)->die_offset);
8475 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8476 "%s", name);
8478 break;
8480 case dw_val_class_fde_ref:
8482 char l1[20];
8484 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8485 a->dw_attr_val.v.val_fde_index * 2);
8486 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8487 "%s", name);
8489 break;
8491 case dw_val_class_vms_delta:
8492 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
8493 AT_vms_delta2 (a), AT_vms_delta1 (a),
8494 "%s", name);
8495 break;
8497 case dw_val_class_lbl_id:
8498 output_attr_index_or_value (a);
8499 break;
8501 case dw_val_class_lineptr:
8502 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8503 debug_line_section, "%s", name);
8504 break;
8506 case dw_val_class_macptr:
8507 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8508 debug_macinfo_section, "%s", name);
8509 break;
8511 case dw_val_class_str:
8512 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
8513 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8514 a->dw_attr_val.v.val_str->label,
8515 debug_str_section,
8516 "%s: \"%s\"", name, AT_string (a));
8517 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
8518 dw2_asm_output_data_uleb128 (AT_index (a),
8519 "%s: \"%s\"", name, AT_string (a));
8520 else
8521 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8522 break;
8524 case dw_val_class_file:
8526 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8528 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8529 a->dw_attr_val.v.val_file->filename);
8530 break;
8533 case dw_val_class_data8:
8535 int i;
8537 for (i = 0; i < 8; i++)
8538 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
8539 i == 0 ? "%s" : NULL, name);
8540 break;
8543 case dw_val_class_high_pc:
8544 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
8545 get_AT_low_pc (die), "DW_AT_high_pc");
8546 break;
8548 default:
8549 gcc_unreachable ();
8553 FOR_EACH_CHILD (die, c, output_die (c));
8555 /* Add null byte to terminate sibling list. */
8556 if (die->die_child != NULL)
8557 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
8558 (unsigned long) die->die_offset);
8561 /* Output the compilation unit that appears at the beginning of the
8562 .debug_info section, and precedes the DIE descriptions. */
8564 static void
8565 output_compilation_unit_header (void)
8567 int ver = dwarf_version;
8569 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8570 dw2_asm_output_data (4, 0xffffffff,
8571 "Initial length escape value indicating 64-bit DWARF extension");
8572 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8573 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8574 "Length of Compilation Unit Info");
8575 dw2_asm_output_data (2, ver, "DWARF version number");
8576 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8577 debug_abbrev_section,
8578 "Offset Into Abbrev. Section");
8579 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8582 /* Output the compilation unit DIE and its children. */
8584 static void
8585 output_comp_unit (dw_die_ref die, int output_if_empty)
8587 const char *secname, *oldsym;
8588 char *tmp;
8589 htab_t extern_map;
8591 /* Unless we are outputting main CU, we may throw away empty ones. */
8592 if (!output_if_empty && die->die_child == NULL)
8593 return;
8595 /* Even if there are no children of this DIE, we must output the information
8596 about the compilation unit. Otherwise, on an empty translation unit, we
8597 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
8598 will then complain when examining the file. First mark all the DIEs in
8599 this CU so we know which get local refs. */
8600 mark_dies (die);
8602 extern_map = optimize_external_refs (die);
8604 build_abbrev_table (die, extern_map);
8606 htab_delete (extern_map);
8608 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8609 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8610 calc_die_sizes (die);
8612 oldsym = die->die_id.die_symbol;
8613 if (oldsym)
8615 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8617 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8618 secname = tmp;
8619 die->die_id.die_symbol = NULL;
8620 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8622 else
8624 switch_to_section (debug_info_section);
8625 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
8626 info_section_emitted = true;
8629 /* Output debugging information. */
8630 output_compilation_unit_header ();
8631 output_die (die);
8633 /* Leave the marks on the main CU, so we can check them in
8634 output_pubnames. */
8635 if (oldsym)
8637 unmark_dies (die);
8638 die->die_id.die_symbol = oldsym;
8642 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
8643 and .debug_pubtypes. This is configured per-target, but can be
8644 overridden by the -gpubnames or -gno-pubnames options. */
8646 static inline bool
8647 want_pubnames (void)
8649 return (debug_generate_pub_sections != -1
8650 ? debug_generate_pub_sections
8651 : targetm.want_debug_pub_sections);
8654 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
8656 static void
8657 add_AT_pubnames (dw_die_ref die)
8659 if (want_pubnames ())
8660 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
8663 /* Helper function to generate top-level dies for skeleton debug_info and
8664 debug_types. */
8666 static void
8667 add_top_level_skeleton_die_attrs (dw_die_ref die)
8669 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
8670 dw_attr_ref attr;
8672 add_comp_dir_attribute (die);
8673 add_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
8674 /* The specification suggests that these attributes be inline to avoid
8675 having a .debug_str section. We know that they exist in the die because
8676 we just added them. */
8677 attr = get_AT (die, DW_AT_GNU_dwo_name);
8678 attr->dw_attr_val.v.val_str->form = DW_FORM_string;
8679 attr = get_AT (die, DW_AT_comp_dir);
8680 attr->dw_attr_val.v.val_str->form = DW_FORM_string;
8682 add_AT_pubnames (die);
8683 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
8686 /* Return the single type-unit die for skeleton type units. */
8688 static dw_die_ref
8689 get_skeleton_type_unit (void)
8691 /* For dwarf_split_debug_sections with use_type info, all type units in the
8692 skeleton sections have identical dies (but different headers). This
8693 single die will be output many times. */
8695 static dw_die_ref skeleton_type_unit = NULL;
8697 if (skeleton_type_unit == NULL)
8699 skeleton_type_unit = new_die (DW_TAG_type_unit, NULL, NULL);
8700 add_top_level_skeleton_die_attrs (skeleton_type_unit);
8701 skeleton_type_unit->die_abbrev = SKELETON_TYPE_DIE_ABBREV;
8703 return skeleton_type_unit;
8706 /* Output skeleton debug sections that point to the dwo file. */
8708 static void
8709 output_skeleton_debug_sections (dw_die_ref comp_unit)
8711 /* These attributes will be found in the full debug_info section. */
8712 remove_AT (comp_unit, DW_AT_producer);
8713 remove_AT (comp_unit, DW_AT_language);
8715 /* Add attributes common to skeleton compile_units and type_units. */
8716 add_top_level_skeleton_die_attrs (comp_unit);
8718 switch_to_section (debug_skeleton_info_section);
8719 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
8721 /* Produce the skeleton compilation-unit header. This one differs enough from
8722 a normal CU header that it's better not to call output_compilation_unit
8723 header. */
8724 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8725 dw2_asm_output_data (4, 0xffffffff,
8726 "Initial length escape value indicating 64-bit DWARF extension");
8728 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8729 DWARF_COMPILE_UNIT_HEADER_SIZE
8730 - DWARF_INITIAL_LENGTH_SIZE
8731 + size_of_die (comp_unit),
8732 "Length of Compilation Unit Info");
8733 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
8734 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
8735 debug_abbrev_section,
8736 "Offset Into Abbrev. Section");
8737 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8739 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
8740 output_die (comp_unit);
8742 /* Build the skeleton debug_abbrev section. */
8743 switch_to_section (debug_skeleton_abbrev_section);
8744 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
8746 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
8747 if (use_debug_types)
8748 output_die_abbrevs (SKELETON_TYPE_DIE_ABBREV, get_skeleton_type_unit ());
8750 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
8753 /* Output a comdat type unit DIE and its children. */
8755 static void
8756 output_comdat_type_unit (comdat_type_node *node)
8758 const char *secname;
8759 char *tmp;
8760 int i;
8761 #if defined (OBJECT_FORMAT_ELF)
8762 tree comdat_key;
8763 #endif
8764 htab_t extern_map;
8766 /* First mark all the DIEs in this CU so we know which get local refs. */
8767 mark_dies (node->root_die);
8769 extern_map = optimize_external_refs (node->root_die);
8771 build_abbrev_table (node->root_die, extern_map);
8773 htab_delete (extern_map);
8775 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8776 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
8777 calc_die_sizes (node->root_die);
8779 #if defined (OBJECT_FORMAT_ELF)
8780 if (!dwarf_split_debug_info)
8781 secname = ".debug_types";
8782 else
8783 secname = ".debug_types.dwo";
8785 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
8786 sprintf (tmp, "wt.");
8787 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8788 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
8789 comdat_key = get_identifier (tmp);
8790 targetm.asm_out.named_section (secname,
8791 SECTION_DEBUG | SECTION_LINKONCE,
8792 comdat_key);
8793 #else
8794 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
8795 sprintf (tmp, ".gnu.linkonce.wt.");
8796 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8797 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
8798 secname = tmp;
8799 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8800 #endif
8802 /* Output debugging information. */
8803 output_compilation_unit_header ();
8804 output_signature (node->signature, "Type Signature");
8805 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
8806 "Offset to Type DIE");
8807 output_die (node->root_die);
8809 unmark_dies (node->root_die);
8811 #if defined (OBJECT_FORMAT_ELF)
8812 if (dwarf_split_debug_info)
8814 /* Produce the skeleton type-unit header. */
8815 const char *secname = ".debug_types";
8817 targetm.asm_out.named_section (secname,
8818 SECTION_DEBUG | SECTION_LINKONCE,
8819 comdat_key);
8820 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8821 dw2_asm_output_data (4, 0xffffffff,
8822 "Initial length escape value indicating 64-bit DWARF extension");
8824 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8825 DWARF_COMPILE_UNIT_HEADER_SIZE
8826 - DWARF_INITIAL_LENGTH_SIZE
8827 + size_of_die (get_skeleton_type_unit ())
8828 + DWARF_TYPE_SIGNATURE_SIZE + DWARF_OFFSET_SIZE,
8829 "Length of Type Unit Info");
8830 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
8831 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8832 debug_skeleton_abbrev_section_label,
8833 debug_abbrev_section,
8834 "Offset Into Abbrev. Section");
8835 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8836 output_signature (node->signature, "Type Signature");
8837 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, "Offset to Type DIE");
8839 output_die (get_skeleton_type_unit ());
8841 #endif
8844 /* Return the DWARF2/3 pubname associated with a decl. */
8846 static const char *
8847 dwarf2_name (tree decl, int scope)
8849 if (DECL_NAMELESS (decl))
8850 return NULL;
8851 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
8854 /* Add a new entry to .debug_pubnames if appropriate. */
8856 static void
8857 add_pubname_string (const char *str, dw_die_ref die)
8859 pubname_entry e;
8861 e.die = die;
8862 e.name = xstrdup (str);
8863 vec_safe_push (pubname_table, e);
8866 static void
8867 add_pubname (tree decl, dw_die_ref die)
8869 if (!want_pubnames ())
8870 return;
8872 /* Don't add items to the table when we expect that the consumer will have
8873 just read the enclosing die. For example, if the consumer is looking at a
8874 class_member, it will either be inside the class already, or will have just
8875 looked up the class to find the member. Either way, searching the class is
8876 faster than searching the index. */
8877 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
8878 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
8880 const char *name = dwarf2_name (decl, 1);
8882 if (name)
8883 add_pubname_string (name, die);
8887 /* Add an enumerator to the pubnames section. */
8889 static void
8890 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
8892 pubname_entry e;
8894 gcc_assert (scope_name);
8895 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
8896 e.die = die;
8897 vec_safe_push (pubname_table, e);
8900 /* Add a new entry to .debug_pubtypes if appropriate. */
8902 static void
8903 add_pubtype (tree decl, dw_die_ref die)
8905 pubname_entry e;
8907 if (!want_pubnames ())
8908 return;
8910 if ((TREE_PUBLIC (decl)
8911 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
8912 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
8914 tree scope = NULL;
8915 const char *scope_name = "";
8916 const char *sep = is_cxx () ? "::" : ".";
8917 const char *name;
8919 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
8920 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
8922 scope_name = lang_hooks.dwarf_name (scope, 1);
8923 if (scope_name != NULL && scope_name[0] != '\0')
8924 scope_name = concat (scope_name, sep, NULL);
8925 else
8926 scope_name = "";
8929 if (TYPE_P (decl))
8930 name = type_tag (decl);
8931 else
8932 name = lang_hooks.dwarf_name (decl, 1);
8934 /* If we don't have a name for the type, there's no point in adding
8935 it to the table. */
8936 if (name != NULL && name[0] != '\0')
8938 e.die = die;
8939 e.name = concat (scope_name, name, NULL);
8940 vec_safe_push (pubtype_table, e);
8943 /* Although it might be more consistent to add the pubinfo for the
8944 enumerators as their dies are created, they should only be added if the
8945 enum type meets the criteria above. So rather than re-check the parent
8946 enum type whenever an enumerator die is created, just output them all
8947 here. This isn't protected by the name conditional because anonymous
8948 enums don't have names. */
8949 if (die->die_tag == DW_TAG_enumeration_type)
8951 dw_die_ref c;
8953 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
8958 /* Output the public names table used to speed up access to externally
8959 visible names; or the public types table used to find type definitions. */
8961 static void
8962 output_pubnames (vec<pubname_entry, va_gc> *names)
8964 unsigned i;
8965 unsigned long pubnames_length = size_of_pubnames (names);
8966 pubname_ref pub;
8968 if (!want_pubnames () || !info_section_emitted)
8969 return;
8970 if (names == pubname_table)
8971 switch_to_section (debug_pubnames_section);
8972 else
8973 switch_to_section (debug_pubtypes_section);
8974 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8975 dw2_asm_output_data (4, 0xffffffff,
8976 "Initial length escape value indicating 64-bit DWARF extension");
8977 if (names == pubname_table)
8978 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8979 "Length of Public Names Info");
8980 else
8981 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8982 "Length of Public Type Names Info");
8983 /* Version number for pubnames/pubtypes is still 2, even in DWARF3. */
8984 dw2_asm_output_data (2, 2, "DWARF Version");
8985 if (dwarf_split_debug_info)
8986 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
8987 debug_skeleton_info_section,
8988 "Offset of Compilation Unit Info");
8989 else
8990 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8991 debug_info_section,
8992 "Offset of Compilation Unit Info");
8993 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
8994 "Compilation Unit Length");
8996 FOR_EACH_VEC_ELT (*names, i, pub)
8998 /* Enumerator names are part of the pubname table, but the parent
8999 DW_TAG_enumeration_type die may have been pruned. Don't output
9000 them if that is the case. */
9001 if (pub->die->die_tag == DW_TAG_enumerator && !pub->die->die_mark)
9002 continue;
9004 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9005 if (names == pubname_table)
9006 gcc_assert (pub->die->die_mark);
9008 if (names != pubtype_table
9009 || pub->die->die_offset != 0
9010 || !flag_eliminate_unused_debug_types)
9012 dw_offset die_offset = pub->die->die_offset;
9014 /* If we're putting types in their own .debug_types sections,
9015 the .debug_pubtypes table will still point to the compile
9016 unit (not the type unit), so we want to use the offset of
9017 the skeleton DIE (if there is one). */
9018 if (pub->die->comdat_type_p && names == pubtype_table)
9020 comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
9022 if (type_node != NULL)
9023 die_offset = (type_node->skeleton_die != NULL
9024 ? type_node->skeleton_die->die_offset
9025 : 0);
9028 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9030 dw2_asm_output_nstring (pub->name, -1, "external name");
9034 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9037 /* Output the information that goes into the .debug_aranges table.
9038 Namely, define the beginning and ending address range of the
9039 text section generated for this compilation unit. */
9041 static void
9042 output_aranges (unsigned long aranges_length)
9044 unsigned i;
9046 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9047 dw2_asm_output_data (4, 0xffffffff,
9048 "Initial length escape value indicating 64-bit DWARF extension");
9049 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9050 "Length of Address Ranges Info");
9051 /* Version number for aranges is still 2, even in DWARF3. */
9052 dw2_asm_output_data (2, 2, "DWARF Version");
9053 if (dwarf_split_debug_info)
9054 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9055 debug_skeleton_info_section,
9056 "Offset of Compilation Unit Info");
9057 else
9058 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9059 debug_info_section,
9060 "Offset of Compilation Unit Info");
9061 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9062 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9064 /* We need to align to twice the pointer size here. */
9065 if (DWARF_ARANGES_PAD_SIZE)
9067 /* Pad using a 2 byte words so that padding is correct for any
9068 pointer size. */
9069 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9070 2 * DWARF2_ADDR_SIZE);
9071 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9072 dw2_asm_output_data (2, 0, NULL);
9075 /* It is necessary not to output these entries if the sections were
9076 not used; if the sections were not used, the length will be 0 and
9077 the address may end up as 0 if the section is discarded by ld
9078 --gc-sections, leaving an invalid (0, 0) entry that can be
9079 confused with the terminator. */
9080 if (text_section_used)
9082 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9083 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9084 text_section_label, "Length");
9086 if (cold_text_section_used)
9088 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9089 "Address");
9090 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9091 cold_text_section_label, "Length");
9094 if (have_multiple_function_sections)
9096 unsigned fde_idx;
9097 dw_fde_ref fde;
9099 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9101 if (DECL_IGNORED_P (fde->decl))
9102 continue;
9103 if (!fde->in_std_section)
9105 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9106 "Address");
9107 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9108 fde->dw_fde_begin, "Length");
9110 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9112 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9113 "Address");
9114 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9115 fde->dw_fde_second_begin, "Length");
9120 /* Output the terminator words. */
9121 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9122 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9125 /* Add a new entry to .debug_ranges. Return the offset at which it
9126 was placed. */
9128 static unsigned int
9129 add_ranges_num (int num)
9131 unsigned int in_use = ranges_table_in_use;
9133 if (in_use == ranges_table_allocated)
9135 ranges_table_allocated += RANGES_TABLE_INCREMENT;
9136 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9137 ranges_table_allocated);
9138 memset (ranges_table + ranges_table_in_use, 0,
9139 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9142 ranges_table[in_use].num = num;
9143 ranges_table_in_use = in_use + 1;
9145 return in_use * 2 * DWARF2_ADDR_SIZE;
9148 /* Add a new entry to .debug_ranges corresponding to a block, or a
9149 range terminator if BLOCK is NULL. */
9151 static unsigned int
9152 add_ranges (const_tree block)
9154 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9157 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9158 When using dwarf_split_debug_info, address attributes in dies destined
9159 for the final executable should be direct references--setting the
9160 parameter force_direct ensures this behavior. */
9162 static void
9163 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9164 bool *added, bool force_direct)
9166 unsigned int in_use = ranges_by_label_in_use;
9167 unsigned int offset;
9169 if (in_use == ranges_by_label_allocated)
9171 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9172 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9173 ranges_by_label,
9174 ranges_by_label_allocated);
9175 memset (ranges_by_label + ranges_by_label_in_use, 0,
9176 RANGES_TABLE_INCREMENT
9177 * sizeof (struct dw_ranges_by_label_struct));
9180 ranges_by_label[in_use].begin = begin;
9181 ranges_by_label[in_use].end = end;
9182 ranges_by_label_in_use = in_use + 1;
9184 offset = add_ranges_num (-(int)in_use - 1);
9185 if (!*added)
9187 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9188 *added = true;
9192 static void
9193 output_ranges (void)
9195 unsigned i;
9196 static const char *const start_fmt = "Offset %#x";
9197 const char *fmt = start_fmt;
9199 for (i = 0; i < ranges_table_in_use; i++)
9201 int block_num = ranges_table[i].num;
9203 if (block_num > 0)
9205 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9206 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9208 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9209 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9211 /* If all code is in the text section, then the compilation
9212 unit base address defaults to DW_AT_low_pc, which is the
9213 base of the text section. */
9214 if (!have_multiple_function_sections)
9216 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9217 text_section_label,
9218 fmt, i * 2 * DWARF2_ADDR_SIZE);
9219 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9220 text_section_label, NULL);
9223 /* Otherwise, the compilation unit base address is zero,
9224 which allows us to use absolute addresses, and not worry
9225 about whether the target supports cross-section
9226 arithmetic. */
9227 else
9229 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9230 fmt, i * 2 * DWARF2_ADDR_SIZE);
9231 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9234 fmt = NULL;
9237 /* Negative block_num stands for an index into ranges_by_label. */
9238 else if (block_num < 0)
9240 int lab_idx = - block_num - 1;
9242 if (!have_multiple_function_sections)
9244 gcc_unreachable ();
9245 #if 0
9246 /* If we ever use add_ranges_by_labels () for a single
9247 function section, all we have to do is to take out
9248 the #if 0 above. */
9249 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9250 ranges_by_label[lab_idx].begin,
9251 text_section_label,
9252 fmt, i * 2 * DWARF2_ADDR_SIZE);
9253 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9254 ranges_by_label[lab_idx].end,
9255 text_section_label, NULL);
9256 #endif
9258 else
9260 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9261 ranges_by_label[lab_idx].begin,
9262 fmt, i * 2 * DWARF2_ADDR_SIZE);
9263 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9264 ranges_by_label[lab_idx].end,
9265 NULL);
9268 else
9270 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9271 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9272 fmt = start_fmt;
9277 /* Data structure containing information about input files. */
9278 struct file_info
9280 const char *path; /* Complete file name. */
9281 const char *fname; /* File name part. */
9282 int length; /* Length of entire string. */
9283 struct dwarf_file_data * file_idx; /* Index in input file table. */
9284 int dir_idx; /* Index in directory table. */
9287 /* Data structure containing information about directories with source
9288 files. */
9289 struct dir_info
9291 const char *path; /* Path including directory name. */
9292 int length; /* Path length. */
9293 int prefix; /* Index of directory entry which is a prefix. */
9294 int count; /* Number of files in this directory. */
9295 int dir_idx; /* Index of directory used as base. */
9298 /* Callback function for file_info comparison. We sort by looking at
9299 the directories in the path. */
9301 static int
9302 file_info_cmp (const void *p1, const void *p2)
9304 const struct file_info *const s1 = (const struct file_info *) p1;
9305 const struct file_info *const s2 = (const struct file_info *) p2;
9306 const unsigned char *cp1;
9307 const unsigned char *cp2;
9309 /* Take care of file names without directories. We need to make sure that
9310 we return consistent values to qsort since some will get confused if
9311 we return the same value when identical operands are passed in opposite
9312 orders. So if neither has a directory, return 0 and otherwise return
9313 1 or -1 depending on which one has the directory. */
9314 if ((s1->path == s1->fname || s2->path == s2->fname))
9315 return (s2->path == s2->fname) - (s1->path == s1->fname);
9317 cp1 = (const unsigned char *) s1->path;
9318 cp2 = (const unsigned char *) s2->path;
9320 while (1)
9322 ++cp1;
9323 ++cp2;
9324 /* Reached the end of the first path? If so, handle like above. */
9325 if ((cp1 == (const unsigned char *) s1->fname)
9326 || (cp2 == (const unsigned char *) s2->fname))
9327 return ((cp2 == (const unsigned char *) s2->fname)
9328 - (cp1 == (const unsigned char *) s1->fname));
9330 /* Character of current path component the same? */
9331 else if (*cp1 != *cp2)
9332 return *cp1 - *cp2;
9336 struct file_name_acquire_data
9338 struct file_info *files;
9339 int used_files;
9340 int max_files;
9343 /* Traversal function for the hash table. */
9345 static int
9346 file_name_acquire (void ** slot, void *data)
9348 struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
9349 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
9350 struct file_info *fi;
9351 const char *f;
9353 gcc_assert (fnad->max_files >= d->emitted_number);
9355 if (! d->emitted_number)
9356 return 1;
9358 gcc_assert (fnad->max_files != fnad->used_files);
9360 fi = fnad->files + fnad->used_files++;
9362 /* Skip all leading "./". */
9363 f = d->filename;
9364 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9365 f += 2;
9367 /* Create a new array entry. */
9368 fi->path = f;
9369 fi->length = strlen (f);
9370 fi->file_idx = d;
9372 /* Search for the file name part. */
9373 f = strrchr (f, DIR_SEPARATOR);
9374 #if defined (DIR_SEPARATOR_2)
9376 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9378 if (g != NULL)
9380 if (f == NULL || f < g)
9381 f = g;
9384 #endif
9386 fi->fname = f == NULL ? fi->path : f + 1;
9387 return 1;
9390 /* Output the directory table and the file name table. We try to minimize
9391 the total amount of memory needed. A heuristic is used to avoid large
9392 slowdowns with many input files. */
9394 static void
9395 output_file_names (void)
9397 struct file_name_acquire_data fnad;
9398 int numfiles;
9399 struct file_info *files;
9400 struct dir_info *dirs;
9401 int *saved;
9402 int *savehere;
9403 int *backmap;
9404 int ndirs;
9405 int idx_offset;
9406 int i;
9408 if (!last_emitted_file)
9410 dw2_asm_output_data (1, 0, "End directory table");
9411 dw2_asm_output_data (1, 0, "End file name table");
9412 return;
9415 numfiles = last_emitted_file->emitted_number;
9417 /* Allocate the various arrays we need. */
9418 files = XALLOCAVEC (struct file_info, numfiles);
9419 dirs = XALLOCAVEC (struct dir_info, numfiles);
9421 fnad.files = files;
9422 fnad.used_files = 0;
9423 fnad.max_files = numfiles;
9424 htab_traverse (file_table, file_name_acquire, &fnad);
9425 gcc_assert (fnad.used_files == fnad.max_files);
9427 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9429 /* Find all the different directories used. */
9430 dirs[0].path = files[0].path;
9431 dirs[0].length = files[0].fname - files[0].path;
9432 dirs[0].prefix = -1;
9433 dirs[0].count = 1;
9434 dirs[0].dir_idx = 0;
9435 files[0].dir_idx = 0;
9436 ndirs = 1;
9438 for (i = 1; i < numfiles; i++)
9439 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9440 && memcmp (dirs[ndirs - 1].path, files[i].path,
9441 dirs[ndirs - 1].length) == 0)
9443 /* Same directory as last entry. */
9444 files[i].dir_idx = ndirs - 1;
9445 ++dirs[ndirs - 1].count;
9447 else
9449 int j;
9451 /* This is a new directory. */
9452 dirs[ndirs].path = files[i].path;
9453 dirs[ndirs].length = files[i].fname - files[i].path;
9454 dirs[ndirs].count = 1;
9455 dirs[ndirs].dir_idx = ndirs;
9456 files[i].dir_idx = ndirs;
9458 /* Search for a prefix. */
9459 dirs[ndirs].prefix = -1;
9460 for (j = 0; j < ndirs; j++)
9461 if (dirs[j].length < dirs[ndirs].length
9462 && dirs[j].length > 1
9463 && (dirs[ndirs].prefix == -1
9464 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9465 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9466 dirs[ndirs].prefix = j;
9468 ++ndirs;
9471 /* Now to the actual work. We have to find a subset of the directories which
9472 allow expressing the file name using references to the directory table
9473 with the least amount of characters. We do not do an exhaustive search
9474 where we would have to check out every combination of every single
9475 possible prefix. Instead we use a heuristic which provides nearly optimal
9476 results in most cases and never is much off. */
9477 saved = XALLOCAVEC (int, ndirs);
9478 savehere = XALLOCAVEC (int, ndirs);
9480 memset (saved, '\0', ndirs * sizeof (saved[0]));
9481 for (i = 0; i < ndirs; i++)
9483 int j;
9484 int total;
9486 /* We can always save some space for the current directory. But this
9487 does not mean it will be enough to justify adding the directory. */
9488 savehere[i] = dirs[i].length;
9489 total = (savehere[i] - saved[i]) * dirs[i].count;
9491 for (j = i + 1; j < ndirs; j++)
9493 savehere[j] = 0;
9494 if (saved[j] < dirs[i].length)
9496 /* Determine whether the dirs[i] path is a prefix of the
9497 dirs[j] path. */
9498 int k;
9500 k = dirs[j].prefix;
9501 while (k != -1 && k != (int) i)
9502 k = dirs[k].prefix;
9504 if (k == (int) i)
9506 /* Yes it is. We can possibly save some memory by
9507 writing the filenames in dirs[j] relative to
9508 dirs[i]. */
9509 savehere[j] = dirs[i].length;
9510 total += (savehere[j] - saved[j]) * dirs[j].count;
9515 /* Check whether we can save enough to justify adding the dirs[i]
9516 directory. */
9517 if (total > dirs[i].length + 1)
9519 /* It's worthwhile adding. */
9520 for (j = i; j < ndirs; j++)
9521 if (savehere[j] > 0)
9523 /* Remember how much we saved for this directory so far. */
9524 saved[j] = savehere[j];
9526 /* Remember the prefix directory. */
9527 dirs[j].dir_idx = i;
9532 /* Emit the directory name table. */
9533 idx_offset = dirs[0].length > 0 ? 1 : 0;
9534 for (i = 1 - idx_offset; i < ndirs; i++)
9535 dw2_asm_output_nstring (dirs[i].path,
9536 dirs[i].length
9537 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9538 "Directory Entry: %#x", i + idx_offset);
9540 dw2_asm_output_data (1, 0, "End directory table");
9542 /* We have to emit them in the order of emitted_number since that's
9543 used in the debug info generation. To do this efficiently we
9544 generate a back-mapping of the indices first. */
9545 backmap = XALLOCAVEC (int, numfiles);
9546 for (i = 0; i < numfiles; i++)
9547 backmap[files[i].file_idx->emitted_number - 1] = i;
9549 /* Now write all the file names. */
9550 for (i = 0; i < numfiles; i++)
9552 int file_idx = backmap[i];
9553 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9555 #ifdef VMS_DEBUGGING_INFO
9556 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
9558 /* Setting these fields can lead to debugger miscomparisons,
9559 but VMS Debug requires them to be set correctly. */
9561 int ver;
9562 long long cdt;
9563 long siz;
9564 int maxfilelen = strlen (files[file_idx].path)
9565 + dirs[dir_idx].length
9566 + MAX_VMS_VERSION_LEN + 1;
9567 char *filebuf = XALLOCAVEC (char, maxfilelen);
9569 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
9570 snprintf (filebuf, maxfilelen, "%s;%d",
9571 files[file_idx].path + dirs[dir_idx].length, ver);
9573 dw2_asm_output_nstring
9574 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
9576 /* Include directory index. */
9577 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9579 /* Modification time. */
9580 dw2_asm_output_data_uleb128
9581 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
9582 ? cdt : 0,
9583 NULL);
9585 /* File length in bytes. */
9586 dw2_asm_output_data_uleb128
9587 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
9588 ? siz : 0,
9589 NULL);
9590 #else
9591 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
9592 "File Entry: %#x", (unsigned) i + 1);
9594 /* Include directory index. */
9595 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9597 /* Modification time. */
9598 dw2_asm_output_data_uleb128 (0, NULL);
9600 /* File length in bytes. */
9601 dw2_asm_output_data_uleb128 (0, NULL);
9602 #endif /* VMS_DEBUGGING_INFO */
9605 dw2_asm_output_data (1, 0, "End file name table");
9609 /* Output one line number table into the .debug_line section. */
9611 static void
9612 output_one_line_info_table (dw_line_info_table *table)
9614 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9615 unsigned int current_line = 1;
9616 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
9617 dw_line_info_entry *ent;
9618 size_t i;
9620 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
9622 switch (ent->opcode)
9624 case LI_set_address:
9625 /* ??? Unfortunately, we have little choice here currently, and
9626 must always use the most general form. GCC does not know the
9627 address delta itself, so we can't use DW_LNS_advance_pc. Many
9628 ports do have length attributes which will give an upper bound
9629 on the address range. We could perhaps use length attributes
9630 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
9631 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
9633 /* This can handle any delta. This takes
9634 4+DWARF2_ADDR_SIZE bytes. */
9635 dw2_asm_output_data (1, 0, "set address %s", line_label);
9636 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9637 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9638 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9639 break;
9641 case LI_set_line:
9642 if (ent->val == current_line)
9644 /* We still need to start a new row, so output a copy insn. */
9645 dw2_asm_output_data (1, DW_LNS_copy,
9646 "copy line %u", current_line);
9648 else
9650 int line_offset = ent->val - current_line;
9651 int line_delta = line_offset - DWARF_LINE_BASE;
9653 current_line = ent->val;
9654 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9656 /* This can handle deltas from -10 to 234, using the current
9657 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
9658 This takes 1 byte. */
9659 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9660 "line %u", current_line);
9662 else
9664 /* This can handle any delta. This takes at least 4 bytes,
9665 depending on the value being encoded. */
9666 dw2_asm_output_data (1, DW_LNS_advance_line,
9667 "advance to line %u", current_line);
9668 dw2_asm_output_data_sleb128 (line_offset, NULL);
9669 dw2_asm_output_data (1, DW_LNS_copy, NULL);
9672 break;
9674 case LI_set_file:
9675 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
9676 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9677 break;
9679 case LI_set_column:
9680 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
9681 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9682 break;
9684 case LI_negate_stmt:
9685 current_is_stmt = !current_is_stmt;
9686 dw2_asm_output_data (1, DW_LNS_negate_stmt,
9687 "is_stmt %d", current_is_stmt);
9688 break;
9690 case LI_set_prologue_end:
9691 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
9692 "set prologue end");
9693 break;
9695 case LI_set_epilogue_begin:
9696 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
9697 "set epilogue begin");
9698 break;
9700 case LI_set_discriminator:
9701 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
9702 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
9703 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
9704 dw2_asm_output_data_uleb128 (ent->val, NULL);
9705 break;
9709 /* Emit debug info for the address of the end of the table. */
9710 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
9711 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9712 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9713 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
9715 dw2_asm_output_data (1, 0, "end sequence");
9716 dw2_asm_output_data_uleb128 (1, NULL);
9717 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9720 /* Output the source line number correspondence information. This
9721 information goes into the .debug_line section. */
9723 static void
9724 output_line_info (bool prologue_only)
9726 char l1[20], l2[20], p1[20], p2[20];
9727 int ver = dwarf_version;
9728 bool saw_one = false;
9729 int opc;
9731 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
9732 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
9733 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
9734 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
9736 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9737 dw2_asm_output_data (4, 0xffffffff,
9738 "Initial length escape value indicating 64-bit DWARF extension");
9739 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
9740 "Length of Source Line Info");
9741 ASM_OUTPUT_LABEL (asm_out_file, l1);
9743 dw2_asm_output_data (2, ver, "DWARF Version");
9744 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
9745 ASM_OUTPUT_LABEL (asm_out_file, p1);
9747 /* Define the architecture-dependent minimum instruction length (in bytes).
9748 In this implementation of DWARF, this field is used for information
9749 purposes only. Since GCC generates assembly language, we have no
9750 a priori knowledge of how many instruction bytes are generated for each
9751 source line, and therefore can use only the DW_LNE_set_address and
9752 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
9753 this as '1', which is "correct enough" for all architectures,
9754 and don't let the target override. */
9755 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
9757 if (ver >= 4)
9758 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
9759 "Maximum Operations Per Instruction");
9760 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
9761 "Default is_stmt_start flag");
9762 dw2_asm_output_data (1, DWARF_LINE_BASE,
9763 "Line Base Value (Special Opcodes)");
9764 dw2_asm_output_data (1, DWARF_LINE_RANGE,
9765 "Line Range Value (Special Opcodes)");
9766 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
9767 "Special Opcode Base");
9769 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
9771 int n_op_args;
9772 switch (opc)
9774 case DW_LNS_advance_pc:
9775 case DW_LNS_advance_line:
9776 case DW_LNS_set_file:
9777 case DW_LNS_set_column:
9778 case DW_LNS_fixed_advance_pc:
9779 case DW_LNS_set_isa:
9780 n_op_args = 1;
9781 break;
9782 default:
9783 n_op_args = 0;
9784 break;
9787 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
9788 opc, n_op_args);
9791 /* Write out the information about the files we use. */
9792 output_file_names ();
9793 ASM_OUTPUT_LABEL (asm_out_file, p2);
9794 if (prologue_only)
9796 /* Output the marker for the end of the line number info. */
9797 ASM_OUTPUT_LABEL (asm_out_file, l2);
9798 return;
9801 if (separate_line_info)
9803 dw_line_info_table *table;
9804 size_t i;
9806 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
9807 if (table->in_use)
9809 output_one_line_info_table (table);
9810 saw_one = true;
9813 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
9815 output_one_line_info_table (cold_text_section_line_info);
9816 saw_one = true;
9819 /* ??? Some Darwin linkers crash on a .debug_line section with no
9820 sequences. Further, merely a DW_LNE_end_sequence entry is not
9821 sufficient -- the address column must also be initialized.
9822 Make sure to output at least one set_address/end_sequence pair,
9823 choosing .text since that section is always present. */
9824 if (text_section_line_info->in_use || !saw_one)
9825 output_one_line_info_table (text_section_line_info);
9827 /* Output the marker for the end of the line number info. */
9828 ASM_OUTPUT_LABEL (asm_out_file, l2);
9831 /* Given a pointer to a tree node for some base type, return a pointer to
9832 a DIE that describes the given type.
9834 This routine must only be called for GCC type nodes that correspond to
9835 Dwarf base (fundamental) types. */
9837 static dw_die_ref
9838 base_type_die (tree type)
9840 dw_die_ref base_type_result;
9841 enum dwarf_type encoding;
9843 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
9844 return 0;
9846 /* If this is a subtype that should not be emitted as a subrange type,
9847 use the base type. See subrange_type_for_debug_p. */
9848 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
9849 type = TREE_TYPE (type);
9851 switch (TREE_CODE (type))
9853 case INTEGER_TYPE:
9854 if ((dwarf_version >= 4 || !dwarf_strict)
9855 && TYPE_NAME (type)
9856 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
9857 && DECL_IS_BUILTIN (TYPE_NAME (type))
9858 && DECL_NAME (TYPE_NAME (type)))
9860 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
9861 if (strcmp (name, "char16_t") == 0
9862 || strcmp (name, "char32_t") == 0)
9864 encoding = DW_ATE_UTF;
9865 break;
9868 if (TYPE_STRING_FLAG (type))
9870 if (TYPE_UNSIGNED (type))
9871 encoding = DW_ATE_unsigned_char;
9872 else
9873 encoding = DW_ATE_signed_char;
9875 else if (TYPE_UNSIGNED (type))
9876 encoding = DW_ATE_unsigned;
9877 else
9878 encoding = DW_ATE_signed;
9879 break;
9881 case REAL_TYPE:
9882 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
9884 if (dwarf_version >= 3 || !dwarf_strict)
9885 encoding = DW_ATE_decimal_float;
9886 else
9887 encoding = DW_ATE_lo_user;
9889 else
9890 encoding = DW_ATE_float;
9891 break;
9893 case FIXED_POINT_TYPE:
9894 if (!(dwarf_version >= 3 || !dwarf_strict))
9895 encoding = DW_ATE_lo_user;
9896 else if (TYPE_UNSIGNED (type))
9897 encoding = DW_ATE_unsigned_fixed;
9898 else
9899 encoding = DW_ATE_signed_fixed;
9900 break;
9902 /* Dwarf2 doesn't know anything about complex ints, so use
9903 a user defined type for it. */
9904 case COMPLEX_TYPE:
9905 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
9906 encoding = DW_ATE_complex_float;
9907 else
9908 encoding = DW_ATE_lo_user;
9909 break;
9911 case BOOLEAN_TYPE:
9912 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
9913 encoding = DW_ATE_boolean;
9914 break;
9916 default:
9917 /* No other TREE_CODEs are Dwarf fundamental types. */
9918 gcc_unreachable ();
9921 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
9923 add_AT_unsigned (base_type_result, DW_AT_byte_size,
9924 int_size_in_bytes (type));
9925 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
9926 add_pubtype (type, base_type_result);
9928 return base_type_result;
9931 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
9932 given input type is a Dwarf "fundamental" type. Otherwise return null. */
9934 static inline int
9935 is_base_type (tree type)
9937 switch (TREE_CODE (type))
9939 case ERROR_MARK:
9940 case VOID_TYPE:
9941 case INTEGER_TYPE:
9942 case REAL_TYPE:
9943 case FIXED_POINT_TYPE:
9944 case COMPLEX_TYPE:
9945 case BOOLEAN_TYPE:
9946 return 1;
9948 case ARRAY_TYPE:
9949 case RECORD_TYPE:
9950 case UNION_TYPE:
9951 case QUAL_UNION_TYPE:
9952 case ENUMERAL_TYPE:
9953 case FUNCTION_TYPE:
9954 case METHOD_TYPE:
9955 case POINTER_TYPE:
9956 case REFERENCE_TYPE:
9957 case NULLPTR_TYPE:
9958 case OFFSET_TYPE:
9959 case LANG_TYPE:
9960 case VECTOR_TYPE:
9961 return 0;
9963 default:
9964 gcc_unreachable ();
9967 return 0;
9970 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
9971 node, return the size in bits for the type if it is a constant, or else
9972 return the alignment for the type if the type's size is not constant, or
9973 else return BITS_PER_WORD if the type actually turns out to be an
9974 ERROR_MARK node. */
9976 static inline unsigned HOST_WIDE_INT
9977 simple_type_size_in_bits (const_tree type)
9979 if (TREE_CODE (type) == ERROR_MARK)
9980 return BITS_PER_WORD;
9981 else if (TYPE_SIZE (type) == NULL_TREE)
9982 return 0;
9983 else if (host_integerp (TYPE_SIZE (type), 1))
9984 return tree_low_cst (TYPE_SIZE (type), 1);
9985 else
9986 return TYPE_ALIGN (type);
9989 /* Similarly, but return a double_int instead of UHWI. */
9991 static inline double_int
9992 double_int_type_size_in_bits (const_tree type)
9994 if (TREE_CODE (type) == ERROR_MARK)
9995 return double_int::from_uhwi (BITS_PER_WORD);
9996 else if (TYPE_SIZE (type) == NULL_TREE)
9997 return double_int_zero;
9998 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
9999 return tree_to_double_int (TYPE_SIZE (type));
10000 else
10001 return double_int::from_uhwi (TYPE_ALIGN (type));
10004 /* Given a pointer to a tree node for a subrange type, return a pointer
10005 to a DIE that describes the given type. */
10007 static dw_die_ref
10008 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10010 dw_die_ref subrange_die;
10011 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10013 if (context_die == NULL)
10014 context_die = comp_unit_die ();
10016 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10018 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10020 /* The size of the subrange type and its base type do not match,
10021 so we need to generate a size attribute for the subrange type. */
10022 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10025 if (low)
10026 add_bound_info (subrange_die, DW_AT_lower_bound, low);
10027 if (high)
10028 add_bound_info (subrange_die, DW_AT_upper_bound, high);
10030 return subrange_die;
10033 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10034 entry that chains various modifiers in front of the given type. */
10036 static dw_die_ref
10037 modified_type_die (tree type, int type_quals,
10038 dw_die_ref context_die)
10040 const int is_const_type = ((type_quals & TYPE_QUAL_CONST) != 0);
10041 const int is_volatile_type = ((type_quals & TYPE_QUAL_VOLATILE) != 0);
10042 enum tree_code code = TREE_CODE (type);
10043 dw_die_ref mod_type_die;
10044 dw_die_ref sub_die = NULL;
10045 tree item_type = NULL;
10046 tree qualified_type;
10047 tree name, low, high;
10048 dw_die_ref mod_scope;
10050 if (code == ERROR_MARK)
10051 return NULL;
10053 /* See if we already have the appropriately qualified variant of
10054 this type. */
10055 qualified_type
10056 = get_qualified_type (type, type_quals);
10058 if (qualified_type == sizetype
10059 && TYPE_NAME (qualified_type)
10060 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10062 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10064 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10065 && TYPE_PRECISION (t)
10066 == TYPE_PRECISION (qualified_type)
10067 && TYPE_UNSIGNED (t)
10068 == TYPE_UNSIGNED (qualified_type));
10069 qualified_type = t;
10072 /* If we do, then we can just use its DIE, if it exists. */
10073 if (qualified_type)
10075 mod_type_die = lookup_type_die (qualified_type);
10076 if (mod_type_die)
10077 return mod_type_die;
10080 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10082 /* Handle C typedef types. */
10083 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10084 && !DECL_ARTIFICIAL (name))
10086 tree dtype = TREE_TYPE (name);
10088 if (qualified_type == dtype)
10090 /* For a named type, use the typedef. */
10091 gen_type_die (qualified_type, context_die);
10092 return lookup_type_die (qualified_type);
10094 else if (type_quals == TYPE_QUALS (dtype))
10095 /* cv-unqualified version of named type. Just use the unnamed
10096 type to which it refers. */
10097 return modified_type_die (DECL_ORIGINAL_TYPE (name), type_quals, context_die);
10098 /* Else cv-qualified version of named type; fall through. */
10101 mod_scope = scope_die_for (type, context_die);
10103 if ((type_quals & TYPE_QUAL_CONST)
10104 /* If both is_const_type and is_volatile_type, prefer the path
10105 which leads to a qualified type. */
10106 && (!is_volatile_type
10107 || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
10108 || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
10110 mod_type_die = new_die (DW_TAG_const_type, mod_scope, type);
10111 sub_die = modified_type_die (type, type_quals & ~TYPE_QUAL_CONST, context_die);
10113 else if (type_quals & TYPE_QUAL_VOLATILE)
10115 mod_type_die = new_die (DW_TAG_volatile_type, mod_scope, type);
10116 sub_die = modified_type_die (type, type_quals & ~TYPE_QUAL_VOLATILE, context_die);
10118 else if (use_upc_dwarf2_extensions
10119 && (type_quals & TYPE_QUAL_SHARED))
10121 HOST_WIDE_INT block_factor = 1;
10123 /* Inside the compiler,
10124 "shared int x;" TYPE_BLOCK_FACTOR is null.
10125 "shared [] int *p;" TYPE_BLOCK_FACTOR is zero.
10126 "shared [10] int x[50];" TYPE_BLOCK_FACTOR is 10 * bitsize(int)
10127 The DWARF2 encoding is as follows:
10128 "shared int x;" DW_AT_count: 1
10129 "shared [] int *p;" <no DW_AT_count attribute>
10130 "shared [10] int x[50];" DW_AT_count: 10
10131 The logic below handles thse various contingencies. */
10133 mod_type_die = new_die (DW_TAG_upc_shared_type,
10134 comp_unit_die (), type);
10136 if (TYPE_HAS_BLOCK_FACTOR (type))
10137 block_factor = TREE_INT_CST_LOW (TYPE_BLOCK_FACTOR (type));
10139 if (block_factor != 0)
10140 add_AT_unsigned (mod_type_die, DW_AT_count, block_factor);
10142 sub_die = modified_type_die (type,
10143 type_quals & ~TYPE_QUAL_SHARED,
10144 context_die);
10146 else if (use_upc_dwarf2_extensions && type_quals & TYPE_QUAL_STRICT)
10148 mod_type_die = new_die (DW_TAG_upc_strict_type,
10149 comp_unit_die (), type);
10150 sub_die = modified_type_die (type,
10151 type_quals & ~TYPE_QUAL_STRICT,
10152 context_die);
10154 else if (use_upc_dwarf2_extensions && type_quals & TYPE_QUAL_RELAXED)
10156 mod_type_die = new_die (DW_TAG_upc_relaxed_type,
10157 comp_unit_die (), type);
10158 sub_die = modified_type_die (type,
10159 type_quals & ~TYPE_QUAL_RELAXED,
10160 context_die);
10162 else if (code == POINTER_TYPE)
10164 mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10165 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10166 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10167 item_type = TREE_TYPE (type);
10168 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10169 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10170 TYPE_ADDR_SPACE (item_type));
10172 else if (code == REFERENCE_TYPE)
10174 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10175 mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10176 type);
10177 else
10178 mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10179 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10180 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10181 item_type = TREE_TYPE (type);
10182 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10183 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10184 TYPE_ADDR_SPACE (item_type));
10186 else if (code == INTEGER_TYPE
10187 && TREE_TYPE (type) != NULL_TREE
10188 && subrange_type_for_debug_p (type, &low, &high))
10190 mod_type_die = subrange_type_die (type, low, high, context_die);
10191 item_type = TREE_TYPE (type);
10193 else if (is_base_type (type))
10194 mod_type_die = base_type_die (type);
10195 else
10197 gen_type_die (type, context_die);
10199 /* We have to get the type_main_variant here (and pass that to the
10200 `lookup_type_die' routine) because the ..._TYPE node we have
10201 might simply be a *copy* of some original type node (where the
10202 copy was created to help us keep track of typedef names) and
10203 that copy might have a different TYPE_UID from the original
10204 ..._TYPE node. */
10205 if (TREE_CODE (type) != VECTOR_TYPE)
10206 return lookup_type_die (type_main_variant (type));
10207 else
10208 /* Vectors have the debugging information in the type,
10209 not the main variant. */
10210 return lookup_type_die (type);
10213 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
10214 don't output a DW_TAG_typedef, since there isn't one in the
10215 user's program; just attach a DW_AT_name to the type.
10216 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10217 if the base type already has the same name. */
10218 if (name
10219 && ((TREE_CODE (name) != TYPE_DECL
10220 && (qualified_type == TYPE_MAIN_VARIANT (type)
10221 || (!is_const_type && !is_volatile_type)))
10222 || (TREE_CODE (name) == TYPE_DECL
10223 && TREE_TYPE (name) == qualified_type
10224 && DECL_NAME (name))))
10226 if (TREE_CODE (name) == TYPE_DECL)
10227 /* Could just call add_name_and_src_coords_attributes here,
10228 but since this is a builtin type it doesn't have any
10229 useful source coordinates anyway. */
10230 name = DECL_NAME (name);
10231 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10233 /* This probably indicates a bug. */
10234 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10236 name = TYPE_NAME (type);
10237 if (name
10238 && TREE_CODE (name) == TYPE_DECL)
10239 name = DECL_NAME (name);
10240 add_name_attribute (mod_type_die,
10241 name ? IDENTIFIER_POINTER (name) : "__unknown__");
10244 if (qualified_type)
10245 equate_type_number_to_die (qualified_type, mod_type_die);
10247 if (item_type)
10248 /* We must do this after the equate_type_number_to_die call, in case
10249 this is a recursive type. This ensures that the modified_type_die
10250 recursion will terminate even if the type is recursive. Recursive
10251 types are possible in Ada. */
10252 sub_die = modified_type_die (item_type, TYPE_QUALS (item_type), context_die);
10254 if (sub_die != NULL)
10255 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10257 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10258 if (TYPE_ARTIFICIAL (type))
10259 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10261 return mod_type_die;
10264 /* Generate DIEs for the generic parameters of T.
10265 T must be either a generic type or a generic function.
10266 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
10268 static void
10269 gen_generic_params_dies (tree t)
10271 tree parms, args;
10272 int parms_num, i;
10273 dw_die_ref die = NULL;
10275 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10276 return;
10278 if (TYPE_P (t))
10279 die = lookup_type_die (t);
10280 else if (DECL_P (t))
10281 die = lookup_decl_die (t);
10283 gcc_assert (die);
10285 parms = lang_hooks.get_innermost_generic_parms (t);
10286 if (!parms)
10287 /* T has no generic parameter. It means T is neither a generic type
10288 or function. End of story. */
10289 return;
10291 parms_num = TREE_VEC_LENGTH (parms);
10292 args = lang_hooks.get_innermost_generic_args (t);
10293 for (i = 0; i < parms_num; i++)
10295 tree parm, arg, arg_pack_elems;
10297 parm = TREE_VEC_ELT (parms, i);
10298 arg = TREE_VEC_ELT (args, i);
10299 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
10300 gcc_assert (parm && TREE_VALUE (parm) && arg);
10302 if (parm && TREE_VALUE (parm) && arg)
10304 /* If PARM represents a template parameter pack,
10305 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
10306 by DW_TAG_template_*_parameter DIEs for the argument
10307 pack elements of ARG. Note that ARG would then be
10308 an argument pack. */
10309 if (arg_pack_elems)
10310 template_parameter_pack_die (TREE_VALUE (parm),
10311 arg_pack_elems,
10312 die);
10313 else
10314 generic_parameter_die (TREE_VALUE (parm), arg,
10315 true /* Emit DW_AT_name */, die);
10320 /* Create and return a DIE for PARM which should be
10321 the representation of a generic type parameter.
10322 For instance, in the C++ front end, PARM would be a template parameter.
10323 ARG is the argument to PARM.
10324 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
10325 name of the PARM.
10326 PARENT_DIE is the parent DIE which the new created DIE should be added to,
10327 as a child node. */
10329 static dw_die_ref
10330 generic_parameter_die (tree parm, tree arg,
10331 bool emit_name_p,
10332 dw_die_ref parent_die)
10334 dw_die_ref tmpl_die = NULL;
10335 const char *name = NULL;
10337 if (!parm || !DECL_NAME (parm) || !arg)
10338 return NULL;
10340 /* We support non-type generic parameters and arguments,
10341 type generic parameters and arguments, as well as
10342 generic generic parameters (a.k.a. template template parameters in C++)
10343 and arguments. */
10344 if (TREE_CODE (parm) == PARM_DECL)
10345 /* PARM is a nontype generic parameter */
10346 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10347 else if (TREE_CODE (parm) == TYPE_DECL)
10348 /* PARM is a type generic parameter. */
10349 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10350 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10351 /* PARM is a generic generic parameter.
10352 Its DIE is a GNU extension. It shall have a
10353 DW_AT_name attribute to represent the name of the template template
10354 parameter, and a DW_AT_GNU_template_name attribute to represent the
10355 name of the template template argument. */
10356 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10357 parent_die, parm);
10358 else
10359 gcc_unreachable ();
10361 if (tmpl_die)
10363 tree tmpl_type;
10365 /* If PARM is a generic parameter pack, it means we are
10366 emitting debug info for a template argument pack element.
10367 In other terms, ARG is a template argument pack element.
10368 In that case, we don't emit any DW_AT_name attribute for
10369 the die. */
10370 if (emit_name_p)
10372 name = IDENTIFIER_POINTER (DECL_NAME (parm));
10373 gcc_assert (name);
10374 add_AT_string (tmpl_die, DW_AT_name, name);
10377 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10379 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10380 TMPL_DIE should have a child DW_AT_type attribute that is set
10381 to the type of the argument to PARM, which is ARG.
10382 If PARM is a type generic parameter, TMPL_DIE should have a
10383 child DW_AT_type that is set to ARG. */
10384 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10385 add_type_attribute (tmpl_die, tmpl_type, 0,
10386 TREE_THIS_VOLATILE (tmpl_type),
10387 parent_die);
10389 else
10391 /* So TMPL_DIE is a DIE representing a
10392 a generic generic template parameter, a.k.a template template
10393 parameter in C++ and arg is a template. */
10395 /* The DW_AT_GNU_template_name attribute of the DIE must be set
10396 to the name of the argument. */
10397 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
10398 if (name)
10399 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
10402 if (TREE_CODE (parm) == PARM_DECL)
10403 /* So PARM is a non-type generic parameter.
10404 DWARF3 5.6.8 says we must set a DW_AT_const_value child
10405 attribute of TMPL_DIE which value represents the value
10406 of ARG.
10407 We must be careful here:
10408 The value of ARG might reference some function decls.
10409 We might currently be emitting debug info for a generic
10410 type and types are emitted before function decls, we don't
10411 know if the function decls referenced by ARG will actually be
10412 emitted after cgraph computations.
10413 So must defer the generation of the DW_AT_const_value to
10414 after cgraph is ready. */
10415 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
10418 return tmpl_die;
10421 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
10422 PARM_PACK must be a template parameter pack. The returned DIE
10423 will be child DIE of PARENT_DIE. */
10425 static dw_die_ref
10426 template_parameter_pack_die (tree parm_pack,
10427 tree parm_pack_args,
10428 dw_die_ref parent_die)
10430 dw_die_ref die;
10431 int j;
10433 gcc_assert (parent_die && parm_pack);
10435 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
10436 add_name_and_src_coords_attributes (die, parm_pack);
10437 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
10438 generic_parameter_die (parm_pack,
10439 TREE_VEC_ELT (parm_pack_args, j),
10440 false /* Don't emit DW_AT_name */,
10441 die);
10442 return die;
10445 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
10446 an enumerated type. */
10448 static inline int
10449 type_is_enum (const_tree type)
10451 return TREE_CODE (type) == ENUMERAL_TYPE;
10454 /* Return the DBX register number described by a given RTL node. */
10456 static unsigned int
10457 dbx_reg_number (const_rtx rtl)
10459 unsigned regno = REGNO (rtl);
10461 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10463 #ifdef LEAF_REG_REMAP
10464 if (crtl->uses_only_leaf_regs)
10466 int leaf_reg = LEAF_REG_REMAP (regno);
10467 if (leaf_reg != -1)
10468 regno = (unsigned) leaf_reg;
10470 #endif
10472 regno = DBX_REGISTER_NUMBER (regno);
10473 gcc_assert (regno != INVALID_REGNUM);
10474 return regno;
10477 /* Optionally add a DW_OP_piece term to a location description expression.
10478 DW_OP_piece is only added if the location description expression already
10479 doesn't end with DW_OP_piece. */
10481 static void
10482 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10484 dw_loc_descr_ref loc;
10486 if (*list_head != NULL)
10488 /* Find the end of the chain. */
10489 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10492 if (loc->dw_loc_opc != DW_OP_piece)
10493 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10497 /* Return a location descriptor that designates a machine register or
10498 zero if there is none. */
10500 static dw_loc_descr_ref
10501 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10503 rtx regs;
10505 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10506 return 0;
10508 /* We only use "frame base" when we're sure we're talking about the
10509 post-prologue local stack frame. We do this by *not* running
10510 register elimination until this point, and recognizing the special
10511 argument pointer and soft frame pointer rtx's.
10512 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
10513 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
10514 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
10516 dw_loc_descr_ref result = NULL;
10518 if (dwarf_version >= 4 || !dwarf_strict)
10520 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
10521 initialized);
10522 if (result)
10523 add_loc_descr (&result,
10524 new_loc_descr (DW_OP_stack_value, 0, 0));
10526 return result;
10529 regs = targetm.dwarf_register_span (rtl);
10531 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
10532 return multiple_reg_loc_descriptor (rtl, regs, initialized);
10533 else
10535 unsigned int dbx_regnum = dbx_reg_number (rtl);
10536 if (dbx_regnum == IGNORED_DWARF_REGNUM)
10537 return 0;
10538 return one_reg_loc_descriptor (dbx_regnum, initialized);
10542 /* Return a location descriptor that designates a machine register for
10543 a given hard register number. */
10545 static dw_loc_descr_ref
10546 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
10548 dw_loc_descr_ref reg_loc_descr;
10550 if (regno <= 31)
10551 reg_loc_descr
10552 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
10553 else
10554 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
10556 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10557 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10559 return reg_loc_descr;
10562 /* Given an RTL of a register, return a location descriptor that
10563 designates a value that spans more than one register. */
10565 static dw_loc_descr_ref
10566 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
10567 enum var_init_status initialized)
10569 int nregs, size, i;
10570 unsigned reg;
10571 dw_loc_descr_ref loc_result = NULL;
10573 reg = REGNO (rtl);
10574 #ifdef LEAF_REG_REMAP
10575 if (crtl->uses_only_leaf_regs)
10577 int leaf_reg = LEAF_REG_REMAP (reg);
10578 if (leaf_reg != -1)
10579 reg = (unsigned) leaf_reg;
10581 #endif
10582 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
10583 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
10585 /* Simple, contiguous registers. */
10586 if (regs == NULL_RTX)
10588 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
10590 loc_result = NULL;
10591 while (nregs--)
10593 dw_loc_descr_ref t;
10595 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
10596 VAR_INIT_STATUS_INITIALIZED);
10597 add_loc_descr (&loc_result, t);
10598 add_loc_descr_op_piece (&loc_result, size);
10599 ++reg;
10601 return loc_result;
10604 /* Now onto stupid register sets in non contiguous locations. */
10606 gcc_assert (GET_CODE (regs) == PARALLEL);
10608 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10609 loc_result = NULL;
10611 for (i = 0; i < XVECLEN (regs, 0); ++i)
10613 dw_loc_descr_ref t;
10615 t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
10616 VAR_INIT_STATUS_INITIALIZED);
10617 add_loc_descr (&loc_result, t);
10618 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10619 add_loc_descr_op_piece (&loc_result, size);
10622 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10623 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10624 return loc_result;
10627 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
10629 /* Return a location descriptor that designates a constant i,
10630 as a compound operation from constant (i >> shift), constant shift
10631 and DW_OP_shl. */
10633 static dw_loc_descr_ref
10634 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10636 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
10637 add_loc_descr (&ret, int_loc_descriptor (shift));
10638 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
10639 return ret;
10642 /* Return a location descriptor that designates a constant. */
10644 static dw_loc_descr_ref
10645 int_loc_descriptor (HOST_WIDE_INT i)
10647 enum dwarf_location_atom op;
10649 /* Pick the smallest representation of a constant, rather than just
10650 defaulting to the LEB encoding. */
10651 if (i >= 0)
10653 int clz = clz_hwi (i);
10654 int ctz = ctz_hwi (i);
10655 if (i <= 31)
10656 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
10657 else if (i <= 0xff)
10658 op = DW_OP_const1u;
10659 else if (i <= 0xffff)
10660 op = DW_OP_const2u;
10661 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10662 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10663 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
10664 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
10665 while DW_OP_const4u is 5 bytes. */
10666 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
10667 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10668 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10669 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
10670 while DW_OP_const4u is 5 bytes. */
10671 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10672 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10673 op = DW_OP_const4u;
10674 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10675 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10676 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
10677 while DW_OP_constu of constant >= 0x100000000 takes at least
10678 6 bytes. */
10679 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10680 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10681 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
10682 >= HOST_BITS_PER_WIDE_INT)
10683 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
10684 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
10685 while DW_OP_constu takes in this case at least 6 bytes. */
10686 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
10687 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10688 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10689 && size_of_uleb128 (i) > 6)
10690 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
10691 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
10692 else
10693 op = DW_OP_constu;
10695 else
10697 if (i >= -0x80)
10698 op = DW_OP_const1s;
10699 else if (i >= -0x8000)
10700 op = DW_OP_const2s;
10701 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10703 if (size_of_int_loc_descriptor (i) < 5)
10705 dw_loc_descr_ref ret = int_loc_descriptor (-i);
10706 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10707 return ret;
10709 op = DW_OP_const4s;
10711 else
10713 if (size_of_int_loc_descriptor (i)
10714 < (unsigned long) 1 + size_of_sleb128 (i))
10716 dw_loc_descr_ref ret = int_loc_descriptor (-i);
10717 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10718 return ret;
10720 op = DW_OP_consts;
10724 return new_loc_descr (op, i, 0);
10727 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
10728 without actually allocating it. */
10730 static unsigned long
10731 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10733 return size_of_int_loc_descriptor (i >> shift)
10734 + size_of_int_loc_descriptor (shift)
10735 + 1;
10738 /* Return size_of_locs (int_loc_descriptor (i)) without
10739 actually allocating it. */
10741 static unsigned long
10742 size_of_int_loc_descriptor (HOST_WIDE_INT i)
10744 unsigned long s;
10746 if (i >= 0)
10748 int clz, ctz;
10749 if (i <= 31)
10750 return 1;
10751 else if (i <= 0xff)
10752 return 2;
10753 else if (i <= 0xffff)
10754 return 3;
10755 clz = clz_hwi (i);
10756 ctz = ctz_hwi (i);
10757 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10758 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10759 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10760 - clz - 5);
10761 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10762 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10763 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10764 - clz - 8);
10765 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10766 return 5;
10767 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
10768 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10769 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10770 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10771 - clz - 8);
10772 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10773 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
10774 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10775 - clz - 16);
10776 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10777 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10778 && s > 6)
10779 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10780 - clz - 32);
10781 else
10782 return 1 + s;
10784 else
10786 if (i >= -0x80)
10787 return 2;
10788 else if (i >= -0x8000)
10789 return 3;
10790 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10792 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
10794 s = size_of_int_loc_descriptor (-i) + 1;
10795 if (s < 5)
10796 return s;
10798 return 5;
10800 else
10802 unsigned long r = 1 + size_of_sleb128 (i);
10803 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
10805 s = size_of_int_loc_descriptor (-i) + 1;
10806 if (s < r)
10807 return s;
10809 return r;
10814 /* Return loc description representing "address" of integer value.
10815 This can appear only as toplevel expression. */
10817 static dw_loc_descr_ref
10818 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
10820 int litsize;
10821 dw_loc_descr_ref loc_result = NULL;
10823 if (!(dwarf_version >= 4 || !dwarf_strict))
10824 return NULL;
10826 litsize = size_of_int_loc_descriptor (i);
10827 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
10828 is more compact. For DW_OP_stack_value we need:
10829 litsize + 1 (DW_OP_stack_value)
10830 and for DW_OP_implicit_value:
10831 1 (DW_OP_implicit_value) + 1 (length) + size. */
10832 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
10834 loc_result = int_loc_descriptor (i);
10835 add_loc_descr (&loc_result,
10836 new_loc_descr (DW_OP_stack_value, 0, 0));
10837 return loc_result;
10840 loc_result = new_loc_descr (DW_OP_implicit_value,
10841 size, 0);
10842 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
10843 loc_result->dw_loc_oprnd2.v.val_int = i;
10844 return loc_result;
10847 /* Return a location descriptor that designates a base+offset location. */
10849 static dw_loc_descr_ref
10850 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
10851 enum var_init_status initialized)
10853 unsigned int regno;
10854 dw_loc_descr_ref result;
10855 dw_fde_ref fde = cfun->fde;
10857 /* We only use "frame base" when we're sure we're talking about the
10858 post-prologue local stack frame. We do this by *not* running
10859 register elimination until this point, and recognizing the special
10860 argument pointer and soft frame pointer rtx's. */
10861 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
10863 rtx elim = (ira_use_lra_p
10864 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
10865 : eliminate_regs (reg, VOIDmode, NULL_RTX));
10867 if (elim != reg)
10869 if (GET_CODE (elim) == PLUS)
10871 offset += INTVAL (XEXP (elim, 1));
10872 elim = XEXP (elim, 0);
10874 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
10875 && (elim == hard_frame_pointer_rtx
10876 || elim == stack_pointer_rtx))
10877 || elim == (frame_pointer_needed
10878 ? hard_frame_pointer_rtx
10879 : stack_pointer_rtx));
10881 /* If drap register is used to align stack, use frame
10882 pointer + offset to access stack variables. If stack
10883 is aligned without drap, use stack pointer + offset to
10884 access stack variables. */
10885 if (crtl->stack_realign_tried
10886 && reg == frame_pointer_rtx)
10888 int base_reg
10889 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
10890 ? HARD_FRAME_POINTER_REGNUM
10891 : REGNO (elim));
10892 return new_reg_loc_descr (base_reg, offset);
10895 gcc_assert (frame_pointer_fb_offset_valid);
10896 offset += frame_pointer_fb_offset;
10897 return new_loc_descr (DW_OP_fbreg, offset, 0);
10901 regno = DWARF_FRAME_REGNUM (REGNO (reg));
10903 if (!optimize && fde
10904 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
10906 /* Use cfa+offset to represent the location of arguments passed
10907 on the stack when drap is used to align stack.
10908 Only do this when not optimizing, for optimized code var-tracking
10909 is supposed to track where the arguments live and the register
10910 used as vdrap or drap in some spot might be used for something
10911 else in other part of the routine. */
10912 return new_loc_descr (DW_OP_fbreg, offset, 0);
10915 if (regno <= 31)
10916 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
10917 offset, 0);
10918 else
10919 result = new_loc_descr (DW_OP_bregx, regno, offset);
10921 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10922 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10924 return result;
10927 /* Return true if this RTL expression describes a base+offset calculation. */
10929 static inline int
10930 is_based_loc (const_rtx rtl)
10932 return (GET_CODE (rtl) == PLUS
10933 && ((REG_P (XEXP (rtl, 0))
10934 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
10935 && CONST_INT_P (XEXP (rtl, 1)))));
10938 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
10939 failed. */
10941 static dw_loc_descr_ref
10942 tls_mem_loc_descriptor (rtx mem)
10944 tree base;
10945 dw_loc_descr_ref loc_result;
10947 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
10948 return NULL;
10950 base = get_base_address (MEM_EXPR (mem));
10951 if (base == NULL
10952 || TREE_CODE (base) != VAR_DECL
10953 || !DECL_THREAD_LOCAL_P (base))
10954 return NULL;
10956 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
10957 if (loc_result == NULL)
10958 return NULL;
10960 if (MEM_OFFSET (mem))
10961 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
10963 return loc_result;
10966 /* Output debug info about reason why we failed to expand expression as dwarf
10967 expression. */
10969 static void
10970 expansion_failed (tree expr, rtx rtl, char const *reason)
10972 if (dump_file && (dump_flags & TDF_DETAILS))
10974 fprintf (dump_file, "Failed to expand as dwarf: ");
10975 if (expr)
10976 print_generic_expr (dump_file, expr, dump_flags);
10977 if (rtl)
10979 fprintf (dump_file, "\n");
10980 print_rtl (dump_file, rtl);
10982 fprintf (dump_file, "\nReason: %s\n", reason);
10986 /* Helper function for const_ok_for_output, called either directly
10987 or via for_each_rtx. */
10989 static int
10990 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
10992 rtx rtl = *rtlp;
10994 if (GET_CODE (rtl) == UNSPEC)
10996 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
10997 we can't express it in the debug info. */
10998 #ifdef ENABLE_CHECKING
10999 /* Don't complain about TLS UNSPECs, those are just too hard to
11000 delegitimize. Note this could be a non-decl SYMBOL_REF such as
11001 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11002 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
11003 if (XVECLEN (rtl, 0) == 0
11004 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11005 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
11006 inform (current_function_decl
11007 ? DECL_SOURCE_LOCATION (current_function_decl)
11008 : UNKNOWN_LOCATION,
11009 #if NUM_UNSPEC_VALUES > 0
11010 "non-delegitimized UNSPEC %s (%d) found in variable location",
11011 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11012 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11013 XINT (rtl, 1));
11014 #else
11015 "non-delegitimized UNSPEC %d found in variable location",
11016 XINT (rtl, 1));
11017 #endif
11018 #endif
11019 expansion_failed (NULL_TREE, rtl,
11020 "UNSPEC hasn't been delegitimized.\n");
11021 return 1;
11024 if (targetm.const_not_ok_for_debug_p (rtl))
11026 expansion_failed (NULL_TREE, rtl,
11027 "Expression rejected for debug by the backend.\n");
11028 return 1;
11031 if (GET_CODE (rtl) != SYMBOL_REF)
11032 return 0;
11034 if (CONSTANT_POOL_ADDRESS_P (rtl))
11036 bool marked;
11037 get_pool_constant_mark (rtl, &marked);
11038 /* If all references to this pool constant were optimized away,
11039 it was not output and thus we can't represent it. */
11040 if (!marked)
11042 expansion_failed (NULL_TREE, rtl,
11043 "Constant was removed from constant pool.\n");
11044 return 1;
11048 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11049 return 1;
11051 /* Avoid references to external symbols in debug info, on several targets
11052 the linker might even refuse to link when linking a shared library,
11053 and in many other cases the relocations for .debug_info/.debug_loc are
11054 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
11055 to be defined within the same shared library or executable are fine. */
11056 if (SYMBOL_REF_EXTERNAL_P (rtl))
11058 tree decl = SYMBOL_REF_DECL (rtl);
11060 if (decl == NULL || !targetm.binds_local_p (decl))
11062 expansion_failed (NULL_TREE, rtl,
11063 "Symbol not defined in current TU.\n");
11064 return 1;
11068 return 0;
11071 /* Return true if constant RTL can be emitted in DW_OP_addr or
11072 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
11073 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
11075 static bool
11076 const_ok_for_output (rtx rtl)
11078 if (GET_CODE (rtl) == SYMBOL_REF)
11079 return const_ok_for_output_1 (&rtl, NULL) == 0;
11081 if (GET_CODE (rtl) == CONST)
11082 return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
11084 return true;
11087 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11088 if possible, NULL otherwise. */
11090 static dw_die_ref
11091 base_type_for_mode (enum machine_mode mode, bool unsignedp)
11093 dw_die_ref type_die;
11094 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11096 if (type == NULL)
11097 return NULL;
11098 switch (TREE_CODE (type))
11100 case INTEGER_TYPE:
11101 case REAL_TYPE:
11102 break;
11103 default:
11104 return NULL;
11106 type_die = lookup_type_die (type);
11107 if (!type_die)
11108 type_die = modified_type_die (type, TYPE_UNQUALIFIED, comp_unit_die ());
11109 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11110 return NULL;
11111 return type_die;
11114 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11115 type matching MODE, or, if MODE is narrower than or as wide as
11116 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
11117 possible. */
11119 static dw_loc_descr_ref
11120 convert_descriptor_to_mode (enum machine_mode mode, dw_loc_descr_ref op)
11122 enum machine_mode outer_mode = mode;
11123 dw_die_ref type_die;
11124 dw_loc_descr_ref cvt;
11126 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11128 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11129 return op;
11131 type_die = base_type_for_mode (outer_mode, 1);
11132 if (type_die == NULL)
11133 return NULL;
11134 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11135 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11136 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11137 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11138 add_loc_descr (&op, cvt);
11139 return op;
11142 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
11144 static dw_loc_descr_ref
11145 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11146 dw_loc_descr_ref op1)
11148 dw_loc_descr_ref ret = op0;
11149 add_loc_descr (&ret, op1);
11150 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11151 if (STORE_FLAG_VALUE != 1)
11153 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11154 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11156 return ret;
11159 /* Return location descriptor for signed comparison OP RTL. */
11161 static dw_loc_descr_ref
11162 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11163 enum machine_mode mem_mode)
11165 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11166 dw_loc_descr_ref op0, op1;
11167 int shift;
11169 if (op_mode == VOIDmode)
11170 op_mode = GET_MODE (XEXP (rtl, 1));
11171 if (op_mode == VOIDmode)
11172 return NULL;
11174 if (dwarf_strict
11175 && (GET_MODE_CLASS (op_mode) != MODE_INT
11176 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11177 return NULL;
11179 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11180 VAR_INIT_STATUS_INITIALIZED);
11181 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11182 VAR_INIT_STATUS_INITIALIZED);
11184 if (op0 == NULL || op1 == NULL)
11185 return NULL;
11187 if (GET_MODE_CLASS (op_mode) != MODE_INT
11188 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11189 return compare_loc_descriptor (op, op0, op1);
11191 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11193 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11194 dw_loc_descr_ref cvt;
11196 if (type_die == NULL)
11197 return NULL;
11198 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11199 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11200 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11201 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11202 add_loc_descr (&op0, cvt);
11203 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11204 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11205 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11206 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11207 add_loc_descr (&op1, cvt);
11208 return compare_loc_descriptor (op, op0, op1);
11211 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11212 /* For eq/ne, if the operands are known to be zero-extended,
11213 there is no need to do the fancy shifting up. */
11214 if (op == DW_OP_eq || op == DW_OP_ne)
11216 dw_loc_descr_ref last0, last1;
11217 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11219 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11221 /* deref_size zero extends, and for constants we can check
11222 whether they are zero extended or not. */
11223 if (((last0->dw_loc_opc == DW_OP_deref_size
11224 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11225 || (CONST_INT_P (XEXP (rtl, 0))
11226 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11227 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11228 && ((last1->dw_loc_opc == DW_OP_deref_size
11229 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11230 || (CONST_INT_P (XEXP (rtl, 1))
11231 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11232 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11233 return compare_loc_descriptor (op, op0, op1);
11235 /* EQ/NE comparison against constant in narrower type than
11236 DWARF2_ADDR_SIZE can be performed either as
11237 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11238 DW_OP_{eq,ne}
11240 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11241 DW_OP_{eq,ne}. Pick whatever is shorter. */
11242 if (CONST_INT_P (XEXP (rtl, 1))
11243 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11244 && (size_of_int_loc_descriptor (shift) + 1
11245 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11246 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11247 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11248 & GET_MODE_MASK (op_mode))))
11250 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
11251 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11252 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11253 & GET_MODE_MASK (op_mode));
11254 return compare_loc_descriptor (op, op0, op1);
11257 add_loc_descr (&op0, int_loc_descriptor (shift));
11258 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11259 if (CONST_INT_P (XEXP (rtl, 1)))
11260 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11261 else
11263 add_loc_descr (&op1, int_loc_descriptor (shift));
11264 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11266 return compare_loc_descriptor (op, op0, op1);
11269 /* Return location descriptor for unsigned comparison OP RTL. */
11271 static dw_loc_descr_ref
11272 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11273 enum machine_mode mem_mode)
11275 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11276 dw_loc_descr_ref op0, op1;
11278 if (op_mode == VOIDmode)
11279 op_mode = GET_MODE (XEXP (rtl, 1));
11280 if (op_mode == VOIDmode)
11281 return NULL;
11282 if (GET_MODE_CLASS (op_mode) != MODE_INT)
11283 return NULL;
11285 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11286 return NULL;
11288 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11289 VAR_INIT_STATUS_INITIALIZED);
11290 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11291 VAR_INIT_STATUS_INITIALIZED);
11293 if (op0 == NULL || op1 == NULL)
11294 return NULL;
11296 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
11298 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
11299 dw_loc_descr_ref last0, last1;
11300 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11302 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11304 if (CONST_INT_P (XEXP (rtl, 0)))
11305 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
11306 /* deref_size zero extends, so no need to mask it again. */
11307 else if (last0->dw_loc_opc != DW_OP_deref_size
11308 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11310 add_loc_descr (&op0, int_loc_descriptor (mask));
11311 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11313 if (CONST_INT_P (XEXP (rtl, 1)))
11314 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11315 /* deref_size zero extends, so no need to mask it again. */
11316 else if (last1->dw_loc_opc != DW_OP_deref_size
11317 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11319 add_loc_descr (&op1, int_loc_descriptor (mask));
11320 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11323 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11325 HOST_WIDE_INT bias = 1;
11326 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11327 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11328 if (CONST_INT_P (XEXP (rtl, 1)))
11329 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
11330 + INTVAL (XEXP (rtl, 1)));
11331 else
11332 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
11333 bias, 0));
11335 return compare_loc_descriptor (op, op0, op1);
11338 /* Return location descriptor for {U,S}{MIN,MAX}. */
11340 static dw_loc_descr_ref
11341 minmax_loc_descriptor (rtx rtl, enum machine_mode mode,
11342 enum machine_mode mem_mode)
11344 enum dwarf_location_atom op;
11345 dw_loc_descr_ref op0, op1, ret;
11346 dw_loc_descr_ref bra_node, drop_node;
11348 if (dwarf_strict
11349 && (GET_MODE_CLASS (mode) != MODE_INT
11350 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
11351 return NULL;
11353 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11354 VAR_INIT_STATUS_INITIALIZED);
11355 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11356 VAR_INIT_STATUS_INITIALIZED);
11358 if (op0 == NULL || op1 == NULL)
11359 return NULL;
11361 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
11362 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
11363 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
11364 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
11366 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11368 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11369 add_loc_descr (&op0, int_loc_descriptor (mask));
11370 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11371 add_loc_descr (&op1, int_loc_descriptor (mask));
11372 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11374 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11376 HOST_WIDE_INT bias = 1;
11377 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11378 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11379 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11382 else if (GET_MODE_CLASS (mode) == MODE_INT
11383 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11385 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
11386 add_loc_descr (&op0, int_loc_descriptor (shift));
11387 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11388 add_loc_descr (&op1, int_loc_descriptor (shift));
11389 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11391 else if (GET_MODE_CLASS (mode) == MODE_INT
11392 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11394 dw_die_ref type_die = base_type_for_mode (mode, 0);
11395 dw_loc_descr_ref cvt;
11396 if (type_die == NULL)
11397 return NULL;
11398 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11399 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11400 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11401 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11402 add_loc_descr (&op0, cvt);
11403 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11404 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11405 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11406 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11407 add_loc_descr (&op1, cvt);
11410 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
11411 op = DW_OP_lt;
11412 else
11413 op = DW_OP_gt;
11414 ret = op0;
11415 add_loc_descr (&ret, op1);
11416 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11417 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11418 add_loc_descr (&ret, bra_node);
11419 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11420 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11421 add_loc_descr (&ret, drop_node);
11422 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11423 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11424 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
11425 && GET_MODE_CLASS (mode) == MODE_INT
11426 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11427 ret = convert_descriptor_to_mode (mode, ret);
11428 return ret;
11431 /* Helper function for mem_loc_descriptor. Perform OP binary op,
11432 but after converting arguments to type_die, afterwards
11433 convert back to unsigned. */
11435 static dw_loc_descr_ref
11436 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
11437 enum machine_mode mode, enum machine_mode mem_mode)
11439 dw_loc_descr_ref cvt, op0, op1;
11441 if (type_die == NULL)
11442 return NULL;
11443 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11444 VAR_INIT_STATUS_INITIALIZED);
11445 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11446 VAR_INIT_STATUS_INITIALIZED);
11447 if (op0 == NULL || op1 == NULL)
11448 return NULL;
11449 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11450 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11451 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11452 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11453 add_loc_descr (&op0, cvt);
11454 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11455 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11456 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11457 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11458 add_loc_descr (&op1, cvt);
11459 add_loc_descr (&op0, op1);
11460 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
11461 return convert_descriptor_to_mode (mode, op0);
11464 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
11465 const0 is DW_OP_lit0 or corresponding typed constant,
11466 const1 is DW_OP_lit1 or corresponding typed constant
11467 and constMSB is constant with just the MSB bit set
11468 for the mode):
11469 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11470 L1: const0 DW_OP_swap
11471 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
11472 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11473 L3: DW_OP_drop
11474 L4: DW_OP_nop
11476 CTZ is similar:
11477 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11478 L1: const0 DW_OP_swap
11479 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11480 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11481 L3: DW_OP_drop
11482 L4: DW_OP_nop
11484 FFS is similar:
11485 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
11486 L1: const1 DW_OP_swap
11487 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11488 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11489 L3: DW_OP_drop
11490 L4: DW_OP_nop */
11492 static dw_loc_descr_ref
11493 clz_loc_descriptor (rtx rtl, enum machine_mode mode,
11494 enum machine_mode mem_mode)
11496 dw_loc_descr_ref op0, ret, tmp;
11497 HOST_WIDE_INT valv;
11498 dw_loc_descr_ref l1jump, l1label;
11499 dw_loc_descr_ref l2jump, l2label;
11500 dw_loc_descr_ref l3jump, l3label;
11501 dw_loc_descr_ref l4jump, l4label;
11502 rtx msb;
11504 if (GET_MODE_CLASS (mode) != MODE_INT
11505 || GET_MODE (XEXP (rtl, 0)) != mode
11506 || (GET_CODE (rtl) == CLZ
11507 && GET_MODE_BITSIZE (mode) > HOST_BITS_PER_DOUBLE_INT))
11508 return NULL;
11510 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11511 VAR_INIT_STATUS_INITIALIZED);
11512 if (op0 == NULL)
11513 return NULL;
11514 ret = op0;
11515 if (GET_CODE (rtl) == CLZ)
11517 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11518 valv = GET_MODE_BITSIZE (mode);
11520 else if (GET_CODE (rtl) == FFS)
11521 valv = 0;
11522 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11523 valv = GET_MODE_BITSIZE (mode);
11524 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11525 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
11526 add_loc_descr (&ret, l1jump);
11527 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11528 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
11529 VAR_INIT_STATUS_INITIALIZED);
11530 if (tmp == NULL)
11531 return NULL;
11532 add_loc_descr (&ret, tmp);
11533 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
11534 add_loc_descr (&ret, l4jump);
11535 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
11536 ? const1_rtx : const0_rtx,
11537 mode, mem_mode,
11538 VAR_INIT_STATUS_INITIALIZED);
11539 if (l1label == NULL)
11540 return NULL;
11541 add_loc_descr (&ret, l1label);
11542 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11543 l2label = new_loc_descr (DW_OP_dup, 0, 0);
11544 add_loc_descr (&ret, l2label);
11545 if (GET_CODE (rtl) != CLZ)
11546 msb = const1_rtx;
11547 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11548 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
11549 << (GET_MODE_BITSIZE (mode) - 1));
11550 else
11551 msb = immed_double_const (0, (unsigned HOST_WIDE_INT) 1
11552 << (GET_MODE_BITSIZE (mode)
11553 - HOST_BITS_PER_WIDE_INT - 1), mode);
11554 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
11555 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11556 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
11557 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
11558 else
11559 tmp = mem_loc_descriptor (msb, mode, mem_mode,
11560 VAR_INIT_STATUS_INITIALIZED);
11561 if (tmp == NULL)
11562 return NULL;
11563 add_loc_descr (&ret, tmp);
11564 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11565 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
11566 add_loc_descr (&ret, l3jump);
11567 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11568 VAR_INIT_STATUS_INITIALIZED);
11569 if (tmp == NULL)
11570 return NULL;
11571 add_loc_descr (&ret, tmp);
11572 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
11573 ? DW_OP_shl : DW_OP_shr, 0, 0));
11574 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11575 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
11576 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11577 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
11578 add_loc_descr (&ret, l2jump);
11579 l3label = new_loc_descr (DW_OP_drop, 0, 0);
11580 add_loc_descr (&ret, l3label);
11581 l4label = new_loc_descr (DW_OP_nop, 0, 0);
11582 add_loc_descr (&ret, l4label);
11583 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11584 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11585 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11586 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11587 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11588 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
11589 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11590 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
11591 return ret;
11594 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
11595 const1 is DW_OP_lit1 or corresponding typed constant):
11596 const0 DW_OP_swap
11597 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11598 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11599 L2: DW_OP_drop
11601 PARITY is similar:
11602 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11603 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11604 L2: DW_OP_drop */
11606 static dw_loc_descr_ref
11607 popcount_loc_descriptor (rtx rtl, enum machine_mode mode,
11608 enum machine_mode mem_mode)
11610 dw_loc_descr_ref op0, ret, tmp;
11611 dw_loc_descr_ref l1jump, l1label;
11612 dw_loc_descr_ref l2jump, l2label;
11614 if (GET_MODE_CLASS (mode) != MODE_INT
11615 || GET_MODE (XEXP (rtl, 0)) != mode)
11616 return NULL;
11618 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11619 VAR_INIT_STATUS_INITIALIZED);
11620 if (op0 == NULL)
11621 return NULL;
11622 ret = op0;
11623 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11624 VAR_INIT_STATUS_INITIALIZED);
11625 if (tmp == NULL)
11626 return NULL;
11627 add_loc_descr (&ret, tmp);
11628 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11629 l1label = new_loc_descr (DW_OP_dup, 0, 0);
11630 add_loc_descr (&ret, l1label);
11631 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11632 add_loc_descr (&ret, l2jump);
11633 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11634 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11635 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11636 VAR_INIT_STATUS_INITIALIZED);
11637 if (tmp == NULL)
11638 return NULL;
11639 add_loc_descr (&ret, tmp);
11640 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11641 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
11642 ? DW_OP_plus : DW_OP_xor, 0, 0));
11643 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11644 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11645 VAR_INIT_STATUS_INITIALIZED);
11646 add_loc_descr (&ret, tmp);
11647 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11648 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11649 add_loc_descr (&ret, l1jump);
11650 l2label = new_loc_descr (DW_OP_drop, 0, 0);
11651 add_loc_descr (&ret, l2label);
11652 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11653 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11654 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11655 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11656 return ret;
11659 /* BSWAP (constS is initial shift count, either 56 or 24):
11660 constS const0
11661 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
11662 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
11663 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
11664 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
11665 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
11667 static dw_loc_descr_ref
11668 bswap_loc_descriptor (rtx rtl, enum machine_mode mode,
11669 enum machine_mode mem_mode)
11671 dw_loc_descr_ref op0, ret, tmp;
11672 dw_loc_descr_ref l1jump, l1label;
11673 dw_loc_descr_ref l2jump, l2label;
11675 if (GET_MODE_CLASS (mode) != MODE_INT
11676 || BITS_PER_UNIT != 8
11677 || (GET_MODE_BITSIZE (mode) != 32
11678 && GET_MODE_BITSIZE (mode) != 64))
11679 return NULL;
11681 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11682 VAR_INIT_STATUS_INITIALIZED);
11683 if (op0 == NULL)
11684 return NULL;
11686 ret = op0;
11687 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11688 mode, mem_mode,
11689 VAR_INIT_STATUS_INITIALIZED);
11690 if (tmp == NULL)
11691 return NULL;
11692 add_loc_descr (&ret, tmp);
11693 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11694 VAR_INIT_STATUS_INITIALIZED);
11695 if (tmp == NULL)
11696 return NULL;
11697 add_loc_descr (&ret, tmp);
11698 l1label = new_loc_descr (DW_OP_pick, 2, 0);
11699 add_loc_descr (&ret, l1label);
11700 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11701 mode, mem_mode,
11702 VAR_INIT_STATUS_INITIALIZED);
11703 add_loc_descr (&ret, tmp);
11704 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
11705 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11706 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11707 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
11708 VAR_INIT_STATUS_INITIALIZED);
11709 if (tmp == NULL)
11710 return NULL;
11711 add_loc_descr (&ret, tmp);
11712 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11713 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
11714 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11715 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11716 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11717 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11718 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11719 VAR_INIT_STATUS_INITIALIZED);
11720 add_loc_descr (&ret, tmp);
11721 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
11722 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11723 add_loc_descr (&ret, l2jump);
11724 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
11725 VAR_INIT_STATUS_INITIALIZED);
11726 add_loc_descr (&ret, tmp);
11727 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11728 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11729 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11730 add_loc_descr (&ret, l1jump);
11731 l2label = new_loc_descr (DW_OP_drop, 0, 0);
11732 add_loc_descr (&ret, l2label);
11733 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11734 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11735 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11736 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11737 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11738 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11739 return ret;
11742 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
11743 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11744 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
11745 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
11747 ROTATERT is similar:
11748 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
11749 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11750 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
11752 static dw_loc_descr_ref
11753 rotate_loc_descriptor (rtx rtl, enum machine_mode mode,
11754 enum machine_mode mem_mode)
11756 rtx rtlop1 = XEXP (rtl, 1);
11757 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
11758 int i;
11760 if (GET_MODE_CLASS (mode) != MODE_INT)
11761 return NULL;
11763 if (GET_MODE (rtlop1) != VOIDmode
11764 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
11765 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
11766 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11767 VAR_INIT_STATUS_INITIALIZED);
11768 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
11769 VAR_INIT_STATUS_INITIALIZED);
11770 if (op0 == NULL || op1 == NULL)
11771 return NULL;
11772 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11773 for (i = 0; i < 2; i++)
11775 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
11776 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
11777 mode, mem_mode,
11778 VAR_INIT_STATUS_INITIALIZED);
11779 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
11780 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11781 ? DW_OP_const4u
11782 : HOST_BITS_PER_WIDE_INT == 64
11783 ? DW_OP_const8u : DW_OP_constu,
11784 GET_MODE_MASK (mode), 0);
11785 else
11786 mask[i] = NULL;
11787 if (mask[i] == NULL)
11788 return NULL;
11789 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
11791 ret = op0;
11792 add_loc_descr (&ret, op1);
11793 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11794 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11795 if (GET_CODE (rtl) == ROTATERT)
11797 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11798 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11799 GET_MODE_BITSIZE (mode), 0));
11801 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11802 if (mask[0] != NULL)
11803 add_loc_descr (&ret, mask[0]);
11804 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11805 if (mask[1] != NULL)
11807 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11808 add_loc_descr (&ret, mask[1]);
11809 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11811 if (GET_CODE (rtl) == ROTATE)
11813 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11814 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11815 GET_MODE_BITSIZE (mode), 0));
11817 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11818 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11819 return ret;
11822 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
11823 for DEBUG_PARAMETER_REF RTL. */
11825 static dw_loc_descr_ref
11826 parameter_ref_descriptor (rtx rtl)
11828 dw_loc_descr_ref ret;
11829 dw_die_ref ref;
11831 if (dwarf_strict)
11832 return NULL;
11833 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
11834 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
11835 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
11836 if (ref)
11838 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11839 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
11840 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
11842 else
11844 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
11845 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
11847 return ret;
11850 /* The following routine converts the RTL for a variable or parameter
11851 (resident in memory) into an equivalent Dwarf representation of a
11852 mechanism for getting the address of that same variable onto the top of a
11853 hypothetical "address evaluation" stack.
11855 When creating memory location descriptors, we are effectively transforming
11856 the RTL for a memory-resident object into its Dwarf postfix expression
11857 equivalent. This routine recursively descends an RTL tree, turning
11858 it into Dwarf postfix code as it goes.
11860 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
11862 MEM_MODE is the mode of the memory reference, needed to handle some
11863 autoincrement addressing modes.
11865 Return 0 if we can't represent the location. */
11867 dw_loc_descr_ref
11868 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
11869 enum machine_mode mem_mode,
11870 enum var_init_status initialized)
11872 dw_loc_descr_ref mem_loc_result = NULL;
11873 enum dwarf_location_atom op;
11874 dw_loc_descr_ref op0, op1;
11875 rtx inner = NULL_RTX;
11877 if (mode == VOIDmode)
11878 mode = GET_MODE (rtl);
11880 /* Note that for a dynamically sized array, the location we will generate a
11881 description of here will be the lowest numbered location which is
11882 actually within the array. That's *not* necessarily the same as the
11883 zeroth element of the array. */
11885 rtl = targetm.delegitimize_address (rtl);
11887 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
11888 return NULL;
11890 switch (GET_CODE (rtl))
11892 case POST_INC:
11893 case POST_DEC:
11894 case POST_MODIFY:
11895 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
11897 case SUBREG:
11898 /* The case of a subreg may arise when we have a local (register)
11899 variable or a formal (register) parameter which doesn't quite fill
11900 up an entire register. For now, just assume that it is
11901 legitimate to make the Dwarf info refer to the whole register which
11902 contains the given subreg. */
11903 if (!subreg_lowpart_p (rtl))
11904 break;
11905 inner = SUBREG_REG (rtl);
11906 case TRUNCATE:
11907 if (inner == NULL_RTX)
11908 inner = XEXP (rtl, 0);
11909 if (GET_MODE_CLASS (mode) == MODE_INT
11910 && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
11911 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11912 #ifdef POINTERS_EXTEND_UNSIGNED
11913 || (mode == Pmode && mem_mode != VOIDmode)
11914 #endif
11916 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
11918 mem_loc_result = mem_loc_descriptor (inner,
11919 GET_MODE (inner),
11920 mem_mode, initialized);
11921 break;
11923 if (dwarf_strict)
11924 break;
11925 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
11926 break;
11927 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
11928 && (GET_MODE_CLASS (mode) != MODE_INT
11929 || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
11930 break;
11931 else
11933 dw_die_ref type_die;
11934 dw_loc_descr_ref cvt;
11936 mem_loc_result = mem_loc_descriptor (inner,
11937 GET_MODE (inner),
11938 mem_mode, initialized);
11939 if (mem_loc_result == NULL)
11940 break;
11941 type_die = base_type_for_mode (mode,
11942 GET_MODE_CLASS (mode) == MODE_INT);
11943 if (type_die == NULL)
11945 mem_loc_result = NULL;
11946 break;
11948 if (GET_MODE_SIZE (mode)
11949 != GET_MODE_SIZE (GET_MODE (inner)))
11950 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11951 else
11952 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
11953 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11954 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11955 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11956 add_loc_descr (&mem_loc_result, cvt);
11958 break;
11960 case REG:
11961 if (GET_MODE_CLASS (mode) != MODE_INT
11962 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
11963 && rtl != arg_pointer_rtx
11964 && rtl != frame_pointer_rtx
11965 #ifdef POINTERS_EXTEND_UNSIGNED
11966 && (mode != Pmode || mem_mode == VOIDmode)
11967 #endif
11970 dw_die_ref type_die;
11971 unsigned int dbx_regnum;
11973 if (dwarf_strict)
11974 break;
11975 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
11976 break;
11977 type_die = base_type_for_mode (mode,
11978 GET_MODE_CLASS (mode) == MODE_INT);
11979 if (type_die == NULL)
11980 break;
11982 dbx_regnum = dbx_reg_number (rtl);
11983 if (dbx_regnum == IGNORED_DWARF_REGNUM)
11984 break;
11985 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
11986 dbx_regnum, 0);
11987 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
11988 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
11989 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
11990 break;
11992 /* Whenever a register number forms a part of the description of the
11993 method for calculating the (dynamic) address of a memory resident
11994 object, DWARF rules require the register number be referred to as
11995 a "base register". This distinction is not based in any way upon
11996 what category of register the hardware believes the given register
11997 belongs to. This is strictly DWARF terminology we're dealing with
11998 here. Note that in cases where the location of a memory-resident
11999 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12000 OP_CONST (0)) the actual DWARF location descriptor that we generate
12001 may just be OP_BASEREG (basereg). This may look deceptively like
12002 the object in question was allocated to a register (rather than in
12003 memory) so DWARF consumers need to be aware of the subtle
12004 distinction between OP_REG and OP_BASEREG. */
12005 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12006 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12007 else if (stack_realign_drap
12008 && crtl->drap_reg
12009 && crtl->args.internal_arg_pointer == rtl
12010 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12012 /* If RTL is internal_arg_pointer, which has been optimized
12013 out, use DRAP instead. */
12014 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12015 VAR_INIT_STATUS_INITIALIZED);
12017 break;
12019 case SIGN_EXTEND:
12020 case ZERO_EXTEND:
12021 if (GET_MODE_CLASS (mode) != MODE_INT)
12022 break;
12023 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12024 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12025 if (op0 == 0)
12026 break;
12027 else if (GET_CODE (rtl) == ZERO_EXTEND
12028 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12029 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12030 < HOST_BITS_PER_WIDE_INT
12031 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12032 to expand zero extend as two shifts instead of
12033 masking. */
12034 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12036 enum machine_mode imode = GET_MODE (XEXP (rtl, 0));
12037 mem_loc_result = op0;
12038 add_loc_descr (&mem_loc_result,
12039 int_loc_descriptor (GET_MODE_MASK (imode)));
12040 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12042 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12044 int shift = DWARF2_ADDR_SIZE
12045 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12046 shift *= BITS_PER_UNIT;
12047 if (GET_CODE (rtl) == SIGN_EXTEND)
12048 op = DW_OP_shra;
12049 else
12050 op = DW_OP_shr;
12051 mem_loc_result = op0;
12052 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12053 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12054 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12055 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12057 else if (!dwarf_strict)
12059 dw_die_ref type_die1, type_die2;
12060 dw_loc_descr_ref cvt;
12062 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12063 GET_CODE (rtl) == ZERO_EXTEND);
12064 if (type_die1 == NULL)
12065 break;
12066 type_die2 = base_type_for_mode (mode, 1);
12067 if (type_die2 == NULL)
12068 break;
12069 mem_loc_result = op0;
12070 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12071 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12072 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12073 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12074 add_loc_descr (&mem_loc_result, cvt);
12075 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12076 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12077 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12078 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12079 add_loc_descr (&mem_loc_result, cvt);
12081 break;
12083 case MEM:
12085 rtx new_rtl = avoid_constant_pool_reference (rtl);
12086 if (new_rtl != rtl)
12088 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12089 initialized);
12090 if (mem_loc_result != NULL)
12091 return mem_loc_result;
12094 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12095 get_address_mode (rtl), mode,
12096 VAR_INIT_STATUS_INITIALIZED);
12097 if (mem_loc_result == NULL)
12098 mem_loc_result = tls_mem_loc_descriptor (rtl);
12099 if (mem_loc_result != NULL)
12101 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12102 || GET_MODE_CLASS (mode) != MODE_INT)
12104 dw_die_ref type_die;
12105 dw_loc_descr_ref deref;
12107 if (dwarf_strict)
12108 return NULL;
12109 type_die
12110 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12111 if (type_die == NULL)
12112 return NULL;
12113 deref = new_loc_descr (DW_OP_GNU_deref_type,
12114 GET_MODE_SIZE (mode), 0);
12115 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12116 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12117 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12118 add_loc_descr (&mem_loc_result, deref);
12120 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12121 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12122 else
12123 add_loc_descr (&mem_loc_result,
12124 new_loc_descr (DW_OP_deref_size,
12125 GET_MODE_SIZE (mode), 0));
12127 break;
12129 case LO_SUM:
12130 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12132 case LABEL_REF:
12133 /* Some ports can transform a symbol ref into a label ref, because
12134 the symbol ref is too far away and has to be dumped into a constant
12135 pool. */
12136 case CONST:
12137 case SYMBOL_REF:
12138 if (GET_MODE_CLASS (mode) != MODE_INT
12139 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12140 #ifdef POINTERS_EXTEND_UNSIGNED
12141 && (mode != Pmode || mem_mode == VOIDmode)
12142 #endif
12144 break;
12145 if (GET_CODE (rtl) == SYMBOL_REF
12146 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12148 dw_loc_descr_ref temp;
12150 /* If this is not defined, we have no way to emit the data. */
12151 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12152 break;
12154 temp = new_addr_loc_descr (rtl, dtprel_true);
12156 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12157 add_loc_descr (&mem_loc_result, temp);
12159 break;
12162 if (!const_ok_for_output (rtl))
12163 break;
12165 symref:
12166 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12167 vec_safe_push (used_rtx_array, rtl);
12168 break;
12170 case CONCAT:
12171 case CONCATN:
12172 case VAR_LOCATION:
12173 case DEBUG_IMPLICIT_PTR:
12174 expansion_failed (NULL_TREE, rtl,
12175 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12176 return 0;
12178 case ENTRY_VALUE:
12179 if (dwarf_strict)
12180 return NULL;
12181 if (REG_P (ENTRY_VALUE_EXP (rtl)))
12183 if (GET_MODE_CLASS (mode) != MODE_INT
12184 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12185 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12186 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12187 else
12189 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12190 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12191 return NULL;
12192 op0 = one_reg_loc_descriptor (dbx_regnum,
12193 VAR_INIT_STATUS_INITIALIZED);
12196 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12197 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12199 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12200 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12201 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12202 return NULL;
12204 else
12205 gcc_unreachable ();
12206 if (op0 == NULL)
12207 return NULL;
12208 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12209 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12210 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12211 break;
12213 case DEBUG_PARAMETER_REF:
12214 mem_loc_result = parameter_ref_descriptor (rtl);
12215 break;
12217 case PRE_MODIFY:
12218 /* Extract the PLUS expression nested inside and fall into
12219 PLUS code below. */
12220 rtl = XEXP (rtl, 1);
12221 goto plus;
12223 case PRE_INC:
12224 case PRE_DEC:
12225 /* Turn these into a PLUS expression and fall into the PLUS code
12226 below. */
12227 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12228 GEN_INT (GET_CODE (rtl) == PRE_INC
12229 ? GET_MODE_UNIT_SIZE (mem_mode)
12230 : -GET_MODE_UNIT_SIZE (mem_mode)));
12232 /* ... fall through ... */
12234 case PLUS:
12235 plus:
12236 if (is_based_loc (rtl)
12237 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12238 || XEXP (rtl, 0) == arg_pointer_rtx
12239 || XEXP (rtl, 0) == frame_pointer_rtx)
12240 && GET_MODE_CLASS (mode) == MODE_INT)
12241 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12242 INTVAL (XEXP (rtl, 1)),
12243 VAR_INIT_STATUS_INITIALIZED);
12244 else
12246 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12247 VAR_INIT_STATUS_INITIALIZED);
12248 if (mem_loc_result == 0)
12249 break;
12251 if (CONST_INT_P (XEXP (rtl, 1))
12252 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12253 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12254 else
12256 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12257 VAR_INIT_STATUS_INITIALIZED);
12258 if (op1 == 0)
12259 break;
12260 add_loc_descr (&mem_loc_result, op1);
12261 add_loc_descr (&mem_loc_result,
12262 new_loc_descr (DW_OP_plus, 0, 0));
12265 break;
12267 /* If a pseudo-reg is optimized away, it is possible for it to
12268 be replaced with a MEM containing a multiply or shift. */
12269 case MINUS:
12270 op = DW_OP_minus;
12271 goto do_binop;
12273 case MULT:
12274 op = DW_OP_mul;
12275 goto do_binop;
12277 case DIV:
12278 if (!dwarf_strict
12279 && GET_MODE_CLASS (mode) == MODE_INT
12280 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12282 mem_loc_result = typed_binop (DW_OP_div, rtl,
12283 base_type_for_mode (mode, 0),
12284 mode, mem_mode);
12285 break;
12287 op = DW_OP_div;
12288 goto do_binop;
12290 case UMOD:
12291 op = DW_OP_mod;
12292 goto do_binop;
12294 case ASHIFT:
12295 op = DW_OP_shl;
12296 goto do_shift;
12298 case ASHIFTRT:
12299 op = DW_OP_shra;
12300 goto do_shift;
12302 case LSHIFTRT:
12303 op = DW_OP_shr;
12304 goto do_shift;
12306 do_shift:
12307 if (GET_MODE_CLASS (mode) != MODE_INT)
12308 break;
12309 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12310 VAR_INIT_STATUS_INITIALIZED);
12312 rtx rtlop1 = XEXP (rtl, 1);
12313 if (GET_MODE (rtlop1) != VOIDmode
12314 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
12315 < GET_MODE_BITSIZE (mode))
12316 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12317 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12318 VAR_INIT_STATUS_INITIALIZED);
12321 if (op0 == 0 || op1 == 0)
12322 break;
12324 mem_loc_result = op0;
12325 add_loc_descr (&mem_loc_result, op1);
12326 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12327 break;
12329 case AND:
12330 op = DW_OP_and;
12331 goto do_binop;
12333 case IOR:
12334 op = DW_OP_or;
12335 goto do_binop;
12337 case XOR:
12338 op = DW_OP_xor;
12339 goto do_binop;
12341 do_binop:
12342 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12343 VAR_INIT_STATUS_INITIALIZED);
12344 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12345 VAR_INIT_STATUS_INITIALIZED);
12347 if (op0 == 0 || op1 == 0)
12348 break;
12350 mem_loc_result = op0;
12351 add_loc_descr (&mem_loc_result, op1);
12352 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12353 break;
12355 case MOD:
12356 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
12358 mem_loc_result = typed_binop (DW_OP_mod, rtl,
12359 base_type_for_mode (mode, 0),
12360 mode, mem_mode);
12361 break;
12364 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12365 VAR_INIT_STATUS_INITIALIZED);
12366 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12367 VAR_INIT_STATUS_INITIALIZED);
12369 if (op0 == 0 || op1 == 0)
12370 break;
12372 mem_loc_result = op0;
12373 add_loc_descr (&mem_loc_result, op1);
12374 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12375 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12376 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
12377 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
12378 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
12379 break;
12381 case UDIV:
12382 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
12384 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
12386 op = DW_OP_div;
12387 goto do_binop;
12389 mem_loc_result = typed_binop (DW_OP_div, rtl,
12390 base_type_for_mode (mode, 1),
12391 mode, mem_mode);
12393 break;
12395 case NOT:
12396 op = DW_OP_not;
12397 goto do_unop;
12399 case ABS:
12400 op = DW_OP_abs;
12401 goto do_unop;
12403 case NEG:
12404 op = DW_OP_neg;
12405 goto do_unop;
12407 do_unop:
12408 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12409 VAR_INIT_STATUS_INITIALIZED);
12411 if (op0 == 0)
12412 break;
12414 mem_loc_result = op0;
12415 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12416 break;
12418 case CONST_INT:
12419 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12420 #ifdef POINTERS_EXTEND_UNSIGNED
12421 || (mode == Pmode
12422 && mem_mode != VOIDmode
12423 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
12424 #endif
12427 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12428 break;
12430 if (!dwarf_strict
12431 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
12432 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
12434 dw_die_ref type_die = base_type_for_mode (mode, 1);
12435 enum machine_mode amode;
12436 if (type_die == NULL)
12437 return NULL;
12438 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
12439 MODE_INT, 0);
12440 if (INTVAL (rtl) >= 0
12441 && amode != BLKmode
12442 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
12443 /* const DW_OP_GNU_convert <XXX> vs.
12444 DW_OP_GNU_const_type <XXX, 1, const>. */
12445 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
12446 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
12448 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12449 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12450 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12451 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12452 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
12453 add_loc_descr (&mem_loc_result, op0);
12454 return mem_loc_result;
12456 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
12457 INTVAL (rtl));
12458 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12459 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12460 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12461 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12462 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12463 else
12465 mem_loc_result->dw_loc_oprnd2.val_class
12466 = dw_val_class_const_double;
12467 mem_loc_result->dw_loc_oprnd2.v.val_double
12468 = double_int::from_shwi (INTVAL (rtl));
12471 break;
12473 case CONST_DOUBLE:
12474 if (!dwarf_strict)
12476 dw_die_ref type_die;
12478 /* Note that a CONST_DOUBLE rtx could represent either an integer
12479 or a floating-point constant. A CONST_DOUBLE is used whenever
12480 the constant requires more than one word in order to be
12481 adequately represented. We output CONST_DOUBLEs as blocks. */
12482 if (mode == VOIDmode
12483 || (GET_MODE (rtl) == VOIDmode
12484 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
12485 break;
12486 type_die = base_type_for_mode (mode,
12487 GET_MODE_CLASS (mode) == MODE_INT);
12488 if (type_die == NULL)
12489 return NULL;
12490 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
12491 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12492 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12493 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12494 if (SCALAR_FLOAT_MODE_P (mode))
12496 unsigned int length = GET_MODE_SIZE (mode);
12497 unsigned char *array
12498 = (unsigned char*) ggc_alloc_atomic (length);
12500 insert_float (rtl, array);
12501 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12502 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12503 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12504 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12506 else
12508 mem_loc_result->dw_loc_oprnd2.val_class
12509 = dw_val_class_const_double;
12510 mem_loc_result->dw_loc_oprnd2.v.val_double
12511 = rtx_to_double_int (rtl);
12514 break;
12516 case EQ:
12517 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
12518 break;
12520 case GE:
12521 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12522 break;
12524 case GT:
12525 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12526 break;
12528 case LE:
12529 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12530 break;
12532 case LT:
12533 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12534 break;
12536 case NE:
12537 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
12538 break;
12540 case GEU:
12541 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12542 break;
12544 case GTU:
12545 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12546 break;
12548 case LEU:
12549 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12550 break;
12552 case LTU:
12553 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12554 break;
12556 case UMIN:
12557 case UMAX:
12558 if (GET_MODE_CLASS (mode) != MODE_INT)
12559 break;
12560 /* FALLTHRU */
12561 case SMIN:
12562 case SMAX:
12563 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
12564 break;
12566 case ZERO_EXTRACT:
12567 case SIGN_EXTRACT:
12568 if (CONST_INT_P (XEXP (rtl, 1))
12569 && CONST_INT_P (XEXP (rtl, 2))
12570 && ((unsigned) INTVAL (XEXP (rtl, 1))
12571 + (unsigned) INTVAL (XEXP (rtl, 2))
12572 <= GET_MODE_BITSIZE (mode))
12573 && GET_MODE_CLASS (mode) == MODE_INT
12574 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12575 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
12577 int shift, size;
12578 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12579 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12580 if (op0 == 0)
12581 break;
12582 if (GET_CODE (rtl) == SIGN_EXTRACT)
12583 op = DW_OP_shra;
12584 else
12585 op = DW_OP_shr;
12586 mem_loc_result = op0;
12587 size = INTVAL (XEXP (rtl, 1));
12588 shift = INTVAL (XEXP (rtl, 2));
12589 if (BITS_BIG_ENDIAN)
12590 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12591 - shift - size;
12592 if (shift + size != (int) DWARF2_ADDR_SIZE)
12594 add_loc_descr (&mem_loc_result,
12595 int_loc_descriptor (DWARF2_ADDR_SIZE
12596 - shift - size));
12597 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12599 if (size != (int) DWARF2_ADDR_SIZE)
12601 add_loc_descr (&mem_loc_result,
12602 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
12603 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12606 break;
12608 case IF_THEN_ELSE:
12610 dw_loc_descr_ref op2, bra_node, drop_node;
12611 op0 = mem_loc_descriptor (XEXP (rtl, 0),
12612 GET_MODE (XEXP (rtl, 0)) == VOIDmode
12613 ? word_mode : GET_MODE (XEXP (rtl, 0)),
12614 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12615 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12616 VAR_INIT_STATUS_INITIALIZED);
12617 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
12618 VAR_INIT_STATUS_INITIALIZED);
12619 if (op0 == NULL || op1 == NULL || op2 == NULL)
12620 break;
12622 mem_loc_result = op1;
12623 add_loc_descr (&mem_loc_result, op2);
12624 add_loc_descr (&mem_loc_result, op0);
12625 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12626 add_loc_descr (&mem_loc_result, bra_node);
12627 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
12628 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12629 add_loc_descr (&mem_loc_result, drop_node);
12630 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12631 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12633 break;
12635 case FLOAT_EXTEND:
12636 case FLOAT_TRUNCATE:
12637 case FLOAT:
12638 case UNSIGNED_FLOAT:
12639 case FIX:
12640 case UNSIGNED_FIX:
12641 if (!dwarf_strict)
12643 dw_die_ref type_die;
12644 dw_loc_descr_ref cvt;
12646 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12647 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12648 if (op0 == NULL)
12649 break;
12650 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
12651 && (GET_CODE (rtl) == FLOAT
12652 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
12653 <= DWARF2_ADDR_SIZE))
12655 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12656 GET_CODE (rtl) == UNSIGNED_FLOAT);
12657 if (type_die == NULL)
12658 break;
12659 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12660 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12661 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12662 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12663 add_loc_descr (&op0, cvt);
12665 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
12666 if (type_die == NULL)
12667 break;
12668 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12669 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12670 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12671 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12672 add_loc_descr (&op0, cvt);
12673 if (GET_MODE_CLASS (mode) == MODE_INT
12674 && (GET_CODE (rtl) == FIX
12675 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
12677 op0 = convert_descriptor_to_mode (mode, op0);
12678 if (op0 == NULL)
12679 break;
12681 mem_loc_result = op0;
12683 break;
12685 case CLZ:
12686 case CTZ:
12687 case FFS:
12688 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
12689 break;
12691 case POPCOUNT:
12692 case PARITY:
12693 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
12694 break;
12696 case BSWAP:
12697 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
12698 break;
12700 case ROTATE:
12701 case ROTATERT:
12702 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
12703 break;
12705 case COMPARE:
12706 /* In theory, we could implement the above. */
12707 /* DWARF cannot represent the unsigned compare operations
12708 natively. */
12709 case SS_MULT:
12710 case US_MULT:
12711 case SS_DIV:
12712 case US_DIV:
12713 case SS_PLUS:
12714 case US_PLUS:
12715 case SS_MINUS:
12716 case US_MINUS:
12717 case SS_NEG:
12718 case US_NEG:
12719 case SS_ABS:
12720 case SS_ASHIFT:
12721 case US_ASHIFT:
12722 case SS_TRUNCATE:
12723 case US_TRUNCATE:
12724 case UNORDERED:
12725 case ORDERED:
12726 case UNEQ:
12727 case UNGE:
12728 case UNGT:
12729 case UNLE:
12730 case UNLT:
12731 case LTGT:
12732 case FRACT_CONVERT:
12733 case UNSIGNED_FRACT_CONVERT:
12734 case SAT_FRACT:
12735 case UNSIGNED_SAT_FRACT:
12736 case SQRT:
12737 case ASM_OPERANDS:
12738 case VEC_MERGE:
12739 case VEC_SELECT:
12740 case VEC_CONCAT:
12741 case VEC_DUPLICATE:
12742 case UNSPEC:
12743 case HIGH:
12744 case FMA:
12745 case STRICT_LOW_PART:
12746 case CONST_VECTOR:
12747 case CONST_FIXED:
12748 case CLRSB:
12749 case CLOBBER:
12750 /* If delegitimize_address couldn't do anything with the UNSPEC, we
12751 can't express it in the debug info. This can happen e.g. with some
12752 TLS UNSPECs. */
12753 break;
12755 case CONST_STRING:
12756 resolve_one_addr (&rtl, NULL);
12757 goto symref;
12759 default:
12760 #ifdef ENABLE_CHECKING
12761 print_rtl (stderr, rtl);
12762 gcc_unreachable ();
12763 #else
12764 break;
12765 #endif
12768 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12769 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12771 return mem_loc_result;
12774 /* Return a descriptor that describes the concatenation of two locations.
12775 This is typically a complex variable. */
12777 static dw_loc_descr_ref
12778 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
12780 dw_loc_descr_ref cc_loc_result = NULL;
12781 dw_loc_descr_ref x0_ref
12782 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12783 dw_loc_descr_ref x1_ref
12784 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12786 if (x0_ref == 0 || x1_ref == 0)
12787 return 0;
12789 cc_loc_result = x0_ref;
12790 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
12792 add_loc_descr (&cc_loc_result, x1_ref);
12793 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
12795 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
12796 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12798 return cc_loc_result;
12801 /* Return a descriptor that describes the concatenation of N
12802 locations. */
12804 static dw_loc_descr_ref
12805 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
12807 unsigned int i;
12808 dw_loc_descr_ref cc_loc_result = NULL;
12809 unsigned int n = XVECLEN (concatn, 0);
12811 for (i = 0; i < n; ++i)
12813 dw_loc_descr_ref ref;
12814 rtx x = XVECEXP (concatn, 0, i);
12816 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12817 if (ref == NULL)
12818 return NULL;
12820 add_loc_descr (&cc_loc_result, ref);
12821 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
12824 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12825 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12827 return cc_loc_result;
12830 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
12831 for DEBUG_IMPLICIT_PTR RTL. */
12833 static dw_loc_descr_ref
12834 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
12836 dw_loc_descr_ref ret;
12837 dw_die_ref ref;
12839 if (dwarf_strict)
12840 return NULL;
12841 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
12842 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
12843 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
12844 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
12845 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
12846 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
12847 if (ref)
12849 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12850 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12851 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12853 else
12855 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12856 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
12858 return ret;
12861 /* Output a proper Dwarf location descriptor for a variable or parameter
12862 which is either allocated in a register or in a memory location. For a
12863 register, we just generate an OP_REG and the register number. For a
12864 memory location we provide a Dwarf postfix expression describing how to
12865 generate the (dynamic) address of the object onto the address stack.
12867 MODE is mode of the decl if this loc_descriptor is going to be used in
12868 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
12869 allowed, VOIDmode otherwise.
12871 If we don't know how to describe it, return 0. */
12873 static dw_loc_descr_ref
12874 loc_descriptor (rtx rtl, enum machine_mode mode,
12875 enum var_init_status initialized)
12877 dw_loc_descr_ref loc_result = NULL;
12879 switch (GET_CODE (rtl))
12881 case SUBREG:
12882 /* The case of a subreg may arise when we have a local (register)
12883 variable or a formal (register) parameter which doesn't quite fill
12884 up an entire register. For now, just assume that it is
12885 legitimate to make the Dwarf info refer to the whole register which
12886 contains the given subreg. */
12887 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
12888 loc_result = loc_descriptor (SUBREG_REG (rtl),
12889 GET_MODE (SUBREG_REG (rtl)), initialized);
12890 else
12891 goto do_default;
12892 break;
12894 case REG:
12895 loc_result = reg_loc_descriptor (rtl, initialized);
12896 break;
12898 case MEM:
12899 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
12900 GET_MODE (rtl), initialized);
12901 if (loc_result == NULL)
12902 loc_result = tls_mem_loc_descriptor (rtl);
12903 if (loc_result == NULL)
12905 rtx new_rtl = avoid_constant_pool_reference (rtl);
12906 if (new_rtl != rtl)
12907 loc_result = loc_descriptor (new_rtl, mode, initialized);
12909 break;
12911 case CONCAT:
12912 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
12913 initialized);
12914 break;
12916 case CONCATN:
12917 loc_result = concatn_loc_descriptor (rtl, initialized);
12918 break;
12920 case VAR_LOCATION:
12921 /* Single part. */
12922 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
12924 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
12925 if (GET_CODE (loc) == EXPR_LIST)
12926 loc = XEXP (loc, 0);
12927 loc_result = loc_descriptor (loc, mode, initialized);
12928 break;
12931 rtl = XEXP (rtl, 1);
12932 /* FALLTHRU */
12934 case PARALLEL:
12936 rtvec par_elems = XVEC (rtl, 0);
12937 int num_elem = GET_NUM_ELEM (par_elems);
12938 enum machine_mode mode;
12939 int i;
12941 /* Create the first one, so we have something to add to. */
12942 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
12943 VOIDmode, initialized);
12944 if (loc_result == NULL)
12945 return NULL;
12946 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
12947 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
12948 for (i = 1; i < num_elem; i++)
12950 dw_loc_descr_ref temp;
12952 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
12953 VOIDmode, initialized);
12954 if (temp == NULL)
12955 return NULL;
12956 add_loc_descr (&loc_result, temp);
12957 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
12958 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
12961 break;
12963 case CONST_INT:
12964 if (mode != VOIDmode && mode != BLKmode)
12965 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
12966 INTVAL (rtl));
12967 break;
12969 case CONST_DOUBLE:
12970 if (mode == VOIDmode)
12971 mode = GET_MODE (rtl);
12973 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
12975 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
12977 /* Note that a CONST_DOUBLE rtx could represent either an integer
12978 or a floating-point constant. A CONST_DOUBLE is used whenever
12979 the constant requires more than one word in order to be
12980 adequately represented. We output CONST_DOUBLEs as blocks. */
12981 loc_result = new_loc_descr (DW_OP_implicit_value,
12982 GET_MODE_SIZE (mode), 0);
12983 if (SCALAR_FLOAT_MODE_P (mode))
12985 unsigned int length = GET_MODE_SIZE (mode);
12986 unsigned char *array
12987 = (unsigned char*) ggc_alloc_atomic (length);
12989 insert_float (rtl, array);
12990 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12991 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12992 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12993 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12995 else
12997 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
12998 loc_result->dw_loc_oprnd2.v.val_double
12999 = rtx_to_double_int (rtl);
13002 break;
13004 case CONST_VECTOR:
13005 if (mode == VOIDmode)
13006 mode = GET_MODE (rtl);
13008 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13010 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13011 unsigned int length = CONST_VECTOR_NUNITS (rtl);
13012 unsigned char *array = (unsigned char *)
13013 ggc_alloc_atomic (length * elt_size);
13014 unsigned int i;
13015 unsigned char *p;
13017 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13018 switch (GET_MODE_CLASS (mode))
13020 case MODE_VECTOR_INT:
13021 for (i = 0, p = array; i < length; i++, p += elt_size)
13023 rtx elt = CONST_VECTOR_ELT (rtl, i);
13024 double_int val = rtx_to_double_int (elt);
13026 if (elt_size <= sizeof (HOST_WIDE_INT))
13027 insert_int (val.to_shwi (), elt_size, p);
13028 else
13030 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
13031 insert_double (val, p);
13034 break;
13036 case MODE_VECTOR_FLOAT:
13037 for (i = 0, p = array; i < length; i++, p += elt_size)
13039 rtx elt = CONST_VECTOR_ELT (rtl, i);
13040 insert_float (elt, p);
13042 break;
13044 default:
13045 gcc_unreachable ();
13048 loc_result = new_loc_descr (DW_OP_implicit_value,
13049 length * elt_size, 0);
13050 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13051 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13052 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13053 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13055 break;
13057 case CONST:
13058 if (mode == VOIDmode
13059 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13060 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13061 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13063 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13064 break;
13066 /* FALLTHROUGH */
13067 case SYMBOL_REF:
13068 if (!const_ok_for_output (rtl))
13069 break;
13070 case LABEL_REF:
13071 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13072 && (dwarf_version >= 4 || !dwarf_strict))
13074 loc_result = new_addr_loc_descr (rtl, dtprel_false);
13075 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13076 vec_safe_push (used_rtx_array, rtl);
13078 break;
13080 case DEBUG_IMPLICIT_PTR:
13081 loc_result = implicit_ptr_descriptor (rtl, 0);
13082 break;
13084 case PLUS:
13085 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13086 && CONST_INT_P (XEXP (rtl, 1)))
13088 loc_result
13089 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13090 break;
13092 /* FALLTHRU */
13093 do_default:
13094 default:
13095 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13096 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13097 && dwarf_version >= 4)
13098 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13100 /* Value expression. */
13101 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13102 if (loc_result)
13103 add_loc_descr (&loc_result,
13104 new_loc_descr (DW_OP_stack_value, 0, 0));
13106 break;
13109 return loc_result;
13112 /* We need to figure out what section we should use as the base for the
13113 address ranges where a given location is valid.
13114 1. If this particular DECL has a section associated with it, use that.
13115 2. If this function has a section associated with it, use that.
13116 3. Otherwise, use the text section.
13117 XXX: If you split a variable across multiple sections, we won't notice. */
13119 static const char *
13120 secname_for_decl (const_tree decl)
13122 const char *secname;
13124 if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
13126 tree sectree = DECL_SECTION_NAME (decl);
13127 secname = TREE_STRING_POINTER (sectree);
13129 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13131 tree sectree = DECL_SECTION_NAME (current_function_decl);
13132 secname = TREE_STRING_POINTER (sectree);
13134 else if (cfun && in_cold_section_p)
13135 secname = crtl->subsections.cold_section_label;
13136 else
13137 secname = text_section_label;
13139 return secname;
13142 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
13144 static bool
13145 decl_by_reference_p (tree decl)
13147 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13148 || TREE_CODE (decl) == VAR_DECL)
13149 && DECL_BY_REFERENCE (decl));
13152 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13153 for VARLOC. */
13155 static dw_loc_descr_ref
13156 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13157 enum var_init_status initialized)
13159 int have_address = 0;
13160 dw_loc_descr_ref descr;
13161 enum machine_mode mode;
13163 if (want_address != 2)
13165 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13166 /* Single part. */
13167 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13169 varloc = PAT_VAR_LOCATION_LOC (varloc);
13170 if (GET_CODE (varloc) == EXPR_LIST)
13171 varloc = XEXP (varloc, 0);
13172 mode = GET_MODE (varloc);
13173 if (MEM_P (varloc))
13175 rtx addr = XEXP (varloc, 0);
13176 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13177 mode, initialized);
13178 if (descr)
13179 have_address = 1;
13180 else
13182 rtx x = avoid_constant_pool_reference (varloc);
13183 if (x != varloc)
13184 descr = mem_loc_descriptor (x, mode, VOIDmode,
13185 initialized);
13188 else
13189 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13191 else
13192 return 0;
13194 else
13196 if (GET_CODE (varloc) == VAR_LOCATION)
13197 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13198 else
13199 mode = DECL_MODE (loc);
13200 descr = loc_descriptor (varloc, mode, initialized);
13201 have_address = 1;
13204 if (!descr)
13205 return 0;
13207 if (want_address == 2 && !have_address
13208 && (dwarf_version >= 4 || !dwarf_strict))
13210 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13212 expansion_failed (loc, NULL_RTX,
13213 "DWARF address size mismatch");
13214 return 0;
13216 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13217 have_address = 1;
13219 /* Show if we can't fill the request for an address. */
13220 if (want_address && !have_address)
13222 expansion_failed (loc, NULL_RTX,
13223 "Want address and only have value");
13224 return 0;
13227 /* If we've got an address and don't want one, dereference. */
13228 if (!want_address && have_address)
13230 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13231 enum dwarf_location_atom op;
13233 if (size > DWARF2_ADDR_SIZE || size == -1)
13235 expansion_failed (loc, NULL_RTX,
13236 "DWARF address size mismatch");
13237 return 0;
13239 else if (size == DWARF2_ADDR_SIZE)
13240 op = DW_OP_deref;
13241 else
13242 op = DW_OP_deref_size;
13244 add_loc_descr (&descr, new_loc_descr (op, size, 0));
13247 return descr;
13250 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
13251 if it is not possible. */
13253 static dw_loc_descr_ref
13254 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
13256 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
13257 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
13258 else if (dwarf_version >= 3 || !dwarf_strict)
13259 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
13260 else
13261 return NULL;
13264 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13265 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
13267 static dw_loc_descr_ref
13268 dw_sra_loc_expr (tree decl, rtx loc)
13270 rtx p;
13271 unsigned int padsize = 0;
13272 dw_loc_descr_ref descr, *descr_tail;
13273 unsigned HOST_WIDE_INT decl_size;
13274 rtx varloc;
13275 enum var_init_status initialized;
13277 if (DECL_SIZE (decl) == NULL
13278 || !host_integerp (DECL_SIZE (decl), 1))
13279 return NULL;
13281 decl_size = tree_low_cst (DECL_SIZE (decl), 1);
13282 descr = NULL;
13283 descr_tail = &descr;
13285 for (p = loc; p; p = XEXP (p, 1))
13287 unsigned int bitsize = decl_piece_bitsize (p);
13288 rtx loc_note = *decl_piece_varloc_ptr (p);
13289 dw_loc_descr_ref cur_descr;
13290 dw_loc_descr_ref *tail, last = NULL;
13291 unsigned int opsize = 0;
13293 if (loc_note == NULL_RTX
13294 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
13296 padsize += bitsize;
13297 continue;
13299 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
13300 varloc = NOTE_VAR_LOCATION (loc_note);
13301 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
13302 if (cur_descr == NULL)
13304 padsize += bitsize;
13305 continue;
13308 /* Check that cur_descr either doesn't use
13309 DW_OP_*piece operations, or their sum is equal
13310 to bitsize. Otherwise we can't embed it. */
13311 for (tail = &cur_descr; *tail != NULL;
13312 tail = &(*tail)->dw_loc_next)
13313 if ((*tail)->dw_loc_opc == DW_OP_piece)
13315 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
13316 * BITS_PER_UNIT;
13317 last = *tail;
13319 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
13321 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
13322 last = *tail;
13325 if (last != NULL && opsize != bitsize)
13327 padsize += bitsize;
13328 continue;
13331 /* If there is a hole, add DW_OP_*piece after empty DWARF
13332 expression, which means that those bits are optimized out. */
13333 if (padsize)
13335 if (padsize > decl_size)
13336 return NULL;
13337 decl_size -= padsize;
13338 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
13339 if (*descr_tail == NULL)
13340 return NULL;
13341 descr_tail = &(*descr_tail)->dw_loc_next;
13342 padsize = 0;
13344 *descr_tail = cur_descr;
13345 descr_tail = tail;
13346 if (bitsize > decl_size)
13347 return NULL;
13348 decl_size -= bitsize;
13349 if (last == NULL)
13351 HOST_WIDE_INT offset = 0;
13352 if (GET_CODE (varloc) == VAR_LOCATION
13353 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13355 varloc = PAT_VAR_LOCATION_LOC (varloc);
13356 if (GET_CODE (varloc) == EXPR_LIST)
13357 varloc = XEXP (varloc, 0);
13361 if (GET_CODE (varloc) == CONST
13362 || GET_CODE (varloc) == SIGN_EXTEND
13363 || GET_CODE (varloc) == ZERO_EXTEND)
13364 varloc = XEXP (varloc, 0);
13365 else if (GET_CODE (varloc) == SUBREG)
13366 varloc = SUBREG_REG (varloc);
13367 else
13368 break;
13370 while (1);
13371 /* DW_OP_bit_size offset should be zero for register
13372 or implicit location descriptions and empty location
13373 descriptions, but for memory addresses needs big endian
13374 adjustment. */
13375 if (MEM_P (varloc))
13377 unsigned HOST_WIDE_INT memsize
13378 = MEM_SIZE (varloc) * BITS_PER_UNIT;
13379 if (memsize != bitsize)
13381 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
13382 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
13383 return NULL;
13384 if (memsize < bitsize)
13385 return NULL;
13386 if (BITS_BIG_ENDIAN)
13387 offset = memsize - bitsize;
13391 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
13392 if (*descr_tail == NULL)
13393 return NULL;
13394 descr_tail = &(*descr_tail)->dw_loc_next;
13398 /* If there were any non-empty expressions, add padding till the end of
13399 the decl. */
13400 if (descr != NULL && decl_size != 0)
13402 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
13403 if (*descr_tail == NULL)
13404 return NULL;
13406 return descr;
13409 /* Return the dwarf representation of the location list LOC_LIST of
13410 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
13411 function. */
13413 static dw_loc_list_ref
13414 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
13416 const char *endname, *secname;
13417 rtx varloc;
13418 enum var_init_status initialized;
13419 struct var_loc_node *node;
13420 dw_loc_descr_ref descr;
13421 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13422 dw_loc_list_ref list = NULL;
13423 dw_loc_list_ref *listp = &list;
13425 /* Now that we know what section we are using for a base,
13426 actually construct the list of locations.
13427 The first location information is what is passed to the
13428 function that creates the location list, and the remaining
13429 locations just get added on to that list.
13430 Note that we only know the start address for a location
13431 (IE location changes), so to build the range, we use
13432 the range [current location start, next location start].
13433 This means we have to special case the last node, and generate
13434 a range of [last location start, end of function label]. */
13436 secname = secname_for_decl (decl);
13438 for (node = loc_list->first; node; node = node->next)
13439 if (GET_CODE (node->loc) == EXPR_LIST
13440 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
13442 if (GET_CODE (node->loc) == EXPR_LIST)
13444 /* This requires DW_OP_{,bit_}piece, which is not usable
13445 inside DWARF expressions. */
13446 if (want_address != 2)
13447 continue;
13448 descr = dw_sra_loc_expr (decl, node->loc);
13449 if (descr == NULL)
13450 continue;
13452 else
13454 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13455 varloc = NOTE_VAR_LOCATION (node->loc);
13456 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
13458 if (descr)
13460 bool range_across_switch = false;
13461 /* If section switch happens in between node->label
13462 and node->next->label (or end of function) and
13463 we can't emit it as a single entry list,
13464 emit two ranges, first one ending at the end
13465 of first partition and second one starting at the
13466 beginning of second partition. */
13467 if (node == loc_list->last_before_switch
13468 && (node != loc_list->first || loc_list->first->next)
13469 && current_function_decl)
13471 endname = cfun->fde->dw_fde_end;
13472 range_across_switch = true;
13474 /* The variable has a location between NODE->LABEL and
13475 NODE->NEXT->LABEL. */
13476 else if (node->next)
13477 endname = node->next->label;
13478 /* If the variable has a location at the last label
13479 it keeps its location until the end of function. */
13480 else if (!current_function_decl)
13481 endname = text_end_label;
13482 else
13484 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13485 current_function_funcdef_no);
13486 endname = ggc_strdup (label_id);
13489 *listp = new_loc_list (descr, node->label, endname, secname);
13490 if (TREE_CODE (decl) == PARM_DECL
13491 && node == loc_list->first
13492 && GET_CODE (node->loc) == NOTE
13493 && strcmp (node->label, endname) == 0)
13494 (*listp)->force = true;
13495 listp = &(*listp)->dw_loc_next;
13497 if (range_across_switch)
13499 if (GET_CODE (node->loc) == EXPR_LIST)
13500 descr = dw_sra_loc_expr (decl, node->loc);
13501 else
13503 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13504 varloc = NOTE_VAR_LOCATION (node->loc);
13505 descr = dw_loc_list_1 (decl, varloc, want_address,
13506 initialized);
13508 gcc_assert (descr);
13509 /* The variable has a location between NODE->LABEL and
13510 NODE->NEXT->LABEL. */
13511 if (node->next)
13512 endname = node->next->label;
13513 else
13514 endname = cfun->fde->dw_fde_second_end;
13515 *listp = new_loc_list (descr,
13516 cfun->fde->dw_fde_second_begin,
13517 endname, secname);
13518 listp = &(*listp)->dw_loc_next;
13523 /* Try to avoid the overhead of a location list emitting a location
13524 expression instead, but only if we didn't have more than one
13525 location entry in the first place. If some entries were not
13526 representable, we don't want to pretend a single entry that was
13527 applies to the entire scope in which the variable is
13528 available. */
13529 if (list && loc_list->first->next)
13530 gen_llsym (list);
13532 return list;
13535 /* Return if the loc_list has only single element and thus can be represented
13536 as location description. */
13538 static bool
13539 single_element_loc_list_p (dw_loc_list_ref list)
13541 gcc_assert (!list->dw_loc_next || list->ll_symbol);
13542 return !list->ll_symbol;
13545 /* To each location in list LIST add loc descr REF. */
13547 static void
13548 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
13550 dw_loc_descr_ref copy;
13551 add_loc_descr (&list->expr, ref);
13552 list = list->dw_loc_next;
13553 while (list)
13555 copy = ggc_alloc_dw_loc_descr_node ();
13556 memcpy (copy, ref, sizeof (dw_loc_descr_node));
13557 add_loc_descr (&list->expr, copy);
13558 while (copy->dw_loc_next)
13560 dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
13561 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
13562 copy->dw_loc_next = new_copy;
13563 copy = new_copy;
13565 list = list->dw_loc_next;
13569 /* Given two lists RET and LIST
13570 produce location list that is result of adding expression in LIST
13571 to expression in RET on each position in program.
13572 Might be destructive on both RET and LIST.
13574 TODO: We handle only simple cases of RET or LIST having at most one
13575 element. General case would inolve sorting the lists in program order
13576 and merging them that will need some additional work.
13577 Adding that will improve quality of debug info especially for SRA-ed
13578 structures. */
13580 static void
13581 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
13583 if (!list)
13584 return;
13585 if (!*ret)
13587 *ret = list;
13588 return;
13590 if (!list->dw_loc_next)
13592 add_loc_descr_to_each (*ret, list->expr);
13593 return;
13595 if (!(*ret)->dw_loc_next)
13597 add_loc_descr_to_each (list, (*ret)->expr);
13598 *ret = list;
13599 return;
13601 expansion_failed (NULL_TREE, NULL_RTX,
13602 "Don't know how to merge two non-trivial"
13603 " location lists.\n");
13604 *ret = NULL;
13605 return;
13608 /* LOC is constant expression. Try a luck, look it up in constant
13609 pool and return its loc_descr of its address. */
13611 static dw_loc_descr_ref
13612 cst_pool_loc_descr (tree loc)
13614 /* Get an RTL for this, if something has been emitted. */
13615 rtx rtl = lookup_constant_def (loc);
13617 if (!rtl || !MEM_P (rtl))
13619 gcc_assert (!rtl);
13620 return 0;
13622 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
13624 /* TODO: We might get more coverage if we was actually delaying expansion
13625 of all expressions till end of compilation when constant pools are fully
13626 populated. */
13627 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
13629 expansion_failed (loc, NULL_RTX,
13630 "CST value in contant pool but not marked.");
13631 return 0;
13633 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13634 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
13637 /* Return dw_loc_list representing address of addr_expr LOC
13638 by looking for inner INDIRECT_REF expression and turning
13639 it into simple arithmetics. */
13641 static dw_loc_list_ref
13642 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
13644 tree obj, offset;
13645 HOST_WIDE_INT bitsize, bitpos, bytepos;
13646 enum machine_mode mode;
13647 int unsignedp, volatilep = 0;
13648 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13650 obj = get_inner_reference (TREE_OPERAND (loc, 0),
13651 &bitsize, &bitpos, &offset, &mode,
13652 &unsignedp, &volatilep, false);
13653 STRIP_NOPS (obj);
13654 if (bitpos % BITS_PER_UNIT)
13656 expansion_failed (loc, NULL_RTX, "bitfield access");
13657 return 0;
13659 if (!INDIRECT_REF_P (obj))
13661 expansion_failed (obj,
13662 NULL_RTX, "no indirect ref in inner refrence");
13663 return 0;
13665 if (!offset && !bitpos)
13666 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
13667 else if (toplev
13668 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
13669 && (dwarf_version >= 4 || !dwarf_strict))
13671 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
13672 if (!list_ret)
13673 return 0;
13674 if (offset)
13676 /* Variable offset. */
13677 list_ret1 = loc_list_from_tree (offset, 0);
13678 if (list_ret1 == 0)
13679 return 0;
13680 add_loc_list (&list_ret, list_ret1);
13681 if (!list_ret)
13682 return 0;
13683 add_loc_descr_to_each (list_ret,
13684 new_loc_descr (DW_OP_plus, 0, 0));
13686 bytepos = bitpos / BITS_PER_UNIT;
13687 if (bytepos > 0)
13688 add_loc_descr_to_each (list_ret,
13689 new_loc_descr (DW_OP_plus_uconst,
13690 bytepos, 0));
13691 else if (bytepos < 0)
13692 loc_list_plus_const (list_ret, bytepos);
13693 add_loc_descr_to_each (list_ret,
13694 new_loc_descr (DW_OP_stack_value, 0, 0));
13696 return list_ret;
13700 /* Generate Dwarf location list representing LOC.
13701 If WANT_ADDRESS is false, expression computing LOC will be computed
13702 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
13703 if WANT_ADDRESS is 2, expression computing address useable in location
13704 will be returned (i.e. DW_OP_reg can be used
13705 to refer to register values). */
13707 static dw_loc_list_ref
13708 loc_list_from_tree (tree loc, int want_address)
13710 dw_loc_descr_ref ret = NULL, ret1 = NULL;
13711 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13712 int have_address = 0;
13713 enum dwarf_location_atom op;
13715 /* ??? Most of the time we do not take proper care for sign/zero
13716 extending the values properly. Hopefully this won't be a real
13717 problem... */
13719 switch (TREE_CODE (loc))
13721 case ERROR_MARK:
13722 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
13723 return 0;
13725 case PLACEHOLDER_EXPR:
13726 /* This case involves extracting fields from an object to determine the
13727 position of other fields. We don't try to encode this here. The
13728 only user of this is Ada, which encodes the needed information using
13729 the names of types. */
13730 expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
13731 return 0;
13733 case CALL_EXPR:
13734 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
13735 /* There are no opcodes for these operations. */
13736 return 0;
13738 case PREINCREMENT_EXPR:
13739 case PREDECREMENT_EXPR:
13740 case POSTINCREMENT_EXPR:
13741 case POSTDECREMENT_EXPR:
13742 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
13743 /* There are no opcodes for these operations. */
13744 return 0;
13746 case ADDR_EXPR:
13747 /* If we already want an address, see if there is INDIRECT_REF inside
13748 e.g. for &this->field. */
13749 if (want_address)
13751 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
13752 (loc, want_address == 2);
13753 if (list_ret)
13754 have_address = 1;
13755 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
13756 && (ret = cst_pool_loc_descr (loc)))
13757 have_address = 1;
13759 /* Otherwise, process the argument and look for the address. */
13760 if (!list_ret && !ret)
13761 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
13762 else
13764 if (want_address)
13765 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
13766 return NULL;
13768 break;
13770 case VAR_DECL:
13771 if (DECL_THREAD_LOCAL_P (loc))
13773 rtx rtl;
13774 enum dwarf_location_atom tls_op;
13775 enum dtprel_bool dtprel = dtprel_false;
13777 if (targetm.have_tls)
13779 /* If this is not defined, we have no way to emit the
13780 data. */
13781 if (!targetm.asm_out.output_dwarf_dtprel)
13782 return 0;
13784 /* The way DW_OP_GNU_push_tls_address is specified, we
13785 can only look up addresses of objects in the current
13786 module. We used DW_OP_addr as first op, but that's
13787 wrong, because DW_OP_addr is relocated by the debug
13788 info consumer, while DW_OP_GNU_push_tls_address
13789 operand shouldn't be. */
13790 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
13791 return 0;
13792 dtprel = dtprel_true;
13793 tls_op = DW_OP_GNU_push_tls_address;
13795 else
13797 if (!targetm.emutls.debug_form_tls_address
13798 || !(dwarf_version >= 3 || !dwarf_strict))
13799 return 0;
13800 /* We stuffed the control variable into the DECL_VALUE_EXPR
13801 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
13802 no longer appear in gimple code. We used the control
13803 variable in specific so that we could pick it up here. */
13804 loc = DECL_VALUE_EXPR (loc);
13805 tls_op = DW_OP_form_tls_address;
13808 rtl = rtl_for_decl_location (loc);
13809 if (rtl == NULL_RTX)
13810 return 0;
13812 if (!MEM_P (rtl))
13813 return 0;
13814 rtl = XEXP (rtl, 0);
13815 if (! CONSTANT_P (rtl))
13816 return 0;
13818 ret = new_addr_loc_descr (rtl, dtprel);
13819 ret1 = new_loc_descr (tls_op, 0, 0);
13820 add_loc_descr (&ret, ret1);
13822 have_address = 1;
13823 break;
13825 /* FALLTHRU */
13827 case PARM_DECL:
13828 case RESULT_DECL:
13829 if (DECL_HAS_VALUE_EXPR_P (loc))
13830 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
13831 want_address);
13832 /* FALLTHRU */
13834 case FUNCTION_DECL:
13836 rtx rtl;
13837 var_loc_list *loc_list = lookup_decl_loc (loc);
13839 if (loc_list && loc_list->first)
13841 list_ret = dw_loc_list (loc_list, loc, want_address);
13842 have_address = want_address != 0;
13843 break;
13845 rtl = rtl_for_decl_location (loc);
13846 if (rtl == NULL_RTX)
13848 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
13849 return 0;
13851 else if (CONST_INT_P (rtl))
13853 HOST_WIDE_INT val = INTVAL (rtl);
13854 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13855 val &= GET_MODE_MASK (DECL_MODE (loc));
13856 ret = int_loc_descriptor (val);
13858 else if (GET_CODE (rtl) == CONST_STRING)
13860 expansion_failed (loc, NULL_RTX, "CONST_STRING");
13861 return 0;
13863 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
13864 ret = new_addr_loc_descr (rtl, dtprel_false);
13865 else
13867 enum machine_mode mode, mem_mode;
13869 /* Certain constructs can only be represented at top-level. */
13870 if (want_address == 2)
13872 ret = loc_descriptor (rtl, VOIDmode,
13873 VAR_INIT_STATUS_INITIALIZED);
13874 have_address = 1;
13876 else
13878 mode = GET_MODE (rtl);
13879 mem_mode = VOIDmode;
13880 if (MEM_P (rtl))
13882 mem_mode = mode;
13883 mode = get_address_mode (rtl);
13884 rtl = XEXP (rtl, 0);
13885 have_address = 1;
13887 ret = mem_loc_descriptor (rtl, mode, mem_mode,
13888 VAR_INIT_STATUS_INITIALIZED);
13890 if (!ret)
13891 expansion_failed (loc, rtl,
13892 "failed to produce loc descriptor for rtl");
13895 break;
13897 case MEM_REF:
13898 /* ??? FIXME. */
13899 if (!integer_zerop (TREE_OPERAND (loc, 1)))
13900 return 0;
13901 /* Fallthru. */
13902 case INDIRECT_REF:
13903 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13904 have_address = 1;
13905 break;
13907 case COMPOUND_EXPR:
13908 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
13910 CASE_CONVERT:
13911 case VIEW_CONVERT_EXPR:
13912 case SAVE_EXPR:
13913 case MODIFY_EXPR:
13914 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
13916 case COMPONENT_REF:
13917 case BIT_FIELD_REF:
13918 case ARRAY_REF:
13919 case ARRAY_RANGE_REF:
13920 case REALPART_EXPR:
13921 case IMAGPART_EXPR:
13923 tree obj, offset;
13924 HOST_WIDE_INT bitsize, bitpos, bytepos;
13925 enum machine_mode mode;
13926 int unsignedp, volatilep = 0;
13928 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
13929 &unsignedp, &volatilep, false);
13931 gcc_assert (obj != loc);
13933 list_ret = loc_list_from_tree (obj,
13934 want_address == 2
13935 && !bitpos && !offset ? 2 : 1);
13936 /* TODO: We can extract value of the small expression via shifting even
13937 for nonzero bitpos. */
13938 if (list_ret == 0)
13939 return 0;
13940 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
13942 expansion_failed (loc, NULL_RTX,
13943 "bitfield access");
13944 return 0;
13947 if (offset != NULL_TREE)
13949 /* Variable offset. */
13950 list_ret1 = loc_list_from_tree (offset, 0);
13951 if (list_ret1 == 0)
13952 return 0;
13953 add_loc_list (&list_ret, list_ret1);
13954 if (!list_ret)
13955 return 0;
13956 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
13959 bytepos = bitpos / BITS_PER_UNIT;
13960 if (bytepos > 0)
13961 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
13962 else if (bytepos < 0)
13963 loc_list_plus_const (list_ret, bytepos);
13965 have_address = 1;
13966 break;
13969 case INTEGER_CST:
13970 if ((want_address || !host_integerp (loc, 0))
13971 && (ret = cst_pool_loc_descr (loc)))
13972 have_address = 1;
13973 else if (want_address == 2
13974 && host_integerp (loc, 0)
13975 && (ret = address_of_int_loc_descriptor
13976 (int_size_in_bytes (TREE_TYPE (loc)),
13977 tree_low_cst (loc, 0))))
13978 have_address = 1;
13979 else if (host_integerp (loc, 0))
13980 ret = int_loc_descriptor (tree_low_cst (loc, 0));
13981 else
13983 expansion_failed (loc, NULL_RTX,
13984 "Integer operand is not host integer");
13985 return 0;
13987 break;
13989 case CONSTRUCTOR:
13990 case REAL_CST:
13991 case STRING_CST:
13992 case COMPLEX_CST:
13993 if ((ret = cst_pool_loc_descr (loc)))
13994 have_address = 1;
13995 else
13996 /* We can construct small constants here using int_loc_descriptor. */
13997 expansion_failed (loc, NULL_RTX,
13998 "constructor or constant not in constant pool");
13999 break;
14001 case TRUTH_AND_EXPR:
14002 case TRUTH_ANDIF_EXPR:
14003 case BIT_AND_EXPR:
14004 op = DW_OP_and;
14005 goto do_binop;
14007 case TRUTH_XOR_EXPR:
14008 case BIT_XOR_EXPR:
14009 op = DW_OP_xor;
14010 goto do_binop;
14012 case TRUTH_OR_EXPR:
14013 case TRUTH_ORIF_EXPR:
14014 case BIT_IOR_EXPR:
14015 op = DW_OP_or;
14016 goto do_binop;
14018 case FLOOR_DIV_EXPR:
14019 case CEIL_DIV_EXPR:
14020 case ROUND_DIV_EXPR:
14021 case TRUNC_DIV_EXPR:
14022 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14023 return 0;
14024 op = DW_OP_div;
14025 goto do_binop;
14027 case MINUS_EXPR:
14028 op = DW_OP_minus;
14029 goto do_binop;
14031 case FLOOR_MOD_EXPR:
14032 case CEIL_MOD_EXPR:
14033 case ROUND_MOD_EXPR:
14034 case TRUNC_MOD_EXPR:
14035 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14037 op = DW_OP_mod;
14038 goto do_binop;
14040 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14041 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14042 if (list_ret == 0 || list_ret1 == 0)
14043 return 0;
14045 add_loc_list (&list_ret, list_ret1);
14046 if (list_ret == 0)
14047 return 0;
14048 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14049 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14050 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14051 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14052 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14053 break;
14055 case MULT_EXPR:
14056 op = DW_OP_mul;
14057 goto do_binop;
14059 case LSHIFT_EXPR:
14060 op = DW_OP_shl;
14061 goto do_binop;
14063 case RSHIFT_EXPR:
14064 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14065 goto do_binop;
14067 case POINTER_PLUS_EXPR:
14068 case PLUS_EXPR:
14069 if (host_integerp (TREE_OPERAND (loc, 1), 0))
14071 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14072 if (list_ret == 0)
14073 return 0;
14075 loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
14076 break;
14079 op = DW_OP_plus;
14080 goto do_binop;
14082 case LE_EXPR:
14083 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14084 return 0;
14086 op = DW_OP_le;
14087 goto do_binop;
14089 case GE_EXPR:
14090 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14091 return 0;
14093 op = DW_OP_ge;
14094 goto do_binop;
14096 case LT_EXPR:
14097 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14098 return 0;
14100 op = DW_OP_lt;
14101 goto do_binop;
14103 case GT_EXPR:
14104 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14105 return 0;
14107 op = DW_OP_gt;
14108 goto do_binop;
14110 case EQ_EXPR:
14111 op = DW_OP_eq;
14112 goto do_binop;
14114 case NE_EXPR:
14115 op = DW_OP_ne;
14116 goto do_binop;
14118 do_binop:
14119 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14120 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14121 if (list_ret == 0 || list_ret1 == 0)
14122 return 0;
14124 add_loc_list (&list_ret, list_ret1);
14125 if (list_ret == 0)
14126 return 0;
14127 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14128 break;
14130 case TRUTH_NOT_EXPR:
14131 case BIT_NOT_EXPR:
14132 op = DW_OP_not;
14133 goto do_unop;
14135 case ABS_EXPR:
14136 op = DW_OP_abs;
14137 goto do_unop;
14139 case NEGATE_EXPR:
14140 op = DW_OP_neg;
14141 goto do_unop;
14143 do_unop:
14144 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14145 if (list_ret == 0)
14146 return 0;
14148 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14149 break;
14151 case MIN_EXPR:
14152 case MAX_EXPR:
14154 const enum tree_code code =
14155 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14157 loc = build3 (COND_EXPR, TREE_TYPE (loc),
14158 build2 (code, integer_type_node,
14159 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14160 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
14163 /* ... fall through ... */
14165 case COND_EXPR:
14167 dw_loc_descr_ref lhs
14168 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
14169 dw_loc_list_ref rhs
14170 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
14171 dw_loc_descr_ref bra_node, jump_node, tmp;
14173 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14174 if (list_ret == 0 || lhs == 0 || rhs == 0)
14175 return 0;
14177 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14178 add_loc_descr_to_each (list_ret, bra_node);
14180 add_loc_list (&list_ret, rhs);
14181 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
14182 add_loc_descr_to_each (list_ret, jump_node);
14184 add_loc_descr_to_each (list_ret, lhs);
14185 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14186 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
14188 /* ??? Need a node to point the skip at. Use a nop. */
14189 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14190 add_loc_descr_to_each (list_ret, tmp);
14191 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14192 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
14194 break;
14196 case FIX_TRUNC_EXPR:
14197 return 0;
14199 default:
14200 /* Leave front-end specific codes as simply unknown. This comes
14201 up, for instance, with the C STMT_EXPR. */
14202 if ((unsigned int) TREE_CODE (loc)
14203 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
14205 expansion_failed (loc, NULL_RTX,
14206 "language specific tree node");
14207 return 0;
14210 #ifdef ENABLE_CHECKING
14211 /* Otherwise this is a generic code; we should just lists all of
14212 these explicitly. We forgot one. */
14213 gcc_unreachable ();
14214 #else
14215 /* In a release build, we want to degrade gracefully: better to
14216 generate incomplete debugging information than to crash. */
14217 return NULL;
14218 #endif
14221 if (!ret && !list_ret)
14222 return 0;
14224 if (want_address == 2 && !have_address
14225 && (dwarf_version >= 4 || !dwarf_strict))
14227 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14229 expansion_failed (loc, NULL_RTX,
14230 "DWARF address size mismatch");
14231 return 0;
14233 if (ret)
14234 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
14235 else
14236 add_loc_descr_to_each (list_ret,
14237 new_loc_descr (DW_OP_stack_value, 0, 0));
14238 have_address = 1;
14240 /* Show if we can't fill the request for an address. */
14241 if (want_address && !have_address)
14243 expansion_failed (loc, NULL_RTX,
14244 "Want address and only have value");
14245 return 0;
14248 gcc_assert (!ret || !list_ret);
14250 /* If we've got an address and don't want one, dereference. */
14251 if (!want_address && have_address)
14253 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14255 if (size > DWARF2_ADDR_SIZE || size == -1)
14257 expansion_failed (loc, NULL_RTX,
14258 "DWARF address size mismatch");
14259 return 0;
14261 else if (size == DWARF2_ADDR_SIZE)
14262 op = DW_OP_deref;
14263 else
14264 op = DW_OP_deref_size;
14266 if (ret)
14267 add_loc_descr (&ret, new_loc_descr (op, size, 0));
14268 else
14269 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
14271 if (ret)
14272 list_ret = new_loc_list (ret, NULL, NULL, NULL);
14274 return list_ret;
14277 /* Same as above but return only single location expression. */
14278 static dw_loc_descr_ref
14279 loc_descriptor_from_tree (tree loc, int want_address)
14281 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
14282 if (!ret)
14283 return NULL;
14284 if (ret->dw_loc_next)
14286 expansion_failed (loc, NULL_RTX,
14287 "Location list where only loc descriptor needed");
14288 return NULL;
14290 return ret->expr;
14293 /* Given a value, round it up to the lowest multiple of `boundary'
14294 which is not less than the value itself. */
14296 static inline HOST_WIDE_INT
14297 ceiling (HOST_WIDE_INT value, unsigned int boundary)
14299 return (((value + boundary - 1) / boundary) * boundary);
14302 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
14303 pointer to the declared type for the relevant field variable, or return
14304 `integer_type_node' if the given node turns out to be an
14305 ERROR_MARK node. */
14307 static inline tree
14308 field_type (const_tree decl)
14310 tree type;
14312 if (TREE_CODE (decl) == ERROR_MARK)
14313 return integer_type_node;
14315 type = DECL_BIT_FIELD_TYPE (decl);
14316 if (type == NULL_TREE)
14317 type = TREE_TYPE (decl);
14319 return type;
14322 /* Given a pointer to a tree node, return the alignment in bits for
14323 it, or else return BITS_PER_WORD if the node actually turns out to
14324 be an ERROR_MARK node. */
14326 static inline unsigned
14327 simple_type_align_in_bits (const_tree type)
14329 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
14332 static inline unsigned
14333 simple_decl_align_in_bits (const_tree decl)
14335 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
14338 /* Return the result of rounding T up to ALIGN. */
14340 static inline double_int
14341 round_up_to_align (double_int t, unsigned int align)
14343 double_int alignd = double_int::from_uhwi (align);
14344 t += alignd;
14345 t += double_int_minus_one;
14346 t = t.div (alignd, true, TRUNC_DIV_EXPR);
14347 t *= alignd;
14348 return t;
14351 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
14352 lowest addressed byte of the "containing object" for the given FIELD_DECL,
14353 or return 0 if we are unable to determine what that offset is, either
14354 because the argument turns out to be a pointer to an ERROR_MARK node, or
14355 because the offset is actually variable. (We can't handle the latter case
14356 just yet). */
14358 static HOST_WIDE_INT
14359 field_byte_offset (const_tree decl)
14361 double_int object_offset_in_bits;
14362 double_int object_offset_in_bytes;
14363 double_int bitpos_int;
14365 if (TREE_CODE (decl) == ERROR_MARK)
14366 return 0;
14368 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
14370 /* We cannot yet cope with fields whose positions are variable, so
14371 for now, when we see such things, we simply return 0. Someday, we may
14372 be able to handle such cases, but it will be damn difficult. */
14373 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
14374 return 0;
14376 bitpos_int = tree_to_double_int (bit_position (decl));
14378 #ifdef PCC_BITFIELD_TYPE_MATTERS
14379 if (PCC_BITFIELD_TYPE_MATTERS)
14381 tree type;
14382 tree field_size_tree;
14383 double_int deepest_bitpos;
14384 double_int field_size_in_bits;
14385 unsigned int type_align_in_bits;
14386 unsigned int decl_align_in_bits;
14387 double_int type_size_in_bits;
14389 type = field_type (decl);
14390 type_size_in_bits = double_int_type_size_in_bits (type);
14391 type_align_in_bits = simple_type_align_in_bits (type);
14393 field_size_tree = DECL_SIZE (decl);
14395 /* The size could be unspecified if there was an error, or for
14396 a flexible array member. */
14397 if (!field_size_tree)
14398 field_size_tree = bitsize_zero_node;
14400 /* If the size of the field is not constant, use the type size. */
14401 if (TREE_CODE (field_size_tree) == INTEGER_CST)
14402 field_size_in_bits = tree_to_double_int (field_size_tree);
14403 else
14404 field_size_in_bits = type_size_in_bits;
14406 decl_align_in_bits = simple_decl_align_in_bits (decl);
14408 /* The GCC front-end doesn't make any attempt to keep track of the
14409 starting bit offset (relative to the start of the containing
14410 structure type) of the hypothetical "containing object" for a
14411 bit-field. Thus, when computing the byte offset value for the
14412 start of the "containing object" of a bit-field, we must deduce
14413 this information on our own. This can be rather tricky to do in
14414 some cases. For example, handling the following structure type
14415 definition when compiling for an i386/i486 target (which only
14416 aligns long long's to 32-bit boundaries) can be very tricky:
14418 struct S { int field1; long long field2:31; };
14420 Fortunately, there is a simple rule-of-thumb which can be used
14421 in such cases. When compiling for an i386/i486, GCC will
14422 allocate 8 bytes for the structure shown above. It decides to
14423 do this based upon one simple rule for bit-field allocation.
14424 GCC allocates each "containing object" for each bit-field at
14425 the first (i.e. lowest addressed) legitimate alignment boundary
14426 (based upon the required minimum alignment for the declared
14427 type of the field) which it can possibly use, subject to the
14428 condition that there is still enough available space remaining
14429 in the containing object (when allocated at the selected point)
14430 to fully accommodate all of the bits of the bit-field itself.
14432 This simple rule makes it obvious why GCC allocates 8 bytes for
14433 each object of the structure type shown above. When looking
14434 for a place to allocate the "containing object" for `field2',
14435 the compiler simply tries to allocate a 64-bit "containing
14436 object" at each successive 32-bit boundary (starting at zero)
14437 until it finds a place to allocate that 64- bit field such that
14438 at least 31 contiguous (and previously unallocated) bits remain
14439 within that selected 64 bit field. (As it turns out, for the
14440 example above, the compiler finds it is OK to allocate the
14441 "containing object" 64-bit field at bit-offset zero within the
14442 structure type.)
14444 Here we attempt to work backwards from the limited set of facts
14445 we're given, and we try to deduce from those facts, where GCC
14446 must have believed that the containing object started (within
14447 the structure type). The value we deduce is then used (by the
14448 callers of this routine) to generate DW_AT_location and
14449 DW_AT_bit_offset attributes for fields (both bit-fields and, in
14450 the case of DW_AT_location, regular fields as well). */
14452 /* Figure out the bit-distance from the start of the structure to
14453 the "deepest" bit of the bit-field. */
14454 deepest_bitpos = bitpos_int + field_size_in_bits;
14456 /* This is the tricky part. Use some fancy footwork to deduce
14457 where the lowest addressed bit of the containing object must
14458 be. */
14459 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14461 /* Round up to type_align by default. This works best for
14462 bitfields. */
14463 object_offset_in_bits
14464 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
14466 if (object_offset_in_bits.ugt (bitpos_int))
14468 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14470 /* Round up to decl_align instead. */
14471 object_offset_in_bits
14472 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
14475 else
14476 #endif /* PCC_BITFIELD_TYPE_MATTERS */
14477 object_offset_in_bits = bitpos_int;
14479 object_offset_in_bytes
14480 = object_offset_in_bits.div (double_int::from_uhwi (BITS_PER_UNIT),
14481 true, TRUNC_DIV_EXPR);
14482 return object_offset_in_bytes.to_shwi ();
14485 /* The following routines define various Dwarf attributes and any data
14486 associated with them. */
14488 /* Add a location description attribute value to a DIE.
14490 This emits location attributes suitable for whole variables and
14491 whole parameters. Note that the location attributes for struct fields are
14492 generated by the routine `data_member_location_attribute' below. */
14494 static inline void
14495 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
14496 dw_loc_list_ref descr)
14498 if (descr == 0)
14499 return;
14500 if (single_element_loc_list_p (descr))
14501 add_AT_loc (die, attr_kind, descr->expr);
14502 else
14503 add_AT_loc_list (die, attr_kind, descr);
14506 /* Add DW_AT_accessibility attribute to DIE if needed. */
14508 static void
14509 add_accessibility_attribute (dw_die_ref die, tree decl)
14511 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
14512 children, otherwise the default is DW_ACCESS_public. In DWARF2
14513 the default has always been DW_ACCESS_public. */
14514 if (TREE_PROTECTED (decl))
14515 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
14516 else if (TREE_PRIVATE (decl))
14518 if (dwarf_version == 2
14519 || die->die_parent == NULL
14520 || die->die_parent->die_tag != DW_TAG_class_type)
14521 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
14523 else if (dwarf_version > 2
14524 && die->die_parent
14525 && die->die_parent->die_tag == DW_TAG_class_type)
14526 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
14529 /* Attach the specialized form of location attribute used for data members of
14530 struct and union types. In the special case of a FIELD_DECL node which
14531 represents a bit-field, the "offset" part of this special location
14532 descriptor must indicate the distance in bytes from the lowest-addressed
14533 byte of the containing struct or union type to the lowest-addressed byte of
14534 the "containing object" for the bit-field. (See the `field_byte_offset'
14535 function above).
14537 For any given bit-field, the "containing object" is a hypothetical object
14538 (of some integral or enum type) within which the given bit-field lives. The
14539 type of this hypothetical "containing object" is always the same as the
14540 declared type of the individual bit-field itself (for GCC anyway... the
14541 DWARF spec doesn't actually mandate this). Note that it is the size (in
14542 bytes) of the hypothetical "containing object" which will be given in the
14543 DW_AT_byte_size attribute for this bit-field. (See the
14544 `byte_size_attribute' function below.) It is also used when calculating the
14545 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
14546 function below.) */
14548 static void
14549 add_data_member_location_attribute (dw_die_ref die, tree decl)
14551 HOST_WIDE_INT offset;
14552 dw_loc_descr_ref loc_descr = 0;
14554 if (TREE_CODE (decl) == TREE_BINFO)
14556 /* We're working on the TAG_inheritance for a base class. */
14557 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
14559 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
14560 aren't at a fixed offset from all (sub)objects of the same
14561 type. We need to extract the appropriate offset from our
14562 vtable. The following dwarf expression means
14564 BaseAddr = ObAddr + *((*ObAddr) - Offset)
14566 This is specific to the V3 ABI, of course. */
14568 dw_loc_descr_ref tmp;
14570 /* Make a copy of the object address. */
14571 tmp = new_loc_descr (DW_OP_dup, 0, 0);
14572 add_loc_descr (&loc_descr, tmp);
14574 /* Extract the vtable address. */
14575 tmp = new_loc_descr (DW_OP_deref, 0, 0);
14576 add_loc_descr (&loc_descr, tmp);
14578 /* Calculate the address of the offset. */
14579 offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
14580 gcc_assert (offset < 0);
14582 tmp = int_loc_descriptor (-offset);
14583 add_loc_descr (&loc_descr, tmp);
14584 tmp = new_loc_descr (DW_OP_minus, 0, 0);
14585 add_loc_descr (&loc_descr, tmp);
14587 /* Extract the offset. */
14588 tmp = new_loc_descr (DW_OP_deref, 0, 0);
14589 add_loc_descr (&loc_descr, tmp);
14591 /* Add it to the object address. */
14592 tmp = new_loc_descr (DW_OP_plus, 0, 0);
14593 add_loc_descr (&loc_descr, tmp);
14595 else
14596 offset = tree_low_cst (BINFO_OFFSET (decl), 0);
14598 else
14599 offset = field_byte_offset (decl);
14601 if (! loc_descr)
14603 if (dwarf_version > 2)
14605 /* Don't need to output a location expression, just the constant. */
14606 if (offset < 0)
14607 add_AT_int (die, DW_AT_data_member_location, offset);
14608 else
14609 add_AT_unsigned (die, DW_AT_data_member_location, offset);
14610 return;
14612 else
14614 enum dwarf_location_atom op;
14616 /* The DWARF2 standard says that we should assume that the structure
14617 address is already on the stack, so we can specify a structure
14618 field address by using DW_OP_plus_uconst. */
14619 op = DW_OP_plus_uconst;
14620 loc_descr = new_loc_descr (op, offset, 0);
14624 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
14627 /* Writes integer values to dw_vec_const array. */
14629 static void
14630 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
14632 while (size != 0)
14634 *dest++ = val & 0xff;
14635 val >>= 8;
14636 --size;
14640 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
14642 static HOST_WIDE_INT
14643 extract_int (const unsigned char *src, unsigned int size)
14645 HOST_WIDE_INT val = 0;
14647 src += size;
14648 while (size != 0)
14650 val <<= 8;
14651 val |= *--src & 0xff;
14652 --size;
14654 return val;
14657 /* Writes double_int values to dw_vec_const array. */
14659 static void
14660 insert_double (double_int val, unsigned char *dest)
14662 unsigned char *p0 = dest;
14663 unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
14665 if (WORDS_BIG_ENDIAN)
14667 p0 = p1;
14668 p1 = dest;
14671 insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
14672 insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
14675 /* Writes floating point values to dw_vec_const array. */
14677 static void
14678 insert_float (const_rtx rtl, unsigned char *array)
14680 REAL_VALUE_TYPE rv;
14681 long val[4];
14682 int i;
14684 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
14685 real_to_target (val, &rv, GET_MODE (rtl));
14687 /* real_to_target puts 32-bit pieces in each long. Pack them. */
14688 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
14690 insert_int (val[i], 4, array);
14691 array += 4;
14695 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
14696 does not have a "location" either in memory or in a register. These
14697 things can arise in GNU C when a constant is passed as an actual parameter
14698 to an inlined function. They can also arise in C++ where declared
14699 constants do not necessarily get memory "homes". */
14701 static bool
14702 add_const_value_attribute (dw_die_ref die, rtx rtl)
14704 switch (GET_CODE (rtl))
14706 case CONST_INT:
14708 HOST_WIDE_INT val = INTVAL (rtl);
14710 if (val < 0)
14711 add_AT_int (die, DW_AT_const_value, val);
14712 else
14713 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
14715 return true;
14717 case CONST_DOUBLE:
14718 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
14719 floating-point constant. A CONST_DOUBLE is used whenever the
14720 constant requires more than one word in order to be adequately
14721 represented. */
14723 enum machine_mode mode = GET_MODE (rtl);
14725 if (SCALAR_FLOAT_MODE_P (mode))
14727 unsigned int length = GET_MODE_SIZE (mode);
14728 unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
14730 insert_float (rtl, array);
14731 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
14733 else
14734 add_AT_double (die, DW_AT_const_value,
14735 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
14737 return true;
14739 case CONST_VECTOR:
14741 enum machine_mode mode = GET_MODE (rtl);
14742 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
14743 unsigned int length = CONST_VECTOR_NUNITS (rtl);
14744 unsigned char *array = (unsigned char *) ggc_alloc_atomic
14745 (length * elt_size);
14746 unsigned int i;
14747 unsigned char *p;
14749 switch (GET_MODE_CLASS (mode))
14751 case MODE_VECTOR_INT:
14752 for (i = 0, p = array; i < length; i++, p += elt_size)
14754 rtx elt = CONST_VECTOR_ELT (rtl, i);
14755 double_int val = rtx_to_double_int (elt);
14757 if (elt_size <= sizeof (HOST_WIDE_INT))
14758 insert_int (val.to_shwi (), elt_size, p);
14759 else
14761 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
14762 insert_double (val, p);
14765 break;
14767 case MODE_VECTOR_FLOAT:
14768 for (i = 0, p = array; i < length; i++, p += elt_size)
14770 rtx elt = CONST_VECTOR_ELT (rtl, i);
14771 insert_float (elt, p);
14773 break;
14775 default:
14776 gcc_unreachable ();
14779 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
14781 return true;
14783 case CONST_STRING:
14784 if (dwarf_version >= 4 || !dwarf_strict)
14786 dw_loc_descr_ref loc_result;
14787 resolve_one_addr (&rtl, NULL);
14788 rtl_addr:
14789 loc_result = new_addr_loc_descr (rtl, dtprel_false);
14790 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14791 add_AT_loc (die, DW_AT_location, loc_result);
14792 vec_safe_push (used_rtx_array, rtl);
14793 return true;
14795 return false;
14797 case CONST:
14798 if (CONSTANT_P (XEXP (rtl, 0)))
14799 return add_const_value_attribute (die, XEXP (rtl, 0));
14800 /* FALLTHROUGH */
14801 case SYMBOL_REF:
14802 if (!const_ok_for_output (rtl))
14803 return false;
14804 case LABEL_REF:
14805 if (dwarf_version >= 4 || !dwarf_strict)
14806 goto rtl_addr;
14807 return false;
14809 case PLUS:
14810 /* In cases where an inlined instance of an inline function is passed
14811 the address of an `auto' variable (which is local to the caller) we
14812 can get a situation where the DECL_RTL of the artificial local
14813 variable (for the inlining) which acts as a stand-in for the
14814 corresponding formal parameter (of the inline function) will look
14815 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
14816 exactly a compile-time constant expression, but it isn't the address
14817 of the (artificial) local variable either. Rather, it represents the
14818 *value* which the artificial local variable always has during its
14819 lifetime. We currently have no way to represent such quasi-constant
14820 values in Dwarf, so for now we just punt and generate nothing. */
14821 return false;
14823 case HIGH:
14824 case CONST_FIXED:
14825 return false;
14827 case MEM:
14828 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
14829 && MEM_READONLY_P (rtl)
14830 && GET_MODE (rtl) == BLKmode)
14832 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
14833 return true;
14835 return false;
14837 default:
14838 /* No other kinds of rtx should be possible here. */
14839 gcc_unreachable ();
14841 return false;
14844 /* Determine whether the evaluation of EXPR references any variables
14845 or functions which aren't otherwise used (and therefore may not be
14846 output). */
14847 static tree
14848 reference_to_unused (tree * tp, int * walk_subtrees,
14849 void * data ATTRIBUTE_UNUSED)
14851 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
14852 *walk_subtrees = 0;
14854 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
14855 && ! TREE_ASM_WRITTEN (*tp))
14856 return *tp;
14857 /* ??? The C++ FE emits debug information for using decls, so
14858 putting gcc_unreachable here falls over. See PR31899. For now
14859 be conservative. */
14860 else if (!cgraph_global_info_ready
14861 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
14862 return *tp;
14863 else if (TREE_CODE (*tp) == VAR_DECL)
14865 struct varpool_node *node = varpool_get_node (*tp);
14866 if (!node || !node->analyzed)
14867 return *tp;
14869 else if (TREE_CODE (*tp) == FUNCTION_DECL
14870 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
14872 /* The call graph machinery must have finished analyzing,
14873 optimizing and gimplifying the CU by now.
14874 So if *TP has no call graph node associated
14875 to it, it means *TP will not be emitted. */
14876 if (!cgraph_get_node (*tp))
14877 return *tp;
14879 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
14880 return *tp;
14882 return NULL_TREE;
14885 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
14886 for use in a later add_const_value_attribute call. */
14888 static rtx
14889 rtl_for_decl_init (tree init, tree type)
14891 rtx rtl = NULL_RTX;
14893 STRIP_NOPS (init);
14895 /* If a variable is initialized with a string constant without embedded
14896 zeros, build CONST_STRING. */
14897 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
14899 tree enttype = TREE_TYPE (type);
14900 tree domain = TYPE_DOMAIN (type);
14901 enum machine_mode mode = TYPE_MODE (enttype);
14903 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
14904 && domain
14905 && integer_zerop (TYPE_MIN_VALUE (domain))
14906 && compare_tree_int (TYPE_MAX_VALUE (domain),
14907 TREE_STRING_LENGTH (init) - 1) == 0
14908 && ((size_t) TREE_STRING_LENGTH (init)
14909 == strlen (TREE_STRING_POINTER (init)) + 1))
14911 rtl = gen_rtx_CONST_STRING (VOIDmode,
14912 ggc_strdup (TREE_STRING_POINTER (init)));
14913 rtl = gen_rtx_MEM (BLKmode, rtl);
14914 MEM_READONLY_P (rtl) = 1;
14917 /* Other aggregates, and complex values, could be represented using
14918 CONCAT: FIXME! */
14919 else if (AGGREGATE_TYPE_P (type)
14920 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
14921 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
14922 || TREE_CODE (type) == COMPLEX_TYPE)
14924 /* Vectors only work if their mode is supported by the target.
14925 FIXME: generic vectors ought to work too. */
14926 else if (TREE_CODE (type) == VECTOR_TYPE
14927 && !VECTOR_MODE_P (TYPE_MODE (type)))
14929 /* If the initializer is something that we know will expand into an
14930 immediate RTL constant, expand it now. We must be careful not to
14931 reference variables which won't be output. */
14932 else if (initializer_constant_valid_p (init, type)
14933 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
14935 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
14936 possible. */
14937 if (TREE_CODE (type) == VECTOR_TYPE)
14938 switch (TREE_CODE (init))
14940 case VECTOR_CST:
14941 break;
14942 case CONSTRUCTOR:
14943 if (TREE_CONSTANT (init))
14945 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
14946 bool constant_p = true;
14947 tree value;
14948 unsigned HOST_WIDE_INT ix;
14950 /* Even when ctor is constant, it might contain non-*_CST
14951 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
14952 belong into VECTOR_CST nodes. */
14953 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
14954 if (!CONSTANT_CLASS_P (value))
14956 constant_p = false;
14957 break;
14960 if (constant_p)
14962 init = build_vector_from_ctor (type, elts);
14963 break;
14966 /* FALLTHRU */
14968 default:
14969 return NULL;
14972 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
14974 /* If expand_expr returns a MEM, it wasn't immediate. */
14975 gcc_assert (!rtl || !MEM_P (rtl));
14978 return rtl;
14981 /* Generate RTL for the variable DECL to represent its location. */
14983 static rtx
14984 rtl_for_decl_location (tree decl)
14986 rtx rtl;
14988 /* Here we have to decide where we are going to say the parameter "lives"
14989 (as far as the debugger is concerned). We only have a couple of
14990 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
14992 DECL_RTL normally indicates where the parameter lives during most of the
14993 activation of the function. If optimization is enabled however, this
14994 could be either NULL or else a pseudo-reg. Both of those cases indicate
14995 that the parameter doesn't really live anywhere (as far as the code
14996 generation parts of GCC are concerned) during most of the function's
14997 activation. That will happen (for example) if the parameter is never
14998 referenced within the function.
15000 We could just generate a location descriptor here for all non-NULL
15001 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15002 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15003 where DECL_RTL is NULL or is a pseudo-reg.
15005 Note however that we can only get away with using DECL_INCOMING_RTL as
15006 a backup substitute for DECL_RTL in certain limited cases. In cases
15007 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15008 we can be sure that the parameter was passed using the same type as it is
15009 declared to have within the function, and that its DECL_INCOMING_RTL
15010 points us to a place where a value of that type is passed.
15012 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15013 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15014 because in these cases DECL_INCOMING_RTL points us to a value of some
15015 type which is *different* from the type of the parameter itself. Thus,
15016 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15017 such cases, the debugger would end up (for example) trying to fetch a
15018 `float' from a place which actually contains the first part of a
15019 `double'. That would lead to really incorrect and confusing
15020 output at debug-time.
15022 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15023 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
15024 are a couple of exceptions however. On little-endian machines we can
15025 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15026 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15027 an integral type that is smaller than TREE_TYPE (decl). These cases arise
15028 when (on a little-endian machine) a non-prototyped function has a
15029 parameter declared to be of type `short' or `char'. In such cases,
15030 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15031 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15032 passed `int' value. If the debugger then uses that address to fetch
15033 a `short' or a `char' (on a little-endian machine) the result will be
15034 the correct data, so we allow for such exceptional cases below.
15036 Note that our goal here is to describe the place where the given formal
15037 parameter lives during most of the function's activation (i.e. between the
15038 end of the prologue and the start of the epilogue). We'll do that as best
15039 as we can. Note however that if the given formal parameter is modified
15040 sometime during the execution of the function, then a stack backtrace (at
15041 debug-time) will show the function as having been called with the *new*
15042 value rather than the value which was originally passed in. This happens
15043 rarely enough that it is not a major problem, but it *is* a problem, and
15044 I'd like to fix it.
15046 A future version of dwarf2out.c may generate two additional attributes for
15047 any given DW_TAG_formal_parameter DIE which will describe the "passed
15048 type" and the "passed location" for the given formal parameter in addition
15049 to the attributes we now generate to indicate the "declared type" and the
15050 "active location" for each parameter. This additional set of attributes
15051 could be used by debuggers for stack backtraces. Separately, note that
15052 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15053 This happens (for example) for inlined-instances of inline function formal
15054 parameters which are never referenced. This really shouldn't be
15055 happening. All PARM_DECL nodes should get valid non-NULL
15056 DECL_INCOMING_RTL values. FIXME. */
15058 /* Use DECL_RTL as the "location" unless we find something better. */
15059 rtl = DECL_RTL_IF_SET (decl);
15061 /* When generating abstract instances, ignore everything except
15062 constants, symbols living in memory, and symbols living in
15063 fixed registers. */
15064 if (! reload_completed)
15066 if (rtl
15067 && (CONSTANT_P (rtl)
15068 || (MEM_P (rtl)
15069 && CONSTANT_P (XEXP (rtl, 0)))
15070 || (REG_P (rtl)
15071 && TREE_CODE (decl) == VAR_DECL
15072 && TREE_STATIC (decl))))
15074 rtl = targetm.delegitimize_address (rtl);
15075 return rtl;
15077 rtl = NULL_RTX;
15079 else if (TREE_CODE (decl) == PARM_DECL)
15081 if (rtl == NULL_RTX
15082 || is_pseudo_reg (rtl)
15083 || (MEM_P (rtl)
15084 && is_pseudo_reg (XEXP (rtl, 0))
15085 && DECL_INCOMING_RTL (decl)
15086 && MEM_P (DECL_INCOMING_RTL (decl))
15087 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15089 tree declared_type = TREE_TYPE (decl);
15090 tree passed_type = DECL_ARG_TYPE (decl);
15091 enum machine_mode dmode = TYPE_MODE (declared_type);
15092 enum machine_mode pmode = TYPE_MODE (passed_type);
15094 /* This decl represents a formal parameter which was optimized out.
15095 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15096 all cases where (rtl == NULL_RTX) just below. */
15097 if (dmode == pmode)
15098 rtl = DECL_INCOMING_RTL (decl);
15099 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15100 && SCALAR_INT_MODE_P (dmode)
15101 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15102 && DECL_INCOMING_RTL (decl))
15104 rtx inc = DECL_INCOMING_RTL (decl);
15105 if (REG_P (inc))
15106 rtl = inc;
15107 else if (MEM_P (inc))
15109 if (BYTES_BIG_ENDIAN)
15110 rtl = adjust_address_nv (inc, dmode,
15111 GET_MODE_SIZE (pmode)
15112 - GET_MODE_SIZE (dmode));
15113 else
15114 rtl = inc;
15119 /* If the parm was passed in registers, but lives on the stack, then
15120 make a big endian correction if the mode of the type of the
15121 parameter is not the same as the mode of the rtl. */
15122 /* ??? This is the same series of checks that are made in dbxout.c before
15123 we reach the big endian correction code there. It isn't clear if all
15124 of these checks are necessary here, but keeping them all is the safe
15125 thing to do. */
15126 else if (MEM_P (rtl)
15127 && XEXP (rtl, 0) != const0_rtx
15128 && ! CONSTANT_P (XEXP (rtl, 0))
15129 /* Not passed in memory. */
15130 && !MEM_P (DECL_INCOMING_RTL (decl))
15131 /* Not passed by invisible reference. */
15132 && (!REG_P (XEXP (rtl, 0))
15133 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15134 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15135 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15136 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15137 #endif
15139 /* Big endian correction check. */
15140 && BYTES_BIG_ENDIAN
15141 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15142 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15143 < UNITS_PER_WORD))
15145 enum machine_mode addr_mode = get_address_mode (rtl);
15146 int offset = (UNITS_PER_WORD
15147 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15149 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15150 plus_constant (addr_mode, XEXP (rtl, 0), offset));
15153 else if (TREE_CODE (decl) == VAR_DECL
15154 && rtl
15155 && MEM_P (rtl)
15156 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
15157 && BYTES_BIG_ENDIAN)
15159 enum machine_mode addr_mode = get_address_mode (rtl);
15160 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
15161 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
15163 /* If a variable is declared "register" yet is smaller than
15164 a register, then if we store the variable to memory, it
15165 looks like we're storing a register-sized value, when in
15166 fact we are not. We need to adjust the offset of the
15167 storage location to reflect the actual value's bytes,
15168 else gdb will not be able to display it. */
15169 if (rsize > dsize)
15170 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15171 plus_constant (addr_mode, XEXP (rtl, 0),
15172 rsize - dsize));
15175 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
15176 and will have been substituted directly into all expressions that use it.
15177 C does not have such a concept, but C++ and other languages do. */
15178 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
15179 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
15181 if (rtl)
15182 rtl = targetm.delegitimize_address (rtl);
15184 /* If we don't look past the constant pool, we risk emitting a
15185 reference to a constant pool entry that isn't referenced from
15186 code, and thus is not emitted. */
15187 if (rtl)
15188 rtl = avoid_constant_pool_reference (rtl);
15190 /* Try harder to get a rtl. If this symbol ends up not being emitted
15191 in the current CU, resolve_addr will remove the expression referencing
15192 it. */
15193 if (rtl == NULL_RTX
15194 && TREE_CODE (decl) == VAR_DECL
15195 && !DECL_EXTERNAL (decl)
15196 && TREE_STATIC (decl)
15197 && DECL_NAME (decl)
15198 && !DECL_HARD_REGISTER (decl)
15199 && DECL_MODE (decl) != VOIDmode)
15201 rtl = make_decl_rtl_for_debug (decl);
15202 if (!MEM_P (rtl)
15203 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
15204 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
15205 rtl = NULL_RTX;
15208 return rtl;
15211 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
15212 returned. If so, the decl for the COMMON block is returned, and the
15213 value is the offset into the common block for the symbol. */
15215 static tree
15216 fortran_common (tree decl, HOST_WIDE_INT *value)
15218 tree val_expr, cvar;
15219 enum machine_mode mode;
15220 HOST_WIDE_INT bitsize, bitpos;
15221 tree offset;
15222 int unsignedp, volatilep = 0;
15224 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
15225 it does not have a value (the offset into the common area), or if it
15226 is thread local (as opposed to global) then it isn't common, and shouldn't
15227 be handled as such. */
15228 if (TREE_CODE (decl) != VAR_DECL
15229 || !TREE_STATIC (decl)
15230 || !DECL_HAS_VALUE_EXPR_P (decl)
15231 || !is_fortran ())
15232 return NULL_TREE;
15234 val_expr = DECL_VALUE_EXPR (decl);
15235 if (TREE_CODE (val_expr) != COMPONENT_REF)
15236 return NULL_TREE;
15238 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
15239 &mode, &unsignedp, &volatilep, true);
15241 if (cvar == NULL_TREE
15242 || TREE_CODE (cvar) != VAR_DECL
15243 || DECL_ARTIFICIAL (cvar)
15244 || !TREE_PUBLIC (cvar))
15245 return NULL_TREE;
15247 *value = 0;
15248 if (offset != NULL)
15250 if (!host_integerp (offset, 0))
15251 return NULL_TREE;
15252 *value = tree_low_cst (offset, 0);
15254 if (bitpos != 0)
15255 *value += bitpos / BITS_PER_UNIT;
15257 return cvar;
15260 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
15261 data attribute for a variable or a parameter. We generate the
15262 DW_AT_const_value attribute only in those cases where the given variable
15263 or parameter does not have a true "location" either in memory or in a
15264 register. This can happen (for example) when a constant is passed as an
15265 actual argument in a call to an inline function. (It's possible that
15266 these things can crop up in other ways also.) Note that one type of
15267 constant value which can be passed into an inlined function is a constant
15268 pointer. This can happen for example if an actual argument in an inlined
15269 function call evaluates to a compile-time constant address.
15271 CACHE_P is true if it is worth caching the location list for DECL,
15272 so that future calls can reuse it rather than regenerate it from scratch.
15273 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
15274 since we will need to refer to them each time the function is inlined. */
15276 static bool
15277 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
15278 enum dwarf_attribute attr)
15280 rtx rtl;
15281 dw_loc_list_ref list;
15282 var_loc_list *loc_list;
15283 cached_dw_loc_list *cache;
15284 void **slot;
15286 if (TREE_CODE (decl) == ERROR_MARK)
15287 return false;
15289 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
15290 || TREE_CODE (decl) == RESULT_DECL);
15292 /* Try to get some constant RTL for this decl, and use that as the value of
15293 the location. */
15295 rtl = rtl_for_decl_location (decl);
15296 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15297 && add_const_value_attribute (die, rtl))
15298 return true;
15300 /* See if we have single element location list that is equivalent to
15301 a constant value. That way we are better to use add_const_value_attribute
15302 rather than expanding constant value equivalent. */
15303 loc_list = lookup_decl_loc (decl);
15304 if (loc_list
15305 && loc_list->first
15306 && loc_list->first->next == NULL
15307 && NOTE_P (loc_list->first->loc)
15308 && NOTE_VAR_LOCATION (loc_list->first->loc)
15309 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
15311 struct var_loc_node *node;
15313 node = loc_list->first;
15314 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
15315 if (GET_CODE (rtl) == EXPR_LIST)
15316 rtl = XEXP (rtl, 0);
15317 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15318 && add_const_value_attribute (die, rtl))
15319 return true;
15321 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
15322 list several times. See if we've already cached the contents. */
15323 list = NULL;
15324 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
15325 cache_p = false;
15326 if (cache_p)
15328 cache = (cached_dw_loc_list *)
15329 htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
15330 if (cache)
15331 list = cache->loc_list;
15333 if (list == NULL)
15335 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
15336 /* It is usually worth caching this result if the decl is from
15337 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
15338 if (cache_p && list && list->dw_loc_next)
15340 slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
15341 DECL_UID (decl), INSERT);
15342 cache = ggc_alloc_cleared_cached_dw_loc_list ();
15343 cache->decl_id = DECL_UID (decl);
15344 cache->loc_list = list;
15345 *slot = cache;
15348 if (list)
15350 add_AT_location_description (die, attr, list);
15351 return true;
15353 /* None of that worked, so it must not really have a location;
15354 try adding a constant value attribute from the DECL_INITIAL. */
15355 return tree_add_const_value_attribute_for_decl (die, decl);
15358 /* Add VARIABLE and DIE into deferred locations list. */
15360 static void
15361 defer_location (tree variable, dw_die_ref die)
15363 deferred_locations entry;
15364 entry.variable = variable;
15365 entry.die = die;
15366 vec_safe_push (deferred_locations_list, entry);
15369 /* Helper function for tree_add_const_value_attribute. Natively encode
15370 initializer INIT into an array. Return true if successful. */
15372 static bool
15373 native_encode_initializer (tree init, unsigned char *array, int size)
15375 tree type;
15377 if (init == NULL_TREE)
15378 return false;
15380 STRIP_NOPS (init);
15381 switch (TREE_CODE (init))
15383 case STRING_CST:
15384 type = TREE_TYPE (init);
15385 if (TREE_CODE (type) == ARRAY_TYPE)
15387 tree enttype = TREE_TYPE (type);
15388 enum machine_mode mode = TYPE_MODE (enttype);
15390 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
15391 return false;
15392 if (int_size_in_bytes (type) != size)
15393 return false;
15394 if (size > TREE_STRING_LENGTH (init))
15396 memcpy (array, TREE_STRING_POINTER (init),
15397 TREE_STRING_LENGTH (init));
15398 memset (array + TREE_STRING_LENGTH (init),
15399 '\0', size - TREE_STRING_LENGTH (init));
15401 else
15402 memcpy (array, TREE_STRING_POINTER (init), size);
15403 return true;
15405 return false;
15406 case CONSTRUCTOR:
15407 type = TREE_TYPE (init);
15408 if (int_size_in_bytes (type) != size)
15409 return false;
15410 if (TREE_CODE (type) == ARRAY_TYPE)
15412 HOST_WIDE_INT min_index;
15413 unsigned HOST_WIDE_INT cnt;
15414 int curpos = 0, fieldsize;
15415 constructor_elt *ce;
15417 if (TYPE_DOMAIN (type) == NULL_TREE
15418 || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
15419 return false;
15421 fieldsize = int_size_in_bytes (TREE_TYPE (type));
15422 if (fieldsize <= 0)
15423 return false;
15425 min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
15426 memset (array, '\0', size);
15427 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15429 tree val = ce->value;
15430 tree index = ce->index;
15431 int pos = curpos;
15432 if (index && TREE_CODE (index) == RANGE_EXPR)
15433 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
15434 * fieldsize;
15435 else if (index)
15436 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
15438 if (val)
15440 STRIP_NOPS (val);
15441 if (!native_encode_initializer (val, array + pos, fieldsize))
15442 return false;
15444 curpos = pos + fieldsize;
15445 if (index && TREE_CODE (index) == RANGE_EXPR)
15447 int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
15448 - tree_low_cst (TREE_OPERAND (index, 0), 0);
15449 while (count-- > 0)
15451 if (val)
15452 memcpy (array + curpos, array + pos, fieldsize);
15453 curpos += fieldsize;
15456 gcc_assert (curpos <= size);
15458 return true;
15460 else if (TREE_CODE (type) == RECORD_TYPE
15461 || TREE_CODE (type) == UNION_TYPE)
15463 tree field = NULL_TREE;
15464 unsigned HOST_WIDE_INT cnt;
15465 constructor_elt *ce;
15467 if (int_size_in_bytes (type) != size)
15468 return false;
15470 if (TREE_CODE (type) == RECORD_TYPE)
15471 field = TYPE_FIELDS (type);
15473 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15475 tree val = ce->value;
15476 int pos, fieldsize;
15478 if (ce->index != 0)
15479 field = ce->index;
15481 if (val)
15482 STRIP_NOPS (val);
15484 if (field == NULL_TREE || DECL_BIT_FIELD (field))
15485 return false;
15487 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
15488 && TYPE_DOMAIN (TREE_TYPE (field))
15489 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
15490 return false;
15491 else if (DECL_SIZE_UNIT (field) == NULL_TREE
15492 || !host_integerp (DECL_SIZE_UNIT (field), 0))
15493 return false;
15494 fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
15495 pos = int_byte_position (field);
15496 gcc_assert (pos + fieldsize <= size);
15497 if (val
15498 && !native_encode_initializer (val, array + pos, fieldsize))
15499 return false;
15501 return true;
15503 return false;
15504 case VIEW_CONVERT_EXPR:
15505 case NON_LVALUE_EXPR:
15506 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
15507 default:
15508 return native_encode_expr (init, array, size) == size;
15512 /* Attach a DW_AT_const_value attribute to DIE. The value of the
15513 attribute is the const value T. */
15515 static bool
15516 tree_add_const_value_attribute (dw_die_ref die, tree t)
15518 tree init;
15519 tree type = TREE_TYPE (t);
15520 rtx rtl;
15522 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
15523 return false;
15525 init = t;
15526 gcc_assert (!DECL_P (init));
15528 rtl = rtl_for_decl_init (init, type);
15529 if (rtl)
15530 return add_const_value_attribute (die, rtl);
15531 /* If the host and target are sane, try harder. */
15532 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
15533 && initializer_constant_valid_p (init, type))
15535 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
15536 if (size > 0 && (int) size == size)
15538 unsigned char *array = (unsigned char *)
15539 ggc_alloc_cleared_atomic (size);
15541 if (native_encode_initializer (init, array, size))
15543 add_AT_vec (die, DW_AT_const_value, size, 1, array);
15544 return true;
15548 return false;
15551 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
15552 attribute is the const value of T, where T is an integral constant
15553 variable with static storage duration
15554 (so it can't be a PARM_DECL or a RESULT_DECL). */
15556 static bool
15557 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
15560 if (!decl
15561 || (TREE_CODE (decl) != VAR_DECL
15562 && TREE_CODE (decl) != CONST_DECL)
15563 || (TREE_CODE (decl) == VAR_DECL
15564 && !TREE_STATIC (decl)))
15565 return false;
15567 if (TREE_READONLY (decl)
15568 && ! TREE_THIS_VOLATILE (decl)
15569 && DECL_INITIAL (decl))
15570 /* OK */;
15571 else
15572 return false;
15574 /* Don't add DW_AT_const_value if abstract origin already has one. */
15575 if (get_AT (var_die, DW_AT_const_value))
15576 return false;
15578 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
15581 /* Convert the CFI instructions for the current function into a
15582 location list. This is used for DW_AT_frame_base when we targeting
15583 a dwarf2 consumer that does not support the dwarf3
15584 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
15585 expressions. */
15587 static dw_loc_list_ref
15588 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
15590 int ix;
15591 dw_fde_ref fde;
15592 dw_loc_list_ref list, *list_tail;
15593 dw_cfi_ref cfi;
15594 dw_cfa_location last_cfa, next_cfa;
15595 const char *start_label, *last_label, *section;
15596 dw_cfa_location remember;
15598 fde = cfun->fde;
15599 gcc_assert (fde != NULL);
15601 section = secname_for_decl (current_function_decl);
15602 list_tail = &list;
15603 list = NULL;
15605 memset (&next_cfa, 0, sizeof (next_cfa));
15606 next_cfa.reg = INVALID_REGNUM;
15607 remember = next_cfa;
15609 start_label = fde->dw_fde_begin;
15611 /* ??? Bald assumption that the CIE opcode list does not contain
15612 advance opcodes. */
15613 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
15614 lookup_cfa_1 (cfi, &next_cfa, &remember);
15616 last_cfa = next_cfa;
15617 last_label = start_label;
15619 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
15621 /* If the first partition contained no CFI adjustments, the
15622 CIE opcodes apply to the whole first partition. */
15623 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15624 fde->dw_fde_begin, fde->dw_fde_end, section);
15625 list_tail =&(*list_tail)->dw_loc_next;
15626 start_label = last_label = fde->dw_fde_second_begin;
15629 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
15631 switch (cfi->dw_cfi_opc)
15633 case DW_CFA_set_loc:
15634 case DW_CFA_advance_loc1:
15635 case DW_CFA_advance_loc2:
15636 case DW_CFA_advance_loc4:
15637 if (!cfa_equal_p (&last_cfa, &next_cfa))
15639 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15640 start_label, last_label, section);
15642 list_tail = &(*list_tail)->dw_loc_next;
15643 last_cfa = next_cfa;
15644 start_label = last_label;
15646 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
15647 break;
15649 case DW_CFA_advance_loc:
15650 /* The encoding is complex enough that we should never emit this. */
15651 gcc_unreachable ();
15653 default:
15654 lookup_cfa_1 (cfi, &next_cfa, &remember);
15655 break;
15657 if (ix + 1 == fde->dw_fde_switch_cfi_index)
15659 if (!cfa_equal_p (&last_cfa, &next_cfa))
15661 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15662 start_label, last_label, section);
15664 list_tail = &(*list_tail)->dw_loc_next;
15665 last_cfa = next_cfa;
15666 start_label = last_label;
15668 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15669 start_label, fde->dw_fde_end, section);
15670 list_tail = &(*list_tail)->dw_loc_next;
15671 start_label = last_label = fde->dw_fde_second_begin;
15675 if (!cfa_equal_p (&last_cfa, &next_cfa))
15677 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15678 start_label, last_label, section);
15679 list_tail = &(*list_tail)->dw_loc_next;
15680 start_label = last_label;
15683 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
15684 start_label,
15685 fde->dw_fde_second_begin
15686 ? fde->dw_fde_second_end : fde->dw_fde_end,
15687 section);
15689 if (list && list->dw_loc_next)
15690 gen_llsym (list);
15692 return list;
15695 /* Compute a displacement from the "steady-state frame pointer" to the
15696 frame base (often the same as the CFA), and store it in
15697 frame_pointer_fb_offset. OFFSET is added to the displacement
15698 before the latter is negated. */
15700 static void
15701 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
15703 rtx reg, elim;
15705 #ifdef FRAME_POINTER_CFA_OFFSET
15706 reg = frame_pointer_rtx;
15707 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
15708 #else
15709 reg = arg_pointer_rtx;
15710 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
15711 #endif
15713 elim = (ira_use_lra_p
15714 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
15715 : eliminate_regs (reg, VOIDmode, NULL_RTX));
15716 if (GET_CODE (elim) == PLUS)
15718 offset += INTVAL (XEXP (elim, 1));
15719 elim = XEXP (elim, 0);
15722 frame_pointer_fb_offset = -offset;
15724 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
15725 in which to eliminate. This is because it's stack pointer isn't
15726 directly accessible as a register within the ISA. To work around
15727 this, assume that while we cannot provide a proper value for
15728 frame_pointer_fb_offset, we won't need one either. */
15729 frame_pointer_fb_offset_valid
15730 = ((SUPPORTS_STACK_ALIGNMENT
15731 && (elim == hard_frame_pointer_rtx
15732 || elim == stack_pointer_rtx))
15733 || elim == (frame_pointer_needed
15734 ? hard_frame_pointer_rtx
15735 : stack_pointer_rtx));
15738 /* Generate a DW_AT_name attribute given some string value to be included as
15739 the value of the attribute. */
15741 static void
15742 add_name_attribute (dw_die_ref die, const char *name_string)
15744 if (name_string != NULL && *name_string != 0)
15746 if (demangle_name_func)
15747 name_string = (*demangle_name_func) (name_string);
15749 add_AT_string (die, DW_AT_name, name_string);
15753 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
15754 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
15755 of TYPE accordingly.
15757 ??? This is a temporary measure until after we're able to generate
15758 regular DWARF for the complex Ada type system. */
15760 static void
15761 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
15762 dw_die_ref context_die)
15764 tree dtype;
15765 dw_die_ref dtype_die;
15767 if (!lang_hooks.types.descriptive_type)
15768 return;
15770 dtype = lang_hooks.types.descriptive_type (type);
15771 if (!dtype)
15772 return;
15774 dtype_die = lookup_type_die (dtype);
15775 if (!dtype_die)
15777 gen_type_die (dtype, context_die);
15778 dtype_die = lookup_type_die (dtype);
15779 gcc_assert (dtype_die);
15782 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
15785 /* Generate a DW_AT_comp_dir attribute for DIE. */
15787 static void
15788 add_comp_dir_attribute (dw_die_ref die)
15790 const char *wd = get_src_pwd ();
15791 char *wd1;
15793 if (wd == NULL)
15794 return;
15796 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
15798 int wdlen;
15800 wdlen = strlen (wd);
15801 wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
15802 strcpy (wd1, wd);
15803 wd1 [wdlen] = DIR_SEPARATOR;
15804 wd1 [wdlen + 1] = 0;
15805 wd = wd1;
15808 add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
15811 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
15812 default. */
15814 static int
15815 lower_bound_default (void)
15817 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
15819 case DW_LANG_C:
15820 case DW_LANG_C89:
15821 case DW_LANG_C99:
15822 case DW_LANG_C_plus_plus:
15823 case DW_LANG_ObjC:
15824 case DW_LANG_ObjC_plus_plus:
15825 case DW_LANG_Java:
15826 return 0;
15827 case DW_LANG_Fortran77:
15828 case DW_LANG_Fortran90:
15829 case DW_LANG_Fortran95:
15830 return 1;
15831 case DW_LANG_UPC:
15832 case DW_LANG_D:
15833 case DW_LANG_Python:
15834 return dwarf_version >= 4 ? 0 : -1;
15835 case DW_LANG_Ada95:
15836 case DW_LANG_Ada83:
15837 case DW_LANG_Cobol74:
15838 case DW_LANG_Cobol85:
15839 case DW_LANG_Pascal83:
15840 case DW_LANG_Modula2:
15841 case DW_LANG_PLI:
15842 return dwarf_version >= 4 ? 1 : -1;
15843 default:
15844 return -1;
15848 /* Given a tree node describing an array bound (either lower or upper) output
15849 a representation for that bound. */
15851 static void
15852 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
15854 switch (TREE_CODE (bound))
15856 case ERROR_MARK:
15857 return;
15859 /* All fixed-bounds are represented by INTEGER_CST nodes. */
15860 case INTEGER_CST:
15862 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
15863 int dflt;
15865 /* Use the default if possible. */
15866 if (bound_attr == DW_AT_lower_bound
15867 && host_integerp (bound, 0)
15868 && (dflt = lower_bound_default ()) != -1
15869 && tree_low_cst (bound, 0) == dflt)
15872 /* Otherwise represent the bound as an unsigned value with the
15873 precision of its type. The precision and signedness of the
15874 type will be necessary to re-interpret it unambiguously. */
15875 else if (prec < HOST_BITS_PER_WIDE_INT)
15877 unsigned HOST_WIDE_INT mask
15878 = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
15879 add_AT_unsigned (subrange_die, bound_attr,
15880 TREE_INT_CST_LOW (bound) & mask);
15882 else if (prec == HOST_BITS_PER_WIDE_INT
15883 || TREE_INT_CST_HIGH (bound) == 0)
15884 add_AT_unsigned (subrange_die, bound_attr,
15885 TREE_INT_CST_LOW (bound));
15886 else
15887 add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
15888 TREE_INT_CST_LOW (bound));
15890 break;
15892 CASE_CONVERT:
15893 case VIEW_CONVERT_EXPR:
15894 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
15895 break;
15897 case SAVE_EXPR:
15898 break;
15900 case VAR_DECL:
15901 case PARM_DECL:
15902 case RESULT_DECL:
15904 dw_die_ref decl_die = lookup_decl_die (bound);
15906 /* ??? Can this happen, or should the variable have been bound
15907 first? Probably it can, since I imagine that we try to create
15908 the types of parameters in the order in which they exist in
15909 the list, and won't have created a forward reference to a
15910 later parameter. */
15911 if (decl_die != NULL)
15913 add_AT_die_ref (subrange_die, bound_attr, decl_die);
15914 break;
15917 /* FALLTHRU */
15919 default:
15921 /* Otherwise try to create a stack operation procedure to
15922 evaluate the value of the array bound. */
15924 dw_die_ref ctx, decl_die;
15925 dw_loc_list_ref list;
15927 list = loc_list_from_tree (bound, 2);
15928 if (list == NULL || single_element_loc_list_p (list))
15930 /* If DW_AT_*bound is not a reference nor constant, it is
15931 a DWARF expression rather than location description.
15932 For that loc_list_from_tree (bound, 0) is needed.
15933 If that fails to give a single element list,
15934 fall back to outputting this as a reference anyway. */
15935 dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
15936 if (list2 && single_element_loc_list_p (list2))
15938 add_AT_loc (subrange_die, bound_attr, list2->expr);
15939 break;
15942 if (list == NULL)
15943 break;
15945 if (current_function_decl == 0)
15946 ctx = comp_unit_die ();
15947 else
15948 ctx = lookup_decl_die (current_function_decl);
15950 decl_die = new_die (DW_TAG_variable, ctx, bound);
15951 add_AT_flag (decl_die, DW_AT_artificial, 1);
15952 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
15953 add_AT_location_description (decl_die, DW_AT_location, list);
15954 add_AT_die_ref (subrange_die, bound_attr, decl_die);
15955 break;
15960 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
15961 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
15962 Note that the block of subscript information for an array type also
15963 includes information about the element type of the given array type. */
15965 static void
15966 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
15968 unsigned dimension_number;
15969 tree lower, upper;
15970 dw_die_ref subrange_die;
15972 for (dimension_number = 0;
15973 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
15974 type = TREE_TYPE (type), dimension_number++)
15976 tree domain = TYPE_DOMAIN (type);
15978 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
15979 break;
15981 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
15982 and (in GNU C only) variable bounds. Handle all three forms
15983 here. */
15984 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
15986 if (use_upc_dwarf2_extensions && TYPE_HAS_THREADS_FACTOR (type))
15988 add_AT_flag (subrange_die, DW_AT_upc_threads_scaled, 1);
15991 if (domain)
15993 /* We have an array type with specified bounds. */
15994 lower = TYPE_MIN_VALUE (domain);
15995 upper = TYPE_MAX_VALUE (domain);
15997 /* Define the index type. */
15998 if (TREE_TYPE (domain))
16000 /* ??? This is probably an Ada unnamed subrange type. Ignore the
16001 TREE_TYPE field. We can't emit debug info for this
16002 because it is an unnamed integral type. */
16003 if (TREE_CODE (domain) == INTEGER_TYPE
16004 && TYPE_NAME (domain) == NULL_TREE
16005 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16006 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16008 else
16009 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
16010 type_die);
16013 /* ??? If upper is NULL, the array has unspecified length,
16014 but it does have a lower bound. This happens with Fortran
16015 dimension arr(N:*)
16016 Since the debugger is definitely going to need to know N
16017 to produce useful results, go ahead and output the lower
16018 bound solo, and hope the debugger can cope. */
16020 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
16021 if (upper)
16022 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
16025 /* Otherwise we have an array type with an unspecified length. The
16026 DWARF-2 spec does not say how to handle this; let's just leave out the
16027 bounds. */
16031 static void
16032 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16034 dw_die_ref decl_die;
16035 unsigned size;
16037 switch (TREE_CODE (tree_node))
16039 case ERROR_MARK:
16040 size = 0;
16041 break;
16042 case ENUMERAL_TYPE:
16043 case RECORD_TYPE:
16044 case UNION_TYPE:
16045 case QUAL_UNION_TYPE:
16046 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
16047 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
16049 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
16050 return;
16052 size = int_size_in_bytes (tree_node);
16053 break;
16054 case FIELD_DECL:
16055 /* For a data member of a struct or union, the DW_AT_byte_size is
16056 generally given as the number of bytes normally allocated for an
16057 object of the *declared* type of the member itself. This is true
16058 even for bit-fields. */
16059 size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
16060 break;
16061 default:
16062 gcc_unreachable ();
16065 /* Note that `size' might be -1 when we get to this point. If it is, that
16066 indicates that the byte size of the entity in question is variable. We
16067 have no good way of expressing this fact in Dwarf at the present time,
16068 so just let the -1 pass on through. */
16069 add_AT_unsigned (die, DW_AT_byte_size, size);
16072 /* For a FIELD_DECL node which represents a bit-field, output an attribute
16073 which specifies the distance in bits from the highest order bit of the
16074 "containing object" for the bit-field to the highest order bit of the
16075 bit-field itself.
16077 For any given bit-field, the "containing object" is a hypothetical object
16078 (of some integral or enum type) within which the given bit-field lives. The
16079 type of this hypothetical "containing object" is always the same as the
16080 declared type of the individual bit-field itself. The determination of the
16081 exact location of the "containing object" for a bit-field is rather
16082 complicated. It's handled by the `field_byte_offset' function (above).
16084 Note that it is the size (in bytes) of the hypothetical "containing object"
16085 which will be given in the DW_AT_byte_size attribute for this bit-field.
16086 (See `byte_size_attribute' above). */
16088 static inline void
16089 add_bit_offset_attribute (dw_die_ref die, tree decl)
16091 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
16092 tree type = DECL_BIT_FIELD_TYPE (decl);
16093 HOST_WIDE_INT bitpos_int;
16094 HOST_WIDE_INT highest_order_object_bit_offset;
16095 HOST_WIDE_INT highest_order_field_bit_offset;
16096 HOST_WIDE_INT bit_offset;
16098 /* Must be a field and a bit field. */
16099 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
16101 /* We can't yet handle bit-fields whose offsets are variable, so if we
16102 encounter such things, just return without generating any attribute
16103 whatsoever. Likewise for variable or too large size. */
16104 if (! host_integerp (bit_position (decl), 0)
16105 || ! host_integerp (DECL_SIZE (decl), 1))
16106 return;
16108 bitpos_int = int_bit_position (decl);
16110 /* Note that the bit offset is always the distance (in bits) from the
16111 highest-order bit of the "containing object" to the highest-order bit of
16112 the bit-field itself. Since the "high-order end" of any object or field
16113 is different on big-endian and little-endian machines, the computation
16114 below must take account of these differences. */
16115 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
16116 highest_order_field_bit_offset = bitpos_int;
16118 if (! BYTES_BIG_ENDIAN)
16120 highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
16121 highest_order_object_bit_offset += simple_type_size_in_bits (type);
16124 bit_offset
16125 = (! BYTES_BIG_ENDIAN
16126 ? highest_order_object_bit_offset - highest_order_field_bit_offset
16127 : highest_order_field_bit_offset - highest_order_object_bit_offset);
16129 if (bit_offset < 0)
16130 add_AT_int (die, DW_AT_bit_offset, bit_offset);
16131 else
16132 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
16135 /* For a FIELD_DECL node which represents a bit field, output an attribute
16136 which specifies the length in bits of the given field. */
16138 static inline void
16139 add_bit_size_attribute (dw_die_ref die, tree decl)
16141 /* Must be a field and a bit field. */
16142 gcc_assert (TREE_CODE (decl) == FIELD_DECL
16143 && DECL_BIT_FIELD_TYPE (decl));
16145 if (host_integerp (DECL_SIZE (decl), 1))
16146 add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
16149 /* If the compiled language is ANSI C, then add a 'prototyped'
16150 attribute, if arg types are given for the parameters of a function. */
16152 static inline void
16153 add_prototyped_attribute (dw_die_ref die, tree func_type)
16155 if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
16156 && prototype_p (func_type))
16157 add_AT_flag (die, DW_AT_prototyped, 1);
16160 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
16161 by looking in either the type declaration or object declaration
16162 equate table. */
16164 static inline dw_die_ref
16165 add_abstract_origin_attribute (dw_die_ref die, tree origin)
16167 dw_die_ref origin_die = NULL;
16169 if (TREE_CODE (origin) != FUNCTION_DECL)
16171 /* We may have gotten separated from the block for the inlined
16172 function, if we're in an exception handler or some such; make
16173 sure that the abstract function has been written out.
16175 Doing this for nested functions is wrong, however; functions are
16176 distinct units, and our context might not even be inline. */
16177 tree fn = origin;
16179 if (TYPE_P (fn))
16180 fn = TYPE_STUB_DECL (fn);
16182 fn = decl_function_context (fn);
16183 if (fn)
16184 dwarf2out_abstract_function (fn);
16187 if (DECL_P (origin))
16188 origin_die = lookup_decl_die (origin);
16189 else if (TYPE_P (origin))
16190 origin_die = lookup_type_die (origin);
16192 /* XXX: Functions that are never lowered don't always have correct block
16193 trees (in the case of java, they simply have no block tree, in some other
16194 languages). For these functions, there is nothing we can really do to
16195 output correct debug info for inlined functions in all cases. Rather
16196 than die, we'll just produce deficient debug info now, in that we will
16197 have variables without a proper abstract origin. In the future, when all
16198 functions are lowered, we should re-add a gcc_assert (origin_die)
16199 here. */
16201 if (origin_die)
16202 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
16203 return origin_die;
16206 /* We do not currently support the pure_virtual attribute. */
16208 static inline void
16209 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
16211 if (DECL_VINDEX (func_decl))
16213 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
16215 if (host_integerp (DECL_VINDEX (func_decl), 0))
16216 add_AT_loc (die, DW_AT_vtable_elem_location,
16217 new_loc_descr (DW_OP_constu,
16218 tree_low_cst (DECL_VINDEX (func_decl), 0),
16219 0));
16221 /* GNU extension: Record what type this method came from originally. */
16222 if (debug_info_level > DINFO_LEVEL_TERSE
16223 && DECL_CONTEXT (func_decl))
16224 add_AT_die_ref (die, DW_AT_containing_type,
16225 lookup_type_die (DECL_CONTEXT (func_decl)));
16229 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
16230 given decl. This used to be a vendor extension until after DWARF 4
16231 standardized it. */
16233 static void
16234 add_linkage_attr (dw_die_ref die, tree decl)
16236 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
16238 /* Mimic what assemble_name_raw does with a leading '*'. */
16239 if (name[0] == '*')
16240 name = &name[1];
16242 if (dwarf_version >= 4)
16243 add_AT_string (die, DW_AT_linkage_name, name);
16244 else
16245 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
16248 /* Add source coordinate attributes for the given decl. */
16250 static void
16251 add_src_coords_attributes (dw_die_ref die, tree decl)
16253 expanded_location s;
16255 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
16256 return;
16257 s = expand_location (DECL_SOURCE_LOCATION (decl));
16258 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
16259 add_AT_unsigned (die, DW_AT_decl_line, s.line);
16262 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
16264 static void
16265 add_linkage_name (dw_die_ref die, tree decl)
16267 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
16268 && TREE_PUBLIC (decl)
16269 && !DECL_ABSTRACT (decl)
16270 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
16271 && die->die_tag != DW_TAG_member)
16273 /* Defer until we have an assembler name set. */
16274 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
16276 limbo_die_node *asm_name;
16278 asm_name = ggc_alloc_cleared_limbo_die_node ();
16279 asm_name->die = die;
16280 asm_name->created_for = decl;
16281 asm_name->next = deferred_asm_name;
16282 deferred_asm_name = asm_name;
16284 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
16285 add_linkage_attr (die, decl);
16289 /* Add a DW_AT_name attribute and source coordinate attribute for the
16290 given decl, but only if it actually has a name. */
16292 static void
16293 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
16295 tree decl_name;
16297 decl_name = DECL_NAME (decl);
16298 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
16300 const char *name = dwarf2_name (decl, 0);
16301 if (name)
16302 add_name_attribute (die, name);
16303 if (! DECL_ARTIFICIAL (decl))
16304 add_src_coords_attributes (die, decl);
16306 add_linkage_name (die, decl);
16309 #ifdef VMS_DEBUGGING_INFO
16310 /* Get the function's name, as described by its RTL. This may be different
16311 from the DECL_NAME name used in the source file. */
16312 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
16314 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
16315 XEXP (DECL_RTL (decl), 0), false);
16316 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
16318 #endif /* VMS_DEBUGGING_INFO */
16321 #ifdef VMS_DEBUGGING_INFO
16322 /* Output the debug main pointer die for VMS */
16324 void
16325 dwarf2out_vms_debug_main_pointer (void)
16327 char label[MAX_ARTIFICIAL_LABEL_BYTES];
16328 dw_die_ref die;
16330 /* Allocate the VMS debug main subprogram die. */
16331 die = ggc_alloc_cleared_die_node ();
16332 die->die_tag = DW_TAG_subprogram;
16333 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
16334 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
16335 current_function_funcdef_no);
16336 add_AT_lbl_id (die, DW_AT_entry_pc, label);
16338 /* Make it the first child of comp_unit_die (). */
16339 die->die_parent = comp_unit_die ();
16340 if (comp_unit_die ()->die_child)
16342 die->die_sib = comp_unit_die ()->die_child->die_sib;
16343 comp_unit_die ()->die_child->die_sib = die;
16345 else
16347 die->die_sib = die;
16348 comp_unit_die ()->die_child = die;
16351 #endif /* VMS_DEBUGGING_INFO */
16353 /* Push a new declaration scope. */
16355 static void
16356 push_decl_scope (tree scope)
16358 vec_safe_push (decl_scope_table, scope);
16361 /* Pop a declaration scope. */
16363 static inline void
16364 pop_decl_scope (void)
16366 decl_scope_table->pop ();
16369 /* walk_tree helper function for uses_local_type, below. */
16371 static tree
16372 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
16374 if (!TYPE_P (*tp))
16375 *walk_subtrees = 0;
16376 else
16378 tree name = TYPE_NAME (*tp);
16379 if (name && DECL_P (name) && decl_function_context (name))
16380 return *tp;
16382 return NULL_TREE;
16385 /* If TYPE involves a function-local type (including a local typedef to a
16386 non-local type), returns that type; otherwise returns NULL_TREE. */
16388 static tree
16389 uses_local_type (tree type)
16391 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
16392 return used;
16395 /* Return the DIE for the scope that immediately contains this type.
16396 Non-named types that do not involve a function-local type get global
16397 scope. Named types nested in namespaces or other types get their
16398 containing scope. All other types (i.e. function-local named types) get
16399 the current active scope. */
16401 static dw_die_ref
16402 scope_die_for (tree t, dw_die_ref context_die)
16404 dw_die_ref scope_die = NULL;
16405 tree containing_scope;
16407 /* Non-types always go in the current scope. */
16408 gcc_assert (TYPE_P (t));
16410 /* Use the scope of the typedef, rather than the scope of the type
16411 it refers to. */
16412 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
16413 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
16414 else
16415 containing_scope = TYPE_CONTEXT (t);
16417 /* Use the containing namespace if there is one. */
16418 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
16420 if (context_die == lookup_decl_die (containing_scope))
16421 /* OK */;
16422 else if (debug_info_level > DINFO_LEVEL_TERSE)
16423 context_die = get_context_die (containing_scope);
16424 else
16425 containing_scope = NULL_TREE;
16428 /* Ignore function type "scopes" from the C frontend. They mean that
16429 a tagged type is local to a parmlist of a function declarator, but
16430 that isn't useful to DWARF. */
16431 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
16432 containing_scope = NULL_TREE;
16434 if (SCOPE_FILE_SCOPE_P (containing_scope))
16436 /* If T uses a local type keep it local as well, to avoid references
16437 to function-local DIEs from outside the function. */
16438 if (current_function_decl && uses_local_type (t))
16439 scope_die = context_die;
16440 else
16441 scope_die = comp_unit_die ();
16443 else if (TYPE_P (containing_scope))
16445 /* For types, we can just look up the appropriate DIE. */
16446 if (debug_info_level > DINFO_LEVEL_TERSE)
16447 scope_die = get_context_die (containing_scope);
16448 else
16450 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
16451 if (scope_die == NULL)
16452 scope_die = comp_unit_die ();
16455 else
16456 scope_die = context_die;
16458 return scope_die;
16461 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
16463 static inline int
16464 local_scope_p (dw_die_ref context_die)
16466 for (; context_die; context_die = context_die->die_parent)
16467 if (context_die->die_tag == DW_TAG_inlined_subroutine
16468 || context_die->die_tag == DW_TAG_subprogram)
16469 return 1;
16471 return 0;
16474 /* Returns nonzero if CONTEXT_DIE is a class. */
16476 static inline int
16477 class_scope_p (dw_die_ref context_die)
16479 return (context_die
16480 && (context_die->die_tag == DW_TAG_structure_type
16481 || context_die->die_tag == DW_TAG_class_type
16482 || context_die->die_tag == DW_TAG_interface_type
16483 || context_die->die_tag == DW_TAG_union_type));
16486 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
16487 whether or not to treat a DIE in this context as a declaration. */
16489 static inline int
16490 class_or_namespace_scope_p (dw_die_ref context_die)
16492 return (class_scope_p (context_die)
16493 || (context_die && context_die->die_tag == DW_TAG_namespace));
16496 /* Many forms of DIEs require a "type description" attribute. This
16497 routine locates the proper "type descriptor" die for the type given
16498 by 'type', and adds a DW_AT_type attribute below the given die. */
16500 static void
16501 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
16502 int decl_volatile, dw_die_ref context_die)
16504 enum tree_code code = TREE_CODE (type);
16505 dw_die_ref type_die = NULL;
16506 int type_quals;
16508 /* ??? If this type is an unnamed subrange type of an integral, floating-point
16509 or fixed-point type, use the inner type. This is because we have no
16510 support for unnamed types in base_type_die. This can happen if this is
16511 an Ada subrange type. Correct solution is emit a subrange type die. */
16512 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
16513 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
16514 type = TREE_TYPE (type), code = TREE_CODE (type);
16516 if (code == ERROR_MARK
16517 /* Handle a special case. For functions whose return type is void, we
16518 generate *no* type attribute. (Note that no object may have type
16519 `void', so this only applies to function return types). */
16520 || code == VOID_TYPE)
16521 return;
16523 type_quals = TYPE_QUALS (type)
16524 | (decl_const * TYPE_QUAL_CONST)
16525 | (decl_volatile * TYPE_QUAL_VOLATILE);
16526 type_die = modified_type_die (type, type_quals, context_die);
16528 if (type_die != NULL)
16529 add_AT_die_ref (object_die, DW_AT_type, type_die);
16532 /* Given an object die, add the calling convention attribute for the
16533 function call type. */
16534 static void
16535 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
16537 enum dwarf_calling_convention value = DW_CC_normal;
16539 value = ((enum dwarf_calling_convention)
16540 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
16542 if (is_fortran ()
16543 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
16545 /* DWARF 2 doesn't provide a way to identify a program's source-level
16546 entry point. DW_AT_calling_convention attributes are only meant
16547 to describe functions' calling conventions. However, lacking a
16548 better way to signal the Fortran main program, we used this for
16549 a long time, following existing custom. Now, DWARF 4 has
16550 DW_AT_main_subprogram, which we add below, but some tools still
16551 rely on the old way, which we thus keep. */
16552 value = DW_CC_program;
16554 if (dwarf_version >= 4 || !dwarf_strict)
16555 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
16558 /* Only add the attribute if the backend requests it, and
16559 is not DW_CC_normal. */
16560 if (value && (value != DW_CC_normal))
16561 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
16564 /* Given a tree pointer to a struct, class, union, or enum type node, return
16565 a pointer to the (string) tag name for the given type, or zero if the type
16566 was declared without a tag. */
16568 static const char *
16569 type_tag (const_tree type)
16571 const char *name = 0;
16573 if (TYPE_NAME (type) != 0)
16575 tree t = 0;
16577 /* Find the IDENTIFIER_NODE for the type name. */
16578 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
16579 && !TYPE_NAMELESS (type))
16580 t = TYPE_NAME (type);
16582 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
16583 a TYPE_DECL node, regardless of whether or not a `typedef' was
16584 involved. */
16585 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
16586 && ! DECL_IGNORED_P (TYPE_NAME (type)))
16588 /* We want to be extra verbose. Don't call dwarf_name if
16589 DECL_NAME isn't set. The default hook for decl_printable_name
16590 doesn't like that, and in this context it's correct to return
16591 0, instead of "<anonymous>" or the like. */
16592 if (DECL_NAME (TYPE_NAME (type))
16593 && !DECL_NAMELESS (TYPE_NAME (type)))
16594 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
16597 /* Now get the name as a string, or invent one. */
16598 if (!name && t != 0)
16599 name = IDENTIFIER_POINTER (t);
16602 return (name == 0 || *name == '\0') ? 0 : name;
16605 /* Return the type associated with a data member, make a special check
16606 for bit field types. */
16608 static inline tree
16609 member_declared_type (const_tree member)
16611 return (DECL_BIT_FIELD_TYPE (member)
16612 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
16615 /* Get the decl's label, as described by its RTL. This may be different
16616 from the DECL_NAME name used in the source file. */
16618 #if 0
16619 static const char *
16620 decl_start_label (tree decl)
16622 rtx x;
16623 const char *fnname;
16625 x = DECL_RTL (decl);
16626 gcc_assert (MEM_P (x));
16628 x = XEXP (x, 0);
16629 gcc_assert (GET_CODE (x) == SYMBOL_REF);
16631 fnname = XSTR (x, 0);
16632 return fnname;
16634 #endif
16636 /* These routines generate the internal representation of the DIE's for
16637 the compilation unit. Debugging information is collected by walking
16638 the declaration trees passed in from dwarf2out_decl(). */
16640 static void
16641 gen_array_type_die (tree type, dw_die_ref context_die)
16643 dw_die_ref scope_die = scope_die_for (type, context_die);
16644 dw_die_ref array_die;
16646 /* GNU compilers represent multidimensional array types as sequences of one
16647 dimensional array types whose element types are themselves array types.
16648 We sometimes squish that down to a single array_type DIE with multiple
16649 subscripts in the Dwarf debugging info. The draft Dwarf specification
16650 say that we are allowed to do this kind of compression in C, because
16651 there is no difference between an array of arrays and a multidimensional
16652 array. We don't do this for Ada to remain as close as possible to the
16653 actual representation, which is especially important against the language
16654 flexibilty wrt arrays of variable size. */
16656 bool collapse_nested_arrays = !is_ada ();
16657 tree element_type;
16659 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
16660 DW_TAG_string_type doesn't have DW_AT_type attribute). */
16661 if (TYPE_STRING_FLAG (type)
16662 && TREE_CODE (type) == ARRAY_TYPE
16663 && is_fortran ()
16664 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
16666 HOST_WIDE_INT size;
16668 array_die = new_die (DW_TAG_string_type, scope_die, type);
16669 add_name_attribute (array_die, type_tag (type));
16670 equate_type_number_to_die (type, array_die);
16671 size = int_size_in_bytes (type);
16672 if (size >= 0)
16673 add_AT_unsigned (array_die, DW_AT_byte_size, size);
16674 else if (TYPE_DOMAIN (type) != NULL_TREE
16675 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
16676 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
16678 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
16679 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
16681 size = int_size_in_bytes (TREE_TYPE (szdecl));
16682 if (loc && size > 0)
16684 add_AT_location_description (array_die, DW_AT_string_length, loc);
16685 if (size != DWARF2_ADDR_SIZE)
16686 add_AT_unsigned (array_die, DW_AT_byte_size, size);
16689 return;
16692 array_die = new_die (DW_TAG_array_type, scope_die, type);
16693 add_name_attribute (array_die, type_tag (type));
16694 equate_type_number_to_die (type, array_die);
16696 if (TREE_CODE (type) == VECTOR_TYPE)
16697 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
16699 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
16700 if (is_fortran ()
16701 && TREE_CODE (type) == ARRAY_TYPE
16702 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
16703 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
16704 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16706 #if 0
16707 /* We default the array ordering. SDB will probably do
16708 the right things even if DW_AT_ordering is not present. It's not even
16709 an issue until we start to get into multidimensional arrays anyway. If
16710 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
16711 then we'll have to put the DW_AT_ordering attribute back in. (But if
16712 and when we find out that we need to put these in, we will only do so
16713 for multidimensional arrays. */
16714 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
16715 #endif
16717 if (TREE_CODE (type) == VECTOR_TYPE)
16719 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
16720 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
16721 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
16722 add_bound_info (subrange_die, DW_AT_upper_bound,
16723 size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
16725 else
16726 add_subscript_info (array_die, type, collapse_nested_arrays);
16728 /* Add representation of the type of the elements of this array type and
16729 emit the corresponding DIE if we haven't done it already. */
16730 element_type = TREE_TYPE (type);
16731 if (collapse_nested_arrays)
16732 while (TREE_CODE (element_type) == ARRAY_TYPE)
16734 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
16735 break;
16736 element_type = TREE_TYPE (element_type);
16739 add_type_attribute (array_die, element_type, 0, 0, context_die);
16741 add_gnat_descriptive_type_attribute (array_die, type, context_die);
16742 if (TYPE_ARTIFICIAL (type))
16743 add_AT_flag (array_die, DW_AT_artificial, 1);
16745 if (get_AT (array_die, DW_AT_name))
16746 add_pubtype (type, array_die);
16749 static dw_loc_descr_ref
16750 descr_info_loc (tree val, tree base_decl)
16752 HOST_WIDE_INT size;
16753 dw_loc_descr_ref loc, loc2;
16754 enum dwarf_location_atom op;
16756 if (val == base_decl)
16757 return new_loc_descr (DW_OP_push_object_address, 0, 0);
16759 switch (TREE_CODE (val))
16761 CASE_CONVERT:
16762 return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16763 case VAR_DECL:
16764 return loc_descriptor_from_tree (val, 0);
16765 case INTEGER_CST:
16766 if (host_integerp (val, 0))
16767 return int_loc_descriptor (tree_low_cst (val, 0));
16768 break;
16769 case INDIRECT_REF:
16770 size = int_size_in_bytes (TREE_TYPE (val));
16771 if (size < 0)
16772 break;
16773 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16774 if (!loc)
16775 break;
16776 if (size == DWARF2_ADDR_SIZE)
16777 add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
16778 else
16779 add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
16780 return loc;
16781 case POINTER_PLUS_EXPR:
16782 case PLUS_EXPR:
16783 if (host_integerp (TREE_OPERAND (val, 1), 1)
16784 && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
16785 < 16384)
16787 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16788 if (!loc)
16789 break;
16790 loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
16792 else
16794 op = DW_OP_plus;
16795 do_binop:
16796 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16797 if (!loc)
16798 break;
16799 loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
16800 if (!loc2)
16801 break;
16802 add_loc_descr (&loc, loc2);
16803 add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
16805 return loc;
16806 case MINUS_EXPR:
16807 op = DW_OP_minus;
16808 goto do_binop;
16809 case MULT_EXPR:
16810 op = DW_OP_mul;
16811 goto do_binop;
16812 case EQ_EXPR:
16813 op = DW_OP_eq;
16814 goto do_binop;
16815 case NE_EXPR:
16816 op = DW_OP_ne;
16817 goto do_binop;
16818 default:
16819 break;
16821 return NULL;
16824 static void
16825 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
16826 tree val, tree base_decl)
16828 dw_loc_descr_ref loc;
16830 if (host_integerp (val, 0))
16832 add_AT_unsigned (die, attr, tree_low_cst (val, 0));
16833 return;
16836 loc = descr_info_loc (val, base_decl);
16837 if (!loc)
16838 return;
16840 add_AT_loc (die, attr, loc);
16843 /* This routine generates DIE for array with hidden descriptor, details
16844 are filled into *info by a langhook. */
16846 static void
16847 gen_descr_array_type_die (tree type, struct array_descr_info *info,
16848 dw_die_ref context_die)
16850 dw_die_ref scope_die = scope_die_for (type, context_die);
16851 dw_die_ref array_die;
16852 int dim;
16854 array_die = new_die (DW_TAG_array_type, scope_die, type);
16855 add_name_attribute (array_die, type_tag (type));
16856 equate_type_number_to_die (type, array_die);
16858 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
16859 if (is_fortran ()
16860 && info->ndimensions >= 2)
16861 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16863 if (info->data_location)
16864 add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
16865 info->base_decl);
16866 if (info->associated)
16867 add_descr_info_field (array_die, DW_AT_associated, info->associated,
16868 info->base_decl);
16869 if (info->allocated)
16870 add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
16871 info->base_decl);
16873 for (dim = 0; dim < info->ndimensions; dim++)
16875 dw_die_ref subrange_die
16876 = new_die (DW_TAG_subrange_type, array_die, NULL);
16878 if (info->dimen[dim].lower_bound)
16880 /* If it is the default value, omit it. */
16881 int dflt;
16883 if (host_integerp (info->dimen[dim].lower_bound, 0)
16884 && (dflt = lower_bound_default ()) != -1
16885 && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
16887 else
16888 add_descr_info_field (subrange_die, DW_AT_lower_bound,
16889 info->dimen[dim].lower_bound,
16890 info->base_decl);
16892 if (info->dimen[dim].upper_bound)
16893 add_descr_info_field (subrange_die, DW_AT_upper_bound,
16894 info->dimen[dim].upper_bound,
16895 info->base_decl);
16896 if (info->dimen[dim].stride)
16897 add_descr_info_field (subrange_die, DW_AT_byte_stride,
16898 info->dimen[dim].stride,
16899 info->base_decl);
16902 gen_type_die (info->element_type, context_die);
16903 add_type_attribute (array_die, info->element_type, 0, 0, context_die);
16905 if (get_AT (array_die, DW_AT_name))
16906 add_pubtype (type, array_die);
16909 #if 0
16910 static void
16911 gen_entry_point_die (tree decl, dw_die_ref context_die)
16913 tree origin = decl_ultimate_origin (decl);
16914 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
16916 if (origin != NULL)
16917 add_abstract_origin_attribute (decl_die, origin);
16918 else
16920 add_name_and_src_coords_attributes (decl_die, decl);
16921 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
16922 0, 0, context_die);
16925 if (DECL_ABSTRACT (decl))
16926 equate_decl_number_to_die (decl, decl_die);
16927 else
16928 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
16930 #endif
16932 /* Walk through the list of incomplete types again, trying once more to
16933 emit full debugging info for them. */
16935 static void
16936 retry_incomplete_types (void)
16938 int i;
16940 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
16941 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
16942 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
16945 /* Determine what tag to use for a record type. */
16947 static enum dwarf_tag
16948 record_type_tag (tree type)
16950 if (! lang_hooks.types.classify_record)
16951 return DW_TAG_structure_type;
16953 switch (lang_hooks.types.classify_record (type))
16955 case RECORD_IS_STRUCT:
16956 return DW_TAG_structure_type;
16958 case RECORD_IS_CLASS:
16959 return DW_TAG_class_type;
16961 case RECORD_IS_INTERFACE:
16962 if (dwarf_version >= 3 || !dwarf_strict)
16963 return DW_TAG_interface_type;
16964 return DW_TAG_structure_type;
16966 default:
16967 gcc_unreachable ();
16971 /* Generate a DIE to represent an enumeration type. Note that these DIEs
16972 include all of the information about the enumeration values also. Each
16973 enumerated type name/value is listed as a child of the enumerated type
16974 DIE. */
16976 static dw_die_ref
16977 gen_enumeration_type_die (tree type, dw_die_ref context_die)
16979 dw_die_ref type_die = lookup_type_die (type);
16981 if (type_die == NULL)
16983 type_die = new_die (DW_TAG_enumeration_type,
16984 scope_die_for (type, context_die), type);
16985 equate_type_number_to_die (type, type_die);
16986 add_name_attribute (type_die, type_tag (type));
16987 if (dwarf_version >= 4 || !dwarf_strict)
16989 if (ENUM_IS_SCOPED (type))
16990 add_AT_flag (type_die, DW_AT_enum_class, 1);
16991 if (ENUM_IS_OPAQUE (type))
16992 add_AT_flag (type_die, DW_AT_declaration, 1);
16995 else if (! TYPE_SIZE (type))
16996 return type_die;
16997 else
16998 remove_AT (type_die, DW_AT_declaration);
17000 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
17001 given enum type is incomplete, do not generate the DW_AT_byte_size
17002 attribute or the DW_AT_element_list attribute. */
17003 if (TYPE_SIZE (type))
17005 tree link;
17007 TREE_ASM_WRITTEN (type) = 1;
17008 add_byte_size_attribute (type_die, type);
17009 if (TYPE_STUB_DECL (type) != NULL_TREE)
17011 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17012 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17015 /* If the first reference to this type was as the return type of an
17016 inline function, then it may not have a parent. Fix this now. */
17017 if (type_die->die_parent == NULL)
17018 add_child_die (scope_die_for (type, context_die), type_die);
17020 for (link = TYPE_VALUES (type);
17021 link != NULL; link = TREE_CHAIN (link))
17023 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17024 tree value = TREE_VALUE (link);
17026 add_name_attribute (enum_die,
17027 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17029 if (TREE_CODE (value) == CONST_DECL)
17030 value = DECL_INITIAL (value);
17032 if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
17033 /* DWARF2 does not provide a way of indicating whether or
17034 not enumeration constants are signed or unsigned. GDB
17035 always assumes the values are signed, so we output all
17036 values as if they were signed. That means that
17037 enumeration constants with very large unsigned values
17038 will appear to have negative values in the debugger. */
17039 add_AT_int (enum_die, DW_AT_const_value,
17040 tree_low_cst (value, tree_int_cst_sgn (value) > 0));
17043 add_gnat_descriptive_type_attribute (type_die, type, context_die);
17044 if (TYPE_ARTIFICIAL (type))
17045 add_AT_flag (type_die, DW_AT_artificial, 1);
17047 else
17048 add_AT_flag (type_die, DW_AT_declaration, 1);
17050 add_pubtype (type, type_die);
17052 return type_die;
17055 /* Generate a DIE to represent either a real live formal parameter decl or to
17056 represent just the type of some formal parameter position in some function
17057 type.
17059 Note that this routine is a bit unusual because its argument may be a
17060 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17061 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17062 node. If it's the former then this function is being called to output a
17063 DIE to represent a formal parameter object (or some inlining thereof). If
17064 it's the latter, then this function is only being called to output a
17065 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17066 argument type of some subprogram type.
17067 If EMIT_NAME_P is true, name and source coordinate attributes
17068 are emitted. */
17070 static dw_die_ref
17071 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17072 dw_die_ref context_die)
17074 tree node_or_origin = node ? node : origin;
17075 tree ultimate_origin;
17076 dw_die_ref parm_die
17077 = new_die (DW_TAG_formal_parameter, context_die, node);
17079 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
17081 case tcc_declaration:
17082 ultimate_origin = decl_ultimate_origin (node_or_origin);
17083 if (node || ultimate_origin)
17084 origin = ultimate_origin;
17085 if (origin != NULL)
17086 add_abstract_origin_attribute (parm_die, origin);
17087 else if (emit_name_p)
17088 add_name_and_src_coords_attributes (parm_die, node);
17089 if (origin == NULL
17090 || (! DECL_ABSTRACT (node_or_origin)
17091 && variably_modified_type_p (TREE_TYPE (node_or_origin),
17092 decl_function_context
17093 (node_or_origin))))
17095 tree type = TREE_TYPE (node_or_origin);
17096 if (decl_by_reference_p (node_or_origin))
17097 add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
17098 context_die);
17099 else
17100 add_type_attribute (parm_die, type,
17101 TREE_READONLY (node_or_origin),
17102 TREE_THIS_VOLATILE (node_or_origin),
17103 context_die);
17105 if (origin == NULL && DECL_ARTIFICIAL (node))
17106 add_AT_flag (parm_die, DW_AT_artificial, 1);
17108 if (node && node != origin)
17109 equate_decl_number_to_die (node, parm_die);
17110 if (! DECL_ABSTRACT (node_or_origin))
17111 add_location_or_const_value_attribute (parm_die, node_or_origin,
17112 node == NULL, DW_AT_location);
17114 break;
17116 case tcc_type:
17117 /* We were called with some kind of a ..._TYPE node. */
17118 add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
17119 break;
17121 default:
17122 gcc_unreachable ();
17125 return parm_die;
17128 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
17129 children DW_TAG_formal_parameter DIEs representing the arguments of the
17130 parameter pack.
17132 PARM_PACK must be a function parameter pack.
17133 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
17134 must point to the subsequent arguments of the function PACK_ARG belongs to.
17135 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
17136 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
17137 following the last one for which a DIE was generated. */
17139 static dw_die_ref
17140 gen_formal_parameter_pack_die (tree parm_pack,
17141 tree pack_arg,
17142 dw_die_ref subr_die,
17143 tree *next_arg)
17145 tree arg;
17146 dw_die_ref parm_pack_die;
17148 gcc_assert (parm_pack
17149 && lang_hooks.function_parameter_pack_p (parm_pack)
17150 && subr_die);
17152 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
17153 add_src_coords_attributes (parm_pack_die, parm_pack);
17155 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
17157 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
17158 parm_pack))
17159 break;
17160 gen_formal_parameter_die (arg, NULL,
17161 false /* Don't emit name attribute. */,
17162 parm_pack_die);
17164 if (next_arg)
17165 *next_arg = arg;
17166 return parm_pack_die;
17169 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
17170 at the end of an (ANSI prototyped) formal parameters list. */
17172 static void
17173 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
17175 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
17178 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
17179 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
17180 parameters as specified in some function type specification (except for
17181 those which appear as part of a function *definition*). */
17183 static void
17184 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
17186 tree link;
17187 tree formal_type = NULL;
17188 tree first_parm_type;
17189 tree arg;
17191 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
17193 arg = DECL_ARGUMENTS (function_or_method_type);
17194 function_or_method_type = TREE_TYPE (function_or_method_type);
17196 else
17197 arg = NULL_TREE;
17199 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
17201 /* Make our first pass over the list of formal parameter types and output a
17202 DW_TAG_formal_parameter DIE for each one. */
17203 for (link = first_parm_type; link; )
17205 dw_die_ref parm_die;
17207 formal_type = TREE_VALUE (link);
17208 if (formal_type == void_type_node)
17209 break;
17211 /* Output a (nameless) DIE to represent the formal parameter itself. */
17212 parm_die = gen_formal_parameter_die (formal_type, NULL,
17213 true /* Emit name attribute. */,
17214 context_die);
17215 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
17216 && link == first_parm_type)
17218 add_AT_flag (parm_die, DW_AT_artificial, 1);
17219 if (dwarf_version >= 3 || !dwarf_strict)
17220 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
17222 else if (arg && DECL_ARTIFICIAL (arg))
17223 add_AT_flag (parm_die, DW_AT_artificial, 1);
17225 link = TREE_CHAIN (link);
17226 if (arg)
17227 arg = DECL_CHAIN (arg);
17230 /* If this function type has an ellipsis, add a
17231 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
17232 if (formal_type != void_type_node)
17233 gen_unspecified_parameters_die (function_or_method_type, context_die);
17235 /* Make our second (and final) pass over the list of formal parameter types
17236 and output DIEs to represent those types (as necessary). */
17237 for (link = TYPE_ARG_TYPES (function_or_method_type);
17238 link && TREE_VALUE (link);
17239 link = TREE_CHAIN (link))
17240 gen_type_die (TREE_VALUE (link), context_die);
17243 /* We want to generate the DIE for TYPE so that we can generate the
17244 die for MEMBER, which has been defined; we will need to refer back
17245 to the member declaration nested within TYPE. If we're trying to
17246 generate minimal debug info for TYPE, processing TYPE won't do the
17247 trick; we need to attach the member declaration by hand. */
17249 static void
17250 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
17252 gen_type_die (type, context_die);
17254 /* If we're trying to avoid duplicate debug info, we may not have
17255 emitted the member decl for this function. Emit it now. */
17256 if (TYPE_STUB_DECL (type)
17257 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
17258 && ! lookup_decl_die (member))
17260 dw_die_ref type_die;
17261 gcc_assert (!decl_ultimate_origin (member));
17263 push_decl_scope (type);
17264 type_die = lookup_type_die_strip_naming_typedef (type);
17265 if (TREE_CODE (member) == FUNCTION_DECL)
17266 gen_subprogram_die (member, type_die);
17267 else if (TREE_CODE (member) == FIELD_DECL)
17269 /* Ignore the nameless fields that are used to skip bits but handle
17270 C++ anonymous unions and structs. */
17271 if (DECL_NAME (member) != NULL_TREE
17272 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
17273 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
17275 gen_type_die (member_declared_type (member), type_die);
17276 gen_field_die (member, type_die);
17279 else
17280 gen_variable_die (member, NULL_TREE, type_die);
17282 pop_decl_scope ();
17286 /* Forward declare these functions, because they are mutually recursive
17287 with their set_block_* pairing functions. */
17288 static void set_decl_origin_self (tree);
17289 static void set_decl_abstract_flags (tree, int);
17291 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
17292 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
17293 that it points to the node itself, thus indicating that the node is its
17294 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
17295 the given node is NULL, recursively descend the decl/block tree which
17296 it is the root of, and for each other ..._DECL or BLOCK node contained
17297 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
17298 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
17299 values to point to themselves. */
17301 static void
17302 set_block_origin_self (tree stmt)
17304 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
17306 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
17309 tree local_decl;
17311 for (local_decl = BLOCK_VARS (stmt);
17312 local_decl != NULL_TREE;
17313 local_decl = DECL_CHAIN (local_decl))
17314 if (! DECL_EXTERNAL (local_decl))
17315 set_decl_origin_self (local_decl); /* Potential recursion. */
17319 tree subblock;
17321 for (subblock = BLOCK_SUBBLOCKS (stmt);
17322 subblock != NULL_TREE;
17323 subblock = BLOCK_CHAIN (subblock))
17324 set_block_origin_self (subblock); /* Recurse. */
17329 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
17330 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
17331 node to so that it points to the node itself, thus indicating that the
17332 node represents its own (abstract) origin. Additionally, if the
17333 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
17334 the decl/block tree of which the given node is the root of, and for
17335 each other ..._DECL or BLOCK node contained therein whose
17336 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
17337 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
17338 point to themselves. */
17340 static void
17341 set_decl_origin_self (tree decl)
17343 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
17345 DECL_ABSTRACT_ORIGIN (decl) = decl;
17346 if (TREE_CODE (decl) == FUNCTION_DECL)
17348 tree arg;
17350 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17351 DECL_ABSTRACT_ORIGIN (arg) = arg;
17352 if (DECL_INITIAL (decl) != NULL_TREE
17353 && DECL_INITIAL (decl) != error_mark_node)
17354 set_block_origin_self (DECL_INITIAL (decl));
17359 /* Given a pointer to some BLOCK node, and a boolean value to set the
17360 "abstract" flags to, set that value into the BLOCK_ABSTRACT flag for
17361 the given block, and for all local decls and all local sub-blocks
17362 (recursively) which are contained therein. */
17364 static void
17365 set_block_abstract_flags (tree stmt, int setting)
17367 tree local_decl;
17368 tree subblock;
17369 unsigned int i;
17371 BLOCK_ABSTRACT (stmt) = setting;
17373 for (local_decl = BLOCK_VARS (stmt);
17374 local_decl != NULL_TREE;
17375 local_decl = DECL_CHAIN (local_decl))
17376 if (! DECL_EXTERNAL (local_decl))
17377 set_decl_abstract_flags (local_decl, setting);
17379 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
17381 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
17382 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
17383 || TREE_CODE (local_decl) == PARM_DECL)
17384 set_decl_abstract_flags (local_decl, setting);
17387 for (subblock = BLOCK_SUBBLOCKS (stmt);
17388 subblock != NULL_TREE;
17389 subblock = BLOCK_CHAIN (subblock))
17390 set_block_abstract_flags (subblock, setting);
17393 /* Given a pointer to some ..._DECL node, and a boolean value to set the
17394 "abstract" flags to, set that value into the DECL_ABSTRACT flag for the
17395 given decl, and (in the case where the decl is a FUNCTION_DECL) also
17396 set the abstract flags for all of the parameters, local vars, local
17397 blocks and sub-blocks (recursively) to the same setting. */
17399 static void
17400 set_decl_abstract_flags (tree decl, int setting)
17402 DECL_ABSTRACT (decl) = setting;
17403 if (TREE_CODE (decl) == FUNCTION_DECL)
17405 tree arg;
17407 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17408 DECL_ABSTRACT (arg) = setting;
17409 if (DECL_INITIAL (decl) != NULL_TREE
17410 && DECL_INITIAL (decl) != error_mark_node)
17411 set_block_abstract_flags (DECL_INITIAL (decl), setting);
17415 /* Generate the DWARF2 info for the "abstract" instance of a function which we
17416 may later generate inlined and/or out-of-line instances of. */
17418 static void
17419 dwarf2out_abstract_function (tree decl)
17421 dw_die_ref old_die;
17422 tree save_fn;
17423 tree context;
17424 int was_abstract;
17425 htab_t old_decl_loc_table;
17426 htab_t old_cached_dw_loc_list_table;
17427 int old_call_site_count, old_tail_call_site_count;
17428 struct call_arg_loc_node *old_call_arg_locations;
17430 /* Make sure we have the actual abstract inline, not a clone. */
17431 decl = DECL_ORIGIN (decl);
17433 old_die = lookup_decl_die (decl);
17434 if (old_die && get_AT (old_die, DW_AT_inline))
17435 /* We've already generated the abstract instance. */
17436 return;
17438 /* We can be called while recursively when seeing block defining inlined subroutine
17439 DIE. Be sure to not clobber the outer location table nor use it or we would
17440 get locations in abstract instantces. */
17441 old_decl_loc_table = decl_loc_table;
17442 decl_loc_table = NULL;
17443 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
17444 cached_dw_loc_list_table = NULL;
17445 old_call_arg_locations = call_arg_locations;
17446 call_arg_locations = NULL;
17447 old_call_site_count = call_site_count;
17448 call_site_count = -1;
17449 old_tail_call_site_count = tail_call_site_count;
17450 tail_call_site_count = -1;
17452 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
17453 we don't get confused by DECL_ABSTRACT. */
17454 if (debug_info_level > DINFO_LEVEL_TERSE)
17456 context = decl_class_context (decl);
17457 if (context)
17458 gen_type_die_for_member
17459 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
17462 /* Pretend we've just finished compiling this function. */
17463 save_fn = current_function_decl;
17464 current_function_decl = decl;
17466 was_abstract = DECL_ABSTRACT (decl);
17467 set_decl_abstract_flags (decl, 1);
17468 dwarf2out_decl (decl);
17469 if (! was_abstract)
17470 set_decl_abstract_flags (decl, 0);
17472 current_function_decl = save_fn;
17473 decl_loc_table = old_decl_loc_table;
17474 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
17475 call_arg_locations = old_call_arg_locations;
17476 call_site_count = old_call_site_count;
17477 tail_call_site_count = old_tail_call_site_count;
17480 /* Helper function of premark_used_types() which gets called through
17481 htab_traverse.
17483 Marks the DIE of a given type in *SLOT as perennial, so it never gets
17484 marked as unused by prune_unused_types. */
17486 static int
17487 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
17489 tree type;
17490 dw_die_ref die;
17492 type = (tree) *slot;
17493 die = lookup_type_die (type);
17494 if (die != NULL)
17495 die->die_perennial_p = 1;
17496 return 1;
17499 /* Helper function of premark_types_used_by_global_vars which gets called
17500 through htab_traverse.
17502 Marks the DIE of a given type in *SLOT as perennial, so it never gets
17503 marked as unused by prune_unused_types. The DIE of the type is marked
17504 only if the global variable using the type will actually be emitted. */
17506 static int
17507 premark_types_used_by_global_vars_helper (void **slot,
17508 void *data ATTRIBUTE_UNUSED)
17510 struct types_used_by_vars_entry *entry;
17511 dw_die_ref die;
17513 entry = (struct types_used_by_vars_entry *) *slot;
17514 gcc_assert (entry->type != NULL
17515 && entry->var_decl != NULL);
17516 die = lookup_type_die (entry->type);
17517 if (die)
17519 /* Ask cgraph if the global variable really is to be emitted.
17520 If yes, then we'll keep the DIE of ENTRY->TYPE. */
17521 struct varpool_node *node = varpool_get_node (entry->var_decl);
17522 if (node && node->analyzed)
17524 die->die_perennial_p = 1;
17525 /* Keep the parent DIEs as well. */
17526 while ((die = die->die_parent) && die->die_perennial_p == 0)
17527 die->die_perennial_p = 1;
17530 return 1;
17533 /* Mark all members of used_types_hash as perennial. */
17535 static void
17536 premark_used_types (struct function *fun)
17538 if (fun && fun->used_types_hash)
17539 htab_traverse (fun->used_types_hash, premark_used_types_helper, NULL);
17542 /* Mark all members of types_used_by_vars_entry as perennial. */
17544 static void
17545 premark_types_used_by_global_vars (void)
17547 if (types_used_by_vars_hash)
17548 htab_traverse (types_used_by_vars_hash,
17549 premark_types_used_by_global_vars_helper, NULL);
17552 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
17553 for CA_LOC call arg loc node. */
17555 static dw_die_ref
17556 gen_call_site_die (tree decl, dw_die_ref subr_die,
17557 struct call_arg_loc_node *ca_loc)
17559 dw_die_ref stmt_die = NULL, die;
17560 tree block = ca_loc->block;
17562 while (block
17563 && block != DECL_INITIAL (decl)
17564 && TREE_CODE (block) == BLOCK)
17566 if (block_map.length () > BLOCK_NUMBER (block))
17567 stmt_die = block_map[BLOCK_NUMBER (block)];
17568 if (stmt_die)
17569 break;
17570 block = BLOCK_SUPERCONTEXT (block);
17572 if (stmt_die == NULL)
17573 stmt_die = subr_die;
17574 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
17575 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
17576 if (ca_loc->tail_call_p)
17577 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
17578 if (ca_loc->symbol_ref)
17580 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
17581 if (tdie)
17582 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
17583 else
17584 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
17586 return die;
17589 /* Generate a DIE to represent a declared function (either file-scope or
17590 block-local). */
17592 static void
17593 gen_subprogram_die (tree decl, dw_die_ref context_die)
17595 tree origin = decl_ultimate_origin (decl);
17596 dw_die_ref subr_die;
17597 tree outer_scope;
17598 dw_die_ref old_die = lookup_decl_die (decl);
17599 int declaration = (current_function_decl != decl
17600 || class_or_namespace_scope_p (context_die));
17602 premark_used_types (DECL_STRUCT_FUNCTION (decl));
17604 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
17605 started to generate the abstract instance of an inline, decided to output
17606 its containing class, and proceeded to emit the declaration of the inline
17607 from the member list for the class. If so, DECLARATION takes priority;
17608 we'll get back to the abstract instance when done with the class. */
17610 /* The class-scope declaration DIE must be the primary DIE. */
17611 if (origin && declaration && class_or_namespace_scope_p (context_die))
17613 origin = NULL;
17614 gcc_assert (!old_die);
17617 /* Now that the C++ front end lazily declares artificial member fns, we
17618 might need to retrofit the declaration into its class. */
17619 if (!declaration && !origin && !old_die
17620 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
17621 && !class_or_namespace_scope_p (context_die)
17622 && debug_info_level > DINFO_LEVEL_TERSE)
17623 old_die = force_decl_die (decl);
17625 if (origin != NULL)
17627 gcc_assert (!declaration || local_scope_p (context_die));
17629 /* Fixup die_parent for the abstract instance of a nested
17630 inline function. */
17631 if (old_die && old_die->die_parent == NULL)
17632 add_child_die (context_die, old_die);
17634 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17635 add_abstract_origin_attribute (subr_die, origin);
17636 /* This is where the actual code for a cloned function is.
17637 Let's emit linkage name attribute for it. This helps
17638 debuggers to e.g, set breakpoints into
17639 constructors/destructors when the user asks "break
17640 K::K". */
17641 add_linkage_name (subr_die, decl);
17643 else if (old_die)
17645 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17646 struct dwarf_file_data * file_index = lookup_filename (s.file);
17648 if (!get_AT_flag (old_die, DW_AT_declaration)
17649 /* We can have a normal definition following an inline one in the
17650 case of redefinition of GNU C extern inlines.
17651 It seems reasonable to use AT_specification in this case. */
17652 && !get_AT (old_die, DW_AT_inline))
17654 /* Detect and ignore this case, where we are trying to output
17655 something we have already output. */
17656 return;
17659 /* If the definition comes from the same place as the declaration,
17660 maybe use the old DIE. We always want the DIE for this function
17661 that has the *_pc attributes to be under comp_unit_die so the
17662 debugger can find it. We also need to do this for abstract
17663 instances of inlines, since the spec requires the out-of-line copy
17664 to have the same parent. For local class methods, this doesn't
17665 apply; we just use the old DIE. */
17666 if ((is_cu_die (old_die->die_parent) || context_die == NULL)
17667 && (DECL_ARTIFICIAL (decl)
17668 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
17669 && (get_AT_unsigned (old_die, DW_AT_decl_line)
17670 == (unsigned) s.line))))
17672 subr_die = old_die;
17674 /* Clear out the declaration attribute and the formal parameters.
17675 Do not remove all children, because it is possible that this
17676 declaration die was forced using force_decl_die(). In such
17677 cases die that forced declaration die (e.g. TAG_imported_module)
17678 is one of the children that we do not want to remove. */
17679 remove_AT (subr_die, DW_AT_declaration);
17680 remove_AT (subr_die, DW_AT_object_pointer);
17681 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
17683 else
17685 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17686 add_AT_specification (subr_die, old_die);
17687 add_pubname (decl, subr_die);
17688 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
17689 add_AT_file (subr_die, DW_AT_decl_file, file_index);
17690 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
17691 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
17694 else
17696 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17698 if (TREE_PUBLIC (decl))
17699 add_AT_flag (subr_die, DW_AT_external, 1);
17701 add_name_and_src_coords_attributes (subr_die, decl);
17702 add_pubname (decl, subr_die);
17703 if (debug_info_level > DINFO_LEVEL_TERSE)
17705 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
17706 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
17707 0, 0, context_die);
17710 add_pure_or_virtual_attribute (subr_die, decl);
17711 if (DECL_ARTIFICIAL (decl))
17712 add_AT_flag (subr_die, DW_AT_artificial, 1);
17714 add_accessibility_attribute (subr_die, decl);
17717 if (declaration)
17719 if (!old_die || !get_AT (old_die, DW_AT_inline))
17721 add_AT_flag (subr_die, DW_AT_declaration, 1);
17723 /* If this is an explicit function declaration then generate
17724 a DW_AT_explicit attribute. */
17725 if (lang_hooks.decls.function_decl_explicit_p (decl)
17726 && (dwarf_version >= 3 || !dwarf_strict))
17727 add_AT_flag (subr_die, DW_AT_explicit, 1);
17729 /* The first time we see a member function, it is in the context of
17730 the class to which it belongs. We make sure of this by emitting
17731 the class first. The next time is the definition, which is
17732 handled above. The two may come from the same source text.
17734 Note that force_decl_die() forces function declaration die. It is
17735 later reused to represent definition. */
17736 equate_decl_number_to_die (decl, subr_die);
17739 else if (DECL_ABSTRACT (decl))
17741 if (DECL_DECLARED_INLINE_P (decl))
17743 if (cgraph_function_possibly_inlined_p (decl))
17744 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
17745 else
17746 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
17748 else
17750 if (cgraph_function_possibly_inlined_p (decl))
17751 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
17752 else
17753 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
17756 if (DECL_DECLARED_INLINE_P (decl)
17757 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
17758 add_AT_flag (subr_die, DW_AT_artificial, 1);
17760 equate_decl_number_to_die (decl, subr_die);
17762 else if (!DECL_EXTERNAL (decl))
17764 HOST_WIDE_INT cfa_fb_offset;
17765 struct function *fun = DECL_STRUCT_FUNCTION (decl);
17767 if (!old_die || !get_AT (old_die, DW_AT_inline))
17768 equate_decl_number_to_die (decl, subr_die);
17770 gcc_checking_assert (fun);
17771 if (!flag_reorder_blocks_and_partition)
17773 dw_fde_ref fde = fun->fde;
17774 if (fde->dw_fde_begin)
17776 /* We have already generated the labels. */
17777 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
17778 fde->dw_fde_end, false);
17780 else
17782 /* Create start/end labels and add the range. */
17783 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
17784 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
17785 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
17786 current_function_funcdef_no);
17787 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
17788 current_function_funcdef_no);
17789 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
17790 false);
17793 #if VMS_DEBUGGING_INFO
17794 /* HP OpenVMS Industry Standard 64: DWARF Extensions
17795 Section 2.3 Prologue and Epilogue Attributes:
17796 When a breakpoint is set on entry to a function, it is generally
17797 desirable for execution to be suspended, not on the very first
17798 instruction of the function, but rather at a point after the
17799 function's frame has been set up, after any language defined local
17800 declaration processing has been completed, and before execution of
17801 the first statement of the function begins. Debuggers generally
17802 cannot properly determine where this point is. Similarly for a
17803 breakpoint set on exit from a function. The prologue and epilogue
17804 attributes allow a compiler to communicate the location(s) to use. */
17807 if (fde->dw_fde_vms_end_prologue)
17808 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
17809 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
17811 if (fde->dw_fde_vms_begin_epilogue)
17812 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
17813 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
17815 #endif
17818 else
17820 /* Generate pubnames entries for the split function code ranges. */
17821 dw_fde_ref fde = fun->fde;
17823 if (fde->dw_fde_second_begin)
17825 if (dwarf_version >= 3 || !dwarf_strict)
17827 /* We should use ranges for non-contiguous code section
17828 addresses. Use the actual code range for the initial
17829 section, since the HOT/COLD labels might precede an
17830 alignment offset. */
17831 bool range_list_added = false;
17832 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
17833 fde->dw_fde_end, &range_list_added,
17834 false);
17835 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
17836 fde->dw_fde_second_end,
17837 &range_list_added, false);
17838 if (range_list_added)
17839 add_ranges (NULL);
17841 else
17843 /* There is no real support in DW2 for this .. so we make
17844 a work-around. First, emit the pub name for the segment
17845 containing the function label. Then make and emit a
17846 simplified subprogram DIE for the second segment with the
17847 name pre-fixed by __hot/cold_sect_of_. We use the same
17848 linkage name for the second die so that gdb will find both
17849 sections when given "b foo". */
17850 const char *name = NULL;
17851 tree decl_name = DECL_NAME (decl);
17852 dw_die_ref seg_die;
17854 /* Do the 'primary' section. */
17855 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
17856 fde->dw_fde_end, false);
17858 /* Build a minimal DIE for the secondary section. */
17859 seg_die = new_die (DW_TAG_subprogram,
17860 subr_die->die_parent, decl);
17862 if (TREE_PUBLIC (decl))
17863 add_AT_flag (seg_die, DW_AT_external, 1);
17865 if (decl_name != NULL
17866 && IDENTIFIER_POINTER (decl_name) != NULL)
17868 name = dwarf2_name (decl, 1);
17869 if (! DECL_ARTIFICIAL (decl))
17870 add_src_coords_attributes (seg_die, decl);
17872 add_linkage_name (seg_die, decl);
17874 gcc_assert (name != NULL);
17875 add_pure_or_virtual_attribute (seg_die, decl);
17876 if (DECL_ARTIFICIAL (decl))
17877 add_AT_flag (seg_die, DW_AT_artificial, 1);
17879 name = concat ("__second_sect_of_", name, NULL);
17880 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
17881 fde->dw_fde_second_end, false);
17882 add_name_attribute (seg_die, name);
17883 if (want_pubnames ())
17884 add_pubname_string (name, seg_die);
17887 else
17888 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
17889 false);
17892 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
17894 /* We define the "frame base" as the function's CFA. This is more
17895 convenient for several reasons: (1) It's stable across the prologue
17896 and epilogue, which makes it better than just a frame pointer,
17897 (2) With dwarf3, there exists a one-byte encoding that allows us
17898 to reference the .debug_frame data by proxy, but failing that,
17899 (3) We can at least reuse the code inspection and interpretation
17900 code that determines the CFA position at various points in the
17901 function. */
17902 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
17904 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
17905 add_AT_loc (subr_die, DW_AT_frame_base, op);
17907 else
17909 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
17910 if (list->dw_loc_next)
17911 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
17912 else
17913 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
17916 /* Compute a displacement from the "steady-state frame pointer" to
17917 the CFA. The former is what all stack slots and argument slots
17918 will reference in the rtl; the later is what we've told the
17919 debugger about. We'll need to adjust all frame_base references
17920 by this displacement. */
17921 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
17923 if (fun->static_chain_decl)
17924 add_AT_location_description (subr_die, DW_AT_static_link,
17925 loc_list_from_tree (fun->static_chain_decl, 2));
17928 /* Generate child dies for template paramaters. */
17929 if (debug_info_level > DINFO_LEVEL_TERSE)
17930 gen_generic_params_dies (decl);
17932 /* Now output descriptions of the arguments for this function. This gets
17933 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
17934 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
17935 `...' at the end of the formal parameter list. In order to find out if
17936 there was a trailing ellipsis or not, we must instead look at the type
17937 associated with the FUNCTION_DECL. This will be a node of type
17938 FUNCTION_TYPE. If the chain of type nodes hanging off of this
17939 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
17940 an ellipsis at the end. */
17942 /* In the case where we are describing a mere function declaration, all we
17943 need to do here (and all we *can* do here) is to describe the *types* of
17944 its formal parameters. */
17945 if (debug_info_level <= DINFO_LEVEL_TERSE)
17947 else if (declaration)
17948 gen_formal_types_die (decl, subr_die);
17949 else
17951 /* Generate DIEs to represent all known formal parameters. */
17952 tree parm = DECL_ARGUMENTS (decl);
17953 tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
17954 tree generic_decl_parm = generic_decl
17955 ? DECL_ARGUMENTS (generic_decl)
17956 : NULL;
17958 /* Now we want to walk the list of parameters of the function and
17959 emit their relevant DIEs.
17961 We consider the case of DECL being an instance of a generic function
17962 as well as it being a normal function.
17964 If DECL is an instance of a generic function we walk the
17965 parameters of the generic function declaration _and_ the parameters of
17966 DECL itself. This is useful because we want to emit specific DIEs for
17967 function parameter packs and those are declared as part of the
17968 generic function declaration. In that particular case,
17969 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
17970 That DIE has children DIEs representing the set of arguments
17971 of the pack. Note that the set of pack arguments can be empty.
17972 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
17973 children DIE.
17975 Otherwise, we just consider the parameters of DECL. */
17976 while (generic_decl_parm || parm)
17978 if (generic_decl_parm
17979 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
17980 gen_formal_parameter_pack_die (generic_decl_parm,
17981 parm, subr_die,
17982 &parm);
17983 else if (parm)
17985 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
17987 if (parm == DECL_ARGUMENTS (decl)
17988 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
17989 && parm_die
17990 && (dwarf_version >= 3 || !dwarf_strict))
17991 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
17993 parm = DECL_CHAIN (parm);
17996 if (generic_decl_parm)
17997 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
18000 /* Decide whether we need an unspecified_parameters DIE at the end.
18001 There are 2 more cases to do this for: 1) the ansi ... declaration -
18002 this is detectable when the end of the arg list is not a
18003 void_type_node 2) an unprototyped function declaration (not a
18004 definition). This just means that we have no info about the
18005 parameters at all. */
18006 if (prototype_p (TREE_TYPE (decl)))
18008 /* This is the prototyped case, check for.... */
18009 if (stdarg_p (TREE_TYPE (decl)))
18010 gen_unspecified_parameters_die (decl, subr_die);
18012 else if (DECL_INITIAL (decl) == NULL_TREE)
18013 gen_unspecified_parameters_die (decl, subr_die);
18016 /* Output Dwarf info for all of the stuff within the body of the function
18017 (if it has one - it may be just a declaration). */
18018 outer_scope = DECL_INITIAL (decl);
18020 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
18021 a function. This BLOCK actually represents the outermost binding contour
18022 for the function, i.e. the contour in which the function's formal
18023 parameters and labels get declared. Curiously, it appears that the front
18024 end doesn't actually put the PARM_DECL nodes for the current function onto
18025 the BLOCK_VARS list for this outer scope, but are strung off of the
18026 DECL_ARGUMENTS list for the function instead.
18028 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
18029 the LABEL_DECL nodes for the function however, and we output DWARF info
18030 for those in decls_for_scope. Just within the `outer_scope' there will be
18031 a BLOCK node representing the function's outermost pair of curly braces,
18032 and any blocks used for the base and member initializers of a C++
18033 constructor function. */
18034 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
18036 int call_site_note_count = 0;
18037 int tail_call_site_note_count = 0;
18039 /* Emit a DW_TAG_variable DIE for a named return value. */
18040 if (DECL_NAME (DECL_RESULT (decl)))
18041 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
18043 current_function_has_inlines = 0;
18044 decls_for_scope (outer_scope, subr_die, 0);
18046 if (call_arg_locations && !dwarf_strict)
18048 struct call_arg_loc_node *ca_loc;
18049 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
18051 dw_die_ref die = NULL;
18052 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
18053 rtx arg, next_arg;
18055 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
18056 arg; arg = next_arg)
18058 dw_loc_descr_ref reg, val;
18059 enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
18060 dw_die_ref cdie, tdie = NULL;
18062 next_arg = XEXP (arg, 1);
18063 if (REG_P (XEXP (XEXP (arg, 0), 0))
18064 && next_arg
18065 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
18066 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
18067 && REGNO (XEXP (XEXP (arg, 0), 0))
18068 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
18069 next_arg = XEXP (next_arg, 1);
18070 if (mode == VOIDmode)
18072 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
18073 if (mode == VOIDmode)
18074 mode = GET_MODE (XEXP (arg, 0));
18076 if (mode == VOIDmode || mode == BLKmode)
18077 continue;
18078 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
18080 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18081 tloc = XEXP (XEXP (arg, 0), 1);
18082 continue;
18084 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
18085 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
18087 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18088 tlocc = XEXP (XEXP (arg, 0), 1);
18089 continue;
18091 reg = NULL;
18092 if (REG_P (XEXP (XEXP (arg, 0), 0)))
18093 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
18094 VAR_INIT_STATUS_INITIALIZED);
18095 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
18097 rtx mem = XEXP (XEXP (arg, 0), 0);
18098 reg = mem_loc_descriptor (XEXP (mem, 0),
18099 get_address_mode (mem),
18100 GET_MODE (mem),
18101 VAR_INIT_STATUS_INITIALIZED);
18103 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
18104 == DEBUG_PARAMETER_REF)
18106 tree tdecl
18107 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
18108 tdie = lookup_decl_die (tdecl);
18109 if (tdie == NULL)
18110 continue;
18112 else
18113 continue;
18114 if (reg == NULL
18115 && GET_CODE (XEXP (XEXP (arg, 0), 0))
18116 != DEBUG_PARAMETER_REF)
18117 continue;
18118 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
18119 VOIDmode,
18120 VAR_INIT_STATUS_INITIALIZED);
18121 if (val == NULL)
18122 continue;
18123 if (die == NULL)
18124 die = gen_call_site_die (decl, subr_die, ca_loc);
18125 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
18126 NULL_TREE);
18127 if (reg != NULL)
18128 add_AT_loc (cdie, DW_AT_location, reg);
18129 else if (tdie != NULL)
18130 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
18131 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
18132 if (next_arg != XEXP (arg, 1))
18134 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
18135 if (mode == VOIDmode)
18136 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
18137 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
18138 0), 1),
18139 mode, VOIDmode,
18140 VAR_INIT_STATUS_INITIALIZED);
18141 if (val != NULL)
18142 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
18145 if (die == NULL
18146 && (ca_loc->symbol_ref || tloc))
18147 die = gen_call_site_die (decl, subr_die, ca_loc);
18148 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
18150 dw_loc_descr_ref tval = NULL;
18152 if (tloc != NULL_RTX)
18153 tval = mem_loc_descriptor (tloc,
18154 GET_MODE (tloc) == VOIDmode
18155 ? Pmode : GET_MODE (tloc),
18156 VOIDmode,
18157 VAR_INIT_STATUS_INITIALIZED);
18158 if (tval)
18159 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
18160 else if (tlocc != NULL_RTX)
18162 tval = mem_loc_descriptor (tlocc,
18163 GET_MODE (tlocc) == VOIDmode
18164 ? Pmode : GET_MODE (tlocc),
18165 VOIDmode,
18166 VAR_INIT_STATUS_INITIALIZED);
18167 if (tval)
18168 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
18169 tval);
18172 if (die != NULL)
18174 call_site_note_count++;
18175 if (ca_loc->tail_call_p)
18176 tail_call_site_note_count++;
18180 call_arg_locations = NULL;
18181 call_arg_loc_last = NULL;
18182 if (tail_call_site_count >= 0
18183 && tail_call_site_count == tail_call_site_note_count
18184 && !dwarf_strict)
18186 if (call_site_count >= 0
18187 && call_site_count == call_site_note_count)
18188 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
18189 else
18190 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
18192 call_site_count = -1;
18193 tail_call_site_count = -1;
18195 /* Add the calling convention attribute if requested. */
18196 add_calling_convention_attribute (subr_die, decl);
18200 /* Returns a hash value for X (which really is a die_struct). */
18202 static hashval_t
18203 common_block_die_table_hash (const void *x)
18205 const_dw_die_ref d = (const_dw_die_ref) x;
18206 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
18209 /* Return nonzero if decl_id and die_parent of die_struct X is the same
18210 as decl_id and die_parent of die_struct Y. */
18212 static int
18213 common_block_die_table_eq (const void *x, const void *y)
18215 const_dw_die_ref d = (const_dw_die_ref) x;
18216 const_dw_die_ref e = (const_dw_die_ref) y;
18217 return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
18220 /* Generate a DIE to represent a declared data object.
18221 Either DECL or ORIGIN must be non-null. */
18223 static void
18224 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
18226 HOST_WIDE_INT off = 0;
18227 tree com_decl;
18228 tree decl_or_origin = decl ? decl : origin;
18229 tree ultimate_origin;
18230 dw_die_ref var_die;
18231 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
18232 dw_die_ref origin_die;
18233 bool declaration = (DECL_EXTERNAL (decl_or_origin)
18234 || class_or_namespace_scope_p (context_die));
18235 bool specialization_p = false;
18237 ultimate_origin = decl_ultimate_origin (decl_or_origin);
18238 if (decl || ultimate_origin)
18239 origin = ultimate_origin;
18240 com_decl = fortran_common (decl_or_origin, &off);
18242 /* Symbol in common gets emitted as a child of the common block, in the form
18243 of a data member. */
18244 if (com_decl)
18246 dw_die_ref com_die;
18247 dw_loc_list_ref loc;
18248 die_node com_die_arg;
18250 var_die = lookup_decl_die (decl_or_origin);
18251 if (var_die)
18253 if (get_AT (var_die, DW_AT_location) == NULL)
18255 loc = loc_list_from_tree (com_decl, off ? 1 : 2);
18256 if (loc)
18258 if (off)
18260 /* Optimize the common case. */
18261 if (single_element_loc_list_p (loc)
18262 && loc->expr->dw_loc_opc == DW_OP_addr
18263 && loc->expr->dw_loc_next == NULL
18264 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
18265 == SYMBOL_REF)
18267 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18268 loc->expr->dw_loc_oprnd1.v.val_addr
18269 = plus_constant (GET_MODE (x), x , off);
18271 else
18272 loc_list_plus_const (loc, off);
18274 add_AT_location_description (var_die, DW_AT_location, loc);
18275 remove_AT (var_die, DW_AT_declaration);
18278 return;
18281 if (common_block_die_table == NULL)
18282 common_block_die_table
18283 = htab_create_ggc (10, common_block_die_table_hash,
18284 common_block_die_table_eq, NULL);
18286 com_die_arg.decl_id = DECL_UID (com_decl);
18287 com_die_arg.die_parent = context_die;
18288 com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
18289 loc = loc_list_from_tree (com_decl, 2);
18290 if (com_die == NULL)
18292 const char *cnam
18293 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
18294 void **slot;
18296 com_die = new_die (DW_TAG_common_block, context_die, decl);
18297 add_name_and_src_coords_attributes (com_die, com_decl);
18298 if (loc)
18300 add_AT_location_description (com_die, DW_AT_location, loc);
18301 /* Avoid sharing the same loc descriptor between
18302 DW_TAG_common_block and DW_TAG_variable. */
18303 loc = loc_list_from_tree (com_decl, 2);
18305 else if (DECL_EXTERNAL (decl))
18306 add_AT_flag (com_die, DW_AT_declaration, 1);
18307 if (want_pubnames ())
18308 add_pubname_string (cnam, com_die); /* ??? needed? */
18309 com_die->decl_id = DECL_UID (com_decl);
18310 slot = htab_find_slot (common_block_die_table, com_die, INSERT);
18311 *slot = (void *) com_die;
18313 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
18315 add_AT_location_description (com_die, DW_AT_location, loc);
18316 loc = loc_list_from_tree (com_decl, 2);
18317 remove_AT (com_die, DW_AT_declaration);
18319 var_die = new_die (DW_TAG_variable, com_die, decl);
18320 add_name_and_src_coords_attributes (var_die, decl);
18321 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
18322 TREE_THIS_VOLATILE (decl), context_die);
18323 add_AT_flag (var_die, DW_AT_external, 1);
18324 if (loc)
18326 if (off)
18328 /* Optimize the common case. */
18329 if (single_element_loc_list_p (loc)
18330 && loc->expr->dw_loc_opc == DW_OP_addr
18331 && loc->expr->dw_loc_next == NULL
18332 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
18334 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18335 loc->expr->dw_loc_oprnd1.v.val_addr
18336 = plus_constant (GET_MODE (x), x, off);
18338 else
18339 loc_list_plus_const (loc, off);
18341 add_AT_location_description (var_die, DW_AT_location, loc);
18343 else if (DECL_EXTERNAL (decl))
18344 add_AT_flag (var_die, DW_AT_declaration, 1);
18345 equate_decl_number_to_die (decl, var_die);
18346 return;
18349 /* If the compiler emitted a definition for the DECL declaration
18350 and if we already emitted a DIE for it, don't emit a second
18351 DIE for it again. Allow re-declarations of DECLs that are
18352 inside functions, though. */
18353 if (old_die && declaration && !local_scope_p (context_die))
18354 return;
18356 /* For static data members, the declaration in the class is supposed
18357 to have DW_TAG_member tag; the specification should still be
18358 DW_TAG_variable referencing the DW_TAG_member DIE. */
18359 if (declaration && class_scope_p (context_die))
18360 var_die = new_die (DW_TAG_member, context_die, decl);
18361 else
18362 var_die = new_die (DW_TAG_variable, context_die, decl);
18364 origin_die = NULL;
18365 if (origin != NULL)
18366 origin_die = add_abstract_origin_attribute (var_die, origin);
18368 /* Loop unrolling can create multiple blocks that refer to the same
18369 static variable, so we must test for the DW_AT_declaration flag.
18371 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
18372 copy decls and set the DECL_ABSTRACT flag on them instead of
18373 sharing them.
18375 ??? Duplicated blocks have been rewritten to use .debug_ranges.
18377 ??? The declare_in_namespace support causes us to get two DIEs for one
18378 variable, both of which are declarations. We want to avoid considering
18379 one to be a specification, so we must test that this DIE is not a
18380 declaration. */
18381 else if (old_die && TREE_STATIC (decl) && ! declaration
18382 && get_AT_flag (old_die, DW_AT_declaration) == 1)
18384 /* This is a definition of a C++ class level static. */
18385 add_AT_specification (var_die, old_die);
18386 specialization_p = true;
18387 if (DECL_NAME (decl))
18389 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18390 struct dwarf_file_data * file_index = lookup_filename (s.file);
18392 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18393 add_AT_file (var_die, DW_AT_decl_file, file_index);
18395 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18396 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
18398 if (old_die->die_tag == DW_TAG_member)
18399 add_linkage_name (var_die, decl);
18402 else
18403 add_name_and_src_coords_attributes (var_die, decl);
18405 if ((origin == NULL && !specialization_p)
18406 || (origin != NULL
18407 && !DECL_ABSTRACT (decl_or_origin)
18408 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
18409 decl_function_context
18410 (decl_or_origin))))
18412 tree type = TREE_TYPE (decl_or_origin);
18414 if (decl_by_reference_p (decl_or_origin))
18415 add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
18416 else
18417 add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
18418 TREE_THIS_VOLATILE (decl_or_origin), context_die);
18421 if (origin == NULL && !specialization_p)
18423 if (TREE_PUBLIC (decl))
18424 add_AT_flag (var_die, DW_AT_external, 1);
18426 if (DECL_ARTIFICIAL (decl))
18427 add_AT_flag (var_die, DW_AT_artificial, 1);
18429 add_accessibility_attribute (var_die, decl);
18432 if (declaration)
18433 add_AT_flag (var_die, DW_AT_declaration, 1);
18435 if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
18436 equate_decl_number_to_die (decl, var_die);
18438 if (! declaration
18439 && (! DECL_ABSTRACT (decl_or_origin)
18440 /* Local static vars are shared between all clones/inlines,
18441 so emit DW_AT_location on the abstract DIE if DECL_RTL is
18442 already set. */
18443 || (TREE_CODE (decl_or_origin) == VAR_DECL
18444 && TREE_STATIC (decl_or_origin)
18445 && DECL_RTL_SET_P (decl_or_origin)))
18446 /* When abstract origin already has DW_AT_location attribute, no need
18447 to add it again. */
18448 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
18450 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
18451 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
18452 defer_location (decl_or_origin, var_die);
18453 else
18454 add_location_or_const_value_attribute (var_die, decl_or_origin,
18455 decl == NULL, DW_AT_location);
18456 add_pubname (decl_or_origin, var_die);
18458 else
18459 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
18462 /* Generate a DIE to represent a named constant. */
18464 static void
18465 gen_const_die (tree decl, dw_die_ref context_die)
18467 dw_die_ref const_die;
18468 tree type = TREE_TYPE (decl);
18470 const_die = new_die (DW_TAG_constant, context_die, decl);
18471 add_name_and_src_coords_attributes (const_die, decl);
18472 add_type_attribute (const_die, type, 1, 0, context_die);
18473 if (TREE_PUBLIC (decl))
18474 add_AT_flag (const_die, DW_AT_external, 1);
18475 if (DECL_ARTIFICIAL (decl))
18476 add_AT_flag (const_die, DW_AT_artificial, 1);
18477 tree_add_const_value_attribute_for_decl (const_die, decl);
18480 /* Generate a DIE to represent a label identifier. */
18482 static void
18483 gen_label_die (tree decl, dw_die_ref context_die)
18485 tree origin = decl_ultimate_origin (decl);
18486 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
18487 rtx insn;
18488 char label[MAX_ARTIFICIAL_LABEL_BYTES];
18490 if (origin != NULL)
18491 add_abstract_origin_attribute (lbl_die, origin);
18492 else
18493 add_name_and_src_coords_attributes (lbl_die, decl);
18495 if (DECL_ABSTRACT (decl))
18496 equate_decl_number_to_die (decl, lbl_die);
18497 else
18499 insn = DECL_RTL_IF_SET (decl);
18501 /* Deleted labels are programmer specified labels which have been
18502 eliminated because of various optimizations. We still emit them
18503 here so that it is possible to put breakpoints on them. */
18504 if (insn
18505 && (LABEL_P (insn)
18506 || ((NOTE_P (insn)
18507 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
18509 /* When optimization is enabled (via -O) some parts of the compiler
18510 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
18511 represent source-level labels which were explicitly declared by
18512 the user. This really shouldn't be happening though, so catch
18513 it if it ever does happen. */
18514 gcc_assert (!INSN_DELETED_P (insn));
18516 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
18517 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18519 else if (insn
18520 && NOTE_P (insn)
18521 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
18522 && CODE_LABEL_NUMBER (insn) != -1)
18524 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
18525 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18530 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
18531 attributes to the DIE for a block STMT, to describe where the inlined
18532 function was called from. This is similar to add_src_coords_attributes. */
18534 static inline void
18535 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
18537 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
18539 if (dwarf_version >= 3 || !dwarf_strict)
18541 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
18542 add_AT_unsigned (die, DW_AT_call_line, s.line);
18547 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
18548 Add low_pc and high_pc attributes to the DIE for a block STMT. */
18550 static inline void
18551 add_high_low_attributes (tree stmt, dw_die_ref die)
18553 char label[MAX_ARTIFICIAL_LABEL_BYTES];
18555 if (BLOCK_FRAGMENT_CHAIN (stmt)
18556 && (dwarf_version >= 3 || !dwarf_strict))
18558 tree chain, superblock = NULL_TREE;
18559 dw_die_ref pdie;
18560 dw_attr_ref attr = NULL;
18562 if (inlined_function_outer_scope_p (stmt))
18564 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18565 BLOCK_NUMBER (stmt));
18566 add_AT_lbl_id (die, DW_AT_entry_pc, label);
18569 /* Optimize duplicate .debug_ranges lists or even tails of
18570 lists. If this BLOCK has same ranges as its supercontext,
18571 lookup DW_AT_ranges attribute in the supercontext (and
18572 recursively so), verify that the ranges_table contains the
18573 right values and use it instead of adding a new .debug_range. */
18574 for (chain = stmt, pdie = die;
18575 BLOCK_SAME_RANGE (chain);
18576 chain = BLOCK_SUPERCONTEXT (chain))
18578 dw_attr_ref new_attr;
18580 pdie = pdie->die_parent;
18581 if (pdie == NULL)
18582 break;
18583 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
18584 break;
18585 new_attr = get_AT (pdie, DW_AT_ranges);
18586 if (new_attr == NULL
18587 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
18588 break;
18589 attr = new_attr;
18590 superblock = BLOCK_SUPERCONTEXT (chain);
18592 if (attr != NULL
18593 && (ranges_table[attr->dw_attr_val.v.val_offset
18594 / 2 / DWARF2_ADDR_SIZE].num
18595 == BLOCK_NUMBER (superblock))
18596 && BLOCK_FRAGMENT_CHAIN (superblock))
18598 unsigned long off = attr->dw_attr_val.v.val_offset
18599 / 2 / DWARF2_ADDR_SIZE;
18600 unsigned long supercnt = 0, thiscnt = 0;
18601 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
18602 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
18604 ++supercnt;
18605 gcc_checking_assert (ranges_table[off + supercnt].num
18606 == BLOCK_NUMBER (chain));
18608 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
18609 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
18610 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
18611 ++thiscnt;
18612 gcc_assert (supercnt >= thiscnt);
18613 add_AT_range_list (die, DW_AT_ranges,
18614 ((off + supercnt - thiscnt)
18615 * 2 * DWARF2_ADDR_SIZE),
18616 false);
18617 return;
18620 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
18622 chain = BLOCK_FRAGMENT_CHAIN (stmt);
18625 add_ranges (chain);
18626 chain = BLOCK_FRAGMENT_CHAIN (chain);
18628 while (chain);
18629 add_ranges (NULL);
18631 else
18633 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
18634 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18635 BLOCK_NUMBER (stmt));
18636 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
18637 BLOCK_NUMBER (stmt));
18638 add_AT_low_high_pc (die, label, label_high, false);
18642 /* Generate a DIE for a lexical block. */
18644 static void
18645 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
18647 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
18649 if (call_arg_locations)
18651 if (block_map.length () <= BLOCK_NUMBER (stmt))
18652 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
18653 block_map[BLOCK_NUMBER (stmt)] = stmt_die;
18656 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
18657 add_high_low_attributes (stmt, stmt_die);
18659 decls_for_scope (stmt, stmt_die, depth);
18662 /* Generate a DIE for an inlined subprogram. */
18664 static void
18665 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
18667 tree decl;
18669 /* The instance of function that is effectively being inlined shall not
18670 be abstract. */
18671 gcc_assert (! BLOCK_ABSTRACT (stmt));
18673 decl = block_ultimate_origin (stmt);
18675 /* Emit info for the abstract instance first, if we haven't yet. We
18676 must emit this even if the block is abstract, otherwise when we
18677 emit the block below (or elsewhere), we may end up trying to emit
18678 a die whose origin die hasn't been emitted, and crashing. */
18679 dwarf2out_abstract_function (decl);
18681 if (! BLOCK_ABSTRACT (stmt))
18683 dw_die_ref subr_die
18684 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
18686 if (call_arg_locations)
18688 if (block_map.length () <= BLOCK_NUMBER (stmt))
18689 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
18690 block_map[BLOCK_NUMBER (stmt)] = subr_die;
18692 add_abstract_origin_attribute (subr_die, decl);
18693 if (TREE_ASM_WRITTEN (stmt))
18694 add_high_low_attributes (stmt, subr_die);
18695 add_call_src_coords_attributes (stmt, subr_die);
18697 decls_for_scope (stmt, subr_die, depth);
18698 current_function_has_inlines = 1;
18702 /* Generate a DIE for a field in a record, or structure. */
18704 static void
18705 gen_field_die (tree decl, dw_die_ref context_die)
18707 dw_die_ref decl_die;
18709 if (TREE_TYPE (decl) == error_mark_node)
18710 return;
18712 decl_die = new_die (DW_TAG_member, context_die, decl);
18713 add_name_and_src_coords_attributes (decl_die, decl);
18714 add_type_attribute (decl_die, member_declared_type (decl), 0, 0, context_die);
18716 if (DECL_BIT_FIELD_TYPE (decl))
18718 add_byte_size_attribute (decl_die, decl);
18719 add_bit_size_attribute (decl_die, decl);
18720 add_bit_offset_attribute (decl_die, decl);
18723 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
18724 add_data_member_location_attribute (decl_die, decl);
18726 if (DECL_ARTIFICIAL (decl))
18727 add_AT_flag (decl_die, DW_AT_artificial, 1);
18729 add_accessibility_attribute (decl_die, decl);
18731 /* Equate decl number to die, so that we can look up this decl later on. */
18732 equate_decl_number_to_die (decl, decl_die);
18735 #if 0
18736 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18737 Use modified_type_die instead.
18738 We keep this code here just in case these types of DIEs may be needed to
18739 represent certain things in other languages (e.g. Pascal) someday. */
18741 static void
18742 gen_pointer_type_die (tree type, dw_die_ref context_die)
18744 dw_die_ref ptr_die
18745 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
18747 equate_type_number_to_die (type, ptr_die);
18748 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18749 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18752 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18753 Use modified_type_die instead.
18754 We keep this code here just in case these types of DIEs may be needed to
18755 represent certain things in other languages (e.g. Pascal) someday. */
18757 static void
18758 gen_reference_type_die (tree type, dw_die_ref context_die)
18760 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
18762 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
18763 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
18764 else
18765 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
18767 equate_type_number_to_die (type, ref_die);
18768 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
18769 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18771 #endif
18773 /* Generate a DIE for a pointer to a member type. */
18775 static void
18776 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
18778 dw_die_ref ptr_die
18779 = new_die (DW_TAG_ptr_to_member_type,
18780 scope_die_for (type, context_die), type);
18782 equate_type_number_to_die (type, ptr_die);
18783 add_AT_die_ref (ptr_die, DW_AT_containing_type,
18784 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
18785 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18788 typedef const char *dchar_p; /* For DEF_VEC_P. */
18790 static char *producer_string;
18792 /* Return a heap allocated producer string including command line options
18793 if -grecord-gcc-switches. */
18795 static char *
18796 gen_producer_string (void)
18798 size_t j;
18799 vec<dchar_p> switches = vNULL;
18800 const char *language_string = lang_hooks.name;
18801 char *producer, *tail;
18802 const char *p;
18803 size_t len = dwarf_record_gcc_switches ? 0 : 3;
18804 size_t plen = strlen (language_string) + 1 + strlen (version_string);
18806 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
18807 switch (save_decoded_options[j].opt_index)
18809 case OPT_o:
18810 case OPT_d:
18811 case OPT_dumpbase:
18812 case OPT_dumpdir:
18813 case OPT_auxbase:
18814 case OPT_auxbase_strip:
18815 case OPT_quiet:
18816 case OPT_version:
18817 case OPT_v:
18818 case OPT_w:
18819 case OPT_L:
18820 case OPT_D:
18821 case OPT_I:
18822 case OPT_U:
18823 case OPT_SPECIAL_unknown:
18824 case OPT_SPECIAL_ignore:
18825 case OPT_SPECIAL_program_name:
18826 case OPT_SPECIAL_input_file:
18827 case OPT_grecord_gcc_switches:
18828 case OPT_gno_record_gcc_switches:
18829 case OPT__output_pch_:
18830 case OPT_fdiagnostics_show_location_:
18831 case OPT_fdiagnostics_show_option:
18832 case OPT_fdiagnostics_show_caret:
18833 case OPT_fverbose_asm:
18834 case OPT____:
18835 case OPT__sysroot_:
18836 case OPT_nostdinc:
18837 case OPT_nostdinc__:
18838 /* Ignore these. */
18839 continue;
18840 default:
18841 if (cl_options[save_decoded_options[j].opt_index].flags
18842 & CL_NO_DWARF_RECORD)
18843 continue;
18844 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
18845 == '-');
18846 switch (save_decoded_options[j].canonical_option[0][1])
18848 case 'M':
18849 case 'i':
18850 case 'W':
18851 continue;
18852 case 'f':
18853 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
18854 "dump", 4) == 0)
18855 continue;
18856 break;
18857 default:
18858 break;
18860 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
18861 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
18862 break;
18865 producer = XNEWVEC (char, plen + 1 + len + 1);
18866 tail = producer;
18867 sprintf (tail, "%s %s", language_string, version_string);
18868 tail += plen;
18870 FOR_EACH_VEC_ELT (switches, j, p)
18872 len = strlen (p);
18873 *tail = ' ';
18874 memcpy (tail + 1, p, len);
18875 tail += len + 1;
18878 *tail = '\0';
18879 switches.release ();
18880 return producer;
18883 /* Generate the DIE for the compilation unit. */
18885 static dw_die_ref
18886 gen_compile_unit_die (const char *filename)
18888 dw_die_ref die;
18889 const char *language_string = lang_hooks.name;
18890 int language;
18892 die = new_die (DW_TAG_compile_unit, NULL, NULL);
18894 if (filename)
18896 add_name_attribute (die, filename);
18897 /* Don't add cwd for <built-in>. */
18898 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
18899 add_comp_dir_attribute (die);
18902 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
18904 /* If our producer is LTO try to figure out a common language to use
18905 from the global list of translation units. */
18906 if (strcmp (language_string, "GNU GIMPLE") == 0)
18908 unsigned i;
18909 tree t;
18910 const char *common_lang = NULL;
18912 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
18914 if (!TRANSLATION_UNIT_LANGUAGE (t))
18915 continue;
18916 if (!common_lang)
18917 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
18918 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
18920 else if (strncmp (common_lang, "GNU C", 5) == 0
18921 && strncmp(TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
18922 /* Mixing C and C++ is ok, use C++ in that case. */
18923 common_lang = "GNU C++";
18924 else
18926 /* Fall back to C. */
18927 common_lang = NULL;
18928 break;
18932 if (common_lang)
18933 language_string = common_lang;
18936 language = DW_LANG_C89;
18937 if (strcmp (language_string, "GNU C++") == 0)
18938 language = DW_LANG_C_plus_plus;
18939 else if (strcmp (language_string, "GNU F77") == 0)
18940 language = DW_LANG_Fortran77;
18941 else if (strcmp (language_string, "GNU Pascal") == 0)
18942 language = DW_LANG_Pascal83;
18943 else if (use_upc_dwarf2_extensions
18944 && (strcmp (language_string, "GNU UPC") == 0))
18945 language = DW_LANG_Upc;
18946 else if (dwarf_version >= 3 || !dwarf_strict)
18948 if (strcmp (language_string, "GNU Ada") == 0)
18949 language = DW_LANG_Ada95;
18950 else if (strcmp (language_string, "GNU Fortran") == 0)
18951 language = DW_LANG_Fortran95;
18952 else if (strcmp (language_string, "GNU Java") == 0)
18953 language = DW_LANG_Java;
18954 else if (strcmp (language_string, "GNU Objective-C") == 0)
18955 language = DW_LANG_ObjC;
18956 else if (strcmp (language_string, "GNU Objective-C++") == 0)
18957 language = DW_LANG_ObjC_plus_plus;
18958 else if (dwarf_version >= 5 || !dwarf_strict)
18960 if (strcmp (language_string, "GNU Go") == 0)
18961 language = DW_LANG_Go;
18964 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
18965 else if (strcmp (language_string, "GNU Fortran") == 0)
18966 language = DW_LANG_Fortran90;
18968 add_AT_unsigned (die, DW_AT_language, language);
18970 switch (language)
18972 case DW_LANG_Fortran77:
18973 case DW_LANG_Fortran90:
18974 case DW_LANG_Fortran95:
18975 /* Fortran has case insensitive identifiers and the front-end
18976 lowercases everything. */
18977 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
18978 break;
18979 default:
18980 /* The default DW_ID_case_sensitive doesn't need to be specified. */
18981 break;
18983 return die;
18986 /* Generate the DIE for a base class. */
18988 static void
18989 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
18991 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
18993 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
18994 add_data_member_location_attribute (die, binfo);
18996 if (BINFO_VIRTUAL_P (binfo))
18997 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
18999 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19000 children, otherwise the default is DW_ACCESS_public. In DWARF2
19001 the default has always been DW_ACCESS_private. */
19002 if (access == access_public_node)
19004 if (dwarf_version == 2
19005 || context_die->die_tag == DW_TAG_class_type)
19006 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19008 else if (access == access_protected_node)
19009 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19010 else if (dwarf_version > 2
19011 && context_die->die_tag != DW_TAG_class_type)
19012 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19015 /* Generate a DIE for a class member. */
19017 static void
19018 gen_member_die (tree type, dw_die_ref context_die)
19020 tree member;
19021 tree binfo = TYPE_BINFO (type);
19022 dw_die_ref child;
19024 /* If this is not an incomplete type, output descriptions of each of its
19025 members. Note that as we output the DIEs necessary to represent the
19026 members of this record or union type, we will also be trying to output
19027 DIEs to represent the *types* of those members. However the `type'
19028 function (above) will specifically avoid generating type DIEs for member
19029 types *within* the list of member DIEs for this (containing) type except
19030 for those types (of members) which are explicitly marked as also being
19031 members of this (containing) type themselves. The g++ front- end can
19032 force any given type to be treated as a member of some other (containing)
19033 type by setting the TYPE_CONTEXT of the given (member) type to point to
19034 the TREE node representing the appropriate (containing) type. */
19036 /* First output info about the base classes. */
19037 if (binfo)
19039 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
19040 int i;
19041 tree base;
19043 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19044 gen_inheritance_die (base,
19045 (accesses ? (*accesses)[i] : access_public_node),
19046 context_die);
19049 /* Now output info about the data members and type members. */
19050 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19052 /* If we thought we were generating minimal debug info for TYPE
19053 and then changed our minds, some of the member declarations
19054 may have already been defined. Don't define them again, but
19055 do put them in the right order. */
19057 child = lookup_decl_die (member);
19058 if (child)
19059 splice_child_die (context_die, child);
19060 else
19061 gen_decl_die (member, NULL, context_die);
19064 /* Now output info about the function members (if any). */
19065 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19067 /* Don't include clones in the member list. */
19068 if (DECL_ABSTRACT_ORIGIN (member))
19069 continue;
19071 child = lookup_decl_die (member);
19072 if (child)
19073 splice_child_die (context_die, child);
19074 else
19075 gen_decl_die (member, NULL, context_die);
19079 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
19080 is set, we pretend that the type was never defined, so we only get the
19081 member DIEs needed by later specification DIEs. */
19083 static void
19084 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19085 enum debug_info_usage usage)
19087 dw_die_ref type_die = lookup_type_die (type);
19088 dw_die_ref scope_die = 0;
19089 int nested = 0;
19090 int complete = (TYPE_SIZE (type)
19091 && (! TYPE_STUB_DECL (type)
19092 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19093 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19094 complete = complete && should_emit_struct_debug (type, usage);
19096 if (type_die && ! complete)
19097 return;
19099 if (TYPE_CONTEXT (type) != NULL_TREE
19100 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19101 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19102 nested = 1;
19104 scope_die = scope_die_for (type, context_die);
19106 if (! type_die || (nested && is_cu_die (scope_die)))
19107 /* First occurrence of type or toplevel definition of nested class. */
19109 dw_die_ref old_die = type_die;
19111 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
19112 ? record_type_tag (type) : DW_TAG_union_type,
19113 scope_die, type);
19114 equate_type_number_to_die (type, type_die);
19115 if (old_die)
19116 add_AT_specification (type_die, old_die);
19117 else
19118 add_name_attribute (type_die, type_tag (type));
19120 else
19121 remove_AT (type_die, DW_AT_declaration);
19123 /* Generate child dies for template paramaters. */
19124 if (debug_info_level > DINFO_LEVEL_TERSE
19125 && COMPLETE_TYPE_P (type))
19126 schedule_generic_params_dies_gen (type);
19128 /* If this type has been completed, then give it a byte_size attribute and
19129 then give a list of members. */
19130 if (complete && !ns_decl)
19132 /* Prevent infinite recursion in cases where the type of some member of
19133 this type is expressed in terms of this type itself. */
19134 TREE_ASM_WRITTEN (type) = 1;
19135 add_byte_size_attribute (type_die, type);
19136 if (TYPE_STUB_DECL (type) != NULL_TREE)
19138 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19139 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19142 /* If the first reference to this type was as the return type of an
19143 inline function, then it may not have a parent. Fix this now. */
19144 if (type_die->die_parent == NULL)
19145 add_child_die (scope_die, type_die);
19147 push_decl_scope (type);
19148 gen_member_die (type, type_die);
19149 pop_decl_scope ();
19151 add_gnat_descriptive_type_attribute (type_die, type, context_die);
19152 if (TYPE_ARTIFICIAL (type))
19153 add_AT_flag (type_die, DW_AT_artificial, 1);
19155 /* GNU extension: Record what type our vtable lives in. */
19156 if (TYPE_VFIELD (type))
19158 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
19160 gen_type_die (vtype, context_die);
19161 add_AT_die_ref (type_die, DW_AT_containing_type,
19162 lookup_type_die (vtype));
19165 else
19167 add_AT_flag (type_die, DW_AT_declaration, 1);
19169 /* We don't need to do this for function-local types. */
19170 if (TYPE_STUB_DECL (type)
19171 && ! decl_function_context (TYPE_STUB_DECL (type)))
19172 vec_safe_push (incomplete_types, type);
19175 if (get_AT (type_die, DW_AT_name))
19176 add_pubtype (type, type_die);
19179 /* Generate a DIE for a subroutine _type_. */
19181 static void
19182 gen_subroutine_type_die (tree type, dw_die_ref context_die)
19184 tree return_type = TREE_TYPE (type);
19185 dw_die_ref subr_die
19186 = new_die (DW_TAG_subroutine_type,
19187 scope_die_for (type, context_die), type);
19189 equate_type_number_to_die (type, subr_die);
19190 add_prototyped_attribute (subr_die, type);
19191 add_type_attribute (subr_die, return_type, 0, 0, context_die);
19192 gen_formal_types_die (type, subr_die);
19194 if (get_AT (subr_die, DW_AT_name))
19195 add_pubtype (type, subr_die);
19198 /* Generate a DIE for a type definition. */
19200 static void
19201 gen_typedef_die (tree decl, dw_die_ref context_die)
19203 dw_die_ref type_die;
19204 tree origin;
19206 if (TREE_ASM_WRITTEN (decl))
19207 return;
19209 TREE_ASM_WRITTEN (decl) = 1;
19210 type_die = new_die (DW_TAG_typedef, context_die, decl);
19211 origin = decl_ultimate_origin (decl);
19212 if (origin != NULL)
19213 add_abstract_origin_attribute (type_die, origin);
19214 else
19216 tree type;
19218 add_name_and_src_coords_attributes (type_die, decl);
19219 if (DECL_ORIGINAL_TYPE (decl))
19221 type = DECL_ORIGINAL_TYPE (decl);
19223 gcc_assert (type != TREE_TYPE (decl));
19224 equate_type_number_to_die (TREE_TYPE (decl), type_die);
19226 else
19228 type = TREE_TYPE (decl);
19230 if (is_naming_typedef_decl (TYPE_NAME (type)))
19232 /* Here, we are in the case of decl being a typedef naming
19233 an anonymous type, e.g:
19234 typedef struct {...} foo;
19235 In that case TREE_TYPE (decl) is not a typedef variant
19236 type and TYPE_NAME of the anonymous type is set to the
19237 TYPE_DECL of the typedef. This construct is emitted by
19238 the C++ FE.
19240 TYPE is the anonymous struct named by the typedef
19241 DECL. As we need the DW_AT_type attribute of the
19242 DW_TAG_typedef to point to the DIE of TYPE, let's
19243 generate that DIE right away. add_type_attribute
19244 called below will then pick (via lookup_type_die) that
19245 anonymous struct DIE. */
19246 if (!TREE_ASM_WRITTEN (type))
19247 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
19249 /* This is a GNU Extension. We are adding a
19250 DW_AT_linkage_name attribute to the DIE of the
19251 anonymous struct TYPE. The value of that attribute
19252 is the name of the typedef decl naming the anonymous
19253 struct. This greatly eases the work of consumers of
19254 this debug info. */
19255 add_linkage_attr (lookup_type_die (type), decl);
19259 add_type_attribute (type_die, type, TREE_READONLY (decl),
19260 TREE_THIS_VOLATILE (decl), context_die);
19262 if (is_naming_typedef_decl (decl))
19263 /* We want that all subsequent calls to lookup_type_die with
19264 TYPE in argument yield the DW_TAG_typedef we have just
19265 created. */
19266 equate_type_number_to_die (type, type_die);
19268 add_accessibility_attribute (type_die, decl);
19271 if (DECL_ABSTRACT (decl))
19272 equate_decl_number_to_die (decl, type_die);
19274 if (get_AT (type_die, DW_AT_name))
19275 add_pubtype (decl, type_die);
19278 /* Generate a DIE for a struct, class, enum or union type. */
19280 static void
19281 gen_tagged_type_die (tree type,
19282 dw_die_ref context_die,
19283 enum debug_info_usage usage)
19285 int need_pop;
19287 if (type == NULL_TREE
19288 || !is_tagged_type (type))
19289 return;
19291 /* If this is a nested type whose containing class hasn't been written
19292 out yet, writing it out will cover this one, too. This does not apply
19293 to instantiations of member class templates; they need to be added to
19294 the containing class as they are generated. FIXME: This hurts the
19295 idea of combining type decls from multiple TUs, since we can't predict
19296 what set of template instantiations we'll get. */
19297 if (TYPE_CONTEXT (type)
19298 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19299 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
19301 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
19303 if (TREE_ASM_WRITTEN (type))
19304 return;
19306 /* If that failed, attach ourselves to the stub. */
19307 push_decl_scope (TYPE_CONTEXT (type));
19308 context_die = lookup_type_die (TYPE_CONTEXT (type));
19309 need_pop = 1;
19311 else if (TYPE_CONTEXT (type) != NULL_TREE
19312 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
19314 /* If this type is local to a function that hasn't been written
19315 out yet, use a NULL context for now; it will be fixed up in
19316 decls_for_scope. */
19317 context_die = lookup_decl_die (TYPE_CONTEXT (type));
19318 /* A declaration DIE doesn't count; nested types need to go in the
19319 specification. */
19320 if (context_die && is_declaration_die (context_die))
19321 context_die = NULL;
19322 need_pop = 0;
19324 else
19326 context_die = declare_in_namespace (type, context_die);
19327 need_pop = 0;
19330 if (TREE_CODE (type) == ENUMERAL_TYPE)
19332 /* This might have been written out by the call to
19333 declare_in_namespace. */
19334 if (!TREE_ASM_WRITTEN (type))
19335 gen_enumeration_type_die (type, context_die);
19337 else
19338 gen_struct_or_union_type_die (type, context_die, usage);
19340 if (need_pop)
19341 pop_decl_scope ();
19343 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
19344 it up if it is ever completed. gen_*_type_die will set it for us
19345 when appropriate. */
19348 /* Generate a type description DIE. */
19350 static void
19351 gen_type_die_with_usage (tree type, dw_die_ref context_die,
19352 enum debug_info_usage usage)
19354 struct array_descr_info info;
19356 if (type == NULL_TREE || type == error_mark_node)
19357 return;
19359 if (TYPE_NAME (type) != NULL_TREE
19360 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
19361 && is_redundant_typedef (TYPE_NAME (type))
19362 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
19363 /* The DECL of this type is a typedef we don't want to emit debug
19364 info for but we want debug info for its underlying typedef.
19365 This can happen for e.g, the injected-class-name of a C++
19366 type. */
19367 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
19369 /* If TYPE is a typedef type variant, let's generate debug info
19370 for the parent typedef which TYPE is a type of. */
19371 if (typedef_variant_p (type))
19373 if (TREE_ASM_WRITTEN (type))
19374 return;
19376 /* Prevent broken recursion; we can't hand off to the same type. */
19377 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
19379 /* Give typedefs the right scope. */
19380 context_die = scope_die_for (type, context_die);
19382 TREE_ASM_WRITTEN (type) = 1;
19384 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19385 return;
19388 /* If type is an anonymous tagged type named by a typedef, let's
19389 generate debug info for the typedef. */
19390 if (is_naming_typedef_decl (TYPE_NAME (type)))
19392 /* Use the DIE of the containing namespace as the parent DIE of
19393 the type description DIE we want to generate. */
19394 if (DECL_CONTEXT (TYPE_NAME (type))
19395 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
19396 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
19398 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19399 return;
19402 /* If this is an array type with hidden descriptor, handle it first. */
19403 if (!TREE_ASM_WRITTEN (type)
19404 && lang_hooks.types.get_array_descr_info
19405 && lang_hooks.types.get_array_descr_info (type, &info)
19406 && (dwarf_version >= 3 || !dwarf_strict))
19408 gen_descr_array_type_die (type, &info, context_die);
19409 TREE_ASM_WRITTEN (type) = 1;
19410 return;
19413 /* We are going to output a DIE to represent the unqualified version
19414 of this type (i.e. without any const or volatile qualifiers) so
19415 get the main variant (i.e. the unqualified version) of this type
19416 now. (Vectors are special because the debugging info is in the
19417 cloned type itself). */
19418 if (TREE_CODE (type) != VECTOR_TYPE)
19419 type = type_main_variant (type);
19421 if (TREE_ASM_WRITTEN (type))
19422 return;
19424 switch (TREE_CODE (type))
19426 case ERROR_MARK:
19427 break;
19429 case POINTER_TYPE:
19430 case REFERENCE_TYPE:
19431 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
19432 ensures that the gen_type_die recursion will terminate even if the
19433 type is recursive. Recursive types are possible in Ada. */
19434 /* ??? We could perhaps do this for all types before the switch
19435 statement. */
19436 TREE_ASM_WRITTEN (type) = 1;
19438 /* For these types, all that is required is that we output a DIE (or a
19439 set of DIEs) to represent the "basis" type. */
19440 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19441 DINFO_USAGE_IND_USE);
19442 break;
19444 case OFFSET_TYPE:
19445 /* This code is used for C++ pointer-to-data-member types.
19446 Output a description of the relevant class type. */
19447 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
19448 DINFO_USAGE_IND_USE);
19450 /* Output a description of the type of the object pointed to. */
19451 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19452 DINFO_USAGE_IND_USE);
19454 /* Now output a DIE to represent this pointer-to-data-member type
19455 itself. */
19456 gen_ptr_to_mbr_type_die (type, context_die);
19457 break;
19459 case FUNCTION_TYPE:
19460 /* Force out return type (in case it wasn't forced out already). */
19461 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19462 DINFO_USAGE_DIR_USE);
19463 gen_subroutine_type_die (type, context_die);
19464 break;
19466 case METHOD_TYPE:
19467 /* Force out return type (in case it wasn't forced out already). */
19468 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19469 DINFO_USAGE_DIR_USE);
19470 gen_subroutine_type_die (type, context_die);
19471 break;
19473 case ARRAY_TYPE:
19474 gen_array_type_die (type, context_die);
19475 break;
19477 case VECTOR_TYPE:
19478 gen_array_type_die (type, context_die);
19479 break;
19481 case ENUMERAL_TYPE:
19482 case RECORD_TYPE:
19483 case UNION_TYPE:
19484 case QUAL_UNION_TYPE:
19485 gen_tagged_type_die (type, context_die, usage);
19486 return;
19488 case VOID_TYPE:
19489 case INTEGER_TYPE:
19490 case REAL_TYPE:
19491 case FIXED_POINT_TYPE:
19492 case COMPLEX_TYPE:
19493 case BOOLEAN_TYPE:
19494 /* No DIEs needed for fundamental types. */
19495 break;
19497 case NULLPTR_TYPE:
19498 case LANG_TYPE:
19499 /* Just use DW_TAG_unspecified_type. */
19501 dw_die_ref type_die = lookup_type_die (type);
19502 if (type_die == NULL)
19504 tree name = TYPE_NAME (type);
19505 if (TREE_CODE (name) == TYPE_DECL)
19506 name = DECL_NAME (name);
19507 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
19508 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
19509 equate_type_number_to_die (type, type_die);
19512 break;
19514 default:
19515 gcc_unreachable ();
19518 TREE_ASM_WRITTEN (type) = 1;
19521 static void
19522 gen_type_die (tree type, dw_die_ref context_die)
19524 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
19527 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
19528 things which are local to the given block. */
19530 static void
19531 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
19533 int must_output_die = 0;
19534 bool inlined_func;
19536 /* Ignore blocks that are NULL. */
19537 if (stmt == NULL_TREE)
19538 return;
19540 inlined_func = inlined_function_outer_scope_p (stmt);
19542 /* If the block is one fragment of a non-contiguous block, do not
19543 process the variables, since they will have been done by the
19544 origin block. Do process subblocks. */
19545 if (BLOCK_FRAGMENT_ORIGIN (stmt))
19547 tree sub;
19549 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
19550 gen_block_die (sub, context_die, depth + 1);
19552 return;
19555 /* Determine if we need to output any Dwarf DIEs at all to represent this
19556 block. */
19557 if (inlined_func)
19558 /* The outer scopes for inlinings *must* always be represented. We
19559 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
19560 must_output_die = 1;
19561 else
19563 /* Determine if this block directly contains any "significant"
19564 local declarations which we will need to output DIEs for. */
19565 if (debug_info_level > DINFO_LEVEL_TERSE)
19566 /* We are not in terse mode so *any* local declaration counts
19567 as being a "significant" one. */
19568 must_output_die = ((BLOCK_VARS (stmt) != NULL
19569 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
19570 && (TREE_USED (stmt)
19571 || TREE_ASM_WRITTEN (stmt)
19572 || BLOCK_ABSTRACT (stmt)));
19573 else if ((TREE_USED (stmt)
19574 || TREE_ASM_WRITTEN (stmt)
19575 || BLOCK_ABSTRACT (stmt))
19576 && !dwarf2out_ignore_block (stmt))
19577 must_output_die = 1;
19580 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
19581 DIE for any block which contains no significant local declarations at
19582 all. Rather, in such cases we just call `decls_for_scope' so that any
19583 needed Dwarf info for any sub-blocks will get properly generated. Note
19584 that in terse mode, our definition of what constitutes a "significant"
19585 local declaration gets restricted to include only inlined function
19586 instances and local (nested) function definitions. */
19587 if (must_output_die)
19589 if (inlined_func)
19591 /* If STMT block is abstract, that means we have been called
19592 indirectly from dwarf2out_abstract_function.
19593 That function rightfully marks the descendent blocks (of
19594 the abstract function it is dealing with) as being abstract,
19595 precisely to prevent us from emitting any
19596 DW_TAG_inlined_subroutine DIE as a descendent
19597 of an abstract function instance. So in that case, we should
19598 not call gen_inlined_subroutine_die.
19600 Later though, when cgraph asks dwarf2out to emit info
19601 for the concrete instance of the function decl into which
19602 the concrete instance of STMT got inlined, the later will lead
19603 to the generation of a DW_TAG_inlined_subroutine DIE. */
19604 if (! BLOCK_ABSTRACT (stmt))
19605 gen_inlined_subroutine_die (stmt, context_die, depth);
19607 else
19608 gen_lexical_block_die (stmt, context_die, depth);
19610 else
19611 decls_for_scope (stmt, context_die, depth);
19614 /* Process variable DECL (or variable with origin ORIGIN) within
19615 block STMT and add it to CONTEXT_DIE. */
19616 static void
19617 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
19619 dw_die_ref die;
19620 tree decl_or_origin = decl ? decl : origin;
19622 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
19623 die = lookup_decl_die (decl_or_origin);
19624 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
19625 && TYPE_DECL_IS_STUB (decl_or_origin))
19626 die = lookup_type_die (TREE_TYPE (decl_or_origin));
19627 else
19628 die = NULL;
19630 if (die != NULL && die->die_parent == NULL)
19631 add_child_die (context_die, die);
19632 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
19633 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
19634 stmt, context_die);
19635 else
19636 gen_decl_die (decl, origin, context_die);
19639 /* Generate all of the decls declared within a given scope and (recursively)
19640 all of its sub-blocks. */
19642 static void
19643 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
19645 tree decl;
19646 unsigned int i;
19647 tree subblocks;
19649 /* Ignore NULL blocks. */
19650 if (stmt == NULL_TREE)
19651 return;
19653 /* Output the DIEs to represent all of the data objects and typedefs
19654 declared directly within this block but not within any nested
19655 sub-blocks. Also, nested function and tag DIEs have been
19656 generated with a parent of NULL; fix that up now. */
19657 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
19658 process_scope_var (stmt, decl, NULL_TREE, context_die);
19659 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
19660 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
19661 context_die);
19663 /* If we're at -g1, we're not interested in subblocks. */
19664 if (debug_info_level <= DINFO_LEVEL_TERSE)
19665 return;
19667 /* Output the DIEs to represent all sub-blocks (and the items declared
19668 therein) of this block. */
19669 for (subblocks = BLOCK_SUBBLOCKS (stmt);
19670 subblocks != NULL;
19671 subblocks = BLOCK_CHAIN (subblocks))
19672 gen_block_die (subblocks, context_die, depth + 1);
19675 /* Is this a typedef we can avoid emitting? */
19677 static inline int
19678 is_redundant_typedef (const_tree decl)
19680 if (TYPE_DECL_IS_STUB (decl))
19681 return 1;
19683 if (DECL_ARTIFICIAL (decl)
19684 && DECL_CONTEXT (decl)
19685 && is_tagged_type (DECL_CONTEXT (decl))
19686 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
19687 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
19688 /* Also ignore the artificial member typedef for the class name. */
19689 return 1;
19691 return 0;
19694 /* Return TRUE if TYPE is a typedef that names a type for linkage
19695 purposes. This kind of typedefs is produced by the C++ FE for
19696 constructs like:
19698 typedef struct {...} foo;
19700 In that case, there is no typedef variant type produced for foo.
19701 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
19702 struct type. */
19704 static bool
19705 is_naming_typedef_decl (const_tree decl)
19707 if (decl == NULL_TREE
19708 || TREE_CODE (decl) != TYPE_DECL
19709 || !is_tagged_type (TREE_TYPE (decl))
19710 || DECL_IS_BUILTIN (decl)
19711 || is_redundant_typedef (decl)
19712 /* It looks like Ada produces TYPE_DECLs that are very similar
19713 to C++ naming typedefs but that have different
19714 semantics. Let's be specific to c++ for now. */
19715 || !is_cxx ())
19716 return FALSE;
19718 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
19719 && TYPE_NAME (TREE_TYPE (decl)) == decl
19720 && (TYPE_STUB_DECL (TREE_TYPE (decl))
19721 != TYPE_NAME (TREE_TYPE (decl))));
19724 /* Returns the DIE for a context. */
19726 static inline dw_die_ref
19727 get_context_die (tree context)
19729 if (context)
19731 /* Find die that represents this context. */
19732 if (TYPE_P (context))
19734 context = TYPE_MAIN_VARIANT (context);
19735 return strip_naming_typedef (context, force_type_die (context));
19737 else
19738 return force_decl_die (context);
19740 return comp_unit_die ();
19743 /* Returns the DIE for decl. A DIE will always be returned. */
19745 static dw_die_ref
19746 force_decl_die (tree decl)
19748 dw_die_ref decl_die;
19749 unsigned saved_external_flag;
19750 tree save_fn = NULL_TREE;
19751 decl_die = lookup_decl_die (decl);
19752 if (!decl_die)
19754 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
19756 decl_die = lookup_decl_die (decl);
19757 if (decl_die)
19758 return decl_die;
19760 switch (TREE_CODE (decl))
19762 case FUNCTION_DECL:
19763 /* Clear current_function_decl, so that gen_subprogram_die thinks
19764 that this is a declaration. At this point, we just want to force
19765 declaration die. */
19766 save_fn = current_function_decl;
19767 current_function_decl = NULL_TREE;
19768 gen_subprogram_die (decl, context_die);
19769 current_function_decl = save_fn;
19770 break;
19772 case VAR_DECL:
19773 /* Set external flag to force declaration die. Restore it after
19774 gen_decl_die() call. */
19775 saved_external_flag = DECL_EXTERNAL (decl);
19776 DECL_EXTERNAL (decl) = 1;
19777 gen_decl_die (decl, NULL, context_die);
19778 DECL_EXTERNAL (decl) = saved_external_flag;
19779 break;
19781 case NAMESPACE_DECL:
19782 if (dwarf_version >= 3 || !dwarf_strict)
19783 dwarf2out_decl (decl);
19784 else
19785 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
19786 decl_die = comp_unit_die ();
19787 break;
19789 case TRANSLATION_UNIT_DECL:
19790 decl_die = comp_unit_die ();
19791 break;
19793 default:
19794 gcc_unreachable ();
19797 /* We should be able to find the DIE now. */
19798 if (!decl_die)
19799 decl_die = lookup_decl_die (decl);
19800 gcc_assert (decl_die);
19803 return decl_die;
19806 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
19807 always returned. */
19809 static dw_die_ref
19810 force_type_die (tree type)
19812 dw_die_ref type_die;
19814 type_die = lookup_type_die (type);
19815 if (!type_die)
19817 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
19819 type_die = modified_type_die (type, TYPE_QUALS (type), context_die);
19820 gcc_assert (type_die);
19822 return type_die;
19825 /* Force out any required namespaces to be able to output DECL,
19826 and return the new context_die for it, if it's changed. */
19828 static dw_die_ref
19829 setup_namespace_context (tree thing, dw_die_ref context_die)
19831 tree context = (DECL_P (thing)
19832 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
19833 if (context && TREE_CODE (context) == NAMESPACE_DECL)
19834 /* Force out the namespace. */
19835 context_die = force_decl_die (context);
19837 return context_die;
19840 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
19841 type) within its namespace, if appropriate.
19843 For compatibility with older debuggers, namespace DIEs only contain
19844 declarations; all definitions are emitted at CU scope. */
19846 static dw_die_ref
19847 declare_in_namespace (tree thing, dw_die_ref context_die)
19849 dw_die_ref ns_context;
19851 if (debug_info_level <= DINFO_LEVEL_TERSE)
19852 return context_die;
19854 /* If this decl is from an inlined function, then don't try to emit it in its
19855 namespace, as we will get confused. It would have already been emitted
19856 when the abstract instance of the inline function was emitted anyways. */
19857 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
19858 return context_die;
19860 ns_context = setup_namespace_context (thing, context_die);
19862 if (ns_context != context_die)
19864 if (is_fortran ())
19865 return ns_context;
19866 if (DECL_P (thing))
19867 gen_decl_die (thing, NULL, ns_context);
19868 else
19869 gen_type_die (thing, ns_context);
19871 return context_die;
19874 /* Generate a DIE for a namespace or namespace alias. */
19876 static void
19877 gen_namespace_die (tree decl, dw_die_ref context_die)
19879 dw_die_ref namespace_die;
19881 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
19882 they are an alias of. */
19883 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
19885 /* Output a real namespace or module. */
19886 context_die = setup_namespace_context (decl, comp_unit_die ());
19887 namespace_die = new_die (is_fortran ()
19888 ? DW_TAG_module : DW_TAG_namespace,
19889 context_die, decl);
19890 /* For Fortran modules defined in different CU don't add src coords. */
19891 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
19893 const char *name = dwarf2_name (decl, 0);
19894 if (name)
19895 add_name_attribute (namespace_die, name);
19897 else
19898 add_name_and_src_coords_attributes (namespace_die, decl);
19899 if (DECL_EXTERNAL (decl))
19900 add_AT_flag (namespace_die, DW_AT_declaration, 1);
19901 equate_decl_number_to_die (decl, namespace_die);
19903 else
19905 /* Output a namespace alias. */
19907 /* Force out the namespace we are an alias of, if necessary. */
19908 dw_die_ref origin_die
19909 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
19911 if (DECL_FILE_SCOPE_P (decl)
19912 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
19913 context_die = setup_namespace_context (decl, comp_unit_die ());
19914 /* Now create the namespace alias DIE. */
19915 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
19916 add_name_and_src_coords_attributes (namespace_die, decl);
19917 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
19918 equate_decl_number_to_die (decl, namespace_die);
19920 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
19921 if (want_pubnames ())
19922 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
19925 /* Generate Dwarf debug information for a decl described by DECL.
19926 The return value is currently only meaningful for PARM_DECLs,
19927 for all other decls it returns NULL. */
19929 static dw_die_ref
19930 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
19932 tree decl_or_origin = decl ? decl : origin;
19933 tree class_origin = NULL, ultimate_origin;
19935 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
19936 return NULL;
19938 switch (TREE_CODE (decl_or_origin))
19940 case ERROR_MARK:
19941 break;
19943 case CONST_DECL:
19944 if (!is_fortran () && !is_ada ())
19946 /* The individual enumerators of an enum type get output when we output
19947 the Dwarf representation of the relevant enum type itself. */
19948 break;
19951 /* Emit its type. */
19952 gen_type_die (TREE_TYPE (decl), context_die);
19954 /* And its containing namespace. */
19955 context_die = declare_in_namespace (decl, context_die);
19957 gen_const_die (decl, context_die);
19958 break;
19960 case FUNCTION_DECL:
19961 /* Don't output any DIEs to represent mere function declarations,
19962 unless they are class members or explicit block externs. */
19963 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
19964 && DECL_FILE_SCOPE_P (decl_or_origin)
19965 && (current_function_decl == NULL_TREE
19966 || DECL_ARTIFICIAL (decl_or_origin)))
19967 break;
19969 #if 0
19970 /* FIXME */
19971 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
19972 on local redeclarations of global functions. That seems broken. */
19973 if (current_function_decl != decl)
19974 /* This is only a declaration. */;
19975 #endif
19977 /* If we're emitting a clone, emit info for the abstract instance. */
19978 if (origin || DECL_ORIGIN (decl) != decl)
19979 dwarf2out_abstract_function (origin
19980 ? DECL_ORIGIN (origin)
19981 : DECL_ABSTRACT_ORIGIN (decl));
19983 /* If we're emitting an out-of-line copy of an inline function,
19984 emit info for the abstract instance and set up to refer to it. */
19985 else if (cgraph_function_possibly_inlined_p (decl)
19986 && ! DECL_ABSTRACT (decl)
19987 && ! class_or_namespace_scope_p (context_die)
19988 /* dwarf2out_abstract_function won't emit a die if this is just
19989 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
19990 that case, because that works only if we have a die. */
19991 && DECL_INITIAL (decl) != NULL_TREE)
19993 dwarf2out_abstract_function (decl);
19994 set_decl_origin_self (decl);
19997 /* Otherwise we're emitting the primary DIE for this decl. */
19998 else if (debug_info_level > DINFO_LEVEL_TERSE)
20000 /* Before we describe the FUNCTION_DECL itself, make sure that we
20001 have its containing type. */
20002 if (!origin)
20003 origin = decl_class_context (decl);
20004 if (origin != NULL_TREE)
20005 gen_type_die (origin, context_die);
20007 /* And its return type. */
20008 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20010 /* And its virtual context. */
20011 if (DECL_VINDEX (decl) != NULL_TREE)
20012 gen_type_die (DECL_CONTEXT (decl), context_die);
20014 /* Make sure we have a member DIE for decl. */
20015 if (origin != NULL_TREE)
20016 gen_type_die_for_member (origin, decl, context_die);
20018 /* And its containing namespace. */
20019 context_die = declare_in_namespace (decl, context_die);
20022 /* Now output a DIE to represent the function itself. */
20023 if (decl)
20024 gen_subprogram_die (decl, context_die);
20025 break;
20027 case TYPE_DECL:
20028 /* If we are in terse mode, don't generate any DIEs to represent any
20029 actual typedefs. */
20030 if (debug_info_level <= DINFO_LEVEL_TERSE)
20031 break;
20033 /* In the special case of a TYPE_DECL node representing the declaration
20034 of some type tag, if the given TYPE_DECL is marked as having been
20035 instantiated from some other (original) TYPE_DECL node (e.g. one which
20036 was generated within the original definition of an inline function) we
20037 used to generate a special (abbreviated) DW_TAG_structure_type,
20038 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
20039 should be actually referencing those DIEs, as variable DIEs with that
20040 type would be emitted already in the abstract origin, so it was always
20041 removed during unused type prunning. Don't add anything in this
20042 case. */
20043 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20044 break;
20046 if (is_redundant_typedef (decl))
20047 gen_type_die (TREE_TYPE (decl), context_die);
20048 else
20049 /* Output a DIE to represent the typedef itself. */
20050 gen_typedef_die (decl, context_die);
20051 break;
20053 case LABEL_DECL:
20054 if (debug_info_level >= DINFO_LEVEL_NORMAL)
20055 gen_label_die (decl, context_die);
20056 break;
20058 case VAR_DECL:
20059 case RESULT_DECL:
20060 /* If we are in terse mode, don't generate any DIEs to represent any
20061 variable declarations or definitions. */
20062 if (debug_info_level <= DINFO_LEVEL_TERSE)
20063 break;
20065 /* Output any DIEs that are needed to specify the type of this data
20066 object. */
20067 if (decl_by_reference_p (decl_or_origin))
20068 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20069 else
20070 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20072 /* And its containing type. */
20073 class_origin = decl_class_context (decl_or_origin);
20074 if (class_origin != NULL_TREE)
20075 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
20077 /* And its containing namespace. */
20078 context_die = declare_in_namespace (decl_or_origin, context_die);
20080 /* Now output the DIE to represent the data object itself. This gets
20081 complicated because of the possibility that the VAR_DECL really
20082 represents an inlined instance of a formal parameter for an inline
20083 function. */
20084 ultimate_origin = decl_ultimate_origin (decl_or_origin);
20085 if (ultimate_origin != NULL_TREE
20086 && TREE_CODE (ultimate_origin) == PARM_DECL)
20087 gen_formal_parameter_die (decl, origin,
20088 true /* Emit name attribute. */,
20089 context_die);
20090 else
20091 gen_variable_die (decl, origin, context_die);
20092 break;
20094 case FIELD_DECL:
20095 /* Ignore the nameless fields that are used to skip bits but handle C++
20096 anonymous unions and structs. */
20097 if (DECL_NAME (decl) != NULL_TREE
20098 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
20099 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
20101 gen_type_die (member_declared_type (decl), context_die);
20102 gen_field_die (decl, context_die);
20104 break;
20106 case PARM_DECL:
20107 if (DECL_BY_REFERENCE (decl_or_origin))
20108 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20109 else
20110 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20111 return gen_formal_parameter_die (decl, origin,
20112 true /* Emit name attribute. */,
20113 context_die);
20115 case NAMESPACE_DECL:
20116 case IMPORTED_DECL:
20117 if (dwarf_version >= 3 || !dwarf_strict)
20118 gen_namespace_die (decl, context_die);
20119 break;
20121 default:
20122 /* Probably some frontend-internal decl. Assume we don't care. */
20123 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
20124 break;
20127 return NULL;
20130 /* Output debug information for global decl DECL. Called from toplev.c after
20131 compilation proper has finished. */
20133 static void
20134 dwarf2out_global_decl (tree decl)
20136 /* Output DWARF2 information for file-scope tentative data object
20137 declarations, file-scope (extern) function declarations (which
20138 had no corresponding body) and file-scope tagged type declarations
20139 and definitions which have not yet been forced out. */
20140 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
20141 dwarf2out_decl (decl);
20144 /* Output debug information for type decl DECL. Called from toplev.c
20145 and from language front ends (to record built-in types). */
20146 static void
20147 dwarf2out_type_decl (tree decl, int local)
20149 if (!local)
20150 dwarf2out_decl (decl);
20153 /* Output debug information for imported module or decl DECL.
20154 NAME is non-NULL name in the lexical block if the decl has been renamed.
20155 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
20156 that DECL belongs to.
20157 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
20158 static void
20159 dwarf2out_imported_module_or_decl_1 (tree decl,
20160 tree name,
20161 tree lexical_block,
20162 dw_die_ref lexical_block_die)
20164 expanded_location xloc;
20165 dw_die_ref imported_die = NULL;
20166 dw_die_ref at_import_die;
20168 if (TREE_CODE (decl) == IMPORTED_DECL)
20170 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
20171 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
20172 gcc_assert (decl);
20174 else
20175 xloc = expand_location (input_location);
20177 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
20179 at_import_die = force_type_die (TREE_TYPE (decl));
20180 /* For namespace N { typedef void T; } using N::T; base_type_die
20181 returns NULL, but DW_TAG_imported_declaration requires
20182 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
20183 if (!at_import_die)
20185 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
20186 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
20187 at_import_die = lookup_type_die (TREE_TYPE (decl));
20188 gcc_assert (at_import_die);
20191 else
20193 at_import_die = lookup_decl_die (decl);
20194 if (!at_import_die)
20196 /* If we're trying to avoid duplicate debug info, we may not have
20197 emitted the member decl for this field. Emit it now. */
20198 if (TREE_CODE (decl) == FIELD_DECL)
20200 tree type = DECL_CONTEXT (decl);
20202 if (TYPE_CONTEXT (type)
20203 && TYPE_P (TYPE_CONTEXT (type))
20204 && !should_emit_struct_debug (TYPE_CONTEXT (type),
20205 DINFO_USAGE_DIR_USE))
20206 return;
20207 gen_type_die_for_member (type, decl,
20208 get_context_die (TYPE_CONTEXT (type)));
20210 at_import_die = force_decl_die (decl);
20214 if (TREE_CODE (decl) == NAMESPACE_DECL)
20216 if (dwarf_version >= 3 || !dwarf_strict)
20217 imported_die = new_die (DW_TAG_imported_module,
20218 lexical_block_die,
20219 lexical_block);
20220 else
20221 return;
20223 else
20224 imported_die = new_die (DW_TAG_imported_declaration,
20225 lexical_block_die,
20226 lexical_block);
20228 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
20229 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
20230 if (name)
20231 add_AT_string (imported_die, DW_AT_name,
20232 IDENTIFIER_POINTER (name));
20233 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
20236 /* Output debug information for imported module or decl DECL.
20237 NAME is non-NULL name in context if the decl has been renamed.
20238 CHILD is true if decl is one of the renamed decls as part of
20239 importing whole module. */
20241 static void
20242 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
20243 bool child)
20245 /* dw_die_ref at_import_die; */
20246 dw_die_ref scope_die;
20248 if (debug_info_level <= DINFO_LEVEL_TERSE)
20249 return;
20251 gcc_assert (decl);
20253 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
20254 We need decl DIE for reference and scope die. First, get DIE for the decl
20255 itself. */
20257 /* Get the scope die for decl context. Use comp_unit_die for global module
20258 or decl. If die is not found for non globals, force new die. */
20259 if (context
20260 && TYPE_P (context)
20261 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
20262 return;
20264 if (!(dwarf_version >= 3 || !dwarf_strict))
20265 return;
20267 scope_die = get_context_die (context);
20269 if (child)
20271 gcc_assert (scope_die->die_child);
20272 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
20273 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
20274 scope_die = scope_die->die_child;
20277 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
20278 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
20282 /* Write the debugging output for DECL. */
20284 void
20285 dwarf2out_decl (tree decl)
20287 dw_die_ref context_die = comp_unit_die ();
20289 switch (TREE_CODE (decl))
20291 case ERROR_MARK:
20292 return;
20294 case FUNCTION_DECL:
20295 /* What we would really like to do here is to filter out all mere
20296 file-scope declarations of file-scope functions which are never
20297 referenced later within this translation unit (and keep all of ones
20298 that *are* referenced later on) but we aren't clairvoyant, so we have
20299 no idea which functions will be referenced in the future (i.e. later
20300 on within the current translation unit). So here we just ignore all
20301 file-scope function declarations which are not also definitions. If
20302 and when the debugger needs to know something about these functions,
20303 it will have to hunt around and find the DWARF information associated
20304 with the definition of the function.
20306 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
20307 nodes represent definitions and which ones represent mere
20308 declarations. We have to check DECL_INITIAL instead. That's because
20309 the C front-end supports some weird semantics for "extern inline"
20310 function definitions. These can get inlined within the current
20311 translation unit (and thus, we need to generate Dwarf info for their
20312 abstract instances so that the Dwarf info for the concrete inlined
20313 instances can have something to refer to) but the compiler never
20314 generates any out-of-lines instances of such things (despite the fact
20315 that they *are* definitions).
20317 The important point is that the C front-end marks these "extern
20318 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
20319 them anyway. Note that the C++ front-end also plays some similar games
20320 for inline function definitions appearing within include files which
20321 also contain `#pragma interface' pragmas.
20323 If we are called from dwarf2out_abstract_function output a DIE
20324 anyway. We can end up here this way with early inlining and LTO
20325 where the inlined function is output in a different LTRANS unit
20326 or not at all. */
20327 if (DECL_INITIAL (decl) == NULL_TREE
20328 && ! DECL_ABSTRACT (decl))
20329 return;
20331 /* If we're a nested function, initially use a parent of NULL; if we're
20332 a plain function, this will be fixed up in decls_for_scope. If
20333 we're a method, it will be ignored, since we already have a DIE. */
20334 if (decl_function_context (decl)
20335 /* But if we're in terse mode, we don't care about scope. */
20336 && debug_info_level > DINFO_LEVEL_TERSE)
20337 context_die = NULL;
20338 break;
20340 case VAR_DECL:
20341 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
20342 declaration and if the declaration was never even referenced from
20343 within this entire compilation unit. We suppress these DIEs in
20344 order to save space in the .debug section (by eliminating entries
20345 which are probably useless). Note that we must not suppress
20346 block-local extern declarations (whether used or not) because that
20347 would screw-up the debugger's name lookup mechanism and cause it to
20348 miss things which really ought to be in scope at a given point. */
20349 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
20350 return;
20352 /* For local statics lookup proper context die. */
20353 if (TREE_STATIC (decl)
20354 && DECL_CONTEXT (decl)
20355 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
20356 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20358 /* If we are in terse mode, don't generate any DIEs to represent any
20359 variable declarations or definitions. */
20360 if (debug_info_level <= DINFO_LEVEL_TERSE)
20361 return;
20362 break;
20364 case CONST_DECL:
20365 if (debug_info_level <= DINFO_LEVEL_TERSE)
20366 return;
20367 if (!is_fortran () && !is_ada ())
20368 return;
20369 if (TREE_STATIC (decl) && decl_function_context (decl))
20370 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20371 break;
20373 case NAMESPACE_DECL:
20374 case IMPORTED_DECL:
20375 if (debug_info_level <= DINFO_LEVEL_TERSE)
20376 return;
20377 if (lookup_decl_die (decl) != NULL)
20378 return;
20379 break;
20381 case TYPE_DECL:
20382 /* Don't emit stubs for types unless they are needed by other DIEs. */
20383 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
20384 return;
20386 /* Don't bother trying to generate any DIEs to represent any of the
20387 normal built-in types for the language we are compiling. */
20388 if (DECL_IS_BUILTIN (decl))
20389 return;
20391 /* If we are in terse mode, don't generate any DIEs for types. */
20392 if (debug_info_level <= DINFO_LEVEL_TERSE)
20393 return;
20395 /* If we're a function-scope tag, initially use a parent of NULL;
20396 this will be fixed up in decls_for_scope. */
20397 if (decl_function_context (decl))
20398 context_die = NULL;
20400 break;
20402 default:
20403 return;
20406 gen_decl_die (decl, NULL, context_die);
20409 /* Write the debugging output for DECL. */
20411 static void
20412 dwarf2out_function_decl (tree decl)
20414 dwarf2out_decl (decl);
20415 call_arg_locations = NULL;
20416 call_arg_loc_last = NULL;
20417 call_site_count = -1;
20418 tail_call_site_count = -1;
20419 block_map.release ();
20420 htab_empty (decl_loc_table);
20421 htab_empty (cached_dw_loc_list_table);
20424 /* Output a marker (i.e. a label) for the beginning of the generated code for
20425 a lexical block. */
20427 static void
20428 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
20429 unsigned int blocknum)
20431 switch_to_section (current_function_section ());
20432 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
20435 /* Output a marker (i.e. a label) for the end of the generated code for a
20436 lexical block. */
20438 static void
20439 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
20441 switch_to_section (current_function_section ());
20442 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
20445 /* Returns nonzero if it is appropriate not to emit any debugging
20446 information for BLOCK, because it doesn't contain any instructions.
20448 Don't allow this for blocks with nested functions or local classes
20449 as we would end up with orphans, and in the presence of scheduling
20450 we may end up calling them anyway. */
20452 static bool
20453 dwarf2out_ignore_block (const_tree block)
20455 tree decl;
20456 unsigned int i;
20458 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
20459 if (TREE_CODE (decl) == FUNCTION_DECL
20460 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20461 return 0;
20462 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
20464 decl = BLOCK_NONLOCALIZED_VAR (block, i);
20465 if (TREE_CODE (decl) == FUNCTION_DECL
20466 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20467 return 0;
20470 return 1;
20473 /* Hash table routines for file_hash. */
20475 static int
20476 file_table_eq (const void *p1_p, const void *p2_p)
20478 const struct dwarf_file_data *const p1 =
20479 (const struct dwarf_file_data *) p1_p;
20480 const char *const p2 = (const char *) p2_p;
20481 return filename_cmp (p1->filename, p2) == 0;
20484 static hashval_t
20485 file_table_hash (const void *p_p)
20487 const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
20488 return htab_hash_string (p->filename);
20491 /* Lookup FILE_NAME (in the list of filenames that we know about here in
20492 dwarf2out.c) and return its "index". The index of each (known) filename is
20493 just a unique number which is associated with only that one filename. We
20494 need such numbers for the sake of generating labels (in the .debug_sfnames
20495 section) and references to those files numbers (in the .debug_srcinfo
20496 and.debug_macinfo sections). If the filename given as an argument is not
20497 found in our current list, add it to the list and assign it the next
20498 available unique index number. In order to speed up searches, we remember
20499 the index of the filename was looked up last. This handles the majority of
20500 all searches. */
20502 static struct dwarf_file_data *
20503 lookup_filename (const char *file_name)
20505 void ** slot;
20506 struct dwarf_file_data * created;
20508 /* Check to see if the file name that was searched on the previous
20509 call matches this file name. If so, return the index. */
20510 if (file_table_last_lookup
20511 && (file_name == file_table_last_lookup->filename
20512 || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
20513 return file_table_last_lookup;
20515 /* Didn't match the previous lookup, search the table. */
20516 slot = htab_find_slot_with_hash (file_table, file_name,
20517 htab_hash_string (file_name), INSERT);
20518 if (*slot)
20519 return (struct dwarf_file_data *) *slot;
20521 created = ggc_alloc_dwarf_file_data ();
20522 created->filename = file_name;
20523 created->emitted_number = 0;
20524 *slot = created;
20525 return created;
20528 /* If the assembler will construct the file table, then translate the compiler
20529 internal file table number into the assembler file table number, and emit
20530 a .file directive if we haven't already emitted one yet. The file table
20531 numbers are different because we prune debug info for unused variables and
20532 types, which may include filenames. */
20534 static int
20535 maybe_emit_file (struct dwarf_file_data * fd)
20537 if (! fd->emitted_number)
20539 if (last_emitted_file)
20540 fd->emitted_number = last_emitted_file->emitted_number + 1;
20541 else
20542 fd->emitted_number = 1;
20543 last_emitted_file = fd;
20545 if (DWARF2_ASM_LINE_DEBUG_INFO)
20547 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
20548 output_quoted_string (asm_out_file,
20549 remap_debug_filename (fd->filename));
20550 fputc ('\n', asm_out_file);
20554 return fd->emitted_number;
20557 /* Schedule generation of a DW_AT_const_value attribute to DIE.
20558 That generation should happen after function debug info has been
20559 generated. The value of the attribute is the constant value of ARG. */
20561 static void
20562 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
20564 die_arg_entry entry;
20566 if (!die || !arg)
20567 return;
20569 if (!tmpl_value_parm_die_table)
20570 vec_alloc (tmpl_value_parm_die_table, 32);
20572 entry.die = die;
20573 entry.arg = arg;
20574 vec_safe_push (tmpl_value_parm_die_table, entry);
20577 /* Return TRUE if T is an instance of generic type, FALSE
20578 otherwise. */
20580 static bool
20581 generic_type_p (tree t)
20583 if (t == NULL_TREE || !TYPE_P (t))
20584 return false;
20585 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
20588 /* Schedule the generation of the generic parameter dies for the
20589 instance of generic type T. The proper generation itself is later
20590 done by gen_scheduled_generic_parms_dies. */
20592 static void
20593 schedule_generic_params_dies_gen (tree t)
20595 if (!generic_type_p (t))
20596 return;
20598 if (!generic_type_instances)
20599 vec_alloc (generic_type_instances, 256);
20601 vec_safe_push (generic_type_instances, t);
20604 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
20605 by append_entry_to_tmpl_value_parm_die_table. This function must
20606 be called after function DIEs have been generated. */
20608 static void
20609 gen_remaining_tmpl_value_param_die_attribute (void)
20611 if (tmpl_value_parm_die_table)
20613 unsigned i;
20614 die_arg_entry *e;
20616 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
20617 tree_add_const_value_attribute (e->die, e->arg);
20621 /* Generate generic parameters DIEs for instances of generic types
20622 that have been previously scheduled by
20623 schedule_generic_params_dies_gen. This function must be called
20624 after all the types of the CU have been laid out. */
20626 static void
20627 gen_scheduled_generic_parms_dies (void)
20629 unsigned i;
20630 tree t;
20632 if (!generic_type_instances)
20633 return;
20635 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
20636 gen_generic_params_dies (t);
20640 /* Replace DW_AT_name for the decl with name. */
20642 static void
20643 dwarf2out_set_name (tree decl, tree name)
20645 dw_die_ref die;
20646 dw_attr_ref attr;
20647 const char *dname;
20649 die = TYPE_SYMTAB_DIE (decl);
20650 if (!die)
20651 return;
20653 dname = dwarf2_name (name, 0);
20654 if (!dname)
20655 return;
20657 attr = get_AT (die, DW_AT_name);
20658 if (attr)
20660 struct indirect_string_node *node;
20662 node = find_AT_string (dname);
20663 /* replace the string. */
20664 attr->dw_attr_val.v.val_str = node;
20667 else
20668 add_name_attribute (die, dname);
20671 /* Called by the final INSN scan whenever we see a var location. We
20672 use it to drop labels in the right places, and throw the location in
20673 our lookup table. */
20675 static void
20676 dwarf2out_var_location (rtx loc_note)
20678 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
20679 struct var_loc_node *newloc;
20680 rtx next_real, next_note;
20681 static const char *last_label;
20682 static const char *last_postcall_label;
20683 static bool last_in_cold_section_p;
20684 static rtx expected_next_loc_note;
20685 tree decl;
20686 bool var_loc_p;
20688 if (!NOTE_P (loc_note))
20690 if (CALL_P (loc_note))
20692 call_site_count++;
20693 if (SIBLING_CALL_P (loc_note))
20694 tail_call_site_count++;
20696 return;
20699 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
20700 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
20701 return;
20703 /* Optimize processing a large consecutive sequence of location
20704 notes so we don't spend too much time in next_real_insn. If the
20705 next insn is another location note, remember the next_real_insn
20706 calculation for next time. */
20707 next_real = cached_next_real_insn;
20708 if (next_real)
20710 if (expected_next_loc_note != loc_note)
20711 next_real = NULL_RTX;
20714 next_note = NEXT_INSN (loc_note);
20715 if (! next_note
20716 || INSN_DELETED_P (next_note)
20717 || GET_CODE (next_note) != NOTE
20718 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
20719 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
20720 next_note = NULL_RTX;
20722 if (! next_real)
20723 next_real = next_real_insn (loc_note);
20725 if (next_note)
20727 expected_next_loc_note = next_note;
20728 cached_next_real_insn = next_real;
20730 else
20731 cached_next_real_insn = NULL_RTX;
20733 /* If there are no instructions which would be affected by this note,
20734 don't do anything. */
20735 if (var_loc_p
20736 && next_real == NULL_RTX
20737 && !NOTE_DURING_CALL_P (loc_note))
20738 return;
20740 if (next_real == NULL_RTX)
20741 next_real = get_last_insn ();
20743 /* If there were any real insns between note we processed last time
20744 and this note (or if it is the first note), clear
20745 last_{,postcall_}label so that they are not reused this time. */
20746 if (last_var_location_insn == NULL_RTX
20747 || last_var_location_insn != next_real
20748 || last_in_cold_section_p != in_cold_section_p)
20750 last_label = NULL;
20751 last_postcall_label = NULL;
20754 if (var_loc_p)
20756 decl = NOTE_VAR_LOCATION_DECL (loc_note);
20757 newloc = add_var_loc_to_decl (decl, loc_note,
20758 NOTE_DURING_CALL_P (loc_note)
20759 ? last_postcall_label : last_label);
20760 if (newloc == NULL)
20761 return;
20763 else
20765 decl = NULL_TREE;
20766 newloc = NULL;
20769 /* If there were no real insns between note we processed last time
20770 and this note, use the label we emitted last time. Otherwise
20771 create a new label and emit it. */
20772 if (last_label == NULL)
20774 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
20775 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
20776 loclabel_num++;
20777 last_label = ggc_strdup (loclabel);
20780 if (!var_loc_p)
20782 struct call_arg_loc_node *ca_loc
20783 = ggc_alloc_cleared_call_arg_loc_node ();
20784 rtx prev = prev_real_insn (loc_note), x;
20785 ca_loc->call_arg_loc_note = loc_note;
20786 ca_loc->next = NULL;
20787 ca_loc->label = last_label;
20788 gcc_assert (prev
20789 && (CALL_P (prev)
20790 || (NONJUMP_INSN_P (prev)
20791 && GET_CODE (PATTERN (prev)) == SEQUENCE
20792 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
20793 if (!CALL_P (prev))
20794 prev = XVECEXP (PATTERN (prev), 0, 0);
20795 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
20796 x = get_call_rtx_from (PATTERN (prev));
20797 if (x)
20799 x = XEXP (XEXP (x, 0), 0);
20800 if (GET_CODE (x) == SYMBOL_REF
20801 && SYMBOL_REF_DECL (x)
20802 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
20803 ca_loc->symbol_ref = x;
20805 ca_loc->block = insn_scope (prev);
20806 if (call_arg_locations)
20807 call_arg_loc_last->next = ca_loc;
20808 else
20809 call_arg_locations = ca_loc;
20810 call_arg_loc_last = ca_loc;
20812 else if (!NOTE_DURING_CALL_P (loc_note))
20813 newloc->label = last_label;
20814 else
20816 if (!last_postcall_label)
20818 sprintf (loclabel, "%s-1", last_label);
20819 last_postcall_label = ggc_strdup (loclabel);
20821 newloc->label = last_postcall_label;
20824 last_var_location_insn = next_real;
20825 last_in_cold_section_p = in_cold_section_p;
20828 /* Note in one location list that text section has changed. */
20830 static int
20831 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
20833 var_loc_list *list = (var_loc_list *) *slot;
20834 if (list->first)
20835 list->last_before_switch
20836 = list->last->next ? list->last->next : list->last;
20837 return 1;
20840 /* Note in all location lists that text section has changed. */
20842 static void
20843 var_location_switch_text_section (void)
20845 if (decl_loc_table == NULL)
20846 return;
20848 htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
20851 /* Create a new line number table. */
20853 static dw_line_info_table *
20854 new_line_info_table (void)
20856 dw_line_info_table *table;
20858 table = ggc_alloc_cleared_dw_line_info_table_struct ();
20859 table->file_num = 1;
20860 table->line_num = 1;
20861 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
20863 return table;
20866 /* Lookup the "current" table into which we emit line info, so
20867 that we don't have to do it for every source line. */
20869 static void
20870 set_cur_line_info_table (section *sec)
20872 dw_line_info_table *table;
20874 if (sec == text_section)
20875 table = text_section_line_info;
20876 else if (sec == cold_text_section)
20878 table = cold_text_section_line_info;
20879 if (!table)
20881 cold_text_section_line_info = table = new_line_info_table ();
20882 table->end_label = cold_end_label;
20885 else
20887 const char *end_label;
20889 if (flag_reorder_blocks_and_partition)
20891 if (in_cold_section_p)
20892 end_label = crtl->subsections.cold_section_end_label;
20893 else
20894 end_label = crtl->subsections.hot_section_end_label;
20896 else
20898 char label[MAX_ARTIFICIAL_LABEL_BYTES];
20899 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
20900 current_function_funcdef_no);
20901 end_label = ggc_strdup (label);
20904 table = new_line_info_table ();
20905 table->end_label = end_label;
20907 vec_safe_push (separate_line_info, table);
20910 if (DWARF2_ASM_LINE_DEBUG_INFO)
20911 table->is_stmt = (cur_line_info_table
20912 ? cur_line_info_table->is_stmt
20913 : DWARF_LINE_DEFAULT_IS_STMT_START);
20914 cur_line_info_table = table;
20918 /* We need to reset the locations at the beginning of each
20919 function. We can't do this in the end_function hook, because the
20920 declarations that use the locations won't have been output when
20921 that hook is called. Also compute have_multiple_function_sections here. */
20923 static void
20924 dwarf2out_begin_function (tree fun)
20926 section *sec = function_section (fun);
20928 if (sec != text_section)
20929 have_multiple_function_sections = true;
20931 if (flag_reorder_blocks_and_partition && !cold_text_section)
20933 gcc_assert (current_function_decl == fun);
20934 cold_text_section = unlikely_text_section ();
20935 switch_to_section (cold_text_section);
20936 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
20937 switch_to_section (sec);
20940 dwarf2out_note_section_used ();
20941 call_site_count = 0;
20942 tail_call_site_count = 0;
20944 set_cur_line_info_table (sec);
20947 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
20949 static void
20950 push_dw_line_info_entry (dw_line_info_table *table,
20951 enum dw_line_info_opcode opcode, unsigned int val)
20953 dw_line_info_entry e;
20954 e.opcode = opcode;
20955 e.val = val;
20956 vec_safe_push (table->entries, e);
20959 /* Output a label to mark the beginning of a source code line entry
20960 and record information relating to this source line, in
20961 'line_info_table' for later output of the .debug_line section. */
20962 /* ??? The discriminator parameter ought to be unsigned. */
20964 static void
20965 dwarf2out_source_line (unsigned int line, const char *filename,
20966 int discriminator, bool is_stmt)
20968 unsigned int file_num;
20969 dw_line_info_table *table;
20971 if (debug_info_level < DINFO_LEVEL_NORMAL || line == 0)
20972 return;
20974 /* The discriminator column was added in dwarf4. Simplify the below
20975 by simply removing it if we're not supposed to output it. */
20976 if (dwarf_version < 4 && dwarf_strict)
20977 discriminator = 0;
20979 table = cur_line_info_table;
20980 file_num = maybe_emit_file (lookup_filename (filename));
20982 /* ??? TODO: Elide duplicate line number entries. Traditionally,
20983 the debugger has used the second (possibly duplicate) line number
20984 at the beginning of the function to mark the end of the prologue.
20985 We could eliminate any other duplicates within the function. For
20986 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
20987 that second line number entry. */
20988 /* Recall that this end-of-prologue indication is *not* the same thing
20989 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
20990 to which the hook corresponds, follows the last insn that was
20991 emitted by gen_prologue. What we need is to precede the first insn
20992 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
20993 insn that corresponds to something the user wrote. These may be
20994 very different locations once scheduling is enabled. */
20996 if (0 && file_num == table->file_num
20997 && line == table->line_num
20998 && discriminator == table->discrim_num
20999 && is_stmt == table->is_stmt)
21000 return;
21002 switch_to_section (current_function_section ());
21004 /* If requested, emit something human-readable. */
21005 if (flag_debug_asm)
21006 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
21008 if (DWARF2_ASM_LINE_DEBUG_INFO)
21010 /* Emit the .loc directive understood by GNU as. */
21011 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
21012 file_num, line, is_stmt, discriminator */
21013 fputs ("\t.loc ", asm_out_file);
21014 fprint_ul (asm_out_file, file_num);
21015 putc (' ', asm_out_file);
21016 fprint_ul (asm_out_file, line);
21017 putc (' ', asm_out_file);
21018 putc ('0', asm_out_file);
21020 if (is_stmt != table->is_stmt)
21022 fputs (" is_stmt ", asm_out_file);
21023 putc (is_stmt ? '1' : '0', asm_out_file);
21025 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
21027 gcc_assert (discriminator > 0);
21028 fputs (" discriminator ", asm_out_file);
21029 fprint_ul (asm_out_file, (unsigned long) discriminator);
21031 putc ('\n', asm_out_file);
21033 else
21035 unsigned int label_num = ++line_info_label_num;
21037 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
21039 push_dw_line_info_entry (table, LI_set_address, label_num);
21040 if (file_num != table->file_num)
21041 push_dw_line_info_entry (table, LI_set_file, file_num);
21042 if (discriminator != table->discrim_num)
21043 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
21044 if (is_stmt != table->is_stmt)
21045 push_dw_line_info_entry (table, LI_negate_stmt, 0);
21046 push_dw_line_info_entry (table, LI_set_line, line);
21049 table->file_num = file_num;
21050 table->line_num = line;
21051 table->discrim_num = discriminator;
21052 table->is_stmt = is_stmt;
21053 table->in_use = true;
21056 /* Record the beginning of a new source file. */
21058 static void
21059 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
21061 if (flag_eliminate_dwarf2_dups)
21063 /* Record the beginning of the file for break_out_includes. */
21064 dw_die_ref bincl_die;
21066 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
21067 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
21070 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21072 macinfo_entry e;
21073 e.code = DW_MACINFO_start_file;
21074 e.lineno = lineno;
21075 e.info = ggc_strdup (filename);
21076 vec_safe_push (macinfo_table, e);
21080 /* Record the end of a source file. */
21082 static void
21083 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
21085 if (flag_eliminate_dwarf2_dups)
21086 /* Record the end of the file for break_out_includes. */
21087 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
21089 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21091 macinfo_entry e;
21092 e.code = DW_MACINFO_end_file;
21093 e.lineno = lineno;
21094 e.info = NULL;
21095 vec_safe_push (macinfo_table, e);
21099 /* Called from debug_define in toplev.c. The `buffer' parameter contains
21100 the tail part of the directive line, i.e. the part which is past the
21101 initial whitespace, #, whitespace, directive-name, whitespace part. */
21103 static void
21104 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
21105 const char *buffer ATTRIBUTE_UNUSED)
21107 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21109 macinfo_entry e;
21110 /* Insert a dummy first entry to be able to optimize the whole
21111 predefined macro block using DW_MACRO_GNU_transparent_include. */
21112 if (macinfo_table->is_empty () && lineno <= 1)
21114 e.code = 0;
21115 e.lineno = 0;
21116 e.info = NULL;
21117 vec_safe_push (macinfo_table, e);
21119 e.code = DW_MACINFO_define;
21120 e.lineno = lineno;
21121 e.info = ggc_strdup (buffer);
21122 vec_safe_push (macinfo_table, e);
21126 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
21127 the tail part of the directive line, i.e. the part which is past the
21128 initial whitespace, #, whitespace, directive-name, whitespace part. */
21130 static void
21131 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
21132 const char *buffer ATTRIBUTE_UNUSED)
21134 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21136 macinfo_entry e;
21137 /* Insert a dummy first entry to be able to optimize the whole
21138 predefined macro block using DW_MACRO_GNU_transparent_include. */
21139 if (macinfo_table->is_empty () && lineno <= 1)
21141 e.code = 0;
21142 e.lineno = 0;
21143 e.info = NULL;
21144 vec_safe_push (macinfo_table, e);
21146 e.code = DW_MACINFO_undef;
21147 e.lineno = lineno;
21148 e.info = ggc_strdup (buffer);
21149 vec_safe_push (macinfo_table, e);
21153 /* Routines to manipulate hash table of CUs. */
21155 static hashval_t
21156 htab_macinfo_hash (const void *of)
21158 const macinfo_entry *const entry =
21159 (const macinfo_entry *) of;
21161 return htab_hash_string (entry->info);
21164 static int
21165 htab_macinfo_eq (const void *of1, const void *of2)
21167 const macinfo_entry *const entry1 = (const macinfo_entry *) of1;
21168 const macinfo_entry *const entry2 = (const macinfo_entry *) of2;
21170 return !strcmp (entry1->info, entry2->info);
21173 /* Output a single .debug_macinfo entry. */
21175 static void
21176 output_macinfo_op (macinfo_entry *ref)
21178 int file_num;
21179 size_t len;
21180 struct indirect_string_node *node;
21181 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21182 struct dwarf_file_data *fd;
21184 switch (ref->code)
21186 case DW_MACINFO_start_file:
21187 fd = lookup_filename (ref->info);
21188 file_num = maybe_emit_file (fd);
21189 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
21190 dw2_asm_output_data_uleb128 (ref->lineno,
21191 "Included from line number %lu",
21192 (unsigned long) ref->lineno);
21193 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
21194 break;
21195 case DW_MACINFO_end_file:
21196 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
21197 break;
21198 case DW_MACINFO_define:
21199 case DW_MACINFO_undef:
21200 len = strlen (ref->info) + 1;
21201 if (!dwarf_strict
21202 && len > DWARF_OFFSET_SIZE
21203 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21204 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21206 ref->code = ref->code == DW_MACINFO_define
21207 ? DW_MACRO_GNU_define_indirect
21208 : DW_MACRO_GNU_undef_indirect;
21209 output_macinfo_op (ref);
21210 return;
21212 dw2_asm_output_data (1, ref->code,
21213 ref->code == DW_MACINFO_define
21214 ? "Define macro" : "Undefine macro");
21215 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21216 (unsigned long) ref->lineno);
21217 dw2_asm_output_nstring (ref->info, -1, "The macro");
21218 break;
21219 case DW_MACRO_GNU_define_indirect:
21220 case DW_MACRO_GNU_undef_indirect:
21221 node = find_AT_string (ref->info);
21222 gcc_assert (node
21223 && ((node->form == DW_FORM_strp)
21224 || (node->form == DW_FORM_GNU_str_index)));
21225 dw2_asm_output_data (1, ref->code,
21226 ref->code == DW_MACRO_GNU_define_indirect
21227 ? "Define macro indirect"
21228 : "Undefine macro indirect");
21229 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21230 (unsigned long) ref->lineno);
21231 if (node->form == DW_FORM_strp)
21232 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
21233 debug_str_section, "The macro: \"%s\"",
21234 ref->info);
21235 else
21236 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
21237 ref->info);
21238 break;
21239 case DW_MACRO_GNU_transparent_include:
21240 dw2_asm_output_data (1, ref->code, "Transparent include");
21241 ASM_GENERATE_INTERNAL_LABEL (label,
21242 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
21243 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
21244 break;
21245 default:
21246 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
21247 ASM_COMMENT_START, (unsigned long) ref->code);
21248 break;
21252 /* Attempt to make a sequence of define/undef macinfo ops shareable with
21253 other compilation unit .debug_macinfo sections. IDX is the first
21254 index of a define/undef, return the number of ops that should be
21255 emitted in a comdat .debug_macinfo section and emit
21256 a DW_MACRO_GNU_transparent_include entry referencing it.
21257 If the define/undef entry should be emitted normally, return 0. */
21259 static unsigned
21260 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
21261 htab_t *macinfo_htab)
21263 macinfo_entry *first, *second, *cur, *inc;
21264 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
21265 unsigned char checksum[16];
21266 struct md5_ctx ctx;
21267 char *grp_name, *tail;
21268 const char *base;
21269 unsigned int i, count, encoded_filename_len, linebuf_len;
21270 void **slot;
21272 first = &(*macinfo_table)[idx];
21273 second = &(*macinfo_table)[idx + 1];
21275 /* Optimize only if there are at least two consecutive define/undef ops,
21276 and either all of them are before first DW_MACINFO_start_file
21277 with lineno {0,1} (i.e. predefined macro block), or all of them are
21278 in some included header file. */
21279 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
21280 return 0;
21281 if (vec_safe_is_empty (files))
21283 if (first->lineno > 1 || second->lineno > 1)
21284 return 0;
21286 else if (first->lineno == 0)
21287 return 0;
21289 /* Find the last define/undef entry that can be grouped together
21290 with first and at the same time compute md5 checksum of their
21291 codes, linenumbers and strings. */
21292 md5_init_ctx (&ctx);
21293 for (i = idx; macinfo_table->iterate (i, &cur); i++)
21294 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
21295 break;
21296 else if (vec_safe_is_empty (files) && cur->lineno > 1)
21297 break;
21298 else
21300 unsigned char code = cur->code;
21301 md5_process_bytes (&code, 1, &ctx);
21302 checksum_uleb128 (cur->lineno, &ctx);
21303 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
21305 md5_finish_ctx (&ctx, checksum);
21306 count = i - idx;
21308 /* From the containing include filename (if any) pick up just
21309 usable characters from its basename. */
21310 if (vec_safe_is_empty (files))
21311 base = "";
21312 else
21313 base = lbasename (files->last ().info);
21314 for (encoded_filename_len = 0, i = 0; base[i]; i++)
21315 if (ISIDNUM (base[i]) || base[i] == '.')
21316 encoded_filename_len++;
21317 /* Count . at the end. */
21318 if (encoded_filename_len)
21319 encoded_filename_len++;
21321 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
21322 linebuf_len = strlen (linebuf);
21324 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
21325 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
21326 + 16 * 2 + 1);
21327 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
21328 tail = grp_name + 4;
21329 if (encoded_filename_len)
21331 for (i = 0; base[i]; i++)
21332 if (ISIDNUM (base[i]) || base[i] == '.')
21333 *tail++ = base[i];
21334 *tail++ = '.';
21336 memcpy (tail, linebuf, linebuf_len);
21337 tail += linebuf_len;
21338 *tail++ = '.';
21339 for (i = 0; i < 16; i++)
21340 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
21342 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
21343 in the empty vector entry before the first define/undef. */
21344 inc = &(*macinfo_table)[idx - 1];
21345 inc->code = DW_MACRO_GNU_transparent_include;
21346 inc->lineno = 0;
21347 inc->info = ggc_strdup (grp_name);
21348 if (*macinfo_htab == NULL)
21349 *macinfo_htab = htab_create (10, htab_macinfo_hash, htab_macinfo_eq, NULL);
21350 /* Avoid emitting duplicates. */
21351 slot = htab_find_slot (*macinfo_htab, inc, INSERT);
21352 if (*slot != NULL)
21354 inc->code = 0;
21355 inc->info = NULL;
21356 /* If such an entry has been used before, just emit
21357 a DW_MACRO_GNU_transparent_include op. */
21358 inc = (macinfo_entry *) *slot;
21359 output_macinfo_op (inc);
21360 /* And clear all macinfo_entry in the range to avoid emitting them
21361 in the second pass. */
21362 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
21364 cur->code = 0;
21365 cur->info = NULL;
21368 else
21370 *slot = inc;
21371 inc->lineno = htab_elements (*macinfo_htab);
21372 output_macinfo_op (inc);
21374 return count;
21377 /* Save any strings needed by the macinfo table in the debug str
21378 table. All strings must be collected into the table by the time
21379 index_string is called. */
21381 static void
21382 save_macinfo_strings (void)
21384 unsigned len;
21385 unsigned i;
21386 macinfo_entry *ref;
21388 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
21390 switch (ref->code)
21392 /* Match the logic in output_macinfo_op to decide on
21393 indirect strings. */
21394 case DW_MACINFO_define:
21395 case DW_MACINFO_undef:
21396 len = strlen (ref->info) + 1;
21397 if (!dwarf_strict
21398 && len > DWARF_OFFSET_SIZE
21399 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21400 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21401 set_indirect_string (find_AT_string (ref->info));
21402 break;
21403 case DW_MACRO_GNU_define_indirect:
21404 case DW_MACRO_GNU_undef_indirect:
21405 set_indirect_string (find_AT_string (ref->info));
21406 break;
21407 default:
21408 break;
21413 /* Output macinfo section(s). */
21415 static void
21416 output_macinfo (void)
21418 unsigned i;
21419 unsigned long length = vec_safe_length (macinfo_table);
21420 macinfo_entry *ref;
21421 vec<macinfo_entry, va_gc> *files = NULL;
21422 htab_t macinfo_htab = NULL;
21424 if (! length)
21425 return;
21427 /* output_macinfo* uses these interchangeably. */
21428 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
21429 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
21430 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
21431 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
21433 /* For .debug_macro emit the section header. */
21434 if (!dwarf_strict)
21436 dw2_asm_output_data (2, 4, "DWARF macro version number");
21437 if (DWARF_OFFSET_SIZE == 8)
21438 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
21439 else
21440 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
21441 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
21442 (!dwarf_split_debug_info ? debug_line_section_label
21443 : debug_skeleton_line_section_label),
21444 debug_line_section, NULL);
21447 /* In the first loop, it emits the primary .debug_macinfo section
21448 and after each emitted op the macinfo_entry is cleared.
21449 If a longer range of define/undef ops can be optimized using
21450 DW_MACRO_GNU_transparent_include, the
21451 DW_MACRO_GNU_transparent_include op is emitted and kept in
21452 the vector before the first define/undef in the range and the
21453 whole range of define/undef ops is not emitted and kept. */
21454 for (i = 0; macinfo_table->iterate (i, &ref); i++)
21456 switch (ref->code)
21458 case DW_MACINFO_start_file:
21459 vec_safe_push (files, *ref);
21460 break;
21461 case DW_MACINFO_end_file:
21462 if (!vec_safe_is_empty (files))
21463 files->pop ();
21464 break;
21465 case DW_MACINFO_define:
21466 case DW_MACINFO_undef:
21467 if (!dwarf_strict
21468 && HAVE_COMDAT_GROUP
21469 && vec_safe_length (files) != 1
21470 && i > 0
21471 && i + 1 < length
21472 && (*macinfo_table)[i - 1].code == 0)
21474 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
21475 if (count)
21477 i += count - 1;
21478 continue;
21481 break;
21482 case 0:
21483 /* A dummy entry may be inserted at the beginning to be able
21484 to optimize the whole block of predefined macros. */
21485 if (i == 0)
21486 continue;
21487 default:
21488 break;
21490 output_macinfo_op (ref);
21491 ref->info = NULL;
21492 ref->code = 0;
21495 if (macinfo_htab == NULL)
21496 return;
21498 htab_delete (macinfo_htab);
21500 /* If any DW_MACRO_GNU_transparent_include were used, on those
21501 DW_MACRO_GNU_transparent_include entries terminate the
21502 current chain and switch to a new comdat .debug_macinfo
21503 section and emit the define/undef entries within it. */
21504 for (i = 0; macinfo_table->iterate (i, &ref); i++)
21505 switch (ref->code)
21507 case 0:
21508 continue;
21509 case DW_MACRO_GNU_transparent_include:
21511 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21512 tree comdat_key = get_identifier (ref->info);
21513 /* Terminate the previous .debug_macinfo section. */
21514 dw2_asm_output_data (1, 0, "End compilation unit");
21515 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
21516 SECTION_DEBUG
21517 | SECTION_LINKONCE,
21518 comdat_key);
21519 ASM_GENERATE_INTERNAL_LABEL (label,
21520 DEBUG_MACRO_SECTION_LABEL,
21521 ref->lineno);
21522 ASM_OUTPUT_LABEL (asm_out_file, label);
21523 ref->code = 0;
21524 ref->info = NULL;
21525 dw2_asm_output_data (2, 4, "DWARF macro version number");
21526 if (DWARF_OFFSET_SIZE == 8)
21527 dw2_asm_output_data (1, 1, "Flags: 64-bit");
21528 else
21529 dw2_asm_output_data (1, 0, "Flags: 32-bit");
21531 break;
21532 case DW_MACINFO_define:
21533 case DW_MACINFO_undef:
21534 output_macinfo_op (ref);
21535 ref->code = 0;
21536 ref->info = NULL;
21537 break;
21538 default:
21539 gcc_unreachable ();
21543 /* Set up for Dwarf output at the start of compilation. */
21545 static void
21546 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
21548 /* Allocate the file_table. */
21549 file_table = htab_create_ggc (50, file_table_hash,
21550 file_table_eq, NULL);
21552 /* Allocate the decl_die_table. */
21553 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
21554 decl_die_table_eq, NULL);
21556 /* Allocate the decl_loc_table. */
21557 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
21558 decl_loc_table_eq, NULL);
21560 /* Allocate the cached_dw_loc_list_table. */
21561 cached_dw_loc_list_table
21562 = htab_create_ggc (10, cached_dw_loc_list_table_hash,
21563 cached_dw_loc_list_table_eq, NULL);
21565 /* Allocate the initial hunk of the decl_scope_table. */
21566 vec_alloc (decl_scope_table, 256);
21568 /* Allocate the initial hunk of the abbrev_die_table. */
21569 abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
21570 (ABBREV_DIE_TABLE_INCREMENT);
21571 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
21572 /* Zero-th entry is allocated, but unused. */
21573 abbrev_die_table_in_use = 1;
21575 /* Allocate the pubtypes and pubnames vectors. */
21576 vec_alloc (pubname_table, 32);
21577 vec_alloc (pubtype_table, 32);
21579 vec_alloc (incomplete_types, 64);
21581 vec_alloc (used_rtx_array, 32);
21583 if (!dwarf_split_debug_info)
21585 debug_info_section = get_section (DEBUG_INFO_SECTION,
21586 SECTION_DEBUG, NULL);
21587 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
21588 SECTION_DEBUG, NULL);
21589 debug_loc_section = get_section (DEBUG_LOC_SECTION,
21590 SECTION_DEBUG, NULL);
21592 else
21594 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
21595 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
21596 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
21597 SECTION_DEBUG | SECTION_EXCLUDE,
21598 NULL);
21599 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
21600 SECTION_DEBUG, NULL);
21601 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
21602 SECTION_DEBUG, NULL);
21603 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
21604 SECTION_DEBUG, NULL);
21605 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
21606 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
21608 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
21609 the main .o, but the skeleton_line goes into the split off dwo. */
21610 debug_skeleton_line_section
21611 = get_section (DEBUG_DWO_LINE_SECTION,
21612 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
21613 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
21614 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
21615 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
21616 SECTION_DEBUG | SECTION_EXCLUDE,
21617 NULL);
21618 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
21619 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
21620 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
21621 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
21623 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
21624 SECTION_DEBUG, NULL);
21625 debug_macinfo_section = get_section (dwarf_strict
21626 ? DEBUG_MACINFO_SECTION
21627 : DEBUG_MACRO_SECTION,
21628 DEBUG_MACRO_SECTION_FLAGS, NULL);
21629 debug_line_section = get_section (DEBUG_LINE_SECTION,
21630 SECTION_DEBUG, NULL);
21631 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
21632 SECTION_DEBUG, NULL);
21633 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
21634 SECTION_DEBUG, NULL);
21635 debug_str_section = get_section (DEBUG_STR_SECTION,
21636 DEBUG_STR_SECTION_FLAGS, NULL);
21637 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
21638 SECTION_DEBUG, NULL);
21639 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
21640 SECTION_DEBUG, NULL);
21642 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
21643 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
21644 DEBUG_ABBREV_SECTION_LABEL, 0);
21645 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
21646 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
21647 COLD_TEXT_SECTION_LABEL, 0);
21648 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
21650 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
21651 DEBUG_INFO_SECTION_LABEL, 0);
21652 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
21653 DEBUG_LINE_SECTION_LABEL, 0);
21654 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
21655 DEBUG_RANGES_SECTION_LABEL, 0);
21656 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
21657 DEBUG_ADDR_SECTION_LABEL, 0);
21658 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
21659 dwarf_strict
21660 ? DEBUG_MACINFO_SECTION_LABEL
21661 : DEBUG_MACRO_SECTION_LABEL, 0);
21662 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
21664 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21665 vec_alloc (macinfo_table, 64);
21667 switch_to_section (text_section);
21668 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
21670 /* Make sure the line number table for .text always exists. */
21671 text_section_line_info = new_line_info_table ();
21672 text_section_line_info->end_label = text_end_label;
21675 /* Called before compile () starts outputtting functions, variables
21676 and toplevel asms into assembly. */
21678 static void
21679 dwarf2out_assembly_start (void)
21681 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
21682 && dwarf2out_do_cfi_asm ()
21683 && (!(flag_unwind_tables || flag_exceptions)
21684 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
21685 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
21688 /* A helper function for dwarf2out_finish called through
21689 htab_traverse. Assign a string its index. All strings must be
21690 collected into the table by the time index_string is called,
21691 because the indexing code relies on htab_traverse to traverse nodes
21692 in the same order for each run. */
21694 static int
21695 index_string (void **h, void *v)
21697 struct indirect_string_node *node = (struct indirect_string_node *) *h;
21698 unsigned int *index = (unsigned int *) v;
21700 find_string_form (node);
21701 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
21703 gcc_assert(node->index == NO_INDEX_ASSIGNED);
21704 node->index = *index;
21705 *index += 1;
21707 return 1;
21710 /* A helper function for output_indirect_strings called through
21711 htab_traverse. Output the offset to a string and update the
21712 current offset. */
21714 static int
21715 output_index_string_offset (void **h, void *v)
21717 struct indirect_string_node *node = (struct indirect_string_node *) *h;
21718 unsigned int *offset = (unsigned int *) v;
21720 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
21722 /* Assert that this node has been assigned an index. */
21723 gcc_assert (node->index != NO_INDEX_ASSIGNED
21724 && node->index != NOT_INDEXED);
21725 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
21726 "indexed string 0x%x: %s", node->index, node->str);
21727 *offset += strlen (node->str) + 1;
21729 return 1;
21732 /* A helper function for dwarf2out_finish called through
21733 htab_traverse. Output the indexed string. */
21735 static int
21736 output_index_string (void **h, void *v)
21738 struct indirect_string_node *node = (struct indirect_string_node *) *h;
21739 unsigned int *cur_idx = (unsigned int *) v;
21741 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
21743 /* Assert that the strings are output in the same order as their
21744 indexes were assigned. */
21745 gcc_assert (*cur_idx == node->index);
21746 ASM_OUTPUT_LABEL (asm_out_file, node->label);
21747 assemble_string (node->str, strlen (node->str) + 1);
21748 *cur_idx += 1;
21750 return 1;
21753 /* A helper function for dwarf2out_finish called through
21754 htab_traverse. Emit one queued .debug_str string. */
21756 static int
21757 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
21759 struct indirect_string_node *node = (struct indirect_string_node *) *h;
21761 if (node->form == DW_FORM_strp && node->refcount > 0)
21763 ASM_OUTPUT_LABEL (asm_out_file, node->label);
21764 assemble_string (node->str, strlen (node->str) + 1);
21767 return 1;
21770 /* Output the indexed string table. */
21772 static void
21773 output_indirect_strings (void)
21775 if (!dwarf_split_debug_info)
21777 switch_to_section (debug_str_section);
21778 htab_traverse (debug_str_hash, output_indirect_string, NULL);
21780 else
21782 unsigned int offset = 0;
21783 unsigned int cur_idx = 0;
21785 switch_to_section (debug_str_offsets_section);
21786 htab_traverse_noresize (debug_str_hash,
21787 output_index_string_offset,
21788 &offset);
21789 switch_to_section (debug_str_section);
21790 htab_traverse_noresize (debug_str_hash,
21791 output_index_string,
21792 &cur_idx);
21796 /* Callback for htab_traverse to assign an index to an entry in the
21797 table, and to write that entry to the .debug_addr section. */
21799 static int
21800 output_addr_table_entry (void **slot, void *data)
21802 addr_table_entry *entry = (addr_table_entry *) *slot;
21803 unsigned int *cur_index = (unsigned int *)data;
21805 if (entry->refcount == 0)
21807 gcc_assert (entry->index == NO_INDEX_ASSIGNED
21808 || entry->index == NOT_INDEXED);
21809 return 1;
21812 gcc_assert (entry->index == *cur_index);
21813 (*cur_index)++;
21815 switch (entry->kind)
21817 case ate_kind_rtx:
21818 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
21819 "0x%x", entry->index);
21820 break;
21821 case ate_kind_rtx_dtprel:
21822 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
21823 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
21824 DWARF2_ADDR_SIZE,
21825 entry->addr.rtl);
21826 fputc ('\n', asm_out_file);
21827 break;
21828 case ate_kind_label:
21829 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
21830 "0x%x", entry->index);
21831 break;
21832 default:
21833 gcc_unreachable ();
21835 return 1;
21838 /* Produce the .debug_addr section. */
21840 static void
21841 output_addr_table (void)
21843 unsigned int index = 0;
21844 if (addr_index_table == NULL || htab_size (addr_index_table) == 0)
21845 return;
21847 switch_to_section (debug_addr_section);
21848 htab_traverse_noresize (addr_index_table, output_addr_table_entry, &index);
21851 #if ENABLE_ASSERT_CHECKING
21852 /* Verify that all marks are clear. */
21854 static void
21855 verify_marks_clear (dw_die_ref die)
21857 dw_die_ref c;
21859 gcc_assert (! die->die_mark);
21860 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
21862 #endif /* ENABLE_ASSERT_CHECKING */
21864 /* Clear the marks for a die and its children.
21865 Be cool if the mark isn't set. */
21867 static void
21868 prune_unmark_dies (dw_die_ref die)
21870 dw_die_ref c;
21872 if (die->die_mark)
21873 die->die_mark = 0;
21874 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
21877 /* Given DIE that we're marking as used, find any other dies
21878 it references as attributes and mark them as used. */
21880 static void
21881 prune_unused_types_walk_attribs (dw_die_ref die)
21883 dw_attr_ref a;
21884 unsigned ix;
21886 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
21888 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
21890 /* A reference to another DIE.
21891 Make sure that it will get emitted.
21892 If it was broken out into a comdat group, don't follow it. */
21893 if (! AT_ref (a)->comdat_type_p
21894 || a->dw_attr == DW_AT_specification)
21895 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
21897 /* Set the string's refcount to 0 so that prune_unused_types_mark
21898 accounts properly for it. */
21899 if (AT_class (a) == dw_val_class_str)
21900 a->dw_attr_val.v.val_str->refcount = 0;
21904 /* Mark the generic parameters and arguments children DIEs of DIE. */
21906 static void
21907 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
21909 dw_die_ref c;
21911 if (die == NULL || die->die_child == NULL)
21912 return;
21913 c = die->die_child;
21916 switch (c->die_tag)
21918 case DW_TAG_template_type_param:
21919 case DW_TAG_template_value_param:
21920 case DW_TAG_GNU_template_template_param:
21921 case DW_TAG_GNU_template_parameter_pack:
21922 prune_unused_types_mark (c, 1);
21923 break;
21924 default:
21925 break;
21927 c = c->die_sib;
21928 } while (c && c != die->die_child);
21931 /* Mark DIE as being used. If DOKIDS is true, then walk down
21932 to DIE's children. */
21934 static void
21935 prune_unused_types_mark (dw_die_ref die, int dokids)
21937 dw_die_ref c;
21939 if (die->die_mark == 0)
21941 /* We haven't done this node yet. Mark it as used. */
21942 die->die_mark = 1;
21943 /* If this is the DIE of a generic type instantiation,
21944 mark the children DIEs that describe its generic parms and
21945 args. */
21946 prune_unused_types_mark_generic_parms_dies (die);
21948 /* We also have to mark its parents as used.
21949 (But we don't want to mark our parents' kids due to this.) */
21950 if (die->die_parent)
21951 prune_unused_types_mark (die->die_parent, 0);
21953 /* Mark any referenced nodes. */
21954 prune_unused_types_walk_attribs (die);
21956 /* If this node is a specification,
21957 also mark the definition, if it exists. */
21958 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
21959 prune_unused_types_mark (die->die_definition, 1);
21962 if (dokids && die->die_mark != 2)
21964 /* We need to walk the children, but haven't done so yet.
21965 Remember that we've walked the kids. */
21966 die->die_mark = 2;
21968 /* If this is an array type, we need to make sure our
21969 kids get marked, even if they're types. If we're
21970 breaking out types into comdat sections, do this
21971 for all type definitions. */
21972 if (die->die_tag == DW_TAG_array_type
21973 || (use_debug_types
21974 && is_type_die (die) && ! is_declaration_die (die)))
21975 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
21976 else
21977 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
21981 /* For local classes, look if any static member functions were emitted
21982 and if so, mark them. */
21984 static void
21985 prune_unused_types_walk_local_classes (dw_die_ref die)
21987 dw_die_ref c;
21989 if (die->die_mark == 2)
21990 return;
21992 switch (die->die_tag)
21994 case DW_TAG_structure_type:
21995 case DW_TAG_union_type:
21996 case DW_TAG_class_type:
21997 break;
21999 case DW_TAG_subprogram:
22000 if (!get_AT_flag (die, DW_AT_declaration)
22001 || die->die_definition != NULL)
22002 prune_unused_types_mark (die, 1);
22003 return;
22005 default:
22006 return;
22009 /* Mark children. */
22010 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
22013 /* Walk the tree DIE and mark types that we actually use. */
22015 static void
22016 prune_unused_types_walk (dw_die_ref die)
22018 dw_die_ref c;
22020 /* Don't do anything if this node is already marked and
22021 children have been marked as well. */
22022 if (die->die_mark == 2)
22023 return;
22025 switch (die->die_tag)
22027 case DW_TAG_structure_type:
22028 case DW_TAG_union_type:
22029 case DW_TAG_class_type:
22030 if (die->die_perennial_p)
22031 break;
22033 for (c = die->die_parent; c; c = c->die_parent)
22034 if (c->die_tag == DW_TAG_subprogram)
22035 break;
22037 /* Finding used static member functions inside of classes
22038 is needed just for local classes, because for other classes
22039 static member function DIEs with DW_AT_specification
22040 are emitted outside of the DW_TAG_*_type. If we ever change
22041 it, we'd need to call this even for non-local classes. */
22042 if (c)
22043 prune_unused_types_walk_local_classes (die);
22045 /* It's a type node --- don't mark it. */
22046 return;
22048 case DW_TAG_const_type:
22049 case DW_TAG_packed_type:
22050 case DW_TAG_pointer_type:
22051 case DW_TAG_reference_type:
22052 case DW_TAG_rvalue_reference_type:
22053 case DW_TAG_volatile_type:
22054 case DW_TAG_typedef:
22055 case DW_TAG_array_type:
22056 case DW_TAG_interface_type:
22057 case DW_TAG_friend:
22058 case DW_TAG_variant_part:
22059 case DW_TAG_enumeration_type:
22060 case DW_TAG_subroutine_type:
22061 case DW_TAG_string_type:
22062 case DW_TAG_set_type:
22063 case DW_TAG_subrange_type:
22064 case DW_TAG_ptr_to_member_type:
22065 case DW_TAG_file_type:
22066 if (die->die_perennial_p)
22067 break;
22069 /* It's a type node --- don't mark it. */
22070 return;
22072 default:
22073 /* Mark everything else. */
22074 break;
22077 if (die->die_mark == 0)
22079 die->die_mark = 1;
22081 /* Now, mark any dies referenced from here. */
22082 prune_unused_types_walk_attribs (die);
22085 die->die_mark = 2;
22087 /* Mark children. */
22088 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22091 /* Increment the string counts on strings referred to from DIE's
22092 attributes. */
22094 static void
22095 prune_unused_types_update_strings (dw_die_ref die)
22097 dw_attr_ref a;
22098 unsigned ix;
22100 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22101 if (AT_class (a) == dw_val_class_str)
22103 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22104 s->refcount++;
22105 /* Avoid unnecessarily putting strings that are used less than
22106 twice in the hash table. */
22107 if (s->refcount
22108 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22110 void ** slot;
22111 slot = htab_find_slot_with_hash (debug_str_hash, s->str,
22112 htab_hash_string (s->str),
22113 INSERT);
22114 gcc_assert (*slot == NULL);
22115 *slot = s;
22120 /* Remove from the tree DIE any dies that aren't marked. */
22122 static void
22123 prune_unused_types_prune (dw_die_ref die)
22125 dw_die_ref c;
22126 int pruned = 0;
22128 gcc_assert (die->die_mark);
22129 prune_unused_types_update_strings (die);
22131 if (! die->die_child)
22132 return;
22134 c = die->die_child;
22135 do {
22136 dw_die_ref prev = c;
22137 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
22138 if (c == die->die_child)
22140 /* No marked children between 'prev' and the end of the list. */
22141 if (prev == c)
22142 /* No marked children at all. */
22143 die->die_child = NULL;
22144 else
22146 prev->die_sib = c->die_sib;
22147 die->die_child = prev;
22149 pruned = 1;
22150 goto finished;
22153 if (c != prev->die_sib)
22155 prev->die_sib = c;
22156 pruned = 1;
22158 prune_unused_types_prune (c);
22159 } while (c != die->die_child);
22161 finished:
22162 /* If we pruned children, and this is a class, mark it as a
22163 declaration to inform debuggers that this is not a complete
22164 class definition. */
22165 if (pruned && die->die_mark == 1 && class_scope_p (die)
22166 && ! is_declaration_die (die))
22167 add_AT_flag (die, DW_AT_declaration, 1);
22170 /* Remove dies representing declarations that we never use. */
22172 static void
22173 prune_unused_types (void)
22175 unsigned int i;
22176 limbo_die_node *node;
22177 comdat_type_node *ctnode;
22178 pubname_ref pub;
22179 dw_die_ref base_type;
22181 #if ENABLE_ASSERT_CHECKING
22182 /* All the marks should already be clear. */
22183 verify_marks_clear (comp_unit_die ());
22184 for (node = limbo_die_list; node; node = node->next)
22185 verify_marks_clear (node->die);
22186 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22187 verify_marks_clear (ctnode->root_die);
22188 #endif /* ENABLE_ASSERT_CHECKING */
22190 /* Mark types that are used in global variables. */
22191 premark_types_used_by_global_vars ();
22193 /* Set the mark on nodes that are actually used. */
22194 prune_unused_types_walk (comp_unit_die ());
22195 for (node = limbo_die_list; node; node = node->next)
22196 prune_unused_types_walk (node->die);
22197 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22199 prune_unused_types_walk (ctnode->root_die);
22200 prune_unused_types_mark (ctnode->type_die, 1);
22203 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
22204 are unusual in that they are pubnames that are the children of pubtypes.
22205 They should only be marked via their parent DW_TAG_enumeration_type die,
22206 not as roots in themselves. */
22207 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
22208 if (pub->die->die_tag != DW_TAG_enumerator)
22209 prune_unused_types_mark (pub->die, 1);
22210 for (i = 0; base_types.iterate (i, &base_type); i++)
22211 prune_unused_types_mark (base_type, 1);
22213 if (debug_str_hash)
22214 htab_empty (debug_str_hash);
22215 prune_unused_types_prune (comp_unit_die ());
22216 for (node = limbo_die_list; node; node = node->next)
22217 prune_unused_types_prune (node->die);
22218 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22219 prune_unused_types_prune (ctnode->root_die);
22221 /* Leave the marks clear. */
22222 prune_unmark_dies (comp_unit_die ());
22223 for (node = limbo_die_list; node; node = node->next)
22224 prune_unmark_dies (node->die);
22225 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22226 prune_unmark_dies (ctnode->root_die);
22229 /* Set the parameter to true if there are any relative pathnames in
22230 the file table. */
22231 static int
22232 file_table_relative_p (void ** slot, void *param)
22234 bool *p = (bool *) param;
22235 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
22236 if (!IS_ABSOLUTE_PATH (d->filename))
22238 *p = true;
22239 return 0;
22241 return 1;
22244 /* Routines to manipulate hash table of comdat type units. */
22246 static hashval_t
22247 htab_ct_hash (const void *of)
22249 hashval_t h;
22250 const comdat_type_node *const type_node = (const comdat_type_node *) of;
22252 memcpy (&h, type_node->signature, sizeof (h));
22253 return h;
22256 static int
22257 htab_ct_eq (const void *of1, const void *of2)
22259 const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
22260 const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
22262 return (! memcmp (type_node_1->signature, type_node_2->signature,
22263 DWARF_TYPE_SIGNATURE_SIZE));
22266 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
22267 to the location it would have been added, should we know its
22268 DECL_ASSEMBLER_NAME when we added other attributes. This will
22269 probably improve compactness of debug info, removing equivalent
22270 abbrevs, and hide any differences caused by deferring the
22271 computation of the assembler name, triggered by e.g. PCH. */
22273 static inline void
22274 move_linkage_attr (dw_die_ref die)
22276 unsigned ix = vec_safe_length (die->die_attr);
22277 dw_attr_node linkage = (*die->die_attr)[ix - 1];
22279 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
22280 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
22282 while (--ix > 0)
22284 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
22286 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
22287 break;
22290 if (ix != vec_safe_length (die->die_attr) - 1)
22292 die->die_attr->pop ();
22293 die->die_attr->quick_insert (ix, linkage);
22297 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
22298 referenced from typed stack ops and count how often they are used. */
22300 static void
22301 mark_base_types (dw_loc_descr_ref loc)
22303 dw_die_ref base_type = NULL;
22305 for (; loc; loc = loc->dw_loc_next)
22307 switch (loc->dw_loc_opc)
22309 case DW_OP_GNU_regval_type:
22310 case DW_OP_GNU_deref_type:
22311 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
22312 break;
22313 case DW_OP_GNU_convert:
22314 case DW_OP_GNU_reinterpret:
22315 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
22316 continue;
22317 /* FALLTHRU */
22318 case DW_OP_GNU_const_type:
22319 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
22320 break;
22321 case DW_OP_GNU_entry_value:
22322 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
22323 continue;
22324 default:
22325 continue;
22327 gcc_assert (base_type->die_parent == comp_unit_die ());
22328 if (base_type->die_mark)
22329 base_type->die_mark++;
22330 else
22332 base_types.safe_push (base_type);
22333 base_type->die_mark = 1;
22338 /* Comparison function for sorting marked base types. */
22340 static int
22341 base_type_cmp (const void *x, const void *y)
22343 dw_die_ref dx = *(const dw_die_ref *) x;
22344 dw_die_ref dy = *(const dw_die_ref *) y;
22345 unsigned int byte_size1, byte_size2;
22346 unsigned int encoding1, encoding2;
22347 if (dx->die_mark > dy->die_mark)
22348 return -1;
22349 if (dx->die_mark < dy->die_mark)
22350 return 1;
22351 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
22352 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
22353 if (byte_size1 < byte_size2)
22354 return 1;
22355 if (byte_size1 > byte_size2)
22356 return -1;
22357 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
22358 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
22359 if (encoding1 < encoding2)
22360 return 1;
22361 if (encoding1 > encoding2)
22362 return -1;
22363 return 0;
22366 /* Move base types marked by mark_base_types as early as possible
22367 in the CU, sorted by decreasing usage count both to make the
22368 uleb128 references as small as possible and to make sure they
22369 will have die_offset already computed by calc_die_sizes when
22370 sizes of typed stack loc ops is computed. */
22372 static void
22373 move_marked_base_types (void)
22375 unsigned int i;
22376 dw_die_ref base_type, die, c;
22378 if (base_types.is_empty ())
22379 return;
22381 /* Sort by decreasing usage count, they will be added again in that
22382 order later on. */
22383 base_types.qsort (base_type_cmp);
22384 die = comp_unit_die ();
22385 c = die->die_child;
22388 dw_die_ref prev = c;
22389 c = c->die_sib;
22390 while (c->die_mark)
22392 remove_child_with_prev (c, prev);
22393 /* As base types got marked, there must be at least
22394 one node other than DW_TAG_base_type. */
22395 gcc_assert (c != c->die_sib);
22396 c = c->die_sib;
22399 while (c != die->die_child);
22400 gcc_assert (die->die_child);
22401 c = die->die_child;
22402 for (i = 0; base_types.iterate (i, &base_type); i++)
22404 base_type->die_mark = 0;
22405 base_type->die_sib = c->die_sib;
22406 c->die_sib = base_type;
22407 c = base_type;
22411 /* Helper function for resolve_addr, attempt to resolve
22412 one CONST_STRING, return non-zero if not successful. Similarly verify that
22413 SYMBOL_REFs refer to variables emitted in the current CU. */
22415 static int
22416 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
22418 rtx rtl = *addr;
22420 if (GET_CODE (rtl) == CONST_STRING)
22422 size_t len = strlen (XSTR (rtl, 0)) + 1;
22423 tree t = build_string (len, XSTR (rtl, 0));
22424 tree tlen = size_int (len - 1);
22425 TREE_TYPE (t)
22426 = build_array_type (char_type_node, build_index_type (tlen));
22427 rtl = lookup_constant_def (t);
22428 if (!rtl || !MEM_P (rtl))
22429 return 1;
22430 rtl = XEXP (rtl, 0);
22431 vec_safe_push (used_rtx_array, rtl);
22432 *addr = rtl;
22433 return 0;
22436 if (GET_CODE (rtl) == SYMBOL_REF
22437 && SYMBOL_REF_DECL (rtl))
22439 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
22441 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
22442 return 1;
22444 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22445 return 1;
22448 if (GET_CODE (rtl) == CONST
22449 && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
22450 return 1;
22452 return 0;
22455 /* Helper function for resolve_addr, handle one location
22456 expression, return false if at least one CONST_STRING or SYMBOL_REF in
22457 the location list couldn't be resolved. */
22459 static bool
22460 resolve_addr_in_expr (dw_loc_descr_ref loc)
22462 dw_loc_descr_ref keep = NULL;
22463 for (; loc; loc = loc->dw_loc_next)
22464 switch (loc->dw_loc_opc)
22466 case DW_OP_addr:
22467 if (resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
22468 return false;
22469 break;
22470 case DW_OP_GNU_addr_index:
22471 case DW_OP_GNU_const_index:
22473 if ((loc->dw_loc_opc == DW_OP_GNU_addr_index
22474 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
22475 && resolve_one_addr (&loc->dw_loc_oprnd1.val_entry->addr.rtl,
22476 NULL))
22477 return false;
22479 break;
22480 case DW_OP_const4u:
22481 case DW_OP_const8u:
22482 if (loc->dtprel
22483 && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
22484 return false;
22485 break;
22486 case DW_OP_plus_uconst:
22487 if (size_of_loc_descr (loc)
22488 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
22490 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
22492 dw_loc_descr_ref repl
22493 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
22494 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
22495 add_loc_descr (&repl, loc->dw_loc_next);
22496 *loc = *repl;
22498 break;
22499 case DW_OP_implicit_value:
22500 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
22501 && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL))
22502 return false;
22503 break;
22504 case DW_OP_GNU_implicit_pointer:
22505 case DW_OP_GNU_parameter_ref:
22506 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
22508 dw_die_ref ref
22509 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
22510 if (ref == NULL)
22511 return false;
22512 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
22513 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
22514 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
22516 break;
22517 case DW_OP_GNU_const_type:
22518 case DW_OP_GNU_regval_type:
22519 case DW_OP_GNU_deref_type:
22520 case DW_OP_GNU_convert:
22521 case DW_OP_GNU_reinterpret:
22522 while (loc->dw_loc_next
22523 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
22525 dw_die_ref base1, base2;
22526 unsigned enc1, enc2, size1, size2;
22527 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
22528 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
22529 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
22530 else if (loc->dw_loc_oprnd1.val_class
22531 == dw_val_class_unsigned_const)
22532 break;
22533 else
22534 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
22535 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
22536 == dw_val_class_unsigned_const)
22537 break;
22538 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
22539 gcc_assert (base1->die_tag == DW_TAG_base_type
22540 && base2->die_tag == DW_TAG_base_type);
22541 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
22542 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
22543 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
22544 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
22545 if (size1 == size2
22546 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
22547 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
22548 && loc != keep)
22549 || enc1 == enc2))
22551 /* Optimize away next DW_OP_GNU_convert after
22552 adjusting LOC's base type die reference. */
22553 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
22554 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
22555 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
22556 else
22557 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
22558 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
22559 continue;
22561 /* Don't change integer DW_OP_GNU_convert after e.g. floating
22562 point typed stack entry. */
22563 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
22564 keep = loc->dw_loc_next;
22565 break;
22567 break;
22568 default:
22569 break;
22571 return true;
22574 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
22575 an address in .rodata section if the string literal is emitted there,
22576 or remove the containing location list or replace DW_AT_const_value
22577 with DW_AT_location and empty location expression, if it isn't found
22578 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
22579 to something that has been emitted in the current CU. */
22581 static void
22582 resolve_addr (dw_die_ref die)
22584 dw_die_ref c;
22585 dw_attr_ref a;
22586 dw_loc_list_ref *curr, *start, loc;
22587 unsigned ix;
22589 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22590 switch (AT_class (a))
22592 case dw_val_class_loc_list:
22593 start = curr = AT_loc_list_ptr (a);
22594 loc = *curr;
22595 gcc_assert (loc);
22596 /* The same list can be referenced more than once. See if we have
22597 already recorded the result from a previous pass. */
22598 if (loc->replaced)
22599 *curr = loc->dw_loc_next;
22600 else if (!loc->resolved_addr)
22602 /* As things stand, we do not expect or allow one die to
22603 reference a suffix of another die's location list chain.
22604 References must be identical or completely separate.
22605 There is therefore no need to cache the result of this
22606 pass on any list other than the first; doing so
22607 would lead to unnecessary writes. */
22608 while (*curr)
22610 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
22611 if (!resolve_addr_in_expr ((*curr)->expr))
22613 dw_loc_list_ref next = (*curr)->dw_loc_next;
22614 dw_loc_descr_ref l = (*curr)->expr;
22616 if (next && (*curr)->ll_symbol)
22618 gcc_assert (!next->ll_symbol);
22619 next->ll_symbol = (*curr)->ll_symbol;
22621 if (dwarf_split_debug_info)
22622 remove_loc_list_addr_table_entries (l);
22623 *curr = next;
22625 else
22627 mark_base_types ((*curr)->expr);
22628 curr = &(*curr)->dw_loc_next;
22631 if (loc == *start)
22632 loc->resolved_addr = 1;
22633 else
22635 loc->replaced = 1;
22636 if (dwarf_split_debug_info)
22637 remove_loc_list_addr_table_entries (loc->expr);
22638 loc->dw_loc_next = *start;
22641 if (!*start)
22643 remove_AT (die, a->dw_attr);
22644 ix--;
22646 break;
22647 case dw_val_class_loc:
22649 dw_loc_descr_ref l = AT_loc (a);
22650 /* For -gdwarf-2 don't attempt to optimize
22651 DW_AT_data_member_location containing
22652 DW_OP_plus_uconst - older consumers might
22653 rely on it being that op instead of a more complex,
22654 but shorter, location description. */
22655 if ((dwarf_version > 2
22656 || a->dw_attr != DW_AT_data_member_location
22657 || l == NULL
22658 || l->dw_loc_opc != DW_OP_plus_uconst
22659 || l->dw_loc_next != NULL)
22660 && !resolve_addr_in_expr (l))
22662 if (dwarf_split_debug_info)
22663 remove_loc_list_addr_table_entries (l);
22664 remove_AT (die, a->dw_attr);
22665 ix--;
22667 else
22668 mark_base_types (l);
22670 break;
22671 case dw_val_class_addr:
22672 if (a->dw_attr == DW_AT_const_value
22673 && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
22675 if (AT_index (a) != NOT_INDEXED)
22676 remove_addr_table_entry (a->dw_attr_val.val_entry);
22677 remove_AT (die, a->dw_attr);
22678 ix--;
22680 if (die->die_tag == DW_TAG_GNU_call_site
22681 && a->dw_attr == DW_AT_abstract_origin)
22683 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
22684 dw_die_ref tdie = lookup_decl_die (tdecl);
22685 if (tdie == NULL
22686 && DECL_EXTERNAL (tdecl)
22687 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
22689 force_decl_die (tdecl);
22690 tdie = lookup_decl_die (tdecl);
22692 if (tdie)
22694 a->dw_attr_val.val_class = dw_val_class_die_ref;
22695 a->dw_attr_val.v.val_die_ref.die = tdie;
22696 a->dw_attr_val.v.val_die_ref.external = 0;
22698 else
22700 if (AT_index (a) != NOT_INDEXED)
22701 remove_addr_table_entry (a->dw_attr_val.val_entry);
22702 remove_AT (die, a->dw_attr);
22703 ix--;
22706 break;
22707 default:
22708 break;
22711 FOR_EACH_CHILD (die, c, resolve_addr (c));
22714 /* Helper routines for optimize_location_lists.
22715 This pass tries to share identical local lists in .debug_loc
22716 section. */
22718 /* Iteratively hash operands of LOC opcode. */
22720 static inline hashval_t
22721 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
22723 dw_val_ref val1 = &loc->dw_loc_oprnd1;
22724 dw_val_ref val2 = &loc->dw_loc_oprnd2;
22726 switch (loc->dw_loc_opc)
22728 case DW_OP_const4u:
22729 case DW_OP_const8u:
22730 if (loc->dtprel)
22731 goto hash_addr;
22732 /* FALLTHRU */
22733 case DW_OP_const1u:
22734 case DW_OP_const1s:
22735 case DW_OP_const2u:
22736 case DW_OP_const2s:
22737 case DW_OP_const4s:
22738 case DW_OP_const8s:
22739 case DW_OP_constu:
22740 case DW_OP_consts:
22741 case DW_OP_pick:
22742 case DW_OP_plus_uconst:
22743 case DW_OP_breg0:
22744 case DW_OP_breg1:
22745 case DW_OP_breg2:
22746 case DW_OP_breg3:
22747 case DW_OP_breg4:
22748 case DW_OP_breg5:
22749 case DW_OP_breg6:
22750 case DW_OP_breg7:
22751 case DW_OP_breg8:
22752 case DW_OP_breg9:
22753 case DW_OP_breg10:
22754 case DW_OP_breg11:
22755 case DW_OP_breg12:
22756 case DW_OP_breg13:
22757 case DW_OP_breg14:
22758 case DW_OP_breg15:
22759 case DW_OP_breg16:
22760 case DW_OP_breg17:
22761 case DW_OP_breg18:
22762 case DW_OP_breg19:
22763 case DW_OP_breg20:
22764 case DW_OP_breg21:
22765 case DW_OP_breg22:
22766 case DW_OP_breg23:
22767 case DW_OP_breg24:
22768 case DW_OP_breg25:
22769 case DW_OP_breg26:
22770 case DW_OP_breg27:
22771 case DW_OP_breg28:
22772 case DW_OP_breg29:
22773 case DW_OP_breg30:
22774 case DW_OP_breg31:
22775 case DW_OP_regx:
22776 case DW_OP_fbreg:
22777 case DW_OP_piece:
22778 case DW_OP_deref_size:
22779 case DW_OP_xderef_size:
22780 hash = iterative_hash_object (val1->v.val_int, hash);
22781 break;
22782 case DW_OP_skip:
22783 case DW_OP_bra:
22785 int offset;
22787 gcc_assert (val1->val_class == dw_val_class_loc);
22788 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
22789 hash = iterative_hash_object (offset, hash);
22791 break;
22792 case DW_OP_implicit_value:
22793 hash = iterative_hash_object (val1->v.val_unsigned, hash);
22794 switch (val2->val_class)
22796 case dw_val_class_const:
22797 hash = iterative_hash_object (val2->v.val_int, hash);
22798 break;
22799 case dw_val_class_vec:
22801 unsigned int elt_size = val2->v.val_vec.elt_size;
22802 unsigned int len = val2->v.val_vec.length;
22804 hash = iterative_hash_object (elt_size, hash);
22805 hash = iterative_hash_object (len, hash);
22806 hash = iterative_hash (val2->v.val_vec.array,
22807 len * elt_size, hash);
22809 break;
22810 case dw_val_class_const_double:
22811 hash = iterative_hash_object (val2->v.val_double.low, hash);
22812 hash = iterative_hash_object (val2->v.val_double.high, hash);
22813 break;
22814 case dw_val_class_addr:
22815 hash = iterative_hash_rtx (val2->v.val_addr, hash);
22816 break;
22817 default:
22818 gcc_unreachable ();
22820 break;
22821 case DW_OP_bregx:
22822 case DW_OP_bit_piece:
22823 hash = iterative_hash_object (val1->v.val_int, hash);
22824 hash = iterative_hash_object (val2->v.val_int, hash);
22825 break;
22826 case DW_OP_addr:
22827 hash_addr:
22828 if (loc->dtprel)
22830 unsigned char dtprel = 0xd1;
22831 hash = iterative_hash_object (dtprel, hash);
22833 hash = iterative_hash_rtx (val1->v.val_addr, hash);
22834 break;
22835 case DW_OP_GNU_addr_index:
22836 case DW_OP_GNU_const_index:
22838 if (loc->dtprel)
22840 unsigned char dtprel = 0xd1;
22841 hash = iterative_hash_object (dtprel, hash);
22843 hash = iterative_hash_rtx (val1->val_entry->addr.rtl, hash);
22845 break;
22846 case DW_OP_GNU_implicit_pointer:
22847 hash = iterative_hash_object (val2->v.val_int, hash);
22848 break;
22849 case DW_OP_GNU_entry_value:
22850 hash = hash_loc_operands (val1->v.val_loc, hash);
22851 break;
22852 case DW_OP_GNU_regval_type:
22853 case DW_OP_GNU_deref_type:
22855 unsigned int byte_size
22856 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
22857 unsigned int encoding
22858 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
22859 hash = iterative_hash_object (val1->v.val_int, hash);
22860 hash = iterative_hash_object (byte_size, hash);
22861 hash = iterative_hash_object (encoding, hash);
22863 break;
22864 case DW_OP_GNU_convert:
22865 case DW_OP_GNU_reinterpret:
22866 if (val1->val_class == dw_val_class_unsigned_const)
22868 hash = iterative_hash_object (val1->v.val_unsigned, hash);
22869 break;
22871 /* FALLTHRU */
22872 case DW_OP_GNU_const_type:
22874 unsigned int byte_size
22875 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
22876 unsigned int encoding
22877 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
22878 hash = iterative_hash_object (byte_size, hash);
22879 hash = iterative_hash_object (encoding, hash);
22880 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
22881 break;
22882 hash = iterative_hash_object (val2->val_class, hash);
22883 switch (val2->val_class)
22885 case dw_val_class_const:
22886 hash = iterative_hash_object (val2->v.val_int, hash);
22887 break;
22888 case dw_val_class_vec:
22890 unsigned int elt_size = val2->v.val_vec.elt_size;
22891 unsigned int len = val2->v.val_vec.length;
22893 hash = iterative_hash_object (elt_size, hash);
22894 hash = iterative_hash_object (len, hash);
22895 hash = iterative_hash (val2->v.val_vec.array,
22896 len * elt_size, hash);
22898 break;
22899 case dw_val_class_const_double:
22900 hash = iterative_hash_object (val2->v.val_double.low, hash);
22901 hash = iterative_hash_object (val2->v.val_double.high, hash);
22902 break;
22903 default:
22904 gcc_unreachable ();
22907 break;
22909 default:
22910 /* Other codes have no operands. */
22911 break;
22913 return hash;
22916 /* Iteratively hash the whole DWARF location expression LOC. */
22918 static inline hashval_t
22919 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
22921 dw_loc_descr_ref l;
22922 bool sizes_computed = false;
22923 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
22924 size_of_locs (loc);
22926 for (l = loc; l != NULL; l = l->dw_loc_next)
22928 enum dwarf_location_atom opc = l->dw_loc_opc;
22929 hash = iterative_hash_object (opc, hash);
22930 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
22932 size_of_locs (loc);
22933 sizes_computed = true;
22935 hash = hash_loc_operands (l, hash);
22937 return hash;
22940 /* Compute hash of the whole location list LIST_HEAD. */
22942 static inline void
22943 hash_loc_list (dw_loc_list_ref list_head)
22945 dw_loc_list_ref curr = list_head;
22946 hashval_t hash = 0;
22948 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
22950 hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
22951 hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
22952 if (curr->section)
22953 hash = iterative_hash (curr->section, strlen (curr->section) + 1,
22954 hash);
22955 hash = hash_locs (curr->expr, hash);
22957 list_head->hash = hash;
22960 /* Return true if X and Y opcodes have the same operands. */
22962 static inline bool
22963 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
22965 dw_val_ref valx1 = &x->dw_loc_oprnd1;
22966 dw_val_ref valx2 = &x->dw_loc_oprnd2;
22967 dw_val_ref valy1 = &y->dw_loc_oprnd1;
22968 dw_val_ref valy2 = &y->dw_loc_oprnd2;
22970 switch (x->dw_loc_opc)
22972 case DW_OP_const4u:
22973 case DW_OP_const8u:
22974 if (x->dtprel)
22975 goto hash_addr;
22976 /* FALLTHRU */
22977 case DW_OP_const1u:
22978 case DW_OP_const1s:
22979 case DW_OP_const2u:
22980 case DW_OP_const2s:
22981 case DW_OP_const4s:
22982 case DW_OP_const8s:
22983 case DW_OP_constu:
22984 case DW_OP_consts:
22985 case DW_OP_pick:
22986 case DW_OP_plus_uconst:
22987 case DW_OP_breg0:
22988 case DW_OP_breg1:
22989 case DW_OP_breg2:
22990 case DW_OP_breg3:
22991 case DW_OP_breg4:
22992 case DW_OP_breg5:
22993 case DW_OP_breg6:
22994 case DW_OP_breg7:
22995 case DW_OP_breg8:
22996 case DW_OP_breg9:
22997 case DW_OP_breg10:
22998 case DW_OP_breg11:
22999 case DW_OP_breg12:
23000 case DW_OP_breg13:
23001 case DW_OP_breg14:
23002 case DW_OP_breg15:
23003 case DW_OP_breg16:
23004 case DW_OP_breg17:
23005 case DW_OP_breg18:
23006 case DW_OP_breg19:
23007 case DW_OP_breg20:
23008 case DW_OP_breg21:
23009 case DW_OP_breg22:
23010 case DW_OP_breg23:
23011 case DW_OP_breg24:
23012 case DW_OP_breg25:
23013 case DW_OP_breg26:
23014 case DW_OP_breg27:
23015 case DW_OP_breg28:
23016 case DW_OP_breg29:
23017 case DW_OP_breg30:
23018 case DW_OP_breg31:
23019 case DW_OP_regx:
23020 case DW_OP_fbreg:
23021 case DW_OP_piece:
23022 case DW_OP_deref_size:
23023 case DW_OP_xderef_size:
23024 return valx1->v.val_int == valy1->v.val_int;
23025 case DW_OP_skip:
23026 case DW_OP_bra:
23027 /* If splitting debug info, the use of DW_OP_GNU_addr_index
23028 can cause irrelevant differences in dw_loc_addr. */
23029 gcc_assert (valx1->val_class == dw_val_class_loc
23030 && valy1->val_class == dw_val_class_loc
23031 && (dwarf_split_debug_info
23032 || x->dw_loc_addr == y->dw_loc_addr));
23033 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
23034 case DW_OP_implicit_value:
23035 if (valx1->v.val_unsigned != valy1->v.val_unsigned
23036 || valx2->val_class != valy2->val_class)
23037 return false;
23038 switch (valx2->val_class)
23040 case dw_val_class_const:
23041 return valx2->v.val_int == valy2->v.val_int;
23042 case dw_val_class_vec:
23043 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23044 && valx2->v.val_vec.length == valy2->v.val_vec.length
23045 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23046 valx2->v.val_vec.elt_size
23047 * valx2->v.val_vec.length) == 0;
23048 case dw_val_class_const_double:
23049 return valx2->v.val_double.low == valy2->v.val_double.low
23050 && valx2->v.val_double.high == valy2->v.val_double.high;
23051 case dw_val_class_addr:
23052 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
23053 default:
23054 gcc_unreachable ();
23056 case DW_OP_bregx:
23057 case DW_OP_bit_piece:
23058 return valx1->v.val_int == valy1->v.val_int
23059 && valx2->v.val_int == valy2->v.val_int;
23060 case DW_OP_addr:
23061 hash_addr:
23062 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
23063 case DW_OP_GNU_addr_index:
23064 case DW_OP_GNU_const_index:
23066 rtx ax1 = valx1->val_entry->addr.rtl;
23067 rtx ay1 = valy1->val_entry->addr.rtl;
23068 return rtx_equal_p (ax1, ay1);
23070 case DW_OP_GNU_implicit_pointer:
23071 return valx1->val_class == dw_val_class_die_ref
23072 && valx1->val_class == valy1->val_class
23073 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
23074 && valx2->v.val_int == valy2->v.val_int;
23075 case DW_OP_GNU_entry_value:
23076 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
23077 case DW_OP_GNU_const_type:
23078 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
23079 || valx2->val_class != valy2->val_class)
23080 return false;
23081 switch (valx2->val_class)
23083 case dw_val_class_const:
23084 return valx2->v.val_int == valy2->v.val_int;
23085 case dw_val_class_vec:
23086 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23087 && valx2->v.val_vec.length == valy2->v.val_vec.length
23088 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23089 valx2->v.val_vec.elt_size
23090 * valx2->v.val_vec.length) == 0;
23091 case dw_val_class_const_double:
23092 return valx2->v.val_double.low == valy2->v.val_double.low
23093 && valx2->v.val_double.high == valy2->v.val_double.high;
23094 default:
23095 gcc_unreachable ();
23097 case DW_OP_GNU_regval_type:
23098 case DW_OP_GNU_deref_type:
23099 return valx1->v.val_int == valy1->v.val_int
23100 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
23101 case DW_OP_GNU_convert:
23102 case DW_OP_GNU_reinterpret:
23103 if (valx1->val_class != valy1->val_class)
23104 return false;
23105 if (valx1->val_class == dw_val_class_unsigned_const)
23106 return valx1->v.val_unsigned == valy1->v.val_unsigned;
23107 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
23108 case DW_OP_GNU_parameter_ref:
23109 return valx1->val_class == dw_val_class_die_ref
23110 && valx1->val_class == valy1->val_class
23111 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
23112 default:
23113 /* Other codes have no operands. */
23114 return true;
23118 /* Return true if DWARF location expressions X and Y are the same. */
23120 static inline bool
23121 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
23123 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
23124 if (x->dw_loc_opc != y->dw_loc_opc
23125 || x->dtprel != y->dtprel
23126 || !compare_loc_operands (x, y))
23127 break;
23128 return x == NULL && y == NULL;
23131 /* Return precomputed hash of location list X. */
23133 static hashval_t
23134 loc_list_hash (const void *x)
23136 return ((const struct dw_loc_list_struct *) x)->hash;
23139 /* Return 1 if location lists X and Y are the same. */
23141 static int
23142 loc_list_eq (const void *x, const void *y)
23144 const struct dw_loc_list_struct *a = (const struct dw_loc_list_struct *) x;
23145 const struct dw_loc_list_struct *b = (const struct dw_loc_list_struct *) y;
23146 if (a == b)
23147 return 1;
23148 if (a->hash != b->hash)
23149 return 0;
23150 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
23151 if (strcmp (a->begin, b->begin) != 0
23152 || strcmp (a->end, b->end) != 0
23153 || (a->section == NULL) != (b->section == NULL)
23154 || (a->section && strcmp (a->section, b->section) != 0)
23155 || !compare_locs (a->expr, b->expr))
23156 break;
23157 return a == NULL && b == NULL;
23160 /* Recursively optimize location lists referenced from DIE
23161 children and share them whenever possible. */
23163 static void
23164 optimize_location_lists_1 (dw_die_ref die, htab_t htab)
23166 dw_die_ref c;
23167 dw_attr_ref a;
23168 unsigned ix;
23169 void **slot;
23171 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23172 if (AT_class (a) == dw_val_class_loc_list)
23174 dw_loc_list_ref list = AT_loc_list (a);
23175 /* TODO: perform some optimizations here, before hashing
23176 it and storing into the hash table. */
23177 hash_loc_list (list);
23178 slot = htab_find_slot_with_hash (htab, list, list->hash,
23179 INSERT);
23180 if (*slot == NULL)
23181 *slot = (void *) list;
23182 else
23183 a->dw_attr_val.v.val_loc_list = (dw_loc_list_ref) *slot;
23186 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
23190 /* Recursively assign each location list a unique index into the debug_addr
23191 section. */
23193 static void
23194 index_location_lists (dw_die_ref die)
23196 dw_die_ref c;
23197 dw_attr_ref a;
23198 unsigned ix;
23200 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23201 if (AT_class (a) == dw_val_class_loc_list)
23203 dw_loc_list_ref list = AT_loc_list (a);
23204 dw_loc_list_ref curr;
23205 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
23207 /* Don't index an entry that has already been indexed
23208 or won't be output. */
23209 if (curr->begin_entry != NULL
23210 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
23211 continue;
23213 curr->begin_entry
23214 = add_addr_table_entry (xstrdup (curr->begin),
23215 ate_kind_label);
23219 FOR_EACH_CHILD (die, c, index_location_lists (c));
23222 /* Optimize location lists referenced from DIE
23223 children and share them whenever possible. */
23225 static void
23226 optimize_location_lists (dw_die_ref die)
23228 htab_t htab = htab_create (500, loc_list_hash, loc_list_eq, NULL);
23229 optimize_location_lists_1 (die, htab);
23230 htab_delete (htab);
23233 /* Output stuff that dwarf requires at the end of every file,
23234 and generate the DWARF-2 debugging info. */
23236 static void
23237 dwarf2out_finish (const char *filename)
23239 limbo_die_node *node, *next_node;
23240 comdat_type_node *ctnode;
23241 htab_t comdat_type_table;
23242 unsigned int i;
23243 dw_die_ref main_comp_unit_die;
23245 /* PCH might result in DW_AT_producer string being restored from the
23246 header compilation, so always fill it with empty string initially
23247 and overwrite only here. */
23248 dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
23249 producer_string = gen_producer_string ();
23250 producer->dw_attr_val.v.val_str->refcount--;
23251 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
23253 gen_scheduled_generic_parms_dies ();
23254 gen_remaining_tmpl_value_param_die_attribute ();
23256 /* Add the name for the main input file now. We delayed this from
23257 dwarf2out_init to avoid complications with PCH. */
23258 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
23259 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
23260 add_comp_dir_attribute (comp_unit_die ());
23261 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
23263 bool p = false;
23264 htab_traverse (file_table, file_table_relative_p, &p);
23265 if (p)
23266 add_comp_dir_attribute (comp_unit_die ());
23269 if (deferred_locations_list)
23270 for (i = 0; i < deferred_locations_list->length (); i++)
23272 add_location_or_const_value_attribute (
23273 (*deferred_locations_list)[i].die,
23274 (*deferred_locations_list)[i].variable,
23275 false,
23276 DW_AT_location);
23279 /* Traverse the limbo die list, and add parent/child links. The only
23280 dies without parents that should be here are concrete instances of
23281 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
23282 For concrete instances, we can get the parent die from the abstract
23283 instance. */
23284 for (node = limbo_die_list; node; node = next_node)
23286 dw_die_ref die = node->die;
23287 next_node = node->next;
23289 if (die->die_parent == NULL)
23291 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
23293 if (origin && origin->die_parent)
23294 add_child_die (origin->die_parent, die);
23295 else if (is_cu_die (die))
23297 else if (seen_error ())
23298 /* It's OK to be confused by errors in the input. */
23299 add_child_die (comp_unit_die (), die);
23300 else
23302 /* In certain situations, the lexical block containing a
23303 nested function can be optimized away, which results
23304 in the nested function die being orphaned. Likewise
23305 with the return type of that nested function. Force
23306 this to be a child of the containing function.
23308 It may happen that even the containing function got fully
23309 inlined and optimized out. In that case we are lost and
23310 assign the empty child. This should not be big issue as
23311 the function is likely unreachable too. */
23312 gcc_assert (node->created_for);
23314 if (DECL_P (node->created_for))
23315 origin = get_context_die (DECL_CONTEXT (node->created_for));
23316 else if (TYPE_P (node->created_for))
23317 origin = scope_die_for (node->created_for, comp_unit_die ());
23318 else
23319 origin = comp_unit_die ();
23321 add_child_die (origin, die);
23326 limbo_die_list = NULL;
23328 #if ENABLE_ASSERT_CHECKING
23330 dw_die_ref die = comp_unit_die (), c;
23331 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
23333 #endif
23334 resolve_addr (comp_unit_die ());
23335 move_marked_base_types ();
23337 for (node = deferred_asm_name; node; node = node->next)
23339 tree decl = node->created_for;
23340 /* When generating LTO bytecode we can not generate new assembler
23341 names at this point and all important decls got theirs via
23342 free-lang-data. */
23343 if ((!flag_generate_lto || DECL_ASSEMBLER_NAME_SET_P (decl))
23344 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
23346 add_linkage_attr (node->die, decl);
23347 move_linkage_attr (node->die);
23351 deferred_asm_name = NULL;
23353 /* Walk through the list of incomplete types again, trying once more to
23354 emit full debugging info for them. */
23355 retry_incomplete_types ();
23357 if (flag_eliminate_unused_debug_types)
23358 prune_unused_types ();
23360 /* Generate separate COMDAT sections for type DIEs. */
23361 if (use_debug_types)
23363 break_out_comdat_types (comp_unit_die ());
23365 /* Each new type_unit DIE was added to the limbo die list when created.
23366 Since these have all been added to comdat_type_list, clear the
23367 limbo die list. */
23368 limbo_die_list = NULL;
23370 /* For each new comdat type unit, copy declarations for incomplete
23371 types to make the new unit self-contained (i.e., no direct
23372 references to the main compile unit). */
23373 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23374 copy_decls_for_unworthy_types (ctnode->root_die);
23375 copy_decls_for_unworthy_types (comp_unit_die ());
23377 /* In the process of copying declarations from one unit to another,
23378 we may have left some declarations behind that are no longer
23379 referenced. Prune them. */
23380 prune_unused_types ();
23383 /* Generate separate CUs for each of the include files we've seen.
23384 They will go into limbo_die_list. */
23385 if (flag_eliminate_dwarf2_dups)
23386 break_out_includes (comp_unit_die ());
23388 /* Traverse the DIE's and add add sibling attributes to those DIE's
23389 that have children. */
23390 add_sibling_attributes (comp_unit_die ());
23391 for (node = limbo_die_list; node; node = node->next)
23392 add_sibling_attributes (node->die);
23393 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23394 add_sibling_attributes (ctnode->root_die);
23396 /* When splitting DWARF info, we put some attributes in the
23397 skeleton compile_unit DIE that remains in the .o, while
23398 most attributes go in the DWO compile_unit_die. */
23399 if (dwarf_split_debug_info)
23400 main_comp_unit_die = gen_compile_unit_die (NULL);
23401 else
23402 main_comp_unit_die = comp_unit_die ();
23404 /* Output a terminator label for the .text section. */
23405 switch_to_section (text_section);
23406 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
23407 if (cold_text_section)
23409 switch_to_section (cold_text_section);
23410 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
23413 /* We can only use the low/high_pc attributes if all of the code was
23414 in .text. */
23415 if (!have_multiple_function_sections
23416 || (dwarf_version < 3 && dwarf_strict))
23418 /* Don't add if the CU has no associated code. */
23419 if (text_section_used)
23420 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
23421 text_end_label, true);
23423 else
23425 unsigned fde_idx;
23426 dw_fde_ref fde;
23427 bool range_list_added = false;
23429 if (text_section_used)
23430 add_ranges_by_labels (main_comp_unit_die, text_section_label,
23431 text_end_label, &range_list_added, true);
23432 if (cold_text_section_used)
23433 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
23434 cold_end_label, &range_list_added, true);
23436 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
23438 if (DECL_IGNORED_P (fde->decl))
23439 continue;
23440 if (!fde->in_std_section)
23441 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
23442 fde->dw_fde_end, &range_list_added,
23443 true);
23444 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
23445 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
23446 fde->dw_fde_second_end, &range_list_added,
23447 true);
23450 if (range_list_added)
23452 /* We need to give .debug_loc and .debug_ranges an appropriate
23453 "base address". Use zero so that these addresses become
23454 absolute. Historically, we've emitted the unexpected
23455 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
23456 Emit both to give time for other tools to adapt. */
23457 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
23458 if (! dwarf_strict && dwarf_version < 4)
23459 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
23461 add_ranges (NULL);
23465 if (debug_info_level >= DINFO_LEVEL_NORMAL)
23466 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
23467 debug_line_section_label);
23469 if (have_macinfo)
23470 add_AT_macptr (comp_unit_die (),
23471 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
23472 macinfo_section_label);
23474 if (dwarf_split_debug_info && addr_index_table != NULL)
23476 /* optimize_location_lists calculates the size of the lists,
23477 so index them first, and assign indices to the entries.
23478 Although optimize_location_lists will remove entries from
23479 the table, it only does so for duplicates, and therefore
23480 only reduces ref_counts to 1. */
23481 unsigned int index = 0;
23482 index_location_lists (comp_unit_die ());
23483 htab_traverse_noresize (addr_index_table,
23484 index_addr_table_entry, &index);
23486 if (have_location_lists)
23487 optimize_location_lists (comp_unit_die ());
23489 save_macinfo_strings ();
23490 if (dwarf_split_debug_info)
23492 unsigned int index = 0;
23493 htab_traverse_noresize (debug_str_hash, index_string, &index);
23496 /* Output all of the compilation units. We put the main one last so that
23497 the offsets are available to output_pubnames. */
23498 for (node = limbo_die_list; node; node = node->next)
23499 output_comp_unit (node->die, 0);
23501 comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
23502 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23504 void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
23506 /* Don't output duplicate types. */
23507 if (*slot != HTAB_EMPTY_ENTRY)
23508 continue;
23510 /* Add a pointer to the line table for the main compilation unit
23511 so that the debugger can make sense of DW_AT_decl_file
23512 attributes. */
23513 if (debug_info_level >= DINFO_LEVEL_NORMAL)
23514 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
23515 (!dwarf_split_debug_info
23516 ? debug_line_section_label
23517 : debug_skeleton_line_section_label));
23519 output_comdat_type_unit (ctnode);
23520 *slot = ctnode;
23522 htab_delete (comdat_type_table);
23524 /* The AT_pubnames attribute needs to go in all skeleton dies, including
23525 both the main_cu and all skeleton TUs. Making this call unconditional
23526 would end up either adding a second copy of the AT_pubnames attribute, or
23527 requiring a special case in add_top_level_skeleton_die_attrs. */
23528 if (!dwarf_split_debug_info)
23529 add_AT_pubnames (comp_unit_die ());
23531 if (dwarf_split_debug_info)
23533 int mark;
23534 unsigned char checksum[16];
23535 struct md5_ctx ctx;
23537 /* Compute a checksum of the comp_unit to use as the dwo_id. */
23538 md5_init_ctx (&ctx);
23539 mark = 0;
23540 die_checksum (comp_unit_die (), &ctx, &mark);
23541 unmark_all_dies (comp_unit_die ());
23542 md5_finish_ctx (&ctx, checksum);
23544 /* Use the first 8 bytes of the checksum as the dwo_id,
23545 and add it to both comp-unit DIEs. */
23546 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
23547 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
23549 /* Add the base offset of the ranges table to the skeleton
23550 comp-unit DIE. */
23551 if (ranges_table_in_use)
23552 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
23553 ranges_section_label);
23555 switch_to_section (debug_addr_section);
23556 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
23557 output_addr_table ();
23560 /* Output the main compilation unit if non-empty or if .debug_macinfo
23561 or .debug_macro will be emitted. */
23562 output_comp_unit (comp_unit_die (), have_macinfo);
23564 if (dwarf_split_debug_info && info_section_emitted)
23565 output_skeleton_debug_sections (main_comp_unit_die);
23567 /* Output the abbreviation table. */
23568 if (abbrev_die_table_in_use != 1)
23570 switch_to_section (debug_abbrev_section);
23571 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
23572 output_abbrev_section ();
23575 /* Output location list section if necessary. */
23576 if (have_location_lists)
23578 /* Output the location lists info. */
23579 switch_to_section (debug_loc_section);
23580 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
23581 output_location_lists (comp_unit_die ());
23584 /* Output public names and types tables if necessary. */
23585 output_pubnames (pubname_table);
23586 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
23587 It shouldn't hurt to emit it always, since pure DWARF2 consumers
23588 simply won't look for the section. */
23589 output_pubnames (pubtype_table);
23591 /* Output the address range information if a CU (.debug_info section)
23592 was emitted. We output an empty table even if we had no functions
23593 to put in it. This because the consumer has no way to tell the
23594 difference between an empty table that we omitted and failure to
23595 generate a table that would have contained data. */
23596 if (info_section_emitted)
23598 unsigned long aranges_length = size_of_aranges ();
23600 switch_to_section (debug_aranges_section);
23601 output_aranges (aranges_length);
23604 /* Output ranges section if necessary. */
23605 if (ranges_table_in_use)
23607 switch_to_section (debug_ranges_section);
23608 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
23609 output_ranges ();
23612 /* Have to end the macro section. */
23613 if (have_macinfo)
23615 switch_to_section (debug_macinfo_section);
23616 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
23617 output_macinfo ();
23618 dw2_asm_output_data (1, 0, "End compilation unit");
23621 /* Output the source line correspondence table. We must do this
23622 even if there is no line information. Otherwise, on an empty
23623 translation unit, we will generate a present, but empty,
23624 .debug_info section. IRIX 6.5 `nm' will then complain when
23625 examining the file. This is done late so that any filenames
23626 used by the debug_info section are marked as 'used'. */
23627 switch_to_section (debug_line_section);
23628 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
23629 if (! DWARF2_ASM_LINE_DEBUG_INFO)
23630 output_line_info (false);
23632 if (dwarf_split_debug_info && info_section_emitted)
23634 switch_to_section (debug_skeleton_line_section);
23635 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
23636 output_line_info (true);
23639 /* If we emitted any indirect strings, output the string table too. */
23640 if (debug_str_hash)
23641 output_indirect_strings ();
23644 #include "gt-dwarf2out.h"